Skip to content

Commit

Permalink
Structure driver's socket info
Browse files Browse the repository at this point in the history
  • Loading branch information
Veijo Pesonen committed Oct 10, 2018
1 parent 71123ed commit 570d96f
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 13 deletions.
28 changes: 17 additions & 11 deletions ESP8266Interface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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,
Expand Down Expand Up @@ -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;
}
}
Expand Down Expand Up @@ -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;
}
Expand All @@ -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;
}

Expand All @@ -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);
}
Expand Down
10 changes: 8 additions & 2 deletions ESP8266Interface.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit 570d96f

Please sign in to comment.