SDK Script candump.are

candump.are
/* DESC: This script can be used to receive CAN messages
 * Copyright (C) 2014 NetModule AG, Switzerland
 */
 
DEV = "can0"; /* CAN1 */
BITRATE = 125000; /* run 125kbit bitrate */
LISTENONLY = 1; /* open in listen-only mode */
RESTART_TIMEOUT = 0; /* restart timeout in case of bus-off (msecs, 0 = disabled) */
MAX_MESSAGES = 1024; /* max. number of dumped messages */
 
ret = nb_can_setattr(DEV, BITRATE, LISTENONLY, RESTART_TIMEOUT);
if (ret != 0) {
    printf("Unable to set attributes of %s\n", DEV);
    exit(1);
}
 
sock = nb_can_open(DEV);
if (sock < 0) {
    printf("Unable to open %s\n", DEV);
    exit(1);
}
 
if (0) {
    FILTER_ID = 1;
    FILTER_MASK = 0xFFFFFFFF;
 
    if (nb_can_setfilter(sock, FILTER_ID, FILTER_MASK) < 0) {
        printf("Unable to set filter on %s\n", DEV);
        can_close(sock);
        exit(1);
    }
}
 
fp = fopen("/tmp/candump.log", "w+");
if (is_void(fp)) {
    nb_syslog("unable to open %s", to);
    exit(1);
}
 
for (count = 0; count < MAX_MESSAGES;count++) {
    msg = nb_can_recvmsg(sock, 5);
    if (is_void(msg)) continue;
 
    id = struct_get(msg, "id") & CAN_ERR_MASK; /* omit EFF, RTR, ERR flags */
    len = struct_get(msg, "len");
    data = struct_get(msg, "data");
 
    if (len == 8 && id == 0x00e00442) {
        if (data[0] == 0xc0 &&
            data[1] == 0x00 &&
            data[2] == 0x00 &&
            data[3] == 0x48 &&
            data[4] == 0x00 &&
            data[5] == 0x00 &&
            data[6] == 0x37 &&
            data[7] == 0x00) {
            /* Corresponds to a periodic status message as received on the 
               front CAN bus of a Volvo V70,
 
               The RJ 45 plug has been connected to the green HU radio ISO connector:
 
                    ISO 
 
               1  2  3  4  5  6     
               7  8  9 10 11 12                             RJ45
               |  |          |                        
               |  |          +-->  GND   (black)   <----  white green
               |  +------------->  CAN-H (braun)   <----  white orange
               +---------------->  CAN-L (violet)  <----  orange
 
            */
            nb_syslog("Your car sent a hello");
        }
    }
 
    /* log message */
    now = localtime(time());
    timestamp = strftime("%Y-%m-%d %H:%M:%S", now);
    fwrite(fp, sprintf("%s   %08x: ", timestamp, id));
    for (i = 0; i < len; i++) {
        fwrite(fp, sprintf("%02x ", data[i]));
    }
    fwrite(fp, "\n");
 
    count++;
}
 
fclose(fp);
nb_can_close(sock);
 
exit(0);