#include "FreeRTOS.h"
#include "task.h"
#include "diag.h"
#include "main.h"
#include <platform/platform_stdlib.h>
#include <sntp/sntp.h>
#include <lwip/sockets.h>
#include <lwip_netconf.h>
#include <dhcp/dhcps.h>
#include "wifi_conf.h"
#include "sleep_ex_api.h"
extern struct netif xnetif[NET_IF_NUM];
typedef int (*wlan_init_done_ptr)(void);
extern wlan_init_done_ptr p_wlan_init_done_callback;
#define WiFi_SSID "******"
#define WiFi_PASSWORD "*******"
#define WiFi_CHANNEL 1
#define WiFi_SECTYPE RTW_SECURITY_WPA2_AES_PSK
static void show_time(void)
{
unsigned int update_tick = 0;
long update_sec = 0, update_usec = 0;
uint32_t tt = xTaskGetTickCount();
while(xTaskGetTickCount() - tt < 3000) {
sntp_get_lasttime(&update_sec, &update_usec, &update_tick);
if(update_tick) break;
vTaskDelay(1);
}
long tick_diff_sec, tick_diff_ms, current_sec, current_usec;
unsigned int current_tick = xTaskGetTickCount();
tick_diff_sec = (current_tick - update_tick) / configTICK_RATE_HZ;
tick_diff_ms = (current_tick - update_tick) % configTICK_RATE_HZ / portTICK_RATE_MS;
update_sec += tick_diff_sec;
update_usec += (tick_diff_ms * 1000);
current_sec = update_sec + update_usec / 1000000;
current_usec = update_usec % 1000000;
printf("\n%s + %d usec\n", ctime(¤t_sec), current_usec);
}
int wlan_init_done_cb(void)
{
char ssid[] = WiFi_SSID;
char password[] = WiFi_PASSWORD;
int ret;
printf("\r\n\%s: Time at start %d ms.\n", __func__, xTaskGetTickCount());
#if CONFIG_LWIP_LAYER
netif_set_up(&xnetif[0]);
#endif
#if 0 // CONFIG_AUTO_RECONNECT Enable in wlan_network()
// setup reconnection flag
ret = wifi_set_autoreconnect(1);
printf("\nwifi_set_autoreconnect [%d] at start %d ms.\n", ret, xTaskGetTickCount());
if(ret < 0) goto go_deep;
#endif
ret = wifi_connect(ssid, WiFi_SECTYPE, password, strlen(WiFi_SSID), strlen(WiFi_PASSWORD), -1, NULL);
printf("\nwifi_connected [%d] at start %d ms.\n", ret, xTaskGetTickCount());
if(ret == RTW_SUCCESS){
ret = LwIP_DHCP(0, DHCP_START);
printf("\nDHCP_START [%d] at start %d ms.\n", ret , xTaskGetTickCount());
if (ret == DHCP_ADDRESS_ASSIGNED) {
sntp_init();
show_time();
sntp_stop();
}
}
go_deep:
printf("Enter deep sleep at start %d ms.\n", xTaskGetTickCount());
// stop dhcp server
dhcps_deinit();
wifi_off();
// enter deep sleep
deepsleep_ex(DSLEEP_WAKEUP_BY_TIMER, 10000);
return ret;
}
void main(void)
{
if ( rtl_cryptoEngine_init() != 0 ) {
DiagPrintf("crypto engine init failed\r\n");
}
/* Initialize log uart and at command service */
ReRegisterPlatformLogUart();
/* wlan intialization */
#if defined(CONFIG_WIFI_NORMAL) && defined(CONFIG_NETWORK)
wlan_network();
#endif
/* Execute application example */
// Call back from wlan driver after wlan init done
p_wlan_init_done_callback = wlan_init_done_cb;
/*Enable Schedule, Start Kernel*/
#if defined(CONFIG_KERNEL) && !TASK_SCHEDULER_DISABLED
#ifdef PLATFORM_FREERTOS
vTaskStartScheduler();
#endif
#else
RtlConsolTaskRom(NULL);
#endif
}