esp8266-std/user/user_main.c

333 lines
9.2 KiB
C
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/*
* 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, "APP version:%s\n", CURRENT_VERSION);
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();
}
#if 0
static void test_aes(void)
{
int iCount = 0;
mbedtls_aes_context aes_ctx;
//<2F><>Կ<EFBFBD><D4BF>ֵ
unsigned char key[16] = {'0', '1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'};
unsigned char enAes[16] = {0xA5, 0xF4, 0xDD, 0xC8, 0xE5, 0xCF, 0x4B, 0xB5, 0x98, 0xB4, 0xCB, 0xE2, 0x85, 0x74, 0x71, 0xC6};
//<2F><><EFBFBD>Ŀռ<C4BF>
unsigned char plain[16] = "helloworld";
//<2F><><EFBFBD>ܺ<EFBFBD><DCBA><EFBFBD><EFBFBD>ĵĿռ<C4BF>
unsigned char dec_plain[16]={0};
//<2F><><EFBFBD>Ŀռ<C4BF>
unsigned char cipher[16]={0};
unsigned char dec[16]={0};
unsigned char basecode[128];
mbedtls_aes_init( &aes_ctx );
//<2F><><EFBFBD>ü<EFBFBD><C3BC><EFBFBD><EFBFBD><EFBFBD>Կ
mbedtls_aes_setkey_enc( &aes_ctx, key, 128);
LOG_EX(LOG_Debug, "*********** Before encode: %s\n", plain);
mbedtls_aes_crypt_ecb( &aes_ctx, MBEDTLS_AES_ENCRYPT, plain, cipher );
IHW_LOG_BUF(LOG_Debug, "AES_", cipher, 16);
memset(basecode, 0, 128);
mbedtls_base64_encode(basecode, 128, &iCount, cipher, 16);
LOG_EX(LOG_Debug, "\n*********** Encrypt AES: %s\n", basecode);
//<2F><><EFBFBD>ý<EFBFBD><C3BD><EFBFBD><EFBFBD><EFBFBD>Կ
mbedtls_aes_setkey_dec(&aes_ctx, key, 128);
mbedtls_aes_crypt_ecb( &aes_ctx, MBEDTLS_AES_DECRYPT, enAes, dec_plain );
LOG_EX(LOG_Debug, "*********** Decode: %s\n", dec_plain);
IHW_LOG_BUF(LOG_Debug, "CIP_", enAes, 16);
IHW_LOG_BUF(LOG_Debug, "DES_", dec_plain, 16);
mbedtls_aes_free( &aes_ctx );
}
#endif
static void __showTimes(void* pvParams)
{
uint32 cnt = 1;
while (TRUE) {
uint32 cal = system_rtc_clock_cali_proc();
uint32 rtc = system_get_rtc_time();
uint32 iVal = ((cal * 1000) >> 12) / 1000;
uint32 dVal = ((cal * 1000) >> 12) % 1000;
LOG_EX(LOG_Debug, "rtc = %u, cal(%u): %u.%u \n", rtc, cal, iVal, dVal);
vTaskDelay(1000 / portTICK_RATE_MS);
if(cnt++ > 25)
{
system_deep_sleep_instant(30000 * 1000);
}
}
vTaskDelete(NULL);
}
/*
* 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();
//wifi_set_sleep_type(MODEM_SLEEP_T);
//test_aes();
//xTaskCreate(__showTimes, "__showTimes", 256, NULL, 1, NULL);
}