@@ -60,7 +60,8 @@ char *ATHandler_stub::urc_string_table[kATHandler_urc_table_max_size];
60
60
ATHandler::ATHandler (FileHandle *fh, EventQueue &queue, int timeout, const char *output_delimiter, uint16_t send_delay) :
61
61
_nextATHandler(0 ),
62
62
_fileHandle(fh),
63
- _queue(queue)
63
+ _queue(queue),
64
+ _ref_count(1 )
64
65
{
65
66
ATHandler_stub::ref_count = 1 ;
66
67
@@ -72,6 +73,8 @@ ATHandler::ATHandler(FileHandle *fh, EventQueue &queue, int timeout, const char
72
73
ATHandler_stub::urc_string_table[i++] = NULL ;
73
74
}
74
75
}
76
+ ATHandler *ATHandler::_atHandlers = NULL ;
77
+ PlatformMutex ATHandler::_getReleaseMutex;
75
78
76
79
void ATHandler::set_debug (bool debug_on)
77
80
{
@@ -95,28 +98,93 @@ ATHandler::~ATHandler()
95
98
96
99
void ATHandler::inc_ref_count ()
97
100
{
98
- ATHandler_stub::ref_count++;
101
+ _ref_count++;
102
+ ATHandler_stub::ref_count = _ref_count;
99
103
}
100
104
101
105
void ATHandler::dec_ref_count ()
102
106
{
103
- ATHandler_stub::ref_count--;
107
+ _ref_count--;
108
+ ATHandler_stub::ref_count = _ref_count;
104
109
}
105
110
106
111
int ATHandler::get_ref_count ()
107
112
{
108
- return ATHandler_stub::ref_count ;
113
+ return _ref_count ;
109
114
}
110
115
111
116
FileHandle *ATHandler::get_file_handle ()
112
117
{
113
- return ATHandler_stub::fh_value;
118
+ ATHandler_stub::fh_value = (FileHandle_stub *)_fileHandle;
119
+ return _fileHandle;
114
120
}
115
121
116
122
void ATHandler::set_file_handle (FileHandle *fh)
117
123
{
118
124
}
119
125
126
+ // each parser is associated with one filehandle (that is UART)
127
+ ATHandler *ATHandler::get (FileHandle *fileHandle, events::EventQueue &queue, uint32_t timeout,
128
+ const char *delimiter, uint16_t send_delay, bool debug_on)
129
+ {
130
+ if (!fileHandle) {
131
+ return NULL ;
132
+ }
133
+
134
+ _getReleaseMutex.lock ();
135
+ ATHandler *atHandler = _atHandlers;
136
+ while (atHandler) {
137
+ if (atHandler->get_file_handle () == fileHandle) {
138
+ atHandler->inc_ref_count ();
139
+ _getReleaseMutex.unlock ();
140
+ return atHandler;
141
+ }
142
+ atHandler = atHandler->_nextATHandler ;
143
+ }
144
+
145
+ atHandler = new ATHandler (fileHandle, queue, timeout, delimiter, send_delay);
146
+ if (debug_on) {
147
+ atHandler->set_debug (debug_on);
148
+ }
149
+ atHandler->_nextATHandler = _atHandlers;
150
+ _atHandlers = atHandler;
151
+
152
+ _getReleaseMutex.unlock ();
153
+ return atHandler;
154
+ }
155
+
156
+ nsapi_error_t ATHandler::release (ATHandler *at_handler)
157
+ {
158
+ if (!at_handler || at_handler->get_ref_count () == 0 ) {
159
+ return NSAPI_ERROR_PARAMETER;
160
+ }
161
+
162
+ _getReleaseMutex.lock ();
163
+ at_handler->dec_ref_count ();
164
+ if (at_handler->get_ref_count () == 0 ) {
165
+ // we can delete this at_handler
166
+ ATHandler *atHandler = _atHandlers;
167
+ ATHandler *prev = NULL ;
168
+ while (atHandler) {
169
+ if (atHandler == at_handler) {
170
+ if (prev == NULL ) {
171
+ _atHandlers = _atHandlers->_nextATHandler ;
172
+ } else {
173
+ prev->_nextATHandler = atHandler->_nextATHandler ;
174
+ }
175
+ delete atHandler;
176
+ at_handler = NULL ;
177
+ break ;
178
+ } else {
179
+ prev = atHandler;
180
+ atHandler = atHandler->_nextATHandler ;
181
+ }
182
+ }
183
+ }
184
+ _getReleaseMutex.unlock ();
185
+ return NSAPI_ERROR_OK;
186
+ }
187
+
120
188
nsapi_error_t ATHandler::set_urc_handler (const char *urc, mbed::Callback<void ()> cb)
121
189
{
122
190
if (ATHandler_stub::urc_amount < kATHandler_urc_table_max_size ) {
0 commit comments