156 lines
		
	
	
		
			4.7 KiB
		
	
	
	
		
			C
		
	
	
	
	
	
			
		
		
	
	
			156 lines
		
	
	
		
			4.7 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 "os_utils_api.h"
 | |
| 
 | |
| #include "iot_board.h"
 | |
| #include "iot_oem_api.h"
 | |
| #include "iot_io_api.h"
 | |
| 
 | |
| #include "iot_cli.h"
 | |
| #include "iot_cli_host_interface.h"
 | |
| #include "iot_cli_plc_module.h"
 | |
| #include "iot_cli_common.h"
 | |
| #include "iot_cli_plc_tx_rx.h"
 | |
| #include "iot_cli_hw_ver.h"
 | |
| 
 | |
| extern iot_plc_host_config_t *host_config;
 | |
| extern iot_cli_t cli;
 | |
| 
 | |
| timer_id_t hw_ver_query_timer;
 | |
| 
 | |
| /* report hw ver */
 | |
| static void cli_report_hw_ver(uint8_t cco, uint8_t *src_mac)
 | |
| {
 | |
|     (void)src_mac;
 | |
|     cli_hw_ver_report report;
 | |
|     report.hw_ver = iot_board_hw_version_hex();
 | |
|     report.user_type = iot_oem_get_user_type();
 | |
|     iot_mac_addr_cpy(report.dst, host_config->mac_addr);
 | |
| 
 | |
|     iot_printf("cli_report_hw_ver %d\n", cco);
 | |
| 
 | |
|     if (!cco) {
 | |
|         iot_cli_host_send_data_plc(IOT_PLC_MSG_TYPE_UNICAST,
 | |
|             CLI_MSGID_HW_VER_REPORT, host_config->cco_mac,
 | |
|             (uint8_t*)&report, sizeof(cli_hw_ver_report));
 | |
|     }
 | |
| #if PLC_SUPPORT_CCO_ROLE
 | |
|     else {
 | |
|         iot_cli_send_to_host(CLI_MSGID_HW_VER_REPORT,
 | |
|             (uint8_t *)&report, sizeof(report), src_mac);
 | |
|     }
 | |
| #endif
 | |
| }
 | |
| 
 | |
| #if PLC_SUPPORT_CCO_ROLE
 | |
| /* cli handle hw ver report */
 | |
| void cli_cco_forward_hw_ver_report(
 | |
|     uint8_t *buffer, uint32_t bufferlen, uint8_t *src_mac)
 | |
| {
 | |
|     (void)src_mac;
 | |
|     uint8_t *addr =
 | |
|         get_addr_from_mapping_table(host_config->cli_request_id);
 | |
|     iot_cli_send_to_host(CLI_MSGID_HW_VER_REPORT,
 | |
|         buffer, bufferlen, addr);
 | |
| }
 | |
| #endif
 | |
| 
 | |
| /* cli query hw ver handler */
 | |
| void cli_query_hw_ver(
 | |
|     uint8_t *buffer, uint32_t bufferlen, uint8_t *src_mac)
 | |
| {
 | |
|     (void)src_mac;
 | |
|     cli_hw_ver_query *query =
 | |
|         (cli_hw_ver_query *)buffer;
 | |
| 
 | |
|     if ((!query) || (bufferlen < sizeof(*query))) {
 | |
|         iot_printf("cli_hw_ver_query, invalid para\n");
 | |
|         return;
 | |
|     }
 | |
| 
 | |
|     iot_printf("cli_hw_ver_query enter\n");
 | |
| 
 | |
|     if (host_config->dev_role != IOT_PLC_DEV_ROLE_CCO) {
 | |
|         if (!os_is_timer_active(hw_ver_query_timer)) {
 | |
|             uint16_t time = 0;
 | |
|             if (query->report_window >
 | |
|                 CLI_HW_VER_QUERY_TIME_WINDOW) {
 | |
|                 query->report_window =
 | |
|                     CLI_HW_VER_QUERY_TIME_WINDOW;
 | |
|             }
 | |
|             time = (uint16_t)(CLI_HW_VER_QUERY_TIME_BASE +
 | |
|                 os_rand() % query->report_window);
 | |
|             iot_printf("cli_hw_ver_query, %d %d\n",
 | |
|                 query->report_window, time);
 | |
|             // sta start timer to report hw ver
 | |
|             os_start_timer(hw_ver_query_timer, time);
 | |
|         }
 | |
|     }
 | |
| #if PLC_SUPPORT_CCO_ROLE
 | |
|     else {
 | |
|         if (iot_mac_addr_cmp(query->dst, host_config->mac_addr)) {
 | |
|             cli_report_hw_ver(1, src_mac);
 | |
|         } else {
 | |
|             uint8_t send_type = IOT_PLC_MSG_TYPE_UNICAST;
 | |
| 
 | |
|             if (iot_mac_is_bcast(query->dst)) {
 | |
|                 send_type = IOT_PLC_MSG_TYPE_BCAST;
 | |
|                 // report cco hw ver
 | |
|                 cli_report_hw_ver(1, src_mac);
 | |
|             }
 | |
| 
 | |
|             // store requet id for vendor report
 | |
|             host_config->cli_request_id =
 | |
|                 add_addr_to_mapping_table(src_mac);
 | |
| 
 | |
|             iot_printf("cco forward set cmd stype %lu\n", send_type);
 | |
|             iot_cli_host_send_data_plc(
 | |
|                 send_type, CLI_MSGID_QUEYR_HW_VER,
 | |
|                 query->dst, buffer, bufferlen);
 | |
|         }
 | |
|     }
 | |
| #endif
 | |
| }
 | |
| 
 | |
| /* handle query timer msg */
 | |
| void iot_cli_query_hw_ver_handle_timer_msg()
 | |
| {
 | |
|     cli_report_hw_ver(0, NULL);
 | |
| }
 | |
| 
 | |
| /* timer for query hw ver */
 | |
| static void iot_cli_query_hw_ver_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_QUERY_HW_TIMER, NULL);
 | |
| 
 | |
|     if (msg) {
 | |
|         iot_task_queue_msg(cli.cli_task_h, msg, IOT_CLI_QUEUE_TIMER);
 | |
|     }
 | |
| }
 | |
| 
 | |
| /* init query hw ver */
 | |
| void iot_cli_query_hw_ver_init()
 | |
| {
 | |
|     if (host_config->dev_role != IOT_PLC_DEV_ROLE_CCO)
 | |
|     {
 | |
|         hw_ver_query_timer = os_create_timer(
 | |
|                 IOT_CLI_MID, 0, iot_cli_query_hw_ver_timer, NULL);
 | |
|     }
 | |
| } |