/**************************************************************************** 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_cli_ping.h" #include "iot_cli_host_interface.h" #include "iot_cli_plc_module.h" #include "iot_io_api.h" #include "iot_cli_plc_tx_rx.h" extern iot_plc_host_config_t *host_config; extern iot_cli_host_info_t *host_info; #if PLC_SUPPORT_CCO_ROLE cli_ping_info ping_info = {0}; static uint32_t cli_ping_calculate_duration( uint32_t start_time, uint32_t ack_time) { if (ack_time >= start_time) { return ack_time - start_time; } else { return (uint32_t)0xFFFFFFFF - start_time + ack_time; } } #endif void cli_ping_handler(uint8_t *buffer, uint32_t bufferlen, uint8_t *src_mac) { (void)src_mac; cli_host_ping_param *param = NULL; iot_pkt_t *data_pkt = NULL; uint32_t data_len = 0; uint32_t msgid = CLI_MSGID_PING_ACK; param = (cli_host_ping_param *)buffer; if ((!param) || (bufferlen < sizeof(*param))) { return; } if (CLI_PING_MAX_SIZE < param->data_size) { param->data_size = CLI_PING_MAX_SIZE; } data_len = sizeof(*param) + param->data_size; #if PLC_SUPPORT_CCO_ROLE if (IOT_PLC_DEV_ROLE_CCO == host_config->dev_role) { msgid = CLI_MSGID_PING; ping_info.start_time = os_boot_time32(); iot_mac_addr_cpy(ping_info.dst, param->dst); } #endif data_pkt = iot_cli_create_pkt_plc(IOT_PLC_MSG_TYPE_UNICAST, IOT_PLC_LOCAL_RETRY_CNT, CLI_MODULEID_HOSTINTERFACE, msgid, src_mac, param->dst, param->dst, data_len); if (data_pkt != NULL) { cli_host_ping_param *param_for_dst = (cli_host_ping_param *) iot_pkt_block_ptr(data_pkt, IOT_PKT_BLOCK_TAIL); param_for_dst->data_size = param->data_size; iot_mac_addr_cpy(param_for_dst->dst, host_config->mac_addr); iot_pkt_put(data_pkt, data_len); iot_plc_send_msdu(host_config->app_handle, data_pkt); } } #if PLC_SUPPORT_CCO_ROLE void cli_ping_ack_handler( uint8_t *buffer, uint32_t bufferlen, uint8_t *src_mac) { cli_host_ping_param *param = (cli_host_ping_param *)buffer; if ((!param) || (bufferlen < sizeof(*param))) { return; } if (iot_mac_addr_cmp(ping_info.dst, param->dst)) { cli_host_ping_ack ping_ack; iot_mac_addr_cpy(ping_ack.dst, param->dst); ping_ack.ack_duration = cli_ping_calculate_duration(ping_info.start_time, os_boot_time32()); iot_cli_send_to_host( CLI_MSGID_PING_ACK, (uint8_t *)&ping_ack, sizeof(ping_ack), src_mac); } } #endif