Merge pull request #200 from hartkopp/29bit-cansniffer
cansniffer: support EFF frames with 29 bit CAN IDs
Convert cansniffer from CAN_BCM to CAN_RAW sockets and handle the
content filter in user space. Conversion CAN_RAW including slot
concept and sorting contributed by @coryjfowler.
Functional changes visible to users:
- New format for settings file (using 'r' and 'w' interactive commands)
- New command '8' to switch in a compact binary view for 80 columns view
- New head line layout
- The gap time between CAN frames is displayed in milli seconds (ms)
- The interactive filters support 29 bit CAN IDs
- New interactive command to clear/redraw the screen (<SPACE><ENTER>)
- New interactive commands to enable/disable SFF/EFF frames
a<ENTER> - enable 'a'll SFF CAN-IDs to sniff
n<ENTER> - enable 'n'one SFF CAN-IDs to sniff
A<ENTER> - enable 'A'll EFF CAN-IDs to sniff
N<ENTER> - enable 'N'one EFF CAN-IDs to sniff
- Fixes in bit change colorization and gap calculation detected at rework
pull/204/head
commit
c5cb7e1a7c
571
cansniffer.c
571
cansniffer.c
|
|
@ -1,6 +1,6 @@
|
||||||
/* SPDX-License-Identifier: (GPL-2.0-only OR BSD-3-Clause) */
|
/* SPDX-License-Identifier: (GPL-2.0-only OR BSD-3-Clause) */
|
||||||
/*
|
/*
|
||||||
* cansniffer.c
|
* cansniffer.c - volatile CAN content visualizer
|
||||||
*
|
*
|
||||||
* Copyright (c) 2002-2007 Volkswagen Group Electronic Research
|
* Copyright (c) 2002-2007 Volkswagen Group Electronic Research
|
||||||
* All rights reserved.
|
* All rights reserved.
|
||||||
|
|
@ -44,6 +44,8 @@
|
||||||
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <stdbool.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <fcntl.h>
|
#include <fcntl.h>
|
||||||
|
|
@ -61,13 +63,14 @@
|
||||||
#include <net/if.h>
|
#include <net/if.h>
|
||||||
|
|
||||||
#include <linux/can.h>
|
#include <linux/can.h>
|
||||||
#include <linux/can/bcm.h>
|
#include <linux/can/raw.h>
|
||||||
#include <linux/sockios.h>
|
#include <linux/sockios.h>
|
||||||
|
|
||||||
#include "terminal.h"
|
#include "terminal.h"
|
||||||
|
|
||||||
#define SETFNAME "sniffset."
|
#define SETFNAME "sniffset."
|
||||||
#define ANYDEV "any"
|
#define ANYDEV "any"
|
||||||
|
#define MAX_SLOTS 2048
|
||||||
|
|
||||||
/* flags */
|
/* flags */
|
||||||
|
|
||||||
|
|
@ -92,9 +95,10 @@
|
||||||
|
|
||||||
#define ATTCOLOR ATTBOLD FGRED
|
#define ATTCOLOR ATTBOLD FGRED
|
||||||
|
|
||||||
#define STARTLINESTR "XX delta ID data ... "
|
#define LDL " | " /* long delimiter */
|
||||||
|
#define SDL "|" /* short delimiter for binary on 80 chars terminal */
|
||||||
|
|
||||||
struct snif {
|
static struct snif {
|
||||||
int flags;
|
int flags;
|
||||||
long hold;
|
long hold;
|
||||||
long timeout;
|
long timeout;
|
||||||
|
|
@ -104,31 +108,54 @@ struct snif {
|
||||||
struct can_frame current;
|
struct can_frame current;
|
||||||
struct can_frame marker;
|
struct can_frame marker;
|
||||||
struct can_frame notch;
|
struct can_frame notch;
|
||||||
} sniftab[2048];
|
} sniftab[MAX_SLOTS];
|
||||||
|
|
||||||
|
|
||||||
extern int optind, opterr, optopt;
|
extern int optind, opterr, optopt;
|
||||||
|
|
||||||
|
static int idx;
|
||||||
static int running = 1;
|
static int running = 1;
|
||||||
static int clearscreen = 1;
|
static int clearscreen = 1;
|
||||||
|
static int print_eff;
|
||||||
static int notch;
|
static int notch;
|
||||||
static int filter_id_only;
|
|
||||||
static long timeout = TIMEOUT;
|
static long timeout = TIMEOUT;
|
||||||
static long hold = HOLD;
|
static long hold = HOLD;
|
||||||
static long loop = LOOP;
|
static long loop = LOOP;
|
||||||
static unsigned char binary;
|
static unsigned char binary;
|
||||||
|
static unsigned char binary8;
|
||||||
static unsigned char binary_gap;
|
static unsigned char binary_gap;
|
||||||
static unsigned char color;
|
static unsigned char color;
|
||||||
static char *interface;
|
static char *interface;
|
||||||
|
static char *vdl = LDL; /* variable delimiter */
|
||||||
|
static char *ldl = LDL; /* long delimiter */
|
||||||
|
|
||||||
void rx_setup (int fd, int id);
|
void print_snifline(int slot);
|
||||||
void rx_delete (int fd, int id);
|
|
||||||
void print_snifline(int id);
|
|
||||||
int handle_keyb(int fd);
|
int handle_keyb(int fd);
|
||||||
int handle_bcm(int fd, long currcms);
|
int handle_frame(int fd, long currcms);
|
||||||
int handle_timeo(int fd, long currcms);
|
int handle_timeo(long currcms);
|
||||||
void writesettings(char* name);
|
void writesettings(char* name);
|
||||||
void readsettings(char* name, int sockfd);
|
int readsettings(char* name);
|
||||||
|
int sniftab_index(canid_t id);
|
||||||
|
|
||||||
|
void switchvdl(char *delim)
|
||||||
|
{
|
||||||
|
/* reduce delimiter size for EFF IDs in binary display of up
|
||||||
|
to 8 data bytes payload to fit into 80 chars per line */
|
||||||
|
if (binary8)
|
||||||
|
vdl = delim;
|
||||||
|
}
|
||||||
|
|
||||||
|
int comp(const void *elem1, const void *elem2)
|
||||||
|
{
|
||||||
|
unsigned long f = ((struct snif*)elem1)->current.can_id;
|
||||||
|
unsigned long s = ((struct snif*)elem2)->current.can_id;
|
||||||
|
|
||||||
|
if (f > s)
|
||||||
|
return 1;
|
||||||
|
if (f < s)
|
||||||
|
return -1;
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
void print_usage(char *prg)
|
void print_usage(char *prg)
|
||||||
{
|
{
|
||||||
|
|
@ -136,42 +163,62 @@ void print_usage(char *prg)
|
||||||
"commands that can be entered at runtime:\n"
|
"commands that can be entered at runtime:\n"
|
||||||
" q<ENTER> - quit\n"
|
" q<ENTER> - quit\n"
|
||||||
" b<ENTER> - toggle binary / HEX-ASCII output\n"
|
" b<ENTER> - toggle binary / HEX-ASCII output\n"
|
||||||
|
" 8<ENTER> - toggle binary / HEX-ASCII output (small for EFF on 80 chars)\n"
|
||||||
" B<ENTER> - toggle binary with gap / HEX-ASCII output (exceeds 80 chars!)\n"
|
" B<ENTER> - toggle binary with gap / HEX-ASCII output (exceeds 80 chars!)\n"
|
||||||
" c<ENTER> - toggle color mode\n"
|
" c<ENTER> - toggle color mode\n"
|
||||||
|
" <SPACE><ENTER> - force a clear screen\n"
|
||||||
" #<ENTER> - notch currently marked/changed bits (can be used repeatedly)\n"
|
" #<ENTER> - notch currently marked/changed bits (can be used repeatedly)\n"
|
||||||
" *<ENTER> - clear notched marked\n"
|
" *<ENTER> - clear notched marked\n"
|
||||||
" rMYNAME<ENTER> - read settings file (filter/notch)\n"
|
" rMYNAME<ENTER> - read settings file (filter/notch)\n"
|
||||||
" wMYNAME<ENTER> - write settings file (filter/notch)\n"
|
" wMYNAME<ENTER> - write settings file (filter/notch)\n"
|
||||||
|
" a<ENTER> - enable 'a'll SFF CAN-IDs to sniff\n"
|
||||||
|
" n<ENTER> - enable 'n'one SFF CAN-IDs to sniff\n"
|
||||||
|
" A<ENTER> - enable 'A'll EFF CAN-IDs to sniff\n"
|
||||||
|
" N<ENTER> - enable 'N'one EFF CAN-IDs to sniff\n"
|
||||||
" +FILTER<ENTER> - add CAN-IDs to sniff\n"
|
" +FILTER<ENTER> - add CAN-IDs to sniff\n"
|
||||||
" -FILTER<ENTER> - remove CAN-IDs to sniff\n"
|
" -FILTER<ENTER> - remove CAN-IDs to sniff\n"
|
||||||
"\n"
|
"\n"
|
||||||
"FILTER can be a single CAN-ID or a CAN-ID/Bitmask:\n"
|
"FILTER can be a single CAN-ID or a CAN-ID/Bitmask:\n"
|
||||||
" +1F5<ENTER> - add CAN-ID 0x1F5\n"
|
"\n"
|
||||||
" -42E<ENTER> - remove CAN-ID 0x42E\n"
|
" single SFF 11 bit IDs:\n"
|
||||||
" -42E7FF<ENTER> - remove CAN-ID 0x42E (using Bitmask)\n"
|
" +1F5<ENTER> - add SFF CAN-ID 0x1F5\n"
|
||||||
" -500700<ENTER> - remove CAN-IDs 0x500 - 0x5FF\n"
|
" -42E<ENTER> - remove SFF CAN-ID 0x42E\n"
|
||||||
" +400600<ENTER> - add CAN-IDs 0x400 - 0x5FF\n"
|
"\n"
|
||||||
" +000000<ENTER> - add all CAN-IDs\n"
|
" single EFF 29 bit IDs:\n"
|
||||||
" -000000<ENTER> - remove all CAN-IDs\n"
|
" +18FEDF55<ENTER> - add EFF CAN-ID 0x18FEDF55\n"
|
||||||
|
" -00000090<ENTER> - remove EFF CAN-ID 0x00000090\n"
|
||||||
|
"\n"
|
||||||
|
" CAN-ID/Bitmask SFF:\n"
|
||||||
|
" -42E7FF<ENTER> - remove SFF CAN-ID 0x42E (using Bitmask)\n"
|
||||||
|
" -500700<ENTER> - remove SFF CAN-IDs 0x500 - 0x5FF\n"
|
||||||
|
" +400600<ENTER> - add SFF CAN-IDs 0x400 - 0x5FF\n"
|
||||||
|
" +000000<ENTER> - add all SFF CAN-IDs\n"
|
||||||
|
" -000000<ENTER> - remove all SFF CAN-IDs\n"
|
||||||
|
"\n"
|
||||||
|
" CAN-ID/Bitmask EFF:\n"
|
||||||
|
" -0000000000000000<ENTER> - remove all EFF CAN-IDs\n"
|
||||||
|
" +12345678000000FF<ENTER> - add EFF CAN IDs xxxxxx78\n"
|
||||||
|
" +0000000000000000<ENTER> - add all EFF CAN-IDs\n"
|
||||||
"\n"
|
"\n"
|
||||||
"if (id & filter) == (sniff-id & filter) the action (+/-) is performed,\n"
|
"if (id & filter) == (sniff-id & filter) the action (+/-) is performed,\n"
|
||||||
"which is quite easy when the filter is 000\n"
|
"which is quite easy when the filter is 000 resp. 00000000 for EFF.\n"
|
||||||
"\n"
|
"\n"
|
||||||
};
|
};
|
||||||
|
|
||||||
|
fprintf(stderr, "%s - volatile CAN content visualizer.\n", prg);
|
||||||
fprintf(stderr, "\nUsage: %s [can-interface]\n", prg);
|
fprintf(stderr, "\nUsage: %s [can-interface]\n", prg);
|
||||||
fprintf(stderr, "Options:\n");
|
fprintf(stderr, "Options:\n");
|
||||||
fprintf(stderr, " -m <mask> (initial FILTER default 0x00000000)\n");
|
|
||||||
fprintf(stderr, " -v <value> (initial FILTER default 0x00000000)\n");
|
|
||||||
fprintf(stderr, " -q (quiet - all IDs deactivated)\n");
|
fprintf(stderr, " -q (quiet - all IDs deactivated)\n");
|
||||||
fprintf(stderr, " -r <name> (read %sname from file)\n", SETFNAME);
|
fprintf(stderr, " -r <name> (read %sname from file)\n", SETFNAME);
|
||||||
|
fprintf(stderr, " -e (fix extended frame format output - no auto detect)\n");
|
||||||
fprintf(stderr, " -b (start with binary mode)\n");
|
fprintf(stderr, " -b (start with binary mode)\n");
|
||||||
|
fprintf(stderr, " -8 (start with binary mode - for EFF on 80 chars)\n");
|
||||||
fprintf(stderr, " -B (start with binary mode with gap - exceeds 80 chars!)\n");
|
fprintf(stderr, " -B (start with binary mode with gap - exceeds 80 chars!)\n");
|
||||||
fprintf(stderr, " -c (color changes)\n");
|
fprintf(stderr, " -c (color changes)\n");
|
||||||
fprintf(stderr, " -f (filter on CAN-ID only)\n");
|
|
||||||
fprintf(stderr, " -t <time> (timeout for ID display [x10ms] default: %d, 0 = OFF)\n", TIMEOUT);
|
fprintf(stderr, " -t <time> (timeout for ID display [x10ms] default: %d, 0 = OFF)\n", TIMEOUT);
|
||||||
fprintf(stderr, " -h <time> (hold marker on changes [x10ms] default: %d)\n", HOLD);
|
fprintf(stderr, " -h <time> (hold marker on changes [x10ms] default: %d)\n", HOLD);
|
||||||
fprintf(stderr, " -l <time> (loop time (display) [x10ms] default: %d)\n", LOOP);
|
fprintf(stderr, " -l <time> (loop time (display) [x10ms] default: %d)\n", LOOP);
|
||||||
|
fprintf(stderr, " -? (print this help text)\n");
|
||||||
fprintf(stderr, "Use interface name '%s' to receive from all can-interfaces.\n", ANYDEV);
|
fprintf(stderr, "Use interface name '%s' to receive from all can-interfaces.\n", ANYDEV);
|
||||||
fprintf(stderr, "\n");
|
fprintf(stderr, "\n");
|
||||||
fprintf(stderr, "%s", manual);
|
fprintf(stderr, "%s", manual);
|
||||||
|
|
@ -186,37 +233,28 @@ int main(int argc, char **argv)
|
||||||
{
|
{
|
||||||
fd_set rdfs;
|
fd_set rdfs;
|
||||||
int s;
|
int s;
|
||||||
canid_t mask = 0;
|
|
||||||
canid_t value = 0;
|
|
||||||
long currcms = 0;
|
long currcms = 0;
|
||||||
long lastcms = 0;
|
long lastcms = 0;
|
||||||
unsigned char quiet = 0;
|
unsigned char quiet = 0;
|
||||||
int opt, ret;
|
int opt, ret;
|
||||||
struct timeval timeo, start_tv, tv;
|
struct timeval timeo, start_tv, tv;
|
||||||
struct sockaddr_can addr;
|
struct sockaddr_can addr;
|
||||||
struct ifreq ifr;
|
|
||||||
int i;
|
int i;
|
||||||
|
|
||||||
|
|
||||||
signal(SIGTERM, sigterm);
|
signal(SIGTERM, sigterm);
|
||||||
signal(SIGHUP, sigterm);
|
signal(SIGHUP, sigterm);
|
||||||
signal(SIGINT, sigterm);
|
signal(SIGINT, sigterm);
|
||||||
|
|
||||||
for (i=0; i < 2048 ;i++) /* default: check all CAN-IDs */
|
for (i = 0; i < MAX_SLOTS ;i++) /* default: enable all slots */
|
||||||
do_set(i, ENABLE);
|
do_set(i, ENABLE);
|
||||||
|
|
||||||
while ((opt = getopt(argc, argv, "m:v:r:t:h:l:qbBcf?")) != -1) {
|
while ((opt = getopt(argc, argv, "r:t:h:l:qeb8Bc?")) != -1) {
|
||||||
switch (opt) {
|
switch (opt) {
|
||||||
case 'm':
|
|
||||||
sscanf(optarg, "%x", &mask);
|
|
||||||
break;
|
|
||||||
|
|
||||||
case 'v':
|
|
||||||
sscanf(optarg, "%x", &value);
|
|
||||||
break;
|
|
||||||
|
|
||||||
case 'r':
|
case 'r':
|
||||||
readsettings(optarg, 0); /* no BCM-setting here */
|
if (readsettings(optarg) < 0) {
|
||||||
|
fprintf(stderr, "Unable to read setting file '%s%s'!\n", SETFNAME, optarg);
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 't':
|
case 't':
|
||||||
|
|
@ -235,11 +273,22 @@ int main(int argc, char **argv)
|
||||||
quiet = 1;
|
quiet = 1;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
case 'e':
|
||||||
|
print_eff = 1;
|
||||||
|
break;
|
||||||
|
|
||||||
case 'b':
|
case 'b':
|
||||||
binary = 1;
|
binary = 1;
|
||||||
binary_gap = 0;
|
binary_gap = 0;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
case '8':
|
||||||
|
binary = 1;
|
||||||
|
binary8 = 1; /* enable variable delimiter for EFF */
|
||||||
|
switchvdl(SDL); /* switch directly to short delimiter */
|
||||||
|
binary_gap = 0;
|
||||||
|
break;
|
||||||
|
|
||||||
case 'B':
|
case 'B':
|
||||||
binary = 1;
|
binary = 1;
|
||||||
binary_gap = 1;
|
binary_gap = 1;
|
||||||
|
|
@ -249,11 +298,9 @@ int main(int argc, char **argv)
|
||||||
color = 1;
|
color = 1;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 'f':
|
|
||||||
filter_id_only = 1;
|
|
||||||
break;
|
|
||||||
|
|
||||||
case '?':
|
case '?':
|
||||||
|
print_usage(basename(argv[0]));
|
||||||
|
exit(0);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
|
|
@ -268,16 +315,8 @@ int main(int argc, char **argv)
|
||||||
}
|
}
|
||||||
|
|
||||||
if (quiet)
|
if (quiet)
|
||||||
for (i = 0; i < 2048; i++)
|
for (i = 0; i < MAX_SLOTS; i++)
|
||||||
do_clr(i, ENABLE);
|
do_clr(i, ENABLE);
|
||||||
else if (mask || value) {
|
|
||||||
for (i=0; i < 2048 ;i++) {
|
|
||||||
if ((i & mask) == (value & mask))
|
|
||||||
do_set(i, ENABLE);
|
|
||||||
else
|
|
||||||
do_clr(i, ENABLE);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (strlen(argv[optind]) >= IFNAMSIZ) {
|
if (strlen(argv[optind]) >= IFNAMSIZ) {
|
||||||
printf("name of CAN device '%s' is too long!\n", argv[optind]);
|
printf("name of CAN device '%s' is too long!\n", argv[optind]);
|
||||||
|
|
@ -286,33 +325,24 @@ int main(int argc, char **argv)
|
||||||
|
|
||||||
interface = argv[optind];
|
interface = argv[optind];
|
||||||
|
|
||||||
if ((s = socket(PF_CAN, SOCK_DGRAM, CAN_BCM)) < 0) {
|
s = socket(PF_CAN, SOCK_RAW, CAN_RAW);
|
||||||
|
if (s < 0) {
|
||||||
perror("socket");
|
perror("socket");
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
addr.can_family = AF_CAN;
|
addr.can_family = AF_CAN;
|
||||||
|
|
||||||
if (strcmp(ANYDEV, argv[optind])) {
|
if (strcmp(ANYDEV, argv[optind]))
|
||||||
strcpy(ifr.ifr_name, argv[optind]);
|
addr.can_ifindex = if_nametoindex(argv[optind]);
|
||||||
if (ioctl(s, SIOCGIFINDEX, &ifr) < 0) {
|
|
||||||
perror("SIOCGIFINDEX");
|
|
||||||
exit(1);
|
|
||||||
}
|
|
||||||
addr.can_ifindex = ifr.ifr_ifindex;
|
|
||||||
}
|
|
||||||
else
|
else
|
||||||
addr.can_ifindex = 0; /* any can interface */
|
addr.can_ifindex = 0; /* any can interface */
|
||||||
|
|
||||||
if (connect(s, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
|
if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
|
||||||
perror("connect");
|
perror("connect");
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
for (i=0; i < 2048 ;i++) /* initial BCM setup */
|
|
||||||
if (is_set(i, ENABLE))
|
|
||||||
rx_setup(s, i);
|
|
||||||
|
|
||||||
gettimeofday(&start_tv, NULL);
|
gettimeofday(&start_tv, NULL);
|
||||||
tv.tv_sec = tv.tv_usec = 0;
|
tv.tv_sec = tv.tv_usec = 0;
|
||||||
|
|
||||||
|
|
@ -340,10 +370,10 @@ int main(int argc, char **argv)
|
||||||
running &= handle_keyb(s);
|
running &= handle_keyb(s);
|
||||||
|
|
||||||
if (FD_ISSET(s, &rdfs))
|
if (FD_ISSET(s, &rdfs))
|
||||||
running &= handle_bcm(s, currcms);
|
running &= handle_frame(s, currcms);
|
||||||
|
|
||||||
if (currcms - lastcms >= loop) {
|
if (currcms - lastcms >= loop) {
|
||||||
running &= handle_timeo(s, currcms);
|
running &= handle_timeo(currcms);
|
||||||
lastcms = currcms;
|
lastcms = currcms;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -354,85 +384,99 @@ int main(int argc, char **argv)
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
void rx_setup (int fd, int id){
|
void do_modify_sniftab(unsigned int value, unsigned int mask, char cmd)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
|
||||||
struct {
|
for (i = 0; i < idx ;i++) {
|
||||||
struct bcm_msg_head msg_head;
|
if ((sniftab[i].current.can_id & mask) == (value & mask)) {
|
||||||
struct can_frame frame;
|
if (cmd == '+')
|
||||||
} txmsg;
|
do_set(i, ENABLE);
|
||||||
|
else
|
||||||
txmsg.msg_head.opcode = RX_SETUP;
|
do_clr(i, ENABLE);
|
||||||
txmsg.msg_head.can_id = id;
|
}
|
||||||
txmsg.msg_head.flags = RX_CHECK_DLC;
|
}
|
||||||
txmsg.msg_head.ival1.tv_sec = 0;
|
|
||||||
txmsg.msg_head.ival1.tv_usec = 0;
|
|
||||||
txmsg.msg_head.ival2.tv_sec = 0;
|
|
||||||
txmsg.msg_head.ival2.tv_usec = 0;
|
|
||||||
txmsg.msg_head.nframes = 1;
|
|
||||||
|
|
||||||
/* set all bits to be relevant */
|
|
||||||
memset(&txmsg.frame.data, 0xFF, 8);
|
|
||||||
|
|
||||||
if (filter_id_only)
|
|
||||||
txmsg.msg_head.flags |= RX_FILTER_ID;
|
|
||||||
|
|
||||||
if (write(fd, &txmsg, sizeof(txmsg)) < 0)
|
|
||||||
perror("write");
|
|
||||||
};
|
|
||||||
|
|
||||||
void rx_delete (int fd, int id){
|
|
||||||
|
|
||||||
struct bcm_msg_head msg_head;
|
|
||||||
|
|
||||||
msg_head.opcode = RX_DELETE;
|
|
||||||
msg_head.can_id = id;
|
|
||||||
msg_head.nframes = 0;
|
|
||||||
|
|
||||||
if (write(fd, &msg_head, sizeof(msg_head)) < 0)
|
|
||||||
perror("write");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int handle_keyb(int fd){
|
int handle_keyb(int fd)
|
||||||
|
{
|
||||||
char cmd [20] = {0};
|
char cmd [25] = {0};
|
||||||
int i;
|
int i, clen;
|
||||||
unsigned int mask;
|
unsigned int mask;
|
||||||
unsigned int value;
|
unsigned int value;
|
||||||
|
|
||||||
if (read(0, cmd, 19) > strlen("+123456\n"))
|
if (read(0, cmd, 24) > strlen("+1234567812345678\n"))
|
||||||
return 1; /* ignore */
|
return 1; /* ignore */
|
||||||
|
|
||||||
if (strlen(cmd) > 0)
|
if (strlen(cmd) > 0)
|
||||||
cmd[strlen(cmd)-1] = 0; /* chop off trailing newline */
|
cmd[strlen(cmd)-1] = 0; /* chop off trailing newline */
|
||||||
|
|
||||||
|
clen = strlen(&cmd[1]); /* content length behind command */
|
||||||
|
|
||||||
switch (cmd[0]) {
|
switch (cmd[0]) {
|
||||||
|
|
||||||
case '+':
|
case '+':
|
||||||
case '-':
|
case '-':
|
||||||
|
if (clen == 6) {
|
||||||
|
/* masking strict SFF ID content vvvmmm */
|
||||||
sscanf(&cmd[1], "%x", &value);
|
sscanf(&cmd[1], "%x", &value);
|
||||||
if (strlen(&cmd[1]) > 3) {
|
mask = value | 0xFFFF800; /* cleared flags! */
|
||||||
mask = value & 0xFFF;
|
|
||||||
value >>= 12;
|
value >>= 12;
|
||||||
|
value &= 0x7FF;
|
||||||
|
do_modify_sniftab(value, mask, cmd[0]);
|
||||||
|
break;
|
||||||
|
} else if (clen == 16) {
|
||||||
|
sscanf(&cmd[9], "%x", &mask);
|
||||||
|
cmd[9] = 0; /* terminate 'value' */
|
||||||
|
sscanf(&cmd[1], "%x", &value);
|
||||||
|
mask |= CAN_EFF_FLAG;
|
||||||
|
value |= CAN_EFF_FLAG;
|
||||||
|
do_modify_sniftab(value, mask, cmd[0]);
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
else
|
|
||||||
mask = 0x7FF;
|
|
||||||
|
|
||||||
if (cmd[0] == '+') {
|
/* check for single SFF/EFF CAN ID length */
|
||||||
for (i=0; i < 2048 ;i++) {
|
if (!((clen == 3) || (clen == 8)))
|
||||||
if (((i & mask) == (value & mask)) && (is_clr(i, ENABLE))) {
|
break;
|
||||||
|
|
||||||
|
/* enable/disable single SFF/EFF CAN ID */
|
||||||
|
sscanf(&cmd[1], "%x", &value);
|
||||||
|
if (clen == 8)
|
||||||
|
value |= CAN_EFF_FLAG;
|
||||||
|
|
||||||
|
i = sniftab_index(value);
|
||||||
|
if (i < 0)
|
||||||
|
break; /* No Match */
|
||||||
|
|
||||||
|
if (cmd[0] == '+')
|
||||||
do_set(i, ENABLE);
|
do_set(i, ENABLE);
|
||||||
rx_setup(fd, i);
|
else
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else { /* '-' */
|
|
||||||
for (i=0; i < 2048 ;i++) {
|
|
||||||
if (((i & mask) == (value & mask)) && (is_set(i, ENABLE))) {
|
|
||||||
do_clr(i, ENABLE);
|
do_clr(i, ENABLE);
|
||||||
rx_delete(fd, i);
|
|
||||||
}
|
break;
|
||||||
}
|
|
||||||
}
|
case 'a' : /* all SFF CAN IDs */
|
||||||
|
value = 0;
|
||||||
|
mask = 0xFFFF800; /* cleared flags! */
|
||||||
|
do_modify_sniftab(value, mask, '+');
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 'n' : /* none SFF CAN IDs */
|
||||||
|
value = 0;
|
||||||
|
mask = 0xFFFF800; /* cleared flags! */
|
||||||
|
do_modify_sniftab(value, mask, '-');
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 'A' : /* all EFF CAN IDs */
|
||||||
|
value = CAN_EFF_FLAG;
|
||||||
|
mask = CAN_EFF_FLAG;
|
||||||
|
do_modify_sniftab(value, mask, '+');
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 'N' : /* none EFF CAN IDs */
|
||||||
|
value = CAN_EFF_FLAG;
|
||||||
|
mask = CAN_EFF_FLAG;
|
||||||
|
do_modify_sniftab(value, mask, '-');
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 'w' :
|
case 'w' :
|
||||||
|
|
@ -440,7 +484,7 @@ int handle_keyb(int fd){
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 'r' :
|
case 'r' :
|
||||||
readsettings(&cmd[1], fd);
|
readsettings(&cmd[1]); /* don't care about success */
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 'q' :
|
case 'q' :
|
||||||
|
|
@ -449,6 +493,7 @@ int handle_keyb(int fd){
|
||||||
|
|
||||||
case 'B' :
|
case 'B' :
|
||||||
binary_gap = 1;
|
binary_gap = 1;
|
||||||
|
switchvdl(LDL);
|
||||||
if (binary)
|
if (binary)
|
||||||
binary = 0;
|
binary = 0;
|
||||||
else
|
else
|
||||||
|
|
@ -456,13 +501,19 @@ int handle_keyb(int fd){
|
||||||
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
case '8' :
|
||||||
|
binary8 = 1;
|
||||||
|
/* fallthrough */
|
||||||
|
|
||||||
case 'b' :
|
case 'b' :
|
||||||
binary_gap = 0;
|
binary_gap = 0;
|
||||||
if (binary)
|
if (binary) {
|
||||||
binary = 0;
|
binary = 0;
|
||||||
else
|
switchvdl(LDL);
|
||||||
|
} else {
|
||||||
binary = 1;
|
binary = 1;
|
||||||
|
switchvdl(SDL);
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 'c' :
|
case 'c' :
|
||||||
|
|
@ -473,12 +524,16 @@ int handle_keyb(int fd){
|
||||||
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
case ' ' :
|
||||||
|
clearscreen = 1;
|
||||||
|
break;
|
||||||
|
|
||||||
case '#' :
|
case '#' :
|
||||||
notch = 1;
|
notch = 1;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case '*' :
|
case '*' :
|
||||||
for (i=0; i < 2048; i++)
|
for (i = 0; i < idx; i++)
|
||||||
memset(&sniftab[i].notch.data, 0, 8);
|
memset(&sniftab[i].notch.data, 0, 8);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
|
@ -491,66 +546,101 @@ int handle_keyb(int fd){
|
||||||
return 1; /* ok */
|
return 1; /* ok */
|
||||||
};
|
};
|
||||||
|
|
||||||
int handle_bcm(int fd, long currcms){
|
int handle_frame(int fd, long currcms)
|
||||||
|
{
|
||||||
|
bool rx_changed = false;
|
||||||
|
bool run_qsort = false;
|
||||||
|
int nbytes, i, pos;
|
||||||
|
struct can_frame cf;
|
||||||
|
|
||||||
int nbytes, id, i;
|
if ((nbytes = read(fd, &cf, sizeof(cf))) < 0) {
|
||||||
|
perror("raw read");
|
||||||
struct {
|
|
||||||
struct bcm_msg_head msg_head;
|
|
||||||
struct can_frame frame;
|
|
||||||
} bmsg;
|
|
||||||
|
|
||||||
if ((nbytes = read(fd, &bmsg, sizeof(bmsg))) < 0) {
|
|
||||||
perror("bcm read");
|
|
||||||
return 0; /* quit */
|
return 0; /* quit */
|
||||||
}
|
}
|
||||||
|
|
||||||
id = bmsg.msg_head.can_id;
|
if (nbytes != CAN_MTU) {
|
||||||
sniftab[id].laststamp = sniftab[id].currstamp;
|
printf("received strange frame data length %d!\n", nbytes);
|
||||||
ioctl(fd, SIOCGSTAMP, &sniftab[id].currstamp);
|
|
||||||
|
|
||||||
if (bmsg.msg_head.opcode != RX_CHANGED) {
|
|
||||||
printf("received strange BCM opcode %d!\n", bmsg.msg_head.opcode);
|
|
||||||
return 0; /* quit */
|
return 0; /* quit */
|
||||||
}
|
}
|
||||||
|
|
||||||
if (nbytes != sizeof(bmsg)) {
|
if (!print_eff && (cf.can_id & CAN_EFF_FLAG)) {
|
||||||
printf("received strange BCM data length %d!\n", nbytes);
|
print_eff = 1;
|
||||||
return 0; /* quit */
|
clearscreen = 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
sniftab[id].current = bmsg.frame;
|
pos = sniftab_index(cf.can_id);
|
||||||
|
if (pos < 0) {
|
||||||
|
/* CAN ID not existing */
|
||||||
|
if (idx < MAX_SLOTS) {
|
||||||
|
/* assign new slot */
|
||||||
|
pos = idx++;
|
||||||
|
rx_changed = true;
|
||||||
|
run_qsort = true;
|
||||||
|
} else {
|
||||||
|
/* informative exit */
|
||||||
|
perror("number of different CAN IDs exceeded MAX_SLOTS");
|
||||||
|
return 0; /* quit */
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
if (cf.can_dlc == sniftab[pos].current.can_dlc)
|
||||||
|
for (i = 0; i < cf.can_dlc; i++) {
|
||||||
|
if (cf.data[i] != sniftab[pos].current.data[i] ) {
|
||||||
|
rx_changed = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
rx_changed = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* print received frame even if the data didn't change to get a gap time */
|
||||||
|
if ((sniftab[pos].laststamp.tv_sec == 0) && (sniftab[pos].laststamp.tv_usec == 0))
|
||||||
|
rx_changed = true;
|
||||||
|
|
||||||
|
if (rx_changed == true) {
|
||||||
|
sniftab[pos].laststamp = sniftab[pos].currstamp;
|
||||||
|
ioctl(fd, SIOCGSTAMP, &sniftab[pos].currstamp);
|
||||||
|
|
||||||
|
sniftab[pos].current = cf;
|
||||||
for (i = 0; i < 8; i++)
|
for (i = 0; i < 8; i++)
|
||||||
sniftab[id].marker.data[i] |= sniftab[id].current.data[i] ^ sniftab[id].last.data[i];
|
sniftab[pos].marker.data[i] |= sniftab[pos].current.data[i] ^ sniftab[pos].last.data[i];
|
||||||
|
|
||||||
sniftab[id].timeout = (timeout)?(currcms + timeout):0;
|
sniftab[pos].timeout = (timeout)?(currcms + timeout):0;
|
||||||
|
|
||||||
if (is_clr(id, DISPLAY))
|
if (is_clr(pos, DISPLAY))
|
||||||
clearscreen = 1; /* new entry -> new drawing */
|
clearscreen = 1; /* new entry -> new drawing */
|
||||||
|
|
||||||
do_set(id, DISPLAY);
|
do_set(pos, DISPLAY);
|
||||||
do_set(id, UPDATE);
|
do_set(pos, UPDATE);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (run_qsort == true)
|
||||||
|
qsort(sniftab, idx, sizeof(sniftab[0]), comp);
|
||||||
|
|
||||||
return 1; /* ok */
|
return 1; /* ok */
|
||||||
};
|
};
|
||||||
|
|
||||||
int handle_timeo(int fd, long currcms){
|
int handle_timeo(long currcms)
|
||||||
|
{
|
||||||
int i, j;
|
int i, j;
|
||||||
int force_redraw = 0;
|
int force_redraw = 0;
|
||||||
static unsigned int frame_count;
|
static unsigned int frame_count;
|
||||||
|
|
||||||
if (clearscreen) {
|
if (clearscreen) {
|
||||||
char startline[80];
|
if (print_eff)
|
||||||
printf("%s%s", CLR_SCREEN, CSR_HOME);
|
printf("%s%sXX|ms%s-- ID --%sdata ... < %s # l=%ld h=%ld t=%ld slots=%d >",
|
||||||
snprintf(startline, 79, "< cansniffer %s # l=%ld h=%ld t=%ld >", interface, loop, hold, timeout);
|
CLR_SCREEN, CSR_HOME, vdl, vdl, interface, loop, hold, timeout, idx);
|
||||||
printf("%s%*s",STARTLINESTR, 79-(int)strlen(STARTLINESTR), startline);
|
else
|
||||||
|
printf("%s%sXX|ms%sID %sdata ... < %s # l=%ld h=%ld t=%ld slots=%d >",
|
||||||
|
CLR_SCREEN, CSR_HOME, ldl, ldl, interface, loop, hold, timeout, idx);
|
||||||
|
|
||||||
force_redraw = 1;
|
force_redraw = 1;
|
||||||
clearscreen = 0;
|
clearscreen = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (notch) {
|
if (notch) {
|
||||||
for (i=0; i < 2048; i++) {
|
for (i = 0; i < idx; i++) {
|
||||||
for (j = 0; j < 8; j++)
|
for (j = 0; j < 8; j++)
|
||||||
sniftab[i].notch.data[j] |= sniftab[i].marker.data[j];
|
sniftab[i].notch.data[j] |= sniftab[i].marker.data[j];
|
||||||
}
|
}
|
||||||
|
|
@ -561,19 +651,15 @@ int handle_timeo(int fd, long currcms){
|
||||||
printf("%02d\n", frame_count++); /* rolling display update counter */
|
printf("%02d\n", frame_count++); /* rolling display update counter */
|
||||||
frame_count %= 100;
|
frame_count %= 100;
|
||||||
|
|
||||||
for (i=0; i < 2048; i++) {
|
for (i = 0; i < idx; i++) {
|
||||||
|
|
||||||
if is_set(i, ENABLE) {
|
if is_set(i, ENABLE) {
|
||||||
|
|
||||||
if is_set(i, DISPLAY) {
|
if is_set(i, DISPLAY) {
|
||||||
|
|
||||||
if (is_set(i, UPDATE) || (force_redraw)) {
|
if (is_set(i, UPDATE) || (force_redraw)) {
|
||||||
print_snifline(i);
|
print_snifline(i);
|
||||||
sniftab[i].hold = currcms + hold;
|
sniftab[i].hold = currcms + hold;
|
||||||
do_clr(i, UPDATE);
|
do_clr(i, UPDATE);
|
||||||
}
|
}
|
||||||
else
|
else if ((sniftab[i].hold) && (sniftab[i].hold < currcms)) {
|
||||||
if ((sniftab[i].hold) && (sniftab[i].hold < currcms)) {
|
|
||||||
memset(&sniftab[i].marker.data, 0, 8);
|
memset(&sniftab[i].marker.data, 0, 8);
|
||||||
print_snifline(i);
|
print_snifline(i);
|
||||||
sniftab[i].hold = 0; /* disable update by hold */
|
sniftab[i].hold = 0; /* disable update by hold */
|
||||||
|
|
@ -592,14 +678,14 @@ int handle_timeo(int fd, long currcms){
|
||||||
}
|
}
|
||||||
|
|
||||||
return 1; /* ok */
|
return 1; /* ok */
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
void print_snifline(int id){
|
void print_snifline(int slot)
|
||||||
|
{
|
||||||
long diffsec = sniftab[id].currstamp.tv_sec - sniftab[id].laststamp.tv_sec;
|
long diffsec = sniftab[slot].currstamp.tv_sec - sniftab[slot].laststamp.tv_sec;
|
||||||
long diffusec = sniftab[id].currstamp.tv_usec - sniftab[id].laststamp.tv_usec;
|
long diffusec = sniftab[slot].currstamp.tv_usec - sniftab[slot].laststamp.tv_usec;
|
||||||
int dlc_diff = sniftab[id].last.can_dlc - sniftab[id].current.can_dlc;
|
int dlc_diff = sniftab[slot].last.can_dlc - sniftab[slot].current.can_dlc;
|
||||||
|
canid_t cid = sniftab[slot].current.can_id;
|
||||||
int i,j;
|
int i,j;
|
||||||
|
|
||||||
if (diffusec < 0)
|
if (diffusec < 0)
|
||||||
|
|
@ -608,23 +694,27 @@ void print_snifline(int id){
|
||||||
if (diffsec < 0)
|
if (diffsec < 0)
|
||||||
diffsec = diffusec = 0;
|
diffsec = diffusec = 0;
|
||||||
|
|
||||||
if (diffsec > 10)
|
if (diffsec >= 100)
|
||||||
diffsec = 9, diffusec = 999999;
|
diffsec = 99, diffusec = 999999;
|
||||||
|
|
||||||
printf("%ld.%06ld %3X ", diffsec, diffusec, id);
|
if (cid & CAN_EFF_FLAG)
|
||||||
|
printf("%02ld%03ld%s%08X%s", diffsec, diffusec/1000, vdl, cid & CAN_EFF_MASK, vdl);
|
||||||
|
else if (print_eff)
|
||||||
|
printf("%02ld%03ld%s---- %03X%s", diffsec, diffusec/1000, vdl, cid & CAN_SFF_MASK, vdl);
|
||||||
|
else
|
||||||
|
printf("%02ld%03ld%s%03X%s", diffsec, diffusec/1000, ldl, cid & CAN_SFF_MASK, ldl);
|
||||||
|
|
||||||
if (binary) {
|
if (binary) {
|
||||||
|
for (i = 0; i < sniftab[slot].current.can_dlc; i++) {
|
||||||
for (i=0; i<sniftab[id].current.can_dlc; i++) {
|
|
||||||
for (j=7; j >= 0; j--) {
|
for (j=7; j >= 0; j--) {
|
||||||
if ((color) && (sniftab[id].marker.data[i] & 1<<j) &&
|
if ((color) && (sniftab[slot].marker.data[i] & 1<<j) &&
|
||||||
(!(sniftab[id].notch.data[i] & 1<<j)))
|
(!(sniftab[slot].notch.data[i] & 1<<j)))
|
||||||
if (sniftab[id].current.data[i] & 1<<j)
|
if (sniftab[slot].current.data[i] & 1<<j)
|
||||||
printf("%s1%s", ATTCOLOR, ATTRESET);
|
printf("%s1%s", ATTCOLOR, ATTRESET);
|
||||||
else
|
else
|
||||||
printf("%s0%s", ATTCOLOR, ATTRESET);
|
printf("%s0%s", ATTCOLOR, ATTRESET);
|
||||||
else
|
else
|
||||||
if (sniftab[id].current.data[i] & 1<<j)
|
if (sniftab[slot].current.data[i] & 1<<j)
|
||||||
putchar('1');
|
putchar('1');
|
||||||
else
|
else
|
||||||
putchar('0');
|
putchar('0');
|
||||||
|
|
@ -644,23 +734,22 @@ void print_snifline(int id){
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
|
for (i = 0; i < sniftab[slot].current.can_dlc; i++)
|
||||||
for (i=0; i<sniftab[id].current.can_dlc; i++)
|
if ((color) && (sniftab[slot].marker.data[i] & ~sniftab[slot].notch.data[i]))
|
||||||
if ((color) && (sniftab[id].marker.data[i] & ~sniftab[id].notch.data[i]))
|
printf("%s%02X%s ", ATTCOLOR, sniftab[slot].current.data[i], ATTRESET);
|
||||||
printf("%s%02X%s ", ATTCOLOR, sniftab[id].current.data[i], ATTRESET);
|
|
||||||
else
|
else
|
||||||
printf("%02X ", sniftab[id].current.data[i]);
|
printf("%02X ", sniftab[slot].current.data[i]);
|
||||||
|
|
||||||
if (sniftab[id].current.can_dlc < 8)
|
if (sniftab[slot].current.can_dlc < 8)
|
||||||
printf("%*s", (8 - sniftab[id].current.can_dlc) * 3, "");
|
printf("%*s", (8 - sniftab[slot].current.can_dlc) * 3, "");
|
||||||
|
|
||||||
for (i=0; i<sniftab[id].current.can_dlc; i++)
|
for (i = 0; i<sniftab[slot].current.can_dlc; i++)
|
||||||
if ((sniftab[id].current.data[i] > 0x1F) &&
|
if ((sniftab[slot].current.data[i] > 0x1F) &&
|
||||||
(sniftab[id].current.data[i] < 0x7F))
|
(sniftab[slot].current.data[i] < 0x7F))
|
||||||
if ((color) && (sniftab[id].marker.data[i] & ~sniftab[id].notch.data[i]))
|
if ((color) && (sniftab[slot].marker.data[i] & ~sniftab[slot].notch.data[i]))
|
||||||
printf("%s%c%s", ATTCOLOR, sniftab[id].current.data[i], ATTRESET);
|
printf("%s%c%s", ATTCOLOR, sniftab[slot].current.data[i], ATTRESET);
|
||||||
else
|
else
|
||||||
putchar(sniftab[id].current.data[i]);
|
putchar(sniftab[slot].current.data[i]);
|
||||||
else
|
else
|
||||||
putchar('.');
|
putchar('.');
|
||||||
|
|
||||||
|
|
@ -674,25 +763,23 @@ void print_snifline(int id){
|
||||||
|
|
||||||
putchar('\n');
|
putchar('\n');
|
||||||
|
|
||||||
memset(&sniftab[id].marker.data, 0, 8);
|
memset(&sniftab[slot].marker.data, 0, 8);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
void writesettings(char* name)
|
||||||
void writesettings(char* name){
|
{
|
||||||
|
|
||||||
int fd;
|
int fd;
|
||||||
char fname[30] = SETFNAME;
|
char fname[30] = SETFNAME;
|
||||||
int i,j;
|
int i,j;
|
||||||
char buf[8]= {0};
|
char buf[13]= {0};
|
||||||
|
|
||||||
strncat(fname, name, 29 - strlen(fname));
|
strncat(fname, name, 29 - strlen(fname));
|
||||||
fd = open(fname, O_WRONLY|O_CREAT, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH);
|
fd = open(fname, O_WRONLY|O_CREAT, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH);
|
||||||
|
|
||||||
if (fd > 0) {
|
if (fd > 0) {
|
||||||
|
for (i = 0; i < idx ;i++) {
|
||||||
for (i=0; i < 2048 ;i++) {
|
sprintf(buf, "<%08X>%c.", sniftab[i].current.can_id, (is_set(i, ENABLE))?'1':'0');
|
||||||
sprintf(buf, "<%03X>%c.", i, (is_set(i, ENABLE))?'1':'0');
|
if (write(fd, buf, 12) < 0)
|
||||||
if (write(fd, buf, 7) < 0)
|
|
||||||
perror("write");
|
perror("write");
|
||||||
for (j = 0; j < 8 ; j++) {
|
for (j = 0; j < 8 ; j++) {
|
||||||
sprintf(buf, "%02X", sniftab[i].notch.data[j]);
|
sprintf(buf, "%02X", sniftab[i].notch.data[j]);
|
||||||
|
|
@ -701,7 +788,7 @@ void writesettings(char* name){
|
||||||
}
|
}
|
||||||
if (write(fd, "\n", 1) < 0)
|
if (write(fd, "\n", 1) < 0)
|
||||||
perror("write");
|
perror("write");
|
||||||
/* 7 + 16 + 1 = 24 bytes per entry */
|
/* 12 + 16 + 1 = 29 bytes per entry */
|
||||||
}
|
}
|
||||||
close(fd);
|
close(fd);
|
||||||
}
|
}
|
||||||
|
|
@ -709,53 +796,57 @@ void writesettings(char* name){
|
||||||
printf("unable to write setting file '%s'!\n", fname);
|
printf("unable to write setting file '%s'!\n", fname);
|
||||||
};
|
};
|
||||||
|
|
||||||
void readsettings(char* name, int sockfd){
|
int readsettings(char* name)
|
||||||
|
{
|
||||||
int fd;
|
int fd;
|
||||||
char fname[30] = SETFNAME;
|
char fname[30] = SETFNAME;
|
||||||
char buf[25] = {0};
|
char buf[25] = {0};
|
||||||
int i,j;
|
int j;
|
||||||
|
bool done = false;
|
||||||
|
|
||||||
strncat(fname, name, 29 - strlen(fname));
|
strncat(fname, name, 29 - strlen(fname));
|
||||||
fd = open(fname, O_RDONLY);
|
fd = open(fname, O_RDONLY);
|
||||||
|
|
||||||
if (fd > 0) {
|
if (fd > 0) {
|
||||||
if (!sockfd)
|
idx = 0;
|
||||||
printf("reading setting file '%s' ... ", fname);
|
while (!done) {
|
||||||
|
if (read(fd, &buf, 29) == 29) {
|
||||||
|
unsigned long id = strtoul(&buf[1], (char **)NULL, 16);
|
||||||
|
|
||||||
for (i=0; i < 2048 ;i++) {
|
sniftab[idx].current.can_id = id;
|
||||||
if (read(fd, &buf, 24) == 24) {
|
|
||||||
if (buf[5] & 1) {
|
if (buf[10] & 1)
|
||||||
if (is_clr(i, ENABLE)) {
|
do_set(idx, ENABLE);
|
||||||
do_set(i, ENABLE);
|
else
|
||||||
if (sockfd)
|
do_clr(idx, ENABLE);
|
||||||
rx_setup(sockfd, i);
|
|
||||||
|
for (j = 7; j >= 0 ; j--) {
|
||||||
|
sniftab[idx].notch.data[j] =
|
||||||
|
(__u8) strtoul(&buf[2*j+12], (char **)NULL, 16) & 0xFF;
|
||||||
|
buf[2*j+12] = 0; /* cut off each time */
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (++idx >= MAX_SLOTS)
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
if (is_set(i, ENABLE)) {
|
done = true;
|
||||||
do_clr(i, ENABLE);
|
|
||||||
if (sockfd)
|
|
||||||
rx_delete(sockfd, i);
|
|
||||||
}
|
}
|
||||||
for (j=7; j>=0 ; j--){
|
|
||||||
sniftab[i].notch.data[j] =
|
|
||||||
(__u8) strtoul(&buf[2*j+7], (char **)NULL, 16) & 0xFF;
|
|
||||||
buf[2*j+7] = 0; /* cut off each time */
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
if (!sockfd)
|
|
||||||
printf("was only able to read until index %d from setting file '%s'!\n",
|
|
||||||
i, fname);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!sockfd)
|
|
||||||
printf("done\n");
|
|
||||||
|
|
||||||
close(fd);
|
close(fd);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
printf("unable to read setting file '%s'!\n", fname);
|
return -1;
|
||||||
|
|
||||||
|
return idx;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
int sniftab_index(canid_t id)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
|
||||||
|
for (i = 0; i <= idx; i++)
|
||||||
|
if (id == sniftab[i].current.can_id)
|
||||||
|
return i;
|
||||||
|
|
||||||
|
return -1; /* No match */
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue