
this is using the mbed os version 5-13-1
source/network-helper.h@129:590bdc2dcf5b, 2019-07-19 (annotated)
- Committer:
- ocomeni
- Date:
- Fri Jul 19 16:49:26 2019 +0000
- Branch:
- PassingRegression
- Revision:
- 129:590bdc2dcf5b
- Parent:
- 75:08eff6258e1b
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 | 73:6f5021cbe752 | 1 | #ifndef _MBED_HTTP_EXAMPLE_H_ |
ocomeni | 73:6f5021cbe752 | 2 | #define _MBED_HTTP_EXAMPLE_H_ |
ocomeni | 73:6f5021cbe752 | 3 | |
ocomeni | 73:6f5021cbe752 | 4 | #include "mbed.h" |
ocomeni | 73:6f5021cbe752 | 5 | #ifdef TARGET_SIMULATOR |
ocomeni | 73:6f5021cbe752 | 6 | #include "EthernetInterface.h" |
ocomeni | 73:6f5021cbe752 | 7 | #endif |
ocomeni | 73:6f5021cbe752 | 8 | |
ocomeni | 73:6f5021cbe752 | 9 | |
ocomeni | 73:6f5021cbe752 | 10 | const char * sec2str(nsapi_security_t sec) |
ocomeni | 73:6f5021cbe752 | 11 | { |
ocomeni | 73:6f5021cbe752 | 12 | switch (sec) { |
ocomeni | 73:6f5021cbe752 | 13 | case NSAPI_SECURITY_NONE: |
ocomeni | 73:6f5021cbe752 | 14 | return "None"; |
ocomeni | 73:6f5021cbe752 | 15 | case NSAPI_SECURITY_WEP: |
ocomeni | 73:6f5021cbe752 | 16 | return "WEP"; |
ocomeni | 73:6f5021cbe752 | 17 | case NSAPI_SECURITY_WPA: |
ocomeni | 73:6f5021cbe752 | 18 | return "WPA"; |
ocomeni | 73:6f5021cbe752 | 19 | case NSAPI_SECURITY_WPA2: |
ocomeni | 73:6f5021cbe752 | 20 | return "WPA2"; |
ocomeni | 73:6f5021cbe752 | 21 | case NSAPI_SECURITY_WPA_WPA2: |
ocomeni | 73:6f5021cbe752 | 22 | return "WPA/WPA2"; |
ocomeni | 73:6f5021cbe752 | 23 | case NSAPI_SECURITY_UNKNOWN: |
ocomeni | 73:6f5021cbe752 | 24 | default: |
ocomeni | 73:6f5021cbe752 | 25 | return "Unknown"; |
ocomeni | 73:6f5021cbe752 | 26 | } |
ocomeni | 73:6f5021cbe752 | 27 | } |
ocomeni | 74:f26e846adfe9 | 28 | #define MAX_CHANNEL_NUM 4 |
ocomeni | 74:f26e846adfe9 | 29 | const int channel_list [] = {1,11, 36, 44}; |
ocomeni | 74:f26e846adfe9 | 30 | int getAPcount(WiFiInterface *wifi) |
ocomeni | 74:f26e846adfe9 | 31 | { |
ocomeni | 74:f26e846adfe9 | 32 | int count = 0; |
ocomeni | 74:f26e846adfe9 | 33 | for(uint8_t idx=0; idx< MAX_CHANNEL_NUM;idx++){ |
ocomeni | 74:f26e846adfe9 | 34 | printf("\n Scanning channel %d - APs so far = %d\n", channel_list[idx], count); |
ocomeni | 74:f26e846adfe9 | 35 | wifi->set_channel(channel_list[idx]); |
ocomeni | 74:f26e846adfe9 | 36 | count += wifi->scan(NULL,0); |
ocomeni | 74:f26e846adfe9 | 37 | wait_ms(150); // wait for 150 ms between scans |
ocomeni | 74:f26e846adfe9 | 38 | } |
ocomeni | 74:f26e846adfe9 | 39 | printf("\n Total APs found = %d\n", count); |
ocomeni | 74:f26e846adfe9 | 40 | return count; |
ocomeni | 74:f26e846adfe9 | 41 | } |
ocomeni | 74:f26e846adfe9 | 42 | |
ocomeni | 74:f26e846adfe9 | 43 | #define MAX_APs_PER_CHAN 3 |
ocomeni | 74:f26e846adfe9 | 44 | int getAPdetails(WiFiAccessPoint *ap, WiFiInterface *wifi, int apCount) |
ocomeni | 74:f26e846adfe9 | 45 | { |
ocomeni | 74:f26e846adfe9 | 46 | int count =0; |
ocomeni | 74:f26e846adfe9 | 47 | for(uint8_t idx=0; idx< MAX_CHANNEL_NUM;idx++){ |
ocomeni | 74:f26e846adfe9 | 48 | printf("\n Scanning channel %d - APs so far = %d apCount =%d\n", channel_list[idx], count, apCount); |
ocomeni | 74:f26e846adfe9 | 49 | wifi->set_channel(channel_list[idx]); |
ocomeni | 74:f26e846adfe9 | 50 | count += wifi->scan(&ap[count],(apCount - count) % MAX_APs_PER_CHAN); |
ocomeni | 74:f26e846adfe9 | 51 | wait_ms(150); // wait for 150 ms between scans |
ocomeni | 74:f26e846adfe9 | 52 | if(count >= apCount) break; |
ocomeni | 74:f26e846adfe9 | 53 | } |
ocomeni | 74:f26e846adfe9 | 54 | wifi->set_channel(0); |
ocomeni | 74:f26e846adfe9 | 55 | printf("\n Total APs found = %d\n", count); |
ocomeni | 74:f26e846adfe9 | 56 | return count; |
ocomeni | 74:f26e846adfe9 | 57 | } |
ocomeni | 74:f26e846adfe9 | 58 | |
ocomeni | 74:f26e846adfe9 | 59 | int scan_demo2(WiFiInterface *wifi) |
ocomeni | 74:f26e846adfe9 | 60 | { |
ocomeni | 74:f26e846adfe9 | 61 | WiFiAccessPoint *ap; |
ocomeni | 74:f26e846adfe9 | 62 | |
ocomeni | 74:f26e846adfe9 | 63 | printf("Scan:\n"); |
ocomeni | 74:f26e846adfe9 | 64 | |
ocomeni | 74:f26e846adfe9 | 65 | //int count = wifi->scan(NULL,0); |
ocomeni | 74:f26e846adfe9 | 66 | int count; |
ocomeni | 74:f26e846adfe9 | 67 | count = getAPcount(wifi); |
ocomeni | 74:f26e846adfe9 | 68 | if (count <= 0) { |
ocomeni | 74:f26e846adfe9 | 69 | printf("scan() failed with return value: %d\n", count); |
ocomeni | 74:f26e846adfe9 | 70 | return 0; |
ocomeni | 74:f26e846adfe9 | 71 | } |
ocomeni | 74:f26e846adfe9 | 72 | |
ocomeni | 74:f26e846adfe9 | 73 | /* Limit number of network arbitrary to 15 */ |
ocomeni | 74:f26e846adfe9 | 74 | count = count < 15 ? count : 15; |
ocomeni | 74:f26e846adfe9 | 75 | |
ocomeni | 74:f26e846adfe9 | 76 | ap = new WiFiAccessPoint[count]; |
ocomeni | 74:f26e846adfe9 | 77 | //count = wifi->scan(ap, count); |
ocomeni | 74:f26e846adfe9 | 78 | count = getAPdetails(ap, wifi, count); |
ocomeni | 74:f26e846adfe9 | 79 | |
ocomeni | 74:f26e846adfe9 | 80 | if (count <= 0) { |
ocomeni | 74:f26e846adfe9 | 81 | printf("scan() failed with return value: %d\n", count); |
ocomeni | 74:f26e846adfe9 | 82 | return 0; |
ocomeni | 74:f26e846adfe9 | 83 | } |
ocomeni | 74:f26e846adfe9 | 84 | |
ocomeni | 74:f26e846adfe9 | 85 | for (int i = 0; i < count; i++) { |
ocomeni | 74:f26e846adfe9 | 86 | printf("Network: %s secured: %s BSSID: %hhX:%hhX:%hhX:%hhx:%hhx:%hhx RSSI: %hhd Ch: %hhd\n", ap[i].get_ssid(), |
ocomeni | 74:f26e846adfe9 | 87 | sec2str(ap[i].get_security()), ap[i].get_bssid()[0], ap[i].get_bssid()[1], ap[i].get_bssid()[2], |
ocomeni | 74:f26e846adfe9 | 88 | ap[i].get_bssid()[3], ap[i].get_bssid()[4], ap[i].get_bssid()[5], ap[i].get_rssi(), ap[i].get_channel()); |
ocomeni | 74:f26e846adfe9 | 89 | } |
ocomeni | 74:f26e846adfe9 | 90 | printf("%d networks available.\n", count); |
ocomeni | 74:f26e846adfe9 | 91 | |
ocomeni | 74:f26e846adfe9 | 92 | delete[] ap; |
ocomeni | 74:f26e846adfe9 | 93 | return count; |
ocomeni | 74:f26e846adfe9 | 94 | } |
ocomeni | 74:f26e846adfe9 | 95 | |
ocomeni | 73:6f5021cbe752 | 96 | |
ocomeni | 73:6f5021cbe752 | 97 | int scan_demo(WiFiInterface *wifi) |
ocomeni | 73:6f5021cbe752 | 98 | { |
ocomeni | 73:6f5021cbe752 | 99 | WiFiAccessPoint *ap; |
ocomeni | 73:6f5021cbe752 | 100 | |
ocomeni | 73:6f5021cbe752 | 101 | printf("Scan:\n"); |
ocomeni | 73:6f5021cbe752 | 102 | |
ocomeni | 73:6f5021cbe752 | 103 | int count = wifi->scan(NULL,0); |
ocomeni | 73:6f5021cbe752 | 104 | |
ocomeni | 73:6f5021cbe752 | 105 | if (count <= 0) { |
ocomeni | 73:6f5021cbe752 | 106 | printf("scan() failed with return value: %d\n", count); |
ocomeni | 73:6f5021cbe752 | 107 | return 0; |
ocomeni | 73:6f5021cbe752 | 108 | } |
ocomeni | 73:6f5021cbe752 | 109 | |
ocomeni | 73:6f5021cbe752 | 110 | /* Limit number of network arbitrary to 15 */ |
ocomeni | 73:6f5021cbe752 | 111 | count = count < 15 ? count : 15; |
ocomeni | 73:6f5021cbe752 | 112 | |
ocomeni | 73:6f5021cbe752 | 113 | ap = new WiFiAccessPoint[count]; |
ocomeni | 73:6f5021cbe752 | 114 | count = wifi->scan(ap, count); |
ocomeni | 73:6f5021cbe752 | 115 | |
ocomeni | 73:6f5021cbe752 | 116 | if (count <= 0) { |
ocomeni | 73:6f5021cbe752 | 117 | printf("scan() failed with return value: %d\n", count); |
ocomeni | 73:6f5021cbe752 | 118 | return 0; |
ocomeni | 73:6f5021cbe752 | 119 | } |
ocomeni | 73:6f5021cbe752 | 120 | |
ocomeni | 73:6f5021cbe752 | 121 | for (int i = 0; i < count; i++) { |
ocomeni | 73:6f5021cbe752 | 122 | printf("Network: %s secured: %s BSSID: %hhX:%hhX:%hhX:%hhx:%hhx:%hhx RSSI: %hhd Ch: %hhd\n", ap[i].get_ssid(), |
ocomeni | 73:6f5021cbe752 | 123 | sec2str(ap[i].get_security()), ap[i].get_bssid()[0], ap[i].get_bssid()[1], ap[i].get_bssid()[2], |
ocomeni | 73:6f5021cbe752 | 124 | ap[i].get_bssid()[3], ap[i].get_bssid()[4], ap[i].get_bssid()[5], ap[i].get_rssi(), ap[i].get_channel()); |
ocomeni | 73:6f5021cbe752 | 125 | } |
ocomeni | 73:6f5021cbe752 | 126 | printf("%d networks available.\n", count); |
ocomeni | 73:6f5021cbe752 | 127 | |
ocomeni | 73:6f5021cbe752 | 128 | delete[] ap; |
ocomeni | 73:6f5021cbe752 | 129 | return count; |
ocomeni | 73:6f5021cbe752 | 130 | } |
ocomeni | 73:6f5021cbe752 | 131 | |
ocomeni | 73:6f5021cbe752 | 132 | |
ocomeni | 74:f26e846adfe9 | 133 | |
ocomeni | 73:6f5021cbe752 | 134 | /** |
ocomeni | 73:6f5021cbe752 | 135 | * Connect to the network using the default networking interface, |
ocomeni | 73:6f5021cbe752 | 136 | * you can also swap this out with a driver for a different networking interface |
ocomeni | 73:6f5021cbe752 | 137 | * if you use WiFi: see mbed_app.json for the credentials |
ocomeni | 73:6f5021cbe752 | 138 | */ |
ocomeni | 73:6f5021cbe752 | 139 | NetworkInterface *connect_to_default_network_interface() { |
ocomeni | 73:6f5021cbe752 | 140 | printf("[NWKH] Connecting to network...\n"); |
ocomeni | 73:6f5021cbe752 | 141 | |
ocomeni | 73:6f5021cbe752 | 142 | #ifdef TARGET_SIMULATOR |
ocomeni | 73:6f5021cbe752 | 143 | NetworkInterface* network = new EthernetInterface(); |
ocomeni | 73:6f5021cbe752 | 144 | #else |
ocomeni | 73:6f5021cbe752 | 145 | //NetworkInterface* network = NetworkInterface::get_default_instance(); |
ocomeni | 73:6f5021cbe752 | 146 | WiFiInterface *network = WiFiInterface::get_default_instance(); |
ocomeni | 73:6f5021cbe752 | 147 | if (!network) { |
ocomeni | 73:6f5021cbe752 | 148 | printf("ERROR: No WiFiInterface found.\n"); |
ocomeni | 73:6f5021cbe752 | 149 | return NULL; |
ocomeni | 73:6f5021cbe752 | 150 | } |
ocomeni | 74:f26e846adfe9 | 151 | nsapi_error_t error; |
ocomeni | 74:f26e846adfe9 | 152 | |
ocomeni | 74:f26e846adfe9 | 153 | error = network->set_blocking(false); |
ocomeni | 74:f26e846adfe9 | 154 | printf("Wifi Interface set to Non-Blocking .\n"); |
ocomeni | 75:08eff6258e1b | 155 | printf("Waiting for 5 seconds before starting scan...\n"); |
ocomeni | 75:08eff6258e1b | 156 | for(int i=0;i<5;i++){ |
ocomeni | 74:f26e846adfe9 | 157 | printf("%d", i); |
ocomeni | 74:f26e846adfe9 | 158 | wait(1); |
ocomeni | 74:f26e846adfe9 | 159 | } |
ocomeni | 73:6f5021cbe752 | 160 | int count; |
ocomeni | 73:6f5021cbe752 | 161 | count = scan_demo(network); |
ocomeni | 74:f26e846adfe9 | 162 | //count = scan_demo2(network); |
ocomeni | 73:6f5021cbe752 | 163 | if (count == 0) { |
ocomeni | 73:6f5021cbe752 | 164 | printf("No WIFI APNs found - can't continue further.\n"); |
ocomeni | 73:6f5021cbe752 | 165 | return NULL; |
ocomeni | 73:6f5021cbe752 | 166 | } |
ocomeni | 73:6f5021cbe752 | 167 | |
ocomeni | 74:f26e846adfe9 | 168 | printf("\nConnecting to %s...\n", MBED_CONF_APP_WIFI_SSID); |
ocomeni | 73:6f5021cbe752 | 169 | //ret = network->connect(MBED_CONF_APP_WIFI_SSID, MBED_CONF_APP_WIFI_PASSWORD, NSAPI_SECURITY_WPA_WPA2); |
ocomeni | 73:6f5021cbe752 | 170 | //if (ret != 0) { |
ocomeni | 73:6f5021cbe752 | 171 | // printf("\nConnection error: %d\n", ret); |
ocomeni | 73:6f5021cbe752 | 172 | // return -1; |
ocomeni | 73:6f5021cbe752 | 173 | //} |
ocomeni | 73:6f5021cbe752 | 174 | #endif |
ocomeni | 75:08eff6258e1b | 175 | printf("Waiting for 5 seconds before connecting...\n"); |
ocomeni | 75:08eff6258e1b | 176 | for(int i=0;i<5;i++){ |
ocomeni | 74:f26e846adfe9 | 177 | printf("%d", i); |
ocomeni | 74:f26e846adfe9 | 178 | wait(1); |
ocomeni | 74:f26e846adfe9 | 179 | } |
ocomeni | 73:6f5021cbe752 | 180 | |
ocomeni | 73:6f5021cbe752 | 181 | //nsapi_error_t connect_status = network->connect(); |
ocomeni | 73:6f5021cbe752 | 182 | nsapi_error_t connect_status = network->connect(MBED_CONF_APP_WIFI_SSID, MBED_CONF_APP_WIFI_PASSWORD, NSAPI_SECURITY_WPA_WPA2); |
ocomeni | 73:6f5021cbe752 | 183 | |
ocomeni | 73:6f5021cbe752 | 184 | if (connect_status != NSAPI_ERROR_OK) { |
ocomeni | 73:6f5021cbe752 | 185 | printf("[NWKH] Failed to connect to network (%d)\n", connect_status); |
ocomeni | 73:6f5021cbe752 | 186 | return NULL; |
ocomeni | 73:6f5021cbe752 | 187 | } |
ocomeni | 73:6f5021cbe752 | 188 | |
ocomeni | 73:6f5021cbe752 | 189 | printf("[NWKH] Connected to the network\n"); |
ocomeni | 73:6f5021cbe752 | 190 | printf("[NWKH] IP address: %s\n", network->get_ip_address()); |
ocomeni | 73:6f5021cbe752 | 191 | return network; |
ocomeni | 73:6f5021cbe752 | 192 | } |
ocomeni | 73:6f5021cbe752 | 193 | |
ocomeni | 73:6f5021cbe752 | 194 | #endif // _MBED_HTTP_EXAMPLE_H_ |