Skip to content

Commit aff5546

Browse files
author
Mirela Chirica
committed
Cellular: Fix for ATHandler's read string and hexstring NULL termination
1 parent bcec185 commit aff5546

File tree

2 files changed

+69
-21
lines changed

2 files changed

+69
-21
lines changed

features/cellular/framework/AT/ATHandler.cpp

Lines changed: 68 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -454,31 +454,88 @@ ssize_t ATHandler::read_bytes(uint8_t *buf, size_t len)
454454
return read_len;
455455
}
456456

457-
ssize_t ATHandler::read(char *buf, size_t size, bool read_even_stop_tag, bool hex)
457+
ssize_t ATHandler::read_string(char *buf, size_t size, bool read_even_stop_tag)
458458
{
459459
if (_last_err || !_stop_tag || (_stop_tag->found && read_even_stop_tag == false)) {
460460
return -1;
461461
}
462462

463+
consume_char('\"');
464+
465+
if (_last_err) {
466+
return -1;
467+
}
468+
469+
size_t len = 0;
470+
size_t match_pos = 0;
471+
472+
for (; len < (size - 1 + match_pos); len++) {
473+
int c = get_char();
474+
if (c == -1) {
475+
set_error(NSAPI_ERROR_DEVICE_ERROR);
476+
return -1;
477+
} else if (c == _delimiter) {
478+
buf[len] = '\0';
479+
break;
480+
} else if (c == '\"') {
481+
match_pos = 0;
482+
len--;
483+
continue;
484+
} else if (_stop_tag->len && c == _stop_tag->tag[match_pos]) {
485+
match_pos++;
486+
if (match_pos == _stop_tag->len) {
487+
_stop_tag->found = true;
488+
// remove tag from string if it was matched
489+
len -= (_stop_tag->len - 1);
490+
buf[len] = '\0';
491+
break;
492+
}
493+
} else if (match_pos) {
494+
match_pos = 0;
495+
}
496+
497+
buf[len] = c;
498+
}
499+
500+
if (len && (len == size - 1 + match_pos)) {
501+
buf[len] = '\0';
502+
}
503+
504+
return len;
505+
}
506+
507+
ssize_t ATHandler::read_hex_string(char *buf, size_t size)
508+
{
509+
if (_last_err || !_stop_tag || _stop_tag->found) {
510+
return -1;
511+
}
512+
463513
size_t match_pos = 0;
464-
size_t read_size = hex ? size * 2 : size;
465514

466515
consume_char('\"');
467516

517+
if (_last_err) {
518+
return -1;
519+
}
520+
468521
size_t read_idx = 0;
469522
size_t buf_idx = 0;
470523
char hexbuf[2];
471524

472-
for (; read_idx < (read_size + match_pos); read_idx++) {
525+
for (; read_idx < size * 2 + match_pos; read_idx++) {
473526
int c = get_char();
474-
buf_idx = hex ? read_idx / 2 : read_idx;
527+
528+
if (match_pos) {
529+
buf_idx++;
530+
} else {
531+
buf_idx = read_idx / 2;
532+
}
533+
475534
if (c == -1) {
476-
buf[buf_idx] = '\0';
477535
set_error(NSAPI_ERROR_DEVICE_ERROR);
478536
return -1;
479537
}
480538
if (c == _delimiter) {
481-
buf[buf_idx] = '\0';
482539
break;
483540
} else if (c == '\"') {
484541
match_pos = 0;
@@ -490,14 +547,13 @@ ssize_t ATHandler::read(char *buf, size_t size, bool read_even_stop_tag, bool he
490547
_stop_tag->found = true;
491548
// remove tag from string if it was matched
492549
buf_idx -= (_stop_tag->len - 1);
493-
buf[buf_idx] = '\0';
494550
break;
495551
}
496552
} else if (match_pos) {
497553
match_pos = 0;
498554
}
499555

500-
if (!hex) {
556+
if (match_pos) {
501557
buf[buf_idx] = c;
502558
} else {
503559
hexbuf[read_idx % 2] = c;
@@ -507,17 +563,11 @@ ssize_t ATHandler::read(char *buf, size_t size, bool read_even_stop_tag, bool he
507563
}
508564
}
509565

510-
return buf_idx;
511-
}
512-
513-
ssize_t ATHandler::read_string(char *buf, size_t size, bool read_even_stop_tag)
514-
{
515-
return read(buf, size, read_even_stop_tag, false);
516-
}
566+
if (read_idx && (read_idx == size * 2 + match_pos)) {
567+
buf_idx++;
568+
}
517569

518-
ssize_t ATHandler::read_hex_string(char *buf, size_t size)
519-
{
520-
return read(buf, size, false, true);
570+
return buf_idx;
521571
}
522572

523573
int32_t ATHandler::read_int()

features/cellular/framework/AT/ATHandler.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,7 @@ class ATHandler {
302302
* Stops on delimiter or stop tag.
303303
*
304304
* @param str output buffer for the read
305-
* @param size maximum number of chars to output
305+
* @param size maximum number of chars to output including NULL
306306
* @param read_even_stop_tag if true then try to read even if the stop tag was found previously
307307
* @return length of output string or -1 in case of read timeout before delimiter or stop tag is found
308308
*/
@@ -512,8 +512,6 @@ class ATHandler {
512512
// check is urc is already added
513513
bool find_urc_handler(const char *prefix, mbed::Callback<void()> callback);
514514

515-
ssize_t read(char *buf, size_t size, bool read_even_stop_tag, bool hex);
516-
517515
// print contents of a buffer to trace log
518516
void debug_print(char *p, int len);
519517
};

0 commit comments

Comments
 (0)