208 lines
5.6 KiB
C
208 lines
5.6 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_task.h"
|
|
#include "app_common.h"
|
|
|
|
/* log print enable: true or false */
|
|
static uint8_t g_printf_enbale = true;
|
|
|
|
uint32_t app_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);
|
|
}
|
|
|
|
void app_set_freq_band(uint8_t band_id)
|
|
{
|
|
app_entity_t *app_entry = NULL;
|
|
|
|
app_entry = iot_main_get_app_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;
|
|
}
|
|
|
|
void app_set_scan_band_bitmap(uint8_t *band_bitmap)
|
|
{
|
|
app_entity_t *app_entry = NULL;
|
|
|
|
app_entry = iot_main_get_app_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;
|
|
}
|
|
|
|
uint8_t *app_get_cco_mac_addr(void)
|
|
{
|
|
app_entity_t *app_entry = NULL;
|
|
|
|
app_entry = iot_main_get_app_entry();
|
|
|
|
return app_entry->dev.cco_addr;
|
|
}
|
|
|
|
uint8_t *app_get_mac_addr(void)
|
|
{
|
|
app_entity_t *app_entry = NULL;
|
|
|
|
app_entry = iot_main_get_app_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 = iot_main_get_app_entry();
|
|
|
|
APP_PRINTF("[INF] Set Device MAC: "MAC_FMT, MAC_ARG(mac));
|
|
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_log_enable(void)
|
|
{
|
|
return g_printf_enbale;
|
|
}
|
|
|
|
void app_set_log_enable(uint8_t flag)
|
|
{
|
|
g_printf_enbale = !!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 = iot_main_get_app_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_MAIN_MSG_FROM_TIMER;
|
|
task_msg->msg.id = (app_timer_id_e)arg;
|
|
task_msg->data = NULL;
|
|
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_DL645_MID, type,
|
|
(os_timer_func_t)app_timer_callback, (void *)id);
|
|
if (0 == tm_id) {
|
|
APP_PRINTF("[ERR] %s Create Timer Failed !!", __FUNCTION__);
|
|
return tm_id;
|
|
}
|
|
|
|
/* Start the timer */
|
|
os_start_timer(tm_id, period);
|
|
|
|
return tm_id;
|
|
}
|
|
|
|
uint32_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);
|
|
if (NULL == (pkt = iot_pkt_alloc(frame_length, IOT_APP_DEMO_MID))) {
|
|
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);
|
|
iot_mac_addr_reverse(custom_data->mac);
|
|
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 != iot_main_task_msg_post(E_MAIN_MSG_FROM_MAINTASK,
|
|
E_MAIN_MSG_ID_645PKT_TO_PLC, (void*)pkt)) {
|
|
APP_PRINTF("[ERR] %s Post MSG Failed !!", __FUNCTION__);
|
|
iot_pkt_free(pkt);
|
|
return ERR_FAIL;
|
|
}
|
|
|
|
return ERR_OK;
|
|
}
|