From 570d96ffd58c57fb8f9fc6ddc390fcab44d5192a Mon Sep 17 00:00:00 2001 From: Veijo Pesonen Date: Mon, 8 Oct 2018 14:33:55 +0300 Subject: [PATCH] Structure driver's socket info --- ESP8266Interface.cpp | 28 +++++++++++++++++----------- ESP8266Interface.h | 10 ++++++++-- 2 files changed, 25 insertions(+), 13 deletions(-) diff --git a/ESP8266Interface.cpp b/ESP8266Interface.cpp index 8409082..9c866d8 100644 --- a/ESP8266Interface.cpp +++ b/ESP8266Interface.cpp @@ -51,15 +51,18 @@ ESP8266Interface::ESP8266Interface() _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)); _esp.sigio(this, &ESP8266Interface::event); _esp.set_timeout(); _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 @@ -72,15 +75,18 @@ ESP8266Interface::ESP8266Interface(PinName tx, PinName rx, bool debug, PinName r _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)); _esp.sigio(this, &ESP8266Interface::event); _esp.set_timeout(); _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, @@ -324,9 +330,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; } } @@ -362,8 +368,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; } @@ -382,13 +388,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; } @@ -410,7 +416,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); } diff --git a/ESP8266Interface.h b/ESP8266Interface.h index 8f914f6..9a5bf84 100644 --- a/ESP8266Interface.h +++ b/ESP8266Interface.h @@ -323,14 +323,20 @@ class ESP8266Interface : public NetworkStack, public WiFiInterface static const int ESP8266_PASSPHRASE_MIN_LENGTH = 8; /* The shortest allowed passphrase */ ESP8266 _esp; - bool _ids[ESP8266_SOCKET_COUNT]; + + // Drivers's socket info + struct _sock_info { + bool open; + uint16_t sport; + }; + struct _sock_info _sock_i[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; char ap_pass[ESP8266_PASSPHRASE_MAX_LENGTH + 1]; - uint16_t _local_ports[ESP8266_SOCKET_COUNT]; bool _disable_default_softap(); void event();