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

835 lines
20 KiB
C
Executable File
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/****************************************************************************
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_timer_api.h"
#include "os_utils_api.h"
#include "iot_io_api.h"
#include "iot_pkt_api.h"
#include "iot_errno_api.h"
#include "iot_utils_api.h"
#include "iot_version_api.h"
#include "iot_plc_api.h"
#include "iot_plc_msg_api.h"
/* called by meta to get api information */
#include "iot_app_meta_api.h"
#include "iot_app_meta_handle.h"
#include "iot_app_meta_buffer_handle.h"
typedef uint8_t (* META_CMD_HANDLE)(void);
#define META_MSDU_RECD_CNT (5)
#define OP_QUERY (0x00)
#define OP_RESPONSE (0x01)
/* update interval (ms) */
#define META_UPDATE_INTERVAL (10000)
#define META_VERSION (0x00000001)
/**********type of version information ************/
#define META_INFO_TYPE_NULL (0x00)
/**********type of version information ************/
#define META_INFO_TYPE_VER (0x01)
/********** type of network information ***********/
#define META_INFO_TYPE_NETWORK (0x02)
/********** type of app information ***************/
#define META_INFO_TYPE_APP (0x03)
/********** type of hardware information **********/
#define META_INFO_TYPE_HW (0x04)
/********** type of peripheral information ********/
#define META_INFO_TYPE_PERIPHERAL (0x05)
/********** type of upgrade information ***********/
#define META_INFO_TYPE_UPGRAD (0x06)
/********** TYPE COUNT ****************************/
#define META_INFO_TYPE_MAX (0x06)
/**********type of version information ************/
enum iot_meta_version {
META_INFO_VERSION = 0,
};
/********** type of network information ***********/
enum iot_meta_network {
/* meta cvg version */
META_NETWORK_CVG_VER = 0x00,
/* meta local tei and proxy tei */
META_NETWORK_TEI = 0x01,
/* reboot count */
META_NETWORK_REBOOT_CNT,
/* last reboot reason */
META_NETWORK_LAST_REBOOT_REASON,
/* logic phase */
META_NETWORK_LOGIC_PHASE,
/* physic phase */
META_NETWORK_PHY_PHASE,
/* traffic success ratio */
META_NETWORK_TRAFFIC_SUCCESS_RATIO,
/* snr */
META_NETWORK_SNR,
/* the SOF count recieved in 5 second */
META_NETWORK_RECV_SOF_5S,
/* the SOF count send in 5 second */
META_NETWORK_SEND_SOF_5S,
/* the network ID */
META_NETWORK_NID,
/* the neighbor cco infomation, sta only */
META_NETWORK_NEIGHBOR_CCO_INFO,
/* how long has joind the cco, sta only */
META_NETWORK_JOIND_TIME,
/* the time sta last request join cco, sta only */
META_NETWORK_LAST_JOINING_TIME,
/* the reason sta last leave cco, sta only */
META_NETWORK_LAST_LEAVE_REASON,
/* sta join cco record, sta only */
META_NETWORK_JOINDE_CCO_RECORD,
/* proxy mode mac, sta only */
META_NETWORK_PROXY_NODE_MAC,
/* meta level, sta only */
META_NETWORK_LEVEL,
/* sta state machine in this time, sta only */
META_NETWORK_STA_CUR_STATE,
/* proxy change reason and time, pco only */
META_NETWORK_PROXY_CHANGE_INFO,
/* sta join and leave cco record, cco only */
META_NETWORK_JOINED_OR_LEAVE_STA_RECORD,
};
/********** type of app information ***************/
enum iot_app_info {
/* app version */
META_APP_VER = 0x00,
/* GE,AT,DEMO */
META_APP_TYPE = 0x01,
/* role cco, sta , pco */
META_APP_ROLE = 0x02,
/* whiteliset count */
META_APP_WL_COUNT,
/* whitelist information */
META_APP_WL_INFO,
/* is whitelist enable */
META_APP_WL_ENABLE,
/* blacklist count */
META_APP_BL_COUNT,
/* blacklist information */
META_APP_BL_INFO,
/* is blacklist enable */
META_APP_BL_ENABLE,
/* the msdu count app has cend */
META_APP_SEND_MSDU_COUNT,
/* the msdu count app has recived */
META_APP_RECV_MSDU_COUNT,
/* the time recieve the first msdu */
META_APP_TIME_FIRST_RECV_MDSU,
/* the time send last msdu */
META_APP_TIME_LAST_SEND_MSDU,
/* the recent msdu app has recived */
META_APP_RECV_MSDU_INFO,
};
/********** type of hardware information **********/
enum iot_hw_info {
/* chip id */
META_HW_CHIP_ID = 0x00,
/* chip efuse id */
META_HW_CHIP_EFUSE_ID = 0x01,
/* chip serial */
META_HW_CHIP_SERIAL,
/* module type */
META_HW_MODULE_TYPE,
/* module version */
META_HW_MODULE_VER,
};
/********** type of peripheral information ********/
enum iot_peripheral_info {
/* GPIO information */
META_PERIPHERAL_GPIO_INFO = 0x01,
/* ADC information */
META_PERIPHERAL_ADC_INFO,
/* PWM information */
META_PERIPHERAL_PWM_INFO,
/* uart send and receive status*/
META_PERIPHERA_UART_INFO,
};
/********** type of upgrade information ***********/
enum iot_upgrade_info {
/* upgrade state */
META_UPGRADE_STATE = 0x00,
/* upgrade type */
META_UPGRADE_TYPE = 0x01,
/* upgrade time */
META_UPGRADE_TIME,
/* upgrade firmwork version */
META_UPGRADE_FW_VER,
/* receive data info now */
META_UPGRADE_CUR_RECV_INFO,
};
typedef struct _meta_cmd_table_t
{
/* app information */
uint16_t id_group;
uint16_t id;
META_CMD_HANDLE handle;
} meta_cmd_table_t;
/* pack for the structures in the whole file */
#pragma pack(push) // save the pack status
#pragma pack(1) // 1 byte align
typedef struct _meta_recv_msdu_info_t
{
/* msdu recv from mac */
uint8_t mac[IOT_MAC_ADDR_LEN];
/* msdu length */
uint16_t len;
/* recv msdu snr */
int8_t snr;
/* recv msdu connection type */
uint8_t conn_type;
/* record msdu info tm: ms */
uint32_t tm;
} meta_recv_msdu_info_t;
#pragma pack(pop) /* restore the pack status */
typedef struct _meta_msdu_rcd_t
{
/* recd recv msdu info save index */
uint8_t rcd_index;
/* recv msdu info */
meta_recv_msdu_info_t msdu_info[META_MSDU_RECD_CNT];
} meta_msdu_rcd_t;
typedef struct _meta_context_t
{
/* module id for alloc_pkt or alloc_mem */
uint16_t module_id;
/* app info */
uint8_t app_id;
/* device role,cco or sta */
uint8_t role;
/* msdu info send and recv */
meta_msdu_rcd_t msdu_rcd;
/* update meta info timer */
timer_id_t meta_update_timer;
} meta_context_t;
extern void iot_meta_bin_dump(uint8_t *data, uint32_t dlen);
meta_context_t g_meta_obj;
static uint8_t iot_meta_app_type_recv_msdu_pack_handle(void);
static meta_cmd_table_t meta_period_update_tbl[] =
{
{META_INFO_TYPE_APP, META_APP_RECV_MSDU_INFO, iot_meta_app_type_recv_msdu_pack_handle},
};
/**
* @brief iot_meta_app_type_recv_msdu_pack_handle() - pack msdu info to meta info
* @return: success or fail
*/
static uint8_t iot_meta_app_type_recv_msdu_pack_handle(void)
{
static uint8_t index = 0;
uint8_t ret = ERR_OK;
uint16_t datalen = sizeof(meta_recv_msdu_info_t);
iot_pkt_t * p_pkt;
meta_cmd_t *cmd;
if (index >= META_MSDU_RECD_CNT) {
index = 0;
}
if (g_meta_obj.msdu_rcd.msdu_info[index].tm == 0) {
goto out;
}
p_pkt = iot_pkt_alloc(datalen + MIN_META_CMD_LEN, g_meta_obj.module_id);
if (p_pkt == NULL) {
ret = ERR_FAIL;
iot_cus_printf("%s pkt Malloc Failed!\n", __FUNCTION__);
goto out;
}
cmd = (meta_cmd_t *)iot_pkt_put(p_pkt, datalen);
cmd->id_group = META_INFO_TYPE_APP;
cmd->id = META_APP_RECV_MSDU_INFO;
cmd->op = OP_RESPONSE;
cmd->datalen = datalen;
iot_pkt_put(p_pkt, MIN_META_CMD_LEN);
os_mem_cpy(cmd->data, &g_meta_obj.msdu_rcd.msdu_info[index], datalen);
iot_cus_printf("dump_recv msdu: %d\n", datalen);
iot_meta_bin_dump(cmd->data, datalen);
iot_meta_put_cmd_to_buf(cmd);
iot_pkt_free(p_pkt);
out:
index++;
return ret;
}
/* iot_meta_type_app_recv_msdu_unpack_handle - dump msdu info */
static void iot_meta_type_app_recv_msdu_unpack_handle(uint8_t *data)
{
meta_recv_msdu_info_t *msdu = (meta_recv_msdu_info_t *)data;
iot_cus_printf("meta recv msdu type:%d mac:%02X%02X%02X%02X%02X%02X snr=%d "
"len=%d tm=%lu \n", msdu->conn_type, msdu->mac[0], msdu->mac[1],
msdu->mac[2], msdu->mac[3], msdu->mac[4], msdu->mac[5],
msdu->snr, msdu->len, msdu->tm);
}
/**
* @brief meta_handle_id_ver() - handle version type cmd
* @param id:point to the cmd id
* @param data:point to the meta data
* @param op:operationquery or response
* @return: ERR_OK, or error reason
*/
static uint8_t meta_handle_id_ver(uint16_t id, uint8_t *data, uint8_t op)
{
uint8_t ret = ERR_OK;
switch (id) {
case META_INFO_VERSION:
{
/* todo */
break;
}
default:
{
iot_cus_printf("type ver id is %d, error\n", id);
break;
}
}
return ret;
}
/**
* @brief meta_handle_id_network() - handle network type cmd
* @param id:point to the cmd id
* @param data:point to the meta data
* @param op:operationquery or response
* @return: ERR_OK, or error reason
*/
static uint8_t meta_handle_id_network(uint16_t id, uint8_t *data, uint8_t op)
{
uint8_t ret = ERR_OK;
switch (id) {
case META_NETWORK_CVG_VER:
{
/* todo */
break;
}
case META_NETWORK_TEI:
{
/* todo */
break;
}
case META_NETWORK_REBOOT_CNT:
{
/* todo */
break;
}
case META_NETWORK_LAST_REBOOT_REASON:
{
/* todo */
break;
}
case META_NETWORK_LOGIC_PHASE:
{
/* todo */
break;
}
case META_NETWORK_PHY_PHASE:
{
/* todo */
break;
}
case META_NETWORK_TRAFFIC_SUCCESS_RATIO:
{
/* todo */
break;
}
case META_NETWORK_SNR:
{
/* todo */
break;
}
case META_NETWORK_RECV_SOF_5S:
{
/* todo */
break;
}
case META_NETWORK_SEND_SOF_5S:
{
/* todo */
break;
}
case META_NETWORK_NID:
{
/* todo */
break;
}
case META_NETWORK_NEIGHBOR_CCO_INFO:
{
/* todo */
break;
}
case META_NETWORK_JOIND_TIME:
{
/* todo */
break;
}
case META_NETWORK_LAST_JOINING_TIME:
{
/* todo */
break;
}
case META_NETWORK_LAST_LEAVE_REASON:
{
/* todo */
break;
}
case META_NETWORK_JOINDE_CCO_RECORD:
{
/* todo */
break;
}
case META_NETWORK_PROXY_NODE_MAC:
{
/* todo */
break;
}
case META_NETWORK_LEVEL:
{
/* todo */
break;
}
case META_NETWORK_STA_CUR_STATE:
{
/* todo */
break;
}
case META_NETWORK_PROXY_CHANGE_INFO:
{
/* todo */
break;
}
case META_NETWORK_JOINED_OR_LEAVE_STA_RECORD:
{
/* todo */
break;
}
default:
{
iot_cus_printf("type network id is %d, error\n", id);
break;
}
}
return ret;
}
/**
* @brief meta_handle_id_app() - handle APP type cmd
* @param id:point to the cmd id
* @param data:point to the meta data
* @param op:operationquery or response
* @return: ERR_OK, or error reason
*/
static uint8_t meta_handle_id_app(uint16_t id, uint8_t *data, uint8_t op)
{
uint8_t ret = ERR_OK;
switch (id) {
case META_APP_VER:
{
/* todo */
break;
}
case META_APP_TYPE:
{
/* todo */
break;
}
case META_APP_ROLE:
{
/* todo */
break;
}
case META_APP_WL_COUNT:
{
/* todo */
break;
}
case META_APP_WL_INFO:
{
/* todo */
break;
}
case META_APP_WL_ENABLE:
{
/* todo */
break;
}
case META_APP_BL_COUNT:
{
/* todo */
break;
}
case META_APP_BL_INFO:
{
/* todo */
break;
}
case META_APP_BL_ENABLE:
{
/* todo */
break;
}
case META_APP_SEND_MSDU_COUNT:
{
/* todo */
break;
}
case META_APP_RECV_MSDU_COUNT:
{
/* todo */
break;
}
case META_APP_TIME_FIRST_RECV_MDSU:
{
/* todo */
break;
}
case META_APP_TIME_LAST_SEND_MSDU:
{
/* todo */
break;
}
case META_APP_RECV_MSDU_INFO:
{
if (op == OP_RESPONSE) {
iot_meta_type_app_recv_msdu_unpack_handle(data);
} else {
iot_meta_app_type_recv_msdu_pack_handle();
}
break;
}
default:
{
iot_cus_printf("type app id is %d, error\n", id);
break;
}
}
return ret;
}
/**
* @brief meta_handle_id_hw() - handle hardware type cmd
* @param id:point to the cmd id
* @param data:point to the meta data
* @param op:operationquery or response
* @return: ERR_OK, or error reason
*/
static uint8_t meta_handle_id_hw(uint16_t id, uint8_t *data, uint8_t op)
{
uint8_t ret = ERR_OK;
switch (id) {
case META_HW_CHIP_ID:
{
/* todo */
break;
}
case META_HW_CHIP_EFUSE_ID:
{
/* todo */
break;
}
case META_HW_CHIP_SERIAL:
{
/* todo */
break;
}
case META_HW_MODULE_TYPE:
{
/* todo */
break;
}
case META_HW_MODULE_VER:
{
break;
}
default:
{
iot_cus_printf("type hw id is %d, error\n", id);
break;
}
}
return ret;
}
/**
* @brief meta_handle_id_peripheral() - handle peripheral type cmd
* @param id:point to the cmd id
* @param data:point to the meta data
* @param op:operationquery or response
* @return: ERR_OK, or error reason
*/
static uint8_t meta_handle_id_peripheral(uint16_t id, uint8_t *data, uint8_t op)
{
uint8_t ret = ERR_OK;
switch (id) {
case META_PERIPHERAL_GPIO_INFO:
{
/* todo */
break;
}
case META_PERIPHERAL_ADC_INFO:
{
/* todo */
break;
}
case META_PERIPHERAL_PWM_INFO:
{
/* todo */
break;
}
case META_PERIPHERA_UART_INFO:
{
/* todo */
break;
}
default:
{
iot_cus_printf("type peripheral id is %d, error\n", id);
break;
}
}
return ret;
}
/**
* @brief meta_handle_id_upgrade() - handle upgrade type cmd
* @param id:point to the cmd id
* @param data:point to the meta data
* @param op:operationquery or response
* @return: ERR_OK, or error reason
*/
static uint8_t meta_handle_id_upgrade(uint16_t id, uint8_t *data, uint8_t op)
{
uint8_t ret = ERR_OK;
switch (id) {
case META_UPGRADE_STATE:
{
break;
}
case META_UPGRADE_TYPE:
{
break;
}
case META_UPGRADE_TIME:
{
break;
}
case META_UPGRADE_FW_VER:
{
break;
}
case META_UPGRADE_CUR_RECV_INFO:
{
break;
}
default:
{
iot_cus_printf("type upgrade id is %d, error\n", id);
break;
}
}
return ret;
}
/* 定时轮询meta命令直接打包进meta buffer这里数据都是同步获取 */
static void iot_meta_update_timer_handle(timer_id_t timer_id, void * arg)
{
static uint16_t cmd_idx = 0;
if (cmd_idx >= IOT_ARRAY_CNT(meta_period_update_tbl)) {
cmd_idx = 0;
}
iot_cus_printf("meta timer id_group = %d, id = %d\n",
meta_period_update_tbl[cmd_idx].id_group,
meta_period_update_tbl[cmd_idx].id);
meta_period_update_tbl[cmd_idx].handle();
cmd_idx++;
os_start_timer(g_meta_obj.meta_update_timer, META_UPDATE_INTERVAL);
}
/* 这函数打包所有query命令到buffer */
uint8_t meta_make_query_cmd(uint16_t id_group, uint16_t id)
{
uint8_t ret = ERR_OK;
iot_pkt_t *p_pkt;
meta_cmd_t *cmd;
p_pkt = iot_pkt_alloc(MIN_META_CMD_LEN, g_meta_obj.module_id);
if (p_pkt == NULL) {
ret = ERR_FAIL;
iot_cus_printf("%s pkt Malloc Failed!\n", __FUNCTION__);
goto out;
}
cmd = (meta_cmd_t *)iot_pkt_put(p_pkt, MIN_META_CMD_LEN);
cmd->id_group = id_group;
cmd->id = id;
cmd->op = OP_QUERY;
cmd->datalen = 0;
iot_meta_put_cmd_to_buf(cmd);
iot_pkt_free(p_pkt);
out:
return ret;
}
void iot_meta_cmd_handle(uint8_t *data, uint16_t data_len)
{
uint8_t *data_input = data;
meta_cmd_t *p_cmd;
iot_meta_bin_dump(data, data_len);
/* get all the tlv cmd */
while (data_len >= MIN_META_CMD_LEN) {
p_cmd = (meta_cmd_t *)data_input;
/* 发送方需要清理未使用的meta空间 */
if (p_cmd->id_group != META_INFO_TYPE_NULL) {
switch (p_cmd->id_group) {
case META_INFO_TYPE_VER:
{
meta_handle_id_ver(p_cmd->id, p_cmd->data, p_cmd->op);
break;
}
case META_INFO_TYPE_NETWORK:
{
meta_handle_id_network(p_cmd->id, p_cmd->data, p_cmd->op);
break;
}
case META_INFO_TYPE_APP:
{
meta_handle_id_app(p_cmd->id, p_cmd->data, p_cmd->op);
break;
}
case META_INFO_TYPE_HW:
{
meta_handle_id_hw(p_cmd->id, p_cmd->data, p_cmd->op);
break;
}
case META_INFO_TYPE_PERIPHERAL:
{
meta_handle_id_peripheral(p_cmd->id, p_cmd->data, p_cmd->op);
break;
}
case META_INFO_TYPE_UPGRAD:
{
meta_handle_id_upgrade(p_cmd->id, p_cmd->data, p_cmd->op);
break;
}
default:
{
iot_cus_printf("meta id group is error\n");
break;
}
}
if (data_len >= (MIN_META_CMD_LEN + p_cmd->datalen)) {
data_len -= (MIN_META_CMD_LEN + p_cmd->datalen);
data_input += (MIN_META_CMD_LEN + p_cmd->datalen);
} else {
break;
}
} else {
break;
}
}
return;
}
uint8_t meta_rcd_app_msdu_info(iot_plc_msdu_recv_t *msdu, uint8_t conn_type)
{
meta_recv_msdu_info_t *rcd_msdu_info;
g_meta_obj.msdu_rcd.rcd_index++;
if (g_meta_obj.msdu_rcd.rcd_index >= META_MSDU_RECD_CNT) {
g_meta_obj.msdu_rcd.rcd_index = 0;
}
rcd_msdu_info =
&g_meta_obj.msdu_rcd.msdu_info[g_meta_obj.msdu_rcd.rcd_index];
iot_cus_printf("recd_msdu_info %d %02X%02X%02X%02X%02X%02X len=%d\n",
g_meta_obj.msdu_rcd.rcd_index,
msdu->src[0], msdu->src[1], msdu->src[2],
msdu->src[3], msdu->src[4], msdu->src[5], msdu->len);
iot_mac_addr_cpy(rcd_msdu_info->mac, msdu->src);
rcd_msdu_info->len = msdu->len;
rcd_msdu_info->snr = msdu->snr;
rcd_msdu_info->conn_type = conn_type;
rcd_msdu_info->tm = os_boot_time32();
return g_meta_obj.msdu_rcd.rcd_index;
}
void iot_meta_info_init(uint8_t app_id, uint16_t module_id)
{
os_mem_set(&g_meta_obj, 0, sizeof(meta_context_t));
if (iot_plc_is_client_mode()) {
g_meta_obj.role = IOT_PLC_DEV_ROLE_STA;
} else {
g_meta_obj.role = IOT_PLC_DEV_ROLE_CCO;
}
g_meta_obj.app_id = app_id;
g_meta_obj.module_id = module_id;
g_meta_obj.meta_update_timer = os_create_timer(g_meta_obj.module_id, false,
iot_meta_update_timer_handle, NULL);
os_start_timer(g_meta_obj.meta_update_timer, META_UPDATE_INTERVAL);
}