/**************************************************************************** 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.h" #include "iot_cli_msg.h" #include "iot_cli_host_interface.h" #include "iot_cli_plc_module.h" #include "iot_cli_ul_buf.h" #include "iot_cli_plc_tx_rx.h" extern iot_plc_host_config_t *host_config; extern iot_cli_host_info_t *host_info; extern iot_cli_t cli; static inline uint8_t cli_module_send_type_convert_helper(uint8_t type) { if (type == CLI_UNICAST_SEND_TYPE) { return IOT_PLC_MSG_TYPE_UNICAST; } else if (type == CLI_BCAST_SEND_TYPE) { return IOT_PLC_MSG_TYPE_BCAST; } return IOT_PLC_MSG_TYPE_BCAST; } void iot_cli_module_send_to_host_with_retry(uint32_t msgid, uint8_t *buffer, uint32_t length, uint16_t sn, uint8_t *dst_mac, uint8_t *src_mac) { (void) dst_mac; if (cli.enable_commu) { if (host_info->pfunc_send_ul_package) { host_info->pfunc_send_ul_package( MODULEID_HOSTINTERFACE, msgid, buffer, length, sn, src_mac); } } else { #if IOT_CLI_CKB_ADDR_MAPPING // if ckb exist uint8_t *addr = get_first_ckb_addr_from_mapping_table(); if (addr) { iot_cli_module_send_data_with_retry(IOT_PLC_MSG_TYPE_UNICAST, 0, MODULEID_HOSTINTERFACE, msgid, NULL, addr, buffer, length); } #else iot_printf("%s ckb module error !\n", __FUNCTION__); #endif } } void cli_ul_send_with_retry(uint32_t msgid, uint8_t *buffer, uint32_t length, uint8_t *dst_mac) { uint16_t sn = 0; #if PLC_SUPPORT_CCO_ROLE sn = cli_ul_buf_add_entry(msgid, buffer, length); #endif iot_cli_module_send_to_host_with_retry(msgid, buffer, length, sn, dst_mac, NULL); } iot_pkt_t * iot_cli_create_pkt_plc(uint8_t send_type, uint8_t retry_cnt, uint16_t moduleid, uint32_t msgid, uint8_t *src_mac, uint8_t *cli_dest_mac, uint8_t *plc_dest_mac, uint32_t data_len) { iot_pkt_t *msdu = NULL; uint32_t pkt_data_len = sizeof(cli_msg_hdr_t) + data_len; if (iot_cli_host_is_ctrl_connected()) { msdu = iot_plc_alloc_ctrl_proto_msdu(host_config->app_handle, plc_dest_mac, host_config->mac_addr, host_info->link_id, (uint16_t)pkt_data_len, retry_cnt); } else { msdu = iot_plc_alloc_msdu(host_config->app_handle, send_type, IOT_PLC_ACK_TYPE_NONE, plc_dest_mac, host_config->mac_addr, host_info->link_id, (uint16_t)pkt_data_len, retry_cnt); } IOT_ASSERT(msdu); if (msdu) { cli_msg_hdr_t* hdr; hdr = (cli_msg_hdr_t*)iot_pkt_block_ptr(msdu, IOT_PKT_BLOCK_TAIL); hdr->module_id = moduleid; hdr->msg_id = (uint16_t)msgid; hdr->msg_len = (uint16_t)(data_len + sizeof(cli_msg_hdr_t)); hdr->sn = CLI_UL_BUF_IVALID_SN; if (src_mac == NULL || MAC_IS_EMPTY(src_mac)) { os_mem_cpy(hdr->src_mac, host_config->mac_addr, IOT_MAC_ADDR_LEN); } else { os_mem_cpy(hdr->src_mac, src_mac, IOT_MAC_ADDR_LEN); } if (IOT_PLC_MSG_TYPE_UNICAST == send_type) { os_mem_cpy(hdr->target_mac, cli_dest_mac, IOT_MAC_ADDR_LEN); } else { os_mem_set(hdr->target_mac, 0, CLI_MAC_ADDR_LEN); } iot_pkt_put(msdu, sizeof(cli_msg_hdr_t)); } return msdu; } void iot_cli_module_send_data(uint8_t send_type, uint8_t retry_cnt, uint16_t moduleid, uint32_t msgid, uint8_t *src_mac, uint8_t *dest_mac, uint8_t *buf, uint32_t len) { iot_cli_module_send_data_with_retry( cli_module_send_type_convert_helper(send_type), retry_cnt, moduleid, msgid, src_mac, dest_mac, buf, len); } void iot_cli_module_send_data_with_retry(uint8_t send_type, uint8_t retry_cnt, uint16_t moduleid, uint32_t msgid, uint8_t *src_mac, uint8_t *dest_mac, uint8_t *buf, uint32_t len) { iot_pkt_t *msdu = NULL; uint8_t* data_ptr = NULL; uint8_t *plc_dest_mac = dest_mac; //STA could not send msg to a STA dircelty, it has to send the msg to cco. //cco will transfer the msg if ((!iot_cli_host_is_ctrl_connected()) && iot_plc_is_client_mode()) { if (dest_mac && (os_mem_cmp(dest_mac, host_config->cco_mac, IOT_MAC_ADDR_LEN) != 0) && (IOT_PLC_MSG_TYPE_UNICAST == send_type)) { plc_dest_mac = host_config->cco_mac; } } msdu = iot_cli_create_pkt_plc(send_type, retry_cnt, moduleid, msgid, src_mac, dest_mac, plc_dest_mac, len); if (msdu) { data_ptr = iot_pkt_block_ptr(msdu, IOT_PKT_BLOCK_TAIL); os_mem_cpy(data_ptr, buf, len); iot_pkt_put(msdu, len); iot_plc_send_msdu(host_config->app_handle, msdu); } } void iot_cli_host_send_data_plc(uint8_t send_type, uint32_t msgid, uint8_t *dest_mac, uint8_t *buf, uint32_t len) { iot_cli_module_send_data_with_retry(send_type, IOT_PLC_LOCAL_RETRY_CNT, CLI_MODULEID_HOSTINTERFACE, msgid, NULL, dest_mac, buf, len); } void iot_cli_plc_recv(void *param, iot_pkt_t *pkt) { (void)param; iot_task_msg_t *t_msg; if ((host_config == NULL) || (host_info->host_task_h == NULL) || (pkt == NULL)) { iot_printf("Invalid param (%s)\n", __FUNCTION__); return; } t_msg = iot_cli_create_cli_msg(IOT_CLI_HOST_MSG, pkt); if (t_msg) { iot_task_queue_msg( host_info->host_task_h, t_msg, IOT_CLI_QUEUE_HOST); } else if (pkt) { iot_pkt_free(pkt); } }