|
| 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 | + |
| 44 | +#define SIM_TIMEOUT (180*1000) |
| 45 | + |
| 46 | +static UARTSerial cellular_serial(MDMTXD, MDMRXD, MBED_CONF_PLATFORM_DEFAULT_SERIAL_BAUD_RATE); |
| 47 | +static EventQueue queue(2 * EVENTS_EVENT_SIZE); |
| 48 | +static CellularConnectionFSM cellular; |
| 49 | +static CellularConnectionFSM::CellularState cellular_target_state; |
| 50 | +static rtos::Semaphore fsm_semaphore(0); |
| 51 | + |
| 52 | +static bool fsm_callback(int state, int next_state) |
| 53 | +{ |
| 54 | + if (next_state == CellularConnectionFSM::STATE_SIM_PIN) { |
| 55 | + TEST_ASSERT(fsm_semaphore.release() == osOK); |
| 56 | + return false; |
| 57 | + } |
| 58 | + return true; |
| 59 | +} |
| 60 | + |
| 61 | +static void init_to_sim_state() |
| 62 | +{ |
| 63 | + cellular.set_serial(&cellular_serial); |
| 64 | + TEST_ASSERT(cellular.init() == NSAPI_ERROR_OK); |
| 65 | +#if defined (MDMRTS) && defined (MDMCTS) |
| 66 | + cellular_serial.set_flow_control(SerialBase::RTSCTS, MDMRTS, MDMCTS); |
| 67 | +#endif |
| 68 | + cellular.set_callback(&fsm_callback); |
| 69 | + TEST_ASSERT(cellular.start_dispatch() == NSAPI_ERROR_OK); |
| 70 | + cellular_target_state = CellularConnectionFSM::STATE_SIM_PIN; |
| 71 | + TEST_ASSERT(cellular.continue_to_state(cellular_target_state) == NSAPI_ERROR_OK); |
| 72 | + TEST_ASSERT(fsm_semaphore.wait(SIM_TIMEOUT) == 1); |
| 73 | +} |
| 74 | + |
| 75 | +static void test_information_interface() |
| 76 | +{ |
| 77 | + CellularInformation *info = cellular.get_device()->open_information(&cellular_serial); |
| 78 | + |
| 79 | + char buf[100]; |
| 80 | + TEST_ASSERT(info->get_manufacturer(buf, 100) == NSAPI_ERROR_OK); |
| 81 | + TEST_ASSERT(info->get_model(buf, 100) == NSAPI_ERROR_OK); |
| 82 | + TEST_ASSERT(info->get_revision(buf, 100) == NSAPI_ERROR_OK); |
| 83 | + TEST_ASSERT(info->get_serial_number(buf, 100, CellularInformation::SN) == NSAPI_ERROR_OK); |
| 84 | + |
| 85 | + nsapi_error_t err = info->get_serial_number(buf, 100, CellularInformation::IMEI); |
| 86 | + TEST_ASSERT(err == NSAPI_ERROR_UNSUPPORTED || err == NSAPI_ERROR_OK); |
| 87 | + |
| 88 | + err = info->get_serial_number(buf, 100, CellularInformation::IMEISV); |
| 89 | + TEST_ASSERT(err == NSAPI_ERROR_UNSUPPORTED || err == NSAPI_ERROR_OK); |
| 90 | + |
| 91 | + err = info->get_serial_number(buf, 100, CellularInformation::SVN); |
| 92 | + TEST_ASSERT(err == NSAPI_ERROR_UNSUPPORTED || err == NSAPI_ERROR_OK); |
| 93 | + |
| 94 | + cellular.get_device()->close_information(); |
| 95 | +} |
| 96 | + |
| 97 | +using namespace utest::v1; |
| 98 | + |
| 99 | +static utest::v1::status_t greentea_failure_handler(const Case *const source, const failure_t reason) |
| 100 | +{ |
| 101 | + greentea_case_failure_abort_handler(source, reason); |
| 102 | + return STATUS_ABORT; |
| 103 | +} |
| 104 | + |
| 105 | +static Case cases[] = { |
| 106 | + Case("CellularInformation init", init_to_sim_state, greentea_failure_handler), |
| 107 | + Case("CellularInformation test interface", test_information_interface, greentea_failure_handler) |
| 108 | +}; |
| 109 | + |
| 110 | +static utest::v1::status_t test_setup(const size_t number_of_cases) |
| 111 | +{ |
| 112 | + GREENTEA_SETUP(10*60, "default_auto"); |
| 113 | + return verbose_test_setup_handler(number_of_cases); |
| 114 | +} |
| 115 | + |
| 116 | +static Specification specification(test_setup, cases); |
| 117 | + |
| 118 | +int main() |
| 119 | +{ |
| 120 | +#if MBED_CONF_MBED_TRACE_ENABLE |
| 121 | + trace_open(); |
| 122 | +#endif |
| 123 | + int ret = Harness::run(specification); |
| 124 | +#if MBED_CONF_MBED_TRACE_ENABLE |
| 125 | + trace_close(); |
| 126 | +#endif |
| 127 | + return ret; |
| 128 | +} |
0 commit comments