this is using the mbed os version 5-13-1
Embed:
(wiki syntax)
Show/hide line numbers
WiFiManager.h
00001 #ifndef __WIFI_MANAGER_H__ 00002 #define __WIFI_MANAGER_H__ 00003 #ifndef __MBED_H__ 00004 #define __MBED_H__ 00005 #include "mbed.h" 00006 #endif 00007 00008 #include "mbed_trace.h" 00009 #include "https_request.h" 00010 #include "http_request.h" 00011 /* List of trusted root CA certificates 00012 * currently two: GlobalSign, the CA for os.mbed.com and Let's Encrypt, the CA for httpbin.org 00013 * 00014 * To add more root certificates, just concatenate them. 00015 */ 00016 #include "https_certificates.h" 00017 #include "common_types.h" 00018 //const char HELLO_MSG[] = 00019 #define TOKEN_RESPONSE_SIZE 5*1024 00020 #define ID_TOKEN_SIZE 1024 00021 #define REFRESH_TOKEN_SIZE 2*1024 00022 #define KEEPALIVE_MSG_SIZE 1280 00023 #define SECS_PER_HOUR 3600 00024 #define MSECS_PER_SEC 1000 00025 #define SECS_IN_MIN 60 00026 #define TOKEN_VALID_PERIOD_MS (SECS_PER_HOUR-SECS_IN_MIN)*MSECS_PER_SEC // 1 hour less 1 minute 00027 #define ID_TOKEN_STR_START "\"IdToken\":\"" 00028 #define REFRESH_TOKEN_STR_START "\"RefreshToken\":\"" 00029 #define ACCESS_TOKEN_STR_END "\",\"" 00030 00031 const char TOKEN_REQ_HDR[] = "POST / HTTP/1.1\r\n" 00032 "Host: cognito-idp.eu-west-2.amazonaws.com\r\n" 00033 "X-Amz-Target: AWSCognitoIdentityProviderService.InitiateAuth\r\n" 00034 "Content-Type: application/x-amz-json-1.1"; 00035 const char UserName[] = "box1"; 00036 const char Password[] = "DnaN2dg3B4x!"; 00037 const char AuthFlow[] = "USER_PASSWORD_AUTH"; 00038 const char ClientId[] = "3el1h42i52p0e1gkb0l86ougdv"; 00039 const char AWS_HOST_NAME[] = "cognito-idp.eu-west-2.amazonaws.com"; 00040 const uint8_t HELLO_MSG[] = {0x50,0x4f 00041 ,0x53,0x54,0x20,0x2f,0x6e,0x75,0x64,0x67 00042 ,0x65,0x62,0x6f,0x78,0x2f,0x76,0x31,0x20 00043 ,0x48,0x54,0x54,0x50,0x2f,0x31,0x2e,0x31 00044 ,0x0d,0x0a,0x48,0x6f,0x73,0x74,0x3a,0x20 00045 ,0x33,0x35,0x2e,0x31,0x37,0x36,0x2e,0x31 00046 ,0x39,0x32,0x2e,0x33,0x33,0x3a,0x38,0x30 00047 ,0x0d,0x0a,0x41,0x63,0x63,0x65,0x70,0x74 00048 ,0x3a,0x20,0x2a,0x2f,0x2a,0x0d,0x0a,0x43 00049 ,0x6f,0x6e,0x74,0x65,0x6e,0x74,0x2d,0x54 00050 ,0x79,0x70,0x65,0x3a,0x20,0x61,0x70,0x70 00051 ,0x6c,0x69,0x63,0x61,0x74,0x69,0x6f,0x6e 00052 ,0x2f,0x6f,0x63,0x74,0x65,0x74,0x2d,0x73 00053 ,0x74,0x72,0x65,0x61,0x6d,0x0d,0x0a,0x43 00054 ,0x6f,0x6e,0x74,0x65,0x6e,0x74,0x2d,0x4c 00055 ,0x65,0x6e,0x67,0x74,0x68,0x3a,0x20,0x32 00056 ,0x30,0x0d,0x0a,0x0d,0x0a,0x00,0x08,0xd4 00057 ,0xca,0x6e,0x79,0x05,0x4e,0x01,0x68,0x65 00058 ,0x6c,0x6c,0x6f,0x00,0x00,0x91,0xb5,0xa4 00059 ,0x10}; 00060 const char KEEPALIVE_MSG_HDR[] = "POST /nudgebox/v1 HTTP/1.1\r\n" 00061 "Host: dev-box.dnanudge.io\r\n" 00062 "Content-Type: application/octet-stream\r\n" 00063 "Content-Length: 20" 00064 ""; 00065 00066 const uint8_t KEEPALIVE_MSG_BDY[] = {0x00,0x08,0xd4, 0xca,0x6e,0x79,0x05,0x4e, 00067 0x01,0x68,0x65, 0x6c,0x6c,0x6f,0x00,0x00, 00068 0x91,0xb5,0xa4,0x10}; 00069 extern void print_memory_info(); 00070 #define CLOUD_KEEP_ALIVE_INTERVAL 9000 00071 class WiFiManager { 00072 public: 00073 WiFiManager(wifi_config_t *wifi_config, WiFiInterface *wifi, 00074 internet_config_t *internet_config, 00075 events::EventQueue &event_queue, 00076 MemoryPool<wifi_cmd_message_t, 16> *aT2WiFimPool, 00077 Queue<wifi_cmd_message_t, 16> *aT2WiFiCmdQueue, 00078 MemoryPool<at_resp_message_t, 16> *wiFi2ATmPool, 00079 Queue<at_resp_message_t, 16> *wiFi2ATCmdQueue, 00080 MemoryPool<wifi_data_msg_t, PQDSZ> *aT2WiFiDatamPool, 00081 Queue<wifi_data_msg_t, PQDSZ> *aT2WiFiDataQueue, 00082 MemoryPool<at_data_msg_t, PQDSZ> *wiFi2ATDatamPool, 00083 Queue<at_data_msg_t, PQDSZ> *wiFi2ATDataQueue); 00084 ~WiFiManager(); 00085 void runMain(); 00086 void status_callback(nsapi_event_t status, intptr_t param); 00087 00088 00089 private: 00090 Mutex _wmutex; // Protect wifi thread 00091 wifi_config_t *wifi_config; 00092 WiFiInterface *network; 00093 internet_config_t *internet_config; 00094 // define event queue 00095 events::EventQueue &_event_queue; 00096 HttpsRequest* https_request; 00097 HttpRequest* http_request; 00098 HttpResponse* http_response; 00099 http_request_t *http_req_cfg; 00100 at_data_msg_t *at_data_resp; 00101 nsapi_size_or_error_t lastScanCount; 00102 bool https_connection_active; 00103 TLSSocket* socket; 00104 char* responseString; // response string formated for Box 00105 uint8_t* responseBytes; // response bytes formated for Box 00106 wifi_cmd_t wifiCmd; 00107 wifi_cmd_t fromWiFiCmd; 00108 wifi_cmd_t nextWiFiCmd; 00109 bool backgroundTaskCompleted; 00110 //at_data_msg_t *at_data_resp; 00111 int chunkNum; 00112 /* Queue and memory pool for AT to Wifi commands */ 00113 MemoryPool<wifi_cmd_message_t, 16> *_aT2WiFimPool; 00114 Queue<wifi_cmd_message_t, 16> *_aT2WiFiCmdQueue; 00115 00116 /* Queue and memory pool for WiFi to AT commands */ 00117 MemoryPool<at_resp_message_t, 16> *_wiFi2ATmPool; 00118 Queue<at_resp_message_t, 16> *_wiFi2ATCmdQueue; 00119 00120 /* Queue and memory pool for AT to WiFi data */ 00121 MemoryPool<wifi_data_msg_t, PQDSZ> *_aT2WiFiDatamPool; 00122 Queue<wifi_data_msg_t, PQDSZ> *_aT2WiFiDataQueue; 00123 wifi_data_msg_t *data_msg; 00124 00125 /* Queue and memory pool for WiFi to AT data */ 00126 MemoryPool<at_data_msg_t, PQDSZ> *_wiFi2ATDatamPool; 00127 Queue<at_data_msg_t, PQDSZ> *_wiFi2ATDataQueue; 00128 00129 bool is_connected; 00130 bool http_response_hdr_sent; 00131 bool http_request_result; 00132 int wifiBusy; 00133 Timer wifiWatchdogTimer; 00134 Ticker watchDogTick; 00135 uint32_t watchdogCnt; 00136 http_result_t http_result; 00137 bool use_full_hostname; 00138 int keep_alive_id; 00139 bool https_token_valid; 00140 char * aws_id_token; 00141 char * aws_refresh_token; 00142 int token_refresh_count; 00143 uint8_t *http_req_log_buf; 00144 00145 #ifdef DNANUDGE_DEBUG 00146 rtos::Semaphore callback_semaphore; 00147 #endif 00148 nsapi_size_or_error_t scanNetworks(); 00149 nsapi_size_or_error_t getAvailableAPs(nsapi_size_t count); 00150 void set_WIFI_SSID(char * wifi_ssid); 00151 void set_WIFI_PASSWORD(char * wifi_pass); 00152 void set_WIFI_SECURITY(nsapi_security_t wifi_security); 00153 void set_WIFI_CONFIG(); 00154 void set_internet_config(); 00155 void getWiFiInstance(); 00156 nsapi_error_t connect(); 00157 nsapi_error_t disconnect(); 00158 void createSendHttpsRequest(); 00159 bool createHttpsRequest(); 00160 void createHttpRequest(http_method method, 00161 const char* url, 00162 Callback<void(const char *at, uint32_t length)> body_callback = 0 00163 ); 00164 void setHttpHeader(string key, string value); 00165 void setHttpsHeader(string key, string value); 00166 void sendHttpsRequest(const char * body, int bodyLen); 00167 void sendHttpRequest(const char * body, int bodyLen); 00168 bool setNextCommand(wifi_cmd_t cmd); 00169 bool dequeueWiFiCommands(); 00170 bool queueATresponse(at_cmd_resp_t resp); 00171 bool dequeueATdataResponse(); 00172 bool queueWiFiDataResponse(at_data_msg_t at_resp); 00173 const char * sec2str(nsapi_security_t sec); 00174 void free_DataMsg(); 00175 void body_callback(const char *at, uint32_t length); 00176 void return_response(HttpResponse* res); 00177 void sendResponseDownloadData(at_cmd_resp_t at_cmd, 00178 const uint8_t * buf, 00179 int bufLen); 00180 bool createTLSconnection(const char *hostName); 00181 void printBufferInHex(const uint8_t *buf, int pLen); 00182 bool copyResponseHdr2Queue(const uint8_t * buf); 00183 void sendATresponseString(at_cmd_resp_t at_cmd); 00184 void sendThreadATresponseString(const char *buf, at_cmd_resp_t at_cmd); 00185 void sendATresponseBytes(at_cmd_resp_t at_cmd, int len); 00186 void getNetworkStatus(); 00187 void getWiFiStatus(); 00188 void getWiFiMACaddress(); 00189 void status_callback_event(nsapi_event_t status, intptr_t param); 00190 void gethostbyname_callback(nsapi_error_t res, SocketAddress *addr); 00191 void gethostbyname(); 00192 void sendSocketConnectionEvent(); 00193 void updateRemotePeerDetails(); 00194 void sendDebugMessage(); 00195 void callWifiWatchDog(); 00196 void callWifiWatchDogIsr(); 00197 void processGetHostByNameResult(nsapi_error_t result, SocketAddress *addr); 00198 bool outputBuffersAvailable(); 00199 void callInternetKeepAlive(); 00200 void keepSocketAlive(); 00201 void CreateTokenHttpsRequest(char * httpsReq); 00202 void GetCloudAccessToken(); 00203 void printHttpRequestLogBuffer(); 00204 void invalidateAccessToken(); 00205 void freeAccessTokenMemory(); 00206 void createTLSconnThreadCall(const char * hostName); 00207 void waitForBackgroundTask(int timeout_ms, int inc_ms); 00208 00209 00210 00211 /** 00212 * Allows timeout to be changed between commands 00213 * 00214 * @param timeout_ms timeout of the connection 00215 */ 00216 //void set_timeout(uint32_t timeout_ms = UBLOX_ODIN_W2_MISC_TIMEOUT); 00217 }; 00218 #endif // __WIFI_MANAGER_H__
Generated on Wed Jul 13 2022 05:40:26 by
1.7.2