best way for destroy mutex and cond (resource free)

Hello everyone, please see the pseudo-code below:

static void *func1(void *arg) {
    while(1){


    }
}


static void *func2(void *arg) {
    while(1){


    }
}


int main()
{
    pthread_t t1, t2;
    pthread_create(&t1, NULL, func1, NULL);
    pthread_create(&t2, NULL, func2, NULL);


    pthread_join(t1, NULL);
    pthread_join(t2, NULL);


    return 0;
}

If the two threads above use a mutex (lock and unlock) and a condition variable, and I close the process with Ctrl+C (SIGINT), then if I write a simple signal handler to destroy the mutex and cond, it shows UB (undefined behavior). Now, how can I write a safe signal handler? How can I unlock and destroy safely?

reddit.com
u/Yousef_Tele — 4 hours ago

best way for destroy mutex and cond (resource free)

Hello everyone, please see the pseudo-code below:

static void *func1(void *arg) {
    while(1){


    }
}


static void *func2(void *arg) {
    while(1){


    }
}


int main()
{
    pthread_t t1, t2;
    pthread_create(&t1, NULL, func1, NULL);
    pthread_create(&t2, NULL, func2, NULL);


    pthread_join(t1, NULL);
    pthread_join(t2, NULL);


    return 0;
}

If the two threads above use a mutex (lock and unlock) and a condition variable, and I close the process with Ctrl+C (SIGINT), then if I write a simple signal handler to destroy the mutex and cond, it shows UB (undefined behavior). Now, how can I write a safe signal handler? How can I unlock and destroy safely?

reddit.com
u/Yousef_Tele — 4 hours ago
▲ 10 r/kernel

Simple firewall, please check it and give it to me feedback

Hello everyone, I created a simple firewall used by netfilter hooks and netlink sockets to communicate between the kernel and the user space. Please, can anyone check it and give me feedback on this project, and which part I can write better or which part write mistake. Repo

reddit.com
u/Yousef_Tele — 11 days ago
▲ 0 r/foss

Simple firewall, please check it and give it to me feedback

Hello everyone, I created a simple firewall used by netfilter hooks and netlink sockets to communicate between the kernel and the user space. Please, can anyone check it and give me feedback on this project, and which part I can write better or which part write mistake. Repo

reddit.com
u/Yousef_Tele — 11 days ago

Simple firewall, please check it and give it to me feedback

Hello everyone, I created a simple firewall used by netfilter hooks and netlink sockets to communicate between the kernel and the user space. Please, can anyone check it and give me feedback on this project, and which part I can write better or which part write mistake. Repo

reddit.com
u/Yousef_Tele — 11 days ago

How can I transfer a structure to kernel module then store it?

Hey everyone, I created a lightweight firewall with C, created a kernel module with the netfilter API, and a pre-routing hook. Now I want to send rules via netlink socket. My idea is to create a structure, then send it. But I cannot find the best way to store all rules in the kernel, then use them in a hook. Sometimes I think I can compress the rules into bits, then send them. If anyone has experience with my problem, please help me understand how I can implement a optimize protocol and store it in the kernel module

reddit.com
u/Yousef_Tele — 28 days ago
▲ 9 r/kernel

How can I transfer a structure to kernel module then store it?

Hey everyone, I created a lightweight firewall with C, created a kernel module with the netfilter API, and a pre-routing hook. Now I want to send rules via netlink socket. My idea is to create a structure, then send it. But I cannot find the best way to store all rules in the kernel, then use them in a hook. Sometimes I think I can compress the rules into bits, then send them. If anyone has experience with my problem, please help me understand how I can implement a optimize protocol and store it in the kernel module

reddit.com
u/Yousef_Tele — 28 days ago

How can I transfer a structure to kernel module then store it?

Hey everyone, I created a lightweight firewall with C, created a kernel module with the netfilter API, and a pre-routing hook. Now I want to send rules via netlink socket. My idea is to create a structure, then send it. But I cannot find the best way to store all rules in the kernel, then use them in a hook. Sometimes I think I can compress the rules into bits, then send them. If anyone has experience with my problem, please help me understand how I can implement a optimize protocol and store it in the kernel module

reddit.com
u/Yousef_Tele — 28 days ago

DMA doesn't work correctly on STM32F103. What is the problem?

Hey, I configured DMA channel 3 in circular mode for USART3-RX on the STM32F103 Blue Pill, then I enabled IDLEIE to check the idle line in USART3 and store the transferred data from USART to the local main buffer via DMA. But for testing, I use a USB-to-serial converter and send one character, but CNDTR does not jump to the next number; each character jumps 116 numbers. Why?

I attached my code. I think my main bug AHB bus 72MHz, but usart2 and usart3 use APB1 with 36MHz, and this clock mismatched not true

/*
uint8_t buffer[256];
uint32_t buffer_size = 256;
*/
void dma_init( uint8_t *buffer, const uint32_t buffer_size)
{
    RCC->AHBENR |= RCC_AHBENR_DMA1EN;


    DMA1_Channel3->CCR = 0x00U; /* Clear control register */


    DMA1_Channel3->CPAR  = ( uint32_t )&USART1->DR; /* Peripheral address */
    DMA1_Channel3->CMAR  = ( uint32_t )buffer;  /* Memory Address */
    DMA1_Channel3->CNDTR = buffer_size;
    /**
     * Set peripheral and buffer size.
     */
    DMA1_Channel3->CCR &= ~( DMA_CCR_MEM2MEM );
    DMA1_Channel3->CCR &= ~( DMA_CCR_MSIZE ); /* Data buffer store each charecter 1 Byte 8 Bit */
    DMA1_Channel3->CCR &= ~( DMA_CCR_PSIZE ); /* Pripheral size is 8 Bit or 1 Byte */
    DMA1_Channel3->CCR &= ~( DMA_CCR_PINC );
    DMA1_Channel3->CCR |= DMA_CCR_CIRC;               /* Circular mode enable */
    DMA1_Channel3->CCR &= ~( DMA_CCR_DIR );           /* Read from peripheral */
    DMA1_Channel3->CCR |= DMA_CCR_MINC;               /* Set data memory 8 bit */
    DMA1_Channel3->CCR |= ( 0x03 << DMA_CCR_PL_Pos ); /* Priority level set very high */
    DMA1_Channel3->CCR |= DMA_CCR_EN;                 /* Enable channel */
}void dma_init( uint8_t *buffer, const uint32_t buffer_size)
{
    RCC->AHBENR |= RCC_AHBENR_DMA1EN;


    DMA1_Channel3->CCR = 0x00U; /* Clear control register */


    DMA1_Channel3->CPAR  = ( uint32_t )&USART1->DR; /* Peripheral address */
    DMA1_Channel3->CMAR  = ( uint32_t )buffer;  /* Memory Address */
    DMA1_Channel3->CNDTR = buffer_size;
    /**
     * Set peripheral and buffer size.
     */
    DMA1_Channel3->CCR &= ~( DMA_CCR_MEM2MEM );
    DMA1_Channel3->CCR &= ~( DMA_CCR_MSIZE ); /* Data buffer store each charecter 1 Byte 8 Bit */
    DMA1_Channel3->CCR &= ~( DMA_CCR_PSIZE ); /* Pripheral size is 8 Bit or 1 Byte */
    DMA1_Channel3->CCR &= ~( DMA_CCR_PINC );
    DMA1_Channel3->CCR |= DMA_CCR_CIRC;               /* Circular mode enable */
    DMA1_Channel3->CCR &= ~( DMA_CCR_DIR );           /* Read from peripheral */
    DMA1_Channel3->CCR |= DMA_CCR_MINC;               /* Set data memory 8 bit */
    DMA1_Channel3->CCR |= ( 0x03 << DMA_CCR_PL_Pos ); /* Priority level set very high */
    DMA1_Channel3->CCR |= DMA_CCR_EN;                 /* Enable channel */
}

And in the ISR store CNDTR:

void USART3_IRQHandler( void )
{
    if ( USART3->SR & USART_SR_IDLE )
    {
        volatile uint32_t tmp;
        tmp = USART3->DR;
        (void)tmp;


        new_pos = DMA1_Channel3->CNDTR;
    }
}void USART3_IRQHandler( void )
{
    if ( USART3->SR & USART_SR_IDLE )
    {
        volatile uint32_t tmp;
        tmp = USART3->DR;
        (void)tmp;


        new_pos = DMA1_Channel3->CNDTR;
    }
}
reddit.com
u/Yousef_Tele — 1 month ago

Best practice for show NMEA NEO-6M data with UART printf?

Hello everyone, I tried to implement a CMSIS-based STM32F103C8T6 NMEA receiver from NEO-6M. Still, I enabled receiver mode on USART1, then implemented printf on USART2, an efficient way to implement it, I think. I can implement a method like this:

USART1 RX (receive nmea) --> DMA like ch15 ---> RAM --> DMA like ch14 ----> USART2 TX (printf)

Please help me, and give me some advice on writing better code

UPDATE: Please for better understanding, check last line on main() in this code "https://github.com/yousefsmt/GSM-GPS-Telemetry/tree/feature/add-peripheral"

reddit.com
u/Yousef_Tele — 2 months ago

If everything block, how can I find best VPN protocol?

Hello everyone, in my environment internet blocks just some sites in the white list like Google and github.com are open.

How can I create a vpn setup with use it connect to main internet?

Some partners used by specific setup with combine vless, vmess. But I want to run my own setup

reddit.com
u/Yousef_Tele — 2 months ago
▲ 2 r/VPN

If everything block, how can I find best VPN protocol?

Hello everyone, in my environment internet blocks just some sites in the white list like Google and github.com are open.

How can I create a vpn setup with use it connect to main internet?

Some partners used by specific setup with combine vless, vmess. But I want to run my own setup

reddit.com
u/Yousef_Tele — 2 months ago

Hello everyone, I wrote the code and wrote it with FTDI to the STM32F103C8T6 Blue Pill, but the on-board LED (PC13) turns on, and doesn't blink. I think this happend for didn't set the interrupt. I set it, but it doesn't work anyway. Why? thanks everyone

#include "FreeRTOSTasks.h"


#include "flash.h"
#include "rcc.h"
#include "gpio.h"


#define WAITE_STATE (0x00U) /* True latency for HSI (8MHz) SYSCLK clock */


void StartTask( void *param );
void InterruptPriority_Config( void );


int main( void )
{
    FLASH_ConfigWaitState( WAITE_STATE );


    RCC_Init();


    SystemCoreClockUpdate();


    GPIO_Init();


    InterruptPriority_Config();


    ( void )xTaskCreate(StartTask, "Task1", STARTUP_STACK_SIZE, NULL, STARTUP_STACK_PRIORITY, NULL);


    vTaskStartScheduler();


    while (1)
    {
        /* code */
    }
    
    return 0;
}


void StartTask( void *param )
{
    ( void )param;


    while (1)
    {
        GPIOC->BSRR = GPIO_BSRR_BS13;
        vTaskDelay(1000);


        GPIOC->BRR = GPIO_BRR_BR13;
        vTaskDelay(1000);
    }
}


void InterruptPriority_Config( void )
{
    /* Priority grouping: 4 bits preemption, 0 bits subpriority */
    NVIC_SetPriorityGrouping(0);


    /* FreeRTOS core exceptions */
    NVIC_SetPriority(SVCall_IRQn, 0);
    NVIC_SetPriority(PendSV_IRQn, 15);
    NVIC_SetPriority(SysTick_IRQn, 15);
}#include "FreeRTOSTasks.h"


#include "flash.h"
#include "rcc.h"
#include "gpio.h"


#define WAITE_STATE (0x00U) /* True latency for HSI (8MHz) SYSCLK clock */


void StartTask( void *param );
void InterruptPriority_Config( void );


int main( void )
{
    FLASH_ConfigWaitState( WAITE_STATE );


    RCC_Init();


    SystemCoreClockUpdate();


    GPIO_Init();


    InterruptPriority_Config();


    ( void )xTaskCreate(StartTask, "Task1", STARTUP_STACK_SIZE, NULL, STARTUP_STACK_PRIORITY, NULL);


    vTaskStartScheduler();


    while (1)
    {
        /* code */
    }
    
    return 0;
}


void StartTask( void *param )
{
    ( void )param;


    while (1)
    {
        GPIOC->BSRR = GPIO_BSRR_BS13;
        vTaskDelay(1000);


        GPIOC->BRR = GPIO_BRR_BR13;
        vTaskDelay(1000);
    }
}


void InterruptPriority_Config( void )
{
    /* Priority grouping: 4 bits preemption, 0 bits subpriority */
    NVIC_SetPriorityGrouping(0);


    /* FreeRTOS core exceptions */
    NVIC_SetPriority(SVCall_IRQn, 0);
    NVIC_SetPriority(PendSV_IRQn, 15);
    NVIC_SetPriority(SysTick_IRQn, 15);
}
reddit.com
u/Yousef_Tele — 2 months ago

Hi everyone, I'm working on an idea about creating an RL gateway agent with the LoRaWAN module NS3, and the RL part works on NS3Gym.

I created an environment with 10 end devices and 1 network server. Gateway, like an UAV, then collects data from each end device. In this scenario, I must minimize the time difference between the data generation time on each node and the network server. But now I think, how can I add some constraints for the end device or gateway, or all parts of the environment? Please give me some idea and any advice for me. Thanks to everyone.

Note that all scenarios were simulated with NS3 (C++) and an RL agent with Python.

reddit.com
u/Yousef_Tele — 2 months ago

Hello everyone, I created a simple task with STM32F103C8T6 and FreeRTOS, then I ran it in Proteus simulator, but it doesn't work. I checked everything, maybe bad, but I didn't find my answer.

I uploaded my project to GitHub at the link below:

https://github.com/yousefsmt/RTOS_TempMon/archive/refs/heads/fix/integerate-all-api.zip

https://github.com/yousefsmt/RTOS_TempMon

Anyone can help me, thank you so much.

** Note: Please check "fix/integerate-all-api" branch **

github.com
u/Yousef_Tele — 2 months ago