this is using the mbed os version 5-13-1
source/WiFiManager.cpp@80:e8f0e92e3ac9, 2019-03-21 (annotated)
- Committer:
- ocomeni
- Date:
- Thu Mar 21 22:17:28 2019 +0000
- Revision:
- 80:e8f0e92e3ac9
- Parent:
- 79:a2187bbfa407
- Child:
- 81:637a87eb8170
implemented queue and memory pools for command and data passing between ATCmdManager and WiFiManager classes
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| ocomeni | 78:07bb86e3ce14 | 1 | #include "WiFiManager.h" |
| ocomeni | 78:07bb86e3ce14 | 2 | #include "common_config.h" |
| ocomeni | 78:07bb86e3ce14 | 3 | |
| ocomeni | 78:07bb86e3ce14 | 4 | |
| ocomeni | 79:a2187bbfa407 | 5 | WiFiManager::WiFiManager(wifi_config_t wifi_config, WiFiInterface *wifi, |
| ocomeni | 80:e8f0e92e3ac9 | 6 | MemoryPool<wifi_cmd_message_t, 16> *aT2WiFimPool, |
| ocomeni | 80:e8f0e92e3ac9 | 7 | Queue<wifi_cmd_message_t, 16> *aT2WiFiCmdQueue, |
| ocomeni | 80:e8f0e92e3ac9 | 8 | MemoryPool<wifi_cmd_message_t, 16> *wiFi2ATmPool, |
| ocomeni | 80:e8f0e92e3ac9 | 9 | Queue<wifi_cmd_message_t, 16> *wiFi2ATCmdQueue, |
| ocomeni | 80:e8f0e92e3ac9 | 10 | MemoryPool<wifi_data_msg_t, 4> *aT2WiFiDatamPool, |
| ocomeni | 80:e8f0e92e3ac9 | 11 | Queue<wifi_data_msg_t, 4> *aT2WiFiDataQueue, |
| ocomeni | 80:e8f0e92e3ac9 | 12 | MemoryPool<wifi_data_msg_t, 4> *wiFi2ATDatamPool, |
| ocomeni | 80:e8f0e92e3ac9 | 13 | Queue<wifi_data_msg_t, 4> *wiFi2ATDataQueue) |
| ocomeni | 78:07bb86e3ce14 | 14 | : |
| ocomeni | 78:07bb86e3ce14 | 15 | wifi_config(wifi_config), |
| ocomeni | 79:a2187bbfa407 | 16 | network(wifi), |
| ocomeni | 79:a2187bbfa407 | 17 | _aT2WiFimPool(aT2WiFimPool), |
| ocomeni | 79:a2187bbfa407 | 18 | _aT2WiFiCmdQueue(aT2WiFiCmdQueue) |
| ocomeni | 78:07bb86e3ce14 | 19 | |
| ocomeni | 78:07bb86e3ce14 | 20 | { |
| ocomeni | 79:a2187bbfa407 | 21 | lastScanCount = 0; |
| ocomeni | 79:a2187bbfa407 | 22 | wifiCmd = WIFI_CMD_NONE; |
| ocomeni | 78:07bb86e3ce14 | 23 | } |
| ocomeni | 78:07bb86e3ce14 | 24 | |
| ocomeni | 78:07bb86e3ce14 | 25 | WiFiManager::~WiFiManager() |
| ocomeni | 78:07bb86e3ce14 | 26 | { |
| ocomeni | 78:07bb86e3ce14 | 27 | } |
| ocomeni | 79:a2187bbfa407 | 28 | |
| ocomeni | 79:a2187bbfa407 | 29 | |
| ocomeni | 79:a2187bbfa407 | 30 | |
| ocomeni | 79:a2187bbfa407 | 31 | void WiFiManager::runMain(){ |
| ocomeni | 79:a2187bbfa407 | 32 | while(true){ |
| ocomeni | 79:a2187bbfa407 | 33 | dequeueWiFiCommands(); |
| ocomeni | 79:a2187bbfa407 | 34 | switch(wifiCmd){ |
| ocomeni | 79:a2187bbfa407 | 35 | case WIFI_CMD_NONE: |
| ocomeni | 79:a2187bbfa407 | 36 | // IDLE STATE |
| ocomeni | 79:a2187bbfa407 | 37 | break; |
| ocomeni | 79:a2187bbfa407 | 38 | case WIFI_CMD_SCAN: |
| ocomeni | 79:a2187bbfa407 | 39 | nsapi_error_t error; |
| ocomeni | 79:a2187bbfa407 | 40 | error = scanNetworks(); |
| ocomeni | 79:a2187bbfa407 | 41 | wifiCmd = WIFI_CMD_NONE; |
| ocomeni | 79:a2187bbfa407 | 42 | break; |
| ocomeni | 79:a2187bbfa407 | 43 | case WIFI_CMD_CONNECT: |
| ocomeni | 79:a2187bbfa407 | 44 | break; |
| ocomeni | 79:a2187bbfa407 | 45 | case WIFI_CMD_DISCONNECT: |
| ocomeni | 79:a2187bbfa407 | 46 | break; |
| ocomeni | 79:a2187bbfa407 | 47 | case WIFI_CMD_SEND_HTTPS_REQ: |
| ocomeni | 79:a2187bbfa407 | 48 | break; |
| ocomeni | 79:a2187bbfa407 | 49 | case WIFI_CMD_SEND_HTTP_REQ: |
| ocomeni | 79:a2187bbfa407 | 50 | break; |
| ocomeni | 79:a2187bbfa407 | 51 | default: |
| ocomeni | 79:a2187bbfa407 | 52 | break; |
| ocomeni | 79:a2187bbfa407 | 53 | } |
| ocomeni | 79:a2187bbfa407 | 54 | wait_ms(100); // |
| ocomeni | 79:a2187bbfa407 | 55 | } |
| ocomeni | 79:a2187bbfa407 | 56 | |
| ocomeni | 78:07bb86e3ce14 | 57 | } |
| ocomeni | 79:a2187bbfa407 | 58 | |
| ocomeni | 79:a2187bbfa407 | 59 | bool WiFiManager::dequeueWiFiCommands(){ |
| ocomeni | 79:a2187bbfa407 | 60 | osEvent evt = _aT2WiFiCmdQueue->get(); |
| ocomeni | 79:a2187bbfa407 | 61 | if(evt.status == osEventMessage){ |
| ocomeni | 79:a2187bbfa407 | 62 | wifi_cmd_message_t *cmd = (wifi_cmd_message_t*)evt.value.p; |
| ocomeni | 79:a2187bbfa407 | 63 | setNextCommand(cmd->wifi_cmd); |
| ocomeni | 79:a2187bbfa407 | 64 | _aT2WiFimPool->free(cmd); |
| ocomeni | 79:a2187bbfa407 | 65 | } |
| ocomeni | 79:a2187bbfa407 | 66 | return true; |
| ocomeni | 79:a2187bbfa407 | 67 | } |
| ocomeni | 79:a2187bbfa407 | 68 | |
| ocomeni | 79:a2187bbfa407 | 69 | |
| ocomeni | 79:a2187bbfa407 | 70 | bool WiFiManager::setNextCommand(wifi_cmd_t cmd) |
| ocomeni | 78:07bb86e3ce14 | 71 | { |
| ocomeni | 79:a2187bbfa407 | 72 | printf("\n [WIFI-MAN] About to set next WiFi manager command \n"); |
| ocomeni | 79:a2187bbfa407 | 73 | if(wifiCmd == WIFI_CMD_NONE){ |
| ocomeni | 79:a2187bbfa407 | 74 | wifiCmd = cmd; |
| ocomeni | 79:a2187bbfa407 | 75 | return true; // success |
| ocomeni | 79:a2187bbfa407 | 76 | } |
| ocomeni | 79:a2187bbfa407 | 77 | return false; // wiFiManager busy |
| ocomeni | 78:07bb86e3ce14 | 78 | } |
| ocomeni | 79:a2187bbfa407 | 79 | |
| ocomeni | 79:a2187bbfa407 | 80 | |
| ocomeni | 79:a2187bbfa407 | 81 | nsapi_size_or_error_t WiFiManager::scanNetworks() |
| ocomeni | 79:a2187bbfa407 | 82 | { |
| ocomeni | 79:a2187bbfa407 | 83 | nsapi_error_t error; |
| ocomeni | 79:a2187bbfa407 | 84 | printf("\n [WIFI-MAN] About to start scan for WiFi networks\n"); |
| ocomeni | 79:a2187bbfa407 | 85 | lastScanCount = network->scan(NULL, 0); |
| ocomeni | 79:a2187bbfa407 | 86 | printf("\n [WIFI-MAN] Scan for WiFi networks completed - \n"); |
| ocomeni | 79:a2187bbfa407 | 87 | return lastScanCount; |
| ocomeni | 79:a2187bbfa407 | 88 | } |
| ocomeni | 79:a2187bbfa407 | 89 | |
| ocomeni | 79:a2187bbfa407 | 90 | |
| ocomeni | 79:a2187bbfa407 | 91 | nsapi_size_or_error_t WiFiManager::getAvailableAPs(WiFiAccessPoint * res, |
| ocomeni | 79:a2187bbfa407 | 92 | nsapi_size_t count) |
| ocomeni | 79:a2187bbfa407 | 93 | { |
| ocomeni | 79:a2187bbfa407 | 94 | count = network->scan(res, count); |
| ocomeni | 79:a2187bbfa407 | 95 | return count; |
| ocomeni | 79:a2187bbfa407 | 96 | } |
| ocomeni | 79:a2187bbfa407 | 97 | |
| ocomeni | 79:a2187bbfa407 | 98 | |
| ocomeni | 78:07bb86e3ce14 | 99 | void WiFiManager::set_WIFI_SSID(char * wifi_ssid) |
| ocomeni | 78:07bb86e3ce14 | 100 | { |
| ocomeni | 78:07bb86e3ce14 | 101 | strcpy(wifi_config.ssid, wifi_ssid); |
| ocomeni | 78:07bb86e3ce14 | 102 | } |
| ocomeni | 79:a2187bbfa407 | 103 | |
| ocomeni | 79:a2187bbfa407 | 104 | |
| ocomeni | 78:07bb86e3ce14 | 105 | void WiFiManager::set_WIFI_PASSWORD(char * wifi_pass) |
| ocomeni | 78:07bb86e3ce14 | 106 | { |
| ocomeni | 78:07bb86e3ce14 | 107 | strcpy(wifi_config.pass, wifi_pass); |
| ocomeni | 78:07bb86e3ce14 | 108 | } |
| ocomeni | 79:a2187bbfa407 | 109 | |
| ocomeni | 79:a2187bbfa407 | 110 | |
| ocomeni | 78:07bb86e3ce14 | 111 | void WiFiManager::set_WIFI_SECURITY(nsapi_security_t wifi_security) |
| ocomeni | 78:07bb86e3ce14 | 112 | { |
| ocomeni | 78:07bb86e3ce14 | 113 | wifi_config.security = wifi_security; |
| ocomeni | 78:07bb86e3ce14 | 114 | } |
| ocomeni | 79:a2187bbfa407 | 115 | |
| ocomeni | 79:a2187bbfa407 | 116 | |
| ocomeni | 79:a2187bbfa407 | 117 | // NSAPI_STATUS_LOCAL_UP = 0, /*!< local IP address set */ |
| ocomeni | 79:a2187bbfa407 | 118 | // NSAPI_STATUS_GLOBAL_UP = 1, /*!< global IP address set */ |
| ocomeni | 79:a2187bbfa407 | 119 | // NSAPI_STATUS_DISCONNECTED = 2, /*!< no connection to network */ |
| ocomeni | 79:a2187bbfa407 | 120 | // NSAPI_STATUS_CONNECTING = 3, /*!< connecting to network */ |
| ocomeni | 79:a2187bbfa407 | 121 | // NSAPI_STATUS_ERROR_UNSUPPORTED = NSAPI_ERROR_UNSUPPORTED |
| ocomeni | 79:a2187bbfa407 | 122 | |
| ocomeni | 79:a2187bbfa407 | 123 | nsapi_error_t WiFiManager::connect() |
| ocomeni | 79:a2187bbfa407 | 124 | { |
| ocomeni | 79:a2187bbfa407 | 125 | nsapi_error_t error; |
| ocomeni | 79:a2187bbfa407 | 126 | printf("\n [WIFI-MAN] About to connect to WiFi networks\n"); |
| ocomeni | 79:a2187bbfa407 | 127 | error = network->set_blocking(false); |
| ocomeni | 79:a2187bbfa407 | 128 | if(error) |
| ocomeni | 79:a2187bbfa407 | 129 | { |
| ocomeni | 79:a2187bbfa407 | 130 | printf("\n [WIFI-MAN] Could not set non-blocking mode for Wifi -- aborting!! - \n"); |
| ocomeni | 79:a2187bbfa407 | 131 | return error; |
| ocomeni | 79:a2187bbfa407 | 132 | } |
| ocomeni | 79:a2187bbfa407 | 133 | error = network->connect(wifi_config.ssid, |
| ocomeni | 79:a2187bbfa407 | 134 | wifi_config.pass, |
| ocomeni | 79:a2187bbfa407 | 135 | wifi_config.security); |
| ocomeni | 79:a2187bbfa407 | 136 | if(error) |
| ocomeni | 79:a2187bbfa407 | 137 | { |
| ocomeni | 79:a2187bbfa407 | 138 | printf("\n [WIFI-MAN] Could not connect to Wifi -- aborting!! - \n"); |
| ocomeni | 79:a2187bbfa407 | 139 | return error; |
| ocomeni | 79:a2187bbfa407 | 140 | } |
| ocomeni | 79:a2187bbfa407 | 141 | nsapi_connection_status_t conn_status = NSAPI_STATUS_CONNECTING; |
| ocomeni | 79:a2187bbfa407 | 142 | int loopCount = 0; |
| ocomeni | 79:a2187bbfa407 | 143 | while(conn_status == NSAPI_STATUS_CONNECTING){ |
| ocomeni | 79:a2187bbfa407 | 144 | loopCount++; |
| ocomeni | 79:a2187bbfa407 | 145 | conn_status = network->get_connection_status(); |
| ocomeni | 79:a2187bbfa407 | 146 | printf("\n [WIFI-MAN] Waiting for WiFi network connect to complete asynchronously [status = %d] - %d\n", conn_status, loopCount); |
| ocomeni | 79:a2187bbfa407 | 147 | wait(0.1); |
| ocomeni | 79:a2187bbfa407 | 148 | } |
| ocomeni | 79:a2187bbfa407 | 149 | if(conn_status < 0) |
| ocomeni | 79:a2187bbfa407 | 150 | { |
| ocomeni | 79:a2187bbfa407 | 151 | printf("\n [WIFI-MAN] Error connecting to Wifi -- %d!! - \n", conn_status); |
| ocomeni | 79:a2187bbfa407 | 152 | return conn_status; |
| ocomeni | 79:a2187bbfa407 | 153 | } |
| ocomeni | 79:a2187bbfa407 | 154 | printf("\n [WIFI-MAN] Connect to WiFi network completed - \n"); |
| ocomeni | 79:a2187bbfa407 | 155 | return conn_status; |
| ocomeni | 79:a2187bbfa407 | 156 | } |
| ocomeni | 79:a2187bbfa407 | 157 | |
| ocomeni | 79:a2187bbfa407 | 158 | |
| ocomeni | 79:a2187bbfa407 | 159 | nsapi_error_t WiFiManager::disconnect() |
| ocomeni | 78:07bb86e3ce14 | 160 | { |
| ocomeni | 79:a2187bbfa407 | 161 | nsapi_error_t error; |
| ocomeni | 79:a2187bbfa407 | 162 | error = network->disconnect(); |
| ocomeni | 79:a2187bbfa407 | 163 | return error; |
| ocomeni | 78:07bb86e3ce14 | 164 | } |
| ocomeni | 79:a2187bbfa407 | 165 | |
| ocomeni | 79:a2187bbfa407 | 166 | |
| ocomeni | 79:a2187bbfa407 | 167 | /* |
| ocomeni | 79:a2187bbfa407 | 168 | |
| ocomeni | 79:a2187bbfa407 | 169 | HttpsRequest(NetworkInterface* network, |
| ocomeni | 79:a2187bbfa407 | 170 | const char* ssl_ca_pem, |
| ocomeni | 79:a2187bbfa407 | 171 | http_method method, |
| ocomeni | 79:a2187bbfa407 | 172 | const char* url, |
| ocomeni | 79:a2187bbfa407 | 173 | Callback<void(const char *at, uint32_t length)> body_callback = 0) |
| ocomeni | 79:a2187bbfa407 | 174 | HttpsRequest* get_req = new HttpsRequest(network, SSL_CA_PEM, HTTP_GET, "https://os.mbed.com/media/uploads/mbed_official/hello.txt", &dump_chunked_response); |
| ocomeni | 79:a2187bbfa407 | 175 | HttpRequest(NetworkInterface* network, http_method method, const char* url, Callback<void(const char *at, uint32_t length)> bodyCallback = 0) |
| ocomeni | 79:a2187bbfa407 | 176 | post_req->set_header("Content-Type", "application/json"); |
| ocomeni | 79:a2187bbfa407 | 177 | HttpResponse* get_res = get_req->send(); |
| ocomeni | 79:a2187bbfa407 | 178 | |
| ocomeni | 79:a2187bbfa407 | 179 | const char body[] = "{\"hello\":\"world\"}"; |
| ocomeni | 79:a2187bbfa407 | 180 | |
| ocomeni | 79:a2187bbfa407 | 181 | HttpResponse* post_res = post_req->send(body, strlen(body)); |
| ocomeni | 79:a2187bbfa407 | 182 | |
| ocomeni | 79:a2187bbfa407 | 183 | |
| ocomeni | 79:a2187bbfa407 | 184 | */ |
| ocomeni | 79:a2187bbfa407 | 185 | |
| ocomeni | 79:a2187bbfa407 | 186 | void WiFiManager::createHttpsRequest(http_method method, |
| ocomeni | 79:a2187bbfa407 | 187 | const char* url, |
| ocomeni | 79:a2187bbfa407 | 188 | Callback<void(const char *at, uint32_t length)> body_callback) |
| ocomeni | 79:a2187bbfa407 | 189 | { |
| ocomeni | 79:a2187bbfa407 | 190 | https_request = new HttpsRequest(network, SSL_CA_PEM, method, url, body_callback); |
| ocomeni | 79:a2187bbfa407 | 191 | } |
| ocomeni | 79:a2187bbfa407 | 192 | |
| ocomeni | 79:a2187bbfa407 | 193 | void WiFiManager::createHttpRequest(http_method method, |
| ocomeni | 79:a2187bbfa407 | 194 | const char* url, |
| ocomeni | 79:a2187bbfa407 | 195 | Callback<void(const char *at, uint32_t length)> body_callback) |
| ocomeni | 79:a2187bbfa407 | 196 | { |
| ocomeni | 79:a2187bbfa407 | 197 | http_request = new HttpRequest(network, method, url, body_callback);; |
| ocomeni | 79:a2187bbfa407 | 198 | } |
| ocomeni | 79:a2187bbfa407 | 199 | |
| ocomeni | 79:a2187bbfa407 | 200 | void WiFiManager::setHttpHeader(string key, string value) |
| ocomeni | 79:a2187bbfa407 | 201 | { |
| ocomeni | 79:a2187bbfa407 | 202 | http_request->set_header(key, value); |
| ocomeni | 79:a2187bbfa407 | 203 | } |
| ocomeni | 79:a2187bbfa407 | 204 | |
| ocomeni | 79:a2187bbfa407 | 205 | void WiFiManager::setHttpsHeader(string key, string value) |
| ocomeni | 79:a2187bbfa407 | 206 | { |
| ocomeni | 79:a2187bbfa407 | 207 | https_request->set_header(key, value); |
| ocomeni | 79:a2187bbfa407 | 208 | } |
| ocomeni | 79:a2187bbfa407 | 209 | |
| ocomeni | 79:a2187bbfa407 | 210 | void WiFiManager::sendHttpsRequest(const char * body, int bodyLen) |
| ocomeni | 78:07bb86e3ce14 | 211 | { |
| ocomeni | 78:07bb86e3ce14 | 212 | } |
| ocomeni | 79:a2187bbfa407 | 213 | |
| ocomeni | 79:a2187bbfa407 | 214 | void WiFiManager::sendHttpRequest(const char * body, int bodyLen) |
| ocomeni | 78:07bb86e3ce14 | 215 | { |
| ocomeni | 78:07bb86e3ce14 | 216 | } |
| ocomeni | 79:a2187bbfa407 | 217 |