17
17
18
18
#include < ctype.h>
19
19
#include " nsapi_types.h"
20
- #include " ATHandler.h"
21
20
#include " events/EventQueue.h"
22
21
#include " ATHandler_stub.h"
23
22
@@ -27,6 +26,7 @@ using namespace events;
27
26
#include " CellularLog.h"
28
27
29
28
const int DEFAULT_AT_TIMEOUT = 1000 ; // at default timeout in milliseconds
29
+ const uint8_t MAX_RESP_LENGTH = 7 ;
30
30
31
31
nsapi_error_t ATHandler_stub::nsapi_error_value = 0 ;
32
32
uint8_t ATHandler_stub::nsapi_error_ok_counter = 0 ;
@@ -55,9 +55,6 @@ bool ATHandler_stub::process_oob_urc = false;
55
55
int ATHandler_stub::read_string_index = kRead_string_table_size ;
56
56
const char *ATHandler_stub::read_string_table[kRead_string_table_size ] = {' \0 ' };
57
57
int 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 };
61
58
62
59
bool ATHandler_stub::get_debug_flag = false ;
63
60
uint8_t ATHandler_stub::set_debug_call_count = 0 ;
@@ -86,17 +83,14 @@ ATHandler::ATHandler(FileHandle *fh, EventQueue &queue, uint32_t timeout, const
86
83
_nextATHandler(0 ),
87
84
_fileHandle(fh),
88
85
_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)
90
90
{
91
91
ATHandler_stub::ref_count = 1 ;
92
92
93
93
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
- }
100
94
}
101
95
102
96
void ATHandler::set_debug (bool debug_on)
@@ -115,15 +109,10 @@ bool ATHandler::get_debug() const
115
109
ATHandler::~ATHandler ()
116
110
{
117
111
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;
127
116
}
128
117
}
129
118
@@ -154,46 +143,66 @@ void ATHandler::set_file_handle(FileHandle *fh)
154
143
{
155
144
}
156
145
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
+
157
159
void ATHandler::set_urc_handler (const char *urc, mbed::Callback<void ()> cb)
158
160
{
159
161
if (!cb) {
160
162
remove_urc_handler (urc);
161
163
return ;
162
164
}
163
165
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;
171
176
}
172
- ATHandler_stub::urc_amount++;
173
- } else {
174
- ATHandler_stub::callback[0 ] = cb;
175
- MBED_ASSERT (" ATHandler URC amount limit reached" );
176
177
}
178
+
179
+ oob->prefix = urc;
180
+ oob->prefix_len = prefix_len;
181
+ oob->cb = cb;
182
+ oob->next = _oobs;
183
+ _oobs = oob;
184
+
177
185
if (ATHandler_stub::call_immediately) {
178
186
cb ();
179
187
}
180
188
}
181
189
182
190
void ATHandler::remove_urc_handler (const char *prefix)
183
191
{
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 ;
193
203
}
194
- }
195
- if (found_urc) {
196
- ATHandler_stub::urc_amount--;
204
+ prev = current;
205
+ current = prev->next ;
197
206
}
198
207
}
199
208
@@ -232,21 +241,13 @@ void ATHandler::restore_at_timeout()
232
241
void ATHandler::process_oob ()
233
242
{
234
243
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 ;
248
250
}
249
- i++;
250
251
}
251
252
}
252
253
}
0 commit comments