#include <stdio.h>
#include <signal.h>
#include <errno.h>
#include <unistd.h>
#include <time.h>
#include "vcanevt.h"
#define READ_WAIT_INFINITE (unsigned long)(-1)
static unsigned int msgCounter = 0;
{
char buf[50];
buf[0] = '\0';
printf("%s: failed, stat=%d (%s)\n", id, (int)stat, buf);
}
}
static void printUsageAndExit(char *prgName)
{
printf("Usage: '%s <channel>'\n", prgName);
exit(1);
}
static void sighand(int sig)
{
(void)sig;
}
static char* busStatToStr(const unsigned long flag) {
char* tempStr = NULL;
#define MACRO2STR(x) case x: tempStr = #x; break
switch (flag) {
MACRO2STR( CHIPSTAT_BUSOFF );
MACRO2STR( CHIPSTAT_ERROR_PASSIVE );
MACRO2STR( CHIPSTAT_ERROR_WARNING );
MACRO2STR( CHIPSTAT_ERROR_ACTIVE );
default: tempStr = ""; break;
}
#undef MACRO2STR
return tempStr;
}
printf(
"CAN Status Event: %s\n", busStatToStr(data->
info.
status.busStatus));
break;
printf("CAN Error Event\n");
break;
printf("CAN Tx Event\n");
break;
printf("CAN Rx Event\n");
break;
}
return;
}
int main(int argc, char *argv[])
{
int channel;
if (argc != 2) {
printUsageAndExit(argv[0]);
}
{
char *endPtr = NULL;
errno = 0;
channel = strtol(argv[1], &endPtr, 10);
if ( (errno != 0) || ((channel == 0) && (endPtr == argv[1])) ) {
printUsageAndExit(argv[0]);
}
}
printf("Reading messages on channel %d\n", channel);
signal(SIGINT, sighand);
siginterrupt(SIGINT, 1);
if (hnd < 0) {
printf("canOpenChannel %d", channel);
check("", hnd);
return -1;
}
check("canSetNotify", stat);
check("canSetBusParams", stat);
goto ErrorExit;
}
check("canBusOn", stat);
goto ErrorExit;
}
do {
long id;
unsigned char msg[8];
unsigned int dlc;
unsigned int flag;
unsigned long time;
stat =
canReadWait(hnd, &
id, &msg, &dlc, &flag, &time, READ_WAIT_INFINITE);
msgCounter++;
printf("(%u) ERROR FRAME", msgCounter);
}
else {
unsigned j;
printf("(%u) id:%ld dlc:%u data: ", msgCounter, id, dlc);
if (dlc > 8) {
dlc = 8;
}
for (j = 0; j < dlc; j++) {
printf("%2.2x ", msg[j]);
}
}
printf(" flags:0x%x time:%lu\n", flag, time);
}
else {
if (errno == 0) {
check("\ncanReadWait", stat);
}
else {
perror("\ncanReadWait error");
}
}
ErrorExit:
check("canBusOff", stat);
usleep(50*1000);
check("canClose", stat);
check("canUnloadLibrary", stat);
return 0;
}