|
| 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 | +} |
0 commit comments