Files
kunlun/app/iot_cus_at_app/customer/app_cus_task.c
2024-09-28 14:24:04 +08:00

343 lines
9.6 KiB
C
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/****************************************************************************
Copyright(c) 2019 by Aerospace C.Power (Chongqing) Microelectronics. ALL RIGHTS RESERVED.
This Information is proprietary to Aerospace C.Power (Chongqing) Microelectronics and MAY NOT
be copied by any method or incorporated into another program without
the express written consent of Aerospace C.Power. This Information or any portion
thereof remains the property of Aerospace C.Power. The Information contained herein
is believed to be accurate and Aerospace C.Power assumes no responsibility or
liability for its use in any way and conveys no license or title under
any patent or copyright and makes no representation or warranty that this
Information is free from patent or copyright infringement.
****************************************************************************/
#include "app_uart.h"
#include "app_main.h"
#include "app_cus_task.h"
#include "iot_plc_led_api.h"
#if (IOT_APP_SELECTION == IOT_APP_DEF_24_SILA_APP)
#include "app_proto_proc.h"
#include "sila_dev.h"
#endif
app_cus_task_t cus_task;
static uint16_t app_tx_to_cus_uart(uint8_t *data, uint16_t data_length)
{
iot_pkt_t *cus_pkt;
cus_pkt = iot_pkt_alloc(data_length, IOT_APP_DEMO_MID);
if (NULL == cus_pkt) {
APP_PRINTF("[ERR] %s Packet Alloc Failed !!", __FUNCTION__);
return ERR_FAIL;
}
os_mem_cpy(iot_pkt_put(cus_pkt, data_length), (void *)data, data_length);
app_cus_task_msg_post(E_CUS_FROM_APPTASK_MSG, APP_CUS_TASK_MSG_ID_FROM_APP,
(void*)cus_pkt);
return ERR_OK;
}
#if IOT_SILA_DEV_ENABLE
/* Funtion used to send data to sila dev module. */
static uint16_t app_tx_to_cus_device(app_proto_frame_head *p_frm)
{
return iot_sila_dev_plc_receive(p_frm->data, p_frm->length, p_frm->seq);
}
static uint32_t cus_dev_tx_to_app_msg (uint8_t *p_data, uint32_t data_len, uint16_t seq)
{
iot_pkt_t *pkt;
app_proto_frame_head *p_head;
iot_plc_led_request(IOT_PLC_LED_REQ_PLC_485_RX);
APP_PRINT_BUF("[CUS_DEV_TX_TO_APP]: ", p_data, data_len);
pkt = iot_pkt_alloc(data_len + sizeof(*p_head) + 2, IOT_APP_DEMO_MID);
if (NULL == pkt) {
APP_PRINTF("[ERR] %s Packet Alloc Failed !!", __FUNCTION__);
return ERR_NOMEM;
}
p_head = (app_proto_frame_head *)iot_pkt_put(pkt, data_len + sizeof(*p_head) + 2);
os_mem_cpy(p_head->data, p_data, data_len);
fill_proto_frame_fixed_info(p_head, ID_PROTO_CMD_SYSCTRL_DATA_TRANS, seq, data_len);
app_cus_task_msg_post(E_CUS_FROM_UART_MSG, APP_CUS_TASK_MSG_ID_UART, (void*)pkt);
return ERR_OK;
}
/* 从app抛送uart消息到当前task处理然后发送到串口 */
uint16_t app_tx_to_cus_uart_msg(uint8_t *data, uint16_t data_length)
{
app_proto_frame_head *p_head = (app_proto_frame_head *)data;
if (NULL == data || data_length == 0) {
APP_PRINTF("[ERR] %s Data is NULL !!", __FUNCTION__);
return ERR_FAIL;
}
APP_PRINTF("[INF]PLC cmd id %x.\r\n", p_head->cmd);
if (ID_PROTO_CMD_SYSCTRL_DATA_TRANS == p_head->cmd) {
APP_PRINT_BUF("[APP_TX_TO_CUS_DEV]", data, data_length);
return app_tx_to_cus_device(p_head);
} else {
APP_PRINT_BUF("[APP_TX_TO_CUS_UART]", data, data_length);
return app_tx_to_cus_uart(data, data_length);
}
}
#else /* IOT_SILA_DEV_ENABLE */
/* 从app抛送uart消息到当前task处理然后发送到串口 */
uint16_t app_tx_to_cus_uart_msg(uint8_t *data, uint16_t data_length)
{
if (NULL == data || data_length == 0) {
APP_PRINTF("[ERR] %s Data is NULL !!", __FUNCTION__);
return ERR_FAIL;
}
return app_tx_to_cus_uart(data, data_length);
}
#endif /* IOT_SILA_DEV_ENABLE */
/* 将串口数据发送到app_main task 中处理 */
uint16_t cus_tx_to_app_uart_msg(app_cus_task_msg_t *msg)
{
/* post msg to app_msg_task */
app_post_msg(E_APP_MSG_PROCESS, E_CMD_ID_RCV_FROM_UART, (iot_pkt_t*)msg->data);
return ERR_OK;
}
/* 定义msg post 函数 */
void app_cus_task_msg_post(uint16_t msg_type, uint16_t msg_id, iot_pkt_t *data)
{
iot_task_msg_t *msg;
app_cus_task_msg_t *task_msg;
msg = iot_task_alloc_msg_with_reserved(cus_task.task, 0);
if (NULL == msg) {
if (NULL != data) {
iot_pkt_free(data);
}
IOT_ASSERT(0);
return;
}
task_msg = (app_cus_task_msg_t*)msg;
task_msg->msg.type = msg_type;
task_msg->msg.id = msg_id;
task_msg->data = data;
iot_task_queue_msg(cus_task.task, &task_msg->msg, 0);
return;
}
/* timer handle, post message to task for handle */
static void app_cus_task_peroid_timer_exe(timer_id_t timer_id, void *arg)
{
(void)arg;
if (timer_id == cus_task.peroid_timer) {
app_cus_task_msg_post(E_CUS_PEROID_TIMER_MSG,
APP_CUS_TASK_MSG_ID_PEROID_TMR, NULL);
}
return;
}
/* handle data received from uart */
static void cus_task_uart_msg_handle_func(app_cus_task_msg_t *msg)
{
cus_tx_to_app_uart_msg(msg);
}
/* handle data from local timer */
static void cus_task_timer_msg_handle_func(app_cus_task_msg_t *msg)
{
switch(msg->msg.id)
{
case E_CUS_PEROID_TIMER_MSG:
{
// APP_PRINTF("PEROID TIMER FIRED\n");
break;
}
default:
{
break;
}
}
}
/* handle data from app_msg_task */
static void cus_task_app_msg_handle_func(app_cus_task_msg_t *msg)
{
switch(msg->msg.id)
{
case APP_CUS_TASK_MSG_ID_FROM_APP:
{
iot_plc_led_request(IOT_PLC_LED_REQ_PLC_485_TX);
iot_uart_send(cus_task.uart_h, (iot_pkt_t*)msg->data, NULL);
break;
}
default:
{
break;
}
}
return;
}
/* 当前task事件处理函数 */
static void app_cus_event_handle(iot_task_h task_h, uint32_t event)
{
(void)task_h;
(void)event;
APP_PRINTF("[INF] %s", __FUNCTION__);
return;
}
/* 当前task消息处理函数 */
static void app_cus_handle(iot_task_h task_h, iot_task_msg_t *msg)
{
app_cus_task_msg_t *dm_msg = (app_cus_task_msg_t *)msg;
(void)task_h;
if ((NULL == dm_msg) || (!APP_CUS_VALID(dm_msg->msg.type))) {
/* Maybe this can cause memory overflow! */
APP_PRINTF("[ERR] %s Invalid MSG !!", __FUNCTION__);
return;
}
switch (dm_msg->msg.type)
{
case E_CUS_FROM_UART_MSG:
{
cus_task_uart_msg_handle_func(dm_msg);
break;
}
case E_CUS_PEROID_TIMER_MSG:
{
cus_task_timer_msg_handle_func(dm_msg);
break;
}
case E_CUS_FROM_APPTASK_MSG:
{
cus_task_app_msg_handle_func(dm_msg);
break;
}
default:
{
break;
}
}
/* need free task message */
iot_task_free_msg(task_h, msg);
return;
}
static void app_cus_common_cancel(uint16_t id, void *data)
{
iot_pkt_t *pkt = (iot_pkt_t*)data;
if (pkt) {
iot_pkt_free(pkt);
}
(void)id;
return;
}
/* function pointer to handle message canceling, use iot_task_clean_msg cancel */
static void app_cus_cancel(iot_task_h task_h, iot_task_msg_t *msg)
{
app_cus_task_msg_t *dm_msg = (app_cus_task_msg_t *)msg;
(void)task_h;
if ((NULL == dm_msg) || (!APP_CUS_VALID(dm_msg->msg.type))) {
/* Maybe this can cause memory overflow! */
APP_PRINTF("[ERR] CANCEL AN INVALID MSG !!");
return;
}
if ((E_CUS_FROM_UART_MSG == dm_msg->msg.type)
||(E_CUS_FROM_APPTASK_MSG == dm_msg->msg.type)
||(E_CUS_PEROID_TIMER_MSG == dm_msg->msg.type)) {
app_cus_common_cancel(dm_msg->msg.id, dm_msg->data);
}
iot_task_free_msg(task_h, &(dm_msg->msg));
return;
}
/* app 初始化, 创建task, timer等 */
uint16_t app_cus_task_init(void)
{
iot_task_config_t t_cfg;
os_mem_set(&cus_task, 0x0, sizeof(cus_task));
os_mem_set(&t_cfg, 0x0, sizeof(t_cfg));
t_cfg.stack_size = 0;
/* task priority */
t_cfg.task_prio = APP_CUS_HANDLE_TASK_PRIO;
t_cfg.msg_size = sizeof(app_cus_task_msg_t);
/* task message count */
t_cfg.msg_cnt = APP_CUS_PENDING_LIMIT;
/* task message queue count */
t_cfg.queue_cnt = APP_CUS_TASK_PRIO_QUE;
t_cfg.queue_cfg[0].quota = 0;
t_cfg.task_event_func = app_cus_event_handle; /* 事件处理函数 */
t_cfg.msg_exe_func = app_cus_handle; /* 消息处理函数 */
/* function pointer to handle message canceling */
t_cfg.msg_cancel_func = app_cus_cancel;
/* 创建task */
cus_task.task = iot_task_create(IOT_APP_DEMO_MID, &t_cfg);
if (NULL == cus_task.task) {
APP_PRINTF("[ERR] %s Create Task Failed !!", __FUNCTION__);
return ERR_FAIL;
}
/* 串口初始化 */
if (ERR_OK != app_uart_init(&(cus_task.uart_h))) {
APP_PRINTF("[ERR] UART INIT FAILED !!");
return ERR_FAIL;
}
#if IOT_SILA_DEV_ENABLE
iot_sila_dev_init();
iot_sila_dev_plc_send_register(cus_dev_tx_to_app_msg);
#endif
/* 创建定时器 */
cus_task.peroid_timer = os_create_timer(IOT_APP_DEMO_MID, true,
app_cus_task_peroid_timer_exe, NULL);
if (0 == cus_task.peroid_timer) {
iot_cus_printf("[cus_task]create cmd timer failed.\n");
return ERR_FAIL;
}
/* 启动定时器 */
os_start_timer(cus_task.peroid_timer, APP_CUS_TASK_TIMER_PERIOD);
return ERR_OK;
}