Files
HSP_demo/has_project/has_app/acm/has_acm.c

156 lines
4.0 KiB
C
Raw Normal View History

2026-04-10 18:10:51 +08:00
#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);
2026-04-23 14:48:24 +08:00
return;
2026-04-10 18:10:51 +08:00
}
static void has_receive_from_mcu_1(const has_tlv_t *box)
{
printf("receive event:%d\n", box->event);
2026-04-23 14:48:24 +08:00
return;
2026-04-10 18:10:51 +08:00
}
static void has_receive_from_mcu_2(const has_tlv_t *box)
{
printf("receive event:%d\n", box->event);
2026-04-23 14:48:24 +08:00
return;
2026-04-10 18:10:51 +08:00
}
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);
}
2026-04-23 14:48:24 +08:00
static void protocol_task(void *param)
2026-04-10 18:10:51 +08:00
{
int actuall_len;
2026-04-23 14:48:24 +08:00
(void)param;
2026-04-10 18:10:51 +08:00
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);
}
}
2026-04-23 14:48:24 +08:00
static void send_test_task(void *param)
2026-04-10 18:10:51 +08:00
{
unsigned char data = 3;
2026-04-23 14:48:24 +08:00
(void)param;
2026-04-10 18:10:51 +08:00
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;
2026-04-23 14:48:24 +08:00
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 },
};
2026-04-10 18:10:51 +08:00
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;
2026-04-23 14:48:24 +08:00
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;
}
}
2026-04-10 18:10:51 +08:00
g_handler = has_protocol_create(&attr);
if (g_handler == NULL)
{
printf("create ACM handler err!\n");
2026-04-23 14:48:24 +08:00
has_free(read_buffer);
read_buffer = NULL;
return -1;
2026-04-10 18:10:51 +08:00
}
printf("module:ACM\n");
/* 创建线程 */
2026-04-23 14:48:24 +08:00
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;
2026-04-10 18:10:51 +08:00
return -1;
}
2026-04-23 14:48:24 +08:00
if (has_task_create(&send_task_attr, &send_test_tid) != 0) {
printf("module:ACM create send task failed\n");
// return -1;
2026-04-10 18:10:51 +08:00
}
return 0;
}
HAS_DECLARE_MODULE(ACM, example_protocol_init)