Skip to content

Commit 4e0112c

Browse files
author
Cruz Monrreal
authored
Merge pull request ARMmbed#7531 from TeemuKultala/sms_tests
Cellular: sms greentea tests
2 parents d360da9 + df82525 commit 4e0112c

File tree

2 files changed

+298
-11
lines changed

2 files changed

+298
-11
lines changed
Lines changed: 271 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,271 @@
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+
#ifndef MBED_CONF_APP_CELLULAR_SIM_PIN
31+
#error [NOT_SUPPORTED] SIM pin code is needed. Skipping this build.
32+
#endif
33+
34+
35+
36+
#include "greentea-client/test_env.h"
37+
#include "unity.h"
38+
#include "utest.h"
39+
40+
#include "mbed.h"
41+
42+
#include "AT_CellularSMS.h"
43+
#include "CellularConnectionFSM.h"
44+
#include "CellularDevice.h"
45+
#include "../../cellular_tests_common.h"
46+
#include CELLULAR_STRINGIFY(CELLULAR_DEVICE.h)
47+
48+
#define NETWORK_TIMEOUT (600*1000)
49+
50+
static UARTSerial cellular_serial(MDMTXD, MDMRXD, MBED_CONF_PLATFORM_DEFAULT_SERIAL_BAUD_RATE);
51+
static EventQueue queue(8 * EVENTS_EVENT_SIZE);
52+
static rtos::Semaphore network_semaphore(0);
53+
static CellularConnectionFSM cellularConnectionFSM;
54+
static CellularConnectionFSM::CellularState cellular_target_state;
55+
static CellularSMS* sms;
56+
static char service_center_address[SMS_MAX_PHONE_NUMBER_SIZE];
57+
static int service_address_type;
58+
59+
static bool cellular_status(int state, int next_state)
60+
{
61+
if (cellular_target_state == state) {
62+
(void)network_semaphore.release();
63+
return false; // return false -> state machine is halted
64+
}
65+
return true;
66+
}
67+
68+
static void init()
69+
{
70+
//the service center address is checked before any modification tests on it
71+
ATHandler *at_init = new ATHandler(&cellular_serial, queue, 30000, "\r");
72+
at_init->cmd_start("AT+CSCA?");
73+
at_init->cmd_stop();
74+
75+
TEST_ASSERT(at_init->get_last_error() == NSAPI_ERROR_OK);
76+
77+
at_init->resp_start("+CSCA:");
78+
at_init->read_string(service_center_address, sizeof(service_center_address));
79+
service_address_type = at_init->read_int();
80+
at_init->resp_stop();
81+
82+
TEST_ASSERT(at_init->get_last_error() == NSAPI_ERROR_OK);
83+
84+
delete at_init;
85+
86+
#if defined (MDMRTS) && defined (MDMCTS)
87+
cellular_serial.set_flow_control(SerialBase::RTSCTS, MDMRTS, MDMCTS);
88+
#endif
89+
cellularConnectionFSM.set_serial(&cellular_serial);
90+
cellularConnectionFSM.set_callback(&cellular_status);
91+
92+
TEST_ASSERT(cellularConnectionFSM.init() == NSAPI_ERROR_OK);
93+
TEST_ASSERT(cellularConnectionFSM.start_dispatch() == NSAPI_ERROR_OK);
94+
95+
96+
CellularDevice *device = cellularConnectionFSM.get_device();
97+
98+
TEST_ASSERT(device != NULL);
99+
device->set_timeout(30000);
100+
101+
sms = device->open_sms(&cellular_serial);
102+
TEST_ASSERT(sms != NULL);
103+
104+
wait(3);
105+
106+
}
107+
108+
static void activate_context()
109+
{
110+
CellularNetwork *network = cellularConnectionFSM.get_network();
111+
112+
TEST_ASSERT(network != NULL);
113+
TEST_ASSERT(network->set_credentials(MBED_CONF_APP_APN, NULL, NULL) == NSAPI_ERROR_OK);
114+
115+
cellularConnectionFSM.set_sim_pin(MBED_CONF_APP_CELLULAR_SIM_PIN);
116+
117+
cellular_target_state = CellularConnectionFSM::STATE_REGISTERING_NETWORK;
118+
TEST_ASSERT(cellularConnectionFSM.continue_to_state(cellular_target_state) == NSAPI_ERROR_OK);
119+
TEST_ASSERT(network_semaphore.wait(NETWORK_TIMEOUT) == 1); // cellular network searching may take several minutes
120+
}
121+
122+
static void test_sms_initialize_text_mode()
123+
{
124+
TEST_ASSERT(sms->initialize(CellularSMS::CellularSMSMmodeText) == NSAPI_ERROR_OK);
125+
}
126+
127+
static void test_sms_initialize_pdu_mode()
128+
{
129+
TEST_ASSERT(sms->initialize(CellularSMS::CellularSMSMmodePDU) == NSAPI_ERROR_OK);
130+
}
131+
132+
static void test_set_cscs()
133+
{
134+
TEST_ASSERT(sms->set_cscs("IRA") == NSAPI_ERROR_OK);
135+
TEST_ASSERT(sms->set_cscs("8859-1") == NSAPI_ERROR_OK);
136+
TEST_ASSERT(sms->set_cscs("PCCP437") == NSAPI_ERROR_OK);
137+
TEST_ASSERT(sms->set_cscs("UCS2") == NSAPI_ERROR_OK);
138+
TEST_ASSERT(sms->set_cscs("GSM") == NSAPI_ERROR_OK);
139+
}
140+
141+
static void test_set_csca()
142+
{
143+
TEST_ASSERT(sms->set_csca("55555", 129) == NSAPI_ERROR_OK);
144+
TEST_ASSERT(sms->set_csca("+35855555", 145) == NSAPI_ERROR_OK);
145+
TEST_ASSERT(sms->set_csca(service_center_address, service_address_type) == NSAPI_ERROR_OK);
146+
}
147+
148+
static void test_set_cpms_me()
149+
{
150+
TEST_ASSERT(sms->set_cpms("ME", "ME", "ME") == NSAPI_ERROR_OK);
151+
}
152+
153+
#ifdef MBED_CONF_APP_CELLULAR_PHONE_NUMBER
154+
static const char TEST_MESSAGE[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
155+
static int callbacks_received = 0;
156+
157+
static void sms_callback()
158+
{
159+
callbacks_received++;
160+
}
161+
162+
static void test_set_sms_callback()
163+
{
164+
sms->set_sms_callback(sms_callback);
165+
}
166+
167+
168+
static void test_set_cpms_sm()
169+
{
170+
TEST_ASSERT(sms->set_cpms("SM", "SM", "SM") == NSAPI_ERROR_OK);
171+
}
172+
173+
static void test_sms_send()
174+
{
175+
const int msg_len = strlen(TEST_MESSAGE);
176+
TEST_ASSERT(sms->send_sms(MBED_CONF_APP_CELLULAR_PHONE_NUMBER, TEST_MESSAGE, msg_len) == msg_len);
177+
}
178+
179+
static void test_get_sms()
180+
{
181+
uint16_t buf_len = sizeof(TEST_MESSAGE);
182+
char buf[buf_len];
183+
184+
char phone_num[SMS_MAX_PHONE_NUMBER_SIZE];
185+
186+
char time_stamp[SMS_MAX_TIME_STAMP_SIZE];
187+
188+
int buf_size = 0;
189+
190+
wait(7);
191+
192+
TEST_ASSERT(sms->get_sms(buf, buf_len, phone_num, SMS_MAX_PHONE_NUMBER_SIZE, time_stamp, SMS_MAX_TIME_STAMP_SIZE, &buf_size) == buf_len-1);
193+
TEST_ASSERT(strcmp(phone_num, MBED_CONF_APP_CELLULAR_PHONE_NUMBER) == 0);
194+
TEST_ASSERT(strcmp(buf, TEST_MESSAGE) == 0);
195+
TEST_ASSERT(buf_size == 0);
196+
TEST_ASSERT(callbacks_received > 0);
197+
callbacks_received = 0;
198+
199+
}
200+
201+
static void test_delete_all_messages()
202+
{
203+
//send a message so that there is something to delete
204+
test_sms_send();
205+
wait(7);
206+
TEST_ASSERT(sms->delete_all_messages() == NSAPI_ERROR_OK);
207+
callbacks_received = 0;
208+
}
209+
210+
static void test_set_extra_sim_wait_time_1000()
211+
{
212+
sms->set_extra_sim_wait_time(1000);
213+
}
214+
215+
#endif
216+
217+
218+
using namespace utest::v1;
219+
220+
static utest::v1::status_t greentea_failure_handler(const Case *const source, const failure_t reason)
221+
{
222+
greentea_case_failure_abort_handler(source, reason);
223+
return STATUS_ABORT;
224+
}
225+
226+
227+
static Case cases[] = {
228+
Case("CellularSMS init", init, greentea_failure_handler),
229+
Case("CellularSMS activate context", activate_context, greentea_failure_handler),
230+
Case("CellularSMS test ME for storage", test_set_cpms_me, greentea_failure_handler),
231+
Case("CellularSMS test initialize to PDU mode", test_sms_initialize_pdu_mode, greentea_failure_handler),
232+
Case("CellularSMS test character sets", test_set_cscs, greentea_failure_handler),
233+
Case("CellularSMS test service center address", test_set_csca, greentea_failure_handler)
234+
#ifdef MBED_CONF_APP_CELLULAR_PHONE_NUMBER
235+
,
236+
Case("CellularSMS test delete all messages", test_delete_all_messages, greentea_failure_handler),
237+
Case("CellularSMS test sms callback", test_set_sms_callback, greentea_failure_handler),
238+
Case("CellularSMS test sms send", test_sms_send, greentea_failure_handler),
239+
Case("CellularSMS test get sms", test_get_sms, greentea_failure_handler),
240+
Case("CellularSMS test set sim wait time 1s", test_set_extra_sim_wait_time_1000, greentea_failure_handler),
241+
Case("CellularSMS test SM for storage", test_set_cpms_sm, greentea_failure_handler),
242+
Case("CellularSMS test initialize to text mode", test_sms_initialize_text_mode, greentea_failure_handler),
243+
Case("CellularSMS test delete all messages", test_delete_all_messages, greentea_failure_handler),
244+
Case("CellularSMS test sms send", test_sms_send, greentea_failure_handler),
245+
Case("CellularSMS test get sms", test_get_sms, greentea_failure_handler),
246+
Case("CellularSMS test delete all messages", test_delete_all_messages, greentea_failure_handler)
247+
#endif
248+
};
249+
250+
251+
252+
253+
static utest::v1::status_t test_setup(const size_t number_of_cases)
254+
{
255+
GREENTEA_SETUP(600, "default_auto");
256+
return verbose_test_setup_handler(number_of_cases);
257+
}
258+
259+
static Specification specification(test_setup, cases);
260+
261+
int main()
262+
{
263+
#if MBED_CONF_MBED_TRACE_ENABLE
264+
trace_open();
265+
#endif
266+
int ret = Harness::run(specification);
267+
#if MBED_CONF_MBED_TRACE_ENABLE
268+
trace_close();
269+
#endif
270+
return ret;
271+
}

features/cellular/framework/AT/AT_CellularSMS.cpp

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -640,7 +640,11 @@ nsapi_size_or_error_t AT_CellularSMS::read_sms_from_index(int msg_index, char *b
640640
}
641641
_at.skip_param(); // <alpha>
642642
if (time_stamp) {
643-
_at.read_string(time_stamp, SMS_MAX_TIME_STAMP_SIZE);
643+
int len = _at.read_string(time_stamp, SMS_MAX_TIME_STAMP_SIZE);
644+
if (len < (SMS_MAX_TIME_STAMP_SIZE - 2)) {
645+
time_stamp[len++] = ',';
646+
_at.read_string(&time_stamp[len], SMS_MAX_TIME_STAMP_SIZE-len);
647+
}
644648
}
645649
(void)_at.consume_to_stop_tag(); // consume until <CR><LF>
646650
if (buf) {
@@ -735,7 +739,7 @@ nsapi_size_or_error_t AT_CellularSMS::get_sms(char *buf, uint16_t len, char *pho
735739
sms_info_t *info = get_oldest_sms_index();
736740

737741
if (info) {
738-
if (info->msg_size + 1 > len) { // +1 for '\0'
742+
if (info->msg_size > len) {
739743
tr_warn("Given buf too small, len is: %d but is must be: %d", len, info->msg_size);
740744
if (buf_size) {
741745
*buf_size = info->msg_size;
@@ -802,22 +806,34 @@ nsapi_size_or_error_t AT_CellularSMS::get_data_from_pdu(const char *pdu, sms_inf
802806
// originating address length
803807
oaLength = hex_str_to_int(pdu + index, 2);
804808
index += 2; // add index over address length
805-
index += 2; // skip number type
809+
int type = hex_str_to_int(pdu + index, 1);
810+
index += 2; // add index over type
806811
if (phone_number) {
807812
// phone number as reverse nibble encoded
808-
int a = 0;
809-
for (; a < oaLength; a += 2) {
810-
if (a + 1 == oaLength) {
811-
phone_number[a] = pdu[index + a + 1];
813+
int a = 0, field_length = oaLength;
814+
815+
if (type == 9) {
816+
//add the plus sign in case of international number (23.040 chapter address fields)
817+
phone_number[a++] = '+';
818+
field_length++;
819+
}
820+
821+
for (; a < field_length; a += 2) {
822+
if ((a + 1) == field_length) {
823+
phone_number[a] = pdu[index + 1];
824+
index++;
812825
} else {
813-
phone_number[a] = pdu[index + a + 1];
814-
phone_number[a + 1] = pdu[index + a];
826+
phone_number[a] = pdu[index + 1];
827+
phone_number[a + 1] = pdu[index];
828+
index += 2;
815829
}
816830
}
817-
phone_number[oaLength] = '\0';
831+
phone_number[field_length] = '\0';
832+
} else {
833+
index += oaLength;
818834
}
819835

820-
index += oaLength;
836+
821837
if (oaLength & 0x01) { // if phone number length is odd then it has padded F so skip that
822838
index++;
823839
}

0 commit comments

Comments
 (0)