|
| 1 | +/* |
| 2 | + ESP8266WiFiSTA-WPS.cpp - WiFi library for esp8266 |
| 3 | +
|
| 4 | + Copyright (c) 2014 Ivan Grokhotkov. All rights reserved. |
| 5 | + This file is part of the esp8266 core for Arduino environment. |
| 6 | +
|
| 7 | + This library is free software; you can redistribute it and/or |
| 8 | + modify it under the terms of the GNU Lesser General Public |
| 9 | + License as published by the Free Software Foundation; either |
| 10 | + version 2.1 of the License, or (at your option) any later version. |
| 11 | +
|
| 12 | + This library is distributed in the hope that it will be useful, |
| 13 | + but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 | + Lesser General Public License for more details. |
| 16 | +
|
| 17 | + You should have received a copy of the GNU Lesser General Public |
| 18 | + License along with this library; if not, write to the Free Software |
| 19 | + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 20 | +
|
| 21 | + Reworked on 28 Dec 2015 by Markus Sattler |
| 22 | +
|
| 23 | + */ |
| 24 | + |
| 25 | + |
| 26 | +#include "ESP8266WiFi.h" |
| 27 | +#include "ESP8266WiFiGeneric.h" |
| 28 | +#include "ESP8266WiFiSTA.h" |
| 29 | +#include "coredecls.h" // disable_extra4k_at_link_time() |
| 30 | + |
| 31 | +static void wifi_wps_status_cb(wps_cb_status status); |
| 32 | + |
| 33 | +/** |
| 34 | + * WPS config |
| 35 | + * so far only WPS_TYPE_PBC is supported (SDK 1.2.0) |
| 36 | + * @return ok |
| 37 | + */ |
| 38 | +bool ESP8266WiFiSTAClass::beginWPSConfig(void) { |
| 39 | + |
| 40 | + // SYS ram is used by WPS, let's configure user stack inside user's HEAP |
| 41 | + disable_extra4k_at_link_time(); |
| 42 | + |
| 43 | + if(!WiFi.enableSTA(true)) { |
| 44 | + // enable STA failed |
| 45 | + return false; |
| 46 | + } |
| 47 | + |
| 48 | + disconnect(); |
| 49 | + |
| 50 | + DEBUGV("wps begin\n"); |
| 51 | + |
| 52 | + if(!wifi_wps_disable()) { |
| 53 | + DEBUGV("wps disable failed\n"); |
| 54 | + return false; |
| 55 | + } |
| 56 | + |
| 57 | + // so far only WPS_TYPE_PBC is supported (SDK 1.2.0) |
| 58 | + if(!wifi_wps_enable(WPS_TYPE_PBC)) { |
| 59 | + DEBUGV("wps enable failed\n"); |
| 60 | + return false; |
| 61 | + } |
| 62 | + |
| 63 | + if(!wifi_set_wps_cb((wps_st_cb_t) &wifi_wps_status_cb)) { |
| 64 | + DEBUGV("wps cb failed\n"); |
| 65 | + return false; |
| 66 | + } |
| 67 | + |
| 68 | + if(!wifi_wps_start()) { |
| 69 | + DEBUGV("wps start failed\n"); |
| 70 | + return false; |
| 71 | + } |
| 72 | + |
| 73 | + esp_yield(); |
| 74 | + // will return here when wifi_wps_status_cb fires |
| 75 | + |
| 76 | + return true; |
| 77 | +} |
| 78 | + |
| 79 | +/** |
| 80 | + * WPS callback |
| 81 | + * @param status wps_cb_status |
| 82 | + */ |
| 83 | +void wifi_wps_status_cb(wps_cb_status status) { |
| 84 | + DEBUGV("wps cb status: %d\r\n", status); |
| 85 | + switch(status) { |
| 86 | + case WPS_CB_ST_SUCCESS: |
| 87 | + if(!wifi_wps_disable()) { |
| 88 | + DEBUGV("wps disable failed\n"); |
| 89 | + } |
| 90 | + wifi_station_connect(); |
| 91 | + break; |
| 92 | + case WPS_CB_ST_FAILED: |
| 93 | + DEBUGV("wps FAILED\n"); |
| 94 | + break; |
| 95 | + case WPS_CB_ST_TIMEOUT: |
| 96 | + DEBUGV("wps TIMEOUT\n"); |
| 97 | + break; |
| 98 | + case WPS_CB_ST_WEP: |
| 99 | + DEBUGV("wps WEP\n"); |
| 100 | + break; |
| 101 | + case WPS_CB_ST_UNK: |
| 102 | + DEBUGV("wps UNKNOWN\n"); |
| 103 | + if(!wifi_wps_disable()) { |
| 104 | + DEBUGV("wps disable failed\n"); |
| 105 | + } |
| 106 | + break; |
| 107 | + } |
| 108 | + // TODO user function to get status |
| 109 | + |
| 110 | + esp_schedule(); // resume the beginWPSConfig function |
| 111 | +} |
0 commit comments