Formatting, Cleanup, & Suggested Changes

Corrected formatting, removed old commented code, made suggested code changes.

Update README.md
pull/88/head
Cory 2018-08-06 20:21:40 -04:00 committed by Cory
parent 384ed8d919
commit 042038fb52
2 changed files with 34 additions and 66 deletions

View File

@ -13,7 +13,7 @@ subsystem (aka SocketCAN):
* canplayer : replay CAN logfiles * canplayer : replay CAN logfiles
* cansend : send a single frame * cansend : send a single frame
* cangen : generate (random) CAN traffic * cangen : generate (random) CAN traffic
* cansniffer : display CAN data content differences ~~(just 11bit CAN IDs)~~ * cansniffer : display CAN data content differences
#### CAN access via IP sockets #### CAN access via IP sockets
* canlogserver : log CAN frames from a remote/local host * canlogserver : log CAN frames from a remote/local host

View File

@ -106,9 +106,9 @@ struct snif {
struct can_frame notch; struct can_frame notch;
} sniftab[MAX_SLOTS]; } 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 notch; static int notch;
@ -124,23 +124,23 @@ void print_snifline(int slot);
int handle_keyb(int fd); int handle_keyb(int fd);
int handle_frame(int fd, long currcms); int handle_frame(int fd, long currcms);
int handle_timeo(int fd, long currcms); int handle_timeo(int fd, long currcms);
int sniftab_index(canid_t id);
void writesettings(char* name); void writesettings(char* name);
void readsettings(char* name, int sockfd); void readsettings(char* name, int sockfd);
static int sniftab_index(canid_t id);
int idx = 0;
int comp (const void * elem1, const void * elem2) int comp (const void * elem1, const void * elem2)
{ {
unsigned long f = ((struct snif*)elem1)->current.can_id; unsigned long f = ((struct snif*)elem1)->current.can_id;
unsigned long s = ((struct snif*)elem2)->current.can_id; unsigned long s = ((struct snif*)elem2)->current.can_id;
if (f > s) return 1;
if (f < s) return -1; if (f > s)
return 1;
if (f < s)
return -1;
return 0; return 0;
} }
void print_usage(char *prg) void print_usage(char *prg)
{ {
const char manual [] = { const char manual [] = {
@ -164,20 +164,10 @@ void print_usage(char *prg)
"29 bit IDs:\n" "29 bit IDs:\n"
"+18FEDF55<ENTER> - add CAN-ID 0x18FEDF55\n" "+18FEDF55<ENTER> - add CAN-ID 0x18FEDF55\n"
"-00000090<ENTER> - remove CAN-ID 0x00000090\n" "-00000090<ENTER> - remove CAN-ID 0x00000090\n"
/* "-42E7FF<ENTER> - remove CAN-ID 0x42E (using Bitmask)\N"*/
/* "-500700<ENTER> - remove CAN-IDs 0x500 - 0x5FF\n"*/
/* "+400600<ENTER> - add CAN-IDs 0x400 - 0x5FF\n"*/
/* "+000000<ENTER> - add all CAN-IDs\n"*/
/* "-000000<ENTER> - remove all CAN-IDs\n"*/
/* "\n"*/
/* "if (id & filter) == (sniff-id & filter) the action (+/-) IS PERFORMED,\N" */
/* "which is quite easy when the filter is 000\n"*/
"\n" "\n"
}; };
fprintf(stderr, "\nUsage: %s [can-interface]\n", prg); fprintf(stderr, "\nUsage: %s [can-interface]\n", prg);
// fprintf(stderr, "Options: -m <mask> (initial FILTER default 0x00000000)\n");
// fprintf(stderr, " -v <value> (initial FILTER default 0x00000000)\n");
fprintf(stderr, "Options: -q (quiet - all slots are deactivated)\n"); fprintf(stderr, "Options: -q (quiet - all slots are deactivated)\n");
fprintf(stderr, " -r <name> (read %sname from file)\n", SETFNAME); fprintf(stderr, " -r <name> (read %sname from file)\n", SETFNAME);
fprintf(stderr, " -b (start with binary mode)\n"); fprintf(stderr, " -b (start with binary mode)\n");
@ -200,8 +190,6 @@ 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;
@ -211,7 +199,6 @@ int main(int argc, char **argv)
struct ifreq ifr; 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);
@ -221,14 +208,6 @@ int main(int argc, char **argv)
while ((opt = getopt(argc, argv, "m:v:r:t:h:l:qbBcf?")) != -1) { while ((opt = getopt(argc, argv, "m:v:r:t:h:l:qbBcf?")) != -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); readsettings(optarg, 0);
break; break;
@ -281,16 +260,6 @@ int main(int argc, char **argv)
for (i = 0; i < MAX_SLOTS; i++) for (i = 0; i < MAX_SLOTS; i++)
do_clr(i, ENABLE); do_clr(i, ENABLE);
// Change this to do socket filtering
// else if (mask || value) {
// for (i=0; i < MAX_SLOTS ;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]);
return 1; return 1;
@ -362,7 +331,7 @@ int main(int argc, char **argv)
return 0; return 0;
} }
int handle_keyb(int fd){ int handle_keyb(int fd) {
char cmd [20] = {0}; char cmd [20] = {0};
int i; int i;
@ -384,7 +353,7 @@ int handle_keyb(int fd){
i = sniftab_index(value); i = sniftab_index(value);
if(i < 0) if (i < 0)
break; /* No Match */ break; /* No Match */
if (cmd[0] == '+') { if (cmd[0] == '+') {
@ -451,7 +420,7 @@ int handle_keyb(int fd){
return 1; /* ok */ return 1; /* ok */
}; };
int handle_frame(int fd, long currcms){ int handle_frame(int fd, long currcms) {
bool match = false; bool match = false;
bool rx_changed = false; bool rx_changed = false;
@ -469,19 +438,19 @@ int handle_frame(int fd, long currcms){
pos = sniftab_index(id); pos = sniftab_index(id);
if(pos >= 0) if (pos >= 0)
match = true; match = true;
if(!match){ if (!match) {
if(idx < MAX_SLOTS){ if (idx < MAX_SLOTS) {
pos = idx++; pos = idx++;
rx_changed = true; rx_changed = true;
} }
} }
else { else {
if(frame.can_dlc == sniftab[pos].current.can_dlc) if (frame.can_dlc == sniftab[pos].current.can_dlc)
for(i=0; i<frame.can_dlc; i++){ for (i=0; i<frame.can_dlc; i++) {
if(frame.data[i] != sniftab[pos].current.data[i] ){ if (frame.data[i] != sniftab[pos].current.data[i] ) {
rx_changed = true; rx_changed = true;
break; break;
} }
@ -495,7 +464,7 @@ int handle_frame(int fd, long currcms){
return 0; /* quit */ return 0; /* quit */
} }
if(rx_changed == true){ if (rx_changed == true) {
ioctl(fd, SIOCGSTAMP, &sniftab[pos].currstamp); ioctl(fd, SIOCGSTAMP, &sniftab[pos].currstamp);
sniftab[pos].current = frame; sniftab[pos].current = frame;
@ -516,7 +485,7 @@ int handle_frame(int fd, long currcms){
return 1; /* ok */ return 1; /* ok */
}; };
int handle_timeo(int fd, long currcms){ int handle_timeo(int fd, long currcms) {
int i, j; int i, j;
int force_redraw = 0; int force_redraw = 0;
@ -549,7 +518,7 @@ int handle_timeo(int fd, long currcms){
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);
@ -578,7 +547,7 @@ int handle_timeo(int fd, long currcms){
}; };
void print_snifline(int slot){ void print_snifline(int slot) {
long diffsec = sniftab[slot].currstamp.tv_sec - sniftab[slot].laststamp.tv_sec; long diffsec = sniftab[slot].currstamp.tv_sec - sniftab[slot].laststamp.tv_sec;
long diffusec = sniftab[slot].currstamp.tv_usec - sniftab[slot].laststamp.tv_usec; long diffusec = sniftab[slot].currstamp.tv_usec - sniftab[slot].laststamp.tv_usec;
@ -595,7 +564,7 @@ void print_snifline(int slot){
if (diffsec > 10) if (diffsec > 10)
diffsec = 9, diffusec = 999999; diffsec = 9, diffusec = 999999;
if(id & CAN_EFF_FLAG) if (id & CAN_EFF_FLAG)
printf("%ld.%06ld EXT 0x%08lX ", diffsec, diffusec, (id & CAN_EFF_MASK)); printf("%ld.%06ld EXT 0x%08lX ", diffsec, diffusec, (id & CAN_EFF_MASK));
else else
printf("%ld.%06ld STD 0x%03lX ", diffsec, diffusec, id); printf("%ld.%06ld STD 0x%03lX ", diffsec, diffusec, id);
@ -664,7 +633,7 @@ void print_snifline(int slot){
memset(&sniftab[slot].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;
@ -678,7 +647,7 @@ void writesettings(char* name){
for (i=0; i < idx; i++) { for (i=0; i < idx; i++) {
sprintf(buf, "<%08X>%c.", sniftab[i].current.can_id, (is_set(i, ENABLE))?'1':'0'); sprintf(buf, "<%08X>%c.", sniftab[i].current.can_id, (is_set(i, ENABLE))?'1':'0');
write(fd, buf, 12); write(fd, buf, 12);
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]);
write(fd, buf, 2); write(fd, buf, 2);
} }
@ -691,7 +660,7 @@ 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){ void readsettings(char* name, int sockfd) {
int fd; int fd;
char fname[30] = SETFNAME; char fname[30] = SETFNAME;
@ -706,13 +675,13 @@ void readsettings(char* name, int sockfd){
if (!sockfd) if (!sockfd)
printf("reading setting file '%s' ... ", fname); printf("reading setting file '%s' ... ", fname);
while(!done){ while (!done) {
if (read(fd, &buf, 29) == 29) { if (read(fd, &buf, 29) == 29) {
unsigned long id = strtoul(&buf[1], (char **)NULL, 16); unsigned long id = strtoul(&buf[1], (char **)NULL, 16);
pos = sniftab_index(id); pos = sniftab_index(id);
if(pos < 0){ if (pos < 0) {
if(idx < MAX_SLOTS){ if (idx < MAX_SLOTS) {
pos = idx++; pos = idx++;
sniftab[pos].current.can_id = id; sniftab[pos].current.can_id = id;
} }
@ -728,12 +697,11 @@ void readsettings(char* name, int sockfd){
if (is_set(pos, ENABLE)) { if (is_set(pos, ENABLE)) {
do_clr(pos, ENABLE); do_clr(pos, ENABLE);
} }
for (j=7; j>=0 ; j--){ for (j=7; j>=0 ; j--) {
sniftab[pos].notch.data[j] = sniftab[pos].notch.data[j] =
(__u8) strtoul(&buf[2*j+12], (char **)NULL, 16) & 0xFF; (__u8) strtoul(&buf[2*j+12], (char **)NULL, 16) & 0xFF;
buf[2*j+12] = 0; /* cut off each time */ buf[2*j+12] = 0; /* cut off each time */
} }
} }
else { else {
if (!sockfd) if (!sockfd)
@ -752,12 +720,12 @@ void readsettings(char* name, int sockfd){
printf("unable to read setting file '%s'!\n", fname); printf("unable to read setting file '%s'!\n", fname);
}; };
int sniftab_index(canid_t id){ static int sniftab_index(canid_t id) {
int i; int i;
for(i = 0; i <= idx; i++) for (i = 0; i <= idx; i++)
if(id == sniftab[i].current.can_id) if (id == sniftab[i].current.can_id)
return i; return i;
return -1; /* No match */ return -1; /* No match */