264 lines
7.2 KiB
C
264 lines
7.2 KiB
C
|
/*
|
||
|
* ESPRESSIF MIT License
|
||
|
*
|
||
|
* Copyright (c) 2015 <ESPRESSIF SYSTEMS (SHANGHAI) PTE LTD>
|
||
|
*
|
||
|
* Permission is hereby granted for use on ESPRESSIF SYSTEMS ESP8266 only, in which case,
|
||
|
* it is free of charge, to any person obtaining a copy of this software and associated
|
||
|
* documentation files (the "Software"), to deal in the Software without restriction, including
|
||
|
* without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
||
|
* and/or sell copies of the Software, and to permit persons to whom the Software is furnished
|
||
|
* to do so, subject to the following conditions:
|
||
|
*
|
||
|
* The above copyright notice and this permission notice shall be included in all copies or
|
||
|
* substantial portions of the Software.
|
||
|
*
|
||
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
||
|
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
||
|
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
||
|
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||
|
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||
|
*
|
||
|
*/
|
||
|
#include "freertos/FreeRTOS.h"
|
||
|
#include "freertos/task.h"
|
||
|
|
||
|
#include "esp_common.h"
|
||
|
#include "user_config.h"
|
||
|
#include "apps/sntp.h"
|
||
|
#include "uart.h"
|
||
|
#include "cfg.h"
|
||
|
#include "log.h"
|
||
|
#include "soft_ap.h"
|
||
|
#include "user_main.h"
|
||
|
#include "cfg.h"
|
||
|
|
||
|
static char g_DeviceId[33];
|
||
|
static char g_staMacAddr[16];
|
||
|
|
||
|
|
||
|
void set_platform_product_id(uint8 *param, uint8 len)
|
||
|
{
|
||
|
char g_ProductId[5];
|
||
|
|
||
|
if(param == NULL || len != 4){
|
||
|
LOG_EX(LOG_Info, "param error\r\n");
|
||
|
|
||
|
return ;
|
||
|
}
|
||
|
|
||
|
memset(g_ProductId, 0, sizeof(g_ProductId));
|
||
|
memcpy(g_ProductId, param, len);
|
||
|
LOG_EX(LOG_Info, "enter_softap! g_ProductId=%s\r\n", g_ProductId);
|
||
|
|
||
|
if(ne_cfg_save_product_id(g_ProductId, sizeof(g_ProductId)) != ERR_OK){
|
||
|
LOG_EX(LOG_Info, "savr product error\r\n");
|
||
|
}
|
||
|
}
|
||
|
|
||
|
|
||
|
const char* GetPlatformDevId(void)
|
||
|
{
|
||
|
return g_DeviceId;
|
||
|
}
|
||
|
|
||
|
const char* GetSTAMacAddr(void)
|
||
|
{
|
||
|
return g_staMacAddr;
|
||
|
}
|
||
|
/******************************************************************************
|
||
|
* FunctionName : user_rf_cal_sector_set
|
||
|
* Description : SDK just reversed 4 sectors, used for rf init data and paramters.
|
||
|
* We add this function to force users to set rf cal sector, since
|
||
|
* we don't know which sector is free in user's application.
|
||
|
* sector map for last several sectors : ABCCC
|
||
|
* A : rf cal
|
||
|
* B : rf init data
|
||
|
* C : sdk parameters
|
||
|
* Parameters : none
|
||
|
* Returns : rf cal sector
|
||
|
*******************************************************************************/
|
||
|
uint32 user_rf_cal_sector_set(void)
|
||
|
{
|
||
|
flash_size_map size_map = system_get_flash_size_map();
|
||
|
uint32 rf_cal_sec = 0;
|
||
|
|
||
|
switch (size_map) {
|
||
|
case FLASH_SIZE_4M_MAP_256_256:
|
||
|
rf_cal_sec = 128 - 5;
|
||
|
break;
|
||
|
|
||
|
case FLASH_SIZE_8M_MAP_512_512:
|
||
|
rf_cal_sec = 256 - 5;
|
||
|
break;
|
||
|
|
||
|
case FLASH_SIZE_16M_MAP_512_512:
|
||
|
case FLASH_SIZE_16M_MAP_1024_1024:
|
||
|
rf_cal_sec = 512 - 5;
|
||
|
break;
|
||
|
|
||
|
case FLASH_SIZE_32M_MAP_512_512:
|
||
|
case FLASH_SIZE_32M_MAP_1024_1024:
|
||
|
rf_cal_sec = 1024 - 5;
|
||
|
break;
|
||
|
case FLASH_SIZE_64M_MAP_1024_1024:
|
||
|
rf_cal_sec = 2048 - 5;
|
||
|
break;
|
||
|
case FLASH_SIZE_128M_MAP_1024_1024:
|
||
|
rf_cal_sec = 4096 - 5;
|
||
|
break;
|
||
|
default:
|
||
|
rf_cal_sec = 0;
|
||
|
break;
|
||
|
}
|
||
|
|
||
|
return rf_cal_sec;
|
||
|
}
|
||
|
|
||
|
static const char* _get_used_server(int svrType)
|
||
|
{
|
||
|
switch(svrType)
|
||
|
{
|
||
|
case ONLINE_PUBLISH: return "ONLINE_PUBLISH";
|
||
|
case TEST_LAN_PUBLISN: return "TEST_LAN_PUBLISN";
|
||
|
case TEST_WAN_PUBLISH: return "TEST_WAN_PUBLISH";
|
||
|
default:
|
||
|
return "Error Server Configure";
|
||
|
}
|
||
|
}
|
||
|
|
||
|
static void init_sntp(void)
|
||
|
{
|
||
|
sntp_setoperatingmode(SNTP_OPMODE_POLL);
|
||
|
|
||
|
// for set more sntp server, plz modify macro SNTP_MAX_SERVERS in sntp_opts.h file
|
||
|
sntp_setservername(0, "202.112.29.82"); // set sntp server after got ip address, you had better to adjust the sntp server to your area
|
||
|
// sntp_setservername(1, "time-a.nist.gov");
|
||
|
// sntp_setservername(2, "ntp.sjtu.edu.cn");
|
||
|
// sntp_setservername(3, "0.nettime.pool.ntp.org");
|
||
|
// sntp_setservername(4, "time-b.nist.gov");
|
||
|
// sntp_setservername(5, "time-a.timefreq.bldrdoc.gov");
|
||
|
// sntp_setservername(6, "time-b.timefreq.bldrdoc.gov");
|
||
|
// sntp_setservername(7, "time-c.timefreq.bldrdoc.gov");
|
||
|
// sntp_setservername(8, "utcnist.colorado.edu");
|
||
|
// sntp_setservername(9, "time.nist.gov");
|
||
|
|
||
|
sntp_init();
|
||
|
|
||
|
while (1) {
|
||
|
u32_t ts = 0;
|
||
|
ts = sntp_get_current_timestamp();
|
||
|
LOG_EX(LOG_Debug, "Current time: %s\n", sntp_get_real_time(ts));
|
||
|
|
||
|
if (ts == 0) {
|
||
|
} else {
|
||
|
break;
|
||
|
}
|
||
|
|
||
|
vTaskDelay(2000 / portTICK_RATE_MS);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
static void __dumpHeapSize(void* pvParams)
|
||
|
{
|
||
|
while (TRUE) {
|
||
|
LOG_EX(LOG_Debug, "Free Heap Size: %u\n", system_get_free_heap_size());
|
||
|
vTaskDelay(1000 / portTICK_RATE_MS);
|
||
|
}
|
||
|
|
||
|
vTaskDelete(NULL);
|
||
|
}
|
||
|
|
||
|
/*
|
||
|
* system device id init.
|
||
|
*/
|
||
|
static void ne_device_id_init(void)
|
||
|
{
|
||
|
u8_t devMd5[16];
|
||
|
uint32_t uDevId = system_get_chip_id();
|
||
|
u8_t mac[6];
|
||
|
|
||
|
wifi_get_macaddr(STATION_IF, mac);
|
||
|
mbedtls_md5(mac, 6, devMd5);
|
||
|
|
||
|
memset(g_DeviceId, 0, 33);
|
||
|
SysBin2HexStr(g_DeviceId, devMd5, 16);
|
||
|
|
||
|
LOG_EX(LOG_Info, "SDK version:%s %d firmware version=%s\n", system_get_sdk_version(), system_get_free_heap_size(), FIRMWARE_VERSION);
|
||
|
LOG_EX(LOG_Info, "Chip ID: %s(%u)\n", g_DeviceId, uDevId);
|
||
|
|
||
|
LOG_EX(LOG_Info, "Product Key: %s\n", USR_CFG_SERVER(ProductKey));
|
||
|
LOG_EX(LOG_Info, "Product Secert: %s\n", USR_CFG_SERVER(ProductSecury));
|
||
|
LOG_EX(LOG_Info, "HTTPS Server: %s\n", USR_CFG_SERVER(HTTPSServer));
|
||
|
LOG_EX(LOG_Info, "MQTT Server: %s\n", USR_CFG_SERVER(MQTTServer));
|
||
|
LOG_EX(LOG_Info, "MQTT Port: %d\n", USR_CFG_SERVER(MQTTPort));
|
||
|
}
|
||
|
|
||
|
/*
|
||
|
* system get wifi mac address.
|
||
|
*/
|
||
|
static void ne_get_wifi_mac(void)
|
||
|
{
|
||
|
u8_t mac[6];
|
||
|
|
||
|
wifi_get_macaddr(STATION_IF, mac);
|
||
|
memset(g_staMacAddr, 0, 16);
|
||
|
SysBin2HexStr(g_staMacAddr, mac, 6);
|
||
|
|
||
|
LOG_EX(LOG_Info, "STA MAC: %02X:%02X:%02X:%02X:%02X:%02X\n", MAC2STR(mac));
|
||
|
|
||
|
wifi_get_macaddr(SOFTAP_IF, mac);
|
||
|
LOG_EX(LOG_Info, "AP MAC: %02X:%02X:%02X:%02X:%02X:%02X\n", MAC2STR(mac));
|
||
|
}
|
||
|
|
||
|
/*
|
||
|
* system product init.
|
||
|
*/
|
||
|
static int ne_product_init(void)
|
||
|
{
|
||
|
CfgInitEnv();
|
||
|
ne_device_init();
|
||
|
ne_device_id_init();
|
||
|
ne_get_wifi_mac();
|
||
|
ShowCfgConfigure();
|
||
|
}
|
||
|
|
||
|
|
||
|
/*
|
||
|
* system server init.
|
||
|
*/
|
||
|
static int ne_server_init(void)
|
||
|
{
|
||
|
// spi_flash_erase_sector(PARAM_SAVE_DEVICESECRET);
|
||
|
ne_thread_monitor_init();
|
||
|
ne_thread_monitor_start();
|
||
|
ProtocolInitEnv();
|
||
|
user_conn_init();
|
||
|
}
|
||
|
|
||
|
/*
|
||
|
* entry of user application, init user function here.
|
||
|
*/
|
||
|
void ICACHE_FLASH_ATTR user_init(void)
|
||
|
{
|
||
|
wifi_set_opmode(1); // set wifi start up only STA mode.
|
||
|
|
||
|
ne_product_init();
|
||
|
|
||
|
ne_server_init();
|
||
|
|
||
|
#if NE_DEBUG_VERSION
|
||
|
ne_timer_init();
|
||
|
|
||
|
ne_keypad_init();
|
||
|
#endif
|
||
|
|
||
|
wifi_station_set_auto_connect(0);
|
||
|
|
||
|
ne_network_init();
|
||
|
|
||
|
//xTaskCreate(__dumpHeapSize, "heap_dump", 256, NULL, 1, NULL);
|
||
|
}
|
||
|
|