Files
kunlun/cli/host_interface/plc/iot_cli_discovery.c

281 lines
8.1 KiB
C
Raw Normal View History

2024-09-28 14:24:04 +08:00
/****************************************************************************
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_version.h"
#include "iot_board_api.h"
#include "iot_cli_host_interface.h"
#include "iot_cli_plc_module.h"
#include "iot_cli_discovery.h"
#include "iot_cli_plc_tx_rx.h"
#include "iot_cli_sg_ctrl_api.h"
#include "iot_cli_sg.h"
#if (IOT_STA_CONTROL_MODE == IOT_STA_CONTROL_TYPE_STA)
extern iot_cli_host_info_t *host_info;
extern iot_plc_host_config_t *host_config;
cli_dis_node_info dis_nod_info;
static uint8_t band_id = CLI_JOIN_NODE_DEFAULT_BAND;
static uint16_t query_times = 0;
static void cli_scan_band_bitmap_init()
{
uint8_t fb_bitmap[IOT_PLC_BAND_BITMAP_SIZE] = { 0 };
/* default bitmap 0~3
* assign to mac band in customer range:
* PLC_LIB_FREQ_BAND_0 = 0, //2M to 12M
* PLC_LIB_FREQ_BAND_1 = 1, //2.4M to 5.6M
* PLC_LIB_FREQ_BAND_2 = 2, //700K to 3M
* PLC_LIB_FREQ_BAND_3 = 3, //1.7M to 3M
* PLC_LIB_FREQ_BAND_4 = 4, //5.6M to 12M
* PLC_LIB_FREQ_BAND_8 = 8, //5.8M to 9M
* PLC_LIB_FREQ_BAND_9 = 9, //4.9M to 24.4M
* PLC_LIB_FREQ_BAND_10 = 10,//28.3M to 34.5M
* PLC_LIB_FREQ_BAND_11 = 11,//4.9M to 12.2M
*/
fb_bitmap[0] |= 1 << PLC_LIB_FREQ_BAND_0;
fb_bitmap[0] |= 1 << PLC_LIB_FREQ_BAND_1;
fb_bitmap[0] |= 1 << PLC_LIB_FREQ_BAND_2;
fb_bitmap[0] |= 1 << PLC_LIB_FREQ_BAND_3;
#if (IOT_SMART_CONFIG)
fb_bitmap[PLC_LIB_FREQ_BAND_9 / 8] |= 1 << (PLC_LIB_FREQ_BAND_9 % 8);
#endif
iot_plc_set_scan_band_bitmap(host_config->app_handle,
CLI_HOST_USE_API_BY_PLCM, fb_bitmap, IOT_PLC_BAND_BITMAP_SIZE);
}
void cli_enable_discovery_mode(
uint8_t *buffer, uint32_t bufferlen, uint8_t *src_mac)
{
(void)src_mac;
cli_enable_discovery_mode_dl *enable =
(cli_enable_discovery_mode_dl *)buffer;
if ((!enable) || (bufferlen < sizeof(*enable))) {
return;
}
if (!enable->enabled) {
goto out;
}
// set scan band
cli_scan_band_bitmap_init();
iot_plc_set_whitelist(
host_config->app_handle,
CLI_HOST_USE_API_BY_PLCM,
IOT_PLC_WL_DEL, 1,
host_config->cco_mac);
// enable whitelist
iot_plc_set_whitelist(
host_config->app_handle,
CLI_HOST_USE_API_BY_PLCM,
IOT_PLC_WL_ENABLE, 0, NULL);
// start nw fmt
iot_plc_start_nw_fmt(
host_config->app_handle, 0);
out:
// set discovery mode
iot_plc_set_discovery_mode(
host_config->app_handle,
CLI_HOST_USE_API_BY_PLCM,
enable->enabled);
}
void cli_disable_discovery_mode()
{
iot_plc_set_discovery_mode(
host_config->app_handle,
CLI_HOST_USE_API_BY_PLCM, 0);
}
void cli_join_node(
uint8_t *buffer, uint32_t bufferlen, uint8_t *src_mac)
{
iot_cli_sg_ctrl_join_node_dl *join =
(iot_cli_sg_ctrl_join_node_dl *)buffer;
iot_cli_sg_ctrl_join_node_ack ack;
if ((!join) || (bufferlen < sizeof(*join))) {
return;
}
if (join->ckq) {
iot_sg_cli_send_cmd(0, IOT_CLI_SG_CTRL_MSG_REQ_JOIN_NODE, buffer,
bufferlen);
goto out;
}
if (join->band_id == CLI_JOIN_STA) {
query_times = 0;
band_id = join->band_id;
iot_plc_ctrl_proto_connect(
host_config->app_handle,
CLI_HOST_USE_API_BY_PLCM,
join->node
);
} else {
iot_plc_set_whitelist(
host_config->app_handle,
CLI_HOST_USE_API_BY_PLCM,
IOT_PLC_WL_DEL, 1,
host_config->cco_mac);
iot_plc_set_whitelist(
host_config->app_handle,
CLI_HOST_USE_API_BY_PLCM,
IOT_PLC_WL_ADD, 1, join->node);
iot_mac_addr_cpy(
host_config->cco_mac, join->node);
query_times = 0;
band_id = join->band_id;
iot_printf("cli_join_node set band %d\n", band_id);
iot_plc_start_nw_fmt(
host_config->app_handle, 1);
iot_plc_set_freq_band(
host_config->app_handle,
CLI_HOST_USE_API_BY_PLCM,
band_id);
}
out:
ack.result = 0;
iot_cli_send_to_host(
CLI_MSGID_JOIN_NODE_ACK,
(uint8_t *)&ack, sizeof(ack), src_mac);
}
void cli_node_is_ready(
uint8_t *buffer, uint32_t bufferlen, uint8_t *src_mac)
{
(void)buffer;
(void)bufferlen;
uint8_t ready = (!!host_info->is_ready);
iot_printf("cli_node_is_ready %lu\n", ready);
query_times++;
if ((!ready) && (band_id != CLI_JOIN_STA) &&
(0 == (query_times % CLI_JOIN_NODE_SET_BAND_INYERVAL)))
{
iot_printf("ckb join, start nw fmt and set band %d\n", band_id);
iot_plc_start_nw_fmt(
host_config->app_handle, 1);
iot_plc_set_freq_band(
host_config->app_handle,
CLI_HOST_USE_API_BY_PLCM,
band_id);
}
iot_cli_send_to_host(
CLI_MSGID_NODE_IS_READY_RESPONSE,
(uint8_t *)&ready, sizeof(uint8_t), src_mac);
}
static void cli_discovery_node_notify_timeout(timer_id_t timer_id, void *arg)
{
(void)timer_id;
(void)arg;
upload_dis_nod_info_list();
}
void cli_discovery_node_notify(
iot_plc_discovery_node_rpt_t *info)
{
if (!info) {
iot_printf("discovery node notify, invalid data\n");
return;
}
uint32_t num = dis_nod_info.dis_node_num %
CLI_DISCOVERY_MAX_DIS_NODE;
dis_nod_info.nodelist[num].tei = (uint16_t)info->tei;
dis_nod_info.nodelist[num].nid = info->nid;
dis_nod_info.nodelist[num].phase = info->phase;
dis_nod_info.nodelist[num].role = (uint8_t)info->role;
dis_nod_info.nodelist[num].rx_snr = info->rx_snr;
dis_nod_info.nodelist[num].band_id = info->band_id;
iot_mac_addr_cpy(dis_nod_info.nodelist[num].addr,
info->addr);
iot_mac_addr_cpy(dis_nod_info.nodelist[num].cco_addr,
info->cco_addr);
dis_nod_info.dis_node_num++;
if (dis_nod_info.dis_node_num >= CLI_DISCOVERY_MAX_DIS_NODE) {
os_stop_timer(dis_nod_info.rpt_timer);
upload_dis_nod_info_list();
} else if (!os_is_timer_active(dis_nod_info.rpt_timer)) {
os_start_timer(dis_nod_info.rpt_timer, CLI_DISCOVERY_MAX_INTERVAL_TM);
}
}
void cli_local_device_info(uint8_t *buffer, uint32_t bufferlen,
uint8_t *src_mac)
{
(void)buffer;
(void)bufferlen;
cli_sg_device_info_ul_t device_info = { 0 };
iot_build_info_t build_info;
iot_version_get_user_build_info(&build_info);
device_info.sw_ver = build_info.sw_ver;
device_info.vendor_id = iot_board_load_user_vendor_id();
iot_cli_send_to_host(CLI_MSGID_GET_DEVICE_INFO_RESP,
(uint8_t *)&device_info, sizeof(device_info), src_mac);
}
void upload_dis_nod_info_list()
{
if (!dis_nod_info.dis_node_num) {
return;
}
iot_cli_send_to_host(CLI_MSGID_DISCOVERY_NODE_RESP,
(uint8_t*)&dis_nod_info, sizeof(cli_discovery_node_rpt) *
dis_nod_info.dis_node_num + sizeof(dis_nod_info.dis_node_num),
NULL);
dis_nod_info.dis_node_num = 0;
}
void cli_discovery_init()
{
os_mem_set(&dis_nod_info, 0, sizeof(dis_nod_info));
dis_nod_info.rpt_timer = os_create_timer(IOT_CLI_MID, false,
cli_discovery_node_notify_timeout, NULL);
if (dis_nod_info.rpt_timer == 0) {
IOT_ASSERT(0);
}
}
void cli_discovery_deinit()
{
if (dis_nod_info.rpt_timer) {
os_delete_timer(dis_nod_info.rpt_timer);
dis_nod_info.rpt_timer = 0;
}
}
#endif