Skip to content

Commit 3bcf513

Browse files
author
Ari Parkkila
committed
Cellular: Handling of TCP endpoint closed
1 parent 5aaf42c commit 3bcf513

File tree

3 files changed

+56
-8
lines changed

3 files changed

+56
-8
lines changed

features/cellular/framework/AT/AT_CellularContext.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,8 @@ nsapi_error_t AT_CellularContext::connect()
112112
if (_is_connected) {
113113
return NSAPI_ERROR_IS_CONNECTED;
114114
}
115+
call_network_cb(NSAPI_STATUS_CONNECTING);
116+
115117
nsapi_error_t err = _device->attach_to_network();
116118
_cb_data.error = check_operation(err, OP_CONNECT);
117119

@@ -130,6 +132,10 @@ nsapi_error_t AT_CellularContext::connect()
130132
}
131133
}
132134

135+
if (_cb_data.error == NSAPI_ERROR_ALREADY) {
136+
return NSAPI_ERROR_OK;
137+
}
138+
133139
return _cb_data.error;
134140
}
135141

@@ -543,8 +549,6 @@ nsapi_error_t AT_CellularContext::activate_context()
543549

544550
void AT_CellularContext::do_connect()
545551
{
546-
call_network_cb(NSAPI_STATUS_CONNECTING);
547-
548552
if (!_is_context_active) {
549553
_cb_data.error = do_activate_context();
550554
#if !NSAPI_PPP_AVAILABLE
@@ -677,10 +681,11 @@ nsapi_error_t AT_CellularContext::disconnect()
677681
} else {
678682
deactivate_ip_context();
679683
}
684+
} else {
685+
call_network_cb(NSAPI_STATUS_DISCONNECTED);
680686
}
681687

682688
_is_connected = false;
683-
call_network_cb(NSAPI_STATUS_DISCONNECTED);
684689

685690
return _at.unlock_return_error();
686691
}
@@ -736,11 +741,6 @@ void AT_CellularContext::deactivate_context()
736741
_at.write_int(_cid);
737742
_at.cmd_stop_read_resp();
738743
}
739-
740-
_at.clear_error();
741-
_at.cmd_start("AT+CGATT=0");
742-
_at.cmd_stop_read_resp();
743-
_at.restore_at_timeout();
744744
}
745745

746746
nsapi_error_t AT_CellularContext::get_apn_backoff_timer(int &backoff_timer)

features/cellular/framework/AT/AT_CellularStack.cpp

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,19 @@ nsapi_size_or_error_t AT_CellularStack::socket_sendto(nsapi_socket_t handle, con
253253
return NSAPI_ERROR_DEVICE_ERROR;
254254
}
255255

256+
if (socket->closed && !socket->rx_avail) {
257+
tr_info("sendto socket %d closed", socket->id);
258+
return NSAPI_ERROR_NO_CONNECTION;
259+
}
260+
261+
if (size == 0) {
262+
if (socket->proto == NSAPI_UDP) {
263+
return NSAPI_ERROR_UNSUPPORTED;
264+
} else if (socket->proto == NSAPI_TCP) {
265+
return 0;
266+
}
267+
}
268+
256269
nsapi_size_or_error_t ret_val = NSAPI_ERROR_OK;
257270

258271
if (!socket->created) {
@@ -299,6 +312,11 @@ nsapi_size_or_error_t AT_CellularStack::socket_recvfrom(nsapi_socket_t handle, S
299312
return NSAPI_ERROR_DEVICE_ERROR;
300313
}
301314

315+
if (socket->closed) {
316+
tr_info("recvfrom socket %d closed", socket->id);
317+
return 0;
318+
}
319+
302320
nsapi_size_or_error_t ret_val = NSAPI_ERROR_OK;
303321

304322
if (!socket->created) {
@@ -319,6 +337,11 @@ nsapi_size_or_error_t AT_CellularStack::socket_recvfrom(nsapi_socket_t handle, S
319337

320338
_at.unlock();
321339

340+
if (socket->closed) {
341+
tr_info("recvfrom socket %d closed", socket->id);
342+
return 0;
343+
}
344+
322345
if (ret_val >= 0) {
323346
if (addr) {
324347
tr_info("Socket %d recv %d bytes from %s port %d", find_socket_index(socket), ret_val, addr->get_ip_address(), addr->get_port());
@@ -352,3 +375,18 @@ int AT_CellularStack::get_socket_index_by_port(uint16_t port)
352375
}
353376
return -1;
354377
}
378+
379+
AT_CellularStack::CellularSocket *AT_CellularStack::find_socket(int sock_id)
380+
{
381+
CellularSocket *sock = NULL;
382+
for (int i = 0; i < _socket_count; i++) {
383+
if (_socket[i] && _socket[i]->id == sock_id) {
384+
sock = _socket[i];
385+
break;
386+
}
387+
}
388+
if (!sock) {
389+
tr_error("Socket not found %d", sock_id);
390+
}
391+
return sock;
392+
}

features/cellular/framework/AT/AT_CellularStack.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ class AT_CellularStack : public NetworkStack, public AT_CellularBase {
9292
_cb(NULL),
9393
_data(NULL),
9494
created(false),
95+
closed(false),
9596
started(false),
9697
tx_ready(false),
9798
rx_avail(false),
@@ -108,6 +109,7 @@ class AT_CellularStack : public NetworkStack, public AT_CellularBase {
108109
void (*_cb)(void *);
109110
void *_data;
110111
bool created; // socket has been created on modem stack
112+
bool closed; // socket has been closed by a peer
111113
bool started; // socket has been opened on modem stack
112114
bool tx_ready; // socket is ready for sending on modem stack
113115
bool rx_avail; // socket has data for reading on modem stack
@@ -166,6 +168,14 @@ class AT_CellularStack : public NetworkStack, public AT_CellularBase {
166168
virtual nsapi_size_or_error_t socket_recvfrom_impl(CellularSocket *socket, SocketAddress *address,
167169
void *buffer, nsapi_size_t size) = 0;
168170

171+
/**
172+
* Find the socket handle based on socket identifier
173+
*
174+
* @param sock_id Socket identifier
175+
* @return Socket handle, NULL on error
176+
*/
177+
CellularSocket *find_socket(int sock_id);
178+
169179
// socket container
170180
CellularSocket **_socket;
171181

0 commit comments

Comments
 (0)