1717
1818#include < ctype.h>
1919#include " nsapi_types.h"
20- #include " ATHandler.h"
2120#include " events/EventQueue.h"
2221#include " ATHandler_stub.h"
2322
@@ -27,6 +26,7 @@ using namespace events;
2726#include " CellularLog.h"
2827
2928const int DEFAULT_AT_TIMEOUT = 1000 ; // at default timeout in milliseconds
29+ const uint8_t MAX_RESP_LENGTH = 7 ;
3030
3131nsapi_error_t ATHandler_stub::nsapi_error_value = 0 ;
3232uint8_t ATHandler_stub::nsapi_error_ok_counter = 0 ;
@@ -55,9 +55,6 @@ bool ATHandler_stub::process_oob_urc = false;
5555int ATHandler_stub::read_string_index = kRead_string_table_size ;
5656const char *ATHandler_stub::read_string_table[kRead_string_table_size ] = {' \0 ' };
5757int ATHandler_stub::resp_stop_success_count = kResp_stop_count_default ;
58- int ATHandler_stub::urc_amount = 0 ;
59- mbed::Callback<void ()> ATHandler_stub::callback[kATHandler_urc_table_max_size ];
60- char *ATHandler_stub::urc_string_table[kATHandler_urc_table_max_size ] = {NULL };
6158
6259bool ATHandler_stub::get_debug_flag = false ;
6360uint8_t ATHandler_stub::set_debug_call_count = 0 ;
@@ -86,17 +83,14 @@ ATHandler::ATHandler(FileHandle *fh, EventQueue &queue, uint32_t timeout, const
8683 _nextATHandler(0 ),
8784 _fileHandle(fh),
8885 _queue(queue),
89- _ref_count(1 )
86+ _ref_count(1 ),
87+ _oob_string_max_length(0 ),
88+ _oobs(NULL ),
89+ _max_resp_length(MAX_RESP_LENGTH)
9090{
9191 ATHandler_stub::ref_count = 1 ;
9292
9393 ATHandler_stub::process_oob_urc = false ;
94- ATHandler_stub::urc_amount = 0 ;
95- int i = 0 ;
96- while (i < kATHandler_urc_table_max_size ) {
97- ATHandler_stub::callback[i] = NULL ;
98- ATHandler_stub::urc_string_table[i++] = NULL ;
99- }
10094}
10195
10296void ATHandler::set_debug (bool debug_on)
@@ -115,15 +109,10 @@ bool ATHandler::get_debug() const
115109ATHandler::~ATHandler ()
116110{
117111 ATHandler_stub::ref_count = kATHandler_destructor_ref_ount ;
118-
119- int i = 0 ;
120- while (i < kATHandler_urc_table_max_size ) {
121- if (ATHandler_stub::urc_string_table[i]) {
122- delete [] ATHandler_stub::urc_string_table[i];
123- i++;
124- } else {
125- break ;
126- }
112+ while (_oobs) {
113+ struct oob_t *oob = _oobs;
114+ _oobs = oob->next ;
115+ delete oob;
127116 }
128117}
129118
@@ -154,46 +143,66 @@ void ATHandler::set_file_handle(FileHandle *fh)
154143{
155144}
156145
146+ bool ATHandler::find_urc_handler (const char *prefix)
147+ {
148+ struct oob_t *oob = _oobs;
149+ while (oob) {
150+ if (strcmp (prefix, oob->prefix ) == 0 ) {
151+ return true ;
152+ }
153+ oob = oob->next ;
154+ }
155+
156+ return false ;
157+ }
158+
157159void ATHandler::set_urc_handler (const char *urc, mbed::Callback<void ()> cb)
158160{
159161 if (!cb) {
160162 remove_urc_handler (urc);
161163 return ;
162164 }
163165
164- if (ATHandler_stub::urc_amount < kATHandler_urc_table_max_size ) {
165- ATHandler_stub::callback[ATHandler_stub::urc_amount] = cb;
166- if (urc) {
167- ATHandler_stub::urc_string_table[ATHandler_stub::urc_amount] = new char [kATHandler_urc_string_max_size ];
168- memset (ATHandler_stub::urc_string_table[ATHandler_stub::urc_amount], 0 , sizeof (ATHandler_stub::urc_string_table[ATHandler_stub::urc_amount]));
169- int bytes_to_copy = strlen (urc) < kATHandler_urc_string_max_size ? strlen (urc) : kATHandler_urc_string_max_size ;
170- memcpy (ATHandler_stub::urc_string_table[ATHandler_stub::urc_amount], urc, bytes_to_copy);
166+ if (find_urc_handler (urc)) {
167+ return ;
168+ }
169+
170+ struct oob_t *oob = new struct oob_t ;
171+ size_t prefix_len = strlen (urc);
172+ if (prefix_len > _oob_string_max_length) {
173+ _oob_string_max_length = prefix_len;
174+ if (_oob_string_max_length > _max_resp_length) {
175+ _max_resp_length = _oob_string_max_length;
171176 }
172- ATHandler_stub::urc_amount++;
173- } else {
174- ATHandler_stub::callback[0 ] = cb;
175- MBED_ASSERT (" ATHandler URC amount limit reached" );
176177 }
178+
179+ oob->prefix = urc;
180+ oob->prefix_len = prefix_len;
181+ oob->cb = cb;
182+ oob->next = _oobs;
183+ _oobs = oob;
184+
177185 if (ATHandler_stub::call_immediately) {
178186 cb ();
179187 }
180188}
181189
182190void ATHandler::remove_urc_handler (const char *prefix)
183191{
184- bool found_urc = false ;
185- for (int i = 0 ; i < ATHandler_stub::urc_amount; i++) {
186- if (found_urc && i < 0 ) {
187- ATHandler_stub::urc_string_table[i - 1 ] = ATHandler_stub::urc_string_table[i];
188- ATHandler_stub::urc_string_table[i] = 0 ;
189- } else if (ATHandler_stub::urc_string_table[i] && strcmp (prefix, ATHandler_stub::urc_string_table[i]) == 0 ) {
190- delete [] ATHandler_stub::urc_string_table[i];
191- ATHandler_stub::urc_string_table[i] = 0 ;
192- found_urc = true ;
192+ struct oob_t *current = _oobs;
193+ struct oob_t *prev = NULL ;
194+ while (current) {
195+ if (strcmp (prefix, current->prefix ) == 0 ) {
196+ if (prev) {
197+ prev->next = current->next ;
198+ } else {
199+ _oobs = current->next ;
200+ }
201+ delete current;
202+ break ;
193203 }
194- }
195- if (found_urc) {
196- ATHandler_stub::urc_amount--;
204+ prev = current;
205+ current = prev->next ;
197206 }
198207}
199208
@@ -232,21 +241,13 @@ void ATHandler::restore_at_timeout()
232241void ATHandler::process_oob ()
233242{
234243 if (ATHandler_stub::process_oob_urc) {
235- int i = 0 ;
236- while (i < ATHandler_stub::urc_amount) {
237- if (ATHandler_stub::read_string_index >= 0 ) {
238- int len = 0 ;
239- if (ATHandler_stub::urc_string_table[i]) {
240- len = strlen (ATHandler_stub::urc_string_table[i]);
241- }
242- if (!memcmp (ATHandler_stub::urc_string_table[i],
243- ATHandler_stub::read_string_table[ATHandler_stub::read_string_index],
244- len)) {
245- ATHandler_stub::callback[i]();
246- break ;
247- }
244+ size_t prefix_len = 0 ;
245+ for (struct oob_t *oob = _oobs; oob; oob = oob->next ) {
246+ prefix_len = oob->prefix_len ;
247+ if (!memcmp (oob->prefix , ATHandler_stub::read_string_table[ATHandler_stub::read_string_index], prefix_len)) {
248+ oob->cb ();
249+ break ;
248250 }
249- i++;
250251 }
251252 }
252253}
0 commit comments