Files
kunlun/common/plc_lib/src/iot_plc_api_cco.c
2024-09-28 14:24:04 +08:00

643 lines
17 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.
****************************************************************************/
/* os shim includes */
#include "os_types.h"
/* common includes */
#include "iot_errno.h"
#include "iot_module.h"
#include "iot_utils.h"
#include "iot_ipc.h"
#include "iot_pkt_api.h"
#include "iot_plc_cco_api.h"
#include "iot_config.h"
#include "iot_plc_lib.h"
/* plc lib internal includes */
#include "plc_lib_internal.h"
#if PLC_SUPPORT_CCO_ROLE
/* max queried node count within long buffer size */
#define IOT_PLC_NODE_QUERY_MAX_CNT ((PLC_LIB_MSG_LONG_BUF_SIZE - \
sizeof(iot_plc_node_info_query_t) - \
sizeof(iot_plc_msg_header_t)) / IOT_MAC_ADDR_LEN)
void iot_plc_lid_alloc_req(iot_plc_app_h handle, uint8_t lid)
{
iot_pkt_t *buf;
iot_plc_lid_release_req_t *q;
iot_plc_app_t* slot = NULL;
iot_plc_app_t* app = (iot_plc_app_t*)handle;
os_acquire_mutex(p_plc_lib->lock);
/* check if app id is already registered */
slot = plc_lib_find_app(app->app_id);
if (slot == NULL || slot != app) {
/* application not registered */
IOT_ASSERT(0);
}
buf = iot_ipc_pkt_alloc(PLC_LIB_MSG_SHORT_BUF_SIZE, IOT_PLC_LIB_MID);
IOT_ASSERT(buf);
q = (iot_plc_lid_release_req_t*)plc_lib_prep_msg(buf, app->app_id,
IOT_PLC_MSG_LID_REQ, PLC_LIB_REQ_ID_DEFAULT);
q->lid = lid;
iot_pkt_put(buf, sizeof(*q));
os_release_mutex(p_plc_lib->lock);
iot_ipc_send(p_plc_lib->ipc_h, &plc_stack_addr, buf);
return;
}
void iot_plc_lid_rel_req(iot_plc_app_h handle, uint8_t lid)
{
iot_pkt_t *buf;
iot_plc_lid_release_req_t *q;
iot_plc_app_t* slot = NULL;
iot_plc_app_t* app = (iot_plc_app_t*)handle;
os_acquire_mutex(p_plc_lib->lock);
/* check if app id is already registered */
slot = plc_lib_find_app(app->app_id);
if (slot == NULL || slot != app) {
/* application not registered */
IOT_ASSERT(0);
}
buf = iot_ipc_pkt_alloc(PLC_LIB_MSG_SHORT_BUF_SIZE, IOT_PLC_LIB_MID);
IOT_ASSERT(buf);
q = (iot_plc_lid_release_req_t*)plc_lib_prep_msg(buf, app->app_id,
IOT_PLC_MSG_LID_REL, PLC_LIB_REQ_ID_DEFAULT);
q->lid = lid;
iot_pkt_put(buf, sizeof(*q));
os_release_mutex(p_plc_lib->lock);
iot_ipc_send(p_plc_lib->ipc_h, &plc_stack_addr, buf);
return;
}
void iot_plc_query_nw_topo(iot_plc_app_h handle, uint8_t req_id,
uint8_t req_data_ver, uint8_t start_type, uint16_t start, uint16_t cnt)
{
iot_pkt_t *buf;
iot_plc_nw_topo_query_t *q;
iot_plc_app_t *slot;
iot_plc_app_t* app = (iot_plc_app_t*)handle;
os_acquire_mutex(p_plc_lib->lock);
/* check if app id is already registered */
slot = plc_lib_find_app(app->app_id);
if (slot == NULL || slot != app) {
/* application not registered */
IOT_ASSERT(0);
}
buf = iot_ipc_pkt_alloc(PLC_LIB_MSG_SHORT_BUF_SIZE, IOT_PLC_LIB_MID);
IOT_ASSERT(buf);
q = (iot_plc_nw_topo_query_t *)plc_lib_prep_msg(buf, app->app_id,
IOT_PLC_MSG_NW_TOPO_QUERY, req_id);
q->start = start;
q->count = cnt;
q->version = req_data_ver;
q->start_type = start_type;
iot_pkt_put(buf, sizeof(*q));
os_release_mutex(p_plc_lib->lock);
iot_ipc_send(p_plc_lib->ipc_h, &plc_stack_addr, buf);
return;
}
void iot_plc_set_nid(iot_plc_app_h handle, uint8_t req_id, uint32_t new_nid)
{
iot_pkt_t *buf;
iot_plc_app_t *slot, *app;
iot_plc_set_nid_req_t *q = NULL;
app = handle;
os_acquire_mutex(p_plc_lib->lock);
/* check if app id is already registered */
slot = plc_lib_find_app(app->app_id);
if (slot == NULL || slot != app) {
/* application not registered */
IOT_ASSERT(0);
}
buf = iot_ipc_pkt_alloc(PLC_LIB_MSG_SHORT_BUF_SIZE, IOT_PLC_LIB_MID);
IOT_ASSERT(buf);
// prepare a IOT_PLC_MSG_NW_ID_SET message
q = (iot_plc_set_nid_req_t*)plc_lib_prep_msg(buf, app->app_id,
IOT_PLC_MSG_NW_ID_SET, req_id);
q->nid = new_nid;
iot_pkt_put(buf, sizeof(*q));
os_release_mutex(p_plc_lib->lock);
iot_ipc_send(p_plc_lib->ipc_h, &plc_stack_addr, buf);
}
uint32_t iot_plc_set_beacon_data(iot_plc_app_h handle, uint8_t *data,
uint8_t len)
{
iot_pkt_t *buf;
iot_plc_app_t *slot, *app;
iot_plc_beacon_data_set_t *q = NULL;
app = handle;
if (len > IOT_PLC_BEACON_DATA_MAX) {
return ERR_INVAL;
}
os_acquire_mutex(p_plc_lib->lock);
/* check if app id is already registered */
slot = plc_lib_find_app(app->app_id);
if (slot == NULL || slot != app) {
/* application not registered */
IOT_ASSERT(0);
}
buf = iot_ipc_pkt_alloc(PLC_LIB_MSG_SHORT_BUF_SIZE, IOT_PLC_LIB_MID);
IOT_ASSERT(buf);
/* prepare a IOT_PLC_MSG_BEACON_DATA_SET message */
q = (iot_plc_beacon_data_set_t *)plc_lib_prep_msg(buf, app->app_id,
IOT_PLC_MSG_BEACON_DATA_SET, PLC_LIB_REQ_ID_DEFAULT);
os_mem_set(q->data, 0, sizeof(q->data));
os_mem_cpy(q->data, data, len);
iot_pkt_put(buf, sizeof(*q));
os_release_mutex(p_plc_lib->lock);
iot_ipc_send(p_plc_lib->ipc_h, &plc_stack_addr, buf);
return 0;
}
void iot_plc_query_node_info(iot_plc_app_h handle, uint8_t req_id,
uint8_t ver, uint8_t *sta_mac, uint8_t sta_cnt)
{
iot_pkt_t *buf;
iot_plc_app_t *slot, *app;
iot_plc_node_info_query_t *req;
app = handle;
uint16_t alloc_len = 0;
uint8_t real_cnt = 0;
os_acquire_mutex(p_plc_lib->lock);
/* check if app id is already registered */
slot = plc_lib_find_app(app->app_id);
if (slot == NULL || slot != app) {
/* application not registered */
IOT_ASSERT(0);
}
real_cnt = min(sta_cnt, IOT_PLC_NODE_QUERY_MAX_CNT);
alloc_len = sizeof(iot_plc_node_info_query_t) + real_cnt * IOT_MAC_ADDR_LEN
+ sizeof(iot_plc_msg_header_t);
if (alloc_len > PLC_LIB_MSG_SHORT_BUF_SIZE) {
buf = iot_ipc_pkt_alloc(PLC_LIB_MSG_LONG_BUF_SIZE, IOT_PLC_LIB_MID);
} else {
buf = iot_ipc_pkt_alloc(PLC_LIB_MSG_SHORT_BUF_SIZE, IOT_PLC_LIB_MID);
}
IOT_ASSERT(buf);
/* prepare a IOT_PLC_MSG_NODE_INFO_QUERY message */
req = (iot_plc_node_info_query_t*)plc_lib_prep_msg(buf, app->app_id,
IOT_PLC_MSG_NODE_INFO_QUERY, req_id);
req->version = ver;
req->node_cnt = real_cnt;
os_mem_cpy((uint8_t *)req->mac, sta_mac, real_cnt * IOT_MAC_ADDR_LEN);
os_release_mutex(p_plc_lib->lock);
iot_ipc_send(p_plc_lib->ipc_h, &plc_stack_addr, buf);
}
void iot_plc_query_nw_info(iot_plc_app_h handle, uint8_t req_id)
{
iot_pkt_t *buf;
iot_plc_app_t *slot, *app;
app = handle;
os_acquire_mutex(p_plc_lib->lock);
/* check if app id is already registered */
slot = plc_lib_find_app(app->app_id);
if (slot == NULL || slot != app) {
/* application not registered */
IOT_ASSERT(0);
}
buf = iot_ipc_pkt_alloc(PLC_LIB_MSG_SHORT_BUF_SIZE, IOT_PLC_LIB_MID);
IOT_ASSERT(buf);
// prepare a IOT_PLC_MSG_NW_INFO_QUERY message
plc_lib_prep_msg(buf, app->app_id,
IOT_PLC_MSG_NW_INFO_QUERY, req_id);
os_release_mutex(p_plc_lib->lock);
iot_ipc_send(p_plc_lib->ipc_h, &plc_stack_addr, buf);
}
void iot_plc_set_sta_phy_phase(iot_plc_app_h handle, uint8_t req_id,
uint8_t mac[], uint8_t phase, uint8_t opposite_phase)
{
iot_pkt_t *buf;
iot_plc_app_t *slot, *app;
iot_plc_phy_phase_set_req_t *q = NULL;
app = handle;
os_acquire_mutex(p_plc_lib->lock);
/* check if app id is already registered */
slot = plc_lib_find_app(app->app_id);
if (slot == NULL || slot != app) {
/* application not registered */
IOT_ASSERT(0);
}
buf = iot_ipc_pkt_alloc(PLC_LIB_MSG_SHORT_BUF_SIZE, IOT_PLC_LIB_MID);
IOT_ASSERT(buf);
/* prepare a IOT_PLC_MSG_PHY_PHASE_SET_REQ message */
q = (iot_plc_phy_phase_set_req_t*)plc_lib_prep_msg(buf, app->app_id,
IOT_PLC_MSG_PHY_PHASE_SET_REQ, req_id);
os_mem_cpy(q->mac, mac, IOT_MAC_ADDR_LEN);
q->phy_phase = phase & 0x7;
q->opposite_phase = !!opposite_phase;
iot_pkt_put(buf, sizeof(*q));
os_release_mutex(p_plc_lib->lock);
iot_ipc_send(p_plc_lib->ipc_h, &plc_stack_addr, buf);
}
void iot_plc_set_app_load(iot_plc_app_h handle, uint8_t req_id, uint8_t load)
{
uint8_t i;
uint8_t old_load = IOT_PLC_APP_LOAD_MIN;
uint8_t new_load = IOT_PLC_APP_LOAD_MIN;
iot_pkt_t *buf;
iot_plc_app_t *slot, *app;
iot_plc_app_load_set_req_t *q = NULL;
app = handle;
if (load > IOT_PLC_APP_LOAD_MAX) {
return;
}
os_acquire_mutex(p_plc_lib->lock);
/* check if app id is already registered */
slot = plc_lib_find_app(app->app_id);
if (slot == NULL || slot != app) {
/* application not registered */
IOT_ASSERT(0);
}
/* find the heaviest load as the applications load */
for (i = 0; i < PLC_LIB_APP_SUPP_MAX; i++) {
if (p_plc_lib->app[i].app_id != 0) {
if (old_load < p_plc_lib->app[i].load) {
old_load = p_plc_lib->app[i].load;
}
}
}
app->load = load;
for (i = 0; i < PLC_LIB_APP_SUPP_MAX; i++) {
if (p_plc_lib->app[i].app_id != 0) {
if (new_load < p_plc_lib->app[i].load) {
new_load = p_plc_lib->app[i].load;
}
}
}
if (new_load == old_load) {
os_release_mutex(p_plc_lib->lock);
return;
}
buf = iot_ipc_pkt_alloc(PLC_LIB_MSG_SHORT_BUF_SIZE, IOT_PLC_LIB_MID);
IOT_ASSERT(buf);
/* prepare a IOT_PLC_MSG_HEAVY_LOAD_SET_REQ message */
q = (iot_plc_app_load_set_req_t*)plc_lib_prep_msg(buf,
app->app_id, IOT_PLC_MSG_APP_LOAD_SET_REQ, req_id);
q->load = load;
iot_pkt_put(buf, sizeof(*q));
os_release_mutex(p_plc_lib->lock);
iot_ipc_send(p_plc_lib->ipc_h, &plc_stack_addr, buf);
}
void iot_plc_set_repeater_addr_range(iot_plc_app_h handle, uint8_t req_id,
uint8_t *start_addr, uint8_t *end_addr)
{
iot_pkt_t *buf;
iot_plc_app_t *slot, *app;
iot_plc_repeater_addr_range_set_req_t *req = NULL;
app = handle;
os_acquire_mutex(p_plc_lib->lock);
/* check if app id is already registered */
slot = plc_lib_find_app(app->app_id);
if (slot == NULL || slot != app) {
/* application not registered */
IOT_ASSERT(0);
}
buf = iot_ipc_pkt_alloc(PLC_LIB_MSG_SHORT_BUF_SIZE, IOT_PLC_LIB_MID);
IOT_ASSERT(buf);
/* prepare a IOT_PLC_MSG_SET_REPEATER_ADDR_RANGE message */
req = (iot_plc_repeater_addr_range_set_req_t*)plc_lib_prep_msg(buf,
app->app_id, IOT_PLC_MSG_SET_REPEATER_ADDR_RANGE, req_id);
iot_mac_addr_cpy(req->repeater_addr_start, start_addr);
iot_mac_addr_cpy(req->repeater_addr_end, end_addr);
iot_pkt_put(buf, sizeof(*req));
os_release_mutex(p_plc_lib->lock);
iot_ipc_send(p_plc_lib->ipc_h, &plc_stack_addr, buf);
}
void iot_plc_set_app_sniffer_cmd(iot_plc_app_h handle, uint8_t req_id,
uint8_t cmd)
{
iot_pkt_t *buf;
iot_plc_app_t *slot, *app;
iot_plc_app_sniffer_cfg_t *cfg = NULL;
app = handle;
os_acquire_mutex(p_plc_lib->lock);
/* check if app id is already registered */
slot = plc_lib_find_app(app->app_id);
if (slot == NULL || slot != app) {
/* application not registered */
IOT_ASSERT(0);
}
buf = iot_ipc_pkt_alloc(PLC_LIB_MSG_SHORT_BUF_SIZE, IOT_PLC_LIB_MID);
IOT_ASSERT(buf);
/* prepare a IOT_PLC_MSG_APP_SNIFFER_CFG message */
cfg = (iot_plc_app_sniffer_cfg_t*)plc_lib_prep_msg(buf,
app->app_id, IOT_PLC_MSG_APP_SNIFFER_CFG, req_id);
cfg->cmd = !!cmd;
iot_pkt_put(buf, sizeof(*cfg));
os_release_mutex(p_plc_lib->lock);
iot_ipc_send(p_plc_lib->ipc_h, &plc_stack_addr, buf);
}
void iot_plc_set_nw_nego(iot_plc_app_h handle, uint8_t req_id,
uint8_t enable)
{
iot_pkt_t *buf;
iot_plc_app_t *slot, *app;
iot_plc_set_nw_nego_t *q = NULL;
app = handle;
os_acquire_mutex(p_plc_lib->lock);
/* check if app id is already registered */
slot = plc_lib_find_app(app->app_id);
if (slot == NULL || slot != app) {
/* application not registered */
IOT_ASSERT(0);
}
buf = iot_ipc_pkt_alloc(sizeof(iot_plc_msg_header_t) +
sizeof(iot_plc_set_nw_nego_t), IOT_PLC_LIB_MID);
IOT_ASSERT(buf);
/* prepare a IOT_PLC_MSG_NW_NEGO_SET message */
q = (iot_plc_set_nw_nego_t *)plc_lib_prep_msg(buf, app->app_id,
IOT_PLC_MSG_NW_NEGO_SET, req_id);
q->enable = !!enable;
iot_pkt_put(buf, sizeof(*q));
os_release_mutex(p_plc_lib->lock);
iot_ipc_send(p_plc_lib->ipc_h, &plc_stack_addr, buf);
}
void iot_plc_set_phy_phase_ident(iot_plc_app_h handle, uint8_t req_id,
uint8_t enable)
{
iot_pkt_t *buf;
iot_plc_app_t *slot, *app;
iot_plc_phy_phase_ident_set_req_t *q = NULL;
app = handle;
os_acquire_mutex(p_plc_lib->lock);
/* check if app id is already registered */
slot = plc_lib_find_app(app->app_id);
if (slot == NULL || slot != app) {
/* application not registered */
IOT_ASSERT(0);
}
buf = iot_ipc_pkt_alloc(PLC_LIB_MSG_SHORT_BUF_SIZE, IOT_PLC_LIB_MID);
IOT_ASSERT(buf);
/* prepare a IOT_PLC_MSG_PHY_PHASE_IDENT_SET message */
q = (iot_plc_phy_phase_ident_set_req_t*)plc_lib_prep_msg(buf, app->app_id,
IOT_PLC_MSG_PHY_PHASE_IDENT_SET, req_id);
q->enable = !!enable;
iot_pkt_put(buf, sizeof(*q));
os_release_mutex(p_plc_lib->lock);
iot_ipc_send(p_plc_lib->ipc_h, &plc_stack_addr, buf);
}
void iot_plc_query_wl_ext(iot_plc_app_h handle, uint8_t req_id,
uint16_t start, uint16_t cnt)
{
iot_pkt_t *buf = NULL;
iot_plc_app_t *slot = NULL, *app = NULL;
iot_plc_wl_query_t *q = NULL;
app = handle;
os_acquire_mutex(p_plc_lib->lock);
/* check if app id is already registered */
slot = plc_lib_find_app(app->app_id);
if (slot == NULL || slot != app) {
/* application not registered */
IOT_ASSERT(0);
}
buf = iot_ipc_pkt_alloc(PLC_LIB_MSG_SHORT_BUF_SIZE, IOT_PLC_LIB_MID);
IOT_ASSERT(buf);
q = (iot_plc_wl_query_t *)plc_lib_prep_msg(buf, app->app_id,
IOT_PLC_MSG_NW_WL_EXT_QUERY, req_id);
q->start = start;
q->count = cnt;
iot_pkt_put(buf, sizeof(*q));
os_release_mutex(p_plc_lib->lock);
iot_ipc_send(p_plc_lib->ipc_h, &plc_stack_addr, buf);
return;
}
#else /* PLC_SUPPORT_CCO_ROLE */
void iot_plc_lid_alloc_req(iot_plc_app_h handle, uint8_t lid)
{
(void)handle;
(void)lid;
}
void iot_plc_lid_rel_req(iot_plc_app_h handle, uint8_t lid)
{
(void)handle;
(void)lid;
}
void iot_plc_query_nw_topo(iot_plc_app_h handle, uint8_t req_id,
uint8_t req_data_type, uint8_t start_type, uint16_t start, uint16_t cnt)
{
(void)handle;
(void)req_id;
(void)req_data_type;
(void)start_type;
(void)start;
(void)cnt;
}
void iot_plc_set_nid(iot_plc_app_h handle, uint8_t req_id, uint32_t new_nid)
{
(void)handle;
(void)req_id;
(void)new_nid;
}
uint32_t iot_plc_set_beacon_data(iot_plc_app_h handle, uint8_t *data,
uint8_t len)
{
(void)handle;
(void)data;
(void)len;
return ERR_NOSUPP;
}
void iot_plc_query_node_info(iot_plc_app_h handle, uint8_t req_id,
uint8_t ver, uint8_t *sta_mac, uint8_t sta_cnt)
{
(void)handle;
(void)req_id;
(void)ver;
(void)sta_mac;
(void)sta_cnt;
}
void iot_plc_query_nw_info(iot_plc_app_h handle, uint8_t req_id)
{
(void)handle;
(void)req_id;
}
void iot_plc_set_sta_phy_phase(iot_plc_app_h handle, uint8_t req_id,
uint8_t mac[], uint8_t phase, uint8_t opposite_phase)
{
(void)handle;
(void)req_id;
(void)mac;
(void)phase;
(void)opposite_phase;
}
void iot_plc_set_app_load(iot_plc_app_h handle, uint8_t req_id, uint8_t load)
{
(void)handle;
(void)req_id;
(void)load;
}
void iot_plc_set_repeater_addr_range(iot_plc_app_h handle, uint8_t req_id,
uint8_t *start_addr, uint8_t *end_addr)
{
(void)handle;
(void)req_id;
(void)start_addr;
(void)end_addr;
}
void iot_plc_set_app_sniffer_cmd(iot_plc_app_h handle, uint8_t req_id,
uint8_t cmd)
{
(void)handle;
(void)req_id;
(void)cmd;
}
void iot_plc_set_nw_nego(iot_plc_app_h handle, uint8_t req_id,
uint8_t enable)
{
(void)handle;
(void)req_id;
(void)enable;
}
void iot_plc_set_phy_phase_ident(iot_plc_app_h handle, uint8_t req_id,
uint8_t enable)
{
(void)handle;
(void)req_id;
(void)enable;
}
void iot_plc_query_wl_ext(iot_plc_app_h handle, uint8_t req_id,
uint16_t start, uint16_t cnt)
{
(void)handle;
(void)req_id;
(void)start;
(void)cnt;
}
#endif /* PLC_SUPPORT_CCO_ROLE */