Cellular: fixed cellular greentea tests after major refactoring.

pull/8579/head
Teppo Järvelin 2018-11-13 10:31:08 +02:00
parent 1b3db96634
commit 14f3740c13
14 changed files with 139 additions and 358 deletions

View File

@ -41,6 +41,7 @@ set(unittest-test-sources
stubs/Semaphore_stub.cpp
stubs/UARTSerial_stub.cpp
stubs/SerialBase_stub.cpp
stubs/CellularStateMachine_stub.cpp
)
# defines

View File

@ -39,11 +39,10 @@
static UARTSerial cellular_serial(MDMTXD, MDMRXD, MBED_CONF_PLATFORM_DEFAULT_SERIAL_BAUD_RATE);
static CellularDevice *device;
static EventQueue queue(8 * EVENTS_EVENT_SIZE);
static void create_device()
{
device = new CELLULAR_DEVICE(queue);
device = new CELLULAR_DEVICE(&cellular_serial);
TEST_ASSERT(device != NULL);
}
@ -52,32 +51,26 @@ static void open_close_interfaces()
CellularNetwork *nw = device->open_network(&cellular_serial);
TEST_ASSERT(nw != NULL);
device->close_network();
nw = device->open_network(NULL);
TEST_ASSERT(nw == NULL);
CellularSIM *sim = device->open_sim(&cellular_serial);
TEST_ASSERT(sim != NULL);
device->close_sim();
sim = device->open_sim(NULL);
TEST_ASSERT(sim == NULL);
CellularInformation *info = device->open_information(&cellular_serial);
TEST_ASSERT(info != NULL);
device->close_information();
info = device->open_information(NULL);
TEST_ASSERT(info == NULL);
CellularPower *power = device->open_power(&cellular_serial);
TEST_ASSERT(power != NULL);
device->close_power();
power = device->open_power(NULL);
TEST_ASSERT(power == NULL);
CellularSMS *sms = device->open_sms(&cellular_serial);
TEST_ASSERT(sms != NULL);
device->close_sms();
sms = device->open_sms(NULL);
TEST_ASSERT(sms == NULL);
CellularContext *ctx = device->create_context();
TEST_ASSERT(ctx != NULL);
device->delete_context(ctx);
}
static void other_methods()
@ -86,8 +79,6 @@ static void other_methods()
device->set_timeout(5000);
device->modem_debug_on(true);
device->modem_debug_on(false);
NetworkStack *stack = device->get_stack();
TEST_ASSERT(stack == NULL);
CellularNetwork *nw = device->open_network(&cellular_serial);
TEST_ASSERT(nw != NULL);
@ -96,8 +87,6 @@ static void other_methods()
device->set_timeout(5000);
device->modem_debug_on(true);
device->modem_debug_on(false);
stack = device->get_stack();
TEST_ASSERT(stack != NULL);
}
static void delete_device()

View File

@ -39,44 +39,28 @@
#include "AT_CellularInformation.h"
#include "CellularConnectionFSM.h"
#include "CellularContext.h"
#include "CellularDevice.h"
#include "../../cellular_tests_common.h"
#define SIM_TIMEOUT (180*1000)
static UARTSerial cellular_serial(MDMTXD, MDMRXD, MBED_CONF_PLATFORM_DEFAULT_SERIAL_BAUD_RATE);
static EventQueue queue(2 * EVENTS_EVENT_SIZE);
static CellularConnectionFSM cellular;
static CellularConnectionFSM::CellularState cellular_target_state;
static rtos::Semaphore fsm_semaphore(0);
static bool fsm_callback(int state, int next_state)
{
if (next_state == CellularConnectionFSM::STATE_SIM_PIN) {
TEST_ASSERT(fsm_semaphore.release() == osOK);
return false;
}
return true;
}
static CellularContext *ctx;
static void init_to_sim_state()
{
cellular.set_serial(&cellular_serial);
TEST_ASSERT(cellular.init() == NSAPI_ERROR_OK);
#if defined (MDMRTS) && defined (MDMCTS)
cellular_serial.set_flow_control(SerialBase::RTSCTS, MDMRTS, MDMCTS);
ctx = CellularContext::get_default_instance();
TEST_ASSERT(ctx != NULL);
ctx->set_sim_pin(MBED_CONF_APP_CELLULAR_SIM_PIN);
#ifdef MBED_CONF_APP_APN
ctx->set_credentials(MBED_CONF_APP_APN);
#endif
cellular.set_callback(&fsm_callback);
TEST_ASSERT(cellular.start_dispatch() == NSAPI_ERROR_OK);
cellular_target_state = CellularConnectionFSM::STATE_SIM_PIN;
TEST_ASSERT(cellular.continue_to_state(cellular_target_state) == NSAPI_ERROR_OK);
TEST_ASSERT(fsm_semaphore.wait(SIM_TIMEOUT) == 1);
TEST_ASSERT(ctx->set_sim_ready() == NSAPI_ERROR_OK);
}
static void test_information_interface()
{
CellularInformation *info = cellular.get_device()->open_information(&cellular_serial);
CellularDevice *dev = CellularDevice::get_default_instance();
CellularInformation *info = dev->open_information();
const int kbuf_size = 100;
char *buf = new char[kbuf_size];
@ -96,7 +80,7 @@ static void test_information_interface()
err = info->get_serial_number(buf, kbuf_size, CellularInformation::SVN);
TEST_ASSERT(err == NSAPI_ERROR_UNSUPPORTED || err == NSAPI_ERROR_OK);
cellular.get_device()->close_information();
dev->close_information();
delete [] buf;
}

View File

@ -31,6 +31,10 @@
#error [NOT_SUPPORTED] SIM pin code is needed. Skipping this build.
#endif
#if defined(TARGET_ADV_WISE_1570) || defined(TARGET_MTB_ADV_WISE_1570)
#error [NOT_SUPPORTED] target MTB_ADV_WISE_1570 is too unstable for network tests, IoT network is unstable
#endif
#include "greentea-client/test_env.h"
#include "unity.h"
#include "utest.h"
@ -38,65 +42,30 @@
#include "mbed.h"
#include "AT_CellularNetwork.h"
#include "CellularConnectionFSM.h"
#include "CellularContext.h"
#include "CellularDevice.h"
#include "../../cellular_tests_common.h"
#include CELLULAR_STRINGIFY(CELLULAR_DEVICE.h)
#define NETWORK_TIMEOUT (180*1000)
static UARTSerial cellular_serial(MDMTXD, MDMRXD, MBED_CONF_PLATFORM_DEFAULT_SERIAL_BAUD_RATE);
static EventQueue queue(8 * EVENTS_EVENT_SIZE);
static rtos::Semaphore network_semaphore(0);
static CellularConnectionFSM cellular;
static CellularConnectionFSM::CellularState cellular_target_state;
static CELLULAR_DEVICE *device;
static CellularContext *ctx;
static CellularDevice *device;
static CellularNetwork *nw;
static bool fsm_callback(int state, int next_state)
{
if (next_state == cellular_target_state) {
TEST_ASSERT(network_semaphore.release() == osOK);
return false;
}
return true;
}
// test methods that are already run in state machine (CellularConnectionFSM) but as it might change
// we wan't to run these 'manually' also
static void test_network_interface_fsm()
{
#if defined (MDMRTS) && defined (MDMCTS)
cellular_serial.set_flow_control(SerialBase::RTSCTS, MDMRTS, MDMCTS);
#endif
device = new CELLULAR_DEVICE(queue);
TEST_ASSERT(device != NULL);
CellularNetwork *nw = device->open_network(&cellular_serial);
TEST_ASSERT(nw != NULL);
TEST_ASSERT(nw->init() == NSAPI_ERROR_OK);
delete device;
device = NULL;
}
static void init_network_interface()
{
cellular.set_serial(&cellular_serial);
TEST_ASSERT(cellular.init() == NSAPI_ERROR_OK);
cellular.set_callback(&fsm_callback);
TEST_ASSERT(cellular.start_dispatch() == NSAPI_ERROR_OK);
cellular.set_sim_pin(MBED_CONF_APP_CELLULAR_SIM_PIN);
ctx = CellularContext::get_default_instance();
TEST_ASSERT(ctx != NULL);
ctx->set_sim_pin(MBED_CONF_APP_CELLULAR_SIM_PIN);
#ifdef MBED_CONF_APP_APN
CellularNetwork *network = cellular.get_network();
TEST_ASSERT(network->set_credentials(MBED_CONF_APP_APN) == NSAPI_ERROR_OK);
ctx->set_credentials(MBED_CONF_APP_APN);
#endif
cellular_target_state = CellularConnectionFSM::STATE_REGISTERING_NETWORK;
TEST_ASSERT(cellular.continue_to_state(cellular_target_state) == NSAPI_ERROR_OK);
TEST_ASSERT(network_semaphore.wait(NETWORK_TIMEOUT) == 1);
}
TEST_ASSERT(ctx->register_to_network() == NSAPI_ERROR_OK);
device = CellularDevice::get_default_instance();
TEST_ASSERT(device != NULL);
}
static bool get_network_registration(CellularNetwork::RegistrationType type,
CellularNetwork::RegistrationStatus &status, bool &is_registered)
@ -149,8 +118,8 @@ static void nw_callback(nsapi_event_t ev, intptr_t intptr)
static void test_network_registration()
{
cellular.get_device()->set_timeout(10 * 1000);
nw = cellular.get_network();
device->set_timeout(10 * 1000);
nw = device->open_network();
TEST_ASSERT(nw != NULL);
nw->attach(&nw_callback);
@ -194,97 +163,22 @@ static void test_attach()
TEST_ASSERT(status == CellularNetwork::Attached);
}
static void test_activate_context()
{
TEST_ASSERT(nw->activate_context() == NSAPI_ERROR_OK);
}
static void test_connect()
{
TEST_ASSERT(nw->connect() == NSAPI_ERROR_OK);
char count = 0;
while ((nw->get_connection_status() != NSAPI_STATUS_GLOBAL_UP) && (count++ < 60)) {
wait(1);
}
nsapi_connection_status_t st = nw->get_connection_status();
TEST_ASSERT(st == NSAPI_STATUS_GLOBAL_UP);
}
static void test_credentials()
{
nsapi_error_t err = nw->set_credentials(NULL, "username", "pass");
TEST_ASSERT(err == NSAPI_ERROR_OK || err == NSAPI_ERROR_UNSUPPORTED);
err = nw->set_credentials("internet", "user", NULL);
TEST_ASSERT(err == NSAPI_ERROR_OK || err == NSAPI_ERROR_UNSUPPORTED);
err = nw->set_credentials("internet", CellularNetwork::NOAUTH, "user", "pass");
TEST_ASSERT(err == NSAPI_ERROR_OK || err == NSAPI_ERROR_UNSUPPORTED);
TEST_ASSERT(nw->set_credentials("internet", NULL, "pass") == NSAPI_ERROR_OK);
}
static void test_other()
{
const char *devi = CELLULAR_STRINGIFY(CELLULAR_DEVICE);
TEST_ASSERT(nw->get_3gpp_error() == 0);
CellularNetwork::RateControlExceptionReports reports;
CellularNetwork::RateControlUplinkTimeUnit timeUnit;
int uplinkRate;
// can't test values as they are optional
nsapi_error_t err = nw->get_rate_control(reports, timeUnit, uplinkRate);
TEST_ASSERT(err == NSAPI_ERROR_OK || err == NSAPI_ERROR_DEVICE_ERROR);
if (strcmp(devi, "QUECTEL_BG96") != 0 && strcmp(devi, "TELIT_HE910") != 0 && strcmp(devi, "SARA4_PPP") != 0) { // QUECTEL_BG96 does not give any specific reason for device error
if (err == NSAPI_ERROR_DEVICE_ERROR) {
TEST_ASSERT(((AT_CellularNetwork *)nw)->get_device_error().errCode == 100 && // 100 == unknown command for modem
((AT_CellularNetwork *)nw)->get_device_error().errType == 3); // 3 == CME error from the modem
}
}
uplinkRate = -1;
err = nw->get_apn_backoff_timer(uplinkRate);
TEST_ASSERT(err == NSAPI_ERROR_OK || err == NSAPI_ERROR_DEVICE_ERROR || err == NSAPI_ERROR_PARAMETER);
if (err == NSAPI_ERROR_DEVICE_ERROR) {
if (strcmp(devi, "QUECTEL_BG96") != 0 && strcmp(devi, "TELIT_HE910") != 0 && strcmp(devi, "SARA4_PPP") != 0) { // QUECTEL_BG96 does not give any specific reason for device error
TEST_ASSERT(((AT_CellularNetwork *)nw)->get_device_error().errCode == 100 && // 100 == unknown command for modem
((AT_CellularNetwork *)nw)->get_device_error().errType == 3); // 3 == CME error from the modem
}
} else if (err == NSAPI_ERROR_PARAMETER) {
TEST_ASSERT(uplinkRate == -1);
} else {
TEST_ASSERT(uplinkRate >= 0);
}
err = nw->set_access_technology(CellularNetwork::RAT_GSM);
nsapi_error_t err = nw->set_access_technology(CellularNetwork::RAT_GSM);
TEST_ASSERT(err == NSAPI_ERROR_OK || err == NSAPI_ERROR_UNSUPPORTED);
// scanning of operators requires some delay before operation is allowed(seen with WISE_1570)
wait(5);
// scanning of operators might take a long time
cellular.get_device()->set_timeout(240 * 1000);
device->set_timeout(240 * 1000);
CellularNetwork::operList_t operators;
int uplinkRate = -1;
TEST_ASSERT(nw->scan_plmn(operators, uplinkRate) == NSAPI_ERROR_OK);
cellular.get_device()->set_timeout(10 * 1000);
// all current targets support IPV4
nsapi_ip_stack_t stack_type = IPV4_STACK;
TEST_ASSERT(nw->set_stack_type(stack_type) == NSAPI_ERROR_OK);
TEST_ASSERT(nw->get_stack_type() == stack_type);
CellularNetwork::pdpContextList_t params_list;
err = nw->get_pdpcontext_params(params_list);
TEST_ASSERT(err == NSAPI_ERROR_OK || err == NSAPI_ERROR_DEVICE_ERROR);
if (err == NSAPI_ERROR_DEVICE_ERROR) {
if (strcmp(devi, "TELIT_HE910") != 0) { // TELIT_HE910 just gives an error and no specific error number so we can't know is this real error or that modem/network does not support the command
TEST_ASSERT((((AT_CellularNetwork *)nw)->get_device_error().errType == 3) && // 3 == CME error from the modem
((((AT_CellularNetwork *)nw)->get_device_error().errCode == 100) || // 100 == unknown command for modem
(((AT_CellularNetwork *)nw)->get_device_error().errCode == 50))); // 50 == incorrect parameters // seen in wise_1570 for not supported commands
}
} else {
// should have some values, only not optional are apn and bearer id
CellularNetwork::pdpcontext_params_t *params = params_list.get_head();
TEST_ASSERT(params->bearer_id >= 0)
}
device->set_timeout(10 * 1000);
int rxlev = -1, ber = -1, rscp = -1, ecno = -1, rsrq = -1, rsrp = -1;
err = nw->get_extended_signal_quality(rxlev, ber, rscp, ecno, rsrq, rsrp);
@ -328,8 +222,6 @@ static void test_other()
nsapi_connection_status_t st = nw->get_connection_status();
TEST_ASSERT(st == NSAPI_STATUS_DISCONNECTED);
TEST_ASSERT(nw->set_blocking(true) == NSAPI_ERROR_OK);
#ifndef TARGET_UBLOX_C027 // AT command is supported, but excluded as it runs out of memory easily (there can be very many operator names)
if (strcmp(devi, "QUECTEL_BG96") != 0 && strcmp(devi, "SARA4_PPP") != 0) {
// QUECTEL_BG96 timeouts with this one, tested with 3 minute timeout
@ -378,15 +270,6 @@ static void test_other()
}
}
static void test_disconnect()
{
nsapi_connection_status_t st = nw->get_connection_status();
TEST_ASSERT(st == NSAPI_STATUS_GLOBAL_UP);
TEST_ASSERT(nw->disconnect() == NSAPI_ERROR_OK);
// wait to process URC's, received after disconnect
rtos::ThisThread::sleep_for(500);
}
static void test_detach()
{
// in PPP mode there is NO CARRIER waiting so flush it out
@ -412,17 +295,11 @@ static utest::v1::status_t greentea_failure_handler(const Case *const source, co
}
static Case cases[] = {
Case("CellularNetwork state machine methods", test_network_interface_fsm, greentea_failure_handler),
Case("CellularNetwork init", init_network_interface, greentea_failure_handler),
Case("CellularNetwork test registering", test_network_registration, greentea_failure_handler),
Case("CellularNetwork test attach", test_attach, greentea_failure_handler),
Case("CellularNetwork test activate pdp context", test_activate_context, greentea_failure_handler),
Case("CellularNetwork test other functions", test_other, greentea_failure_handler),
Case("CellularNetwork test connect", test_connect, greentea_failure_handler),
Case("CellularNetwork test credentials", test_credentials, greentea_failure_handler),
Case("CellularNetwork test disconnect", test_disconnect, greentea_failure_handler),
Case("CellularNetwork test detach", test_detach, greentea_failure_handler)
};
static utest::v1::status_t test_setup(const size_t number_of_cases)

View File

@ -44,9 +44,7 @@
#define NETWORK_TIMEOUT (180*1000)
static UARTSerial cellular_serial(MDMTXD, MDMRXD, MBED_CONF_PLATFORM_DEFAULT_SERIAL_BAUD_RATE);
static CELLULAR_DEVICE *cellular_device;
static EventQueue queue(2 * EVENTS_EVENT_SIZE);
static CellularDevice *cellular_device;
static void urc_callback()
{
@ -74,9 +72,9 @@ static void wait_for_power(CellularPower *pwr)
static void test_power_interface()
{
const char *devi = CELLULAR_STRINGIFY(CELLULAR_DEVICE);
cellular_device = new CELLULAR_DEVICE(queue);
cellular_device->set_timeout(5000);
CellularPower *pwr = cellular_device->open_power(&cellular_serial);
cellular_device = CellularDevice::get_default_instance();
cellular_device->set_timeout(9000);
CellularPower *pwr = cellular_device->open_power();
TEST_ASSERT(pwr != NULL);
nsapi_error_t err = pwr->on();
@ -89,6 +87,7 @@ static void test_power_interface()
TEST_ASSERT(err == NSAPI_ERROR_OK);
wait_for_power(pwr);
wait(1);
err = pwr->opt_power_save_mode(0, 0);
TEST_ASSERT(err == NSAPI_ERROR_OK || err == NSAPI_ERROR_DEVICE_ERROR);
if (err == NSAPI_ERROR_DEVICE_ERROR) {
@ -98,6 +97,7 @@ static void test_power_interface()
}
}
wait(1);
err = pwr->opt_receive_period(0, CellularPower::EDRXEUTRAN_NB_S1_mode, 3);
TEST_ASSERT(err == NSAPI_ERROR_OK || err == NSAPI_ERROR_DEVICE_ERROR);
if (err == NSAPI_ERROR_DEVICE_ERROR) {
@ -109,6 +109,7 @@ static void test_power_interface()
err = pwr->off();
TEST_ASSERT(err == NSAPI_ERROR_OK || err == NSAPI_ERROR_UNSUPPORTED);
}
using namespace utest::v1;

View File

@ -37,86 +37,40 @@
#include "mbed.h"
#include "CellularConnectionFSM.h"
#include "CellularContext.h"
#include "CellularDevice.h"
#include "CellularSIM.h"
#include "../../cellular_tests_common.h"
#include CELLULAR_STRINGIFY(CELLULAR_DEVICE.h)
#define NETWORK_TIMEOUT (180*1000)
static UARTSerial cellular_serial(MDMTXD, MDMRXD, MBED_CONF_PLATFORM_DEFAULT_SERIAL_BAUD_RATE);
static EventQueue queue(8 * EVENTS_EVENT_SIZE);
static rtos::Semaphore network_semaphore(0);
static CellularConnectionFSM cellular;
static CellularConnectionFSM::CellularState cellular_target_state;
static CellularContext *ctx;
static CellularDevice *device;
static char *get_rand_string(char *str, size_t size)
static void init_to_device_ready_state()
{
const char charset[] = "0123456789";
if (size) {
--size;
for (size_t n = 0; n < size; n++) {
int key = rand() % (int)(sizeof charset - 1);
str[n] = charset[key];
}
str[size] = '\0';
}
return str;
}
ctx = CellularContext::get_default_instance();
TEST_ASSERT(ctx != NULL);
TEST_ASSERT(ctx->set_device_ready() == NSAPI_ERROR_OK);
static bool fsm_callback(int state, int next_state)
{
if (next_state == CellularConnectionFSM::STATE_SIM_PIN) {
TEST_ASSERT(network_semaphore.release() == osOK);
return false;
}
return true;
}
static void init_to_sim_state()
{
cellular.set_serial(&cellular_serial);
TEST_ASSERT(cellular.init() == NSAPI_ERROR_OK);
#if defined (MDMRTS) && defined (MDMCTS)
cellular_serial.set_flow_control(SerialBase::RTSCTS, MDMRTS, MDMCTS);
#endif
cellular.set_callback(&fsm_callback);
TEST_ASSERT(cellular.start_dispatch() == NSAPI_ERROR_OK);
cellular_target_state = CellularConnectionFSM::STATE_SIM_PIN;
TEST_ASSERT(cellular.continue_to_state(cellular_target_state) == NSAPI_ERROR_OK);
TEST_ASSERT(network_semaphore.wait(NETWORK_TIMEOUT) == 1);
device = CellularDevice::get_default_instance();
TEST_ASSERT(device != NULL);
}
static void test_sim_interface()
{
CellularSIM *sim = cellular.get_sim();
CellularSIM *sim = device->open_sim();
TEST_ASSERT(sim != NULL);
// set SIM at time out to 3000
cellular.get_device()->set_timeout(3000);
// set SIM at time out to 9000
device->set_timeout(9000);
wait(4); // we need to wait for some time so that SIM interface is working in all modules.
// 1. test set_pin
nsapi_error_t err = sim->set_pin(MBED_CONF_APP_CELLULAR_SIM_PIN);
MBED_ASSERT(err == NSAPI_ERROR_OK);
// 2. test change pin
char pin[5];
int sanity_count = 0;
while (strcmp(get_rand_string(pin, 5), MBED_CONF_APP_CELLULAR_SIM_PIN) == 0) {
sanity_count++;
TEST_ASSERT(sanity_count < 50);
};
// change pin and change it back
wait(1);
err = sim->change_pin(MBED_CONF_APP_CELLULAR_SIM_PIN, pin);
TEST_ASSERT(err == NSAPI_ERROR_OK || err == NSAPI_ERROR_UNSUPPORTED);
wait(1);
err = sim->change_pin(pin, MBED_CONF_APP_CELLULAR_SIM_PIN);
TEST_ASSERT(err == NSAPI_ERROR_OK || err == NSAPI_ERROR_UNSUPPORTED);
// 3. test set_pin_query
// 2. test set_pin_query
wait(1);
err = sim->set_pin_query(MBED_CONF_APP_CELLULAR_SIM_PIN, false);
TEST_ASSERT(err == NSAPI_ERROR_OK || err == NSAPI_ERROR_UNSUPPORTED);
@ -126,14 +80,14 @@ static void test_sim_interface()
TEST_ASSERT(err == NSAPI_ERROR_OK || err == NSAPI_ERROR_UNSUPPORTED);
wait(1);
// 4. test get_sim_state
// 3. test get_sim_state
CellularSIM::SimState state;
err = sim->get_sim_state(state);
TEST_ASSERT(err == NSAPI_ERROR_OK);
TEST_ASSERT(state == CellularSIM::SimStateReady);
wait(1);
// 5. test get_imsi
// 4. test get_imsi
char imsi[16] = {0};
err = sim->get_imsi(imsi);
TEST_ASSERT(err == NSAPI_ERROR_OK);
@ -149,7 +103,7 @@ static utest::v1::status_t greentea_failure_handler(const Case *const source, co
}
static Case cases[] = {
Case("CellularSIM init", init_to_sim_state, greentea_failure_handler),
Case("CellularSIM init", init_to_device_ready_state, greentea_failure_handler),
Case("CellularSIM test interface", test_sim_interface, greentea_failure_handler)
};

View File

@ -31,7 +31,9 @@
#error [NOT_SUPPORTED] SIM pin code is needed. Skipping this build.
#endif
#if defined(TARGET_ADV_WISE_1570) || defined(TARGET_MTB_ADV_WISE_1570)
#error [NOT_SUPPORTED] target MTB_ADV_WISE_1570 does not have SMS functionality
#endif
#include "greentea-client/test_env.h"
#include "unity.h"
@ -40,59 +42,43 @@
#include "mbed.h"
#include "AT_CellularSMS.h"
#include "CellularConnectionFSM.h"
#include "CellularContext.h"
#include "CellularDevice.h"
#include "../../cellular_tests_common.h"
#include CELLULAR_STRINGIFY(CELLULAR_DEVICE.h)
#define NETWORK_TIMEOUT (600*1000)
#define SIM_BUSY 314
static UARTSerial cellular_serial(MDMTXD, MDMRXD, MBED_CONF_PLATFORM_DEFAULT_SERIAL_BAUD_RATE);
static EventQueue queue(8 * EVENTS_EVENT_SIZE);
static rtos::Semaphore network_semaphore(0);
static CellularConnectionFSM *cellularConnectionFSM;
static CellularConnectionFSM::CellularState cellular_target_state;
static CellularContext *ctx;
static CellularDevice *device;
static CellularSMS *sms;
static char service_center_address[SMS_MAX_PHONE_NUMBER_SIZE];
static int service_address_type;
static bool cellular_status(int state, int next_state)
static void create_context()
{
if (cellular_target_state == state) {
(void)network_semaphore.release();
return false; // return false -> state machine is halted
}
return true;
}
static void createFSM()
{
#if defined (MDMRTS) && defined (MDMCTS)
cellular_serial.set_flow_control(SerialBase::RTSCTS, MDMRTS, MDMCTS);
#endif
cellularConnectionFSM = new CellularConnectionFSM();
cellularConnectionFSM->set_serial(&cellular_serial);
cellularConnectionFSM->set_callback(&cellular_status);
TEST_ASSERT(cellularConnectionFSM->init() == NSAPI_ERROR_OK);
TEST_ASSERT(cellularConnectionFSM->start_dispatch() == NSAPI_ERROR_OK);
cellularConnectionFSM->set_sim_pin(MBED_CONF_APP_CELLULAR_SIM_PIN);
CellularDevice *device = cellularConnectionFSM->get_device();
device = new CELLULAR_DEVICE(&cellular_serial);
TEST_ASSERT(device != NULL);
device->set_timeout(30000);
device->set_timeout(9000);
ctx = device->create_context();
TEST_ASSERT(ctx != NULL);
ctx->set_sim_pin(MBED_CONF_APP_CELLULAR_SIM_PIN);
#ifdef MBED_CONF_APP_APN
ctx->set_credentials(MBED_CONF_APP_APN);
#endif
}
static void store_service_center_address()
{
// Frist we need to go SIM_PIN state to make sure that we can get service address and device ready to accept AT commands
createFSM();
cellular_target_state = CellularConnectionFSM::STATE_SIM_PIN;
TEST_ASSERT(cellularConnectionFSM->continue_to_state(cellular_target_state) == NSAPI_ERROR_OK);
TEST_ASSERT(network_semaphore.wait(NETWORK_TIMEOUT) == 1); // cellular network searching may take several minutes
// First we need to go SIM_PIN state to make sure that we can get service address and device ready to accept AT commands
create_context();
TEST_ASSERT(ctx->set_sim_ready() == NSAPI_ERROR_OK);
wait(1);
delete device; // will also delete context
delete cellularConnectionFSM;
cellularConnectionFSM = NULL;
wait(3); // some modems needs more time access sim
ATHandler *at_init = new ATHandler(&cellular_serial, queue, 30000, "\r");
at_init->cmd_start("AT+CSCA?");
@ -114,50 +100,60 @@ static void init()
{
// First store current service address
store_service_center_address();
create_context();
createFSM();
CellularNetwork *network = cellularConnectionFSM->get_network();
TEST_ASSERT(network != NULL);
TEST_ASSERT(network->set_credentials(MBED_CONF_APP_APN, NULL, NULL) == NSAPI_ERROR_OK);
cellular_target_state = CellularConnectionFSM::STATE_REGISTERING_NETWORK;
TEST_ASSERT(cellularConnectionFSM->continue_to_state(cellular_target_state) == NSAPI_ERROR_OK);
TEST_ASSERT(network_semaphore.wait(NETWORK_TIMEOUT) == 1); // cellular network searching may take several minutes
sms = cellularConnectionFSM->get_device()->open_sms(&cellular_serial);
TEST_ASSERT(ctx->register_to_network() == NSAPI_ERROR_OK);
sms = device->open_sms(&cellular_serial);
TEST_ASSERT(sms != NULL);
wait(3);
wait(3); // some modems needs more time access sim
}
static void test_sms_initialize_text_mode()
{
TEST_ASSERT(sms->initialize(CellularSMS::CellularSMSMmodeText) == NSAPI_ERROR_OK);
nsapi_error_t err = sms->initialize(CellularSMS::CellularSMSMmodeText);
TEST_ASSERT(err == NSAPI_ERROR_OK || (err == NSAPI_ERROR_DEVICE_ERROR &&
((AT_CellularSMS *)sms)->get_device_error().errCode == SIM_BUSY));
}
static void test_sms_initialize_pdu_mode()
{
TEST_ASSERT(sms->initialize(CellularSMS::CellularSMSMmodePDU) == NSAPI_ERROR_OK);
nsapi_error_t err = sms->initialize(CellularSMS::CellularSMSMmodePDU);
TEST_ASSERT(err == NSAPI_ERROR_OK || (err == NSAPI_ERROR_DEVICE_ERROR &&
((AT_CellularSMS *)sms)->get_device_error().errCode == SIM_BUSY));
}
static void test_set_cscs()
{
TEST_ASSERT(sms->set_cscs("IRA") == NSAPI_ERROR_OK);
TEST_ASSERT(sms->set_cscs("UCS2") == NSAPI_ERROR_OK);
TEST_ASSERT(sms->set_cscs("GSM") == NSAPI_ERROR_OK);
nsapi_error_t err = sms->set_cscs("IRA");
TEST_ASSERT(err == NSAPI_ERROR_OK || (err == NSAPI_ERROR_DEVICE_ERROR &&
((AT_CellularSMS *)sms)->get_device_error().errCode == SIM_BUSY));
err = sms->set_cscs("UCS2");
TEST_ASSERT(err == NSAPI_ERROR_OK || (err == NSAPI_ERROR_DEVICE_ERROR &&
((AT_CellularSMS *)sms)->get_device_error().errCode == SIM_BUSY));
err = sms->set_cscs("GSM");
TEST_ASSERT(err == NSAPI_ERROR_OK || (err == NSAPI_ERROR_DEVICE_ERROR &&
((AT_CellularSMS *)sms)->get_device_error().errCode == SIM_BUSY));
}
static void test_set_csca()
{
TEST_ASSERT(sms->set_csca("55555", 129) == NSAPI_ERROR_OK);
TEST_ASSERT(sms->set_csca("+35855555", 145) == NSAPI_ERROR_OK);
TEST_ASSERT(sms->set_csca(service_center_address, service_address_type) == NSAPI_ERROR_OK);
nsapi_error_t err = sms->set_csca("55555", 129);
TEST_ASSERT(err == NSAPI_ERROR_OK || (err == NSAPI_ERROR_DEVICE_ERROR &&
((AT_CellularSMS *)sms)->get_device_error().errCode == SIM_BUSY));
err = sms->set_csca("+35855555", 145);
TEST_ASSERT(err == NSAPI_ERROR_OK || (err == NSAPI_ERROR_DEVICE_ERROR &&
((AT_CellularSMS *)sms)->get_device_error().errCode == SIM_BUSY));
err = sms->set_csca(service_center_address, service_address_type);
TEST_ASSERT(err == NSAPI_ERROR_OK || (err == NSAPI_ERROR_DEVICE_ERROR &&
((AT_CellularSMS *)sms)->get_device_error().errCode == SIM_BUSY));
}
static void test_set_cpms_me()
{
TEST_ASSERT(sms->set_cpms("ME", "ME", "ME") == NSAPI_ERROR_OK);
nsapi_error_t err = sms->set_cpms("ME", "ME", "ME");
TEST_ASSERT(err == NSAPI_ERROR_OK || (err == NSAPI_ERROR_DEVICE_ERROR &&
((AT_CellularSMS *)sms)->get_device_error().errCode == SIM_BUSY));
}
#ifdef MBED_CONF_APP_CELLULAR_PHONE_NUMBER
@ -224,7 +220,6 @@ static void test_set_extra_sim_wait_time_1000()
#endif
using namespace utest::v1;
static utest::v1::status_t greentea_failure_handler(const Case *const source, const failure_t reason)
@ -233,7 +228,6 @@ static utest::v1::status_t greentea_failure_handler(const Case *const source, co
return STATUS_ABORT;
}
static Case cases[] = {
Case("CellularSMS init", init, greentea_failure_handler),
Case("CellularSMS test ME for storage", test_set_cpms_me, greentea_failure_handler),
@ -256,9 +250,6 @@ static Case cases[] = {
#endif
};
static utest::v1::status_t test_setup(const size_t number_of_cases)
{
GREENTEA_SETUP(600, "default_auto");

View File

@ -30,13 +30,17 @@
#error [NOT_SUPPORTED] SIM pin code is needed. Skipping this build.
#endif
#if defined(TARGET_ADV_WISE_1570) || defined(TARGET_MTB_ADV_WISE_1570)
#error [NOT_SUPPORTED] target MTB_ADV_WISE_1570 is too unstable for network tests, IoT network is unstable
#endif
#include "greentea-client/test_env.h"
#include "unity.h"
#include "utest.h"
#include "mbed.h"
#include "CellularConnectionFSM.h"
#include "CellularContext.h"
#if MBED_CONF_CELLULAR_USE_APN_LOOKUP || MBED_CONF_PPP_CELL_IFACE_APN_LOOKUP
#include "APN_db.h"
@ -48,13 +52,9 @@
#define SOCKET_TIMEOUT (30*1000)
#define ECHO_SERVER_NAME "echo.mbedcloudtesting.com"
#define ECHO_SERVER_UDP_PORT 7
static CellularConnectionFSM::CellularState cellular_target_state;
static UARTSerial cellular_serial(MDMTXD, MDMRXD, MBED_CONF_PLATFORM_DEFAULT_SERIAL_BAUD_RATE);
static rtos::Semaphore network_semaphore(0);
static CellularConnectionFSM cellular;
#define ECHO_SERVER_UDP_PORT 8877
static CellularContext *ctx;
static SocketAddress echo_server_addr;
static rtos::EventFlags eventFlags;
@ -141,37 +141,20 @@ private:
bool _rx_pending;
};
static void network_callback(nsapi_event_t ev, intptr_t ptr)
{
if (ev == NSAPI_EVENT_CONNECTION_STATUS_CHANGE) {
if (ptr == NSAPI_STATUS_GLOBAL_UP) {
TEST_ASSERT(network_semaphore.release() == osOK);
}
}
}
static void udp_network_stack()
{
cellular.set_serial(&cellular_serial);
TEST_ASSERT(cellular.init() == NSAPI_ERROR_OK);
#if defined (MDMRTS) && defined (MDMCTS)
cellular_serial.set_flow_control(SerialBase::RTSCTS, MDMRTS, MDMCTS);
#endif
cellular.attach(&network_callback);
TEST_ASSERT(cellular.start_dispatch() == NSAPI_ERROR_OK);
cellular.set_sim_pin(MBED_CONF_APP_CELLULAR_SIM_PIN);
ctx = CellularContext::get_default_instance();
TEST_ASSERT(ctx != NULL);
ctx->set_sim_pin(MBED_CONF_APP_CELLULAR_SIM_PIN);
#ifdef MBED_CONF_APP_APN
CellularNetwork *network = cellular.get_network();
TEST_ASSERT(network->set_credentials(MBED_CONF_APP_APN, MBED_CONF_APP_USERNAME, MBED_CONF_APP_PASSWORD) == NSAPI_ERROR_OK);
ctx->set_credentials(MBED_CONF_APP_APN, MBED_CONF_APP_USERNAME, MBED_CONF_APP_PASSWORD);
#endif
cellular_target_state = CellularConnectionFSM::STATE_CONNECTED;
TEST_ASSERT(cellular.continue_to_state(cellular_target_state) == NSAPI_ERROR_OK);
TEST_ASSERT(network_semaphore.wait(NETWORK_TIMEOUT) == 1);
TEST_ASSERT(ctx->connect() == NSAPI_ERROR_OK);
}
static void udp_gethostbyname()
{
TEST_ASSERT(cellular.get_network()->gethostbyname(ECHO_SERVER_NAME, &echo_server_addr) == 0);
TEST_ASSERT(ctx->gethostbyname(ECHO_SERVER_NAME, &echo_server_addr) == 0);
tr_info("Echo server IP: %s", echo_server_addr.get_ip_address());
echo_server_addr.set_port(7);
}
@ -179,7 +162,7 @@ static void udp_gethostbyname()
static void udp_socket_send_receive()
{
EchoSocket echo_socket(4);
TEST_ASSERT(echo_socket.open(cellular.get_network()) == NSAPI_ERROR_OK);
TEST_ASSERT(echo_socket.open(ctx) == NSAPI_ERROR_OK);
echo_socket.set_blocking(true);
echo_socket.set_timeout(SOCKET_TIMEOUT);
echo_socket.test_sendto();
@ -193,7 +176,7 @@ static void udp_socket_send_receive_async()
TEST_ASSERT(!(eventFlags.clear(async_flag) & osFlagsError));
EchoSocket echo_socket(4);
TEST_ASSERT(echo_socket.open(cellular.get_network()) == NSAPI_ERROR_OK);
TEST_ASSERT(echo_socket.open(ctx) == NSAPI_ERROR_OK);
echo_socket.set_async(async_flag);
echo_socket.test_sendto();
echo_socket.test_recvfrom();

View File

@ -24,6 +24,7 @@
},
"target_overrides": {
"*": {
"nsapi.dns-response-wait-time": 30000,
"ppp-cell-iface.apn-lookup": false,
"cellular.use-apn-lookup": false,
"target.features_add": ["LWIP"],

View File

@ -253,11 +253,11 @@ protected:
int _info_ref_count;
FileHandle *_fh;
events::EventQueue _queue;
CellularStateMachine *_state_machine;
private:
nsapi_error_t start_state_machine(CellularStateMachine::CellularState target_state);
nsapi_error_t create_state_machine();
CellularStateMachine *_state_machine;
CellularNetwork *_nw;
char _sim_pin[MAX_PIN_SIZE + 1];
char _plmn[MAX_PLMN_SIZE +1];

View File

@ -41,6 +41,8 @@ AT_CellularDevice::AT_CellularDevice(FileHandle *fh) : CellularDevice(fh), _atHa
AT_CellularDevice::~AT_CellularDevice()
{
delete _state_machine;
// make sure that all is deleted even if somewhere close was not called and reference counting is messed up.
_network_ref_count = 1;
_sms_ref_count = 1;

View File

@ -59,7 +59,6 @@ CellularDevice::CellularDevice(FileHandle *fh) : _network_ref_count(0), _sms_ref
CellularDevice::~CellularDevice()
{
delete _state_machine;
}
void CellularDevice::stop()

View File

@ -74,7 +74,6 @@ CellularStateMachine::CellularStateMachine(CellularDevice &device, events::Event
CellularStateMachine::~CellularStateMachine()
{
stop();
_queue.break_dispatch();
}
void CellularStateMachine::reset()
@ -90,9 +89,8 @@ void CellularStateMachine::reset()
void CellularStateMachine::stop()
{
_queue.cancel(_event_id);
if (_queue_thread) {
_queue.break_dispatch();
_queue_thread->terminate();
delete _queue_thread;
_queue_thread = NULL;

View File

@ -42,6 +42,7 @@ class CellularStateMachine {
private:
// friend of CellularDevice so that it's the only way to close/delete this class.
friend class CellularDevice;
friend class AT_CellularDevice;
/** Constructor
*
* @param device reference to CellularDevice