rx_tx_test.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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. iso15765_enqueue(&handler2, &frame);
  127. return 0;
  128. }
  129. static uint8_t send_frame2(cbus_id_type id_type, uint32_t id, cbus_fr_format fr_fmt, uint8_t dlc, uint8_t *dt)
  130. {
  131. print_frame(2, handler1.addr_md, id_type, id, fr_fmt, dlc, dt);
  132. canbus_frame_t frame = { .id = id, .dlc = dlc, .id_type = id_type, .fr_format = fr_fmt };
  133. memmove(frame.dt, dt, dlc);
  134. iso15765_enqueue(&handler1, &frame);
  135. return 0;
  136. }
  137. static void on_error(n_rslt err_type)
  138. {
  139. printf("ERROR OCCURED!:%04x", err_type);
  140. }
  141. static void indn1(n_indn_t *info)
  142. {
  143. //收到完整的一包数据.
  144. uint8_t v = 'X';
  145. uint8_t s = 'X';
  146. if (info->msg_sz == frame2.msg_sz)
  147. {
  148. s = 'V';
  149. }
  150. if (memcmp(info->msg, frame2.msg, info->msg_sz) == 0)
  151. {
  152. v = 'V';
  153. }
  154. printf("- Reception of H1. Msg_sz:[%d] | SZ_CH[%c] MSG_CH[%c]\n", info->msg_sz, s, v);
  155. if ((v != 'V') || (s != 'V'))
  156. {
  157. printf("--------- ERROR -----------\n");
  158. sleep(1000);
  159. }
  160. printf("- END OF TRANSMISSION/RECEPTION-");
  161. for (int i = 0; i < info->msg_sz; i++)
  162. {
  163. printf("%x", info->msg[i]);
  164. }
  165. printf("\r\n");
  166. frame1.msg_sz = f_sz1;
  167. rand_string(frame1.msg, frame1.msg_sz);
  168. f_sz1 = f_sz1 == 512 ? 0 : f_sz1 + 1;
  169. iso15765_send(&handler1, &frame1);
  170. }
  171. static void indn2(n_indn_t *info)
  172. {
  173. uint8_t v = 'X';
  174. uint8_t s = 'X';
  175. if (info->msg_sz == frame1.msg_sz)
  176. {
  177. s = 'V';
  178. }
  179. if (memcmp(info->msg, frame1.msg, info->msg_sz) == 0)
  180. {
  181. v = 'V';
  182. }
  183. printf("- Reception of H2. Msg_sz:[%d] | SZ_CH[%c] MSG_CH[%c]\n", info->msg_sz, s, v);
  184. for (int i = 0; i < info->msg_sz; i++)
  185. {
  186. printf("0x%x", info->msg[i]);
  187. }
  188. printf("\r\n");
  189. if ((v != 'V') || (s != 'V'))
  190. {
  191. printf("--------- ERROR -----------\n");
  192. sleep(1000);
  193. }
  194. printf("- END OF TRANSMISSION/RECEPTION \r\n");
  195. frame2.msg_sz = f_sz2;
  196. rand_string(frame2.msg, frame2.msg_sz);
  197. f_sz2 = f_sz2 == 512 ? 0 : f_sz2 + 1;
  198. iso15765_send(&handler2, &frame2);
  199. }
  200. /******************************************************************************
  201. * Definition | Public Functions
  202. ******************************************************************************/
  203. int main()
  204. {
  205. uint8_t cnt = 0;
  206. sys_log_set_lvl(LOG_LVL_I);
  207. iso15765_init(&handler1);
  208. iso15765_init(&handler2);
  209. frame1.msg_sz = f_sz1;
  210. rand_string(frame1.msg, frame1.msg_sz);
  211. f_sz1++;
  212. iso15765_send(&handler1, &frame1);
  213. while (1)
  214. {
  215. cnt += 1;
  216. iso15765_process(&handler1);
  217. iso15765_process(&handler2);
  218. if (100 == cnt)
  219. {
  220. // break;
  221. }
  222. }
  223. return 0;
  224. }
  225. #endif
  226. // 1.ST应用收到数据时,调用iso15765_send往解析库发送,解析库底层调用send_frame1发给can接口.
  227. // 2.can驱动接收到数据后,调用iso15765_enqueue(&handler2, &frame)发送给解析库,解析完成后向应用触发indn1事件.