From e5c3024849a6f67993378ef1702d448b3698a296 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Teppo=20J=C3=A4rvelin?= Date: Fri, 9 Nov 2018 15:31:37 +0200 Subject: [PATCH] Cellular: adding unit tests for new class CellularContext and changed classes. --- .../at_cellularcontexttest.cpp | 473 +++++++++++++++++- .../AT/at_cellularcontext/unittest.cmake | 20 +- .../at_cellulardevicetest.cpp | 39 +- .../AT/at_cellulardevice/unittest.cmake | 2 +- .../cellulardevice/cellulardevicetest.cpp | 231 +++++++++ .../device/cellulardevice/unittest.cmake | 37 ++ UNITTESTS/stubs/ATHandler_stub.cpp | 2 + UNITTESTS/stubs/AT_CellularDevice_stub.cpp | 102 +++- UNITTESTS/stubs/AT_CellularStack_stub.cpp | 8 +- UNITTESTS/stubs/CellularDevice_stub.cpp | 22 +- UNITTESTS/stubs/CellularDevice_stub.h | 26 + UNITTESTS/stubs/CellularStateMachine_stub.cpp | 26 +- UNITTESTS/stubs/CellularStateMachine_stub.h | 41 ++ UNITTESTS/stubs/Semaphore_stub.cpp | 7 +- UNITTESTS/stubs/Semaphore_stub.h | 26 + UNITTESTS/stubs/UARTSerial_stub.cpp | 6 + UNITTESTS/target_h/PinNames.h | 2 + UNITTESTS/target_h/myCellularDevice.h | 93 ++++ .../test_at_cellulardevice.cpp | 272 ---------- .../UNITTESTS/stubs/ATHandler_stub.cpp | 297 ----------- .../cellular/UNITTESTS/stubs/ATHandler_stub.h | 56 --- .../framework/device/CellularDevice.cpp | 9 +- 22 files changed, 1126 insertions(+), 671 deletions(-) create mode 100644 UNITTESTS/features/cellular/framework/device/cellulardevice/cellulardevicetest.cpp create mode 100644 UNITTESTS/features/cellular/framework/device/cellulardevice/unittest.cmake create mode 100644 UNITTESTS/stubs/CellularDevice_stub.h create mode 100644 UNITTESTS/stubs/CellularStateMachine_stub.h create mode 100644 UNITTESTS/stubs/Semaphore_stub.h create mode 100644 UNITTESTS/target_h/myCellularDevice.h delete mode 100644 features/cellular/UNITTESTS/at/at_cellulardevice/test_at_cellulardevice.cpp delete mode 100644 features/cellular/UNITTESTS/stubs/ATHandler_stub.cpp delete mode 100644 features/cellular/UNITTESTS/stubs/ATHandler_stub.h diff --git a/UNITTESTS/features/cellular/framework/AT/at_cellularcontext/at_cellularcontexttest.cpp b/UNITTESTS/features/cellular/framework/AT/at_cellularcontext/at_cellularcontexttest.cpp index bd6bb2c42b..258fd68711 100644 --- a/UNITTESTS/features/cellular/framework/AT/at_cellularcontext/at_cellularcontexttest.cpp +++ b/UNITTESTS/features/cellular/framework/AT/at_cellularcontext/at_cellularcontexttest.cpp @@ -24,6 +24,10 @@ #include "CellularLog.h" #include "ATHandler_stub.h" #include "AT_CellularStack.h" +#include "Semaphore_stub.h" +#include "CellularDevice_stub.h" +#include "equeue_stub.h" +#include "CellularSIM.h" using namespace mbed; using namespace events; @@ -35,11 +39,30 @@ protected: void SetUp() { + ATHandler_stub::nsapi_error_value = 0; + ATHandler_stub::nsapi_error_ok_counter = 0; + ATHandler_stub::int_value = -1; + ATHandler_stub::ref_count = 0; + ATHandler_stub::timeout = 0; + ATHandler_stub::default_timeout = 0; + ATHandler_stub::debug_on = 0; + ATHandler_stub::ssize_value = 0; + ATHandler_stub::read_string_value = NULL; + ATHandler_stub::size_value = 0; + ATHandler_stub::return_given_size = false; + ATHandler_stub::bool_value = false; + ATHandler_stub::uint8_value = 0; + ATHandler_stub::fh_value = NULL; + ATHandler_stub::callback = NULL; + ATHandler_stub::call_immediately = false; + ATHandler_stub::resp_info_true_counter = false; + ATHandler_stub::info_elem_true_counter = false; + ATHandler_stub::int_valid_count_table[kRead_int_table_size]; ATHandler_stub::int_count = kRead_int_table_size; ATHandler_stub::read_string_index = kRead_string_table_size; + ATHandler_stub::read_string_table[kRead_string_table_size]; ATHandler_stub::resp_stop_success_count = kResp_stop_count_default; - ATHandler_stub::nsapi_error_value = NSAPI_ERROR_OK; - ATHandler_stub::int_value = -1; + CellularDevice_stub::connect_counter = 2; } void TearDown() @@ -80,20 +103,32 @@ public: { return 100; } + + virtual const char *get_ip_address() { return "1.2.3.4";} }; class my_AT_CTX : public AT_CellularContext { public: my_AT_CTX(ATHandler &at, CellularDevice *device, const char *apn = MBED_CONF_NSAPI_DEFAULT_CELLULAR_APN) : - AT_CellularContext(at, device, apn) {} + AT_CellularContext(at, device, apn), _st(at) {} virtual ~my_AT_CTX() {} + virtual bool stack_type_supported(nsapi_ip_stack_t stack_type) { return false;} + virtual NetworkStack *get_stack() {return &_st;} + virtual uint32_t get_timeout_for_operation(ContextOperation op) const { return 10;} + virtual void cellular_callback(nsapi_event_t ev, intptr_t ptr) { AT_CellularContext::cellular_callback(ev, ptr);} + + my_stack _st; }; class my_AT_CTXIPV6 : public AT_CellularContext { public: my_AT_CTXIPV6(ATHandler &at, CellularDevice *device, const char *apn = MBED_CONF_NSAPI_DEFAULT_CELLULAR_APN) : - AT_CellularContext(at, device, apn) {} + AT_CellularContext(at, device, apn), _st(at) {} virtual ~my_AT_CTXIPV6() {} + virtual bool stack_type_supported(nsapi_ip_stack_t stack_type) {return true;} + virtual NetworkStack *get_stack() {return &_st;} + virtual uint32_t get_timeout_for_operation(ContextOperation op) const { return 10;} + my_stack _st; }; static int network_cb_count; @@ -107,8 +142,438 @@ TEST_F(TestAT_CellularContext, Create) EventQueue que; FileHandle_stub fh1; ATHandler at(&fh1, que, 0, ","); + AT_CellularDevice dev(&fh1); AT_CellularContext *ctx = new AT_CellularContext(at, NULL); EXPECT_TRUE(ctx != NULL); delete ctx; + + ctx = new AT_CellularContext(at, &dev); + EXPECT_TRUE(ctx != NULL); + delete ctx; +} + +TEST_F(TestAT_CellularContext, set_blocking) +{ + EventQueue que; + FileHandle_stub fh1; + ATHandler at(&fh1, que, 0, ","); + AT_CellularDevice dev(&fh1); + AT_CellularContext ctx(at, &dev); + + ATHandler_stub::nsapi_error_value = NSAPI_ERROR_OK; + ASSERT_EQ(NSAPI_ERROR_OK, ctx.set_blocking(false)); + ASSERT_EQ(NSAPI_ERROR_OK, ctx.set_blocking(true)); + + ATHandler_stub::nsapi_error_value = NSAPI_ERROR_DEVICE_ERROR; + ASSERT_EQ(NSAPI_ERROR_OK, ctx.set_blocking(false)); + ASSERT_EQ(NSAPI_ERROR_OK, ctx.set_blocking(true)); +} + +TEST_F(TestAT_CellularContext, get_stack) +{ + EventQueue que; + FileHandle_stub fh1; + ATHandler at(&fh1, que, 0, ","); + AT_CellularDevice dev(&fh1); + AT_CellularContext ctx(at, &dev); + NetworkStack *stack = ctx.get_stack(); + EXPECT_TRUE(stack == NULL); + + my_AT_CTX ctx1(at, NULL); + stack = ctx1.get_stack(); + EXPECT_TRUE(stack != NULL); +} + +TEST_F(TestAT_CellularContext, get_ip_address) +{ + EventQueue que; + FileHandle_stub fh1; + ATHandler at(&fh1, que, 0, ","); + AT_CellularDevice dev(&fh1); + AT_CellularContext ctx(at, &dev); + const char *ip = ctx.get_ip_address(); + EXPECT_TRUE(ip == NULL); + + my_AT_CTX ctx1(at, NULL); + ip = ctx1.get_ip_address(); + EXPECT_TRUE(ip != NULL); +} + +TEST_F(TestAT_CellularContext, attach) +{ + EventQueue que; + FileHandle_stub fh1; + AT_CellularDevice dev(&fh1); + ATHandler at(&fh1, que, 0, ","); + + AT_CellularContext ctx(at, &dev); + ctx.attach(&network_cb); +} + +TEST_F(TestAT_CellularContext, set_plmn) +{ + EventQueue que; + FileHandle_stub fh1; + ATHandler at(&fh1, que, 0, ","); + AT_CellularDevice dev(&fh1); + AT_CellularContext ctx(at, &dev); + ctx.set_plmn(NULL); + ctx.set_plmn("12345"); +} + +TEST_F(TestAT_CellularContext, set_sim_pin) +{ + EventQueue que; + FileHandle_stub fh1; + ATHandler at(&fh1, que, 0, ","); + AT_CellularDevice dev(&fh1); + AT_CellularContext ctx(at, &dev); + ctx.set_sim_pin(NULL); + ctx.set_sim_pin("12345"); +} + +TEST_F(TestAT_CellularContext, set_credentials) +{ + EventQueue que; + FileHandle_stub fh1; + ATHandler at(&fh1, que, 0, ","); + AT_CellularDevice dev(&fh1); + AT_CellularContext ctx(at, &dev); + + ctx.set_credentials(NULL); + ctx.set_credentials("apn"); + ctx.set_credentials("apn", NULL); + ctx.set_credentials("apn", NULL, NULL); + ctx.set_credentials("apn", NULL, "passwd"); + ctx.set_credentials("apn", "user", NULL); + ctx.set_credentials("apn", "user"); + ctx.set_credentials("apn", "user", "passwd"); +} + +TEST_F(TestAT_CellularContext, get_netmask_gateway) +{ + EventQueue que; + FileHandle_stub fh1; + ATHandler at(&fh1, que, 0, ","); + AT_CellularDevice dev(&fh1); + AT_CellularContext ctx(at, &dev); + const char* gg = ctx.get_netmask(); + EXPECT_TRUE(gg == NULL); + gg = ctx.get_gateway(); + EXPECT_TRUE(gg == NULL); +} + +TEST_F(TestAT_CellularContext, get_pdpcontext_params) +{ + EventQueue que; + FileHandle_stub fh1; + ATHandler at(&fh1, que, 0, ","); + AT_CellularDevice dev(&fh1); + AT_CellularContext cn(at, &dev); + CellularContext::pdpContextList_t list; + + ATHandler_stub::nsapi_error_value = NSAPI_ERROR_DEVICE_ERROR; + EXPECT_TRUE(NSAPI_ERROR_DEVICE_ERROR == cn.get_pdpcontext_params(list)); + + // don't got to while loop + ATHandler_stub::nsapi_error_value = NSAPI_ERROR_OK; + ATHandler_stub::bool_value = false; + ATHandler_stub::resp_info_true_counter = 0; + ASSERT_EQ(cn.get_pdpcontext_params(list), NSAPI_ERROR_OK); + EXPECT_TRUE(NULL == list.get_head()); + + // go to while loop and check values + ATHandler_stub::nsapi_error_value = NSAPI_ERROR_OK; + ATHandler_stub::resp_info_true_counter = 1; + ATHandler_stub::int_count = 9; + ATHandler_stub::int_valid_count_table[8] = 1; + ATHandler_stub::int_valid_count_table[7] = 2; + ATHandler_stub::int_valid_count_table[6] = 3; + ATHandler_stub::int_valid_count_table[5] = 4; + ATHandler_stub::int_valid_count_table[4] = 5; + ATHandler_stub::int_valid_count_table[3] = 6; + ATHandler_stub::int_valid_count_table[2] = 7; + ATHandler_stub::int_valid_count_table[1] = 8; + ATHandler_stub::int_valid_count_table[0] = 9; + + ATHandler_stub::read_string_index = 7; + ATHandler_stub::read_string_table[6] = (char *)"internet"; + ATHandler_stub::read_string_table[5] = (char *)"1.2.3.4.5.6.7.8.9.10.11.112.13.14.15.16.1.2.3.44.55.6.7.8.9.10.11.12.13.14.15.16"; + ATHandler_stub::read_string_table[4] = (char *)"23.33.44.1.2.3.55.123.225.34.11.1.0.0.123.234"; + ATHandler_stub::read_string_table[3] = (char *)"1.2.3.4"; + ATHandler_stub::read_string_table[2] = (char *)"0.255.0.255"; + ATHandler_stub::read_string_table[1] = (char *)"25.66.77.88"; + ATHandler_stub::read_string_table[0] = (char *)"004.003.002.001"; + + EXPECT_TRUE(NSAPI_ERROR_OK == cn.get_pdpcontext_params(list)); + CellularContext::pdpcontext_params_t *params = list.get_head(); + EXPECT_TRUE(params != NULL); + EXPECT_TRUE(params->next == NULL); + ASSERT_EQ(params->cid, 1); + ASSERT_EQ(params->bearer_id, 2); + ASSERT_EQ(params->im_signalling_flag, 3); + ASSERT_EQ(params->lipa_indication, 4); + ASSERT_EQ(params->ipv4_mtu, 5); + ASSERT_EQ(params->wlan_offload, 6); + ASSERT_EQ(params->local_addr_ind, 7); + ASSERT_EQ(params->non_ip_mtu, 8); + ASSERT_EQ(params->serving_plmn_rate_control_value, 9); + ASSERT_STREQ(params->apn, "internet"); + ASSERT_STREQ(params->local_addr, "102:304:506:708:90A:B70:D0E:F10"); + ASSERT_STREQ(params->local_subnet_mask, "102:32C:3706:708:90A:B0C:D0E:F10"); + ASSERT_STREQ(params->gateway_addr, "1721:2C01:203:377B:E122:B01:000:7BEA"); + ASSERT_STREQ(params->dns_primary_addr, "1.2.3.4"); + ASSERT_STREQ(params->dns_secondary_addr, "0.255.0.255"); + ASSERT_STREQ(params->p_cscf_prim_addr, "25.66.77.88"); + ASSERT_STREQ(params->p_cscf_sec_addr, "004.003.002.001"); +} + +TEST_F(TestAT_CellularContext, get_rate_control) +{ + EventQueue que; + FileHandle_stub fh1; + ATHandler at(&fh1, que, 0, ","); + AT_CellularDevice dev(&fh1); + AT_CellularContext cn(at, &dev); + int ur = -1; + CellularContext::RateControlExceptionReports reports = CellularContext::NotAllowedToBeSent; + CellularContext::RateControlUplinkTimeUnit timeUnit = CellularContext::Unrestricted; + ATHandler_stub::nsapi_error_value = NSAPI_ERROR_DEVICE_ERROR; + ASSERT_EQ(NSAPI_ERROR_DEVICE_ERROR, cn.get_rate_control(reports, timeUnit, ur)); + ASSERT_EQ(reports, CellularContext::NotAllowedToBeSent); + ASSERT_EQ(timeUnit, CellularContext::Unrestricted); + ASSERT_EQ(ur, -1); + + ATHandler_stub::nsapi_error_value = NSAPI_ERROR_OK; + ATHandler_stub::int_value = 1; + ASSERT_EQ(NSAPI_ERROR_OK, cn.get_rate_control(reports, timeUnit, ur)); + ASSERT_EQ(reports, CellularContext::AllowedToBeSent); + ASSERT_EQ(timeUnit, CellularContext::Minute); + ASSERT_EQ(ur, 1); + + // test second if in get_rate_control + reports = CellularContext::NotAllowedToBeSent; + timeUnit = CellularContext::Unrestricted; + ur = -1; + + ATHandler_stub::int_count = 1; + ATHandler_stub::int_valid_count_table[0] = 1; + ATHandler_stub::nsapi_error_value = NSAPI_ERROR_OK; + ASSERT_EQ(NSAPI_ERROR_DEVICE_ERROR, cn.get_rate_control(reports, timeUnit, ur)); + ASSERT_EQ(reports, CellularContext::NotAllowedToBeSent); + ASSERT_EQ(timeUnit, CellularContext::Unrestricted); + ASSERT_EQ(ur, -1); + + // test second if in get_rate_control + ATHandler_stub::int_count = 2; + ATHandler_stub::int_valid_count_table[0] = 1; + ATHandler_stub::int_valid_count_table[1] = 1; + ATHandler_stub::nsapi_error_value = NSAPI_ERROR_OK; + ASSERT_EQ(NSAPI_ERROR_DEVICE_ERROR, cn.get_rate_control(reports, timeUnit, ur)); + ASSERT_EQ(reports, CellularContext::AllowedToBeSent); + ASSERT_EQ(timeUnit, CellularContext::Unrestricted); + ASSERT_EQ(ur, -1); + + // test third if in get_rate_control + ATHandler_stub::int_count = 3; + ATHandler_stub::int_valid_count_table[0] = 3; + ATHandler_stub::int_valid_count_table[1] = 1; + ATHandler_stub::int_valid_count_table[2] = 1; + ATHandler_stub::nsapi_error_value = NSAPI_ERROR_OK; + EXPECT_TRUE(NSAPI_ERROR_DEVICE_ERROR == cn.get_rate_control(reports, timeUnit, ur)); + EXPECT_TRUE(reports == CellularContext::AllowedToBeSent); + EXPECT_TRUE(timeUnit == CellularContext::Day); + EXPECT_TRUE(ur == -1); + + ATHandler_stub::int_count = 4; + ATHandler_stub::int_valid_count_table[0] = 5; + ATHandler_stub::int_valid_count_table[1] = 3; + ATHandler_stub::int_valid_count_table[2] = 1; + ATHandler_stub::int_valid_count_table[3] = 1; + ATHandler_stub::nsapi_error_value = NSAPI_ERROR_OK; + ASSERT_EQ(NSAPI_ERROR_OK, cn.get_rate_control(reports, timeUnit, ur)); + ASSERT_EQ(reports, CellularContext::AllowedToBeSent); + ASSERT_EQ(timeUnit, CellularContext::Day); + ASSERT_EQ(ur, 5); +} + +TEST_F(TestAT_CellularContext, get_apn_backoff_timer) +{ + EventQueue que; + FileHandle_stub fh1; + ATHandler at(&fh1, que, 0, ","); + AT_CellularDevice dev(&fh1); + AT_CellularContext cn(at, &dev); + int time = -1; + ATHandler_stub::nsapi_error_value = NSAPI_ERROR_DEVICE_ERROR; + ASSERT_EQ(NSAPI_ERROR_PARAMETER, cn.get_apn_backoff_timer(time)); + ASSERT_EQ(time, -1); + + ATHandler_stub::nsapi_error_value = NSAPI_ERROR_OK; + ASSERT_EQ(NSAPI_ERROR_PARAMETER, cn.get_apn_backoff_timer(time)); + ASSERT_EQ(time, -1); + + ATHandler_stub::resp_info_true_counter = 0; + ATHandler_stub::bool_value = false; + ATHandler_stub::nsapi_error_value = NSAPI_ERROR_OK; + cn.set_credentials("internet", NULL, NULL); + ASSERT_EQ(NSAPI_ERROR_OK, cn.get_apn_backoff_timer(time)); + ASSERT_EQ(time, -1); + + ATHandler_stub::resp_info_true_counter = 0; + ATHandler_stub::bool_value = true; + ATHandler_stub::int_value = 55; + ATHandler_stub::nsapi_error_value = NSAPI_ERROR_OK; + cn.set_credentials("internet", NULL, NULL); + ASSERT_EQ(NSAPI_ERROR_OK, cn.get_apn_backoff_timer(time)); + ASSERT_EQ(time, 55); +} + +TEST_F(TestAT_CellularContext, set_file_handle) +{ + EventQueue que; + FileHandle_stub fh1; + ATHandler at(&fh1, que, 0, ","); + AT_CellularDevice dev(&fh1); + AT_CellularContext ctx(at, &dev); + ctx.set_file_handle(&fh1); +} + +TEST_F(TestAT_CellularContext, connect_disconnect_sync) +{ + EventQueue que; + FileHandle_stub fh1; + ATHandler at(&fh1, que, 0, ","); + AT_CellularDevice dev(&fh1); + AT_CellularContext ctx(at, &dev); + ctx.attach(&network_cb); + network_cb_count = 0; + // connect in sync mode, semaphore will return 0 so timeout is returned + ASSERT_EQ(ctx.connect(), NSAPI_ERROR_TIMEOUT); + ASSERT_EQ(network_cb_count, 0); + + my_AT_CTX ctx1(at, &dev); + ctx1.attach(&network_cb); + Semaphore_stub::wait_return_value = 1; + + // call callback so that network is opened which is needed in disconnect + cell_callback_data_t data; + data.error = NSAPI_ERROR_OK; + ctx1.cellular_callback((nsapi_event_t)CellularDeviceReady, (intptr_t)&data); + + ASSERT_EQ(ctx1.connect(), NSAPI_ERROR_OK); + + ASSERT_EQ(network_cb_count, 4); + + ASSERT_EQ(ctx1.connect(), NSAPI_ERROR_IS_CONNECTED); + + EXPECT_TRUE(ctx1.is_connected() == true); + + ASSERT_EQ(ctx1.disconnect(), NSAPI_ERROR_OK); + EXPECT_TRUE(ctx1.is_connected() == false); + + ASSERT_EQ(ctx1.connect("1234", "internet", "usern", "pwd"), NSAPI_ERROR_OK); + + // More connect test after we are re-writted getting of PDP context... +} + +TEST_F(TestAT_CellularContext, set_device_ready_sync) +{ + EventQueue que; + FileHandle_stub fh1; + ATHandler at(&fh1, que, 0, ","); + AT_CellularDevice dev(&fh1); + my_AT_CTX ctx(at, &dev); + cell_callback_data_t data; + data.error = NSAPI_ERROR_OK; + ctx.cellular_callback((nsapi_event_t)CellularDeviceReady, (intptr_t)&data); + ASSERT_EQ(NSAPI_ERROR_OK, ctx.set_device_ready()); +} + +TEST_F(TestAT_CellularContext, set_sim_ready) +{ + EventQueue que; + FileHandle_stub fh1; + ATHandler at(&fh1, que, 0, ","); + AT_CellularDevice dev(&fh1); + my_AT_CTX ctx(at, &dev); + ASSERT_EQ(NSAPI_ERROR_OK, ctx.set_sim_ready()); + cell_callback_data_t data; + data.error = NSAPI_ERROR_OK; + ctx.cellular_callback((nsapi_event_t)CellularDeviceReady, (intptr_t)&data); + data.status_data = CellularSIM::SimStateReady; + ctx.cellular_callback((nsapi_event_t)CellularSIMStatusChanged, (intptr_t)&data); +} + +TEST_F(TestAT_CellularContext, register_to_network) +{ + EventQueue que; + FileHandle_stub fh1; + ATHandler at(&fh1, que, 0, ","); + AT_CellularDevice dev(&fh1); + my_AT_CTX ctx(at, &dev); + ASSERT_EQ(NSAPI_ERROR_OK, ctx.register_to_network()); + cell_callback_data_t data; + data.error = NSAPI_ERROR_OK; + ctx.cellular_callback((nsapi_event_t)CellularDeviceReady, (intptr_t)&data); + data.status_data = CellularNetwork::RegisteredHomeNetwork; + ctx.cellular_callback((nsapi_event_t)CellularRegistrationStatusChanged, (intptr_t)&data); +} + +TEST_F(TestAT_CellularContext, attach_to_network) +{ + EventQueue que; + FileHandle_stub fh1; + ATHandler at(&fh1, que, 0, ","); + AT_CellularDevice dev(&fh1); + my_AT_CTX ctx(at, &dev); + ASSERT_EQ(NSAPI_ERROR_OK, ctx.attach_to_network()); + cell_callback_data_t data; + data.error = NSAPI_ERROR_OK; + ctx.cellular_callback((nsapi_event_t)CellularDeviceReady, (intptr_t)&data); + data.status_data = CellularNetwork::Attached; + ctx.cellular_callback((nsapi_event_t)CellularAttachNetwork, (intptr_t)&data); +} + +TEST_F(TestAT_CellularContext, connect_disconnect_async) +{ + EventQueue que; + FileHandle_stub fh1; + ATHandler at(&fh1, que, 0, ","); + network_cb_count = 0; + + AT_CellularDevice dev(&fh1); + my_AT_CTX ctx1(at, &dev); + ctx1.attach(&network_cb); + ASSERT_EQ(ctx1.set_blocking(false), NSAPI_ERROR_OK); + + ASSERT_EQ(ctx1.connect(), NSAPI_ERROR_OK); + + // call callback so that network is opened which is needed in disconnect + cell_callback_data_t data; + data.error = NSAPI_ERROR_OK; + ctx1.cellular_callback((nsapi_event_t)CellularDeviceReady, (intptr_t)&data); + + // attach ready, ctx will continue in callback + data.status_data = CellularNetwork::Attached; + ctx1.cellular_callback((nsapi_event_t)CellularAttachNetwork, (intptr_t)&data); + + ASSERT_EQ(network_cb_count, 5); + ASSERT_EQ(ctx1.connect(), NSAPI_ERROR_IS_CONNECTED); + EXPECT_TRUE(ctx1.is_connected() == true); + ASSERT_EQ(ctx1.disconnect(), NSAPI_ERROR_OK); + EXPECT_TRUE(ctx1.is_connected() == false); + + // sdet CellularDevice_stub::connect_counter = 0 so device is already attached and will return NSAPI_ERROR_ALREADY to context when calling connect + CellularDevice_stub::connect_counter = 0; + // queue can't allocate so return NSAPI_ERROR_NO_MEMORY + ASSERT_EQ(ctx1.connect(), NSAPI_ERROR_NO_MEMORY); + + struct equeue_event ptr; + equeue_stub.void_ptr = &ptr; + equeue_stub.call_cb_immediately = true; + ASSERT_EQ(ctx1.connect(), NSAPI_ERROR_OK); + + // More connect test after we are re-writted getting of PDP context... } diff --git a/UNITTESTS/features/cellular/framework/AT/at_cellularcontext/unittest.cmake b/UNITTESTS/features/cellular/framework/AT/at_cellularcontext/unittest.cmake index 99c674ac0f..1d9c56320a 100644 --- a/UNITTESTS/features/cellular/framework/AT/at_cellularcontext/unittest.cmake +++ b/UNITTESTS/features/cellular/framework/AT/at_cellularcontext/unittest.cmake @@ -14,6 +14,7 @@ set(unittest-includes ${unittest-includes} # Source files set(unittest-sources ../features/cellular/framework/AT/AT_CellularContext.cpp + ../features/cellular/framework/common/CellularUtil.cpp ) # Test files @@ -21,15 +22,18 @@ set(unittest-test-sources features/cellular/framework/AT/at_cellularcontext/at_cellularcontexttest.cpp stubs/ATHandler_stub.cpp stubs/AT_CellularBase_stub.cpp - stubs/EventQueue_stub.cpp - stubs/FileHandle_stub.cpp - stubs/NetworkInterface_stub.cpp - stubs/NetworkStack_stub.cpp - stubs/us_ticker_stub.cpp - stubs/mbed_assert_stub.c + stubs/AT_CellularDevice_stub.cpp + stubs/AT_CellularStack_stub.cpp + stubs/AT_CellularNetwork_stub.cpp stubs/CellularDevice_stub.cpp stubs/CellularStateMachine_stub.cpp - stubs/Semaphore_stub.cpp - stubs/CellularUtil_stub.cpp stubs/equeue_stub.c + stubs/EventQueue_stub.cpp + stubs/FileHandle_stub.cpp + stubs/mbed_assert_stub.c + stubs/NetworkInterface_stub.cpp + stubs/NetworkStack_stub.cpp + stubs/randLIB_stub.cpp + stubs/Semaphore_stub.cpp + stubs/us_ticker_stub.cpp ) diff --git a/UNITTESTS/features/cellular/framework/AT/at_cellulardevice/at_cellulardevicetest.cpp b/UNITTESTS/features/cellular/framework/AT/at_cellulardevice/at_cellulardevicetest.cpp index e2e5db2e5e..8609e8a38a 100644 --- a/UNITTESTS/features/cellular/framework/AT/at_cellulardevice/at_cellulardevicetest.cpp +++ b/UNITTESTS/features/cellular/framework/AT/at_cellulardevice/at_cellulardevicetest.cpp @@ -15,10 +15,10 @@ * limitations under the License. */ #include "gtest/gtest.h" +#include #include "AT_CellularDevice.h" #include "ATHandler_stub.h" #include "AT_CellularBase_stub.h" -#include using namespace mbed; using namespace events; @@ -270,3 +270,40 @@ TEST_F(TestAT_CellularDevice, test_AT_CellularDevice_init_module) AT_CellularDevice dev(&fh1); EXPECT_TRUE(NSAPI_ERROR_OK == dev.init_module()); } + +TEST_F(TestAT_CellularDevice, test_AT_CellularDevice_create_delete_context) +{ + FileHandle_stub fh1; + AT_CellularDevice *dev = new AT_CellularDevice(&fh1); + + CellularContext *ctx = dev->create_context(NULL); + delete dev; + + dev = new AT_CellularDevice(&fh1); + ctx = dev->create_context(NULL); + CellularContext *ctx1 = dev->create_context(&fh1); + CellularContext *ctx2 = dev->create_context(&fh1); + + EXPECT_TRUE(ctx); + EXPECT_TRUE(ctx1); + EXPECT_TRUE(ctx1 != ctx); + EXPECT_TRUE(ctx1 != ctx2); + + CellularContext *xx = dev->get_context_list(); + EXPECT_TRUE(xx); + + dev->delete_context(ctx); + dev->delete_context(ctx1); + dev->delete_context(NULL); + dev->delete_context(ctx2); + + ctx = dev->create_context(NULL); + ctx1 = dev->create_context(&fh1); + ctx2 = dev->create_context(&fh1); + EXPECT_TRUE(ctx); + EXPECT_TRUE(ctx1); + EXPECT_TRUE(ctx1 != ctx); + EXPECT_TRUE(ctx1 != ctx2); + + delete dev; +} diff --git a/UNITTESTS/features/cellular/framework/AT/at_cellulardevice/unittest.cmake b/UNITTESTS/features/cellular/framework/AT/at_cellulardevice/unittest.cmake index bd2d4382ff..0897cb4a9e 100644 --- a/UNITTESTS/features/cellular/framework/AT/at_cellulardevice/unittest.cmake +++ b/UNITTESTS/features/cellular/framework/AT/at_cellulardevice/unittest.cmake @@ -5,7 +5,7 @@ # Add test specific include paths set(unittest-includes ${unittest-includes} - features/cellular/framework/common/util + ../features/cellular/framework/common/util ../features/cellular/framework/common ../features/cellular/framework/AT ../features/cellular/framework/device diff --git a/UNITTESTS/features/cellular/framework/device/cellulardevice/cellulardevicetest.cpp b/UNITTESTS/features/cellular/framework/device/cellulardevice/cellulardevicetest.cpp new file mode 100644 index 0000000000..4eb5e3d6e6 --- /dev/null +++ b/UNITTESTS/features/cellular/framework/device/cellulardevice/cellulardevicetest.cpp @@ -0,0 +1,231 @@ +/* + * 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 + +#include "CellularDevice.h" +#include "FileHandle_stub.h" +#include "myCellularDevice.h" +#include "CellularStateMachine_stub.h" +#include "CellularSIM.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_TRUE(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_MANUAL_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_MANUAL_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 = NULL; + + dev = new myCellularDevice(&fh1); + EXPECT_TRUE(dev); + EXPECT_FALSE(dev->get_context_list()); +} + +TEST_F(TestCellularDevice, test_stop) +{ + FileHandle_stub fh1; + CellularDevice *dev = new myCellularDevice(&fh1); + EXPECT_TRUE(dev); + + CellularStateMachine_stub::nsapi_error_value = NSAPI_ERROR_OK; + ASSERT_EQ(dev->attach_to_network(), NSAPI_ERROR_OK); + + dev->stop(); + 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 = CellularSIM::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; +} diff --git a/UNITTESTS/features/cellular/framework/device/cellulardevice/unittest.cmake b/UNITTESTS/features/cellular/framework/device/cellulardevice/unittest.cmake new file mode 100644 index 0000000000..7da80e97b1 --- /dev/null +++ b/UNITTESTS/features/cellular/framework/device/cellulardevice/unittest.cmake @@ -0,0 +1,37 @@ + +#################### +# UNIT TESTS +#################### + +# Add test specific include paths +set(unittest-includes ${unittest-includes} + /features/cellular/framework/device/cellulardevice + ../features/cellular/framework/device + ../features/cellular/framework/common +) + +# Source files +set(unittest-sources + ../features/cellular/framework/device/CellularDevice.cpp +) + +# Test files +set(unittest-test-sources + features/cellular/framework/device/cellulardevice/cellulardevicetest.cpp + stubs/FileHandle_stub.cpp + stubs/CellularStateMachine_stub.cpp + stubs/EventQueue_stub.cpp + stubs/mbed_assert_stub.c + stubs/UARTSerial_stub.cpp + stubs/SerialBase_stub.cpp + stubs/ATHandler_stub.cpp + stubs/AT_CellularNetwork_stub.cpp + stubs/AT_CellularBase_stub.cpp + stubs/AT_CellularContext_stub.cpp + stubs/Semaphore_stub.cpp + stubs/NetworkInterface_stub.cpp +) + +# defines +set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DMDMRTS=PTC0 -DMDMCTS=PTC1 -DMDMTXD=NC -DMDMRXD=NC -DMBED_CONF_PLATFORM_DEFAULT_SERIAL_BAUD_RATE=115200 -DCELLULAR_DEVICE=myCellularDevice -DDEVICE_SERIAL_FC=1") +set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DMDMRTS=PTC0 -DMDMCTS=PTC1 -DMDMTXD=NC -DMDMRXD=NC -DMBED_CONF_PLATFORM_DEFAULT_SERIAL_BAUD_RATE=115200 -DCELLULAR_DEVICE=myCellularDevice -DDEVICE_SERIAL_FC=1") diff --git a/UNITTESTS/stubs/ATHandler_stub.cpp b/UNITTESTS/stubs/ATHandler_stub.cpp index 06e6fbfc09..ff58ade4ac 100644 --- a/UNITTESTS/stubs/ATHandler_stub.cpp +++ b/UNITTESTS/stubs/ATHandler_stub.cpp @@ -170,6 +170,7 @@ ssize_t ATHandler::read_string(char *buf, size_t size, bool read_even_stop_tag) if (ATHandler_stub::read_string_index == kRead_string_table_size) { if (ATHandler_stub::read_string_value && ATHandler_stub::ssize_value >= 0) { memcpy(buf, ATHandler_stub::read_string_value, ATHandler_stub::ssize_value + 1); + buf[ATHandler_stub::ssize_value] = '\0'; } return ATHandler_stub::ssize_value; } @@ -179,6 +180,7 @@ ssize_t ATHandler::read_string(char *buf, size_t size, bool read_even_stop_tag) const char *tmp = ATHandler_stub::read_string_table[ATHandler_stub::read_string_index]; ssize_t len = strlen(tmp); memcpy(buf, tmp, len + 1); + buf[len] = '\0'; return len; } diff --git a/UNITTESTS/stubs/AT_CellularDevice_stub.cpp b/UNITTESTS/stubs/AT_CellularDevice_stub.cpp index 9f6749a8f8..9fe2243316 100644 --- a/UNITTESTS/stubs/AT_CellularDevice_stub.cpp +++ b/UNITTESTS/stubs/AT_CellularDevice_stub.cpp @@ -16,19 +16,28 @@ */ #include "AT_CellularDevice.h" +#include "AT_CellularNetwork.h" +#include "ATHandler.h" -AT_CellularDevice::AT_CellularDevice(EventQueue &queue) : - _atHandlers(0), _network(0), _sms(0), _sim(0), _power(0), _multiplexer(0), _information(0), _queue(queue) +const int DEFAULT_AT_TIMEOUT = 1000; + +using namespace mbed; + +AT_CellularDevice::AT_CellularDevice(FileHandle *fh) : CellularDevice(fh), _network(0), _sms(0), + _sim(0), _power(0), _information(0), _context_list(0), _default_timeout(DEFAULT_AT_TIMEOUT), + _modem_debug_on(false) { + _atHandlers = new ATHandler(fh, _queue, 0, ","); } AT_CellularDevice::~AT_CellularDevice() { + delete _atHandlers; } ATHandler *AT_CellularDevice::get_at_handler(FileHandle *fileHandle) { - return NULL; + return _atHandlers; } void AT_CellularDevice::release_at_handler(ATHandler *at_handler) @@ -36,9 +45,19 @@ void AT_CellularDevice::release_at_handler(ATHandler *at_handler) } +CellularContext *create_context(FileHandle *fh = NULL, const char *apn = MBED_CONF_NSAPI_DEFAULT_CELLULAR_APN) +{ + +} + +void delete_context(CellularContext *context) +{ + +} + CellularNetwork *AT_CellularDevice::open_network(FileHandle *fh) { - return NULL; + return new AT_CellularNetwork(*_atHandlers); } CellularSMS *AT_CellularDevice::open_sms(FileHandle *fh) @@ -56,11 +75,6 @@ CellularPower *AT_CellularDevice::open_power(FileHandle *fh) return NULL; } -CellularMultiplexer *AT_CellularDevice::open_multiplexer(FileHandle *fh) -{ - return NULL; -} - CellularInformation *AT_CellularDevice::open_information(FileHandle *fh) { return NULL; @@ -82,10 +96,72 @@ void AT_CellularDevice::close_sim() { } -void AT_CellularDevice::close_multiplexer() -{ -} - void AT_CellularDevice::close_information() { } + +CellularContext *AT_CellularDevice::get_context_list() const +{ + return NULL; +} + +CellularContext *AT_CellularDevice::create_context(FileHandle *fh, const char *apn) +{ + return NULL; +} + +AT_CellularContext *AT_CellularDevice::create_context_impl(ATHandler &at, const char *apn) +{ + return NULL; +} + +void AT_CellularDevice::delete_context(CellularContext *context) +{ + +} + +AT_CellularNetwork *AT_CellularDevice::open_network_impl(ATHandler &at) +{ + return new AT_CellularNetwork(at); +} + +AT_CellularSMS *AT_CellularDevice::open_sms_impl(ATHandler &at) +{ + return NULL; +} + +AT_CellularPower *AT_CellularDevice::open_power_impl(ATHandler &at) +{ + return NULL; +} + +AT_CellularSIM *AT_CellularDevice::open_sim_impl(ATHandler &at) +{ + return NULL; +} + +AT_CellularInformation *AT_CellularDevice::open_information_impl(ATHandler &at) +{ + return NULL; +} + +void AT_CellularDevice::set_timeout(int timeout) +{ + _default_timeout = timeout; +} + +uint16_t AT_CellularDevice::get_send_delay() const +{ + return 0; +} + +void AT_CellularDevice::modem_debug_on(bool on) +{ + _modem_debug_on = on; + +} + +nsapi_error_t AT_CellularDevice::init_module() +{ + return NSAPI_ERROR_OK; +} diff --git a/UNITTESTS/stubs/AT_CellularStack_stub.cpp b/UNITTESTS/stubs/AT_CellularStack_stub.cpp index a4dd04732c..998cd20c93 100644 --- a/UNITTESTS/stubs/AT_CellularStack_stub.cpp +++ b/UNITTESTS/stubs/AT_CellularStack_stub.cpp @@ -19,9 +19,10 @@ #include "CellularUtil.h" #include "CellularLog.h" +using namespace mbed; using namespace mbed_cellular_util; -AT_CellularStack::AT_CellularStack(ATHandler &atHandler, int cid, nsapi_ip_stack_t stack_type) : _at(atHandler), _socket(NULL), _cid(cid), _stack_type(stack_type) +AT_CellularStack::AT_CellularStack(ATHandler &atHandler, int cid, nsapi_ip_stack_t stack_type) : AT_CellularBase(atHandler), _socket(NULL), _cid(cid), _stack_type(stack_type) { } @@ -29,6 +30,11 @@ AT_CellularStack::~AT_CellularStack() { } +nsapi_error_t AT_CellularStack::socket_stack_init() +{ + return NSAPI_ERROR_OK; +} + const char *AT_CellularStack::get_ip_address() { return NULL; diff --git a/UNITTESTS/stubs/CellularDevice_stub.cpp b/UNITTESTS/stubs/CellularDevice_stub.cpp index bf3040e04f..e64118f0f6 100644 --- a/UNITTESTS/stubs/CellularDevice_stub.cpp +++ b/UNITTESTS/stubs/CellularDevice_stub.cpp @@ -16,10 +16,14 @@ */ #include "CellularDevice.h" +#include "CellularDevice_stub.h" #include "EventQueue.h" #include "CellularUtil.h" -namespace mbed { +using namespace mbed; + +int CellularDevice_stub::connect_counter = -1; + MBED_WEAK CellularDevice *CellularDevice::get_default_instance() { @@ -38,7 +42,7 @@ CellularDevice::~CellularDevice() events::EventQueue *CellularDevice::get_queue() { - return NULL; + return &_queue; } void CellularDevice::set_plmn(char const *) @@ -71,9 +75,15 @@ nsapi_error_t CellularDevice::register_to_network() nsapi_error_t CellularDevice::attach_to_network() { + if (CellularDevice_stub::connect_counter == 0) { + return NSAPI_ERROR_ALREADY; + } else if (CellularDevice_stub::connect_counter == 1) { + CellularDevice_stub::connect_counter--; + return NSAPI_ERROR_IN_PROGRESS; + } else if (CellularDevice_stub::connect_counter == 2) { + CellularDevice_stub::connect_counter--; + return NSAPI_ERROR_OK; + } + return NSAPI_ERROR_OK; } - - - -} diff --git a/UNITTESTS/stubs/CellularDevice_stub.h b/UNITTESTS/stubs/CellularDevice_stub.h new file mode 100644 index 0000000000..6e865e353c --- /dev/null +++ b/UNITTESTS/stubs/CellularDevice_stub.h @@ -0,0 +1,26 @@ +/* + * 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. + */ +#ifndef CELLULARDEVICE_STUB_H_ +#define CELLULARDEVICE_STUB_H_ + + +namespace CellularDevice_stub { +extern int connect_counter; +} + + +#endif /* CELLULARDEVICE_STUB_H_ */ diff --git a/UNITTESTS/stubs/CellularStateMachine_stub.cpp b/UNITTESTS/stubs/CellularStateMachine_stub.cpp index 2f23177533..4fa4de49f1 100644 --- a/UNITTESTS/stubs/CellularStateMachine_stub.cpp +++ b/UNITTESTS/stubs/CellularStateMachine_stub.cpp @@ -15,23 +15,29 @@ * limitations under the License. */ - - -#include "CellularStateMachine.h" +#include "CellularStateMachine_stub.h" #include "CellularDevice.h" -namespace mbed { +using namespace mbed; + +nsapi_error_t CellularStateMachine_stub::nsapi_error_value = NSAPI_ERROR_OK; +CellularStubState CellularStateMachine_stub::get_current_target_state = STATE_INIT; +CellularStubState CellularStateMachine_stub::get_current_current_state = STATE_INIT; +bool CellularStateMachine_stub::bool_value = false; CellularStateMachine::CellularStateMachine(CellularDevice &device, events::EventQueue &queue) : _cellularDevice(device), _queue(queue) { - } CellularStateMachine::~CellularStateMachine() { } +void CellularStateMachine::reset() +{ +} + void CellularStateMachine::stop() { } @@ -46,21 +52,25 @@ void CellularStateMachine::set_plmn(const char *plmn) nsapi_error_t CellularStateMachine::run_to_state(CellularStateMachine::CellularState state) { - return NSAPI_ERROR_OK; + return CellularStateMachine_stub::nsapi_error_value; } bool CellularStateMachine::get_current_status(CellularStateMachine::CellularState ¤t_state, CellularStateMachine::CellularState &target_state) { - return true; + target_state = (CellularStateMachine::CellularState)CellularStateMachine_stub::get_current_target_state; + current_state = (CellularStateMachine::CellularState)CellularStateMachine_stub::get_current_current_state; + return CellularStateMachine_stub::bool_value; } nsapi_error_t CellularStateMachine::start_dispatch() { - return NSAPI_ERROR_OK; + return CellularStateMachine_stub::nsapi_error_value; } void CellularStateMachine::set_cellular_callback(mbed::Callback status_cb) { } +void CellularStateMachine::cellular_event_changed(nsapi_event_t ev, intptr_t ptr) +{ } diff --git a/UNITTESTS/stubs/CellularStateMachine_stub.h b/UNITTESTS/stubs/CellularStateMachine_stub.h new file mode 100644 index 0000000000..026829a8a1 --- /dev/null +++ b/UNITTESTS/stubs/CellularStateMachine_stub.h @@ -0,0 +1,41 @@ +/* + * 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. + */ +#ifndef CELLULARSTATEMACHINE_STUB_H_ +#define CELLULARSTATEMACHINE_STUB_H_ + +#include "CellularStateMachine.h" + +enum CellularStubState { + STATE_INIT = 0, + STATE_POWER_ON, + STATE_DEVICE_READY, + STATE_SIM_PIN, + STATE_REGISTERING_NETWORK, + STATE_MANUAL_REGISTERING_NETWORK, + STATE_ATTACHING_NETWORK, + STATE_MAX_FSM_STATE + }; + +namespace CellularStateMachine_stub { +extern nsapi_error_t nsapi_error_value; +extern CellularStubState get_current_target_state; +extern CellularStubState get_current_current_state; +extern bool bool_value; +} + + +#endif /* CELLULARSTATEMACHINE_STUB_H_ */ diff --git a/UNITTESTS/stubs/Semaphore_stub.cpp b/UNITTESTS/stubs/Semaphore_stub.cpp index ada09df114..d521371a5a 100644 --- a/UNITTESTS/stubs/Semaphore_stub.cpp +++ b/UNITTESTS/stubs/Semaphore_stub.cpp @@ -16,6 +16,9 @@ */ #include "Semaphore.h" +#include "Semaphore_stub.h" + +int Semaphore_stub::wait_return_value = 0; namespace rtos { @@ -36,12 +39,12 @@ void Semaphore::constructor(int32_t count, uint16_t max_count) int32_t Semaphore::wait(uint32_t millisec) { - return 0; + return Semaphore_stub::wait_return_value; } int32_t Semaphore::wait_until(uint64_t millisec) { - return 0; + return Semaphore_stub::wait_return_value; } osStatus Semaphore::release(void) diff --git a/UNITTESTS/stubs/Semaphore_stub.h b/UNITTESTS/stubs/Semaphore_stub.h new file mode 100644 index 0000000000..6d34bb7616 --- /dev/null +++ b/UNITTESTS/stubs/Semaphore_stub.h @@ -0,0 +1,26 @@ +/* + * 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. + */ +#ifndef MBED_OS_UNITTESTS_STUBS_SEMAPHORE_STUB_H_ +#define MBED_OS_UNITTESTS_STUBS_SEMAPHORE_STUB_H_ + +namespace Semaphore_stub { +extern int wait_return_value; +} + + + +#endif /* MBED_OS_UNITTESTS_STUBS_SEMAPHORE_STUB_H_ */ diff --git a/UNITTESTS/stubs/UARTSerial_stub.cpp b/UNITTESTS/stubs/UARTSerial_stub.cpp index ab9dfab724..40d518fe1b 100644 --- a/UNITTESTS/stubs/UARTSerial_stub.cpp +++ b/UNITTESTS/stubs/UARTSerial_stub.cpp @@ -124,4 +124,10 @@ void UARTSerial::wait_ms(uint32_t millisec) { } + +void UARTSerial::set_flow_control(mbed::SerialBase::Flow, PinName, PinName) +{ + +} + } diff --git a/UNITTESTS/target_h/PinNames.h b/UNITTESTS/target_h/PinNames.h index 01de8ced48..243c64fd14 100644 --- a/UNITTESTS/target_h/PinNames.h +++ b/UNITTESTS/target_h/PinNames.h @@ -28,6 +28,8 @@ typedef enum { } PinDirection; typedef enum { + PTC0 = 0, + PTC1 = 1, NC = (int)0xFFFFFFFF } PinName; diff --git a/UNITTESTS/target_h/myCellularDevice.h b/UNITTESTS/target_h/myCellularDevice.h new file mode 100644 index 0000000000..608ec09053 --- /dev/null +++ b/UNITTESTS/target_h/myCellularDevice.h @@ -0,0 +1,93 @@ +/* + * 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. + */ +#ifndef MY_CELLULARDEVICE_H_ +#define MY_CELLULARDEVICE_H_ + +#include "CellularDevice.h" +#include "AT_CellularNetwork.h" +#include "FileHandle_stub.h" +#include "ATHandler_stub.h" +#include "AT_CellularContext.h" + +using namespace events; + +namespace mbed { + +class CellularPower; +class CellularSMS; +class CellularSIM; +class CellularInformation; +class CellularContext; +class FileHandle; + +class myCellularDevice : public CellularDevice +{ +public: + myCellularDevice(FileHandle *fh) : CellularDevice (fh), _context_list(0), _network(0) {} + ~myCellularDevice() { + delete _context_list; + delete _network; + } + virtual CellularContext *create_context(FileHandle *fh = NULL, const char *apn = NULL) { + EventQueue que; + FileHandle_stub fh1; + ATHandler at(&fh1, que, 0, ","); + _context_list = new AT_CellularContext(at, NULL); + return _context_list;} + virtual void delete_context(CellularContext *context) { delete _context_list;} + + virtual CellularNetwork *open_network(FileHandle *fh = NULL) { + EventQueue que; + FileHandle_stub fh1; + ATHandler at(&fh1, que, 0, ","); + _network = new AT_CellularNetwork(at); + return _network;} + + virtual CellularSMS *open_sms(FileHandle *fh = NULL) {return NULL;} + + virtual CellularPower *open_power(FileHandle *fh = NULL) {return NULL;} + + virtual CellularSIM *open_sim(FileHandle *fh = NULL) {return NULL;} + + virtual CellularInformation *open_information(FileHandle *fh = NULL) {return NULL;} + + virtual void close_network(){delete _network;} + + virtual void close_sms(){} + + virtual void close_power(){} + + virtual void close_sim(){} + + virtual void close_information(){} + + virtual void set_timeout(int timeout){} + + virtual uint16_t get_send_delay() const{ return 0;} + + virtual void modem_debug_on(bool on){} + + virtual nsapi_error_t init_module(){ return 0;} + + virtual CellularContext *get_context_list() const { return _context_list;} + void cellular_callback(nsapi_event_t ev, intptr_t ptr) {CellularDevice::cellular_callback(ev, ptr);} + AT_CellularNetwork *_network; + AT_CellularContext *_context_list; +}; + +} // namespace +#endif /* MY_CELLULARDEVICE_H_ */ diff --git a/features/cellular/UNITTESTS/at/at_cellulardevice/test_at_cellulardevice.cpp b/features/cellular/UNITTESTS/at/at_cellulardevice/test_at_cellulardevice.cpp deleted file mode 100644 index aabdb717af..0000000000 --- a/features/cellular/UNITTESTS/at/at_cellulardevice/test_at_cellulardevice.cpp +++ /dev/null @@ -1,272 +0,0 @@ -/* - * 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 "CppUTest/TestHarness.h" -#include "test_at_cellulardevice.h" -#include "AT_CellularDevice.h" -#include "ATHandler_stub.h" -#include "AT_CellularBase_stub.h" -#include - -using namespace mbed; -using namespace events; - -Test_AT_CellularDevice::Test_AT_CellularDevice() -{ - -} - -Test_AT_CellularDevice::~Test_AT_CellularDevice() -{ -} - -void Test_AT_CellularDevice::test_AT_CellularDevice_constructor() -{ - EventQueue que; - AT_CellularDevice dev(que); - - CellularDevice *dev2 = new AT_CellularDevice(que); - delete dev2; -} - -void Test_AT_CellularDevice::test_AT_CellularDevice_get_at_handler() -{ - EventQueue que; - AT_CellularDevice dev(que); - FileHandle_stub fh1; - FileHandle_stub fh2; - FileHandle_stub fh3; - - CHECK(dev.open_network(&fh1)); - CHECK(dev.open_sms(&fh2)); - AT_CellularBase_stub::handler_value = AT_CellularBase_stub::handler_at_constructor_value; - CHECK(dev.open_sim(&fh3)); - ATHandler_stub::fh_value = &fh1; - CHECK(dev.open_power(&fh1)); - - ATHandler_stub::fh_value = NULL; -} - -void Test_AT_CellularDevice::test_AT_CellularDevice_open_network() -{ - EventQueue que; - AT_CellularDevice dev(que); - FileHandle_stub fh1; - - CHECK(!dev.open_network(NULL)); - CHECK(dev.open_network(&fh1)); -} - -void Test_AT_CellularDevice::test_AT_CellularDevice_open_sms() -{ - EventQueue que; - AT_CellularDevice dev(que); - FileHandle_stub fh1; - - CHECK(!dev.open_sms(NULL)); - CHECK(dev.open_sms(&fh1)); -} - -void Test_AT_CellularDevice::test_AT_CellularDevice_open_power() -{ - EventQueue que; - AT_CellularDevice dev(que); - FileHandle_stub fh1; - - CHECK(!dev.open_power(NULL)); - CHECK(dev.open_power(&fh1)); -} - -void Test_AT_CellularDevice::test_AT_CellularDevice_open_sim() -{ - EventQueue que; - AT_CellularDevice dev(que); - FileHandle_stub fh1; - - CHECK(! dev.open_sim(NULL)); - CHECK(dev.open_sim(&fh1)); -} - -void Test_AT_CellularDevice::test_AT_CellularDevice_open_information() -{ - EventQueue que; - AT_CellularDevice dev(que); - FileHandle_stub fh1; - - CHECK(!dev.open_information(NULL)); - CHECK(dev.open_information(&fh1)); -} - -void Test_AT_CellularDevice::test_AT_CellularDevice_close_network() -{ - EventQueue que; - AT_CellularDevice dev(que); - FileHandle_stub fh1; - ATHandler_stub::ref_count = 0; - - CHECK(dev.open_network(&fh1)); - AT_CellularBase_stub::handler_value = AT_CellularBase_stub::handler_at_constructor_value; - CHECK(ATHandler_stub::ref_count == 1); - - dev.close_network(); - CHECK(ATHandler_stub::ref_count == kATHandler_destructor_ref_ount); -} - -void Test_AT_CellularDevice::test_AT_CellularDevice_close_sms() -{ - EventQueue que; - AT_CellularDevice dev(que); - FileHandle_stub fh1; - ATHandler_stub::ref_count = 0; - - CHECK(dev.open_sms(&fh1)); - AT_CellularBase_stub::handler_value = AT_CellularBase_stub::handler_at_constructor_value; - CHECK(ATHandler_stub::ref_count == 1); - - dev.close_sms(); - CHECK(ATHandler_stub::ref_count == kATHandler_destructor_ref_ount); -} - -void Test_AT_CellularDevice::test_AT_CellularDevice_close_power() -{ - EventQueue que; - AT_CellularDevice dev(que); - FileHandle_stub fh1; - ATHandler_stub::ref_count = 0; - - CHECK(dev.open_power(&fh1)); - AT_CellularBase_stub::handler_value = AT_CellularBase_stub::handler_at_constructor_value; - CHECK(ATHandler_stub::ref_count == 1); - - dev.close_power(); - CHECK(ATHandler_stub::ref_count == kATHandler_destructor_ref_ount); -} - -void Test_AT_CellularDevice::test_AT_CellularDevice_close_sim() -{ - EventQueue que; - AT_CellularDevice dev(que); - FileHandle_stub fh1; - ATHandler_stub::ref_count = 0; - - - CHECK(dev.open_sim(&fh1)); - AT_CellularBase_stub::handler_value = AT_CellularBase_stub::handler_at_constructor_value; - - dev.close_sms(); // this should not affect to refcount as it's not opened - CHECK(ATHandler_stub::ref_count == 1); - - dev.close_sim(); - CHECK(ATHandler_stub::ref_count == kATHandler_destructor_ref_ount); -} - -void Test_AT_CellularDevice::test_AT_CellularDevice_close_information() -{ - EventQueue que; - AT_CellularDevice dev(que); - FileHandle_stub fh1; - ATHandler_stub::int_value = 0; - ATHandler_stub::ref_count = 0; - ATHandler_stub::fh_value = NULL; - AT_CellularBase_stub::handler_value = NULL; - - CHECK(dev.open_information(&fh1)); - CHECK(ATHandler_stub::ref_count == 1); - - // at handler is not found as it's NULL (e.g. AT_CellularBase_stub::handler_value is NULL) - dev.close_information(); - CHECK(ATHandler_stub::ref_count == 1); - - // same filehandle but different at - ATHandler_stub::fh_value = &fh1; - ATHandler at(&fh1, que, 0, ","); - AT_CellularBase_stub::handler_value = &at; - - CHECK(dev.open_information(&fh1)); - // refcount is two it's one when we create athandler and then in open_information it's incremented by one - CHECK(ATHandler_stub::ref_count == 2); - - dev.close_information(); - CHECK(ATHandler_stub::ref_count == 1); - - ATHandler_stub::fh_value = NULL; -} - -void Test_AT_CellularDevice::test_AT_CellularDevice_set_timeout() -{ - EventQueue que; - AT_CellularDevice dev(que); - FileHandle_stub fh1; - ATHandler_stub::timeout = 0; - ATHandler_stub::default_timeout = false; - - // no interfaces open so settings timeout should not change anything - dev.set_timeout(5000); - CHECK(ATHandler_stub::timeout == 0); - CHECK(ATHandler_stub::default_timeout == false); - - CHECK(dev.open_sim(&fh1)); - CHECK(ATHandler_stub::ref_count == 1); - - dev.set_timeout(5000); - CHECK(ATHandler_stub::timeout == 5000); - CHECK(ATHandler_stub::default_timeout == true); - - dev.close_sim(); -} - -void Test_AT_CellularDevice::test_AT_CellularDevice_get_send_delay() -{ - EventQueue que; - AT_CellularDevice dev(que); - CHECK(0 == dev.get_send_delay()); -} - -void Test_AT_CellularDevice::test_AT_CellularDevice_modem_debug_on() -{ - EventQueue que; - AT_CellularDevice dev(que); - FileHandle_stub fh1; - ATHandler_stub::debug_on = false; - - // no interfaces open so debug toggling should not affect - dev.modem_debug_on(true); - CHECK(ATHandler_stub::debug_on == false); - - CHECK(dev.open_sim(&fh1)); - CHECK(ATHandler_stub::ref_count == 1); - - dev.modem_debug_on(true); - CHECK(ATHandler_stub::debug_on == true); - - dev.close_sim(); -} - -void Test_AT_CellularDevice::test_AT_CellularDevice_get_stack() -{ - EventQueue que; - AT_CellularDevice dev(que); - FileHandle_stub fh1; - - NetworkStack *stack = dev.get_stack(); - CHECK(stack == NULL); - - CHECK(dev.open_network(&fh1)); - - stack = dev.get_stack(); - CHECK(stack == NULL); // Not in PPP so also null but this is got from the network class -} - diff --git a/features/cellular/UNITTESTS/stubs/ATHandler_stub.cpp b/features/cellular/UNITTESTS/stubs/ATHandler_stub.cpp deleted file mode 100644 index 5d8a906209..0000000000 --- a/features/cellular/UNITTESTS/stubs/ATHandler_stub.cpp +++ /dev/null @@ -1,297 +0,0 @@ -/* - * Copyright (c) 2017, 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 -#include "nsapi_types.h" -#include "ATHandler.h" -#include "EventQueue.h" -#include "ATHandler_stub.h" - -using namespace mbed; -using namespace events; - -#include "CellularLog.h" - -const int DEFAULT_AT_TIMEOUT = 1000; // at default timeout in milliseconds - -nsapi_error_t ATHandler_stub::nsapi_error_value = 0; -uint8_t ATHandler_stub::nsapi_error_ok_counter = 0; -int ATHandler_stub::int_value = -1; -int ATHandler_stub::ref_count = 0; -int ATHandler_stub::timeout = 0; -bool ATHandler_stub::default_timeout = 0; -bool ATHandler_stub::debug_on = 0; -ssize_t ATHandler_stub::ssize_value = 0; -char *ATHandler_stub::read_string_value = NULL; -size_t ATHandler_stub::size_value = 0; -size_t ATHandler_stub::return_given_size = false; -bool ATHandler_stub::bool_value = false; -uint8_t ATHandler_stub::uint8_value = 0; -FileHandle_stub *ATHandler_stub::fh_value = NULL; -device_err_t ATHandler_stub::device_err_value; -Callback ATHandler_stub::callback = NULL; -uint8_t ATHandler_stub::resp_info_true_counter = false; -uint8_t ATHandler_stub::info_elem_true_counter = false; -int ATHandler_stub::int_valid_count_table[kRead_int_table_size]; -int ATHandler_stub::int_count = kRead_int_table_size; - -int ATHandler_stub::read_string_index = kRead_string_table_size; -char *ATHandler_stub::read_string_table[kRead_string_table_size]; -int ATHandler_stub::resp_stop_success_count = kResp_stop_count_default; - -ATHandler::ATHandler(FileHandle *fh, EventQueue &queue, int timeout, const char *output_delimiter, uint16_t send_delay) : - _nextATHandler(0), - _fileHandle(fh), - _queue(queue) -{ - ATHandler_stub::ref_count = 1; -} - -void ATHandler::set_debug(bool debug_on) -{ - ATHandler_stub::debug_on = debug_on; -} - -ATHandler::~ATHandler() -{ - ATHandler_stub::ref_count = kATHandler_destructor_ref_ount; -} - -void ATHandler::inc_ref_count() -{ - ATHandler_stub::ref_count++; -} - -void ATHandler::dec_ref_count() -{ - ATHandler_stub::ref_count--; -} - -int ATHandler::get_ref_count() -{ - return ATHandler_stub::ref_count; -} - -FileHandle *ATHandler::get_file_handle() -{ - return ATHandler_stub::fh_value; -} - -void ATHandler::set_file_handle(FileHandle *fh) -{ -} - -nsapi_error_t ATHandler::set_urc_handler(const char *urc, mbed::Callback cb) -{ - ATHandler_stub::callback = cb; - return ATHandler_stub::nsapi_error_value; -} - -void ATHandler::remove_urc_handler(const char *prefix, mbed::Callback callback) -{ -} - -nsapi_error_t ATHandler::get_last_error() const -{ - if (ATHandler_stub::nsapi_error_ok_counter) { - ATHandler_stub::nsapi_error_ok_counter--; - return NSAPI_ERROR_OK; - } - return ATHandler_stub::nsapi_error_value; -} - -void ATHandler::lock() -{ -} - -void ATHandler::unlock() -{ -} - -nsapi_error_t ATHandler::unlock_return_error() -{ - return ATHandler_stub::nsapi_error_value; -} - -void ATHandler::set_at_timeout(uint32_t timeout_milliseconds, bool default_timeout) -{ - ATHandler_stub::timeout = timeout_milliseconds; - ATHandler_stub::default_timeout = default_timeout; -} - -void ATHandler::restore_at_timeout() -{ -} - -void ATHandler::process_oob() -{ -} - -void ATHandler::clear_error() -{ - ATHandler_stub::nsapi_error_ok_counter++; -} - -void ATHandler::skip_param(uint32_t count) -{ - -} - -void ATHandler::skip_param(ssize_t len, uint32_t count) -{ -} - -ssize_t ATHandler::read_bytes(uint8_t *buf, size_t len) -{ - return ATHandler_stub::ssize_value; -} - -ssize_t ATHandler::read_string(char *buf, size_t size, bool read_even_stop_tag) -{ - - if (ATHandler_stub::read_string_index == kRead_string_table_size) { - if (ATHandler_stub::read_string_value && ATHandler_stub::ssize_value >= 0) { - memcpy(buf, ATHandler_stub::read_string_value, ATHandler_stub::ssize_value + 1); - } - return ATHandler_stub::ssize_value; - } - - ATHandler_stub::read_string_index--; - if (ATHandler_stub::read_string_index >= 0) { - char *tmp = ATHandler_stub::read_string_table[ATHandler_stub::read_string_index]; - ssize_t len = strlen(tmp); - memcpy(buf, tmp, len + 1); - return len; - } - - ATHandler_stub::nsapi_error_value = NSAPI_ERROR_DEVICE_ERROR; - return -1; -} - -int32_t ATHandler::read_int() -{ - if (ATHandler_stub::nsapi_error_value != NSAPI_ERROR_OK) { - return -1; - } - - if (ATHandler_stub::int_count == kRead_int_table_size) { - return ATHandler_stub::int_value; - } - - //printf("ATHandler_stub::int_count: %d", ATHandler_stub::int_count); - ATHandler_stub::int_count--; - if (ATHandler_stub::int_count < kRead_int_table_size && ATHandler_stub::int_count >= 0) { - return ATHandler_stub::int_valid_count_table[ATHandler_stub::int_count]; - } - - ATHandler_stub::nsapi_error_value = NSAPI_ERROR_DEVICE_ERROR; - return -1; -} - -void ATHandler::set_delimiter(char delimiter) -{ -} - -void ATHandler::set_default_delimiter() -{ -} - -void ATHandler::set_stop_tag(const char *stop_tag_seq) -{ -} - -int ATHandler::get_3gpp_error() -{ - return ATHandler_stub::int_value; -} - -void ATHandler::resp_start(const char *prefix, bool stop) -{ -} - -bool ATHandler::info_resp() -{ - if (ATHandler_stub::resp_info_true_counter) { - ATHandler_stub::resp_info_true_counter--; - return true; - } - return ATHandler_stub::bool_value; -} - -bool ATHandler::info_elem(char start_tag) -{ - if (ATHandler_stub::info_elem_true_counter) { - ATHandler_stub::info_elem_true_counter--; - return true; - } - return ATHandler_stub::bool_value; -} - -bool ATHandler::consume_to_stop_tag() -{ - return ATHandler_stub::bool_value; -} - -void ATHandler::resp_stop() -{ - if (ATHandler_stub::resp_stop_success_count > 0) { - ATHandler_stub::resp_stop_success_count--; - return; - } - ATHandler_stub::nsapi_error_value = NSAPI_ERROR_DEVICE_ERROR; -} - -void ATHandler::cmd_start(const char *cmd) -{ -} - -void ATHandler::write_int(int32_t param) -{ -} - -void ATHandler::write_string(const char *param, bool useQuotations) -{ -} - -size_t ATHandler::write_bytes(const uint8_t *param, size_t len) -{ - if (ATHandler_stub::return_given_size) { - return len; - } - return ATHandler_stub::size_value; -} - -void ATHandler::cmd_stop() -{ -} - -void ATHandler::cmd_stop_read_resp() -{ - cmd_stop(); - resp_start(); - resp_stop(); -} - -device_err_t ATHandler::get_last_device_error() const -{ - return ATHandler_stub::device_err_value; -} - -void ATHandler::flush() -{ - -} diff --git a/features/cellular/UNITTESTS/stubs/ATHandler_stub.h b/features/cellular/UNITTESTS/stubs/ATHandler_stub.h deleted file mode 100644 index 5cb1a772f8..0000000000 --- a/features/cellular/UNITTESTS/stubs/ATHandler_stub.h +++ /dev/null @@ -1,56 +0,0 @@ -/* - * Copyright (c) 2017, 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 "stdint.h" -#include "stdbool.h" -#include -#include "nsapi_types.h" -#include "ATHandler.h" -#include "FileHandle_stub.h" -#include "Callback.h" - -static const int kRead_string_table_size = 100; -static const int kRead_int_table_size = 100; -static const int kResp_stop_count_default = 100; -// set reference count to -909 to separate it from zero so we can test that ATHandler is really deleted. -static const int kATHandler_destructor_ref_ount = -909; - -namespace ATHandler_stub { -extern nsapi_error_t nsapi_error_value; -extern uint8_t nsapi_error_ok_counter; -extern int int_value; -extern int ref_count; -extern int timeout; -extern bool default_timeout; -extern bool debug_on; -extern ssize_t ssize_value; -extern char *read_string_value; -extern size_t size_value; -extern size_t return_given_size; -extern bool bool_value; -extern uint8_t resp_info_true_counter; -extern uint8_t info_elem_true_counter; -extern uint8_t uint8_value; -extern mbed::FileHandle_stub *fh_value; -extern mbed::device_err_t device_err_value; -extern mbed::Callback callback; -extern char *read_string_table[kRead_string_table_size]; -extern int read_string_index; -extern int int_valid_count_table[kRead_int_table_size]; -extern int int_count; -extern int resp_stop_success_count; -} diff --git a/features/cellular/framework/device/CellularDevice.cpp b/features/cellular/framework/device/CellularDevice.cpp index 6adfce2aa1..7ebd1f14ef 100644 --- a/features/cellular/framework/device/CellularDevice.cpp +++ b/features/cellular/framework/device/CellularDevice.cpp @@ -119,12 +119,17 @@ nsapi_error_t CellularDevice::attach_to_network() nsapi_error_t CellularDevice::create_state_machine() { + nsapi_error_t err = NSAPI_ERROR_OK; if (!_state_machine) { _state_machine = new CellularStateMachine(*this, *get_queue()); _state_machine->set_cellular_callback(callback(this, &CellularDevice::cellular_callback)); - return _state_machine->start_dispatch(); + err = _state_machine->start_dispatch(); + if (err) { + delete _state_machine; + _state_machine = NULL; + } } - return NSAPI_ERROR_OK; + return err; } nsapi_error_t CellularDevice::start_state_machine(CellularStateMachine::CellularState target_state)