Skip to content

Commit 570d96f

Browse files
author
Veijo Pesonen
committed
Structure driver's socket info
1 parent 71123ed commit 570d96f

File tree

2 files changed

+25
-13
lines changed

2 files changed

+25
-13
lines changed

ESP8266Interface.cpp

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -51,15 +51,18 @@ ESP8266Interface::ESP8266Interface()
5151
_conn_stat(NSAPI_STATUS_DISCONNECTED),
5252
_conn_stat_cb(NULL)
5353
{
54-
memset(_ids, 0, sizeof(_ids));
5554
memset(_cbs, 0, sizeof(_cbs));
5655
memset(ap_ssid, 0, sizeof(ap_ssid));
5756
memset(ap_pass, 0, sizeof(ap_pass));
58-
memset(_local_ports, 0, sizeof(_local_ports));
5957

6058
_esp.sigio(this, &ESP8266Interface::event);
6159
_esp.set_timeout();
6260
_esp.attach(this, &ESP8266Interface::update_conn_state_cb);
61+
62+
for(int i= 0; i < ESP8266_SOCKET_COUNT; i++) {
63+
_sock_i[i].open = false;
64+
_sock_i[i].sport = -1;
65+
}
6366
}
6467
#endif
6568

@@ -72,15 +75,18 @@ ESP8266Interface::ESP8266Interface(PinName tx, PinName rx, bool debug, PinName r
7275
_conn_stat(NSAPI_STATUS_DISCONNECTED),
7376
_conn_stat_cb(NULL)
7477
{
75-
memset(_ids, 0, sizeof(_ids));
7678
memset(_cbs, 0, sizeof(_cbs));
7779
memset(ap_ssid, 0, sizeof(ap_ssid));
7880
memset(ap_pass, 0, sizeof(ap_pass));
79-
memset(_local_ports, 0, sizeof(_local_ports));
8081

8182
_esp.sigio(this, &ESP8266Interface::event);
8283
_esp.set_timeout();
8384
_esp.attach(this, &ESP8266Interface::update_conn_state_cb);
85+
86+
for(int i= 0; i < ESP8266_SOCKET_COUNT; i++) {
87+
_sock_i[i].open = false;
88+
_sock_i[i].sport = -1;
89+
}
8490
}
8591

8692
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)
324330
int id = -1;
325331

326332
for (int i = 0; i < ESP8266_SOCKET_COUNT; i++) {
327-
if (!_ids[i]) {
333+
if (!_sock_i[i].open) {
328334
id = i;
329-
_ids[i] = true;
335+
_sock_i[i].open = true;
330336
break;
331337
}
332338
}
@@ -362,8 +368,8 @@ int ESP8266Interface::socket_close(void *handle)
362368
}
363369

364370
socket->connected = false;
365-
_ids[socket->id] = false;
366-
_local_ports[socket->id] = 0;
371+
_sock_i[socket->id].open = false;
372+
_sock_i[socket->id].sport = -1;
367373
delete socket;
368374
return err;
369375
}
@@ -382,13 +388,13 @@ int ESP8266Interface::socket_bind(void *handle, const SocketAddress &address)
382388
}
383389

384390
for(int id = 0; id < ESP8266_SOCKET_COUNT; id++) {
385-
if(_local_ports[id] == address.get_port() && id != socket->id) { // Port already reserved by another socket
391+
if(_sock_i[id].sport == address.get_port() && id != socket->id) { // Port already reserved by another socket
386392
return NSAPI_ERROR_PARAMETER;
387393
} else if (id == socket->id && socket->connected) {
388394
return NSAPI_ERROR_PARAMETER;
389395
}
390396
}
391-
_local_ports[socket->id] = address.get_port();
397+
_sock_i[socket->id].sport = address.get_port();
392398
return 0;
393399
}
394400

@@ -410,7 +416,7 @@ int ESP8266Interface::socket_connect(void *handle, const SocketAddress &addr)
410416
}
411417

412418
if (socket->proto == NSAPI_UDP) {
413-
ret = _esp.open_udp(socket->id, addr.get_ip_address(), addr.get_port(), _local_ports[socket->id]);
419+
ret = _esp.open_udp(socket->id, addr.get_ip_address(), addr.get_port(), _sock_i[socket->id].sport);
414420
} else {
415421
ret = _esp.open_tcp(socket->id, addr.get_ip_address(), addr.get_port(), socket->keepalive);
416422
}

ESP8266Interface.h

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -323,14 +323,20 @@ class ESP8266Interface : public NetworkStack, public WiFiInterface
323323
static const int ESP8266_PASSPHRASE_MIN_LENGTH = 8; /* The shortest allowed passphrase */
324324

325325
ESP8266 _esp;
326-
bool _ids[ESP8266_SOCKET_COUNT];
326+
327+
// Drivers's socket info
328+
struct _sock_info {
329+
bool open;
330+
uint16_t sport;
331+
};
332+
struct _sock_info _sock_i[ESP8266_SOCKET_COUNT];
333+
327334
int _initialized;
328335
int _started;
329336

330337
char ap_ssid[ESP8266_SSID_MAX_LENGTH + 1]; /* 32 is what 802.11 defines as longest possible name; +1 for the \0 */
331338
nsapi_security_t _ap_sec;
332339
char ap_pass[ESP8266_PASSPHRASE_MAX_LENGTH + 1];
333-
uint16_t _local_ports[ESP8266_SOCKET_COUNT];
334340

335341
bool _disable_default_softap();
336342
void event();

0 commit comments

Comments
 (0)