| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281 |
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.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"
- static uint32_t GetTickCount()
- {
- struct timespec ts;
- clock_gettime(CLOCK_MONOTONIC, &ts);
- return (ts.tv_sec * 1000 + ts.tv_nsec / 1000000);
- }
- #define ISO16765Example
- #ifdef ISO16765Example
- /******************************************************************************
- * Declaration | Static Functions
- ******************************************************************************/
- static uint8_t send_frame1(cbus_id_type id_type, uint32_t id, cbus_fr_format fr_fmt, uint8_t dlc, uint8_t *dt);
- static uint8_t send_frame2(cbus_id_type id_type, uint32_t id, cbus_fr_format fr_fmt, uint8_t dlc, uint8_t *dt);
- static void indn1(n_indn_t *info);
- static void indn2(n_indn_t *info);
- static void on_error(n_rslt err_type);
- static uint32_t getms();
- /******************************************************************************
- * Enumerations, structures & Variables
- ******************************************************************************/
- static iso15765_t handler1 =
- {
- .addr_md = N_ADM_BMW, //N_ADM_EXTENDED, N_ADM_BMW
- .fr_id_type = CBUS_ID_T_STANDARD,
- .clbs.send_frame = send_frame1, //底层can发送接口.
- .clbs.on_error = on_error,
- .clbs.get_ms = getms,
- .clbs.indn = indn1, //解析完一包调用这个.
- .config.stmin = 0x3,
- .config.bs = 0x0f,
- .config.n_bs = 100,
- .config.n_cr = 3
- };
- static iso15765_t handler2 =
- {
- .addr_md = N_ADM_BMW, //N_ADM_EXTENDED
- .fr_id_type = CBUS_ID_T_STANDARD,
- .clbs.send_frame = send_frame2,
- .clbs.on_error = on_error,
- .clbs.get_ms = getms,
- .clbs.indn = indn2,
- .config.stmin = 0x3,
- .config.bs = 0x0f,
- .config.n_bs = 100,
- .config.n_cr = 3
- };
- n_req_t frame1 =
- {
- .n_ai.n_pr = 0x06,
- .n_ai.n_sa = 0x12,
- .n_ai.n_ta = 0xdf,
- .n_ai.n_ae = 0x00,
- .n_ai.n_tt = N_TA_T_PHY,
- .fr_fmt = CBUS_FR_FRM_STD,
- .msg = {0x22, 0xf1, 0x01, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09},
- .msg_sz = 0x0d,
- };
- n_req_t frame2 =
- {
- .n_ai.n_pr = 0x06,
- .n_ai.n_sa = 0x12,
- .n_ai.n_ta = 0xdf,
- .n_ai.n_ae = 0x00,
- .n_ai.n_tt = N_TA_T_PHY,
- .fr_fmt = CBUS_FR_FRM_STD,
- .msg = {0},
- .msg_sz = 0,
- };
- /******************************************************************************
- * Definition | Static Functions
- ******************************************************************************/
- uint16_t f_sz1 = 0x0c;
- uint16_t f_sz2 = 0x0c;
- static void rand_string(char *str, size_t size)
- {
- const char charset[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJK";
- for (size_t n = 0; n < size; n++)
- {
- int key = rand() % ((int)sizeof(charset) - 1);
- str[n] = charset[key];
- }
- }
- static uint32_t getms()
- {
- return GetTickCount();;
- }
- 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,
- uint8_t *dt)
- {
- printf("send_frame:%d,| TpMode: [%02x] | FrameType: [%2d] IdType: [%d] Id: [%8x] DLC: [%02d]\t\t", instance,
- tp_mode, mode, ctp_ft, id, dlc);
- for (int s = 0; s < (int)dlc; s += 8)
- {
- int elements = (int)dlc - s > 8 ? 8 : (int)dlc - s;
- for (int i = s; i < (s + 8); i++)
- {
- printf((i < (s + elements)) ? "%02x " : " ", dt[i]);
- }
- printf("\t");
- if ((s + elements) == (int)dlc)
- {
- printf("\n");
- }
- else
- {
- printf("\n |\t\t\t\t\t\t\t\t\t\t\t\t\t");
- }
- }
- }
- static uint8_t send_frame1(cbus_id_type id_type, uint32_t id, cbus_fr_format fr_fmt, uint8_t dlc, uint8_t *dt)
- {
- print_frame(1, handler2.addr_md, id_type, id, fr_fmt, dlc, dt);
- canbus_frame_t frame = { .id = id, .dlc = dlc, .id_type = id_type, .fr_format = fr_fmt };
- memmove(frame.dt, dt, dlc);
- //使用这个接口把收到的can数据放入解析库中.
- printf("send can data size:%d,", dlc);
- for (int i=0; i<dlc; i++)
- {
- printf("%x", frame.dt[i]);
- }
- printf("\r\n");
- iso15765_enqueue(&handler2, &frame);
- return 0;
- }
- static uint8_t send_frame2(cbus_id_type id_type, uint32_t id, cbus_fr_format fr_fmt, uint8_t dlc, uint8_t *dt)
- {
- print_frame(2, handler1.addr_md, id_type, id, fr_fmt, dlc, dt);
- canbus_frame_t frame = { .id = id, .dlc = dlc, .id_type = id_type, .fr_format = fr_fmt };
- memmove(frame.dt, dt, dlc);
- iso15765_enqueue(&handler1, &frame);
- return 0;
- }
- static void on_error(n_rslt err_type)
- {
- printf("ERROR OCCURED!:%04x", err_type);
- }
- static void indn1(n_indn_t *info)
- {
- //收到完整的一包数据.
- uint8_t v = 'X';
- uint8_t s = 'X';
- if (info->msg_sz == frame2.msg_sz)
- {
- s = 'V';
- }
- if (memcmp(info->msg, frame2.msg, info->msg_sz) == 0)
- {
- v = 'V';
- }
- printf("- Reception of H1. Msg_sz:[%d] | SZ_CH[%c] MSG_CH[%c]\n", info->msg_sz, s, v);
- if ((v != 'V') || (s != 'V'))
- {
- printf("--------- ERROR -----------\n");
- sleep(1000);
- }
- printf("- END OF TRANSMISSION/RECEPTION-");
- for (int i = 0; i < info->msg_sz; i++)
- {
- printf("%x", info->msg[i]);
- }
- printf("\r\n");
- frame1.msg_sz = f_sz1;
- rand_string(frame1.msg, frame1.msg_sz);
- f_sz1 = f_sz1 == 512 ? 0 : f_sz1 + 1;
- printf("handler1 send msg_size:%d,data:", frame1.msg_sz);
- for(int i=0; i<frame1.msg_sz; i++)
- {
- printf("%x", frame1.msg[i]);
- }
- printf("\r\n");
- iso15765_send(&handler1, &frame1);
- }
- static void indn2(n_indn_t *info)
- {
- uint8_t v = 'X';
- uint8_t s = 'X';
- if (info->msg_sz == frame1.msg_sz)
- {
- s = 'V';
- }
- if (memcmp(info->msg, frame1.msg, info->msg_sz) == 0)
- {
- v = 'V';
- }
- printf("- Reception of H2. Msg_sz:[%d] | SZ_CH[%c] MSG_CH[%c]\n", info->msg_sz, s, v);
- for (int i = 0; i < info->msg_sz; i++)
- {
- printf("%x", info->msg[i]);
- }
- printf("\r\n");
- if ((v != 'V') || (s != 'V'))
- {
- printf("--------- ERROR -----------\n");
- sleep(1000);
- }
- printf("- END OF TRANSMISSION/RECEPTION \r\n");
- frame2.msg_sz = f_sz2;
- rand_string(frame2.msg, frame2.msg_sz);
- f_sz2 = f_sz2 == 512 ? 0 : f_sz2 + 1;
- printf("handler2 send msg_size:%d,", frame2.msg_sz);
- for(int i=0; i<frame2.msg_sz; i++)
- {
- printf("%x", frame2.msg[i]);
- }
- printf("\r\n");
- iso15765_send(&handler2, &frame2);
- }
- /******************************************************************************
- * Definition | Public Functions
- ******************************************************************************/
- int main()
- {
- uint8_t cnt = 0;
- sys_log_set_lvl(LOG_LVL_I);
- iso15765_init(&handler1);
- iso15765_init(&handler2);
- frame1.msg_sz = f_sz1;
- rand_string(frame1.msg, frame1.msg_sz);
- f_sz1++;
- iso15765_send(&handler1, &frame1);
- printf("handler1 send msg_size:%d,", frame1.msg_sz);
- for(int i=0; i<frame1.msg_sz; i++)
- {
- printf("%x", frame1.msg[i]);
- }
- printf("\r\n");
- while (1)
- {
- cnt += 1;
- iso15765_process(&handler1);
- iso15765_process(&handler2);
- if (100 == cnt)
- {
- // break;
- }
- }
- return 0;
- }
- #endif
- // 1.ST应用收到数据时,调用iso15765_send往解析库发送,解析库底层调用send_frame1发给can接口.
- // 2.can驱动接收到数据后,调用iso15765_enqueue(&handler2, &frame)发送给解析库,解析完成后向应用触发indn1事件.
- // 1.开发板调用iso15765_send,解析库底层是send_frame1为can发送接口.
- //
|