Okundu Omeni
/
wifi-https-ble-sm-uart-atcmd-5-13-1
this is using the mbed os version 5-13-1
source/WiFiManager.h@129:590bdc2dcf5b, 2019-07-19 (annotated)
- Committer:
- ocomeni
- Date:
- Fri Jul 19 16:49:26 2019 +0000
- Branch:
- PassingRegression
- Revision:
- 129:590bdc2dcf5b
- Parent:
- 127:a21788227ca6
Implementation of Access token acquisition; 1. make request with credentials - DONE; 2. get response - DONE; 3. extract Id and refresh tokens from response - DONE; 4. integrate with code - DONE; Testing ongoing
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 | 127:a21788227ca6 | 19 | #define TOKEN_RESPONSE_SIZE 5*1024 |
ocomeni | 127:a21788227ca6 | 20 | #define ID_TOKEN_SIZE 1024 |
ocomeni | 127:a21788227ca6 | 21 | #define REFRESH_TOKEN_SIZE 2*1024 |
ocomeni | 129:590bdc2dcf5b | 22 | #define KEEPALIVE_MSG_SIZE 1280 |
ocomeni | 129:590bdc2dcf5b | 23 | #define SECS_PER_HOUR 3600 |
ocomeni | 129:590bdc2dcf5b | 24 | #define MSECS_PER_SEC 1000 |
ocomeni | 129:590bdc2dcf5b | 25 | #define SECS_IN_MIN 60 |
ocomeni | 129:590bdc2dcf5b | 26 | #define TOKEN_VALID_PERIOD_MS (SECS_PER_HOUR-SECS_IN_MIN)*MSECS_PER_SEC // 1 hour less 1 minute |
ocomeni | 127:a21788227ca6 | 27 | #define ID_TOKEN_STR_START "\"IdToken\":\"" |
ocomeni | 127:a21788227ca6 | 28 | #define REFRESH_TOKEN_STR_START "\"RefreshToken\":\"" |
ocomeni | 127:a21788227ca6 | 29 | #define ACCESS_TOKEN_STR_END "\",\"" |
ocomeni | 127:a21788227ca6 | 30 | |
ocomeni | 129:590bdc2dcf5b | 31 | const char TOKEN_REQ_HDR[] = "POST / HTTP/1.1\r\n" |
ocomeni | 127:a21788227ca6 | 32 | "Host: cognito-idp.eu-west-2.amazonaws.com\r\n" |
ocomeni | 127:a21788227ca6 | 33 | "X-Amz-Target: AWSCognitoIdentityProviderService.InitiateAuth\r\n" |
ocomeni | 127:a21788227ca6 | 34 | "Content-Type: application/x-amz-json-1.1"; |
ocomeni | 127:a21788227ca6 | 35 | const char UserName[] = "box1"; |
ocomeni | 127:a21788227ca6 | 36 | const char Password[] = "DnaN2dg3B4x!"; |
ocomeni | 127:a21788227ca6 | 37 | const char AuthFlow[] = "USER_PASSWORD_AUTH"; |
ocomeni | 127:a21788227ca6 | 38 | const char ClientId[] = "3el1h42i52p0e1gkb0l86ougdv"; |
ocomeni | 129:590bdc2dcf5b | 39 | const char AWS_HOST_NAME[] = "cognito-idp.eu-west-2.amazonaws.com"; |
ocomeni | 119:8d939a902333 | 40 | const uint8_t HELLO_MSG[] = {0x50,0x4f |
ocomeni | 119:8d939a902333 | 41 | ,0x53,0x54,0x20,0x2f,0x6e,0x75,0x64,0x67 |
ocomeni | 119:8d939a902333 | 42 | ,0x65,0x62,0x6f,0x78,0x2f,0x76,0x31,0x20 |
ocomeni | 119:8d939a902333 | 43 | ,0x48,0x54,0x54,0x50,0x2f,0x31,0x2e,0x31 |
ocomeni | 119:8d939a902333 | 44 | ,0x0d,0x0a,0x48,0x6f,0x73,0x74,0x3a,0x20 |
ocomeni | 119:8d939a902333 | 45 | ,0x33,0x35,0x2e,0x31,0x37,0x36,0x2e,0x31 |
ocomeni | 119:8d939a902333 | 46 | ,0x39,0x32,0x2e,0x33,0x33,0x3a,0x38,0x30 |
ocomeni | 119:8d939a902333 | 47 | ,0x0d,0x0a,0x41,0x63,0x63,0x65,0x70,0x74 |
ocomeni | 119:8d939a902333 | 48 | ,0x3a,0x20,0x2a,0x2f,0x2a,0x0d,0x0a,0x43 |
ocomeni | 119:8d939a902333 | 49 | ,0x6f,0x6e,0x74,0x65,0x6e,0x74,0x2d,0x54 |
ocomeni | 119:8d939a902333 | 50 | ,0x79,0x70,0x65,0x3a,0x20,0x61,0x70,0x70 |
ocomeni | 119:8d939a902333 | 51 | ,0x6c,0x69,0x63,0x61,0x74,0x69,0x6f,0x6e |
ocomeni | 119:8d939a902333 | 52 | ,0x2f,0x6f,0x63,0x74,0x65,0x74,0x2d,0x73 |
ocomeni | 119:8d939a902333 | 53 | ,0x74,0x72,0x65,0x61,0x6d,0x0d,0x0a,0x43 |
ocomeni | 119:8d939a902333 | 54 | ,0x6f,0x6e,0x74,0x65,0x6e,0x74,0x2d,0x4c |
ocomeni | 119:8d939a902333 | 55 | ,0x65,0x6e,0x67,0x74,0x68,0x3a,0x20,0x32 |
ocomeni | 119:8d939a902333 | 56 | ,0x30,0x0d,0x0a,0x0d,0x0a,0x00,0x08,0xd4 |
ocomeni | 119:8d939a902333 | 57 | ,0xca,0x6e,0x79,0x05,0x4e,0x01,0x68,0x65 |
ocomeni | 119:8d939a902333 | 58 | ,0x6c,0x6c,0x6f,0x00,0x00,0x91,0xb5,0xa4 |
ocomeni | 119:8d939a902333 | 59 | ,0x10}; |
ocomeni | 129:590bdc2dcf5b | 60 | const char KEEPALIVE_MSG_HDR[] = "POST /nudgebox/v1 HTTP/1.1\r\n" |
ocomeni | 129:590bdc2dcf5b | 61 | "Host: dev-box.dnanudge.io\r\n" |
ocomeni | 129:590bdc2dcf5b | 62 | "Content-Type: application/octet-stream\r\n" |
ocomeni | 129:590bdc2dcf5b | 63 | "Content-Length: 20" |
ocomeni | 129:590bdc2dcf5b | 64 | ""; |
ocomeni | 78:07bb86e3ce14 | 65 | |
ocomeni | 129:590bdc2dcf5b | 66 | const uint8_t KEEPALIVE_MSG_BDY[] = {0x00,0x08,0xd4, 0xca,0x6e,0x79,0x05,0x4e, |
ocomeni | 129:590bdc2dcf5b | 67 | 0x01,0x68,0x65, 0x6c,0x6c,0x6f,0x00,0x00, |
ocomeni | 129:590bdc2dcf5b | 68 | 0x91,0xb5,0xa4,0x10}; |
ocomeni | 87:99b37d26ff2a | 69 | extern void print_memory_info(); |
ocomeni | 119:8d939a902333 | 70 | #define CLOUD_KEEP_ALIVE_INTERVAL 9000 |
ocomeni | 78:07bb86e3ce14 | 71 | class WiFiManager { |
ocomeni | 78:07bb86e3ce14 | 72 | public: |
ocomeni | 116:2296cf274661 | 73 | WiFiManager(wifi_config_t *wifi_config, WiFiInterface *wifi, |
ocomeni | 116:2296cf274661 | 74 | internet_config_t *internet_config, |
ocomeni | 98:65c2333a38b6 | 75 | events::EventQueue &event_queue, |
ocomeni | 79:a2187bbfa407 | 76 | MemoryPool<wifi_cmd_message_t, 16> *aT2WiFimPool, |
ocomeni | 80:e8f0e92e3ac9 | 77 | Queue<wifi_cmd_message_t, 16> *aT2WiFiCmdQueue, |
ocomeni | 81:637a87eb8170 | 78 | MemoryPool<at_resp_message_t, 16> *wiFi2ATmPool, |
ocomeni | 81:637a87eb8170 | 79 | Queue<at_resp_message_t, 16> *wiFi2ATCmdQueue, |
ocomeni | 87:99b37d26ff2a | 80 | MemoryPool<wifi_data_msg_t, PQDSZ> *aT2WiFiDatamPool, |
ocomeni | 87:99b37d26ff2a | 81 | Queue<wifi_data_msg_t, PQDSZ> *aT2WiFiDataQueue, |
ocomeni | 87:99b37d26ff2a | 82 | MemoryPool<at_data_msg_t, PQDSZ> *wiFi2ATDatamPool, |
ocomeni | 87:99b37d26ff2a | 83 | Queue<at_data_msg_t, PQDSZ> *wiFi2ATDataQueue); |
ocomeni | 78:07bb86e3ce14 | 84 | ~WiFiManager(); |
ocomeni | 81:637a87eb8170 | 85 | void runMain(); |
ocomeni | 81:637a87eb8170 | 86 | void status_callback(nsapi_event_t status, intptr_t param); |
ocomeni | 81:637a87eb8170 | 87 | |
ocomeni | 81:637a87eb8170 | 88 | |
ocomeni | 81:637a87eb8170 | 89 | private: |
ocomeni | 88:7ffa053be662 | 90 | Mutex _wmutex; // Protect wifi thread |
ocomeni | 116:2296cf274661 | 91 | wifi_config_t *wifi_config; |
ocomeni | 122:62166886db5f | 92 | WiFiInterface *network; |
ocomeni | 116:2296cf274661 | 93 | internet_config_t *internet_config; |
ocomeni | 98:65c2333a38b6 | 94 | // define event queue |
ocomeni | 98:65c2333a38b6 | 95 | events::EventQueue &_event_queue; |
ocomeni | 81:637a87eb8170 | 96 | HttpsRequest* https_request; |
ocomeni | 81:637a87eb8170 | 97 | HttpRequest* http_request; |
ocomeni | 81:637a87eb8170 | 98 | HttpResponse* http_response; |
ocomeni | 84:7c7add00f4bf | 99 | http_request_t *http_req_cfg; |
ocomeni | 90:ed0267eca7b5 | 100 | at_data_msg_t *at_data_resp; |
ocomeni | 81:637a87eb8170 | 101 | nsapi_size_or_error_t lastScanCount; |
ocomeni | 88:7ffa053be662 | 102 | bool https_connection_active; |
ocomeni | 88:7ffa053be662 | 103 | TLSSocket* socket; |
ocomeni | 95:290859010c8c | 104 | char* responseString; // response string formated for Box |
ocomeni | 99:05398b3184f8 | 105 | uint8_t* responseBytes; // response bytes formated for Box |
ocomeni | 81:637a87eb8170 | 106 | wifi_cmd_t wifiCmd; |
ocomeni | 129:590bdc2dcf5b | 107 | wifi_cmd_t fromWiFiCmd; |
ocomeni | 129:590bdc2dcf5b | 108 | wifi_cmd_t nextWiFiCmd; |
ocomeni | 107:f1a83fd41b17 | 109 | bool backgroundTaskCompleted; |
ocomeni | 84:7c7add00f4bf | 110 | //at_data_msg_t *at_data_resp; |
ocomeni | 84:7c7add00f4bf | 111 | int chunkNum; |
ocomeni | 81:637a87eb8170 | 112 | /* Queue and memory pool for AT to Wifi commands */ |
ocomeni | 81:637a87eb8170 | 113 | MemoryPool<wifi_cmd_message_t, 16> *_aT2WiFimPool; |
ocomeni | 124:eae4512b131b | 114 | Queue<wifi_cmd_message_t, 16> *_aT2WiFiCmdQueue; |
ocomeni | 81:637a87eb8170 | 115 | |
ocomeni | 81:637a87eb8170 | 116 | /* Queue and memory pool for WiFi to AT commands */ |
ocomeni | 81:637a87eb8170 | 117 | MemoryPool<at_resp_message_t, 16> *_wiFi2ATmPool; |
ocomeni | 124:eae4512b131b | 118 | Queue<at_resp_message_t, 16> *_wiFi2ATCmdQueue; |
ocomeni | 81:637a87eb8170 | 119 | |
ocomeni | 81:637a87eb8170 | 120 | /* Queue and memory pool for AT to WiFi data */ |
ocomeni | 87:99b37d26ff2a | 121 | MemoryPool<wifi_data_msg_t, PQDSZ> *_aT2WiFiDatamPool; |
ocomeni | 124:eae4512b131b | 122 | Queue<wifi_data_msg_t, PQDSZ> *_aT2WiFiDataQueue; |
ocomeni | 81:637a87eb8170 | 123 | wifi_data_msg_t *data_msg; |
ocomeni | 81:637a87eb8170 | 124 | |
ocomeni | 81:637a87eb8170 | 125 | /* Queue and memory pool for WiFi to AT data */ |
ocomeni | 87:99b37d26ff2a | 126 | MemoryPool<at_data_msg_t, PQDSZ> *_wiFi2ATDatamPool; |
ocomeni | 124:eae4512b131b | 127 | Queue<at_data_msg_t, PQDSZ> *_wiFi2ATDataQueue; |
ocomeni | 81:637a87eb8170 | 128 | |
ocomeni | 81:637a87eb8170 | 129 | bool is_connected; |
ocomeni | 90:ed0267eca7b5 | 130 | bool http_response_hdr_sent; |
ocomeni | 107:f1a83fd41b17 | 131 | bool http_request_result; |
ocomeni | 104:11e9605093c9 | 132 | int wifiBusy; |
ocomeni | 104:11e9605093c9 | 133 | Timer wifiWatchdogTimer; |
ocomeni | 109:c274780ff609 | 134 | Ticker watchDogTick; |
ocomeni | 104:11e9605093c9 | 135 | uint32_t watchdogCnt; |
ocomeni | 104:11e9605093c9 | 136 | http_result_t http_result; |
ocomeni | 113:888e262ff0a9 | 137 | bool use_full_hostname; |
ocomeni | 119:8d939a902333 | 138 | int keep_alive_id; |
ocomeni | 129:590bdc2dcf5b | 139 | bool https_token_valid; |
ocomeni | 127:a21788227ca6 | 140 | char * aws_id_token; |
ocomeni | 127:a21788227ca6 | 141 | char * aws_refresh_token; |
ocomeni | 129:590bdc2dcf5b | 142 | int token_refresh_count; |
ocomeni | 129:590bdc2dcf5b | 143 | uint8_t *http_req_log_buf; |
ocomeni | 123:a49e9ffbaca6 | 144 | |
ocomeni | 111:3ab1d9644835 | 145 | #ifdef DNANUDGE_DEBUG |
ocomeni | 111:3ab1d9644835 | 146 | rtos::Semaphore callback_semaphore; |
ocomeni | 111:3ab1d9644835 | 147 | #endif |
ocomeni | 79:a2187bbfa407 | 148 | nsapi_size_or_error_t scanNetworks(); |
ocomeni | 81:637a87eb8170 | 149 | nsapi_size_or_error_t getAvailableAPs(nsapi_size_t count); |
ocomeni | 79:a2187bbfa407 | 150 | void set_WIFI_SSID(char * wifi_ssid); |
ocomeni | 79:a2187bbfa407 | 151 | void set_WIFI_PASSWORD(char * wifi_pass); |
ocomeni | 79:a2187bbfa407 | 152 | void set_WIFI_SECURITY(nsapi_security_t wifi_security); |
ocomeni | 81:637a87eb8170 | 153 | void set_WIFI_CONFIG(); |
ocomeni | 100:80ef4bc31b7a | 154 | void set_internet_config(); |
ocomeni | 114:b11bb96c09f3 | 155 | void getWiFiInstance(); |
ocomeni | 79:a2187bbfa407 | 156 | nsapi_error_t connect(); |
ocomeni | 79:a2187bbfa407 | 157 | nsapi_error_t disconnect(); |
ocomeni | 107:f1a83fd41b17 | 158 | void createSendHttpsRequest(); |
ocomeni | 98:65c2333a38b6 | 159 | bool createHttpsRequest(); |
ocomeni | 79:a2187bbfa407 | 160 | void createHttpRequest(http_method method, |
ocomeni | 79:a2187bbfa407 | 161 | const char* url, |
ocomeni | 79:a2187bbfa407 | 162 | Callback<void(const char *at, uint32_t length)> body_callback = 0 |
ocomeni | 79:a2187bbfa407 | 163 | ); |
ocomeni | 79:a2187bbfa407 | 164 | void setHttpHeader(string key, string value); |
ocomeni | 79:a2187bbfa407 | 165 | void setHttpsHeader(string key, string value); |
ocomeni | 79:a2187bbfa407 | 166 | void sendHttpsRequest(const char * body, int bodyLen); |
ocomeni | 79:a2187bbfa407 | 167 | void sendHttpRequest(const char * body, int bodyLen); |
ocomeni | 79:a2187bbfa407 | 168 | bool setNextCommand(wifi_cmd_t cmd); |
ocomeni | 79:a2187bbfa407 | 169 | bool dequeueWiFiCommands(); |
ocomeni | 81:637a87eb8170 | 170 | bool queueATresponse(at_cmd_resp_t resp); |
ocomeni | 81:637a87eb8170 | 171 | bool dequeueATdataResponse(); |
ocomeni | 81:637a87eb8170 | 172 | bool queueWiFiDataResponse(at_data_msg_t at_resp); |
ocomeni | 81:637a87eb8170 | 173 | const char * sec2str(nsapi_security_t sec); |
ocomeni | 81:637a87eb8170 | 174 | void free_DataMsg(); |
ocomeni | 84:7c7add00f4bf | 175 | void body_callback(const char *at, uint32_t length); |
ocomeni | 84:7c7add00f4bf | 176 | void return_response(HttpResponse* res); |
ocomeni | 88:7ffa053be662 | 177 | void sendResponseDownloadData(at_cmd_resp_t at_cmd, |
ocomeni | 88:7ffa053be662 | 178 | const uint8_t * buf, |
ocomeni | 88:7ffa053be662 | 179 | int bufLen); |
ocomeni | 88:7ffa053be662 | 180 | bool createTLSconnection(const char *hostName); |
ocomeni | 104:11e9605093c9 | 181 | void printBufferInHex(const uint8_t *buf, int pLen); |
ocomeni | 102:9748f290a1a5 | 182 | bool copyResponseHdr2Queue(const uint8_t * buf); |
ocomeni | 118:8df0e9c2ee3f | 183 | void sendATresponseString(at_cmd_resp_t at_cmd); |
ocomeni | 118:8df0e9c2ee3f | 184 | void sendThreadATresponseString(const char *buf, at_cmd_resp_t at_cmd); |
ocomeni | 99:05398b3184f8 | 185 | void sendATresponseBytes(at_cmd_resp_t at_cmd, int len); |
ocomeni | 95:290859010c8c | 186 | void getNetworkStatus(); |
ocomeni | 95:290859010c8c | 187 | void getWiFiStatus(); |
ocomeni | 123:a49e9ffbaca6 | 188 | void getWiFiMACaddress(); |
ocomeni | 98:65c2333a38b6 | 189 | void status_callback_event(nsapi_event_t status, intptr_t param); |
ocomeni | 111:3ab1d9644835 | 190 | void gethostbyname_callback(nsapi_error_t res, SocketAddress *addr); |
ocomeni | 100:80ef4bc31b7a | 191 | void gethostbyname(); |
ocomeni | 100:80ef4bc31b7a | 192 | void sendSocketConnectionEvent(); |
ocomeni | 100:80ef4bc31b7a | 193 | void updateRemotePeerDetails(); |
ocomeni | 105:e5ce023eee93 | 194 | void sendDebugMessage(); |
ocomeni | 104:11e9605093c9 | 195 | void callWifiWatchDog(); |
ocomeni | 109:c274780ff609 | 196 | void callWifiWatchDogIsr(); |
ocomeni | 113:888e262ff0a9 | 197 | void processGetHostByNameResult(nsapi_error_t result, SocketAddress *addr); |
ocomeni | 118:8df0e9c2ee3f | 198 | bool outputBuffersAvailable(); |
ocomeni | 119:8d939a902333 | 199 | void callInternetKeepAlive(); |
ocomeni | 119:8d939a902333 | 200 | void keepSocketAlive(); |
ocomeni | 127:a21788227ca6 | 201 | void CreateTokenHttpsRequest(char * httpsReq); |
ocomeni | 127:a21788227ca6 | 202 | void GetCloudAccessToken(); |
ocomeni | 129:590bdc2dcf5b | 203 | void printHttpRequestLogBuffer(); |
ocomeni | 129:590bdc2dcf5b | 204 | void invalidateAccessToken(); |
ocomeni | 129:590bdc2dcf5b | 205 | void freeAccessTokenMemory(); |
ocomeni | 129:590bdc2dcf5b | 206 | void createTLSconnThreadCall(const char * hostName); |
ocomeni | 129:590bdc2dcf5b | 207 | void waitForBackgroundTask(int timeout_ms, int inc_ms); |
ocomeni | 100:80ef4bc31b7a | 208 | |
ocomeni | 90:ed0267eca7b5 | 209 | |
ocomeni | 78:07bb86e3ce14 | 210 | |
ocomeni | 78:07bb86e3ce14 | 211 | /** |
ocomeni | 78:07bb86e3ce14 | 212 | * Allows timeout to be changed between commands |
ocomeni | 78:07bb86e3ce14 | 213 | * |
ocomeni | 78:07bb86e3ce14 | 214 | * @param timeout_ms timeout of the connection |
ocomeni | 78:07bb86e3ce14 | 215 | */ |
ocomeni | 78:07bb86e3ce14 | 216 | //void set_timeout(uint32_t timeout_ms = UBLOX_ODIN_W2_MISC_TIMEOUT); |
ocomeni | 78:07bb86e3ce14 | 217 | }; |
ocomeni | 78:07bb86e3ce14 | 218 | #endif // __WIFI_MANAGER_H__ |