| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313 |
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include "stdbool.h"
- #include <pthread.h>
- #include "sys.h"
- #include "sys_log.h"
- #include <unistd.h>
- #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_I;
- 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("%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,0xf1,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
- };
- 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<sizeof(data_process_handler_list)/sizeof(HANDLER_T); i++)
- {
- //先找下这个id有没有在这个列表里面.如果在的话就放进去,如果不在的话,就重新找新的放入.
- if(data_process_handler_list[i].frame_id == frame.id)
- {
- LOG_D("find frame_id");
- find_frame_id_flag = 1;
- iso15765_enqueue(data_process_handler_list[i].process_handler, &frame);
- }
- }
- if(0 == find_frame_id_flag)
- {
- //这个frame_id不在之前发过的历史列表里面,那找个新的放进去.
- for(int j=0; j<sizeof(data_process_handler_list)/sizeof(HANDLER_T); j++)
- {
- if(UINT32_DEFAULT_VAL == data_process_handler_list[j].frame_id)
- {
- LOG_D("not find frame_id, now set %d", j);
- data_process_handler_list[j].frame_id = frame.id;
- iso15765_enqueue(data_process_handler_list[j].process_handler, &frame);
- break;
- }
- }
- }
- }
- else
- {
- send_index = 0;
- sleep(1);
- }
- }
- }
- void *iso15765_parse_task(void)
- {
- uint32_t at_cnt = 0;
- while(1)
- {
- for (int i=0; i<sizeof(data_process_handler_list)/sizeof(HANDLER_T); i++)
- {
- iso15765_process(data_process_handler_list[i].process_handler);
- }
- }
- }
- int main()
- {
- pthread_t rx_data_thread_handle, parse_thread_handle;
- sys_log_set_lvl(g_s_log_lvl);
- for (int i=0; i<sizeof(data_process_handler_list)/sizeof(HANDLER_T); i++)
- {
- iso15765_init(data_process_handler_list[i].process_handler);
- }
- if(pthread_create(&rx_data_thread_handle, NULL, (void*)rx_data_task, NULL)){
- LOG_E("create error");
- return -1;
- }
- if(pthread_create(&parse_thread_handle, NULL, (void*)iso15765_parse_task, NULL)){
- LOG_E("create error");
- return -1;
- }
- pthread_join(rx_data_thread_handle, NULL);
- pthread_join(parse_thread_handle, NULL);
- return 0;
- }
|