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