Files
kunlun/app/smart_grid_extend/src/iot_cusapp.c

601 lines
16 KiB
C
Raw Normal View History

2024-09-28 14:24:04 +08:00
/****************************************************************************
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.
****************************************************************************/
/* iot common header files */
#include "iot_io_api.h"
#include "iot_module_api.h"
#include "iot_errno_api.h"
#include "iot_task_api.h"
#include "iot_pkt_api.h"
#include "iot_ipc_api.h"
#include "iot_sg_ext_api.h"
#include "iot_board_api.h"
#include "iot_cusapp.h"
#include "iot_socket_api.h"
#include "iot_plc_msg_api.h"
#include "iot_plc_api.h"
#include "iot_app_api.h"
#include "iot_plc_msg_cco_api.h"
#include "iot_plc_msg_sta_api.h"
#include "iot_cusapp_cco.h"
#include "iot_cusapp_sta.h"
#include "iot_oem_api.h"
#if IOT_APP_SELECTION == 5
#define IOT_CUSAPP_MSG_QUEUE 0
#define IOT_CUSAPP_UART_BUF_SIZE (2048 + 64)
#define IOT_CUSAPP_MSG_POOL_SIZE 128
#define IOT_CUSAPP_CMD_PRIO 3
iot_cusapp_t g_iot_cus_lib = { 0 };
/* Target address of cusapp ipc route. */
const iot_ipc_addr_t iot_cusapp_ext_sgapp_addr = \
{IOT_IPC_FID_SG_EXT_SDK, IOT_IPC_CID_SG_EXT_SG_APP};
void iot_cusapp_cmd_msdu_enable(uint8_t enable)
{
uint16_t pkt_len;
iot_pkt_t *pkt;
iot_sg_ext_header_t *p_hdr;
iot_sg_ext_cus_msdu_fwd_enable_t *resp;
pkt_len = sizeof(*p_hdr) + sizeof(*resp);
pkt = iot_pkt_alloc(pkt_len, IOT_SMART_GRID_MID);
if (!pkt) {
goto out;
}
p_hdr = (iot_sg_ext_header_t *)iot_pkt_data(pkt);
p_hdr->mid = IOT_SG_EXT_MID_COMMAND;
p_hdr->arg = IOT_SG_EXT_SID_MSDU_FWD_ENABLE;
resp = (iot_sg_ext_cus_msdu_fwd_enable_t *)(p_hdr + 1);
resp->enable = enable;
iot_pkt_put(pkt, pkt_len);
iot_ipc_send(g_iot_cus_lib.ipc_h,
(iot_ipc_addr_t *)&iot_cusapp_ext_sgapp_addr, pkt);
out:
return;
}
uint32_t iot_cusapp_cmd_uart_config(void *p_data)
{
iot_sg_ext_uart_t *p_cfg = (iot_sg_ext_uart_t *)p_data;
if(NULL == p_cfg || NULL == g_iot_cus_lib.uart_h)
{
return ERR_FAIL;
}
if (iot_uart_set_config(g_iot_cus_lib.uart_h, p_cfg->baudrate,
p_cfg->parity, p_cfg->data, p_cfg->stop))
{
if(p_cfg->fmt_valid)
{
iot_uart_set_frame(g_iot_cus_lib.uart_h, &(p_cfg->fmt));
}
if((IOT_SG_EXT_INVALID_THRD) != p_cfg->thd_rx_tm)
{
iot_uart_set_threshold(g_iot_cus_lib.uart_h, UART_THR_RXTIMEOUT,
p_cfg->thd_rx_tm);
}
if((IOT_SG_EXT_INVALID_THRD) != p_cfg->thd_rx_ful)
{
iot_uart_set_threshold(g_iot_cus_lib.uart_h, UART_THR_RXFULL,
p_cfg->thd_rx_ful);
}
return ERR_OK;
}
return ERR_FAIL;
}
/**
* @brief iot_cusapp_ipc_send() - Send message to sg-app.
* @param p_pkt: packet for sending to sg-app.
* @param arg: argument for sending to sg-app.
*/
static void iot_cusapp_ipc_send(iot_pkt_t *p_pkt, uint32_t arg)
{
(void)arg;
iot_ipc_send(g_iot_cus_lib.ipc_h,
(iot_ipc_addr_t *)&iot_cusapp_ext_sgapp_addr, p_pkt);
return;
}
/**
* @brief iot_cusapp_wireless_send() - Send data to wireless device.
* @param p_pkt: packet for sending to wireless device.
* @param arg: argument for sending to wireless device.
*/
static void iot_cusapp_wireless_send(iot_pkt_t *p_pkt, uint32_t arg)
{
(void)arg;
/* TODO */
iot_pkt_free(p_pkt);
return;
}
uint32_t iot_cusapp_data_path_get(void)
{
/* TODO */
return CUSAPP_PT_PLC;
}
static uint32_t iot_cusapp_plc_msg_process(iot_cusapp_msg_t *p_cus_msg)
{
iot_pkt_t *p_pkt = (iot_pkt_t *)p_cus_msg->data;
IOT_ASSERT(p_pkt);
iot_plc_msg_header_t *hdr = NULL;
hdr = (iot_plc_msg_header_t*)iot_pkt_block_ptr(p_pkt, IOT_PKT_BLOCK_DATA);
iot_plc_app_t *host_app = g_iot_cus_lib.app_handle;
uint32_t consumed = 0;
iot_cus_printf("%s recv plc msg, app id:%lu, msg id:%lu, req id:%lu\n",
__FUNCTION__, hdr->app_id, hdr->msg_id, hdr->req_id);
if (hdr->app_id != IOT_PLC_APP_ID_BCAST &&
host_app->app_id != hdr->app_id) {
iot_pkt_free(p_pkt);
goto out;
}
switch (hdr->msg_id) {
case IOT_PLC_MSG_APP_REG_CONF:
{
iot_plc_app_reg_conf_t* rpt = (iot_plc_app_reg_conf_t*)(hdr + 1);
iot_cus_printf("%s PLC lib registered msdu type %lu, prio %lu\n",
__FUNCTION__, rpt->type, rpt->prio);
break;
}
case IOT_PLC_MSG_DEV_STATE_CHANGE_RPT:
{
iot_plc_dev_state_change_rpt_t *dev_state;
dev_state = (iot_plc_dev_state_change_rpt_t*)(hdr + 1);
iot_cus_printf("dev state change, local_mac:"
"[%02X:%02X:%02X:%02X:%02X:%02X], cco_mac:"
"[%02X:%02X:%02X:%02X:%02X:%02X] \n", dev_state->local_mac[0],
dev_state->local_mac[1], dev_state->local_mac[2],
dev_state->local_mac[3], dev_state->local_mac[4],
dev_state->local_mac[5], dev_state->cco_mac[0],
dev_state->cco_mac[1], dev_state->cco_mac[2],
dev_state->cco_mac[3], dev_state->cco_mac[4],
dev_state->cco_mac[5]);
break;
}
default:
break;
}
out:
if (consumed) {
iot_pkt_free(p_pkt);
}
return consumed;
}
/**
* @brief iot_cusapp_msg_exe_func() - messages handle function.
* @param task_h: handle of task.
* @param p_msg: message that will be processed.
*/
static void iot_cusapp_msg_exe_func(iot_task_h task_h, iot_task_msg_t *p_msg)
{
iot_cusapp_msg_t *p_cus_msg = (iot_cusapp_msg_t *)p_msg;
uint32_t consumed = 0;
switch (p_cus_msg->task_msg.type)
{
#if !IOT_CUSAPP_SOCKET_TO_CCTT_ENABLE
case IOT_CUSAPP_MT_UART:
{
break;
}
#else
case IOT_CUSAPP_MT_SOCKET:
{
break;
}
#endif /* !IOT_CUSAPP_SOCKET_TO_CCTT_ENABLE */
case IOT_CUSAPP_MT_SG:
{
break;
}
case IOT_CUSAPP_MT_WL:
{
break;
}
case IOT_CUSAPP_MT_PLC:
{
consumed = iot_cusapp_plc_msg_process(p_cus_msg);
break;
}
default:
{
iot_pkt_free(p_cus_msg->data);
IOT_ASSERT(0);
break;
}
}
if (consumed == 0) {
g_iot_cus_lib.msg_exe_func(p_cus_msg);
} else {
iot_task_free_msg(task_h, p_msg);
}
return;
}
/**
* @brief iot_cusapp_msg_cancel_func() - pull back messages that sent to this
* task.
* @param task_h: handle of task.
* @param p_msg: message that will be pull back.
*/
static void iot_cusapp_msg_cancel_func(iot_task_h task_h, iot_task_msg_t *p_msg)
{
(void)task_h;
iot_cusapp_msg_t *sg_msg = (iot_cusapp_msg_t *)p_msg;
g_iot_cus_lib.msg_cancel_func(sg_msg);
return;
}
/**
* @brief iot_cusapp_ipc_receive_func() - Receive uart data from IPC
* (sgapp need to post to uart or cusapp).
* @param p_param: param from IPC module.
* @param p_addr: address of sender from IPC module.
* @param p_pkt: packet from sg-app.
*/
static void iot_cusapp_ipc_receive_func(void *p_param,
iot_ipc_addr_t *p_addr, iot_pkt_t *p_pkt)
{
iot_task_msg_t *p_msg;
iot_cusapp_msg_t *p_cus_msg;
if (p_pkt)
{
p_msg = iot_task_alloc_msg(g_iot_cus_lib.task_h);
if (p_msg)
{
p_cus_msg = (iot_cusapp_msg_t *)p_msg;
p_cus_msg->data = p_pkt;
p_msg->type = IOT_CUSAPP_MT_SG;
iot_task_queue_msg(g_iot_cus_lib.task_h, p_msg,
IOT_CUSAPP_MSG_QUEUE);
}
else
{
IOT_ASSERT(0);
iot_pkt_free(p_pkt);
}
}
return;
}
void iot_cusapp_send_msdu_to_sg(iot_pkt_t *pkt)
{
iot_sg_ext_header_t *p_hdr;
if (!pkt) {
goto out;
}
p_hdr = (iot_sg_ext_header_t *)iot_pkt_push(pkt, sizeof(*p_hdr));
p_hdr->mid = IOT_SG_EXT_MID_TRANS_MSDU_FROM_CUS;
p_hdr->arg = IOT_SG_EXT_SID_DEFAULT;
iot_ipc_send(g_iot_cus_lib.ipc_h,
(iot_ipc_addr_t *)&iot_cusapp_ext_sgapp_addr, pkt);
out:
return;
}
void iot_cusapp_send_msdu_to_plc(uint8_t msg_type, uint8_t* dest_mac,
uint8_t pro, uint8_t retry_cnt, uint16_t len, uint8_t *data)
{
uint16_t pkt_len;
iot_pkt_t *pkt;
iot_sg_ext_header_t *p_hdr;
iot_sg_ext_cus_msdu_send_data_t *msdu_data;
pkt_len = sizeof(*p_hdr) + sizeof(*msdu_data) + len;
pkt = iot_pkt_alloc(pkt_len, IOT_SMART_GRID_MID);
if (!pkt) {
goto out;
}
p_hdr = (iot_sg_ext_header_t *)iot_pkt_data(pkt);
p_hdr->mid = IOT_SG_EXT_MID_MSDU_SEND;
p_hdr->arg = IOT_SG_EXT_SID_DEFAULT;
msdu_data = (iot_sg_ext_cus_msdu_send_data_t *)p_hdr->data;
iot_mac_addr_cpy(msdu_data->dest_mac, dest_mac);
msdu_data->msg_type = msg_type;
msdu_data->pro = pro;
msdu_data->retry_cnt = retry_cnt;
msdu_data->len = len;
os_mem_cpy(msdu_data->data, data, len);
iot_pkt_put(pkt, pkt_len);
iot_ipc_send(g_iot_cus_lib.ipc_h,
(iot_ipc_addr_t *)&iot_cusapp_ext_sgapp_addr, pkt);
out:
return;
}
uint32_t iot_cusapp_uart_send(iot_pkt_t *p_pkt)
{
uint32_t ret;
ret = iot_uart_send(g_iot_cus_lib.uart_h, p_pkt, NULL);
return (ERR_OK == ret) ? ERR_OK : ERR_FAIL;
}
#if !IOT_CUSAPP_SOCKET_TO_CCTT_ENABLE
/**
* @brief iot_cusapp_uart_receive_func() - Uart driver callback function
* when data received.
* @param buffer: pointer of data buffer.
* @param buffer_len: length of data received.
* @param is_full_frame: tell if this is a whole frame in this buffer.
* @param invalid_data_len: length of invalid data received. we ignore this.
*/
static void iot_cusapp_uart_receive_func(uint8_t* p_buffer, uint32_t buffer_len,
bool_t is_full_frame, uint32_t invalid_data_len)
{
iot_task_msg_t *p_msg;
iot_pkt_t *p_pkt;
iot_cusapp_msg_t *p_cus_msg;
(void)invalid_data_len;
p_pkt = iot_pkt_alloc(buffer_len + IOT_SG_EXT_HEADROOM,
IOT_SMART_GRID_MID);
if (p_pkt)
{
/* reserved bytes for iot_sg_ext_header_t */
(void)iot_pkt_reserve(p_pkt, IOT_SG_EXT_HEADROOM);
p_msg = iot_task_alloc_msg(g_iot_cus_lib.task_h);
if (p_msg)
{
os_mem_cpy(iot_pkt_put(p_pkt, buffer_len), p_buffer, buffer_len);
p_cus_msg = (iot_cusapp_msg_t *)p_msg;
p_cus_msg->data = p_pkt;
if (is_full_frame)
{
p_cus_msg->data2 = 1;
}
else
{
p_cus_msg->data2 = 0;
}
p_msg->type = IOT_CUSAPP_MT_UART;
iot_task_queue_msg(g_iot_cus_lib.task_h, p_msg,
IOT_CUSAPP_MSG_QUEUE);
}
else
{
iot_pkt_free(p_pkt);
}
}
return;
}
#else
/**
* @brief: callback method to receive data or event on the socket
* @param cb_type: type of event that trigger the callback
* @param socket: the socket on which event occur
* @param pkt: iot_pkt contains data for the callback.
* this method shall free the pkt.
*/
static void iot_cusapp_udp_recv_func(uint32_t cb_type,
int32_t socket, iot_pkt_t *pkt)
{
(void)socket;
/* deliver the message to smart grid task for handling */
iot_task_msg_t *p_msg;
iot_cusapp_msg_t *p_cus_msg;
if (pkt == NULL) {
return;
}
if (cb_type != IOT_SOCKET_CB_DATA_RECV) {
/* incorrect type */
iot_pkt_free(pkt);
return;
}
p_msg = iot_task_alloc_msg(g_iot_cus_lib.task_h);
if (p_msg) {
p_cus_msg = (iot_cusapp_msg_t *)p_msg;
p_cus_msg->data = pkt;
p_cus_msg->data2 = 1;
p_msg->type = IOT_CUSAPP_MT_SOCKET;
iot_task_queue_msg(g_iot_cus_lib.task_h, p_msg,
IOT_CUSAPP_MSG_QUEUE);
} else {
iot_pkt_free(pkt);
}
}
#endif /* !IOT_CUSAPP_SOCKET_TO_CCTT_ENABLE */
static void iot_cusapp_plc_recv_func(void *param, iot_pkt_t *pkt)
{
(void)param;
iot_task_msg_t *t_msg = iot_task_alloc_msg(g_iot_cus_lib.task_h);
if (t_msg) {
iot_cusapp_msg_t *msg = (iot_cusapp_msg_t *)t_msg;
msg->data = pkt;
t_msg->type = IOT_CUSAPP_MT_PLC;
t_msg->id = IOT_CUSAPP_ID_MSG_PLC;
iot_task_queue_msg(g_iot_cus_lib.task_h, t_msg, IOT_CUSAPP_MSG_QUEUE);
}
}
static uint8_t iot_cusapp_register()
{
iot_plc_app_t host_app = { 0 };
/* the demo app id is a example, tongke should modify to IOT_TK_APP_ID
* in actual use.
*/
host_app.app_id = IOT_PLC_APP_DEMO_ID;
host_app.param = &g_iot_cus_lib;
host_app.prio = IOT_CUSAPP_CMD_PRIO;
host_app.recv = iot_cusapp_plc_recv_func;
g_iot_cus_lib.app_handle = iot_plc_register_app(&host_app);
if (g_iot_cus_lib.app_handle == NULL) {
iot_cus_printf("cus App handle NULL (%s)\n", __FUNCTION__);
return ERR_INVAL;
}
return ERR_OK;
}
/**
* @brief iot_cusapp_init() - The main entry to initialize our customer app.
*/
uint32_t iot_cusapp_init(void)
{
iot_ipc_client_t client;
uint32_t ret = ERR_FAIL;
client.addr.f_id = IOT_IPC_FID_SG_EXT_SDK;
client.addr.c_id = IOT_IPC_CID_SG_EXT_CUS_APP;
client.recv = iot_cusapp_ipc_receive_func;
client.param = NULL;
/* Make sure all items have 'zero' value for error_out lable. */
os_mem_set(&g_iot_cus_lib, 0x0, sizeof(g_iot_cus_lib));
g_iot_cus_lib.ipc_h = iot_ipc_register_client(&client);
if (g_iot_cus_lib.ipc_h == NULL)
{
goto error_out;
}
g_iot_cus_lib.module_type = iot_oem_get_module_type();
if (g_iot_cus_lib.module_type == MODULE_TYPE_CCO) {
ret = iot_cusapp_cco_init();
} else {
ret = iot_cusapp_sta_init();
}
if (ret != ERR_OK) {
iot_cus_printf("\ncusapp init failed!");
goto error_plc;
}
/* Task configration. */
g_iot_cus_lib.task_cfg.stack_size = 0;
g_iot_cus_lib.task_cfg.task_prio = IOT_SMART_GRID_TASK_PRIO;
g_iot_cus_lib.task_cfg.msg_size = sizeof(iot_cusapp_msg_t);
g_iot_cus_lib.task_cfg.msg_cnt = IOT_CUSAPP_MSG_POOL_SIZE;
g_iot_cus_lib.task_cfg.queue_cnt = 1;
g_iot_cus_lib.task_cfg.queue_cfg[0].quota = 0;
g_iot_cus_lib.task_cfg.msg_exe_func = iot_cusapp_msg_exe_func;
g_iot_cus_lib.task_cfg.msg_cancel_func = iot_cusapp_msg_cancel_func;
/* Create task. */
g_iot_cus_lib.task_h = iot_task_create(IOT_SMART_GRID_MID,
&g_iot_cus_lib.task_cfg);
if(NULL == g_iot_cus_lib.task_h)
{
iot_cus_printf("\ncreate task failed!");
goto error_role;
}
if (iot_cusapp_register() != ERR_OK) {
iot_cus_printf("\ncusapp register failed!");
goto error_task;
}
g_iot_cus_lib.path[CUSAPP_PT_PLC] = iot_cusapp_ipc_send;
g_iot_cus_lib.path[CUSAPP_PT_WIERLESS] = iot_cusapp_wireless_send;
#if !IOT_CUSAPP_SOCKET_TO_CCTT_ENABLE
g_iot_cus_lib.uart_h = iot_uart_open(iot_board_get_uart(UART_METER_PORT),
iot_cusapp_uart_receive_func, IOT_CUSAPP_UART_BUF_SIZE, NULL);
if(NULL == g_iot_cus_lib.uart_h)
{
iot_cus_printf("\nopen uart failed!");
goto error_task;
}
#else
iot_socket_create(IOT_SOCKET_TYPE_UDP, 0, NULL,
IOT_CUSAPP_SOCKET_LOCAL_PORT, iot_cusapp_udp_recv_func,
IOT_SG_EXT_HEADROOM, (&g_iot_cus_lib.socket_h));
#endif
iot_cus_printf("\nCUSAPP initialization successful.");
return ERR_OK;
error_task:
iot_task_delete(g_iot_cus_lib.task_h);
g_iot_cus_lib.task_h = NULL;
error_role:
if (g_iot_cus_lib.module_type == MODULE_TYPE_CCO) {
iot_cusapp_cco_deinit();
} else {
iot_cusapp_sta_deinit();
}
error_plc:
(void)iot_ipc_deregister_client(g_iot_cus_lib.ipc_h);
g_iot_cus_lib.ipc_h = NULL;
error_out:
iot_cus_printf("\nCUSAPP initialization failed.");
return ERR_FAIL;
}
uint32_t app_cusapp_entry()
{
uint32_t ret = ERR_PENDING;
if (ERR_OK == iot_cusapp_init())
{
ret = ERR_OK;
}
return ret;
}
#endif /* IOT_APP_SELECTION == 5 */