Skip to content

Commit 13d6703

Browse files
author
Cruz Monrreal
authored
Merge pull request #8401 from mirelachirica/at_handler_changes_2
At handler fixes
2 parents 503066a + c8f2ed3 commit 13d6703

File tree

8 files changed

+120
-26
lines changed

8 files changed

+120
-26
lines changed

UNITTESTS/features/cellular/framework/AT/athandler/athandlertest.cpp

Lines changed: 77 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ TEST_F(TestATHandler, test_ATHandler_remove_urc_handler)
143143
at.set_urc_handler(ch, cb);
144144

145145
//This does nothing!!!
146-
at.remove_urc_handler(ch, cb);
146+
at.remove_urc_handler(ch);
147147
}
148148

149149
TEST_F(TestATHandler, test_ATHandler_get_last_error)
@@ -460,7 +460,7 @@ TEST_F(TestATHandler, test_ATHandler_skip_param)
460460
fh1.short_value = POLLIN;
461461
at.resp_start();
462462
at.skip_param();
463-
EXPECT_TRUE(at.get_last_error() == NSAPI_ERROR_DEVICE_ERROR);
463+
EXPECT_TRUE(at.get_last_error() == NSAPI_ERROR_OK);
464464

465465
char table1[] = "ss,sssssssssssss,sssssssssssOK\r\n\0";
466466
filehandle_stub_table = table1;
@@ -935,15 +935,88 @@ TEST_F(TestATHandler, test_ATHandler_resp_start)
935935
filehandle_stub_table_pos = 0;
936936
at.resp_start();
937937

938-
char table7[] = "ssssss\0";
938+
char table7[] = "urc: info\r\nresponseOK\r\n\0";
939+
at.flush();
940+
at.clear_error();
939941
filehandle_stub_table = table7;
940942
filehandle_stub_table_pos = 0;
941943

944+
at.set_urc_handler("urc: ", NULL);
945+
at.resp_start(); // recv_buff: "responseOK\r\n\0"
946+
at.resp_stop(); // consumes to OKCRLF -> OK
947+
EXPECT_TRUE(at.get_last_error() == NSAPI_ERROR_OK);
948+
949+
char table8[] = "urc: info\r\nresponse\0";
950+
at.flush();
951+
at.clear_error();
952+
filehandle_stub_table = table8;
953+
filehandle_stub_table_pos = 0;
954+
955+
at.set_urc_handler("urc: ", NULL);
956+
at.resp_start();
957+
at.resp_stop();
958+
// No stop tag(OKCRLF) found
959+
EXPECT_TRUE(at.get_last_error() == NSAPI_ERROR_DEVICE_ERROR);
960+
961+
char table9[] = "urc: prefix: infoOK\r\n\0";
942962
at.flush();
943963
at.clear_error();
944-
at.set_urc_handler("ss", NULL);
964+
filehandle_stub_table = table9;
945965
filehandle_stub_table_pos = 0;
966+
967+
at.set_urc_handler("urc: ", NULL);
946968
at.resp_start();
969+
// Match URC consumes to CRLF -> nothing to read after that -> ERROR
970+
EXPECT_TRUE(at.get_last_error() == NSAPI_ERROR_DEVICE_ERROR);
971+
972+
char table10[] = "urc: info\r\ngarbage\r\nprefix: info\r\nOK\r\n\0";
973+
at.flush();
974+
at.clear_error();
975+
filehandle_stub_table = table10;
976+
filehandle_stub_table_pos = 0;
977+
978+
at.set_urc_handler("urc: ", NULL);
979+
at.resp_start("prefix"); // match URC -> consumes to first CRLF -> consumes the garbage because there is expected prefix and no match found -> then prefix match
980+
at.resp_stop(); //ends the info scope -> consumes to CRLF -> ends the resp scope -> consumes to OKCRLF
981+
EXPECT_TRUE(at.get_last_error() == NSAPI_ERROR_OK);
982+
983+
// No stop tag(OKCRLF) will be found because, after match URC consumed everything to CRLF, rest of buffer
984+
// is consumed to next/last CRLF because there is expected prefix and no match found
985+
// -> nothing to read after that -> ERROR
986+
char table11[] = "urc: info\r\ngarbageprefix: infoOK\r\n\0";
987+
at.flush();
988+
at.clear_error();
989+
filehandle_stub_table = table11;
990+
filehandle_stub_table_pos = 0;
991+
992+
at.set_urc_handler("urc: ", NULL);
993+
at.resp_start("prefix");
994+
EXPECT_TRUE(at.get_last_error() == NSAPI_ERROR_DEVICE_ERROR);
995+
996+
// After URC match no prefix match -> try to read more -> no more to read
997+
char table12[] = "urc: infoprefix: info\0";
998+
at.flush();
999+
at.clear_error();
1000+
filehandle_stub_table = table12;
1001+
filehandle_stub_table_pos = 0;
1002+
1003+
at.set_urc_handler("urc: ", NULL);
1004+
at.resp_start("prefix");
1005+
EXPECT_TRUE(at.get_last_error() == NSAPI_ERROR_DEVICE_ERROR);
1006+
1007+
// Will run into mem_str check of identical strings
1008+
char table13[] = "\r\n\r\n\0";
1009+
at.flush();
1010+
at.clear_error();
1011+
filehandle_stub_table = table13;
1012+
filehandle_stub_table_pos = 0;
1013+
1014+
char buf[3];
1015+
at.resp_start();
1016+
EXPECT_TRUE(2 == at.read_string(buf, 3));
1017+
EXPECT_TRUE(!strncmp(buf, "\r\n", 2));
1018+
// Consume to delimiter or stop_tag OKCRLF fails -> ERROR
1019+
EXPECT_TRUE(at.get_last_error() == NSAPI_ERROR_DEVICE_ERROR);
9471020
}
9481021

9491022
TEST_F(TestATHandler, test_ATHandler_resp_stop)

UNITTESTS/stubs/ATHandler_stub.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ nsapi_error_t ATHandler::set_urc_handler(const char *urc, mbed::Callback<void()>
105105
return ATHandler_stub::nsapi_error_value;
106106
}
107107

108-
void ATHandler::remove_urc_handler(const char *prefix, mbed::Callback<void()> callback)
108+
void ATHandler::remove_urc_handler(const char *prefix)
109109
{
110110
}
111111

UNITTESTS/stubs/FileHandle_stub.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ class FileHandle_stub : public FileHandle {
4242
if (size < ret) {
4343
ret = size;
4444
}
45-
memcpy(buffer, filehandle_stub_table, ret);
45+
memcpy(buffer, filehandle_stub_table + filehandle_stub_table_pos, ret);
4646
filehandle_stub_table_pos += ret;
4747
return ret;
4848
}

features/cellular/framework/AT/ATHandler.cpp

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ ATHandler::ATHandler(FileHandle *fh, EventQueue &queue, int timeout, const char
8181
_max_resp_length(MAX_RESP_LENGTH),
8282
_debug_on(MBED_CONF_CELLULAR_DEBUG_AT),
8383
_cmd_start(false),
84+
_use_delimiter(true),
8485
_start_time(0)
8586
{
8687
clear_error();
@@ -159,7 +160,7 @@ void ATHandler::set_is_filehandle_usable(bool usable)
159160

160161
nsapi_error_t ATHandler::set_urc_handler(const char *prefix, mbed::Callback<void()> callback)
161162
{
162-
if (find_urc_handler(prefix, &callback)) {
163+
if (find_urc_handler(prefix)) {
163164
tr_warn("URC already added with prefix: %s", prefix);
164165
return NSAPI_ERROR_OK;
165166
}
@@ -186,12 +187,12 @@ nsapi_error_t ATHandler::set_urc_handler(const char *prefix, mbed::Callback<void
186187
return NSAPI_ERROR_OK;
187188
}
188189

189-
void ATHandler::remove_urc_handler(const char *prefix, mbed::Callback<void()> callback)
190+
void ATHandler::remove_urc_handler(const char *prefix)
190191
{
191192
struct oob_t *current = _oobs;
192193
struct oob_t *prev = NULL;
193194
while (current) {
194-
if (strcmp(prefix, current->prefix) == 0 && current->cb == callback) {
195+
if (strcmp(prefix, current->prefix) == 0) {
195196
if (prev) {
196197
prev->next = current->next;
197198
} else {
@@ -205,11 +206,11 @@ void ATHandler::remove_urc_handler(const char *prefix, mbed::Callback<void()> ca
205206
}
206207
}
207208

208-
bool ATHandler::find_urc_handler(const char *prefix, mbed::Callback<void()> *callback)
209+
bool ATHandler::find_urc_handler(const char *prefix)
209210
{
210211
struct oob_t *oob = _oobs;
211212
while (oob) {
212-
if (strcmp(prefix, oob->prefix) == 0 && oob->cb == *callback) {
213+
if (strcmp(prefix, oob->prefix) == 0) {
213214
return true;
214215
}
215216
oob = oob->next;
@@ -612,6 +613,11 @@ void ATHandler::set_default_delimiter()
612613
_delimiter = DEFAULT_DELIMITER;
613614
}
614615

616+
void ATHandler::use_delimiter(bool use_delimiter)
617+
{
618+
_use_delimiter = use_delimiter;
619+
}
620+
615621
void ATHandler::set_tag(tag_t *tag_dst, const char *tag_seq)
616622
{
617623
if (tag_seq) {
@@ -814,6 +820,8 @@ void ATHandler::resp(const char *prefix, bool check_urc)
814820

815821
if (check_urc && match_urc()) {
816822
_urc_matched = true;
823+
clear_error();
824+
continue;
817825
}
818826

819827
// If no match found, look for CRLF and consume everything up to and including CRLF
@@ -1029,7 +1037,7 @@ void ATHandler::set_string(char *dest, const char *src, size_t src_len)
10291037

10301038
const char *ATHandler::mem_str(const char *dest, size_t dest_len, const char *src, size_t src_len)
10311039
{
1032-
if (dest_len > src_len) {
1040+
if (dest_len >= src_len) {
10331041
for (size_t i = 0; i < dest_len - src_len + 1; ++i) {
10341042
if (memcmp(dest + i, src, src_len) == 0) {
10351043
return dest + i;
@@ -1147,6 +1155,11 @@ bool ATHandler::check_cmd_send()
11471155
return false;
11481156
}
11491157

1158+
// Don't write delimiter if flag was set so
1159+
if (!_use_delimiter) {
1160+
return true;
1161+
}
1162+
11501163
// Don't write delimiter if this is the first subparameter
11511164
if (_cmd_start) {
11521165
_cmd_start = false;

features/cellular/framework/AT/ATHandler.h

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -109,9 +109,8 @@ class ATHandler {
109109
/** Remove urc handler from linked list of urc's
110110
*
111111
* @param prefix Register urc prefix for callback. Urc could be for example "+CMTI: "
112-
* @param callback Callback, which is called if urc is found in AT response
113112
*/
114-
void remove_urc_handler(const char *prefix, mbed::Callback<void()> callback);
113+
void remove_urc_handler(const char *prefix);
115114

116115
ATHandler *_nextATHandler; // linked list
117116

@@ -282,6 +281,12 @@ class ATHandler {
282281
*/
283282
void set_default_delimiter();
284283

284+
/** Defines behaviour for using or ignoring the delimiter within an AT command
285+
*
286+
* @param use_delimiter indicating if delimiter should be used or not
287+
*/
288+
void use_delimiter(bool use_delimiter);
289+
285290
/** Consumes the reading buffer up to the delimiter or stop_tag
286291
*
287292
* @param count number of parameters to be skipped
@@ -436,6 +441,7 @@ class ATHandler {
436441
char _info_resp_prefix[BUFF_SIZE];
437442
bool _debug_on;
438443
bool _cmd_start;
444+
bool _use_delimiter;
439445

440446
// time when a command or an URC processing was started
441447
uint64_t _start_time;
@@ -515,7 +521,7 @@ class ATHandler {
515521
const char *mem_str(const char *dest, size_t dest_len, const char *src, size_t src_len);
516522

517523
// check is urc is already added
518-
bool find_urc_handler(const char *prefix, mbed::Callback<void()> *callback);
524+
bool find_urc_handler(const char *prefix);
519525

520526
// print contents of a buffer to trace log
521527
void debug_print(char *p, int len);

features/cellular/framework/AT/AT_CellularNetwork.cpp

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,12 @@ AT_CellularNetwork::~AT_CellularNetwork()
5656

5757
for (int type = 0; type < CellularNetwork::C_MAX; type++) {
5858
if (has_registration((RegistrationType)type) != RegistrationModeDisable) {
59-
_at.remove_urc_handler(at_reg[type].urc_prefix, _urc_funcs[type]);
59+
_at.remove_urc_handler(at_reg[type].urc_prefix);
6060
}
6161
}
6262

63-
_at.remove_urc_handler("NO CARRIER", callback(this, &AT_CellularNetwork::urc_no_carrier));
64-
_at.remove_urc_handler("+CGEV:", callback(this, &AT_CellularNetwork::urc_cgev));
63+
_at.remove_urc_handler("NO CARRIER");
64+
_at.remove_urc_handler("+CGEV:");
6565
free_credentials();
6666
}
6767

@@ -386,9 +386,11 @@ nsapi_error_t AT_CellularNetwork::open_data_channel()
386386
_at.write_int(_cid);
387387
} else {
388388
MBED_ASSERT(_cid >= 0 && _cid <= 99);
389-
char cmd_buf[sizeof("ATD*99***xx#")];
390-
std::sprintf(cmd_buf, "ATD*99***%d#", _cid);
391-
_at.cmd_start(cmd_buf);
389+
_at.cmd_start("ATD*99***");
390+
_at.use_delimiter(false);
391+
_at.write_int(_cid);
392+
_at.write_string("#", false);
393+
_at.use_delimiter(true);
392394
}
393395
_at.cmd_stop();
394396

@@ -456,7 +458,7 @@ nsapi_error_t AT_CellularNetwork::disconnect()
456458

457459
_at.restore_at_timeout();
458460

459-
_at.remove_urc_handler("+CGEV:", callback(this, &AT_CellularNetwork::urc_cgev));
461+
_at.remove_urc_handler("+CGEV:");
460462
call_network_cb(NSAPI_STATUS_DISCONNECTED);
461463

462464
return _at.unlock_return_error();

features/cellular/framework/targets/GEMALTO/CINTERION/GEMALTO_CINTERION_CellularStack.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,9 @@ GEMALTO_CINTERION_CellularStack::GEMALTO_CINTERION_CellularStack(ATHandler &atHa
3939

4040
GEMALTO_CINTERION_CellularStack::~GEMALTO_CINTERION_CellularStack()
4141
{
42-
_at.remove_urc_handler("^SIS:", mbed::Callback<void()>(this, &GEMALTO_CINTERION_CellularStack::urc_sis));
43-
_at.remove_urc_handler("^SISW:", mbed::Callback<void()>(this, &GEMALTO_CINTERION_CellularStack::urc_sisw));
44-
_at.remove_urc_handler("^SISR:", mbed::Callback<void()>(this, &GEMALTO_CINTERION_CellularStack::urc_sisr));
42+
_at.remove_urc_handler("^SIS:");
43+
_at.remove_urc_handler("^SISW:");
44+
_at.remove_urc_handler("^SISR:");
4545
}
4646

4747
GEMALTO_CINTERION_CellularStack::CellularSocket *GEMALTO_CINTERION_CellularStack::find_socket(int sock_id)

features/cellular/framework/targets/QUECTEL/BG96/QUECTEL_BG96_CellularPower.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,5 @@ nsapi_error_t QUECTEL_BG96_CellularPower::set_device_ready_urc_cb(mbed::Callback
3232

3333
void QUECTEL_BG96_CellularPower::remove_device_ready_urc_cb(mbed::Callback<void()> callback)
3434
{
35-
_at.remove_urc_handler(DEVICE_READY_URC, callback);
35+
_at.remove_urc_handler(DEVICE_READY_URC);
3636
}

0 commit comments

Comments
 (0)