Skip to content

Commit 22d0d90

Browse files
author
Teppo Järvelin
committed
Cellular: Removed unnecessary checks after new
After this change we were able to change methods ATHandler::set_urc_handler and CellularDevice::set_ready_cb to void and simplify error handling.
1 parent c4138f4 commit 22d0d90

File tree

17 files changed

+56
-124
lines changed

17 files changed

+56
-124
lines changed

UNITTESTS/features/cellular/framework/AT/at_cellulardevice/at_cellulardevicetest.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,6 @@ TEST_F(TestAT_CellularDevice, test_AT_CellularDevice_set_ready_cb)
397397
FileHandle_stub fh1;
398398
AT_CellularDevice *dev = new AT_CellularDevice(&fh1);
399399

400-
EXPECT_TRUE(NSAPI_ERROR_UNSUPPORTED == dev->set_ready_cb(&device_ready_cb));
401-
EXPECT_TRUE(NSAPI_ERROR_UNSUPPORTED == dev->set_ready_cb(0));
400+
dev->set_ready_cb(&device_ready_cb);
401+
dev->set_ready_cb(0);
402402
}

UNITTESTS/features/cellular/framework/AT/at_cellularsms/at_cellularsmstest.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,14 +71,14 @@ TEST_F(TestAT_CellularSMS, test_AT_CellularSMS_initialize)
7171

7272
AT_CellularSMS sms(at);
7373
ATHandler_stub::nsapi_error_value = NSAPI_ERROR_AUTH_FAILURE;
74-
EXPECT_TRUE(NSAPI_ERROR_NO_MEMORY == sms.initialize(CellularSMS::CellularSMSMmodeText));
74+
EXPECT_EQ(NSAPI_ERROR_AUTH_FAILURE, sms.initialize(CellularSMS::CellularSMSMmodeText));
7575

7676
ATHandler_stub::nsapi_error_value = NSAPI_ERROR_OK;
77-
EXPECT_TRUE(NSAPI_ERROR_OK == sms.initialize(CellularSMS::CellularSMSMmodeText));
77+
EXPECT_EQ(NSAPI_ERROR_OK, sms.initialize(CellularSMS::CellularSMSMmodeText));
7878

7979
sms.set_sms_callback(&my_callback);
8080
ATHandler_stub::nsapi_error_value = NSAPI_ERROR_OK;
81-
EXPECT_TRUE(NSAPI_ERROR_OK == sms.initialize(CellularSMS::CellularSMSMmodeText));
81+
EXPECT_EQ(NSAPI_ERROR_OK, sms.initialize(CellularSMS::CellularSMSMmodeText));
8282

8383
ATHandler_stub::call_immediately = false;
8484
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ TEST_F(TestATHandler, test_ATHandler_set_urc_handler)
128128
at.set_urc_handler(ch, cb);
129129

130130
//THIS IS NOT same callback in find_urc_handler???
131-
EXPECT_TRUE(NSAPI_ERROR_OK == at.set_urc_handler(ch, cb));
131+
at.set_urc_handler(ch, cb);
132132
}
133133

134134
TEST_F(TestATHandler, test_ATHandler_remove_urc_handler)

UNITTESTS/stubs/ATHandler_stub.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ void ATHandler::set_file_handle(FileHandle *fh)
117117
{
118118
}
119119

120-
nsapi_error_t ATHandler::set_urc_handler(const char *urc, mbed::Callback<void()> cb)
120+
void ATHandler::set_urc_handler(const char *urc, mbed::Callback<void()> cb)
121121
{
122122
if (ATHandler_stub::urc_amount < kATHandler_urc_table_max_size) {
123123
ATHandler_stub::callback[ATHandler_stub::urc_amount] = cb;
@@ -134,7 +134,6 @@ nsapi_error_t ATHandler::set_urc_handler(const char *urc, mbed::Callback<void()>
134134
if (ATHandler_stub::call_immediately) {
135135
cb();
136136
}
137-
return ATHandler_stub::nsapi_error_value;
138137
}
139138

140139
void ATHandler::remove_urc_handler(const char *prefix)

UNITTESTS/stubs/AT_CellularDevice_stub.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -149,9 +149,8 @@ nsapi_error_t AT_CellularDevice::is_ready()
149149
return NSAPI_ERROR_OK;
150150
}
151151

152-
nsapi_error_t AT_CellularDevice::set_ready_cb(mbed::Callback<void()> callback)
152+
void AT_CellularDevice::set_ready_cb(mbed::Callback<void()> callback)
153153
{
154-
return NSAPI_ERROR_UNSUPPORTED;
155154
}
156155

157156
nsapi_error_t AT_CellularDevice::set_power_save_mode(int periodic_time, int active_time)

UNITTESTS/target_h/myCellularDevice.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,9 +129,8 @@ class myCellularDevice : public CellularDevice {
129129
return NSAPI_ERROR_OK;
130130
}
131131

132-
virtual nsapi_error_t set_ready_cb(Callback<void()> callback)
132+
virtual void set_ready_cb(Callback<void()> callback)
133133
{
134-
return NSAPI_ERROR_UNSUPPORTED;
135134
}
136135

137136
nsapi_error_t set_power_save_mode(int periodic_time, int active_time)

features/cellular/TESTS/api/cellular_power/main.cpp

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,10 @@ static void urc_callback()
5252

5353
static void wait_for_power(CellularPower *pwr)
5454
{
55-
nsapi_error_t err = cellular_device->set_ready_cb(&urc_callback);
56-
TEST_ASSERT(err == NSAPI_ERROR_OK || err == NSAPI_ERROR_UNSUPPORTED);
55+
cellular_device->set_ready_cb(&urc_callback);
5756

5857
int sanity_count = 0;
59-
err = cellular_device->init();
58+
nsapi_error_t err = cellular_device->init();
6059
while (err != NSAPI_ERROR_OK) {
6160
sanity_count++;
6261
wait(1);
@@ -65,8 +64,7 @@ static void wait_for_power(CellularPower *pwr)
6564
}
6665

6766
TEST_ASSERT(cellular_device->is_ready() == NSAPI_ERROR_OK);
68-
69-
TEST_ASSERT(cellular_device->set_ready_cb(0) == NSAPI_ERROR_OK);
67+
cellular_device->set_ready_cb(0);
7068
}
7169

7270
static void test_power_interface()

features/cellular/framework/API/CellularDevice.h

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -285,12 +285,8 @@ class CellularDevice {
285285
/** Set callback function to listen when device is ready.
286286
*
287287
* @param callback function to call on device ready, or NULL to remove callback.
288-
*
289-
* @return NSAPI_ERROR_OK on success
290-
* NSAPI_ERROR_NO_MEMORY on memory failure
291-
* NSAPI_ERROR_UNSUPPORTED if not overridden by the target modem
292288
*/
293-
virtual nsapi_error_t set_ready_cb(Callback<void()> callback) = 0;
289+
virtual void set_ready_cb(Callback<void()> callback) = 0;
294290

295291
/** Set power save mode
296292
*

features/cellular/framework/AT/ATHandler.cpp

Lines changed: 14 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -90,11 +90,7 @@ ATHandler::ATHandler(FileHandle *fh, EventQueue &queue, int timeout, const char
9090

9191
if (output_delimiter) {
9292
_output_delimiter = new char[strlen(output_delimiter) + 1];
93-
if (!_output_delimiter) {
94-
MBED_ASSERT(0);
95-
} else {
96-
memcpy(_output_delimiter, output_delimiter, strlen(output_delimiter) + 1);
97-
}
93+
memcpy(_output_delimiter, output_delimiter, strlen(output_delimiter) + 1);
9894
} else {
9995
_output_delimiter = NULL;
10096
}
@@ -160,38 +156,32 @@ void ATHandler::set_is_filehandle_usable(bool usable)
160156
_is_fh_usable = usable;
161157
}
162158

163-
nsapi_error_t ATHandler::set_urc_handler(const char *prefix, Callback<void()> callback)
159+
void ATHandler::set_urc_handler(const char *prefix, Callback<void()> callback)
164160
{
165161
if (!callback) {
166162
remove_urc_handler(prefix);
167-
return NSAPI_ERROR_OK;
163+
return;
168164
}
169165

170166
if (find_urc_handler(prefix)) {
171167
tr_warn("URC already added with prefix: %s", prefix);
172-
return NSAPI_ERROR_OK;
168+
return;
173169
}
174170

175171
struct oob_t *oob = new struct oob_t;
176-
if (!oob) {
177-
return NSAPI_ERROR_NO_MEMORY;
178-
} else {
179-
size_t prefix_len = strlen(prefix);
180-
if (prefix_len > _oob_string_max_length) {
181-
_oob_string_max_length = prefix_len;
182-
if (_oob_string_max_length > _max_resp_length) {
183-
_max_resp_length = _oob_string_max_length;
184-
}
172+
size_t prefix_len = strlen(prefix);
173+
if (prefix_len > _oob_string_max_length) {
174+
_oob_string_max_length = prefix_len;
175+
if (_oob_string_max_length > _max_resp_length) {
176+
_max_resp_length = _oob_string_max_length;
185177
}
186-
187-
oob->prefix = prefix;
188-
oob->prefix_len = prefix_len;
189-
oob->cb = callback;
190-
oob->next = _oobs;
191-
_oobs = oob;
192178
}
193179

194-
return NSAPI_ERROR_OK;
180+
oob->prefix = prefix;
181+
oob->prefix_len = prefix_len;
182+
oob->cb = callback;
183+
oob->next = _oobs;
184+
_oobs = oob;
195185
}
196186

197187
void ATHandler::remove_urc_handler(const char *prefix)

features/cellular/framework/AT/ATHandler.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,9 +101,8 @@ class ATHandler {
101101
*
102102
* @param prefix URC text to look for, e.g. "+CMTI:"
103103
* @param callback function to call on prefix, or 0 to remove callback
104-
* @return NSAPI_ERROR_OK or NSAPI_ERROR_NO_MEMORY if no memory
105104
*/
106-
nsapi_error_t set_urc_handler(const char *prefix, Callback<void()> callback);
105+
void set_urc_handler(const char *prefix, Callback<void()> callback);
107106

108107
ATHandler *_nextATHandler; // linked list
109108

features/cellular/framework/AT/AT_CellularDevice.cpp

Lines changed: 22 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -201,26 +201,22 @@ CellularContext *AT_CellularDevice::get_context_list() const
201201

202202
CellularContext *AT_CellularDevice::create_context(FileHandle *fh, const char *apn)
203203
{
204-
ATHandler *atHandler = get_at_handler(fh);
205-
if (atHandler) {
206-
AT_CellularContext *ctx = create_context_impl(*atHandler, apn);
207-
AT_CellularContext *curr = _context_list;
208-
209-
if (_context_list == NULL) {
210-
_context_list = ctx;
211-
return ctx;
212-
}
213-
214-
AT_CellularContext *prev;
215-
while (curr) {
216-
prev = curr;
217-
curr = (AT_CellularContext *)curr->_next;
218-
}
204+
AT_CellularContext *ctx = create_context_impl(*get_at_handler(fh), apn);
205+
AT_CellularContext *curr = _context_list;
219206

220-
prev->_next = ctx;
207+
if (_context_list == NULL) {
208+
_context_list = ctx;
221209
return ctx;
222210
}
223-
return NULL;
211+
212+
AT_CellularContext *prev;
213+
while (curr) {
214+
prev = curr;
215+
curr = (AT_CellularContext *)curr->_next;
216+
}
217+
218+
prev->_next = ctx;
219+
return ctx;
224220
}
225221

226222
AT_CellularContext *AT_CellularDevice::create_context_impl(ATHandler &at, const char *apn)
@@ -249,56 +245,36 @@ void AT_CellularDevice::delete_context(CellularContext *context)
249245
CellularNetwork *AT_CellularDevice::open_network(FileHandle *fh)
250246
{
251247
if (!_network) {
252-
ATHandler *atHandler = get_at_handler(fh);
253-
if (atHandler) {
254-
_network = open_network_impl(*atHandler);
255-
}
256-
}
257-
if (_network) {
258-
_network_ref_count++;
248+
_network = open_network_impl(*get_at_handler(fh));
259249
}
250+
_network_ref_count++;
260251
return _network;
261252
}
262253

263254
CellularSMS *AT_CellularDevice::open_sms(FileHandle *fh)
264255
{
265256
if (!_sms) {
266-
ATHandler *atHandler = get_at_handler(fh);
267-
if (atHandler) {
268-
_sms = open_sms_impl(*atHandler);
269-
}
270-
}
271-
if (_sms) {
272-
_sms_ref_count++;
257+
_sms = open_sms_impl(*get_at_handler(fh));
273258
}
259+
_sms_ref_count++;
274260
return _sms;
275261
}
276262

277263
CellularPower *AT_CellularDevice::open_power(FileHandle *fh)
278264
{
279265
if (!_power) {
280-
ATHandler *atHandler = get_at_handler(fh);
281-
if (atHandler) {
282-
_power = open_power_impl(*atHandler);
283-
}
284-
}
285-
if (_power) {
286-
_power_ref_count++;
266+
_power = open_power_impl(*get_at_handler(fh));
287267
}
268+
_power_ref_count++;
288269
return _power;
289270
}
290271

291272
CellularInformation *AT_CellularDevice::open_information(FileHandle *fh)
292273
{
293274
if (!_information) {
294-
ATHandler *atHandler = get_at_handler(fh);
295-
if (atHandler) {
296-
_information = open_information_impl(*atHandler);
297-
}
298-
}
299-
if (_information) {
300-
_info_ref_count++;
275+
_information = open_information_impl(*get_at_handler(fh));
301276
}
277+
_info_ref_count++;
302278
return _information;
303279
}
304280

@@ -453,9 +429,8 @@ nsapi_error_t AT_CellularDevice::is_ready()
453429
return _at->unlock_return_error();
454430
}
455431

456-
nsapi_error_t AT_CellularDevice::set_ready_cb(Callback<void()> callback)
432+
void AT_CellularDevice::set_ready_cb(Callback<void()> callback)
457433
{
458-
return NSAPI_ERROR_UNSUPPORTED;
459434
}
460435

461436
nsapi_error_t AT_CellularDevice::set_power_save_mode(int periodic_time, int active_time)

features/cellular/framework/AT/AT_CellularDevice.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ class AT_CellularDevice : public CellularDevice {
7878

7979
virtual nsapi_error_t is_ready();
8080

81-
virtual nsapi_error_t set_ready_cb(Callback<void()> callback);
81+
virtual void set_ready_cb(Callback<void()> callback);
8282

8383
virtual nsapi_error_t set_power_save_mode(int periodic_time, int active_time = 0);
8484

features/cellular/framework/AT/AT_CellularSMS.cpp

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -248,10 +248,8 @@ nsapi_error_t AT_CellularSMS::set_csdh(int show_header)
248248

249249
nsapi_error_t AT_CellularSMS::initialize(CellularSMSMmode mode)
250250
{
251-
if (NSAPI_ERROR_OK != _at.set_urc_handler("+CMTI:", callback(this, &AT_CellularSMS::cmti_urc)) ||
252-
NSAPI_ERROR_OK != _at.set_urc_handler("+CMT:", callback(this, &AT_CellularSMS::cmt_urc))) {
253-
return NSAPI_ERROR_NO_MEMORY;
254-
}
251+
_at.set_urc_handler("+CMTI:", callback(this, &AT_CellularSMS::cmti_urc));
252+
_at.set_urc_handler("+CMT:", callback(this, &AT_CellularSMS::cmt_urc));
255253

256254
_at.lock();
257255
set_cnmi(); //set new SMS indication
@@ -1047,11 +1045,6 @@ nsapi_error_t AT_CellularSMS::list_messages()
10471045
_at.resp_start("+CMGL:");
10481046
while (_at.info_resp()) {
10491047
info = new sms_info_t();
1050-
if (!info) {
1051-
_at.resp_stop();
1052-
return NSAPI_ERROR_NO_MEMORY;
1053-
}
1054-
10551048
if (_mode == CellularSMSMmodePDU) {
10561049
//+CMGL: <index>,<stat>,[<alpha>],<length><CR><LF><pdu>[<CR><LF>
10571050
// +CMGL:<index>,<stat>,[<alpha>],<length><CR><LF><pdu>
@@ -1062,11 +1055,6 @@ nsapi_error_t AT_CellularSMS::list_messages()
10621055
length = length * 2 + 20; // *2 as it's hex encoded and +20 as service center number is not included in size given by CMGL
10631056
pdu = new char[length];
10641057
memset(pdu, 0, length);
1065-
if (!pdu) {
1066-
delete info;
1067-
_at.resp_stop();
1068-
return NSAPI_ERROR_NO_MEMORY;
1069-
}
10701058
_at.read_string(pdu, length, true);
10711059
if (_at.get_last_error() == NSAPI_ERROR_OK) {
10721060
info->msg_size = get_data_from_pdu(pdu, info, &part_number);
@@ -1194,9 +1182,6 @@ uint16_t AT_CellularSMS::pack_7_bit_gsm_and_hex(const char *str, uint16_t len, c
11941182
}
11951183
// convert to 7bit gsm first
11961184
char *gsm_str = new char[len];
1197-
if (!gsm_str) {
1198-
return 0;
1199-
}
12001185
for (uint16_t y = 0; y < len; y++) {
12011186
for (int x = 0; x < GSM_TO_ASCII_TABLE_SIZE; x++) {
12021187
if (gsm_to_ascii[x] == str[y]) {

features/cellular/framework/AT/AT_CellularStack.cpp

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -116,11 +116,6 @@ nsapi_error_t AT_CellularStack::socket_open(nsapi_socket_t *handle, nsapi_protoc
116116
}
117117

118118
_socket = new CellularSocket*[max_socket_count];
119-
if (!_socket) {
120-
tr_error("No memory to open socket!");
121-
_socket_mutex.unlock();
122-
return NSAPI_ERROR_NO_SOCKET;
123-
}
124119
_socket_count = max_socket_count;
125120
for (int i = 0; i < max_socket_count; i++) {
126121
_socket[i] = 0;

features/cellular/framework/common/CellularList.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,6 @@ template <class T> class CellularList {
5050
T *add_new()
5151
{
5252
T *temp = new T;
53-
if (!temp) {
54-
return NULL;
55-
}
5653
temp->next = NULL;
5754
if (_head == NULL) {
5855
_head = temp;

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ AT_CellularInformation *QUECTEL_BG96::open_information_impl(ATHandler &at)
6666
return new QUECTEL_BG96_CellularInformation(at);
6767
}
6868

69-
nsapi_error_t QUECTEL_BG96::set_ready_cb(Callback<void()> callback)
69+
void QUECTEL_BG96::set_ready_cb(Callback<void()> callback)
7070
{
71-
return _at->set_urc_handler(DEVICE_READY_URC, callback);
71+
_at->set_urc_handler(DEVICE_READY_URC, callback);
7272
}

0 commit comments

Comments
 (0)