Code:
LOCAL void smart_config_task(void *pvParameter) {
//disconnect station mode
wifi_station_set_reconnect_policy(false);
wifi_station_disconnect();
//now enable both station and ap mode
wifi_start_ap("test1234");
portTickType xLastWakeTime = xTaskGetTickCount();
for(;;) {
vTaskDelayUntil(&xLastWakeTime, 5000 / portTICK_RATE_MS);
}
bool wifi_start_ap(const char *pass) {
//for security issues, must have password with at least 8 characters for WPA
if(!pass || strcmp(pass, "") == 0 || strlen(pass) < 8) {
IOT_ERROR("NULL/Empty/Short password");
return false;
}
//set ap mode
WIFI_MODE mode = wifi_get_opmode();
if((mode & SOFTAP_MODE) == 0){
mode |= SOFTAP_MODE;
if(!wifi_set_mode(mode)){
IOT_ERROR("Failed to enable AP mode");
return false;
}
}
//get saved config
struct softap_config config;
if(!wifi_softap_get_config_default(&config)) {
IOT_ERROR("Failed to get SoftAP config");
return false;
}
//update values
sprintf((char *)config.ssid, "ESP8266");
sprintf((char *)config.password, pass);
config.ssid_len = 0;
config.authmode = AUTH_WPA_WPA2_PSK;
config.max_connection = 1;
//save new config
if(!wifi_softap_set_config(&config)) {
IOT_ERROR("Failed to set SoftAP config");
return false;
}
//check dhcp server
if(!wifi_softap_dhcps_status()){
IOT_DEBUG("DHCP server is not started, starting it...");
if(!wifi_softap_dhcps_start()){
IOT_ERROR("DHCP server start failed");
return false;
}
}
return true;
}
void init_wifi_controller(){
wifi_set_event_handler_cb(wifi_event_handler_cb);
//initial config for wifi ap and station
wifi_station_set_reconnect_policy(true);
wifi_station_set_auto_connect(true);
if(wifi_ap_enabled()) {
wifi_stop_ap();
}
if(!wifi_set_mode(STATION_MODE)) {
IOT_ERROR("Failed to set STATION mode");
}
wifi_station_connect();
}
void user_init(void) {
uart_init();
#ifdef DEBUG
gdbstub_init();
initiMemoryDump();
#endif
init_wifi_controller();
}
Statistics: Posted by gustavomassa — Thu Jun 01, 2017 11:10 am
Code:
bool wifi_set_mode(WIFI_MODE mode){
if(!mode){
bool s = wifi_set_opmode(mode);
wifi_fpm_open();
wifi_fpm_set_sleep_type(MODEM_SLEEP_T);
wifi_fpm_do_sleep(0xFFFFFFFF);
wifi_fpm_close();
return s;
}
return wifi_set_opmode(mode);
}
bool wifi_ap_set_visible(bool value) {
struct softap_config config;
if(!wifi_softap_get_config(&config)) {
IOT_ERROR("Failed to get softAP config");
return false;
}
//check value
if(value) {
config.ssid_hidden = false;
} else {
config.ssid_hidden = true;
}
if(!wifi_softap_set_config(&config)) {
IOT_ERROR("Failed to set softAP config");
return false;
}
return true;
}
void wifi_event_handler_cb(System_Event_t *event)
{
static bool station_was_connected = false;
if (event == NULL) {
return;
}
switch (event->event_id) {
case EVENT_STAMODE_DISCONNECTED:
wifi_station_is_connected = false;
Event_StaMode_Disconnected_t *ev = (Event_StaMode_Disconnected_t *)&event->event_info;
if(ev != NULL) {
//notify callback
if(on_station_disconnect){
on_station_disconnect(ev->reason);
}
//check ap not found count
if(ev->reason == REASON_NO_AP_FOUND) {
++wifi_ap_not_found_count;
if(wifi_ap_not_found_count >= 10) {
//call smart config
init_smart_config();
//reset ap not found count
wifi_ap_not_found_count = 0;
}
}
//check auth fail count
if(ev->reason == REASON_AUTH_FAIL) {
++wifi_auth_fail_count;
if(wifi_auth_fail_count >= 10) {
//call smart config
init_smart_config();
//reset count
wifi_auth_fail_count = 0;
}
}
} else {
IOT_ERROR("Event_StaMode_Disconnected_t is NULL");
}
break;
case EVENT_STAMODE_CONNECTED:
if(wifi_station_static_ip){
wifi_station_is_connected = true;
if(!station_was_connected){
station_was_connected = true;
//notify first connection callback
if(on_station_first_connect){
on_station_first_connect();
}
}
//notify callback
if(on_station_connect){
on_station_connect();
}
}
break;
case EVENT_STAMODE_DHCP_TIMEOUT:
if(wifi_station_is_connected){
wifi_station_is_connected = false;
if(on_station_disconnect){
on_station_disconnect(REASON_UNSPECIFIED);
}
}
break;
case EVENT_STAMODE_GOT_IP:
wifi_station_is_connected = true;
if(!station_was_connected){
station_was_connected = true;
if(on_station_first_connect){
on_station_first_connect();
}
}
if(on_station_connect){
on_station_connect();
}
break;
case EVENT_SOFTAPMODE_STACONNECTED:
if(on_client_connect){
on_client_connect();
}
break;
case EVENT_SOFTAPMODE_STADISCONNECTED:
if(on_client_disconnect){
on_client_disconnect();
}
break;
default:
break;
}
}
void init_wifi_controller(){
wifi_set_event_handler_cb(wifi_event_handler_cb);
//initial config for wifi ap and station
wifi_station_set_reconnect_policy(true);
wifi_station_set_auto_connect(true);
wifi_ap_set_visible(false);
if(!wifi_set_mode(STATIONAP_MODE)) {
IOT_ERROR("Failed to set STATIONAP_MODE mode");
}
if(!wifi_station_dhcpc_status()){
IOT_DEBUG("DHCP client is not started, starting it...");
if(!wifi_station_dhcpc_start()){
IOT_ERROR("DHCP client start failed");
}
}
wifi_station_connect();
}
LOCAL void smart_config_task(void *pvParameter) {
wifi_station_set_reconnect_policy(false);
wifi_station_disconnect();
//EMULATION TRAP HAPPENS HERE INTERNALLY..
wifi_ap_set_visible(true);
portTickType xLastWakeTime = xTaskGetTickCount();
for(;;) {
//vTaskDelay(5000 / portTICK_RATE_MS);
vTaskDelayUntil(&xLastWakeTime, 5000 / portTICK_RATE_MS);
}
}
void init_smart_config() {
if(smartConfigTaskControllerHandle == NULL) {
//create smart config task
portBASE_TYPE xReturned;
xReturned = xTaskCreate(smart_config_task, (const signed char * const )"smart_config_task", 256, NULL, tskIDLE_PRIORITY + 1, &smartConfigTaskControllerHandle);
if( xReturned != pdPASS ) {
IOT_ERROR("Could not create smart_config task: %d", (int32)xReturned);
}
}
}
void user_init(void) {
uart_init();
#ifdef DEBUG
gdbstub_init();
initiMemoryDump();
#endif
init_wifi_controller();
}
Statistics: Posted by gustavomassa — Tue May 30, 2017 4:25 pm