Skip to content

Commit b87b52a

Browse files
author
Teemu Kultala
committed
cellular: AT Handler API changes after review
1 parent dbdbae3 commit b87b52a

File tree

11 files changed

+178
-183
lines changed

11 files changed

+178
-183
lines changed

UNITTESTS/features/cellular/framework/AT/at_cellularbase/unittest.cmake

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ set(unittest-includes ${unittest-includes}
1616
# Source files
1717
set(unittest-sources
1818
../features/cellular/framework/AT/AT_CellularBase.cpp
19+
../features/cellular/framework/AT/ATHandler_factory.cpp
1920
)
2021

2122
# Test files

UNITTESTS/features/cellular/framework/AT/at_cellularcontext/unittest.cmake

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ set(unittest-includes ${unittest-includes}
1414
# Source files
1515
set(unittest-sources
1616
../features/cellular/framework/AT/AT_CellularContext.cpp
17+
../features/cellular/framework/AT/ATHandler_factory.cpp
1718
../features/cellular/framework/common/CellularUtil.cpp
1819
)
1920

UNITTESTS/features/cellular/framework/AT/at_cellulardevice/unittest.cmake

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ set(unittest-includes ${unittest-includes}
1818
set(unittest-sources
1919
stubs/randLIB_stub.c
2020
../features/cellular/framework/AT/AT_CellularDevice.cpp
21+
../features/cellular/framework/AT/ATHandler_factory.cpp
2122
)
2223

2324
# Test files

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

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -86,23 +86,36 @@ TEST_F(TestATHandler, test_ATHandler_set_file_handle)
8686
at.set_file_handle(&fh2);
8787
}
8888

89-
TEST_F(TestATHandler, test_ATHandler_get_release)
89+
TEST_F(TestATHandler, test_ATHandler_list)
9090
{
9191
EventQueue que;
9292
FileHandle_stub fh1;
9393

94-
ATHandler *at1 = ATHandler::get(&fh1, que, 0, ",", 0, 0);
94+
ATHandler::set_at_timeout_list(1000, false);
95+
ATHandler::set_debug_list(false);
96+
97+
ATHandler *at1 = ATHandler::get_instance(&fh1, que, 0, ",", 0, 0);
9598
EXPECT_TRUE(at1->get_ref_count() == 1);
96-
EXPECT_TRUE(ATHandler::get(NULL, que, 0, ",", 0, 0) == NULL);
9799

98-
ATHandler *at2 = ATHandler::get(&fh1, que, 0, ",", 0, 0);
100+
ATHandler::set_at_timeout_list(1000, false);
101+
ATHandler::set_debug_list(true);
102+
103+
EXPECT_TRUE(ATHandler::get_instance(NULL, que, 0, ",", 0, 0) == NULL);
104+
105+
ATHandler *at2 = ATHandler::get_instance(&fh1, que, 0, ",", 0, 0);
99106
EXPECT_TRUE(at1->get_ref_count() == 2);
100107
EXPECT_TRUE(at2->get_ref_count() == 2);
101108

102-
EXPECT_TRUE(ATHandler::release(at1) == NSAPI_ERROR_OK);
109+
ATHandler::set_at_timeout_list(2000, true);
110+
ATHandler::set_debug_list(false);
111+
112+
EXPECT_TRUE(at1->close() == NSAPI_ERROR_OK);
103113
EXPECT_TRUE(at2->get_ref_count() == 1);
104-
EXPECT_TRUE(ATHandler::release(at2) == NSAPI_ERROR_OK);
105-
EXPECT_TRUE(ATHandler::release(NULL) == NSAPI_ERROR_PARAMETER);
114+
EXPECT_TRUE(at2->close() == NSAPI_ERROR_OK);
115+
EXPECT_TRUE(at1->close() == NSAPI_ERROR_PARAMETER);
116+
117+
ATHandler::set_at_timeout_list(1000, false);
118+
ATHandler::set_debug_list(false);
106119
}
107120

108121
TEST_F(TestATHandler, test_ATHandler_lock)

UNITTESTS/features/cellular/framework/AT/athandler/unittest.cmake

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ set(unittest-includes ${unittest-includes}
1414
# Source files
1515
set(unittest-sources
1616
../features/cellular/framework/AT/ATHandler.cpp
17+
../features/cellular/framework/AT/ATHandler_factory.cpp
1718
../features/cellular/framework/common/CellularUtil.cpp
1819
)
1920

UNITTESTS/stubs/ATHandler_stub.cpp

Lines changed: 1 addition & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ int ATHandler_stub::urc_amount = 0;
5757
mbed::Callback<void()> ATHandler_stub::callback[kATHandler_urc_table_max_size];
5858
char *ATHandler_stub::urc_string_table[kATHandler_urc_table_max_size];
5959

60-
ATHandler::ATHandler(FileHandle *fh, EventQueue &queue, int timeout, const char *output_delimiter, uint16_t send_delay) :
60+
ATHandler::ATHandler(FileHandle *fh, EventQueue &queue, uint32_t timeout, const char *output_delimiter, uint16_t send_delay) :
6161
_nextATHandler(0),
6262
_fileHandle(fh),
6363
_queue(queue),
@@ -73,8 +73,6 @@ ATHandler::ATHandler(FileHandle *fh, EventQueue &queue, int timeout, const char
7373
ATHandler_stub::urc_string_table[i++] = NULL;
7474
}
7575
}
76-
ATHandler *ATHandler::_atHandlers = NULL;
77-
PlatformMutex ATHandler::_getReleaseMutex;
7876

7977
void ATHandler::set_debug(bool debug_on)
8078
{
@@ -123,68 +121,6 @@ void ATHandler::set_file_handle(FileHandle *fh)
123121
{
124122
}
125123

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-
188124
nsapi_error_t ATHandler::set_urc_handler(const char *urc, mbed::Callback<void()> cb)
189125
{
190126
if (ATHandler_stub::urc_amount < kATHandler_urc_table_max_size) {

UNITTESTS/stubs/AT_CellularDevice_stub.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ AT_CellularDevice::~AT_CellularDevice()
3535

3636
ATHandler *AT_CellularDevice::get_at_handler(FileHandle *fileHandle)
3737
{
38-
return ATHandler::get(fileHandle, _queue, _default_timeout, "\r", get_send_delay(), _modem_debug_on);
38+
return ATHandler::get_instance(fileHandle, _queue, _default_timeout, "\r", get_send_delay(), _modem_debug_on);
3939
}
4040

4141
ATHandler *AT_CellularDevice::get_at_handler()
@@ -45,7 +45,11 @@ ATHandler *AT_CellularDevice::get_at_handler()
4545

4646
nsapi_error_t AT_CellularDevice::release_at_handler(ATHandler *at_handler)
4747
{
48-
return ATHandler::release(at_handler);
48+
if (at_handler) {
49+
return at_handler->close();
50+
} else {
51+
return NSAPI_ERROR_PARAMETER;
52+
}
4953
}
5054

5155
CellularContext *create_context(FileHandle *fh = NULL, const char *apn = MBED_CONF_NSAPI_DEFAULT_CELLULAR_APN)
@@ -60,7 +64,7 @@ void delete_context(CellularContext *context)
6064

6165
CellularNetwork *AT_CellularDevice::open_network(FileHandle *fh)
6266
{
63-
return new AT_CellularNetwork(*ATHandler::get(fh, _queue, _default_timeout, "\r", get_send_delay(), _modem_debug_on));
67+
return new AT_CellularNetwork(*ATHandler::get_instance(fh, _queue, _default_timeout, "\r", get_send_delay(), _modem_debug_on));
6468
}
6569

6670
CellularSMS *AT_CellularDevice::open_sms(FileHandle *fh)

features/cellular/framework/AT/ATHandler.cpp

Lines changed: 1 addition & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ static const uint8_t map_3gpp_errors[][2] = {
6060
{ 146, 46 }, { 178, 65 }, { 179, 66 }, { 180, 48 }, { 181, 83 }, { 171, 49 },
6161
};
6262

63-
ATHandler::ATHandler(FileHandle *fh, EventQueue &queue, int timeout, const char *output_delimiter, uint16_t send_delay) :
63+
ATHandler::ATHandler(FileHandle *fh, EventQueue &queue, uint32_t timeout, const char *output_delimiter, uint16_t send_delay) :
6464
_nextATHandler(0),
6565
_fileHandle(fh),
6666
_queue(queue),
@@ -110,8 +110,6 @@ ATHandler::ATHandler(FileHandle *fh, EventQueue &queue, int timeout, const char
110110

111111
set_file_handle(fh);
112112
}
113-
ATHandler *ATHandler::_atHandlers = NULL;
114-
PlatformMutex ATHandler::_getReleaseMutex;
115113

116114
void ATHandler::set_debug(bool debug_on)
117115
{
@@ -162,69 +160,6 @@ void ATHandler::set_is_filehandle_usable(bool usable)
162160
_is_fh_usable = usable;
163161
}
164162

165-
166-
// each parser is associated with one filehandle (that is UART)
167-
ATHandler *ATHandler::get(FileHandle *fileHandle, events::EventQueue &queue, uint32_t timeout,
168-
const char *delimiter, uint16_t send_delay, bool debug_on)
169-
{
170-
if (!fileHandle) {
171-
return NULL;
172-
}
173-
174-
_getReleaseMutex.lock();
175-
ATHandler *atHandler = _atHandlers;
176-
while (atHandler) {
177-
if (atHandler->get_file_handle() == fileHandle) {
178-
atHandler->inc_ref_count();
179-
_getReleaseMutex.unlock();
180-
return atHandler;
181-
}
182-
atHandler = atHandler->_nextATHandler;
183-
}
184-
185-
atHandler = new ATHandler(fileHandle, queue, timeout, delimiter, send_delay);
186-
if (debug_on) {
187-
atHandler->set_debug(debug_on);
188-
}
189-
atHandler->_nextATHandler = _atHandlers;
190-
_atHandlers = atHandler;
191-
192-
_getReleaseMutex.unlock();
193-
return atHandler;
194-
}
195-
196-
nsapi_error_t ATHandler::release(ATHandler *at_handler)
197-
{
198-
if (!at_handler || at_handler->get_ref_count() == 0) {
199-
return NSAPI_ERROR_PARAMETER;
200-
}
201-
202-
_getReleaseMutex.lock();
203-
at_handler->dec_ref_count();
204-
if (at_handler->get_ref_count() == 0) {
205-
// we can delete this at_handler
206-
ATHandler *atHandler = _atHandlers;
207-
ATHandler *prev = NULL;
208-
while (atHandler) {
209-
if (atHandler == at_handler) {
210-
if (prev == NULL) {
211-
_atHandlers = _atHandlers->_nextATHandler;
212-
} else {
213-
prev->_nextATHandler = atHandler->_nextATHandler;
214-
}
215-
delete atHandler;
216-
at_handler = NULL;
217-
break;
218-
} else {
219-
prev = atHandler;
220-
atHandler = atHandler->_nextATHandler;
221-
}
222-
}
223-
}
224-
_getReleaseMutex.unlock();
225-
return NSAPI_ERROR_OK;
226-
}
227-
228163
nsapi_error_t ATHandler::set_urc_handler(const char *prefix, mbed::Callback<void()> callback)
229164
{
230165
if (find_urc_handler(prefix)) {

features/cellular/framework/AT/ATHandler.h

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ class ATHandler {
7474
* @param output_delimiter delimiter used when parsing at responses, "\r" should be used as output_delimiter
7575
* @param send_delay the minimum delay in ms between the end of last response and the beginning of a new command
7676
*/
77-
ATHandler(FileHandle *fh, events::EventQueue &queue, int timeout, const char *output_delimiter, uint16_t send_delay = 0);
77+
ATHandler(FileHandle *fh, events::EventQueue &queue, uint32_t timeout, const char *output_delimiter, uint16_t send_delay = 0);
7878
virtual ~ATHandler();
7979

8080
/** Return used file handle.
@@ -83,9 +83,14 @@ class ATHandler {
8383
*/
8484
FileHandle *get_file_handle();
8585

86-
/** Get a new ATHandler instance, and update the linked list
86+
/** Get a new ATHandler instance, and update the linked list. Once the use of the ATHandler
87+
* has finished, call to close() has to be made
8788
*
88-
* @param fileHandle filehandle used for reading AT responses and writing AT commands
89+
* @param fileHandle filehandle used for reading AT responses and writing AT commands.
90+
* If there is already an ATHandler with the same fileHandle pointer,
91+
* then a pointer to that ATHandler instance will be returned with
92+
* that ATHandler's queue, timeout, delimiter, send_delay and debug_on
93+
* values
8994
* @param queue Event queue used to transfer sigio events to this thread
9095
* @param timeout Timeout when reading for AT response
9196
* @param delimiter delimiter used when parsing at responses, "\r" should be used as output_delimiter
@@ -94,15 +99,15 @@ class ATHandler {
9499
* @return NULL, if fileHandle is not set, or a pointer to an existing ATHandler, if the fileHandle is
95100
* already in use. Otherwise a pointer to a new ATHandler instance is returned
96101
*/
97-
static ATHandler *get(FileHandle *fileHandle, events::EventQueue &queue, uint32_t timeout,
98-
const char *delimiter, uint16_t send_delay, bool debug_on);
102+
static ATHandler *get_instance(FileHandle *fileHandle, events::EventQueue &queue, uint32_t timeout,
103+
const char *delimiter, uint16_t send_delay, bool debug_on);
99104

100-
/** Release an ATHandler instance, and update the linked list
105+
/** Close and delete the current ATHandler instance, if the reference count to it is 0.
106+
* Close() can be only called, if the ATHandler was opened with get_instance()
101107
*
102-
* @param at_handler Pointer to the ATHandler
103108
* @return NSAPI_ERROR_OK on success, NSAPI_ERROR_PARAMETER on failure
104109
*/
105-
static nsapi_error_t release(ATHandler *at_handler);
110+
nsapi_error_t close();
106111

107112
/** Locks the mutex for file handle if AT_HANDLER_MUTEX is defined.
108113
*/
@@ -148,10 +153,14 @@ class ATHandler {
148153
device_err_t get_last_device_error() const;
149154

150155
/** Increase reference count. Used for counting references to this instance.
156+
* Note that this should be used with care, if the ATHandler was taken into use
157+
* with get_instance()
151158
*/
152159
void inc_ref_count();
153160

154161
/** Decrease reference count. Used for counting references to this instance.
162+
* Note that this should be used with care, if the ATHandler was taken into use
163+
* with get_instance()
155164
*/
156165
void dec_ref_count();
157166

@@ -168,6 +177,13 @@ class ATHandler {
168177
*/
169178
void set_at_timeout(uint32_t timeout_milliseconds, bool default_timeout = false);
170179

180+
/** Set timeout in milliseconds for all ATHandlers in the _atHandlers list
181+
*
182+
* @param timeout_milliseconds Timeout in milliseconds
183+
* @param default_timeout Store as default timeout
184+
*/
185+
static void set_at_timeout_list(uint32_t timeout_milliseconds, bool default_timeout = false);
186+
171187
/** Restore timeout to previous timeout. Handy if there is a need to change timeout temporarily.
172188
*/
173189
void restore_at_timeout();
@@ -244,7 +260,6 @@ class ATHandler {
244260
bool _is_fh_usable;
245261

246262
static ATHandler *_atHandlers;
247-
static PlatformMutex _getReleaseMutex;
248263

249264
//*************************************
250265
public:
@@ -422,6 +437,12 @@ class ATHandler {
422437
*/
423438
void set_debug(bool debug_on);
424439

440+
/** Set debug_on for all ATHandlers in the _atHandlers list
441+
*
442+
* @param debug_on Set true to enable debug traces
443+
*/
444+
static void set_debug_list(bool debug_on);
445+
425446
private:
426447

427448
// should fit any prefix and int

0 commit comments

Comments
 (0)