198 lines
		
	
	
		
			5.4 KiB
		
	
	
	
		
			C
		
	
	
	
	
	
			
		
		
	
	
			198 lines
		
	
	
		
			5.4 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_uart.h"
 | 
						|
#include "app_main_task.h"
 | 
						|
#include "app_common.h"
 | 
						|
#include "app_config.h"
 | 
						|
#include "iot_bitmap_api.h"
 | 
						|
#include "iot_app_pib_api.h"
 | 
						|
#include "iot_app_pib_sta_api.h"
 | 
						|
#include "iot_app_pib_cco_api.h"
 | 
						|
#include "iot_plc_sync_api.h"
 | 
						|
 | 
						|
app_hw_ver_e app_get_hw_ver(void)
 | 
						|
{
 | 
						|
    app_entity_t *app_entry = iot_main_get_app_entry();
 | 
						|
 | 
						|
    return (app_hw_ver_e)app_entry->app_cfg.factory.hw_ver;
 | 
						|
}
 | 
						|
 | 
						|
uint16_t app_get_factory_from_nv(nv_ftm_factory_id *param)
 | 
						|
{
 | 
						|
    uint16_t ret;
 | 
						|
    uint32_t offset;
 | 
						|
    nv_ftm_factory_id nv_value = {0};
 | 
						|
 | 
						|
    os_mem_set(&nv_value, 0, sizeof(nv_value));
 | 
						|
    offset = NV_FTM_FACTORY_ID * APP_NV_FTM_SIZE;
 | 
						|
 | 
						|
    ret = app_flash_read(offset, (uint8_t*)&nv_value, sizeof(nv_value));
 | 
						|
    if (ret != ERR_OK) {
 | 
						|
        APP_PRINTF("[ERR] %s Read Factory Failed !!", __FUNCTION__);
 | 
						|
        return ret;
 | 
						|
    }
 | 
						|
 | 
						|
    os_mem_cpy(param, &nv_value, sizeof(nv_ftm_factory_id));
 | 
						|
 | 
						|
    return ERR_OK;
 | 
						|
}
 | 
						|
 | 
						|
uint16_t app_save_factory_to_nv(void)
 | 
						|
{
 | 
						|
    nv_ftm_factory_id nv_value = {0};
 | 
						|
    uint16_t ret;
 | 
						|
    uint32_t offset;
 | 
						|
    app_entity_t *app_entry = iot_main_get_app_entry();
 | 
						|
 | 
						|
    os_mem_cpy(&nv_value, &app_entry->app_cfg.factory,
 | 
						|
        sizeof(nv_ftm_factory_id));
 | 
						|
 | 
						|
    offset = NV_FTM_FACTORY_ID * APP_NV_FTM_SIZE;
 | 
						|
    ret = app_flash_write(offset, (uint8_t*)&nv_value, sizeof(nv_value));
 | 
						|
    if (ret != ERR_OK) {
 | 
						|
        APP_PRINTF("[ERR] %s Write Factory Failed !!", __FUNCTION__);
 | 
						|
        return ret;
 | 
						|
    }
 | 
						|
 | 
						|
    return ERR_OK;
 | 
						|
}
 | 
						|
 | 
						|
void app_restore_factory(void)
 | 
						|
{
 | 
						|
    uint32_t ret;
 | 
						|
    app_entity_t *app_entry = NULL;
 | 
						|
 | 
						|
    app_entry = iot_main_get_app_entry();
 | 
						|
 | 
						|
    os_mem_cpy(app_entry->app_cfg.factory.multicast_mac, APP_GRP_MAC_ADDR,
 | 
						|
        IOT_MAC_ADDR_LEN);
 | 
						|
    app_entry->app_cfg.factory.sta_join_notify = APP_DEFAULT_JOIN_NOTIFY;
 | 
						|
 | 
						|
    ret = app_save_factory_to_nv();
 | 
						|
    if (ret != ERR_OK) {
 | 
						|
        APP_PRINTF("[ERR] %s Save Factory Failed !!", __FUNCTION__);
 | 
						|
        return;
 | 
						|
    }
 | 
						|
    APP_PRINTF("[INF] %s Success", __FUNCTION__);
 | 
						|
 | 
						|
    return;
 | 
						|
}
 | 
						|
 | 
						|
void app_load_nv_conf(void)
 | 
						|
{
 | 
						|
    uint32_t ret;
 | 
						|
    uint8_t need_restore = true;
 | 
						|
    app_entity_t *app_entry = NULL;
 | 
						|
 | 
						|
    app_entry = iot_main_get_app_entry();
 | 
						|
 | 
						|
    ret = app_get_factory_from_nv(&app_entry->app_cfg.factory);
 | 
						|
    if (ret != ERR_OK) {
 | 
						|
        APP_PRINTF("[ERR] %s Read Factory Failed !!", __FUNCTION__);
 | 
						|
        goto end;
 | 
						|
    }
 | 
						|
 | 
						|
    /* load nv normal */
 | 
						|
    need_restore = false;
 | 
						|
 | 
						|
end:
 | 
						|
    if (need_restore) {
 | 
						|
        /* load nv abnormal, restore */
 | 
						|
        app_restore_factory();
 | 
						|
        APP_PRINTF("%s restore factory, reset sys!", __FUNCTION__);
 | 
						|
        os_delay(500);
 | 
						|
        iot_system_restart(IOT_SYS_RST_REASON_APP_REQ);
 | 
						|
    }
 | 
						|
 | 
						|
    APP_PRINTF("%s success", __FUNCTION__);
 | 
						|
    return;
 | 
						|
}
 | 
						|
 | 
						|
void app_pib_restore_factory(void)
 | 
						|
{
 | 
						|
    uint32_t err = 0;
 | 
						|
    uart_cfg_t uart_para = {0};
 | 
						|
    uint8_t fb_bitmap[IOT_PLC_BAND_BITMAP_SIZE] = { 0 };
 | 
						|
 | 
						|
    uart_para.baud_rate = APP_DEFAULT_BAUND_RATE;
 | 
						|
    uart_para.data_bits = APP_DEFAULT_DATA_BITS;
 | 
						|
    uart_para.stop_bits = APP_DEFAULT_STOP_BITS;
 | 
						|
    uart_para.parity = APP_DEFAULT_PARITY;
 | 
						|
    uart_para.threshold_value = APP_DEFAULT_THDVALUE;
 | 
						|
 | 
						|
    if (ERR_OK != iot_app_save_uart_cfg_to_pib(&uart_para)) {
 | 
						|
        err = 1;
 | 
						|
        goto out;
 | 
						|
    }
 | 
						|
 | 
						|
    if (iot_plc_is_client_mode()) {
 | 
						|
        fb_bitmap[APP_DEFAULT_FREQ_BAND / 8] |= 1 << (APP_DEFAULT_FREQ_BAND % 8);
 | 
						|
        if (ERR_OK != iot_app_save_scan_band_to_pib(fb_bitmap,
 | 
						|
            IOT_PLC_BAND_BITMAP_SIZE)) {
 | 
						|
            err = 2;
 | 
						|
            goto out;
 | 
						|
        }
 | 
						|
    }
 | 
						|
 | 
						|
    if (ERR_OK != iot_app_save_work_band_to_pib(APP_DEFAULT_FREQ_BAND)) {
 | 
						|
        err = 3;
 | 
						|
        goto out;
 | 
						|
    }
 | 
						|
 | 
						|
    if (ERR_OK != iot_plc_cco_set_whitelist_func(IOT_PLC_WL_DEL_ALL, 0, NULL)) {
 | 
						|
        err = 4;
 | 
						|
        goto out;
 | 
						|
    }
 | 
						|
 | 
						|
    if (ERR_OK != iot_plc_cco_set_whitelist_func(IOT_PLC_WL_DISABLE, 0, NULL)) {
 | 
						|
        err = 5;
 | 
						|
    }
 | 
						|
 | 
						|
out:
 | 
						|
    if (err) {
 | 
						|
        APP_PRINTF("%s failed for reason[%d]", __FUNCTION__, err);
 | 
						|
        return;
 | 
						|
    }
 | 
						|
 | 
						|
    APP_PRINTF("%s success!", __FUNCTION__);
 | 
						|
}
 | 
						|
 | 
						|
void app_pib_conf_init(void)
 | 
						|
{
 | 
						|
    uint8_t need_restore = false;
 | 
						|
    uart_cfg_t uart_para = {0};
 | 
						|
 | 
						|
    if (iot_app_get_uart_cfg_in_pib(&uart_para) != ERR_OK) {
 | 
						|
        APP_PRINTF("%s get uart_para failed!", __FUNCTION__);
 | 
						|
        return;
 | 
						|
    }
 | 
						|
 | 
						|
    if (1 == uart_para.uart_conf_vaild
 | 
						|
        && false == app_uart_is_valid(uart_para.baud_rate, uart_para.data_bits,
 | 
						|
        uart_para.stop_bits, uart_para.parity, uart_para.threshold_value)) {
 | 
						|
        APP_PRINTF("%s uart_para error", __FUNCTION__);
 | 
						|
        need_restore = true;
 | 
						|
    }
 | 
						|
 | 
						|
    if (need_restore) {
 | 
						|
        /* pib config abnormal, restore */
 | 
						|
        app_pib_restore_factory();
 | 
						|
    }
 | 
						|
 | 
						|
    APP_PRINTF("%s success", __FUNCTION__);
 | 
						|
    return;
 | 
						|
}
 |