430 lines
		
	
	
		
			12 KiB
		
	
	
	
		
			C
		
	
	
	
	
	
		
		
			
		
	
	
			430 lines
		
	
	
		
			12 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.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"
							 | 
						||
| 
								 | 
							
								#include "iot_config_api.h"
							 | 
						||
| 
								 | 
							
								#if APP_IO_OPERATION_ENABLE
							 | 
						||
| 
								 | 
							
								#include "app_gpio.h"
							 | 
						||
| 
								 | 
							
								#endif /* end APP_IO_OPERATION_ENABLE */
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								uint8_t app_get_init_work_mode(void)
							 | 
						||
| 
								 | 
							
								{
							 | 
						||
| 
								 | 
							
								    app_entity_t *app_entry = app_get_main_entry();
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    return app_entry->app_cfg.factory.work_mode;
							 | 
						||
| 
								 | 
							
								}
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								uint16_t app_set_work_mode_to_nv(uint8_t mode)
							 | 
						||
| 
								 | 
							
								{
							 | 
						||
| 
								 | 
							
								    app_entity_t *app_entry = app_get_main_entry();
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    if (app_entry->app_cfg.factory.work_mode == mode ) {
							 | 
						||
| 
								 | 
							
								        return ERR_OK;
							 | 
						||
| 
								 | 
							
								    }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    app_entry->app_cfg.factory.work_mode = mode;
							 | 
						||
| 
								 | 
							
								    return app_save_factory_to_nv();
							 | 
						||
| 
								 | 
							
								}
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								uint8_t app_check_hw_ver(app_hw_ver_e hw_ver)
							 | 
						||
| 
								 | 
							
								{
							 | 
						||
| 
								 | 
							
								    switch (hw_ver) {
							 | 
						||
| 
								 | 
							
								        case APP_HW_IPL_YY01:
							 | 
						||
| 
								 | 
							
								        case APP_HW_IPL_YY03:
							 | 
						||
| 
								 | 
							
								        case APP_HW_Q111N_H:
							 | 
						||
| 
								 | 
							
								        case APP_HW_Q111N_HS:
							 | 
						||
| 
								 | 
							
								        case APP_HW_Q111N_ISC:
							 | 
						||
| 
								 | 
							
								        case APP_HW_Q111N_ISF:
							 | 
						||
| 
								 | 
							
								        case APP_HW_Q111N_ISF03:
							 | 
						||
| 
								 | 
							
								        case APP_HW_Q111N_ISG:
							 | 
						||
| 
								 | 
							
								        case APP_HW_LEDC_P:
							 | 
						||
| 
								 | 
							
								        case APP_HW_LEDC:
							 | 
						||
| 
								 | 
							
								        case APP_HW_HTZD_STA_1_PHASE:
							 | 
						||
| 
								 | 
							
								        case APP_HW_HTZD_CCO_3_PHASE:
							 | 
						||
| 
								 | 
							
								            break;
							 | 
						||
| 
								 | 
							
								        default:
							 | 
						||
| 
								 | 
							
								            return false;
							 | 
						||
| 
								 | 
							
								    }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    return true;
							 | 
						||
| 
								 | 
							
								}
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								app_hw_ver_e app_get_hw_ver(void)
							 | 
						||
| 
								 | 
							
								{
							 | 
						||
| 
								 | 
							
								    app_entity_t *app_entry = app_get_main_entry();
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    return (app_hw_ver_e)app_entry->app_cfg.factory.hw_ver;
							 | 
						||
| 
								 | 
							
								}
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								uint16_t app_set_mac_addr_to_nv(uint8_t addr[IOT_MAC_ADDR_LEN])
							 | 
						||
| 
								 | 
							
								{
							 | 
						||
| 
								 | 
							
								    uint16_t ret = ERR_OK;
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    return ret;
							 | 
						||
| 
								 | 
							
								}
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								uint8_t app_get_sta_join_notify(void)
							 | 
						||
| 
								 | 
							
								{
							 | 
						||
| 
								 | 
							
								    app_entity_t *app_entry = app_get_main_entry();
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    return app_entry->app_cfg.factory.sta_join_notify ? true : false;
							 | 
						||
| 
								 | 
							
								}
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								uint16_t app_set_sta_join_notify(uint8_t mode)
							 | 
						||
| 
								 | 
							
								{
							 | 
						||
| 
								 | 
							
								    app_entity_t *app_entry = app_get_main_entry();
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    if (app_entry->app_cfg.factory.sta_join_notify == mode ) {
							 | 
						||
| 
								 | 
							
								        return ERR_OK;
							 | 
						||
| 
								 | 
							
								    }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    app_entry->app_cfg.factory.sta_join_notify = mode;
							 | 
						||
| 
								 | 
							
								    return app_save_factory_to_nv();
							 | 
						||
| 
								 | 
							
								}
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								uint8_t app_get_idf_status(void)
							 | 
						||
| 
								 | 
							
								{
							 | 
						||
| 
								 | 
							
								    app_entity_t *app_entry = app_get_main_entry();
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    return app_entry->app_cfg.factory.idf_status ? true : false;
							 | 
						||
| 
								 | 
							
								}
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								uint16_t app_set_idf_status(uint8_t status)
							 | 
						||
| 
								 | 
							
								{
							 | 
						||
| 
								 | 
							
								    app_entity_t *app_entry = app_get_main_entry();
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    if (app_entry->app_cfg.factory.idf_status == status) {
							 | 
						||
| 
								 | 
							
								        return ERR_OK;
							 | 
						||
| 
								 | 
							
								    }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    app_entry->app_cfg.factory.idf_status = status > 0;
							 | 
						||
| 
								 | 
							
								    return app_save_factory_to_nv();
							 | 
						||
| 
								 | 
							
								}
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								uint8_t app_get_lock_mac(void)
							 | 
						||
| 
								 | 
							
								{
							 | 
						||
| 
								 | 
							
								    app_entity_t *app_entry = app_get_main_entry();
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    return app_entry->app_cfg.factory.lock_mac;
							 | 
						||
| 
								 | 
							
								}
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								uint16_t app_set_lock_mac(uint8_t status)
							 | 
						||
| 
								 | 
							
								{
							 | 
						||
| 
								 | 
							
								    app_entity_t *app_entry = app_get_main_entry();
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    if (app_entry->app_cfg.factory.lock_mac == status) {
							 | 
						||
| 
								 | 
							
								        return ERR_OK;
							 | 
						||
| 
								 | 
							
								    }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    app_entry->app_cfg.factory.lock_mac = status;
							 | 
						||
| 
								 | 
							
								    return app_save_factory_to_nv();
							 | 
						||
| 
								 | 
							
								}
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								uint8_t app_load_group_from_nv(void)
							 | 
						||
| 
								 | 
							
								{
							 | 
						||
| 
								 | 
							
								    app_entity_t *app_entry = app_get_main_entry();
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    os_mem_cpy(app_entry->dev.sta_group.group,
							 | 
						||
| 
								 | 
							
								        app_entry->app_cfg.factory.group, IOT_STA_GROUP_BITMAP);
							 | 
						||
| 
								 | 
							
								    app_entry->dev.sta_group.group_cnt =
							 | 
						||
| 
								 | 
							
								        iot_bitmap_cbs(app_entry->dev.sta_group.group, IOT_STA_GROUP_BITMAP);
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    /* load err, reset group info */
							 | 
						||
| 
								 | 
							
								    if (app_entry->dev.sta_group.group_cnt > IOT_STA_SUPPORT_MAX_GROUP) {
							 | 
						||
| 
								 | 
							
								        APP_PRINTF("load flash group info err:%d reset",
							 | 
						||
| 
								 | 
							
								            app_entry->dev.sta_group.group_cnt);
							 | 
						||
| 
								 | 
							
								        app_entry->dev.sta_group.group_cnt = 0;
							 | 
						||
| 
								 | 
							
								        iot_bitmap_reset(app_entry->dev.sta_group.group, IOT_STA_GROUP_BITMAP);
							 | 
						||
| 
								 | 
							
								    }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    return ERR_OK;
							 | 
						||
| 
								 | 
							
								}
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								uint16_t app_save_group_to_nv(void)
							 | 
						||
| 
								 | 
							
								{
							 | 
						||
| 
								 | 
							
								    app_entity_t *app_entry = app_get_main_entry();
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    os_mem_cpy(app_entry->app_cfg.factory.group,
							 | 
						||
| 
								 | 
							
								        app_entry->dev.sta_group.group, IOT_STA_GROUP_BITMAP);
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    return app_save_factory_to_nv();
							 | 
						||
| 
								 | 
							
								}
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								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 = app_get_main_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;
							 | 
						||
| 
								 | 
							
								}
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								uint16_t app_get_bcast_from_nv(nv_ftm_bcast_id *param)
							 | 
						||
| 
								 | 
							
								{
							 | 
						||
| 
								 | 
							
								    uint16_t ret;
							 | 
						||
| 
								 | 
							
								    uint32_t offset;
							 | 
						||
| 
								 | 
							
								    nv_ftm_bcast_id nv_value = {0};
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    os_mem_set(&nv_value, 0, sizeof(nv_value));
							 | 
						||
| 
								 | 
							
								    offset = NV_FTM_BCAST_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 bcast param Failed !!", __FUNCTION__);
							 | 
						||
| 
								 | 
							
								        return ret;
							 | 
						||
| 
								 | 
							
								    }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    os_mem_cpy(param, &nv_value, sizeof(nv_ftm_bcast_id));
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    return ERR_OK;
							 | 
						||
| 
								 | 
							
								}
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								uint16_t app_save_bcast_to_nv(void)
							 | 
						||
| 
								 | 
							
								{
							 | 
						||
| 
								 | 
							
								    nv_ftm_bcast_id nv_value = {0};
							 | 
						||
| 
								 | 
							
								    uint16_t ret;
							 | 
						||
| 
								 | 
							
								    uint32_t offset;
							 | 
						||
| 
								 | 
							
								    app_entity_t *app_entry = app_get_main_entry();
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    os_mem_cpy(&nv_value, &app_entry->app_cfg.bcast, sizeof(nv_ftm_bcast_id));
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    offset = NV_FTM_BCAST_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 bcast param Failed !!", __FUNCTION__);
							 | 
						||
| 
								 | 
							
								        return ret;
							 | 
						||
| 
								 | 
							
								    }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    return ERR_OK;
							 | 
						||
| 
								 | 
							
								}
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								void app_restore_bcast_param(void)
							 | 
						||
| 
								 | 
							
								{
							 | 
						||
| 
								 | 
							
								    app_entity_t *app_entry = NULL;
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    app_entry = app_get_main_entry();
							 | 
						||
| 
								 | 
							
								    os_mem_set(&app_entry->app_cfg.bcast, 0, sizeof(nv_ftm_bcast_id));
							 | 
						||
| 
								 | 
							
								    app_save_bcast_to_nv();
							 | 
						||
| 
								 | 
							
								}
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								void app_restore_factory(void)
							 | 
						||
| 
								 | 
							
								{
							 | 
						||
| 
								 | 
							
								    uint32_t ret;
							 | 
						||
| 
								 | 
							
								    app_entity_t *app_entry = NULL;
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    app_entry = app_get_main_entry();
							 | 
						||
| 
								 | 
							
								#if (IOT_APP_SELECTION == IOT_APP_DEF_18_AT_APP)
							 | 
						||
| 
								 | 
							
								    app_entry->app_cfg.factory.work_mode = APP_WORK_MODE_AT;
							 | 
						||
| 
								 | 
							
								#elif (IOT_APP_SELECTION == IOT_APP_DEF_24_SILA_APP)
							 | 
						||
| 
								 | 
							
								    app_entry->app_cfg.factory.work_mode = APP_WORK_MODE_PROTO;
							 | 
						||
| 
								 | 
							
								#else
							 | 
						||
| 
								 | 
							
								    app_entry->app_cfg.factory.work_mode = APP_DEFAULT_WORK_MODE;
							 | 
						||
| 
								 | 
							
								#endif
							 | 
						||
| 
								 | 
							
								    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;
							 | 
						||
| 
								 | 
							
								    app_entry->app_cfg.factory.idf_status = false;
							 | 
						||
| 
								 | 
							
								    app_entry->app_cfg.factory.lock_mac = false;
							 | 
						||
| 
								 | 
							
								    os_mem_set(app_entry->app_cfg.factory.group, 0, IOT_STA_GROUP_BITMAP);
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								#if APP_IO_OPERATION_ENABLE
							 | 
						||
| 
								 | 
							
								    app_restore_hwio();
							 | 
						||
| 
								 | 
							
								#endif /* end APP_IO_OPERATION_ENABLE */
							 | 
						||
| 
								 | 
							
								    app_restore_bcast_param();
							 | 
						||
| 
								 | 
							
								    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;
							 | 
						||
| 
								 | 
							
								    uint32_t hw_version;
							 | 
						||
| 
								 | 
							
								    app_entity_t *app_entry = NULL;
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    app_entry = app_get_main_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;
							 | 
						||
| 
								 | 
							
								    }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    if (false == app_check_hw_ver(app_entry->app_cfg.factory.hw_ver)) {
							 | 
						||
| 
								 | 
							
								        /* init factory data */
							 | 
						||
| 
								 | 
							
								        os_mem_set(&app_entry->app_cfg.factory, 0, sizeof(nv_ftm_factory_id));
							 | 
						||
| 
								 | 
							
								        APP_PRINTF("%s hw_ver error\n", __FUNCTION__);
							 | 
						||
| 
								 | 
							
								        hw_version = iot_board_hw_version_hex();
							 | 
						||
| 
								 | 
							
								        if (true == app_check_hw_ver(hw_version)) {
							 | 
						||
| 
								 | 
							
								            app_entry->app_cfg.factory.hw_ver = hw_version;
							 | 
						||
| 
								 | 
							
								        } else {
							 | 
						||
| 
								 | 
							
								            app_entry->app_cfg.factory.hw_ver = APP_HW_LEDC;
							 | 
						||
| 
								 | 
							
								        }
							 | 
						||
| 
								 | 
							
								        ret = app_save_factory_to_nv();
							 | 
						||
| 
								 | 
							
								        if (ret != ERR_OK) {
							 | 
						||
| 
								 | 
							
								            APP_PRINTF("%s Save Factory Failed !!", __FUNCTION__);
							 | 
						||
| 
								 | 
							
								            goto END;
							 | 
						||
| 
								 | 
							
								        }
							 | 
						||
| 
								 | 
							
								        app_restore_factory();
							 | 
						||
| 
								 | 
							
								    }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    if (app_entry->app_cfg.factory.work_mode > APP_WORK_MODE_MAX) {
							 | 
						||
| 
								 | 
							
								        APP_PRINTF("%s work_mode error\n", __FUNCTION__);
							 | 
						||
| 
								 | 
							
								        goto END;
							 | 
						||
| 
								 | 
							
								    }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    ret = app_get_bcast_from_nv(&app_entry->app_cfg.bcast);
							 | 
						||
| 
								 | 
							
								    if (ret != ERR_OK) {
							 | 
						||
| 
								 | 
							
								        APP_PRINTF("[ERR] %s Read param 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;
							 | 
						||
| 
								 | 
							
								        }
							 | 
						||
| 
								 | 
							
								    } else {
							 | 
						||
| 
								 | 
							
								        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;
							 | 
						||
| 
								 | 
							
								}
							 |