Khakimov Rishat
/
Kurs
87ty78ot7iyu
main.cpp@66:2cf02c7d430c, 2018-06-06 (annotated)
- Committer:
- mbed_official
- Date:
- Wed Jun 06 15:00:03 2018 +0100
- Revision:
- 66:2cf02c7d430c
- Parent:
- 63:99e063d738ee
- Child:
- 67:ebff4a8d228d
ONME-3694: Check network interface and socket error codes.
.
Commit copied from https://github.com/ARMmbed/mbed-os-example-wifi
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mbed_official | 0:857719181846 | 1 | /* WiFi Example |
mbed_official | 0:857719181846 | 2 | * Copyright (c) 2016 ARM Limited |
mbed_official | 0:857719181846 | 3 | * |
mbed_official | 0:857719181846 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
mbed_official | 0:857719181846 | 5 | * you may not use this file except in compliance with the License. |
mbed_official | 0:857719181846 | 6 | * You may obtain a copy of the License at |
mbed_official | 0:857719181846 | 7 | * |
mbed_official | 0:857719181846 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
mbed_official | 0:857719181846 | 9 | * |
mbed_official | 0:857719181846 | 10 | * Unless required by applicable law or agreed to in writing, software |
mbed_official | 0:857719181846 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
mbed_official | 0:857719181846 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
mbed_official | 0:857719181846 | 13 | * See the License for the specific language governing permissions and |
mbed_official | 0:857719181846 | 14 | * limitations under the License. |
mbed_official | 0:857719181846 | 15 | */ |
mbed_official | 0:857719181846 | 16 | |
mbed_official | 0:857719181846 | 17 | #include "mbed.h" |
mbed_official | 0:857719181846 | 18 | #include "TCPSocket.h" |
mbed_official | 0:857719181846 | 19 | |
mbed_official | 32:bca3f5f442b3 | 20 | #define WIFI_ESP8266 1 |
mbed_official | 44:63be19b7a3db | 21 | #define WIFI_IDW0XX1 2 |
mbed_official | 63:99e063d738ee | 22 | #define WIFI_ISM43362 3 |
mbed_official | 32:bca3f5f442b3 | 23 | |
mbed_official | 0:857719181846 | 24 | #if TARGET_UBLOX_EVK_ODIN_W2 |
mbed_official | 1:aea78e21a7da | 25 | #include "OdinWiFiInterface.h" |
mbed_official | 1:aea78e21a7da | 26 | OdinWiFiInterface wifi; |
mbed_official | 10:5b5beb106156 | 27 | |
mbed_official | 21:b2f2f6a840b4 | 28 | #elif TARGET_REALTEK_RTL8195AM |
mbed_official | 21:b2f2f6a840b4 | 29 | #include "RTWInterface.h" |
mbed_official | 21:b2f2f6a840b4 | 30 | RTWInterface wifi; |
mbed_official | 21:b2f2f6a840b4 | 31 | |
mbed_official | 33:12f0df4d51d7 | 32 | #else // External WiFi modules |
mbed_official | 21:b2f2f6a840b4 | 33 | |
mbed_official | 32:bca3f5f442b3 | 34 | #if MBED_CONF_APP_WIFI_SHIELD == WIFI_ESP8266 |
mbed_official | 1:aea78e21a7da | 35 | #include "ESP8266Interface.h" |
mbed_official | 10:5b5beb106156 | 36 | ESP8266Interface wifi(MBED_CONF_APP_WIFI_TX, MBED_CONF_APP_WIFI_RX); |
mbed_official | 63:99e063d738ee | 37 | |
mbed_official | 63:99e063d738ee | 38 | #elif MBED_CONF_APP_WIFI_SHIELD == WIFI_ISM43362 |
mbed_official | 63:99e063d738ee | 39 | #include "ISM43362Interface.h" |
mbed_official | 63:99e063d738ee | 40 | ISM43362Interface wifi; |
mbed_official | 63:99e063d738ee | 41 | |
mbed_official | 44:63be19b7a3db | 42 | #elif MBED_CONF_APP_WIFI_SHIELD == WIFI_IDW0XX1 |
mbed_official | 32:bca3f5f442b3 | 43 | #include "SpwfSAInterface.h" |
mbed_official | 32:bca3f5f442b3 | 44 | SpwfSAInterface wifi(MBED_CONF_APP_WIFI_TX, MBED_CONF_APP_WIFI_RX); |
mbed_official | 63:99e063d738ee | 45 | |
mbed_official | 44:63be19b7a3db | 46 | #endif // MBED_CONF_APP_WIFI_SHIELD == WIFI_IDW0XX1 |
mbed_official | 10:5b5beb106156 | 47 | |
mbed_official | 33:12f0df4d51d7 | 48 | #endif |
mbed_official | 0:857719181846 | 49 | |
mbed_official | 0:857719181846 | 50 | const char *sec2str(nsapi_security_t sec) |
mbed_official | 0:857719181846 | 51 | { |
mbed_official | 0:857719181846 | 52 | switch (sec) { |
mbed_official | 0:857719181846 | 53 | case NSAPI_SECURITY_NONE: |
mbed_official | 0:857719181846 | 54 | return "None"; |
mbed_official | 0:857719181846 | 55 | case NSAPI_SECURITY_WEP: |
mbed_official | 0:857719181846 | 56 | return "WEP"; |
mbed_official | 0:857719181846 | 57 | case NSAPI_SECURITY_WPA: |
mbed_official | 0:857719181846 | 58 | return "WPA"; |
mbed_official | 0:857719181846 | 59 | case NSAPI_SECURITY_WPA2: |
mbed_official | 0:857719181846 | 60 | return "WPA2"; |
mbed_official | 0:857719181846 | 61 | case NSAPI_SECURITY_WPA_WPA2: |
mbed_official | 0:857719181846 | 62 | return "WPA/WPA2"; |
mbed_official | 0:857719181846 | 63 | case NSAPI_SECURITY_UNKNOWN: |
mbed_official | 0:857719181846 | 64 | default: |
mbed_official | 0:857719181846 | 65 | return "Unknown"; |
mbed_official | 0:857719181846 | 66 | } |
mbed_official | 0:857719181846 | 67 | } |
mbed_official | 0:857719181846 | 68 | |
mbed_official | 37:3a31525e2971 | 69 | int scan_demo(WiFiInterface *wifi) |
mbed_official | 0:857719181846 | 70 | { |
mbed_official | 0:857719181846 | 71 | WiFiAccessPoint *ap; |
mbed_official | 0:857719181846 | 72 | |
mbed_official | 35:052c1ba06ce7 | 73 | printf("Scan:\n"); |
mbed_official | 0:857719181846 | 74 | |
mbed_official | 0:857719181846 | 75 | int count = wifi->scan(NULL,0); |
mbed_official | 0:857719181846 | 76 | |
mbed_official | 66:2cf02c7d430c | 77 | if (count <= 0) { |
mbed_official | 66:2cf02c7d430c | 78 | printf("scan() failed with return value: %d\n", count); |
mbed_official | 66:2cf02c7d430c | 79 | return 0; |
mbed_official | 66:2cf02c7d430c | 80 | } |
mbed_official | 66:2cf02c7d430c | 81 | |
mbed_official | 0:857719181846 | 82 | /* Limit number of network arbitrary to 15 */ |
mbed_official | 0:857719181846 | 83 | count = count < 15 ? count : 15; |
mbed_official | 0:857719181846 | 84 | |
mbed_official | 0:857719181846 | 85 | ap = new WiFiAccessPoint[count]; |
mbed_official | 0:857719181846 | 86 | count = wifi->scan(ap, count); |
mbed_official | 66:2cf02c7d430c | 87 | |
mbed_official | 66:2cf02c7d430c | 88 | if (count <= 0) { |
mbed_official | 66:2cf02c7d430c | 89 | printf("scan() failed with return value: %d\n", count); |
mbed_official | 66:2cf02c7d430c | 90 | return 0; |
mbed_official | 66:2cf02c7d430c | 91 | } |
mbed_official | 66:2cf02c7d430c | 92 | |
mbed_official | 66:2cf02c7d430c | 93 | for (int i = 0; i < count; i++) { |
mbed_official | 35:052c1ba06ce7 | 94 | printf("Network: %s secured: %s BSSID: %hhX:%hhX:%hhX:%hhx:%hhx:%hhx RSSI: %hhd Ch: %hhd\n", ap[i].get_ssid(), |
mbed_official | 0:857719181846 | 95 | sec2str(ap[i].get_security()), ap[i].get_bssid()[0], ap[i].get_bssid()[1], ap[i].get_bssid()[2], |
mbed_official | 0:857719181846 | 96 | ap[i].get_bssid()[3], ap[i].get_bssid()[4], ap[i].get_bssid()[5], ap[i].get_rssi(), ap[i].get_channel()); |
mbed_official | 0:857719181846 | 97 | } |
mbed_official | 35:052c1ba06ce7 | 98 | printf("%d networks available.\n", count); |
mbed_official | 0:857719181846 | 99 | |
mbed_official | 0:857719181846 | 100 | delete[] ap; |
mbed_official | 37:3a31525e2971 | 101 | return count; |
mbed_official | 0:857719181846 | 102 | } |
mbed_official | 0:857719181846 | 103 | |
mbed_official | 0:857719181846 | 104 | void http_demo(NetworkInterface *net) |
mbed_official | 0:857719181846 | 105 | { |
mbed_official | 0:857719181846 | 106 | TCPSocket socket; |
mbed_official | 24:323569a565ec | 107 | nsapi_error_t response; |
mbed_official | 0:857719181846 | 108 | |
mbed_official | 35:052c1ba06ce7 | 109 | printf("Sending HTTP request to www.arm.com...\n"); |
mbed_official | 0:857719181846 | 110 | |
mbed_official | 0:857719181846 | 111 | // Open a socket on the network interface, and create a TCP connection to www.arm.com |
mbed_official | 66:2cf02c7d430c | 112 | response = socket.open(net); |
mbed_official | 66:2cf02c7d430c | 113 | if(0 != response) { |
mbed_official | 66:2cf02c7d430c | 114 | printf("socket.open() failed: %d\n", response); |
mbed_official | 66:2cf02c7d430c | 115 | return; |
mbed_official | 66:2cf02c7d430c | 116 | } |
mbed_official | 66:2cf02c7d430c | 117 | |
mbed_official | 66:2cf02c7d430c | 118 | response = socket.connect("api.ipify.org", 80); |
mbed_official | 24:323569a565ec | 119 | if(0 != response) { |
mbed_official | 35:052c1ba06ce7 | 120 | printf("Error connecting: %d\n", response); |
mbed_official | 24:323569a565ec | 121 | socket.close(); |
mbed_official | 24:323569a565ec | 122 | return; |
mbed_official | 24:323569a565ec | 123 | } |
mbed_official | 0:857719181846 | 124 | |
mbed_official | 0:857719181846 | 125 | // Send a simple http request |
mbed_official | 66:2cf02c7d430c | 126 | char sbuffer[] = "GET / HTTP/1.1\r\nHost: api.ipify.org\r\nConnection: close\r\n\r\n"; |
mbed_official | 33:12f0df4d51d7 | 127 | nsapi_size_t size = strlen(sbuffer); |
mbed_official | 66:2cf02c7d430c | 128 | |
mbed_official | 66:2cf02c7d430c | 129 | // Loop until whole request send |
mbed_official | 66:2cf02c7d430c | 130 | while(size) { |
mbed_official | 24:323569a565ec | 131 | response = socket.send(sbuffer+response, size); |
mbed_official | 24:323569a565ec | 132 | if (response < 0) { |
mbed_official | 35:052c1ba06ce7 | 133 | printf("Error sending data: %d\n", response); |
mbed_official | 24:323569a565ec | 134 | socket.close(); |
mbed_official | 24:323569a565ec | 135 | return; |
mbed_official | 24:323569a565ec | 136 | } |
mbed_official | 66:2cf02c7d430c | 137 | size -= response; |
mbed_official | 66:2cf02c7d430c | 138 | printf("sent %d [%.*s]\n", response, strstr(sbuffer, "\r\n")-sbuffer, sbuffer); |
mbed_official | 24:323569a565ec | 139 | } |
mbed_official | 0:857719181846 | 140 | |
mbed_official | 66:2cf02c7d430c | 141 | // Receieve a simple http response and print out the response line |
mbed_official | 0:857719181846 | 142 | char rbuffer[64]; |
mbed_official | 24:323569a565ec | 143 | response = socket.recv(rbuffer, sizeof rbuffer); |
mbed_official | 24:323569a565ec | 144 | if (response < 0) { |
mbed_official | 35:052c1ba06ce7 | 145 | printf("Error receiving data: %d\n", response); |
mbed_official | 24:323569a565ec | 146 | } else { |
mbed_official | 35:052c1ba06ce7 | 147 | printf("recv %d [%.*s]\n", response, strstr(rbuffer, "\r\n")-rbuffer, rbuffer); |
mbed_official | 24:323569a565ec | 148 | } |
mbed_official | 0:857719181846 | 149 | |
mbed_official | 0:857719181846 | 150 | // Close the socket to return its memory and bring down the network interface |
mbed_official | 0:857719181846 | 151 | socket.close(); |
mbed_official | 0:857719181846 | 152 | } |
mbed_official | 0:857719181846 | 153 | |
mbed_official | 0:857719181846 | 154 | int main() |
mbed_official | 0:857719181846 | 155 | { |
mbed_official | 37:3a31525e2971 | 156 | int count = 0; |
mbed_official | 37:3a31525e2971 | 157 | |
mbed_official | 35:052c1ba06ce7 | 158 | printf("WiFi example\n\n"); |
mbed_official | 0:857719181846 | 159 | |
mbed_official | 37:3a31525e2971 | 160 | count = scan_demo(&wifi); |
mbed_official | 37:3a31525e2971 | 161 | if (count == 0) { |
mbed_official | 37:3a31525e2971 | 162 | printf("No WIFI APNs found - can't continue further.\n"); |
mbed_official | 37:3a31525e2971 | 163 | return -1; |
mbed_official | 37:3a31525e2971 | 164 | } |
mbed_official | 0:857719181846 | 165 | |
mbed_official | 37:3a31525e2971 | 166 | printf("\nConnecting to %s...\n", MBED_CONF_APP_WIFI_SSID); |
mbed_official | 0:857719181846 | 167 | int ret = wifi.connect(MBED_CONF_APP_WIFI_SSID, MBED_CONF_APP_WIFI_PASSWORD, NSAPI_SECURITY_WPA_WPA2); |
mbed_official | 0:857719181846 | 168 | if (ret != 0) { |
mbed_official | 66:2cf02c7d430c | 169 | printf("\nConnection error: %d\n", ret); |
mbed_official | 0:857719181846 | 170 | return -1; |
mbed_official | 0:857719181846 | 171 | } |
mbed_official | 0:857719181846 | 172 | |
mbed_official | 35:052c1ba06ce7 | 173 | printf("Success\n\n"); |
mbed_official | 35:052c1ba06ce7 | 174 | printf("MAC: %s\n", wifi.get_mac_address()); |
mbed_official | 35:052c1ba06ce7 | 175 | printf("IP: %s\n", wifi.get_ip_address()); |
mbed_official | 35:052c1ba06ce7 | 176 | printf("Netmask: %s\n", wifi.get_netmask()); |
mbed_official | 35:052c1ba06ce7 | 177 | printf("Gateway: %s\n", wifi.get_gateway()); |
mbed_official | 35:052c1ba06ce7 | 178 | printf("RSSI: %d\n\n", wifi.get_rssi()); |
mbed_official | 0:857719181846 | 179 | |
mbed_official | 0:857719181846 | 180 | http_demo(&wifi); |
mbed_official | 0:857719181846 | 181 | |
mbed_official | 0:857719181846 | 182 | wifi.disconnect(); |
mbed_official | 0:857719181846 | 183 | |
mbed_official | 35:052c1ba06ce7 | 184 | printf("\nDone\n"); |
mbed_official | 0:857719181846 | 185 | } |