rx_tx_test.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <pthread.h>
  5. #include "sys.h"
  6. #include "sys_log.h"
  7. #include <unistd.h>
  8. #include "time.h"
  9. #include "sys/time.h"
  10. #include "lib_iso15765.h"
  11. static uint32_t GetTickCount()
  12. {
  13. struct timespec ts;
  14. clock_gettime(CLOCK_MONOTONIC, &ts);
  15. return (ts.tv_sec * 1000 + ts.tv_nsec / 1000000);
  16. }
  17. #define ISO16765Example
  18. #ifdef ISO16765Example
  19. /******************************************************************************
  20. * Declaration | Static Functions
  21. ******************************************************************************/
  22. static uint8_t send_frame1(cbus_id_type id_type, uint32_t id, cbus_fr_format fr_fmt, uint8_t dlc, uint8_t *dt);
  23. static uint8_t send_frame2(cbus_id_type id_type, uint32_t id, cbus_fr_format fr_fmt, uint8_t dlc, uint8_t *dt);
  24. static void indn1(n_indn_t *info);
  25. static void indn2(n_indn_t *info);
  26. static void on_error(n_rslt err_type);
  27. static uint32_t getms();
  28. /******************************************************************************
  29. * Enumerations, structures & Variables
  30. ******************************************************************************/
  31. static iso15765_t handler1 =
  32. {
  33. .addr_md = N_ADM_BMW, //N_ADM_EXTENDED, N_ADM_BMW
  34. .fr_id_type = CBUS_ID_T_STANDARD,
  35. .clbs.send_frame = send_frame1, //底层can发送接口.
  36. .clbs.on_error = on_error,
  37. .clbs.get_ms = getms,
  38. .clbs.indn = indn1, //解析完一包调用这个.
  39. .config.stmin = 0x3,
  40. .config.bs = 0x0f,
  41. .config.n_bs = 100,
  42. .config.n_cr = 3
  43. };
  44. static iso15765_t handler2 =
  45. {
  46. .addr_md = N_ADM_BMW, //N_ADM_EXTENDED
  47. .fr_id_type = CBUS_ID_T_STANDARD,
  48. .clbs.send_frame = send_frame2,
  49. .clbs.on_error = on_error,
  50. .clbs.get_ms = getms,
  51. .clbs.indn = indn2,
  52. .config.stmin = 0x3,
  53. .config.bs = 0x0f,
  54. .config.n_bs = 100,
  55. .config.n_cr = 3
  56. };
  57. n_req_t frame1 =
  58. {
  59. .n_ai.n_pr = 0x06,
  60. .n_ai.n_sa = 0x12,
  61. .n_ai.n_ta = 0xdf,
  62. .n_ai.n_ae = 0x00,
  63. .n_ai.n_tt = N_TA_T_PHY,
  64. .fr_fmt = CBUS_FR_FRM_STD,
  65. .msg = {0x22, 0xf1, 0x01, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09},
  66. .msg_sz = 0x0d,
  67. };
  68. n_req_t frame2 =
  69. {
  70. .n_ai.n_pr = 0x06,
  71. .n_ai.n_sa = 0x12,
  72. .n_ai.n_ta = 0xdf,
  73. .n_ai.n_ae = 0x00,
  74. .n_ai.n_tt = N_TA_T_PHY,
  75. .fr_fmt = CBUS_FR_FRM_STD,
  76. .msg = {0},
  77. .msg_sz = 0,
  78. };
  79. /******************************************************************************
  80. * Definition | Static Functions
  81. ******************************************************************************/
  82. uint16_t f_sz1 = 0x0c;
  83. uint16_t f_sz2 = 0x0c;
  84. static void rand_string(char *str, size_t size)
  85. {
  86. const char charset[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJK";
  87. for (size_t n = 0; n < size; n++)
  88. {
  89. int key = rand() % ((int)sizeof(charset) - 1);
  90. str[n] = charset[key];
  91. }
  92. }
  93. static uint32_t getms()
  94. {
  95. return GetTickCount();;
  96. }
  97. static void print_frame(uint8_t instance, uint8_t tp_mode, cbus_id_type mode, uint32_t id, uint8_t ctp_ft, uint8_t dlc,
  98. uint8_t *dt)
  99. {
  100. printf("send_frame:%d,| TpMode: [%02x] | FrameType: [%2d] IdType: [%d] Id: [%8x] DLC: [%02d]\t\t", instance,
  101. tp_mode, mode, ctp_ft, id, dlc);
  102. for (int s = 0; s < (int)dlc; s += 8)
  103. {
  104. int elements = (int)dlc - s > 8 ? 8 : (int)dlc - s;
  105. for (int i = s; i < (s + 8); i++)
  106. {
  107. printf((i < (s + elements)) ? "%02x " : " ", dt[i]);
  108. }
  109. printf("\t");
  110. if ((s + elements) == (int)dlc)
  111. {
  112. printf("\n");
  113. }
  114. else
  115. {
  116. printf("\n |\t\t\t\t\t\t\t\t\t\t\t\t\t");
  117. }
  118. }
  119. }
  120. static uint8_t send_frame1(cbus_id_type id_type, uint32_t id, cbus_fr_format fr_fmt, uint8_t dlc, uint8_t *dt)
  121. {
  122. print_frame(1, handler2.addr_md, id_type, id, fr_fmt, dlc, dt);
  123. canbus_frame_t frame = { .id = id, .dlc = dlc, .id_type = id_type, .fr_format = fr_fmt };
  124. memmove(frame.dt, dt, dlc);
  125. //使用这个接口把收到的can数据放入解析库中.
  126. printf("send can data size:%d,", dlc);
  127. for (int i=0; i<dlc; i++)
  128. {
  129. printf("%x", frame.dt[i]);
  130. }
  131. printf("\r\n");
  132. iso15765_enqueue(&handler2, &frame);
  133. return 0;
  134. }
  135. static uint8_t send_frame2(cbus_id_type id_type, uint32_t id, cbus_fr_format fr_fmt, uint8_t dlc, uint8_t *dt)
  136. {
  137. print_frame(2, handler1.addr_md, id_type, id, fr_fmt, dlc, dt);
  138. canbus_frame_t frame = { .id = id, .dlc = dlc, .id_type = id_type, .fr_format = fr_fmt };
  139. memmove(frame.dt, dt, dlc);
  140. iso15765_enqueue(&handler1, &frame);
  141. return 0;
  142. }
  143. static void on_error(n_rslt err_type)
  144. {
  145. printf("ERROR OCCURED!:%04x", err_type);
  146. }
  147. static void indn1(n_indn_t *info)
  148. {
  149. //收到完整的一包数据.
  150. uint8_t v = 'X';
  151. uint8_t s = 'X';
  152. if (info->msg_sz == frame2.msg_sz)
  153. {
  154. s = 'V';
  155. }
  156. if (memcmp(info->msg, frame2.msg, info->msg_sz) == 0)
  157. {
  158. v = 'V';
  159. }
  160. printf("- Reception of H1. Msg_sz:[%d] | SZ_CH[%c] MSG_CH[%c]\n", info->msg_sz, s, v);
  161. if ((v != 'V') || (s != 'V'))
  162. {
  163. printf("--------- ERROR -----------\n");
  164. sleep(1000);
  165. }
  166. printf("- END OF TRANSMISSION/RECEPTION-");
  167. for (int i = 0; i < info->msg_sz; i++)
  168. {
  169. printf("%x", info->msg[i]);
  170. }
  171. printf("\r\n");
  172. frame1.msg_sz = f_sz1;
  173. rand_string(frame1.msg, frame1.msg_sz);
  174. f_sz1 = f_sz1 == 512 ? 0 : f_sz1 + 1;
  175. printf("handler1 send msg_size:%d,data:", frame1.msg_sz);
  176. for(int i=0; i<frame1.msg_sz; i++)
  177. {
  178. printf("%x", frame1.msg[i]);
  179. }
  180. printf("\r\n");
  181. iso15765_send(&handler1, &frame1);
  182. }
  183. static void indn2(n_indn_t *info)
  184. {
  185. uint8_t v = 'X';
  186. uint8_t s = 'X';
  187. if (info->msg_sz == frame1.msg_sz)
  188. {
  189. s = 'V';
  190. }
  191. if (memcmp(info->msg, frame1.msg, info->msg_sz) == 0)
  192. {
  193. v = 'V';
  194. }
  195. printf("- Reception of H2. Msg_sz:[%d] | SZ_CH[%c] MSG_CH[%c]\n", info->msg_sz, s, v);
  196. for (int i = 0; i < info->msg_sz; i++)
  197. {
  198. printf("%x", info->msg[i]);
  199. }
  200. printf("\r\n");
  201. if ((v != 'V') || (s != 'V'))
  202. {
  203. printf("--------- ERROR -----------\n");
  204. sleep(1000);
  205. }
  206. printf("- END OF TRANSMISSION/RECEPTION \r\n");
  207. frame2.msg_sz = f_sz2;
  208. rand_string(frame2.msg, frame2.msg_sz);
  209. f_sz2 = f_sz2 == 512 ? 0 : f_sz2 + 1;
  210. printf("handler2 send msg_size:%d,", frame2.msg_sz);
  211. for(int i=0; i<frame2.msg_sz; i++)
  212. {
  213. printf("%x", frame2.msg[i]);
  214. }
  215. printf("\r\n");
  216. iso15765_send(&handler2, &frame2);
  217. }
  218. /******************************************************************************
  219. * Definition | Public Functions
  220. ******************************************************************************/
  221. int main()
  222. {
  223. uint8_t cnt = 0;
  224. sys_log_set_lvl(LOG_LVL_I);
  225. iso15765_init(&handler1);
  226. iso15765_init(&handler2);
  227. frame1.msg_sz = f_sz1;
  228. rand_string(frame1.msg, frame1.msg_sz);
  229. f_sz1++;
  230. iso15765_send(&handler1, &frame1);
  231. printf("handler1 send msg_size:%d,", frame1.msg_sz);
  232. for(int i=0; i<frame1.msg_sz; i++)
  233. {
  234. printf("%x", frame1.msg[i]);
  235. }
  236. printf("\r\n");
  237. while (1)
  238. {
  239. cnt += 1;
  240. iso15765_process(&handler1);
  241. iso15765_process(&handler2);
  242. if (100 == cnt)
  243. {
  244. // break;
  245. }
  246. }
  247. return 0;
  248. }
  249. #endif
  250. // 1.ST应用收到数据时,调用iso15765_send往解析库发送,解析库底层调用send_frame1发给can接口.
  251. // 2.can驱动接收到数据后,调用iso15765_enqueue(&handler2, &frame)发送给解析库,解析完成后向应用触发indn1事件.
  252. // 1.开发板调用iso15765_send,解析库底层是send_frame1为can发送接口.
  253. //