初始提交

This commit is contained in:
2024-09-28 14:24:04 +08:00
commit c756587541
5564 changed files with 2413077 additions and 0 deletions

View File

@@ -0,0 +1,142 @@
/****************************************************************************
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.
****************************************************************************/
#ifndef PLC_LIB_INTERNAL_H
#define PLC_LIB_INTERNAL_H
/* os shim includes */
#include "os_types.h"
#include "os_lock.h"
#include "os_event.h"
#include "os_task.h"
/* common includes */
#include "iot_queue.h"
#include "iot_mem_pool.h"
#include "iot_ipc.h"
#include "iot_plc_api.h"
#include "iot_task.h"
#include "iot_pkt_api.h"
#include "iot_config.h"
#ifdef __cplusplus
extern "C" {
#endif
#if PLC_SUPPORT_CCO_ROLE
/* define plc lib message pool size */
#define PLC_LIB_MSG_POOL_SIZE 255
#else
/* define plc lib message pool size */
#define PLC_LIB_MSG_POOL_SIZE 128
#endif
/* define the plc lib message queue count and priorities, the higer the priority
* the lower the queue number.
*/
#define PLC_LIB_MSG_QUEUE_HP 0
#define PLC_LIB_MSG_QUEUE_MAX_PRIO 1
/* define max number of concurrent app supported */
#define PLC_LIB_APP_SUPP_MAX 4
/* define plc lib internal message id */
#define PLC_LIB_MSG_IPC_EVENT 1
/* default request id */
#define PLC_LIB_REQ_ID_DEFAULT 0
/* plc lib internal message */
typedef struct _plc_lib_msg {
/* iot task message */
iot_task_msg_t task_msg;
/* pointer to message data */
void *data;
} plc_lib_msg_t;
/* plc lib gobal variable */
typedef struct _plc_lib_global {
/* plc lib task configuration */
iot_task_config_t task_cfg;
/* plc lib task handle */
iot_task_h task_h;
/* ipc handler to communicate with plc stack */
iot_ipc_h ipc_h;
/* lock to protect the app array */
os_mutex_h lock;
/* app info array */
iot_plc_app_t app[PLC_LIB_APP_SUPP_MAX];
/* device running mode */
uint8_t proto;
/* device running role */
uint8_t client;
} plc_lib_global_t;
extern plc_lib_global_t *p_plc_lib;
extern iot_ipc_addr_t plc_stack_addr;
/**
* @brief plc_lib_find_app() - find plc app info by app id
* @param app_id: application id to be searched
*
* @return:
* NULL -- for failure case
* othersie -- plc application info pointer
*/
iot_plc_app_t *plc_lib_find_app(uint8_t app_id);
/**
* @brief plc_lib_find_free_app() - find free plc app slot
*
* @return:
* NULL -- for failure case
* othersie -- free plc application slot pointer
*/
iot_plc_app_t *plc_lib_find_free_app();
/**
* @brief plc_lib_prep_msg() - prepare plc lib message header a iot pkt
* @param pkt: pointer to the packet
* @param app_id: application id of the packet
* @param msg_id: msg id of the packet
* @param req_id: request id
* @return:
* pointer to the buffer right after the plc lib message header
*/
uint8_t *plc_lib_prep_msg(iot_pkt_t *buf, uint8_t app_id,
uint8_t msg_id, uint8_t req_id);
/**
* @brief iot_plc_query() - query plc info
* @param handle: plc application handler
* @param req_id: request id, the request id will be transferred back to
* app in the report. app can define the mean of each id
* itself.
* @param msg_id: message id of the info to be queried.
* see IOT_PLC_MSG_XXX.
*/
void iot_plc_query(iot_plc_app_h handle, uint8_t req_id, uint8_t msg_id);
#ifdef __cplusplus
}
#endif
#endif /* PLC_LIB_INTERNAL_H */

1295
common/plc_lib/src/iot_plc_api.c Executable file

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,642 @@
/****************************************************************************
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 */

View File

@@ -0,0 +1,380 @@
/****************************************************************************
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_sta_api.h"
#include "iot_config.h"
#include "iot_plc_lib.h"
/* plc lib internal includes */
#include "plc_lib_internal.h"
#if PLC_SUPPORT_STA_ROLE
void iot_plc_set_discovery_mode(iot_plc_app_h handle, uint8_t req_id,
uint8_t enable)
{
iot_pkt_t *buf;
iot_plc_app_t *slot, *app;
iot_plc_enable_discovery_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_ENABLE_DISCOVERY_REQ message */
q = (iot_plc_enable_discovery_req_t *)plc_lib_prep_msg(buf, app->app_id,
IOT_PLC_MSG_ENABLE_DISCOVERY_REQ, 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_tsfm_status(iot_plc_app_h handle, uint8_t req_id)
{
iot_plc_query(handle, req_id, IOT_PLC_MSG_TSFM_STATUS_REQ);
}
void iot_plc_ctrl_proto_connect(iot_plc_app_h handle, uint8_t req_id,
uint8_t *addr)
{
iot_pkt_t *buf;
iot_plc_app_t *slot, *app;
iot_plc_ctrl_proto_connect_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_CTRL_PROTO_CONNECT_REQ message */
req = (iot_plc_ctrl_proto_connect_req_t *)plc_lib_prep_msg(buf, app->app_id,
IOT_PLC_MSG_CTRL_PROTO_CONNECT_REQ, req_id);
iot_mac_addr_cpy(req->addr, 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_scan_band_bitmap(iot_plc_app_h handle, uint8_t req_id,
uint8_t *band_bitmap, uint32_t size)
{
IOT_ASSERT(band_bitmap && size == IOT_PLC_BAND_BITMAP_SIZE);
iot_pkt_t *buf;
iot_plc_app_t *slot, *app;
iot_plc_set_scanband_bitmap_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_SCANBAND_BITMAP_SET message */
q = (iot_plc_set_scanband_bitmap_t *)plc_lib_prep_msg(buf, app->app_id,
IOT_PLC_MSG_SCANBAND_BITMAP_SET, req_id);
os_mem_cpy(q->band_bitmap, band_bitmap, size);
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_phase_mask(iot_plc_app_h handle, uint8_t req_id,
uint8_t phase_mask)
{
iot_pkt_t *buf;
iot_plc_app_t *slot, *app;
iot_plc_set_phase_mask_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_PHASE_MASK_SET message */
q = (iot_plc_set_phase_mask_t *)plc_lib_prep_msg(buf, app->app_id,
IOT_PLC_MSG_PHASE_MASK_SET, req_id);
q->phase_mask = phase_mask;
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_pkt_capture(iot_plc_app_h handle, uint8_t req_id,
uint8_t *bm, uint16_t bm_len, uint8_t enable)
{
iot_pkt_t *buf;
iot_plc_app_t *slot, *app;
iot_plc_set_pkt_capture_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(sizeof(iot_plc_msg_header_t) +
sizeof(iot_plc_set_pkt_capture_req_t) + bm_len, IOT_PLC_LIB_MID);
IOT_ASSERT(buf);
/* prepare a IOT_PLC_MSG_PKT_CAPTURE_SET_REQ message */
q = (iot_plc_set_pkt_capture_req_t *)plc_lib_prep_msg(buf, app->app_id,
IOT_PLC_MSG_PKT_CAPTURE_SET_REQ, req_id);
q->bm_len = bm_len;
os_mem_cpy(q->bm, bm, bm_len);
q->enable = enable;
iot_pkt_put(buf, sizeof(*q) + bm_len);
os_release_mutex(p_plc_lib->lock);
iot_ipc_send(p_plc_lib->ipc_h, &plc_stack_addr, buf);
}
void iot_plc_set_pm_addr(iot_plc_app_h handle, uint8_t req_id,
uint8_t *addr_array, uint8_t cnt)
{
iot_pkt_t *buf;
iot_plc_app_t *slot, *app;
iot_plc_set_pm_addr_t *q = NULL;
uint32_t len = 0;
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);
}
/* allocate buf according to power meter count */
buf = iot_ipc_pkt_alloc(sizeof(iot_plc_msg_header_t)
+ sizeof(iot_plc_set_pm_addr_t) + cnt * IOT_MAC_ADDR_LEN,
IOT_PLC_LIB_MID);
IOT_ASSERT(buf);
/* prepare a IOT_PLC_MSG_PM_ADDR_SET message */
q = (iot_plc_set_pm_addr_t *)plc_lib_prep_msg(buf, app->app_id,
IOT_PLC_MSG_PM_ADDR_SET, req_id);
q->cnt = cnt;
iot_pkt_put(buf, sizeof(*q));
/* make sure there is enough space for storing power meter address */
len = iot_pkt_block_len(buf, IOT_PKT_BLOCK_TAIL);
IOT_ASSERT(len >= (uint32_t)(cnt * IOT_MAC_ADDR_LEN));
if (cnt) {
os_mem_cpy(q->addr, addr_array, cnt * IOT_MAC_ADDR_LEN);
iot_pkt_put(buf, cnt * IOT_MAC_ADDR_LEN);
}
os_release_mutex(p_plc_lib->lock);
iot_ipc_send(p_plc_lib->ipc_h, &plc_stack_addr, buf);
}
uint32_t iot_plc_hw_tsfm_pa_send(uint8_t *data, uint8_t len, uint8_t send_cnt,
uint32_t freq_tone_num)
{
iot_pkt_t *buf;
iot_plc_hw_tsfm_pa_send_t *q = NULL;
/* frequency tone num max support 16380 */
if ((0 == freq_tone_num) || (freq_tone_num >= 16380)) {
return ERR_INVAL;
}
os_acquire_mutex(p_plc_lib->lock);
/* allocate buf according to power meter count */
buf = iot_ipc_pkt_alloc(sizeof(iot_plc_msg_header_t) + sizeof(*q),
IOT_PLC_LIB_MID);
IOT_ASSERT(buf);
/* prepare a IOT_PLC_MSG_PM_ADDR_SET message */
q = (iot_plc_hw_tsfm_pa_send_t *)plc_lib_prep_msg(buf, 0,
IOT_PLC_MSG_HW_TSFM_PA_SEND, 0);
q->len = len;
q->send_cnt = send_cnt;
q->freq_tone_num = freq_tone_num;
iot_pkt_put(buf, sizeof(*q));
os_mem_cpy(q->data, data, len);
os_release_mutex(p_plc_lib->lock);
iot_ipc_send(p_plc_lib->ipc_h, &plc_stack_addr, buf);
return ERR_OK;
}
void iot_plc_set_pm_save_cfg(uint8_t allow_sleep)
{
iot_pkt_t *buf;
iot_plc_set_pm_save_cfg_t *q = NULL;
os_acquire_mutex(p_plc_lib->lock);
/* allocate buf according to power save cfg */
buf = iot_ipc_pkt_alloc(sizeof(iot_plc_msg_header_t) + sizeof(*q),
IOT_PLC_LIB_MID);
IOT_ASSERT(buf);
/* prepare a IOT_PLC_MSG_PM_SAVE_CFG_SET message */
q = (iot_plc_set_pm_save_cfg_t *)plc_lib_prep_msg(buf, 0,
IOT_PLC_MSG_PM_SAVE_CFG_SET, 0);
q->allow_sleep = !!allow_sleep;
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);
}
#else /* PLC_SUPPORT_STA_ROLE */
void iot_plc_set_discovery_mode(iot_plc_app_h handle, uint8_t req_id,
uint8_t enable)
{
(void)handle;
(void)req_id;
(void)enable;
}
void iot_plc_query_tsfm_status(iot_plc_app_h handle, uint8_t req_id)
{
(void)handle;
(void)req_id;
}
void iot_plc_ctrl_proto_connect(iot_plc_app_h handle, uint8_t req_id,
uint8_t *addr)
{
(void)handle;
(void)req_id;
(void)addr;
}
void iot_plc_set_scan_band_bitmap(iot_plc_app_h handle, uint8_t req_id,
uint8_t *band_bitmap, uint32_t size)
{
(void)handle;
(void)req_id;
(void)band_bitmap;
(void)size;
}
void iot_plc_set_phase_mask(iot_plc_app_h handle, uint8_t req_id,
uint8_t phase_mask)
{
(void)handle;
(void)req_id;
(void)phase_mask;
}
void iot_plc_set_pkt_capture(iot_plc_app_h handle, uint8_t req_id,
uint8_t *bm, uint16_t bm_len, uint8_t enable)
{
(void)handle;
(void)req_id;
(void)bm;
(void)bm_len;
(void)enable;
}
void iot_plc_set_pm_addr(iot_plc_app_h handle, uint8_t req_id,
uint8_t *addr_array, uint8_t cnt)
{
(void)handle;
(void)req_id;
(void)addr_array;
(void)cnt;
}
uint32_t iot_plc_hw_tsfm_pa_send(uint8_t *data, uint8_t len, uint8_t send_cnt,
uint32_t freq_tone_num)
{
(void)data;
(void)len;
(void)send_cnt;
(void)freq_tone_num;
return ERR_NOSUPP;
}
void iot_plc_set_pm_save_cfg(uint8_t allow_sleep)
{
(void)allow_sleep;
}
#endif /* PLC_SUPPORT_STA_ROLE */

View File

@@ -0,0 +1,260 @@
/****************************************************************************
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.
****************************************************************************/
/* common includes */
#include "iot_errno.h"
#include "iot_module.h"
#include "iot_config.h"
#include "iot_utils.h"
#include "iot_ipc.h"
#include "iot_plc_api.h"
#include "iot_dbglog_api.h"
#include "iot_task.h"
#include "iot_oem_api.h"
/* plc lib internal includes */
#include "plc_lib_internal.h"
#ifndef PLC_LIB_STANDALONE_TASK
#define PLC_LIB_STANDALONE_TASK 0
#endif
#ifndef PLC_LIB_TASK_PRIO
#define PLC_LIB_TASK_PRIO 7
#endif
plc_lib_global_t *p_plc_lib = NULL;
static inline void plc_lib_handle_ipc_event(plc_lib_global_t *glb,
iot_pkt_t *buf)
{
iot_pkt_t *tmp_pkt = NULL;
iot_plc_msg_header_t *header = NULL;
iot_plc_app_t *app = NULL;
uint8_t index = 0;
uint32_t buf_len = 0;
/* as the message is delivered from internal plc stack, so skip security
* check to improve efficient.
*/
buf_len = iot_pkt_block_len(buf, IOT_PKT_BLOCK_ALL);
header = (iot_plc_msg_header_t *)iot_pkt_block_ptr(buf, IOT_PKT_BLOCK_DATA);
os_acquire_mutex(glb->lock);
if (header->app_id == IOT_PLC_APP_ID_BCAST) {
/* if it's a broadcast message, broadcast it to all apps */
for (index = 0; index < PLC_LIB_APP_SUPP_MAX; index++) {
if (p_plc_lib->app[index].app_id) {
tmp_pkt = iot_pkt_alloc(buf_len, IOT_PLC_LIB_MID);
if (tmp_pkt) {
/* allocate a new pkt to for each app broadcasting */
iot_pkt_cpy(tmp_pkt, buf);
p_plc_lib->app[index].recv(
p_plc_lib->app[index].param, tmp_pkt);
}
}
}
/* free the original packet */
iot_pkt_free(buf);
} else {
app = plc_lib_find_app(header->app_id);
if (app) {
app->recv(app->param, buf);
} else {
iot_pkt_free(buf);
}
}
os_release_mutex(glb->lock);
}
/* ipc callback to receive packet from plc stack */
static void plc_lib_ipc_recv(void *param, iot_ipc_addr_t *addr,
iot_pkt_t *pkt)
{
(void)param;
(void)addr;
if (PLC_LIB_STANDALONE_TASK) {
iot_task_msg_t *msg = iot_task_alloc_msg(p_plc_lib->task_h);
plc_lib_msg_t *plc_msg = (plc_lib_msg_t *)msg;
if (msg) {
plc_msg->data = pkt;
msg->id = PLC_LIB_MSG_IPC_EVENT;
iot_task_queue_msg(p_plc_lib->task_h, msg, PLC_LIB_MSG_QUEUE_HP);
} else {
iot_pkt_free(pkt);
IOT_ASSERT(0);
}
} else {
plc_lib_handle_ipc_event(p_plc_lib, pkt);
}
}
/* plc lib internal msg handling function */
static void plc_lib_handle_msg(iot_task_h task_h, iot_task_msg_t *msg)
{
plc_lib_msg_t *plc_msg = (plc_lib_msg_t *)msg;
switch (msg->id) {
case PLC_LIB_MSG_IPC_EVENT:
plc_lib_handle_ipc_event(p_plc_lib, plc_msg->data);
break;
default:
IOT_ASSERT(0);
break;
}
iot_task_free_msg(task_h, msg);
}
/* plc lib internal msg cancel function */
static void plc_lib_handle_msg_cancel(iot_task_h task_h,
iot_task_msg_t *msg)
{
plc_lib_msg_t *plc_msg = (plc_lib_msg_t *)msg;
switch (msg->id) {
case PLC_LIB_MSG_IPC_EVENT:
iot_pkt_free((iot_pkt_t *)plc_msg->data);
break;
default:
IOT_ASSERT(0);
break;
}
iot_task_free_msg(task_h, msg);
}
iot_plc_app_t *plc_lib_find_app(uint8_t app_id)
{
uint8_t i;
for (i = 0; i < PLC_LIB_APP_SUPP_MAX; i++) {
if (p_plc_lib->app[i].app_id == app_id)
return &p_plc_lib->app[i];
}
return NULL;
}
iot_plc_app_t *plc_lib_find_free_app()
{
uint8_t i;
iot_plc_app_t *ret = NULL;
for (i = 0; i < PLC_LIB_APP_SUPP_MAX; i++) {
if (p_plc_lib->app[i].app_id == 0) {
ret = &p_plc_lib->app[i];
break;
}
}
return ret;
}
uint32_t iot_plc_lib_init(uint8_t proto)
{
uint32_t ret = 0;
iot_ipc_client_t client;
if (p_plc_lib)
goto out;
p_plc_lib = os_mem_malloc(IOT_PLC_LIB_MID, sizeof(*p_plc_lib));
if (p_plc_lib == NULL) {
ret = ERR_NOMEM;
goto out;
}
p_plc_lib->lock = os_create_mutex(IOT_PLC_LIB_MID);
if (p_plc_lib->lock == NULL) {
ret = ERR_NOMEM;
goto err;
}
p_plc_lib->proto = proto;
os_mem_set(&p_plc_lib->task_cfg, 0, sizeof(p_plc_lib->task_cfg));
if (PLC_LIB_STANDALONE_TASK) {
/* create plc lib task */
p_plc_lib->task_cfg.stack_size = 0;
p_plc_lib->task_cfg.task_prio = PLC_LIB_TASK_PRIO;
p_plc_lib->task_cfg.msg_size = sizeof(plc_lib_msg_t);
p_plc_lib->task_cfg.msg_cnt = PLC_LIB_MSG_POOL_SIZE;
p_plc_lib->task_cfg.queue_cnt = PLC_LIB_MSG_QUEUE_MAX_PRIO;
p_plc_lib->task_cfg.queue_cfg[PLC_LIB_MSG_QUEUE_HP].quota = 0;
p_plc_lib->task_cfg.msg_exe_func = plc_lib_handle_msg;
p_plc_lib->task_cfg.msg_cancel_func = plc_lib_handle_msg_cancel;
p_plc_lib->task_h = iot_task_create(IOT_PLC_LIB_MID,
&p_plc_lib->task_cfg);
if (p_plc_lib->task_h == NULL)
goto err_task;
}
/* register ipc to communicate with plc stack */
client.addr.f_id = IOT_IPC_FID_PLC;
client.addr.c_id = IOT_IPC_CID_PLC_LIB;
client.recv = plc_lib_ipc_recv;
client.param = p_plc_lib;
p_plc_lib->ipc_h = iot_ipc_register_client(&client);
if (p_plc_lib->ipc_h == NULL) {
ret = ERR_NOMEM;
goto err_ipc;
}
#if HW_PLATFORM == HW_PLATFORM_SIMU
extern uint8_t ucIsClientMode;
p_plc_lib->client = ucIsClientMode;
#else /* HW_PLATFORM == HW_PLATFORM_SIMU */
iot_oem_base_cfg_t *oem_cfg = NULL;
iot_oem_get_base_cfg(&oem_cfg);
if (oem_cfg->module_type == MODULE_TYPE_CCO) {
p_plc_lib->client = 0;
} else {
p_plc_lib->client = 1;
}
#endif /* HW_PLATFORM == HW_PLATFORM_SIMU */
goto out;
err_ipc:
if (p_plc_lib->task_h) {
iot_task_delete(p_plc_lib->task_h);
}
err_task:
os_delete_mutex(p_plc_lib->lock);
err:
os_mem_free(p_plc_lib);
p_plc_lib = NULL;
out:
return ret;
}
void iot_plc_lib_deinit()
{
if (p_plc_lib) {
iot_ipc_deregister_client(p_plc_lib->ipc_h);
iot_task_delete(p_plc_lib->task_h);
os_delete_mutex(p_plc_lib->lock);
os_mem_free(p_plc_lib);
p_plc_lib = NULL;
}
}