Skip to content

Commit c82af3d

Browse files
author
Cruz Monrreal
authored
Merge pull request #7102 from jarvte/adding_cellular_tests
Adding cellular tests
2 parents c3e639a + 9a3c3b5 commit c82af3d

File tree

16 files changed

+363
-14
lines changed

16 files changed

+363
-14
lines changed
Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
/*
2+
* Copyright (c) 2018, Arm Limited and affiliates.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
19+
#if !defined(MBED_CONF_NSAPI_PRESENT)
20+
#error [NOT_SUPPORTED] A json configuration file is needed. Skipping this build.
21+
#endif
22+
23+
#include "CellularUtil.h" // for CELLULAR_ helper macros
24+
#include "CellularTargets.h"
25+
26+
#ifndef CELLULAR_DEVICE
27+
#error [NOT_SUPPORTED] CELLULAR_DEVICE must be defined
28+
#endif
29+
30+
#include "greentea-client/test_env.h"
31+
#include "unity.h"
32+
#include "utest.h"
33+
34+
#include "mbed.h"
35+
36+
#include "CellularLog.h"
37+
#include "CellularDevice.h"
38+
#include CELLULAR_STRINGIFY(CELLULAR_DEVICE.h)
39+
40+
static UARTSerial cellular_serial(MDMTXD, MDMRXD, MBED_CONF_PLATFORM_DEFAULT_SERIAL_BAUD_RATE);
41+
static CellularDevice *device;
42+
static EventQueue queue(8 * EVENTS_EVENT_SIZE);
43+
44+
static void create_device()
45+
{
46+
device = new CELLULAR_DEVICE(queue);
47+
TEST_ASSERT(device != NULL);
48+
}
49+
50+
static void open_close_interfaces()
51+
{
52+
CellularNetwork *nw = device->open_network(&cellular_serial);
53+
TEST_ASSERT(nw != NULL);
54+
device->close_network();
55+
nw = device->open_network(NULL);
56+
TEST_ASSERT(nw == NULL);
57+
58+
CellularSIM* sim = device->open_sim(&cellular_serial);
59+
TEST_ASSERT(sim != NULL);
60+
device->close_sim();
61+
sim = device->open_sim(NULL);
62+
TEST_ASSERT(sim == NULL);
63+
64+
CellularInformation* info = device->open_information(&cellular_serial);
65+
TEST_ASSERT(info != NULL);
66+
device->close_information();
67+
info = device->open_information(NULL);
68+
TEST_ASSERT(info == NULL);
69+
70+
CellularPower* power = device->open_power(&cellular_serial);
71+
TEST_ASSERT(power != NULL);
72+
device->close_power();
73+
power = device->open_power(NULL);
74+
TEST_ASSERT(power == NULL);
75+
76+
CellularSMS* sms = device->open_sms(&cellular_serial);
77+
TEST_ASSERT(sms != NULL);
78+
device->close_sms();
79+
sms = device->open_sms(NULL);
80+
TEST_ASSERT(sms == NULL);
81+
}
82+
83+
static void other_methods()
84+
{
85+
// test first without any open interfaces
86+
device->set_timeout(5000);
87+
device->modem_debug_on(true);
88+
device->modem_debug_on(false);
89+
NetworkStack* stack = device->get_stack();
90+
TEST_ASSERT(stack == NULL);
91+
92+
CellularNetwork *nw = device->open_network(&cellular_serial);
93+
TEST_ASSERT(nw != NULL);
94+
95+
// then test witj open interface which is called
96+
device->set_timeout(5000);
97+
device->modem_debug_on(true);
98+
device->modem_debug_on(false);
99+
stack = device->get_stack();
100+
TEST_ASSERT(stack != NULL);
101+
}
102+
103+
static void delete_device()
104+
{
105+
// delete will close all opened interfaces
106+
delete device;
107+
device = NULL;
108+
}
109+
110+
using namespace utest::v1;
111+
112+
static utest::v1::status_t greentea_failure_handler(const Case *const source, const failure_t reason)
113+
{
114+
greentea_case_failure_abort_handler(source, reason);
115+
return STATUS_ABORT;
116+
}
117+
118+
static Case cases[] = {
119+
Case("CellularDevice create device", create_device, greentea_failure_handler),
120+
Case("CellularDevice Open and close interfaces", open_close_interfaces, greentea_failure_handler),
121+
Case("CellularDevice other methods", other_methods, greentea_failure_handler),
122+
Case("CellularDevice delete device", delete_device, greentea_failure_handler)
123+
};
124+
125+
static utest::v1::status_t test_setup(const size_t number_of_cases)
126+
{
127+
GREENTEA_SETUP(10*60, "default_auto");
128+
return verbose_test_setup_handler(number_of_cases);
129+
}
130+
131+
static Specification specification(test_setup, cases);
132+
133+
int main()
134+
{
135+
mbed_trace_init();
136+
137+
return Harness::run(specification);
138+
}
139+

features/cellular/UNITTESTS/at/at_cellulardevice/at_cellulardevicetest.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,3 +97,17 @@ TEST(AT_CellularDevice, test_AT_CellularDevice_close_information)
9797
unit->test_AT_CellularDevice_close_information();
9898
}
9999

100+
TEST(AT_CellularDevice, test_AT_CellularDevice_set_timeout)
101+
{
102+
unit->test_AT_CellularDevice_set_timeout();
103+
}
104+
105+
TEST(AT_CellularDevice, test_AT_CellularDevice_modem_debug_on)
106+
{
107+
unit->test_AT_CellularDevice_modem_debug_on();
108+
}
109+
110+
TEST(AT_CellularDevice, test_AT_CellularDevice_get_stack)
111+
{
112+
unit->test_AT_CellularDevice_get_stack();
113+
}

features/cellular/UNITTESTS/at/at_cellulardevice/test_at_cellulardevice.cpp

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,22 +112,56 @@ void Test_AT_CellularDevice::test_AT_CellularDevice_open_information()
112112

113113
void Test_AT_CellularDevice::test_AT_CellularDevice_close_network()
114114
{
115+
EventQueue que;
116+
AT_CellularDevice dev(que);
117+
FileHandle_stub fh1;
118+
ATHandler_stub::ref_count = 0;
115119

120+
CHECK(dev.open_network(&fh1));
121+
CHECK(ATHandler_stub::ref_count == 1);
122+
123+
dev.close_network();
116124
}
117125

118126
void Test_AT_CellularDevice::test_AT_CellularDevice_close_sms()
119127
{
128+
EventQueue que;
129+
AT_CellularDevice dev(que);
130+
FileHandle_stub fh1;
131+
ATHandler_stub::ref_count = 0;
132+
133+
CHECK(dev.open_sms(&fh1));
134+
CHECK(ATHandler_stub::ref_count == 1);
120135

136+
dev.close_sms();
121137
}
122138

123139
void Test_AT_CellularDevice::test_AT_CellularDevice_close_power()
124140
{
141+
EventQueue que;
142+
AT_CellularDevice dev(que);
143+
FileHandle_stub fh1;
144+
ATHandler_stub::ref_count = 0;
125145

146+
CHECK(dev.open_power(&fh1));
147+
CHECK(ATHandler_stub::ref_count == 1);
148+
149+
dev.close_power();
126150
}
127151

128152
void Test_AT_CellularDevice::test_AT_CellularDevice_close_sim()
129153
{
154+
EventQueue que;
155+
AT_CellularDevice dev(que);
156+
FileHandle_stub fh1;
157+
ATHandler_stub::ref_count = 0;
158+
159+
160+
CHECK(dev.open_sim(&fh1));
161+
dev.close_sms(); // this should not affect to refcount as it's not opened
162+
CHECK(ATHandler_stub::ref_count == 1);
130163

164+
dev.close_sim();
131165
}
132166

133167
void Test_AT_CellularDevice::test_AT_CellularDevice_close_information()
@@ -155,3 +189,61 @@ void Test_AT_CellularDevice::test_AT_CellularDevice_close_information()
155189
ATHandler_stub::fh_value = NULL;
156190
}
157191

192+
void Test_AT_CellularDevice::test_AT_CellularDevice_set_timeout()
193+
{
194+
EventQueue que;
195+
AT_CellularDevice dev(que);
196+
FileHandle_stub fh1;
197+
ATHandler_stub::timeout = 0;
198+
ATHandler_stub::default_timeout = false;
199+
200+
// no interfaces open so settings timeout should not change anything
201+
dev.set_timeout(5000);
202+
CHECK(ATHandler_stub::timeout == 0);
203+
CHECK(ATHandler_stub::default_timeout == false);
204+
205+
CHECK(dev.open_sim(&fh1));
206+
CHECK(ATHandler_stub::ref_count == 1);
207+
208+
dev.set_timeout(5000);
209+
CHECK(ATHandler_stub::timeout == 5000);
210+
CHECK(ATHandler_stub::default_timeout == true);
211+
212+
dev.close_sim();
213+
}
214+
215+
void Test_AT_CellularDevice::test_AT_CellularDevice_modem_debug_on()
216+
{
217+
EventQueue que;
218+
AT_CellularDevice dev(que);
219+
FileHandle_stub fh1;
220+
ATHandler_stub::debug_on = false;
221+
222+
// no interfaces open so debug toggling should not affect
223+
dev.modem_debug_on(true);
224+
CHECK(ATHandler_stub::debug_on == false);
225+
226+
CHECK(dev.open_sim(&fh1));
227+
CHECK(ATHandler_stub::ref_count == 1);
228+
229+
dev.modem_debug_on(true);
230+
CHECK(ATHandler_stub::debug_on == true);
231+
232+
dev.close_sim();
233+
}
234+
235+
void Test_AT_CellularDevice::test_AT_CellularDevice_get_stack()
236+
{
237+
EventQueue que;
238+
AT_CellularDevice dev(que);
239+
FileHandle_stub fh1;
240+
241+
NetworkStack *stack = dev.get_stack();
242+
CHECK(stack == NULL);
243+
244+
CHECK(dev.open_network(&fh1));
245+
246+
stack = dev.get_stack();
247+
CHECK(stack == NULL); // Not in PPP so also null but this is got from the network class
248+
}
249+

features/cellular/UNITTESTS/at/at_cellulardevice/test_at_cellulardevice.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,12 @@ class Test_AT_CellularDevice
4747
void test_AT_CellularDevice_close_sim();
4848

4949
void test_AT_CellularDevice_close_information();
50+
51+
void test_AT_CellularDevice_set_timeout();
52+
53+
void test_AT_CellularDevice_modem_debug_on();
54+
55+
void test_AT_CellularDevice_get_stack();
5056
};
5157

5258
#endif // TEST_AT_CELLULARDEVICE_H

features/cellular/UNITTESTS/at/athandler/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,5 @@ TEST_SRC_FILES = \
2525

2626
include ../../MakefileWorker.mk
2727

28-
CPPUTESTFLAGS += -DFEA_TRACE_SUPPORT
28+
CPPUTESTFLAGS += -DFEA_TRACE_SUPPORT -DMBED_CONF_CELLULAR_DEBUG_AT=1
2929

features/cellular/UNITTESTS/at/athandler/athandlertest.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -207,9 +207,9 @@ TEST(ATHandler, test_ATHandler_consume_to_stop_tag)
207207
unit->test_ATHandler_consume_to_stop_tag();
208208
}
209209

210-
TEST(ATHandler, test_ATHandler_enable_debug)
210+
TEST(ATHandler, test_ATHandler_set_debug)
211211
{
212-
unit->test_ATHandler_enable_debug();
212+
unit->test_ATHandler_set_debug();
213213
}
214214

215215
TEST(ATHandler, test_ATHandler_get_3gpp_error)

features/cellular/UNITTESTS/at/athandler/test_athandler.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -778,15 +778,15 @@ void Test_ATHandler::test_ATHandler_consume_to_stop_tag()
778778
CHECK(at.consume_to_stop_tag());
779779
}
780780

781-
void Test_ATHandler::test_ATHandler_enable_debug()
781+
void Test_ATHandler::test_ATHandler_set_debug()
782782
{
783783
EventQueue que;
784784
FileHandle_stub fh1;
785785

786786
ATHandler at(&fh1, que, 0, ",");
787-
at.enable_debug(true);
787+
at.set_debug(true);
788788

789-
at.enable_debug(false);
789+
at.set_debug(false);
790790
}
791791

792792
void Test_ATHandler::test_ATHandler_get_3gpp_error()

features/cellular/UNITTESTS/at/athandler/test_athandler.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ class Test_ATHandler
9292

9393
void test_ATHandler_consume_to_stop_tag();
9494

95-
void test_ATHandler_enable_debug();
95+
void test_ATHandler_set_debug();
9696

9797
void test_ATHandler_get_3gpp_error();
9898
};

features/cellular/UNITTESTS/stubs/ATHandler_stub.cpp

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ const int DEFAULT_AT_TIMEOUT = 1000; // at default timeout in milliseconds
3131
nsapi_error_t ATHandler_stub::nsapi_error_value = 0;
3232
uint8_t ATHandler_stub::nsapi_error_ok_counter = 0;
3333
int ATHandler_stub::int_value = -1;
34+
int ATHandler_stub::ref_count = 0;
35+
int ATHandler_stub::timeout = 0;
36+
bool ATHandler_stub::default_timeout = 0;
37+
bool ATHandler_stub::debug_on = 0;
3438
ssize_t ATHandler_stub::ssize_value = 0;
3539
char* ATHandler_stub::read_string_value = NULL;
3640
size_t ATHandler_stub::size_value = 0;
@@ -47,27 +51,32 @@ ATHandler::ATHandler(FileHandle *fh, EventQueue &queue, int timeout, const char
4751
_fileHandle(fh),
4852
_queue(queue)
4953
{
54+
ATHandler_stub::ref_count = 1;
5055
}
5156

52-
void ATHandler::enable_debug(bool enable)
57+
void ATHandler::set_debug(bool debug_on)
5358
{
59+
ATHandler_stub::debug_on = debug_on;
5460
}
5561

5662
ATHandler::~ATHandler()
5763
{
64+
ATHandler_stub::ref_count = -909;
5865
}
5966

6067
void ATHandler::inc_ref_count()
6168
{
69+
ATHandler_stub::ref_count++;
6270
}
6371

6472
void ATHandler::dec_ref_count()
6573
{
74+
ATHandler_stub::ref_count--;
6675
}
6776

6877
int ATHandler::get_ref_count()
6978
{
70-
return ATHandler_stub::int_value;
79+
return ATHandler_stub::ref_count;
7180
}
7281

7382
FileHandle *ATHandler::get_file_handle()
@@ -113,6 +122,8 @@ nsapi_error_t ATHandler::unlock_return_error()
113122

114123
void ATHandler::set_at_timeout(uint32_t timeout_milliseconds, bool default_timeout)
115124
{
125+
ATHandler_stub::timeout = timeout_milliseconds;
126+
ATHandler_stub::default_timeout = default_timeout;
116127
}
117128

118129
void ATHandler::restore_at_timeout()

0 commit comments

Comments
 (0)