337 lines
		
	
	
		
			9.3 KiB
		
	
	
	
		
			C
		
	
	
		
			Executable File
		
	
	
	
	
			
		
		
	
	
			337 lines
		
	
	
		
			9.3 KiB
		
	
	
	
		
			C
		
	
	
		
			Executable File
		
	
	
	
	
| /****************************************************************************
 | |
| 
 | |
| 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"
 | |
| 
 | |
| /* smart grid internal header files */
 | |
| #include "iot_sg.h"
 | |
| #include "iot_sg_sta_gw.h"
 | |
| 
 | |
| #if IOT_SG_EXT_SDK_ENABLE
 | |
| 
 | |
| iot_ipc_h iot_sg_ext_ipc_h;
 | |
| const iot_ipc_addr_t iot_sg_ext_cusapp_addr = \
 | |
|     {IOT_IPC_FID_SG_EXT_SDK, IOT_IPC_CID_SG_EXT_CUS_APP};
 | |
| 
 | |
| /**
 | |
|  * @brief iot_sg_ext_msdu_func() - Post a command to sg-task.
 | |
|  * @param p_pkt: packet of command from cus-app.
 | |
|  * @param sub_mid: sub_mid of command, see enum _iot_sg_ext_command_id.
 | |
|  */
 | |
| static void iot_sg_ext_msdu_func(iot_pkt_t *p_pkt, uint8_t sub_mid)
 | |
| {
 | |
|     /* deliver the message to smart grid task for handling */
 | |
|     iot_task_msg_t *t_msg;
 | |
| 
 | |
|     if (p_pkt) {
 | |
|         t_msg = iot_task_alloc_msg(p_sg_glb->task_h);
 | |
|         if (t_msg) {
 | |
|             iot_sg_msg_t *msg = (iot_sg_msg_t *)t_msg;
 | |
|             msg->data = p_pkt;
 | |
|             msg->data2 = sub_mid;
 | |
|             t_msg->type = IOT_SG_MSG_TYPE_COMMAND;
 | |
|             t_msg->id = IOT_SG_MSG_ID_MSDU_CUS;
 | |
|             iot_task_queue_msg(p_sg_glb->task_h, t_msg, IOT_SG_MSG_QUEUE_LP);
 | |
|         } else {
 | |
|             IOT_ASSERT(0);
 | |
|             iot_pkt_free(p_pkt);
 | |
|         }
 | |
|     }
 | |
| 
 | |
|     return;
 | |
| }
 | |
| 
 | |
| /**
 | |
|  * @brief iot_sg_ext_msdu_send_func() - send msdu data to plc.
 | |
|  * @param p_pkt: packet of command from cus-app.
 | |
|  * @param sub_mid: sub_mid of command, see enum _iot_sg_ext_command_id.
 | |
|  */
 | |
| static void iot_sg_ext_msdu_send_func(iot_pkt_t *p_pkt, uint8_t sub_mid)
 | |
| {
 | |
|     iot_sg_ext_cus_msdu_send_data_t *msdu_send;
 | |
|     uint8_t *data_ptr = NULL;
 | |
|     iot_pkt_t *msdu_pkt = NULL;
 | |
|     uint8_t pro;
 | |
| 
 | |
|     if (p_pkt == NULL) {
 | |
|         return;
 | |
|     }
 | |
| 
 | |
|     (void)sub_mid;
 | |
|     msdu_send = (iot_sg_ext_cus_msdu_send_data_t *)iot_pkt_data(p_pkt);
 | |
|     if (msdu_send->pro > IOT_SG_EXT_CUS_PRO_MAX) {
 | |
|         pro = IOT_SG_EXT_CUS_PRO_MAX;
 | |
|     }
 | |
|     msdu_pkt = iot_plc_alloc_msdu(p_sg_glb->plc_app_h, msdu_send->msg_type,
 | |
|         0, msdu_send->dest_mac, p_sg_glb->plc_state.addr, pro, msdu_send->len,
 | |
|         msdu_send->retry_cnt);
 | |
|     IOT_ASSERT(msdu_pkt);
 | |
|     data_ptr = iot_pkt_block_ptr(msdu_pkt, IOT_PKT_BLOCK_TAIL);
 | |
|     os_mem_cpy(data_ptr, msdu_send->data, msdu_send->len);
 | |
|     iot_pkt_put(msdu_pkt, msdu_send->len);
 | |
| 
 | |
|     iot_plc_send_msdu(p_sg_glb->plc_app_h, msdu_pkt);
 | |
| 
 | |
|     iot_pkt_free(p_pkt);
 | |
|     return;
 | |
| }
 | |
| 
 | |
| /**
 | |
|  * @brief iot_sg_ext_command_func() - Post a command to sg-task.
 | |
|  * @param p_pkt: packet of command from cus-app.
 | |
|  * @param sub_mid: sub_mid of command, see enum _iot_sg_ext_command_id.
 | |
|  */
 | |
| static void iot_sg_ext_command_func(iot_pkt_t *p_pkt, uint8_t sub_mid)
 | |
| {
 | |
|     /* deliver the message to smart grid task for handling */
 | |
|     iot_task_msg_t *t_msg;
 | |
| 
 | |
|     if (p_pkt) {
 | |
|         t_msg = iot_task_alloc_msg(p_sg_glb->task_h);
 | |
|         if (t_msg) {
 | |
|             iot_sg_msg_t *msg = (iot_sg_msg_t *)t_msg;
 | |
|             msg->data = p_pkt;
 | |
|             msg->data2 = sub_mid;
 | |
|             t_msg->type = IOT_SG_MSG_TYPE_COMMAND;
 | |
|             t_msg->id = IOT_SG_MSG_ID_COMMAND_CUS;
 | |
|             iot_task_queue_msg(p_sg_glb->task_h, t_msg, IOT_SG_MSG_QUEUE_LP);
 | |
|         } else {
 | |
|             IOT_ASSERT(0);
 | |
|             iot_pkt_free(p_pkt);
 | |
|         }
 | |
|     }
 | |
| 
 | |
|     return;
 | |
| }
 | |
| 
 | |
| /**
 | |
|  * @brief iot_sg_ext_uart_data_func() - Post a data packet to sg-task.
 | |
|  * @param p_pkt: packet of data from cus-app.
 | |
|  * @param arg: arg from cus-app message.
 | |
|  */
 | |
| static void iot_sg_ext_uart_data_func(iot_pkt_t *p_pkt, uint8_t arg)
 | |
| {
 | |
|     /* deliver the message to smart grid task for handling */
 | |
|     iot_task_msg_t *t_msg;
 | |
| 
 | |
|     if (p_pkt) {
 | |
|         t_msg = iot_task_alloc_msg(p_sg_glb->task_h);
 | |
|         if (t_msg) {
 | |
|             iot_sg_msg_t *msg = (iot_sg_msg_t *)t_msg;
 | |
|             msg->data = p_pkt;
 | |
|             msg->data2 = arg;
 | |
| #if PLC_SUPPORT_CCO_ROLE
 | |
|             msg->data3 = iot_plc_get_ntb(p_sg_glb->plc_app_h);
 | |
| #endif
 | |
|             t_msg->type = IOT_SG_MSG_TYPE_UART;
 | |
|             t_msg->id = IOT_SG_MSG_ID_UART_DATA;
 | |
|             iot_task_queue_msg(p_sg_glb->task_h, t_msg, IOT_SG_MSG_QUEUE_LP);
 | |
|         } else {
 | |
|             IOT_ASSERT(0);
 | |
|             iot_pkt_free(p_pkt);
 | |
|         }
 | |
|     }
 | |
| 
 | |
|     return;
 | |
| }
 | |
| 
 | |
| /**
 | |
|  * @brief iot_sg_ext_receive_from_cusapp() - Receive uart data from IPC
 | |
|  *   (cusapp received from uart and posted to sgapp) and forward it to sgapp.
 | |
|  * @param p_param: param from IPC module.
 | |
|  * @param p_addr: address of sender from IPC module.
 | |
|  * @param p_pkt: packet from cus-app.
 | |
|  */
 | |
| static void iot_sg_ext_receive_from_cusapp(void *p_param, iot_ipc_addr_t *p_addr,
 | |
|     iot_pkt_t *p_pkt)
 | |
| {
 | |
|     iot_sg_ext_header_t *p_header;
 | |
| 
 | |
|     if (NULL == p_pkt) {
 | |
|         return;
 | |
|     }
 | |
| 
 | |
|     (void)p_param;
 | |
|     (void)p_addr;
 | |
| 
 | |
|     p_header = (iot_sg_ext_header_t *)iot_pkt_data(p_pkt);
 | |
| 
 | |
|     (void)iot_pkt_pull(p_pkt, sizeof(*p_header));
 | |
| 
 | |
|     switch(p_header->mid)
 | |
|     {
 | |
|         case IOT_SG_EXT_MID_UART_DATA_FROM_CUS:
 | |
|         {
 | |
|             iot_sg_ext_uart_data_func(p_pkt, p_header->arg);
 | |
|             break;
 | |
|         }
 | |
|         case IOT_SG_EXT_MID_COMMAND:
 | |
|         {
 | |
|             iot_sg_ext_command_func(p_pkt, p_header->arg);
 | |
|             break;
 | |
|         }
 | |
|         case IOT_SG_EXT_MID_TRANS_MSDU_FROM_CUS:
 | |
|         {
 | |
|             iot_sg_ext_msdu_func(p_pkt, p_header->arg);
 | |
|             break;
 | |
|         }
 | |
|         case IOT_SG_EXT_MID_MSDU_SEND:
 | |
|         {
 | |
|             iot_sg_ext_msdu_send_func(p_pkt, p_header->arg);
 | |
|             break;
 | |
|         }
 | |
|         default:
 | |
|         {
 | |
|             iot_pkt_free(p_pkt);
 | |
|         }
 | |
|     }
 | |
| 
 | |
|     return;
 | |
| }
 | |
| 
 | |
| void iot_sg_ext_ipc_send(iot_pkt_t *p_pkt)
 | |
| {
 | |
|     iot_ipc_send(iot_sg_ext_ipc_h, (iot_ipc_addr_t *)&iot_sg_ext_cusapp_addr,
 | |
|         p_pkt);
 | |
|     return;
 | |
| }
 | |
| 
 | |
| uint32_t iot_sg_ext_cusuart_config(uint32_t brate, uint8_t parity, uint8_t data,
 | |
|     uint8_t stop, iot_frame_fmt *p_fmt, uint32_t thd_rx_tm, uint32_t thd_rx_ful)
 | |
| 
 | |
| {
 | |
|     iot_pkt_t *p_pkt;
 | |
|     iot_sg_ext_header_t *p_hdr;
 | |
|     iot_sg_ext_uart_t *p_uart;
 | |
| 
 | |
|     p_pkt = iot_pkt_alloc(sizeof(*p_hdr) + sizeof(*p_uart), IOT_SMART_GRID_MID);
 | |
| 
 | |
|     if (NULL == p_pkt) {
 | |
|         return ERR_FAIL;
 | |
|     }
 | |
| 
 | |
|     p_hdr = (iot_sg_ext_header_t *)iot_pkt_put(p_pkt,
 | |
|         sizeof(*p_hdr) + sizeof(*p_uart));
 | |
| 
 | |
|     p_hdr->mid = IOT_SG_EXT_MID_COMMAND;
 | |
|     p_hdr->arg = IOT_SG_EXT_SID_UART_CFG;
 | |
| 
 | |
|     p_uart = (iot_sg_ext_uart_t *)p_hdr->data;
 | |
| 
 | |
|     p_uart->baudrate = brate;
 | |
|     p_uart->parity = parity;
 | |
|     p_uart->data = data;
 | |
|     p_uart->stop = stop;
 | |
|     p_uart->thd_rx_tm = thd_rx_tm;
 | |
|     p_uart->thd_rx_ful = thd_rx_ful;
 | |
| 
 | |
|     if (NULL != p_fmt) {
 | |
|         p_uart->fmt_valid = 1;
 | |
|         os_mem_cpy(&p_uart->fmt, p_fmt, sizeof(*p_fmt));
 | |
|     } else {
 | |
|         p_uart->fmt_valid = 0;
 | |
|     }
 | |
| 
 | |
|     iot_sg_ext_ipc_send(p_pkt);
 | |
| 
 | |
|     return ERR_OK;
 | |
| }
 | |
| 
 | |
| uint32_t iot_sg_ext_send_to_cusapp(iot_pkt_t *p_pkt, uint8_t arg)
 | |
| {
 | |
|     iot_sg_ext_header_t *p_hdr;
 | |
|     iot_pkt_t *p_tgt_pkt;
 | |
|     uint32_t org_len;
 | |
| 
 | |
|     /* Check if there is reserved space for 'p_hdr' */
 | |
|     if (sizeof(*p_hdr) > iot_pkt_block_len(p_pkt, IOT_PKT_BLOCK_HEAD)) {
 | |
|         org_len = iot_pkt_data_len(p_pkt);
 | |
| 
 | |
|         p_tgt_pkt = iot_pkt_alloc(sizeof(*p_hdr) + org_len, IOT_SMART_GRID_MID);
 | |
| 
 | |
|         if (NULL == p_tgt_pkt) {
 | |
|             iot_pkt_free(p_pkt);
 | |
|             return ERR_FAIL;
 | |
|         }
 | |
| 
 | |
|         os_mem_cpy(iot_pkt_reserve(p_tgt_pkt, sizeof(*p_hdr)),
 | |
|             iot_pkt_data(p_pkt), org_len);
 | |
| 
 | |
|         (void)iot_pkt_put(p_tgt_pkt, org_len);
 | |
| 
 | |
|         iot_pkt_free(p_pkt);
 | |
|     } else {
 | |
|         p_tgt_pkt = p_pkt;
 | |
|     }
 | |
| 
 | |
|     p_hdr = (iot_sg_ext_header_t *)iot_pkt_push(p_tgt_pkt, sizeof(*p_hdr));
 | |
| 
 | |
|     /* arg will be ignored. */
 | |
|     p_hdr->mid = IOT_SG_EXT_MID_UART_DATA_FROM_SG;
 | |
|     p_hdr->arg = arg;
 | |
| 
 | |
|     iot_sg_ext_ipc_send(p_tgt_pkt);
 | |
| 
 | |
|     return ERR_OK;
 | |
| }
 | |
| 
 | |
| void iot_sg_ext_send_msdu_msg_to_cus(iot_pkt_t *pkt, iot_plc_msdu_recv_t *msdu)
 | |
| {
 | |
|     iot_sg_ext_header_t *p_hdr = NULL;
 | |
| 
 | |
|     iot_pkt_set_data(pkt, (uint8_t *)msdu);
 | |
|     p_hdr = (iot_sg_ext_header_t *)iot_pkt_push(pkt, sizeof(*p_hdr));
 | |
|     IOT_ASSERT(p_hdr);
 | |
|     p_hdr->mid = IOT_SG_EXT_MID_TRANS_MSDU_FROM_SG;
 | |
|     p_hdr->arg = IOT_SG_EXT_SID_DEFAULT;
 | |
|     iot_sg_ext_ipc_send(pkt);
 | |
| }
 | |
| 
 | |
| uint32_t iot_sg_ext_app_init(void)
 | |
| {
 | |
|     iot_ipc_client_t client;
 | |
| 
 | |
|     client.addr.f_id = IOT_IPC_FID_SG_EXT_SDK;
 | |
|     client.addr.c_id = IOT_IPC_CID_SG_EXT_SG_APP;
 | |
|     client.recv = iot_sg_ext_receive_from_cusapp;
 | |
|     client.param = NULL;
 | |
| 
 | |
|     iot_sg_ext_ipc_h = iot_ipc_register_client(&client);
 | |
| 
 | |
|     if (iot_sg_ext_ipc_h == NULL) {
 | |
|         return ERR_FAIL;
 | |
|     }
 | |
| 
 | |
|     return ERR_OK;
 | |
| }
 | |
| 
 | |
| #else /* IOT_SG_EXT_SDK_ENABLE */
 | |
| 
 | |
| void iot_sg_ext_send_msdu_msg_to_cus(iot_pkt_t *pkt, iot_plc_msdu_recv_t *msdu)
 | |
| {
 | |
|     (void)pkt;
 | |
|     (void)msdu;
 | |
|     IOT_ASSERT(0);
 | |
| }
 | |
| 
 | |
| #endif /* IOT_SG_EXT_SDK_ENABLE */
 |