Files
kunlun/app/smart_grid/sta/iot_sg_sta_topo.c
2024-09-28 14:24:04 +08:00

517 lines
17 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.
****************************************************************************/
/* os_shim header files */
#include "os_types_api.h"
#include "os_utils_api.h"
/* iot common header files */
#include "iot_io_api.h"
#include "iot_bitmap_api.h"
/* smart grid internal header files */
#include "iot_sg_fr.h"
#include "iot_sg.h"
#include "proto_645_vendor.h"
#include "proto_645_topo.h"
#include "proto_hw_tsfm.h"
#include "iot_sg_sta_topo.h"
#include "iot_sg_cfg.h"
/* define interval of tsfm topo repeat, uint is 1s. */
#define IOT_SG_STA_TSFM_TOPO_REPEAT_INTERVAL 2 * 60
#define IOT_SG_STA_TSFM_SN_IS_VALID(__sn) \
((__sn) >= IOT_SG_STA_TSFM_SN_FIRST && (__sn) <= IOT_SG_STA_TSFM_SN_LAST)
/* topo sn to bitmap index conversion */
#define IOT_SG_STA_TSFM_SN_TO_BM(sn) (sn + 1)
/* bitmap index to topo sn */
#define IOT_SG_STA_TSFM_BM_TO_SN(bm) ((uint16_t)((bm) - 1))
#if (IOT_SMART_GRID_BRANCH_DETECT_ENABLE && PLC_SUPPORT_STA_ROLE)
void iot_sg_sta_save_tsfm_topo_info(iot_sg_sta_tsfm_topo_info_t *tsfm_info)
{
iot_sg_sta_hw_tsfm_t *hw_tsfm = &p_sg_glb->desc.sta->hw_tsfm_info;
iot_sg_sta_topo_info_t *hw_topo = &hw_tsfm->tsfm_topo_info;
uint16_t energy, energy_valid;
uint8_t *phase_map = NULL;
uint16_t len = sizeof(hw_topo->sn_bm->map);
uint32_t bm_index;
energy = tsfm_info->energy ? tsfm_info->energy : 1;
iot_sg_printf("rec tsfm_topo sn:%lu phase %lu\n", tsfm_info->sn,
tsfm_info->phase);
switch (tsfm_info->phase) {
case IOT_PLC_PHASE_A:
{
hw_tsfm->topo_phase_rec |= 0x01;
phase_map = hw_topo->sn_bm->a_map;
hw_tsfm->topo_energy_rec[0] = energy;
break;
}
case IOT_PLC_PHASE_B:
{
hw_tsfm->topo_phase_rec |= 0x02;
phase_map = hw_topo->sn_bm->b_map;
hw_tsfm->topo_energy_rec[1] = energy;
break;
}
case IOT_PLC_PHASE_C:
{
hw_tsfm->topo_phase_rec |= 0x04;
phase_map = hw_topo->sn_bm->c_map;
hw_tsfm->topo_energy_rec[2] = energy;
break;
}
default:
break;
}
if (!IOT_SG_STA_TSFM_SN_IS_VALID(tsfm_info->sn)
|| hw_topo->sn_bm == NULL) {
goto drop;
}
bm_index = IOT_SG_STA_TSFM_SN_TO_BM(tsfm_info->sn);
energy_valid = energy != IOT_PLC_HW_REC_INVALID_ENERGY ? 1 : 0;
if (phase_map) {
if (!iot_bitmap_is_set(phase_map, len, bm_index)) {
iot_bitmap_set(phase_map, len, bm_index);
if (energy_valid) {
hw_topo->sn_bm->energy_cnt[tsfm_info->phase]++;
}
}
}
if (!iot_bitmap_is_set(hw_topo->sn_bm->map, len, bm_index)) {
iot_bitmap_set(hw_topo->sn_bm->map, len, bm_index);
if (energy_valid) {
hw_topo->sn_bm->energy_cnt[IOT_PLC_PHASE_ALL]++;
}
}
if (!hw_topo->sn_bm->table[tsfm_info->sn - 1]) {
hw_topo->sn_bm->table[tsfm_info->sn - 1] = energy;
} else if (tsfm_info->energy > hw_topo->sn_bm->table[tsfm_info->sn - 1]) {
hw_topo->sn_bm->table[tsfm_info->sn - 1] = tsfm_info->energy;
}
return;
drop:
iot_sg_printf("%s err\n", __FUNCTION__);
}
void iot_sg_sta_clear_tsfm_topo_info(void)
{
iot_sg_sta_global_t *sta_glb = p_sg_glb->desc.sta;
iot_sg_sta_hw_tsfm_t *hw_tsfm = &sta_glb->hw_tsfm_info;
iot_sg_sta_topo_info_t *hw_topo = &hw_tsfm->tsfm_topo_info;
hw_tsfm->topo_phase_rec = 0;
os_mem_set(hw_tsfm->topo_energy_rec, 0, sizeof(hw_tsfm->topo_energy_rec));
hw_topo->repeat_interval = 0;
hw_topo->repeat_cnt = 0;
hw_topo->send_mode = IOT_PLC_HW_TSFM_SEND_MODE_INVALID;
if (hw_topo->sn_bm) {
os_mem_set(hw_topo->sn_bm, 0x0, sizeof(iot_sg_sta_tsfm_topo_bm_t));
}
os_mem_set(&hw_topo->branch1, 0, sizeof(hw_topo->branch1));
os_mem_set(&hw_topo->branch2, 0, sizeof(hw_topo->branch2));
}
void iot_sg_sta_tsfm_topo_bitmap_dump(void)
{
uint8_t phase_type, *map;
uint16_t len, cnt = 0;
uint32_t first_index, last_index, i;
uint16_t sn_start, sn_end;
iot_sg_sta_hw_tsfm_t *hw_tsfm = &p_sg_glb->desc.sta->hw_tsfm_info;
iot_sg_sta_topo_info_t *hw_topo = &hw_tsfm->tsfm_topo_info;
iot_sg_sta_tsfm_topo_bm_t *local_bm = hw_topo->sn_bm;
if (local_bm == NULL) {
return;
}
for (phase_type = PROTO_645_2007_EXT_BRM_PHASE_ALL;
phase_type <= PROTO_645_2007_EXT_BRM_PHASE_C; phase_type++) {
switch (phase_type) {
case PROTO_645_2007_EXT_BRM_PHASE_ALL:
map = local_bm->map;
len = sizeof(local_bm->map);
break;
case PROTO_645_2007_EXT_BRM_PHASE_A:
map = local_bm->a_map;
len = sizeof(local_bm->a_map);
break;
case PROTO_645_2007_EXT_BRM_PHASE_B:
map = local_bm->b_map;
len = sizeof(local_bm->b_map);
break;
case PROTO_645_2007_EXT_BRM_PHASE_C:
map = local_bm->c_map;
len = sizeof(local_bm->c_map);
break;
default:
continue;
}
first_index = iot_bitmap_ffs(map, len);
last_index = iot_bitmap_fls(map, len);
if (!first_index) {
iot_sg_printf("%s phase %lu no rec topo sn\n", __FUNCTION__,
phase_type);
continue;
}
sn_start = IOT_SG_STA_TSFM_BM_TO_SN(first_index);
sn_end = IOT_SG_STA_TSFM_BM_TO_SN(last_index);
for (i = first_index; i <= last_index; i++) {
if (iot_bitmap_is_set(map, len, i)) {
cnt++;
}
}
iot_sg_printf("%s phase %lu cnt %lu first_sn %lu sn_end %lu\n",
__FUNCTION__, phase_type, cnt, sn_start, sn_end);
}
}
uint32_t iot_sg_sta_get_tsfm_topo_bitmap_info(
iot_sg_sta_tsfm_topo_desc_t *bitmap, uint8_t phase_type,
uint8_t block_seq)
{
uint8_t reason, *map;
uint16_t len, uint_cnt, energy_cnt_last_block;
uint32_t cnt;
uint32_t first_index;
uint16_t sn_start, sn_end, length_max;
iot_sg_sta_hw_tsfm_t *hw_tsfm = &p_sg_glb->desc.sta->hw_tsfm_info;
iot_sg_sta_topo_info_t *hw_topo = &hw_tsfm->tsfm_topo_info;
iot_sg_sta_tsfm_topo_bm_t *local_bm = hw_topo->sn_bm;
uint16_t *energy_cnt;
if (local_bm == NULL) {
reason = 1;
goto drop;
}
switch (phase_type) {
case PROTO_645_2007_EXT_BRM_PHASE_ALL:
map = local_bm->map;
len = sizeof(local_bm->map);
break;
case PROTO_645_2007_EXT_BRM_PHASE_A:
map = local_bm->a_map;
len = sizeof(local_bm->a_map);
break;
case PROTO_645_2007_EXT_BRM_PHASE_B:
map = local_bm->b_map;
len = sizeof(local_bm->b_map);
break;
case PROTO_645_2007_EXT_BRM_PHASE_C:
map = local_bm->c_map;
len = sizeof(local_bm->c_map);
break;
default:
reason = 2;
goto drop;
}
energy_cnt = &local_bm->energy_cnt[phase_type];
first_index = iot_bitmap_ffs(map, len);
if (!first_index) {
reason = 3;
goto drop;
}
if (*energy_cnt == 0) {
bitmap->block_cnt = 1;
bitmap->curr_seq = 0;
bitmap->first_sn = IOT_SG_STA_TSFM_BM_TO_SN(first_index);
bitmap->first_sn = (bitmap->first_sn >> 3) << 3;
sn_start = bitmap->first_sn;
sn_end = IOT_SG_STA_TSFM_BM_TO_SN(iot_bitmap_fls(map, len));
bitmap->length = ((sn_end - sn_start) >> 3) + 1;
length_max = IOT_SG_STA_TSFM_TOPO_SN_BM_LEN - (sn_start >> 3);
if (bitmap->length > length_max) {
bitmap->length = length_max;
}
bitmap->energy_idx = 0;
bitmap->energy_cnt = 0;
} else {
/* if the branch identification information with effective energy is
* received on the current phase, the energy format is used to report
* all the results.
*/
cnt = iot_bitmap_cbs(map, len);
len = PROTO_645_MAX_DATA_LEN - sizeof(proto_645_ext_br_rsp_data_t) -
sizeof(proto_645_ext_br_energy_rsp_t);
uint_cnt = len / sizeof(proto_645_ext_br_energy_unit_t);
energy_cnt_last_block = uint_cnt;
bitmap->block_cnt = (uint8_t)(cnt / uint_cnt);
if (cnt % uint_cnt) {
energy_cnt_last_block = cnt % uint_cnt;
bitmap->block_cnt++;
}
bitmap->curr_seq = min(block_seq, bitmap->block_cnt - 1);
bitmap->first_sn = 0;
bitmap->length = 0;
bitmap->energy_idx = (bitmap->curr_seq) * uint_cnt;
if (bitmap->curr_seq == bitmap->block_cnt - 1)
bitmap->energy_cnt = energy_cnt_last_block;
else
bitmap->energy_cnt = uint_cnt;
}
return ERR_OK;
drop:
iot_sg_printf("%s err %lu\n", __FUNCTION__, reason);
return ERR_FAIL;
}
void iot_sg_sta_tsfm_topo_fill_energy_info(uint8_t *data,
uint8_t phase, uint16_t start_index, uint16_t fill_cnt)
{
uint16_t idx;
iot_sg_sta_hw_tsfm_t *hw_tsfm = &p_sg_glb->desc.sta->hw_tsfm_info;
iot_sg_sta_topo_info_t *hw_topo = &hw_tsfm->tsfm_topo_info;
iot_sg_sta_tsfm_topo_bm_t *local_bm = hw_topo->sn_bm;
proto_645_ext_br_energy_unit_t *table;
uint8_t *map = NULL;
uint16_t len = 0, cnt = 0, i = 0;
switch (phase) {
case PROTO_645_2007_EXT_BRM_PHASE_ALL:
map = local_bm->map;
len = sizeof(local_bm->map);
break;
case PROTO_645_2007_EXT_BRM_PHASE_A:
map = local_bm->a_map;
len = sizeof(local_bm->a_map);
break;
case PROTO_645_2007_EXT_BRM_PHASE_B:
map = local_bm->b_map;
len = sizeof(local_bm->b_map);
break;
case PROTO_645_2007_EXT_BRM_PHASE_C:
map = local_bm->c_map;
len = sizeof(local_bm->c_map);
break;
default:
IOT_ASSERT(0);
break;
}
table = (proto_645_ext_br_energy_unit_t*)data;
for (i = 0, idx = 0; i < len * 8; i++) {
if (!iot_bitmap_is_set(map, len, i + 1))
continue;
if (idx >= start_index) {
table[cnt].sn = i;
table[cnt].energy = local_bm->table[table[cnt].sn - 1];
cnt++;
if (cnt >= fill_cnt) {
break;
}
}
idx++;
}
}
void iot_sg_sta_tsfm_topo_fill_bitmap_info(
uint8_t *map, iot_sg_sta_tsfm_topo_desc_t *bitmap_info,
uint8_t phase_type)
{
uint8_t *local_map = NULL;
uint16_t len = 0, idx;
iot_sg_sta_hw_tsfm_t *hw_tsfm = &p_sg_glb->desc.sta->hw_tsfm_info;
iot_sg_sta_topo_info_t *hw_topo = &hw_tsfm->tsfm_topo_info;
iot_sg_sta_tsfm_topo_bm_t *local_bm = hw_topo->sn_bm;
if (local_bm == NULL) {
goto out;
}
switch (phase_type) {
case PROTO_645_2007_EXT_BRM_PHASE_ALL:
local_map = local_bm->map;
len = sizeof(local_bm->map);
break;
case PROTO_645_2007_EXT_BRM_PHASE_A:
local_map = local_bm->a_map;
len = sizeof(local_bm->a_map);
break;
case PROTO_645_2007_EXT_BRM_PHASE_B:
local_map = local_bm->b_map;
len = sizeof(local_bm->b_map);
break;
case PROTO_645_2007_EXT_BRM_PHASE_C:
local_map = local_bm->c_map;
len = sizeof(local_bm->c_map);
break;
default:
break;
}
if (!map || !bitmap_info || !local_map) {
goto out;
}
if (!iot_bitmap_ffs(local_map, len)
|| (bitmap_info->first_sn > IOT_SG_STA_TSFM_SN_LAST)
|| !bitmap_info->length) {
goto out;
}
idx = bitmap_info->first_sn >> 3;
os_mem_cpy(map, &local_map[idx], bitmap_info->length);
out:
return;
}
void iot_sg_sta_hw_topo_init(void)
{
iot_sg_sta_global_t *sta_glb = p_sg_glb->desc.sta;
iot_sg_sta_hw_tsfm_t *hw_tsfm = &sta_glb->hw_tsfm_info;
iot_sg_sta_topo_info_t *hw_topo = &hw_tsfm->tsfm_topo_info;
os_mem_set(hw_topo, 0x0, sizeof(*hw_topo));
#if IOT_BRM_ENABLE || IOT_BSRM_MODE
hw_topo->sn_bm = os_mem_malloc(IOT_SMART_GRID_MID,
sizeof(iot_sg_sta_tsfm_topo_bm_t));
#else
iot_sg_printf("%s no supp rec\n", __FUNCTION__);
hw_topo->sn_bm = NULL;
#endif
}
void iot_sg_sta_hw_topo_deinit(void)
{
iot_sg_sta_hw_tsfm_t *hw_tsfm = &p_sg_glb->desc.sta->hw_tsfm_info;
iot_sg_sta_topo_info_t *hw_topo = &hw_tsfm->tsfm_topo_info;
if (hw_topo->sn_bm) {
os_mem_free(hw_topo->sn_bm);
}
os_mem_set(hw_topo, 0x0, sizeof(*hw_topo));
}
void iot_sg_sta_tsfm_topo_repeat(void)
{
uint8_t data[PROTO_645_VENDOR_BR_DATA_LEN], len;
uint32_t ret;
hw_tsfm_send_cfg_t cfg = { 0 };
proto_hw_tsfm_hdr_t *topo_hdr;
proto_hw_tsfm_topo_sn_t *topo_data;
iot_sg_sta_hw_tsfm_t *hw_tsfm = &p_sg_glb->desc.sta->hw_tsfm_info;
iot_sg_sta_topo_info_t *hw_topo = &hw_tsfm->tsfm_topo_info;
if ((hw_topo->send_mode == IOT_PLC_HW_TSFM_SEND_MODE_ZC
|| hw_topo->send_mode == IOT_PLC_HW_TSFM_SEND_MODE_LOAD)
&& hw_topo->repeat_cnt) {
if (hw_topo->repeat_interval) {
hw_topo->repeat_interval--;
if (hw_topo->repeat_interval) {
return;
}
cfg.phase = IOT_PLC_PHASE_A;
cfg.mod_mode = hw_topo->send_mode;
if (hw_topo->send_mode == IOT_PLC_HW_TSFM_SEND_MODE_LOAD) {
topo_data = (proto_hw_tsfm_topo_sn_t*)data;
topo_data->sn = hw_topo->branch1.sn;
len = sizeof(*topo_data);
cfg.para.load.encode = hw_topo->branch2.encode;
cfg.para.load.pulse_high_dur = hw_topo->branch2.pulse_high_dur;
cfg.para.load.pulse_low_dur = hw_topo->branch2.pulse_low_dur;
cfg.para.load.chara_bit_dur = hw_topo->branch2.chara_bit_dur;
} else {
topo_hdr = (proto_hw_tsfm_hdr_t*)data;
topo_hdr->data_id = PROTO_HW_TSFM_ID_TOPO_SN;
topo_hdr->data_len = sizeof(*topo_data);
topo_data = (proto_hw_tsfm_topo_sn_t*)topo_hdr->data;
topo_data->sn = hw_topo->branch1.sn;
len = PROTO_645_VENDOR_BR_DATA_LEN;
cfg.para.zc.ahead = hw_topo->branch1.ahead;
cfg.para.zc.dur_output = hw_topo->branch1.dur_output;
}
ret = proto_645_vendor_br_launch(data, len, &cfg);
if (ret != ERR_BUSY) {
iot_sg_printf("%s ahead %lu dur_output %lu sn %lu ret %lu "
"repeat cnt %lu\n", __FUNCTION__, hw_topo->branch1.ahead,
hw_topo->branch1.dur_output,
topo_data->sn, ret, hw_topo->repeat_cnt);
hw_topo->repeat_cnt--;
if (ret == ERR_NOSUPP) {
hw_topo->repeat_cnt = 0;
hw_topo->repeat_interval = 0;
}
}
} else {
ret = iot_plc_hw_tsfm_get_transmitter_state();
if (ret == ERR_OK) {
hw_topo->repeat_interval = IOT_SG_STA_TSFM_TOPO_REPEAT_INTERVAL;
} else if (ret == ERR_NOSUPP) {
hw_topo->repeat_cnt = 0;
hw_topo->repeat_interval = 0;
}
}
}
}
#else /* IOT_SMART_GRID_BRANCH_DETECT_ENABLE && PLC_SUPPORT_STA_ROLE */
void iot_sg_sta_save_tsfm_topo_info(iot_sg_sta_tsfm_topo_info_t *tsfm_info)
{
(void)tsfm_info;
}
void iot_sg_sta_clear_tsfm_topo_info(void)
{
}
void iot_sg_sta_tsfm_topo_bitmap_dump(void)
{
}
uint32_t iot_sg_sta_get_tsfm_topo_bitmap_info(
iot_sg_sta_tsfm_topo_desc_t *bitmap, uint8_t phase_type,
uint8_t block_seq)
{
(void)bitmap;
(void)phase_type;
(void)block_seq;
return ERR_NOSUPP;
}
void iot_sg_sta_tsfm_topo_fill_energy_info(uint8_t *data,
uint8_t phase, uint16_t start_index, uint16_t fill_cnt)
{
(void)data;
(void)phase;
(void)start_index;
(void)fill_cnt;
}
void iot_sg_sta_tsfm_topo_fill_bitmap_info(
uint8_t *map, iot_sg_sta_tsfm_topo_desc_t *bitmap_info,
uint8_t phase_type)
{
(void)map;
(void)bitmap_info;
(void)phase_type;
}
void iot_sg_sta_hw_topo_init(void)
{
}
void iot_sg_sta_hw_topo_deinit(void)
{
}
void iot_sg_sta_tsfm_topo_repeat(void)
{
}
#endif /* IOT_SMART_GRID_BRANCH_DETECT_ENABLE && PLC_SUPPORT_STA_ROLE */