Files
kunlun/app/iot_cus_at_app/common/app_common.c
2024-09-28 14:24:04 +08:00

324 lines
8.8 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 "app_main.h"
#include "app_common.h"
uint8_t g_printf_enable = true;
uint32_t get_random_num32(uint32_t min, uint32_t max)
{
uint32_t random = 0;
uint32_t module = max + 1 - min;
if (module == 0) {
module = 0xffffffff;
}
random = os_rand();
return (uint32_t)((random % module) + min);
}
/* Set Carrier Communication Frequency Band */
void app_set_freq_band(uint8_t band_id)
{
app_entity_t *app_entry = NULL;
app_entry = app_get_main_entry();
APP_PRINTF("[INF] %s Set band to id:%d", __FUNCTION__, band_id);
iot_plc_set_freq_band(app_entry->app_hdl, IOT_PLC_API_REQ_ID_DEFAULT, band_id);
return;
}
/* Set scan band bitmap */
void app_set_scan_band_bitmap(uint8_t *band_bitmap)
{
app_entity_t *app_entry = NULL;
app_entry = app_get_main_entry();
APP_PRINT_BUF("[INF]Set scan band bitmap", band_bitmap, IOT_PLC_BAND_BITMAP_SIZE);
iot_plc_set_scan_band_bitmap(app_entry->app_hdl, IOT_PLC_API_REQ_ID_DEFAULT,
band_bitmap, IOT_PLC_BAND_BITMAP_SIZE);
return;
}
void app_get_user_info(app_user_info_t *user_info)
{
iot_pkt_t *pkt_buf;
uint8_t temp[2];
pkt_buf = iot_pkt_alloc(sizeof(iot_build_info_t), IOT_APP_DEMO_MID);
if (NULL == pkt_buf) {
APP_PRINTF("[ERR] %s Packet Alloc Failed !!", __FUNCTION__);
return;
}
iot_build_info_t *info = (iot_build_info_t *)iot_pkt_put(pkt_buf, sizeof(iot_build_info_t));
iot_version_get_user_build_info(info);
user_info->sw_ver = APP_VER2BCD(iot_version_micro(), APP_SW_VERSION_R, iot_version_build());
user_info->boot_ver = APP_BOOT_VER;
user_info->year = info->year;
user_info->month = info->month;
user_info->day = info->day;
temp[0] = (uint8_t)(MANU_CODE);
temp[1] = (uint8_t)(MANU_CODE>>8);
os_mem_cpy(user_info->manu_code,temp, sizeof(user_info->manu_code));
temp[0] = (uint8_t)(WQ_CHIP3011);
temp[1] = (uint8_t)(WQ_CHIP3011>>8);
os_mem_cpy(user_info->chip_code,temp,sizeof(user_info->chip_code));
iot_pkt_free(pkt_buf);
return;
}
uint8_t *app_get_cco_mac_addr(void)
{
app_entity_t *app_entry = NULL;
app_entry = app_get_main_entry();
return app_entry->dev.cco_addr;
}
uint8_t *app_get_mac_addr(void)
{
app_entity_t *app_entry = NULL;
app_entry = app_get_main_entry();
return app_entry->dev.mac_addr;
}
void app_set_mac_addr(uint8_t *mac)
{
iot_plc_cfg_set_req_t cfg;
app_entity_t *app_entry = NULL;
app_entry = app_get_main_entry();
APP_PRINTF("[INF] Set Device MAC: %02x-%02x-%02x-%02x-%02x-%02x",
mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
os_mem_set(&cfg, 0, sizeof(cfg));
cfg.addr_valid = 1;
cfg.addr_type = IOT_PLC_MAC_ADDR_TYPE_MODULE;
iot_mac_addr_cpy(cfg.addr, mac);
cfg.reset = 1;
iot_plc_set_cfg(app_entry->app_hdl, IOT_PLC_API_REQ_ID_DEFAULT, &cfg);
}
uint8_t app_get_online_state(void)
{
app_entity_t *app_entry = NULL;
app_entry = app_get_main_entry();
return app_entry->dev.dev_ready;
}
uint8_t app_get_log_enable(void)
{
return g_printf_enable;
}
void app_set_log_enable(uint8_t flag)
{
g_printf_enable = flag;
}
uint16_t app_timer_callback(timer_id_t tid, void *arg)
{
iot_task_msg_t *msg;
app_msg_t *task_msg;
app_entity_t *app_entry = NULL;
app_entry = app_get_main_entry();
msg = iot_task_alloc_msg_with_reserved(app_entry->msg_task.handle, 0);
if (NULL == msg) {
APP_PRINTF("[ERR] %s Alloc MSG Buffer Failed !!", __FUNCTION__);
return ERR_FAIL;
}
task_msg = (app_msg_t *)msg;
task_msg->msg.type = E_APP_MSG_TIMER;
task_msg->msg.id = (app_timer_id_e)arg;
task_msg->data = (void*)tid;
iot_task_queue_msg(app_entry->msg_task.handle, &task_msg->msg, APP_TASK_MSG_PRIO);
return ERR_OK;
}
timer_id_t app_timer_start(app_timer_id_e id, uint32_t period, uint8_t type)
{
timer_id_t tm_id = 0;
/* Create a timer */
tm_id = os_create_timer(IOT_APP_DEMO_MID, type,
(os_timer_func_t)app_timer_callback, (void *)id);
if (0 == tm_id) {
APP_PRINTF("[ERR] %s Create Timer %d Failed !!", __FUNCTION__, id);
return tm_id;
}
/* Start the timer */
os_start_timer(tm_id, period);
return tm_id;
}
uint8_t app_get_net_enable(void)
{
app_entity_t *app_entry = app_get_main_entry();
return app_entry->net_enable;
}
void app_set_net_enable(uint8_t flag)
{
app_entity_t *app_entry = app_get_main_entry();
flag = !!flag;
if (flag != app_entry->net_enable) {
app_entry->net_enable = flag;
if (flag) {
iot_plc_start_nw_fmt(app_entry->app_hdl, 0);
} else {
iot_plc_stop_nw_fmt(app_entry->app_hdl);
}
}
}
/* send data to dst_addr throw plc, add app_custom_data before data */
uint16_t app_plc_tx(uint8_t *data, uint16_t data_length, uint8_t *dst_addr,
uint16_t id, append_tx_info_t *tx_info)
{
iot_pkt_t *pkt;
uint8_t *frame;
uint16_t frame_length;
app_custom_data *custom_data;
append_tx_info_t def_tx_info = {0};
if (NULL == data || 0 == data_length) {
APP_PRINTF("[ERR] %s Frame is NULL !!", __FUNCTION__);
return ERR_FAIL;
}
if(!iot_mac_addr_valid(dst_addr)) {
APP_PRINTF("[ERR] invalid dst addr, data[%d] dropped !!", data_length);
return ERR_FAIL;
}
frame_length = data_length + sizeof(app_custom_data) + sizeof(append_tx_info_t);
pkt = iot_pkt_alloc(frame_length, IOT_APP_DEMO_MID);
if (NULL == pkt) {
APP_PRINTF("[ERR] %s Packet Alloc Failed !!", __FUNCTION__);
return ERR_FAIL;
}
frame = iot_pkt_put(pkt, frame_length);
if (tx_info) {
/* fill tx_info if trans not NULL, else use default 0 */
os_mem_cpy(frame, tx_info, sizeof(append_tx_info_t));
} else {
os_mem_cpy(frame, &def_tx_info, sizeof(append_tx_info_t));
}
frame += sizeof(append_tx_info_t);
custom_data = (app_custom_data*)frame;
custom_data->id = id;
iot_mac_addr_cpy(custom_data->mac, dst_addr);
frame += sizeof(app_custom_data);
os_mem_cpy(frame, data, data_length);
APP_PRINT_BUF("[PLC_TX]", iot_pkt_data(pkt), iot_pkt_data_len(pkt));
APP_PRINTF("pkt id[%x] will be sent to "MAC_FMT, custom_data->id,
MAC_ARG(custom_data->mac));
if (ERR_OK != app_post_msg(E_APP_MSG_PROCESS, E_CMD_ID_SEND_DATA_TO_PLC,
(void*)pkt)) {
APP_PRINTF("[ERR] %s Post MSG Failed !!", __FUNCTION__);
iot_pkt_free(pkt);
return ERR_FAIL;
}
return ERR_OK;
}
#if PLC_SUPPORT_CCO_ROLE
void app_ntb_to_us(uint8_t *data)
{
uint8_t *pdata = data;
uint8_t i;
uint32_t timebuf;
uint32_t timebuf_next;
uint8_t overflow_sign = false;
/* current_type is not smaller than overflow_index means value is
* overflow, the value should add a (0xFFFFFFFF / 25)
*/
for (i = CCO_TX_TIME; i <= CCO_RX_TIME; i++) {
pdata = data + sizeof(uint32_t) * i;
timebuf =iot_bytes_to_uint32(pdata, 0);
if ((overflow_sign == false) && (i < CCO_RX_TIME)) {
timebuf_next = iot_bytes_to_uint32(pdata + sizeof(uint32_t), 0);
if (timebuf > timebuf_next) {
overflow_sign = true;
}
}
if (overflow_sign) {
timebuf = (timebuf + (0xFFFFFFFF % 25)) / 25 + 0xFFFFFFFF / 25;
} else {
timebuf /= 25;
}
iot_uint32_to_bytes(timebuf, pdata, 0);
}
}
#endif /* PLC_SUPPORT_CCO_ROLE */
void delay_timer_get(uint8_t *data, uint8_t time_type, uint32_t set_time)
{
uint32_t cur_time ;
app_entity_t *app_entry = NULL;
app_entry = app_get_main_entry();
if (set_time == 0) {
cur_time = iot_plc_get_ntb(app_entry->app_hdl);
} else {
cur_time = set_time;
}
if (time_type > CCO_RX_TIME) {
APP_PRINTF("[ERR] %s get type error", __FUNCTION__);
return;
}
os_mem_cpy(data + sizeof(uint32_t) * time_type , &cur_time, sizeof(uint32_t));
APP_PRINT_BUF("now timer is :", data, sizeof(delay_time_test));
}