Files
kunlun/cli/host_interface/plc/iot_cli_ckb.c
2024-09-28 14:24:04 +08:00

224 lines
6.0 KiB
C

/****************************************************************************
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_cli.h"
#include "iot_cli_host_interface.h"
#include "iot_cli_plc_module.h"
#include "iot_cli_ckb.h"
extern iot_cli_host_info_t *host_info;
extern iot_plc_host_config_t *host_config;
uint8_t is_local_mac(uint8_t* mac)
{
//check if mac is local or not
uint8_t is_local = 1;
is_local = ((!mac) || MAC_IS_EMPTY(mac) ||
iot_mac_addr_cmp(mac, host_config->mac_addr));
return is_local;
}
#if IOT_CLI_CKB_ADDR_MAPPING
void remove_addr_from_mapping_table(uint8_t* mac)
{
if (host_config->cmd_mapping_table == NULL) {
return;
}
os_acquire_mutex(host_config->m_mapping_table_lock);
for (uint8_t i = 1; i < CMD_MAPPING_TABLE_CNT; i++) {
if (host_config->cmd_mapping_table[i].is_used &&
iot_mac_addr_cmp(mac,
host_config->cmd_mapping_table[i].mac)) {
host_config->cmd_mapping_table[i].is_used = 0;
break;
}
}
os_release_mutex(host_config->m_mapping_table_lock);
}
//return 0 mean cco, otherwise return index
uint8_t add_addr_to_mapping_table(uint8_t* mac)
{
uint8_t i = 1;
if ((host_config->cmd_mapping_table == NULL) ||
(mac == NULL) || is_local_mac(mac)) {
return 0;
}
os_acquire_mutex(host_config->m_mapping_table_lock);
// find mac in cmd mapping table
for (i = 1; i < CMD_MAPPING_TABLE_CNT; i++) {
if (host_config->cmd_mapping_table[i].is_used) {
if (iot_mac_addr_cmp(mac,
host_config->cmd_mapping_table[i].mac)) {
os_release_mutex(
host_config->m_mapping_table_lock);
return i;
}
}
}
for (i = 1; i < CMD_MAPPING_TABLE_CNT; i++) {
if (!host_config->cmd_mapping_table[i].is_used) {
break;
}
}
if (i < CMD_MAPPING_TABLE_CNT) {
iot_mac_addr_cpy(
host_config->cmd_mapping_table[i].mac, mac);
host_config->cmd_mapping_table[i].is_used = 1;
if ((i + 1) < CMD_MAPPING_TABLE_CNT) {
host_config->cmd_mapping_table[i + 1].is_used = 0;
}
} else {
i = 1;
iot_mac_addr_cpy(
host_config->cmd_mapping_table[i].mac, mac);
host_config->cmd_mapping_table[i].is_used = 1;
if ((i + 1) < CMD_MAPPING_TABLE_CNT) {
host_config->cmd_mapping_table[i + 1].is_used = 0;
}
}
os_release_mutex(host_config->m_mapping_table_lock);
iot_printf("add_addr_to_mapping_table"
" fail count(%d)\n", CMD_MAPPING_TABLE_CNT);
return i;
}
uint8_t *get_first_ckb_addr_from_mapping_table()
{
uint8_t *addr = NULL;
if (host_config->cmd_mapping_table == NULL) {
return addr;
}
for (uint8_t i = 1; i < CMD_MAPPING_TABLE_CNT; i++) {
if (host_config->cmd_mapping_table[i].is_used) {
addr = host_config->cmd_mapping_table[i].mac;
break;
}
}
return addr;
}
uint8_t *get_addr_from_mapping_table(uint8_t index)
{
uint8_t *addr = NULL;
cli_app_cmd_mapping_item *cmd = NULL;
cmd = get_cmd_from_mapping_table(index);
if (cmd) {
addr = cmd->mac;
}
return addr;
}
cli_app_cmd_mapping_item *get_cmd_from_mapping_table(uint8_t index)
{
cli_app_cmd_mapping_item *p_cmd_item = NULL;
if ((host_config->cmd_mapping_table == NULL) ||
(index >= CMD_MAPPING_TABLE_CNT)) {
return NULL;
}
os_acquire_mutex(host_config->m_mapping_table_lock);
p_cmd_item = host_config->cmd_mapping_table + index;
os_release_mutex(host_config->m_mapping_table_lock);
return p_cmd_item;
}
void iot_cli_addr_mapping_init()
{
if (host_config->m_mapping_table_lock) {
return;
}
host_config->m_mapping_table_lock = os_create_mutex(IOT_CLI_MID);
host_config->cmd_mapping_table =
(cli_app_cmd_mapping_item*)os_mem_malloc(IOT_CLI_MID,
CMD_MAPPING_TABLE_CNT * sizeof(cli_app_cmd_mapping_item));
if (host_config->cmd_mapping_table) {
os_mem_set(host_config->cmd_mapping_table, 0,
CMD_MAPPING_TABLE_CNT * sizeof(cli_app_cmd_mapping_item));
// reserved for cco
host_config->cmd_mapping_table[0].is_used = 1;
}
}
void iot_cli_addr_mapping_deinit()
{
if (host_config->cmd_mapping_table) {
os_mem_free(host_config->cmd_mapping_table);
host_config->cmd_mapping_table = NULL;
}
if (host_config->m_mapping_table_lock) {
os_delete_mutex(host_config->m_mapping_table_lock);
host_config->m_mapping_table_lock = NULL;
}
}
#else
uint8_t add_addr_to_mapping_table(uint8_t* mac)
{
(void)mac;
return 0;
}
#endif /* IOT_CLI_CKB_ADDR_MAPPING */
#if PLC_SUPPORT_CCO_ROLE
void iot_cli_ckb_init()
{
iot_cli_addr_mapping_init();
}
void iot_cli_ckb_deinit()
{
iot_cli_addr_mapping_deinit();
}
#endif /* PLC_SUPPORT_CCO_ROLE */
#if (IOT_STA_CONTROL_MODE == IOT_STA_CONTROL_TYPE_STA)
void iot_cli_host_set_ckb_dev_type()
{
iot_plc_cfg_set_req_t cfg = { 0 };
iot_printf("sta control mode, set dev type to meter controller\n");
cfg.dev_type_valid = 1;
cfg.dev_type = IOT_PLC_DEV_TYPE_METER_CONTROLLER;
iot_plc_set_cfg(host_config->app_handle,
IOT_PLC_API_REQ_ID_DEFAULT, &cfg);
}
#endif