Skip to content

Commit 0425869

Browse files
author
Mirela Chirica
committed
Cellular: Common routine for string and hexstring reading
1 parent b482389 commit 0425869

File tree

2 files changed

+36
-72
lines changed

2 files changed

+36
-72
lines changed

features/cellular/framework/AT/ATHandler.cpp

Lines changed: 34 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -458,120 +458,82 @@ ssize_t ATHandler::read_bytes(uint8_t *buf, size_t len)
458458
return read_len;
459459
}
460460

461-
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)
462462
{
463463
tr_debug("%s", __func__);
464-
at_debug("\n----------read_string buff:----------\n");
464+
at_debug("\n----------read buff:----------\n");
465465
for (size_t i = _recv_pos; i < _recv_len; i++) {
466466
at_debug("%c", _recv_buff[i]);
467467
}
468-
at_debug("\n----------buff----------\n");
468+
at_debug("\n----------read end----------\n");
469469

470470
if (_last_err || !_stop_tag || (_stop_tag->found && read_even_stop_tag == false)) {
471471
return -1;
472472
}
473473

474-
uint8_t *pbuf = (uint8_t*)buf;
475-
476-
size_t len = 0;
477474
size_t match_pos = 0;
478-
479-
consume_char('\"');
480-
481-
for (; len < (size + match_pos); len++) {
482-
int c = get_char();
483-
if (c == -1) {
484-
pbuf[len] = '\0';
485-
set_error(NSAPI_ERROR_DEVICE_ERROR);
486-
return -1;
487-
} else if (c == _delimiter) {
488-
pbuf[len] = '\0';
489-
break;
490-
} else if (c == '\"') {
491-
match_pos = 0;
492-
if (len > 0) {
493-
len--;
494-
}
495-
continue;
496-
} else if (_stop_tag->len && c == _stop_tag->tag[match_pos]) {
497-
match_pos++;
498-
if (match_pos == _stop_tag->len) {
499-
_stop_tag->found = true;
500-
// remove tag from string if it was matched
501-
len -= (_stop_tag->len - 1);
502-
pbuf[len] = '\0';
503-
break;
504-
}
505-
} else if (match_pos) {
506-
match_pos = 0;
507-
}
508-
509-
pbuf[len] = c;
510-
}
511-
512-
// Do we need _stop_found set after reading by size -> is _stop_tag_by_len needed or not?
513-
return len;
514-
}
515-
516-
ssize_t ATHandler::read_hex_string(char *buf, size_t size)
517-
{
518-
tr_debug("%s", __func__);
519-
at_debug("\n----------read_hex_string buff:----------\n");
520-
for (size_t i = _recv_pos; i < _recv_len; i++) {
521-
at_debug("%c", _recv_buff[i]);
522-
}
523-
at_debug("\n----------read_hex_string end----------\n");
524-
525-
if (_last_err || !_stop_tag || _stop_tag->found) {
526-
return -1;
527-
}
528-
529-
size_t match_pos = 0, str_len = 0;
530475
size_t upper = 0, lower = 0;
531-
size_t hex_size = size*2;
476+
size_t read_size = hex ? size*2 : size;
532477

533478
uint8_t *pbuf = (uint8_t*)buf;
534479

535480
consume_char('\"');
536481

537-
for (size_t hex_len = 0; hex_len < (hex_size + match_pos); hex_len++) {
482+
size_t read_idx = 0;
483+
size_t buf_idx = 0;
484+
485+
for (; read_idx < (read_size + match_pos); read_idx++) {
538486
char c = get_char();
487+
buf_idx = hex ? read_idx/2 : read_idx;
539488
if (c == -1) {
540-
pbuf[str_len] = '\0';
489+
pbuf[buf_idx] = '\0';
541490
set_error(NSAPI_ERROR_DEVICE_ERROR);
542491
return -1;
543492
} else if (c == _delimiter) {
544-
pbuf[str_len] = '\0';
493+
pbuf[buf_idx] = '\0';
545494
break;
546495
} else if (c == '\"') {
547496
match_pos = 0;
548-
if (str_len > 0) {
549-
str_len--;
497+
if (read_idx > 0) {
498+
read_idx--;
550499
}
551500
continue;
552501
} else if (_stop_tag->len && c == _stop_tag->tag[match_pos]) {
553502
match_pos++;
554503
if (match_pos == _stop_tag->len) {
555504
_stop_tag->found = true;
556505
// remove tag from string if it was matched
557-
str_len -= (_stop_tag->len - 1);
558-
pbuf[str_len] = '\0';
506+
buf_idx -= (_stop_tag->len - 1);
507+
pbuf[buf_idx] = '\0';
559508
break;
560509
}
561510
} else if (match_pos) {
562511
match_pos = 0;
563512
}
564513

565-
if (hex_len % 2 == 0) {
566-
upper = hex_str_to_int(&c, 1);
514+
if (!hex) {
515+
pbuf[buf_idx] = c;
567516
} else {
568-
lower = hex_str_to_int(&c, 1);
569-
pbuf[str_len] = ((upper<<4) & 0xF0) | (lower & 0x0F);
570-
str_len++;
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+
}
571523
}
572524
}
573525

574-
return str_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);
575537
}
576538

577539
int32_t ATHandler::read_int()

features/cellular/framework/AT/ATHandler.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -493,6 +493,8 @@ class ATHandler
493493

494494
// check is urc is already added
495495
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);
496498
};
497499

498500
} // namespace mbed

0 commit comments

Comments
 (0)