#include #include #include #include "stdbool.h" #include #include "sys.h" #include "sys_log.h" #include #include "time.h" #include "sys/time.h" #include "lib_iso15765.h" #define TASK_TEST 1 #define MULTI_FRAME_EN 1 #define UINT32_DEFAULT_VAL 0xFFFFFFFF static uint8_t g_s_log_lvl = LOG_LVL_D; static uint32_t GetTickCount() { struct timespec ts; clock_gettime(CLOCK_MONOTONIC, &ts); return (ts.tv_sec * 1000 + ts.tv_nsec / 1000000); } /****************************************************************************** * Declaration | Static Functions ******************************************************************************/ static uint8_t send_frame_via_can_0(cbus_id_type id_type, uint32_t id, cbus_fr_format fr_fmt, uint8_t dlc, uint8_t *dt); static void parse_finish_callback(n_indn_t *info); static void on_error(n_rslt err_type); static uint32_t getms(); /****************************************************************************** * Enumerations, structures & Variables ******************************************************************************/ static iso15765_t handler0 = { .addr_md = N_ADM_BMW, //N_ADM_EXTENDED, N_ADM_BMW .fr_id_type = CBUS_ID_T_STANDARD, .clbs.send_frame = send_frame_via_can_0, .clbs.on_error = on_error, .clbs.get_ms = getms, .clbs.indn = parse_finish_callback, //解析完一包调用这个. .config.stmin = 0x3, .config.bs = 0x0f, .config.n_bs = 100, .config.n_cr = 3 }; static iso15765_t handler1 = { .addr_md = N_ADM_BMW, .fr_id_type = CBUS_ID_T_STANDARD, .clbs.send_frame = send_frame_via_can_0, .clbs.on_error = on_error, .clbs.get_ms = getms, .clbs.indn = parse_finish_callback, //解析完一包调用这个. .config.stmin = 0x3, .config.bs = 0x0f, .config.n_bs = 100, .config.n_cr = 3 }; static iso15765_t handler2 = { .addr_md = N_ADM_BMW, .fr_id_type = CBUS_ID_T_STANDARD, .clbs.send_frame = send_frame_via_can_0, .clbs.on_error = on_error, .clbs.get_ms = getms, .clbs.indn = parse_finish_callback, //解析完一包调用这个. .config.stmin = 0x3, .config.bs = 0x0f, .config.n_bs = 100, .config.n_cr = 3 }; static iso15765_t handler3 = { .addr_md = N_ADM_BMW, .fr_id_type = CBUS_ID_T_STANDARD, .clbs.send_frame = send_frame_via_can_0, .clbs.on_error = on_error, .clbs.get_ms = getms, .clbs.indn = parse_finish_callback, //解析完一包调用这个. .config.stmin = 0x3, .config.bs = 0x0f, .config.n_bs = 100, .config.n_cr = 3 }; /****************************************************************************** * Definition | Static Functions ******************************************************************************/ uint16_t f_sz1 = 0x0c; uint16_t f_sz2 = 0x0c; static uint32_t getms() { return GetTickCount();; } static uint8_t send_frame_via_can_0(cbus_id_type id_type, uint32_t id, cbus_fr_format fr_fmt, uint8_t dlc, uint8_t *dt) { //暂时没看到啥用处. return 0; } static void on_error(n_rslt err_type) { LOG_E("ERROR OCCURED!:0x%04x", err_type); } typedef struct { uint32_t frame_id; iso15765_t* process_handler; }HANDLER_T; HANDLER_T data_process_handler_list[] = { { .frame_id = UINT32_DEFAULT_VAL, .process_handler = &handler0, }, { .frame_id = UINT32_DEFAULT_VAL, .process_handler = &handler1, }, { .frame_id = UINT32_DEFAULT_VAL, .process_handler = &handler2, }, { .frame_id = UINT32_DEFAULT_VAL, .process_handler = &handler3, }, }; static void parse_finish_callback(n_indn_t *info) { if(0 == info->rslt) { LOG_I("parse success.frame_type:0x%x,rslt:0x%x,frame_id:0x%x,msg_sz:0x%x,data:", info->frame_type, info->rslt, (info->n_ai.n_sa<<8)|(info->n_ai.n_ta), info->msg_sz); if(info->frame_type == N_PCI_T_SF) { for (int i = 0; i < info->msg_sz; i++) { printf("%02X", info->sf_msg[i]); } } else { for (int i = 0; i < info->msg_sz; i++) { printf("0x%02x,", info->msg[i]); } } printf("\r\n"); } else { LOG_E("parse fail.rslt:0x%x,frame_id:0x%x", info->rslt, (info->n_ai.n_sa<<8)|(info->n_ai.n_ta)); } } /****************************************************************************** * Definition | Public Functions ******************************************************************************/ // uint8_t send_buf[] = { // 0x06,0x12,0x05,0xdf,0x03,0x22,0xf1,0x01, // 0x06,0x12,0x08,0xf1,0x10,0x34,0x62,0xf1,0x01,0x01,0x01, // 0x06,0x12,0x08,0xf1,0x21,0x00,0x04,0x21,0x06,0x22,0x8f, // 0x06,0x12,0x08,0xf1,0x22,0x04,0xd2,0x01,0x00,0x00,0x10, // 0x06,0x12,0x08,0xf1,0x23,0x00,0x00,0x00,0x01,0x00,0x00, // 0x06,0x12,0x08,0xf1,0x24,0x3c,0xfb,0x04,0x00,0x00,0x06, // 0x06,0x12,0x08,0xf1,0x25,0x00,0x00,0x3c,0xfc,0x04,0x00, // 0x06,0x60,0x08,0xf1,0x10,0x34,0x62,0xf1,0x01,0x01,0x01, // 0x06,0x12,0x08,0xf1,0x26,0x00,0x08,0x00,0x00,0x34,0xec, // 0x06,0x12,0x08,0xf1,0x27,0x0d,0x00,0x00,0x08,0x00,0x00, // 0x06,0x60,0x08,0xf1,0x21,0x00,0x04,0x21,0x06,0x06,0x8f, // 0x06,0x12,0x08,0xf1,0x28,0x43,0x4e,0x0d,0x00,0x01,0xaa, // 0x06,0x60,0x08,0xf1,0x22,0x04,0xd2,0x01,0x00,0x00,0x10, // 0x06,0x60,0x08,0xf1,0x23,0x00,0x00,0x00,0x01,0x00,0x00, // 0x06,0x60,0x08,0xf1,0x24,0x36,0x15,0x01,0x02,0x00,0x06, // 0x06,0x29,0x08,0xf1,0x10,0x2c,0x62,0xf1,0x01,0x01,0x01, // 0x06,0x60,0x08,0xf1,0x25,0x00,0x00,0x36,0x16,0x01,0x02, // 0x06,0x60,0x08,0xf1,0x26,0x01,0x08,0x00,0x00,0x36,0x17, // 0x06,0x29,0x08,0xf1,0x21,0x00,0x03,0x21,0x06,0x06,0x8f, // 0x06,0x60,0x08,0xf1,0x27,0x01,0x07,0x00,0x05,0x00,0x00, // 0x06,0x60,0x08,0xf1,0x28,0x36,0x18,0x01,0x03,0x0f,0xff, // 0x06,0x29,0x08,0xf1,0x22,0x04,0xd2,0x01,0x00,0x00,0x10, // 0x06,0x29,0x08,0xf1,0x03,0x7f,0x22,0x78,0xff,0xff,0xff, //连续帧中间来了个单帧. // 0x06,0x29,0x08,0xf1,0x23,0x00,0x00,0x00,0x01,0x00,0x00, // 0x06,0x29,0x08,0xf1,0x24,0x38,0x72,0x00,0x02,0x00,0x06, // 0x06,0x29,0x08,0xf1,0x25,0x00,0x00,0x38,0x73,0x00,0x03, // 0x06,0x29,0x08,0xf1,0x26,0x02,0x08,0x00,0x00,0x38,0x74, // 0x06,0x29,0x08,0xf1,0x27,0x01,0x01,0x00,0xff,0xff,0xff // }; uint8_t send_buf[] = { 0x06,0x12,0x08,0xf1,0x10,0x34,0x62,0xf1,0x01,0x01,0x01, //中间省略一个流控帧. 0x06,0x12,0x08,0xf1,0x21,0x00,0x04,0x21,0x06,0x22,0x8f, 0x06,0x12,0x08,0xf1,0x22,0x04,0xd2,0x01,0x00,0x00,0x10, 0x06,0x12,0x08,0xf1,0x23,0x00,0x00,0x00,0x01,0x00,0x00, 0x06,0x12,0x08,0xf1,0x24,0x3c,0xfb,0x04,0x00,0x00,0x06, 0x06,0x12,0x08,0xf1,0x25,0x00,0x00,0x3c,0xfc,0x04,0x00, 0x06,0x12,0x08,0xf1,0x26,0x00,0x08,0x00,0x00,0x34,0xec, 0x06,0x12,0x08,0xf1,0x27,0x0d,0x00,0x00,0x08,0x00,0x00, 0x06,0x12,0x08,0xf1,0x28,0x43,0x4e,0x0d,0x00,0x01,0xaa, }; void *rx_data_task(void) { uint32_t test_cnt = 0; uint32_t send_len = sizeof(send_buf); canbus_frame_t frame; uint32_t send_index = 0; while(1) { if(send_index < send_len) { if(send_index==0) { printf("begin parse\r\n"); } frame.id = MAKEWORD(send_buf[send_index], send_buf[send_index+1]); //帧id frame.fr_format = CBUS_FR_FRM_STD; //帧格式 frame.id_type = CBUS_ID_T_STANDARD; //帧类型 frame.dlc = send_buf[send_index+2]; //帧长度 memcpy(&frame.dt, &send_buf[send_index+3], frame.dlc); //帧数据 send_index += 3; send_index += frame.dlc; LOG_D("frame.id:0x%x", frame.id); //开始往队列中放入. uint8_t find_frame_id_flag = 0; for(int i=0; i