Skip to content

Commit 380973a

Browse files
author
Cruz Monrreal
authored
Merge pull request #6691 from mirelachirica/cellular_fixes
Cellular fixes
2 parents 6ccc963 + c0629c8 commit 380973a

File tree

7 files changed

+74
-31
lines changed

7 files changed

+74
-31
lines changed

features/cellular/TESTS/socket/udp/main.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ class EchoSocket : public UDPSocket {
8282
_data[i] = (uint8_t)rand();
8383
}
8484
// clear pending events
85-
while ((EchoSocket::eventFlags.wait_any(_async_flag, SOCKET_TIMEOUT) & (osFlagsError | _async_flag)) == _async_flag);
85+
TEST_ASSERT(!(EchoSocket::eventFlags.clear(_async_flag) & osFlagsError));
8686
if (hostname) {
8787
TEST_ASSERT(sendto(hostname, ECHO_SERVER_UDP_PORT, _data, _size) == _size);
8888
} else {

features/cellular/framework/AT/ATHandler.cpp

Lines changed: 42 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,11 @@
2525
#include "rtos/Thread.h"
2626
#endif
2727
#include "Kernel.h"
28+
#include "CellularUtil.h"
2829

2930
using namespace mbed;
3031
using namespace events;
32+
using namespace mbed_cellular_util;
3133

3234
//#define MBED_TRACE_MAX_LEVEL TRACE_LEVEL_DEBUG
3335
#include "CellularLog.h"
@@ -456,59 +458,82 @@ ssize_t ATHandler::read_bytes(uint8_t *buf, size_t len)
456458
return read_len;
457459
}
458460

459-
ssize_t ATHandler::read_string(char *buf, size_t size, bool read_even_stop_tag)
461+
ssize_t ATHandler::read(char *buf, size_t size, bool read_even_stop_tag, bool hex)
460462
{
461463
tr_debug("%s", __func__);
462-
at_debug("\n----------read_string buff:----------\n");
464+
at_debug("\n----------read buff:----------\n");
463465
for (size_t i = _recv_pos; i < _recv_len; i++) {
464466
at_debug("%c", _recv_buff[i]);
465467
}
466-
at_debug("\n----------buff----------\n");
468+
at_debug("\n----------read end----------\n");
467469

468470
if (_last_err || !_stop_tag || (_stop_tag->found && read_even_stop_tag == false)) {
469471
return -1;
470472
}
471473

472-
uint8_t *pbuf = (uint8_t*)buf;
473-
474-
size_t len = 0;
475474
size_t match_pos = 0;
475+
size_t upper = 0, lower = 0;
476+
size_t read_size = hex ? size*2 : size;
477+
478+
uint8_t *pbuf = (uint8_t*)buf;
476479

477480
consume_char('\"');
478481

479-
for (; len < (size + match_pos); len++) {
480-
int c = get_char();
482+
size_t read_idx = 0;
483+
size_t buf_idx = 0;
484+
485+
for (; read_idx < (read_size + match_pos); read_idx++) {
486+
char c = get_char();
487+
buf_idx = hex ? read_idx/2 : read_idx;
481488
if (c == -1) {
482-
pbuf[len] = '\0';
489+
pbuf[buf_idx] = '\0';
483490
set_error(NSAPI_ERROR_DEVICE_ERROR);
484491
return -1;
485492
} else if (c == _delimiter) {
486-
pbuf[len] = '\0';
493+
pbuf[buf_idx] = '\0';
487494
break;
488495
} else if (c == '\"') {
489496
match_pos = 0;
490-
if (len > 0) {
491-
len--;
497+
if (read_idx > 0) {
498+
read_idx--;
492499
}
493500
continue;
494501
} else if (_stop_tag->len && c == _stop_tag->tag[match_pos]) {
495502
match_pos++;
496503
if (match_pos == _stop_tag->len) {
497504
_stop_tag->found = true;
498505
// remove tag from string if it was matched
499-
len -= (_stop_tag->len - 1);
500-
pbuf[len] = '\0';
506+
buf_idx -= (_stop_tag->len - 1);
507+
pbuf[buf_idx] = '\0';
501508
break;
502509
}
503510
} else if (match_pos) {
504511
match_pos = 0;
505512
}
506513

507-
pbuf[len] = c;
514+
if (!hex) {
515+
pbuf[buf_idx] = c;
516+
} else {
517+
if (read_idx % 2 == 0) {
518+
upper = hex_str_to_int(&c, 1);
519+
} else {
520+
lower = hex_str_to_int(&c, 1);
521+
pbuf[buf_idx] = ((upper<<4) & 0xF0) | (lower & 0x0F);
522+
}
523+
}
508524
}
509525

510-
// Do we need _stop_found set after reading by size -> is _stop_tag_by_len needed or not?
511-
return len;
526+
return buf_idx + 1;
527+
}
528+
529+
ssize_t ATHandler::read_string(char *buf, size_t size, bool read_even_stop_tag)
530+
{
531+
return read(buf, size, read_even_stop_tag, false);
532+
}
533+
534+
ssize_t ATHandler::read_hex_string(char *buf, size_t size)
535+
{
536+
return read(buf, size, false, true);
512537
}
513538

514539
int32_t ATHandler::read_int()

features/cellular/framework/AT/ATHandler.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,17 @@ class ATHandler
303303
*/
304304
ssize_t read_string(char *str, size_t size, bool read_even_stop_tag = false);
305305

306+
/** Reads chars representing hex ascii values and converts them to the corresponding chars.
307+
* For example: "4156" to "AV".
308+
* Terminates with null. Skips the quotation marks.
309+
* Stops on delimiter or stop tag.
310+
*
311+
* @param str output buffer for the read
312+
* @param size maximum number of chars to output
313+
* @return length of output string or -1 in case of read timeout before delimiter or stop tag is found
314+
*/
315+
ssize_t read_hex_string(char *str, size_t size);
316+
306317
/** Reads as string and converts result to integer. Supports only positive integers.
307318
*
308319
* @return the positive integer or -1 in case of error.
@@ -482,6 +493,8 @@ class ATHandler
482493

483494
// check is urc is already added
484495
bool find_urc_handler(const char *prefix, mbed::Callback<void()> callback);
496+
497+
ssize_t read(char *buf, size_t size, bool read_even_stop_tag, bool hex);
485498
};
486499

487500
} // namespace mbed

features/cellular/framework/common/CellularList.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,16 @@ template <class T> class CellularList
3838
_tail=NULL;
3939
}
4040

41+
~CellularList()
42+
{
43+
T *temp = _head;
44+
while (temp) {
45+
_head = _head->next;
46+
delete temp;
47+
temp = _head;
48+
}
49+
}
50+
4151
T* add_new()
4252
{
4353
T *temp=new T;
@@ -88,6 +98,7 @@ template <class T> class CellularList
8898
delete temp;
8999
temp = _head;
90100
}
101+
_tail=NULL;
91102
}
92103

93104

features/cellular/framework/targets/QUECTEL/BC95/QUECTEL_BC95_CellularStack.cpp

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ nsapi_size_or_error_t QUECTEL_BC95_CellularStack::socket_sendto_impl(CellularSoc
154154
_at.write_int(socket->id);
155155
_at.write_string(address.get_ip_address(), false);
156156
_at.write_int(address.get_port());
157-
_at.write_int(size);
157+
_at.write_int(size <= BC95_MAX_PACKET_SIZE ? size : BC95_MAX_PACKET_SIZE);
158158
_at.write_string(hexstr, false);
159159
_at.cmd_stop();
160160
_at.resp_start();
@@ -178,25 +178,23 @@ nsapi_size_or_error_t QUECTEL_BC95_CellularStack::socket_recvfrom_impl(CellularS
178178
nsapi_size_or_error_t recv_len=0;
179179
int port;
180180
char ip_address[NSAPI_IP_SIZE];
181-
char *hexstr = new char[BC95_MAX_PACKET_SIZE*2+1];
182181

183182
_at.cmd_start("AT+NSORF=");
184183
_at.write_int(socket->id);
185-
_at.write_int(size);
184+
_at.write_int(size <= BC95_MAX_PACKET_SIZE ? size : BC95_MAX_PACKET_SIZE);
186185
_at.cmd_stop();
187186
_at.resp_start();
188187
// receiving socket id
189188
_at.skip_param();
190189
_at.read_string(ip_address, sizeof(ip_address));
191190
port = _at.read_int();
192191
recv_len = _at.read_int();
193-
int hexlen = _at.read_string(hexstr, BC95_MAX_PACKET_SIZE*2+1);
192+
int hexlen = _at.read_hex_string((char*)buffer, size);
194193
// remaining length
195194
_at.skip_param();
196195
_at.resp_stop();
197196

198197
if (!recv_len || (recv_len == -1) || (_at.get_last_error() != NSAPI_ERROR_OK)) {
199-
delete hexstr;
200198
return NSAPI_ERROR_WOULD_BLOCK;
201199
}
202200

@@ -205,10 +203,8 @@ nsapi_size_or_error_t QUECTEL_BC95_CellularStack::socket_recvfrom_impl(CellularS
205203
address->set_port(port);
206204
}
207205

208-
if (hexlen > 0) {
209-
hex_str_to_char_str((const char*) hexstr, hexlen, (char*)buffer);
206+
if (recv_len != hexlen) {
207+
tr_error("Not received as much data as expected. Should receive: %d bytes, received: %d bytes", recv_len, hexlen);
210208
}
211-
212-
delete hexstr;
213209
return recv_len;
214210
}

features/cellular/framework/targets/QUECTEL/BC95/QUECTEL_BC95_CellularStack.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
#include "AT_CellularStack.h"
2222

2323
#define BC95_SOCKET_MAX 7
24-
#define BC95_MAX_PACKET_SIZE 512
24+
#define BC95_MAX_PACKET_SIZE 1358
2525

2626
namespace mbed {
2727

features/cellular/framework/targets/QUECTEL/BG96/QUECTEL_BG96_CellularNetwork.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,7 @@ QUECTEL_BG96_CellularNetwork::~QUECTEL_BG96_CellularNetwork()
3030

3131
bool QUECTEL_BG96_CellularNetwork::get_modem_stack_type(nsapi_ip_stack_t requested_stack)
3232
{
33-
if ((requested_stack == IPV4_STACK) ||
34-
(requested_stack == IPV6_STACK) ||
35-
(requested_stack == IPV4V6_STACK)) {
33+
if (requested_stack == IPV4_STACK) {
3634
return true;
3735
}
3836

0 commit comments

Comments
 (0)