#include "has_platform.h" static OS_THREAD_ID protocol_tid; static OS_THREAD_ID send_test_tid; static unsigned char *read_buffer; #define REC_BUFFER_LEN_MAX 1024 #define PROCESS_MSG_PERIOD 3000 #define TEST_EVENT 0 #define TEST_EVENT1 1 #define TEST_EVENT2 2 static has_ptcl_handler *g_handler = NULL; static void has_receive_from_mcu(const has_tlv_t *box) { printf("receive event:%d\n", box->event); return; } static void has_receive_from_mcu_1(const has_tlv_t *box) { printf("receive event:%d\n", box->event); return; } static void has_receive_from_mcu_2(const has_tlv_t *box) { printf("receive event:%d\n", box->event); return; } int acm_hal_send(uint8_t *buff, uint32_t len) { return has_dev_write(UART, 0, buff, len); } void resend_callback(has_tlv_t *p_tlv, uint8_t resend_cnt) { printf("retry to send msg event:%d cnt:%d\n", p_tlv->event, resend_cnt); } void send_success_callback(has_tlv_t *p_tlv) { printf("event %x send successfully\n", p_tlv->event); } void send_fail_callback(has_tlv_t *p_tlv) { printf("event %x send fail\n", p_tlv->event); } static void protocol_task(void *param) { int actuall_len; (void)param; while (1) { /* reads */ actuall_len = has_dev_read(UART, 0, read_buffer, REC_BUFFER_LEN_MAX); if (actuall_len > 0) { has_protocol_recv_process(g_handler, read_buffer, actuall_len); } has_sleep_ms(PROCESS_MSG_PERIOD); } } static void msg_handle_cb(unsigned char module_id, const unsigned char *buf, unsigned int len) { if (has_protocol_send(g_handler, module_id, buf, 1, need_confirm) != 0) { printf("send err module:%x\n", module_id); } } static void send_test_task(void *param) { unsigned char data = 3; (void)param; while (1) { /* write */ has_msg_handle(ACM, msg_handle_cb, PROCESS_MSG_PERIOD); /* 重发,发送失败需要publish,注册发送的硬件接口 */ // has_protocol_send(g_handler, TEST_EVENT2, &data, sizeof(data), need_confirm); } } int example_protocol_init(void) { has_protocol_attr attr; has_task_attr_t protocol_task_attr = { .name = "acm_read", .entry = protocol_task, .arg = NULL, .stack_size_bytes = 1024U, .priority = HAS_TASK_PRIORITY_NORMAL, .linux_attr = { .sched_policy = -1 }, }; has_task_attr_t send_task_attr = { .name = "acm_send", .entry = send_test_task, .arg = NULL, .stack_size_bytes = 1024U, .priority = HAS_TASK_PRIORITY_NORMAL, .linux_attr = { .sched_policy = -1 }, }; kbox_event uart_list[] = { {TEST_EVENT, has_receive_from_mcu}, {TEST_EVENT1, has_receive_from_mcu_1}, {TEST_EVENT2, has_receive_from_mcu_2}, }; attr.ev_list = uart_list; attr.ev_list_sz = sizeof(uart_list) / sizeof(uart_list[0]); attr.hal_send_cb = acm_hal_send; attr.resend_cb = resend_callback; attr.retry_max_cnt = 1; attr.wait_period = 500; attr.success_cb = send_success_callback; attr.fail_cb = send_fail_callback; if (read_buffer == NULL) { read_buffer = has_malloc(REC_BUFFER_LEN_MAX); if (read_buffer == NULL) { printf("alloc ACM read buffer failed\n"); return -1; } } g_handler = has_protocol_create(&attr); if (g_handler == NULL) { printf("create ACM handler err!\n"); has_free(read_buffer); read_buffer = NULL; return -1; } printf("module:ACM\n"); /* 创建线程 */ if (has_task_create(&protocol_task_attr, &protocol_tid) != 0) { printf("module:ACM create read task failed\n"); has_protocol_destroy(g_handler); g_handler = NULL; has_free(read_buffer); read_buffer = NULL; return -1; } if (has_task_create(&send_task_attr, &send_test_tid) != 0) { printf("module:ACM create send task failed\n"); // return -1; } return 0; } HAS_DECLARE_MODULE(ACM, example_protocol_init)