this is using the mbed os version 5-13-1

Dependencies:   mbed-http

Committer:
ocomeni
Date:
Mon Jul 15 21:37:22 2019 +0000
Branch:
PassingRegression
Revision:
127:a21788227ca6
Parent:
124:eae4512b131b
Child:
128:3a641aaad2d9
started coding 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 - pending; 5. test - pending

Who changed what in which revision?

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