|
| 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 | +#include "greentea-client/test_env.h" |
| 35 | +#include "unity.h" |
| 36 | +#include "utest.h" |
| 37 | + |
| 38 | +#include "mbed.h" |
| 39 | + |
| 40 | +#include "CellularConnectionFSM.h" |
| 41 | +#include "CellularDevice.h" |
| 42 | +#include "../../cellular_tests_common.h" |
| 43 | +#include CELLULAR_STRINGIFY(CELLULAR_DEVICE.h) |
| 44 | + |
| 45 | +#define NETWORK_TIMEOUT (180*1000) |
| 46 | + |
| 47 | +static UARTSerial cellular_serial(MDMTXD, MDMRXD, MBED_CONF_PLATFORM_DEFAULT_SERIAL_BAUD_RATE); |
| 48 | +static EventQueue queue(8 * EVENTS_EVENT_SIZE); |
| 49 | +static rtos::Semaphore network_semaphore(0); |
| 50 | +static CellularConnectionFSM cellular; |
| 51 | +static CellularConnectionFSM::CellularState cellular_target_state; |
| 52 | + |
| 53 | +static char *get_rand_string(char *str, size_t size) |
| 54 | +{ |
| 55 | + const char charset[] = "0123456789"; |
| 56 | + if (size) { |
| 57 | + --size; |
| 58 | + for (size_t n = 0; n < size; n++) { |
| 59 | + int key = rand() % (int) (sizeof charset - 1); |
| 60 | + str[n] = charset[key]; |
| 61 | + } |
| 62 | + str[size] = '\0'; |
| 63 | + } |
| 64 | + return str; |
| 65 | +} |
| 66 | + |
| 67 | +static bool fsm_callback(int state, int next_state) |
| 68 | +{ |
| 69 | + if (next_state == CellularConnectionFSM::STATE_SIM_PIN) { |
| 70 | + MBED_ASSERT(network_semaphore.release() == osOK); |
| 71 | + return false; |
| 72 | + } |
| 73 | + return true; |
| 74 | +} |
| 75 | + |
| 76 | +static void init_to_sim_state() |
| 77 | +{ |
| 78 | + cellular.set_serial(&cellular_serial); |
| 79 | + TEST_ASSERT(cellular.init() == NSAPI_ERROR_OK); |
| 80 | +#if defined (MDMRTS) && defined (MDMCTS) |
| 81 | + cellular_serial.set_flow_control(SerialBase::RTSCTS, MDMRTS, MDMCTS); |
| 82 | +#endif |
| 83 | + cellular.set_callback(&fsm_callback); |
| 84 | + TEST_ASSERT(cellular.start_dispatch() == NSAPI_ERROR_OK); |
| 85 | + cellular_target_state = CellularConnectionFSM::STATE_SIM_PIN; |
| 86 | + TEST_ASSERT(cellular.continue_to_state(cellular_target_state) == NSAPI_ERROR_OK); |
| 87 | + TEST_ASSERT(network_semaphore.wait(NETWORK_TIMEOUT) == 1); |
| 88 | +} |
| 89 | + |
| 90 | +static void test_sim_interface() |
| 91 | +{ |
| 92 | + CellularSIM *sim = cellular.get_sim(); |
| 93 | + TEST_ASSERT(sim != NULL); |
| 94 | + |
| 95 | + // 1. test set_pin |
| 96 | + nsapi_error_t err = sim->set_pin(MBED_CONF_APP_CELLULAR_SIM_PIN); |
| 97 | + MBED_ASSERT(err == NSAPI_ERROR_OK); |
| 98 | + |
| 99 | + // 2. test change pin |
| 100 | + char pin[5]; |
| 101 | + int sanity_count = 0; |
| 102 | + while (strcmp(get_rand_string(pin, 5), MBED_CONF_APP_CELLULAR_SIM_PIN) == 0) { |
| 103 | + sanity_count++; |
| 104 | + TEST_ASSERT(sanity_count < 50); |
| 105 | + }; |
| 106 | + |
| 107 | + // change pin and change it back |
| 108 | + err = sim->change_pin(MBED_CONF_APP_CELLULAR_SIM_PIN, pin); |
| 109 | + MBED_ASSERT(err == NSAPI_ERROR_OK); |
| 110 | + err = sim->change_pin(pin, MBED_CONF_APP_CELLULAR_SIM_PIN); |
| 111 | + MBED_ASSERT(err == NSAPI_ERROR_OK); |
| 112 | + |
| 113 | + // 3. test set_pin_query |
| 114 | + err = sim->set_pin_query(MBED_CONF_APP_CELLULAR_SIM_PIN, false); |
| 115 | + MBED_ASSERT(err == NSAPI_ERROR_OK); |
| 116 | + err = sim->set_pin_query(MBED_CONF_APP_CELLULAR_SIM_PIN, true); |
| 117 | + MBED_ASSERT(err == NSAPI_ERROR_OK); |
| 118 | + |
| 119 | + // 4. test get_sim_state |
| 120 | + CellularSIM::SimState state; |
| 121 | + err = sim->get_sim_state(state); |
| 122 | + MBED_ASSERT(err == NSAPI_ERROR_OK); |
| 123 | + MBED_ASSERT(state == CellularSIM::SimStateReady); |
| 124 | + |
| 125 | + // 5. test get_imsi |
| 126 | + char imsi[16] = {0}; |
| 127 | + err = sim->get_imsi(imsi); |
| 128 | + MBED_ASSERT(err == NSAPI_ERROR_OK); |
| 129 | + MBED_ASSERT(strlen(imsi) > 0); |
| 130 | +} |
| 131 | + |
| 132 | +using namespace utest::v1; |
| 133 | + |
| 134 | +static utest::v1::status_t greentea_failure_handler(const Case *const source, const failure_t reason) |
| 135 | +{ |
| 136 | + greentea_case_failure_abort_handler(source, reason); |
| 137 | + return STATUS_ABORT; |
| 138 | +} |
| 139 | + |
| 140 | +static Case cases[] = { |
| 141 | + Case("CellularSIM init", init_to_sim_state, greentea_failure_handler), |
| 142 | + Case("CellularSIM test interface", test_sim_interface, greentea_failure_handler) |
| 143 | +}; |
| 144 | + |
| 145 | +static utest::v1::status_t test_setup(const size_t number_of_cases) |
| 146 | +{ |
| 147 | + GREENTEA_SETUP(10*60, "default_auto"); |
| 148 | + return verbose_test_setup_handler(number_of_cases); |
| 149 | +} |
| 150 | + |
| 151 | +static Specification specification(test_setup, cases); |
| 152 | + |
| 153 | +int main() |
| 154 | +{ |
| 155 | +#if MBED_CONF_MBED_TRACE_ENABLE |
| 156 | + trace_open(); |
| 157 | +#endif |
| 158 | + int ret = Harness::run(specification); |
| 159 | +#if MBED_CONF_MBED_TRACE_ENABLE |
| 160 | + trace_close(); |
| 161 | +#endif |
| 162 | + return ret; |
| 163 | +} |
0 commit comments