Skip to content

CAN silkit code crashing intermediately #214

@komalpatil1811

Description

@komalpatil1811

CAN silkit code is getting aborted intermediately after transmitting few frames.

Silkit Version: 4.0.55

Source code details: Created thread running at every 1ms and called transmit frames at every 1ms task rate. At receiving end registered reception callback for receiving frames. CAN echo device utility used for testing.

Observed that after sometime the code gets aborted abruptly after Sendframe.

OS: WSL on Windows 11 (x86 gcc make environments)

Below is code snippet for same:

#define _GNU_SOURCE
#include <stdio.h>
#include <signal.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
#include <errno.h>
#include <sched.h>
#include <pthread.h>
#include <sys/mman.h>
#include <stdatomic.h>
#include "Can.h"
 
#define SIGNAL_NUM (SIGRTMIN)
#define TIMER_INTERVAL_NS 1000000L // 1 ms

volatile sig_atomic_t in_handler = 0;
static timer_t timerid;
static sigset_t sigset_new;
pthread_t thread1;

void GetTimeStamp(char *buffer, size_t buffer_size) 
{
    struct timeval tv;
    struct tm *tm_info;
    gettimeofday(&tv, NULL);
    tm_info = localtime(&tv.tv_sec);
    strftime(buffer, buffer_size, "%Y-%m-%d %H:%M:%S", tm_info);
    // Append microseconds
    size_t len = strlen(buffer);
    snprintf(buffer + len, buffer_size - len, ".%03ld", tv.tv_usec / 1000);
}

void* signal_handler_thread(void* arg) 
{
    Can_PduType g_canPdus;
    uint8_t txHTH;
    uint8_t resultData[8] = {'v', 'C', 'A', 'N', '_', 'D', 'r', 'v'}; // Example data to be sent  
    static int occurence = 0;
    char timestamp[64];

    struct sched_param param = { .sched_priority =  99};
    pthread_setschedparam(pthread_self(), SCHED_FIFO, &param);
    siginfo_t info;
 
    while (1) 
    {
        int sig = sigwaitinfo(&sigset_new, &info);
        if (sig == SIGNAL_NUM) 
        {
            g_canPdus.id = occurence; 
            g_canPdus.length = 0x08; 
            g_canPdus.sdu = resultData; 
            g_canPdus.swPduHandle = 0x10; 
            txHTH = occurence+ 10;
            GetTimeStamp(timestamp, sizeof(timestamp));
            printf("[%s] Can Write Called\n", timestamp);
            Can_Write(txHTH, &g_canPdus);
            GetTimeStamp(timestamp, sizeof(timestamp));
            printf("[%s] Can Write End\n", timestamp);
            occurence++;
            if(occurence>6)
            {
                occurence = 0;
            }
         }
    }
    return NULL;
}

void tpl_start_auto_timer_( long unsigned)
{
    sigemptyset(&sigset_new);
    sigaddset(&sigset_new, SIGNAL_NUM);
    if (pthread_sigmask(SIG_BLOCK, &sigset_new, NULL) != 0)
    {
        perror("pthread_sigmask");
        exit(1);
    }
    // Start signal handler thread
    pthread_create(&thread1, NULL, signal_handler_thread, NULL);
    struct sigevent sev = 
    {
        .sigev_notify = SIGEV_SIGNAL,
        .sigev_signo = SIGNAL_NUM
    };
    if (timer_create(CLOCK_MONOTONIC, &sev, &timerid) != 0) 
    {
        perror("timer_create");
        exit(1);
    }
    struct itimerspec its = 
    {
        .it_value = { .tv_sec = 0, .tv_nsec = TIMER_INTERVAL_NS * 30 },
        .it_interval = { .tv_sec = 0, .tv_nsec = TIMER_INTERVAL_NS }
    };
    if (timer_settime(timerid, 0, &its, NULL) != 0) 
    {
        perror("timer_settime");
        exit(1);
    }
}

int main() 
{
    // Lock memory to avoid page faults during RT
    if (mlockall(MCL_CURRENT | MCL_FUTURE) == -1) 
    {
        perror("mlockall");
    }
    tpl_start_auto_timer_(0);    
    Can_Init();
    for (int i = 0; i < 7; ++i)
    {
        Can_SetControllerMode(i, CAN_CS_STARTED);
    }
    while (1) 
    {
        pause(); // Wait for signals
    }
    return 0;
}

EDIT(VDanielEdwards): Wrapped the code in a code block.

Metadata

Metadata

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions