Skip to content

Commit 400fd82

Browse files
authored
Merge pull request #9705 from AriParkkila/cellular-socket-gt
Cellular: Fix cellular stack and drivers for netsocket tests
2 parents 23c494b + 605a42d commit 400fd82

File tree

14 files changed

+275
-146
lines changed

14 files changed

+275
-146
lines changed

UNITTESTS/features/cellular/framework/AT/at_cellularcontext/at_cellularcontexttest.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -473,7 +473,7 @@ TEST_F(TestAT_CellularContext, connect_disconnect_sync)
473473
network_cb_count = 0;
474474
// connect in sync mode, semaphore will return 0 so timeout is returned
475475
ASSERT_EQ(ctx.connect(), NSAPI_ERROR_TIMEOUT);
476-
ASSERT_EQ(network_cb_count, 0);
476+
ASSERT_EQ(network_cb_count, 1);
477477

478478
my_AT_CTX ctx1(at, &dev);
479479
ctx1.attach(&network_cb);
@@ -486,7 +486,7 @@ TEST_F(TestAT_CellularContext, connect_disconnect_sync)
486486

487487
ASSERT_EQ(ctx1.connect(), NSAPI_ERROR_OK);
488488

489-
ASSERT_EQ(network_cb_count, 4);
489+
ASSERT_EQ(network_cb_count, 5);
490490

491491
ASSERT_EQ(ctx1.connect(), NSAPI_ERROR_IS_CONNECTED);
492492

features/cellular/framework/AT/ATHandler.cpp

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ using namespace mbed_cellular_util;
3737
#define PROCESS_URC_TIME 20
3838

3939
// Suppress logging of very big packet payloads, maxlen is approximate due to write/read are cached
40-
#define DEBUG_MAXLEN 80
40+
#define DEBUG_MAXLEN 60
41+
#define DEBUG_END_MARK "..\r"
4142

4243
const char *mbed::OK = "OK\r\n";
4344
const uint8_t OK_LENGTH = 4;
@@ -463,7 +464,7 @@ ssize_t ATHandler::read_bytes(uint8_t *buf, size_t len)
463464
}
464465
buf[read_len] = c;
465466
if (_debug_on && read_len >= DEBUG_MAXLEN) {
466-
debug_print("..", sizeof(".."));
467+
debug_print(DEBUG_END_MARK, sizeof(DEBUG_END_MARK) - 1);
467468
_debug_on = false;
468469
}
469470
}
@@ -555,9 +556,15 @@ ssize_t ATHandler::read_hex_string(char *buf, size_t size)
555556
size_t buf_idx = 0;
556557
char hexbuf[2];
557558

559+
bool debug_on = _debug_on;
558560
for (; read_idx < size * 2 + match_pos; read_idx++) {
559561
int c = get_char();
560562

563+
if (_debug_on && read_idx >= DEBUG_MAXLEN) {
564+
debug_print(DEBUG_END_MARK, sizeof(DEBUG_END_MARK) - 1);
565+
_debug_on = false;
566+
}
567+
561568
if (match_pos) {
562569
buf_idx++;
563570
} else {
@@ -595,6 +602,7 @@ ssize_t ATHandler::read_hex_string(char *buf, size_t size)
595602
}
596603
}
597604
}
605+
_debug_on = debug_on;
598606

599607
if (read_idx && (read_idx == size * 2 + match_pos)) {
600608
buf_idx++;
@@ -1168,7 +1176,7 @@ size_t ATHandler::write(const void *data, size_t len)
11681176
if (write_len + ret < DEBUG_MAXLEN) {
11691177
debug_print((char *)data + write_len, ret);
11701178
} else {
1171-
debug_print("..", sizeof(".."));
1179+
debug_print(DEBUG_END_MARK, sizeof(DEBUG_END_MARK) - 1);
11721180
_debug_on = false;
11731181
}
11741182
}
@@ -1228,7 +1236,7 @@ void ATHandler::debug_print(const char *p, int len)
12281236
debug("\n");
12291237
} else if (c == '\n') {
12301238
} else {
1231-
debug("[%d]", c);
1239+
debug("#%02x", c);
12321240
}
12331241
} else {
12341242
debug("%c", c);

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

features/cellular/framework/device/CellularDevice.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ nsapi_error_t CellularDevice::start_state_machine(CellularStateMachine::Cellular
129129
_mutex.lock();
130130
nsapi_error_t err = create_state_machine();
131131
if (err) {
132+
_mutex.unlock();
132133
return err;
133134
}
134135

0 commit comments

Comments
 (0)