Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
source/WiFiManager.h@122:62166886db5f, 2019-06-11 (annotated)
- Committer:
- ocomeni
- Date:
- Tue Jun 11 20:23:43 2019 +0000
- Branch:
- PassingRegression
- Revision:
- 122:62166886db5f
- Parent:
- 119:8d939a902333
- Child:
- 123:a49e9ffbaca6
added error handling for Cloud connection keep alive
Who changed what in which revision?
| User | Revision | Line number | New contents of line | 
|---|---|---|---|
| ocomeni | 78:07bb86e3ce14 | 1 | #ifndef __WIFI_MANAGER_H__ | 
| ocomeni | 78:07bb86e3ce14 | 2 | #define __WIFI_MANAGER_H__ | 
| ocomeni | 79:a2187bbfa407 | 3 | #ifndef __MBED_H__ | 
| ocomeni | 79:a2187bbfa407 | 4 | #define __MBED_H__ | 
| ocomeni | 79:a2187bbfa407 | 5 | #include "mbed.h" | 
| ocomeni | 79:a2187bbfa407 | 6 | #endif | 
| ocomeni | 79:a2187bbfa407 | 7 | |
| ocomeni | 79:a2187bbfa407 | 8 | #include "mbed_trace.h" | 
| ocomeni | 79:a2187bbfa407 | 9 | #include "https_request.h" | 
| ocomeni | 79:a2187bbfa407 | 10 | #include "http_request.h" | 
| ocomeni | 79:a2187bbfa407 | 11 | /* List of trusted root CA certificates | 
| ocomeni | 79:a2187bbfa407 | 12 | * currently two: GlobalSign, the CA for os.mbed.com and Let's Encrypt, the CA for httpbin.org | 
| ocomeni | 79:a2187bbfa407 | 13 | * | 
| ocomeni | 79:a2187bbfa407 | 14 | * To add more root certificates, just concatenate them. | 
| ocomeni | 79:a2187bbfa407 | 15 | */ | 
| ocomeni | 79:a2187bbfa407 | 16 | #include "https_certificates.h" | 
| ocomeni | 78:07bb86e3ce14 | 17 | #include "common_types.h" | 
| ocomeni | 119:8d939a902333 | 18 | //const char HELLO_MSG[] = | 
| ocomeni | 119:8d939a902333 | 19 | |
| ocomeni | 119:8d939a902333 | 20 | const uint8_t HELLO_MSG[] = {0x50,0x4f | 
| ocomeni | 119:8d939a902333 | 21 | ,0x53,0x54,0x20,0x2f,0x6e,0x75,0x64,0x67 | 
| ocomeni | 119:8d939a902333 | 22 | ,0x65,0x62,0x6f,0x78,0x2f,0x76,0x31,0x20 | 
| ocomeni | 119:8d939a902333 | 23 | ,0x48,0x54,0x54,0x50,0x2f,0x31,0x2e,0x31 | 
| ocomeni | 119:8d939a902333 | 24 | ,0x0d,0x0a,0x48,0x6f,0x73,0x74,0x3a,0x20 | 
| ocomeni | 119:8d939a902333 | 25 | ,0x33,0x35,0x2e,0x31,0x37,0x36,0x2e,0x31 | 
| ocomeni | 119:8d939a902333 | 26 | ,0x39,0x32,0x2e,0x33,0x33,0x3a,0x38,0x30 | 
| ocomeni | 119:8d939a902333 | 27 | ,0x0d,0x0a,0x41,0x63,0x63,0x65,0x70,0x74 | 
| ocomeni | 119:8d939a902333 | 28 | ,0x3a,0x20,0x2a,0x2f,0x2a,0x0d,0x0a,0x43 | 
| ocomeni | 119:8d939a902333 | 29 | ,0x6f,0x6e,0x74,0x65,0x6e,0x74,0x2d,0x54 | 
| ocomeni | 119:8d939a902333 | 30 | ,0x79,0x70,0x65,0x3a,0x20,0x61,0x70,0x70 | 
| ocomeni | 119:8d939a902333 | 31 | ,0x6c,0x69,0x63,0x61,0x74,0x69,0x6f,0x6e | 
| ocomeni | 119:8d939a902333 | 32 | ,0x2f,0x6f,0x63,0x74,0x65,0x74,0x2d,0x73 | 
| ocomeni | 119:8d939a902333 | 33 | ,0x74,0x72,0x65,0x61,0x6d,0x0d,0x0a,0x43 | 
| ocomeni | 119:8d939a902333 | 34 | ,0x6f,0x6e,0x74,0x65,0x6e,0x74,0x2d,0x4c | 
| ocomeni | 119:8d939a902333 | 35 | ,0x65,0x6e,0x67,0x74,0x68,0x3a,0x20,0x32 | 
| ocomeni | 119:8d939a902333 | 36 | ,0x30,0x0d,0x0a,0x0d,0x0a,0x00,0x08,0xd4 | 
| ocomeni | 119:8d939a902333 | 37 | ,0xca,0x6e,0x79,0x05,0x4e,0x01,0x68,0x65 | 
| ocomeni | 119:8d939a902333 | 38 | ,0x6c,0x6c,0x6f,0x00,0x00,0x91,0xb5,0xa4 | 
| ocomeni | 119:8d939a902333 | 39 | ,0x10}; | 
| ocomeni | 78:07bb86e3ce14 | 40 | |
| ocomeni | 87:99b37d26ff2a | 41 | extern void print_memory_info(); | 
| ocomeni | 119:8d939a902333 | 42 | #define CLOUD_KEEP_ALIVE_INTERVAL 9000 | 
| ocomeni | 78:07bb86e3ce14 | 43 | class WiFiManager { | 
| ocomeni | 78:07bb86e3ce14 | 44 | public: | 
| ocomeni | 116:2296cf274661 | 45 | WiFiManager(wifi_config_t *wifi_config, WiFiInterface *wifi, | 
| ocomeni | 116:2296cf274661 | 46 | internet_config_t *internet_config, | 
| ocomeni | 98:65c2333a38b6 | 47 | events::EventQueue &event_queue, | 
| ocomeni | 79:a2187bbfa407 | 48 | MemoryPool<wifi_cmd_message_t, 16> *aT2WiFimPool, | 
| ocomeni | 80:e8f0e92e3ac9 | 49 | Queue<wifi_cmd_message_t, 16> *aT2WiFiCmdQueue, | 
| ocomeni | 81:637a87eb8170 | 50 | MemoryPool<at_resp_message_t, 16> *wiFi2ATmPool, | 
| ocomeni | 81:637a87eb8170 | 51 | Queue<at_resp_message_t, 16> *wiFi2ATCmdQueue, | 
| ocomeni | 87:99b37d26ff2a | 52 | MemoryPool<wifi_data_msg_t, PQDSZ> *aT2WiFiDatamPool, | 
| ocomeni | 87:99b37d26ff2a | 53 | Queue<wifi_data_msg_t, PQDSZ> *aT2WiFiDataQueue, | 
| ocomeni | 87:99b37d26ff2a | 54 | MemoryPool<at_data_msg_t, PQDSZ> *wiFi2ATDatamPool, | 
| ocomeni | 87:99b37d26ff2a | 55 | Queue<at_data_msg_t, PQDSZ> *wiFi2ATDataQueue); | 
| ocomeni | 78:07bb86e3ce14 | 56 | ~WiFiManager(); | 
| ocomeni | 81:637a87eb8170 | 57 | void runMain(); | 
| ocomeni | 81:637a87eb8170 | 58 | void status_callback(nsapi_event_t status, intptr_t param); | 
| ocomeni | 81:637a87eb8170 | 59 | |
| ocomeni | 81:637a87eb8170 | 60 | |
| ocomeni | 81:637a87eb8170 | 61 | private: | 
| ocomeni | 88:7ffa053be662 | 62 | Mutex _wmutex; // Protect wifi thread | 
| ocomeni | 116:2296cf274661 | 63 | wifi_config_t *wifi_config; | 
| ocomeni | 122:62166886db5f | 64 | WiFiInterface *network; | 
| ocomeni | 116:2296cf274661 | 65 | internet_config_t *internet_config; | 
| ocomeni | 98:65c2333a38b6 | 66 | // define event queue | 
| ocomeni | 98:65c2333a38b6 | 67 | events::EventQueue &_event_queue; | 
| ocomeni | 81:637a87eb8170 | 68 | HttpsRequest* https_request; | 
| ocomeni | 81:637a87eb8170 | 69 | HttpRequest* http_request; | 
| ocomeni | 81:637a87eb8170 | 70 | HttpResponse* http_response; | 
| ocomeni | 84:7c7add00f4bf | 71 | http_request_t *http_req_cfg; | 
| ocomeni | 90:ed0267eca7b5 | 72 | at_data_msg_t *at_data_resp; | 
| ocomeni | 81:637a87eb8170 | 73 | nsapi_size_or_error_t lastScanCount; | 
| ocomeni | 88:7ffa053be662 | 74 | bool https_connection_active; | 
| ocomeni | 88:7ffa053be662 | 75 | TLSSocket* socket; | 
| ocomeni | 95:290859010c8c | 76 | char* responseString; // response string formated for Box | 
| ocomeni | 99:05398b3184f8 | 77 | uint8_t* responseBytes; // response bytes formated for Box | 
| ocomeni | 81:637a87eb8170 | 78 | wifi_cmd_t wifiCmd; | 
| ocomeni | 107:f1a83fd41b17 | 79 | bool backgroundTaskCompleted; | 
| ocomeni | 84:7c7add00f4bf | 80 | //at_data_msg_t *at_data_resp; | 
| ocomeni | 84:7c7add00f4bf | 81 | int chunkNum; | 
| ocomeni | 81:637a87eb8170 | 82 | /* Queue and memory pool for AT to Wifi commands */ | 
| ocomeni | 81:637a87eb8170 | 83 | MemoryPool<wifi_cmd_message_t, 16> *_aT2WiFimPool; | 
| ocomeni | 81:637a87eb8170 | 84 | Queue<wifi_cmd_message_t, 16> *_aT2WiFiCmdQueue; | 
| ocomeni | 81:637a87eb8170 | 85 | |
| ocomeni | 81:637a87eb8170 | 86 | /* Queue and memory pool for WiFi to AT commands */ | 
| ocomeni | 81:637a87eb8170 | 87 | MemoryPool<at_resp_message_t, 16> *_wiFi2ATmPool; | 
| ocomeni | 81:637a87eb8170 | 88 | Queue<at_resp_message_t, 16> *_wiFi2ATCmdQueue; | 
| ocomeni | 81:637a87eb8170 | 89 | |
| ocomeni | 81:637a87eb8170 | 90 | /* Queue and memory pool for AT to WiFi data */ | 
| ocomeni | 87:99b37d26ff2a | 91 | MemoryPool<wifi_data_msg_t, PQDSZ> *_aT2WiFiDatamPool; | 
| ocomeni | 87:99b37d26ff2a | 92 | Queue<wifi_data_msg_t, PQDSZ> *_aT2WiFiDataQueue; | 
| ocomeni | 81:637a87eb8170 | 93 | wifi_data_msg_t *data_msg; | 
| ocomeni | 81:637a87eb8170 | 94 | |
| ocomeni | 81:637a87eb8170 | 95 | /* Queue and memory pool for WiFi to AT data */ | 
| ocomeni | 87:99b37d26ff2a | 96 | MemoryPool<at_data_msg_t, PQDSZ> *_wiFi2ATDatamPool; | 
| ocomeni | 87:99b37d26ff2a | 97 | Queue<at_data_msg_t, PQDSZ> *_wiFi2ATDataQueue; | 
| ocomeni | 81:637a87eb8170 | 98 | |
| ocomeni | 81:637a87eb8170 | 99 | bool is_connected; | 
| ocomeni | 90:ed0267eca7b5 | 100 | bool http_response_hdr_sent; | 
| ocomeni | 107:f1a83fd41b17 | 101 | bool http_request_result; | 
| ocomeni | 104:11e9605093c9 | 102 | int wifiBusy; | 
| ocomeni | 104:11e9605093c9 | 103 | Timer wifiWatchdogTimer; | 
| ocomeni | 109:c274780ff609 | 104 | Ticker watchDogTick; | 
| ocomeni | 104:11e9605093c9 | 105 | uint32_t watchdogCnt; | 
| ocomeni | 104:11e9605093c9 | 106 | http_result_t http_result; | 
| ocomeni | 113:888e262ff0a9 | 107 | bool use_full_hostname; | 
| ocomeni | 119:8d939a902333 | 108 | int keep_alive_id; | 
| ocomeni | 111:3ab1d9644835 | 109 | #ifdef DNANUDGE_DEBUG | 
| ocomeni | 111:3ab1d9644835 | 110 | rtos::Semaphore callback_semaphore; | 
| ocomeni | 111:3ab1d9644835 | 111 | #endif | 
| ocomeni | 79:a2187bbfa407 | 112 | nsapi_size_or_error_t scanNetworks(); | 
| ocomeni | 81:637a87eb8170 | 113 | nsapi_size_or_error_t getAvailableAPs(nsapi_size_t count); | 
| ocomeni | 79:a2187bbfa407 | 114 | void set_WIFI_SSID(char * wifi_ssid); | 
| ocomeni | 79:a2187bbfa407 | 115 | void set_WIFI_PASSWORD(char * wifi_pass); | 
| ocomeni | 79:a2187bbfa407 | 116 | void set_WIFI_SECURITY(nsapi_security_t wifi_security); | 
| ocomeni | 81:637a87eb8170 | 117 | void set_WIFI_CONFIG(); | 
| ocomeni | 100:80ef4bc31b7a | 118 | void set_internet_config(); | 
| ocomeni | 114:b11bb96c09f3 | 119 | void getWiFiInstance(); | 
| ocomeni | 79:a2187bbfa407 | 120 | nsapi_error_t connect(); | 
| ocomeni | 79:a2187bbfa407 | 121 | nsapi_error_t disconnect(); | 
| ocomeni | 107:f1a83fd41b17 | 122 | void createSendHttpsRequest(); | 
| ocomeni | 98:65c2333a38b6 | 123 | bool createHttpsRequest(); | 
| ocomeni | 79:a2187bbfa407 | 124 | void createHttpRequest(http_method method, | 
| ocomeni | 79:a2187bbfa407 | 125 | const char* url, | 
| ocomeni | 79:a2187bbfa407 | 126 | Callback<void(const char *at, uint32_t length)> body_callback = 0 | 
| ocomeni | 79:a2187bbfa407 | 127 | ); | 
| ocomeni | 79:a2187bbfa407 | 128 | void setHttpHeader(string key, string value); | 
| ocomeni | 79:a2187bbfa407 | 129 | void setHttpsHeader(string key, string value); | 
| ocomeni | 79:a2187bbfa407 | 130 | void sendHttpsRequest(const char * body, int bodyLen); | 
| ocomeni | 79:a2187bbfa407 | 131 | void sendHttpRequest(const char * body, int bodyLen); | 
| ocomeni | 79:a2187bbfa407 | 132 | bool setNextCommand(wifi_cmd_t cmd); | 
| ocomeni | 79:a2187bbfa407 | 133 | bool dequeueWiFiCommands(); | 
| ocomeni | 81:637a87eb8170 | 134 | bool queueATresponse(at_cmd_resp_t resp); | 
| ocomeni | 81:637a87eb8170 | 135 | bool dequeueATdataResponse(); | 
| ocomeni | 81:637a87eb8170 | 136 | bool queueWiFiDataResponse(at_data_msg_t at_resp); | 
| ocomeni | 81:637a87eb8170 | 137 | const char * sec2str(nsapi_security_t sec); | 
| ocomeni | 81:637a87eb8170 | 138 | void free_DataMsg(); | 
| ocomeni | 84:7c7add00f4bf | 139 | void body_callback(const char *at, uint32_t length); | 
| ocomeni | 84:7c7add00f4bf | 140 | void return_response(HttpResponse* res); | 
| ocomeni | 88:7ffa053be662 | 141 | void sendResponseDownloadData(at_cmd_resp_t at_cmd, | 
| ocomeni | 88:7ffa053be662 | 142 | const uint8_t * buf, | 
| ocomeni | 88:7ffa053be662 | 143 | int bufLen); | 
| ocomeni | 88:7ffa053be662 | 144 | bool createTLSconnection(const char *hostName); | 
| ocomeni | 104:11e9605093c9 | 145 | void printBufferInHex(const uint8_t *buf, int pLen); | 
| ocomeni | 102:9748f290a1a5 | 146 | bool copyResponseHdr2Queue(const uint8_t * buf); | 
| ocomeni | 118:8df0e9c2ee3f | 147 | void sendATresponseString(at_cmd_resp_t at_cmd); | 
| ocomeni | 118:8df0e9c2ee3f | 148 | void sendThreadATresponseString(const char *buf, at_cmd_resp_t at_cmd); | 
| ocomeni | 99:05398b3184f8 | 149 | void sendATresponseBytes(at_cmd_resp_t at_cmd, int len); | 
| ocomeni | 95:290859010c8c | 150 | void getNetworkStatus(); | 
| ocomeni | 95:290859010c8c | 151 | void getWiFiStatus(); | 
| ocomeni | 98:65c2333a38b6 | 152 | void status_callback_event(nsapi_event_t status, intptr_t param); | 
| ocomeni | 111:3ab1d9644835 | 153 | void gethostbyname_callback(nsapi_error_t res, SocketAddress *addr); | 
| ocomeni | 100:80ef4bc31b7a | 154 | void gethostbyname(); | 
| ocomeni | 100:80ef4bc31b7a | 155 | void sendSocketConnectionEvent(); | 
| ocomeni | 100:80ef4bc31b7a | 156 | void updateRemotePeerDetails(); | 
| ocomeni | 105:e5ce023eee93 | 157 | void sendDebugMessage(); | 
| ocomeni | 104:11e9605093c9 | 158 | void callWifiWatchDog(); | 
| ocomeni | 109:c274780ff609 | 159 | void callWifiWatchDogIsr(); | 
| ocomeni | 113:888e262ff0a9 | 160 | void processGetHostByNameResult(nsapi_error_t result, SocketAddress *addr); | 
| ocomeni | 118:8df0e9c2ee3f | 161 | bool outputBuffersAvailable(); | 
| ocomeni | 119:8d939a902333 | 162 | void callInternetKeepAlive(); | 
| ocomeni | 119:8d939a902333 | 163 | void keepSocketAlive(); | 
| ocomeni | 100:80ef4bc31b7a | 164 | |
| ocomeni | 90:ed0267eca7b5 | 165 | |
| ocomeni | 78:07bb86e3ce14 | 166 | |
| ocomeni | 78:07bb86e3ce14 | 167 | /** | 
| ocomeni | 78:07bb86e3ce14 | 168 | * Allows timeout to be changed between commands | 
| ocomeni | 78:07bb86e3ce14 | 169 | * | 
| ocomeni | 78:07bb86e3ce14 | 170 | * @param timeout_ms timeout of the connection | 
| ocomeni | 78:07bb86e3ce14 | 171 | */ | 
| ocomeni | 78:07bb86e3ce14 | 172 | //void set_timeout(uint32_t timeout_ms = UBLOX_ODIN_W2_MISC_TIMEOUT); | 
| ocomeni | 78:07bb86e3ce14 | 173 | }; | 
| ocomeni | 78:07bb86e3ce14 | 174 | #endif // __WIFI_MANAGER_H__ |