|
| 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_SIM_PIN_CODE |
| 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 CELLULAR_DEVICE *device; |
| 56 | +static CellularSMS* sms; |
| 57 | +static char service_center_address[SMS_MAX_PHONE_NUMBER_SIZE]; |
| 58 | +static int service_address_type; |
| 59 | + |
| 60 | +static bool cellular_status(int state, int next_state) |
| 61 | +{ |
| 62 | + if (cellular_target_state == state) { |
| 63 | + (void)network_semaphore.release(); |
| 64 | + return false; // return false -> state machine is halted |
| 65 | + } |
| 66 | + return true; |
| 67 | +} |
| 68 | + |
| 69 | +static void init() |
| 70 | +{ |
| 71 | + //the service center address is checked before any modification tests on it |
| 72 | + ATHandler *at_init = new ATHandler(&cellular_serial, queue, 30000, "\r"); |
| 73 | + at_init->cmd_start("AT+CSCA?"); |
| 74 | + at_init->cmd_stop(); |
| 75 | + |
| 76 | + TEST_ASSERT(at_init->get_last_error() == NSAPI_ERROR_OK); |
| 77 | + |
| 78 | + at_init->resp_start("+CSCA:"); |
| 79 | + at_init->read_string(service_center_address, sizeof(service_center_address)); |
| 80 | + service_address_type = at_init->read_int(); |
| 81 | + at_init->resp_stop(); |
| 82 | + |
| 83 | + TEST_ASSERT(at_init->get_last_error() == NSAPI_ERROR_OK); |
| 84 | + |
| 85 | + delete at_init; |
| 86 | + |
| 87 | +#if defined (MDMRTS) && defined (MDMCTS) |
| 88 | + cellular_serial.set_flow_control(SerialBase::RTSCTS, MDMRTS, MDMCTS); |
| 89 | +#endif |
| 90 | + cellularConnectionFSM.set_serial(&cellular_serial); |
| 91 | + cellularConnectionFSM.set_callback(&cellular_status); |
| 92 | + |
| 93 | + TEST_ASSERT(cellularConnectionFSM.init() == NSAPI_ERROR_OK); |
| 94 | + TEST_ASSERT(cellularConnectionFSM.start_dispatch() == NSAPI_ERROR_OK); |
| 95 | + |
| 96 | + device = new CELLULAR_DEVICE(queue); |
| 97 | + TEST_ASSERT(device != NULL); |
| 98 | + device->set_timeout(30000); |
| 99 | + |
| 100 | + sms = device->open_sms(&cellular_serial); |
| 101 | + TEST_ASSERT(sms != NULL); |
| 102 | + |
| 103 | + wait(3); |
| 104 | + |
| 105 | +} |
| 106 | + |
| 107 | +static void activate_context() |
| 108 | +{ |
| 109 | + CellularNetwork *network = cellularConnectionFSM.get_network(); |
| 110 | + |
| 111 | + TEST_ASSERT(network != NULL); |
| 112 | + TEST_ASSERT(network->set_credentials(MBED_CONF_APP_APN, NULL, NULL) == NSAPI_ERROR_OK); |
| 113 | + |
| 114 | + cellularConnectionFSM.set_sim_pin(MBED_CONF_APP_SIM_PIN_CODE); |
| 115 | + |
| 116 | + cellular_target_state = CellularConnectionFSM::STATE_REGISTERING_NETWORK; |
| 117 | + TEST_ASSERT(cellularConnectionFSM.continue_to_state(cellular_target_state) == NSAPI_ERROR_OK); |
| 118 | + TEST_ASSERT(network_semaphore.wait(NETWORK_TIMEOUT) == 1); // cellular network searching may take several minutes |
| 119 | +} |
| 120 | + |
| 121 | +static void test_sms_initialize_text_mode() |
| 122 | +{ |
| 123 | + TEST_ASSERT(sms->initialize(CellularSMS::CellularSMSMmodeText) == NSAPI_ERROR_OK); |
| 124 | +} |
| 125 | + |
| 126 | +static void test_sms_initialize_pdu_mode() |
| 127 | +{ |
| 128 | + TEST_ASSERT(sms->initialize(CellularSMS::CellularSMSMmodePDU) == NSAPI_ERROR_OK); |
| 129 | +} |
| 130 | + |
| 131 | +static void test_set_cscs() |
| 132 | +{ |
| 133 | + TEST_ASSERT(sms->set_cscs("IRA") == NSAPI_ERROR_OK); |
| 134 | + TEST_ASSERT(sms->set_cscs("8859-1") == NSAPI_ERROR_OK); |
| 135 | + TEST_ASSERT(sms->set_cscs("PCCP437") == NSAPI_ERROR_OK); |
| 136 | + TEST_ASSERT(sms->set_cscs("UCS2") == NSAPI_ERROR_OK); |
| 137 | + TEST_ASSERT(sms->set_cscs("GSM") == NSAPI_ERROR_OK); |
| 138 | +} |
| 139 | + |
| 140 | +static void test_set_csca() |
| 141 | +{ |
| 142 | + TEST_ASSERT(sms->set_csca("55555", 129) == NSAPI_ERROR_OK); |
| 143 | + TEST_ASSERT(sms->set_csca("+35855555", 145) == NSAPI_ERROR_OK); |
| 144 | + TEST_ASSERT(sms->set_csca(service_center_address, service_address_type) == NSAPI_ERROR_OK); |
| 145 | +} |
| 146 | + |
| 147 | +static void test_set_cpms_me() |
| 148 | +{ |
| 149 | + TEST_ASSERT(sms->set_cpms("ME", "ME", "ME") == NSAPI_ERROR_OK); |
| 150 | +} |
| 151 | + |
| 152 | +#ifdef MBED_CONF_APP_CELLULAR_PHONE_NUMBER |
| 153 | +static const char TEST_MESSAGE[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; |
| 154 | +static int callbacks_received = 0; |
| 155 | + |
| 156 | +static void sms_callback() |
| 157 | +{ |
| 158 | + callbacks_received++; |
| 159 | +} |
| 160 | + |
| 161 | +static void test_set_sms_callback() |
| 162 | +{ |
| 163 | + sms->set_sms_callback(sms_callback); |
| 164 | +} |
| 165 | + |
| 166 | + |
| 167 | +static void test_set_cpms_sm() |
| 168 | +{ |
| 169 | + TEST_ASSERT(sms->set_cpms("SM", "SM", "SM") == NSAPI_ERROR_OK); |
| 170 | +} |
| 171 | + |
| 172 | +static void test_sms_send() |
| 173 | +{ |
| 174 | + const int msg_len = sizeof(TEST_MESSAGE); |
| 175 | + TEST_ASSERT(sms->send_sms(MBED_CONF_APP_CELLULAR_PHONE_NUMBER, TEST_MESSAGE, msg_len) == msg_len); |
| 176 | +} |
| 177 | + |
| 178 | +static void test_get_sms() |
| 179 | +{ |
| 180 | + uint16_t buf_len = sizeof(TEST_MESSAGE); |
| 181 | + char buf[buf_len]; |
| 182 | + |
| 183 | + const uint16_t phone_len = sizeof(MBED_CONF_APP_CELLULAR_PHONE_NUMBER); |
| 184 | + char phone_num[phone_len]; |
| 185 | + |
| 186 | + const uint16_t time_len = sizeof("yy/MM/dd,hh:mm:ss-zz"); |
| 187 | + char time_stamp[time_len]; |
| 188 | + |
| 189 | + int buf_size = 0; |
| 190 | + |
| 191 | + wait(7); |
| 192 | + TEST_ASSERT(sms->get_sms(buf, buf_len, phone_num, phone_len, time_stamp, time_len, &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 | + |
| 197 | + TEST_ASSERT(callbacks_received > 0); |
| 198 | + callbacks_received = 0; |
| 199 | + |
| 200 | +} |
| 201 | + |
| 202 | +static void test_delete_all_messages() |
| 203 | +{ |
| 204 | + //send a message so that there is something to delete |
| 205 | + test_sms_send(); |
| 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 | +static Case cases[] = { |
| 227 | + Case("CellularSMS init", init, greentea_failure_handler), |
| 228 | + Case("CellularSMS activate context", activate_context, greentea_failure_handler), |
| 229 | + Case("CellularSMS test ME for storage", test_set_cpms_me, greentea_failure_handler), |
| 230 | + Case("CellularSMS test initialize to PDU mode", test_sms_initialize_pdu_mode, greentea_failure_handler), |
| 231 | + Case("CellularSMS test character sets", test_set_cscs, greentea_failure_handler), |
| 232 | + Case("CellularSMS test service center address", test_set_csca, greentea_failure_handler) |
| 233 | +#ifdef MBED_CONF_APP_CELLULAR_PHONE_NUMBER |
| 234 | + , |
| 235 | + Case("CellularSMS test delete all messages", test_delete_all_messages, greentea_failure_handler), |
| 236 | + Case("CellularSMS test sms callback", test_set_sms_callback, greentea_failure_handler), |
| 237 | + Case("CellularSMS test sms send", test_sms_send, greentea_failure_handler), |
| 238 | + Case("CellularSMS test get sms", test_get_sms, greentea_failure_handler), |
| 239 | + Case("CellularSMS test set sim wait time 1s", test_set_extra_sim_wait_time_1000, greentea_failure_handler), |
| 240 | + Case("CellularSMS test SM for storage", test_set_cpms_sm, greentea_failure_handler), |
| 241 | + Case("CellularSMS test initialize to text mode", test_sms_initialize_text_mode, greentea_failure_handler), |
| 242 | + Case("CellularSMS test delete all messages", test_delete_all_messages, greentea_failure_handler), |
| 243 | + Case("CellularSMS test sms send", test_sms_send, greentea_failure_handler), |
| 244 | + Case("CellularSMS test get sms", test_get_sms, greentea_failure_handler), |
| 245 | + Case("CellularSMS test delete all messages", test_delete_all_messages, greentea_failure_handler) |
| 246 | +#endif |
| 247 | +}; |
| 248 | + |
| 249 | + |
| 250 | + |
| 251 | + |
| 252 | +static utest::v1::status_t test_setup(const size_t number_of_cases) |
| 253 | +{ |
| 254 | + GREENTEA_SETUP(10*60, "default_auto"); |
| 255 | + return verbose_test_setup_handler(number_of_cases); |
| 256 | +} |
| 257 | + |
| 258 | +static Specification specification(test_setup, cases); |
| 259 | + |
| 260 | +int main() |
| 261 | +{ |
| 262 | +#if MBED_CONF_MBED_TRACE_ENABLE |
| 263 | + trace_open(); |
| 264 | +#endif |
| 265 | + int ret = Harness::run(specification); |
| 266 | +#if MBED_CONF_MBED_TRACE_ENABLE |
| 267 | + trace_close(); |
| 268 | +#endif |
| 269 | + return ret; |
| 270 | +} |
0 commit comments