nucleo f767zi onenet edp test
Dependencies: cJSON_lib Common_lib EdpKit_lib
main.cpp@56:02a6401ec508, 2018-08-15 (annotated)
- Committer:
- mbed_official
- Date:
- Wed Aug 15 11:15:04 2018 +0100
- Revision:
- 56:02a6401ec508
- Parent:
- 49:1923a727df5b
- Child:
- 58:4aeb931b8475
Improve error handling.
* socket.connect() was not causing program to stop, in case of error
* socket.send() was printing "Error! socket.connect()..." in case of error
* rename labels to upper case for readability
.
Commit copied from https://github.com/ARMmbed/mbed-os-example-sockets
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mbed_official | 0:17bd84fc5087 | 1 | #include "mbed.h" |
mbed_official | 0:17bd84fc5087 | 2 | |
mbed_official | 5:3e952c60d705 | 3 | // Network interface |
mbed_official | 49:1923a727df5b | 4 | NetworkInterface *net; |
mbed_official | 0:17bd84fc5087 | 5 | |
mbed_official | 0:17bd84fc5087 | 6 | // Socket demo |
mbed_official | 5:3e952c60d705 | 7 | int main() { |
mbed_official | 47:08787ef063cb | 8 | int remaining; |
mbed_official | 47:08787ef063cb | 9 | int rcount; |
mbed_official | 47:08787ef063cb | 10 | char *p; |
mbed_official | 47:08787ef063cb | 11 | char *buffer = new char[256]; |
mbed_official | 47:08787ef063cb | 12 | nsapi_size_or_error_t r; |
mbed_official | 47:08787ef063cb | 13 | |
mbed_official | 5:3e952c60d705 | 14 | // Bring up the ethernet interface |
mbed_official | 49:1923a727df5b | 15 | printf("Mbed OS Socket example\n"); |
mbed_official | 48:f2739ac5cb01 | 16 | |
mbed_official | 48:f2739ac5cb01 | 17 | #ifdef MBED_MAJOR_VERSION |
mbed_official | 49:1923a727df5b | 18 | printf("Mbed OS version: %d.%d.%d\n\n", MBED_MAJOR_VERSION, MBED_MINOR_VERSION, MBED_PATCH_VERSION); |
mbed_official | 48:f2739ac5cb01 | 19 | #endif |
mbed_official | 48:f2739ac5cb01 | 20 | |
mbed_official | 49:1923a727df5b | 21 | net = NetworkInterface::get_default_instance(); |
mbed_official | 49:1923a727df5b | 22 | |
mbed_official | 49:1923a727df5b | 23 | if (!net) { |
mbed_official | 49:1923a727df5b | 24 | printf("Error! No network inteface found.\n"); |
mbed_official | 49:1923a727df5b | 25 | return 0; |
mbed_official | 49:1923a727df5b | 26 | } |
mbed_official | 49:1923a727df5b | 27 | |
mbed_official | 49:1923a727df5b | 28 | r = net->connect(); |
mbed_official | 47:08787ef063cb | 29 | if (r != 0) { |
mbed_official | 49:1923a727df5b | 30 | printf("Error! net->connect() returned: %d\n", r); |
mbed_official | 47:08787ef063cb | 31 | } |
mbed_official | 0:17bd84fc5087 | 32 | |
mbed_official | 0:17bd84fc5087 | 33 | // Show the network address |
mbed_official | 49:1923a727df5b | 34 | const char *ip = net->get_ip_address(); |
mbed_official | 49:1923a727df5b | 35 | const char *netmask = net->get_netmask(); |
mbed_official | 49:1923a727df5b | 36 | const char *gateway = net->get_gateway(); |
mbed_official | 13:ed9e4aa00044 | 37 | printf("IP address: %s\n", ip ? ip : "None"); |
mbed_official | 13:ed9e4aa00044 | 38 | printf("Netmask: %s\n", netmask ? netmask : "None"); |
mbed_official | 13:ed9e4aa00044 | 39 | printf("Gateway: %s\n", gateway ? gateway : "None"); |
mbed_official | 0:17bd84fc5087 | 40 | |
mbed_official | 0:17bd84fc5087 | 41 | // Open a socket on the network interface, and create a TCP connection to mbed.org |
mbed_official | 5:3e952c60d705 | 42 | TCPSocket socket; |
mbed_official | 56:02a6401ec508 | 43 | // Send a simple http request |
mbed_official | 56:02a6401ec508 | 44 | char sbuffer[] = "GET / HTTP/1.1\r\nHost: api.ipify.org\r\nConnection: close\r\n\r\n"; |
mbed_official | 56:02a6401ec508 | 45 | nsapi_size_t size = strlen(sbuffer); |
mbed_official | 56:02a6401ec508 | 46 | |
mbed_official | 49:1923a727df5b | 47 | r = socket.open(net); |
mbed_official | 47:08787ef063cb | 48 | if (r != 0) { |
mbed_official | 47:08787ef063cb | 49 | printf("Error! socket.open() returned: %d\n", r); |
mbed_official | 47:08787ef063cb | 50 | } |
mbed_official | 0:17bd84fc5087 | 51 | |
mbed_official | 47:08787ef063cb | 52 | r = socket.connect("api.ipify.org", 80); |
mbed_official | 47:08787ef063cb | 53 | if (r != 0) { |
mbed_official | 47:08787ef063cb | 54 | printf("Error! socket.connect() returned: %d\n", r); |
mbed_official | 56:02a6401ec508 | 55 | goto DISCONNECT; |
mbed_official | 47:08787ef063cb | 56 | } |
mbed_official | 47:08787ef063cb | 57 | |
mbed_official | 47:08787ef063cb | 58 | // Loop until whole request send |
mbed_official | 47:08787ef063cb | 59 | while(size) { |
mbed_official | 47:08787ef063cb | 60 | r = socket.send(sbuffer+r, size); |
mbed_official | 47:08787ef063cb | 61 | if (r < 0) { |
mbed_official | 56:02a6401ec508 | 62 | printf("Error! socket.send() returned: %d\n", r); |
mbed_official | 56:02a6401ec508 | 63 | goto DISCONNECT; |
mbed_official | 47:08787ef063cb | 64 | } |
mbed_official | 47:08787ef063cb | 65 | size -= r; |
mbed_official | 47:08787ef063cb | 66 | printf("sent %d [%.*s]\n", r, strstr(sbuffer, "\r\n")-sbuffer, sbuffer); |
mbed_official | 40:afef93b6d854 | 67 | } |
mbed_official | 47:08787ef063cb | 68 | |
mbed_official | 47:08787ef063cb | 69 | // Receieve an HTTP response and print out the response line |
mbed_official | 47:08787ef063cb | 70 | remaining = 256; |
mbed_official | 47:08787ef063cb | 71 | rcount = 0; |
mbed_official | 47:08787ef063cb | 72 | p = buffer; |
mbed_official | 47:08787ef063cb | 73 | while (0 < (r = socket.recv(p, remaining))) { |
mbed_official | 47:08787ef063cb | 74 | p += r; |
mbed_official | 47:08787ef063cb | 75 | rcount += r; |
mbed_official | 47:08787ef063cb | 76 | remaining -= r; |
mbed_official | 47:08787ef063cb | 77 | } |
mbed_official | 47:08787ef063cb | 78 | if (r < 0) { |
mbed_official | 47:08787ef063cb | 79 | printf("Error! socket.recv() returned: %d\n", r); |
mbed_official | 56:02a6401ec508 | 80 | goto DISCONNECT; |
mbed_official | 47:08787ef063cb | 81 | } |
mbed_official | 47:08787ef063cb | 82 | |
mbed_official | 13:ed9e4aa00044 | 83 | printf("recv %d [%.*s]\n", rcount, strstr(buffer, "\r\n")-buffer, buffer); |
mbed_official | 13:ed9e4aa00044 | 84 | |
mbed_official | 13:ed9e4aa00044 | 85 | // The api.ipify.org service also gives us the device's external IP address |
mbed_official | 47:08787ef063cb | 86 | p = strstr(buffer, "\r\n\r\n")+4; |
mbed_official | 47:08787ef063cb | 87 | printf("External IP address: %.*s\n", rcount-(p-buffer), p); |
mbed_official | 47:08787ef063cb | 88 | delete[] buffer; |
mbed_official | 0:17bd84fc5087 | 89 | |
mbed_official | 56:02a6401ec508 | 90 | DISCONNECT: |
mbed_official | 0:17bd84fc5087 | 91 | // Close the socket to return its memory and bring down the network interface |
mbed_official | 0:17bd84fc5087 | 92 | socket.close(); |
mbed_official | 0:17bd84fc5087 | 93 | |
mbed_official | 5:3e952c60d705 | 94 | // Bring down the ethernet interface |
mbed_official | 49:1923a727df5b | 95 | net->disconnect(); |
mbed_official | 0:17bd84fc5087 | 96 | printf("Done\n"); |
mbed_official | 0:17bd84fc5087 | 97 | } |