@@ -458,120 +458,82 @@ ssize_t ATHandler::read_bytes(uint8_t *buf, size_t len)
458
458
return read_len;
459
459
}
460
460
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 )
462
462
{
463
463
tr_debug (" %s" , __func__);
464
- at_debug (" \n ----------read_string buff:----------\n " );
464
+ at_debug (" \n ----------read buff:----------\n " );
465
465
for (size_t i = _recv_pos; i < _recv_len; i++) {
466
466
at_debug (" %c" , _recv_buff[i]);
467
467
}
468
- at_debug (" \n ----------buff ----------\n " );
468
+ at_debug (" \n ----------read end ----------\n " );
469
469
470
470
if (_last_err || !_stop_tag || (_stop_tag->found && read_even_stop_tag == false )) {
471
471
return -1 ;
472
472
}
473
473
474
- uint8_t *pbuf = (uint8_t *)buf;
475
-
476
- size_t len = 0 ;
477
474
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 ;
530
475
size_t upper = 0 , lower = 0 ;
531
- size_t hex_size = size*2 ;
476
+ size_t read_size = hex ? size*2 : size ;
532
477
533
478
uint8_t *pbuf = (uint8_t *)buf;
534
479
535
480
consume_char (' \" ' );
536
481
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++) {
538
486
char c = get_char ();
487
+ buf_idx = hex ? read_idx/2 : read_idx;
539
488
if (c == -1 ) {
540
- pbuf[str_len ] = ' \0 ' ;
489
+ pbuf[buf_idx ] = ' \0 ' ;
541
490
set_error (NSAPI_ERROR_DEVICE_ERROR);
542
491
return -1 ;
543
492
} else if (c == _delimiter) {
544
- pbuf[str_len ] = ' \0 ' ;
493
+ pbuf[buf_idx ] = ' \0 ' ;
545
494
break ;
546
495
} else if (c == ' \" ' ) {
547
496
match_pos = 0 ;
548
- if (str_len > 0 ) {
549
- str_len --;
497
+ if (read_idx > 0 ) {
498
+ read_idx --;
550
499
}
551
500
continue ;
552
501
} else if (_stop_tag->len && c == _stop_tag->tag [match_pos]) {
553
502
match_pos++;
554
503
if (match_pos == _stop_tag->len ) {
555
504
_stop_tag->found = true ;
556
505
// 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 ' ;
559
508
break ;
560
509
}
561
510
} else if (match_pos) {
562
511
match_pos = 0 ;
563
512
}
564
513
565
- if (hex_len % 2 == 0 ) {
566
- upper = hex_str_to_int (&c, 1 ) ;
514
+ if (!hex ) {
515
+ pbuf[buf_idx] = c ;
567
516
} 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
+ }
571
523
}
572
524
}
573
525
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 );
575
537
}
576
538
577
539
int32_t ATHandler::read_int ()
0 commit comments