📄 c_can_testtool.c
字号:
tmp_listmem = open_dev_head->next; cnt = 0; if ( tmp_listmem != open_dev_head ) { printf(" Opened devices:\n"); } while ( tmp_listmem != open_dev_head ) { cnt++; printf(" %3u - %s\n", cnt, ((struct can_device_t *)tmp_listmem->object)->devname ); tmp_listmem = tmp_listmem->next; } if ( cnt == 0 ) { printf(" No open can devices found.\n"); return NULL; } else { printf(" Choose device: "); fflush( NULL ); scanf("%i", &nr); while (getchar() != '\n') ; /* flush input buffer */ if ( nr < 1 || nr > cnt ) { printf("\n not in list\n"); return NULL; } else { for (cnt=0; cnt<nr; cnt++) { tmp_listmem = tmp_listmem->next; } return (struct can_device_t *)tmp_listmem->object; } }}/* * Lets user select one of the cobnfigured read devices */struct read_device_t * select_readdev ( void ) { int nr, cnt; struct object_list_t *tmp_listmem; tmp_listmem = read_dev_head->next; cnt = 0; if ( tmp_listmem != read_dev_head ) { printf(" Read devices:\n"); } while ( tmp_listmem != read_dev_head ) { cnt++; printf(" %3u - %s\n", cnt, ((struct read_device_t *)tmp_listmem->object)->can_device->devname ); tmp_listmem = tmp_listmem->next; } if ( cnt == 0 ) { printf(" No read devices found.\n"); return NULL; } else { printf(" Choose device: "); fflush( NULL ); scanf("%i", &nr); while (getchar() != '\n') ; /* flush input buffer */ if ( nr < 1 || nr > cnt ) { printf("\n not in list\n"); return NULL; } else { for (cnt=0; cnt<nr; cnt++) { tmp_listmem = tmp_listmem->next; } return (struct read_device_t *)tmp_listmem->object; } }}/* * Prints the members of all linked lists to standard out */int print_state (){ struct object_list_t *cur_obj = open_dev_head->next; char uc, rd, rt, wt, um; printf(" Opened devices:\n"); if ( open_dev_head->next == open_dev_head ) { printf(" No open devices found!\n"); } else { printf(" |Device name |uc|rd|rt|wt|\n"); while (cur_obj != open_dev_head ) { uc = (((struct can_device_t *)cur_obj->object)->flags & CANDEV_UNCONF ) ? '*' : ' '; rd = (((struct can_device_t *)cur_obj->object)->flags & CANDEV_READDEV ) ? '*' : ' '; rt = (((struct can_device_t *)cur_obj->object)->flags & CANDEV_READTSK ) ? '*' : ' '; wt = (((struct can_device_t *)cur_obj->object)->flags & CANDEV_WRITTSK ) ? '*' : ' '; printf(" |%-13s|%c |%c |%c |%c |\n", ((struct can_device_t *)cur_obj->object)->devname, uc, rd, rt, wt ); cur_obj = cur_obj->next; } } cur_obj = read_dev_head->next; printf("\n Devices configured to receive messages:\n"); if ( read_dev_head->next == read_dev_head ) { printf(" No read devices found!\n"); } else { printf(" |Device name |id |um|mask |\n"); while (cur_obj != read_dev_head ) { um = (((struct read_device_t *)cur_obj->object)->usemask ) ? '*' : ' '; printf(" |%-13s|0x%-3X|%c |0x%-3X|\n", ((struct read_device_t *)cur_obj->object)->can_device->devname, ((struct read_device_t *)cur_obj->object)->id, um, ((struct read_device_t *)cur_obj->object)->mask ); cur_obj = cur_obj->next; } } cur_obj = read_tsk_head->next; printf("\n Cyclic receive tasks:\n"); if ( read_tsk_head->next == read_tsk_head ) { printf(" No read tasks found!\n"); } else { printf(" |Device name |readblocksize|cycletime |maxreps |reps |\n"); while (cur_obj != read_tsk_head ) { printf(" |%-13s|%8d msgs|%8d ms|%8d|%8d|\n", ((struct read_task_t *)cur_obj->object)->read_device->can_device->devname, ((struct read_task_t *)cur_obj->object)->cnt_msgs, ((struct read_task_t *)cur_obj->object)->cycle_time, ((struct read_task_t *)cur_obj->object)->max_reps, ((struct read_task_t *)cur_obj->object)->nr_reps ); cur_obj = cur_obj->next; } } cur_obj = writ_tsk_head->next; printf("\n Cyclic send tasks:\n"); if ( writ_tsk_head->next == writ_tsk_head ) { printf(" No write tasks found!\n"); } else { printf(" |Device name |id |l|data (hex) |cycletime |maxreps |reps |\n"); while (cur_obj != writ_tsk_head ) { printf(" |%-13s|0x%-3X|%1d|%02X %02X %02X %02X %02X %02X %02X %02X |%8d ms|%8d|%8d|\n", ((struct write_task_t *)cur_obj->object)->can_device->devname, ((struct write_task_t *)cur_obj->object)->canmsg->id, ((struct write_task_t *)cur_obj->object)->canmsg->length, ((struct write_task_t *)cur_obj->object)->canmsg->data[0], ((struct write_task_t *)cur_obj->object)->canmsg->data[1], ((struct write_task_t *)cur_obj->object)->canmsg->data[2], ((struct write_task_t *)cur_obj->object)->canmsg->data[3], ((struct write_task_t *)cur_obj->object)->canmsg->data[4], ((struct write_task_t *)cur_obj->object)->canmsg->data[5], ((struct write_task_t *)cur_obj->object)->canmsg->data[6], ((struct write_task_t *)cur_obj->object)->canmsg->data[7], ((struct write_task_t *)cur_obj->object)->cycle_time, ((struct write_task_t *)cur_obj->object)->max_reps, ((struct write_task_t *)cur_obj->object)->nr_reps ); cur_obj = cur_obj->next; } } return 0;}/* * Prints supported commands to standard out */int print_commands (){ printf(" o - Open a can device.\n"); printf(" c - Close a can device.\n"); printf(" i - Configure an opened can device for message reception.\n"); printf(" r - Read received messages from read device.\n"); printf(" w - Write messages through an opened can device.\n"); printf(" p - Print state of currently opened devices.\n"); printf(" q - Exit c_can_testtool.\n"); return 0;}/* * Initializes memory structures. */int init(){ /* initialize linked lists */ open_dev_head = (struct object_list_t *) malloc(sizeof (struct object_list_t)); open_dev_head->next = open_dev_head; open_dev_head->object = open_dev_head; read_dev_head = (struct object_list_t *) malloc(sizeof (struct object_list_t)); read_dev_head->next = read_dev_head; read_dev_head->object = read_dev_head; read_tsk_head = (struct object_list_t *) malloc(sizeof (struct object_list_t)); read_tsk_head->next = read_tsk_head; read_tsk_head->object = read_tsk_head; writ_tsk_head = (struct object_list_t *) malloc(sizeof (struct object_list_t)); writ_tsk_head->next = writ_tsk_head; writ_tsk_head->object = writ_tsk_head; if (open_dev_head == NULL || read_dev_head == NULL || read_tsk_head == NULL || writ_tsk_head == NULL) { return ENOMEM; } return 0;}/* * Searches in given list for the member pointing to "entry". */struct object_list_t * find_in_objlst ( void * entry, struct object_list_t * listhead ){ struct object_list_t *temp_listmem = listhead->next; while ( temp_listmem->object != entry && temp_listmem != listhead) { temp_listmem = temp_listmem->next; } if ( temp_listmem->object != entry ) { /* not found in list */ return NULL; } else { return temp_listmem; }}/* * Removes given element from given list. */int rm_lstmem ( struct object_list_t * member, struct object_list_t * listhead ) { struct object_list_t *prev_listmem = listhead; /* tried to delete head */ if ( member == listhead ) { return -1; } /* find previous list element */ while ( prev_listmem->next != member && prev_listmem->next != listhead ) { prev_listmem = prev_listmem->next; } if ( prev_listmem->next == listhead ) { /* member not in list */ return -1; } /* remove from list */ free( (void *)member->object ); prev_listmem->next = member->next; if (member->next == listhead ) { listhead->object = prev_listmem; } free( (void *)member ); return 0;}/* * Removes element pointing to given address from given list. */int rm_lstmempnt ( void * entry, struct object_list_t * listhead ) { struct object_list_t * element; element = find_in_objlst( entry, listhead ); if ( element == NULL ) { /* element not in list, cannot be deleted */ return -1; } else { rm_lstmem( element, listhead ); return 0; }}/* * Reset terminal input settings to original values. */void reset_input_mode (void){ tcsetattr (STDIN_FILENO, TCSANOW, &saved_attributes);}/* * Set terminal input settings to noncanonical mode, * so that single characters can be read */void set_input_mode (void){ struct termios tattr; char *name; /* Make sure stdin is a terminal. */ if (!isatty (STDIN_FILENO)) { fprintf (stderr, "Not a terminal.\n"); //exit (EXIT_FAILURE); return; } /* Save the terminal attributes so we can restore them later. */ tcgetattr (STDIN_FILENO, &saved_attributes); atexit (reset_input_mode); /* Set the funny terminal modes. */ tcgetattr (STDIN_FILENO, &tattr); tattr.c_lflag &= ~(ICANON|ECHO); /* Clear ICANON and ECHO. */ tattr.c_cc[VMIN] = 1; tattr.c_cc[VTIME] = 0; tcsetattr (STDIN_FILENO, TCSAFLUSH, &tattr);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -