|
@@ -1,485 +0,0 @@
|
|
|
-
|
|
|
- * 2016 IdeasX v0.3.1 Module Firmware
|
|
|
- *
|
|
|
- * File Name: wifi.c
|
|
|
- * Author: Tyler Berezowsky
|
|
|
- * Description: This file is going to need alot of help by a C wizard, but it currently works...I think.
|
|
|
- *
|
|
|
- * 2016/8/8, v1.0 created this file
|
|
|
-*******************************************************************************/
|
|
|
-#include "util/wifi.h"
|
|
|
-#include "esp_common.h"
|
|
|
-
|
|
|
-#define WIFI_TIMEOUT 30*1000
|
|
|
-#define WIFI_RECONNECT_LIMIT 5
|
|
|
-
|
|
|
-MQTT_Client mqttClient;
|
|
|
-
|
|
|
-static struct station_config stationConf;
|
|
|
-static WIFI_PROCESS_FLAGS wifi_process_flags;
|
|
|
-static ETSTimer wifi_process_timer;
|
|
|
-static struct bss_info *bss_link, *head_bss_link;
|
|
|
-
|
|
|
-static void ICACHE_FLASH_ATTR wifi_process(void);
|
|
|
-static void ICACHE_FLASH_ATTR wifi_process_timer_cb(void);
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
- * FunctionName : destory_linked_list
|
|
|
- * Description :
|
|
|
- * Parameters :
|
|
|
- * Returns :
|
|
|
-*******************************************************************************/
|
|
|
-static bool ICACHE_FLASH_ATTR destory_linked_list(void)
|
|
|
-{
|
|
|
- if(head_bss_link != NULL)
|
|
|
- {
|
|
|
- while(head_bss_link != NULL)
|
|
|
- {
|
|
|
- bss_link = head_bss_link;
|
|
|
- head_bss_link = head_bss_link->next.stqe_next;
|
|
|
- os_free(bss_link);
|
|
|
- }
|
|
|
- return TRUE;
|
|
|
- }
|
|
|
- else
|
|
|
- {
|
|
|
- return FALSE;
|
|
|
- }
|
|
|
-}
|
|
|
-
|
|
|
- * FunctionName : copy_linked_list
|
|
|
- * Description :
|
|
|
- * Parameters :
|
|
|
- * Returns :
|
|
|
-*******************************************************************************/
|
|
|
-static struct bss_info* ICACHE_FLASH_ATTR copy_linked_list(struct bss_info *ptr_original_node)
|
|
|
-{
|
|
|
- struct bss_info *ptr_new_head, *ptr_new_node;
|
|
|
-
|
|
|
- if(ptr_original_node != NULL)
|
|
|
- {
|
|
|
- ptr_new_head = (struct bss_info *)os_malloc(sizeof(struct bss_info));
|
|
|
- if (ptr_new_head == NULL)
|
|
|
- {
|
|
|
- os_printf("WIFI PROCESS: No RAM!\r\n");
|
|
|
- return NULL;
|
|
|
- }
|
|
|
- }
|
|
|
- else
|
|
|
- return NULL;
|
|
|
-
|
|
|
- os_memcpy(ptr_new_head, ptr_original_node, sizeof(struct bss_info));
|
|
|
- ptr_original_node = ptr_original_node->next.stqe_next;
|
|
|
-
|
|
|
- ptr_new_node = ptr_new_head;
|
|
|
-
|
|
|
- while(ptr_original_node != NULL)
|
|
|
- {
|
|
|
- ptr_new_node->next.stqe_next = (struct bss_info *)os_malloc(sizeof(struct bss_info));
|
|
|
- ptr_new_node = ptr_new_node->next.stqe_next;
|
|
|
- os_memcpy(ptr_new_node, ptr_original_node, sizeof(struct bss_info));
|
|
|
- if (ptr_new_node == NULL)
|
|
|
- {
|
|
|
- os_printf("WIFI PROCESS: No RAM!\r\n");
|
|
|
- while(ptr_new_head != NULL)
|
|
|
- {
|
|
|
- ptr_new_node = ptr_new_head;
|
|
|
- ptr_new_head = ptr_new_head->next.stqe_next;
|
|
|
- os_free(ptr_new_node);
|
|
|
- }
|
|
|
- os_printf("WIFI PROCESS: Cleaned up list\r\n");
|
|
|
- return NULL;
|
|
|
- }
|
|
|
- ptr_original_node = ptr_original_node->next.stqe_next;
|
|
|
-
|
|
|
- }
|
|
|
- return ptr_new_head;
|
|
|
-}
|
|
|
-
|
|
|
- * FunctionName : wifi_set_station
|
|
|
- * Description :
|
|
|
- * Parameters :
|
|
|
- * Returns :
|
|
|
-*******************************************************************************/
|
|
|
-void ICACHE_FLASH_ATTR wifi_set_station(uint8_t* ssid, uint8_t* pass)
|
|
|
-{
|
|
|
- bss_link = NULL;
|
|
|
-
|
|
|
- os_sprintf(stationConf.ssid, "%s", ssid);
|
|
|
- os_sprintf(stationConf.password, "%s", pass);
|
|
|
-
|
|
|
- wifi_station_set_config_current(&stationConf);
|
|
|
- wifi_station_set_auto_connect(TRUE);
|
|
|
- wifi_station_connect();
|
|
|
-}
|
|
|
-
|
|
|
- * FunctionName : disable_wifi_reconnect
|
|
|
- * Description :
|
|
|
- * Parameters :
|
|
|
- * Returns :
|
|
|
-*******************************************************************************/
|
|
|
-void ICACHE_FLASH_ATTR disable_wifi_reconnect(void)
|
|
|
-{
|
|
|
- wifi_station_disconnect();
|
|
|
- wifi_station_set_auto_connect(FALSE);
|
|
|
-}
|
|
|
-
|
|
|
- * FunctionName : show_wifi_config
|
|
|
- * Description : print current wifi configurations
|
|
|
- * Parameters :
|
|
|
- * Returns :
|
|
|
-*******************************************************************************/
|
|
|
-void ICACHE_FLASH_ATTR show_wifi_config(void)
|
|
|
-{
|
|
|
- uint8_t i, j;
|
|
|
- j = sysCfg.registered_stations;
|
|
|
-
|
|
|
- os_printf("Number of APs stored: %d\r\n", j);
|
|
|
- for (i=0;i<j;i++)
|
|
|
- os_printf("AP: %d\n\tSSID: %s\n\tPASS: %s\n", i, sysCfg.sta_ssid[i], sysCfg.sta_pwd[i]);
|
|
|
-
|
|
|
- current station
|
|
|
- */
|
|
|
- i = sysCfg.current_station;
|
|
|
- j = wifi_station_get_connect_status();
|
|
|
- os_printf("Current AP: %d\r\nWiFi Status %d\r\n", i, j);
|
|
|
-}
|
|
|
-
|
|
|
- * FunctionName : wifi_process_timer_cb
|
|
|
- * Description :
|
|
|
- * Parameters :
|
|
|
- * Returns :
|
|
|
-*******************************************************************************/
|
|
|
-static void ICACHE_FLASH_ATTR wifi_process_timer_cb()
|
|
|
-{
|
|
|
- os_printf("WIFI PROCESS: Wi-Fi connection timed out.\r\n");
|
|
|
- wifi_process_flags.wifi_flag = FALSE;
|
|
|
-
|
|
|
-
|
|
|
- wifi_station_set_auto_connect(FALSE);
|
|
|
- wifi_station_disconnect();
|
|
|
- wifi_process();
|
|
|
-}
|
|
|
-
|
|
|
- * FunctionName : wifi_process
|
|
|
- * Description : Looks for AP stored in bss_link
|
|
|
- * Parameters :
|
|
|
- * Returns :
|
|
|
-*******************************************************************************/
|
|
|
-static void ICACHE_FLASH_ATTR wifi_process(void)
|
|
|
-{
|
|
|
- uint8_t ssid[33];
|
|
|
- uint8_t i;
|
|
|
-
|
|
|
- os_timer_disarm(&wifi_process_timer);
|
|
|
-
|
|
|
- if ((wifi_process_flags.wifi_flag == FALSE) && (bss_link != NULL))
|
|
|
- {
|
|
|
- while(bss_link != NULL)
|
|
|
- {
|
|
|
-
|
|
|
-
|
|
|
- os_memset(ssid, 0, 33);
|
|
|
- if (os_strlen(bss_link->ssid) <= 32)
|
|
|
- {
|
|
|
- os_memcpy(ssid, bss_link->ssid, os_strlen(bss_link->ssid));
|
|
|
- }
|
|
|
- else
|
|
|
- {
|
|
|
- os_memcpy(ssid, bss_link->ssid, 32);
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- for(i=0;i<sysCfg.registered_stations;i++)
|
|
|
- {
|
|
|
-
|
|
|
- if (!strcmp(ssid, sysCfg.sta_ssid[i]))
|
|
|
- {
|
|
|
- wifi_process_flags.wifi_flag = TRUE;
|
|
|
- wifi_process_flags.wifi_count = 0;
|
|
|
-
|
|
|
- os_sprintf(stationConf.ssid, "%s", sysCfg.sta_ssid[i]);
|
|
|
- os_sprintf(stationConf.password, "%s", sysCfg.sta_pwd[i]);
|
|
|
- sysCfg.current_station = i;
|
|
|
- wifi_station_set_config_current(&stationConf);
|
|
|
- wifi_station_set_auto_connect(TRUE);
|
|
|
- wifi_station_connect();
|
|
|
-
|
|
|
- bss_link = bss_link->next.stqe_next;
|
|
|
-
|
|
|
-
|
|
|
- os_timer_setfn(&wifi_process_timer, (os_timer_func_t *)wifi_process_timer_cb, NULL);
|
|
|
- os_timer_arm(&wifi_process_timer, WIFI_TIMEOUT, 0);
|
|
|
- break;
|
|
|
- }
|
|
|
-
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- if (wifi_process_flags.wifi_flag == TRUE)
|
|
|
- {
|
|
|
- break;
|
|
|
- }
|
|
|
- else
|
|
|
- {
|
|
|
- bss_link = bss_link->next.stqe_next;
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- }
|
|
|
- if ((wifi_process_flags.wifi_flag == FALSE) && (bss_link == NULL))
|
|
|
- {
|
|
|
-
|
|
|
- sysCfg.module_state.searching = FALSE;
|
|
|
- sysCfg.module_state.asleep = FALSE;
|
|
|
- destory_linked_list();
|
|
|
- os_printf("WIFI PROCESS: Station list removed\r\n");
|
|
|
- os_printf("WIFI PROCESS: No Wi-Fi APs available.\r\n");
|
|
|
-
|
|
|
-
|
|
|
- if (lsm6ds3_read_motion())
|
|
|
- start_wifi_process();
|
|
|
- else
|
|
|
- {
|
|
|
- GPIO_REG_WRITE(GPIO_STATUS_W1TC_ADDRESS, 0xFFFFFFFF);
|
|
|
- sysCfg.module_state.asleep = TRUE;
|
|
|
- ETS_GPIO_INTR_ENABLE();
|
|
|
-
|
|
|
-
|
|
|
- * The ESP8266 should be placed into light sleep or modem sleep here
|
|
|
-
|
|
|
- */
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
-}
|
|
|
-
|
|
|
- * FunctionName : wifi_handle_event_cb
|
|
|
- * Description : callback function for wifi events.
|
|
|
- * Parameters :
|
|
|
- * Returns :
|
|
|
-*******************************************************************************/
|
|
|
-static void ICACHE_FLASH_ATTR wifi_handle_event_cb(System_Event_t *evt)
|
|
|
-{
|
|
|
- switch (evt->event)
|
|
|
- {
|
|
|
- case EVENT_STAMODE_CONNECTED:
|
|
|
- {
|
|
|
- wifi_process_flags.wifi_count = 0;
|
|
|
-
|
|
|
- os_printf("WIFI PROCESS: connect to ssid %s, channel %d\n",
|
|
|
- evt->event_info.connected.ssid,
|
|
|
- evt->event_info.connected.channel);
|
|
|
- break;
|
|
|
- }
|
|
|
- case EVENT_STAMODE_DISCONNECTED:
|
|
|
- {
|
|
|
-
|
|
|
- There is an issue where if the module is connected to an AP and then loses it, it continously scans for the
|
|
|
- last access point it attempted to connect to.
|
|
|
- */
|
|
|
- sysCfg.module_state.connected = FALSE;
|
|
|
- sysCfg.module_state.searching = TRUE;
|
|
|
-
|
|
|
- The only way to cancel the auto-connect functionaity of the SDK's OS is to
|
|
|
- send the command wifi_station_disconnect(), this ALWAYS calles the wifi event
|
|
|
- handler. Checking for this flag lets us ignore it.
|
|
|
- */
|
|
|
- if (wifi_process_flags.wifi_flag == TRUE)
|
|
|
- {
|
|
|
- MQTT_Disconnect(&mqttClient);
|
|
|
- wifi_process_flags.wifi_count++;
|
|
|
-
|
|
|
- os_printf("WIFI PROCESS: disconnect from ssid %s, reason %d, attempt %d/%d\r\n",
|
|
|
- evt->event_info.disconnected.ssid,
|
|
|
- evt->event_info.disconnected.reason,
|
|
|
- wifi_process_flags.wifi_count,
|
|
|
- WIFI_RECONNECT_LIMIT);
|
|
|
-
|
|
|
- if (wifi_process_flags.wifi_count >= WIFI_RECONNECT_LIMIT)
|
|
|
- {
|
|
|
- if (wifi_process_flags.ip_flag == TRUE)
|
|
|
- {
|
|
|
- start_wifi_process();
|
|
|
- }
|
|
|
- else
|
|
|
- {
|
|
|
- wifi_process_flags.wifi_flag = FALSE;
|
|
|
- wifi_station_set_auto_connect(FALSE);
|
|
|
- wifi_station_disconnect();
|
|
|
- wifi_process();
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- break;
|
|
|
- }
|
|
|
- case EVENT_STAMODE_AUTHMODE_CHANGE:
|
|
|
- {
|
|
|
- os_printf("WIFI PROCESS: mode: %d -> %d\n",
|
|
|
- evt->event_info.auth_change.old_mode,
|
|
|
- evt->event_info.auth_change.new_mode);
|
|
|
- break;
|
|
|
- }
|
|
|
- case EVENT_STAMODE_GOT_IP:
|
|
|
- {
|
|
|
- os_timer_disarm(&wifi_process_timer);
|
|
|
- wifi_process_flags.ip_flag = TRUE;
|
|
|
-
|
|
|
- sysCfg.module_state.connected = TRUE;
|
|
|
- sysCfg.module_state.searching = FALSE;
|
|
|
-
|
|
|
- os_printf("WIFI PROCESS: ip:" IPSTR ",mask:" IPSTR ",gw:" IPSTR,
|
|
|
- IP2STR(&evt->event_info.got_ip.ip),
|
|
|
- IP2STR(&evt->event_info.got_ip.mask),
|
|
|
- IP2STR(&evt->event_info.got_ip.gw));
|
|
|
-
|
|
|
- destory_linked_list();
|
|
|
- os_printf("\r\nWIFI PROCESS: Station list removed\r\n");
|
|
|
-
|
|
|
- lsm6ds3_motion_disable();
|
|
|
- MQTT_Connect(&mqttClient);
|
|
|
-
|
|
|
- break;
|
|
|
- }
|
|
|
- default:
|
|
|
- {
|
|
|
- os_printf("WIFI PROCESS: Oh, crap. I have no idea what is going on,\r\n");
|
|
|
- break;
|
|
|
- }
|
|
|
- }
|
|
|
-}
|
|
|
-
|
|
|
- * FunctionName : wifi_station_scan_done
|
|
|
- * Description : Cb function of scan, organizes a linked list of stations
|
|
|
- from scan function. Make sure to free linked list when done.
|
|
|
- * Parameters :
|
|
|
- * Returns :
|
|
|
-*******************************************************************************/
|
|
|
-static void ICACHE_FLASH_ATTR wifi_station_scan_done(void *arg, STATUS status)
|
|
|
-{
|
|
|
-
|
|
|
- uint8_t ssid[33];
|
|
|
- uint8_t bssid[6];
|
|
|
-
|
|
|
- if (status == OK)
|
|
|
- {
|
|
|
- bss_link = (struct bss_info *)arg;
|
|
|
- struct bss_info *bss_link_next, *bss_link_curr, *bss_link_prev, *bss_link_head;
|
|
|
- uint32_t i,numAP,j;
|
|
|
-
|
|
|
- bss_link = copy_linked_list(bss_link);
|
|
|
-
|
|
|
- bss_link_head = bss_link;
|
|
|
-
|
|
|
-
|
|
|
- numAP=0;
|
|
|
- while(bss_link != NULL) {
|
|
|
- numAP++;
|
|
|
- bss_link = bss_link->next.stqe_next;
|
|
|
- }
|
|
|
- os_printf("Number of APs found: %d\r\n", numAP);
|
|
|
-
|
|
|
- bss_link = bss_link_head;
|
|
|
-
|
|
|
-
|
|
|
- for(i=0;i<numAP-1;i++)
|
|
|
- {
|
|
|
-
|
|
|
- bss_link_prev = NULL;
|
|
|
- bss_link_curr = bss_link;
|
|
|
- bss_link_next = bss_link->next.stqe_next;
|
|
|
-
|
|
|
- for(j=0; j<numAP-1-i; j++)
|
|
|
- {
|
|
|
- system_soft_wdt_feed();
|
|
|
- if (bss_link_next->rssi > bss_link_curr->rssi) {
|
|
|
- if (bss_link_prev == NULL) {
|
|
|
-
|
|
|
- bss_link_curr->next.stqe_next = bss_link_next->next.stqe_next;
|
|
|
- bss_link_next->next.stqe_next = bss_link_curr;
|
|
|
- bss_link = bss_link_next;
|
|
|
-
|
|
|
- bss_link_prev = bss_link_next;
|
|
|
- bss_link_next = bss_link_curr->next.stqe_next;
|
|
|
- } else {
|
|
|
-
|
|
|
- bss_link_prev->next.stqe_next = bss_link_next;
|
|
|
- bss_link_curr->next.stqe_next = bss_link_next->next.stqe_next;
|
|
|
- bss_link_next->next.stqe_next = bss_link_curr;
|
|
|
-
|
|
|
- bss_link_prev = bss_link_next;
|
|
|
- bss_link_next = bss_link_curr->next.stqe_next;
|
|
|
- }
|
|
|
- } else {
|
|
|
-
|
|
|
- bss_link_prev = bss_link_curr;
|
|
|
- bss_link_curr = bss_link_next;
|
|
|
- bss_link_next = bss_link_next->next.stqe_next;
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- bss_link_head = bss_link;
|
|
|
-
|
|
|
-
|
|
|
- while (bss_link != NULL) {
|
|
|
- os_memset(ssid, 0, 33);
|
|
|
- os_memset(bssid, 0, 6);
|
|
|
-
|
|
|
- if (os_strlen(bss_link->ssid) <= 32) {
|
|
|
- os_memcpy(ssid, bss_link->ssid, os_strlen(bss_link->ssid));
|
|
|
- } else {
|
|
|
- os_memcpy(ssid, bss_link->ssid, 32);
|
|
|
- }
|
|
|
-
|
|
|
- os_printf("WiFi Scan: (%d,\"%s\",%x, %d)\n", bss_link->authmode, ssid, bssid, bss_link->rssi);
|
|
|
- bss_link = bss_link->next.stqe_next;
|
|
|
- }
|
|
|
- bss_link = bss_link_head;
|
|
|
- head_bss_link = bss_link_head;
|
|
|
-
|
|
|
- os_printf("Activating event handler\r\n");
|
|
|
- wifi_set_event_handler_cb(wifi_handle_event_cb);
|
|
|
- os_printf("Analysis scan results\r\n");
|
|
|
- wifi_process();
|
|
|
- }
|
|
|
- else
|
|
|
- {
|
|
|
- sysCfg.module_state.searching = FALSE;
|
|
|
- os_printf("Scan Failed\r\n");
|
|
|
- GPIO_REG_WRITE(GPIO_STATUS_W1TC_ADDRESS, 0xFFFFFFFF);
|
|
|
- sysCfg.module_state.asleep = TRUE;
|
|
|
- ETS_GPIO_INTR_ENABLE();
|
|
|
-
|
|
|
- }
|
|
|
-}
|
|
|
-
|
|
|
- * FunctionName : start_wifi_process
|
|
|
- * Description : scans for available AP and attempts to connect to the AP with
|
|
|
- the highest RSSI.
|
|
|
- * Parameters : restart - resets wifi_process_count
|
|
|
- * Returns :
|
|
|
-*******************************************************************************/
|
|
|
-void ICACHE_FLASH_ATTR start_wifi_process(void)
|
|
|
-{
|
|
|
-
|
|
|
- ETS_GPIO_INTR_DISABLE();
|
|
|
- lsm6ds3_motion_enable();
|
|
|
-
|
|
|
- wifi_process_flags.process_count = 0;
|
|
|
- wifi_process_flags.wifi_flag = FALSE;
|
|
|
- wifi_process_flags.ip_flag = FALSE;
|
|
|
- wifi_process_flags.wifi_count = 0;
|
|
|
-
|
|
|
- sysCfg.module_state.searching = TRUE;
|
|
|
- sysCfg.module_state.connected = FALSE;
|
|
|
- sysCfg.module_state.broker = FALSE;
|
|
|
-
|
|
|
- wifi_station_set_auto_connect(FALSE);
|
|
|
- wifi_station_ap_number_set(0);
|
|
|
-
|
|
|
- os_printf("Start Wi-Fi Scan\r\n");
|
|
|
- wifi_set_opmode(STATION_MODE);
|
|
|
- wifi_station_scan(NULL, wifi_station_scan_done);
|
|
|
-}
|