@@ -469,97 +469,314 @@ void Test_ATHandler::test_ATHandler_read_bytes()
469
469
{
470
470
EventQueue que;
471
471
FileHandle_stub fh1;
472
+ filehandle_stub_table = NULL ;
473
+ filehandle_stub_table_pos = 0 ;
472
474
473
475
ATHandler at (&fh1, que, 0 , " ," );
474
476
uint8_t buf[5 ];
475
- CHECK (-1 == at.read_bytes (buf, 25 ));
476
477
477
- CHECK (-1 == at.read_bytes (buf, 5 ));
478
+ // TEST EMPTY BUFFER
479
+ // Shouldn't read any byte since buffer is empty
480
+ CHECK (-1 == at.read_bytes (buf, 1 ));
481
+ CHECK (NSAPI_ERROR_DEVICE_ERROR == at.get_last_error ());
482
+ // Return error due to error set to at handler by the above call on empty buffer
483
+ CHECK (-1 == at.read_bytes (buf, 1 ));
478
484
479
- char table[] = " ssssssssssssssssssssssssssssOK\r\n\0 " ;
480
- filehandle_stub_table = table;
485
+ // TEST DATA IN BUFFER
486
+ at.clear_error ();
487
+ char table1[] = " 1234512345678OK\r\n\0 " ;
488
+ filehandle_stub_table = table1;
481
489
filehandle_stub_table_pos = 0 ;
482
490
mbed_poll_stub::revents_value = POLLIN;
483
- mbed_poll_stub::int_value = strlen (table);
484
-
491
+ mbed_poll_stub::int_value = 1 ;;
485
492
486
- at. clear_error ();
493
+ // Read 5 bytes
487
494
CHECK (5 == at.read_bytes (buf, 5 ));
495
+ CHECK (!memcmp (buf, table1, 5 ));
496
+ // get_char triggered above should have filled in the whole reading buffer(fill_buffer())
497
+ CHECK (filehandle_stub_table_pos == (strlen (table1) - 1 ));
498
+ // Read another 8 bytes
499
+ CHECK (8 == at.read_bytes (buf, 8 ) && !memcmp (buf, table1 + 5 , 8 ));
500
+ // Reading more than the 4 bytes left -> ERROR
501
+ CHECK (-1 == at.read_bytes (buf, 5 ));
502
+ CHECK (NSAPI_ERROR_DEVICE_ERROR == at.get_last_error ());
488
503
}
489
504
490
505
void Test_ATHandler::test_ATHandler_read_string ()
491
506
{
492
507
EventQueue que;
493
508
FileHandle_stub fh1;
509
+ filehandle_stub_table = NULL ;
510
+ filehandle_stub_table_pos = 0 ;
494
511
495
512
ATHandler at (&fh1, que, 0 , " ," );
496
513
514
+ // *** EMPTY ***
497
515
at.clear_error ();
498
- char table[] = " \" s,\" OK\r\n\0 " ;
499
- filehandle_stub_table = table;
516
+ char table1[] = " " ;
517
+ at.flush ();
518
+ filehandle_stub_table = table1;
500
519
filehandle_stub_table_pos = 0 ;
501
520
mbed_poll_stub::revents_value = POLLIN;
502
- mbed_poll_stub::int_value = strlen (table);
503
-
504
- char buf[5 ];
505
- uint8_t buf2[5 ];
521
+ mbed_poll_stub::int_value = 1 ;
522
+ char buf1[1 ];
523
+ // No _stop_tag set without resp_start
524
+ CHECK (-1 == at.read_string (buf1, 1 ));
525
+ at.clear_error ();
526
+ // Set _stop_tag to resp_stop(OKCRLF)
506
527
at.resp_start ();
507
- at.read_bytes (buf2, 5 );
508
- CHECK (-1 == at.read_string (buf, 15 ));
509
- at.flush ();
528
+ // Device error because buffer is empty
529
+ CHECK (NSAPI_ERROR_DEVICE_ERROR == at.get_last_error ());
510
530
at.clear_error ();
531
+ // Device error because empty buffer and attempt to fill_buffer by consume_char('\"')
532
+ CHECK (-1 == at.read_string (buf1, 1 ));
511
533
512
- filehandle_stub_table = table;
534
+ // *** 1 BYTE ***
535
+ at.clear_error ();
536
+ char table2[] = " s\0 " ;
537
+ at.flush ();
538
+ filehandle_stub_table = table2;
513
539
filehandle_stub_table_pos = 0 ;
514
-
540
+ mbed_poll_stub::revents_value = POLLIN;
541
+ mbed_poll_stub::int_value = 1 ;
542
+ char buf2[1 ];
543
+ // Set _stop_tag to resp_stop(OKCRLF)
515
544
at.resp_start ();
516
- at.read_bytes (buf2, 1 );
517
- CHECK (1 == at.read_string (buf, 5 , true ));
518
- at.flush ();
545
+ // Device error because no CRLF and no more data to read
546
+ CHECK (NSAPI_ERROR_DEVICE_ERROR == at.get_last_error ());
519
547
at.clear_error ();
548
+ CHECK (0 == at.read_string (buf2, 1 ));
520
549
521
- char table2[] = " \" s\" OK\r\n\0 " ;
522
- filehandle_stub_table = table2;
550
+ // *** CRLF ***
551
+ at.clear_error ();
552
+ char table3[] = " \r\n s\r\n\0 " ;
553
+ at.flush ();
554
+ filehandle_stub_table = table3;
523
555
filehandle_stub_table_pos = 0 ;
524
556
mbed_poll_stub::revents_value = POLLIN;
525
- mbed_poll_stub::int_value = strlen (table2);
557
+ mbed_poll_stub::int_value = 1 ;
558
+ char buf3[1 ];
559
+ // Set _stop_tag to resp_stop(OKCRLF)
560
+ at.resp_start ();
561
+ // OK because after CRLF matched there is more data to read ending in CRLF
562
+ CHECK (NSAPI_ERROR_OK == at.get_last_error ());
563
+ // To read 0 bytes from: s\r\n
564
+ CHECK (0 == at.read_string (buf3, 0 + 1 /* for NULL*/ ));
565
+ // To read 1 byte from: s\r\n -> read s
566
+ CHECK (1 == at.read_string (buf3, 1 + 1 /* for NULL*/ ));
526
567
568
+ // *** Reading more than available in buffer ***
569
+ at.clear_error ();
570
+ char table4[] = " \" s,\" OK\r\n\0 " ;
571
+ at.flush ();
572
+ filehandle_stub_table = table4;
573
+ filehandle_stub_table_pos = 0 ;
574
+ mbed_poll_stub::revents_value = POLLIN;
575
+ mbed_poll_stub::int_value = 1 ;
576
+ char buf4[7 ];
577
+ uint8_t buf5[5 ];
578
+ // NO prefix, NO OK, NO ERROR and NO URC match, CRLF found -> return so buffer could be read
527
579
at.resp_start ();
528
- at.read_bytes (buf2, 1 );
529
- CHECK (1 == at.read_string (buf, 5 , true ));
580
+ // TO read 5 bytes from: "s,"OK\r\n -> read "s,"O
581
+ at.read_bytes (buf5, 5 );
582
+ // K\r\n left to be read -> reading more than 3 + 1(for NULL) -> ERROR
583
+ CHECK (-1 == at.read_string (buf4, 4 + 1 /* for NULL*/ ));
584
+ CHECK (NSAPI_ERROR_DEVICE_ERROR == at.get_last_error ());
585
+
586
+ // *** Encountering delimiter after reading 1 byte ***
587
+ at.clear_error ();
530
588
at.flush ();
589
+ filehandle_stub_table = table4;
590
+ filehandle_stub_table_pos = 0 ;
591
+ // NO prefix, NO OK, NO ERROR and NO URC match, CRLF found -> return so buffer could be read
592
+ at.resp_start ();
593
+ // TO read 1 byte from: "s,"OK\r\n -> read "
594
+ at.read_bytes (buf5, 1 );
595
+ // TO read max 4 from: s,"OK\r\n -> read s and stop on ,
596
+ CHECK (1 == at.read_string (buf4, 4 + 1 /* for NULL*/ ));
597
+
598
+ // *** Encountering delimiter as first char in buffer ***
531
599
at.clear_error ();
600
+ at.flush ();
601
+ filehandle_stub_table = table4;
602
+ filehandle_stub_table_pos = 0 ;
603
+ // NO prefix, NO OK, NO ERROR and NO URC match, CRLF found -> return so buffer could be read
604
+ at.resp_start ();
605
+ // TO read 2 bytes from: "s,"OK\r\n -> read "s
606
+ at.read_bytes (buf5, 2 );
607
+ // TO read max 4 bytes from: ,"OK\r\n -> stop on ,
608
+ CHECK (0 == at.read_string (buf4, 4 + 1 /* for NULL*/ ));
532
609
533
- char table3[] = " sss\r sss\0 " ;
534
- filehandle_stub_table = table3;
610
+ // *** Read as much as buffer size is without encountering any delimiter " or OKCRLF ***
611
+ at.clear_error ();
612
+ char table5[] = " \" s\" OK\r\n abcd\0 " ;
613
+ at.flush ();
614
+ filehandle_stub_table = table5;
535
615
filehandle_stub_table_pos = 0 ;
536
616
mbed_poll_stub::revents_value = POLLIN;
537
- mbed_poll_stub::int_value = strlen (table);
617
+ mbed_poll_stub::int_value = 1 ;
618
+ // NO prefix, NO OK, NO ERROR and NO URC match, CRLF found -> return so buffer could be read
619
+ at.resp_start ();
620
+ // TO read 1 byte from: "s"OK\r\n -> read "
621
+ at.read_bytes (buf5, 1 );
622
+ // TO read max 1 byte from: s"OK\r\n -> read s
623
+ CHECK (1 == at.read_string (buf4, 1 + 1 /* for NULL*/ ));
624
+
625
+ // *** Consume " and run into OKCRLF ***
626
+ // TO read max 1 byte from: "OK\r\n -> consume " and find stop tag OKCRLF
627
+ CHECK (0 == at.read_string (buf4, 1 + 1 /* for NULL*/ ));
628
+
629
+ // *** Try to read after stop tag was found ***
630
+ // stop tag found do not read further
631
+ CHECK (-1 == at.read_string (buf4, 1 + 1 /* for NULL*/ ));
632
+
633
+ // *** Try to read after stop tag was found when parameter allows it ***
634
+ // stop tag found but flag indicates to read despite stop_tag found
635
+ CHECK (4 == at.read_string (buf4, 4 + 1 /* for NULL*/ , true ));
636
+
637
+ // *** Read as much as buffer size is without encountering any delimiter " or OKCRLF ***
638
+ at.clear_error ();
639
+ char table6[] = " sss\r sss\0 " ;
640
+ at.flush ();
641
+ filehandle_stub_table = table6;
642
+ filehandle_stub_table_pos = 0 ;
643
+ mbed_poll_stub::revents_value = POLLIN;
644
+ mbed_poll_stub::int_value = 1 ;
645
+ at.resp_start (" s" );
646
+ // TO read from: ss\rsss -> read all 6 chars ss\rsss
647
+ CHECK (6 == at.read_string (buf4, 6 + 1 /* for NULL*/ ));
538
648
649
+ // *** Reading when buffer only has " ***
650
+ at.clear_error ();
651
+ char table7[] = " s\"\0 " ;
652
+ at.flush ();
653
+ filehandle_stub_table = table7;
654
+ filehandle_stub_table_pos = 0 ;
655
+ mbed_poll_stub::revents_value = POLLIN;
656
+ mbed_poll_stub::int_value = 1 ;
539
657
at.resp_start (" s" );
540
- at.read_string (buf, 5 , true );
658
+ // TO read from buffer having only " -> consume " -> trying to read when nothing in buffer
659
+ CHECK (-1 == at.read_string (buf4, 5 ));
660
+ CHECK (NSAPI_ERROR_DEVICE_ERROR == at.get_last_error ());
661
+
662
+ // *** Reading through partially matching stop tag ***
663
+ at.clear_error ();
664
+ char table8[] = " \" s\" OK\r abcd\r\n\0 " ;
541
665
at.flush ();
666
+ filehandle_stub_table = table8;
667
+ filehandle_stub_table_pos = 0 ;
668
+ mbed_poll_stub::revents_value = POLLIN;
669
+ mbed_poll_stub::int_value = 1 ;
670
+ char buf8[9 ];
671
+ // NO prefix, NO OK, NO ERROR and NO URC match, CRLF found -> return so buffer could be read
672
+ at.resp_start ();
673
+ // TO read from
674
+ CHECK (8 == at.read_string (buf8, 8 + 1 /* for NULL*/ ));
675
+
676
+ // *** Reading through partially matching stop tag ***
542
677
at.clear_error ();
678
+ char table9[] = " \" s\" Oabcd\r\n\0 " ;
679
+ at.flush ();
680
+ filehandle_stub_table = table9;
681
+ filehandle_stub_table_pos = 0 ;
682
+ mbed_poll_stub::revents_value = POLLIN;
683
+ mbed_poll_stub::int_value = 1 ;
684
+ char buf9[5 ];
543
685
544
- char table4[] = " \" s\"\0 " ;
545
- filehandle_stub_table = table4;
686
+ // NO prefix, NO OK, NO ERROR and NO URC match, CRLF found -> return so buffer could be read
687
+ at.resp_start ();
688
+ // TO read from
689
+ CHECK (6 == at.read_string (buf9, 6 + 1 /* for NULL*/ ));
690
+
691
+ // *** CRLF part of the string ***
692
+ at.clear_error ();
693
+ char table10[] = " \" s\"\r\n OK\r\n\0 " ;
694
+ at.flush ();
695
+ filehandle_stub_table = table10;
546
696
filehandle_stub_table_pos = 0 ;
547
697
mbed_poll_stub::revents_value = POLLIN;
548
- mbed_poll_stub::int_value = strlen (table);
698
+ mbed_poll_stub::int_value = 1 ;
699
+ char buf10[10 ];
549
700
550
- at.resp_start (" s" );
551
- at.read_string (buf, 5 , true );
701
+ // NO prefix, NO OK, NO ERROR and NO URC match, CRLF found -> return so buffer could be read
702
+ at.resp_start ();
703
+ // TO read from
704
+ CHECK (3 == at.read_string (buf10, 9 + 1 /* for NULL*/ ));
705
+ }
552
706
707
+ void Test_ATHandler::test_ATHandler_read_hex_string ()
708
+ {
709
+ EventQueue que;
710
+ FileHandle_stub fh1;
553
711
filehandle_stub_table = NULL ;
554
712
filehandle_stub_table_pos = 0 ;
555
- mbed_poll_stub::revents_value = POLLOUT;
556
- mbed_poll_stub::int_value = 0 ;
713
+
714
+ ATHandler at (&fh1, que, 0 , " ," );
715
+
716
+ // *** Read up to delimiter, even length ***
717
+ at.clear_error ();
718
+ char table1[] = " 68656C6C6F," ;
719
+ at.flush ();
720
+ filehandle_stub_table = table1;
721
+ filehandle_stub_table_pos = 0 ;
722
+ mbed_poll_stub::revents_value = POLLIN;
723
+ mbed_poll_stub::int_value = 1 ;
724
+ char buf1[10 ];
725
+ // Set _stop_tag to resp_stop(OKCRLF)
726
+ at.resp_start ();
727
+ CHECK (5 == at.read_hex_string (buf1, 5 ));
728
+ CHECK (!strncmp (buf1, " hello" , 5 ));
729
+
730
+ // *** Read up to delimiter, odd length ***
731
+ at.clear_error ();
732
+ char table2[] = " 68656C6C6F7," ;
733
+ at.flush ();
734
+ filehandle_stub_table = table2;
735
+ filehandle_stub_table_pos = 0 ;
736
+ mbed_poll_stub::revents_value = POLLIN;
737
+ mbed_poll_stub::int_value = 1 ;
738
+ char buf2[10 ];
739
+ // Set _stop_tag to resp_stop(OKCRLF)
740
+ at.resp_start ();
741
+ CHECK (5 == at.read_hex_string (buf2, 6 ));
742
+ CHECK (!strncmp (buf2, " hello" , 5 ));
743
+
744
+ // *** Read with stop tag, even length ***
745
+ at.clear_error ();
746
+ char table3[] = " 6865OK\r\n " ;
747
+ at.flush ();
748
+ filehandle_stub_table = table3;
749
+ filehandle_stub_table_pos = 0 ;
750
+ mbed_poll_stub::revents_value = POLLIN;
751
+ mbed_poll_stub::int_value = 1 ;
752
+ char buf3[6 ];
753
+ // Set _stop_tag to resp_stop(OKCRLF)
754
+ at.resp_start ();
755
+ CHECK (2 == at.read_hex_string (buf3, 2 + 1 /* get to stop tag match*/ ));
756
+ CHECK (!strncmp (buf3, " he" , 2 ));
757
+ at.resp_stop ();
758
+
759
+ // *** Read with stop tag, odd length ***
760
+ at.clear_error ();
761
+ char table4[] = " 686OK\r\n " ;
762
+ at.flush ();
763
+ filehandle_stub_table = table4;
764
+ filehandle_stub_table_pos = 0 ;
765
+ mbed_poll_stub::revents_value = POLLIN;
766
+ mbed_poll_stub::int_value = 1 ;
767
+ char buf4[6 ];
768
+ // Set _stop_tag to resp_stop(OKCRLF)
769
+ at.resp_start ();
770
+ CHECK (1 == at.read_hex_string (buf4, 2 + 1 /* get to stop tag match*/ ));
771
+ CHECK (!strncmp (buf4, " h" , 1 ));
557
772
}
558
773
559
774
void Test_ATHandler::test_ATHandler_read_int ()
560
775
{
561
776
EventQueue que;
562
777
FileHandle_stub fh1;
778
+ filehandle_stub_table = NULL ;
779
+ filehandle_stub_table_pos = 0 ;
563
780
564
781
ATHandler at (&fh1, que, 0 , " ," );
565
782
0 commit comments