main.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include "stdbool.h"
  5. #include <pthread.h>
  6. #include "sys.h"
  7. #include "sys_log.h"
  8. #include <unistd.h>
  9. #include "time.h"
  10. #include "sys/time.h"
  11. #include "lib_iso15765.h"
  12. #define TASK_TEST 1
  13. #define MULTI_FRAME_EN 1
  14. #define UINT32_DEFAULT_VAL 0xFFFFFFFF
  15. static uint8_t g_s_log_lvl = LOG_LVL_D;
  16. static uint32_t GetTickCount()
  17. {
  18. struct timespec ts;
  19. clock_gettime(CLOCK_MONOTONIC, &ts);
  20. return (ts.tv_sec * 1000 + ts.tv_nsec / 1000000);
  21. }
  22. /******************************************************************************
  23. * Declaration | Static Functions
  24. ******************************************************************************/
  25. 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);
  26. static void parse_finish_callback(n_indn_t *info);
  27. static void on_error(n_rslt err_type);
  28. static uint32_t getms();
  29. /******************************************************************************
  30. * Enumerations, structures & Variables
  31. ******************************************************************************/
  32. static iso15765_t handler0 =
  33. {
  34. .addr_md = N_ADM_BMW, //N_ADM_EXTENDED, N_ADM_BMW
  35. .fr_id_type = CBUS_ID_T_STANDARD,
  36. .clbs.send_frame = send_frame_via_can_0,
  37. .clbs.on_error = on_error,
  38. .clbs.get_ms = getms,
  39. .clbs.indn = parse_finish_callback, //解析完一包调用这个.
  40. .config.stmin = 0x3,
  41. .config.bs = 0x0f,
  42. .config.n_bs = 100,
  43. .config.n_cr = 3
  44. };
  45. static iso15765_t handler1 =
  46. {
  47. .addr_md = N_ADM_BMW,
  48. .fr_id_type = CBUS_ID_T_STANDARD,
  49. .clbs.send_frame = send_frame_via_can_0,
  50. .clbs.on_error = on_error,
  51. .clbs.get_ms = getms,
  52. .clbs.indn = parse_finish_callback, //解析完一包调用这个.
  53. .config.stmin = 0x3,
  54. .config.bs = 0x0f,
  55. .config.n_bs = 100,
  56. .config.n_cr = 3
  57. };
  58. static iso15765_t handler2 =
  59. {
  60. .addr_md = N_ADM_BMW,
  61. .fr_id_type = CBUS_ID_T_STANDARD,
  62. .clbs.send_frame = send_frame_via_can_0,
  63. .clbs.on_error = on_error,
  64. .clbs.get_ms = getms,
  65. .clbs.indn = parse_finish_callback, //解析完一包调用这个.
  66. .config.stmin = 0x3,
  67. .config.bs = 0x0f,
  68. .config.n_bs = 100,
  69. .config.n_cr = 3
  70. };
  71. static iso15765_t handler3 =
  72. {
  73. .addr_md = N_ADM_BMW,
  74. .fr_id_type = CBUS_ID_T_STANDARD,
  75. .clbs.send_frame = send_frame_via_can_0,
  76. .clbs.on_error = on_error,
  77. .clbs.get_ms = getms,
  78. .clbs.indn = parse_finish_callback, //解析完一包调用这个.
  79. .config.stmin = 0x3,
  80. .config.bs = 0x0f,
  81. .config.n_bs = 100,
  82. .config.n_cr = 3
  83. };
  84. /******************************************************************************
  85. * Definition | Static Functions
  86. ******************************************************************************/
  87. uint16_t f_sz1 = 0x0c;
  88. uint16_t f_sz2 = 0x0c;
  89. static uint32_t getms()
  90. {
  91. return GetTickCount();;
  92. }
  93. 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)
  94. {
  95. //暂时没看到啥用处.
  96. return 0;
  97. }
  98. static void on_error(n_rslt err_type)
  99. {
  100. LOG_E("ERROR OCCURED!:0x%04x", err_type);
  101. }
  102. typedef struct {
  103. uint32_t frame_id;
  104. iso15765_t* process_handler;
  105. }HANDLER_T;
  106. HANDLER_T data_process_handler_list[] = {
  107. {
  108. .frame_id = UINT32_DEFAULT_VAL,
  109. .process_handler = &handler0,
  110. },
  111. {
  112. .frame_id = UINT32_DEFAULT_VAL,
  113. .process_handler = &handler1,
  114. },
  115. {
  116. .frame_id = UINT32_DEFAULT_VAL,
  117. .process_handler = &handler2,
  118. },
  119. {
  120. .frame_id = UINT32_DEFAULT_VAL,
  121. .process_handler = &handler3,
  122. },
  123. };
  124. static void parse_finish_callback(n_indn_t *info)
  125. {
  126. if(0 == info->rslt)
  127. {
  128. LOG_I("parse success.frame_type:0x%x,rslt:0x%x,frame_id:0x%x,msg_sz:0x%x,data:",
  129. info->frame_type,
  130. info->rslt,
  131. (info->n_ai.n_sa<<8)|(info->n_ai.n_ta),
  132. info->msg_sz);
  133. if(info->frame_type == N_PCI_T_SF)
  134. {
  135. for (int i = 0; i < info->msg_sz; i++)
  136. {
  137. printf("%02X", info->sf_msg[i]);
  138. }
  139. }
  140. else
  141. {
  142. for (int i = 0; i < info->msg_sz; i++)
  143. {
  144. printf("0x%02x,", info->msg[i]);
  145. }
  146. }
  147. printf("\r\n");
  148. }
  149. else
  150. {
  151. LOG_E("parse fail.rslt:0x%x,frame_id:0x%x",
  152. info->rslt,
  153. (info->n_ai.n_sa<<8)|(info->n_ai.n_ta));
  154. }
  155. }
  156. /******************************************************************************
  157. * Definition | Public Functions
  158. ******************************************************************************/
  159. // uint8_t send_buf[] = {
  160. // 0x06,0x12,0x05,0xdf,0x03,0x22,0xf1,0x01,
  161. // 0x06,0x12,0x08,0xf1,0x10,0x34,0x62,0xf1,0x01,0x01,0x01,
  162. // 0x06,0x12,0x08,0xf1,0x21,0x00,0x04,0x21,0x06,0x22,0x8f,
  163. // 0x06,0x12,0x08,0xf1,0x22,0x04,0xd2,0x01,0x00,0x00,0x10,
  164. // 0x06,0x12,0x08,0xf1,0x23,0x00,0x00,0x00,0x01,0x00,0x00,
  165. // 0x06,0x12,0x08,0xf1,0x24,0x3c,0xfb,0x04,0x00,0x00,0x06,
  166. // 0x06,0x12,0x08,0xf1,0x25,0x00,0x00,0x3c,0xfc,0x04,0x00,
  167. // 0x06,0x60,0x08,0xf1,0x10,0x34,0x62,0xf1,0x01,0x01,0x01,
  168. // 0x06,0x12,0x08,0xf1,0x26,0x00,0x08,0x00,0x00,0x34,0xec,
  169. // 0x06,0x12,0x08,0xf1,0x27,0x0d,0x00,0x00,0x08,0x00,0x00,
  170. // 0x06,0x60,0x08,0xf1,0x21,0x00,0x04,0x21,0x06,0x06,0x8f,
  171. // 0x06,0x12,0x08,0xf1,0x28,0x43,0x4e,0x0d,0x00,0x01,0xaa,
  172. // 0x06,0x60,0x08,0xf1,0x22,0x04,0xd2,0x01,0x00,0x00,0x10,
  173. // 0x06,0x60,0x08,0xf1,0x23,0x00,0x00,0x00,0x01,0x00,0x00,
  174. // 0x06,0x60,0x08,0xf1,0x24,0x36,0x15,0x01,0x02,0x00,0x06,
  175. // 0x06,0x29,0x08,0xf1,0x10,0x2c,0x62,0xf1,0x01,0x01,0x01,
  176. // 0x06,0x60,0x08,0xf1,0x25,0x00,0x00,0x36,0x16,0x01,0x02,
  177. // 0x06,0x60,0x08,0xf1,0x26,0x01,0x08,0x00,0x00,0x36,0x17,
  178. // 0x06,0x29,0x08,0xf1,0x21,0x00,0x03,0x21,0x06,0x06,0x8f,
  179. // 0x06,0x60,0x08,0xf1,0x27,0x01,0x07,0x00,0x05,0x00,0x00,
  180. // 0x06,0x60,0x08,0xf1,0x28,0x36,0x18,0x01,0x03,0x0f,0xff,
  181. // 0x06,0x29,0x08,0xf1,0x22,0x04,0xd2,0x01,0x00,0x00,0x10,
  182. // 0x06,0x29,0x08,0xf1,0x03,0x7f,0x22,0x78,0xff,0xff,0xff, //连续帧中间来了个单帧.
  183. // 0x06,0x29,0x08,0xf1,0x23,0x00,0x00,0x00,0x01,0x00,0x00,
  184. // 0x06,0x29,0x08,0xf1,0x24,0x38,0x72,0x00,0x02,0x00,0x06,
  185. // 0x06,0x29,0x08,0xf1,0x25,0x00,0x00,0x38,0x73,0x00,0x03,
  186. // 0x06,0x29,0x08,0xf1,0x26,0x02,0x08,0x00,0x00,0x38,0x74,
  187. // 0x06,0x29,0x08,0xf1,0x27,0x01,0x01,0x00,0xff,0xff,0xff
  188. // };
  189. uint8_t send_buf[] = {
  190. 0x06,0x12,0x08,0xf1,0x10,0x34,0x62,0xf1,0x01,0x01,0x01,
  191. //中间省略一个流控帧.
  192. 0x06,0x12,0x08,0xf1,0x21,0x00,0x04,0x21,0x06,0x22,0x8f,
  193. 0x06,0x12,0x08,0xf1,0x22,0x04,0xd2,0x01,0x00,0x00,0x10,
  194. 0x06,0x12,0x08,0xf1,0x23,0x00,0x00,0x00,0x01,0x00,0x00,
  195. 0x06,0x12,0x08,0xf1,0x24,0x3c,0xfb,0x04,0x00,0x00,0x06,
  196. 0x06,0x12,0x08,0xf1,0x25,0x00,0x00,0x3c,0xfc,0x04,0x00,
  197. 0x06,0x12,0x08,0xf1,0x26,0x00,0x08,0x00,0x00,0x34,0xec,
  198. 0x06,0x12,0x08,0xf1,0x27,0x0d,0x00,0x00,0x08,0x00,0x00,
  199. 0x06,0x12,0x08,0xf1,0x28,0x43,0x4e,0x0d,0x00,0x01,0xaa,
  200. };
  201. void *rx_data_task(void)
  202. {
  203. uint32_t test_cnt = 0;
  204. uint32_t send_len = sizeof(send_buf);
  205. canbus_frame_t frame;
  206. uint32_t send_index = 0;
  207. while(1)
  208. {
  209. if(send_index < send_len)
  210. {
  211. if(send_index==0)
  212. {
  213. printf("begin parse\r\n");
  214. }
  215. frame.id = MAKEWORD(send_buf[send_index], send_buf[send_index+1]); //帧id
  216. frame.fr_format = CBUS_FR_FRM_STD; //帧格式
  217. frame.id_type = CBUS_ID_T_STANDARD; //帧类型
  218. frame.dlc = send_buf[send_index+2]; //帧长度
  219. memcpy(&frame.dt, &send_buf[send_index+3], frame.dlc); //帧数据
  220. send_index += 3;
  221. send_index += frame.dlc;
  222. LOG_D("frame.id:0x%x", frame.id);
  223. //开始往队列中放入.
  224. uint8_t find_frame_id_flag = 0;
  225. for(int i=0; i<sizeof(data_process_handler_list)/sizeof(HANDLER_T); i++)
  226. {
  227. //先找下这个id有没有在这个列表里面.如果在的话就放进去,如果不在的话,就重新找新的放入.
  228. if(data_process_handler_list[i].frame_id == frame.id)
  229. {
  230. LOG_D("find frame_id");
  231. find_frame_id_flag = 1;
  232. iso15765_enqueue(data_process_handler_list[i].process_handler, &frame);
  233. }
  234. }
  235. if(0 == find_frame_id_flag)
  236. {
  237. //这个frame_id不在之前发过的历史列表里面,那找个新的放进去.
  238. for(int j=0; j<sizeof(data_process_handler_list)/sizeof(HANDLER_T); j++)
  239. {
  240. if(UINT32_DEFAULT_VAL == data_process_handler_list[j].frame_id)
  241. {
  242. LOG_D("not find frame_id, now set %d", j);
  243. data_process_handler_list[j].frame_id = frame.id;
  244. iso15765_enqueue(data_process_handler_list[j].process_handler, &frame);
  245. break;
  246. }
  247. }
  248. }
  249. }
  250. else
  251. {
  252. send_index = 0;
  253. sleep(1);
  254. }
  255. }
  256. }
  257. void *iso15765_parse_task(void)
  258. {
  259. uint32_t at_cnt = 0;
  260. while(1)
  261. {
  262. for (int i=0; i<sizeof(data_process_handler_list)/sizeof(HANDLER_T); i++)
  263. {
  264. if(UINT32_DEFAULT_VAL != data_process_handler_list[i].frame_id)
  265. {
  266. iso15765_process(data_process_handler_list[i].process_handler);
  267. }
  268. }
  269. }
  270. }
  271. int main()
  272. {
  273. pthread_t rx_data_thread_handle, parse_thread_handle;
  274. sys_log_set_lvl(g_s_log_lvl);
  275. for (int i=0; i<sizeof(data_process_handler_list)/sizeof(HANDLER_T); i++)
  276. {
  277. iso15765_init(data_process_handler_list[i].process_handler);
  278. }
  279. if(pthread_create(&rx_data_thread_handle, NULL, (void*)rx_data_task, NULL)){
  280. LOG_E("create error");
  281. return -1;
  282. }
  283. if(pthread_create(&parse_thread_handle, NULL, (void*)iso15765_parse_task, NULL)){
  284. LOG_E("create error");
  285. return -1;
  286. }
  287. pthread_join(rx_data_thread_handle, NULL);
  288. pthread_join(parse_thread_handle, NULL);
  289. return 0;
  290. }