481 lines
14 KiB
C
481 lines
14 KiB
C
|
/****************************************************************************
|
||
|
|
||
|
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_sta_api.h"
|
||
|
#include "iot_sg_ext_sta_api.h"
|
||
|
|
||
|
extern iot_cusapp_t g_iot_cus_lib;
|
||
|
extern iot_ipc_addr_t iot_cusapp_ext_sgapp_addr;
|
||
|
|
||
|
/* read meter command reserved length */
|
||
|
#define IOT_CUSAPP_MR_CMD_RSVD_LEN (40)
|
||
|
|
||
|
void iot_cusapp_send_mr_cmd_to_sg(uint8_t proto_type, uint8_t timeout,
|
||
|
uint8_t sn, uint8_t queue_front, uint16_t len, uint8_t *data)
|
||
|
{
|
||
|
iot_sg_ext_header_t *p_hdr;
|
||
|
iot_pkt_t *pkt;
|
||
|
iot_sg_ext_cus_mr_req_t *req;
|
||
|
pkt = iot_pkt_alloc(IOT_CUSAPP_MR_CMD_RSVD_LEN + sizeof(*p_hdr) +
|
||
|
sizeof(*req) + len, IOT_SMART_GRID_MID);
|
||
|
IOT_ASSERT(pkt);
|
||
|
p_hdr = (iot_sg_ext_header_t *)iot_pkt_reserve(pkt,
|
||
|
IOT_CUSAPP_MR_CMD_RSVD_LEN);
|
||
|
p_hdr->mid = IOT_SG_EXT_MID_COMMAND;
|
||
|
p_hdr->arg = IOT_SG_EXT_SID_MR_CMD;
|
||
|
|
||
|
req = (iot_sg_ext_cus_mr_req_t *)(p_hdr + 1);
|
||
|
req->proto_type = proto_type;
|
||
|
req->dev_timeout = timeout;
|
||
|
req->sn = sn;
|
||
|
req->queue_front = !!queue_front;
|
||
|
req->len = len;
|
||
|
|
||
|
os_mem_cpy(req->data, data, len);
|
||
|
iot_pkt_put(pkt, sizeof(*p_hdr) + sizeof(*req) + len);
|
||
|
iot_ipc_send(g_iot_cus_lib.ipc_h, &iot_cusapp_ext_sgapp_addr, pkt);
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
void iot_cusapp_sta_send_expand_stop_cmd_to_sg()
|
||
|
{
|
||
|
iot_sg_ext_header_t *p_hdr;
|
||
|
iot_pkt_t *pkt;
|
||
|
iot_sg_ext_cus_cfg_t *cfg;
|
||
|
iot_sg_ext_cus_cfg_param_t *param;
|
||
|
pkt = iot_pkt_alloc(sizeof(*p_hdr) + sizeof(*cfg) + sizeof(*param),
|
||
|
IOT_SMART_GRID_MID);
|
||
|
IOT_ASSERT(pkt);
|
||
|
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_MR_CFG;
|
||
|
|
||
|
cfg = (iot_sg_ext_cus_cfg_t *)(p_hdr + 1);
|
||
|
cfg->cfg_id = IOT_SG_EXT_CFG_ID_PARAM;
|
||
|
os_mem_set(cfg->data, 0, sizeof(*param));
|
||
|
param = (iot_sg_ext_cus_cfg_param_t *)cfg->data;
|
||
|
param->stop_flag_valid = 1;
|
||
|
param->stop_flag = 1;
|
||
|
|
||
|
iot_pkt_put(pkt, sizeof(*p_hdr) + sizeof(*cfg) + sizeof(*param));
|
||
|
iot_ipc_send(g_iot_cus_lib.ipc_h, &iot_cusapp_ext_sgapp_addr, pkt);
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* @brief iot_cusapp_sta_plc_msg_process() - process data from plc.
|
||
|
* @param p_cus_msg: message received from plc. p_cus_msg->data
|
||
|
* will be freed by this handle but p_cus_msg will not.
|
||
|
*/
|
||
|
static void iot_cusapp_sta_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;
|
||
|
uint8_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);
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
switch (hdr->msg_id) {
|
||
|
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;
|
||
|
}
|
||
|
case IOT_PLC_MSG_PHASE_RPT:
|
||
|
{
|
||
|
iot_plc_phase_rpt_t *phase;
|
||
|
phase = (iot_plc_phase_rpt_t*)(hdr + 1);
|
||
|
iot_cus_printf("phase info, phy_phase:%lu, opposite:%lu, opposite_3p"
|
||
|
":%lu \n", phase->phy_phase, phase->opposite_phase,
|
||
|
phase->opposite_3p);
|
||
|
break;
|
||
|
}
|
||
|
case IOT_PLC_MSG_DISCOVERY_NODE_RPT:
|
||
|
{
|
||
|
iot_plc_discovery_node_rpt_t *dis_node;
|
||
|
dis_node = (iot_plc_discovery_node_rpt_t*)(hdr + 1);
|
||
|
iot_cus_printf("discovery node, addr[%02X:%02X:%02X:%02X:%02X:%02X]"
|
||
|
" cco_addr[%02X:%02X:%02X:%02X:%02X:%02X] \n",
|
||
|
dis_node->addr[0], dis_node->addr[1], dis_node->addr[2],
|
||
|
dis_node->addr[3], dis_node->addr[4], dis_node->addr[5],
|
||
|
dis_node->cco_addr[0], dis_node->cco_addr[1],
|
||
|
dis_node->cco_addr[2], dis_node->cco_addr[3],
|
||
|
dis_node->cco_addr[4], dis_node->cco_addr[5]);
|
||
|
break;
|
||
|
}
|
||
|
case IOT_PLC_MSG_RTC_UPDATE_RPT:
|
||
|
{
|
||
|
iot_plc_rtc_update_rpt_t *rtc_update;
|
||
|
rtc_update = (iot_plc_rtc_update_rpt_t*)(hdr + 1);
|
||
|
iot_cus_printf("rtc update, cco_date:%lu, cco_ntb:%lu \n",
|
||
|
rtc_update->cco_date, rtc_update->cco_ntb);
|
||
|
break;
|
||
|
}
|
||
|
case IOT_PLC_MSG_INVALID_MAC_RPT:
|
||
|
{
|
||
|
iot_plc_invalid_mac_rpt_t *invalid_mac;
|
||
|
invalid_mac = (iot_plc_invalid_mac_rpt_t*)(hdr + 1);
|
||
|
iot_cus_printf("invalid mac, reason:%lu,"
|
||
|
"mac[%02X:%02X:%02X:%02X:%02X:%02X],"
|
||
|
"cco_mac[%02X:%02X:%02X:%02X:%02X:%02X] \n", invalid_mac->reason,
|
||
|
invalid_mac->mac[0], invalid_mac->mac[1], invalid_mac->mac[2],
|
||
|
invalid_mac->mac[3], invalid_mac->mac[4], invalid_mac->mac[5],
|
||
|
invalid_mac->cco_mac[0], invalid_mac->cco_mac[1],
|
||
|
invalid_mac->cco_mac[2], invalid_mac->cco_mac[3],
|
||
|
invalid_mac->cco_mac[4], invalid_mac->cco_mac[5]);
|
||
|
break;
|
||
|
}
|
||
|
default:
|
||
|
break;
|
||
|
}
|
||
|
|
||
|
/* TODO: cost in this module or give to other module. */
|
||
|
if (!consumed) {
|
||
|
iot_pkt_free(p_pkt);
|
||
|
}
|
||
|
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
|
||
|
/**
|
||
|
* @brief iot_cusapp_sta_wl_msg_process() - process data from wireless device.
|
||
|
* @param p_cus_msg: message received from wireless device. p_cus_msg->data
|
||
|
* will be freed by this handle but p_cus_msg will not.
|
||
|
*/
|
||
|
static void iot_cusapp_sta_wl_msg_process(iot_cusapp_msg_t *p_cus_msg)
|
||
|
{
|
||
|
/* TODO: cost in this module or give to other module. */
|
||
|
iot_pkt_free((iot_pkt_t *)p_cus_msg->data);
|
||
|
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* @brief iot_cusapp_sta_cmd_handle_msdu_data() - handle msdu data.
|
||
|
* @param data: msdu data.
|
||
|
*/
|
||
|
static void iot_cusapp_sta_cmd_handle_msdu_data(iot_pkt_t *pkt)
|
||
|
{
|
||
|
iot_plc_msdu_recv_t *msdu;
|
||
|
|
||
|
if (pkt == NULL) {
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
msdu = (iot_plc_msdu_recv_t *)iot_pkt_data(pkt);
|
||
|
(void)msdu;
|
||
|
/* modify condition according to actual demand, and return unwanted msdu
|
||
|
* data to sg.
|
||
|
*/
|
||
|
if (0) {
|
||
|
/* handle msdu data deal with cusapp's demand */
|
||
|
} else {
|
||
|
iot_cusapp_send_msdu_to_sg(pkt);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* @brief iot_cusapp_sta_command_exe() - command execute top function.
|
||
|
* @param sid: id of command.
|
||
|
* @param data: data for this command.
|
||
|
*/
|
||
|
static void iot_cusapp_sta_command_exe(uint32_t sid, void *data)
|
||
|
{
|
||
|
iot_sg_ext_dev_info_t *dev_info;
|
||
|
switch(sid)
|
||
|
{
|
||
|
case IOT_SG_EXT_SID_UART_CFG:
|
||
|
{
|
||
|
iot_cusapp_cmd_uart_config(data);
|
||
|
break;
|
||
|
}
|
||
|
case IOT_SG_EXT_SID_DEV_INFO_SYNC:
|
||
|
{
|
||
|
dev_info = (iot_sg_ext_dev_info_t*)data;
|
||
|
iot_cus_printf("pm addr: %02X:%02X:%02X:%02X:%02X:%02X\n",
|
||
|
dev_info->addr[0], dev_info->addr[1], dev_info->addr[2],
|
||
|
dev_info->addr[3], dev_info->addr[4], dev_info->addr[5]);
|
||
|
break;
|
||
|
}
|
||
|
case IOT_SG_EXT_SID_MR_CMD:
|
||
|
{
|
||
|
iot_sg_ext_cus_mr_resp_t *rsp =
|
||
|
(iot_sg_ext_cus_mr_resp_t *)data;
|
||
|
(void)rsp;
|
||
|
break;
|
||
|
}
|
||
|
default:
|
||
|
{
|
||
|
IOT_ASSERT(0);
|
||
|
break;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* @brief iot_cusapp_sta_sg_msg_process() - process the data from sg-app.
|
||
|
* @param p_cus_msg: message received from sg-app. p_cus_msg->data
|
||
|
* will be freed by this handle but p_cus_msg will not.
|
||
|
*/
|
||
|
static void iot_cusapp_sta_sg_msg_process(iot_cusapp_msg_t *p_cus_msg)
|
||
|
{
|
||
|
iot_pkt_t *p_pkt = (iot_pkt_t *)p_cus_msg->data;
|
||
|
iot_sg_ext_header_t *p_header;
|
||
|
iot_sg_ext_upgrade_state_t *upgrade_state;
|
||
|
|
||
|
p_header = (iot_sg_ext_header_t *)iot_pkt_data(p_pkt);
|
||
|
|
||
|
switch (p_header->mid)
|
||
|
{
|
||
|
case IOT_SG_EXT_MID_UART_DATA_FROM_SG:
|
||
|
{
|
||
|
/* Pull header out of packet. */
|
||
|
iot_pkt_pull(p_pkt, sizeof(*p_header));
|
||
|
|
||
|
#if !IOT_CUSAPP_SOCKET_TO_CCTT_ENABLE
|
||
|
/* Send to uart port. */
|
||
|
iot_cusapp_uart_send(p_pkt);
|
||
|
#else
|
||
|
/* Send data by eth socket */
|
||
|
iot_socket_udp_send(g_iot_cus_lib.socket_h, iot_pkt_data(p_pkt),
|
||
|
iot_pkt_data_len(p_pkt), 0, IOT_CUSAPP_SOCKET_REMOTE_IP,
|
||
|
IOT_CUSAPP_SOCKET_REMOTE_PORT);
|
||
|
iot_pkt_free(p_pkt);
|
||
|
#endif
|
||
|
|
||
|
/* Set p_pkt as NULL avoid of free() repeatly. */
|
||
|
p_pkt = NULL;
|
||
|
break;
|
||
|
}
|
||
|
case IOT_SG_EXT_MID_LINK_STATE_FROM_SG:
|
||
|
{
|
||
|
/* TODO */
|
||
|
break;
|
||
|
}
|
||
|
case IOT_SG_EXT_MID_COMMAND:
|
||
|
{
|
||
|
iot_cusapp_sta_command_exe(((uint32_t)p_header->arg) & 0xFF,
|
||
|
p_header->data);
|
||
|
break;
|
||
|
}
|
||
|
case IOT_SG_EXT_MID_UPGRADE_STATE_FROM_SG:
|
||
|
{
|
||
|
upgrade_state = (iot_sg_ext_upgrade_state_t *)p_header->data;
|
||
|
iot_cus_printf("upgrade state:%d\n", upgrade_state->upgrade_state);
|
||
|
break;
|
||
|
}
|
||
|
case IOT_SG_EXT_MID_TRANS_MSDU_FROM_SG:
|
||
|
{
|
||
|
/* Pull header out of packet. */
|
||
|
iot_pkt_pull(p_pkt, sizeof(*p_header));
|
||
|
iot_cusapp_sta_cmd_handle_msdu_data(p_pkt);
|
||
|
/* Set p_pkt as NULL avoid of free() repeatly. */
|
||
|
p_pkt = NULL;
|
||
|
break;
|
||
|
}
|
||
|
default:
|
||
|
{
|
||
|
IOT_ASSERT(0);
|
||
|
break;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
if (NULL != p_pkt)
|
||
|
{
|
||
|
iot_pkt_free(p_pkt);
|
||
|
}
|
||
|
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
|
||
|
/**
|
||
|
* @brief iot_cusapp_sta_uart_msg_process() - process the data from uart.
|
||
|
* @param p_cus_msg: message received from uart. p_cus_msg->data will be
|
||
|
* freed by this handle but p_cus_msg will not.
|
||
|
*/
|
||
|
static void iot_cusapp_sta_uart_msg_process(iot_cusapp_msg_t *p_cus_msg)
|
||
|
{
|
||
|
uint32_t path;
|
||
|
iot_pkt_t *p_pkt = (iot_pkt_t *)p_cus_msg->data;
|
||
|
iot_sg_ext_header_t *p_header;
|
||
|
|
||
|
path = iot_cusapp_data_path_get();
|
||
|
|
||
|
if ((CUSAPP_PT_MAX <= path)
|
||
|
|| (NULL == g_iot_cus_lib.path[path]))
|
||
|
{
|
||
|
/* drop this packet. */
|
||
|
iot_pkt_free(p_pkt);
|
||
|
}
|
||
|
|
||
|
switch (path)
|
||
|
{
|
||
|
case CUSAPP_PT_PLC:
|
||
|
{
|
||
|
p_header = (iot_sg_ext_header_t *)iot_pkt_push(p_pkt,
|
||
|
sizeof(*p_header));
|
||
|
|
||
|
p_header->mid = IOT_SG_EXT_MID_UART_DATA_FROM_CUS;
|
||
|
|
||
|
p_header->arg = (p_cus_msg->data2) ? IOT_SG_EXT_DF_FULL :
|
||
|
IOT_SG_EXT_DF_PART;
|
||
|
|
||
|
g_iot_cus_lib.path[CUSAPP_PT_PLC](p_pkt, 0);
|
||
|
|
||
|
break;
|
||
|
}
|
||
|
case CUSAPP_PT_WIERLESS:
|
||
|
{
|
||
|
/* TODO */
|
||
|
g_iot_cus_lib.path[CUSAPP_PT_WIERLESS](p_pkt, 0);
|
||
|
|
||
|
break;
|
||
|
}
|
||
|
default:
|
||
|
{
|
||
|
iot_pkt_free(p_pkt);
|
||
|
IOT_ASSERT(0);
|
||
|
break;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
|
||
|
static void iot_cusapp_sta_msg_exe_func(iot_cusapp_msg_t *msg)
|
||
|
{
|
||
|
switch (msg->task_msg.type) {
|
||
|
#if !IOT_CUSAPP_SOCKET_TO_CCTT_ENABLE
|
||
|
case IOT_CUSAPP_MT_UART:
|
||
|
{
|
||
|
iot_cusapp_sta_uart_msg_process(msg);
|
||
|
break;
|
||
|
}
|
||
|
#else
|
||
|
case IOT_CUSAPP_MT_SOCKET:
|
||
|
{
|
||
|
iot_cusapp_sta_uart_msg_process(msg);
|
||
|
break;
|
||
|
}
|
||
|
#endif /* !IOT_CUSAPP_SOCKET_TO_CCTT_ENABLE */
|
||
|
case IOT_CUSAPP_MT_SG:
|
||
|
{
|
||
|
iot_cusapp_sta_sg_msg_process(msg);
|
||
|
break;
|
||
|
}
|
||
|
case IOT_CUSAPP_MT_WL:
|
||
|
{
|
||
|
iot_cusapp_sta_wl_msg_process(msg);
|
||
|
break;
|
||
|
}
|
||
|
case IOT_CUSAPP_MT_PLC:
|
||
|
{
|
||
|
iot_cusapp_sta_plc_msg_process(msg);
|
||
|
break;
|
||
|
}
|
||
|
default:
|
||
|
{
|
||
|
iot_pkt_free(msg->data);
|
||
|
IOT_ASSERT(0);
|
||
|
break;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
iot_task_free_msg(g_iot_cus_lib.task_h, &msg->task_msg);
|
||
|
|
||
|
}
|
||
|
|
||
|
static void iot_cusapp_sta_msg_cancel_func(iot_cusapp_msg_t *msg)
|
||
|
{
|
||
|
switch(msg->task_msg.type)
|
||
|
{
|
||
|
#if IOT_CUSAPP_SOCKET_TO_CCTT_ENABLE
|
||
|
case IOT_CUSAPP_MT_SOCKET:
|
||
|
#else
|
||
|
case IOT_CUSAPP_MT_UART:
|
||
|
#endif
|
||
|
case IOT_CUSAPP_MT_SG:
|
||
|
case IOT_CUSAPP_MT_WL:
|
||
|
case IOT_CUSAPP_MT_PLC:
|
||
|
{
|
||
|
iot_pkt_free(msg->data);
|
||
|
break;
|
||
|
}
|
||
|
default:
|
||
|
{
|
||
|
IOT_ASSERT(0);
|
||
|
break;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
iot_task_free_msg(g_iot_cus_lib.task_h, &msg->task_msg);
|
||
|
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
uint32_t iot_cusapp_sta_init()
|
||
|
{
|
||
|
uint32_t ret = ERR_OK;
|
||
|
|
||
|
g_iot_cus_lib.msg_exe_func = iot_cusapp_sta_msg_exe_func;
|
||
|
g_iot_cus_lib.msg_cancel_func = iot_cusapp_sta_msg_cancel_func;
|
||
|
|
||
|
return ret;
|
||
|
}
|
||
|
|
||
|
void iot_cusapp_sta_deinit()
|
||
|
{
|
||
|
return;
|
||
|
}
|