Skip to content

Add NetworkSocket (TCP) examples #95

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 12, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions APIs_NetworkSocket/DNS_GetHostbyNameAsync/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# DNS_GetHostbyNameAsync

This example shows how to make asynchronous DNS host name resolution.

**Note:** The current example is limited to the ethernet interface on supported devices.
To use the example with a different interface, you will need to modify main.cpp and
replace the EthernetInterface class with the appropriate interface.
42 changes: 42 additions & 0 deletions APIs_NetworkSocket/DNS_GetHostbyNameAsync/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Copyright (c) 2006-2020 Arm Limited and affiliates.
* SPDX-License-Identifier: Apache-2.0
*/
#include "mbed.h"
#include "nsapi_types.h"
#include "EthernetInterface.h"
#include "SocketAddress.h"
#include "Semaphore.h"

rtos::Semaphore callback_semaphore;
SocketAddress address;
nsapi_error_t result;

// Callback for asynchronous host name resolution
void hostbyname_callback(nsapi_error_t res, SocketAddress *addr)
{
// Store result and release semaphore
result = res;
address = *addr;
callback_semaphore.release();
}

int main()
{
// Initialise network interface
EthernetInterface eth;
eth.connect();

// Initiate asynchronous DNS host name resolution
eth.gethostbyname_async("www.mbed.com", hostbyname_callback);

// Wait for callback semaphore
callback_semaphore.acquire();

// Print result
printf("Result %s, Address %s\r\n", result == NSAPI_STATUS_GLOBAL_UP ? "OK" : "FAIL",
result == NSAPI_STATUS_GLOBAL_UP ? address.get_ip_address() : "NONE");

// Disconnect network interface
eth.disconnect();
}
7 changes: 7 additions & 0 deletions APIs_NetworkSocket/TCPServer/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# TCPServer

This example shows how to accepts single telnet client connection and send buffer data.

**Note:** The current example is limited to the ethernet interface on supported
devices. To use the example with a different interface, you will need to modify
main.cpp and replace the EthernetInterface class with the appropriate interface.
51 changes: 51 additions & 0 deletions APIs_NetworkSocket/TCPServer/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* Copyright (c) 2006-2020 Arm Limited and affiliates.
* SPDX-License-Identifier: Apache-2.0
*/
#include "mbed.h"
#include "EthernetInterface.h"
#include "TCPServer.h"
#include "TCPSocket.h"

int main()
{
printf("TCP server example\n");

EthernetInterface eth;
eth.connect();

SocketAddress a;
eth.get_ip_address(&a);
a.set_port(23);

printf("The Server IP address is '%s'\n", a.get_ip_address());

TCPSocket srv;
TCPSocket *client_sock;
SocketAddress client_addr;
char *buffer = new char[256];

/* Open the server on ethernet stack */
srv.open(&eth);

/* Accepts a connection on a socket. */
srv.bind(a);

/* Can handle x simultaneous connections */
srv.listen(1);

nsapi_error_t error;
client_sock = srv.accept(&error);

client_sock->getpeername(&a);

printf("Accepted %s:%d\n", a.get_ip_address(),
a.get_port());
strcpy(buffer, "Hello \n\r");
client_sock->send(buffer, strlen(buffer));
client_sock->recv(buffer, 256);

client_sock->close();
srv.close();
delete[] buffer;
}
11 changes: 11 additions & 0 deletions APIs_NetworkSocket/TCPSocket/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# TCPSocket Example

This is a quick example of a simple HTTP client program using the
network-socket API that is provided as a part of [mbed-os](github.com/armmbed/mbed-os).

The program brings up an underlying network interface, and uses it to perform an HTTP
transaction over a TCPSocket.

**Note:** The current example is limited to the ethernet interface on supported devices.
To use the example with a different interface, you will need to modify main.cpp and
replace the EthernetInterface class with the appropriate interface.
46 changes: 46 additions & 0 deletions APIs_NetworkSocket/TCPSocket/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* Copyright (c) 2006-2020 Arm Limited and affiliates.
* SPDX-License-Identifier: Apache-2.0
*/
#include "mbed.h"
#include "EthernetInterface.h"

// Network interface
EthernetInterface net;

// Socket demo
int main()
{
// Bring up the ethernet interface
printf("Ethernet socket example\n");
net.connect();

// Show the network address
SocketAddress a;
net.get_ip_address(&a);
printf("IP address: %s\n", a.get_ip_address() ? a.get_ip_address() : "None");

// Open a socket on the network interface, and create a TCP connection to mbed.org
TCPSocket socket;
socket.open(&net);

net.gethostbyname("ifconfig.io", &a);
a.set_port(80);
socket.connect(a);
// Send a simple http request
char sbuffer[] = "GET / HTTP/1.1\r\nHost: ifconfig.io\r\n\r\n";
int scount = socket.send(sbuffer, sizeof sbuffer);
printf("sent %d [%.*s]\n", scount, strstr(sbuffer, "\r\n") - sbuffer, sbuffer);

// Recieve a simple http response and print out the response line
char rbuffer[64];
int rcount = socket.recv(rbuffer, sizeof rbuffer);
printf("recv %d [%.*s]\n", rcount, strstr(rbuffer, "\r\n") - rbuffer, rbuffer);

// Close the socket to return its memory and bring down the network interface
socket.close();

// Bring down the ethernet interface
net.disconnect();
printf("Done\n");
}
57 changes: 57 additions & 0 deletions APIs_NetworkSocket/TCPSocketWiFi/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# mbed-os-example-wifi

WiFi example for mbed OS

# Getting started with the WiFi API

This is a quick example of a simple WiFi application using the WiFi and network-socket APIs that is provided as a part of [mbed-os](github.com/armmbed/mbed-os).

The program brings up the WiFi and the underlying network interface, and uses it to scans available networks, connects to a network, prints interface and connection details and performs simple HTTP operation.

# Supported hardware

* [UBLOX Odin board](https://developer.mbed.org/platforms/ublox-EVK-ODIN-W2/) (`UBLOX_EVK_ODIN_W2` target when using mbed CLI)
* Other mbed target with ESP2866 module (Board it's connected to shouldn't have other network interface eg. Ethernet)

ESP2866 is a fallback option and will be used if the build is for unsupported platform.

# Connecting the ESP2866

ESP module needs to be connected to RX and TX UART pins (+ power and ground) on your target board. That can be achieved using Grove shield or connected directly using jumper wires, please note that not all Arduino form factor boards have UART compatible with the Grove shiled.

For Grove shield TX has to be connected to D0 and RX to D1.

Make sure that UART module you're connecting ESP to is different than the debug UART connected to your USB port.

# Getting started

1. Import the example

```
mbed import mbed-os-example-wifi
cd mbed-os-example-wifi
```
2. Configure the WiFi credentials

Edit ```mbed_app.json``` to include correct SSID and Password:

```
"config": {
"wifi-ssid": {
"help": "WiFi SSID",
"value": "\"SSID\""
},
"wifi-password": {
"help": "WiFi Password",
"value": "\"Password\""
}
},
```

3. Compile, generate binary and flash target

For example, for `GCC`:

```
mbed compile -t GCC_ARM -m UBLOX_EVK_ODIN_W2 -f
```
116 changes: 116 additions & 0 deletions APIs_NetworkSocket/TCPSocketWiFi/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
/*
* Copyright (c) 2006-2020 Arm Limited and affiliates.
* SPDX-License-Identifier: Apache-2.0
*/
#include "mbed.h"
#include "TCPSocket.h"

#if TARGET_UBLOX_EVK_ODIN_W2
#include "OdinWiFiInterface.h"
OdinWiFiInterface wifi;
#else
#if !TARGET_FF_ARDUINO
#error [NOT_SUPPORTED] Only Arduino form factor devices are supported at this time
#endif
#include "ESP8266Interface.h"
ESP8266Interface wifi(D1, D0);
#endif

const char *sec2str(nsapi_security_t sec)
{
switch (sec) {
case NSAPI_SECURITY_NONE:
return "None";
case NSAPI_SECURITY_WEP:
return "WEP";
case NSAPI_SECURITY_WPA:
return "WPA";
case NSAPI_SECURITY_WPA2:
return "WPA2";
case NSAPI_SECURITY_WPA_WPA2:
return "WPA/WPA2";
case NSAPI_SECURITY_UNKNOWN:
default:
return "Unknown";
}
}

void scan_demo(WiFiInterface *wifi)
{
WiFiAccessPoint *ap;

printf("Scan:\r\n");

int count = wifi->scan(NULL, 0);

/* Limit number of network arbitrary to 15 */
count = count < 15 ? count : 15;

ap = new WiFiAccessPoint[count];

count = wifi->scan(ap, count);
for (int i = 0; i < count; i++) {
printf("Network: %s secured: %s BSSID: %hhX:%hhX:%hhX:%hhx:%hhx:%hhx RSSI: %hhd Ch: %hhd\r\n", ap[i].get_ssid(),
sec2str(ap[i].get_security()), ap[i].get_bssid()[0], ap[i].get_bssid()[1], ap[i].get_bssid()[2],
ap[i].get_bssid()[3], ap[i].get_bssid()[4], ap[i].get_bssid()[5], ap[i].get_rssi(), ap[i].get_channel());
}
printf("%d networks available.\r\n", count);

delete[] ap;
}

void http_demo(NetworkInterface *net)
{
// Open a socket on the network interface, and create a TCP connection to mbed.org
TCPSocket socket;
socket.open(net);

SocketAddress a;
net->gethostbyname("ifconfig.io", &a);
a.set_port(80);
socket.connect(a);
// Send a simple http request
char sbuffer[] = "GET / HTTP/1.1\r\nHost: ifconfig.io\r\n\r\n";
int scount = socket.send(sbuffer, sizeof sbuffer);
printf("sent %d [%.*s]\n", scount, strstr(sbuffer, "\r\n") - sbuffer, sbuffer);

// Recieve a simple http response and print out the response line
char rbuffer[64];
int rcount = socket.recv(rbuffer, sizeof rbuffer);
printf("recv %d [%.*s]\n", rcount, strstr(rbuffer, "\r\n") - rbuffer, rbuffer);

// Close the socket to return its memory and bring down the network interface
socket.close();
}

int main()
{
SocketAddress a;

printf("WiFi example\r\n\r\n");

scan_demo(&wifi);

printf("\r\nConnecting...\r\n");
int ret = wifi.connect(MBED_CONF_APP_WIFI_SSID, MBED_CONF_APP_WIFI_PASSWORD, NSAPI_SECURITY_WPA_WPA2);
if (ret != 0) {
printf("\r\nConnection error\r\n");
return -1;
}

printf("Success\r\n\r\n");
printf("MAC: %s\r\n", wifi.get_mac_address());
wifi.get_ip_address(&a);
printf("IP: %s\r\n", a.get_ip_address());
wifi.get_netmask(&a);
printf("Netmask: %s\r\n", a.get_ip_address());
wifi.get_gateway(&a);
printf("Gateway: %s\r\n", a.get_ip_address());
printf("RSSI: %d\r\n\r\n", wifi.get_rssi());

http_demo(&wifi);

wifi.disconnect();

printf("\r\nDone\r\n");
}
7 changes: 7 additions & 0 deletions APIs_NetworkSocket/TCPSocket_ConnStateCb/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# TCPSocket Connection State Example

This example shows how to register a network connection state change callback. Possible state changes are listed on the example.

**Note:** The current example is limited to the ethernet interface on supported devices.
To use the example with a different interface, you will need to modify main.cpp and
replace the EthernetInterface class with the appropriate interface.
Loading