280 lines
		
	
	
		
			8.5 KiB
		
	
	
	
		
			C
		
	
	
	
	
	
		
		
			
		
	
	
			280 lines
		
	
	
		
			8.5 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_io_api.h"
 | ||
|  | 
 | ||
|  | #include "iot_cli.h"
 | ||
|  | #include "iot_cli_common.h"
 | ||
|  | #include "iot_cli_plc_tx_rx.h"
 | ||
|  | #include "iot_cli_host_interface.h"
 | ||
|  | #include "iot_cli_plc_module.h"
 | ||
|  | #include "iot_cli_ada_dump.h"
 | ||
|  | 
 | ||
|  | #if (IOT_STA_CONTROL_MODE == IOT_STA_CONTROL_TYPE_STA)
 | ||
|  | 
 | ||
|  | #include "plc_chn_est.h"
 | ||
|  | 
 | ||
|  | extern iot_plc_host_config_t *host_config; | ||
|  | extern iot_cli_host_info_t *host_info; | ||
|  | 
 | ||
|  | iot_ipc_addr_t dump_mac_addr = { IOT_IPC_FID_ADA, IOT_IPC_CID_ADA_MAC }; | ||
|  | iot_cli_ada_dump_info dump_info; | ||
|  | 
 | ||
|  | static void cli_send_ada_dump_ack( | ||
|  |     uint32_t msgid, uint8_t errorCode, uint8_t *src_mac) | ||
|  | { | ||
|  |     cli_ada_dump_param_ack ack; | ||
|  |     ack.result = errorCode; | ||
|  | 
 | ||
|  |     iot_cli_send_to_host(msgid, (uint8_t *)&ack, sizeof(ack), src_mac); | ||
|  | } | ||
|  | 
 | ||
|  | void cli_set_ada_dump_param( | ||
|  |     uint8_t *buffer, uint32_t bufferlen, uint8_t *src_mac) | ||
|  | { | ||
|  |     iot_pkt_t *buf; | ||
|  |     iot_phy_chn_dump_rt_typ_t dumpt_type; | ||
|  |     uint32_t data_size = 0; | ||
|  | 
 | ||
|  |     if ((!buffer) || (bufferlen < sizeof(iot_phy_chn_dump_rt_typ_t))) { | ||
|  |         return; | ||
|  |     } | ||
|  | 
 | ||
|  |     // if there is one active dump, stop it
 | ||
|  |     if (os_is_timer_active(dump_info.dump_timer)) | ||
|  |     { | ||
|  |         os_stop_timer(dump_info.dump_timer); | ||
|  |     } | ||
|  | 
 | ||
|  |     dumpt_type = *((iot_phy_chn_dump_rt_typ_t *)buffer); | ||
|  | 
 | ||
|  |     iot_printf("%s %lu\n", __FUNCTION__, dumpt_type); | ||
|  | 
 | ||
|  |     if (dumpt_type == IOT_PHY_CHN_DUMP_NOISE) | ||
|  |     { | ||
|  |         data_size = sizeof(iot_phy_chn_dump_rt_typ_t) + | ||
|  |             sizeof(iot_phy_chn_dump_rt_param_t); | ||
|  |     } else if (dumpt_type == IOT_PHY_CHN_DUMP_TRIG_THD) { | ||
|  |         data_size = sizeof(iot_phy_chn_dump_rt_typ_t) + | ||
|  |             sizeof(iot_phy_chn_dump_trig_thd_param_t); | ||
|  |     } else if (dumpt_type == IOT_PHY_CHN_DUMP_TRIG_FC) { | ||
|  |         data_size = sizeof(iot_phy_chn_dump_rt_typ_t) + | ||
|  |             sizeof(iot_phy_chn_dump_trig_phy_param_t); | ||
|  |     } else if (dumpt_type == IOT_PHY_CHN_DUMP_RF) { | ||
|  |         data_size = sizeof(iot_phy_chn_dump_rt_typ_t) + | ||
|  |             sizeof(iot_phy_chn_dump_rf_param_t); | ||
|  |     } else { | ||
|  |         iot_printf("%s failed, type nosupp\n", __FUNCTION__); | ||
|  |         cli_send_ada_dump_ack(CLI_MSGID_SET_ADA_DUMP_PARAM_ACK, | ||
|  |             CLI_ADA_DUMP_PARAM_ACK_FAILED, src_mac); | ||
|  |         return; | ||
|  |     } | ||
|  | 
 | ||
|  |     if (bufferlen < data_size) { | ||
|  |         iot_printf("%s failed, param len\n", __FUNCTION__); | ||
|  |         cli_send_ada_dump_ack(CLI_MSGID_SET_ADA_DUMP_PARAM_ACK, | ||
|  |             CLI_ADA_DUMP_PARAM_ACK_FAILED, src_mac); | ||
|  |         return; | ||
|  |     } | ||
|  | 
 | ||
|  |     // store requet id for ada dump
 | ||
|  |     cli_send_ada_dump_ack(CLI_MSGID_SET_ADA_DUMP_PARAM_ACK, | ||
|  |         CLI_ADA_DUMP_PARAM_ACK_SUCCESS, src_mac); | ||
|  | 
 | ||
|  |     buf = iot_ipc_pkt_alloc(CLI_IPC_MSG_SHORT_BUF_SIZE, IOT_PLC_LIB_MID); | ||
|  |     if (buf) { | ||
|  |         uint8_t *data = | ||
|  |             iot_pkt_block_ptr(buf, IOT_PKT_BLOCK_DATA); | ||
|  |         iot_channel_test_msg *msg = (iot_channel_test_msg *)data; | ||
|  |         msg->msg_id = IOT_CHANNEL_DUMP_TEST_START; | ||
|  |         os_mem_cpy(msg->data, buffer, data_size); | ||
|  |         iot_pkt_put(buf, data_size + sizeof(msg->msg_id)); | ||
|  |         iot_ipc_send(host_config->ipc_h, &dump_mac_addr, buf); | ||
|  |     } | ||
|  | } | ||
|  | 
 | ||
|  | static void cli_ada_dump_report_result(uint16_t index) | ||
|  | { | ||
|  |     uint16_t data_size = CLI_ADA_DUMP_DATA_SIZE; | ||
|  |     iot_pkt_t *dump_pkt = NULL; | ||
|  |     cli_ada_dump_data *dump_data = NULL; | ||
|  | 
 | ||
|  |     iot_printf("cli_ada_dump_report_result idx %lu total_num %lu\n", | ||
|  |         index, dump_info.total_cnt); | ||
|  | 
 | ||
|  |     if (index <= (dump_info.total_cnt - 1)) { | ||
|  |         if (index == (dump_info.total_cnt - 1)) { | ||
|  |             data_size = (uint16_t)(dump_info.len - | ||
|  |                 index * CLI_ADA_DUMP_DATA_SIZE); | ||
|  |         } | ||
|  | 
 | ||
|  |         iot_printf("cli_ada_dump_report_result size %lu\n", data_size); | ||
|  | 
 | ||
|  |         if (!data_size) { | ||
|  |             iot_printf("dump report result, data_size 0\n"); | ||
|  |             return; | ||
|  |         } | ||
|  | 
 | ||
|  |         dump_pkt = iot_pkt_alloc(sizeof(cli_ada_dump_data), IOT_CLI_MID); | ||
|  |         if (!dump_pkt) { | ||
|  |             iot_printf("dump report result, dumpdata NULL\n"); | ||
|  |             return; | ||
|  |         } | ||
|  | 
 | ||
|  |         dump_data = (cli_ada_dump_data *)iot_pkt_data(dump_pkt); | ||
|  |         dump_data->cnt = dump_info.total_cnt; | ||
|  |         dump_data->idx = index; | ||
|  |         dump_data->len = data_size; | ||
|  |         dump_data->gain = dump_info.gain; | ||
|  |         os_mem_cpy(dump_data->data, | ||
|  |             dump_info.data + index * CLI_ADA_DUMP_DATA_SIZE, | ||
|  |             data_size); | ||
|  |         os_mem_cpy(dump_data->reserved, dump_info.reserved, | ||
|  |             CLI_ADA_DUMP_RESERVED_DATA_SIZE); | ||
|  | 
 | ||
|  |         iot_pkt_put(dump_pkt, sizeof(cli_ada_dump_data)); | ||
|  | 
 | ||
|  |         cli_ul_send_with_retry(CLI_MSGID_SET_ADA_DUMP_RESULT, | ||
|  |             (uint8_t*)dump_data, sizeof(cli_ada_dump_data), NULL); | ||
|  |         cli_ul_send_with_retry(CLI_MSGID_SET_ADA_DUMP_RESULT, | ||
|  |             (uint8_t*)dump_data, sizeof(cli_ada_dump_data), NULL); | ||
|  | 
 | ||
|  |         iot_pkt_free(dump_pkt); | ||
|  |         dump_info.idx++; | ||
|  |     } | ||
|  | 
 | ||
|  |     if (index >= (dump_info.total_cnt - 1) && | ||
|  |         os_is_timer_active(dump_info.dump_timer)) { | ||
|  |         os_stop_timer(dump_info.dump_timer); | ||
|  |     } | ||
|  | } | ||
|  | 
 | ||
|  | void cli_query_ada_dump_data( | ||
|  |     uint8_t *buffer, uint32_t bufferlen, uint8_t *src_mac) | ||
|  | { | ||
|  |     (void)src_mac; | ||
|  |     cli_ada_dump_query_result *query = | ||
|  |         (cli_ada_dump_query_result *)buffer; | ||
|  | 
 | ||
|  |     if ((!query) || (bufferlen < sizeof(cli_ada_dump_query_result))) { | ||
|  |         return; | ||
|  |     } | ||
|  | 
 | ||
|  |     cli_ada_dump_report_result(query->idx); | ||
|  | } | ||
|  | 
 | ||
|  | void iot_cli_ada_dump_handle_timer_msg() | ||
|  | { | ||
|  |     cli_ada_dump_report_result(dump_info.idx); | ||
|  | } | ||
|  | 
 | ||
|  | static void iot_cli_report_dump_msg( | ||
|  |     iot_phy_chn_dump_result *dump_result) | ||
|  | { | ||
|  |     if (dump_result && dump_result->len) { | ||
|  |         dump_info.data = dump_result->start_ptr; | ||
|  |         dump_info.len = dump_result->len; | ||
|  | 
 | ||
|  |         iot_printf("iot_cli_report_dump_msg len %lu\n", dump_info.len); | ||
|  | 
 | ||
|  |         dump_info.total_cnt = (uint16_t) | ||
|  |             ((dump_result->len + CLI_ADA_DUMP_DATA_SIZE - 1) / | ||
|  |             CLI_ADA_DUMP_DATA_SIZE); | ||
|  |         dump_info.gain = dump_result->gain; | ||
|  |         os_mem_cpy(dump_info.reserved, (uint8_t*)(&(dump_result->report)), | ||
|  |             CLI_ADA_DUMP_RESERVED_DATA_SIZE); | ||
|  |         dump_info.idx = 0; | ||
|  | 
 | ||
|  |         cli_ada_dump_report_result(dump_info.idx); | ||
|  | 
 | ||
|  |         if (os_is_timer_active(dump_info.dump_timer)) | ||
|  |         { | ||
|  |             os_stop_timer(dump_info.dump_timer); | ||
|  |         } | ||
|  |         os_start_timer(dump_info.dump_timer, CLI_ADA_DUMP_TIMER); | ||
|  |     } | ||
|  | } | ||
|  | 
 | ||
|  | void iot_cli_handle_ada_msg(iot_pkt_t *pkt) | ||
|  | { | ||
|  |     iot_printf("iot_cli_handle_ada_msg\n"); | ||
|  |     if (pkt) { | ||
|  |         iot_channel_test_msg *msg = | ||
|  |             (iot_channel_test_msg*)iot_pkt_data(pkt); | ||
|  |         switch (msg->msg_id) { | ||
|  |         case IOT_CHANNEL_DUMP_TEST_DONE: | ||
|  |             iot_cli_report_dump_msg((iot_phy_chn_dump_result *)msg->data); | ||
|  |             break; | ||
|  |         default: | ||
|  |             break; | ||
|  |         } | ||
|  |         iot_pkt_free(pkt); | ||
|  |     } else { | ||
|  |         iot_printf("iot_cli_handle_ada_msg invalid para\n"); | ||
|  |     } | ||
|  | } | ||
|  | 
 | ||
|  | void iot_cli_ipc_recv(void *param, iot_ipc_addr_t *addr, | ||
|  |     iot_pkt_t *pkt) | ||
|  | { | ||
|  |     (void)addr; | ||
|  |     (void)param; | ||
|  |     iot_task_msg_t *ipc_msg = | ||
|  |         iot_cli_create_cli_msg(IOT_CLI_ADA_MSG, pkt); | ||
|  | 
 | ||
|  |     if (ipc_msg) { | ||
|  |         iot_task_queue_msg( | ||
|  |             host_info->host_task_h, ipc_msg, IOT_CLI_QUEUE_HOST); | ||
|  |     } else if (pkt) { | ||
|  |         iot_pkt_free(pkt); | ||
|  |     } | ||
|  | } | ||
|  | 
 | ||
|  | /* timer for set info */ | ||
|  | static void iot_cli_dump_timer(timer_id_t timer_id, void *arg) | ||
|  | { | ||
|  |     (void)timer_id; | ||
|  |     (void)arg; | ||
|  | 
 | ||
|  |     iot_task_msg_t *msg; | ||
|  |     msg = iot_cli_create_cli_msg(IOT_CLI_DUMP_TIMER, NULL); | ||
|  | 
 | ||
|  |     if (msg) { | ||
|  |         iot_task_queue_msg( | ||
|  |             host_info->host_task_h, msg, IOT_CLI_QUEUE_TIMER); | ||
|  |     } | ||
|  | } | ||
|  | 
 | ||
|  | static void iot_cli_ipc_init() | ||
|  | { | ||
|  |     iot_ipc_client_t client; | ||
|  |     /* register ipc to communicate with  */ | ||
|  |     client.addr.f_id = IOT_IPC_FID_ADA; | ||
|  |     client.addr.c_id = IOT_IPC_CID_ADA_CLI; | ||
|  |     client.recv = iot_cli_ipc_recv; | ||
|  |     client.param = NULL; | ||
|  |     host_config->ipc_h = iot_ipc_register_client(&client); | ||
|  | } | ||
|  | 
 | ||
|  | void iot_cli_ada_dump_init() | ||
|  | { | ||
|  |     iot_cli_ipc_init(); | ||
|  |     os_mem_set((uint8_t *)(&dump_info), 0, sizeof(dump_info)); | ||
|  |     dump_info.dump_timer = os_create_timer(IOT_CLI_MID, 1, | ||
|  |         iot_cli_dump_timer, NULL); | ||
|  | } | ||
|  | 
 | ||
|  | #endif
 |