Skip to content
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

Refactoring - organizing socket info etc #102

Merged
merged 7 commits into from
Oct 10, 2018
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
30 changes: 11 additions & 19 deletions ESP8266/ESP8266.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ ESP8266::ESP8266(PinName tx, PinName rx, bool debug, PinName rts, PinName cts)
_fail(false),
_sock_already(false),
_closed(false),
_connection_status(NSAPI_STATUS_DISCONNECTED)
_conn_status(NSAPI_STATUS_DISCONNECTED)
{
_serial.set_baud( ESP8266_DEFAULT_BAUD_RATE );
_parser.debug_on(debug);
Expand All @@ -58,7 +58,7 @@ ESP8266::ESP8266(PinName tx, PinName rx, bool debug, PinName rts, PinName cts)
_parser.oob("+CWJAP:", callback(this, &ESP8266::_oob_connect_err));
_parser.oob("WIFI ", callback(this, &ESP8266::_oob_connection_status));
_parser.oob("UNLINK", callback(this, &ESP8266::_oob_socket_close_err));
_parser.oob("ALREADY CONNECTED", callback(this, &ESP8266::_oob_cipstart_already));
_parser.oob("ALREADY CONNECTED", callback(this, &ESP8266::_oob_conn_already));

for(int i= 0; i < SOCKET_COUNT; i++) {
_sock_i[i].open = false;
Expand Down Expand Up @@ -774,14 +774,9 @@ void ESP8266::sigio(Callback<void()> func)
_serial.sigio(func);
}

void ESP8266::attach(mbed::Callback<void(nsapi_event_t, intptr_t)> status_cb)
void ESP8266::attach(Callback<void()> status_cb)
{
_connection_status_cb = status_cb;
}

void ESP8266::attach_int(mbed::Callback<void()> status_cb)
{
_conn_state_drv_cb = status_cb;
_conn_stat_cb = status_cb;
}

bool ESP8266::_recv_ap(nsapi_wifi_ap_t *ap)
Expand Down Expand Up @@ -814,7 +809,7 @@ void ESP8266::_oob_connect_err()
}


void ESP8266::_oob_cipstart_already()
void ESP8266::_oob_conn_already()
{
_sock_already = true;
_parser.abort();
Expand Down Expand Up @@ -858,11 +853,11 @@ void ESP8266::_oob_connection_status()
char status[13];
if (_parser.recv("%12[^\"]\n", status)) {
if (strcmp(status, "GOT IP\n") == 0) {
_connection_status = NSAPI_STATUS_GLOBAL_UP;
_conn_status = NSAPI_STATUS_GLOBAL_UP;
} else if (strcmp(status, "DISCONNECT\n") == 0) {
_connection_status = NSAPI_STATUS_DISCONNECTED;
_conn_status = NSAPI_STATUS_DISCONNECTED;
} else if (strcmp(status, "CONNECTED\n") == 0) {
_connection_status = NSAPI_STATUS_CONNECTING;
_conn_status = NSAPI_STATUS_CONNECTING;
} else {
MBED_ERROR(MBED_MAKE_ERROR(MBED_MODULE_DRIVER, MBED_ERROR_CODE_EBADMSG), \
"ESP8266::_oob_connection_status: invalid AT cmd\n");
Expand All @@ -872,11 +867,8 @@ void ESP8266::_oob_connection_status()
"ESP8266::_oob_connection_status: network status timed out\n");
}

if(_connection_status_cb) {
_connection_status_cb(NSAPI_EVENT_CONNECTION_STATUS_CHANGE, _connection_status);
}

_conn_state_drv_cb();
MBED_ASSERT(_conn_stat_cb);
_conn_stat_cb();
}

int8_t ESP8266::default_wifi_mode()
Expand Down Expand Up @@ -907,5 +899,5 @@ bool ESP8266::set_default_wifi_mode(const int8_t mode)

nsapi_connection_status_t ESP8266::connection_status() const
{
return _connection_status;
return _conn_status;
}
22 changes: 7 additions & 15 deletions ESP8266/ESP8266.h
Original file line number Diff line number Diff line change
Expand Up @@ -294,22 +294,15 @@ class ESP8266
}

/**
* Attach a function to call whenever network state has changed. Driver external
* Attach a function to call whenever network state has changed.
*
* @param func A pointer to a void function, or 0 to set as none
*/
void attach(mbed::Callback<void(nsapi_event_t, intptr_t)> status_cb);

/**
* Attach a function to call whenever network state has changed. Driver internal
*
* @param func A pointer to a void function, or 0 to set as none
*/
void attach_int(mbed::Callback<void()> status_cb);
void attach(mbed::Callback<void()> status_cb);

template <typename T, typename M>
void attach_int(T *obj, M method) {
attach_int(Callback<void()>(obj, method));
void attach(T *obj, M method) {
attach(mbed::Callback<void()>(obj, method));
}

/**
Expand Down Expand Up @@ -394,7 +387,7 @@ class ESP8266
// OOB message handlers
void _oob_packet_hdlr();
void _oob_connect_err();
void _oob_cipstart_already();
void _oob_conn_already();
void _oob_socket0_closed();
void _oob_socket1_closed();
void _oob_socket2_closed();
Expand Down Expand Up @@ -423,9 +416,8 @@ class ESP8266
struct _sock_info _sock_i[SOCKET_COUNT];

// Connection state reporting
nsapi_connection_status_t _connection_status;
Callback<void(nsapi_event_t, intptr_t)> _connection_status_cb; // Application registered
Callback<void()> _conn_state_drv_cb; // ESP8266Interface registered
nsapi_connection_status_t _conn_status;
Callback<void()> _conn_stat_cb; // ESP8266Interface registered
};

#endif
72 changes: 42 additions & 30 deletions ESP8266Interface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,38 +45,48 @@
#if defined MBED_CONF_ESP8266_TX && defined MBED_CONF_ESP8266_RX
ESP8266Interface::ESP8266Interface()
: _esp(MBED_CONF_ESP8266_TX, MBED_CONF_ESP8266_RX, MBED_CONF_ESP8266_DEBUG, MBED_CONF_ESP8266_RTS, MBED_CONF_ESP8266_CTS),
_ap_sec(NSAPI_SECURITY_UNKNOWN),
_initialized(false),
_started(false)
_started(false),
_conn_stat(NSAPI_STATUS_DISCONNECTED),
_conn_stat_cb(NULL)
{
memset(_ids, 0, sizeof(_ids));
memset(_cbs, 0, sizeof(_cbs));
memset(ap_ssid, 0, sizeof(ap_ssid));
memset(ap_pass, 0, sizeof(ap_pass));
memset(_local_ports, 0, sizeof(_local_ports));
ap_sec = NSAPI_SECURITY_UNKNOWN;

_esp.sigio(this, &ESP8266Interface::event);
_esp.set_timeout();
_esp.attach_int(this, &ESP8266Interface::update_conn_state_cb);
_esp.attach(this, &ESP8266Interface::update_conn_state_cb);

for(int i= 0; i < ESP8266_SOCKET_COUNT; i++) {
_sock_i[i].open = false;
_sock_i[i].sport = -1;
}
}
#endif

// ESP8266Interface implementation
ESP8266Interface::ESP8266Interface(PinName tx, PinName rx, bool debug, PinName rts, PinName cts)
: _esp(tx, rx, debug, rts, cts),
_ap_sec(NSAPI_SECURITY_UNKNOWN),
_initialized(false),
_started(false)
_started(false),
_conn_stat(NSAPI_STATUS_DISCONNECTED),
_conn_stat_cb(NULL)
{
memset(_ids, 0, sizeof(_ids));
memset(_cbs, 0, sizeof(_cbs));
memset(ap_ssid, 0, sizeof(ap_ssid));
memset(ap_pass, 0, sizeof(ap_pass));
memset(_local_ports, 0, sizeof(_local_ports));
ap_sec = NSAPI_SECURITY_UNKNOWN;

_esp.sigio(this, &ESP8266Interface::event);
_esp.set_timeout();
_esp.attach_int(this, &ESP8266Interface::update_conn_state_cb);
_esp.attach(this, &ESP8266Interface::update_conn_state_cb);

for(int i= 0; i < ESP8266_SOCKET_COUNT; i++) {
_sock_i[i].open = false;
_sock_i[i].sport = -1;
}
}

int ESP8266Interface::connect(const char *ssid, const char *pass, nsapi_security_t security,
Expand All @@ -102,7 +112,7 @@ int ESP8266Interface::connect()
return NSAPI_ERROR_NO_SSID;
}

if (ap_sec != NSAPI_SECURITY_NONE) {
if (_ap_sec != NSAPI_SECURITY_NONE) {
if (strlen(ap_pass) < ESP8266_PASSPHRASE_MIN_LENGTH) {
return NSAPI_ERROR_PARAMETER;
}
Expand Down Expand Up @@ -141,7 +151,7 @@ int ESP8266Interface::connect()

int ESP8266Interface::set_credentials(const char *ssid, const char *pass, nsapi_security_t security)
{
ap_sec = security;
_ap_sec = security;

if (!ssid) {
return NSAPI_ERROR_PARAMETER;
Expand All @@ -157,7 +167,7 @@ int ESP8266Interface::set_credentials(const char *ssid, const char *pass, nsapi_
return NSAPI_ERROR_PARAMETER;
}

if (ap_sec != NSAPI_SECURITY_NONE) {
if (_ap_sec != NSAPI_SECURITY_NONE) {

if (!pass) {
return NSAPI_ERROR_PARAMETER;
Expand Down Expand Up @@ -261,11 +271,6 @@ bool ESP8266Interface::_get_firmware_ok()
return true;
}

bool ESP8266Interface::_disable_default_softap()
{
return _esp.set_default_wifi_mode(ESP8266::WIFIMODE_STATION);
}

nsapi_error_t ESP8266Interface::_init(void)
{
if (!_initialized) {
Expand All @@ -284,7 +289,7 @@ nsapi_error_t ESP8266Interface::_init(void)
if (!_get_firmware_ok()) {
return NSAPI_ERROR_DEVICE_ERROR;
}
if (!_disable_default_softap()) {
if (!_esp.set_default_wifi_mode(ESP8266::WIFIMODE_STATION)) {
return NSAPI_ERROR_DEVICE_ERROR;
}
if (!_esp.cond_enable_tcp_passive_mode()) {
Expand Down Expand Up @@ -320,9 +325,9 @@ int ESP8266Interface::socket_open(void **handle, nsapi_protocol_t proto)
int id = -1;

for (int i = 0; i < ESP8266_SOCKET_COUNT; i++) {
if (!_ids[i]) {
if (!_sock_i[i].open) {
id = i;
_ids[i] = true;
_sock_i[i].open = true;
break;
}
}
Expand Down Expand Up @@ -358,8 +363,8 @@ int ESP8266Interface::socket_close(void *handle)
}

socket->connected = false;
_ids[socket->id] = false;
_local_ports[socket->id] = 0;
_sock_i[socket->id].open = false;
_sock_i[socket->id].sport = -1;
delete socket;
return err;
}
Expand All @@ -378,13 +383,13 @@ int ESP8266Interface::socket_bind(void *handle, const SocketAddress &address)
}

for(int id = 0; id < ESP8266_SOCKET_COUNT; id++) {
if(_local_ports[id] == address.get_port() && id != socket->id) { // Port already reserved by another socket
if(_sock_i[id].sport == address.get_port() && id != socket->id) { // Port already reserved by another socket
return NSAPI_ERROR_PARAMETER;
} else if (id == socket->id && socket->connected) {
return NSAPI_ERROR_PARAMETER;
}
}
_local_ports[socket->id] = address.get_port();
_sock_i[socket->id].sport = address.get_port();
return 0;
}

Expand All @@ -406,7 +411,7 @@ int ESP8266Interface::socket_connect(void *handle, const SocketAddress &addr)
}

if (socket->proto == NSAPI_UDP) {
ret = _esp.open_udp(socket->id, addr.get_ip_address(), addr.get_port(), _local_ports[socket->id]);
ret = _esp.open_udp(socket->id, addr.get_ip_address(), addr.get_port(), _sock_i[socket->id].sport);
} else {
ret = _esp.open_tcp(socket->id, addr.get_ip_address(), addr.get_port(), socket->keepalive);
}
Expand Down Expand Up @@ -579,12 +584,12 @@ void ESP8266Interface::event()

void ESP8266Interface::attach(mbed::Callback<void(nsapi_event_t, intptr_t)> status_cb)
{
_esp.attach(status_cb);
_conn_stat_cb = status_cb;
}

nsapi_connection_status_t ESP8266Interface::get_connection_status() const
{
return _esp.connection_status();
return _conn_stat;
}

#if MBED_CONF_ESP8266_PROVIDE_DEFAULT
Expand All @@ -598,7 +603,9 @@ WiFiInterface *WiFiInterface::get_default_instance() {

void ESP8266Interface::update_conn_state_cb()
{
switch(_esp.connection_status()) {
_conn_stat = _esp.connection_status();

switch(_conn_stat) {
// Doesn't require changes
case NSAPI_STATUS_CONNECTING:
case NSAPI_STATUS_GLOBAL_UP:
Expand All @@ -612,6 +619,11 @@ void ESP8266Interface::update_conn_state_cb()
case NSAPI_STATUS_LOCAL_UP:
case NSAPI_STATUS_ERROR_UNSUPPORTED:
default:
break;
MBED_ASSERT(false);
}

// Inform upper layers
if (_conn_stat_cb) {
_conn_stat_cb(NSAPI_EVENT_CONNECTION_STATUS_CHANGE, _conn_stat);
}
}
36 changes: 23 additions & 13 deletions ESP8266Interface.h
Original file line number Diff line number Diff line change
Expand Up @@ -318,32 +318,42 @@ class ESP8266Interface : public NetworkStack, public WiFiInterface
}

private:
// AT layer
ESP8266 _esp;
void update_conn_state_cb();

// Credentials
static const int ESP8266_SSID_MAX_LENGTH = 32; /* 32 is what 802.11 defines as longest possible name */
char ap_ssid[ESP8266_SSID_MAX_LENGTH + 1]; /* The longest possible name; +1 for the \0 */
static const int ESP8266_PASSPHRASE_MAX_LENGTH = 63; /* The longest allowed passphrase */
static const int ESP8266_PASSPHRASE_MIN_LENGTH = 8; /* The shortest allowed passphrase */
char ap_pass[ESP8266_PASSPHRASE_MAX_LENGTH + 1]; /* The longest possible passphrase; +1 for the \0 */
nsapi_security_t _ap_sec;

ESP8266 _esp;
bool _ids[ESP8266_SOCKET_COUNT];
int _initialized;
int _started;

char ap_ssid[ESP8266_SSID_MAX_LENGTH + 1]; /* 32 is what 802.11 defines as longest possible name; +1 for the \0 */
nsapi_security_t ap_sec;
uint8_t ap_ch;
char ap_pass[ESP8266_PASSPHRASE_MAX_LENGTH + 1];
uint16_t _local_ports[ESP8266_SOCKET_COUNT];
// Drivers's socket info
struct _sock_info {
bool open;
uint16_t sport;
};
struct _sock_info _sock_i[ESP8266_SOCKET_COUNT];

bool _disable_default_softap();
void event();
void update_conn_state_cb();
// Driver's state
int _initialized;
bool _get_firmware_ok();
nsapi_error_t _init(void);
int _started;
nsapi_error_t _startup(const int8_t wifi_mode);

//sigio
struct {
void (*callback)(void *);
void *data;
} _cbs[ESP8266_SOCKET_COUNT];
void event();

// Connection state reporting to application
nsapi_connection_status_t _conn_stat;
Callback<void(nsapi_event_t, intptr_t)> _conn_stat_cb;
};

#endif
Loading