Skip to content
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
3 changes: 2 additions & 1 deletion ESP8266Interface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -210,10 +210,11 @@ int ESP8266Interface::socket_close(void *handle)
int err = 0;
_esp.setTimeout(ESP8266_MISC_TIMEOUT);

if (!_esp.close(socket->id)) {
if (socket->connected && !_esp.close(socket->id)) {
err = NSAPI_ERROR_DEVICE_ERROR;
}

socket->connected = false;
_ids[socket->id] = false;
delete socket;
return err;
Expand Down
2 changes: 1 addition & 1 deletion ESP8266Interface.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ class ESP8266Interface : public NetworkStack, public WiFiInterface
* version is chosen by the stack (defaults to NSAPI_UNSPEC)
* @return 0 on success, negative error code on failure
*/
virtual nsapi_error_t gethostbyname(const char *name, SocketAddress *address, nsapi_version_t version);
virtual nsapi_error_t gethostbyname(const char *name, SocketAddress *address, nsapi_version_t version = NSAPI_UNSPEC);

/** Set the WiFi network credentials
*
Expand Down
14 changes: 11 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
# esp8266 mbed-os driver
The driver for the ESP8266 WiFi module
# The ESP8266 WiFi driver for mbed-os
The mbed OS driver for the ESP8266 WiFi module

## Firmware version
esp8266 modules come in different shapes and forms, but most important difference is which firmware version it is programmed with. To make sure that your module has mbed-os compatible firmware follow update guide: https://developer.mbed.org/teams/ESP8266/wiki/Firmware-Update
ESP8266 modules come in different shapes and forms, but most important difference is which firmware version it is programmed with. To make sure that your module has mbed-os compatible firmware follow update guide: https://developer.mbed.org/teams/ESP8266/wiki/Firmware-Update

## Testing
The ESP8266 library contains the core network tests taken from mbed OS. After installing mbed CLI and importing the mbed OS library, the tests can be ran with the `mbed test` command:
``` bash
# Runs the ESP8266 network tests, requires a wifi access point
mbed test -t <COMPILER HERE> -m <BOARD HERE> -n tests-net* --compile -DMBED_CFG_ESP8266_SSID='"<SSID HERE>"' -DMBED_CFG_ESP8266_PASS='"<PASS HERE>"'
mbed test -t <COMPILER HERE> -m <BOARD HERE> -n tests-net* --run --verbose
```
1 change: 1 addition & 0 deletions TESTS/net/.mbedignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
host_tests/*
82 changes: 82 additions & 0 deletions TESTS/net/connectivity/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
#include "mbed.h"
#include "greentea-client/test_env.h"
#include "unity.h"
#include "utest.h"

#include "ESP8266Interface.h"

using namespace utest::v1;

#ifndef MBED_CFG_ESP8266_TX
#define MBED_CFG_ESP8266_TX D1
#endif

#ifndef MBED_CFG_ESP8266_RX
#define MBED_CFG_ESP8266_RX D0
#endif

#ifndef MBED_CFG_ESP8266_DEBUG
#define MBED_CFG_ESP8266_DEBUG false
#endif


// Bringing the network up and down
template <int COUNT>
void test_bring_up_down() {
ESP8266Interface net(MBED_CFG_ESP8266_TX, MBED_CFG_ESP8266_RX, MBED_CFG_ESP8266_DEBUG);
net.set_credentials(MBED_CFG_ESP8266_SSID, MBED_CFG_ESP8266_PASS);

for (int i = 0; i < COUNT; i++) {
int err = net.connect();
TEST_ASSERT_EQUAL(0, err);

printf("MBED: IP Address %s\r\n", net.get_ip_address());
printf("MBED: Netmask %s\r\n", net.get_netmask());
printf("MBED: Gateway %s\r\n", net.get_gateway());
TEST_ASSERT(net.get_ip_address());
TEST_ASSERT(net.get_netmask());
TEST_ASSERT(net.get_gateway());

UDPSocket udp;
err = udp.open(&net);
TEST_ASSERT_EQUAL(0, err);
err = udp.close();
TEST_ASSERT_EQUAL(0, err);

TCPSocket tcp;
err = tcp.open(&net);
TEST_ASSERT_EQUAL(0, err);
err = tcp.close();
TEST_ASSERT_EQUAL(0, err);

err = net.disconnect();
TEST_ASSERT_EQUAL(0, err);
}
}


// Test setup
utest::v1::status_t test_setup(const size_t number_of_cases) {
char uuid[48] = {0};
GREENTEA_SETUP_UUID(120, "default_auto", uuid, sizeof(uuid));

// create mac address based on uuid
uint64_t mac = 0;
for (int i = 0; i < sizeof(uuid); i++) {
mac += uuid[i];
}
mbed_set_mac_address((const char*)mac, /*coerce control bits*/ 1);

return verbose_test_setup_handler(number_of_cases);
}

Case cases[] = {
Case("Bringing the network up and down", test_bring_up_down<1>),
Case("Bringing the network up and down twice", test_bring_up_down<2>),
};

Specification specification(test_setup, cases);

int main() {
return !Harness::run(specification);
}
124 changes: 124 additions & 0 deletions TESTS/net/gethostbyname/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
#include "mbed.h"
#include "greentea-client/test_env.h"
#include "unity.h"
#include "utest.h"
#include "ESP8266Interface.h"

using namespace utest::v1;

// Hostname for testing against
// Must have A and AAAA records
#ifndef MBED_DNS_TEST_HOST
#define MBED_DNS_TEST_HOST "connector.mbed.com"
#endif

#ifndef MBED_CFG_ESP8266_TX
#define MBED_CFG_ESP8266_TX D1
#endif

#ifndef MBED_CFG_ESP8266_RX
#define MBED_CFG_ESP8266_RX D0
#endif

#ifndef MBED_CFG_ESP8266_DEBUG
#define MBED_CFG_ESP8266_DEBUG false
#endif

// Address info from stack
const char *ip_literal;
nsapi_version_t ip_pref;
const char *ip_pref_repr;

// Network setup
ESP8266Interface net(MBED_CFG_ESP8266_TX, MBED_CFG_ESP8266_RX, MBED_CFG_ESP8266_DEBUG);
void net_bringup() {
int err = net.connect(MBED_CFG_ESP8266_SSID, MBED_CFG_ESP8266_PASS);
TEST_ASSERT_EQUAL(0, err);
printf("MBED: Connected to network\n");
printf("MBED: IP Address: %s\n", net.get_ip_address());

ip_literal = net.get_ip_address();
ip_pref = SocketAddress(ip_literal).get_ip_version();
ip_pref_repr = (ip_pref == NSAPI_IPv4) ? "ipv4" :
(ip_pref == NSAPI_IPv6) ? "ipv6" : "unspec";
}


// DNS tests
void test_dns_query() {
SocketAddress addr;
int err = net.gethostbyname(MBED_DNS_TEST_HOST, &addr);
printf("DNS: query \"%s\" => \"%s\"\n",
MBED_DNS_TEST_HOST, addr.get_ip_address());

TEST_ASSERT_EQUAL(0, err);
TEST_ASSERT((bool)addr);
TEST_ASSERT(strlen(addr.get_ip_address()) > 1);
}

void test_dns_query_pref() {
SocketAddress addr;
int err = net.gethostbyname(MBED_DNS_TEST_HOST, &addr, ip_pref);
printf("DNS: query %s \"%s\" => \"%s\"\n",
ip_pref_repr, MBED_DNS_TEST_HOST, addr.get_ip_address());

TEST_ASSERT_EQUAL(0, err);
TEST_ASSERT((bool)addr);
TEST_ASSERT(strlen(addr.get_ip_address()) > 1);
TEST_ASSERT_EQUAL(ip_pref, addr.get_ip_version());
}

void test_dns_literal() {
SocketAddress addr;
int err = net.gethostbyname(ip_literal, &addr);
printf("DNS: literal \"%s\" => \"%s\"\n",
ip_literal, addr.get_ip_address());

TEST_ASSERT_EQUAL(0, err);
TEST_ASSERT((bool)addr);
TEST_ASSERT(strlen(addr.get_ip_address()) > 1);
TEST_ASSERT(strcmp(ip_literal, addr.get_ip_address()) == 0);
}

void test_dns_literal_pref() {
SocketAddress addr;
int err = net.gethostbyname(ip_literal, &addr, ip_pref);
printf("DNS: literal %s \"%s\" => \"%s\"\n",
ip_pref_repr, ip_literal, addr.get_ip_address());

TEST_ASSERT_EQUAL(0, err);
TEST_ASSERT((bool)addr);
TEST_ASSERT(strlen(addr.get_ip_address()) > 1);
TEST_ASSERT_EQUAL(ip_pref, addr.get_ip_version());
TEST_ASSERT(strcmp(ip_literal, addr.get_ip_address()) == 0);
}


// Test setup
utest::v1::status_t test_setup(const size_t number_of_cases) {
char uuid[48] = {0};
GREENTEA_SETUP_UUID(120, "default_auto", uuid, 48);

// create mac address based on uuid
uint64_t mac = 0;
for (int i = 0; i < sizeof(uuid); i++) {
mac += uuid[i];
}
mbed_set_mac_address((const char*)mac, /*coerce control bits*/ 1);
net_bringup();

return verbose_test_setup_handler(number_of_cases);
}

Case cases[] = {
Case("DNS query", test_dns_query),
Case("DNS preference query", test_dns_query_pref),
Case("DNS literal", test_dns_literal),
Case("DNS preference literal", test_dns_literal_pref),
};

Specification specification(test_setup, cases);

int main() {
return !Harness::run(specification);
}
Loading