460 lines
13 KiB
C
460 lines
13 KiB
C
#include <stdint.h>
|
|
#include <stdio.h>
|
|
#include <string.h>
|
|
|
|
#include "esp_common.h"
|
|
#include "esp_wifi.h"
|
|
#include "user_config.h"
|
|
#include "cfg.h"
|
|
#include "log.h"
|
|
#include "jsprase.h"
|
|
#include "user_main.h"
|
|
#include "uart.h"
|
|
|
|
#define GLOBAL_TAGS "NETEASEIOTGLOBALCONFIGURETAG"
|
|
#define NE_PRODUCT_ID_LEN 4
|
|
|
|
static PUSER_CONFIG g_pUserCfg = NULL;
|
|
|
|
static GLOGAL_CONFIG g_globalCfg;
|
|
|
|
static void __dumpGlobalCfg(void)
|
|
{
|
|
int err;
|
|
char *pSerial;
|
|
|
|
pSerial = (char *)Struct2Json(&g_globalCfg, JE_GLOBAL_CFG, CRYPTO_NONE, &err);
|
|
|
|
if(err != ERR_OK || pSerial == NULL)
|
|
{
|
|
return;
|
|
}
|
|
|
|
LOG_EX(LOG_Debug, "Global Configure Information:\n%s\n", pSerial);
|
|
free(pSerial);
|
|
}
|
|
|
|
PUSER_CONFIG ne_cfg_get_user_cfg(void)
|
|
{
|
|
return g_pUserCfg;
|
|
}
|
|
/******************************************************************************
|
|
* FunctionName : ne_cfg_get_init_type
|
|
* Description : config function
|
|
* Parameters : task param
|
|
* Returns : none
|
|
*******************************************************************************/
|
|
int32 ne_cfg_get_init_type(void)
|
|
{
|
|
uint32_t flash_sector = PARAM_SAVE_OTAINFO_TYPE;
|
|
uint8_t data[4];
|
|
|
|
if (spi_flash_read(flash_sector*SECTOR_SIZE, (uint32 *)data, sizeof(data)) != SPI_FLASH_RESULT_OK) {
|
|
LOG_EX(LOG_Error, "get type\r\n");
|
|
}
|
|
|
|
if ((data[0] == 0x55) && (data[1] == 0xAA)) {
|
|
return data[2]-1;
|
|
} else {
|
|
return -1;
|
|
}
|
|
}
|
|
|
|
/******************************************************************************
|
|
* FunctionName : ne_cfg_set_init_type
|
|
* Description : config function
|
|
* Parameters : task param
|
|
* Returns : none
|
|
*******************************************************************************/
|
|
uint32_t ne_cfg_set_init_type(uint8_t type)
|
|
{
|
|
uint32_t flash_sector = PARAM_SAVE_OTAINFO_TYPE;
|
|
uint8_t data[4];
|
|
|
|
data[0] = 0x55;
|
|
data[1] = 0xAA;
|
|
data[2] = type+1;
|
|
data[3] = 0;
|
|
|
|
/* write information to flash */
|
|
if (spi_flash_erase_sector(flash_sector) != SPI_FLASH_RESULT_OK) {
|
|
LOG_EX(LOG_Error, "flash erase\r\n");
|
|
} else {
|
|
if (spi_flash_write(flash_sector*SECTOR_SIZE, (uint32 *)data, sizeof(data)) != SPI_FLASH_RESULT_OK) {
|
|
LOG_EX(LOG_Error, "flash write\r\n");
|
|
}
|
|
}
|
|
|
|
return TRUE;
|
|
}
|
|
|
|
int ne_cfg_get_product_id(uint8 *product_id, uint32 len)
|
|
{
|
|
uint32_t flash_sector = PARAM_SAVE_PRODUCTID;
|
|
if(product_id == NULL || len < NE_PRODUCT_ID_LEN + 1){
|
|
LOG_EX(LOG_Error, "param error\r\n");
|
|
|
|
return ERR_FAIL;
|
|
}
|
|
|
|
if (spi_flash_read(flash_sector*SECTOR_SIZE, (uint32 *)product_id, sizeof(product_id)) != SPI_FLASH_RESULT_OK) {
|
|
LOG_EX(LOG_Error, "read sector error\r\n");
|
|
|
|
return ERR_FAIL;
|
|
}
|
|
|
|
return ERR_OK;
|
|
}
|
|
|
|
|
|
int ne_cfg_save_product_id(uint8 *product_id, int32 len)
|
|
{
|
|
if(product_id == NULL || len < NE_PRODUCT_ID_LEN + 1){
|
|
LOG_EX(LOG_Error, "param error\n");
|
|
}
|
|
|
|
if(spi_flash_erase_sector(PARAM_SAVE_PRODUCTID) != SPI_FLASH_RESULT_OK)
|
|
{
|
|
LOG_EX(LOG_Error, "Erase Sector error\n");
|
|
return -ERR_ERASE_FLASH;
|
|
}
|
|
|
|
if(spi_flash_write(PARAM_SAVE_PRODUCTID * SECTOR_SIZE, (unsigned int*)product_id, len) != SPI_FLASH_RESULT_OK)
|
|
{
|
|
LOG_EX(LOG_Error, "Erase Sector error\n");
|
|
return -ERR_WRITE_FLASH;
|
|
}
|
|
|
|
return ERR_OK;
|
|
}
|
|
|
|
|
|
int CfgSaveToFlash(void)
|
|
{
|
|
SpiFlashOpResult ret = spi_flash_erase_sector(PARAM_SAVE_CONFIGURE);
|
|
|
|
if(ret != SPI_FLASH_RESULT_OK)
|
|
{
|
|
LOG_EX(LOG_Error, "Erase Sector error: %d\n", ret);
|
|
return -ERR_ERASE_FLASH;
|
|
}
|
|
|
|
ret = spi_flash_write(PARAM_SAVE_CONFIGURE * SECTOR_SIZE, (unsigned int*)&g_globalCfg, sizeof(GLOGAL_CONFIG));
|
|
|
|
if(ret != SPI_FLASH_RESULT_OK)
|
|
{
|
|
LOG_EX(LOG_Error, "Erase Sector error: %d\n", ret);
|
|
return -ERR_WRITE_FLASH;
|
|
}
|
|
|
|
return ERR_OK;
|
|
}
|
|
|
|
void ne_save_ssid_psk(uint8 *ssid, uint8 *psk)
|
|
{
|
|
GLOGAL_CONFIG cfg;
|
|
SpiFlashOpResult ret;
|
|
|
|
if(ssid == NULL)
|
|
return;
|
|
|
|
ret = spi_flash_read(PARAM_SAVE_CONFIGURE * SECTOR_SIZE, (unsigned int*)&cfg, sizeof(GLOGAL_CONFIG));
|
|
|
|
if(ret != SPI_FLASH_RESULT_OK)
|
|
{
|
|
LOG_EX(LOG_Error, "Read Sector error: %d\n", ret);
|
|
}
|
|
|
|
memset(cfg.staWifiCfg.pSSID, 0 , MAX_WIFI_SSID);
|
|
memset(cfg.staWifiCfg.pPassword, 0 , MAX_WIFI_PASSWD);
|
|
|
|
memcpy(cfg.staWifiCfg.pSSID, ssid, strlen(ssid));
|
|
memcpy(cfg.staWifiCfg.pPassword, psk, strlen(psk));
|
|
|
|
/* earse */
|
|
spi_flash_erase_sector(PARAM_SAVE_CONFIGURE);
|
|
|
|
ret = spi_flash_write(PARAM_SAVE_CONFIGURE * SECTOR_SIZE, (unsigned int*)&cfg, sizeof(GLOGAL_CONFIG));
|
|
if(ret != SPI_FLASH_RESULT_OK)
|
|
{
|
|
LOG_EX(LOG_Error, "write Sector error: %d\n", ret);
|
|
}
|
|
}
|
|
|
|
|
|
void ne_clear_ssid_psk(void)
|
|
{
|
|
GLOGAL_CONFIG cfg;
|
|
SpiFlashOpResult ret;
|
|
|
|
ret = spi_flash_read(PARAM_SAVE_CONFIGURE * SECTOR_SIZE, (unsigned int*)&cfg, sizeof(GLOGAL_CONFIG));
|
|
|
|
if(ret != SPI_FLASH_RESULT_OK)
|
|
{
|
|
LOG_EX(LOG_Error, "Read Sector error: %d\n", ret);
|
|
}
|
|
|
|
memset(cfg.staWifiCfg.pSSID, 0, MAX_WIFI_SSID);
|
|
memset(cfg.staWifiCfg.pPassword, 0, MAX_WIFI_PASSWD);
|
|
|
|
ret = spi_flash_write(PARAM_SAVE_CONFIGURE * SECTOR_SIZE, (unsigned int*)&cfg, sizeof(GLOGAL_CONFIG));
|
|
|
|
if(ret != SPI_FLASH_RESULT_OK)
|
|
{
|
|
LOG_EX(LOG_Error, "write Sector error: %d\n", ret);
|
|
}
|
|
}
|
|
|
|
static void __dumpUserConfig(PUSER_CONFIG pCfg)
|
|
{
|
|
char* pStr;
|
|
int i;
|
|
#if 0
|
|
printf("FactoryMode: %d\n", pCfg->FactoryMode);
|
|
printf("DeviceType: %d\n", pCfg->DeviceType);
|
|
printf("ServerCfg:\n");
|
|
printf(" ProductKey: %s\n", pCfg->ServerCfg.ProductKey);
|
|
printf(" ProductSecury: %s\n", pCfg->ServerCfg.ProductSecury);
|
|
printf(" MQTTServer: %s\n", pCfg->ServerCfg.MQTTServer);
|
|
printf(" MQTTPort: %d\n", pCfg->ServerCfg.MQTTPort);
|
|
printf(" HTTPSServer: %s\n", pCfg->ServerCfg.HTTPSServer);
|
|
|
|
printf("SoftAP:\n");
|
|
printf(" UDPSvrPort: %d\n", pCfg->SoftAP.UDPSvrPort);
|
|
printf(" SoftAPFormat: %s\n", pCfg->SoftAP.SoftAPFormat);
|
|
printf(" SoftAPDHCP:\n");
|
|
printf(" IPAddr: %s\n", pCfg->SoftAP.softAPDHCP.IPAddr);
|
|
printf(" Netmask: %s\n", pCfg->SoftAP.softAPDHCP.Netmask);
|
|
printf(" Gatway: %s\n", pCfg->SoftAP.softAPDHCP.Gatway);
|
|
printf(" DHCPStartIP: %s\n", pCfg->SoftAP.softAPDHCP.DHCPStartIP);
|
|
printf(" DHCPEndIP: %s\n", pCfg->SoftAP.softAPDHCP.DHCPEndIP);
|
|
printf("UART:\n");
|
|
for(i = 0; i < 2; i++)
|
|
{
|
|
printf(" Baud[%d]: %d\n", i, pCfg->UART[i].Baud);
|
|
printf(" DataBits[%d]: %d\n", i, pCfg->UART[i].DataBits);
|
|
printf(" StopBits[%d]: %d\n", i, pCfg->UART[i].StopBits);
|
|
printf(" FlowCtrl[%d]: %d\n", i, pCfg->UART[i].FlowCtrl);
|
|
}
|
|
#endif
|
|
pStr = (char*)Struct2Json(pCfg, JE_USER_CFG, CRYPTO_NONE, &i);
|
|
|
|
if(pStr == NULL || i != ERR_OK)
|
|
{
|
|
LOG_EX(LOG_Error, "Create JE_USER_CFG Error\n");
|
|
return;
|
|
}
|
|
|
|
LOG_EX(LOG_Debug, "User Configure Information:\n%s\n", pStr);
|
|
free(pStr);
|
|
}
|
|
|
|
PUSER_CONFIG CreateDefaultUserCfg(void)
|
|
{
|
|
static USER_CONFIG usrCfg;
|
|
|
|
memset(&usrCfg, 0, sizeof(USER_CONFIG));
|
|
|
|
|
|
usrCfg.Version = USER_CFG_VERSION;
|
|
usrCfg.UsedDefault = TRUE;
|
|
usrCfg.FactoryMode = 0;
|
|
usrCfg.DeviceType = 0;
|
|
usrCfg.ServerCfg.ProductKey = "3cfb69e088527ef73941b9b74ba1cd1b";
|
|
usrCfg.ServerCfg.ProductSecury = "ce3f33a99a86";
|
|
usrCfg.ServerCfg.MQTTServer = "link-iot.netease.im";
|
|
usrCfg.ServerCfg.MQTTPort = 8680;
|
|
usrCfg.ServerCfg.HTTPSServer = "https://api-iot.netease.im/iot/RegisterDevice";
|
|
usrCfg.SoftAP.UDPSvrPort = 10086;
|
|
usrCfg.SoftAP.SoftAPFormat = "NetEase_%02x%02x_Robovac";
|
|
usrCfg.WifiAP.pSSID = "";
|
|
usrCfg.WifiAP.pPassword = "";
|
|
usrCfg.SoftAP.softAPDHCP.IPAddr = "192.168.5.1";
|
|
usrCfg.SoftAP.softAPDHCP.Netmask = "255.255.255.0";
|
|
usrCfg.SoftAP.softAPDHCP.Gatway = "192.168.5.1";
|
|
usrCfg.SoftAP.softAPDHCP.DHCPStartIP = "192.168.5.100";
|
|
usrCfg.SoftAP.softAPDHCP.DHCPEndIP = "192.168.5.105";
|
|
usrCfg.UART[0].Baud = BIT_RATE_115200;
|
|
usrCfg.UART[0].DataBits = UART_WordLength_8b;
|
|
usrCfg.UART[0].StopBits = USART_StopBits_1;
|
|
usrCfg.UART[0].Parity = USART_Parity_None;
|
|
usrCfg.UART[0].FlowCtrl = USART_HardwareFlowControl_None;
|
|
usrCfg.UART[1].Baud = BIT_RATE_115200;
|
|
usrCfg.UART[1].DataBits = UART_WordLength_8b;
|
|
usrCfg.UART[1].StopBits = USART_StopBits_1;
|
|
usrCfg.UART[1].Parity = USART_Parity_None;
|
|
usrCfg.UART[1].FlowCtrl = USART_HardwareFlowControl_None;
|
|
usrCfg.UART[0].Baud = 115200;
|
|
usrCfg.UART[0].DataBits = 8;
|
|
usrCfg.UART[0].StopBits = 1;
|
|
usrCfg.UART[0].FlowCtrl = 3;
|
|
usrCfg.UART[1].Baud = 115200;
|
|
usrCfg.UART[1].DataBits = 8;
|
|
usrCfg.UART[1].StopBits = 1;
|
|
usrCfg.UART[1].FlowCtrl = 0;
|
|
|
|
return &usrCfg;
|
|
}
|
|
|
|
int UserCfgSaveToFlash(void)
|
|
{
|
|
char* pStr;
|
|
int err = ERR_OK;
|
|
SpiFlashOpResult ret = spi_flash_erase_sector(PARAM_SAVE_USERCFG);
|
|
|
|
if(ret != SPI_FLASH_RESULT_OK)
|
|
{
|
|
LOG_EX(LOG_Error, "Erase Sector error: %d\n", ret);
|
|
return -ERR_ERASE_FLASH;
|
|
}
|
|
|
|
pStr = (char *)Struct2Json(g_pUserCfg, JE_USER_CFG, CRYPTO_NONE, &err);
|
|
|
|
if(pStr == NULL || err != ERR_OK)
|
|
{
|
|
LOG_EX(LOG_Error, "Create JE_USER_CFG Error: %d\n", err);
|
|
return -ERR_JSON_STR_FORMAT;
|
|
}
|
|
|
|
ret = spi_flash_write(PARAM_SAVE_USERCFG * SECTOR_SIZE, (unsigned int *)pStr, strlen(pStr));
|
|
|
|
if(ret != SPI_FLASH_RESULT_OK)
|
|
{
|
|
LOG_EX(LOG_Error, "Erase Sector error: %d\n", ret);
|
|
return -ERR_WRITE_FLASH;
|
|
}
|
|
|
|
free(pStr);
|
|
return ERR_OK;
|
|
}
|
|
|
|
int CfgReadUserCfgFromFlash(void)
|
|
{
|
|
int i = 0;
|
|
SpiFlashOpResult ret;
|
|
PUSER_CONFIG pCfgInfo = NULL;
|
|
char* pUserCfg = (char*)HAL_Malloc(SECTOR_SIZE);
|
|
|
|
if(pUserCfg == NULL)
|
|
{
|
|
//LOG_EX(LOG_Error, "Malloc Memory Error\n");
|
|
return -ERR_MALLOC_MEMORY;
|
|
}
|
|
|
|
memset(pUserCfg, 0, SECTOR_SIZE);
|
|
|
|
ret = spi_flash_read(PARAM_SAVE_USERCFG * SECTOR_SIZE, (unsigned int*)pUserCfg, SECTOR_SIZE);
|
|
|
|
if(ret != SPI_FLASH_RESULT_OK)
|
|
{
|
|
//LOG_EX(LOG_Error, "Read Sector error: %d\n", ret);
|
|
return -ERR_READ_FLASH;
|
|
}
|
|
|
|
for(i = strlen(pUserCfg); i >= 0; i--)
|
|
{
|
|
if(pUserCfg[i] == '}')
|
|
{
|
|
pUserCfg[i + 1] = 0;
|
|
break;
|
|
}
|
|
}
|
|
|
|
//LOG_EX(LOG_Debug, "User Cfg: \n%s\n\n", pUserCfg);
|
|
//IHW_LOG_BUF(LOG_Debug, "Json", (unsigned char*)pUserCfg, strlen(pUserCfg));
|
|
|
|
pCfgInfo = (PUSER_CONFIG)Json2Struct(pUserCfg, JE_USER_CFG, CRYPTO_NONE, &i);
|
|
|
|
if(i != ERR_OK || pCfgInfo == NULL)
|
|
{
|
|
//LOG_EX(LOG_Error, "Decode User Configure Error: %d\n", i);
|
|
|
|
if(pCfgInfo)
|
|
{
|
|
free(pCfgInfo);
|
|
}
|
|
|
|
return -ERR_JSON_STR_FORMAT;
|
|
}
|
|
|
|
pCfgInfo->UsedDefault = 0;
|
|
//IHW_LOG_BUF(LOG_Debug, "UserCfg", (unsigned char*)pCfgInfo, sizeof(USER_CONFIG));
|
|
//__dumpUserConfig(pCfgInfo);
|
|
g_pUserCfg = pCfgInfo;
|
|
|
|
if(g_pUserCfg->Version != USER_CFG_VERSION)
|
|
{
|
|
UserCfgSaveToFlash();
|
|
}
|
|
free(pUserCfg);
|
|
return ERR_OK;
|
|
}
|
|
|
|
int CfgReadFromFlash(void)
|
|
{
|
|
GLOGAL_CONFIG cfg;
|
|
SpiFlashOpResult ret;
|
|
|
|
memset(&cfg, 0, sizeof(GLOGAL_CONFIG));
|
|
|
|
ret = spi_flash_read(PARAM_SAVE_CONFIGURE * SECTOR_SIZE, (unsigned int*)&cfg, sizeof(GLOGAL_CONFIG));
|
|
|
|
if(ret != SPI_FLASH_RESULT_OK)
|
|
{
|
|
LOG_EX(LOG_Error, "Read Sector error: %d\n", ret);
|
|
return -ERR_READ_FLASH;
|
|
}
|
|
|
|
if(strcmp(cfg.pCfgTagsInfo, GLOBAL_TAGS) != 0)
|
|
{
|
|
LOG_EX(LOG_Warn, "Global Configure Uninit\n");
|
|
return -ERR_ITEM_UNINIT;
|
|
}
|
|
|
|
memcpy(&g_globalCfg, &cfg, sizeof(GLOGAL_CONFIG));
|
|
return ERR_OK;
|
|
}
|
|
|
|
GLOGAL_CONFIG * ne_get_cfg_data(void)
|
|
{
|
|
if(CfgReadFromFlash() == ERR_OK){
|
|
return &g_globalCfg;
|
|
}
|
|
else{
|
|
return NULL;
|
|
}
|
|
}
|
|
|
|
void CfgInitEnv(void)
|
|
{
|
|
g_pUserCfg = CreateDefaultUserCfg();
|
|
|
|
if(CfgReadUserCfgFromFlash() != ERR_OK)
|
|
{
|
|
UserCfgSaveToFlash();
|
|
}
|
|
|
|
if(CfgReadFromFlash() == ERR_OK)
|
|
{
|
|
return;
|
|
}
|
|
|
|
LOG_EX(LOG_Warn, "cfg read fail\n");
|
|
memset(&g_globalCfg, 0, sizeof(GLOGAL_CONFIG));
|
|
|
|
strcpy(g_globalCfg.pCfgTagsInfo, GLOBAL_TAGS);
|
|
|
|
/* set soft ap config */
|
|
g_globalCfg.softapWifiCfg.authMode = AUTH_OPEN;
|
|
g_globalCfg.softapWifiCfg.svrPort = SOFTAP_SERVER_PORT;
|
|
strcpy(g_globalCfg.softapWifiCfg.pPassword, "");
|
|
|
|
CfgSaveToFlash();
|
|
}
|
|
|
|
void ShowCfgConfigure(void)
|
|
{
|
|
__dumpUserConfig(g_pUserCfg);
|
|
__dumpGlobalCfg();
|
|
}
|