198 lines
		
	
	
		
			5.1 KiB
		
	
	
	
		
			C
		
	
	
	
	
	
		
		
			
		
	
	
			198 lines
		
	
	
		
			5.1 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. | ||
|  | 
 | ||
|  | ****************************************************************************/ | ||
|  | 
 | ||
|  | #include "iot_errno.h"
 | ||
|  | #include "iot_io.h"
 | ||
|  | #include "iot_app.h"
 | ||
|  | #include "iot_config.h"
 | ||
|  | #include "iot_system.h"
 | ||
|  | 
 | ||
|  | #include "iot_cli_tx_rx.h"
 | ||
|  | #include "iot_cli_host_interface.h"
 | ||
|  | #include "iot_cli_dbg_log.h"
 | ||
|  | #include "iot_cli_ul_buf.h"
 | ||
|  | #include "iot_cli_plc_mgr_alive.h"
 | ||
|  | 
 | ||
|  | extern CLI_MSG_ENTRY | ||
|  | cli_common_msg_entry[]; | ||
|  | extern iot_cli_t cli; | ||
|  | 
 | ||
|  | /* ctrl connect flag */ | ||
|  | #define CLI_CTRL_PROTO_CONNECT_FLAG                 (0x80)
 | ||
|  | /* join connect flag */ | ||
|  | #define CLI_PLC_PROTO_JOIN_FLAG                     (0x01)
 | ||
|  | 
 | ||
|  | iot_cli_host_info_t *host_info = NULL; | ||
|  | 
 | ||
|  | uint8_t cli_execute_msg_entry( | ||
|  |     cli_moduleid_t moduleid, uint32_t msgid, | ||
|  |     CLI_MSG_ENTRY *msg_entry, iot_cli_msg_t *msg) | ||
|  | { | ||
|  |     uint8_t executed = 0; | ||
|  |     if (msg_entry) { | ||
|  |         for (int i = 0; | ||
|  |             msg_entry[i].module_id != CLI_MODULEID_MAX_NUM; i++) { | ||
|  |             if ((moduleid == msg_entry[i].module_id) && | ||
|  |                 (msgid == msg_entry[i].msg_id) && | ||
|  |                 msg_entry[i].pFun) { | ||
|  |                 msg_entry[i].pFun(iot_pkt_data(msg->data), | ||
|  |                     iot_pkt_data_len(msg->data), &(msg->src_mac[0])); | ||
|  |                 executed = 1; | ||
|  |                 break; | ||
|  |             } | ||
|  |         } | ||
|  |     } | ||
|  | 
 | ||
|  |     return executed; | ||
|  | } | ||
|  | 
 | ||
|  | uint8_t iot_cli_host_interface_handle_msg( | ||
|  |     cli_moduleid_t moduleid, uint32_t msgid, iot_cli_msg_t *msg) | ||
|  | { | ||
|  |     if (cli_execute_msg_entry( | ||
|  |         moduleid, msgid, cli_common_msg_entry, msg)) { | ||
|  |         return 1; | ||
|  |     } else if(iot_cli_module_handle_msg( | ||
|  |         moduleid, msgid, msg)) { | ||
|  |         return 1; | ||
|  |     } | ||
|  | 
 | ||
|  |     return 0; | ||
|  | } | ||
|  | 
 | ||
|  | uint8_t iot_cli_host_interface_handle_task_msg( | ||
|  |     iot_cli_msg_t *cli_msg, uint16_t id) | ||
|  | { | ||
|  |     uint8_t handled = 1; | ||
|  | 
 | ||
|  |     switch (id) { | ||
|  |     case IOT_CLI_LOG_TIMER: | ||
|  |         iot_cli_handle_timer_log(); | ||
|  |         break; | ||
|  | #ifdef HOST_APP_FEATURE_ENABLE
 | ||
|  |     case IOT_CLI_UL_BUF_TIMER: | ||
|  |         iot_cli_handle_ul_buf_timer_msg(); | ||
|  |         break; | ||
|  |     case IOT_CLI_ALIVE_TIMER: | ||
|  |         iot_cli_plc_mgr_alive_handle_timer_msg(); | ||
|  |         break; | ||
|  | #endif
 | ||
|  |     default: | ||
|  |         handled = 0; | ||
|  |     } | ||
|  | 
 | ||
|  |     if (!handled) { | ||
|  |         iot_cli_module_handle_task_msg(cli_msg, id); | ||
|  |     } | ||
|  | 
 | ||
|  |     return handled; | ||
|  | } | ||
|  | 
 | ||
|  | uint8_t iot_cli_host_interface_cancel_task_msg( | ||
|  |     iot_cli_msg_t *cli_msg, uint16_t id) | ||
|  | { | ||
|  |     switch (id) { | ||
|  |     case IOT_CLI_LOG_TIMER: | ||
|  | #ifdef HOST_APP_FEATURE_ENABLE
 | ||
|  |     case IOT_CLI_UL_BUF_TIMER: | ||
|  |     case IOT_CLI_ALIVE_TIMER: | ||
|  | #endif
 | ||
|  |         if (cli_msg->data) { | ||
|  |             iot_pkt_free(cli_msg->data); | ||
|  |         } | ||
|  |         break; | ||
|  |     default: | ||
|  |         iot_cli_module_cancel_task_msg(cli_msg, id); | ||
|  |         break; | ||
|  |     } | ||
|  | 
 | ||
|  |     return 1; | ||
|  | } | ||
|  | 
 | ||
|  | void iot_cli_host_interface_init_uart( | ||
|  |     commnucator_t * com, uint8_t interface_type) | ||
|  | { | ||
|  |     iot_cli_module_init_uart(com, interface_type); | ||
|  | } | ||
|  | 
 | ||
|  | uint8_t iot_cli_host_interface_init(HOST_SEND_PACKAGE_EVENT_CB cb, | ||
|  |     iot_task_h task_h) | ||
|  | { | ||
|  |     uint8_t ret = ERR_OK; | ||
|  |     host_info = (iot_cli_host_info_t *) | ||
|  |         os_mem_malloc(IOT_CLI_MID, sizeof(iot_cli_host_info_t)); | ||
|  |     if (host_info == NULL) { | ||
|  |         ret = ERR_NOMEM; | ||
|  |         goto label_failed1; | ||
|  |     } | ||
|  |     os_mem_set(host_info, 0, sizeof(host_info)); | ||
|  | 
 | ||
|  |     // set cli link_id to 2
 | ||
|  |     host_info->link_id = 2; | ||
|  |     host_info->host_task_h = task_h; | ||
|  |     host_info->pfunc_send_ul_package = cb; | ||
|  |     host_info->link_load = 0; | ||
|  | 
 | ||
|  |     ret = iot_cli_proto_module_init(); | ||
|  |     if (ret != ERR_OK) { | ||
|  |         goto label_failed1; | ||
|  |     } | ||
|  | 
 | ||
|  |     return ERR_OK;   // initialized successfully
 | ||
|  | 
 | ||
|  | label_failed1: | ||
|  |     iot_cli_host_interface_deinit(); | ||
|  |     iot_cli_proto_module_deinit(); | ||
|  |     return ERR_NOMEM; | ||
|  | } | ||
|  | 
 | ||
|  | /* iot_plc_host_deinit() - deinit host application */ | ||
|  | void iot_cli_host_interface_deinit() | ||
|  | { | ||
|  |     if (host_info) { | ||
|  |         os_mem_free(host_info); | ||
|  |         host_info = NULL; | ||
|  |     } | ||
|  | } | ||
|  | 
 | ||
|  | uint8_t iot_cli_host_is_ctrl_connected() | ||
|  | { | ||
|  |     return (host_info->is_ready & CLI_CTRL_PROTO_CONNECT_FLAG) ? 1 : 0; | ||
|  | } | ||
|  | 
 | ||
|  | void iot_cli_host_set_ctrl_connected(uint8_t setting) | ||
|  | { | ||
|  |     if (!setting) { | ||
|  |         host_info->is_ready &= (~CLI_CTRL_PROTO_CONNECT_FLAG); | ||
|  |     } else { | ||
|  |         host_info->is_ready |= CLI_CTRL_PROTO_CONNECT_FLAG; | ||
|  |     } | ||
|  | } | ||
|  | 
 | ||
|  | uint8_t iot_cli_host_is_join_connected() | ||
|  | { | ||
|  |     return (host_info->is_ready & CLI_PLC_PROTO_JOIN_FLAG) ? 1 : 0; | ||
|  | } | ||
|  | 
 | ||
|  | void iot_cli_host_set_join_connected(uint8_t setting) | ||
|  | { | ||
|  |     if (!setting) { | ||
|  |         host_info->is_ready &= (~CLI_PLC_PROTO_JOIN_FLAG); | ||
|  |     } else { | ||
|  |         host_info->is_ready |= CLI_PLC_PROTO_JOIN_FLAG; | ||
|  |     } | ||
|  | } | ||
|  | 
 |