Files
kunlun/app/utils/iot_plc_sync_api.c
2024-09-28 14:24:04 +08:00

1329 lines
38 KiB
C
Executable File

/****************************************************************************
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_task_api.h"
#include "os_event_api.h"
#include "os_timer_api.h"
#include "os_utils_api.h"
#include "iot_module_api.h"
#include "os_lock_api.h"
#include "iot_plc_cco_api.h"
#include "iot_plc_api.h"
#include "iot_config_api.h"
#include "iot_errno_api.h"
#include "iot_io_api.h"
#include "iot_utils_api.h"
#include "iot_oem_api.h"
#include "iot_app_api.h"
#include "iot_plc_sync_api.h"
#include "iot_app_pib_api.h"
#include "iot_cli_sg_api.h"
typedef struct _plc_sync_api_obj
{
/* app handle */
iot_plc_app_h app_handle;
/* 信号量 */
os_sem_h sem;
/* 返回结构体的保留部分,按每个消息结构体去解析 */
uint8_t retval[IOT_SYNC_TASK_PARSER_STRUCT_LEN];
/* 同步请求的API命令记录, 用来处理plc lib返回的消息 */
uint8_t excute_cmd;
/** if lower layer is ready to serve app.
* if is_ready is 0, app shall not call api
* except iot_plc_query_dev_info.
*/
uint8_t is_ready;
/* 代理节点tei */
uint16_t proxy_tei;
/* module id */
module_id_t module_id;
/* recv cli message post to task */
POST_MSG_CB post_msg_cli_cb;
} plc_sync_api_obj;
static plc_sync_api_obj sync_obj;
static iot_cli_sg_interface_t cli_app_interface;
/**
* @function_name:iot_plc_sync_init_check
* @brief 初始化信号量
**/
static void iot_plc_sync_init_check(void)
{
if (sync_obj.app_handle == NULL) {
iot_cus_printf("[sync api]app_handle is NULL.\n");
IOT_ASSERT(0);
return;
}
if (sync_obj.sem != NULL) {
return;
}
sync_obj.sem = os_create_sem(sync_obj.module_id, 1, 0);
if (sync_obj.sem == NULL) {
iot_cus_printf("[sync api]create semaphore failed.\n");
IOT_ASSERT(0);
return;
}
iot_cus_printf("[sync api]create semaphore success.\n");
return;
}
/**
* @function_name:iot_plc_sync_cmd_start
* @brief 命令处理开始,记录执行命令.
**/
static void iot_plc_sync_cmd_start(uint8_t cmd)
{
iot_plc_sync_init_check();
iot_cus_printf("[sync api]%s cmd:%d\n", __FUNCTION__, cmd);
sync_obj.excute_cmd = cmd;
}
/**
* @function_name:iot_plc_sync_cmd_end
* @brief 等待命令处理回复,然后清除记录执行命令.
**/
static void iot_plc_sync_cmd_end(void)
{
/* 等待 信号量 */
os_pend_sem(sync_obj.sem, 0xFFFFFFFF);
sync_obj.excute_cmd = IOT_SYNC_API_CMD_IDLE;
}
void iot_plc_query_band(iot_plc_freq_band_info **band_info)
{
iot_plc_sync_cmd_start(IOT_SYNC_API_CMD_QUERY_BAND);
iot_plc_query_band_info(sync_obj.app_handle, sync_obj.excute_cmd);
iot_plc_sync_cmd_end();
*band_info = (iot_plc_freq_band_info*)sync_obj.retval;
return;
}
uint8_t iot_plc_set_band(uint8_t band)
{
iot_plc_sync_cmd_start(IOT_SYNC_API_CMD_SET_BAND);
iot_plc_set_freq_band(sync_obj.app_handle, sync_obj.excute_cmd, band);
iot_plc_sync_cmd_end();
return sync_obj.retval[0];
}
uint8_t iot_plc_set_tx_power(uint8_t power)
{
iot_plc_sync_cmd_start(IOT_SYNC_API_CMD_SET_TX_POWER);
iot_plc_set_tx_power_cap(sync_obj.app_handle, sync_obj.excute_cmd,
&power, NULL);
iot_plc_sync_cmd_end();
return sync_obj.retval[0];
}
uint8_t iot_plc_cco_set_nid(uint32_t new_nid)
{
iot_plc_sync_cmd_start(IOT_SYNC_API_CMD_CCO_SET_NID);
iot_plc_set_nid(sync_obj.app_handle, sync_obj.excute_cmd, new_nid);
iot_plc_sync_cmd_end();
return sync_obj.retval[0];
}
void iot_plc_cco_get_nid(iot_plc_nid_info **nid_info)
{
iot_plc_sync_cmd_start(IOT_SYNC_API_CMD_CCO_GET_NID);
iot_plc_query_nid(sync_obj.app_handle, sync_obj.excute_cmd);
iot_plc_sync_cmd_end();
*nid_info = (iot_plc_nid_info*)sync_obj.retval;
return;
}
void iot_plc_query_nbnet(iot_plc_nbnet_info **nbnet_info)
{
iot_plc_sync_cmd_start(IOT_SYNC_API_CMD_QUERY_NBNET);
iot_plc_query_nb_nw_info(sync_obj.app_handle, sync_obj.excute_cmd);
iot_plc_sync_cmd_end();
*nbnet_info = (iot_plc_nbnet_info*)sync_obj.retval;
return;
}
void iot_plc_cco_query_nw_topo(iot_plc_topo_info **topo_info,
uint16_t start, uint8_t count)
{
uint8_t max_cnt = (IOT_SYNC_TASK_PARSER_STRUCT_LEN -
sizeof(iot_plc_topo_info)) / sizeof(iot_plc_topo_node_info);
count = min(count, max_cnt);
iot_plc_sync_cmd_start(IOT_SYNC_API_CMD_CCO_QUERY_TOPO);
iot_plc_query_nw_topo(sync_obj.app_handle, sync_obj.excute_cmd,
IOT_PLC_CCO_TOPO_REQ_DATA_VER_V3,
IOT_PLC_QUERY_TOPO_START_AS_TEI,
start + 1, count);
iot_plc_sync_cmd_end();
*topo_info = (iot_plc_topo_info*)sync_obj.retval;
return;
}
void iot_plc_cco_query_node_info(iot_plc_topo_info **node_info,
uint8_t *sta_mac, uint8_t sta_cnt)
{
uint8_t max_cnt = (IOT_SYNC_TASK_PARSER_STRUCT_LEN -
sizeof(iot_plc_topo_info)) / sizeof(iot_plc_topo_node_info);
sta_cnt = min(sta_cnt, max_cnt);
iot_plc_sync_cmd_start(IOT_SYNC_API_CMD_GET_TOPO_NODE);
iot_plc_query_node_info(sync_obj.app_handle, sync_obj.excute_cmd,
IOT_PLC_CCO_TOPO_REQ_DATA_VER_V3, sta_mac, sta_cnt);
iot_plc_sync_cmd_end();
*node_info = (iot_plc_topo_info*)sync_obj.retval;
return;
}
uint8_t iot_plc_cco_query_node_level(uint8_t *sta_mac)
{
iot_plc_topo_info *node_info;
iot_plc_cco_query_node_info(&node_info, sta_mac, 1);
if(node_info->cur_cnt == 0) {
return 0;
} else {
return node_info->topo_info[0].level;
}
}
uint8_t iot_plc_cco_set_whitelist_func(uint8_t action,uint16_t count,
uint8_t *mac_addr_array)
{
iot_plc_sync_cmd_start(IOT_SYNC_API_CMD_CCO_SET_WL);
iot_plc_set_whitelist_ex(sync_obj.app_handle, sync_obj.excute_cmd,
action, count, mac_addr_array, 0);
iot_plc_sync_cmd_end();
switch (action) {
case IOT_PLC_WL_ENABLE:
iot_app_whitelist_pib_enable_set(sync_obj.app_handle, 1);
break;
case IOT_PLC_WL_DISABLE:
iot_app_whitelist_pib_enable_set(sync_obj.app_handle, 0);
break;
case IOT_PLC_WL_ADD:
iot_app_whitelist_pib_add(sync_obj.app_handle, count, mac_addr_array, 0);
break;
case IOT_PLC_WL_ADD_PA:
iot_app_whitelist_pib_add(sync_obj.app_handle, count, mac_addr_array, 1);
break;
case IOT_PLC_WL_ADD_PB:
iot_app_whitelist_pib_add(sync_obj.app_handle, count, mac_addr_array, 2);
break;
case IOT_PLC_WL_ADD_PC:
iot_app_whitelist_pib_add(sync_obj.app_handle, count, mac_addr_array, 3);
break;
case IOT_PLC_WL_DEL:
iot_app_whitelist_pib_rm(sync_obj.app_handle, count, mac_addr_array);
break;
case IOT_PLC_WL_DEL_ALL:
iot_app_whitelist_pib_rm_all(sync_obj.app_handle);
break;
default:
break;
}
return sync_obj.retval[0];
}
uint8_t iot_plc_cco_get_whiltlist_state_func(void)
{
uint8_t value;
if (iot_app_whitelist_pib_enable_qr(sync_obj.app_handle, &value) !=
ERR_OK) {
IOT_ASSERT(0);
}
return value;
}
uint8_t iot_plc_cco_get_whitelist_info_func(iot_plc_white_info **whitelist_info,
uint16_t start, uint8_t count)
{
uint8_t max_cnt = (IOT_SYNC_TASK_PARSER_STRUCT_LEN -
sizeof(iot_plc_white_info)) / IOT_MAC_ADDR_LEN;
count = min(count, max_cnt);
iot_plc_sync_cmd_start(IOT_SYNC_API_CMD_CCO_GET_WL);
iot_plc_query_whitelist(sync_obj.app_handle, sync_obj.excute_cmd,
start + 1, count);
iot_plc_sync_cmd_end();
*whitelist_info = (iot_plc_white_info*)sync_obj.retval;
return (*whitelist_info)->cur_cnt;
}
uint8_t iot_plc_cco_get_wl_info_ext_func(iot_plc_white_info_ext **whitelist_info,
uint16_t start, uint8_t count)
{
uint8_t max_cnt = (IOT_SYNC_TASK_PARSER_STRUCT_LEN -
sizeof(iot_plc_white_info_ext)) / IOT_MAC_ADDR_LEN;
count = min(count, max_cnt);
iot_plc_sync_cmd_start(IOT_SYNC_API_CMD_CCO_GET_WL_EXT);
iot_plc_query_wl_ext(sync_obj.app_handle, sync_obj.excute_cmd,
start + 1, count);
iot_plc_sync_cmd_end();
*whitelist_info = (iot_plc_white_info_ext*)sync_obj.retval;
return (*whitelist_info)->cur_cnt;
}
uint8_t iot_plc_sta_get_atten_by_tei_func(uint16_t tei)
{
iot_plc_sync_cmd_start(IOT_SYNC_API_CMD_STA_GET_ATTEN);
iot_plc_query_neighbor_dev(sync_obj.app_handle, sync_obj.excute_cmd,
tei, 1, IOT_PLC_QUERY_TOPO_START_AS_TEI);
iot_plc_sync_cmd_end();
return sync_obj.retval[0];
}
uint8_t iot_plc_sta_get_snr_by_tei_func(uint16_t tei)
{
iot_plc_sync_cmd_start(IOT_SYNC_API_CMD_STA_GET_SNR);
iot_plc_query_neighbor_dev(sync_obj.app_handle, sync_obj.excute_cmd,
tei, 1, IOT_PLC_QUERY_TOPO_START_AS_TEI);
iot_plc_sync_cmd_end();
return sync_obj.retval[0];
}
uint8_t iot_plc_sta_get_snr_by_addr_func(uint8_t *addr)
{
iot_plc_sync_cmd_start(IOT_SYNC_API_CMD_STA_GET_SNR);
iot_plc_query_node_info(sync_obj.app_handle, sync_obj.excute_cmd,
IOT_PLC_CCO_TOPO_REQ_DATA_VER_V3, addr, 1);
iot_plc_sync_cmd_end();
return sync_obj.retval[0];
}
uint8_t iot_plc_sta_get_tf_sr_by_tei_func(uint16_t tei)
{
iot_plc_sync_cmd_start(IOT_SYNC_API_CMD_STA_GET_TF_SR);
iot_plc_query_neighbor_dev(sync_obj.app_handle, sync_obj.excute_cmd,
tei, 1, IOT_PLC_QUERY_TOPO_START_AS_TEI);
iot_plc_sync_cmd_end();
return sync_obj.retval[0];
}
uint8_t iot_plc_sta_get_tf_sr_by_addr_func(uint8_t *addr)
{
iot_plc_sync_cmd_start(IOT_SYNC_API_CMD_STA_GET_TF_SR);
iot_plc_query_node_info(sync_obj.app_handle, sync_obj.excute_cmd,
IOT_PLC_CCO_TOPO_REQ_DATA_VER_V3, addr, 1);
iot_plc_sync_cmd_end();
return sync_obj.retval[0];
}
uint8_t iot_plc_sta_get_phase_by_tei_func(uint16_t tei)
{
iot_plc_sync_cmd_start(IOT_SYNC_API_CMD_GET_PHASE);
iot_plc_query_neighbor_dev(sync_obj.app_handle, sync_obj.excute_cmd,
tei, 1, IOT_PLC_QUERY_TOPO_START_AS_TEI);
iot_plc_sync_cmd_end();
return sync_obj.retval[0];
}
uint8_t iot_plc_sta_get_phase_by_addr_func(uint8_t *addr)
{
iot_plc_sync_cmd_start(IOT_SYNC_API_CMD_GET_PHASE);
iot_plc_query_node_info(sync_obj.app_handle, sync_obj.excute_cmd,
IOT_PLC_CCO_TOPO_REQ_DATA_VER_V3, addr, 1);
iot_plc_sync_cmd_end();
return sync_obj.retval[0];
}
uint8_t iot_plc_sta_get_cco_flag_func(void)
{
return sync_obj.is_ready;
}
uint16_t iot_plc_sta_get_proxy_tei_func(void)
{
return sync_obj.proxy_tei;
}
uint8_t iot_plc_sta_is_online(uint8_t* mac)
{
iot_plc_topo_info *node_info;
iot_plc_cco_query_node_info(&node_info, mac, 1);
if (node_info->cur_cnt == 0) {
return false;
} else {
return true;
}
}
/**
* @function_name:iot_plc_resp_band_info
* @brief 回复band信息.
* @param band_info: 返回band信息.
* @return : NULL
**/
static void iot_plc_resp_band_info(iot_plc_freq_band_info_query_rpt_t *band_info)
{
iot_plc_freq_band_info *resp_band_info = (iot_plc_freq_band_info*)sync_obj.retval;
resp_band_info->freq_band = band_info->freq_band;
resp_band_info->noise_floor = band_info->noise_floor;
resp_band_info->power_cap = band_info->power_cap;
}
/**
* @function_name:iot_plc_resp_nbnet_info
* @brief 回复网络邻居信息.
* @param topo_info: 返回网络邻居信息.
* @return : NULL
**/
static void iot_plc_resp_nbnet_info(iot_plc_nb_nw_rpt_t *nbnet_info)
{
uint8_t i;
iot_plc_nbnet_info *resp_nbnet_info = (iot_plc_nbnet_info*)sync_obj.retval;
resp_nbnet_info->count = nbnet_info->count;
for (i = 0; i < nbnet_info->count; i++) {
resp_nbnet_info->nbnet_info[i].nid = nbnet_info->nb_info[i].nid;
resp_nbnet_info->nbnet_info[i].snr[0] = nbnet_info->nb_info[i].snr[0];
resp_nbnet_info->nbnet_info[i].snr[1] = nbnet_info->nb_info[i].snr[1];
resp_nbnet_info->nbnet_info[i].snr[2] = nbnet_info->nb_info[i].snr[2];
iot_mac_addr_cpy(resp_nbnet_info->nbnet_info[i].cco_mac,
nbnet_info->nb_info[i].addr);
}
}
/**
* @function_name:iot_plc_resp_topo_info
* @brief 回复topo信息.
* @param topo_info: 返回topo信息.
* @return : NULL
**/
static void iot_plc_resp_topo_info(iot_plc_nw_topo_rpt_t *topo_info)
{
uint8_t i;
iot_plc_topo_info *resp_topo_info = (iot_plc_topo_info*)sync_obj.retval;
iot_plc_node_info_v3_t *v3_info = (iot_plc_node_info_v3_t*)topo_info->data;
resp_topo_info->total_cnt = topo_info->total_count;
resp_topo_info->cur_cnt = (uint8_t)topo_info->count;
if (topo_info->version == IOT_PLC_CCO_TOPO_REQ_DATA_VER_V3 &&
topo_info->count > 0) {
for (i = 0; i <topo_info->count; i++) {
iot_mac_addr_cpy(resp_topo_info->topo_info[i].mac, v3_info[i].addr);
resp_topo_info->topo_info[i].level = v3_info[i].level;
resp_topo_info->topo_info[i].role = v3_info[i].role;
resp_topo_info->topo_info[i].sta_tei = v3_info[i].sta_tei;
resp_topo_info->topo_info[i].proxy_tei = v3_info[i].proxy_tei;
resp_topo_info->topo_info[i].phy_phase1 = v3_info[i].phy_phase1;
resp_topo_info->topo_info[i].phy_phase2 = v3_info[i].phy_phase2;
resp_topo_info->topo_info[i].phy_phase3 = v3_info[i].phy_phase3;
}
}
}
/**
* @function_name:iot_plc_resp_node_info
* @brief 回复查询的节点信息.
* @param node_info: 返回节点信息.
* @return : NULL
**/
static void iot_plc_resp_node_info(iot_plc_node_info_rpt_t *node_info)
{
uint8_t i;
iot_plc_topo_info *resp_node_info = (iot_plc_topo_info*)sync_obj.retval;
iot_plc_node_info_v3_t *v3_info = (iot_plc_node_info_v3_t*)node_info->data;
resp_node_info->total_cnt = node_info->total_count;
resp_node_info->cur_cnt = (uint8_t)node_info->count;
if (node_info->version == IOT_PLC_CCO_TOPO_REQ_DATA_VER_V3 &&
node_info->count > 0) {
for (i = 0; i <node_info->count; i++) {
iot_mac_addr_cpy(resp_node_info->topo_info[i].mac, v3_info[i].addr);
resp_node_info->topo_info[i].level = v3_info[i].level;
resp_node_info->topo_info[i].role = v3_info[i].role;
resp_node_info->topo_info[i].sta_tei = v3_info[i].sta_tei;
resp_node_info->topo_info[i].proxy_tei = v3_info[i].proxy_tei;
resp_node_info->topo_info[i].phy_phase1 = v3_info[i].phy_phase1;
resp_node_info->topo_info[i].phy_phase2 = v3_info[i].phy_phase2;
resp_node_info->topo_info[i].phy_phase3 = v3_info[i].phy_phase3;
}
}
}
/**
* @function_name:iot_plc_resp_dev_snr_info
* @brief 回复snr信息.
* @param dev_info: 返回邻居节点信息.
* @return : NULL
**/
static void iot_plc_resp_dev_snr_info(iot_plc_neighbor_dev_rpt_t *dev_info)
{
iot_plc_neighbor_dev_info_t *node =
(iot_plc_neighbor_dev_info_t*)dev_info->node;
if (dev_info->cnt > 0) {
sync_obj.retval[0] = (uint8_t)node->snr;
} else {
sync_obj.retval[0] = 0;
}
}
/**
* @function_name:iot_plc_resp_dev_tf_sr_info
* @brief 回复通信成功率信息.
* @param dev_info: 返回邻居节点信息.
* @return : NULL
**/
static void iot_plc_resp_dev_tf_sr_info(iot_plc_neighbor_dev_rpt_t *dev_info)
{
iot_plc_neighbor_dev_info_t *node =
(iot_plc_neighbor_dev_info_t*)dev_info->node;
if (dev_info->cnt > 0) {
sync_obj.retval[0] = (uint8_t)(node->dl_tf_sr * node->ul_tf_sr / 100);
} else {
sync_obj.retval[0] = 0;
}
}
/**
* @function_name:iot_plc_resp_dev_atten_info
* @brief 回复信道衰减信息.
* @param dev_info: 返回邻居节点信息.
* @return : NULL
**/
static void iot_plc_resp_dev_atten_info(iot_plc_neighbor_dev_rpt_t *dev_info)
{
iot_plc_neighbor_dev_info_t *node =
(iot_plc_neighbor_dev_info_t*)dev_info->node;
if (dev_info->cnt > 0) {
sync_obj.retval[0] = (uint8_t)node->attenuation;
} else {
sync_obj.retval[0] = 0;
}
}
/**
* @function_name:iot_plc_resp_dev_phase_info
* @brief 回复相位信息.
* @param dev_info: 返回邻居节点信息.
* @return : NULL
**/
static void iot_plc_resp_dev_phase_info(iot_plc_neighbor_dev_rpt_t *dev_info)
{
iot_plc_neighbor_dev_info_t *node =
(iot_plc_neighbor_dev_info_t*)dev_info->node;
if (dev_info->cnt > 0) {
if (node->phase1) {
sync_obj.retval[0] = 1;
} else if (node->phase2) {
sync_obj.retval[0] = 2;
} else if (node->phase3) {
sync_obj.retval[0] = 3;
} else {
sync_obj.retval[0] = 0;
}
} else {
sync_obj.retval[0] = 0;
}
}
/**
* @function_name:handle_mac_msg_for_query_band
* @brief 查询载波频段回复消息处理.
* @param hdr: response message header.
* @return : true - command is executed, false - not executed
**/
static uint8_t handle_mac_msg_for_query_band(iot_plc_msg_header_t *hdr)
{
uint8_t executed = false;
iot_plc_freq_band_info_query_rpt_t *band_info = (iot_plc_freq_band_info_query_rpt_t*)(hdr+1);
if (hdr->msg_id == IOT_PLC_MSG_BAND_INFO_QUERY_RPT) {
iot_cus_printf("[sync api]L%d get band info id=%d/tx_power=%d\n",
__LINE__, band_info->freq_band, band_info->power_cap);
iot_plc_resp_band_info(band_info);
os_post_sem(sync_obj.sem);
executed = true;
}
return executed;
}
/**
* @function_name:handle_mac_msg_for_set_wl
* @brief 设置白名单回复消息处理.
* @param hdr: response message header.
* @return : true - command is executed, false - not executed
**/
static uint8_t handle_mac_msg_for_set_tx_power(iot_plc_msg_header_t *hdr)
{
uint8_t executed = false;
iot_plc_tx_power_cap_set_rpt_t *rpt = (iot_plc_tx_power_cap_set_rpt_t*)(hdr+1);
if (hdr->msg_id == IOT_PLC_MSG_TX_POWER_CAP_SET_RPT) {
sync_obj.retval[0] = rpt->result;
os_post_sem(sync_obj.sem);
executed = true;
}
return executed;
}
/**
* @function_name:handle_mac_msg_for_set_nid
* @brief 设置nid回复消息处理.
* @param hdr: response message header.
* @return : true - command is executed, false - not executed
**/
static uint8_t handle_mac_msg_for_set_nid(iot_plc_msg_header_t *hdr)
{
uint8_t executed = false;
iot_plc_nw_id_set_rpt_t *rpt = (iot_plc_nw_id_set_rpt_t*)(hdr+1);
if (hdr->msg_id == IOT_PLC_MSG_NW_ID_SET_RPT) {
sync_obj.retval[0] = rpt->result;
os_post_sem(sync_obj.sem);
executed = true;
}
return executed;
}
/**
* @function_name:handle_mac_msg_for_get_nid
* @brief 查询nid回复消息处理.
* @param hdr: response message header.
* @return : true - command is executed, false - not executed
**/
static uint8_t handle_mac_msg_for_get_nid(iot_plc_msg_header_t *hdr)
{
uint8_t executed = false;
iot_plc_nid_info *nid_info;
iot_plc_nid_rpt_t *rpt = (iot_plc_nid_rpt_t*)(hdr+1);
if (hdr->msg_id == IOT_PLC_MSG_NW_ID_RPT) {
nid_info = (iot_plc_nid_info*)sync_obj.retval;
nid_info->nid = rpt->nid;
os_post_sem(sync_obj.sem);
executed = true;
}
return executed;
}
/**
* @function_name:handle_mac_msg_for_query_top
* @brief 查询topo回复消息处理.
* @param hdr: response message header.
* @return : true - command is executed, false - not executed
**/
static uint8_t handle_mac_msg_for_query_top(iot_plc_msg_header_t *hdr)
{
uint8_t executed = false;
iot_plc_nw_topo_rpt_t *topo_info = (iot_plc_nw_topo_rpt_t*)(hdr+1);
if (hdr->msg_id == IOT_PLC_MSG_NW_TOPO_RPT) {
iot_cus_printf("[sync api]L%d get topo info cnt=%d/%d\n",
__LINE__, topo_info->count, topo_info->total_count);
iot_plc_resp_topo_info(topo_info);
os_post_sem(sync_obj.sem);
executed = true;
}
return executed;
}
/**
* @function_name:handle_mac_msg_for_query_nbnet_info
* @brief 查询邻居网络回复消息处理.
* @param hdr: response message header.
* @return : true - command is executed, false - not executed
**/
static uint8_t handle_mac_msg_for_query_nbnet_info(iot_plc_msg_header_t *hdr)
{
uint8_t executed = false;
iot_plc_nb_nw_rpt_t *nbnet_info = (iot_plc_nb_nw_rpt_t*)(hdr+1);
if (hdr->msg_id == IOT_PLC_MSG_NW_NEIGHBOR_RPT) {
iot_cus_printf("[sync api]L%d get nbnet info total cnt=%d\n",
__LINE__, nbnet_info->count);
iot_plc_resp_nbnet_info(nbnet_info);
os_post_sem(sync_obj.sem);
executed = true;
}
return executed;
}
/**
* @function_name:handle_mac_msg_for_query_node_info
* @brief 查询node info回复消息处理.
* @param hdr: response message header.
* @return : true - command is executed, false - not executed
**/
static uint8_t handle_mac_msg_for_query_node_info(iot_plc_msg_header_t *hdr)
{
uint8_t executed = false;
iot_plc_node_info_rpt_t *node_info = (iot_plc_node_info_rpt_t*)(hdr+1);
if (hdr->msg_id == IOT_PLC_MSG_NODE_INFO_QUERY_RPT) {
iot_cus_printf("[sync api]L%d get node info cnt=%d/%d\n",
__LINE__, node_info->count, node_info->total_count);
iot_plc_resp_node_info(node_info);
os_post_sem(sync_obj.sem);
executed = true;
}
return executed;
}
/**
* @function_name:handle_mac_msg_for_get_snr
* @brief 查询snr回复消息处理.
* @param hdr: response message header.
* @return : true - command is executed, false - not executed
**/
static uint8_t handle_mac_msg_for_get_snr(iot_plc_msg_header_t *hdr)
{
uint8_t executed = true;
if (hdr->msg_id == IOT_PLC_MSG_NEIGHBOR_DEV_RPT) {
iot_plc_neighbor_dev_rpt_t *dev_info =
(iot_plc_neighbor_dev_rpt_t*)(hdr+1);
iot_cus_printf("[sync api]L%d get dev info cnt=%d \n", __LINE__,
dev_info->cnt);
iot_plc_resp_dev_snr_info(dev_info);
os_post_sem(sync_obj.sem);
executed = true;
} else if (hdr->msg_id == IOT_PLC_MSG_NODE_INFO_QUERY_RPT) {
iot_plc_node_info_rpt_t *node_info = (iot_plc_node_info_rpt_t*)(hdr+1);
if (node_info->count > 0) {
iot_plc_node_info_v3_t *v3_info =
(iot_plc_node_info_v3_t *)node_info->data;
sync_obj.retval[0] = v3_info->dl_snr;
} else {
sync_obj.retval[0] = 0;
}
os_post_sem(sync_obj.sem);
executed = true;
}
return executed;
}
/**
* @function_name:handle_mac_msg_for_get_tf_sr
* @brief 查询通信成功率回复消息处理.
* @param hdr: response message header.
* @return : true - command is executed, false - not executed
**/
static uint8_t handle_mac_msg_for_get_tf_sr(iot_plc_msg_header_t *hdr)
{
uint8_t executed = true;
if (hdr->msg_id == IOT_PLC_MSG_NEIGHBOR_DEV_RPT) {
iot_plc_neighbor_dev_rpt_t *dev_info =
(iot_plc_neighbor_dev_rpt_t*)(hdr + 1);
iot_cus_printf("[sync api]L%d get dev info cnt=%d \n", __LINE__,
dev_info->cnt);
iot_plc_resp_dev_tf_sr_info(dev_info);
os_post_sem(sync_obj.sem);
executed = true;
} else if (hdr->msg_id == IOT_PLC_MSG_NODE_INFO_QUERY_RPT) {
iot_plc_node_info_rpt_t *node_info = (iot_plc_node_info_rpt_t*)(hdr+1);
if (node_info->count > 0) {
iot_plc_node_info_v3_t *v3_info =
(iot_plc_node_info_v3_t *)node_info->data;
sync_obj.retval[0] =
(uint8_t)(v3_info->dl_tf_sr * v3_info->ul_tf_sr / 100);
} else {
sync_obj.retval[0] = 0;
}
os_post_sem(sync_obj.sem);
executed = true;
}
return executed;
}
/**
* @function_name:handle_mac_msg_for_get_atten
* @brief 查询信道衰减回复消息处理.
* @param hdr: response message header.
* @return : true - command is executed, false - not executed
**/
static uint8_t handle_mac_msg_for_get_atten(iot_plc_msg_header_t *hdr)
{
uint8_t executed = true;
iot_plc_neighbor_dev_rpt_t *dev_info = (iot_plc_neighbor_dev_rpt_t*)(hdr+1);
if (hdr->msg_id == IOT_PLC_MSG_NEIGHBOR_DEV_RPT) {
iot_cus_printf("[sync api]L%d get dev info cnt=%d \n", __LINE__,
dev_info->cnt);
iot_plc_resp_dev_atten_info(dev_info);
os_post_sem(sync_obj.sem);
executed = true;
}
return executed;
}
/**
* @function_name:handle_mac_msg_for_get_phase
* @brief 查询相位回复消息处理.
* @param hdr: response message header.
* @return : true - command is executed, false - not executed
**/
static uint8_t handle_mac_msg_for_get_phase(iot_plc_msg_header_t *hdr)
{
uint8_t executed = true;
if (hdr->msg_id == IOT_PLC_MSG_NEIGHBOR_DEV_RPT) {
iot_plc_neighbor_dev_rpt_t *dev_info =
(iot_plc_neighbor_dev_rpt_t*)(hdr + 1);
iot_cus_printf("[sync api]L%d get dev info cnt=%d \n", __LINE__,
dev_info->cnt);
iot_plc_resp_dev_phase_info(dev_info);
os_post_sem(sync_obj.sem);
executed = true;
} else if (hdr->msg_id == IOT_PLC_MSG_NODE_INFO_QUERY_RPT) {
iot_plc_node_info_rpt_t *node_info = (iot_plc_node_info_rpt_t*)(hdr+1);
if (node_info->count > 0) {
iot_plc_node_info_v3_t *v3_info =
(iot_plc_node_info_v3_t *)node_info->data;
if (v3_info->phy_phase1) {
sync_obj.retval[0] = 1;
} else if (v3_info->phy_phase2) {
sync_obj.retval[0] = 2;
} else if (v3_info->phy_phase3) {
sync_obj.retval[0] = 3;
} else {
sync_obj.retval[0] = 0;
}
} else {
sync_obj.retval[0] = 0;
}
os_post_sem(sync_obj.sem);
executed = true;
}
return executed;
}
/**
* @function_name:handle_mac_msg_for_set_wl
* @brief 设置白名单回复消息处理.
* @param hdr: response message header.
* @return : true - command is executed, false - not executed
**/
static uint8_t handle_mac_msg_for_set_wl(iot_plc_msg_header_t *hdr)
{
uint8_t executed = false;
iot_plc_wl_set_rpt_t *rpt = (iot_plc_wl_set_rpt_t*)(hdr+1);
if (hdr->msg_id == IOT_PLC_MSG_NW_WL_SET_RPT) {
sync_obj.retval[0] = rpt->result;
os_post_sem(sync_obj.sem);
executed = true;
}
return executed;
}
/**
* @function_name:handle_mac_msg_for_get_wl
* @brief 查询白名单回复消息处理.
* @param hdr: response message header.
* @return : true - command is executed, false - not executed
**/
static uint8_t handle_mac_msg_for_get_wl(iot_plc_msg_header_t *hdr)
{
uint8_t executed = false;
iot_plc_white_info *wl_rpt;
iot_plc_wl_rpt_t *rpt = (iot_plc_wl_rpt_t*)(hdr+1);
if (hdr->msg_id == IOT_PLC_MSG_NW_WL_RPT) {
wl_rpt = (iot_plc_white_info*)sync_obj.retval;
wl_rpt->total = rpt->total_count;
wl_rpt->cur_cnt = (uint8_t)rpt->count;
os_mem_cpy(wl_rpt->mac_list, rpt->mac_addr,
wl_rpt->cur_cnt * IOT_MAC_ADDR_LEN);
os_post_sem(sync_obj.sem);
executed = true;
}
return executed;
}
/**
* @function_name:handle_mac_msg_for_query_ext_whitelist
* @brief 查询白名单扩展命令回复消息处理.
* @param hdr: response message header.
* @return : true - command is executed, false - not executed
**/
static uint8_t handle_mac_msg_for_query_ext_whitelist(iot_plc_msg_header_t *hdr)
{
uint8_t executed = false;
iot_plc_white_info_ext *wl_rpt;
iot_plc_wl_ext_rpt_t *rpt = (iot_plc_wl_ext_rpt_t*)(hdr+1);
if (hdr->msg_id == IOT_PLC_MSG_NW_WL_EXT_RPT) {
wl_rpt = (iot_plc_white_info_ext*)sync_obj.retval;
wl_rpt->total = rpt->total_count;
wl_rpt->cur_cnt = (uint8_t)rpt->count;
os_mem_cpy(wl_rpt->white_info, rpt->wl,
wl_rpt->cur_cnt * sizeof(white_info));
os_post_sem(sync_obj.sem);
executed = true;
}
return executed;
}
/**
* @function_name:handle_mac_msg_for_others
* @brief 处理非同步主动查询,设置的plc消息.
* @param hdr: response message header.
* @return NULL
**/
static void handle_mac_msg_for_others(iot_plc_msg_header_t *hdr)
{
switch (hdr->msg_id) {
case IOT_PLC_MSG_APP_REG_CONF:
{
iot_plc_app_reg_conf_t* rpt = (iot_plc_app_reg_conf_t*)(hdr + 1);
uint8_t mac[IOT_MAC_ADDR_LEN] = {0};
iot_plc_cfg_set_req_t cfg;
if ((IOT_PLC_SUCCESS == rpt->result) ||
(IOT_PLC_SUCCESS_MODIFIED == rpt->result)) {
if (ERR_OK == iot_app_get_mac_in_pib(mac) &&
iot_mac_addr_valid(mac) &&
!iot_mac_addr_cmp(rpt->dev_mac, mac)) {
os_mem_set(&cfg, 0, sizeof(cfg));
cfg.addr_valid = 1;
iot_mac_addr_cpy(cfg.addr, mac);
cfg.reset = 1;
iot_cus_printf("[sync]set mac %02x%02x%02x%02x%02x%02x\n",
mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
iot_plc_set_cfg(sync_obj.app_handle,
IOT_PLC_API_REQ_ID_DEFAULT, &cfg);
}
}
break;
}
case IOT_PLC_MSG_DEV_STATE_CHANGE_RPT:
{
iot_plc_dev_state_change_rpt_t* rpt =
(iot_plc_dev_state_change_rpt_t*)(hdr + 1);
sync_obj.is_ready = rpt->is_ready;
if (rpt->is_ready) {
sync_obj.proxy_tei = rpt->pco_tei;
} else {
sync_obj.proxy_tei = 0;
}
break;
}
default:
break;
}
}
static uint8_t handle_mac_msg_for_set_band(iot_plc_msg_header_t *hdr)
{
uint8_t executed = false;
iot_plc_freq_band_set_rpt_t *rpt = (iot_plc_freq_band_set_rpt_t*)(hdr+1);
if (hdr->msg_id == IOT_PLC_MSG_FREQ_BAND_SET_RPT) {
sync_obj.retval[0] = rpt->result;
os_post_sem(sync_obj.sem);
executed = true;
}
return executed;
}
uint8_t sync_api_msg_from_mac_handle(iot_pkt_t *pkt)
{
uint8_t executed = false;
iot_plc_msg_header_t *hdr = (iot_plc_msg_header_t*)iot_pkt_data(pkt);
switch (sync_obj.excute_cmd) {
case IOT_SYNC_API_CMD_CCO_SET_NID:
{
executed = handle_mac_msg_for_set_nid(hdr);
break;
}
case IOT_SYNC_API_CMD_CCO_GET_NID:
{
executed = handle_mac_msg_for_get_nid(hdr);
break;
}
case IOT_SYNC_API_CMD_CCO_QUERY_TOPO:
{
executed = handle_mac_msg_for_query_top(hdr);
break;
}
case IOT_SYNC_API_CMD_CCO_SET_WL:
{
executed = handle_mac_msg_for_set_wl(hdr);
break;
}
case IOT_SYNC_API_CMD_CCO_GET_WL:
{
executed = handle_mac_msg_for_get_wl(hdr);
break;
}
case IOT_SYNC_API_CMD_CCO_GET_WL_STATE:
{
// TODO:
break;
}
case IOT_SYNC_API_CMD_STA_GET_SNR:
{
executed = handle_mac_msg_for_get_snr(hdr);
break;
}
case IOT_SYNC_API_CMD_STA_GET_TF_SR:
{
executed = handle_mac_msg_for_get_tf_sr(hdr);
break;
}
case IOT_SYNC_API_CMD_STA_GET_ATTEN:
{
executed = handle_mac_msg_for_get_atten(hdr);
break;
}
case IOT_SYNC_API_CMD_GET_PHASE:
{
executed = handle_mac_msg_for_get_phase(hdr);
break;
}
case IOT_SYNC_API_CMD_QUERY_BAND:
{
executed = handle_mac_msg_for_query_band(hdr);
break;
}
case IOT_SYNC_API_CMD_SET_TX_POWER:
{
executed = handle_mac_msg_for_set_tx_power(hdr);
break;
}
case IOT_SYNC_API_CMD_GET_TOPO_NODE:
{
executed = handle_mac_msg_for_query_node_info(hdr);
break;
}
case IOT_SYNC_API_CMD_QUERY_NBNET:
{
executed = handle_mac_msg_for_query_nbnet_info(hdr);
break;
}
case IOT_SYNC_API_CMD_CCO_GET_WL_EXT:
{
executed = handle_mac_msg_for_query_ext_whitelist(hdr);
break;
}
case IOT_SYNC_API_CMD_SET_BAND:
{
executed = handle_mac_msg_for_set_band(hdr);
break;
}
default:
break;
}
handle_mac_msg_for_others(hdr);
if (executed) {
iot_pkt_free(pkt);
}
return executed;
}
static void iot_app_cli_qr_wl_state(uint8_t req_id)
{
iot_pkt_t *buf_pkt;
iot_cli_sg_rpt_wl_state_t *rsp;
uint8_t ret;
buf_pkt = iot_pkt_alloc(iot_cli_sg_get_headroom_req() + sizeof(*rsp),
sync_obj.module_id);
IOT_ASSERT(buf_pkt);
rsp = (iot_cli_sg_rpt_wl_state_t*)iot_pkt_reserve(buf_pkt,
iot_cli_sg_get_headroom_req());
ret = iot_app_whitelist_pib_enable_qr(sync_obj.app_handle, &rsp->state);
if (ret == ERR_OK) {
iot_cli_sg_send_data_to_cli_interface(buf_pkt,
IOT_CLI_SG_MSG_RPT_WL_STATE, req_id);
} else {
iot_pkt_free(buf_pkt);
}
}
static void iot_app_cli_rpt_wl(uint8_t req_id, uint16_t start_index,
uint16_t count)
{
iot_pkt_t *result_pkt;
BUILD_BUG_ON(sizeof(iot_cli_sg_rpt_wl_t) ==
sizeof(iot_app_wl_entry_info_transfer_t));
BUILD_BUG_ON(sizeof(iot_cli_sg_node_info_t) ==
sizeof(iot_app_wl_entry_info_t));
result_pkt = iot_app_get_wl_entry_info(start_index, count,
iot_cli_sg_get_headroom_req());
IOT_ASSERT(result_pkt);
iot_cli_sg_send_data_to_cli_interface(result_pkt, IOT_CLI_SG_MSG_RPT_WL,
req_id);
}
static void iot_app_cli_add_wl(uint8_t req_id, iot_cli_sg_add_wl_t *req)
{
uint16_t i;
uint8_t *tmp_ptr;
iot_pkt_t *buf_pkt, *tmp_pkt;
iot_cli_sg_result_t *rsp;
buf_pkt = iot_pkt_alloc(iot_cli_sg_get_headroom_req() + sizeof(*rsp),
sync_obj.module_id);
IOT_ASSERT(buf_pkt);
rsp = (iot_cli_sg_result_t*)iot_pkt_reserve(buf_pkt,
iot_cli_sg_get_headroom_req());
tmp_pkt = iot_pkt_alloc(IOT_MAC_ADDR_LEN * req->count, sync_obj.module_id);
IOT_ASSERT(tmp_pkt);
tmp_ptr = iot_pkt_data(tmp_pkt);
for (i = 0; i < req->count; i++) {
iot_mac_addr_cpy(tmp_ptr, req->node_info[i].mac);
tmp_ptr += IOT_MAC_ADDR_LEN;
}
iot_plc_cco_set_whitelist_func(IOT_PLC_WL_ADD, req->count,
iot_pkt_data(tmp_pkt));
iot_pkt_free(tmp_pkt);
rsp->result = ERR_OK;
iot_cli_sg_send_data_to_cli_interface(buf_pkt,
IOT_CLI_SG_MSG_RPT_ADD_WL, req_id);
}
static void iot_app_cli_rm_wl(uint8_t req_id, iot_cli_sg_rm_wl_t *req)
{
iot_pkt_t *buf_pkt;
iot_cli_sg_result_t *rsp;
buf_pkt = iot_pkt_alloc(iot_cli_sg_get_headroom_req() + sizeof(*rsp),
sync_obj.module_id);
IOT_ASSERT(buf_pkt);
rsp = (iot_cli_sg_result_t*)iot_pkt_reserve(buf_pkt,
iot_cli_sg_get_headroom_req());
iot_plc_cco_set_whitelist_func(IOT_PLC_WL_DEL, req->count,
req->mac_addr[0]);
rsp->result = ERR_OK;
iot_cli_sg_send_data_to_cli_interface(buf_pkt,
IOT_CLI_SG_MSG_RPT_RM_WL, req_id);
}
static void iot_app_cli_set_wl_state(uint8_t req_id,
iot_cli_sg_set_wl_state_t *req)
{
iot_pkt_t *buf_pkt;
iot_cli_sg_result_t *rsp;
buf_pkt = iot_pkt_alloc(iot_cli_sg_get_headroom_req() + sizeof(*rsp),
sync_obj.module_id);
IOT_ASSERT(buf_pkt);
rsp = (iot_cli_sg_result_t*)iot_pkt_reserve(buf_pkt,
iot_cli_sg_get_headroom_req());
switch (req->state) {
case IOT_CLI_SG_WL_STATE_ENABLE:
{
iot_plc_cco_set_whitelist_func(IOT_PLC_WL_ENABLE, 0, NULL);
break;
}
case IOT_CLI_SG_WL_STATE_DISABLE:
{
iot_plc_cco_set_whitelist_func(IOT_PLC_WL_DISABLE, 0, NULL);
break;
}
default:
break;
}
rsp->result = ERR_OK;
iot_cli_sg_send_data_to_cli_interface(buf_pkt,
IOT_CLI_SG_MSG_RPT_SET_WL_STATE, req_id);
}
void iot_app_handle_cli_msg(iot_pkt_t *pkt)
{
iot_cli_sg_msg_header_t *hdr;
if (pkt == NULL) {
return;
}
hdr = (iot_cli_sg_msg_header_t *)iot_pkt_block_ptr(pkt, IOT_PKT_BLOCK_HEAD);
switch (hdr->msg_id) {
case IOT_CLI_SG_MSG_QUERY_WL_STATE:
{
iot_app_cli_qr_wl_state(hdr->req_id);
break;
}
case IOT_CLI_SG_MSG_QUERY_WL:
{
iot_cli_sg_query_wl_t *req;
req = (iot_cli_sg_query_wl_t *)iot_pkt_block_ptr(pkt,
IOT_PKT_BLOCK_DATA);
iot_app_cli_rpt_wl(hdr->req_id, req->start_index, req->count);
break;
}
case IOT_CLI_SG_MSG_ADD_WL:
{
iot_cli_sg_add_wl_t *req;
req = (iot_cli_sg_add_wl_t *)iot_pkt_block_ptr(pkt, IOT_PKT_BLOCK_DATA);
iot_app_cli_add_wl(hdr->req_id, req);
break;
}
case IOT_CLI_SG_MSG_RM_WL:
{
iot_cli_sg_rm_wl_t *req;
req = (iot_cli_sg_rm_wl_t *)iot_pkt_block_ptr(pkt, IOT_PKT_BLOCK_DATA);
iot_app_cli_rm_wl(hdr->req_id, req);
break;
}
case IOT_CLI_SG_MSG_SET_WL_STATE:
{
iot_cli_sg_set_wl_state_t *req;
req = (iot_cli_sg_set_wl_state_t *)iot_pkt_block_ptr(pkt,
IOT_PKT_BLOCK_DATA);
iot_app_cli_set_wl_state(hdr->req_id, req);
break;
}
default:
{
break;
}
}
iot_pkt_free(pkt);
}
static void iot_app_cli_callback(void *param, iot_pkt_t *pkt)
{
(void)param;
if (pkt == NULL) {
return;
}
if (sync_obj.post_msg_cli_cb == NULL
|| ERR_OK != sync_obj.post_msg_cli_cb((void*)pkt)) {
iot_pkt_free(pkt);
}
}
uint8_t iot_sync_api_init_app_hdl(iot_plc_app_h handle, module_id_t module_id,
POST_MSG_CB clicb)
{
sync_obj.app_handle = handle;
if (sync_obj.app_handle == NULL) {
return false;
}
sync_obj.post_msg_cli_cb = clicb;
sync_obj.module_id = module_id;
iot_app_whitelist_pib_sync(sync_obj.app_handle);
/* register to cli module */
cli_app_interface.recv = iot_app_cli_callback;
cli_app_interface.param = &cli_app_interface;
if (iot_cli_sg_interface_register(&cli_app_interface)) {
return false;
}
return true;
}