Import Mbed OS hard-float snapshot

This commit is contained in:
Beslan
2026-06-01 20:15:04 +03:00
commit d3738e2f89
16278 changed files with 10628036 additions and 0 deletions

View File

@@ -0,0 +1,48 @@
####################
# UNIT TESTS
####################
# Add test specific include paths
set(unittest-includes ${unittest-includes}
../platform
../connectivity/cellular/tests/UNITTESTS/framework/common/util
../connectivity/cellular/include/cellular/framework/common
../connectivity/cellular/include/cellular/framework/AT
../features/frameworks/mbed-client-randlib/mbed-client-randlib
)
# Source files
set(unittest-sources
../connectivity/cellular/source/framework/device/ATHandler.cpp
)
# Test files
set(unittest-test-sources
${CMAKE_CURRENT_LIST_DIR}/athandlertest.cpp
stubs/EventQueue_stub.cpp
stubs/FileHandle_stub.cpp
stubs/us_ticker_stub.cpp
stubs/BufferedSerial_stub.cpp
stubs/SerialBase_stub.cpp
stubs/mbed_assert_stub.cpp
stubs/mbed_poll_stub.cpp
stubs/equeue_stub.c
stubs/Kernel_stub.cpp
stubs/ThisThread_stub.cpp
stubs/randLIB_stub.cpp
stubs/CellularUtil_stub.cpp
stubs/ConditionVariable_stub.cpp
stubs/Mutex_stub.cpp
stubs/mbed_rtos_rtx_stub.c
stubs/rtx_mutex_stub.c
)
set(unittest-test-flags
-DMBED_CONF_CELLULAR_DEBUG_AT=true
-DOS_STACK_SIZE=2048
-DDEVICE_SERIAL=1
-DDEVICE_INTERRUPTIN=1
-DMBED_CONF_PLATFORM_DEFAULT_SERIAL_BAUD_RATE=115200
)

View File

@@ -0,0 +1,377 @@
/*
* Copyright (c) 2018, Arm Limited and affiliates.
* SPDX-License-Identifier: Apache-2.0
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "gtest/gtest.h"
#include <string.h>
#include "CellularContext.h"
#include "CellularDevice_stub.h"
#include "ControlPlane_netif_stub.h"
#include "myCellularDevice.h"
using namespace mbed;
// AStyle ignored as the definition is not clear due to preprocessor usage
// *INDENT-OFF*
class TestCellularContext : public testing::Test {
protected:
void SetUp()
{
CellularDevice_stub::create_in_get_default = true;
}
void TearDown()
{
}
};
class testContext : public CellularContext
{
public:
testContext(CellularDevice *dev = NULL)
{
_device = dev;
_cp_netif = new ControlPlane_netif_stub();
nonip_pdp_string = NULL;
}
~testContext()
{
delete _cp_netif;
}
int get_retry_count()
{
return _retry_count;
}
CellularContext::AuthenticationType get_auth_type()
{
return _authentication_type;
}
nsapi_error_t set_blocking(bool blocking)
{
_is_blocking = blocking;
return NSAPI_ERROR_OK;
}
NetworkStack *get_stack()
{
return NULL;
}
nsapi_error_t get_ip_address(SocketAddress *address)
{
return NSAPI_ERROR_UNSUPPORTED;
}
const char *get_ip_address()
{
return NULL;
}
virtual void attach(mbed::Callback<void(nsapi_event_t, intptr_t)> status_cb)
{
_status_cb = status_cb;
}
virtual nsapi_error_t connect()
{
return NSAPI_ERROR_OK;
}
virtual nsapi_error_t disconnect()
{
return NSAPI_ERROR_OK;
}
virtual void set_plmn(const char *plmn)
{
}
virtual void set_sim_pin(const char *sim_pin)
{
}
virtual nsapi_error_t connect(const char *sim_pin, const char *apn = 0, const char *uname = 0,
const char *pwd = 0)
{
return NSAPI_ERROR_OK;
}
virtual void set_credentials(const char *apn, const char *uname = 0, const char *pwd = 0)
{
}
virtual bool is_connected()
{
return false;
}
nsapi_error_t set_device_ready()
{
return NSAPI_ERROR_OK;
}
nsapi_error_t set_sim_ready()
{
return NSAPI_ERROR_OK;
}
nsapi_error_t register_to_network()
{
return NSAPI_ERROR_OK;
}
nsapi_error_t attach_to_network()
{
return NSAPI_ERROR_OK;
}
nsapi_error_t get_rate_control(CellularContext::RateControlExceptionReports &reports,
CellularContext::RateControlUplinkTimeUnit &time_unit, int &uplink_rate)
{
return NSAPI_ERROR_OK;
}
nsapi_error_t get_pdpcontext_params(pdpContextList_t &params_list)
{
return NSAPI_ERROR_OK;
}
nsapi_error_t get_apn_backoff_timer(int &backoff_timer)
{
return NSAPI_ERROR_OK;
}
void set_file_handle(FileHandle *fh)
{
}
#if (DEVICE_SERIAL && DEVICE_INTERRUPTIN) || defined(DOXYGEN_ONLY)
virtual nsapi_error_t configure_hup(PinName dcd_pin = NC, bool active_high = false)
{
return NSAPI_ERROR_OK;
}
#endif
ControlPlane_netif *get_cp_netif()
{
return _cp_netif;
}
void cellular_callback(nsapi_event_t ev, intptr_t ptr)
{
}
void enable_hup(bool enable)
{
}
const char *get_nonip_context_type_str()
{
return nonip_pdp_string;
}
void cp_data_received()
{
CellularContext::cp_data_received();
}
virtual void do_connect()
{
}
void set_cell_callback_data(cell_callback_data_t &cb_data)
{
_cb_data = cb_data;
}
void do_connect_with_retry()
{
CellularContext::do_connect_with_retry();
}
void test_string_to_pdp_type()
{
pdp_type_t type = string_to_pdp_type("IPV4V6");
ASSERT_EQ(type, IPV4V6_PDP_TYPE);
type = string_to_pdp_type("IPV6");
ASSERT_EQ(type, IPV6_PDP_TYPE);
type = string_to_pdp_type("IP");
ASSERT_EQ(type, IPV4_PDP_TYPE);
type = string_to_pdp_type("Non-IP");
ASSERT_EQ(type, NON_IP_PDP_TYPE);
nonip_pdp_string = NULL;
type = string_to_pdp_type("diipadaapa");
ASSERT_EQ(type, DEFAULT_PDP_TYPE);
}
void test_nonip_context_type_str()
{
nonip_pdp_string = "NONIP";
pdp_type_t type = string_to_pdp_type("diipadaapa");
ASSERT_EQ(type, DEFAULT_PDP_TYPE);
type = string_to_pdp_type("NONIP");
ASSERT_EQ(type, NON_IP_PDP_TYPE);
type = string_to_pdp_type("nonip");
ASSERT_EQ(type, DEFAULT_PDP_TYPE);
type = string_to_pdp_type("IPV6");
ASSERT_EQ(type, IPV6_PDP_TYPE);
nonip_pdp_string = "testnonip";
type = string_to_pdp_type("diipadaapa");
ASSERT_EQ(type, DEFAULT_PDP_TYPE);
type = string_to_pdp_type("testnonip");
ASSERT_EQ(type, NON_IP_PDP_TYPE);
type = string_to_pdp_type("nonip");
ASSERT_EQ(type, DEFAULT_PDP_TYPE);
type = string_to_pdp_type("IPV6");
ASSERT_EQ(type, IPV6_PDP_TYPE);
}
const char *nonip_pdp_string;
};
static int network_cb_count = 0;
static void network_cb(nsapi_event_t ev, intptr_t intptr)
{
network_cb_count++;
}
// *INDENT-ON*
TEST_F(TestCellularContext, test_create_delete)
{
CellularDevice_stub::create_in_get_default = false;
CellularContext *ctx = CellularContext::get_default_instance();
EXPECT_TRUE(ctx == NULL);
ctx = CellularContext::get_default_nonip_instance();
EXPECT_TRUE(ctx == NULL);
CellularDevice_stub::create_in_get_default = true;
ctx = CellularContext::get_default_instance();
EXPECT_TRUE(ctx != NULL);
ctx = CellularContext::get_default_nonip_instance();
EXPECT_TRUE(ctx != NULL);
}
TEST_F(TestCellularContext, get_device)
{
CellularContext *ctx = CellularContext::get_default_instance();
EXPECT_TRUE(ctx != NULL);
CellularDevice *dev = ctx->get_device();
EXPECT_TRUE(dev != NULL);
}
TEST_F(TestCellularContext, get_cid)
{
CellularContext *ctx = CellularContext::get_default_instance();
EXPECT_TRUE(ctx != NULL);
int cid = ctx->get_cid();
ASSERT_EQ(cid, -1);
}
TEST_F(TestCellularContext, set_authentication_type)
{
testContext *ctx = new testContext();
EXPECT_TRUE(ctx != NULL);
ASSERT_EQ(ctx->get_auth_type(), CellularContext::CHAP);
ctx->set_authentication_type(CellularContext::PAP);
ASSERT_EQ(ctx->get_auth_type(), CellularContext::PAP);
delete ctx;
}
TEST_F(TestCellularContext, cp_data_received)
{
testContext *ctx = new testContext();
EXPECT_TRUE(ctx != NULL);
ControlPlane_netif_stub *netif = (ControlPlane_netif_stub *)ctx->get_cp_netif();
EXPECT_TRUE(!netif->cp_data_received_called);
ctx->cp_data_received();
EXPECT_TRUE(netif->cp_data_received_called);
delete ctx;
}
TEST_F(TestCellularContext, do_connect_with_retry)
{
testContext *ctx = new testContext();
EXPECT_TRUE(ctx != NULL);
ctx->attach(network_cb);
cell_callback_data_t cb_data;
cb_data.final_try = true;
ctx->set_cell_callback_data(cb_data);
ctx->do_connect_with_retry();
ASSERT_EQ(network_cb_count, 0);
cb_data.error = NSAPI_ERROR_OK;
cb_data.final_try = false;
ctx->set_cell_callback_data(cb_data);
ctx->do_connect_with_retry();
CellularDevice_stub::retry_array_length = 2;
cb_data.error = NSAPI_ERROR_DEVICE_ERROR;
cb_data.final_try = false;
ctx->set_cell_callback_data(cb_data);
ctx->do_connect_with_retry();
ASSERT_EQ(ctx->get_retry_count(), 2);
delete ctx;
}
TEST_F(TestCellularContext, do_connect_with_retry_async)
{
myCellularDevice *dev = new myCellularDevice(0);
testContext *ctx = new testContext(dev);
EXPECT_TRUE(ctx != NULL);
ctx->attach(network_cb);
ASSERT_EQ(NSAPI_ERROR_OK, ctx->set_blocking(false));
cell_callback_data_t cb_data;
CellularDevice_stub::retry_array_length = 2;
cb_data.error = NSAPI_ERROR_DEVICE_ERROR;
cb_data.final_try = false;
ctx->set_cell_callback_data(cb_data);
ctx->do_connect_with_retry();
ASSERT_EQ(ctx->get_retry_count(), 1);
delete ctx;
delete dev;
}
TEST_F(TestCellularContext, string_to_pdp_type)
{
testContext *ctx = new testContext();
EXPECT_TRUE(ctx != NULL);
ctx->test_string_to_pdp_type();
delete ctx;
}
TEST_F(TestCellularContext, nonip_context_type_str)
{
testContext *ctx = new testContext();
EXPECT_TRUE(ctx != NULL);
ctx->test_nonip_context_type_str();
delete ctx;
}

View File

@@ -0,0 +1,59 @@
####################
# UNIT TESTS
####################
# Add test specific include paths
set(unittest-includes ${unittest-includes}
../connectivity/cellular/include/cellular/framework/device/cellulardevice
../connectivity/cellular/include/cellular/framework/device
../connectivity/cellular/include/cellular/framework/common
)
# Source files
set(unittest-sources
../connectivity/cellular/source/framework/device/CellularContext.cpp
../connectivity/libraries/nanostack-libservice/source/libip4string/ip4tos.c
../connectivity/libraries/nanostack-libservice/source/libip6string/ip6tos.c
../connectivity/libraries/nanostack-libservice/source/libip4string/stoip4.c
../connectivity/libraries/nanostack-libservice/source/libip6string/stoip6.c
../connectivity/libraries/nanostack-libservice/source/libBits/common_functions.c
../connectivity/netsocket/source/SocketAddress.cpp
)
# Test files
set(unittest-test-sources
${CMAKE_CURRENT_LIST_DIR}/cellularcontexttest.cpp
stubs/FileHandle_stub.cpp
stubs/CellularStateMachine_stub.cpp
stubs/EventQueue_stub.cpp
stubs/mbed_assert_stub.cpp
stubs/BufferedSerial_stub.cpp
stubs/SerialBase_stub.cpp
stubs/ATHandler_stub.cpp
stubs/AT_CellularNetwork_stub.cpp
stubs/AT_CellularContext_stub.cpp
stubs/AT_CellularDevice_stub.cpp
stubs/Semaphore_stub.cpp
stubs/NetworkInterface_stub.cpp
stubs/NetworkInterfaceDefaults_stub.cpp
stubs/CellularDevice_stub.cpp
stubs/equeue_stub.c
stubs/ThisThread_stub.cpp
stubs/ConditionVariable_stub.cpp
stubs/Mutex_stub.cpp
)
set(unittest-test-flags
-DMDMRTS=PTC0
-DMDMCTS=PTC1
-DMDMTXD=NC
-DMDMRXD=NC
-DMBED_CONF_PLATFORM_DEFAULT_SERIAL_BAUD_RATE=115200
-DCELLULAR_DEVICE=myCellularDevice
-DDEVICE_SERIAL_FC=1
-DMBED_CONF_CELLULAR_CONTROL_PLANE_OPT=0
-DDEVICE_SERIAL=1
-DDEVICE_INTERRUPTIN=1
-DMBED_CONF_CELLULAR_USE_SMS=1
)

View File

@@ -0,0 +1,261 @@
/*
* Copyright (c) 2018, Arm Limited and affiliates.
* SPDX-License-Identifier: Apache-2.0
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "gtest/gtest.h"
#include <string.h>
#include "CellularDevice.h"
#include "FileHandle_stub.h"
#include "myCellularDevice.h"
#include "CellularStateMachine_stub.h"
using namespace mbed;
// AStyle ignored as the definition is not clear due to preprocessor usage
// *INDENT-OFF*
class TestCellularDevice : public testing::Test {
protected:
void SetUp()
{
CellularStateMachine_stub::nsapi_error_value = NSAPI_ERROR_OK;
CellularStateMachine_stub::get_current_target_state = STATE_INIT;
CellularStateMachine_stub::get_current_current_state = STATE_INIT;
CellularStateMachine_stub::bool_value = false;
}
void TearDown()
{
}
};
// *INDENT-ON*
TEST_F(TestCellularDevice, test_create_delete)
{
FileHandle_stub fh1;
CellularDevice *dev = new myCellularDevice(&fh1);
EXPECT_TRUE(dev);
delete dev;
dev = NULL;
CellularDevice *dev1 = CellularDevice::get_default_instance();
EXPECT_FALSE(dev1);
}
TEST_F(TestCellularDevice, test_set_sim_pin)
{
FileHandle_stub fh1;
CellularDevice *dev = new myCellularDevice(&fh1);
EXPECT_TRUE(dev);
// MAX_PIN_SIZE = 8; so let's try a longer one
dev->set_sim_pin("123480375462074360276407");
dev->set_sim_pin(NULL);
dev->set_sim_pin("1234");
delete dev;
}
TEST_F(TestCellularDevice, test_set_plmn)
{
FileHandle_stub fh1;
CellularDevice *dev = new myCellularDevice(&fh1);
EXPECT_TRUE(dev);
// MAX_PLMN_SIZE = 16; so let's try a longer one
dev->set_plmn("123480327465023746502734602736073264076340");
dev->set_plmn("1234");
dev->set_plmn(NULL);
delete dev;
}
TEST_F(TestCellularDevice, test_set_device_ready)
{
FileHandle_stub fh1;
CellularDevice *dev = new myCellularDevice(&fh1);
EXPECT_TRUE(dev);
CellularStateMachine_stub::nsapi_error_value = NSAPI_ERROR_NO_MEMORY;
ASSERT_EQ(dev->set_device_ready(), NSAPI_ERROR_NO_MEMORY);
CellularStateMachine_stub::nsapi_error_value = NSAPI_ERROR_OK;
ASSERT_EQ(dev->set_device_ready(), NSAPI_ERROR_OK);
CellularStateMachine_stub::get_current_current_state = STATE_SIM_PIN;
CellularStateMachine_stub::nsapi_error_value = NSAPI_ERROR_OK;
ASSERT_EQ(dev->set_device_ready(), NSAPI_ERROR_ALREADY);
CellularStateMachine_stub::bool_value = true;
CellularStateMachine_stub::get_current_target_state = STATE_SIM_PIN;
CellularStateMachine_stub::get_current_current_state = STATE_POWER_ON;
ASSERT_EQ(dev->set_device_ready(), NSAPI_ERROR_IN_PROGRESS);
delete dev;
}
TEST_F(TestCellularDevice, test_set_sim_ready)
{
FileHandle_stub fh1;
CellularDevice *dev = new myCellularDevice(&fh1);
EXPECT_TRUE(dev);
CellularStateMachine_stub::nsapi_error_value = NSAPI_ERROR_NO_MEMORY;
ASSERT_EQ(dev->set_sim_ready(), NSAPI_ERROR_NO_MEMORY);
CellularStateMachine_stub::nsapi_error_value = NSAPI_ERROR_OK;
ASSERT_EQ(dev->set_sim_ready(), NSAPI_ERROR_OK);
CellularStateMachine_stub::get_current_current_state = STATE_REGISTERING_NETWORK;
CellularStateMachine_stub::nsapi_error_value = NSAPI_ERROR_OK;
ASSERT_EQ(dev->set_sim_ready(), NSAPI_ERROR_ALREADY);
CellularStateMachine_stub::bool_value = true;
CellularStateMachine_stub::get_current_target_state = STATE_REGISTERING_NETWORK;
CellularStateMachine_stub::get_current_current_state = STATE_POWER_ON;
ASSERT_EQ(dev->set_sim_ready(), NSAPI_ERROR_IN_PROGRESS);
delete dev;
}
TEST_F(TestCellularDevice, test_register_to_network)
{
FileHandle_stub fh1;
CellularDevice *dev = new myCellularDevice(&fh1);
EXPECT_TRUE(dev);
CellularStateMachine_stub::nsapi_error_value = NSAPI_ERROR_NO_MEMORY;
ASSERT_EQ(dev->register_to_network(), NSAPI_ERROR_NO_MEMORY);
CellularStateMachine_stub::nsapi_error_value = NSAPI_ERROR_OK;
ASSERT_EQ(dev->register_to_network(), NSAPI_ERROR_OK);
CellularStateMachine_stub::get_current_current_state = STATE_ATTACHING_NETWORK;
CellularStateMachine_stub::nsapi_error_value = NSAPI_ERROR_OK;
ASSERT_EQ(dev->register_to_network(), NSAPI_ERROR_ALREADY);
CellularStateMachine_stub::bool_value = true;
CellularStateMachine_stub::get_current_target_state = STATE_ATTACHING_NETWORK;
CellularStateMachine_stub::get_current_current_state = STATE_POWER_ON;
ASSERT_EQ(dev->register_to_network(), NSAPI_ERROR_IN_PROGRESS);
delete dev;
}
TEST_F(TestCellularDevice, test_attach_to_network)
{
FileHandle_stub fh1;
CellularDevice *dev = new myCellularDevice(&fh1);
EXPECT_TRUE(dev);
CellularStateMachine_stub::nsapi_error_value = NSAPI_ERROR_NO_MEMORY;
ASSERT_EQ(dev->attach_to_network(), NSAPI_ERROR_NO_MEMORY);
CellularStateMachine_stub::nsapi_error_value = NSAPI_ERROR_OK;
ASSERT_EQ(dev->attach_to_network(), NSAPI_ERROR_OK);
CellularStateMachine_stub::get_current_current_state = STATE_ATTACHING_NETWORK;
CellularStateMachine_stub::nsapi_error_value = NSAPI_ERROR_OK;
ASSERT_EQ(dev->attach_to_network(), NSAPI_ERROR_ALREADY);
CellularStateMachine_stub::bool_value = true;
CellularStateMachine_stub::get_current_target_state = STATE_ATTACHING_NETWORK;
CellularStateMachine_stub::get_current_current_state = STATE_POWER_ON;
ASSERT_EQ(dev->attach_to_network(), NSAPI_ERROR_IN_PROGRESS);
delete dev;
}
TEST_F(TestCellularDevice, test_get_context_list)
{
FileHandle_stub fh1;
CellularDevice *dev = new myCellularDevice(&fh1);
EXPECT_TRUE(dev);
CellularContext *ctx = dev->create_context();
EXPECT_TRUE(dev->get_context_list());
delete dev;
dev = new myCellularDevice(&fh1);
EXPECT_TRUE(dev);
EXPECT_FALSE(dev->get_context_list());
delete dev;
}
TEST_F(TestCellularDevice, test_cellular_callback)
{
FileHandle_stub fh1;
myCellularDevice *dev = new myCellularDevice(&fh1);
EXPECT_TRUE(dev);
ASSERT_EQ(dev->attach_to_network(), NSAPI_ERROR_OK);
cell_callback_data_t data;
dev->cellular_callback((nsapi_event_t)CellularRegistrationStatusChanged, (intptr_t)&data);
data.error = NSAPI_ERROR_OK;
dev->set_plmn("1234");
dev->cellular_callback((nsapi_event_t)CellularDeviceReady, (intptr_t)&data);
dev->set_sim_pin("1234");
data.status_data = CellularDevice::SimStatePinNeeded;
dev->cellular_callback((nsapi_event_t)CellularSIMStatusChanged, (intptr_t)&data);
CellularContext *ctx = dev->create_context();
dev->cellular_callback(NSAPI_EVENT_CONNECTION_STATUS_CHANGE, NSAPI_STATUS_DISCONNECTED);
delete dev;
}
TEST_F(TestCellularDevice, test_shutdown)
{
FileHandle_stub fh1;
CellularDevice *dev = new myCellularDevice(&fh1);
EXPECT_TRUE(dev);
CellularStateMachine_stub::nsapi_error_value = NSAPI_ERROR_OK;
ASSERT_EQ(dev->shutdown(), NSAPI_ERROR_OK);
delete dev;
}
TEST_F(TestCellularDevice, test_timeout_array)
{
FileHandle_stub fh1;
myCellularDevice *dev = new myCellularDevice(&fh1);
EXPECT_TRUE(dev);
CellularStateMachine_stub::nsapi_error_value = NSAPI_ERROR_OK;
// Max size
uint16_t set_timeouts[CELLULAR_RETRY_ARRAY_SIZE + 1];
for (int i = 0; i < CELLULAR_RETRY_ARRAY_SIZE; i++) {
set_timeouts[i] = i + 100;
}
dev->set_retry_timeout_array(set_timeouts, CELLULAR_RETRY_ARRAY_SIZE);
uint16_t verify_timeouts[CELLULAR_RETRY_ARRAY_SIZE + 1];
for (int i = 0; i < CELLULAR_RETRY_ARRAY_SIZE; i++) {
verify_timeouts[i] = i + 100;
}
dev->verify_timeout_array(verify_timeouts, CELLULAR_RETRY_ARRAY_SIZE);
// Empty
dev->set_retry_timeout_array(NULL, 0);
dev->verify_timeout_array(NULL, 0);
// Oversize (returns only CELLULAR_RETRY_ARRAY_SIZE)
dev->set_retry_timeout_array(set_timeouts, CELLULAR_RETRY_ARRAY_SIZE + 1);
dev->verify_timeout_array(verify_timeouts, CELLULAR_RETRY_ARRAY_SIZE);
delete dev;
}

View File

@@ -0,0 +1,50 @@
####################
# UNIT TESTS
####################
# Add test specific include paths
set(unittest-includes ${unittest-includes}
../connectivity/cellular/include/cellular/framework/device/cellulardevice
../connectivity/cellular/include/cellular/framework/device
../connectivity/cellular/include/cellular/framework/common
)
# Source files
set(unittest-sources
../connectivity/cellular/source/framework/device/CellularDevice.cpp
)
# Test files
set(unittest-test-sources
${CMAKE_CURRENT_LIST_DIR}/cellulardevicetest.cpp
stubs/FileHandle_stub.cpp
stubs/CellularStateMachine_stub.cpp
stubs/EventQueue_stub.cpp
stubs/mbed_assert_stub.cpp
stubs/BufferedSerial_stub.cpp
stubs/SerialBase_stub.cpp
stubs/ATHandler_stub.cpp
stubs/AT_CellularNetwork_stub.cpp
stubs/AT_CellularContext_stub.cpp
stubs/AT_CellularDevice_stub.cpp
stubs/Semaphore_stub.cpp
stubs/NetworkInterface_stub.cpp
stubs/NetworkInterfaceDefaults_stub.cpp
stubs/CellularContext_stub.cpp
stubs/ConditionVariable_stub.cpp
stubs/Mutex_stub.cpp
stubs/mbed_shared_queues_stub.cpp
)
set(unittest-test-flags
-DMDMRTS=PTC0
-DMDMCTS=PTC1
-DMDMTXD=NC
-DMDMRXD=NC
-DMBED_CONF_PLATFORM_DEFAULT_SERIAL_BAUD_RATE=115200
-DCELLULAR_DEVICE=myCellularDevice
-DDEVICE_SERIAL_FC=1
-DDEVICE_SERIAL=1
-DDEVICE_INTERRUPTIN=1
)

View File

@@ -0,0 +1,409 @@
/*
* Copyright (c) 2018, Arm Limited and affiliates.
* SPDX-License-Identifier: Apache-2.0
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "gtest/gtest.h"
#include <string.h>
#include "CellularDevice.h"
#include "AT_CellularDevice_stub.h"
#include "FileHandle_stub.h"
#include "AT_CellularNetwork_stub.h"
#include "myCellularDevice.h"
#include "Thread_stub.h"
#include "cmsis_os2.h"
#include "equeue_stub.h"
using namespace mbed;
enum UT_CellularState {
UT_STATE_INIT = 0,
UT_STATE_POWER_ON,
UT_STATE_DEVICE_READY,
UT_STATE_SIM_PIN,
UT_STATE_SIGNAL_QUALITY,
UT_STATE_REGISTERING_NETWORK,
UT_STATE_ATTACHING_NETWORK,
UT_STATE_MAX_FSM_STATE
};
// AStyle ignored as the definition is not clear due to preprocessor usage
// *INDENT-OFF*
class TestCellularStateMachine : public testing::Test {
protected:
void SetUp()
{
Thread_stub::osStatus_value = osOK;
}
void TearDown()
{
}
};
static void cellular_callback(nsapi_event_t ev, intptr_t ptr)
{
}
namespace mbed {
class UT_CellularStateMachine {
public:
UT_CellularStateMachine() {
_state_machine = NULL;
}
~UT_CellularStateMachine() {
delete _state_machine;
_state_machine = NULL;
}
CellularStateMachine *create_state_machine(CellularDevice &device, events::EventQueue &queue, CellularNetwork &nw)
{
_state_machine = new CellularStateMachine(device, queue, nw);
return _state_machine;
}
void delete_state_machine() {
delete _state_machine;
_state_machine = NULL;
}
void set_cellular_callback(mbed::Callback<void(nsapi_event_t, intptr_t)> status_cb)
{
_state_machine->set_cellular_callback(status_cb);
}
nsapi_error_t start_dispatch()
{
return _state_machine->start_dispatch();
}
nsapi_error_t run_to_power_on()
{
return _state_machine->run_to_state(CellularStateMachine::STATE_POWER_ON);
}
nsapi_error_t run_to_device_ready()
{
return _state_machine->run_to_state(CellularStateMachine::STATE_DEVICE_READY);
}
nsapi_error_t run_to_device_sim_ready()
{
return _state_machine->run_to_state(CellularStateMachine::STATE_SIM_PIN);
}
nsapi_error_t run_to_device_registered()
{
return _state_machine->run_to_state(CellularStateMachine::STATE_REGISTERING_NETWORK);
}
nsapi_error_t run_to_device_attached()
{
return _state_machine->run_to_state(CellularStateMachine::STATE_ATTACHING_NETWORK);
}
void stop()
{
_state_machine->stop();
}
void set_sim_pin(const char *sim_pin)
{
_state_machine->set_sim_pin(sim_pin);
}
void set_retry_timeout_array(uint16_t *timeout, int array_len)
{
_state_machine->set_retry_timeout_array(timeout, array_len);
}
void set_plmn(const char *plmn)
{
_state_machine->set_plmn(plmn);
}
bool get_current_status(UT_CellularState &current_state, UT_CellularState &target_state)
{
return _state_machine->get_current_status((CellularStateMachine::CellularState&)current_state,
(CellularStateMachine::CellularState&)target_state);
}
void cellular_event_changed(nsapi_event_t ev, intptr_t ptr)
{
_state_machine->cellular_event_changed(ev, ptr);
}
void reset()
{
_state_machine->reset();
}
void ready_urc_cb()
{
_state_machine->device_ready_cb();
}
CellularStateMachine * _state_machine;
};
}
TEST_F(TestCellularStateMachine, test_create_delete)
{
UT_CellularStateMachine ut;
FileHandle_stub fh1;
CellularDevice *dev = new myCellularDevice(&fh1);
EXPECT_TRUE(dev);
CellularStateMachine *stm = ut.create_state_machine(*dev, *dev->get_queue(), *dev->open_network());
EXPECT_TRUE(stm);
ut.delete_state_machine();
delete dev;
dev = NULL;
}
TEST_F(TestCellularStateMachine, test_setters)
{
UT_CellularStateMachine ut;
FileHandle_stub fh1;
CellularDevice *dev = new myCellularDevice(&fh1);
EXPECT_TRUE(dev);
CellularStateMachine *stm = ut.create_state_machine(*dev, *dev->get_queue(), *dev->open_network());
EXPECT_TRUE(stm);
ut.set_cellular_callback(&cellular_callback);
ut.set_sim_pin(NULL);
ut.set_sim_pin("1234");
ut.set_plmn(NULL);
ut.set_plmn("12345");
uint16_t timeout[20] = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20};
ut.set_retry_timeout_array(timeout, 10); // test max length
ut.set_retry_timeout_array(timeout, 20); // test too big array
ut.set_retry_timeout_array(0, 10); // null array
ut.delete_state_machine();
delete dev;
dev = NULL;
}
TEST_F(TestCellularStateMachine, test_start_dispatch)
{
UT_CellularStateMachine ut;
FileHandle_stub fh1;
CellularDevice *dev = new myCellularDevice(&fh1);
EXPECT_TRUE(dev);
CellularStateMachine *stm = ut.create_state_machine(*dev, *dev->get_queue(), *dev->open_network());
EXPECT_TRUE(stm);
nsapi_error_t err = ut.start_dispatch();
ASSERT_EQ(NSAPI_ERROR_OK, err);
ut.delete_state_machine();
delete dev;
dev = NULL;
}
TEST_F(TestCellularStateMachine, test_stop)
{
UT_CellularStateMachine ut;
FileHandle_stub fh1;
CellularDevice *dev = new AT_CellularDevice(&fh1);
EXPECT_TRUE(dev);
CellularStateMachine *stm = ut.create_state_machine(*dev, *dev->get_queue(), *dev->open_network());
EXPECT_TRUE(stm);
ut.stop(); // nothing created, run through
ut.delete_state_machine();
stm = ut.create_state_machine(*dev, *dev->get_queue(), *dev->open_network());
EXPECT_TRUE(stm);
nsapi_error_t err = ut.start_dispatch();
ASSERT_EQ(NSAPI_ERROR_OK, err);
ut.stop(); // thread is created, now stop will delete it
ut.delete_state_machine();
stm = ut.create_state_machine(*dev, *dev->get_queue(), *dev->open_network());
EXPECT_TRUE(stm);
err = ut.start_dispatch();
ASSERT_EQ(NSAPI_ERROR_OK, err);
ut.set_cellular_callback(&cellular_callback);
struct equeue_event ptr;
equeue_stub.void_ptr = &ptr;
equeue_stub.call_cb_immediately = true;
ASSERT_EQ(NSAPI_ERROR_OK, ut.run_to_power_on());
ut.stop(); // thread and power are created, now stop will delete them
ut.delete_state_machine();
stm = ut.create_state_machine(*dev, *dev->get_queue(), *dev->open_network());
EXPECT_TRUE(stm);
err = ut.start_dispatch();
ASSERT_EQ(NSAPI_ERROR_OK, err);
ut.set_cellular_callback(&cellular_callback);
ASSERT_EQ(NSAPI_ERROR_OK, ut.run_to_device_ready());
ut.stop(); // thread and network are created, now stop will delete them
ut.delete_state_machine();
delete dev;
dev = NULL;
}
TEST_F(TestCellularStateMachine, test_run_to_state)
{
UT_CellularStateMachine ut;
FileHandle_stub fh1;
CellularDevice *dev = new AT_CellularDevice(&fh1);
EXPECT_TRUE(dev);
CellularStateMachine *stm = ut.create_state_machine(*dev, *dev->get_queue(), *dev->open_network());
EXPECT_TRUE(stm);
nsapi_error_t err = ut.start_dispatch();
ASSERT_EQ(NSAPI_ERROR_OK, err);
struct equeue_event ptr;
equeue_stub.void_ptr = 0;
equeue_stub.call_cb_immediately = false;
ASSERT_EQ(NSAPI_ERROR_NO_MEMORY, ut.run_to_power_on());
ut.reset();
equeue_stub.void_ptr = &ptr;
equeue_stub.call_cb_immediately = true;
ut.set_cellular_callback(&cellular_callback);
ASSERT_EQ(NSAPI_ERROR_OK, ut.run_to_power_on());
UT_CellularState current_state;
UT_CellularState target_state;
(void)ut.get_current_status(current_state, target_state);
ASSERT_EQ(UT_STATE_POWER_ON, current_state);
ASSERT_EQ(UT_STATE_POWER_ON, target_state);
ut.reset();
ASSERT_EQ(NSAPI_ERROR_OK, ut.run_to_device_ready());
(void)ut.get_current_status(current_state, target_state);
ASSERT_EQ(UT_STATE_DEVICE_READY, current_state);
ASSERT_EQ(UT_STATE_DEVICE_READY, target_state);
ut.ready_urc_cb();
ut.reset();
ASSERT_EQ(NSAPI_ERROR_OK, ut.run_to_device_ready());
(void)ut.get_current_status(current_state, target_state);
ASSERT_EQ(UT_STATE_DEVICE_READY, current_state);
ASSERT_EQ(UT_STATE_DEVICE_READY, target_state);
ut.reset();
ASSERT_EQ(NSAPI_ERROR_OK, ut.run_to_device_ready());
(void)ut.get_current_status(current_state, target_state);
ASSERT_EQ(UT_STATE_DEVICE_READY, current_state);
ASSERT_EQ(UT_STATE_DEVICE_READY, target_state);
ut.reset();
ASSERT_EQ(NSAPI_ERROR_OK, ut.run_to_device_ready());
(void)ut.get_current_status(current_state, target_state);
ASSERT_EQ(UT_STATE_DEVICE_READY, current_state);
ASSERT_EQ(UT_STATE_DEVICE_READY, target_state);
ut.reset();
AT_CellularDevice_stub::init_module_failure_count = 1;
AT_CellularNetwork_stub::set_registration_urc_fail_counter = 4;
ASSERT_EQ(NSAPI_ERROR_OK, ut.run_to_device_sim_ready());
(void) ut.get_current_status(current_state, target_state);
ASSERT_EQ(UT_STATE_SIM_PIN, current_state);
ASSERT_EQ(UT_STATE_SIM_PIN, target_state);
ut.reset();
AT_CellularDevice_stub::pin_needed = true;
ASSERT_EQ(NSAPI_ERROR_OK, ut.run_to_device_sim_ready());
(void)ut.get_current_status(current_state, target_state);
ASSERT_EQ(UT_STATE_SIM_PIN, current_state);
ASSERT_EQ(UT_STATE_SIM_PIN, target_state);
ut.reset();
ut.set_sim_pin("1234");
AT_CellularDevice_stub::pin_needed = true;
ASSERT_EQ(NSAPI_ERROR_OK, ut.run_to_device_sim_ready());
(void)ut.get_current_status(current_state, target_state);
ASSERT_EQ(UT_STATE_SIM_PIN, current_state);
ASSERT_EQ(UT_STATE_SIM_PIN, target_state);
ut.reset();
ut.set_sim_pin("1234");
AT_CellularDevice_stub::pin_needed = true;
AT_CellularDevice_stub::set_pin_failure_count = 1;
AT_CellularDevice_stub::get_sim_failure_count = 1;
ASSERT_EQ(NSAPI_ERROR_OK, ut.run_to_device_sim_ready());
(void)ut.get_current_status(current_state, target_state);
ASSERT_EQ(UT_STATE_SIM_PIN, current_state);
ASSERT_EQ(UT_STATE_SIM_PIN, target_state);
ut.reset();
cell_callback_data_t data;
data.status_data = CellularNetwork::RegisteredHomeNetwork;
AT_CellularNetwork_stub::get_registration_params_fail_counter = 10;
ASSERT_EQ(NSAPI_ERROR_OK, ut.run_to_device_registered());
(void)ut.get_current_status(current_state, target_state);
ASSERT_EQ(UT_STATE_REGISTERING_NETWORK, current_state);
ASSERT_EQ(UT_STATE_REGISTERING_NETWORK, target_state);
ut.cellular_event_changed((nsapi_event_t)CellularRegistrationStatusChanged, (intptr_t)&data);
ut.reset();
// manual registering
ut.set_plmn("12345");
ASSERT_EQ(NSAPI_ERROR_OK, ut.run_to_device_registered());
(void)ut.get_current_status(current_state, target_state);
ASSERT_EQ(UT_STATE_REGISTERING_NETWORK, current_state);
ASSERT_EQ(UT_STATE_REGISTERING_NETWORK, target_state);
ut.cellular_event_changed((nsapi_event_t)CellularRegistrationStatusChanged, (intptr_t)&data);
ut.reset();
ut.set_plmn(0);
ASSERT_EQ(NSAPI_ERROR_OK, ut.run_to_device_attached());
(void)ut.get_current_status(current_state, target_state);
ASSERT_EQ(UT_STATE_ATTACHING_NETWORK, current_state);
ASSERT_EQ(UT_STATE_ATTACHING_NETWORK, target_state);
ut.reset();
ut.delete_state_machine();
delete dev;
dev = NULL;
}

View File

@@ -0,0 +1,55 @@
####################
# UNIT TESTS
####################
# Add test specific include paths
set(unittest-includes ${unittest-includes}
../connectivity/cellular/include/cellular/framework/device/cellularstatemachine
../connectivity/cellular/include/cellular/framework/device
../connectivity/cellular/include/cellular/framework/common
)
# Source files
set(unittest-sources
../connectivity/cellular/source/framework/device/CellularStateMachine.cpp
)
# Test files
set(unittest-test-sources
${CMAKE_CURRENT_LIST_DIR}/cellularstatemachinetest.cpp
stubs/FileHandle_stub.cpp
stubs/CellularDevice_stub.cpp
stubs/EventQueue_stub.cpp
stubs/mbed_assert_stub.cpp
stubs/BufferedSerial_stub.cpp
stubs/SerialBase_stub.cpp
stubs/ATHandler_stub.cpp
stubs/AT_CellularNetwork_stub.cpp
stubs/AT_CellularContext_stub.cpp
stubs/AT_CellularDevice_stub.cpp
stubs/Semaphore_stub.cpp
stubs/NetworkInterface_stub.cpp
stubs/NetworkInterfaceDefaults_stub.cpp
stubs/Thread_stub.cpp
stubs/Mutex_stub.cpp
stubs/EventQueue_stub.cpp
stubs/equeue_stub.c
stubs/CellularContext_stub.cpp
stubs/ConditionVariable_stub.cpp
stubs/Mutex_stub.cpp
)
set(unittest-test-flags
-DMDMRTS=PTC0
-DMDMCTS=PTC1
-DMDMTXD=NC
-DMDMRXD=NC
-DMBED_CONF_PLATFORM_DEFAULT_SERIAL_BAUD_RATE=115200
-DCELLULAR_DEVICE=myCellularDevice
-DDEVICE_SERIAL_FC=1
-DMBED_CONF_RTOS_PRESENT=1
-DDEVICE_SERIAL=1
-DDEVICE_INTERRUPTIN=1
-DMBED_CONF_CELLULAR_USE_SMS=1
)