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

381 lines
10 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_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 */