takashi kadono / Mbed OS Nucleo_446

Dependencies:   ssd1331

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers athandlertest.cpp Source File

athandlertest.cpp

00001 /*
00002  * Copyright (c) 2018, Arm Limited and affiliates.
00003  * SPDX-License-Identifier: Apache-2.0
00004  *
00005  * Licensed under the Apache License, Version 2.0 (the "License");
00006  * you may not use this file except in compliance with the License.
00007  * You may obtain a copy of the License at
00008  *
00009  *     http://www.apache.org/licenses/LICENSE-2.0
00010  *
00011  * Unless required by applicable law or agreed to in writing, software
00012  * distributed under the License is distributed on an "AS IS" BASIS,
00013  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00014  * See the License for the specific language governing permissions and
00015  * limitations under the License.
00016  */
00017 #include "gtest/gtest.h"
00018 #include <string.h>
00019 #include "AT_CellularNetwork.h"
00020 #include "EventQueue.h"
00021 #include "ATHandler.h"
00022 #include "AT_CellularStack.h"
00023 #include "FileHandle_stub.h"
00024 #include "CellularLog.h"
00025 #include "mbed_poll_stub.h"
00026 
00027 #include "Timer_stub.h"
00028 
00029 using namespace mbed;
00030 using namespace events;
00031 
00032 void urc_callback()
00033 {
00034 }
00035 
00036 void urc2_callback()
00037 {
00038 }
00039 
00040 // AStyle ignored as the definition is not clear due to preprocessor usage
00041 // *INDENT-OFF*
00042 class TestATHandler : public testing::Test {
00043 protected:
00044 
00045     void SetUp()
00046     {
00047     }
00048 
00049     void TearDown()
00050     {
00051     }
00052 };
00053 // *INDENT-ON*
00054 
00055 TEST_F(TestATHandler, Create)
00056 {
00057     EventQueue que;
00058     FileHandle_stub fh1;
00059 
00060     ATHandler *at = new ATHandler(&fh1, que, 0, ",");
00061 
00062     delete at;
00063 
00064     at = new ATHandler(&fh1, que, 0, NULL);
00065 
00066     EXPECT_TRUE(at != NULL);
00067     delete at;
00068 }
00069 
00070 TEST_F(TestATHandler, test_ATHandler_get_file_handle)
00071 {
00072     EventQueue que;
00073     FileHandle_stub fh1;
00074 
00075     ATHandler at(&fh1, que, 0, ",");
00076     EXPECT_EQ(&fh1, at.get_file_handle());
00077 }
00078 
00079 TEST_F(TestATHandler, test_ATHandler_set_file_handle)
00080 {
00081     EventQueue que;
00082     FileHandle_stub fh1, fh2;
00083 
00084     ATHandler at(&fh1, que, 0, ",");
00085 
00086     at.set_file_handle(&fh2);
00087 }
00088 
00089 TEST_F(TestATHandler, test_ATHandler_lock)
00090 {
00091     EventQueue que;
00092     FileHandle_stub fh1;
00093 
00094     ATHandler at(&fh1, que, 0, ",");
00095 
00096     at.lock();
00097 }
00098 
00099 TEST_F(TestATHandler, test_ATHandler_unlock)
00100 {
00101     EventQueue que;
00102     FileHandle_stub fh1;
00103 
00104     ATHandler at(&fh1, que, 0, ",");
00105     filehandle_stub_short_value_counter = 1;
00106     fh1.short_value = POLLIN;
00107     at.unlock();
00108 }
00109 
00110 TEST_F(TestATHandler, test_ATHandler_unlock_return_error)
00111 {
00112     EventQueue que;
00113     FileHandle_stub fh1;
00114 
00115     ATHandler at(&fh1, que, 0, ",");
00116     EXPECT_TRUE(NSAPI_ERROR_OK  == at.unlock_return_error());
00117 }
00118 
00119 TEST_F(TestATHandler, test_ATHandler_set_urc_handler)
00120 {
00121     EventQueue que;
00122     FileHandle_stub fh1;
00123 
00124     ATHandler at(&fh1, que, 0, ",");
00125     const char ch[] = "testtesttesttest";
00126 
00127     mbed::Callback<void()> cb(&urc_callback);
00128     at.set_urc_handler(ch, cb);
00129 
00130     //THIS IS NOT same callback in find_urc_handler???
00131     EXPECT_TRUE(NSAPI_ERROR_OK  == at.set_urc_handler(ch, cb));
00132 }
00133 
00134 TEST_F(TestATHandler, test_ATHandler_remove_urc_handler)
00135 {
00136     EventQueue que;
00137     FileHandle_stub fh1;
00138 
00139     ATHandler at(&fh1, que, 0, ",");
00140     const char ch[] = "testtesttesttest";
00141 
00142     mbed::Callback<void()> cb(&urc_callback);
00143     at.set_urc_handler(ch, cb);
00144 
00145     //This does nothing!!!
00146     at.remove_urc_handler(ch, cb);
00147 }
00148 
00149 TEST_F(TestATHandler, test_ATHandler_get_last_error)
00150 {
00151     EventQueue que;
00152     FileHandle_stub fh1;
00153 
00154     ATHandler at(&fh1, que, 0, ",");
00155     EXPECT_TRUE(NSAPI_ERROR_OK  == at.get_last_error());
00156 }
00157 
00158 TEST_F(TestATHandler, test_ATHandler_get_last_device_error)
00159 {
00160     EventQueue que;
00161     FileHandle_stub fh1;
00162 
00163     ATHandler at(&fh1, que, 0, ",");
00164     EXPECT_TRUE(0 == at.get_last_device_error().errCode);
00165 }
00166 
00167 TEST_F(TestATHandler, test_ATHandler_inc_ref_count)
00168 {
00169     EventQueue que;
00170     FileHandle_stub fh1;
00171 
00172     ATHandler at(&fh1, que, 0, ",");
00173     at.inc_ref_count();
00174 }
00175 
00176 TEST_F(TestATHandler, test_ATHandler_dec_ref_count)
00177 {
00178     EventQueue que;
00179     FileHandle_stub fh1;
00180 
00181     ATHandler at(&fh1, que, 0, ",");
00182     at.dec_ref_count();
00183 }
00184 
00185 TEST_F(TestATHandler, test_ATHandler_get_ref_count)
00186 {
00187     EventQueue que;
00188     FileHandle_stub fh1;
00189 
00190     ATHandler at(&fh1, que, 0, ",");
00191     EXPECT_TRUE(1 == at.get_ref_count());
00192 
00193     at.inc_ref_count();
00194     EXPECT_TRUE(2 == at.get_ref_count());
00195 
00196     at.inc_ref_count();
00197     EXPECT_TRUE(3 == at.get_ref_count());
00198 
00199     at.dec_ref_count();
00200     at.dec_ref_count();
00201     EXPECT_TRUE(1 == at.get_ref_count());
00202 }
00203 
00204 TEST_F(TestATHandler, test_ATHandler_set_at_timeout)
00205 {
00206     EventQueue que;
00207     FileHandle_stub fh1;
00208 
00209     ATHandler at(&fh1, que, 0, ",");
00210     at.set_at_timeout(8);
00211 
00212     at.set_at_timeout(80, true);
00213 }
00214 
00215 TEST_F(TestATHandler, test_ATHandler_restore_at_timeout)
00216 {
00217     EventQueue que;
00218     FileHandle_stub fh1;
00219 
00220     ATHandler at(&fh1, que, 0, ",");
00221     at.set_at_timeout(80, true);
00222     at.set_at_timeout(800);
00223     at.restore_at_timeout();
00224 }
00225 
00226 TEST_F(TestATHandler, test_ATHandler_clear_error)
00227 {
00228     EventQueue que;
00229     FileHandle_stub fh1;
00230 
00231     ATHandler at(&fh1, que, 0, ",");
00232     at.clear_error();
00233 }
00234 
00235 TEST_F(TestATHandler, test_ATHandler_process_oob)
00236 {
00237     EventQueue que;
00238     FileHandle_stub fh1;
00239 
00240     ATHandler at(&fh1, que, 0, ",");
00241     at.set_at_timeout(10);
00242 
00243     at.set_is_filehandle_usable(false);
00244     at.process_oob();
00245     at.set_is_filehandle_usable(true);
00246 
00247     filehandle_stub_short_value_counter = 1;
00248     fh1.short_value = POLLIN;
00249     at.set_urc_handler("s", &urc_callback);
00250     at.process_oob();
00251 
00252     filehandle_stub_short_value_counter = 2;
00253     at.process_oob();
00254 
00255     //at.fill_buffer();
00256     uint8_t buf[5];
00257     at.clear_error();
00258     char table[] = "ssssssssssssssssssssssssssssssss\0";
00259     filehandle_stub_table = table;
00260     filehandle_stub_table_pos = 0;
00261     mbed_poll_stub::revents_value = POLLIN;
00262     mbed_poll_stub::int_value = 1;
00263     at.read_bytes(buf, 5);
00264 
00265     filehandle_stub_short_value_counter = 2;
00266     at.process_oob();
00267 
00268     at.clear_error();
00269     timer_stub_value = 0;
00270     filehandle_stub_table_pos = 0;
00271     at.read_bytes(buf, 5);
00272 
00273     filehandle_stub_short_value_counter = 1;
00274     at.process_oob();
00275 
00276     char table2[5];
00277     table2[0] = '\r';
00278     table2[1] = '\r';
00279     table2[2] = '\n';
00280     table2[3] = '\n';
00281     table2[4] = 0;
00282     filehandle_stub_table = table2;
00283 
00284     at.clear_error();
00285     timer_stub_value = 0;
00286     filehandle_stub_table_pos = 0;
00287     mbed_poll_stub::revents_value = POLLIN;
00288     mbed_poll_stub::int_value = 1;
00289     at.read_bytes(buf, 1);
00290 
00291     filehandle_stub_short_value_counter = 1;
00292     at.process_oob();
00293 
00294     filehandle_stub_table = table;
00295 
00296     filehandle_stub_short_value_counter = 0;
00297     filehandle_stub_table_pos = 0;
00298     filehandle_stub_table = NULL;
00299 }
00300 
00301 TEST_F(TestATHandler, test_ATHandler_set_filehandle_sigio)
00302 {
00303     EventQueue que;
00304     FileHandle_stub fh1;
00305 
00306     ATHandler at(&fh1, que, 0, ",");
00307     at.set_filehandle_sigio();
00308 }
00309 
00310 TEST_F(TestATHandler, test_ATHandler_flush)
00311 {
00312     EventQueue que;
00313     FileHandle_stub fh1;
00314 
00315     ATHandler at(&fh1, que, 0, ",");
00316     filehandle_stub_short_value_counter = 1;
00317     fh1.short_value = POLLIN;
00318     at.flush();
00319 }
00320 
00321 TEST_F(TestATHandler, test_ATHandler_cmd_start)
00322 {
00323     EventQueue que;
00324     FileHandle_stub fh1;
00325 
00326     ATHandler at(&fh1, que, 0, ",");
00327     mbed_poll_stub::revents_value = POLLOUT;
00328     mbed_poll_stub::int_value = 1;
00329     fh1.size_value = 3;
00330     at.cmd_start("s");
00331     mbed_poll_stub::revents_value = POLLIN;
00332     mbed_poll_stub::int_value = 0;
00333 
00334     at.cmd_start("s");
00335 
00336     at.cmd_start("s");
00337 }
00338 
00339 TEST_F(TestATHandler, test_ATHandler_write_int)
00340 {
00341     EventQueue que;
00342     FileHandle_stub fh1;
00343 
00344     ATHandler at(&fh1, que, 0, ",");
00345     fh1.size_value = -1;
00346     at.write_int(4);
00347 
00348     at.clear_error();
00349     mbed_poll_stub::revents_value = POLLOUT;
00350     mbed_poll_stub::int_value = 1;
00351     fh1.size_value = 6;
00352     at.write_int(4);
00353 
00354     at.write_int(2147483647);
00355 
00356     at.write_int(2147483647 + 1);
00357 
00358 //    at.at_error(0, DeviceErrorType(0));
00359 //    at.write_int(4);
00360 }
00361 
00362 TEST_F(TestATHandler, test_ATHandler_write_string)
00363 {
00364     EventQueue que;
00365     FileHandle_stub fh1;
00366 
00367     ATHandler at(&fh1, que, 0, ",");
00368     fh1.size_value = -1;
00369     at.write_string("help");
00370     EXPECT_TRUE(NSAPI_ERROR_DEVICE_ERROR  == at.get_last_error());
00371 
00372     at.clear_error();
00373     mbed_poll_stub::revents_value = POLLOUT;
00374     mbed_poll_stub::int_value = 1;
00375     fh1.size_value = -1;
00376     at.cmd_start("s");
00377     at.write_string("help", true);
00378     EXPECT_TRUE(NSAPI_ERROR_DEVICE_ERROR  == at.get_last_error());
00379 
00380     at.clear_error();
00381     mbed_poll_stub::revents_value = POLLOUT;
00382     mbed_poll_stub::int_value = 1;
00383     fh1.size_value = -1;
00384     at.write_string("help", true);
00385     EXPECT_TRUE(NSAPI_ERROR_DEVICE_ERROR  == at.get_last_error());
00386 
00387     at.clear_error();
00388     mbed_poll_stub::revents_value = POLLOUT;
00389     mbed_poll_stub::int_value = 1;
00390     fh1.size_value = 7;
00391     at.write_string("help", true);
00392     EXPECT_TRUE(NSAPI_ERROR_OK  == at.get_last_error());
00393 }
00394 
00395 TEST_F(TestATHandler, test_ATHandler_cmd_stop)
00396 {
00397     EventQueue que;
00398     FileHandle_stub fh1;
00399 
00400     ATHandler at(&fh1, que, 0, ",");
00401     fh1.size_value = -1;
00402     at.cmd_stop();
00403 
00404     at.write_string("help", true);
00405 
00406     at.cmd_stop();
00407     EXPECT_TRUE(NSAPI_ERROR_DEVICE_ERROR  == at.get_last_error());
00408 }
00409 
00410 TEST_F(TestATHandler, test_ATHandler_write_bytes)
00411 {
00412     EventQueue que;
00413     FileHandle_stub fh1;
00414 
00415     ATHandler at(&fh1, que, 0, ",");
00416     fh1.size_value = -1;
00417     uint8_t data[] = "data";
00418     at.write_bytes(data, 4);
00419 
00420     at.write_bytes(data, 4);
00421     EXPECT_TRUE(NSAPI_ERROR_DEVICE_ERROR  == at.get_last_error());
00422 }
00423 
00424 TEST_F(TestATHandler, test_ATHandler_set_stop_tag)
00425 {
00426     EventQueue que;
00427     FileHandle_stub fh1;
00428 
00429     ATHandler at(&fh1, que, 0, ",");
00430     at.set_stop_tag("s");
00431 }
00432 
00433 TEST_F(TestATHandler, test_ATHandler_set_delimiter)
00434 {
00435     EventQueue que;
00436     FileHandle_stub fh1;
00437 
00438     ATHandler at(&fh1, que, 0, ",");
00439     at.set_delimiter('+');
00440 }
00441 
00442 TEST_F(TestATHandler, test_ATHandler_skip_param)
00443 {
00444     EventQueue que;
00445     FileHandle_stub fh1;
00446 
00447     ATHandler at(&fh1, que, 0, ",");
00448     at.set_stop_tag("OK\r\n");
00449     at.skip_param();
00450 
00451     char table[] = "ssssssssssssssssssssssssssssOK\r\n\0";
00452     filehandle_stub_table = table;
00453 
00454     at.flush();
00455     at.clear_error();
00456     filehandle_stub_table_pos = 0;
00457     mbed_poll_stub::revents_value = POLLIN;
00458     mbed_poll_stub::int_value = 1;
00459     filehandle_stub_short_value_counter = 1;
00460     fh1.short_value = POLLIN;
00461     at.resp_start();
00462     at.skip_param();
00463     EXPECT_TRUE(at.get_last_error() == NSAPI_ERROR_DEVICE_ERROR );
00464 
00465     char table1[] = "ss,sssssssssssss,sssssssssssOK\r\n\0";
00466     filehandle_stub_table = table1;
00467 
00468     at.flush();
00469     at.clear_error();
00470     filehandle_stub_short_value_counter = 1;
00471     filehandle_stub_table_pos = 0;
00472     at.resp_start();
00473     at.skip_param();
00474 
00475     char table2[] = "sssOK\r\n\0";
00476     filehandle_stub_table = table2;
00477 
00478     at.flush();
00479     at.clear_error();
00480     filehandle_stub_short_value_counter = 1;
00481     filehandle_stub_table_pos = 0;
00482     at.resp_start();
00483     at.skip_param();
00484 
00485     char table3[] = "sssssssOK\nssss\0";
00486     filehandle_stub_table = table3;
00487 
00488     //Need to create a new instance because stop tag already found
00489     ATHandler at2(&fh1, que, 0, ",");
00490     at2.flush();
00491     at2.clear_error();
00492     filehandle_stub_short_value_counter = 1;
00493     filehandle_stub_table_pos = 0;
00494     at2.resp_start();
00495     at2.skip_param();
00496 
00497     at2.skip_param(4, 3);
00498 
00499     filehandle_stub_table = table3;
00500 
00501     at2.flush();
00502     at2.clear_error();
00503     filehandle_stub_short_value_counter = 1;
00504     filehandle_stub_table_pos = 0;
00505     at2.resp_start();
00506     at2.skip_param(4, 3);
00507 
00508     filehandle_stub_table = table3;
00509 
00510     at2.flush();
00511     at2.clear_error();
00512     filehandle_stub_short_value_counter = 1;
00513     filehandle_stub_table_pos = 0;
00514     at2.resp_start();
00515     at2.skip_param(24, 17);
00516 }
00517 
00518 TEST_F(TestATHandler, test_ATHandler_read_bytes)
00519 {
00520     EventQueue que;
00521     FileHandle_stub fh1;
00522     filehandle_stub_table = NULL;
00523     filehandle_stub_table_pos = 0;
00524 
00525     ATHandler at(&fh1, que, 0, ",");
00526     uint8_t buf[5];
00527 
00528     // TEST EMPTY BUFFER
00529     // Shouldn't read any byte since buffer is empty
00530     EXPECT_TRUE(-1 == at.read_bytes(buf, 1));
00531     EXPECT_TRUE(NSAPI_ERROR_DEVICE_ERROR  == at.get_last_error());
00532     // Return error due to error set to at handler by the above call on empty buffer
00533     EXPECT_TRUE(-1 == at.read_bytes(buf, 1));
00534 
00535     // TEST DATA IN BUFFER
00536     at.clear_error();
00537     char table1[] = "1234512345678OK\r\n\0";
00538     filehandle_stub_table = table1;
00539     filehandle_stub_table_pos = 0;
00540     mbed_poll_stub::revents_value = POLLIN;
00541     mbed_poll_stub::int_value = 1;;
00542 
00543     // Read 5 bytes
00544     EXPECT_TRUE(5 == at.read_bytes(buf, 5));
00545     EXPECT_TRUE(!memcmp(buf, table1, 5));
00546     // get_char triggered above should have filled in the whole reading buffer(fill_buffer())
00547     EXPECT_TRUE(filehandle_stub_table_pos == (strlen(table1) - 1));
00548     // Read another 8 bytes
00549     EXPECT_TRUE(8 == at.read_bytes(buf, 8) && !memcmp(buf, table1 + 5, 8));
00550     // Reading more than the 4 bytes left -> ERROR
00551     EXPECT_TRUE(-1 == at.read_bytes(buf, 5));
00552     EXPECT_TRUE(NSAPI_ERROR_DEVICE_ERROR  == at.get_last_error());
00553 }
00554 
00555 TEST_F(TestATHandler, test_ATHandler_read_string)
00556 {
00557     EventQueue que;
00558     FileHandle_stub fh1;
00559     filehandle_stub_table = NULL;
00560     filehandle_stub_table_pos = 0;
00561 
00562     ATHandler at(&fh1, que, 0, ",");
00563 
00564     // *** EMPTY ***
00565     at.clear_error();
00566     char table1[] = "";
00567     at.flush();
00568     filehandle_stub_table = table1;
00569     filehandle_stub_table_pos = 0;
00570     mbed_poll_stub::revents_value = POLLIN;
00571     mbed_poll_stub::int_value = 1;
00572     char buf1[1];
00573     // No _stop_tag set without resp_start
00574     EXPECT_TRUE(-1 == at.read_string(buf1, 1));
00575     at.clear_error();
00576     // Set _stop_tag to resp_stop(OKCRLF)
00577     at.resp_start();
00578     // Device error because buffer is empty
00579     EXPECT_TRUE(NSAPI_ERROR_DEVICE_ERROR  == at.get_last_error());
00580     at.clear_error();
00581     // Device error because empty buffer and attempt to fill_buffer by consume_char('\"')
00582     EXPECT_TRUE(-1 == at.read_string(buf1, 1));
00583 
00584     // *** 1 BYTE ***
00585     at.clear_error();
00586     char table2[] = "s\0";
00587     at.flush();
00588     filehandle_stub_table = table2;
00589     filehandle_stub_table_pos = 0;
00590     mbed_poll_stub::revents_value = POLLIN;
00591     mbed_poll_stub::int_value = 1;
00592     char buf2[1];
00593     // Set _stop_tag to resp_stop(OKCRLF)
00594     at.resp_start();
00595     // Device error because no CRLF and no more data to read
00596     EXPECT_TRUE(NSAPI_ERROR_DEVICE_ERROR  == at.get_last_error());
00597     at.clear_error();
00598     EXPECT_TRUE(0 == at.read_string(buf2, 1));
00599 
00600     // *** CRLF ***
00601     at.clear_error();
00602     char table3[] = "\r\ns\r\n\0";
00603     at.flush();
00604     filehandle_stub_table = table3;
00605     filehandle_stub_table_pos = 0;
00606     mbed_poll_stub::revents_value = POLLIN;
00607     mbed_poll_stub::int_value = 1;
00608     char buf3[1];
00609     // Set _stop_tag to resp_stop(OKCRLF)
00610     at.resp_start();
00611     // OK because after CRLF matched there is more data to read ending in CRLF
00612     EXPECT_TRUE(NSAPI_ERROR_OK  == at.get_last_error());
00613     // To read 0 bytes from: s\r\n
00614     EXPECT_TRUE(0 == at.read_string(buf3, 0 + 1/*for NULL*/));
00615     // To read 1 byte from: s\r\n -> read s
00616     EXPECT_TRUE(1 == at.read_string(buf3, 1 + 1/*for NULL*/));
00617 
00618     // *** Reading more than available in buffer ***
00619     at.clear_error();
00620     char table4[] = "\"s,\"OK\r\n\0";
00621     mbed_poll_stub::int_value = 0;
00622     at.flush();
00623     filehandle_stub_table = table4;
00624     filehandle_stub_table_pos = 0;
00625     mbed_poll_stub::revents_value = POLLIN;
00626     mbed_poll_stub::int_value = 1;
00627     char buf4[7];
00628     uint8_t buf5[5];
00629     // NO prefix, NO OK, NO ERROR and NO URC match, CRLF found -> return so buffer could be read
00630     at.resp_start();
00631     // TO read 5 bytes from: "s,"OK\r\n -> read "s,"O
00632     at.read_bytes(buf5, 5);
00633     // K\r\n left to be read -> reading more than 3 + 1(for NULL) -> ERROR
00634     EXPECT_TRUE(-1 == at.read_string(buf4, 4 + 1/*for NULL*/));
00635     EXPECT_TRUE(NSAPI_ERROR_DEVICE_ERROR  == at.get_last_error());
00636 
00637     // *** Encountering delimiter after reading 1 byte ***
00638     at.clear_error();
00639     at.flush();
00640     filehandle_stub_table = table4;
00641     filehandle_stub_table_pos = 0;
00642     // NO prefix, NO OK, NO ERROR and NO URC match, CRLF found -> return so buffer could be read
00643     at.resp_start();
00644     // TO read 1 byte from: "s,"OK\r\n -> read "
00645     at.read_bytes(buf5, 1);
00646     // TO read max 4 from: s,"OK\r\n -> read s and stop on ,
00647     EXPECT_TRUE(1 == at.read_string(buf4, 4 + 1/*for NULL*/));
00648 
00649     // *** Encountering delimiter as first char in buffer  ***
00650     at.clear_error();
00651     at.flush();
00652     filehandle_stub_table = table4;
00653     filehandle_stub_table_pos = 0;
00654     // NO prefix, NO OK, NO ERROR and NO URC match, CRLF found -> return so buffer could be read
00655     at.resp_start();
00656     // TO read 2 bytes from: "s,"OK\r\n -> read "s
00657     at.read_bytes(buf5, 2);
00658     // TO read max 4 bytes from: ,"OK\r\n -> stop on ,
00659     EXPECT_TRUE(0 == at.read_string(buf4, 4 + 1/*for NULL*/));
00660 
00661     // *** Read as much as buffer size is without encountering any delimiter " or OKCRLF  ***
00662     at.clear_error();
00663     char table5[] = "\"s\"OK\r\nabcd\0";
00664     at.flush();
00665     filehandle_stub_table = table5;
00666     filehandle_stub_table_pos = 0;
00667     mbed_poll_stub::revents_value = POLLIN;
00668     mbed_poll_stub::int_value = 1;
00669     // NO prefix, NO OK, NO ERROR and NO URC match, CRLF found -> return so buffer could be read
00670     at.resp_start();
00671     // TO read 1 byte from: "s"OK\r\n -> read "
00672     at.read_bytes(buf5, 1);
00673     // TO read max 1 byte from: s"OK\r\n -> read s
00674     EXPECT_TRUE(1 == at.read_string(buf4, 1 + 1/*for NULL*/));
00675 
00676     // *** Consume " and run into OKCRLF  ***
00677     // TO read max 1 byte from: "OK\r\n -> consume " and find stop tag OKCRLF
00678     EXPECT_TRUE(0 == at.read_string(buf4, 1 + 1/*for NULL*/));
00679 
00680     // *** Try to read after stop tag was found  ***
00681     // stop tag found do not read further
00682     EXPECT_TRUE(-1 == at.read_string(buf4, 1 + 1/*for NULL*/));
00683 
00684     // *** Try to read after stop tag was found when parameter allows it  ***
00685     // stop tag found but flag indicates to read despite stop_tag found
00686     EXPECT_TRUE(4 == at.read_string(buf4, 4 + 1/*for NULL*/, true));
00687 
00688     // *** Read as much as buffer size is without encountering any delimiter " or OKCRLF  ***
00689     at.clear_error();
00690     char table6[] = "sss\rsss\0";
00691     at.flush();
00692     filehandle_stub_table = table6;
00693     filehandle_stub_table_pos = 0;
00694     mbed_poll_stub::revents_value = POLLIN;
00695     mbed_poll_stub::int_value = 1;
00696     at.resp_start("s");
00697     // TO read from: ss\rsss -> read all 6 chars ss\rsss
00698     EXPECT_TRUE(6 == at.read_string(buf4, 6 + 1/*for NULL*/));
00699 
00700     // *** Reading when buffer only has "  ***
00701     at.clear_error();
00702     char table7[] = "s\"\0";
00703     at.flush();
00704     filehandle_stub_table = table7;
00705     filehandle_stub_table_pos = 0;
00706     mbed_poll_stub::revents_value = POLLIN;
00707     mbed_poll_stub::int_value = 1;
00708     at.resp_start("s");
00709     // TO read from buffer having only " -> consume " -> trying to read when nothing in buffer
00710     EXPECT_TRUE(-1 == at.read_string(buf4, 5));
00711     EXPECT_TRUE(NSAPI_ERROR_DEVICE_ERROR  == at.get_last_error());
00712 
00713     // *** Reading through partially matching stop tag  ***
00714     at.clear_error();
00715     char table8[] = "\"s\"OK\rabcd\r\n\0";
00716     at.flush();
00717     filehandle_stub_table = table8;
00718     filehandle_stub_table_pos = 0;
00719     mbed_poll_stub::revents_value = POLLIN;
00720     mbed_poll_stub::int_value = 1;
00721     char buf8[9];
00722     // NO prefix, NO OK, NO ERROR and NO URC match, CRLF found -> return so buffer could be read
00723     at.resp_start();
00724     // TO read from
00725     EXPECT_TRUE(8 == at.read_string(buf8, 8 + 1/*for NULL*/));
00726 
00727     // *** Reading through partially matching stop tag  ***
00728     at.clear_error();
00729     char table9[] = "\"s\"Oabcd\r\n\0";
00730     at.flush();
00731     filehandle_stub_table = table9;
00732     filehandle_stub_table_pos = 0;
00733     mbed_poll_stub::revents_value = POLLIN;
00734     mbed_poll_stub::int_value = 1;
00735     char buf9[5];
00736 
00737     // NO prefix, NO OK, NO ERROR and NO URC match, CRLF found -> return so buffer could be read
00738     at.resp_start();
00739     // TO read from
00740     EXPECT_TRUE(6 == at.read_string(buf9, 6 + 1/*for NULL*/));
00741 
00742     // *** CRLF part of the string ***
00743     at.clear_error();
00744     char table10[] = "\"s\"\r\nOK\r\n\0";
00745     mbed_poll_stub::int_value = 0;
00746     at.flush();
00747     filehandle_stub_table = table10;
00748     filehandle_stub_table_pos = 0;
00749     mbed_poll_stub::revents_value = POLLIN;
00750     mbed_poll_stub::int_value = 1;
00751     char buf10[10];
00752 
00753     // NO prefix, NO OK, NO ERROR and NO URC match, CRLF found -> return so buffer could be read
00754     at.resp_start();
00755     // TO read from
00756     EXPECT_TRUE(3 == at.read_string(buf10, 9 + 1/*for NULL*/));
00757 }
00758 
00759 TEST_F(TestATHandler, test_ATHandler_read_hex_string)
00760 {
00761     EventQueue que;
00762     FileHandle_stub fh1;
00763     filehandle_stub_table = NULL;
00764     filehandle_stub_table_pos = 0;
00765 
00766     ATHandler at(&fh1, que, 0, ",");
00767 
00768     // *** Read up to delimiter, even length ***
00769     at.clear_error();
00770     char table1[] = "68656C6C6F,";
00771     at.flush();
00772     filehandle_stub_table = table1;
00773     filehandle_stub_table_pos = 0;
00774     mbed_poll_stub::revents_value = POLLIN;
00775     mbed_poll_stub::int_value = 1;
00776     char buf1[10];
00777     // Set _stop_tag to resp_stop(OKCRLF)
00778     at.resp_start();
00779     EXPECT_TRUE(5 == at.read_hex_string(buf1, 5));
00780     EXPECT_TRUE(!strncmp(buf1, "hello", 5));
00781 
00782     // *** Read up to delimiter, odd length ***
00783     at.clear_error();
00784     char table2[] = "68656C6C6F7,";
00785     at.flush();
00786     filehandle_stub_table = table2;
00787     filehandle_stub_table_pos = 0;
00788     mbed_poll_stub::revents_value = POLLIN;
00789     mbed_poll_stub::int_value = 1;
00790     char buf2[10];
00791     // Set _stop_tag to resp_stop(OKCRLF)
00792     at.resp_start();
00793     EXPECT_TRUE(5 == at.read_hex_string(buf2, 6));
00794     EXPECT_TRUE(!strncmp(buf2, "hello", 5));
00795 
00796     // *** Read with stop tag, even length ***
00797     at.clear_error();
00798     char table3[] = "6865OK\r\n";
00799     at.flush();
00800     filehandle_stub_table = table3;
00801     filehandle_stub_table_pos = 0;
00802     mbed_poll_stub::revents_value = POLLIN;
00803     mbed_poll_stub::int_value = 1;
00804     char buf3[6];
00805     // Set _stop_tag to resp_stop(OKCRLF)
00806     at.resp_start();
00807     EXPECT_TRUE(2 == at.read_hex_string(buf3, 2 + 1/*get to stop tag match*/));
00808     EXPECT_TRUE(!strncmp(buf3, "he", 2));
00809     at.resp_stop();
00810 
00811     // *** Read with stop tag, odd length ***
00812     at.clear_error();
00813     char table4[] = "686OK\r\n";
00814     at.flush();
00815     filehandle_stub_table = table4;
00816     filehandle_stub_table_pos = 0;
00817     mbed_poll_stub::revents_value = POLLIN;
00818     mbed_poll_stub::int_value = 1;
00819     char buf4[6];
00820     // Set _stop_tag to resp_stop(OKCRLF)
00821     at.resp_start();
00822     EXPECT_TRUE(1 == at.read_hex_string(buf4, 2 + 1/*get to stop tag match*/));
00823     EXPECT_TRUE(!strncmp(buf4, "h", 1));
00824 }
00825 
00826 TEST_F(TestATHandler, test_ATHandler_read_int)
00827 {
00828     EventQueue que;
00829     FileHandle_stub fh1;
00830     filehandle_stub_table = NULL;
00831     filehandle_stub_table_pos = 0;
00832 
00833     ATHandler at(&fh1, que, 0, ",");
00834 
00835     int32_t ret = at.read_int();
00836     EXPECT_TRUE(-1 == ret);
00837     at.clear_error();
00838 
00839     char table[] = "\",\"OK\r\n\0";
00840     filehandle_stub_table = table;
00841     filehandle_stub_table_pos = 0;
00842     mbed_poll_stub::revents_value = POLLIN;
00843     mbed_poll_stub::int_value = strlen(table);
00844 
00845     at.resp_start();
00846 
00847     ret = at.read_int();
00848     EXPECT_TRUE(-1 == ret);
00849     at.flush();
00850     at.clear_error();
00851 
00852     char table2[] = "\"2,\"OK\r\n\0";
00853     filehandle_stub_table = table2;
00854     filehandle_stub_table_pos = 0;
00855     mbed_poll_stub::revents_value = POLLIN;
00856     mbed_poll_stub::int_value = strlen(table2);
00857 
00858     at.resp_start();
00859 
00860     ret = at.read_int();
00861     EXPECT_TRUE(2 == ret);
00862 }
00863 
00864 TEST_F(TestATHandler, test_ATHandler_resp_start)
00865 {
00866     EventQueue que;
00867     FileHandle_stub fh1;
00868 
00869     filehandle_stub_table = NULL;
00870     filehandle_stub_table_pos = 0;
00871 
00872     ATHandler at(&fh1, que, 0, ",");
00873     at.resp_start();
00874     at.resp_start();
00875 
00876     char table2[] = "\"2,\"OK\r\n\0";
00877     filehandle_stub_table = table2;
00878     filehandle_stub_table_pos = 0;
00879 
00880     at.flush();
00881     at.clear_error();
00882     filehandle_stub_table_pos = 0;
00883     at.resp_start("ssssaaaassssaaaassss"); //too long prefix
00884 
00885     char table3[] = "+CME ERROR: 108\0";
00886     filehandle_stub_table = table3;
00887     filehandle_stub_table_pos = 0;
00888 
00889     at.flush();
00890     at.clear_error();
00891     filehandle_stub_table_pos = 0;
00892     at.resp_start();
00893 
00894 
00895     at.flush();
00896     at.clear_error();
00897     filehandle_stub_table_pos = 0;
00898     at.resp_start();
00899 
00900     char table4[] = "+CMS ERROR: 6\0";
00901     filehandle_stub_table = table4;
00902 
00903     filehandle_stub_table_pos = 0;
00904     at.flush();
00905     at.clear_error();
00906     filehandle_stub_table_pos = 0;
00907     at.resp_start();
00908 
00909     char table5[] = "ERROR\r\n\0";
00910     filehandle_stub_table = table5;
00911     filehandle_stub_table_pos = 0;
00912 
00913     at.flush();
00914     at.clear_error();
00915     filehandle_stub_table_pos = 0;
00916     at.resp_start();
00917 
00918     char table6[] = "OK\r\n\0";
00919     filehandle_stub_table = table6;
00920     filehandle_stub_table_pos = 0;
00921 
00922     at.flush();
00923     at.clear_error();
00924     filehandle_stub_table_pos = 0;
00925     at.resp_start();
00926 
00927     char table7[] = "ssssss\0";
00928     filehandle_stub_table = table7;
00929     filehandle_stub_table_pos = 0;
00930 
00931     at.flush();
00932     at.clear_error();
00933     at.set_urc_handler("ss", NULL);
00934     filehandle_stub_table_pos = 0;
00935     at.resp_start();
00936 }
00937 
00938 TEST_F(TestATHandler, test_ATHandler_resp_stop)
00939 {
00940     EventQueue que;
00941     FileHandle_stub fh1;
00942 
00943     ATHandler at(&fh1, que, 0, ",");
00944 
00945     char table[] = "21 OK\r\n\0";
00946     filehandle_stub_table = table;
00947     filehandle_stub_table_pos = 0;
00948 
00949     at.info_elem('2');
00950     at.set_stop_tag("OK\r\n");
00951     at.resp_stop();
00952 
00953     char table3[] = "+CME ERROR: 108\0";
00954     filehandle_stub_table = table3;
00955     filehandle_stub_table_pos = 0;
00956 
00957     at.flush();
00958     at.clear_error();
00959     filehandle_stub_table_pos = 0;
00960     at.resp_start();
00961 
00962     at.resp_stop();
00963 
00964     char table7[] = "ssssss\0";
00965     filehandle_stub_table = table7;
00966     filehandle_stub_table_pos = 0;
00967 
00968     at.flush();
00969     at.clear_error();
00970     filehandle_stub_table_pos = 0;
00971     at.resp_start("ss", false);
00972     at.resp_stop();
00973 }
00974 
00975 TEST_F(TestATHandler, test_ATHandler_info_resp)
00976 {
00977     EventQueue que;
00978     FileHandle_stub fh1;
00979 
00980     filehandle_stub_table = NULL;
00981 
00982     ATHandler at(&fh1, que, 0, ",");
00983     EXPECT_TRUE(at.info_resp());
00984 
00985     at.resp_start();
00986     EXPECT_TRUE(!at.info_resp());
00987 
00988     at.flush();
00989     at.clear_error();
00990 
00991     char table2[] = "21 OK\r\n\0";
00992     filehandle_stub_table = table2;
00993     filehandle_stub_table_pos = 0;
00994     mbed_poll_stub::revents_value = POLLIN;
00995     mbed_poll_stub::int_value = strlen(table2);
00996 
00997     at.resp_start("21");
00998     EXPECT_TRUE(at.info_resp());
00999 
01000     EXPECT_TRUE(!at.info_resp());
01001 
01002     at.flush();
01003     at.clear_error();
01004 
01005     char table3[] = "21 OK\r\n\0";
01006     filehandle_stub_table = table3;
01007     filehandle_stub_table_pos = 0;
01008     mbed_poll_stub::revents_value = POLLIN;
01009     mbed_poll_stub::int_value = strlen(table3);
01010 
01011     EXPECT_TRUE(at.info_resp());
01012 }
01013 
01014 TEST_F(TestATHandler, test_ATHandler_info_elem)
01015 {
01016     EventQueue que;
01017     FileHandle_stub fh1;
01018 
01019     char table[] = "21 OK\r\n\0";
01020     filehandle_stub_table = table;
01021     filehandle_stub_table_pos = 0;
01022     mbed_poll_stub::revents_value = POLLIN;
01023     mbed_poll_stub::int_value = strlen(table);
01024 
01025     ATHandler at(&fh1, que, 0, ",");
01026     EXPECT_TRUE(!at.info_elem('O'));
01027     at.flush();
01028 
01029     char table2[] = "21 OK\r\n\0";
01030     filehandle_stub_table = table2;
01031     filehandle_stub_table_pos = 0;
01032     mbed_poll_stub::revents_value = POLLIN;
01033     mbed_poll_stub::int_value = strlen(table2);
01034 
01035     at.clear_error();
01036     at.resp_start("21");
01037     EXPECT_TRUE(at.info_elem('O'));
01038     at.flush();
01039 
01040     filehandle_stub_table = NULL;
01041     filehandle_stub_table_pos = 0;
01042 
01043     at.clear_error();
01044     at.resp_start("21");
01045     EXPECT_TRUE(!at.info_elem('2'));
01046 }
01047 
01048 TEST_F(TestATHandler, test_ATHandler_consume_to_stop_tag)
01049 {
01050     EventQueue que;
01051     FileHandle_stub fh1;
01052 
01053     ATHandler at(&fh1, que, 0, ",");
01054     EXPECT_TRUE(at.consume_to_stop_tag());
01055 }
01056 
01057 TEST_F(TestATHandler, test_ATHandler_set_debug)
01058 {
01059     EventQueue que;
01060     FileHandle_stub fh1;
01061 
01062     ATHandler at(&fh1, que, 0, ",");
01063     at.set_debug(true);
01064 
01065     at.set_debug(false);
01066 }
01067 
01068 TEST_F(TestATHandler, test_ATHandler_get_3gpp_error)
01069 {
01070     EventQueue que;
01071     FileHandle_stub fh1;
01072 
01073     ATHandler at(&fh1, que, 0, ",");
01074     at.get_3gpp_error();
01075 }
01076