📄 c_can_testtool.c
字号:
/*-------------INCLUDE FILES------------------*/#include <stdio.h>#include <errno.h>#include <string.h>#include <fcntl.h>#include <termios.h>#include <sys/time.h>#include <unistd.h>#include <stdlib.h>//#include <unistd.h>//#include <sys/ioctl.h>#include "../include/can.h"/*-------------TYPE DEFINITIONS---------------*//* represents an opened can device */struct can_device_t { char *devname; int filedesc; char flags; void * sub_dev_tsk;};#define CANDEV_UNCONF 1 /* is unconfigured */#define CANDEV_READDEV 2 /* is an read device */#define CANDEV_READTSK 4 /* has a running read task */#define CANDEV_WRITTSK 8 /* has a running write task *//* represents a device configured for reception*/struct read_device_t { struct can_device_t *can_device; unsigned long id; char usemask; unsigned long mask; struct read_task_t *read_task;};/* represents a runing cyclic read task */struct read_task_t { struct read_device_t *read_device; struct canmsg_t *canmsg_buff; unsigned int cnt_msgs; unsigned int cycle_time; unsigned int max_reps; unsigned int nr_reps;};/* represents a runing cyclic write task */struct write_task_t { struct can_device_t *can_device; struct canmsg_t *canmsg; unsigned int cycle_time; unsigned int max_reps; unsigned int nr_reps;};/* linked object list member */struct object_list_t { void *object; /* pointer to object */ struct object_list_t *next; /* pointer to next list element */};/*-------------VARIABLE DEFINITIONS-----------*//* heads of linked lists */struct object_list_t *open_dev_head;struct object_list_t *read_dev_head;struct object_list_t *read_tsk_head;struct object_list_t *writ_tsk_head;/* Use this variable to remember original terminal attributes. */struct termios saved_attributes;/*-------------FUNCTION PROTOTYPES------------*/int open_candevice ( char *dev_filename );int close_candevice ( struct can_device_t *can_device);int add_readdevice ( struct can_device_t *can_device, unsigned long id, char usemask, unsigned long mask );int rem_readdevice ( struct can_device_t *can_device );int log_messages ( struct canmsg_t *canmsgs, int msgcnt, FILE *stream, char *begofline );int send_messages ( struct can_device_t * can_device );int stop_tasks ( struct can_device_t *can_device );struct can_device_t * select_candev ( void );struct read_device_t * select_readdev ( void );int print_commands( void );int print_state ( void );int init( void );struct object_list_t * find_in_objlst ( void *entry, struct object_list_t *listhead );int rm_lstmem ( struct object_list_t *member, struct object_list_t *listhead );int rm_lstmempnt ( void *entry, struct object_list_t *listhead );void reset_input_mode (void);void set_input_mode (void);/* * Main function */int main(int argc, char **argv){ int ret, msgnr = 0; char cmd_letter; char *dev_filename, *log_filename; struct object_list_t *tmp_listmem; struct can_device_t *can_device; struct read_device_t *read_device; unsigned long id = 0, mask = 0; char usemask = 0, ocflag, sfflag; struct canmsg_t *canmsgs; if ( ( ret = init() ) < 0 ) return ret; printf("\n\nEnter command (spacebar to show commands):"); fflush( NULL ); set_input_mode(); read (STDIN_FILENO, &cmd_letter, 1); reset_input_mode(); while ( 1 ) { switch (cmd_letter) { case 'o': { printf("\n\n ------Open-CAN-device-------------------------------------------------\n"); printf(" Enter device file name (e.g. /dev/cana0): "); dev_filename = NULL; scanf("%30as", &dev_filename); open_candevice( dev_filename ); /* release allocated buffer */ printf(" ----------------------------------------------------------------------\n"); break; } case 'c': { printf("\n\n ------Close-CAN-device------------------------------------------------\n"); if ( (can_device = select_candev()) != NULL ) { close_candevice( can_device ); } printf(" ----------------------------------------------------------------------\n"); break; } case 'i': { printf("\n\n ------Configure-message-reception-------------------------------------\n"); if ( (can_device = select_candev()) != NULL ) { printf(" ID (hex) : "); fflush( NULL); scanf("%lx", &id); while (getchar() != '\n') ; /* flush input buffer */ printf(" use mask filtering (y/n) : "); fflush( NULL); usemask = getchar(); while (getchar() != '\n') ; /* flush input buffer */ if ( usemask == 'y' ) { usemask = 1; printf(" Mask (hex) : "); fflush( NULL); scanf("%lx", &mask); } else { usemask = 0; mask = 0; } printf("\n"); add_readdevice ( can_device, id, usemask, mask ); } printf(" ----------------------------------------------------------------------\n"); break; } case 'r': { printf("\n\n ------Read-messages-from-CAN-device-----------------------------------\n"); if ( (read_device = select_readdev()) != NULL ) { printf(" How many messages : "); fflush( NULL); scanf("%d", &msgnr); while (getchar() != '\n') ; /* flush input buffer */ printf(" Once or cyclic (o/c) : "); fflush( NULL); ocflag = getchar(); while (getchar() != '\n') ; /* flush input buffer */ if ( ocflag == 'c' ) { /* start cyclic read task */ printf(" CYCLIC READING NOT SUPPORTED YET !!!\n"); } else { /* allocate storage buffer for can messages */ canmsgs = (struct canmsg_t *) malloc( sizeof(struct canmsg_t) * msgnr ); if ( canmsgs == NULL ) { printf(" ERROR: COULDN'T RESERVE MEMORY FOR MESSAGE BUFFER\n"); break; } /* read given number of messages */ printf(" Trying to read %d can messages from %s : ", msgnr, read_device->can_device->devname); ret = read( read_device->can_device->filedesc, canmsgs, sizeof( struct canmsg_t ) * msgnr); if ( ret < 0 ) { if ( errno == EAGAIN ) { printf("no messages in buffer\n", ret); } else { printf("error (%s)\n", strerror(errno) ); } } else { ret /= sizeof(struct canmsg_t); printf(" ok, read %d\n", ret); printf(" Log to stdout or to file ? (s/f) : "); sfflag = getchar(); while (getchar() != '\n') ; /* flush input buffer */ if ( sfflag == 'f' ) { /* log to file */ printf(" Enter log file name : "); log_filename = NULL; scanf("%30as", &log_filename); log_messages ( canmsgs, ret, fopen(log_filename, "w"), ""); /* release allocated buffer */ free( log_filename ); } else { /* log to stdout */ log_messages ( canmsgs, ret, stdout, " "); } } free( canmsgs ); } } printf(" ----------------------------------------------------------------------\n"); break; } case 'w': { printf("\n\n ------Send-messages---------------------------------------------------\n"); /* option to start cyclic write task is still missing */ if ( (can_device = select_candev()) != NULL ) { send_messages( can_device ); } printf(" ----------------------------------------------------------------------\n"); break; } case 'p': { printf("\n\n ------State-of-currently-opened-devices-------------------------------\n"); print_state(); printf(" ----------------------------------------------------------------------\n"); break; } case ' ': { printf("\n\n ------Available-commands----------------------------------------------\n"); print_commands(); printf(" ----------------------------------------------------------------------\n"); break; } case 'q': { /* close all opened devices */ printf("\n\n ------Leaving-program-------------------------------------------------\n"); tmp_listmem = open_dev_head->next; if ( tmp_listmem != open_dev_head ) { printf(" Closing open can devices\n"); } while ( tmp_listmem != open_dev_head ) { close_candevice( (struct can_device_t *)tmp_listmem->object ); tmp_listmem = open_dev_head->next; } printf(" ------Bye!!!----------------------------------------------------------\n\n"); return 0; break; } default: ; } printf("\n\nEnter command (spacebar to show commands):"); fflush( NULL ); set_input_mode(); read (STDIN_FILENO, &cmd_letter, 1); reset_input_mode(); } return 0;}/* * Opens a can device file given by the user. */int open_candevice ( char *dev_filename ) { int fd; struct object_list_t *new_listmem; printf(" Try to open %s : ", dev_filename); if ((fd=open(dev_filename, O_RDWR | O_NONBLOCK)) < 0) { printf("error (%s)\n", strerror(errno) ); return fd; } printf(" ok\n"); /* add can_device to list */ new_listmem = (struct object_list_t *) malloc(sizeof (struct object_list_t)); if ( new_listmem == NULL ) { printf(" Error: couldn't allocate memory\n"); return ENOMEM; } new_listmem->object = (void *) malloc( sizeof (struct can_device_t));
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -