diff --git a/UNITTESTS/features/cellular/framework/AT/at_cellularbase/at_cellularbasetest.cpp b/UNITTESTS/features/cellular/framework/AT/at_cellularbase/at_cellularbasetest.cpp index 3537aa9c8a8..3296cde7ab1 100644 --- a/UNITTESTS/features/cellular/framework/AT/at_cellularbase/at_cellularbasetest.cpp +++ b/UNITTESTS/features/cellular/framework/AT/at_cellularbase/at_cellularbasetest.cpp @@ -18,7 +18,7 @@ #include "gtest/gtest.h" #include "AT_CellularBase.h" #include "EventQueue.h" -#include "AT_CellularBase.h" +#include "AT_CellularNetwork.h" #include "ATHandler_stub.h" #include "FileHandle_stub.h" #include @@ -33,28 +33,25 @@ class my_base : public AT_CellularBase { } bool check_not_supported() { - static const AT_CellularBase::SupportedFeature unsupported_features[] = { - AT_CellularBase::AT_CGSN_WITH_TYPE, - AT_CellularBase::SUPPORTED_FEATURE_END_MARK + static const intptr_t cellular_properties[AT_CellularBase::PROPERTY_MAX] = { + AT_CellularNetwork::RegistrationModeDisable,// C_EREG + AT_CellularNetwork::RegistrationModeLAC, // C_GREG + AT_CellularNetwork::RegistrationModeLAC, // C_REG + 0, // AT_CGSN_WITH_TYPE + 1, // AT_CGDATA + 1, // AT_CGAUTH + 1, // PROPERTY_IPV4_STACK + 0, // PROPERTY_IPV6_STACK + 0, // PROPERTY_IPV4V6_STACK }; - set_unsupported_features(unsupported_features); - return is_supported(AT_CGSN_WITH_TYPE); - } - bool check_supported() - { - set_unsupported_features(NULL); - return is_supported(AT_CGSN_WITH_TYPE); + set_cellular_properties(cellular_properties); + return get_property(PROPERTY_AT_CGSN_WITH_TYPE); } - bool check_supported_not_found() + bool check_supported() { - static const AT_CellularBase::SupportedFeature unsupported_features[] = { - AT_CellularBase::AT_CGSN_WITH_TYPE, - AT_CellularBase::SUPPORTED_FEATURE_END_MARK - }; - set_unsupported_features(unsupported_features); - return is_supported(SUPPORTED_FEATURE_END_MARK); + return get_property(PROPERTY_AT_CGDATA); } }; @@ -109,19 +106,25 @@ TEST_F(TestAT_CellularBase, test_AT_CellularBase_get_device_error) ATHandler_stub::device_err_value.errCode = 0; } -TEST_F(TestAT_CellularBase, test_AT_CellularBase_set_unsupported_features) +TEST_F(TestAT_CellularBase, test_AT_CellularBase_set_cellular_properties) { EventQueue eq; FileHandle_stub fh; ATHandler ah(&fh, eq, 0, ","); AT_CellularBase at(ah); - static const AT_CellularBase::SupportedFeature unsupported_features[] = { - AT_CellularBase::AT_CGSN_WITH_TYPE, - AT_CellularBase::SUPPORTED_FEATURE_END_MARK + static const intptr_t cellular_properties[AT_CellularBase::PROPERTY_MAX] = { + AT_CellularNetwork::RegistrationModeDisable,// C_EREG + AT_CellularNetwork::RegistrationModeLAC, // C_GREG + AT_CellularNetwork::RegistrationModeLAC, // C_REG + 0, // AT_CGSN_WITH_TYPE + 1, // AT_CGDATA + 1, // AT_CGAUTH + 1, // PROPERTY_IPV4_STACK + 0, // PROPERTY_IPV6_STACK + 0, // PROPERTY_IPV4V6_STACK }; - - at.set_unsupported_features(unsupported_features); + at.set_cellular_properties(cellular_properties); } TEST_F(TestAT_CellularBase, test_AT_CellularBase_is_supported) @@ -131,7 +134,6 @@ TEST_F(TestAT_CellularBase, test_AT_CellularBase_is_supported) ATHandler ah(&fh, eq, 0, ","); my_base my_at(ah); - EXPECT_TRUE(true == my_at.check_supported()); - EXPECT_TRUE(true == my_at.check_supported_not_found()); - EXPECT_TRUE(false == my_at.check_not_supported()); + EXPECT_EQ(true, my_at.check_supported()); + EXPECT_EQ(false, my_at.check_not_supported()); } 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 4d7b80cb98a..f592013ab88 100644 --- a/UNITTESTS/features/cellular/framework/AT/at_cellularcontext/at_cellularcontexttest.cpp +++ b/UNITTESTS/features/cellular/framework/AT/at_cellularcontext/at_cellularcontexttest.cpp @@ -27,7 +27,7 @@ #include "Semaphore_stub.h" #include "CellularDevice_stub.h" #include "equeue_stub.h" -#include "CellularSIM.h" +#include "AT_CellularBase_stub.h" using namespace mbed; using namespace events; @@ -115,12 +115,12 @@ class my_stack : public AT_CellularStack { 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), _st(at) {} - virtual ~my_AT_CTX() {} - virtual bool stack_type_supported(nsapi_ip_stack_t stack_type) + AT_CellularContext(at, device, apn), _st(at) { - return false; + AT_CellularBase_stub::supported_bool = false; } + virtual ~my_AT_CTX() {} + virtual NetworkStack *get_stack() { return &_st; @@ -528,7 +528,7 @@ TEST_F(TestAT_CellularContext, 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; + data.status_data = CellularDevice::SimStateReady; ctx.cellular_callback((nsapi_event_t)CellularSIMStatusChanged, (intptr_t)&data); } diff --git a/UNITTESTS/features/cellular/framework/AT/at_cellularcontext/unittest.cmake b/UNITTESTS/features/cellular/framework/AT/at_cellularcontext/unittest.cmake index 1d9c56320ae..2b6bb729196 100644 --- a/UNITTESTS/features/cellular/framework/AT/at_cellularcontext/unittest.cmake +++ b/UNITTESTS/features/cellular/framework/AT/at_cellularcontext/unittest.cmake @@ -9,6 +9,7 @@ set(unittest-includes ${unittest-includes} ../features/cellular/framework/common ../features/cellular/framework/AT ../features/cellular/framework/device + ../features/netsocket/cellular ) # Source files @@ -36,4 +37,6 @@ set(unittest-test-sources stubs/randLIB_stub.cpp stubs/Semaphore_stub.cpp stubs/us_ticker_stub.cpp + stubs/UARTSerial_stub.cpp + stubs/SerialBase_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 8609e8a38ad..407bdb2cc3a 100644 --- a/UNITTESTS/features/cellular/framework/AT/at_cellulardevice/at_cellulardevicetest.cpp +++ b/UNITTESTS/features/cellular/framework/AT/at_cellulardevice/at_cellulardevicetest.cpp @@ -56,9 +56,7 @@ TEST_F(TestAT_CellularDevice, test_AT_CellularDevice_get_at_handler) EXPECT_TRUE(dev.open_network(&fh1)); EXPECT_TRUE(dev.open_sms(&fh2)); AT_CellularBase_stub::handler_value = AT_CellularBase_stub::handler_at_constructor_value; - EXPECT_TRUE(dev.open_sim(&fh3)); - ATHandler_stub::fh_value = &fh1; - EXPECT_TRUE(dev.open_power(&fh1)); + EXPECT_TRUE(dev.open_information(&fh3)); ATHandler_stub::fh_value = NULL; } @@ -89,32 +87,6 @@ TEST_F(TestAT_CellularDevice, test_AT_CellularDevice_open_sms) EXPECT_TRUE(sms1 == sms); } -TEST_F(TestAT_CellularDevice, test_AT_CellularDevice_open_power) -{ - FileHandle_stub fh1; - AT_CellularDevice dev(&fh1); - - CellularPower *pwr = dev.open_power(NULL); - CellularPower *pwr1 = dev.open_power(&fh1); - - EXPECT_TRUE(pwr); - EXPECT_TRUE(pwr1); - EXPECT_TRUE(pwr1 == pwr); -} - -TEST_F(TestAT_CellularDevice, test_AT_CellularDevice_open_sim) -{ - FileHandle_stub fh1; - AT_CellularDevice dev(&fh1); - - CellularSIM *sim = dev.open_sim(NULL); - CellularSIM *sim1 = dev.open_sim(&fh1); - - EXPECT_TRUE(sim); - EXPECT_TRUE(sim1); - EXPECT_TRUE(sim1 == sim); -} - TEST_F(TestAT_CellularDevice, test_AT_CellularDevice_open_information) { FileHandle_stub fh1; @@ -156,40 +128,6 @@ TEST_F(TestAT_CellularDevice, test_AT_CellularDevice_close_sms) EXPECT_TRUE(ATHandler_stub::ref_count == kATHandler_destructor_ref_ount); } -TEST_F(TestAT_CellularDevice, test_AT_CellularDevice_close_power) -{ - FileHandle_stub fh1; - AT_CellularDevice dev(&fh1); - ATHandler_stub::ref_count = 0; - - EXPECT_TRUE(dev.open_power(&fh1)); - AT_CellularBase_stub::handler_value = AT_CellularBase_stub::handler_at_constructor_value; - EXPECT_TRUE(ATHandler_stub::ref_count == 1); - - dev.close_power(); - EXPECT_TRUE(ATHandler_stub::ref_count == kATHandler_destructor_ref_ount); -} - -TEST_F(TestAT_CellularDevice, test_AT_CellularDevice_close_sim) -{ - FileHandle_stub fh1; - AT_CellularDevice dev(&fh1); - ATHandler_stub::ref_count = 0; - int ana = 0; - - EXPECT_TRUE(dev.open_sim(&fh1)); - AT_CellularBase_stub::handler_value = AT_CellularBase_stub::handler_at_constructor_value; - - ana = ATHandler_stub::ref_count; - - dev.close_sms(); // this should not affect to refcount as it's not opened - EXPECT_TRUE(ATHandler_stub::ref_count == 1); - ana = ATHandler_stub::ref_count; - - dev.close_sim(); - EXPECT_TRUE(ATHandler_stub::ref_count == kATHandler_destructor_ref_ount); -} - TEST_F(TestAT_CellularDevice, test_AT_CellularDevice_close_information) { FileHandle_stub fh1; @@ -223,19 +161,18 @@ TEST_F(TestAT_CellularDevice, test_AT_CellularDevice_set_timeout) ATHandler_stub::timeout = 0; ATHandler_stub::default_timeout = false; - // no interfaces open so settings timeout should not change anything dev.set_timeout(5000); - EXPECT_TRUE(ATHandler_stub::timeout == 0); - EXPECT_TRUE(ATHandler_stub::default_timeout == false); + EXPECT_TRUE(ATHandler_stub::timeout == 5000); + EXPECT_TRUE(ATHandler_stub::default_timeout == true); - EXPECT_TRUE(dev.open_sim(&fh1)); + EXPECT_TRUE(dev.open_sms(&fh1)); EXPECT_TRUE(ATHandler_stub::ref_count == 1); dev.set_timeout(5000); EXPECT_TRUE(ATHandler_stub::timeout == 5000); EXPECT_TRUE(ATHandler_stub::default_timeout == true); - dev.close_sim(); + dev.close_sms(); } TEST_F(TestAT_CellularDevice, test_AT_CellularDevice_modem_debug_on) @@ -244,31 +181,76 @@ TEST_F(TestAT_CellularDevice, test_AT_CellularDevice_modem_debug_on) AT_CellularDevice dev(&fh1); ATHandler_stub::debug_on = false; - // no interfaces open so debug toggling should not affect dev.modem_debug_on(true); - EXPECT_TRUE(ATHandler_stub::debug_on == false); + EXPECT_TRUE(ATHandler_stub::debug_on == true); - EXPECT_TRUE(dev.open_sim(&fh1)); + EXPECT_TRUE(dev.open_sms(&fh1)); EXPECT_TRUE(ATHandler_stub::ref_count == 1); dev.modem_debug_on(true); EXPECT_TRUE(ATHandler_stub::debug_on == true); - dev.close_sim(); + dev.close_sms(); } -TEST_F(TestAT_CellularDevice, test_AT_CellularDevice_get_send_delay) +TEST_F(TestAT_CellularDevice, test_AT_CellularDevice_init) { FileHandle_stub fh1; AT_CellularDevice dev(&fh1); - EXPECT_TRUE(0 == dev.get_send_delay()); + EXPECT_EQ(dev.init(), NSAPI_ERROR_OK); } -TEST_F(TestAT_CellularDevice, test_AT_CellularDevice_init_module) +TEST_F(TestAT_CellularDevice, test_AT_CellularDevice_shutdown) { FileHandle_stub fh1; AT_CellularDevice dev(&fh1); - EXPECT_TRUE(NSAPI_ERROR_OK == dev.init_module()); + EXPECT_EQ(dev.shutdown(), NSAPI_ERROR_OK); +} + +TEST_F(TestAT_CellularDevice, test_AT_CellularDevice_is_ready) +{ + EventQueue que; + FileHandle_stub fh1; + ATHandler at(&fh1, que, 0, ","); + + AT_CellularDevice dev(&fh1); + ATHandler_stub::nsapi_error_value = NSAPI_ERROR_OK; + EXPECT_TRUE(NSAPI_ERROR_OK == dev.is_ready()); +} + +TEST_F(TestAT_CellularDevice, test_AT_CellularDevice_set_power_save_mode) +{ + EventQueue que; + FileHandle_stub fh1; + ATHandler at(&fh1, que, 0, ","); + + AT_CellularDevice dev(&fh1); + ATHandler_stub::nsapi_error_value = NSAPI_ERROR_OK; + EXPECT_TRUE(NSAPI_ERROR_OK == dev.set_power_save_mode(0, 0)); + + EXPECT_TRUE(NSAPI_ERROR_OK == dev.set_power_save_mode(10, 0)); + + EXPECT_TRUE(NSAPI_ERROR_OK == dev.set_power_save_mode(912, 0)); + + EXPECT_TRUE(NSAPI_ERROR_OK == dev.set_power_save_mode(1834, 1834)); + + EXPECT_TRUE(NSAPI_ERROR_OK == dev.set_power_save_mode(18345, 18345)); + + EXPECT_TRUE(NSAPI_ERROR_OK == dev.set_power_save_mode(101234, 101234)); + + EXPECT_TRUE(NSAPI_ERROR_OK == dev.set_power_save_mode(1012345, 1012345)); + + EXPECT_TRUE(NSAPI_ERROR_OK == dev.set_power_save_mode(39612345, 39612345)); + + ATHandler_stub::nsapi_error_value = NSAPI_ERROR_DEVICE_ERROR; + EXPECT_TRUE(NSAPI_ERROR_DEVICE_ERROR == dev.set_power_save_mode(0)); +} + +TEST_F(TestAT_CellularDevice, test_AT_CellularDevice_get_send_delay) +{ + FileHandle_stub fh1; + AT_CellularDevice dev(&fh1); + EXPECT_TRUE(0 == dev.get_send_delay()); } TEST_F(TestAT_CellularDevice, test_AT_CellularDevice_create_delete_context) @@ -307,3 +289,78 @@ TEST_F(TestAT_CellularDevice, test_AT_CellularDevice_create_delete_context) delete dev; } + +TEST_F(TestAT_CellularDevice, TestAT_CellularDevice_set_pin) +{ + FileHandle_stub fh1; + AT_CellularDevice *dev = new AT_CellularDevice(&fh1); + + ATHandler_stub::nsapi_error_value = NSAPI_ERROR_OK; + ASSERT_EQ(NSAPI_ERROR_OK, dev->set_pin("12")); + + ATHandler_stub::nsapi_error_value = NSAPI_ERROR_DEVICE_ERROR; + ASSERT_EQ(NSAPI_ERROR_DEVICE_ERROR, dev->set_pin("12")); + + ATHandler_stub::nsapi_error_value = NSAPI_ERROR_OK; + ATHandler_stub::read_string_value = (char *)"SIM PIN"; + ATHandler_stub::ssize_value = 7; + ASSERT_EQ(NSAPI_ERROR_PARAMETER, dev->set_pin(NULL)); + + ATHandler_stub::nsapi_error_value = NSAPI_ERROR_OK; + ATHandler_stub::read_string_value = (char *)"READY"; + ATHandler_stub::ssize_value = 5; + ASSERT_EQ(NSAPI_ERROR_OK, dev->set_pin("12")); + + ASSERT_EQ(NSAPI_ERROR_OK, dev->set_pin(NULL)); + + delete dev; +} + +TEST_F(TestAT_CellularDevice, TestAT_CellularDevice_get_sim_state) +{ + FileHandle_stub fh1; + AT_CellularDevice *dev = new AT_CellularDevice(&fh1); + + CellularDevice::SimState state; + ATHandler_stub::nsapi_error_value = NSAPI_ERROR_OK; + ATHandler_stub::ssize_value = -1; + ATHandler_stub::read_string_value = NULL; + ASSERT_EQ(NSAPI_ERROR_OK, dev->get_sim_state(state)); + ASSERT_EQ(CellularDevice::SimStateUnknown, state); + + ATHandler_stub::read_string_value = (char *)"READY"; + ATHandler_stub::ssize_value = 5; + ASSERT_EQ(NSAPI_ERROR_OK, dev->get_sim_state(state)); + ASSERT_EQ(CellularDevice::SimStateReady, state); + + ATHandler_stub::read_string_value = (char *)"SIM PIN"; + ATHandler_stub::ssize_value = 7; + ASSERT_EQ(NSAPI_ERROR_OK, dev->get_sim_state(state)); + ASSERT_EQ(CellularDevice::SimStatePinNeeded, state); + + ATHandler_stub::read_string_value = (char *)"SIM PUK"; + ATHandler_stub::ssize_value = 7; + ASSERT_EQ(NSAPI_ERROR_OK, dev->get_sim_state(state)); + ASSERT_EQ(CellularDevice::SimStatePukNeeded, state); + + ATHandler_stub::read_string_value = (char *)"SOME CRAP"; + ATHandler_stub::ssize_value = 9; + ASSERT_EQ(NSAPI_ERROR_OK, dev->get_sim_state(state)); + ASSERT_EQ(CellularDevice::SimStateUnknown, state); + + delete dev; +} + +static void device_ready_cb() +{ +} + +TEST_F(TestAT_CellularDevice, test_AT_CellularDevice_set_ready_cb) +{ + EventQueue que; + FileHandle_stub fh1; + AT_CellularDevice *dev = new AT_CellularDevice(&fh1); + + dev->set_ready_cb(&device_ready_cb); + dev->set_ready_cb(0); +} diff --git a/UNITTESTS/features/cellular/framework/AT/at_cellulardevice/unittest.cmake b/UNITTESTS/features/cellular/framework/AT/at_cellulardevice/unittest.cmake index 0897cb4a9ef..7033d669aab 100644 --- a/UNITTESTS/features/cellular/framework/AT/at_cellulardevice/unittest.cmake +++ b/UNITTESTS/features/cellular/framework/AT/at_cellulardevice/unittest.cmake @@ -12,6 +12,7 @@ set(unittest-includes ${unittest-includes} ../features/frameworks/mbed-client-randlib/mbed-client-randlib ../drivers ../hal + ../features/netsocket/cellular ) # Source files @@ -26,8 +27,6 @@ set(unittest-test-sources stubs/AT_CellularNetwork_stub.cpp stubs/ATHandler_stub.cpp stubs/AT_CellularSMS_stub.cpp - stubs/AT_CellularSIM_stub.cpp - stubs/AT_CellularPower_stub.cpp stubs/AT_CellularInformation_stub.cpp stubs/CellularUtil_stub.cpp stubs/AT_CellularBase_stub.cpp diff --git a/UNITTESTS/features/cellular/framework/AT/at_cellularinformation/at_cellularinformationtest.cpp b/UNITTESTS/features/cellular/framework/AT/at_cellularinformation/at_cellularinformationtest.cpp index 0a6d649e746..883bffd00cf 100644 --- a/UNITTESTS/features/cellular/framework/AT/at_cellularinformation/at_cellularinformationtest.cpp +++ b/UNITTESTS/features/cellular/framework/AT/at_cellularinformation/at_cellularinformationtest.cpp @@ -34,6 +34,10 @@ class TestAT_CellularInformation : public testing::Test { void SetUp() { + ATHandler_stub::read_string_index = kRead_string_table_size; + ATHandler_stub::nsapi_error_value = NSAPI_ERROR_OK; + ATHandler_stub::read_string_value = NULL; + ATHandler_stub::ssize_value = 0; } void TearDown() @@ -118,6 +122,8 @@ TEST_F(TestAT_CellularInformation, test_AT_CellularInformation_get_revision) EXPECT_TRUE(NSAPI_ERROR_DEVICE_ERROR == aci->get_revision(buf, 8)); EXPECT_TRUE(strlen(buf) == 0); + EXPECT_TRUE(NSAPI_ERROR_PARAMETER == aci->get_revision(NULL, 8)); + EXPECT_TRUE(NSAPI_ERROR_PARAMETER == aci->get_revision(buf, 0)); delete aci; } @@ -152,4 +158,59 @@ TEST_F(TestAT_CellularInformation, test_AT_CellularInformation_get_serial_number AT_CellularBase_stub::supported_bool = true; EXPECT_TRUE(NSAPI_ERROR_OK == aci.get_serial_number(buf, 8, CellularInformation::IMEI)); EXPECT_TRUE(strcmp("1234567", buf) == 0); + + EXPECT_TRUE(NSAPI_ERROR_PARAMETER == aci.get_serial_number(NULL, 8, CellularInformation::IMEI)); + EXPECT_TRUE(NSAPI_ERROR_PARAMETER == aci.get_serial_number(buf, 0, CellularInformation::IMEI)); +} + +TEST_F(TestAT_CellularInformation, TestAT_CellularInformation_get_imsi) +{ + EventQueue eq; + FileHandle_stub fh; + ATHandler ah(&fh, eq, 0, ","); + AT_CellularInformation aci(ah); + + char imsi[16]; + ATHandler_stub::nsapi_error_value = NSAPI_ERROR_OK; + ATHandler_stub::read_string_value = (char *)"123456789012345"; + ATHandler_stub::ssize_value = 15; + ASSERT_EQ(NSAPI_ERROR_OK, aci.get_imsi(imsi, sizeof(imsi))); + ASSERT_STREQ(ATHandler_stub::read_string_value, imsi); + + ATHandler_stub::read_string_value = NULL; + ATHandler_stub::ssize_value = -1; + ATHandler_stub::read_string_index = -1; + imsi[0] = 0; + ASSERT_EQ(NSAPI_ERROR_DEVICE_ERROR, aci.get_imsi(imsi, sizeof(imsi))); + ASSERT_EQ(strlen(imsi), 0); + + ASSERT_EQ(NSAPI_ERROR_PARAMETER, aci.get_imsi(NULL, sizeof(imsi))); + + char imsi2[5]; + ASSERT_EQ(NSAPI_ERROR_PARAMETER, aci.get_imsi(imsi2, sizeof(imsi2))); +} + +TEST_F(TestAT_CellularInformation, TestAT_CellularInformation_get_iccid) +{ + EventQueue eq; + FileHandle_stub fh; + ATHandler ah(&fh, eq, 0, ","); + AT_CellularInformation aci(ah); + + char buf[16]; + ATHandler_stub::nsapi_error_value = NSAPI_ERROR_OK; + ATHandler_stub::read_string_value = (char *)"123456789012345"; + ATHandler_stub::ssize_value = 15; + ASSERT_EQ(NSAPI_ERROR_OK, aci.get_iccid(buf, 16)); + ASSERT_STREQ(ATHandler_stub::read_string_value, buf); + + buf[0] = 0; + ATHandler_stub::nsapi_error_value = NSAPI_ERROR_DEVICE_ERROR; + ATHandler_stub::read_string_value = NULL; + ATHandler_stub::ssize_value = -1; + ASSERT_EQ(NSAPI_ERROR_DEVICE_ERROR, aci.get_iccid(buf, 16)); + ASSERT_EQ(strlen(buf), 0); + + ASSERT_EQ(NSAPI_ERROR_PARAMETER, aci.get_iccid(buf, 0)); + ASSERT_EQ(NSAPI_ERROR_PARAMETER, aci.get_iccid(NULL, 16)); } diff --git a/UNITTESTS/features/cellular/framework/AT/at_cellularnetwork/at_cellularnetworktest.cpp b/UNITTESTS/features/cellular/framework/AT/at_cellularnetwork/at_cellularnetworktest.cpp index 9609ab39358..d79bf9e5e96 100644 --- a/UNITTESTS/features/cellular/framework/AT/at_cellularnetwork/at_cellularnetworktest.cpp +++ b/UNITTESTS/features/cellular/framework/AT/at_cellularnetwork/at_cellularnetworktest.cpp @@ -52,13 +52,6 @@ class my_AT_CN : public AT_CellularNetwork { public: my_AT_CN(ATHandler &atHandler) : AT_CellularNetwork(atHandler) {} virtual ~my_AT_CN() {} - virtual AT_CellularNetwork::RegistrationMode has_registration(RegistrationType reg_type) - { - if (reg_type == C_GREG) { - return RegistrationModeDisable; - } - return RegistrationModeEnable; - } virtual nsapi_error_t set_access_technology_impl(RadioAccessTechnology op_rat) { return NSAPI_ERROR_OK; @@ -69,13 +62,6 @@ class my_AT_CNipv6 : public AT_CellularNetwork { public: my_AT_CNipv6(ATHandler &atHandler) : AT_CellularNetwork(atHandler) {} virtual ~my_AT_CNipv6() {} - virtual AT_CellularNetwork::RegistrationMode has_registration(RegistrationType reg_type) - { - if (reg_type == C_GREG) { - return RegistrationModeDisable; - } - return RegistrationModeEnable; - } virtual nsapi_error_t set_access_technology_impl(RadioAccessTechnology op_rat) { return NSAPI_ERROR_OK; @@ -150,32 +136,32 @@ TEST_F(TestAT_CellularNetwork, test_AT_CellularNetwork_get_registration_params) ATHandler_stub::read_string_value = NULL; ATHandler_stub::ssize_value = 0; // Check get_registration_params without specifying the registration type - EXPECT_TRUE(NSAPI_ERROR_OK == cn.get_registration_params(reg_params_check)); - EXPECT_TRUE(reg_params_check._status == CellularNetwork::RegistrationDenied); - EXPECT_TRUE(reg_params_check._act == CellularNetwork::RAT_EGPRS); - EXPECT_TRUE(reg_params_check._cell_id == 305463233); - EXPECT_TRUE(reg_params_check._active_time == 240); - EXPECT_TRUE(reg_params_check._periodic_tau == 70 * 60 * 60); + ASSERT_EQ(NSAPI_ERROR_OK, cn.get_registration_params(reg_params_check)); + ASSERT_EQ(reg_params_check._status, CellularNetwork::RegistrationDenied); + ASSERT_EQ(reg_params_check._act, CellularNetwork::RAT_EGPRS); + ASSERT_EQ(reg_params_check._cell_id, 305463233); + ASSERT_EQ(reg_params_check._active_time, 240); + ASSERT_EQ(reg_params_check._periodic_tau, 70 * 60 * 60); reg_params._status = CellularNetwork::NotRegistered; reg_params._act = CellularNetwork::RAT_GSM; reg_params._cell_id = 1; - EXPECT_TRUE(NSAPI_ERROR_OK == cn.get_registration_params(CellularNetwork::C_GREG, reg_params)); - EXPECT_TRUE(reg_params._status == CellularNetwork::RegistrationDenied); - EXPECT_TRUE(reg_params._act == CellularNetwork::RAT_EGPRS); - EXPECT_TRUE(reg_params._cell_id == -1); + ASSERT_EQ(NSAPI_ERROR_OK, cn.get_registration_params(CellularNetwork::C_REG, reg_params)); + ASSERT_EQ(reg_params._status, CellularNetwork::RegistrationDenied); + ASSERT_EQ(reg_params._act, CellularNetwork::RAT_EGPRS); + ASSERT_EQ(reg_params._cell_id, -1); my_AT_CN nw(at); reg_params._status = CellularNetwork::NotRegistered; reg_params._act = CellularNetwork::RAT_GSM; - EXPECT_TRUE(NSAPI_ERROR_UNSUPPORTED == nw.get_registration_params(CellularNetwork::C_GREG, reg_params)); - EXPECT_TRUE(reg_params._status == CellularNetwork::NotRegistered); - EXPECT_TRUE(reg_params._act == CellularNetwork::RAT_GSM); + ASSERT_EQ(NSAPI_ERROR_UNSUPPORTED, nw.get_registration_params(CellularNetwork::C_GREG, reg_params)); + ASSERT_EQ(reg_params._status, CellularNetwork::NotRegistered); + ASSERT_EQ(reg_params._act, CellularNetwork::RAT_GSM); - EXPECT_TRUE(NSAPI_ERROR_OK == nw.get_registration_params(CellularNetwork::C_EREG, reg_params)); - EXPECT_TRUE(reg_params._status == CellularNetwork::RegistrationDenied); - EXPECT_TRUE(reg_params._act == CellularNetwork::RAT_EGPRS); + ASSERT_EQ(NSAPI_ERROR_OK, nw.get_registration_params(CellularNetwork::C_EREG, reg_params)); + ASSERT_EQ(reg_params._status, CellularNetwork::RegistrationDenied); + ASSERT_EQ(reg_params._act, CellularNetwork::RAT_EGPRS); ATHandler_stub::nsapi_error_value = NSAPI_ERROR_DEVICE_ERROR; reg_params._status = CellularNetwork::NotRegistered; @@ -184,15 +170,15 @@ TEST_F(TestAT_CellularNetwork, test_AT_CellularNetwork_get_registration_params) reg_params._active_time = 2; reg_params._periodic_tau = 3; - EXPECT_TRUE(NSAPI_ERROR_DEVICE_ERROR == cn.get_registration_params(CellularNetwork::C_EREG, reg_params)); - EXPECT_TRUE(reg_params._status == CellularNetwork::NotRegistered); - EXPECT_TRUE(reg_params._act == CellularNetwork::RAT_UNKNOWN); + ASSERT_EQ(NSAPI_ERROR_DEVICE_ERROR, cn.get_registration_params(CellularNetwork::C_EREG, reg_params)); + ASSERT_EQ(reg_params._status, CellularNetwork::NotRegistered); + ASSERT_EQ(reg_params._act, CellularNetwork::RAT_UNKNOWN); EXPECT_TRUE(reg_params._cell_id == -1 && reg_params._active_time == -1 && reg_params._periodic_tau == -1); - EXPECT_TRUE(NSAPI_ERROR_DEVICE_ERROR == cn.get_registration_params(CellularNetwork::C_GREG, reg_params)); - EXPECT_TRUE(reg_params._status == CellularNetwork::NotRegistered); - EXPECT_TRUE(reg_params._act == CellularNetwork::RAT_UNKNOWN); - EXPECT_TRUE(reg_params._cell_id == -1); + ASSERT_EQ(NSAPI_ERROR_DEVICE_ERROR, cn.get_registration_params(CellularNetwork::C_REG, reg_params)); + ASSERT_EQ(reg_params._status, CellularNetwork::NotRegistered); + ASSERT_EQ(reg_params._act, CellularNetwork::RAT_UNKNOWN); + ASSERT_EQ(reg_params._cell_id, -1); reg_params._status = CellularNetwork::SearchingNetwork; reg_params._act = CellularNetwork::RAT_GSM; @@ -304,63 +290,63 @@ TEST_F(TestAT_CellularNetwork, test_AT_CellularNetwork_set_registration_urc) AT_CellularNetwork cn(at); CellularNetwork::RegistrationType type = CellularNetwork::C_EREG; - EXPECT_TRUE(NSAPI_ERROR_OK == cn.set_registration_urc(type, true)); + ASSERT_EQ(NSAPI_ERROR_OK, cn.set_registration_urc(type, true)); type = CellularNetwork::C_GREG; - EXPECT_TRUE(NSAPI_ERROR_OK == cn.set_registration_urc(type, true)); + ASSERT_EQ(NSAPI_ERROR_UNSUPPORTED, cn.set_registration_urc(type, true)); type = CellularNetwork::C_REG; - EXPECT_TRUE(NSAPI_ERROR_OK == cn.set_registration_urc(type, true)); + ASSERT_EQ(NSAPI_ERROR_OK, cn.set_registration_urc(type, true)); my_AT_CN nw(at); type = CellularNetwork::C_EREG; - EXPECT_TRUE(NSAPI_ERROR_OK == nw.set_registration_urc(type, true)); + ASSERT_EQ(NSAPI_ERROR_OK, nw.set_registration_urc(type, true)); type = CellularNetwork::C_GREG; - EXPECT_TRUE(NSAPI_ERROR_UNSUPPORTED == nw.set_registration_urc(type, true)); + ASSERT_EQ(NSAPI_ERROR_UNSUPPORTED, nw.set_registration_urc(type, true)); type = CellularNetwork::C_REG; - EXPECT_TRUE(NSAPI_ERROR_OK == nw.set_registration_urc(type, true)); + ASSERT_EQ(NSAPI_ERROR_OK, nw.set_registration_urc(type, true)); type = CellularNetwork::C_EREG; - EXPECT_TRUE(NSAPI_ERROR_OK == cn.set_registration_urc(type, false)); + ASSERT_EQ(NSAPI_ERROR_OK, cn.set_registration_urc(type, false)); type = CellularNetwork::C_GREG; - EXPECT_TRUE(NSAPI_ERROR_OK == cn.set_registration_urc(type, false)); + ASSERT_EQ(NSAPI_ERROR_UNSUPPORTED, cn.set_registration_urc(type, false)); type = CellularNetwork::C_REG; - EXPECT_TRUE(NSAPI_ERROR_OK == cn.set_registration_urc(type, false)); + ASSERT_EQ(NSAPI_ERROR_OK, cn.set_registration_urc(type, false)); type = CellularNetwork::C_EREG; - EXPECT_TRUE(NSAPI_ERROR_OK == nw.set_registration_urc(type, false)); + ASSERT_EQ(NSAPI_ERROR_OK, nw.set_registration_urc(type, false)); type = CellularNetwork::C_GREG; - EXPECT_TRUE(NSAPI_ERROR_UNSUPPORTED == nw.set_registration_urc(type, false)); + ASSERT_EQ(NSAPI_ERROR_UNSUPPORTED, nw.set_registration_urc(type, false)); type = CellularNetwork::C_REG; - EXPECT_TRUE(NSAPI_ERROR_OK == nw.set_registration_urc(type, false)); + ASSERT_EQ(NSAPI_ERROR_OK, nw.set_registration_urc(type, false)); ATHandler_stub::nsapi_error_value = NSAPI_ERROR_DEVICE_ERROR; type = CellularNetwork::C_EREG; - EXPECT_TRUE(NSAPI_ERROR_DEVICE_ERROR == cn.set_registration_urc(type, true)); + ASSERT_EQ(NSAPI_ERROR_DEVICE_ERROR, cn.set_registration_urc(type, true)); type = CellularNetwork::C_GREG; - EXPECT_TRUE(NSAPI_ERROR_DEVICE_ERROR == cn.set_registration_urc(type, true)); + ASSERT_EQ(NSAPI_ERROR_UNSUPPORTED, cn.set_registration_urc(type, true)); type = CellularNetwork::C_REG; - EXPECT_TRUE(NSAPI_ERROR_DEVICE_ERROR == cn.set_registration_urc(type, true)); + ASSERT_EQ(NSAPI_ERROR_DEVICE_ERROR, cn.set_registration_urc(type, true)); type = CellularNetwork::C_EREG; - EXPECT_TRUE(NSAPI_ERROR_DEVICE_ERROR == nw.set_registration_urc(type, true)); + ASSERT_EQ(NSAPI_ERROR_DEVICE_ERROR, nw.set_registration_urc(type, true)); type = CellularNetwork::C_GREG; - EXPECT_TRUE(NSAPI_ERROR_UNSUPPORTED == nw.set_registration_urc(type, true)); + ASSERT_EQ(NSAPI_ERROR_UNSUPPORTED, nw.set_registration_urc(type, true)); type = CellularNetwork::C_REG; - EXPECT_TRUE(NSAPI_ERROR_DEVICE_ERROR == nw.set_registration_urc(type, true)); + ASSERT_EQ(NSAPI_ERROR_DEVICE_ERROR, nw.set_registration_urc(type, true)); type = CellularNetwork::C_EREG; - EXPECT_TRUE(NSAPI_ERROR_DEVICE_ERROR == cn.set_registration_urc(type, false)); + ASSERT_EQ(NSAPI_ERROR_DEVICE_ERROR, cn.set_registration_urc(type, false)); type = CellularNetwork::C_GREG; - EXPECT_TRUE(NSAPI_ERROR_DEVICE_ERROR == cn.set_registration_urc(type, false)); + ASSERT_EQ(NSAPI_ERROR_UNSUPPORTED, cn.set_registration_urc(type, false)); type = CellularNetwork::C_REG; - EXPECT_TRUE(NSAPI_ERROR_DEVICE_ERROR == cn.set_registration_urc(type, false)); + ASSERT_EQ(NSAPI_ERROR_DEVICE_ERROR, cn.set_registration_urc(type, false)); type = CellularNetwork::C_EREG; - EXPECT_TRUE(NSAPI_ERROR_DEVICE_ERROR == nw.set_registration_urc(type, false)); + ASSERT_EQ(NSAPI_ERROR_DEVICE_ERROR, nw.set_registration_urc(type, false)); type = CellularNetwork::C_GREG; - EXPECT_TRUE(NSAPI_ERROR_UNSUPPORTED == nw.set_registration_urc(type, false)); + ASSERT_EQ(NSAPI_ERROR_UNSUPPORTED, nw.set_registration_urc(type, false)); type = CellularNetwork::C_REG; - EXPECT_TRUE(NSAPI_ERROR_DEVICE_ERROR == nw.set_registration_urc(type, false)); + ASSERT_EQ(NSAPI_ERROR_DEVICE_ERROR, nw.set_registration_urc(type, false)); } TEST_F(TestAT_CellularNetwork, test_AT_CellularNetwork_set_attach) @@ -486,8 +472,8 @@ TEST_F(TestAT_CellularNetwork, test_AT_CellularNetwork_scan_plmn) ATHandler_stub::bool_value = false; EXPECT_TRUE(NSAPI_ERROR_UNSUPPORTED == cn.set_access_technology(CellularNetwork::RAT_UTRAN)); EXPECT_TRUE(NSAPI_ERROR_OK == cn.scan_plmn(ops, c)); - EXPECT_TRUE(c == 0); - EXPECT_TRUE(ops.get_head() == NULL); + EXPECT_TRUE(c == 1); + EXPECT_TRUE(ops.get_head() != NULL); } TEST_F(TestAT_CellularNetwork, test_AT_CellularNetwork_set_ciot_optimization_config) @@ -498,53 +484,49 @@ TEST_F(TestAT_CellularNetwork, test_AT_CellularNetwork_set_ciot_optimization_con AT_CellularNetwork cn(at); ATHandler_stub::nsapi_error_value = NSAPI_ERROR_OK; - EXPECT_TRUE(NSAPI_ERROR_OK == cn.set_ciot_optimization_config(CellularNetwork::SUPPORTED_UE_OPT_NO_SUPPORT, CellularNetwork::PREFERRED_UE_OPT_NO_PREFERENCE)); + EXPECT_TRUE(NSAPI_ERROR_OK == cn.set_ciot_optimization_config(CellularNetwork::CIOT_OPT_NO_SUPPORT, CellularNetwork::PREFERRED_UE_OPT_NO_PREFERENCE, NULL)); ATHandler_stub::nsapi_error_value = NSAPI_ERROR_DEVICE_ERROR; - EXPECT_TRUE(NSAPI_ERROR_DEVICE_ERROR == cn.set_ciot_optimization_config(CellularNetwork::SUPPORTED_UE_OPT_NO_SUPPORT, CellularNetwork::PREFERRED_UE_OPT_NO_PREFERENCE)); + EXPECT_TRUE(NSAPI_ERROR_DEVICE_ERROR == cn.set_ciot_optimization_config(CellularNetwork::CIOT_OPT_NO_SUPPORT, CellularNetwork::PREFERRED_UE_OPT_NO_PREFERENCE, NULL)); } -TEST_F(TestAT_CellularNetwork, test_AT_CellularNetwork_get_ciot_optimization_config) +TEST_F(TestAT_CellularNetwork, test_AT_CellularNetwork_get_ciot_ue_optimization_config) { EventQueue que; FileHandle_stub fh1; ATHandler at(&fh1, que, 0, ","); AT_CellularNetwork cn(at); - CellularNetwork::Supported_UE_Opt sup = CellularNetwork::SUPPORTED_UE_OPT_NO_SUPPORT; - CellularNetwork::Preferred_UE_Opt pref = CellularNetwork::PREFERRED_UE_OPT_NO_PREFERENCE; + CellularNetwork::CIoT_Supported_Opt sup = CellularNetwork::CIOT_OPT_NO_SUPPORT; + CellularNetwork::CIoT_Preferred_UE_Opt pref = CellularNetwork::PREFERRED_UE_OPT_NO_PREFERENCE; ATHandler_stub::int_value = 1; ATHandler_stub::nsapi_error_value = NSAPI_ERROR_OK; - EXPECT_TRUE(NSAPI_ERROR_OK == cn.get_ciot_optimization_config(sup, pref)); - EXPECT_TRUE(sup == CellularNetwork::SUPPORTED_UE_OPT_CONTROL_PLANE); + EXPECT_TRUE(NSAPI_ERROR_OK == cn.get_ciot_ue_optimization_config(sup, pref)); + EXPECT_TRUE(sup == CellularNetwork::CIOT_OPT_CONTROL_PLANE); EXPECT_TRUE(pref == CellularNetwork::PREFERRED_UE_OPT_CONTROL_PLANE); - sup = CellularNetwork::SUPPORTED_UE_OPT_NO_SUPPORT; + sup = CellularNetwork::CIOT_OPT_NO_SUPPORT; pref = CellularNetwork::PREFERRED_UE_OPT_NO_PREFERENCE; ATHandler_stub::int_value = 1; ATHandler_stub::nsapi_error_value = NSAPI_ERROR_DEVICE_ERROR; ATHandler_stub::nsapi_error_ok_counter = 0; - EXPECT_TRUE(NSAPI_ERROR_DEVICE_ERROR == cn.get_ciot_optimization_config(sup, pref)); - EXPECT_TRUE(sup == CellularNetwork::SUPPORTED_UE_OPT_NO_SUPPORT); + EXPECT_TRUE(NSAPI_ERROR_DEVICE_ERROR == cn.get_ciot_ue_optimization_config(sup, pref)); + EXPECT_TRUE(sup == CellularNetwork::CIOT_OPT_NO_SUPPORT); EXPECT_TRUE(pref == CellularNetwork::PREFERRED_UE_OPT_NO_PREFERENCE); } -TEST_F(TestAT_CellularNetwork, test_AT_CellularNetwork_get_extended_signal_quality) +TEST_F(TestAT_CellularNetwork, test_AT_CellularNetwork_get_ciot_network_optimization_config) { EventQueue que; FileHandle_stub fh1; ATHandler at(&fh1, que, 0, ","); AT_CellularNetwork cn(at); - ATHandler_stub::nsapi_error_value = NSAPI_ERROR_DEVICE_ERROR; - int rx = -1, be = -1, rs = -1, ec = -1, rsrq = -1, rsrp = -1; - EXPECT_TRUE(NSAPI_ERROR_DEVICE_ERROR == cn.get_extended_signal_quality(rx, be, rs, ec, rsrq, rsrp)); - EXPECT_TRUE(rx == -1 && be == -1 && rs == -1 && ec == -1 && rsrq == -1 && rsrp == -1); - - ATHandler_stub::int_value = 5; + CellularNetwork::CIoT_Supported_Opt sup = CellularNetwork::CIOT_OPT_NO_SUPPORT; + ATHandler_stub::int_value = 1; ATHandler_stub::nsapi_error_value = NSAPI_ERROR_OK; - EXPECT_TRUE(NSAPI_ERROR_OK == cn.get_extended_signal_quality(rx, be, rs, ec, rsrq, rsrp)); - EXPECT_TRUE(rx == 5 && be == 5 && rs == 5 && ec == 5 && rsrq == 5 && rsrp == 5); + EXPECT_TRUE(NSAPI_ERROR_OK == cn.get_ciot_network_optimization_config(sup)); + EXPECT_TRUE(sup == CellularNetwork::CIOT_OPT_MAX); } TEST_F(TestAT_CellularNetwork, test_AT_CellularNetwork_get_signal_quality) @@ -556,13 +538,14 @@ TEST_F(TestAT_CellularNetwork, test_AT_CellularNetwork_get_signal_quality) AT_CellularNetwork cn(at); int rs = -1, ber = -1; ATHandler_stub::nsapi_error_value = NSAPI_ERROR_DEVICE_ERROR; - EXPECT_TRUE(NSAPI_ERROR_DEVICE_ERROR == cn.get_signal_quality(rs, ber)); + EXPECT_TRUE(NSAPI_ERROR_DEVICE_ERROR == cn.get_signal_quality(rs, &ber)); EXPECT_TRUE(rs == -1 && ber == -1); ATHandler_stub::int_value = 1; ATHandler_stub::nsapi_error_value = NSAPI_ERROR_OK; - EXPECT_TRUE(NSAPI_ERROR_OK == cn.get_signal_quality(rs, ber)); - EXPECT_TRUE(rs == -111 && ber == 1); + EXPECT_TRUE(NSAPI_ERROR_OK == cn.get_signal_quality(rs, &ber)); + EXPECT_EQ(rs, -111); + EXPECT_EQ(ber, 1); } TEST_F(TestAT_CellularNetwork, test_AT_CellularNetwork_get_3gpp_error) @@ -684,3 +667,16 @@ TEST_F(TestAT_CellularNetwork, test_get_connection_status) EXPECT_TRUE(NSAPI_STATUS_DISCONNECTED == cn.get_connection_status()); } +TEST_F(TestAT_CellularNetwork, test_AT_CellularNetwork_set_receive_period) +{ + EventQueue que; + FileHandle_stub fh1; + ATHandler at(&fh1, que, 0, ","); + + AT_CellularNetwork cn(at); + ATHandler_stub::nsapi_error_value = NSAPI_ERROR_OK; + EXPECT_TRUE(NSAPI_ERROR_OK == cn.set_receive_period(1, CellularNetwork::EDRXUTRAN_Iu_mode, 3)); + + ATHandler_stub::nsapi_error_value = NSAPI_ERROR_DEVICE_ERROR; + EXPECT_TRUE(NSAPI_ERROR_DEVICE_ERROR == cn.set_receive_period(1, CellularNetwork::EDRXUTRAN_Iu_mode, 3)); +} diff --git a/UNITTESTS/features/cellular/framework/AT/at_cellularpower/at_cellularpowertest.cpp b/UNITTESTS/features/cellular/framework/AT/at_cellularpower/at_cellularpowertest.cpp deleted file mode 100644 index 48b67abd2f6..00000000000 --- a/UNITTESTS/features/cellular/framework/AT/at_cellularpower/at_cellularpowertest.cpp +++ /dev/null @@ -1,201 +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 "gtest/gtest.h" -#include -#include "AT_CellularNetwork.h" -#include "EventQueue.h" -#include "ATHandler.h" -#include "AT_CellularPower.h" -#include "FileHandle_stub.h" -#include "ATHandler_stub.h" - -using namespace mbed; -using namespace events; - -// AStyle ignored as the definition is not clear due to preprocessor usage -// *INDENT-OFF* -class TestAT_CellularPower : public testing::Test { -protected: - - void SetUp() { - ATHandler_stub::nsapi_error_value = NSAPI_ERROR_OK; - } - - void TearDown() { - } -}; -// *INDENT-ON* - - -static void device_ready_cb() -{ -} - -TEST_F(TestAT_CellularPower, Create) -{ - - EventQueue que; - FileHandle_stub fh1; - ATHandler at(&fh1, que, 0, ","); - - AT_CellularPower *pow = new AT_CellularPower(at); - - EXPECT_TRUE(pow != NULL); - - delete pow; -} - -TEST_F(TestAT_CellularPower, test_AT_CellularPower_on) -{ - EventQueue que; - FileHandle_stub fh1; - ATHandler at(&fh1, que, 0, ","); - - AT_CellularPower pow(at); - EXPECT_TRUE(NSAPI_ERROR_UNSUPPORTED == pow.on()); -} - -TEST_F(TestAT_CellularPower, test_AT_CellularPower_off) -{ - EventQueue que; - FileHandle_stub fh1; - ATHandler at(&fh1, que, 0, ","); - - AT_CellularPower pow(at); - EXPECT_TRUE(NSAPI_ERROR_UNSUPPORTED == pow.off()); -} - -TEST_F(TestAT_CellularPower, test_AT_CellularPower_set_at_mode) -{ - EventQueue que; - FileHandle_stub fh1; - ATHandler at(&fh1, que, 0, ","); - - AT_CellularPower pow(at); - ATHandler_stub::nsapi_error_value = NSAPI_ERROR_OK; - EXPECT_TRUE(NSAPI_ERROR_OK == pow.set_at_mode()); - - ATHandler_stub::nsapi_error_value = NSAPI_ERROR_DEVICE_ERROR; - EXPECT_TRUE(NSAPI_ERROR_DEVICE_ERROR == pow.set_at_mode()); -} - -TEST_F(TestAT_CellularPower, test_AT_CellularPower_set_power_level) -{ - EventQueue que; - FileHandle_stub fh1; - ATHandler at(&fh1, que, 0, ","); - - AT_CellularPower pow(at); - ATHandler_stub::nsapi_error_value = NSAPI_ERROR_OK; - EXPECT_TRUE(NSAPI_ERROR_OK == pow.set_power_level(6)); - EXPECT_TRUE(NSAPI_ERROR_OK == pow.set_power_level(1, 1)); - EXPECT_TRUE(NSAPI_ERROR_OK == pow.set_power_level(1, 0)); - - ATHandler_stub::nsapi_error_value = NSAPI_ERROR_DEVICE_ERROR; - EXPECT_TRUE(NSAPI_ERROR_DEVICE_ERROR == pow.set_power_level(6)); -} - -TEST_F(TestAT_CellularPower, test_AT_CellularPower_reset) -{ - EventQueue que; - FileHandle_stub fh1; - ATHandler at(&fh1, que, 0, ","); - - AT_CellularPower pow(at); - ATHandler_stub::nsapi_error_value = NSAPI_ERROR_OK; - EXPECT_TRUE(NSAPI_ERROR_OK == pow.reset()); - - ATHandler_stub::nsapi_error_value = NSAPI_ERROR_DEVICE_ERROR; - EXPECT_TRUE(NSAPI_ERROR_DEVICE_ERROR == pow.reset()); -} - -TEST_F(TestAT_CellularPower, test_AT_CellularPower_opt_power_save_mode) -{ - EventQueue que; - FileHandle_stub fh1; - ATHandler at(&fh1, que, 0, ","); - - AT_CellularPower pow(at); - ATHandler_stub::nsapi_error_value = NSAPI_ERROR_OK; - EXPECT_TRUE(NSAPI_ERROR_OK == pow.opt_power_save_mode(0, 0)); - - EXPECT_TRUE(NSAPI_ERROR_OK == pow.opt_power_save_mode(10, 0)); - - EXPECT_TRUE(NSAPI_ERROR_OK == pow.opt_power_save_mode(912, 0)); - - EXPECT_TRUE(NSAPI_ERROR_OK == pow.opt_power_save_mode(1834, 1834)); - - EXPECT_TRUE(NSAPI_ERROR_OK == pow.opt_power_save_mode(18345, 18345)); - - EXPECT_TRUE(NSAPI_ERROR_OK == pow.opt_power_save_mode(101234, 101234)); - - EXPECT_TRUE(NSAPI_ERROR_OK == pow.opt_power_save_mode(1012345, 1012345)); - - EXPECT_TRUE(NSAPI_ERROR_OK == pow.opt_power_save_mode(39612345, 39612345)); - - ATHandler_stub::nsapi_error_value = NSAPI_ERROR_DEVICE_ERROR; - EXPECT_TRUE(NSAPI_ERROR_DEVICE_ERROR == pow.opt_power_save_mode(0, 0)); -} - -TEST_F(TestAT_CellularPower, test_AT_CellularPower_opt_receive_period) -{ - EventQueue que; - FileHandle_stub fh1; - ATHandler at(&fh1, que, 0, ","); - - AT_CellularPower pow(at); - ATHandler_stub::nsapi_error_value = NSAPI_ERROR_OK; - EXPECT_TRUE(NSAPI_ERROR_OK == pow.opt_receive_period(1, CellularPower::EDRXUTRAN_Iu_mode, 3)); - - ATHandler_stub::nsapi_error_value = NSAPI_ERROR_DEVICE_ERROR; - EXPECT_TRUE(NSAPI_ERROR_DEVICE_ERROR == pow.opt_receive_period(1, CellularPower::EDRXUTRAN_Iu_mode, 3)); -} - -TEST_F(TestAT_CellularPower, test_AT_CellularPower_is_device_ready) -{ - EventQueue que; - FileHandle_stub fh1; - ATHandler at(&fh1, que, 0, ","); - - AT_CellularPower pow(at); - ATHandler_stub::nsapi_error_value = NSAPI_ERROR_AUTH_FAILURE; - EXPECT_TRUE(NSAPI_ERROR_AUTH_FAILURE == pow.is_device_ready()); -} - -TEST_F(TestAT_CellularPower, test_AT_CellularPower_set_device_ready_urc_cb) -{ - EventQueue que; - FileHandle_stub fh1; - ATHandler at(&fh1, que, 0, ","); - - AT_CellularPower pow(at); - EXPECT_TRUE(NSAPI_ERROR_UNSUPPORTED == pow.set_device_ready_urc_cb(&device_ready_cb)); - EXPECT_TRUE(NSAPI_ERROR_UNSUPPORTED == pow.set_device_ready_urc_cb(NULL)); -} - -TEST_F(TestAT_CellularPower, test_AT_CellularPower_remove_device_ready_urc_cb) -{ - EventQueue que; - FileHandle_stub fh1; - ATHandler at(&fh1, que, 0, ","); - - AT_CellularPower pow(at); - EXPECT_TRUE(NSAPI_ERROR_UNSUPPORTED == pow.set_device_ready_urc_cb(&device_ready_cb)); - - pow.remove_device_ready_urc_cb(NULL); - pow.remove_device_ready_urc_cb(&device_ready_cb); -} diff --git a/UNITTESTS/features/cellular/framework/AT/at_cellularpower/unittest.cmake b/UNITTESTS/features/cellular/framework/AT/at_cellularpower/unittest.cmake deleted file mode 100644 index 82dc0e9b38b..00000000000 --- a/UNITTESTS/features/cellular/framework/AT/at_cellularpower/unittest.cmake +++ /dev/null @@ -1,28 +0,0 @@ - -#################### -# UNIT TESTS -#################### - -# Add test specific include paths -set(unittest-includes ${unittest-includes} - features/cellular/framework/common/util - ../features/cellular/framework/common - ../features/cellular/framework/AT - ../features/frameworks/mbed-client-randlib/mbed-client-randlib -) - -# Source files -set(unittest-sources - ../features/cellular/framework/AT/AT_CellularPower.cpp -) - -# Test files -set(unittest-test-sources - features/cellular/framework/AT/at_cellularpower/at_cellularpowertest.cpp - stubs/ATHandler_stub.cpp - stubs/AT_CellularBase_stub.cpp - stubs/EventQueue_stub.cpp - stubs/FileHandle_stub.cpp - stubs/CellularUtil_stub.cpp - stubs/mbed_assert_stub.c -) diff --git a/UNITTESTS/features/cellular/framework/AT/at_cellularsim/at_cellularsimtest.cpp b/UNITTESTS/features/cellular/framework/AT/at_cellularsim/at_cellularsimtest.cpp deleted file mode 100644 index e4fe6a1ce10..00000000000 --- a/UNITTESTS/features/cellular/framework/AT/at_cellularsim/at_cellularsimtest.cpp +++ /dev/null @@ -1,204 +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 "gtest/gtest.h" -#include "ATHandler_stub.h" -#include -#include "AT_CellularNetwork.h" -#include "EventQueue.h" -#include "ATHandler.h" -#include "AT_CellularSIM.h" -#include "FileHandle_stub.h" -#include "CellularLog.h" -#include "ATHandler_stub.h" - -using namespace mbed; -using namespace events; - -// AStyle ignored as the definition is not clear due to preprocessor usage -// *INDENT-OFF* -class TestAT_CellularSIM : public testing::Test { -protected: - - void SetUp() - { - ATHandler_stub::read_string_index = kRead_string_table_size; - ATHandler_stub::nsapi_error_value = NSAPI_ERROR_OK; - ATHandler_stub::read_string_value = NULL; - ATHandler_stub::ssize_value = 0; - } - - void TearDown() - { - } -}; -// *INDENT-ON* - -TEST_F(TestAT_CellularSIM, Create) -{ - EventQueue que; - FileHandle_stub fh1; - ATHandler at(&fh1, que, 0, ","); - - AT_CellularSIM *sim = new AT_CellularSIM(at); - - EXPECT_TRUE(sim != NULL); - delete sim; -} - -TEST_F(TestAT_CellularSIM, test_AT_CellularSIM_set_pin) -{ - EventQueue que; - FileHandle_stub fh1; - ATHandler at(&fh1, que, 0, ","); - - AT_CellularSIM sim(at); - ATHandler_stub::nsapi_error_value = NSAPI_ERROR_OK; - EXPECT_TRUE(NSAPI_ERROR_OK == sim.set_pin("12")); - - ATHandler_stub::nsapi_error_value = NSAPI_ERROR_DEVICE_ERROR; - EXPECT_TRUE(NSAPI_ERROR_DEVICE_ERROR == sim.set_pin("12")); - - ATHandler_stub::nsapi_error_value = NSAPI_ERROR_OK; - ATHandler_stub::read_string_value = (char *)"READY"; - ATHandler_stub::ssize_value = 5; - EXPECT_TRUE(NSAPI_ERROR_OK == sim.set_pin("12")); - - EXPECT_TRUE(NSAPI_ERROR_OK == sim.set_pin(NULL)); -} - -TEST_F(TestAT_CellularSIM, test_AT_CellularSIM_change_pin) -{ - EventQueue que; - FileHandle_stub fh1; - ATHandler at(&fh1, que, 0, ","); - - AT_CellularSIM sim(at); - ATHandler_stub::nsapi_error_value = NSAPI_ERROR_OK; - EXPECT_TRUE(NSAPI_ERROR_OK == sim.change_pin("12", "34")); - EXPECT_TRUE(NSAPI_ERROR_OK == sim.change_pin(NULL, "34")); - EXPECT_TRUE(NSAPI_ERROR_OK == sim.change_pin("12", NULL)); - EXPECT_TRUE(NSAPI_ERROR_OK == sim.change_pin(NULL, NULL)); - - ATHandler_stub::nsapi_error_value = NSAPI_ERROR_DEVICE_ERROR; - EXPECT_TRUE(NSAPI_ERROR_DEVICE_ERROR == sim.change_pin("12", "34")); -} - -TEST_F(TestAT_CellularSIM, test_AT_CellularSIM_set_pin_query) -{ - EventQueue que; - FileHandle_stub fh1; - ATHandler at(&fh1, que, 0, ","); - - AT_CellularSIM sim(at); - ATHandler_stub::nsapi_error_value = NSAPI_ERROR_OK; - EXPECT_TRUE(NSAPI_ERROR_OK == sim.set_pin_query("12", true)); - EXPECT_TRUE(NSAPI_ERROR_OK == sim.set_pin_query(NULL, true)); - - ATHandler_stub::nsapi_error_value = NSAPI_ERROR_OK; - EXPECT_TRUE(NSAPI_ERROR_OK == sim.set_pin_query("12", false)); - EXPECT_TRUE(NSAPI_ERROR_OK == sim.set_pin_query(NULL, false)); - - ATHandler_stub::nsapi_error_value = NSAPI_ERROR_DEVICE_ERROR; - EXPECT_TRUE(NSAPI_ERROR_DEVICE_ERROR == sim.set_pin_query("12", false)); - EXPECT_TRUE(NSAPI_ERROR_DEVICE_ERROR == sim.set_pin_query("12", true)); -} - -TEST_F(TestAT_CellularSIM, test_AT_CellularSIM_get_sim_state) -{ - EventQueue que; - FileHandle_stub fh1; - ATHandler at(&fh1, que, 0, ","); - - AT_CellularSIM sim(at); - CellularSIM::SimState state; - ATHandler_stub::nsapi_error_value = NSAPI_ERROR_OK; - ATHandler_stub::ssize_value = -1; - ATHandler_stub::read_string_value = NULL; - EXPECT_TRUE(NSAPI_ERROR_OK == sim.get_sim_state(state)); - EXPECT_TRUE(CellularSIM::SimStateUnknown == state); - - ATHandler_stub::read_string_value = (char *)"READY"; - ATHandler_stub::ssize_value = 5; - EXPECT_TRUE(NSAPI_ERROR_OK == sim.get_sim_state(state)); - EXPECT_TRUE(CellularSIM::SimStateReady == state); - - ATHandler_stub::read_string_value = (char *)"SIM PIN"; - ATHandler_stub::ssize_value = 7; - EXPECT_TRUE(NSAPI_ERROR_OK == sim.get_sim_state(state)); - EXPECT_TRUE(CellularSIM::SimStatePinNeeded == state); - - ATHandler_stub::read_string_value = (char *)"SIM PUK"; - ATHandler_stub::ssize_value = 7; - EXPECT_TRUE(NSAPI_ERROR_OK == sim.get_sim_state(state)); - EXPECT_TRUE(CellularSIM::SimStatePukNeeded == state); - - ATHandler_stub::read_string_value = (char *)"SOME CRAP"; - ATHandler_stub::ssize_value = 9; - EXPECT_TRUE(NSAPI_ERROR_OK == sim.get_sim_state(state)); - EXPECT_TRUE(CellularSIM::SimStateUnknown == state); -} - -TEST_F(TestAT_CellularSIM, test_AT_CellularSIM_get_imsi) -{ - EventQueue que; - FileHandle_stub fh1; - ATHandler at(&fh1, que, 0, ","); - - char imsi[16]; - AT_CellularSIM sim(at); - ATHandler_stub::nsapi_error_value = NSAPI_ERROR_OK; - ATHandler_stub::read_string_value = (char *)"123456789012345"; - ATHandler_stub::ssize_value = 15; - EXPECT_TRUE(NSAPI_ERROR_OK == sim.get_imsi(imsi)); - EXPECT_TRUE(strcmp(ATHandler_stub::read_string_value, imsi) == 0); - - ATHandler_stub::read_string_value = NULL; - ATHandler_stub::ssize_value = -1; - ATHandler_stub::read_string_index = -1; - imsi[0] = 0; - EXPECT_TRUE(NSAPI_ERROR_DEVICE_ERROR == sim.get_imsi(imsi)); - EXPECT_TRUE(strlen(imsi) == 0); - - EXPECT_TRUE(NSAPI_ERROR_PARAMETER == sim.get_imsi(NULL)); - - // this would fail as get_imsi should take another param which is the size of the buffer which we could use for validation. - // Now we have improved documentation that that the given imsi buffer size must be over 15. - //char imsi2[5]; - //EXPECT_TRUE(NSAPI_ERROR_PARAMETER == sim.get_imsi(imsi2)); -} - -TEST_F(TestAT_CellularSIM, test_AT_CellularSIM_get_iccid) -{ - EventQueue que; - FileHandle_stub fh1; - ATHandler at(&fh1, que, 0, ","); - - char buf[16]; - AT_CellularSIM sim(at); - ATHandler_stub::nsapi_error_value = NSAPI_ERROR_OK; - ATHandler_stub::read_string_value = (char *)"123456789012345"; - ATHandler_stub::ssize_value = 15; - EXPECT_TRUE(NSAPI_ERROR_OK == sim.get_iccid(buf, 16)); - EXPECT_TRUE(strcmp(ATHandler_stub::read_string_value, buf) == 0); - - buf[0] = 0; - ATHandler_stub::nsapi_error_value = NSAPI_ERROR_DEVICE_ERROR; - ATHandler_stub::read_string_value = NULL; - ATHandler_stub::ssize_value = -1; - EXPECT_TRUE(NSAPI_ERROR_DEVICE_ERROR == sim.get_iccid(buf, 16)); - EXPECT_TRUE(strlen(buf) == 0); -} diff --git a/UNITTESTS/features/cellular/framework/AT/at_cellularsim/unittest.cmake b/UNITTESTS/features/cellular/framework/AT/at_cellularsim/unittest.cmake deleted file mode 100644 index 7adbfef8711..00000000000 --- a/UNITTESTS/features/cellular/framework/AT/at_cellularsim/unittest.cmake +++ /dev/null @@ -1,29 +0,0 @@ - -#################### -# UNIT TESTS -#################### - -# Add test specific include paths -set(unittest-includes ${unittest-includes} - features/cellular/framework/common/util - ../features/cellular/framework/common - ../features/cellular/framework/AT - ../features/frameworks/mbed-client-randlib/mbed-client-randlib -) - -# Source files -set(unittest-sources - ../features/cellular/framework/AT/AT_CellularSIM.cpp -) - -# Test files -set(unittest-test-sources - features/cellular/framework/AT/at_cellularsim/at_cellularsimtest.cpp - stubs/ATHandler_stub.cpp - stubs/AT_CellularBase_stub.cpp - stubs/EventQueue_stub.cpp - stubs/FileHandle_stub.cpp - stubs/CellularUtil_stub.cpp - stubs/us_ticker_stub.cpp - stubs/mbed_assert_stub.c -) diff --git a/UNITTESTS/features/cellular/framework/AT/at_cellularsms/at_cellularsmstest.cpp b/UNITTESTS/features/cellular/framework/AT/at_cellularsms/at_cellularsmstest.cpp index a21b7098dec..fb8eeef021b 100644 --- a/UNITTESTS/features/cellular/framework/AT/at_cellularsms/at_cellularsmstest.cpp +++ b/UNITTESTS/features/cellular/framework/AT/at_cellularsms/at_cellularsmstest.cpp @@ -71,14 +71,14 @@ TEST_F(TestAT_CellularSMS, test_AT_CellularSMS_initialize) AT_CellularSMS sms(at); ATHandler_stub::nsapi_error_value = NSAPI_ERROR_AUTH_FAILURE; - EXPECT_TRUE(NSAPI_ERROR_NO_MEMORY == sms.initialize(CellularSMS::CellularSMSMmodeText)); + EXPECT_EQ(NSAPI_ERROR_AUTH_FAILURE, sms.initialize(CellularSMS::CellularSMSMmodeText)); ATHandler_stub::nsapi_error_value = NSAPI_ERROR_OK; - EXPECT_TRUE(NSAPI_ERROR_OK == sms.initialize(CellularSMS::CellularSMSMmodeText)); + EXPECT_EQ(NSAPI_ERROR_OK, sms.initialize(CellularSMS::CellularSMSMmodeText)); sms.set_sms_callback(&my_callback); ATHandler_stub::nsapi_error_value = NSAPI_ERROR_OK; - EXPECT_TRUE(NSAPI_ERROR_OK == sms.initialize(CellularSMS::CellularSMSMmodeText)); + EXPECT_EQ(NSAPI_ERROR_OK, sms.initialize(CellularSMS::CellularSMSMmodeText)); ATHandler_stub::call_immediately = false; } diff --git a/UNITTESTS/features/cellular/framework/AT/athandler/athandlertest.cpp b/UNITTESTS/features/cellular/framework/AT/athandler/athandlertest.cpp index 3f5d033a723..59d262287f9 100644 --- a/UNITTESTS/features/cellular/framework/AT/athandler/athandlertest.cpp +++ b/UNITTESTS/features/cellular/framework/AT/athandler/athandlertest.cpp @@ -128,7 +128,7 @@ TEST_F(TestATHandler, test_ATHandler_set_urc_handler) at.set_urc_handler(ch, cb); //THIS IS NOT same callback in find_urc_handler??? - EXPECT_TRUE(NSAPI_ERROR_OK == at.set_urc_handler(ch, cb)); + at.set_urc_handler(ch, cb); } TEST_F(TestATHandler, test_ATHandler_remove_urc_handler) @@ -142,8 +142,7 @@ TEST_F(TestATHandler, test_ATHandler_remove_urc_handler) mbed::Callback cb(&urc_callback); at.set_urc_handler(ch, cb); - //This does nothing!!! - at.remove_urc_handler(ch); + at.set_urc_handler(ch, 0); } TEST_F(TestATHandler, test_ATHandler_get_last_error) @@ -964,10 +963,10 @@ TEST_F(TestATHandler, test_ATHandler_resp_start) filehandle_stub_table = table9; filehandle_stub_table_pos = 0; + at.set_urc_handler("urc: ", &urc_callback); at.set_urc_handler("urc: ", NULL); at.resp_start(); - // Match URC consumes to CRLF -> nothing to read after that -> ERROR - EXPECT_TRUE(at.get_last_error() == NSAPI_ERROR_DEVICE_ERROR); + EXPECT_EQ(at.get_last_error(), NSAPI_ERROR_OK); char table10[] = "urc: info\r\ngarbage\r\nprefix: info\r\nOK\r\n\0"; at.flush(); diff --git a/UNITTESTS/features/cellular/framework/device/cellulardevice/cellulardevicetest.cpp b/UNITTESTS/features/cellular/framework/device/cellulardevice/cellulardevicetest.cpp index 4eb5e3d6e6b..79d490bbeca 100644 --- a/UNITTESTS/features/cellular/framework/device/cellulardevice/cellulardevicetest.cpp +++ b/UNITTESTS/features/cellular/framework/device/cellulardevice/cellulardevicetest.cpp @@ -21,7 +21,6 @@ #include "FileHandle_stub.h" #include "myCellularDevice.h" #include "CellularStateMachine_stub.h" -#include "CellularSIM.h" using namespace mbed; @@ -54,7 +53,7 @@ TEST_F(TestCellularDevice, test_create_delete) dev = NULL; CellularDevice *dev1 = CellularDevice::get_default_instance(); - EXPECT_TRUE(dev1); + EXPECT_FALSE(dev1); } TEST_F(TestCellularDevice, test_set_sim_pin) @@ -221,7 +220,7 @@ TEST_F(TestCellularDevice, test_cellular_callback) dev->cellular_callback((nsapi_event_t)CellularDeviceReady, (intptr_t)&data); dev->set_sim_pin("1234"); - data.status_data = CellularSIM::SimStatePinNeeded; + data.status_data = CellularDevice::SimStatePinNeeded; dev->cellular_callback((nsapi_event_t)CellularSIMStatusChanged, (intptr_t)&data); CellularContext *ctx = dev->create_context(); @@ -229,3 +228,15 @@ TEST_F(TestCellularDevice, test_cellular_callback) 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; +} diff --git a/UNITTESTS/features/cellular/framework/device/cellulardevice/unittest.cmake b/UNITTESTS/features/cellular/framework/device/cellulardevice/unittest.cmake index 7da80e97b17..0e9bb234277 100644 --- a/UNITTESTS/features/cellular/framework/device/cellulardevice/unittest.cmake +++ b/UNITTESTS/features/cellular/framework/device/cellulardevice/unittest.cmake @@ -8,6 +8,7 @@ set(unittest-includes ${unittest-includes} /features/cellular/framework/device/cellulardevice ../features/cellular/framework/device ../features/cellular/framework/common + ../features/netsocket/cellular ) # Source files diff --git a/UNITTESTS/features/cellular/framework/device/cellularstatemachine/cellularstatemachinetest.cpp b/UNITTESTS/features/cellular/framework/device/cellularstatemachine/cellularstatemachinetest.cpp new file mode 100644 index 00000000000..5466b72ae6b --- /dev/null +++ b/UNITTESTS/features/cellular/framework/device/cellularstatemachine/cellularstatemachinetest.cpp @@ -0,0 +1,416 @@ +/* + * 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 "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_REGISTERING_NETWORK, + UT_STATE_MANUAL_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) + { + _state_machine = new CellularStateMachine(device, queue); + return _state_machine; + } + + void delete_state_machine() { + delete _state_machine; + _state_machine = NULL; + } + + void set_cellular_callback(mbed::Callback 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 ¤t_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()); + 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()); + 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()); + EXPECT_TRUE(stm); + nsapi_error_t err = ut.start_dispatch(); + ASSERT_EQ(NSAPI_ERROR_OK, err); + ut.delete_state_machine(); + + Thread_stub::osStatus_value = osErrorNoMemory; + stm = ut.create_state_machine(*dev, *dev->get_queue()); + EXPECT_TRUE(stm); + err = ut.start_dispatch(); + ASSERT_EQ(NSAPI_ERROR_NO_MEMORY, 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()); + EXPECT_TRUE(stm); + + ut.stop(); // nothing created, run through + ut.delete_state_machine(); + + stm = ut.create_state_machine(*dev, *dev->get_queue()); + 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()); + 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()); + 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()); + 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_MANUAL_REGISTERING_NETWORK, current_state); + ASSERT_EQ(UT_STATE_MANUAL_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; +} + + + + + diff --git a/UNITTESTS/features/cellular/framework/device/cellularstatemachine/unittest.cmake b/UNITTESTS/features/cellular/framework/device/cellularstatemachine/unittest.cmake new file mode 100644 index 00000000000..359bd46d49e --- /dev/null +++ b/UNITTESTS/features/cellular/framework/device/cellularstatemachine/unittest.cmake @@ -0,0 +1,43 @@ + +#################### +# UNIT TESTS +#################### + +# Add test specific include paths +set(unittest-includes ${unittest-includes} + /features/cellular/framework/device/cellularstatemachine + ../features/cellular/framework/device + ../features/cellular/framework/common + ../features/netsocket/cellular +) + +# Source files +set(unittest-sources + ../features/cellular/framework/device/CellularStateMachine.cpp +) + +# Test files +set(unittest-test-sources + features/cellular/framework/device/cellularstatemachine/cellularstatemachinetest.cpp + stubs/FileHandle_stub.cpp + stubs/CellularDevice_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/AT_CellularDevice_stub.cpp + stubs/Semaphore_stub.cpp + stubs/NetworkInterface_stub.cpp + stubs/Thread_stub.cpp + stubs/Mutex_stub.cpp + stubs/EventQueue_stub.cpp + stubs/equeue_stub.c +) + +# 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 54003807f59..d41fcdfa377 100644 --- a/UNITTESTS/stubs/ATHandler_stub.cpp +++ b/UNITTESTS/stubs/ATHandler_stub.cpp @@ -117,7 +117,7 @@ void ATHandler::set_file_handle(FileHandle *fh) { } -nsapi_error_t ATHandler::set_urc_handler(const char *urc, mbed::Callback cb) +void ATHandler::set_urc_handler(const char *urc, mbed::Callback cb) { if (ATHandler_stub::urc_amount < kATHandler_urc_table_max_size) { ATHandler_stub::callback[ATHandler_stub::urc_amount] = cb; @@ -134,7 +134,6 @@ nsapi_error_t ATHandler::set_urc_handler(const char *urc, mbed::Callback if (ATHandler_stub::call_immediately) { cb(); } - return ATHandler_stub::nsapi_error_value; } void ATHandler::remove_urc_handler(const char *prefix) diff --git a/UNITTESTS/stubs/AT_CellularBase_stub.cpp b/UNITTESTS/stubs/AT_CellularBase_stub.cpp index 179c2e42e11..40e172bab71 100644 --- a/UNITTESTS/stubs/AT_CellularBase_stub.cpp +++ b/UNITTESTS/stubs/AT_CellularBase_stub.cpp @@ -16,10 +16,8 @@ */ #include "nsapi_types.h" -#include "AT_CellularBase.h" #include "AT_CellularBase_stub.h" - - +#include "AT_CellularNetwork.h" using namespace mbed; ATHandler *AT_CellularBase_stub::handler_value = NULL; @@ -42,7 +40,17 @@ device_err_t AT_CellularBase::get_device_error() const return AT_CellularBase_stub::device_err_value; } -bool AT_CellularBase::is_supported(SupportedFeature feature) +intptr_t AT_CellularBase::get_property(CellularProperty key) { + if (key == PROPERTY_C_GREG) { + return AT_CellularNetwork::RegistrationModeDisable; + } else if (key == PROPERTY_C_REG || key == PROPERTY_C_EREG) { + return AT_CellularNetwork::RegistrationModeEnable; + } else if (key == PROPERTY_AT_CGAUTH) { + return true; + } else if (key == PROPERTY_IPV4_PDP_TYPE) { + return true; + } + return AT_CellularBase_stub::supported_bool; } diff --git a/UNITTESTS/stubs/AT_CellularBase_stub.h b/UNITTESTS/stubs/AT_CellularBase_stub.h index ec32d8eb28d..bbf63882feb 100644 --- a/UNITTESTS/stubs/AT_CellularBase_stub.h +++ b/UNITTESTS/stubs/AT_CellularBase_stub.h @@ -15,7 +15,7 @@ * limitations under the License. */ -#include "ATHandler.h" +#include "AT_CellularBase.h" namespace AT_CellularBase_stub { extern mbed::ATHandler *handler_value; diff --git a/UNITTESTS/stubs/AT_CellularContext_stub.cpp b/UNITTESTS/stubs/AT_CellularContext_stub.cpp index 8e8daba61bb..f9ab4d83cae 100644 --- a/UNITTESTS/stubs/AT_CellularContext_stub.cpp +++ b/UNITTESTS/stubs/AT_CellularContext_stub.cpp @@ -19,12 +19,12 @@ using namespace mbed; -AT_CellularContext::AT_CellularContext(ATHandler &at, CellularDevice *device, const char *apn) : - AT_CellularBase(at), _ip_stack_type_requested(DEFAULT_STACK), _is_connected(false), _is_blocking(true), - _current_op(OP_INVALID), _device(device), _nw(0), _fh(0) +AT_CellularContext::AT_CellularContext(ATHandler &at, CellularDevice *device, const char *apn, bool cp_req, bool nonip_req) : + AT_CellularBase(at), _is_blocking(true), _is_connected(false), + _current_op(OP_INVALID), _device(device), _nw(0), _fh(0), _cp_req(cp_req), _nonip_req(nonip_req), _cp_in_use(false), _cp_netif(NULL) { _stack = NULL; - _ip_stack_type = DEFAULT_STACK; + _pdp_type = DEFAULT_PDP_TYPE; _authentication_type = CellularContext::CHAP; _connect_status = NSAPI_STATUS_DISCONNECTED; _is_context_active = false; @@ -42,6 +42,14 @@ AT_CellularContext::~AT_CellularContext() { } +void AT_CellularContext::set_file_handle(UARTSerial *serial, PinName dcd_pin, bool active_high) +{ +} + +void AT_CellularContext::enable_hup(bool enable) +{ +} + void AT_CellularContext::set_file_handle(FileHandle *fh) { } @@ -135,19 +143,23 @@ const char *AT_CellularContext::get_gateway() return NULL; } -bool AT_CellularContext::stack_type_supported(nsapi_ip_stack_t stack_type) +AT_CellularBase::CellularProperty AT_CellularContext::pdp_type_t_to_cellular_property(pdp_type_t pdp_type) { - return true; -} + AT_CellularBase::CellularProperty prop = PROPERTY_IPV4_PDP_TYPE; + if (pdp_type == IPV6_PDP_TYPE) { + prop = PROPERTY_IPV6_PDP_TYPE; + } else if (pdp_type == IPV4V6_PDP_TYPE) { + prop = PROPERTY_IPV4V6_PDP_TYPE; + } else if (pdp_type == NON_IP_PDP_TYPE) { + prop = PROPERTY_NON_IP_PDP_TYPE; + } -nsapi_ip_stack_t AT_CellularContext::get_stack_type() -{ - return IPV4V6_STACK; + return prop; } -nsapi_ip_stack_t AT_CellularContext::string_to_stack_type(const char *pdp_type) +pdp_type_t AT_CellularContext::string_to_pdp_type(const char *pdp_type) { - return IPV4V6_STACK; + return IPV4V6_PDP_TYPE; } // PDP Context handling @@ -222,3 +234,30 @@ void AT_CellularContext::cellular_callback(nsapi_event_t ev, intptr_t ptr) void AT_CellularContext::call_network_cb(nsapi_connection_status_t status) { } + +ControlPlane_netif *AT_CellularContext::get_cp_netif() +{ + return NULL; +} + +nsapi_error_t AT_CellularContext::activate_non_ip_context() +{ + return NSAPI_ERROR_OK; +} + +nsapi_error_t AT_CellularContext::setup_control_plane_opt() +{ + return NSAPI_ERROR_OK; +} + +void AT_CellularContext::deactivate_ip_context() +{ +} + +void AT_CellularContext::deactivate_non_ip_context() +{ +} + +void AT_CellularContext::set_disconnect() +{ +} diff --git a/UNITTESTS/stubs/AT_CellularDevice_stub.cpp b/UNITTESTS/stubs/AT_CellularDevice_stub.cpp index 111b6460e30..3576010f66e 100644 --- a/UNITTESTS/stubs/AT_CellularDevice_stub.cpp +++ b/UNITTESTS/stubs/AT_CellularDevice_stub.cpp @@ -15,23 +15,34 @@ * limitations under the License. */ -#include "AT_CellularDevice.h" +#include "AT_CellularDevice_stub.h" #include "AT_CellularNetwork.h" +#include "AT_CellularContext.h" #include "ATHandler.h" const int DEFAULT_AT_TIMEOUT = 1000; using namespace mbed; + +int AT_CellularDevice_stub::failure_count = 0; +nsapi_error_t AT_CellularDevice_stub::nsapi_error_value = 0; +int AT_CellularDevice_stub::init_module_failure_count = 0; +int AT_CellularDevice_stub::set_pin_failure_count = 0; +int AT_CellularDevice_stub::get_sim_failure_count = 0; +bool AT_CellularDevice_stub::pin_needed = false; + 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), + _information(0), _context_list(0), _default_timeout(DEFAULT_AT_TIMEOUT), _modem_debug_on(false) { _atHandlers = new ATHandler(fh, _queue, 0, ","); + _at = _atHandlers; } AT_CellularDevice::~AT_CellularDevice() { + delete _network; delete _atHandlers; } @@ -42,22 +53,25 @@ ATHandler *AT_CellularDevice::get_at_handler(FileHandle *fileHandle) void AT_CellularDevice::release_at_handler(ATHandler *at_handler) { - } -CellularContext *create_context(FileHandle *fh = NULL, const char *apn = MBED_CONF_NSAPI_DEFAULT_CELLULAR_APN) +CellularContext *AT_CellularDevice::create_context(UARTSerial *serial, const char *const apn, PinName dcd_pin, + bool active_high, bool cp_req, bool nonip_req) { +} +CellularContext *create_context(FileHandle *fh, const char *apn) +{ } void delete_context(CellularContext *context) { - } CellularNetwork *AT_CellularDevice::open_network(FileHandle *fh) { - return new AT_CellularNetwork(*_atHandlers); + _network = new AT_CellularNetwork(*_atHandlers); + return _network; } CellularSMS *AT_CellularDevice::open_sms(FileHandle *fh) @@ -65,16 +79,6 @@ CellularSMS *AT_CellularDevice::open_sms(FileHandle *fh) return NULL; } -CellularSIM *AT_CellularDevice::open_sim(FileHandle *fh) -{ - return NULL; -} - -CellularPower *AT_CellularDevice::open_power(FileHandle *fh) -{ - return NULL; -} - CellularInformation *AT_CellularDevice::open_information(FileHandle *fh) { return NULL; @@ -88,14 +92,6 @@ void AT_CellularDevice::close_sms() { } -void AT_CellularDevice::close_power() -{ -} - -void AT_CellularDevice::close_sim() -{ -} - void AT_CellularDevice::close_information() { } @@ -105,19 +101,18 @@ CellularContext *AT_CellularDevice::get_context_list() const return NULL; } -CellularContext *AT_CellularDevice::create_context(FileHandle *fh, const char *apn) +CellularContext *AT_CellularDevice::create_context(FileHandle *fh, const char *apn, bool cp_req, bool nonip_req) { return NULL; } -AT_CellularContext *AT_CellularDevice::create_context_impl(ATHandler &at, const char *apn) +AT_CellularContext *AT_CellularDevice::create_context_impl(ATHandler &at, const char *apn, bool cp_req, bool nonip_req) { return NULL; } void AT_CellularDevice::delete_context(CellularContext *context) { - } AT_CellularNetwork *AT_CellularDevice::open_network_impl(ATHandler &at) @@ -130,16 +125,6 @@ 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; @@ -161,7 +146,84 @@ void AT_CellularDevice::modem_debug_on(bool on) } -nsapi_error_t AT_CellularDevice::init_module() +nsapi_error_t AT_CellularDevice::is_ready() +{ + if (AT_CellularDevice_stub::init_module_failure_count) { + AT_CellularDevice_stub::init_module_failure_count--; + return NSAPI_ERROR_DEVICE_ERROR; + } + return AT_CellularDevice_stub::nsapi_error_value; +} + +void AT_CellularDevice::set_ready_cb(mbed::Callback callback) +{ +} + +nsapi_error_t AT_CellularDevice::set_power_save_mode(int periodic_time, int active_time) +{ + return NSAPI_ERROR_UNSUPPORTED; +} + +nsapi_error_t AT_CellularDevice::init() +{ + if (AT_CellularDevice_stub::init_module_failure_count) { + AT_CellularDevice_stub::init_module_failure_count--; + return NSAPI_ERROR_DEVICE_ERROR; + } + return AT_CellularDevice_stub::nsapi_error_value; +} + +nsapi_error_t AT_CellularDevice::shutdown() +{ + if (AT_CellularDevice_stub::init_module_failure_count) { + AT_CellularDevice_stub::init_module_failure_count--; + return NSAPI_ERROR_DEVICE_ERROR; + } + return AT_CellularDevice_stub::nsapi_error_value; +} + +nsapi_error_t AT_CellularDevice::set_pin(const char *sim_pin) +{ + if (AT_CellularDevice_stub::set_pin_failure_count) { + AT_CellularDevice_stub::set_pin_failure_count--; + return NSAPI_ERROR_DEVICE_ERROR; + } + return AT_CellularDevice_stub::nsapi_error_value; +} + +nsapi_error_t AT_CellularDevice::get_sim_state(SimState &state) +{ + if (AT_CellularDevice_stub::get_sim_failure_count) { + AT_CellularDevice_stub::get_sim_failure_count--; + return NSAPI_ERROR_DEVICE_ERROR; + } + + if (AT_CellularDevice_stub::pin_needed) { + AT_CellularDevice_stub::pin_needed = false; + state = SimStatePinNeeded; + } else { + state = SimStateReady; + } + + return AT_CellularDevice_stub::nsapi_error_value; +} + +nsapi_error_t AT_CellularDevice::hard_power_on() +{ + return NSAPI_ERROR_OK; +} + +nsapi_error_t AT_CellularDevice::hard_power_off() +{ + return NSAPI_ERROR_OK; +} + +nsapi_error_t AT_CellularDevice::soft_power_on() +{ + return NSAPI_ERROR_OK; +} + +nsapi_error_t AT_CellularDevice::soft_power_off() { return NSAPI_ERROR_OK; } diff --git a/features/cellular/framework/targets/UBLOX/AT/UBLOX_AT_CellularPower.h b/UNITTESTS/stubs/AT_CellularDevice_stub.h similarity index 60% rename from features/cellular/framework/targets/UBLOX/AT/UBLOX_AT_CellularPower.h rename to UNITTESTS/stubs/AT_CellularDevice_stub.h index b3218225066..696475d0df9 100644 --- a/features/cellular/framework/targets/UBLOX/AT/UBLOX_AT_CellularPower.h +++ b/UNITTESTS/stubs/AT_CellularDevice_stub.h @@ -14,26 +14,19 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +#ifndef AT_CELLULARDEVICE_STUB_H_ +#define AT_CELLULARDEVICE_STUB_H_ -#ifndef UBLOX_AT_CELLULARPOWER_H_ -#define UBLOX_AT_CELLULARPOWER_H_ +#include "AT_CellularDevice.h" -#include "AT_CellularPower.h" +namespace AT_CellularDevice_stub { +extern int failure_count; +extern nsapi_error_t nsapi_error_value; +extern int init_module_failure_count; +extern int set_pin_failure_count; +extern int get_sim_failure_count; +extern bool pin_needed; +} -namespace mbed { -class UBLOX_AT_CellularPower : public AT_CellularPower { -public: - UBLOX_AT_CellularPower(ATHandler &atHandler); - virtual ~UBLOX_AT_CellularPower(); - -public: //from CellularPower - - virtual nsapi_error_t on(); - - virtual nsapi_error_t off(); -}; - -} // namespace mbed - -#endif // UBLOX_AT_CELLULARPOWER_H_ +#endif /* AT_CELLULARDEVICE_STUB_H_ */ diff --git a/UNITTESTS/stubs/AT_CellularInformation_stub.cpp b/UNITTESTS/stubs/AT_CellularInformation_stub.cpp index ae35f29bde6..832c0384338 100644 --- a/UNITTESTS/stubs/AT_CellularInformation_stub.cpp +++ b/UNITTESTS/stubs/AT_CellularInformation_stub.cpp @@ -47,3 +47,13 @@ nsapi_error_t AT_CellularInformation::get_serial_number(char *buf, size_t buf_si { return NSAPI_ERROR_OK; } + +nsapi_error_t AT_CellularInformation::get_imsi(char *imsi, size_t buf_size) +{ + return NSAPI_ERROR_OK; +} + +nsapi_error_t AT_CellularInformation::get_iccid(char *buf, size_t buf_size) +{ + return NSAPI_ERROR_OK; +} diff --git a/UNITTESTS/stubs/AT_CellularNetwork_stub.cpp b/UNITTESTS/stubs/AT_CellularNetwork_stub.cpp index 60d42e8e763..899be2fb4c0 100644 --- a/UNITTESTS/stubs/AT_CellularNetwork_stub.cpp +++ b/UNITTESTS/stubs/AT_CellularNetwork_stub.cpp @@ -15,7 +15,7 @@ * limitations under the License. */ -#include "AT_CellularNetwork.h" +#include "AT_CellularNetwork_stub.h" #include "CellularNetwork.h" #include "CellularUtil.h" #include "CellularLog.h" @@ -25,6 +25,13 @@ using namespace mbed; using namespace mbed_cellular_util; + + +nsapi_error_t AT_CellularNetwork_stub::nsapi_error_value = 0; +int AT_CellularNetwork_stub::fail_counter = 0; +int AT_CellularNetwork_stub::set_registration_urc_fail_counter = 0; +int AT_CellularNetwork_stub::get_registration_params_fail_counter = 0; + AT_CellularNetwork::AT_CellularNetwork(ATHandler &atHandler) : AT_CellularBase(atHandler) { } @@ -44,6 +51,10 @@ nsapi_connection_status_t AT_CellularNetwork::get_connection_status() const nsapi_error_t AT_CellularNetwork::set_registration_urc(RegistrationType type, bool urc_on) { + if (AT_CellularNetwork_stub::set_registration_urc_fail_counter) { + AT_CellularNetwork_stub::set_registration_urc_fail_counter--; + return NSAPI_ERROR_DEVICE_ERROR; + } return NSAPI_ERROR_OK; } @@ -60,6 +71,12 @@ nsapi_error_t AT_CellularNetwork::set_registration(const char *plmn) nsapi_error_t AT_CellularNetwork::get_registration_params(RegistrationType type, registration_params_t ®_params) { + if (AT_CellularNetwork_stub::get_registration_params_fail_counter) { + AT_CellularNetwork_stub::get_registration_params_fail_counter--; + return NSAPI_ERROR_DEVICE_ERROR; + } + + reg_params._status = CellularNetwork::RegisteredHomeNetwork; return NSAPI_ERROR_OK; } @@ -68,11 +85,6 @@ nsapi_error_t AT_CellularNetwork::get_registration_params(registration_params_t return NSAPI_ERROR_OK; } -AT_CellularNetwork::RegistrationMode AT_CellularNetwork::has_registration(RegistrationType reg_type) -{ - return RegistrationModeDisable; -} - nsapi_error_t AT_CellularNetwork::set_attach() { return NSAPI_ERROR_OK; @@ -108,24 +120,25 @@ nsapi_error_t AT_CellularNetwork::scan_plmn(operList_t &operators, int &opsCount return NSAPI_ERROR_OK; } -nsapi_error_t AT_CellularNetwork::set_ciot_optimization_config(Supported_UE_Opt supported_opt, - Preferred_UE_Opt preferred_opt) +nsapi_error_t AT_CellularNetwork::set_ciot_optimization_config(CIoT_Supported_Opt supported_opt, + CIoT_Preferred_UE_Opt preferred_opt, + Callback network_support_cb) { return NSAPI_ERROR_OK; } -nsapi_error_t AT_CellularNetwork::get_ciot_optimization_config(Supported_UE_Opt &supported_opt, - Preferred_UE_Opt &preferred_opt) +nsapi_error_t AT_CellularNetwork::get_ciot_ue_optimization_config(CIoT_Supported_Opt &supported_opt, + CIoT_Preferred_UE_Opt &preferred_opt) { return NSAPI_ERROR_OK; } -nsapi_error_t AT_CellularNetwork::get_extended_signal_quality(int &rxlev, int &ber, int &rscp, int &ecno, int &rsrq, int &rsrp) +nsapi_error_t AT_CellularNetwork::get_ciot_network_optimization_config(CIoT_Supported_Opt &supported_opt) { return NSAPI_ERROR_OK; } -nsapi_error_t AT_CellularNetwork::get_signal_quality(int &rssi, int &ber) +nsapi_error_t AT_CellularNetwork::get_signal_quality(int &rssi, int *ber) { return NSAPI_ERROR_OK; } @@ -147,5 +160,10 @@ nsapi_error_t AT_CellularNetwork::get_operator_names(operator_names_list &op_nam bool AT_CellularNetwork::is_active_context() { - return true; + return false; +} + +nsapi_error_t AT_CellularNetwork::set_receive_period(int mode, EDRXAccessTechnology act_type, uint8_t edrx_value) +{ + return NSAPI_ERROR_OK; } diff --git a/features/cellular/framework/targets/UBLOX/PPP/UBLOX_PPP_CellularContext.cpp b/UNITTESTS/stubs/AT_CellularNetwork_stub.h similarity index 60% rename from features/cellular/framework/targets/UBLOX/PPP/UBLOX_PPP_CellularContext.cpp rename to UNITTESTS/stubs/AT_CellularNetwork_stub.h index d360df7dfc5..7f8197c35af 100644 --- a/features/cellular/framework/targets/UBLOX/PPP/UBLOX_PPP_CellularContext.cpp +++ b/UNITTESTS/stubs/AT_CellularNetwork_stub.h @@ -14,22 +14,17 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -#include "UBLOX_PPP_CellularContext.h" +#ifndef AT_CELLULARNETWORK_STUB_H_ +#define AT_CELLULARNETWORK_STUB_H_ -namespace mbed { +#include "AT_CellularNetwork.h" -UBLOX_PPP_CellularContext::UBLOX_PPP_CellularContext(ATHandler &at, CellularDevice *device, const char *apn) : - AT_CellularContext(at, device, apn) -{ +namespace AT_CellularNetwork_stub { +extern nsapi_error_t nsapi_error_value; +extern int fail_counter; +extern int set_registration_urc_fail_counter; +extern int get_registration_params_fail_counter; } -UBLOX_PPP_CellularContext::~UBLOX_PPP_CellularContext() -{ -} - -bool UBLOX_PPP_CellularContext::stack_type_supported(nsapi_ip_stack_t stack_type) -{ - return stack_type == IPV4_STACK ? true : false; -} -} /* namespace mbed */ +#endif /* AT_CELLULARNETWORK_STUB_H_ */ diff --git a/UNITTESTS/stubs/AT_CellularPower_stub.cpp b/UNITTESTS/stubs/AT_CellularPower_stub.cpp deleted file mode 100644 index 56d8a4ca87e..00000000000 --- a/UNITTESTS/stubs/AT_CellularPower_stub.cpp +++ /dev/null @@ -1,81 +0,0 @@ -/* - * Copyright (c) , 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 "AT_CellularPower.h" -#include "CellularUtil.h" -#include "CellularLog.h" - -using namespace mbed_cellular_util; -using namespace mbed; - -AT_CellularPower::AT_CellularPower(ATHandler &at) : AT_CellularBase(at) -{ -} - -AT_CellularPower::~AT_CellularPower() -{ -} - -nsapi_error_t AT_CellularPower::on() -{ - return NSAPI_ERROR_OK; -} - -nsapi_error_t AT_CellularPower::off() -{ - return NSAPI_ERROR_OK; -} - -nsapi_error_t AT_CellularPower::set_at_mode() -{ - return NSAPI_ERROR_OK; -} - -nsapi_error_t AT_CellularPower::set_power_level(int func_level, int do_reset) -{ - return NSAPI_ERROR_OK; -} - -nsapi_error_t AT_CellularPower::reset() -{ - return NSAPI_ERROR_OK; -} - -nsapi_error_t AT_CellularPower::opt_power_save_mode(int periodic_time, int active_time) -{ - return NSAPI_ERROR_OK; -} - -nsapi_error_t AT_CellularPower::opt_receive_period(int mode, EDRXAccessTechnology act_type, uint8_t edrx_value) -{ - return NSAPI_ERROR_OK; -} - -nsapi_error_t AT_CellularPower::set_device_ready_urc_cb(mbed::Callback callback) -{ - return NSAPI_ERROR_OK; -} - -void AT_CellularPower::remove_device_ready_urc_cb(mbed::Callback callback) -{ - -} - -nsapi_error_t AT_CellularPower::is_device_ready() -{ - return NSAPI_ERROR_OK; -} diff --git a/UNITTESTS/stubs/AT_CellularSIM_stub.cpp b/UNITTESTS/stubs/AT_CellularSIM_stub.cpp deleted file mode 100644 index afab1012bd8..00000000000 --- a/UNITTESTS/stubs/AT_CellularSIM_stub.cpp +++ /dev/null @@ -1,59 +0,0 @@ -/* - * Copyright (c) , 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 "AT_CellularSIM.h" -#include "CellularLog.h" - -using namespace mbed; - -AT_CellularSIM::AT_CellularSIM(ATHandler &at) : AT_CellularBase(at) -{ -} - -AT_CellularSIM::~AT_CellularSIM() -{ -} - -nsapi_error_t AT_CellularSIM::get_sim_state(SimState &state) -{ - return NSAPI_ERROR_OK; -} - -nsapi_error_t AT_CellularSIM::set_pin(const char *sim_pin) -{ - return NSAPI_ERROR_OK; -} - -nsapi_error_t AT_CellularSIM::change_pin(const char *sim_pin, const char *new_pin) -{ - return NSAPI_ERROR_OK; -} - -nsapi_error_t AT_CellularSIM::set_pin_query(const char *sim_pin, bool query_pin) -{ - return NSAPI_ERROR_OK; -} - -nsapi_error_t AT_CellularSIM::get_imsi(char *imsi) -{ - return NSAPI_ERROR_OK; -} - -nsapi_error_t AT_CellularSIM::get_iccid(char *buf, size_t buf_size) -{ - return NSAPI_ERROR_OK; -} diff --git a/UNITTESTS/stubs/CellularDevice_stub.cpp b/UNITTESTS/stubs/CellularDevice_stub.cpp index e64118f0f66..7309837f068 100644 --- a/UNITTESTS/stubs/CellularDevice_stub.cpp +++ b/UNITTESTS/stubs/CellularDevice_stub.cpp @@ -30,7 +30,7 @@ MBED_WEAK CellularDevice *CellularDevice::get_default_instance() return NULL; } -CellularDevice::CellularDevice(FileHandle *fh) : _network_ref_count(0), _sms_ref_count(0), _power_ref_count(0), _sim_ref_count(0), +CellularDevice::CellularDevice(FileHandle *fh) : _network_ref_count(0), _sms_ref_count(0), _info_ref_count(0), _fh(fh), _queue(5 * EVENTS_EVENT_SIZE), _state_machine(0), _nw(0) { } @@ -87,3 +87,22 @@ nsapi_error_t CellularDevice::attach_to_network() return NSAPI_ERROR_OK; } + +nsapi_error_t CellularDevice::set_pin(const char *sim_pin) +{ + return NSAPI_ERROR_OK; +} + +nsapi_error_t CellularDevice::get_sim_state(SimState &state) +{ + return NSAPI_ERROR_OK; +} + +nsapi_error_t CellularDevice::shutdown() +{ + return NSAPI_ERROR_OK; +} + +void CellularDevice::cellular_callback(nsapi_event_t ev, intptr_t ptr) +{ +} diff --git a/UNITTESTS/stubs/Thread_stub.cpp b/UNITTESTS/stubs/Thread_stub.cpp index 8eed1e7f2a3..9ab4ebfafd6 100644 --- a/UNITTESTS/stubs/Thread_stub.cpp +++ b/UNITTESTS/stubs/Thread_stub.cpp @@ -15,13 +15,31 @@ * limitations under the License. */ -#include "Thread.h" +#include "Thread_stub.h" -namespace rtos { +using namespace rtos; + +osStatus Thread_stub::osStatus_value = osOK; osStatus Thread::wait_until(uint64_t millisec) { return 0; } +osStatus Thread::terminate() +{ + return 0; +} + +osStatus Thread::start(mbed::Callback task) +{ + return Thread_stub::osStatus_value; +} + +void Thread::constructor(osPriority priority, uint32_t stack_size, unsigned char *stack_mem, const char *name) +{ +} + +Thread::~Thread() +{ } diff --git a/features/cellular/framework/targets/MultiTech/DragonflyNano/PPP/SARA4_PPP_CellularContext.cpp b/UNITTESTS/stubs/Thread_stub.h similarity index 59% rename from features/cellular/framework/targets/MultiTech/DragonflyNano/PPP/SARA4_PPP_CellularContext.cpp rename to UNITTESTS/stubs/Thread_stub.h index 4616099faf3..be13fb841e9 100644 --- a/features/cellular/framework/targets/MultiTech/DragonflyNano/PPP/SARA4_PPP_CellularContext.cpp +++ b/UNITTESTS/stubs/Thread_stub.h @@ -14,22 +14,13 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -#include "SARA4_PPP_CellularContext.h" +#ifndef THREAD_STUB_H_ +#define THREAD_STUB_H_ -namespace mbed { +#include "Thread.h" -SARA4_PPP_CellularContext::SARA4_PPP_CellularContext(ATHandler &at, CellularDevice *device, const char *apn) : - AT_CellularContext(at, device, apn) -{ +namespace Thread_stub { +extern osStatus osStatus_value; } -SARA4_PPP_CellularContext::~SARA4_PPP_CellularContext() -{ -} - -bool SARA4_PPP_CellularContext::stack_type_supported(nsapi_ip_stack_t requested_stack) -{ - return requested_stack == IPV4_STACK ? true : false; -} - -} /* namespace mbed */ +#endif /* THREAD_STUB_H_ */ diff --git a/UNITTESTS/target_h/cmsis_os2.h b/UNITTESTS/target_h/cmsis_os2.h index c45760f27b0..66b1d2f061c 100644 --- a/UNITTESTS/target_h/cmsis_os2.h +++ b/UNITTESTS/target_h/cmsis_os2.h @@ -24,7 +24,7 @@ typedef int32_t osStatus; #define osOK 0 - +#define osErrorNoMemory -5 //These are from cmsis_os2.h diff --git a/UNITTESTS/target_h/myCellularDevice.h b/UNITTESTS/target_h/myCellularDevice.h index a157f8e4ed8..f5c5a8abdbe 100644 --- a/UNITTESTS/target_h/myCellularDevice.h +++ b/UNITTESTS/target_h/myCellularDevice.h @@ -27,9 +27,7 @@ using namespace events; namespace mbed { -class CellularPower; class CellularSMS; -class CellularSIM; class CellularInformation; class CellularContext; class FileHandle; @@ -42,7 +40,24 @@ class myCellularDevice : public CellularDevice { delete _context_list; delete _network; } - virtual CellularContext *create_context(FileHandle *fh = NULL, const char *apn = NULL) + + virtual nsapi_error_t set_pin(const char *sim_pin) + { + return NSAPI_ERROR_OK; + } + + virtual nsapi_error_t get_sim_state(SimState &state) + { + return NSAPI_ERROR_OK; + } + + virtual CellularContext *create_context(UARTSerial *serial, const char *const apn, PinName dcd_pin, + bool active_high, bool cp_req = false, bool nonip_req = false) + { + return NULL; + } + + virtual CellularContext *create_context(FileHandle *fh = NULL, const char *apn = NULL, bool cp_req = false, bool nonip_req = false) { EventQueue que; FileHandle_stub fh1; @@ -69,16 +84,6 @@ class myCellularDevice : public CellularDevice { 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; @@ -91,10 +96,6 @@ class myCellularDevice : public CellularDevice { virtual void close_sms() {} - virtual void close_power() {} - - virtual void close_sim() {} - virtual void close_information() {} virtual void set_timeout(int timeout) {} @@ -106,9 +107,48 @@ class myCellularDevice : public CellularDevice { virtual void modem_debug_on(bool on) {} - virtual nsapi_error_t init_module() + virtual nsapi_error_t init() { - return 0; + return NSAPI_ERROR_OK; + } + + virtual nsapi_error_t shutdown() + { + return NSAPI_ERROR_OK; + } + + virtual nsapi_error_t is_ready() + { + return NSAPI_ERROR_OK; + } + + virtual nsapi_error_t hard_power_on() + { + return NSAPI_ERROR_OK; + } + + virtual nsapi_error_t hard_power_off() + { + return NSAPI_ERROR_OK; + } + + virtual nsapi_error_t soft_power_on() + { + return NSAPI_ERROR_OK; + } + + virtual nsapi_error_t soft_power_off() + { + return NSAPI_ERROR_OK; + } + + virtual void set_ready_cb(Callback callback) + { + } + + nsapi_error_t set_power_save_mode(int periodic_time, int active_time) + { + return NSAPI_ERROR_OK; } virtual CellularContext *get_context_list() const diff --git a/features/cellular/TESTS/api/cellular_device/main.cpp b/features/cellular/TESTS/api/cellular_device/main.cpp index 9246e3e112f..c2a7861f30a 100644 --- a/features/cellular/TESTS/api/cellular_device/main.cpp +++ b/features/cellular/TESTS/api/cellular_device/main.cpp @@ -15,7 +15,6 @@ * limitations under the License. */ - #if !defined(MBED_CONF_NSAPI_PRESENT) #error [NOT_SUPPORTED] A json configuration file is needed. Skipping this build. #endif @@ -27,6 +26,10 @@ #error [NOT_SUPPORTED] CELLULAR_DEVICE must be defined #endif +#ifndef MBED_CONF_APP_CELLULAR_SIM_PIN +#error [NOT_SUPPORTED] SIM pin code is needed. Skipping this build. +#endif + #include "greentea-client/test_env.h" #include "unity.h" #include "utest.h" @@ -36,9 +39,24 @@ #include "CellularLog.h" #include "CellularDevice.h" #include CELLULAR_STRINGIFY(CELLULAR_DEVICE.h) +#include "Semaphore.h" +#include "../../cellular_tests_common.h" static UARTSerial cellular_serial(MDMTXD, MDMRXD, MBED_CONF_PLATFORM_DEFAULT_SERIAL_BAUD_RATE); static CellularDevice *device; +static rtos::Semaphore semaphore; + + +const int TIME_OUT_DEVICE_READY = 5 * 60 * 1000; // 5 minutes +const int TIME_OUT_REGISTER = 10 * 60 * 1000; // 10 minutes + +enum CurrentOp { + OP_DEVICE_READY, + OP_SIM_READY, + OP_REGISTER, + OP_ATTACH +}; +static CurrentOp op; static void create_device() { @@ -52,24 +70,17 @@ static void open_close_interfaces() TEST_ASSERT(nw != NULL); device->close_network(); - CellularSIM *sim = device->open_sim(&cellular_serial); - TEST_ASSERT(sim != NULL); - device->close_sim(); - CellularInformation *info = device->open_information(&cellular_serial); TEST_ASSERT(info != NULL); device->close_information(); - CellularPower *power = device->open_power(&cellular_serial); - TEST_ASSERT(power != NULL); - device->close_power(); - CellularSMS *sms = device->open_sms(&cellular_serial); TEST_ASSERT(sms != NULL); device->close_sms(); CellularContext *ctx = device->create_context(); TEST_ASSERT(ctx != NULL); + TEST_ASSERT(device->get_context_list() == ctx); device->delete_context(ctx); } @@ -83,10 +94,20 @@ static void other_methods() CellularNetwork *nw = device->open_network(&cellular_serial); TEST_ASSERT(nw != NULL); - // then test witj open interface which is called + // then test with open interface which is called device->set_timeout(5000); device->modem_debug_on(true); device->modem_debug_on(false); + + TEST_ASSERT(device->get_queue() != NULL); + TEST_ASSERT_EQUAL_INT(device->init_module(), NSAPI_ERROR_OK); +} + +static void shutdown_reset() +{ + TEST_ASSERT(device->set_device_ready() == NSAPI_ERROR_OK); + TEST_ASSERT(device->shutdown() == NSAPI_ERROR_OK); + TEST_ASSERT(device->set_device_ready() == NSAPI_ERROR_OK); } static void delete_device() @@ -96,10 +117,85 @@ static void delete_device() device = NULL; } +static void callback_func(nsapi_event_t ev, intptr_t ptr) +{ + if (ev >= NSAPI_EVENT_CELLULAR_STATUS_BASE && ev <= NSAPI_EVENT_CELLULAR_STATUS_END) { + cell_callback_data_t *ptr_data = (cell_callback_data_t *)ptr; + cellular_connection_status_t cell_ev = (cellular_connection_status_t)ev; + if (cell_ev == CellularDeviceReady && ptr_data->error == NSAPI_ERROR_OK && op == OP_DEVICE_READY) { + TEST_ASSERT_EQUAL_INT(semaphore.release(), osOK); + } else if (cell_ev == CellularSIMStatusChanged && ptr_data->error == NSAPI_ERROR_OK && + ptr_data->status_data == CellularDevice::SimStateReady && op == OP_SIM_READY) { + TEST_ASSERT_EQUAL_INT(semaphore.release(), osOK); + } else if (cell_ev == CellularRegistrationStatusChanged && + (ptr_data->status_data == CellularNetwork::RegisteredHomeNetwork || + ptr_data->status_data == CellularNetwork::RegisteredRoaming || + ptr_data->status_data == CellularNetwork::AlreadyRegistered) && + ptr_data->error == NSAPI_ERROR_OK && + op == OP_REGISTER) { + TEST_ASSERT_EQUAL_INT(semaphore.release(), osOK); + } else if (cell_ev == CellularAttachNetwork && ptr_data->status_data == CellularNetwork::Attached && + ptr_data->error == NSAPI_ERROR_OK && op == OP_ATTACH) { + TEST_ASSERT_EQUAL_INT(semaphore.release(), osOK); + } + } +} + +static void init_to_device_ready_state() +{ + device = CellularDevice::get_default_instance(); + TEST_ASSERT(device != NULL); + +#ifdef MBED_CONF_APP_CELLULAR_SIM_PIN + device->set_sim_pin(MBED_CONF_APP_CELLULAR_SIM_PIN); +#endif +#ifdef MBED_CONF_APP_CELLULAR_PLMN + device->set_plmn(MBED_CONF_APP_CELLULAR_PLMN); +#endif + device->attach(&callback_func); + + op = OP_DEVICE_READY; + TEST_ASSERT_EQUAL_INT(NSAPI_ERROR_OK, device->init_module()); + TEST_ASSERT_EQUAL_INT(NSAPI_ERROR_OK, device->set_device_ready()); + + int sema_err = semaphore.wait(TIME_OUT_DEVICE_READY); + TEST_ASSERT_EQUAL_INT(1, sema_err); +} + +static void continue_to_sim_ready_state() +{ + op = OP_SIM_READY; + TEST_ASSERT_EQUAL_INT(NSAPI_ERROR_OK, device->set_sim_ready()); + + int sema_err = semaphore.wait(TIME_OUT_DEVICE_READY); + TEST_ASSERT_EQUAL_INT(1, sema_err); +} + +static void continue_to_register_state() +{ + op = OP_REGISTER; + TEST_ASSERT_EQUAL_INT(NSAPI_ERROR_OK, device->register_to_network()); + + int sema_err = semaphore.wait(TIME_OUT_REGISTER); // cellular network searching may take several minutes + TEST_ASSERT_EQUAL_INT(1, sema_err); +} + +static void continue_to_attach_state() +{ + op = OP_ATTACH; + TEST_ASSERT_EQUAL_INT(NSAPI_ERROR_OK, device->attach_to_network()); + + int sema_err = semaphore.wait(TIME_OUT_REGISTER); // cellular network attach may take several minutes + TEST_ASSERT_EQUAL_INT(1, sema_err); +} + using namespace utest::v1; static utest::v1::status_t greentea_failure_handler(const Case *const source, const failure_t reason) { +#if MBED_CONF_MBED_TRACE_ENABLE + trace_close(); +#endif greentea_case_failure_abort_handler(source, reason); return STATUS_ABORT; } @@ -108,6 +204,12 @@ static Case cases[] = { Case("CellularDevice create device", create_device, greentea_failure_handler), Case("CellularDevice Open and close interfaces", open_close_interfaces, greentea_failure_handler), Case("CellularDevice other methods", other_methods, greentea_failure_handler), + Case("CellularDevice delete device", delete_device, greentea_failure_handler), + Case("CellularDevice init to device ready", init_to_device_ready_state, greentea_failure_handler), + Case("CellularDevice sim ready", continue_to_sim_ready_state, greentea_failure_handler), + Case("CellularDevice register", continue_to_register_state, greentea_failure_handler), + Case("CellularDevice attach", continue_to_attach_state, greentea_failure_handler) + Case("CellularDevice shutdown/reset", shutdown_reset, greentea_failure_handler), Case("CellularDevice delete device", delete_device, greentea_failure_handler) }; @@ -121,8 +223,14 @@ static Specification specification(test_setup, cases); int main() { - mbed_trace_init(); +#if MBED_CONF_MBED_TRACE_ENABLE + trace_open(); +#endif - return Harness::run(specification); + int ret = Harness::run(specification); +#if MBED_CONF_MBED_TRACE_ENABLE + trace_close(); +#endif + return ret; } diff --git a/features/cellular/TESTS/api/cellular_information/main.cpp b/features/cellular/TESTS/api/cellular_information/main.cpp index 942da3177fd..bf18c553e26 100644 --- a/features/cellular/TESTS/api/cellular_information/main.cpp +++ b/features/cellular/TESTS/api/cellular_information/main.cpp @@ -80,6 +80,12 @@ static void test_information_interface() err = info->get_serial_number(buf, kbuf_size, CellularInformation::SVN); TEST_ASSERT(err == NSAPI_ERROR_UNSUPPORTED || err == NSAPI_ERROR_OK); + err = info->get_imsi(buf, kbuf_size); + TEST_ASSERT(err == NSAPI_ERROR_UNSUPPORTED || err == NSAPI_ERROR_OK); + + err = info->get_iccid(buf, kbuf_size); + TEST_ASSERT(err == NSAPI_ERROR_UNSUPPORTED || err == NSAPI_ERROR_OK); + dev->close_information(); delete [] buf; diff --git a/features/cellular/TESTS/api/cellular_network/main.cpp b/features/cellular/TESTS/api/cellular_network/main.cpp index a4988f90e16..b918f8df1a0 100644 --- a/features/cellular/TESTS/api/cellular_network/main.cpp +++ b/features/cellular/TESTS/api/cellular_network/main.cpp @@ -180,32 +180,14 @@ static void test_other() TEST_ASSERT(nw->scan_plmn(operators, uplinkRate) == NSAPI_ERROR_OK); device->set_timeout(10 * 1000); - int rxlev = -1, ber = -1, rscp = -1, ecno = -1, rsrq = -1, rsrp = -1; - err = nw->get_extended_signal_quality(rxlev, ber, rscp, ecno, rsrq, rsrp); - TEST_ASSERT(err == NSAPI_ERROR_OK || err == NSAPI_ERROR_DEVICE_ERROR); - if (err == NSAPI_ERROR_DEVICE_ERROR) { - if (strcmp(devi, "QUECTEL_BG96") != 0 && strcmp(devi, "TELIT_HE910") != 0) {// QUECTEL_BG96 does not give any specific reason for device error - TEST_ASSERT((((AT_CellularNetwork *)nw)->get_device_error().errType == 3) && // 3 == CME error from the modem - ((((AT_CellularNetwork *)nw)->get_device_error().errCode == 100) || // 100 == unknown command for modem - (((AT_CellularNetwork *)nw)->get_device_error().errCode == 50))); // 50 == incorrect parameters // seen in wise_1570 for not supported commands - } - } else { - // we should have some values which are not optional - TEST_ASSERT(rxlev >= 0 && ber >= 0 && rscp >= 0 && ecno >= 0 && rsrq >= 0 && rsrp >= 0); - } - int rssi = -1; - ber = -1; - err = nw->get_signal_quality(rssi, ber); + int ber = -1; + err = nw->get_signal_quality(rssi, &ber); TEST_ASSERT(err == NSAPI_ERROR_OK || err == NSAPI_ERROR_DEVICE_ERROR); if (err == NSAPI_ERROR_DEVICE_ERROR) { TEST_ASSERT((((AT_CellularNetwork *)nw)->get_device_error().errType == 3) && // 3 == CME error from the modem ((((AT_CellularNetwork *)nw)->get_device_error().errCode == 100) || // 100 == unknown command for modem (((AT_CellularNetwork *)nw)->get_device_error().errCode == 50))); // 50 == incorrect parameters // seen in wise_1570 for not supported commands - } else { - // test for values - TEST_ASSERT(rssi >= 0); - TEST_ASSERT(ber >= 0); } CellularNetwork::registration_params_t reg_params; @@ -242,9 +224,9 @@ static void test_other() #endif // TELIT_HE910 and QUECTEL_BG96 just gives an error and no specific error number so we can't know is this real error or that modem/network does not support the command - CellularNetwork::Supported_UE_Opt supported_opt = CellularNetwork::SUPPORTED_UE_OPT_MAX; - CellularNetwork::Preferred_UE_Opt preferred_opt = CellularNetwork::PREFERRED_UE_OPT_MAX; - err = nw->get_ciot_optimization_config(supported_opt, preferred_opt); + CellularNetwork::CIoT_Supported_Opt supported_opt = CellularNetwork::CIOT_OPT_MAX; + CellularNetwork::CIoT_Preferred_UE_Opt preferred_opt = CellularNetwork::PREFERRED_UE_OPT_MAX; + err = nw->get_ciot_ue_optimization_config(supported_opt, preferred_opt); TEST_ASSERT(err == NSAPI_ERROR_OK || err == NSAPI_ERROR_DEVICE_ERROR); if (err == NSAPI_ERROR_DEVICE_ERROR) { // if device error then we must check was that really device error or that modem/network does not support the commands @@ -254,11 +236,11 @@ static void test_other() (((AT_CellularNetwork *)nw)->get_device_error().errCode == 50))); // 50 == incorrect parameters // seen in wise_1570 for not supported commands } } else { - TEST_ASSERT(supported_opt != CellularNetwork::SUPPORTED_UE_OPT_MAX); + TEST_ASSERT(supported_opt != CellularNetwork::CIOT_OPT_MAX); TEST_ASSERT(preferred_opt != CellularNetwork::PREFERRED_UE_OPT_MAX); } - err = nw->set_ciot_optimization_config(supported_opt, preferred_opt); + err = nw->set_ciot_optimization_config(supported_opt, preferred_opt, NULL); TEST_ASSERT(err == NSAPI_ERROR_OK || err == NSAPI_ERROR_DEVICE_ERROR); if (err == NSAPI_ERROR_DEVICE_ERROR) { // if device error then we must check was that really device error or that modem/network does not support the commands diff --git a/features/cellular/TESTS/api/cellular_power/main.cpp b/features/cellular/TESTS/api/cellular_power/main.cpp deleted file mode 100644 index 70d2ccba6d3..00000000000 --- a/features/cellular/TESTS/api/cellular_power/main.cpp +++ /dev/null @@ -1,145 +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. - */ - - -#if !defined(MBED_CONF_NSAPI_PRESENT) -#error [NOT_SUPPORTED] A json configuration file is needed. Skipping this build. -#endif - -#include "CellularUtil.h" // for CELLULAR_ helper macros -#include "CellularTargets.h" - -#ifndef CELLULAR_DEVICE -#error [NOT_SUPPORTED] CELLULAR_DEVICE must be defined -#endif - -#ifndef MBED_CONF_APP_CELLULAR_SIM_PIN -#error [NOT_SUPPORTED] SIM pin code is needed. Skipping this build. -#endif - -#include "greentea-client/test_env.h" -#include "unity.h" -#include "utest.h" - -#include "mbed.h" - -#include "AT_CellularPower.h" -#include "CellularDevice.h" -#include "../../cellular_tests_common.h" -#include CELLULAR_STRINGIFY(CELLULAR_DEVICE.h) - -#define NETWORK_TIMEOUT (180*1000) - -static CellularDevice *cellular_device; - -static void urc_callback() -{ -} - -static void wait_for_power(CellularPower *pwr) -{ - nsapi_error_t err = pwr->set_device_ready_urc_cb(&urc_callback); - TEST_ASSERT(err == NSAPI_ERROR_OK || err == NSAPI_ERROR_UNSUPPORTED); - - int sanity_count = 0; - err = pwr->set_at_mode(); - while (err != NSAPI_ERROR_OK) { - sanity_count++; - wait(1); - TEST_ASSERT(sanity_count < 40); - err = pwr->set_at_mode(); - } - - TEST_ASSERT(pwr->is_device_ready() == NSAPI_ERROR_OK); - - pwr->remove_device_ready_urc_cb(&urc_callback); -} - -static void test_power_interface() -{ - const char *devi = CELLULAR_STRINGIFY(CELLULAR_DEVICE); - cellular_device = CellularDevice::get_default_instance(); - cellular_device->set_timeout(9000); - CellularPower *pwr = cellular_device->open_power(); - TEST_ASSERT(pwr != NULL); - - nsapi_error_t err = pwr->on(); - TEST_ASSERT(err == NSAPI_ERROR_OK || err == NSAPI_ERROR_UNSUPPORTED); - wait_for_power(pwr); - - TEST_ASSERT(pwr->set_power_level(1, 0) == NSAPI_ERROR_OK); - - err = pwr->reset(); - TEST_ASSERT(err == NSAPI_ERROR_OK); - wait_for_power(pwr); - - wait(1); - err = pwr->opt_power_save_mode(0, 0); - TEST_ASSERT(err == NSAPI_ERROR_OK || err == NSAPI_ERROR_DEVICE_ERROR); - if (err == NSAPI_ERROR_DEVICE_ERROR) { - if (!(strcmp(devi, "TELIT_HE910") == 0 || strcmp(devi, "QUECTEL_BG96") == 0)) { // TELIT_HE910 and QUECTEL_BG96 just gives an error and no specific error number so we can't know is this real error or that modem/network does not support the command - TEST_ASSERT(((AT_CellularPower *)pwr)->get_device_error().errCode == 100 && // 100 == unknown command for modem - ((AT_CellularPower *)pwr)->get_device_error().errType == 3); // 3 == CME error from the modem - } - } - - wait(1); - err = pwr->opt_receive_period(0, CellularPower::EDRXEUTRAN_NB_S1_mode, 3); - TEST_ASSERT(err == NSAPI_ERROR_OK || err == NSAPI_ERROR_DEVICE_ERROR); - if (err == NSAPI_ERROR_DEVICE_ERROR) { - if (!(strcmp(devi, "TELIT_HE910") == 0 || strcmp(devi, "QUECTEL_BG96") == 0)) { // TELIT_HE910 and QUECTEL_BG96 just gives an error and no specific error number so we can't know is this real error or that modem/network does not support the command - TEST_ASSERT(((AT_CellularPower *)pwr)->get_device_error().errCode == 100 && // 100 == unknown command for modem - ((AT_CellularPower *)pwr)->get_device_error().errType == 3); // 3 == CME error from the modem - } - } - - err = pwr->off(); - TEST_ASSERT(err == NSAPI_ERROR_OK || err == NSAPI_ERROR_UNSUPPORTED); - -} - -using namespace utest::v1; - -static utest::v1::status_t greentea_failure_handler(const Case *const source, const failure_t reason) -{ - greentea_case_failure_abort_handler(source, reason); - return STATUS_ABORT; -} - -static Case cases[] = { - Case("CellularPower test interface", test_power_interface, greentea_failure_handler) -}; - -static utest::v1::status_t test_setup(const size_t number_of_cases) -{ - GREENTEA_SETUP(10 * 60, "default_auto"); - return verbose_test_setup_handler(number_of_cases); -} - -static Specification specification(test_setup, cases); - -int main() -{ -#if MBED_CONF_MBED_TRACE_ENABLE - trace_open(); -#endif - int ret = Harness::run(specification); -#if MBED_CONF_MBED_TRACE_ENABLE - trace_close(); -#endif - return ret; -} diff --git a/features/cellular/TESTS/api/cellular_sim/main.cpp b/features/cellular/TESTS/api/cellular_sim/main.cpp deleted file mode 100644 index 3da5b951bb2..00000000000 --- a/features/cellular/TESTS/api/cellular_sim/main.cpp +++ /dev/null @@ -1,128 +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. - */ - - -#if !defined(MBED_CONF_NSAPI_PRESENT) -#error [NOT_SUPPORTED] A json configuration file is needed. Skipping this build. -#endif - -#include "CellularUtil.h" // for CELLULAR_ helper macros -#include "CellularTargets.h" - -#ifndef CELLULAR_DEVICE -#error [NOT_SUPPORTED] CELLULAR_DEVICE must be defined -#endif - -#ifndef MBED_CONF_APP_CELLULAR_SIM_PIN -#error [NOT_SUPPORTED] SIM pin code is needed. Skipping this build. -#endif - -#include "greentea-client/test_env.h" -#include "unity.h" -#include "utest.h" - -#include "mbed.h" - -#include "CellularContext.h" -#include "CellularDevice.h" -#include "CellularSIM.h" -#include "../../cellular_tests_common.h" -#include CELLULAR_STRINGIFY(CELLULAR_DEVICE.h) - -#define NETWORK_TIMEOUT (180*1000) - -static CellularContext *ctx; -static CellularDevice *device; - -static void init_to_device_ready_state() -{ - ctx = CellularContext::get_default_instance(); - TEST_ASSERT(ctx != NULL); - TEST_ASSERT(ctx->set_device_ready() == NSAPI_ERROR_OK); - - device = CellularDevice::get_default_instance(); - TEST_ASSERT(device != NULL); -} - -static void test_sim_interface() -{ - CellularSIM *sim = device->open_sim(); - TEST_ASSERT(sim != NULL); - - // set SIM at time out to 9000 - device->set_timeout(9000); - wait(4); // we need to wait for some time so that SIM interface is working in all modules. - // 1. test set_pin - nsapi_error_t err = sim->set_pin(MBED_CONF_APP_CELLULAR_SIM_PIN); - MBED_ASSERT(err == NSAPI_ERROR_OK); - - // 2. test set_pin_query - wait(1); - err = sim->set_pin_query(MBED_CONF_APP_CELLULAR_SIM_PIN, false); - TEST_ASSERT(err == NSAPI_ERROR_OK || err == NSAPI_ERROR_UNSUPPORTED); - - wait(1); - err = sim->set_pin_query(MBED_CONF_APP_CELLULAR_SIM_PIN, true); - TEST_ASSERT(err == NSAPI_ERROR_OK || err == NSAPI_ERROR_UNSUPPORTED); - - wait(1); - // 3. test get_sim_state - CellularSIM::SimState state; - err = sim->get_sim_state(state); - TEST_ASSERT(err == NSAPI_ERROR_OK); - TEST_ASSERT(state == CellularSIM::SimStateReady); - - wait(1); - // 4. test get_imsi - char imsi[16] = {0}; - err = sim->get_imsi(imsi); - TEST_ASSERT(err == NSAPI_ERROR_OK); - TEST_ASSERT(strlen(imsi) > 0); -} - -using namespace utest::v1; - -static utest::v1::status_t greentea_failure_handler(const Case *const source, const failure_t reason) -{ - greentea_case_failure_abort_handler(source, reason); - return STATUS_ABORT; -} - -static Case cases[] = { - Case("CellularSIM init", init_to_device_ready_state, greentea_failure_handler), - Case("CellularSIM test interface", test_sim_interface, greentea_failure_handler) -}; - -static utest::v1::status_t test_setup(const size_t number_of_cases) -{ - GREENTEA_SETUP(10 * 60, "default_auto"); - return verbose_test_setup_handler(number_of_cases); -} - -static Specification specification(test_setup, cases); - -int main() -{ -#if MBED_CONF_MBED_TRACE_ENABLE - trace_open(); -#endif - int ret = Harness::run(specification); -#if MBED_CONF_MBED_TRACE_ENABLE - trace_close(); -#endif - return ret; -} diff --git a/features/cellular/TESTS/socket/udp/template_mbed_app.json.txt b/features/cellular/TESTS/socket/udp/template_mbed_app.json.txt index be58e16e68c..983fd68dd97 100644 --- a/features/cellular/TESTS/socket/udp/template_mbed_app.json.txt +++ b/features/cellular/TESTS/socket/udp/template_mbed_app.json.txt @@ -4,6 +4,10 @@ "help": "PIN code", "value": "\"1234\"" }, + "cellular_plmn": { + "help": "plmn to select manual registration", + "value": 0 + }, "apn": { "help": "The APN string to use for this SIM/network, set to 0 if none", "value": 0 @@ -27,7 +31,6 @@ "nsapi.dns-response-wait-time": 30000, "ppp-cell-iface.apn-lookup": false, "cellular.use-apn-lookup": false, - "target.features_add": ["LWIP"], "mbed-trace.enable": false, "lwip.ipv4-enabled": true, "lwip.ipv6-enabled": true, diff --git a/features/cellular/framework/API/CellularContext.h b/features/cellular/framework/API/CellularContext.h index 9107d585e6c..9979f4f28df 100644 --- a/features/cellular/framework/API/CellularContext.h +++ b/features/cellular/framework/API/CellularContext.h @@ -19,12 +19,22 @@ #include "CellularBase.h" #include "CellularDevice.h" +#include "ControlPlane_netif.h" namespace mbed { +typedef enum pdp_type { + DEFAULT_PDP_TYPE = DEFAULT_STACK, + IPV4_PDP_TYPE = IPV4_STACK, + IPV6_PDP_TYPE = IPV6_STACK, + IPV4V6_PDP_TYPE = IPV4V6_STACK, + NON_IP_PDP_TYPE +} pdp_type_t; + class CellularContext : public CellularBase { public: + // max simultaneous PDP contexts active static const int PDP_CONTEXT_COUNT = 4; @@ -101,7 +111,6 @@ class CellularContext : public CellularBase { // friend of CellularDevice so that it's the only way to close/delete this class. friend class CellularDevice; virtual ~CellularContext() {} - public: // from NetworkInterface virtual nsapi_error_t set_blocking(bool blocking) = 0; virtual NetworkStack *get_stack() = 0; @@ -126,7 +135,7 @@ class CellularContext : public CellularBase { /** Start the interface * - * Power on the device and does the initializations for communication with the modem.. + * Initialize modem for communication. * By default this API is synchronous. API can be set to asynchronous with method set_blocking(...). * In synchronous and asynchronous mode application can get result in from callback which is set with * attach(...) @@ -211,6 +220,19 @@ class CellularContext : public CellularBase { */ virtual void set_file_handle(FileHandle *fh) = 0; + /** Set the UART serial used to communicate with the modem. Can be used to change default file handle. + * File handle set with this method will use data carrier detect to be able to detect disconnection much faster in PPP mode. + * + * @param serial UARTSerial used in communication to modem. If null then the default file handle is used. + * @param dcd_pin Pin used to set data carrier detect on/off for the given UART + * @param active_high a boolean set to true if DCD polarity is active low + */ + virtual void set_file_handle(UARTSerial *serial, PinName dcd_pin = NC, bool active_high = false) = 0; + + /** Returns the control plane AT command interface + */ + virtual ControlPlane_netif *get_cp_netif() = 0; + protected: // Device specific implementations might need these so protected enum ContextOperation { OP_INVALID = -1, @@ -230,9 +252,18 @@ class CellularContext : public CellularBase { */ virtual void cellular_callback(nsapi_event_t ev, intptr_t ptr) = 0; + /** Enable or disable hang-up detection + * + * When in PPP data pump mode, it is helpful if the FileHandle will signal hang-up via + * POLLHUP, e.g., if the DCD line is deasserted on a UART. During command mode, this + * signaling is not desired. enable_hup() controls whether this function should be + * active. + */ + virtual void enable_hup(bool enable) = 0; + // member variables needed in target override methods NetworkStack *_stack; // must be pointer because of PPP - nsapi_ip_stack_t _ip_stack_type; + pdp_type_t _pdp_type; CellularContext::AuthenticationType _authentication_type; nsapi_connection_status_t _connect_status; cell_callback_data_t _cb_data; @@ -244,6 +275,8 @@ class CellularContext : public CellularBase { const char *_apn; const char *_uname; const char *_pwd; + PinName _dcd_pin; + bool _active_high; }; } // namespace mbed diff --git a/features/cellular/framework/API/CellularDevice.h b/features/cellular/framework/API/CellularDevice.h index 4dc952f8638..77bc41cb389 100644 --- a/features/cellular/framework/API/CellularDevice.h +++ b/features/cellular/framework/API/CellularDevice.h @@ -20,19 +20,19 @@ #include "CellularTargets.h" #include "CellularStateMachine.h" +#include "Callback.h" +#include "UARTSerial.h" namespace mbed { -class CellularPower; class CellularSMS; -class CellularSIM; class CellularInformation; class CellularNetwork; class CellularContext; -class FileHandle; const int MAX_PIN_SIZE = 8; const int MAX_PLMN_SIZE = 16; +const int MAX_SIM_READY_WAITING_TIME = 30; /** * Class CellularDevice @@ -42,14 +42,30 @@ const int MAX_PLMN_SIZE = 16; */ class CellularDevice { public: - - /** Return singleton instance of CellularDevice if CELLULAR_DEVICE is defined. If CELLULAR_DEVICE is not - * defined then returns NULL. Implementation is marked as weak. + /* enumeration for possible SIM states */ + enum SimState { + SimStateReady = 0, + SimStatePinNeeded, + SimStatePukNeeded, + SimStateUnknown + }; + + /** See NetworkInterface::get_default_instance for details + * + * @remark Application may define default to override (non-weak) this function. * - * @return CellularDevice* instance if any + * @return default CellularDevice, NULL if not defined */ static CellularDevice *get_default_instance(); + /** Return target onboard instance of CellularDevice + * + * @remark Mbed OS target shall override (non-weak) this function for an onboard modem. + * + * @return CellularDevice* instance, NULL if not defined + */ + static CellularDevice *get_target_default_instance(); + /** Default constructor * * @param fh File handle used in communication with the modem. @@ -60,16 +76,114 @@ class CellularDevice { */ virtual ~CellularDevice(); + /** Sets the modem up for powering on + * This is equivalent to plugging in the device, i.e., attaching power and serial port. + * In general, hard_power_on and soft_power_on provides a simple hardware abstraction layer + * on top of the modem drivers written for Mbed OS; they can be overridden + * in a derived class to perform custom power controls in a particular board configuration. + * In many boards this will be a no-op if there is no separate power supply control circuitry. + * + * @remark CellularStateMachine calls hard_power_on at first until successful, + * then soft_power_on and init until the modem responds. + * If you are not using CellularStateMachine then you need to call these functions yourself. + * + * @post You must call soft_power_on to power on the modem after calling hard_power_on. + * + * @return NSAPI_ERROR_OK on success + */ + virtual nsapi_error_t hard_power_on() = 0; + + /** Sets the modem in unplugged state + * + * This is equivalent to pulling the plug off of the device, i.e., + * detaching power and serial port. + * + * This puts the modem in the lowest power state. + * + * @remark CellularStateMachine disconnect or destruct does not shutdown or power off the modem, + * but you need to do that yourself. + * + * @pre You must call soft_power_off to power off the modem before calling hard_power_off. + * + * @return NSAPI_ERROR_OK on success + */ + virtual nsapi_error_t hard_power_off() = 0; + + /** Powers up the modem + * + * This is equivalent to pressing the "power button" to activate or reset the modem + * and usually implemented as a short pulse on a dedicated GPIO signal. + * It is expected to be present to make it possible to reset the modem. + * The driver may repeat this if the modem is not responsive to AT commands. + * + * @remark CellularStateMachine calls this when requested to connect. + * If you are not using CellularStateMachine then you need to call this function yourself. + * + * @post You must call init to setup the modem. + * + * @return NSAPI_ERROR_OK on success + */ + virtual nsapi_error_t soft_power_on() = 0; + + /** Powers down the modem + * + * This is equivalent to turning off the modem by button press. + * + * @remark CellularStateMachine disconnect or destruct does not shutdown or power off the modem, + * but you need to do that yourself. + * + * @pre You must call shutdown to prepare the modem for power off. + * + * @return NSAPI_ERROR_OK on success + */ + virtual nsapi_error_t soft_power_off() = 0; + + /** Open the SIM card by setting the pin code for SIM. + * + * @param sim_pin PIN for the SIM card + * @return NSAPI_ERROR_OK on success + * NSAPI_ERROR_PARAMETER if sim_pin is null and sim is not ready + * NSAPI_ERROR_DEVICE_ERROR on failure + */ + virtual nsapi_error_t set_pin(const char *sim_pin) = 0; + + /** Get SIM card's state + * + * @param state current state of SIM + * @return NSAPI_ERROR_OK on success + * NSAPI_ERROR_DEVICE_ERROR on failure + */ + virtual nsapi_error_t get_sim_state(SimState &state) = 0; + /** Creates a new CellularContext interface. * - * @param fh file handle used in communication to modem. Can be for example UART handle. If null then the default - * file handle is used. - * @param apn access point to use with context, can be null. + * @param fh file handle used in communication to modem. Can be for example UART handle. If null then the default + * file handle is used. + * @param apn access point to use with context, can be null. + * @param cp_req flag indicating if EPS control plane optimisation is required + * @param nonip_req flag indicating if this context is required to be Non-IP + * + * @return new instance of class CellularContext or NULL in case of failure + * + */ + virtual CellularContext *create_context(FileHandle *fh = NULL, const char *apn = NULL, bool cp_req = false, bool nonip_req = false) = 0; + + /** Creates a new CellularContext interface. This API should be used if serial is UART and PPP mode used. + * CellularContext created will use data carrier detect to be able to detect disconnection much faster in PPP mode. + * UARTSerial usually is the same which was given for the CellularDevice. + * + * @param serial UARTSerial used in communication to modem. If null then the default file handle is used. + * @param apn access point to use with context, can be null. + * @param dcd_pin Pin used to set data carrier detect on/off for the given UART + * @param active_high a boolean set to true if DCD polarity is active low + * @param cp_req Flag indicating if EPS control plane optimisation is required + * @param nonip_req Flag indicating if this context is required to be Non-IP * * @return new instance of class CellularContext or NULL in case of failure * */ - virtual CellularContext *create_context(FileHandle *fh = NULL, const char *apn = NULL) = 0; + virtual CellularContext *create_context(UARTSerial *serial, const char *apn, PinName dcd_pin = NC, + bool active_high = false, bool cp_req = false, bool nonip_req = false) = 0; /** Deletes the given CellularContext instance * @@ -82,6 +196,12 @@ class CellularDevice { */ void stop(); + /** Get the current FileHandle item used when communicating with the modem. + * + * @return reference to FileHandle + */ + FileHandle &get_file_handle() const; + /** Get event queue that can be chained to main event queue. * @return event queue */ @@ -103,7 +223,7 @@ class CellularDevice { /** Start the interface * - * Power on the device and does the initializations for communication with the modem.. + * Initialize modem for communication. * By default this API is synchronous. API can be set to asynchronous with method set_blocking(...). * In synchronous and asynchronous mode application can get result in from callback which is set with * attach(...) @@ -149,6 +269,19 @@ class CellularDevice { */ nsapi_error_t attach_to_network(); + /** Register callback for status reporting. + * + * The specified status callback function will be called on the network and cellular device status changes. + * The parameters on the callback are the event type and event-type dependent reason parameter. + * + * @remark deleting CellularDevice/CellularContext in callback not allowed. + * @remark application should not attach to this function if using CellularContext::attach as it will contain the + * same information. + * + * @param status_cb The callback for status changes. + */ + void attach(Callback status_cb); + /** Create new CellularNetwork interface. * * @param fh file handle used in communication to modem. Can be for example UART handle. If null then the default @@ -165,22 +298,6 @@ class CellularDevice { */ virtual CellularSMS *open_sms(FileHandle *fh = NULL) = 0; - /** Create new CellularPower interface. - * - * @param fh file handle used in communication to modem. Can be for example UART handle. If null then the default - * file handle is used. - * @return New instance of interface CellularPower. - */ - virtual CellularPower *open_power(FileHandle *fh = NULL) = 0; - - /** Create new CellularSIM interface. - * - * @param fh file handle used in communication to modem. Can be for example UART handle. If null then the default - * file handle is used. - * @return New instance of interface CellularSIM. - */ - virtual CellularSIM *open_sim(FileHandle *fh = NULL) = 0; - /** Create new CellularInformation interface. * * @param fh file handle used in communication to modem. Can be for example UART handle. If null then the default @@ -197,14 +314,6 @@ class CellularDevice { */ virtual void close_sms() = 0; - /** Closes the opened CellularPower by deleting the CellularPower instance. - */ - virtual void close_power() = 0; - - /** Closes the opened CellularSIM by deleting the CellularSIM instance. - */ - virtual void close_sim() = 0; - /** Closes the opened CellularInformation by deleting the CellularInformation instance. */ virtual void close_information() = 0; @@ -221,17 +330,58 @@ class CellularDevice { */ virtual void modem_debug_on(bool on) = 0; - /** Initialize cellular module must be called right after module is ready. - * For example, when multiple modules are supported in a single AT driver this function detects - * and adapts to an actual module at runtime. + /** Initialize cellular device must be called right after the module is ready. + * + * For example, when multiple cellular modules are supported in a single driver this function + * detects and adapts to an actual module at runtime. + * + * @remark CellularStateMachine calls soft_power_on and init repeatedly when starting to connect + * until the modem responds. * * @return NSAPI_ERROR_OK on success * NSAPI_ERROR_NO_MEMORY on case of memory failure - * NSAPI_ERROR_UNSUPPORTED if current model is not detected + * NSAPI_ERROR_UNSUPPORTED if current cellular module type is not detected * NSAPI_ERROR_DEVICE_ERROR if model information could not be read * */ - virtual nsapi_error_t init_module() = 0; + virtual nsapi_error_t init() = 0; + + /** Shutdown cellular device to minimum functionality. + * + * Actual functionality is modem specific, for example UART may is not be responsive without + * explicit wakeup signal (such as RTS) after shutdown. + * + * @remark You must call shutdown before power off to prepare the modem and to quit cellular network. + * + * @return NSAPI_ERROR_OK on success + * NSAPI_ERROR_DEVICE_ERROR on failure + */ + virtual nsapi_error_t shutdown(); + + /** Check whether the device is ready to accept commands. + * + * @return NSAPI_ERROR_OK on success + * NSAPI_ERROR_DEVICE_ERROR on failure + */ + virtual nsapi_error_t is_ready() = 0; + + /** Set callback function to listen when device is ready. + * + * @param callback function to call on device ready, or NULL to remove callback. + */ + virtual void set_ready_cb(Callback callback) = 0; + + /** Set power save mode + * + * @remark See 3GPP TS 27.007 PSM for details + * + * @param periodic_time in seconds to enable power save, or zero to disable + * @param active_time in seconds to wait before entering power save mode + * + * @return NSAPI_ERROR_OK on success + * NSAPI_ERROR_DEVICE_ERROR on failure + */ + virtual nsapi_error_t set_power_save_mode(int periodic_time, int active_time = 0) = 0; /** Get the linked list of CellularContext instances * @@ -252,8 +402,6 @@ class CellularDevice { int _network_ref_count; int _sms_ref_count; - int _power_ref_count; - int _sim_ref_count; int _info_ref_count; FileHandle *_fh; events::EventQueue _queue; @@ -266,6 +414,7 @@ class CellularDevice { char _sim_pin[MAX_PIN_SIZE + 1]; char _plmn[MAX_PLMN_SIZE + 1]; PlatformMutex _mutex; + Callback _status_cb; }; } // namespace mbed diff --git a/features/cellular/framework/API/CellularInformation.h b/features/cellular/framework/API/CellularInformation.h index 63c02c00913..7f5f693dcf3 100644 --- a/features/cellular/framework/API/CellularInformation.h +++ b/features/cellular/framework/API/CellularInformation.h @@ -21,6 +21,9 @@ #include #include "nsapi_types.h" +const int MAX_IMSI_LENGTH = 15; +const int MAX_ICCID_LENGTH = 20 + 1; // +1 for zero termination + namespace mbed { /** @@ -43,6 +46,7 @@ class CellularInformation { * @param buf manufacturer identification as zero terminated string * @param buf_size max length of manufacturer identification is 2048 characters * @return NSAPI_ERROR_OK on success + * NSAPI_ERROR_PARAMETER if buf is null or buf_size is zero * NSAPI_ERROR_DEVICE_ERROR on failure */ virtual nsapi_error_t get_manufacturer(char *buf, size_t buf_size) = 0; @@ -52,6 +56,7 @@ class CellularInformation { * @param buf model identification as zero terminated string * @param buf_size max length of model identification is 2048 characters * @return NSAPI_ERROR_OK on success + * NSAPI_ERROR_PARAMETER if buf is null or buf_size is zero * NSAPI_ERROR_DEVICE_ERROR on failure */ virtual nsapi_error_t get_model(char *buf, size_t buf_size) = 0; @@ -61,6 +66,7 @@ class CellularInformation { * @param buf revision identification as zero terminated string * @param buf_size max length of revision identification is 2048 characters * @return NSAPI_ERROR_OK on success + * NSAPI_ERROR_PARAMETER if buf is null or buf_size is zero * NSAPI_ERROR_DEVICE_ERROR on failure */ virtual nsapi_error_t get_revision(char *buf, size_t buf_size) = 0; @@ -71,6 +77,7 @@ class CellularInformation { * @param buf_size max length of serial number is 2048 characters * @param type serial number type to read * @return NSAPI_ERROR_OK on success + * NSAPI_ERROR_PARAMETER if buf is null or buf_size is zero * NSAPI_ERROR_UNSUPPORTED if the modem does not support SerialNumberType * NSAPI_ERROR_DEVICE_ERROR on other failures */ @@ -80,7 +87,30 @@ class CellularInformation { IMEISV = 2, // IMEI and Software Version number SVN = 3 // Software Version Number }; - virtual nsapi_error_t get_serial_number(char *buf, size_t buf_size, SerialNumberType type = SN) = 0; + virtual nsapi_size_or_error_t get_serial_number(char *buf, size_t buf_size, SerialNumberType type = SN) = 0; + + /** Get IMSI from the sim card + * + * @remark Given imsi buffer length must be 16 or more as imsi max length is 15! + * + * @param imsi preallocated char* which after successful request contains imsi + * @param buf_size size of imsi buffer + * @return NSAPI_ERROR_OK on success + * NSAPI_ERROR_PARAMETER if imsi is null or buf_size is zero or buf_size is smaller than + * MAX_IMSI_LENGTH + 1 + * NSAPI_ERROR_DEVICE_ERROR on other failures + */ + virtual nsapi_error_t get_imsi(char *imsi, size_t buf_size) = 0; + + /** Get serial number from the SIM card + * + * @param buf SIM ICCID as zero terminated string + * @param buf_size max length of SIM ICCID is MAX_ICCID_LENGTH + * @return NSAPI_ERROR_OK on success + * NSAPI_ERROR_PARAMETER if buf is null or buf_size is zero + * NSAPI_ERROR_DEVICE_ERROR on failure + */ + virtual nsapi_error_t get_iccid(char *buf, size_t buf_size) = 0; }; } // namespace mbed diff --git a/features/cellular/framework/API/CellularNetwork.h b/features/cellular/framework/API/CellularNetwork.h index ec6f3b74a13..40cf169c79b 100644 --- a/features/cellular/framework/API/CellularNetwork.h +++ b/features/cellular/framework/API/CellularNetwork.h @@ -48,17 +48,17 @@ class CellularNetwork { public: /* Definition for Supported CIoT EPS optimizations type. */ - enum Supported_UE_Opt { - SUPPORTED_UE_OPT_NO_SUPPORT = 0, /* No support. */ - SUPPORTED_UE_OPT_CONTROL_PLANE, /* Support for control plane CIoT EPS optimization. */ - SUPPORTED_UE_OPT_USER_PLANE, /* Support for user plane CIoT EPS optimization. */ - SUPPORTED_UE_OPT_BOTH, /* Support for both control plane CIoT EPS optimization and user plane CIoT EPS + enum CIoT_Supported_Opt { + CIOT_OPT_NO_SUPPORT = 0, /* No support. */ + CIOT_OPT_CONTROL_PLANE, /* Support for control plane CIoT EPS optimization. */ + CIOT_OPT_USER_PLANE, /* Support for user plane CIoT EPS optimization. */ + CIOT_OPT_BOTH, /* Support for both control plane CIoT EPS optimization and user plane CIoT EPS optimization. */ - SUPPORTED_UE_OPT_MAX + CIOT_OPT_MAX }; /* Definition for Preferred CIoT EPS optimizations type. */ - enum Preferred_UE_Opt { + enum CIoT_Preferred_UE_Opt { PREFERRED_UE_OPT_NO_PREFERENCE = 0, /* No preference. */ PREFERRED_UE_OPT_CONTROL_PLANE, /* Preference for control plane CIoT EPS optimization. */ PREFERRED_UE_OPT_USER_PLANE, /* Preference for user plane CIoT EPS optimization. */ @@ -259,45 +259,47 @@ class CellularNetwork { /** Set CIoT optimizations. * - * @param supported_opt Supported CIoT EPS optimizations. + * @param supported_opt Supported CIoT EPS optimizations + * (the HW support can be checked with get_ciot_ue_optimization_config). * @param preferred_opt Preferred CIoT EPS optimizations. + * @param network_support_cb This callback will be called when CIoT network optimisation support is known * @return NSAPI_ERROR_OK on success * NSAPI_ERROR_DEVICE_ERROR on failure */ - virtual nsapi_error_t set_ciot_optimization_config(Supported_UE_Opt supported_opt, - Preferred_UE_Opt preferred_opt) = 0; + virtual nsapi_error_t set_ciot_optimization_config(CIoT_Supported_Opt supported_opt, + CIoT_Preferred_UE_Opt preferred_opt, + Callback network_support_cb) = 0; - /** Get CIoT optimizations. + /** Get UE CIoT optimizations. * * @param supported_opt Supported CIoT EPS optimizations. * @param preferred_opt Preferred CIoT EPS optimizations. * @return NSAPI_ERROR_OK on success * NSAPI_ERROR_DEVICE_ERROR on failure */ - virtual nsapi_error_t get_ciot_optimization_config(Supported_UE_Opt &supported_opt, - Preferred_UE_Opt &preferred_opt) = 0; + virtual nsapi_error_t get_ciot_ue_optimization_config(CIoT_Supported_Opt &supported_opt, + CIoT_Preferred_UE_Opt &preferred_opt) = 0; - /** Get extended signal quality parameters. + /** Get Network CIoT optimizations. * - * @param rxlev signal strength level - * @param ber bit error rate - * @param rscp signal code power - * @param ecno ratio of the received energy per PN chip to the total received power spectral density - * @param rsrq signal received quality - * @param rsrp signal received power + * @param supported_network_opt Supported CIoT EPS optimizations. CIOT_OPT_MAX will be returned, + * if the support is not known * @return NSAPI_ERROR_OK on success - * NSAPI_ERROR_DEVICE_ERROR on other failures + * NSAPI_ERROR_DEVICE_ERROR on failure */ - virtual nsapi_error_t get_extended_signal_quality(int &rxlev, int &ber, int &rscp, int &ecno, int &rsrq, int &rsrp) = 0; + virtual nsapi_error_t get_ciot_network_optimization_config(CIoT_Supported_Opt &supported_network_opt) = 0; /** Get signal quality parameters. * - * @param rssi signal strength level - * @param ber bit error rate + * @param rssi signal strength level as defined in 3GPP TS 27.007, range -113..-51 dBm or SignalQualityUnknown + * @param ber bit error rate as RXQUAL as defined in 3GPP TS 45.008, range 0..7 or SignalQualityUnknown * @return NSAPI_ERROR_OK on success * NSAPI_ERROR_DEVICE_ERROR on other failures */ - virtual nsapi_error_t get_signal_quality(int &rssi, int &ber) = 0; + enum SignalQuality { + SignalQualityUnknown = 99 + }; + virtual nsapi_error_t get_signal_quality(int &rssi, int *ber = NULL) = 0; /** Get the last 3GPP error code * @return see 3GPP TS 27.007 error codes @@ -363,6 +365,27 @@ class CellularNetwork { * NSAPI_ERROR_DEVICE_ERROR on failure */ virtual nsapi_error_t get_registration_params(RegistrationType type, registration_params_t ®_params) = 0; + + /** Set discontinuous reception time on cellular device. + * + * @remark See 3GPP TS 27.007 eDRX for details. + * + * @param mode disable or enable the use of eDRX + * @param act_type type of access technology + * @param edrx_value requested edxr value. Extended DRX parameters information element. + * + * @return NSAPI_ERROR_OK on success + * NSAPI_ERROR_DEVICE_ERROR on failure + */ + enum EDRXAccessTechnology { + EDRXGSM_EC_GSM_IoT_mode = 1, + EDRXGSM_A_Gb_mode, + EDRXUTRAN_Iu_mode, + EDRXEUTRAN_WB_S1_mode, + EDRXEUTRAN_NB_S1_mode + }; + virtual nsapi_error_t set_receive_period(int mode, EDRXAccessTechnology act_type, uint8_t edrx_value) = 0; + }; } // namespace mbed diff --git a/features/cellular/framework/API/CellularPower.h b/features/cellular/framework/API/CellularPower.h deleted file mode 100644 index 8fea860fd4f..00000000000 --- a/features/cellular/framework/API/CellularPower.h +++ /dev/null @@ -1,158 +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. - */ -#ifndef CELLULAR_API_CELLULARPOWER_H_ -#define CELLULAR_API_CELLULARPOWER_H_ - -#include "nsapi_types.h" -#include "Callback.h" - -namespace mbed { - -/** - * Class CellularPower - * - * An interface that provides power handling functions for modem/module. - */ -class CellularPower { -protected: - // friend of CellularDevice so that it's the only way to close/delete this class. - friend class CellularDevice; - - /** - * virtual Destructor - */ - virtual ~CellularPower() {} - -public: - /* Access technology used in method opt_receive_period */ - enum EDRXAccessTechnology { - EDRXGSM_EC_GSM_IoT_mode = 1, - EDRXGSM_A_Gb_mode, - EDRXUTRAN_Iu_mode, - EDRXEUTRAN_WB_S1_mode, - EDRXEUTRAN_NB_S1_mode - }; - - /** Set cellular device power on. Default implementation is empty. - * Device power on/off is modem/board specific behavior and must be done on inherited class if needed. - * Power on is done by toggling power pin/button. - * - * @remark set_at_mode must be called to initialise modem - * - * @return NSAPI_ERROR_OK on success - * NSAPI_ERROR_UNSUPPORTED if not overridden by the target modem - */ - virtual nsapi_error_t on() = 0; - - /** Set cellular device power off. Default implementation is empty. - * Device power on/off is modem/board specific behavior and must be done on inherited class if needed. - * Power off is done by toggling power pin/button. - * - * @return NSAPI_ERROR_OK on success - * NSAPI_ERROR_UNSUPPORTED if not overridden by the target modem - */ - virtual nsapi_error_t off() = 0; - - /** Set AT command mode. Blocking until success or failure. - * - * @remark must be called after power on to prepare correct AT mode - * - * @return NSAPI_ERROR_OK on success - * NSAPI_ERROR_DEVICE_ERROR on failure - */ - virtual nsapi_error_t set_at_mode() = 0; - - /** Set cellular device power level by enabling/disabling functionality. - * - * @param func_level: - * 0 minimum functionality - * 1 full functionality. Enable (turn on) the transmit and receive RF circuits for all supported radio access technologies. - * For MTs supporting +CSRA, this equals the RATs indicated by the response of +CSRA=?. Current +CSRA setting is ignored. - * It is not required that the MT transmit and receive RF circuits are in a disabled state for this setting to have effect. - * 2 disable (turn off) MT transmit RF circuits only - * 3 disable (turn off) MT receive RF circuits only - * 4 disable (turn off) both MT transmit and receive RF circuits - * @param do_reset 0 for do not reset, 1 for reset the device when changing the functionality - * - * @remark See 3GPP TS 27.007 CFUN for more details - * - * @return NSAPI_ERROR_OK on success - * NSAPI_ERROR_DEVICE_ERROR on failure - */ - virtual nsapi_error_t set_power_level(int func_level, int do_reset = 0) = 0; - - /** Reset and wake-up cellular device. - * - * @return NSAPI_ERROR_OK on success - * NSAPI_ERROR_DEVICE_ERROR on failure - */ - virtual nsapi_error_t reset() = 0; - - /** Opt for power save setting on cellular device. If both parameters are zero, this disables PSM. - * - * @remark See 3GPP TS 27.007 PSM for details - * - * @param periodic_time Timeout in seconds IoT subsystem is not expecting messaging - * @param active_time Timeout in seconds IoT subsystem waits for response - * - * @return NSAPI_ERROR_OK on success - * NSAPI_ERROR_DEVICE_ERROR on failure - */ - virtual nsapi_error_t opt_power_save_mode(int periodic_time, int active_time) = 0; - - /** Opt for discontinuous reception on cellular device. - * - * @remark See 3GPP TS 27.007 eDRX for details. - * - * @param mode disable or enable the use of eDRX - * @param act_type type of access technology - * @param edrx_value requested edxr value. Extended DRX parameters information element. - * - * @return NSAPI_ERROR_OK on success - * NSAPI_ERROR_DEVICE_ERROR on failure - */ - virtual nsapi_error_t opt_receive_period(int mode, EDRXAccessTechnology act_type, uint8_t edrx_value) = 0; - - /** Check whether the device is ready to accept commands. - * - * @return NSAPI_ERROR_OK on success - * NSAPI_ERROR_DEVICE_ERROR on failure - */ - virtual nsapi_error_t is_device_ready() = 0; - - /** Set URC callback function for device specific ready urc. URC is defined in device specific - * power API. Used in startup sequence to listen when device is ready - * for using at commands and possible sim. - * - * @param callback Callback function called when urc received - * - * @return NSAPI_ERROR_OK on success - * NSAPI_ERROR_NO_MEMORY on memory failure - * NSAPI_ERROR_UNSUPPORTED if not overridden by the target modem - */ - virtual nsapi_error_t set_device_ready_urc_cb(mbed::Callback callback) = 0; - - /** Removes the device ready urc from the list of urc's. - * - * @param callback callback to remove from the list of urc's - */ - virtual void remove_device_ready_urc_cb(mbed::Callback callback) = 0; -}; - -} // namespace mbed - -#endif /* CELLULAR_API_CELLULARPOWER_H_ */ diff --git a/features/cellular/framework/API/CellularSIM.h b/features/cellular/framework/API/CellularSIM.h deleted file mode 100644 index 13ff7d37009..00000000000 --- a/features/cellular/framework/API/CellularSIM.h +++ /dev/null @@ -1,110 +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. - */ - -#ifndef CELLULAR_SIM_H_ -#define CELLULAR_SIM_H_ - -#include - -#include "nsapi_types.h" - -namespace mbed { - -const int MAX_SIM_READY_WAITING_TIME = 30; -const int MAX_IMSI_LENGTH = 15; -const int MAX_ICCID_LENGTH = 20 + 1; // +1 for zero termination -/** - * Class CellularSIM - * - * An abstract interface for SIM card handling. - */ -class CellularSIM { -protected: - // friend of CellularDevice so that it's the only way to close/delete this class. - friend class CellularDevice; - - /** - * virtual Destructor - */ - virtual ~CellularSIM() {}; - -public: - /* enumeration for possible SIM states */ - enum SimState { - SimStateReady = 0, - SimStatePinNeeded, - SimStatePukNeeded, - SimStateUnknown - }; - - /** Open the SIM card by setting the pin code for SIM. - * - * @param sim_pin PIN for the SIM card - * @return NSAPI_ERROR_OK on success - * NSAPI_ERROR_DEVICE_ERROR on failure - */ - virtual nsapi_error_t set_pin(const char *sim_pin) = 0; - - /** Change SIM pin code. - * - * @param sim_pin Current PIN for SIM - * @param new_pin New PIN for SIM - * @return NSAPI_ERROR_OK on success - * NSAPI_ERROR_DEVICE_ERROR on failure - */ - virtual nsapi_error_t change_pin(const char *sim_pin, const char *new_pin) = 0; - - /** Change is pin query needed after boot - * - * @param sim_pin Valid PIN for SIM card - * @param query_pin False if PIN query not needed, True if PIN query needed after boot. - * @return NSAPI_ERROR_OK on success - * NSAPI_ERROR_DEVICE_ERROR on failure - */ - virtual nsapi_error_t set_pin_query(const char *sim_pin, bool query_pin) = 0; - - /** Get SIM card's state - * - * @param state current state of SIM - * @return NSAPI_ERROR_OK on success - * NSAPI_ERROR_DEVICE_ERROR on failure - */ - virtual nsapi_error_t get_sim_state(SimState &state) = 0; - - /** Get IMSI from the sim card - * @remark Given imsi buffer length must be 16 or more as imsi max length is 15! - * - * @param imsi preallocated char* which after successful request contains imsi - * @return NSAPI_ERROR_OK on success - * NSAPI_ERROR_PARAMETER if imsi if null - * NSAPI_ERROR_DEVICE_ERROR on other failures - */ - virtual nsapi_error_t get_imsi(char *imsi) = 0; - - /** Get serial number from the SIM card - * - * @param buf SIM ICCID as zero terminated string - * @param buf_size max length of SIM ICCID is MAX_ICCID_LENGTH - * @return NSAPI_ERROR_OK on success - * NSAPI_ERROR_DEVICE_ERROR on failure - */ - virtual nsapi_error_t get_iccid(char *buf, size_t buf_size) = 0; -}; - -} // namespace mbed - -#endif // CELLULAR_SIM_H_ diff --git a/features/cellular/framework/AT/ATHandler.cpp b/features/cellular/framework/AT/ATHandler.cpp index bbb278be810..589f9af6b0e 100644 --- a/features/cellular/framework/AT/ATHandler.cpp +++ b/features/cellular/framework/AT/ATHandler.cpp @@ -90,11 +90,7 @@ ATHandler::ATHandler(FileHandle *fh, EventQueue &queue, int timeout, const char if (output_delimiter) { _output_delimiter = new char[strlen(output_delimiter) + 1]; - if (!_output_delimiter) { - MBED_ASSERT(0); - } else { - memcpy(_output_delimiter, output_delimiter, strlen(output_delimiter) + 1); - } + memcpy(_output_delimiter, output_delimiter, strlen(output_delimiter) + 1); } else { _output_delimiter = NULL; } @@ -158,35 +154,38 @@ void ATHandler::set_file_handle(FileHandle *fh) void ATHandler::set_is_filehandle_usable(bool usable) { _is_fh_usable = usable; + if (usable) { + // set file handle sigio and blocking mode back + set_file_handle(_fileHandle); + } } -nsapi_error_t ATHandler::set_urc_handler(const char *prefix, mbed::Callback callback) +void ATHandler::set_urc_handler(const char *prefix, Callback callback) { + if (!callback) { + remove_urc_handler(prefix); + return; + } + if (find_urc_handler(prefix)) { tr_warn("URC already added with prefix: %s", prefix); - return NSAPI_ERROR_OK; + return; } struct oob_t *oob = new struct oob_t; - if (!oob) { - return NSAPI_ERROR_NO_MEMORY; - } else { - size_t prefix_len = strlen(prefix); - if (prefix_len > _oob_string_max_length) { - _oob_string_max_length = prefix_len; - if (_oob_string_max_length > _max_resp_length) { - _max_resp_length = _oob_string_max_length; - } + size_t prefix_len = strlen(prefix); + if (prefix_len > _oob_string_max_length) { + _oob_string_max_length = prefix_len; + if (_oob_string_max_length > _max_resp_length) { + _max_resp_length = _oob_string_max_length; } - - oob->prefix = prefix; - oob->prefix_len = prefix_len; - oob->cb = callback; - oob->next = _oobs; - _oobs = oob; } - return NSAPI_ERROR_OK; + oob->prefix = prefix; + oob->prefix_len = prefix_len; + oob->cb = callback; + oob->next = _oobs; + _oobs = oob; } void ATHandler::remove_urc_handler(const char *prefix) @@ -311,7 +310,7 @@ void ATHandler::process_oob() void ATHandler::set_filehandle_sigio() { - _fileHandle->sigio(mbed::Callback(this, &ATHandler::event)); + _fileHandle->sigio(Callback(this, &ATHandler::event)); } void ATHandler::reset_buffer() diff --git a/features/cellular/framework/AT/ATHandler.h b/features/cellular/framework/AT/ATHandler.h index 3d7a128ca50..30712144c2c 100644 --- a/features/cellular/framework/AT/ATHandler.h +++ b/features/cellular/framework/AT/ATHandler.h @@ -97,20 +97,12 @@ class ATHandler { */ nsapi_error_t unlock_return_error(); - /** Set the urc callback for urc. If urc is found when parsing AT responses, then call if called. - * If urc is already set then it's not set twice. + /** Set callback function for URC * - * @param prefix Register urc prefix for callback. Urc could be for example "+CMTI: " - * @param callback Callback, which is called if urc is found in AT response - * @return NSAPI_ERROR_OK or NSAPI_ERROR_NO_MEMORY if no memory - */ - nsapi_error_t set_urc_handler(const char *prefix, mbed::Callback callback); - - /** Remove urc handler from linked list of urc's - * - * @param prefix Register urc prefix for callback. Urc could be for example "+CMTI: " + * @param prefix URC text to look for, e.g. "+CMTI:" + * @param callback function to call on prefix, or 0 to remove callback */ - void remove_urc_handler(const char *prefix); + void set_urc_handler(const char *prefix, Callback callback); ATHandler *_nextATHandler; // linked list @@ -195,6 +187,11 @@ class ATHandler { #endif FileHandle *_fileHandle; private: + /** Remove urc handler from linked list of urc's + * + * @param prefix Register urc prefix for callback. Urc could be for example "+CMTI: " + */ + void remove_urc_handler(const char *prefix); void set_error(nsapi_error_t err); @@ -208,7 +205,7 @@ class ATHandler { struct oob_t { const char *prefix; int prefix_len; - mbed::Callback cb; + Callback cb; oob_t *next; }; oob_t *_oobs; diff --git a/features/cellular/framework/AT/AT_CellularBase.cpp b/features/cellular/framework/AT/AT_CellularBase.cpp index f1225fcfa6e..8d9ab8e1f8e 100644 --- a/features/cellular/framework/AT/AT_CellularBase.cpp +++ b/features/cellular/framework/AT/AT_CellularBase.cpp @@ -34,25 +34,19 @@ device_err_t AT_CellularBase::get_device_error() const return _at.get_last_device_error(); } -AT_CellularBase::SupportedFeature const *AT_CellularBase::_unsupported_features; +const intptr_t *AT_CellularBase::_property_array; -void AT_CellularBase::set_unsupported_features(const SupportedFeature *unsupported_features) +void AT_CellularBase::set_cellular_properties(const intptr_t *property_array) { - _unsupported_features = unsupported_features; -} - -bool AT_CellularBase::is_supported(SupportedFeature feature) -{ - if (!_unsupported_features) { - return true; + if (!property_array) { + tr_warning("trying to set an empty cellular property array"); + return; } - for (int i = 0; _unsupported_features[i] != SUPPORTED_FEATURE_END_MARK; i++) { - if (_unsupported_features[i] == feature) { - tr_debug("Unsupported feature (%d)", (int)feature); - return false; - } - } + _property_array = property_array; +} - return true; +intptr_t AT_CellularBase::get_property(CellularProperty key) +{ + return _property_array[key]; } diff --git a/features/cellular/framework/AT/AT_CellularBase.h b/features/cellular/framework/AT/AT_CellularBase.h index 218167c8632..4a993f40668 100644 --- a/features/cellular/framework/AT/AT_CellularBase.h +++ b/features/cellular/framework/AT/AT_CellularBase.h @@ -42,30 +42,38 @@ class AT_CellularBase { */ device_err_t get_device_error() const; - /** Cellular module need to define an array of unsupported features if any, - * by default all features are supported. + enum CellularProperty { + PROPERTY_C_EREG, // AT_CellularNetwork::RegistrationMode. What support modem has for this registration type. + PROPERTY_C_GREG, // AT_CellularNetwork::RegistrationMode. What support modem has for this registration type. + PROPERTY_C_REG, // AT_CellularNetwork::RegistrationMode. What support modem has for this registration type. + PROPERTY_AT_CGSN_WITH_TYPE, // 0 = not supported, 1 = supported. AT+CGSN without type is likely always supported similar to AT+GSN. + PROPERTY_AT_CGDATA, // 0 = not supported, 1 = supported. Alternative is to support only ATD*99***# + PROPERTY_AT_CGAUTH, // 0 = not supported, 1 = supported. APN authentication AT commands supported + PROPERTY_IPV4_PDP_TYPE, // 0 = not supported, 1 = supported. Does modem support IPV4? + PROPERTY_IPV6_PDP_TYPE, // 0 = not supported, 1 = supported. Does modem support IPV6? + PROPERTY_IPV4V6_PDP_TYPE, // 0 = not supported, 1 = supported. Does modem support dual stack IPV4V6? + PROPERTY_NON_IP_PDP_TYPE, // 0 = not supported, 1 = supported. Does modem support Non-IP? + PROPERTY_MAX + }; + + /** Cellular module need to define an array of cellular properties which defines module supported property values. * - * @param features Array of type SupportedFeature with last element FEATURE_END_MARK + * @param property_array array of module properties */ - enum SupportedFeature { - AT_CGSN_WITH_TYPE, // AT+CGSN without type is likely always supported similar to AT+GSN - AT_CGDATA, // alternative is to support only ATD*99***# - AT_CGAUTH, // APN authentication AT commands supported - SUPPORTED_FEATURE_END_MARK // must be last element in the array of features - }; - static void set_unsupported_features(const SupportedFeature *unsupported_features); + static void set_cellular_properties(const intptr_t *property_array); protected: + + static const intptr_t *_property_array; + ATHandler &_at; - /** Check if some functionality is supported by a cellular module. For example, - * most of standard AT commands are optional and not implemented by all cellular modules. + /** Get value for the given key. * - * @param feature check for feature to support - * @return true on supported, otherwise false + * @param key key for value to be fetched + * @return property value for the given key. Value type is defined in enum CellularProperty */ - static const SupportedFeature *_unsupported_features; - static bool is_supported(SupportedFeature feature); + static intptr_t get_property(CellularProperty key); }; } // namespace mbed diff --git a/features/cellular/framework/AT/AT_CellularContext.cpp b/features/cellular/framework/AT/AT_CellularContext.cpp index 1cb280318f7..6947d6bdce9 100644 --- a/features/cellular/framework/AT/AT_CellularContext.cpp +++ b/features/cellular/framework/AT/AT_CellularContext.cpp @@ -20,12 +20,14 @@ #include "AT_CellularStack.h" #include "CellularLog.h" #include "CellularUtil.h" -#include "CellularSIM.h" #include "UARTSerial.h" #include "mbed_wait_api.h" #define NETWORK_TIMEOUT 30 * 60 * 1000 // 30 minutes #define DEVICE_TIMEOUT 5 * 60 * 1000 // 5 minutes +// Timeout to wait for URC indicating ciot optimization support from network +#define CP_OPT_NW_REPLY_TIMEOUT 3000 // 3 seconds + #if NSAPI_PPP_AVAILABLE #define AT_SYNC_TIMEOUT 1000 // 1 second timeout @@ -35,19 +37,20 @@ #define USE_APN_LOOKUP (MBED_CONF_CELLULAR_USE_APN_LOOKUP || (NSAPI_PPP_AVAILABLE && MBED_CONF_PPP_CELL_IFACE_APN_LOOKUP)) #if USE_APN_LOOKUP +#include "CellularInformation.h" #include "APN_db.h" #endif //USE_APN_LOOKUP using namespace mbed_cellular_util; using namespace mbed; -AT_CellularContext::AT_CellularContext(ATHandler &at, CellularDevice *device, const char *apn) : - AT_CellularBase(at), _ip_stack_type_requested(DEFAULT_STACK), _is_connected(false), _is_blocking(true), - _current_op(OP_INVALID), _device(device), _nw(0), _fh(0) +AT_CellularContext::AT_CellularContext(ATHandler &at, CellularDevice *device, const char *apn, bool cp_req, bool nonip_req) : + AT_CellularBase(at), _is_connected(false), _is_blocking(true), + _current_op(OP_INVALID), _device(device), _nw(0), _fh(0), _cp_req(cp_req), _nonip_req(nonip_req), _cp_in_use(false), _cp_netif(NULL) { tr_info("New CellularContext %s (%p)", apn ? apn : "", this); _stack = NULL; - _ip_stack_type = DEFAULT_STACK; + _pdp_type = DEFAULT_PDP_TYPE; _authentication_type = CellularContext::CHAP; _connect_status = NSAPI_STATUS_DISCONNECTED; _is_context_active = false; @@ -59,11 +62,13 @@ AT_CellularContext::AT_CellularContext(ATHandler &at, CellularDevice *device, co _cid = -1; _new_context_set = false; _next = NULL; + _dcd_pin = NC; + _active_high = false; } AT_CellularContext::~AT_CellularContext() { - tr_info("Delete CellularContext %s (%p)", _apn ? _apn : "", this); + tr_info("Delete CellularContext with apn: [%s] (%p)", _apn ? _apn : "", this); (void)disconnect(); @@ -79,6 +84,23 @@ void AT_CellularContext::set_file_handle(FileHandle *fh) _at.set_file_handle(_fh); } +void AT_CellularContext::set_file_handle(UARTSerial *serial, PinName dcd_pin, bool active_high) +{ + tr_info("CellularContext serial %p", serial); + _dcd_pin = dcd_pin; + _active_high = active_high; + _fh = serial; + _at.set_file_handle(static_cast(serial)); + enable_hup(false); +} + +void AT_CellularContext::enable_hup(bool enable) +{ + if (_dcd_pin != NC) { + static_cast(_fh)->set_data_carrier_detect(enable ? _dcd_pin : NC, _active_high); + } +} + nsapi_error_t AT_CellularContext::connect() { tr_info("CellularContext connect"); @@ -239,33 +261,21 @@ void AT_CellularContext::set_credentials(const char *apn, const char *uname, con _pwd = pwd; } -bool AT_CellularContext::stack_type_supported(nsapi_ip_stack_t stack_type) -{ - if (stack_type == _ip_stack_type) { - return true; - } else { - return false; - } -} - -nsapi_ip_stack_t AT_CellularContext::get_stack_type() +pdp_type_t AT_CellularContext::string_to_pdp_type(const char *pdp_type_str) { - return _ip_stack_type; -} - -nsapi_ip_stack_t AT_CellularContext::string_to_stack_type(const char *pdp_type) -{ - nsapi_ip_stack_t stack = DEFAULT_STACK; - int len = strlen(pdp_type); - - if (len == 6 && memcmp(pdp_type, "IPV4V6", len) == 0) { - stack = IPV4V6_STACK; - } else if (len == 4 && memcmp(pdp_type, "IPV6", len) == 0) { - stack = IPV6_STACK; - } else if (len == 2 && memcmp(pdp_type, "IP", len) == 0) { - stack = IPV4_STACK; + pdp_type_t pdp_type = DEFAULT_PDP_TYPE; + int len = strlen(pdp_type_str); + + if (len == 6 && memcmp(pdp_type_str, "IPV4V6", len) == 0) { + pdp_type = IPV4V6_PDP_TYPE; + } else if (len == 4 && memcmp(pdp_type_str, "IPV6", len) == 0) { + pdp_type = IPV6_PDP_TYPE; + } else if (len == 2 && memcmp(pdp_type_str, "IP", len) == 0) { + pdp_type = IPV4_PDP_TYPE; + } else if (len == 6 && memcmp(pdp_type_str, "Non-IP", len) == 0) { + pdp_type = NON_IP_PDP_TYPE; } - return stack; + return pdp_type; } // PDP Context handling @@ -289,7 +299,7 @@ nsapi_error_t AT_CellularContext::do_user_authentication() { // if user has defined user name and password we need to call CGAUTH before activating or modifying context if (_pwd && _uname) { - if (!is_supported(AT_CGAUTH)) { + if (!get_property(PROPERTY_AT_CGAUTH)) { return NSAPI_ERROR_UNSUPPORTED; } _at.cmd_start("AT+CGAUTH="); @@ -306,8 +316,24 @@ nsapi_error_t AT_CellularContext::do_user_authentication() return NSAPI_ERROR_OK; } +AT_CellularBase::CellularProperty AT_CellularContext::pdp_type_t_to_cellular_property(pdp_type_t pdp_type) +{ + AT_CellularBase::CellularProperty prop = PROPERTY_IPV4_PDP_TYPE; + if (pdp_type == IPV6_PDP_TYPE) { + prop = PROPERTY_IPV6_PDP_TYPE; + } else if (pdp_type == IPV4V6_PDP_TYPE) { + prop = PROPERTY_IPV4V6_PDP_TYPE; + } else if (pdp_type == NON_IP_PDP_TYPE) { + prop = PROPERTY_NON_IP_PDP_TYPE; + } + + return prop; +} + bool AT_CellularContext::get_context() { + bool modem_supports_ipv6 = get_property(PROPERTY_IPV6_PDP_TYPE); + bool modem_supports_ipv4 = get_property(PROPERTY_IPV4_PDP_TYPE); _at.cmd_start("AT+CGDCONT?"); _at.cmd_stop(); _at.resp_start("+CGDCONT:"); @@ -316,9 +342,6 @@ bool AT_CellularContext::get_context() char apn[MAX_ACCESSPOINT_NAME_LENGTH]; int apn_len = 0; - bool modem_supports_ipv6 = stack_type_supported(IPV6_STACK); - bool modem_supports_ipv4 = stack_type_supported(IPV4_STACK); - while (_at.info_resp()) { int cid = _at.read_int(); if (cid > cid_max) { @@ -332,51 +355,20 @@ bool AT_CellularContext::get_context() if (_apn && (strcmp(apn, _apn) != 0)) { continue; } - nsapi_ip_stack_t pdp_stack = string_to_stack_type(pdp_type_from_context); - // Accept dual PDP context for IPv4/IPv6 only modems - if (pdp_stack != DEFAULT_STACK && (stack_type_supported(pdp_stack) || pdp_stack == IPV4V6_STACK)) { - if (_ip_stack_type_requested == IPV4_STACK) { - if (pdp_stack == IPV4_STACK || pdp_stack == IPV4V6_STACK) { - _ip_stack_type = _ip_stack_type_requested; + + // APN matched -> Check PDP type + pdp_type_t pdp_type = string_to_pdp_type(pdp_type_from_context); + + // Accept exact matching PDP context type or dual PDP context for IPv4/IPv6 only modems + if (get_property(pdp_type_t_to_cellular_property(pdp_type)) || + ((pdp_type == IPV4V6_PDP_TYPE && (modem_supports_ipv4 || modem_supports_ipv6)) && !_nonip_req)) { + _pdp_type = pdp_type; _cid = cid; - break; } - } else if (_ip_stack_type_requested == IPV6_STACK) { - if (pdp_stack == IPV6_STACK || pdp_stack == IPV4V6_STACK) { - _ip_stack_type = _ip_stack_type_requested; - _cid = cid; - break; } - } else { - // requested dual stack or stack is not specified - // If dual PDP need to check for IPV4 or IPV6 modem support. Prefer IPv6. - if (pdp_stack == IPV4V6_STACK) { - if (modem_supports_ipv6) { - _ip_stack_type = IPV6_STACK; - _cid = cid; - break; - } else if (modem_supports_ipv4) { - _ip_stack_type = IPV4_STACK; - _cid = cid; - break; } - // If PDP is IPV4 or IPV6 they are already checked if supported - } else { - _ip_stack_type = pdp_stack; - _cid = cid; - - if (pdp_stack == IPV6_STACK) { - break; - } - if (pdp_stack == IPV4_STACK && !modem_supports_ipv6) { - break; - } - } - } - } - } - } } + _at.resp_stop(); if (_cid == -1) { // no suitable context was found so create a new one if (!set_new_context(cid_max + 1)) { @@ -396,75 +388,77 @@ bool AT_CellularContext::get_context() bool AT_CellularContext::set_new_context(int cid) { - nsapi_ip_stack_t tmp_stack = _ip_stack_type_requested; - - if (tmp_stack == DEFAULT_STACK) { - bool modem_supports_ipv6 = stack_type_supported(IPV6_STACK); - bool modem_supports_ipv4 = stack_type_supported(IPV4_STACK); - - if (modem_supports_ipv6 && modem_supports_ipv4) { - tmp_stack = IPV4V6_STACK; + bool modem_supports_ipv6 = get_property(PROPERTY_IPV6_PDP_TYPE); + bool modem_supports_ipv4 = get_property(PROPERTY_IPV4_PDP_TYPE); + bool modem_supports_nonip = get_property(PROPERTY_NON_IP_PDP_TYPE); + + char pdp_type_str[8 + 1] = {0}; + pdp_type_t pdp_type = IPV4_PDP_TYPE; + + if (_nonip_req && _cp_in_use && modem_supports_nonip) { + strncpy(pdp_type_str, "Non-IP", sizeof(pdp_type_str)); + pdp_type = NON_IP_PDP_TYPE; + } else if (modem_supports_ipv6 && modem_supports_ipv4) { + strncpy(pdp_type_str, "IPV4V6", sizeof(pdp_type_str)); + pdp_type = IPV4V6_PDP_TYPE; } else if (modem_supports_ipv6) { - tmp_stack = IPV6_STACK; + strncpy(pdp_type_str, "IPV6", sizeof(pdp_type_str)); + pdp_type = IPV6_PDP_TYPE; } else if (modem_supports_ipv4) { - tmp_stack = IPV4_STACK; + strncpy(pdp_type_str, "IP", sizeof(pdp_type)); + pdp_type = IPV4_PDP_TYPE; + } else { + return false; } - } - - char pdp_type[8 + 1] = {0}; - - switch (tmp_stack) { - case IPV4_STACK: - strncpy(pdp_type, "IP", sizeof(pdp_type)); - break; - case IPV6_STACK: - strncpy(pdp_type, "IPV6", sizeof(pdp_type)); - break; - case IPV4V6_STACK: - strncpy(pdp_type, "IPV6", sizeof(pdp_type)); // try first IPV6 and then fall-back to IPv4 - break; - default: - break; - } //apn: "If the value is null or omitted, then the subscription value will be requested." bool success = false; _at.cmd_start("AT+CGDCONT="); _at.write_int(cid); - _at.write_string(pdp_type); + _at.write_string(pdp_type_str); _at.write_string(_apn); _at.cmd_stop_read_resp(); success = (_at.get_last_error() == NSAPI_ERROR_OK); - // Fall back to ipv4 - if (!success && tmp_stack == IPV4V6_STACK) { - _at.clear_error(); - tmp_stack = IPV4_STACK; - _at.cmd_start("AT+FCLASS=0;+CGDCONT="); - _at.write_int(cid); - _at.write_string("IP"); - _at.write_string(_apn); - _at.cmd_stop_read_resp(); - success = (_at.get_last_error() == NSAPI_ERROR_OK); - } - if (success) { - _ip_stack_type = tmp_stack; + _pdp_type = pdp_type; _cid = cid; _new_context_set = true; - tr_info("New PDP context %d, stack %s", _cid, pdp_type); + tr_info("New PDP context %d, type %s", _cid, pdp_type); } return success; } nsapi_error_t AT_CellularContext::do_activate_context() +{ + if (_nonip_req && _cp_in_use) { + return activate_non_ip_context(); + } + + // In IP case but also when Non-IP is requested and + // control plane optimisation is not established -> activate ip context + _nonip_req = false; + return activate_ip_context(); +} + +nsapi_error_t AT_CellularContext::activate_ip_context() +{ + return activate_context(); +} + +nsapi_error_t AT_CellularContext::activate_non_ip_context() +{ + return activate_context(); +} + +nsapi_error_t AT_CellularContext::activate_context() { _at.lock(); nsapi_error_t err = NSAPI_ERROR_OK; - // try to find or create context with suitable stack + // try to find or create context of suitable type if (get_context()) { #if NSAPI_PPP_AVAILABLE _at.unlock(); @@ -571,8 +565,14 @@ void AT_CellularContext::do_connect() #if NSAPI_PPP_AVAILABLE nsapi_error_t AT_CellularContext::open_data_channel() { + // If Non-IP in use fail + if (_pdp_type == NON_IP_PDP_TYPE) { + tr_error("Attempt of PPP connect over NON-IP: failed to CONNECT"); + return NSAPI_ERROR_PARAMETER; + } + tr_info("CellularContext PPP connect"); - if (is_supported(AT_CGDATA)) { + if (get_property(PROPERTY_AT_CGDATA)) { _at.cmd_start("AT+CGDATA=\"PPP\","); _at.write_int(_cid); } else { @@ -592,11 +592,16 @@ nsapi_error_t AT_CellularContext::open_data_channel() } _at.set_is_filehandle_usable(false); - + enable_hup(true); /* Initialize PPP * If blocking: mbed_ppp_init() is a blocking call, it will block until connected, or timeout after 30 seconds*/ - return nsapi_ppp_connect(_at.get_file_handle(), callback(this, &AT_CellularContext::ppp_status_cb), _uname, _pwd, _ip_stack_type); + nsapi_error_t err = nsapi_ppp_connect(_at.get_file_handle(), callback(this, &AT_CellularContext::ppp_status_cb), _uname, _pwd, (nsapi_ip_stack_t)_pdp_type); + if (err) { + ppp_disconnected(); + } + + return err; } void AT_CellularContext::ppp_status_cb(nsapi_event_t ev, intptr_t ptr) @@ -604,6 +609,8 @@ void AT_CellularContext::ppp_status_cb(nsapi_event_t ev, intptr_t ptr) tr_debug("ppp_status_cb: event %d, ptr %d", ev, ptr); if (ev == NSAPI_EVENT_CONNECTION_STATUS_CHANGE && ptr == NSAPI_STATUS_GLOBAL_UP) { _is_connected = true; + } else if (ev == NSAPI_EVENT_CONNECTION_STATUS_CHANGE && ptr == NSAPI_STATUS_DISCONNECTED) { + ppp_disconnected(); } else { _is_connected = false; } @@ -614,6 +621,20 @@ void AT_CellularContext::ppp_status_cb(nsapi_event_t ev, intptr_t ptr) _device->cellular_callback(ev, ptr); } +void AT_CellularContext::ppp_disconnected() +{ + enable_hup(false); + + // after ppp disconnect if we wan't to use same at handler we need to set filehandle again to athandler so it + // will set the correct sigio and nonblocking + _at.lock(); + _at.set_is_filehandle_usable(true); + if (!_at.sync(AT_SYNC_TIMEOUT)) { // consume extra characters after ppp disconnect, also it may take a while until modem listens AT commands + tr_error("AT sync failed after PPP Disconnect"); + } + _at.unlock(); +} + #endif //#if NSAPI_PPP_AVAILABLE nsapi_error_t AT_CellularContext::disconnect() @@ -628,20 +649,37 @@ nsapi_error_t AT_CellularContext::disconnect() tr_error("CellularContext disconnect failed!"); // continue even in failure due to ppp disconnect in any case releases filehandle } - // after ppp disconnect if we wan't to use same at handler we need to set filehandle again to athandler so it - // will set the correct sigio and nonblocking - _at.lock(); - _at.set_file_handle(_at.get_file_handle()); - _at.set_is_filehandle_usable(true); - if (!_at.sync(AT_SYNC_TIMEOUT)) { // consume extra characters after ppp disconnect, also it may take a while until modem listens AT commands - tr_error("AT sync failed after PPP Disconnect"); - } - _at.unlock(); + ppp_disconnected(); #endif // NSAPI_PPP_AVAILABLE _at.lock(); // deactivate a context only if we have activated if (_is_context_activated) { + if (_nonip_req && _cp_in_use) { + deactivate_non_ip_context(); + } else { + deactivate_ip_context(); + } + } + + _is_connected = false; + call_network_cb(NSAPI_STATUS_DISCONNECTED); + + return _at.unlock_return_error(); +} + +void AT_CellularContext::deactivate_ip_context() +{ + deactivate_context(); +} + +void AT_CellularContext::deactivate_non_ip_context() +{ + deactivate_context(); +} + +void AT_CellularContext::deactivate_context() +{ // CGACT and CGATT commands might take up to 3 minutes to respond. _at.set_at_timeout(180 * 1000); _is_context_active = false; @@ -681,16 +719,11 @@ nsapi_error_t AT_CellularContext::disconnect() _at.write_int(_cid); _at.cmd_stop_read_resp(); } + _at.clear_error(); _at.cmd_start("AT+CGATT=0"); _at.cmd_stop_read_resp(); _at.restore_at_timeout(); - } - - _is_connected = false; - call_network_cb(NSAPI_STATUS_DISCONNECTED); - - return _at.unlock_return_error(); } nsapi_error_t AT_CellularContext::get_apn_backoff_timer(int &backoff_timer) @@ -835,12 +868,12 @@ void AT_CellularContext::cellular_callback(nsapi_event_t ev, intptr_t ptr) _cb_data.error = data->error; tr_debug("CellularContext: event %d, err %d, data %d", ev, data->error, data->status_data); #if USE_APN_LOOKUP - if (st == CellularSIMStatusChanged && data->status_data == CellularSIM::SimStateReady && + if (st == CellularSIMStatusChanged && data->status_data == CellularDevice::SimStateReady && _cb_data.error == NSAPI_ERROR_OK) { if (!_apn) { char imsi[MAX_IMSI_LENGTH + 1]; wait(1); // need to wait to access SIM in some modems - _cb_data.error = _device->open_sim()->get_imsi(imsi); + _cb_data.error = _device->open_information()->get_imsi(imsi, sizeof(imsi)); if (_cb_data.error == NSAPI_ERROR_OK) { const char *apn_config = apnconfig(imsi); if (apn_config) { @@ -858,13 +891,23 @@ void AT_CellularContext::cellular_callback(nsapi_event_t ev, intptr_t ptr) _semaphore.release(); } } - _device->close_sim(); + _device->close_information(); } } #endif // USE_APN_LOOKUP if (!_nw && st == CellularDeviceReady && data->error == NSAPI_ERROR_OK) { _nw = _device->open_network(_fh); + tr_error("OPEN NETWORK"); + } + + if (_cp_req && !_cp_in_use && (data->error == NSAPI_ERROR_OK) && + (st == CellularSIMStatusChanged && data->status_data == CellularDevice::SimStateReady)) { + if (setup_control_plane_opt() != NSAPI_ERROR_OK) { + tr_error("Control plane SETUP failed!"); + } else { + tr_info("Control plane SETUP success!"); + } } if (_is_blocking) { @@ -874,7 +917,7 @@ void AT_CellularContext::cellular_callback(nsapi_event_t ev, intptr_t ptr) } else { if ((st == CellularDeviceReady && _current_op == OP_DEVICE_READY) || (st == CellularSIMStatusChanged && _current_op == OP_SIM_READY && - data->status_data == CellularSIM::SimStateReady)) { + data->status_data == CellularDevice::SimStateReady)) { // target reached, release semaphore _semaphore.release(); } else if (st == CellularRegistrationStatusChanged && (data->status_data == CellularNetwork::RegisteredHomeNetwork || @@ -934,3 +977,61 @@ void AT_CellularContext::call_network_cb(nsapi_connection_status_t status) } } } + +ControlPlane_netif *AT_CellularContext::get_cp_netif() +{ + tr_error("No control plane interface available from base context!"); + return NULL; +} + +nsapi_error_t AT_CellularContext::setup_control_plane_opt() +{ + // check if control plane optimization already set + mbed::CellularNetwork::CIoT_Supported_Opt supported_network_opt; + + if (_nw->get_ciot_network_optimization_config(supported_network_opt)) { + return NSAPI_ERROR_DEVICE_ERROR; + } + + if (supported_network_opt == mbed::CellularNetwork::CIOT_OPT_CONTROL_PLANE || + supported_network_opt == mbed::CellularNetwork::CIOT_OPT_BOTH) { + _cp_in_use = true; + return NSAPI_ERROR_OK; + } + + // ciot optimization not set by app so need to set it now + nsapi_error_t ciot_opt_ret; + ciot_opt_ret = _nw->set_ciot_optimization_config(mbed::CellularNetwork::CIOT_OPT_CONTROL_PLANE, + mbed::CellularNetwork::PREFERRED_UE_OPT_CONTROL_PLANE, + callback(this, &AT_CellularContext::ciot_opt_cb)); + + if (ciot_opt_ret != NSAPI_ERROR_OK) { + return ciot_opt_ret; + } + + //wait for control plane opt call back to release semaphore + _cp_opt_semaphore.wait(CP_OPT_NW_REPLY_TIMEOUT); + + if (_cp_in_use) { + return NSAPI_ERROR_OK; + } + + return NSAPI_ERROR_DEVICE_ERROR; +} + +void AT_CellularContext::ciot_opt_cb(mbed::CellularNetwork::CIoT_Supported_Opt ciot_opt) +{ + if (ciot_opt == mbed::CellularNetwork::CIOT_OPT_CONTROL_PLANE || + ciot_opt == mbed::CellularNetwork::CIOT_OPT_BOTH) { + _cp_in_use = true; + } + _cp_opt_semaphore.release(); +} + +void AT_CellularContext::set_disconnect() +{ + _is_connected = false; + cell_callback_data_t data; + data.error = NSAPI_STATUS_DISCONNECTED; + _device->cellular_callback(NSAPI_EVENT_CONNECTION_STATUS_CHANGE, (intptr_t)&data); +} diff --git a/features/cellular/framework/AT/AT_CellularContext.h b/features/cellular/framework/AT/AT_CellularContext.h index c045c0a3ad6..afcdfc335d3 100644 --- a/features/cellular/framework/AT/AT_CellularContext.h +++ b/features/cellular/framework/AT/AT_CellularContext.h @@ -27,7 +27,7 @@ namespace mbed { class AT_CellularContext : public CellularContext, public AT_CellularBase { public: - AT_CellularContext(ATHandler &at, CellularDevice *device, const char *apn = 0); + AT_CellularContext(ATHandler &at, CellularDevice *device, const char *apn = 0, bool cp_req = false, bool nonip_req = false); virtual ~AT_CellularContext(); // from CellularBase/NetworkInterface @@ -57,6 +57,10 @@ class AT_CellularContext : public CellularContext, public AT_CellularBase { virtual nsapi_error_t register_to_network(); virtual nsapi_error_t attach_to_network(); virtual void set_file_handle(FileHandle *fh); + virtual void set_file_handle(UARTSerial *serial, PinName dcd_pin = NC, bool active_high = false); + virtual void enable_hup(bool enable); + + virtual ControlPlane_netif *get_cp_netif(); protected: virtual void cellular_callback(nsapi_event_t ev, intptr_t ptr); @@ -75,12 +79,6 @@ class AT_CellularContext : public CellularContext, public AT_CellularBase { */ virtual void do_connect(); - /** Check if modem supports the given stack type. Can be overridden by the modem. - * - * @return true if supported - */ - virtual bool stack_type_supported(nsapi_ip_stack_t stack_type); - /** Get the operation specific timeout. Used in synchronous mode when setting the maximum * waiting time. Modem specific implementation can override this to provide different timeouts. * @@ -95,21 +93,31 @@ class AT_CellularContext : public CellularContext, public AT_CellularBase { */ void call_network_cb(nsapi_connection_status_t status); + virtual nsapi_error_t activate_non_ip_context(); + virtual nsapi_error_t setup_control_plane_opt(); + virtual void deactivate_non_ip_context(); + virtual void set_disconnect(); + private: #if NSAPI_PPP_AVAILABLE nsapi_error_t open_data_channel(); void ppp_status_cb(nsapi_event_t ev, intptr_t ptr); + void ppp_disconnected(); #endif // #if NSAPI_PPP_AVAILABLE nsapi_error_t do_activate_context(); + nsapi_error_t activate_context(); + nsapi_error_t activate_ip_context(); + void deactivate_context(); + void deactivate_ip_context(); bool set_new_context(int cid); bool get_context(); nsapi_error_t delete_current_context(); - nsapi_ip_stack_t string_to_stack_type(const char *pdp_type); - nsapi_ip_stack_t get_stack_type(); + pdp_type_t string_to_pdp_type(const char *pdp_type); nsapi_error_t check_operation(nsapi_error_t err, ContextOperation op); + AT_CellularBase::CellularProperty pdp_type_t_to_cellular_property(pdp_type_t pdp_type); + void ciot_opt_cb(mbed::CellularNetwork::CIoT_Supported_Opt ciot_opt); private: - nsapi_ip_stack_t _ip_stack_type_requested; bool _is_connected; bool _is_blocking; ContextOperation _current_op; @@ -118,6 +126,18 @@ class AT_CellularContext : public CellularContext, public AT_CellularBase { CellularNetwork *_nw; FileHandle *_fh; rtos::Semaphore _semaphore; + rtos::Semaphore _cp_opt_semaphore; + +protected: + // flag indicating if CP was requested to be setup + bool _cp_req; + // flag indicating if Non-IP context was requested to be setup + bool _nonip_req; + + // tells if CCIOTOPTI received green from network for CP optimisation use + bool _cp_in_use; + + ControlPlane_netif *_cp_netif; }; } // namespace mbed diff --git a/features/cellular/framework/AT/AT_CellularDevice.cpp b/features/cellular/framework/AT/AT_CellularDevice.cpp index d04a15b113c..a2c50e6552f 100644 --- a/features/cellular/framework/AT/AT_CellularDevice.cpp +++ b/features/cellular/framework/AT/AT_CellularDevice.cpp @@ -15,11 +15,10 @@ * limitations under the License. */ +#include "CellularUtil.h" #include "AT_CellularDevice.h" #include "AT_CellularInformation.h" #include "AT_CellularNetwork.h" -#include "AT_CellularPower.h" -#include "AT_CellularSIM.h" #include "AT_CellularSMS.h" #include "AT_CellularContext.h" #include "AT_CellularStack.h" @@ -28,15 +27,20 @@ #include "UARTSerial.h" #include "FileHandle.h" +using namespace mbed_cellular_util; using namespace events; using namespace mbed; #define DEFAULT_AT_TIMEOUT 1000 // at default timeout in milliseconds +const int MAX_SIM_RESPONSE_LENGTH = 16; AT_CellularDevice::AT_CellularDevice(FileHandle *fh) : CellularDevice(fh), _atHandlers(0), _network(0), _sms(0), - _sim(0), _power(0), _information(0), _context_list(0), _default_timeout(DEFAULT_AT_TIMEOUT), + _information(0), _context_list(0), _default_timeout(DEFAULT_AT_TIMEOUT), _modem_debug_on(false) { + MBED_ASSERT(fh); + _at = get_at_handler(fh); + MBED_ASSERT(_at); } AT_CellularDevice::~AT_CellularDevice() @@ -46,14 +50,10 @@ AT_CellularDevice::~AT_CellularDevice() // make sure that all is deleted even if somewhere close was not called and reference counting is messed up. _network_ref_count = 1; _sms_ref_count = 1; - _power_ref_count = 1; - _sim_ref_count = 1; _info_ref_count = 1; close_network(); close_sms(); - close_power(); - close_sim(); close_information(); AT_CellularContext *curr = _context_list; @@ -72,6 +72,26 @@ AT_CellularDevice::~AT_CellularDevice() } } +nsapi_error_t AT_CellularDevice::hard_power_on() +{ + return NSAPI_ERROR_OK; +} + +nsapi_error_t AT_CellularDevice::hard_power_off() +{ + return NSAPI_ERROR_OK; +} + +nsapi_error_t AT_CellularDevice::soft_power_on() +{ + return NSAPI_ERROR_OK; +} + +nsapi_error_t AT_CellularDevice::soft_power_off() +{ + return NSAPI_ERROR_OK; +} + // each parser is associated with one filehandle (that is UART) ATHandler *AT_CellularDevice::get_at_handler(FileHandle *fileHandle) { @@ -124,38 +144,115 @@ void AT_CellularDevice::release_at_handler(ATHandler *at_handler) } } +nsapi_error_t AT_CellularDevice::get_sim_state(SimState &state) +{ + char simstr[MAX_SIM_RESPONSE_LENGTH]; + _at->lock(); + _at->flush(); + _at->cmd_start("AT+CPIN?"); + _at->cmd_stop(); + _at->resp_start("+CPIN:"); + ssize_t len = _at->read_string(simstr, sizeof(simstr)); + if (len != -1) { + if (len >= 5 && memcmp(simstr, "READY", 5) == 0) { + state = SimStateReady; + } else if (len >= 7 && memcmp(simstr, "SIM PIN", 7) == 0) { + state = SimStatePinNeeded; + } else if (len >= 7 && memcmp(simstr, "SIM PUK", 7) == 0) { + state = SimStatePukNeeded; + } else { + simstr[len] = '\0'; + tr_error("Unknown SIM state %s", simstr); + state = SimStateUnknown; + } + } else { + tr_warn("SIM not readable."); + state = SimStateUnknown; // SIM may not be ready yet or +CPIN may be unsupported command + } + _at->resp_stop(); + nsapi_error_t error = _at->get_last_error(); + _at->unlock(); +#if MBED_CONF_MBED_TRACE_ENABLE + switch (state) { + case SimStatePinNeeded: + tr_info("SIM PIN required"); + break; + case SimStatePukNeeded: + tr_error("SIM PUK required"); + break; + case SimStateUnknown: + tr_warn("SIM state unknown"); + break; + default: + tr_info("SIM is ready"); + break; + } +#endif + return error; +} + +nsapi_error_t AT_CellularDevice::set_pin(const char *sim_pin) +{ + // if SIM is already in ready state then settings the PIN + // will return error so let's check the state before settings the pin. + SimState state; + if (get_sim_state(state) == NSAPI_ERROR_OK && state == SimStateReady) { + return NSAPI_ERROR_OK; + } + + if (sim_pin == NULL) { + return NSAPI_ERROR_PARAMETER; + } + + _at->lock(); + _at->cmd_start("AT+CPIN="); + _at->write_string(sim_pin); + _at->cmd_stop_read_resp(); + return _at->unlock_return_error(); +} + CellularContext *AT_CellularDevice::get_context_list() const { return _context_list; } -CellularContext *AT_CellularDevice::create_context(FileHandle *fh, const char *apn) +CellularContext *AT_CellularDevice::create_context(UARTSerial *serial, const char *const apn, PinName dcd_pin, + bool active_high, bool cp_req, bool nonip_req) { - ATHandler *atHandler = get_at_handler(fh); - if (atHandler) { - AT_CellularContext *ctx = create_context_impl(*atHandler, apn); - AT_CellularContext *curr = _context_list; - - if (_context_list == NULL) { - _context_list = ctx; - return ctx; - } + // Call FileHandle base version - explict upcast to avoid recursing into ourselves + CellularContext *ctx = create_context(static_cast(serial), apn, cp_req, nonip_req); + if (serial) { + ctx->set_file_handle(serial, dcd_pin, active_high); + } + return ctx; +} - AT_CellularContext *prev; - while (curr) { - prev = curr; - curr = (AT_CellularContext *)curr->_next; - } +CellularContext *AT_CellularDevice::create_context(FileHandle *fh, const char *apn, bool cp_req, bool nonip_req) +{ + AT_CellularContext *ctx = create_context_impl(*get_at_handler(fh), apn, cp_req, nonip_req); + AT_CellularContext *curr = _context_list; - prev->_next = ctx; + if (_context_list == NULL) { + _context_list = ctx; return ctx; } - return NULL; + + AT_CellularContext *prev; + while (curr) { + prev = curr; + curr = (AT_CellularContext *)curr->_next; + } + + prev->_next = ctx; + return ctx; } -AT_CellularContext *AT_CellularDevice::create_context_impl(ATHandler &at, const char *apn) +AT_CellularContext *AT_CellularDevice::create_context_impl(ATHandler &at, const char *apn, bool cp_req, bool nonip_req) { - return new AT_CellularContext(at, this, apn); + if (cp_req) { + + } + return new AT_CellularContext(at, this, apn, cp_req, nonip_req); } void AT_CellularDevice::delete_context(CellularContext *context) @@ -179,70 +276,27 @@ void AT_CellularDevice::delete_context(CellularContext *context) CellularNetwork *AT_CellularDevice::open_network(FileHandle *fh) { if (!_network) { - ATHandler *atHandler = get_at_handler(fh); - if (atHandler) { - _network = open_network_impl(*atHandler); - } - } - if (_network) { - _network_ref_count++; + _network = open_network_impl(*get_at_handler(fh)); } + _network_ref_count++; return _network; } CellularSMS *AT_CellularDevice::open_sms(FileHandle *fh) { if (!_sms) { - ATHandler *atHandler = get_at_handler(fh); - if (atHandler) { - _sms = open_sms_impl(*atHandler); - } - } - if (_sms) { - _sms_ref_count++; + _sms = open_sms_impl(*get_at_handler(fh)); } + _sms_ref_count++; return _sms; } -CellularSIM *AT_CellularDevice::open_sim(FileHandle *fh) -{ - if (!_sim) { - ATHandler *atHandler = get_at_handler(fh); - if (atHandler) { - _sim = open_sim_impl(*atHandler); - } - } - if (_sim) { - _sim_ref_count++; - } - return _sim; -} - -CellularPower *AT_CellularDevice::open_power(FileHandle *fh) -{ - if (!_power) { - ATHandler *atHandler = get_at_handler(fh); - if (atHandler) { - _power = open_power_impl(*atHandler); - } - } - if (_power) { - _power_ref_count++; - } - return _power; -} - CellularInformation *AT_CellularDevice::open_information(FileHandle *fh) { if (!_information) { - ATHandler *atHandler = get_at_handler(fh); - if (atHandler) { - _information = open_information_impl(*atHandler); - } - } - if (_information) { - _info_ref_count++; + _information = open_information_impl(*get_at_handler(fh)); } + _info_ref_count++; return _information; } @@ -256,16 +310,6 @@ AT_CellularSMS *AT_CellularDevice::open_sms_impl(ATHandler &at) return new AT_CellularSMS(at); } -AT_CellularPower *AT_CellularDevice::open_power_impl(ATHandler &at) -{ - return new AT_CellularPower(at); -} - -AT_CellularSIM *AT_CellularDevice::open_sim_impl(ATHandler &at) -{ - return new AT_CellularSIM(at); -} - AT_CellularInformation *AT_CellularDevice::open_information_impl(ATHandler &at) { return new AT_CellularInformation(at); @@ -297,32 +341,6 @@ void AT_CellularDevice::close_sms() } } -void AT_CellularDevice::close_power() -{ - if (_power) { - _power_ref_count--; - if (_power_ref_count == 0) { - ATHandler *atHandler = &_power->get_at_handler(); - delete _power; - _power = NULL; - release_at_handler(atHandler); - } - } -} - -void AT_CellularDevice::close_sim() -{ - if (_sim) { - _sim_ref_count--; - if (_sim_ref_count == 0) { - ATHandler *atHandler = &_sim->get_at_handler(); - delete _sim; - _sim = NULL; - release_at_handler(atHandler); - } - } -} - void AT_CellularDevice::close_information() { if (_information) { @@ -363,19 +381,177 @@ void AT_CellularDevice::modem_debug_on(bool on) } } -nsapi_error_t AT_CellularDevice::init_module() +nsapi_error_t AT_CellularDevice::init() { -#if MBED_CONF_MBED_TRACE_ENABLE - CellularInformation *information = open_information(); - if (information) { - char *pbuf = new char[100]; - nsapi_error_t ret = information->get_model(pbuf, sizeof(*pbuf)); - close_information(); - if (ret == NSAPI_ERROR_OK) { - tr_info("Model %s", pbuf); + _at->lock(); + _at->flush(); + _at->cmd_start("ATE0"); // echo off + _at->cmd_stop_read_resp(); + + _at->cmd_start("AT+CMEE=1"); // verbose responses + _at->cmd_stop_read_resp(); + + _at->cmd_start("AT+CFUN=1"); // set full functionality + _at->cmd_stop_read_resp(); + + return _at->unlock_return_error(); +} + +nsapi_error_t AT_CellularDevice::shutdown() +{ + _at->lock(); + if (_state_machine) { + _state_machine->reset(); + } + CellularDevice::shutdown(); + _at->cmd_start("AT+CFUN=0");// set to minimum functionality + _at->cmd_stop_read_resp(); + return _at->unlock_return_error(); +} + +nsapi_error_t AT_CellularDevice::is_ready() +{ + _at->lock(); + _at->cmd_start("AT"); + _at->cmd_stop_read_resp(); + + // we need to do this twice because for example after data mode the first 'AT' command will give modem a + // stimulus that we are back to command mode. + _at->clear_error(); + _at->cmd_start("AT"); + _at->cmd_stop_read_resp(); + + return _at->unlock_return_error(); +} + +void AT_CellularDevice::set_ready_cb(Callback callback) +{ +} + +nsapi_error_t AT_CellularDevice::set_power_save_mode(int periodic_time, int active_time) +{ + _at->lock(); + + if (periodic_time == 0 && active_time == 0) { + // disable PSM + _at->cmd_start("AT+CPSMS="); + _at->write_int(0); + _at->cmd_stop_read_resp(); + } else { + const int PSMTimerBits = 5; + + /** + Table 10.5.163a/3GPP TS 24.008: GPRS Timer 3 information element + + Bits 5 to 1 represent the binary coded timer value. + + Bits 6 to 8 defines the timer value unit for the GPRS timer as follows: + 8 7 6 + 0 0 0 value is incremented in multiples of 10 minutes + 0 0 1 value is incremented in multiples of 1 hour + 0 1 0 value is incremented in multiples of 10 hours + 0 1 1 value is incremented in multiples of 2 seconds + 1 0 0 value is incremented in multiples of 30 seconds + 1 0 1 value is incremented in multiples of 1 minute + 1 1 0 value is incremented in multiples of 320 hours (NOTE 1) + 1 1 1 value indicates that the timer is deactivated (NOTE 2). + */ + char pt[8 + 1]; // timer value encoded as 3GPP IE + const int ie_value_max = 0x1f; + uint32_t periodic_timer = 0; + if (periodic_time <= 2 * ie_value_max) { // multiples of 2 seconds + periodic_timer = periodic_time / 2; + strcpy(pt, "01100000"); + } else { + if (periodic_time <= 30 * ie_value_max) { // multiples of 30 seconds + periodic_timer = periodic_time / 30; + strcpy(pt, "10000000"); + } else { + if (periodic_time <= 60 * ie_value_max) { // multiples of 1 minute + periodic_timer = periodic_time / 60; + strcpy(pt, "10100000"); + } else { + if (periodic_time <= 10 * 60 * ie_value_max) { // multiples of 10 minutes + periodic_timer = periodic_time / (10 * 60); + strcpy(pt, "00000000"); + } else { + if (periodic_time <= 60 * 60 * ie_value_max) { // multiples of 1 hour + periodic_timer = periodic_time / (60 * 60); + strcpy(pt, "00100000"); + } else { + if (periodic_time <= 10 * 60 * 60 * ie_value_max) { // multiples of 10 hours + periodic_timer = periodic_time / (10 * 60 * 60); + strcpy(pt, "01000000"); + } else { // multiples of 320 hours + int t = periodic_time / (320 * 60 * 60); + if (t > ie_value_max) { + t = ie_value_max; + } + periodic_timer = t; + strcpy(pt, "11000000"); + } + } + } + } + } + } + + uint_to_binary_str(periodic_timer, &pt[3], sizeof(pt) - 3, PSMTimerBits); + pt[8] = '\0'; + + /** + Table 10.5.172/3GPP TS 24.008: GPRS Timer information element + + Bits 5 to 1 represent the binary coded timer value. + + Bits 6 to 8 defines the timer value unit for the GPRS timer as follows: + + 8 7 6 + 0 0 0 value is incremented in multiples of 2 seconds + 0 0 1 value is incremented in multiples of 1 minute + 0 1 0 value is incremented in multiples of decihours + 1 1 1 value indicates that the timer is deactivated. + + Other values shall be interpreted as multiples of 1 minute in this version of the protocol. + */ + char at[8 + 1]; + uint32_t active_timer; // timer value encoded as 3GPP IE + if (active_time <= 2 * ie_value_max) { // multiples of 2 seconds + active_timer = active_time / 2; + strcpy(at, "00000000"); + } else { + if (active_time <= 60 * ie_value_max) { // multiples of 1 minute + active_timer = (1 << 5) | (active_time / 60); + strcpy(at, "00100000"); + } else { // multiples of decihours + int t = active_time / (6 * 60); + if (t > ie_value_max) { + t = ie_value_max; + } + active_timer = t; + strcpy(at, "01000000"); + } + } + + uint_to_binary_str(active_timer, &at[3], sizeof(at) - 3, PSMTimerBits); + at[8] = '\0'; + + // request for both GPRS and LTE + _at->cmd_start("AT+CPSMS="); + _at->write_int(1); + _at->write_string(pt); + _at->write_string(at); + _at->write_string(pt); + _at->write_string(at); + _at->cmd_stop_read_resp(); + + if (_at->get_last_error() != NSAPI_ERROR_OK) { + tr_warn("Power save mode not enabled!"); + } else { + // network may not agree with power save options but + // that should be fine as timeout is not longer than requested } - delete[] pbuf; } -#endif - return NSAPI_ERROR_OK; + + return _at->unlock_return_error(); } diff --git a/features/cellular/framework/AT/AT_CellularDevice.h b/features/cellular/framework/AT/AT_CellularDevice.h index 8caf65b5da7..07cee654df6 100644 --- a/features/cellular/framework/AT/AT_CellularDevice.h +++ b/features/cellular/framework/AT/AT_CellularDevice.h @@ -25,8 +25,6 @@ namespace mbed { class ATHandler; class AT_CellularInformation; class AT_CellularNetwork; -class AT_CellularPower; -class AT_CellularSIM; class AT_CellularSMS; class AT_CellularContext; @@ -41,27 +39,34 @@ class AT_CellularDevice : public CellularDevice { AT_CellularDevice(FileHandle *fh); virtual ~AT_CellularDevice(); - virtual CellularContext *create_context(FileHandle *fh = NULL, const char *apn = NULL); + virtual nsapi_error_t hard_power_on(); + + virtual nsapi_error_t hard_power_off(); + + virtual nsapi_error_t soft_power_on(); + + virtual nsapi_error_t soft_power_off(); + + virtual nsapi_error_t set_pin(const char *sim_pin); + + virtual nsapi_error_t get_sim_state(SimState &state); + + virtual CellularContext *create_context(FileHandle *fh = NULL, const char *apn = NULL, bool cp_req = false, bool nonip_req = false); + + virtual CellularContext *create_context(UARTSerial *serial, const char *const apn, PinName dcd_pin = NC, bool active_high = false, bool cp_req = false, bool nonip_req = false); + virtual void delete_context(CellularContext *context); virtual CellularNetwork *open_network(FileHandle *fh = NULL); virtual CellularSMS *open_sms(FileHandle *fh = NULL); - virtual CellularPower *open_power(FileHandle *fh = NULL); - - virtual CellularSIM *open_sim(FileHandle *fh = NULL); - virtual CellularInformation *open_information(FileHandle *fh = NULL); virtual void close_network(); virtual void close_sms(); - virtual void close_power(); - - virtual void close_sim(); - virtual void close_information(); virtual void set_timeout(int timeout); @@ -70,7 +75,15 @@ class AT_CellularDevice : public CellularDevice { virtual void modem_debug_on(bool on); - virtual nsapi_error_t init_module(); + virtual nsapi_error_t init(); + + virtual nsapi_error_t shutdown(); + + virtual nsapi_error_t is_ready(); + + virtual void set_ready_cb(Callback callback); + + virtual nsapi_error_t set_power_save_mode(int periodic_time, int active_time = 0); ATHandler *_atHandlers; @@ -89,7 +102,7 @@ class AT_CellularDevice : public CellularDevice { * @return new instance of class AT_CellularContext * */ - virtual AT_CellularContext *create_context_impl(ATHandler &at, const char *apn); + virtual AT_CellularContext *create_context_impl(ATHandler &at, const char *apn, bool cp_req = false, bool nonip_req = false); /** Create new instance of AT_CellularNetwork or if overridden, modem specific implementation. * @@ -105,20 +118,6 @@ class AT_CellularDevice : public CellularDevice { */ virtual AT_CellularSMS *open_sms_impl(ATHandler &at); - /** Create new instance of AT_CellularPower or if overridden, modem specific implementation. - * - * @param at ATHandler reference for communication with the modem. - * @return new instance of class AT_CellularPower - */ - virtual AT_CellularPower *open_power_impl(ATHandler &at); - - /** Create new instance of AT_CellularSIM or if overridden, modem specific implementation. - * - * @param at ATHandler reference for communication with the modem. - * @return new instance of class AT_CellularSIM - */ - virtual AT_CellularSIM *open_sim_impl(ATHandler &at); - /** Create new instance of AT_CellularInformation or if overridden, modem specific implementation. * * @param at ATHandler reference for communication with the modem. @@ -130,12 +129,11 @@ class AT_CellularDevice : public CellularDevice { AT_CellularNetwork *_network; AT_CellularSMS *_sms; - AT_CellularSIM *_sim; - AT_CellularPower *_power; AT_CellularInformation *_information; AT_CellularContext *_context_list; int _default_timeout; bool _modem_debug_on; + ATHandler *_at; }; } // namespace mbed diff --git a/features/cellular/framework/AT/AT_CellularInformation.cpp b/features/cellular/framework/AT/AT_CellularInformation.cpp index 73a49f4fe01..5bb965090ce 100644 --- a/features/cellular/framework/AT/AT_CellularInformation.cpp +++ b/features/cellular/framework/AT/AT_CellularInformation.cpp @@ -46,11 +46,15 @@ nsapi_error_t AT_CellularInformation::get_revision(char *buf, size_t buf_size) nsapi_error_t AT_CellularInformation::get_serial_number(char *buf, size_t buf_size, SerialNumberType type) { + if (buf == NULL || buf_size == 0) { + return NSAPI_ERROR_PARAMETER; + } + if (type == SN) { return get_info("AT+CGSN", buf, buf_size); } - if (!is_supported(AT_CGSN_WITH_TYPE)) { + if (!get_property(PROPERTY_AT_CGSN_WITH_TYPE)) { return NSAPI_ERROR_UNSUPPORTED; } @@ -66,8 +70,10 @@ nsapi_error_t AT_CellularInformation::get_serial_number(char *buf, size_t buf_si nsapi_error_t AT_CellularInformation::get_info(const char *cmd, char *buf, size_t buf_size) { + if (buf == NULL || buf_size == 0) { + return NSAPI_ERROR_PARAMETER; + } _at.lock(); - _at.cmd_start(cmd); _at.cmd_stop(); _at.set_delimiter(0); @@ -75,6 +81,36 @@ nsapi_error_t AT_CellularInformation::get_info(const char *cmd, char *buf, size_ _at.read_string(buf, buf_size - 1); _at.resp_stop(); _at.set_default_delimiter(); + return _at.unlock_return_error(); +} + +nsapi_error_t AT_CellularInformation::get_imsi(char *imsi, size_t buf_size) +{ + if (imsi == NULL || buf_size == 0 || buf_size < MAX_IMSI_LENGTH + 1) { + return NSAPI_ERROR_PARAMETER; + } + _at.lock(); + _at.cmd_start("AT+CIMI"); + _at.cmd_stop(); + _at.resp_start(); + int len = _at.read_string(imsi, MAX_IMSI_LENGTH); + if (len > 0) { + imsi[len] = '\0'; + } + _at.resp_stop(); + return _at.unlock_return_error(); +} +nsapi_error_t AT_CellularInformation::get_iccid(char *buf, size_t buf_size) +{ + if (buf == NULL || buf_size == 0) { + return NSAPI_ERROR_PARAMETER; + } + _at.lock(); + _at.cmd_start("AT+CCID?"); + _at.cmd_stop(); + _at.resp_start("+CCID:"); + _at.read_string(buf, buf_size); + _at.resp_stop(); return _at.unlock_return_error(); } diff --git a/features/cellular/framework/AT/AT_CellularInformation.h b/features/cellular/framework/AT/AT_CellularInformation.h index d9679d3f25a..6e7e6f8cad9 100644 --- a/features/cellular/framework/AT/AT_CellularInformation.h +++ b/features/cellular/framework/AT/AT_CellularInformation.h @@ -42,6 +42,9 @@ class AT_CellularInformation : public CellularInformation, public AT_CellularBas virtual nsapi_error_t get_serial_number(char *buf, size_t buf_size, SerialNumberType type); + virtual nsapi_error_t get_imsi(char *imsi, size_t buf_size); + + virtual nsapi_error_t get_iccid(char *buf, size_t buf_size); protected: /** Request information text from cellular device * diff --git a/features/cellular/framework/AT/AT_CellularNetwork.cpp b/features/cellular/framework/AT/AT_CellularNetwork.cpp index d04de64d09b..538fb27f81f 100644 --- a/features/cellular/framework/AT/AT_CellularNetwork.cpp +++ b/features/cellular/framework/AT/AT_CellularNetwork.cpp @@ -71,7 +71,8 @@ static const char *const rat_str[AT_CellularNetwork::RAT_MAX] = { AT_CellularNetwork::AT_CellularNetwork(ATHandler &atHandler) : AT_CellularBase(atHandler), - _connection_status_cb(NULL), _op_act(RAT_UNKNOWN), _connect_status(NSAPI_STATUS_DISCONNECTED) + _connection_status_cb(NULL), _ciotopt_network_support_cb(NULL), _op_act(RAT_UNKNOWN), + _connect_status(NSAPI_STATUS_DISCONNECTED), _supported_network_opt(CIOT_OPT_MAX) { _urc_funcs[C_EREG] = callback(this, &AT_CellularNetwork::urc_cereg); @@ -79,7 +80,7 @@ AT_CellularNetwork::AT_CellularNetwork(ATHandler &atHandler) : AT_CellularBase(a _urc_funcs[C_REG] = callback(this, &AT_CellularNetwork::urc_creg); for (int type = 0; type < CellularNetwork::C_MAX; type++) { - if (has_registration((RegistrationType)type) != RegistrationModeDisable) { + if (get_property((AT_CellularBase::CellularProperty)type) != RegistrationModeDisable) { _at.set_urc_handler(at_reg[type].urc_prefix, _urc_funcs[type]); } } @@ -87,6 +88,7 @@ AT_CellularNetwork::AT_CellularNetwork(ATHandler &atHandler) : AT_CellularBase(a _at.set_urc_handler("NO CARRIER", callback(this, &AT_CellularNetwork::urc_no_carrier)); // additional urc to get better disconnect info for application. Not critical. _at.set_urc_handler("+CGEV:", callback(this, &AT_CellularNetwork::urc_cgev)); + _at.set_urc_handler("+CCIOTOPTI:", callback(this, &AT_CellularNetwork::urc_cciotopti)); _at.lock(); _at.cmd_start("AT+CGEREP=1");// discard unsolicited result codes when MT TE link is reserved (e.g. in on line data mode); otherwise forward them directly to the TE _at.cmd_stop_read_resp(); @@ -101,13 +103,13 @@ AT_CellularNetwork::~AT_CellularNetwork() _at.unlock(); for (int type = 0; type < CellularNetwork::C_MAX; type++) { - if (has_registration((RegistrationType)type) != RegistrationModeDisable) { - _at.remove_urc_handler(at_reg[type].urc_prefix); + if (get_property((AT_CellularBase::CellularProperty)type) != RegistrationModeDisable) { + _at.set_urc_handler(at_reg[type].urc_prefix, 0); } } - _at.remove_urc_handler("NO CARRIER"); - _at.remove_urc_handler("+CGEV:"); + _at.set_urc_handler("NO CARRIER", 0); + _at.set_urc_handler("+CGEV:", 0); } void AT_CellularNetwork::urc_no_carrier() @@ -221,7 +223,7 @@ nsapi_error_t AT_CellularNetwork::set_registration_urc(RegistrationType type, bo int index = (int)type; MBED_ASSERT(index >= 0 && index < C_MAX); - RegistrationMode mode = has_registration(type); + RegistrationMode mode = (RegistrationMode)get_property((AT_CellularBase::CellularProperty)type); if (mode == RegistrationModeDisable) { return NSAPI_ERROR_UNSUPPORTED; } else { @@ -320,12 +322,6 @@ void AT_CellularNetwork::read_reg_params(RegistrationType type, registration_par #endif } -AT_CellularNetwork::RegistrationMode AT_CellularNetwork::has_registration(RegistrationType reg_type) -{ - (void)reg_type; - return RegistrationModeLAC; -} - nsapi_error_t AT_CellularNetwork::set_attach() { _at.lock(); @@ -379,6 +375,7 @@ nsapi_error_t AT_CellularNetwork::detach() nsapi_error_t AT_CellularNetwork::set_access_technology_impl(RadioAccessTechnology opsAct) { + _op_act = RAT_UNKNOWN; return NSAPI_ERROR_UNSUPPORTED; } @@ -433,14 +430,15 @@ nsapi_error_t AT_CellularNetwork::scan_plmn(operList_t &operators, int &opsCount return _at.unlock_return_error(); } -nsapi_error_t AT_CellularNetwork::set_ciot_optimization_config(Supported_UE_Opt supported_opt, - Preferred_UE_Opt preferred_opt) +nsapi_error_t AT_CellularNetwork::set_ciot_optimization_config(CIoT_Supported_Opt supported_opt, + CIoT_Preferred_UE_Opt preferred_opt, + Callback network_support_cb) { - + _ciotopt_network_support_cb = network_support_cb; _at.lock(); _at.cmd_start("AT+CCIOTOPT="); - _at.write_int(0); // disable urc + _at.write_int(1); //enable CCIOTOPTI URC _at.write_int(supported_opt); _at.write_int(preferred_opt); _at.cmd_stop_read_resp(); @@ -448,8 +446,17 @@ nsapi_error_t AT_CellularNetwork::set_ciot_optimization_config(Supported_UE_Opt return _at.unlock_return_error(); } -nsapi_error_t AT_CellularNetwork::get_ciot_optimization_config(Supported_UE_Opt &supported_opt, - Preferred_UE_Opt &preferred_opt) +void AT_CellularNetwork::urc_cciotopti() +{ + _supported_network_opt = (CIoT_Supported_Opt)_at.read_int(); + + if (_ciotopt_network_support_cb) { + _ciotopt_network_support_cb(_supported_network_opt); + } +} + +nsapi_error_t AT_CellularNetwork::get_ciot_ue_optimization_config(CIoT_Supported_Opt &supported_opt, + CIoT_Preferred_UE_Opt &preferred_opt) { _at.lock(); @@ -459,8 +466,8 @@ nsapi_error_t AT_CellularNetwork::get_ciot_optimization_config(Supported_UE_Opt _at.resp_start("+CCIOTOPT:"); _at.read_int(); if (_at.get_last_error() == NSAPI_ERROR_OK) { - supported_opt = (Supported_UE_Opt)_at.read_int(); - preferred_opt = (Preferred_UE_Opt)_at.read_int(); + supported_opt = (CIoT_Supported_Opt)_at.read_int(); + preferred_opt = (CIoT_Preferred_UE_Opt)_at.read_int(); } _at.resp_stop(); @@ -468,30 +475,13 @@ nsapi_error_t AT_CellularNetwork::get_ciot_optimization_config(Supported_UE_Opt return _at.unlock_return_error(); } -nsapi_error_t AT_CellularNetwork::get_extended_signal_quality(int &rxlev, int &ber, int &rscp, int &ecno, int &rsrq, int &rsrp) +nsapi_error_t AT_CellularNetwork::get_ciot_network_optimization_config(CIoT_Supported_Opt &supported_network_opt) { - _at.lock(); - - _at.cmd_start("AT+CESQ"); - _at.cmd_stop(); - - _at.resp_start("+CESQ:"); - rxlev = _at.read_int(); - ber = _at.read_int(); - rscp = _at.read_int(); - ecno = _at.read_int(); - rsrq = _at.read_int(); - rsrp = _at.read_int(); - _at.resp_stop(); - if (rxlev < 0 || ber < 0 || rscp < 0 || ecno < 0 || rsrq < 0 || rsrp < 0) { - _at.unlock(); - return NSAPI_ERROR_DEVICE_ERROR; - } - - return _at.unlock_return_error(); + supported_network_opt = _supported_network_opt; + return NSAPI_ERROR_OK; } -nsapi_error_t AT_CellularNetwork::get_signal_quality(int &rssi, int &ber) +nsapi_error_t AT_CellularNetwork::get_signal_quality(int &rssi, int *ber) { _at.lock(); @@ -499,18 +489,27 @@ nsapi_error_t AT_CellularNetwork::get_signal_quality(int &rssi, int &ber) _at.cmd_stop(); _at.resp_start("+CSQ:"); - rssi = _at.read_int(); - ber = _at.read_int(); + int t_rssi = _at.read_int(); + int t_ber = _at.read_int(); _at.resp_stop(); - if (rssi < 0 || ber < 0) { + if (t_rssi < 0 || t_ber < 0) { _at.unlock(); return NSAPI_ERROR_DEVICE_ERROR; } - if (rssi == 99) { - rssi = 0; + // RSSI value is returned in dBm with range from -51 to -113 dBm, see 3GPP TS 27.007 + if (t_rssi == 99) { + rssi = SignalQualityUnknown; } else { - rssi = -113 + 2 * rssi; + rssi = -113 + 2 * t_rssi; + } + + if (ber) { + if (t_ber == 99) { + *ber = SignalQualityUnknown; + } else { + *ber = t_ber; + } } return _at.unlock_return_error(); @@ -608,7 +607,7 @@ nsapi_error_t AT_CellularNetwork::get_registration_params(RegistrationType type, int i = (int)type; MBED_ASSERT(i >= 0 && i < C_MAX); - if (!has_registration(at_reg[i].type)) { + if (!get_property((AT_CellularBase::CellularProperty)at_reg[i].type)) { return NSAPI_ERROR_UNSUPPORTED; } @@ -680,3 +679,20 @@ int AT_CellularNetwork::calculate_periodic_tau(const char *periodic_tau_string, return 0; } } + +nsapi_error_t AT_CellularNetwork::set_receive_period(int mode, EDRXAccessTechnology act_type, uint8_t edrx_value) +{ + char edrx[5]; + uint_to_binary_str(edrx_value, edrx, 5, 4); + edrx[4] = '\0'; + + _at.lock(); + + _at.cmd_start("AT+CEDRXS="); + _at.write_int(mode); + _at.write_int(act_type); + _at.write_string(edrx); + _at.cmd_stop_read_resp(); + + return _at.unlock_return_error(); +} diff --git a/features/cellular/framework/AT/AT_CellularNetwork.h b/features/cellular/framework/AT/AT_CellularNetwork.h index 21b30546a24..044bc50c0de 100644 --- a/features/cellular/framework/AT/AT_CellularNetwork.h +++ b/features/cellular/framework/AT/AT_CellularNetwork.h @@ -43,6 +43,12 @@ class AT_CellularNetwork : public CellularNetwork, public AT_CellularBase { // declare friend so it can access stack friend class AT_CellularDevice; + enum RegistrationMode { + RegistrationModeDisable = 0, + RegistrationModeEnable, // + RegistrationModeLAC, // [,<[lac>,]<[ci>],[],[]] + }; + public: // CellularNetwork virtual nsapi_error_t set_registration(const char *plmn = 0); @@ -63,15 +69,16 @@ class AT_CellularNetwork : public CellularNetwork, public AT_CellularBase { virtual nsapi_error_t scan_plmn(operList_t &operators, int &ops_count); - virtual nsapi_error_t set_ciot_optimization_config(Supported_UE_Opt supported_opt, - Preferred_UE_Opt preferred_opt); + virtual nsapi_error_t set_ciot_optimization_config(CIoT_Supported_Opt supported_opt, + CIoT_Preferred_UE_Opt preferred_opt, + Callback network_support_cb); - virtual nsapi_error_t get_ciot_optimization_config(Supported_UE_Opt &supported_opt, - Preferred_UE_Opt &preferred_opt); + virtual nsapi_error_t get_ciot_ue_optimization_config(CIoT_Supported_Opt &supported_opt, + CIoT_Preferred_UE_Opt &preferred_opt); - virtual nsapi_error_t get_extended_signal_quality(int &rxlev, int &ber, int &rscp, int &ecno, int &rsrq, int &rsrp); + virtual nsapi_error_t get_ciot_network_optimization_config(CIoT_Supported_Opt &supported_network_opt); - virtual nsapi_error_t get_signal_quality(int &rssi, int &ber); + virtual nsapi_error_t get_signal_quality(int &rssi, int *ber = NULL); virtual int get_3gpp_error(); @@ -86,19 +93,10 @@ class AT_CellularNetwork : public CellularNetwork, public AT_CellularBase { virtual nsapi_error_t get_registration_params(registration_params_t ®_params); virtual nsapi_error_t get_registration_params(RegistrationType type, registration_params_t ®_params); -protected: - /** Check if modem supports given registration type. - * - * @param reg_type enum RegistrationType - * @return mode supported on given reg_type as per 3GPP TS 27.007, 0 when unsupported - */ - enum RegistrationMode { - RegistrationModeDisable = 0, - RegistrationModeEnable, // - RegistrationModeLAC, // [,<[lac>,]<[ci>],[],[]] - }; - virtual RegistrationMode has_registration(RegistrationType reg_type); + virtual nsapi_error_t set_receive_period(int mode, EDRXAccessTechnology act_type, uint8_t edrx_value); + +protected: /** Sets access technology to be scanned. Modem specific implementation. * @@ -115,6 +113,7 @@ class AT_CellularNetwork : public CellularNetwork, public AT_CellularBase { void urc_cereg(); void urc_cgreg(); void urc_cgev(); + void urc_cciotopti(); void read_reg_params_and_compare(RegistrationType type); void read_reg_params(RegistrationType type, registration_params_t ®_params); @@ -130,8 +129,11 @@ class AT_CellularNetwork : public CellularNetwork, public AT_CellularBase { protected: Callback _connection_status_cb; + Callback _ciotopt_network_support_cb; RadioAccessTechnology _op_act; nsapi_connection_status_t _connect_status; + CIoT_Supported_Opt _supported_network_opt; + registration_params_t _reg_params; mbed::Callback _urc_funcs[C_MAX]; }; diff --git a/features/cellular/framework/AT/AT_CellularPower.cpp b/features/cellular/framework/AT/AT_CellularPower.cpp deleted file mode 100644 index 29b9045df47..00000000000 --- a/features/cellular/framework/AT/AT_CellularPower.cpp +++ /dev/null @@ -1,244 +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 "AT_CellularPower.h" -#include "CellularUtil.h" -#include "CellularLog.h" -#include "CellularTargets.h" -#include "nsapi_types.h" - -static const int PSMTimerBits = 5; - -using namespace mbed_cellular_util; -using namespace mbed; - -AT_CellularPower::AT_CellularPower(ATHandler &at) : AT_CellularBase(at) -{ -} - -AT_CellularPower::~AT_CellularPower() -{ -} - -nsapi_error_t AT_CellularPower::on() -{ - return NSAPI_ERROR_UNSUPPORTED; -} - -nsapi_error_t AT_CellularPower::off() -{ - return NSAPI_ERROR_UNSUPPORTED; -} - -nsapi_error_t AT_CellularPower::set_at_mode() -{ - _at.lock(); - _at.flush(); - _at.cmd_start("ATE0"); // echo off - _at.cmd_stop_read_resp(); - - _at.cmd_start("AT+CMEE=1"); // verbose responses - _at.cmd_stop_read_resp(); - return _at.unlock_return_error(); -} - -nsapi_error_t AT_CellularPower::set_power_level(int func_level, int do_reset) -{ - _at.lock(); - _at.cmd_start("AT+CFUN="); - _at.write_int(func_level); - _at.write_int(do_reset); - _at.cmd_stop_read_resp(); - return _at.unlock_return_error(); -} - -nsapi_error_t AT_CellularPower::reset() -{ - _at.lock(); - _at.cmd_start("AT+CFUN=");// reset to full power levels - _at.write_int(1); - _at.write_int(1); - _at.cmd_stop_read_resp(); - return _at.unlock_return_error(); -} - -nsapi_error_t AT_CellularPower::opt_power_save_mode(int periodic_time, int active_time) -{ - _at.lock(); - - if (periodic_time == 0 && active_time == 0) { - // disable PSM - _at.cmd_start("AT+CPSMS="); - _at.write_int(0); - _at.cmd_stop_read_resp(); - } else { - /** - Table 10.5.163a/3GPP TS 24.008: GPRS Timer 3 information element - - Bits 5 to 1 represent the binary coded timer value. - - Bits 6 to 8 defines the timer value unit for the GPRS timer as follows: - 8 7 6 - 0 0 0 value is incremented in multiples of 10 minutes - 0 0 1 value is incremented in multiples of 1 hour - 0 1 0 value is incremented in multiples of 10 hours - 0 1 1 value is incremented in multiples of 2 seconds - 1 0 0 value is incremented in multiples of 30 seconds - 1 0 1 value is incremented in multiples of 1 minute - 1 1 0 value is incremented in multiples of 320 hours (NOTE 1) - 1 1 1 value indicates that the timer is deactivated (NOTE 2). - */ - char pt[8 + 1]; // timer value encoded as 3GPP IE - const int ie_value_max = 0x1f; - uint32_t periodic_timer = 0; - if (periodic_time <= 2 * ie_value_max) { // multiples of 2 seconds - periodic_timer = periodic_time / 2; - strcpy(pt, "01100000"); - } else { - if (periodic_time <= 30 * ie_value_max) { // multiples of 30 seconds - periodic_timer = periodic_time / 30; - strcpy(pt, "10000000"); - } else { - if (periodic_time <= 60 * ie_value_max) { // multiples of 1 minute - periodic_timer = periodic_time / 60; - strcpy(pt, "10100000"); - } else { - if (periodic_time <= 10 * 60 * ie_value_max) { // multiples of 10 minutes - periodic_timer = periodic_time / (10 * 60); - strcpy(pt, "00000000"); - } else { - if (periodic_time <= 60 * 60 * ie_value_max) { // multiples of 1 hour - periodic_timer = periodic_time / (60 * 60); - strcpy(pt, "00100000"); - } else { - if (periodic_time <= 10 * 60 * 60 * ie_value_max) { // multiples of 10 hours - periodic_timer = periodic_time / (10 * 60 * 60); - strcpy(pt, "01000000"); - } else { // multiples of 320 hours - int t = periodic_time / (320 * 60 * 60); - if (t > ie_value_max) { - t = ie_value_max; - } - periodic_timer = t; - strcpy(pt, "11000000"); - } - } - } - } - } - } - - uint_to_binary_str(periodic_timer, &pt[3], sizeof(pt) - 3, PSMTimerBits); - pt[8] = '\0'; - - /** - Table 10.5.172/3GPP TS 24.008: GPRS Timer information element - - Bits 5 to 1 represent the binary coded timer value. - - Bits 6 to 8 defines the timer value unit for the GPRS timer as follows: - - 8 7 6 - 0 0 0 value is incremented in multiples of 2 seconds - 0 0 1 value is incremented in multiples of 1 minute - 0 1 0 value is incremented in multiples of decihours - 1 1 1 value indicates that the timer is deactivated. - - Other values shall be interpreted as multiples of 1 minute in this version of the protocol. - */ - char at[8 + 1]; - uint32_t active_timer; // timer value encoded as 3GPP IE - if (active_time <= 2 * ie_value_max) { // multiples of 2 seconds - active_timer = active_time / 2; - strcpy(at, "00000000"); - } else { - if (active_time <= 60 * ie_value_max) { // multiples of 1 minute - active_timer = (1 << 5) | (active_time / 60); - strcpy(at, "00100000"); - } else { // multiples of decihours - int t = active_time / (6 * 60); - if (t > ie_value_max) { - t = ie_value_max; - } - active_timer = t; - strcpy(at, "01000000"); - } - } - - uint_to_binary_str(active_timer, &at[3], sizeof(at) - 3, PSMTimerBits); - at[8] = '\0'; - - // request for both GPRS and LTE - _at.cmd_start("AT+CPSMS="); - _at.write_int(1); - _at.write_string(pt); - _at.write_string(at); - _at.write_string(pt); - _at.write_string(at); - _at.cmd_stop_read_resp(); - - if (_at.get_last_error() != NSAPI_ERROR_OK) { - tr_warn("Power save mode not enabled!"); - } else { - // network may not agree with power save options but - // that should be fine as timeout is not longer than requested - } - } - - return _at.unlock_return_error(); -} - -nsapi_error_t AT_CellularPower::opt_receive_period(int mode, EDRXAccessTechnology act_type, uint8_t edrx_value) -{ - char edrx[5]; - uint_to_binary_str(edrx_value, edrx, 5, 4); - edrx[4] = '\0'; - - _at.lock(); - - _at.cmd_start("AT+CEDRXS="); - _at.write_int(mode); - _at.write_int(act_type); - _at.write_string(edrx); - _at.cmd_stop_read_resp(); - - return _at.unlock_return_error(); -} - -nsapi_error_t AT_CellularPower::is_device_ready() -{ - _at.lock(); - _at.cmd_start("AT"); - _at.cmd_stop_read_resp(); - - // we need to do this twice because for example after data mode the first 'AT' command will give modem a - // stimulus that we are back to command mode. - _at.clear_error(); - _at.cmd_start("AT"); - _at.cmd_stop_read_resp(); - - return _at.unlock_return_error(); -} - -nsapi_error_t AT_CellularPower::set_device_ready_urc_cb(mbed::Callback callback) -{ - return NSAPI_ERROR_UNSUPPORTED; -} - -void AT_CellularPower::remove_device_ready_urc_cb(mbed::Callback callback) -{ -} diff --git a/features/cellular/framework/AT/AT_CellularPower.h b/features/cellular/framework/AT/AT_CellularPower.h deleted file mode 100644 index 4e27abe5b45..00000000000 --- a/features/cellular/framework/AT/AT_CellularPower.h +++ /dev/null @@ -1,60 +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. - */ - -#ifndef AT_CELLULAR_POWER_H_ -#define AT_CELLULAR_POWER_H_ - -#include "CellularPower.h" -#include "AT_CellularBase.h" - -namespace mbed { - -/** - * Class AT_CellularPower - * - * Class that provides power handling functions for modem/module. - */ -class AT_CellularPower : public CellularPower, public AT_CellularBase { -public: - AT_CellularPower(ATHandler &atHandler); - virtual ~AT_CellularPower(); - -public: - virtual nsapi_error_t on(); - - virtual nsapi_error_t off(); - - virtual nsapi_error_t set_at_mode(); - - virtual nsapi_error_t set_power_level(int func_level, int do_reset = 0); - - virtual nsapi_error_t reset(); - - virtual nsapi_error_t opt_power_save_mode(int periodic_time, int active_time); - - virtual nsapi_error_t opt_receive_period(int mode, EDRXAccessTechnology act_type, uint8_t edrx_value); - - virtual nsapi_error_t is_device_ready(); - - virtual nsapi_error_t set_device_ready_urc_cb(mbed::Callback callback); - - virtual void remove_device_ready_urc_cb(mbed::Callback callback); -}; - -} // namespace mbed - -#endif /* AT_CELLULAR_POWER_H_ */ diff --git a/features/cellular/framework/AT/AT_CellularSIM.cpp b/features/cellular/framework/AT/AT_CellularSIM.cpp deleted file mode 100644 index 86425a852a6..00000000000 --- a/features/cellular/framework/AT/AT_CellularSIM.cpp +++ /dev/null @@ -1,155 +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 "AT_CellularSIM.h" -#include "CellularLog.h" - -using namespace mbed; - -const int MAX_SIM_RESPONSE_LENGTH = 16; - -AT_CellularSIM::AT_CellularSIM(ATHandler &at) : AT_CellularBase(at) -{ -} - -AT_CellularSIM::~AT_CellularSIM() -{ -} - -nsapi_error_t AT_CellularSIM::get_sim_state(SimState &state) -{ - char simstr[MAX_SIM_RESPONSE_LENGTH]; - _at.lock(); - _at.flush(); - _at.cmd_start("AT+CPIN?"); - _at.cmd_stop(); - _at.resp_start("+CPIN:"); - ssize_t len = _at.read_string(simstr, sizeof(simstr)); - if (len != -1) { - if (len >= 5 && memcmp(simstr, "READY", 5) == 0) { - state = SimStateReady; - } else if (len >= 6 && memcmp(simstr, "SIM PIN", 6) == 0) { - state = SimStatePinNeeded; - } else if (len >= 6 && memcmp(simstr, "SIM PUK", 6) == 0) { - state = SimStatePukNeeded; - } else { - simstr[len] = '\0'; - tr_error("Unknown SIM state %s", simstr); - state = SimStateUnknown; - } - } else { - tr_warn("SIM not readable."); - state = SimStateUnknown; // SIM may not be ready yet or +CPIN may be unsupported command - } - _at.resp_stop(); - nsapi_error_t error = _at.get_last_error(); - _at.unlock(); -#if MBED_CONF_MBED_TRACE_ENABLE - switch (state) { - case SimStatePinNeeded: - tr_info("SIM PIN required"); - break; - case SimStatePukNeeded: - tr_error("SIM PUK required"); - break; - case SimStateUnknown: - tr_warn("SIM state unknown"); - break; - default: - tr_info("SIM is ready"); - break; - } -#endif - return error; -} - - -nsapi_error_t AT_CellularSIM::set_pin(const char *sim_pin) -{ - // if SIM is already in ready state then settings the PIN - // will return error so let's check the state before settings the pin. - SimState state; - if (get_sim_state(state) == NSAPI_ERROR_OK && state == SimStateReady) { - return NSAPI_ERROR_OK; - } - - _at.lock(); - _at.cmd_start("AT+CPIN="); - _at.write_string(sim_pin); - _at.cmd_stop_read_resp(); - return _at.unlock_return_error(); -} - -nsapi_error_t AT_CellularSIM::change_pin(const char *sim_pin, const char *new_pin) -{ - _at.lock(); - _at.cmd_start("AT+CPWD="); - _at.write_string("SC"); - _at.write_string(sim_pin); - _at.write_string(new_pin); - _at.cmd_stop_read_resp(); - return _at.unlock_return_error(); -} - -nsapi_error_t AT_CellularSIM::set_pin_query(const char *sim_pin, bool query_pin) -{ - _at.lock(); - if (query_pin) { - /* use the SIM locked */ - _at.cmd_start("AT+CLCK="); - _at.write_string("SC"); - _at.write_int(1); - _at.write_string(sim_pin); - _at.cmd_stop_read_resp(); - } else { - /* use the SIM unlocked */ - _at.cmd_start("AT+CLCK="); - _at.write_string("SC"); - _at.write_int(0); - _at.write_string(sim_pin); - _at.cmd_stop_read_resp(); - } - return _at.unlock_return_error(); -} - -nsapi_error_t AT_CellularSIM::get_imsi(char *imsi) -{ - if (imsi == NULL) { - return NSAPI_ERROR_PARAMETER; - } - _at.lock(); - _at.cmd_start("AT+CIMI"); - _at.cmd_stop(); - _at.resp_start(); - int len = _at.read_string(imsi, MAX_IMSI_LENGTH); - if (len > 0) { - imsi[len] = '\0'; - } - _at.resp_stop(); - return _at.unlock_return_error(); -} - -nsapi_error_t AT_CellularSIM::get_iccid(char *buf, size_t buf_size) -{ - _at.lock(); - _at.cmd_start("AT+CCID?"); - _at.cmd_stop(); - _at.resp_start("+CCID:"); - _at.read_string(buf, buf_size); - _at.resp_stop(); - return _at.unlock_return_error(); -} diff --git a/features/cellular/framework/AT/AT_CellularSIM.h b/features/cellular/framework/AT/AT_CellularSIM.h deleted file mode 100644 index 379de706bca..00000000000 --- a/features/cellular/framework/AT/AT_CellularSIM.h +++ /dev/null @@ -1,53 +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. - */ - -#ifndef AT_CELLULAR_SIM_H_ -#define AT_CELLULAR_SIM_H_ - -#include "CellularSIM.h" -#include "AT_CellularBase.h" - -namespace mbed { - -/** - * Class AT_CellularSIM - * - * Class for SIM card handling. - */ -class AT_CellularSIM : public CellularSIM, public AT_CellularBase { - -public: - AT_CellularSIM(ATHandler &atHandler); - virtual ~AT_CellularSIM(); - -public: - virtual nsapi_error_t set_pin(const char *sim_pin); - - virtual nsapi_error_t change_pin(const char *sim_pin, const char *new_pin); - - virtual nsapi_error_t set_pin_query(const char *sim_pin, bool query_pin); - - virtual nsapi_error_t get_sim_state(SimState &state); - - virtual nsapi_error_t get_imsi(char *imsi); - - virtual nsapi_error_t get_iccid(char *buf, size_t buf_size); -}; - -} // namespace mbed - -#endif // AT_CELLULAR_SIM_H_ diff --git a/features/cellular/framework/AT/AT_CellularSMS.cpp b/features/cellular/framework/AT/AT_CellularSMS.cpp index 3dc7125e7e4..fe5ff6d7e55 100644 --- a/features/cellular/framework/AT/AT_CellularSMS.cpp +++ b/features/cellular/framework/AT/AT_CellularSMS.cpp @@ -248,10 +248,8 @@ nsapi_error_t AT_CellularSMS::set_csdh(int show_header) nsapi_error_t AT_CellularSMS::initialize(CellularSMSMmode mode) { - if (NSAPI_ERROR_OK != _at.set_urc_handler("+CMTI:", callback(this, &AT_CellularSMS::cmti_urc)) || - NSAPI_ERROR_OK != _at.set_urc_handler("+CMT:", callback(this, &AT_CellularSMS::cmt_urc))) { - return NSAPI_ERROR_NO_MEMORY; - } + _at.set_urc_handler("+CMTI:", callback(this, &AT_CellularSMS::cmti_urc)); + _at.set_urc_handler("+CMT:", callback(this, &AT_CellularSMS::cmt_urc)); _at.lock(); set_cnmi(); //set new SMS indication @@ -1047,11 +1045,6 @@ nsapi_error_t AT_CellularSMS::list_messages() _at.resp_start("+CMGL:"); while (_at.info_resp()) { info = new sms_info_t(); - if (!info) { - _at.resp_stop(); - return NSAPI_ERROR_NO_MEMORY; - } - if (_mode == CellularSMSMmodePDU) { //+CMGL: ,,[],[ // +CMGL:,,[], @@ -1062,11 +1055,6 @@ nsapi_error_t AT_CellularSMS::list_messages() length = length * 2 + 20; // *2 as it's hex encoded and +20 as service center number is not included in size given by CMGL pdu = new char[length]; memset(pdu, 0, length); - if (!pdu) { - delete info; - _at.resp_stop(); - return NSAPI_ERROR_NO_MEMORY; - } _at.read_string(pdu, length, true); if (_at.get_last_error() == NSAPI_ERROR_OK) { info->msg_size = get_data_from_pdu(pdu, info, &part_number); @@ -1194,9 +1182,6 @@ uint16_t AT_CellularSMS::pack_7_bit_gsm_and_hex(const char *str, uint16_t len, c } // convert to 7bit gsm first char *gsm_str = new char[len]; - if (!gsm_str) { - return 0; - } for (uint16_t y = 0; y < len; y++) { for (int x = 0; x < GSM_TO_ASCII_TABLE_SIZE; x++) { if (gsm_to_ascii[x] == str[y]) { diff --git a/features/cellular/framework/AT/AT_CellularStack.cpp b/features/cellular/framework/AT/AT_CellularStack.cpp index 5008884f61b..994d41d894c 100644 --- a/features/cellular/framework/AT/AT_CellularStack.cpp +++ b/features/cellular/framework/AT/AT_CellularStack.cpp @@ -116,11 +116,6 @@ nsapi_error_t AT_CellularStack::socket_open(nsapi_socket_t *handle, nsapi_protoc } _socket = new CellularSocket*[max_socket_count]; - if (!_socket) { - tr_error("No memory to open socket!"); - _socket_mutex.unlock(); - return NSAPI_ERROR_NO_SOCKET; - } _socket_count = max_socket_count; for (int i = 0; i < max_socket_count; i++) { _socket[i] = 0; diff --git a/features/cellular/framework/AT/AT_ControlPlane_netif.cpp b/features/cellular/framework/AT/AT_ControlPlane_netif.cpp new file mode 100644 index 00000000000..0ed38f5fa01 --- /dev/null +++ b/features/cellular/framework/AT/AT_ControlPlane_netif.cpp @@ -0,0 +1,77 @@ +/*AT_ControlPlane_netif.cpp*/ +#include "AT_ControlPlane_netif.h" + +namespace mbed { + +AT_ControlPlane_netif::AT_ControlPlane_netif(ATHandler &at, int cid) : AT_CellularBase(at), + _cid(cid), _cb(NULL), _data(NULL), _recv_len(0) +{ + _at.set_urc_handler("+CRTDCP:", mbed::Callback(this, &AT_ControlPlane_netif::urc_cp_recv)); +} + +AT_ControlPlane_netif::~AT_ControlPlane_netif() +{} + +void AT_ControlPlane_netif::urc_cp_recv() +{ + //+CRTDCP: ,, + _at.lock(); + int cid = _at.read_int(); + int cpdata_length = _at.read_int(); + int read_len = _at.read_string(_recv_buffer, sizeof(_recv_buffer)); + + _at.unlock(); + + // cid not expected to be different because: one context - one file handle + // so this file handle cannot get urc from different context + if (read_len > 0 && read_len == cpdata_length && cid == _cid) { + _recv_len = read_len; + data_received(); + } +} + +nsapi_size_or_error_t AT_ControlPlane_netif::send(const void *cpdata, nsapi_size_t cpdata_length) +{ + //CSODCP + _at.lock(); + _at.cmd_start("AT+CSODCP="); + _at.write_int(_cid); + _at.write_int(cpdata_length); + _at.write_bytes((uint8_t *)cpdata, cpdata_length); + + return _at.unlock_return_error(); +} + +nsapi_size_or_error_t AT_ControlPlane_netif::recv(void *cpdata, nsapi_size_t cpdata_length) +{ + // If no data received through CRTDCP URC + if (!_recv_len) { + return NSAPI_ERROR_WOULD_BLOCK; + } + + // If too small buffer for data + if (_recv_len > cpdata_length) { + return NSAPI_ERROR_DEVICE_ERROR; + } + + memcpy(cpdata, _recv_buffer, _recv_len); + + return _recv_len = 0; +} + +void AT_ControlPlane_netif::attach(void (*callback)(void *), void *data) +{ + _cb = callback; + _data = data; +} + +void AT_ControlPlane_netif::data_received() +{ + // call socket event + if (!_cb) { + return; + } + _cb(_data); +} + +} //mbed namespace diff --git a/features/cellular/framework/AT/AT_ControlPlane_netif.h b/features/cellular/framework/AT/AT_ControlPlane_netif.h new file mode 100644 index 00000000000..c33d4f3daad --- /dev/null +++ b/features/cellular/framework/AT/AT_ControlPlane_netif.h @@ -0,0 +1,32 @@ +#include "ControlPlane_netif.h" +#include "ATHandler.h" +#include "AT_CellularBase.h" + +namespace mbed { + +class AT_ControlPlane_netif: public ControlPlane_netif, public AT_CellularBase { +public: + AT_ControlPlane_netif(ATHandler &at, int cid); + virtual ~AT_ControlPlane_netif(); + +// ControlPlane_netif + // +CSODCP: 3GPP 27007 10.1.43 + virtual nsapi_size_or_error_t send(const void *cpdata, nsapi_size_t cpdata_length); + // +CRTDCP: 3GPP 27007 10.1.44 + virtual nsapi_size_or_error_t recv(void *cpdata, nsapi_size_t cpdata_length); + virtual void attach(void (*callback)(void *), void *data); + virtual void data_received(); + +protected: + // Id of the PDP context that enables the control plane data connection + int _cid; + +private: + void (*_cb)(void *); + void *_data; + char _recv_buffer[MAX_CP_DATA_RECV_LEN]; + size_t _recv_len; + void urc_cp_recv(); +}; + +} //mbed namespace diff --git a/features/cellular/framework/common/CellularList.h b/features/cellular/framework/common/CellularList.h index d8ee8660fc9..37f41774477 100644 --- a/features/cellular/framework/common/CellularList.h +++ b/features/cellular/framework/common/CellularList.h @@ -50,9 +50,6 @@ template class CellularList { T *add_new() { T *temp = new T; - if (!temp) { - return NULL; - } temp->next = NULL; if (_head == NULL) { _head = temp; diff --git a/features/cellular/framework/device/CellularContext.cpp b/features/cellular/framework/device/CellularContext.cpp index 64dfc7f13a4..0c88ccdf510 100644 --- a/features/cellular/framework/device/CellularContext.cpp +++ b/features/cellular/framework/device/CellularContext.cpp @@ -16,23 +16,24 @@ */ #include "CellularContext.h" +MBED_WEAK CellularBase *CellularBase::get_target_default_instance() +{ + return mbed::CellularContext::get_default_instance(); +} + namespace mbed { -#ifdef CELLULAR_DEVICE + MBED_WEAK CellularContext *CellularContext::get_default_instance() { - // Uses default APN, uname, password from mbed_app.json static CellularDevice *dev = CellularDevice::get_default_instance(); if (!dev) { return NULL; } static CellularContext *context = dev->create_context(); +#if defined(MDMDCD) && defined(MDM_PIN_POLARITY) + context->set_file_handle(static_cast(&dev->get_file_handle()), MDMDCD, MDM_PIN_POLARITY); +#endif // #if defined(MDMDCD) && defined(MDM_PIN_POLARITY) return context; } -#else -MBED_WEAK CellularContext *CellularContext::get_default_instance() -{ - return NULL; -} -#endif // CELLULAR_DEVICE } // namespace mbed diff --git a/features/cellular/framework/device/CellularDevice.cpp b/features/cellular/framework/device/CellularDevice.cpp index 6ad46ddc67d..a8ad05e00f2 100644 --- a/features/cellular/framework/device/CellularDevice.cpp +++ b/features/cellular/framework/device/CellularDevice.cpp @@ -17,42 +17,27 @@ #include "CellularDevice.h" #include "CellularContext.h" -#include "CellularSIM.h" #include "CellularUtil.h" #include "CellularLog.h" #include "CellularTargets.h" #include "EventQueue.h" -#include "UARTSerial.h" - -#ifdef CELLULAR_DEVICE -#include CELLULAR_STRINGIFY(CELLULAR_DEVICE.h) -#endif // CELLULAR_DEVICE namespace mbed { -#ifdef CELLULAR_DEVICE MBED_WEAK CellularDevice *CellularDevice::get_default_instance() { - static UARTSerial serial(MDMTXD, MDMRXD, MBED_CONF_PLATFORM_DEFAULT_SERIAL_BAUD_RATE); -#if DEVICE_SERIAL_FC - if (MDMRTS != NC && MDMCTS != NC) { - tr_info("_USING flow control, MDMRTS: %d MDMCTS: %d", MDMRTS, MDMCTS); - serial.set_flow_control(SerialBase::RTSCTS, MDMRTS, MDMCTS); - } -#endif - static CELLULAR_DEVICE device(&serial); - return &device; + return get_target_default_instance(); } -#else -MBED_WEAK CellularDevice *CellularDevice::get_default_instance() + +MBED_WEAK CellularDevice *CellularDevice::get_target_default_instance() { return NULL; } -#endif // CELLULAR_DEVICE -CellularDevice::CellularDevice(FileHandle *fh) : _network_ref_count(0), _sms_ref_count(0), _power_ref_count(0), _sim_ref_count(0), - _info_ref_count(0), _fh(fh), _queue(5 * EVENTS_EVENT_SIZE), _state_machine(0), _nw(0) +CellularDevice::CellularDevice(FileHandle *fh) : _network_ref_count(0), _sms_ref_count(0), + _info_ref_count(0), _fh(fh), _queue(5 * EVENTS_EVENT_SIZE), _state_machine(0), _nw(0), _status_cb(0) { + MBED_ASSERT(fh); set_sim_pin(NULL); set_plmn(NULL); } @@ -67,6 +52,11 @@ void CellularDevice::stop() _state_machine->stop(); } +FileHandle &CellularDevice::get_file_handle() const +{ + return *_fh; +} + events::EventQueue *CellularDevice::get_queue() { return &_queue; @@ -158,6 +148,11 @@ nsapi_error_t CellularDevice::start_state_machine(CellularStateMachine::Cellular return err; } +void CellularDevice::attach(Callback status_cb) +{ + _status_cb = status_cb; +} + void CellularDevice::cellular_callback(nsapi_event_t ev, intptr_t ptr) { if (ev >= NSAPI_EVENT_CELLULAR_STATUS_BASE && ev <= NSAPI_EVENT_CELLULAR_STATUS_END) { @@ -179,7 +174,7 @@ void CellularDevice::cellular_callback(nsapi_event_t ev, intptr_t ptr) _state_machine->set_plmn(_plmn); } } else if (cell_ev == CellularSIMStatusChanged && ptr_data->error == NSAPI_ERROR_OK && - ptr_data->status_data == CellularSIM::SimStatePinNeeded) { + ptr_data->status_data == SimStatePinNeeded) { if (strlen(_sim_pin)) { _state_machine->set_sim_pin(_sim_pin); } @@ -200,6 +195,21 @@ void CellularDevice::cellular_callback(nsapi_event_t ev, intptr_t ptr) curr->cellular_callback(ev, ptr); curr = curr->_next; } + + // forward to callback function if set by attach(...) + if (_status_cb) { + _status_cb(ev, ptr); + } +} + +nsapi_error_t CellularDevice::shutdown() +{ + CellularContext *curr = get_context_list(); + while (curr) { + curr->cellular_callback(NSAPI_EVENT_CONNECTION_STATUS_CHANGE, NSAPI_STATUS_DISCONNECTED); + curr = (CellularContext *)curr->_next; + } + return NSAPI_ERROR_OK; } } // namespace mbed diff --git a/features/cellular/framework/device/CellularStateMachine.cpp b/features/cellular/framework/device/CellularStateMachine.cpp index 75e9416c14f..2ea5b830fd5 100644 --- a/features/cellular/framework/device/CellularStateMachine.cpp +++ b/features/cellular/framework/device/CellularStateMachine.cpp @@ -17,8 +17,6 @@ #include "CellularStateMachine.h" #include "CellularDevice.h" -#include "CellularPower.h" -#include "CellularSIM.h" #include "CellularLog.h" #include "Thread.h" #include "UARTSerial.h" @@ -45,7 +43,7 @@ namespace mbed { CellularStateMachine::CellularStateMachine(CellularDevice &device, events::EventQueue &queue) : _cellularDevice(device), _state(STATE_INIT), _next_state(_state), _target_state(_state), - _event_status_cb(0), _network(0), _power(0), _sim(0), _queue(queue), _queue_thread(0), _sim_pin(0), + _event_status_cb(0), _network(0), _queue(queue), _queue_thread(0), _sim_pin(0), _retry_count(0), _event_timeout(-1), _event_id(-1), _plmn(0), _command_success(false), _plmn_network_found(false), _is_retry(false), _cb_data(), _current_event(NSAPI_EVENT_CONNECTION_STATUS_CHANGE), _active_context(false) @@ -84,6 +82,7 @@ void CellularStateMachine::reset() _plmn_network_found = false; _is_retry = false; _active_context = false; + _target_state = STATE_INIT; enter_to_state(STATE_INIT); } @@ -99,15 +98,6 @@ void CellularStateMachine::stop() reset(); _event_id = STM_STOPPED; - if (_power) { - _cellularDevice.close_power(); - _power = NULL; - } - - if (_sim) { - _cellularDevice.close_sim(); - _sim = NULL; - } if (_network) { _cellularDevice.close_network(); @@ -117,13 +107,9 @@ void CellularStateMachine::stop() bool CellularStateMachine::power_on() { - _cb_data.error = _power->on(); - if (_cb_data.error != NSAPI_ERROR_OK && _cb_data.error != NSAPI_ERROR_UNSUPPORTED) { - tr_warn("Power on failed. Try to power off/on."); - _cb_data.error = _power->off(); - if (_cb_data.error != NSAPI_ERROR_OK && _cb_data.error != NSAPI_ERROR_UNSUPPORTED) { - tr_error("Power off failed!"); - } + _cb_data.error = _cellularDevice.hard_power_on(); + if (_cb_data.error != NSAPI_ERROR_OK) { + tr_warn("Power on failed."); return false; } return true; @@ -141,15 +127,10 @@ void CellularStateMachine::set_plmn(const char *plmn) bool CellularStateMachine::open_sim() { - if (!_sim) { - // can only fail with allocation with new and then it's critical error - _sim = _cellularDevice.open_sim(); - } - - CellularSIM::SimState state = CellularSIM::SimStateUnknown; + CellularDevice::SimState state = CellularDevice::SimStateUnknown; // wait until SIM is readable // here you could add wait(secs) if you know start delay of your SIM - _cb_data.error = _sim->get_sim_state(state); + _cb_data.error = _cellularDevice.get_sim_state(state); if (_cb_data.error != NSAPI_ERROR_OK) { tr_info("Waiting for SIM (err while reading)..."); return false; @@ -161,10 +142,10 @@ bool CellularStateMachine::open_sim() _event_status_cb((nsapi_event_t)CellularSIMStatusChanged, (intptr_t)&_cb_data); } - if (state == CellularSIM::SimStatePinNeeded) { - if (strlen(_sim_pin)) { - tr_info("Entering PIN to open SIM."); - _cb_data.error = _sim->set_pin(_sim_pin); + if (state == CellularDevice::SimStatePinNeeded) { + if (_sim_pin) { + tr_info("Entering PIN to open SIM"); + _cb_data.error = _cellularDevice.set_pin(_sim_pin); if (_cb_data.error) { tr_error("Failed to set PIN: error %d", _cb_data.error); } @@ -176,7 +157,7 @@ bool CellularStateMachine::open_sim() } } - return state == CellularSIM::SimStateReady; + return state == CellularDevice::SimStateReady; } bool CellularStateMachine::is_registered() @@ -345,10 +326,7 @@ void CellularStateMachine::state_init() { _cellularDevice.set_timeout(TIMEOUT_POWER_ON); tr_info("Start connecting (timeout %d s)", TIMEOUT_POWER_ON / 1000); - if (!_power) { - _power = _cellularDevice.open_power(); - } - _cb_data.error = _power->is_device_ready(); + _cb_data.error = _cellularDevice.is_ready(); if (_cb_data.error != NSAPI_ERROR_OK) { _event_timeout = _start_time; if (_start_time > 0) { @@ -375,9 +353,6 @@ void CellularStateMachine::state_power_on() bool CellularStateMachine::device_ready() { tr_info("Modem ready"); - if (_cellularDevice.init_module() != NSAPI_ERROR_OK) { - return false; - } if (!_network) { _network = _cellularDevice.open_network(); @@ -396,26 +371,25 @@ bool CellularStateMachine::device_ready() if (_event_status_cb) { _event_status_cb((nsapi_event_t)CellularDeviceReady, (intptr_t)&_cb_data); } - - _power->remove_device_ready_urc_cb(mbed::callback(this, &CellularStateMachine::ready_urc_cb)); - _cellularDevice.close_power(); - _power = NULL; + _cellularDevice.set_ready_cb(0); return true; } void CellularStateMachine::state_device_ready() { _cellularDevice.set_timeout(TIMEOUT_POWER_ON); - _cb_data.error = _power->set_at_mode(); + _cb_data.error = _cellularDevice.soft_power_on(); if (_cb_data.error == NSAPI_ERROR_OK) { - if (device_ready()) { - enter_to_state(STATE_SIM_PIN); - } else { - retry_state_or_fail(); + _cb_data.error = _cellularDevice.init(); + if (_cb_data.error == NSAPI_ERROR_OK) { + if (device_ready()) { + enter_to_state(STATE_SIM_PIN); + } } - } else { + } + if (_cb_data.error != NSAPI_ERROR_OK) { if (_retry_count == 0) { - _power->set_device_ready_urc_cb(mbed::callback(this, &CellularStateMachine::ready_urc_cb)); + _cellularDevice.set_ready_cb(callback(this, &CellularStateMachine::device_ready_cb)); } retry_state_or_fail(); } @@ -501,8 +475,6 @@ void CellularStateMachine::state_attaching() tr_info("Attaching network (timeout %d s)", TIMEOUT_CONNECT / 1000); _cb_data.error = _network->set_attach(); if (_cb_data.error == NSAPI_ERROR_OK) { - _cellularDevice.close_sim(); - _sim = NULL; if (_event_status_cb) { _cb_data.status_data = CellularNetwork::Attached; _event_status_cb(_current_event, (intptr_t)&_cb_data); @@ -532,8 +504,13 @@ void CellularStateMachine::continue_from_state(CellularState state) nsapi_error_t CellularStateMachine::run_to_state(CellularStateMachine::CellularState state) { _mutex.lock(); + + CellularState tmp_state = state; + if (_plmn && tmp_state == STATE_REGISTERING_NETWORK) { + tmp_state = STATE_MANUAL_REGISTERING_NETWORK; + } // call pre_event via queue so that it's in same thread and it's safe to decisions - int id = _queue.call_in(0, this, &CellularStateMachine::pre_event, state); + int id = _queue.call_in(0, this, &CellularStateMachine::pre_event, tmp_state); if (!id) { stop(); _mutex.unlock(); @@ -575,7 +552,11 @@ bool CellularStateMachine::get_current_status(CellularStateMachine::CellularStat _mutex.lock(); current_state = _state; target_state = _target_state; - is_running = _event_id != -1; + if (_event_id == -1 || _event_id == STM_STOPPED) { + is_running = false; + } else { + is_running = true; + } _mutex.unlock(); return is_running; } @@ -585,9 +566,8 @@ void CellularStateMachine::event() #if MBED_CONF_MBED_TRACE_ENABLE if (_network) { int rssi; - int ber; - if (_network->get_signal_quality(rssi, ber) == NSAPI_ERROR_OK) { - if (rssi == 0) { + if (_network->get_signal_quality(rssi) == NSAPI_ERROR_OK) { + if (rssi == CellularNetwork::SignalQualityUnknown) { tr_info("RSSI unknown"); } else { tr_info("RSSI %d dBm", rssi); @@ -633,7 +613,7 @@ void CellularStateMachine::event() break; } - if ((_target_state == _state && _cb_data.error == NSAPI_ERROR_OK && !_is_retry) || _event_id == STM_STOPPED) { + if (check_is_target_reached()) { _event_id = -1; return; } @@ -676,6 +656,20 @@ void CellularStateMachine::set_cellular_callback(mbed::Callbackset_at_mode() == NSAPI_ERROR_OK) { + tr_debug("Device ready callback"); + if (_state == STATE_DEVICE_READY && _cellularDevice.init() == NSAPI_ERROR_OK) { tr_debug("State was STATE_DEVICE_READY and at mode ready, cancel state and move to next"); _queue.cancel(_event_id); + _event_id = -1; if (device_ready()) { - continue_from_state(STATE_SIM_PIN); + _is_retry = false; + if (!check_is_target_reached()) { + continue_from_state(STATE_SIM_PIN); + } } else { continue_from_state(STATE_DEVICE_READY); } } } -void CellularStateMachine::set_retry_timeout_array(uint16_t timeout[], int array_len) +void CellularStateMachine::set_retry_timeout_array(const uint16_t timeout[], int array_len) { + if (!timeout || array_len <= 0) { + tr_warn("set_retry_timeout_array, timeout array null or invalid length"); + return; + } _retry_array_length = array_len > RETRY_ARRAY_SIZE ? RETRY_ARRAY_SIZE : array_len; for (int i = 0; i < _retry_array_length; i++) { diff --git a/features/cellular/framework/device/CellularStateMachine.h b/features/cellular/framework/device/CellularStateMachine.h index afff6f05abc..235e15f8d21 100644 --- a/features/cellular/framework/device/CellularStateMachine.h +++ b/features/cellular/framework/device/CellularStateMachine.h @@ -28,8 +28,6 @@ class Thread; namespace mbed { -class CellularPower; -class CellularSIM; class CellularDevice; const int RETRY_ARRAY_SIZE = 10; @@ -43,6 +41,7 @@ class CellularStateMachine { // friend of CellularDevice so that it's the only way to close/delete this class. friend class CellularDevice; friend class AT_CellularDevice; + friend class UT_CellularStateMachine; // for unit tests /** Constructor * * @param device reference to CellularDevice @@ -99,7 +98,7 @@ class CellularStateMachine { * @param timeout timeout array using seconds * @param array_len length of the array */ - void set_retry_timeout_array(uint16_t timeout[], int array_len); + void set_retry_timeout_array(const uint16_t timeout[], int array_len); /** Sets the operator plmn which is used when registering to a network specified by plmn. If plmn is not set then automatic * registering is used when registering to a cellular network. Does not start any operations. @@ -155,8 +154,9 @@ class CellularStateMachine { bool is_registered_to_plmn(); void report_failure(const char *msg); void event(); - void ready_urc_cb(); + void device_ready_cb(); void pre_event(CellularState state); + bool check_is_target_reached(); CellularDevice &_cellularDevice; CellularState _state; @@ -166,8 +166,6 @@ class CellularStateMachine { Callback _event_status_cb; CellularNetwork *_network; - CellularPower *_power; - CellularSIM *_sim; events::EventQueue &_queue; rtos::Thread *_queue_thread; diff --git a/features/cellular/framework/targets/GEMALTO/CINTERION/GEMALTO_CINTERION.cpp b/features/cellular/framework/targets/GEMALTO/CINTERION/GEMALTO_CINTERION.cpp index 5b59dd9a9b4..2397e2aa590 100644 --- a/features/cellular/framework/targets/GEMALTO/CINTERION/GEMALTO_CINTERION.cpp +++ b/features/cellular/framework/targets/GEMALTO/CINTERION/GEMALTO_CINTERION.cpp @@ -15,14 +15,12 @@ * limitations under the License. */ -#include "GEMALTO_CINTERION_CellularNetwork.h" #include "GEMALTO_CINTERION_Module.h" #include "GEMALTO_CINTERION_CellularContext.h" #include "GEMALTO_CINTERION.h" #include "AT_CellularInformation.h" #include "CellularLog.h" - using namespace mbed; using namespace events; @@ -32,22 +30,18 @@ GEMALTO_CINTERION::GEMALTO_CINTERION(FileHandle *fh) : AT_CellularDevice(fh) { } -GEMALTO_CINTERION::~GEMALTO_CINTERION() +AT_CellularContext *GEMALTO_CINTERION::create_context_impl(ATHandler &at, const char *apn, bool cp_req, bool nonip_req) { + return new GEMALTO_CINTERION_CellularContext(at, this, apn, cp_req, nonip_req); } -AT_CellularNetwork *GEMALTO_CINTERION::open_network_impl(ATHandler &at) +nsapi_error_t GEMALTO_CINTERION::init() { - return new GEMALTO_CINTERION_CellularNetwork(at); -} - -AT_CellularContext *GEMALTO_CINTERION::create_context_impl(ATHandler &at, const char *apn) -{ - return new GEMALTO_CINTERION_CellularContext(at, this, apn); -} + nsapi_error_t err = AT_CellularDevice::init(); + if (err != NSAPI_ERROR_OK) { + return err; + } -nsapi_error_t GEMALTO_CINTERION::init_module() -{ CellularInformation *information = open_information(); if (!information) { return NSAPI_ERROR_NO_MEMORY; @@ -66,3 +60,17 @@ uint16_t GEMALTO_CINTERION::get_send_delay() const { return RESPONSE_TO_SEND_DELAY; } + +#if MBED_CONF_GEMALTO_CINTERION_PROVIDE_DEFAULT +#include "UARTSerial.h" +CellularDevice *CellularDevice::get_default_instance() +{ + static UARTSerial serial(MBED_CONF_GEMALTO_CINTERION_TX, MBED_CONF_GEMALTO_CINTERION_RX, MBED_CONF_GEMALTO_CINTERION_BAUDRATE); +#if defined (MBED_CONF_UBLOX_AT_RTS) && defined(MBED_CONF_UBLOX_AT_CTS) + tr_debug("GEMALTO_CINTERION flow control: RTS %d CTS %d", MBED_CONF_GEMALTO_CINTERION_RTS, MBED_CONF_GEMALTO_CINTERION_CTS); + serial.set_flow_control(SerialBase::RTSCTS, MBED_CONF_GEMALTO_CINTERION_RTS, MBED_CONF_GEMALTO_CINTERION_CTS); +#endif + static GEMALTO_CINTERION device(&serial); + return &device; +} +#endif diff --git a/features/cellular/framework/targets/GEMALTO/CINTERION/GEMALTO_CINTERION.h b/features/cellular/framework/targets/GEMALTO/CINTERION/GEMALTO_CINTERION.h index 137641f0f61..dd502de3074 100644 --- a/features/cellular/framework/targets/GEMALTO/CINTERION/GEMALTO_CINTERION.h +++ b/features/cellular/framework/targets/GEMALTO/CINTERION/GEMALTO_CINTERION.h @@ -18,22 +18,28 @@ #ifndef GEMALTO_CINTERION_H_ #define GEMALTO_CINTERION_H_ +#ifdef TARGET_FF_ARDUINO +#ifndef MBED_CONF_GEMALTO_CINTERION_TX +#define MBED_CONF_GEMALTO_CINTERION_TX D1 +#endif +#ifndef MBED_CONF_GEMALTO_CINTERION_RX +#define MBED_CONF_GEMALTO_CINTERION_RX D0 +#endif +#endif /* TARGET_FF_ARDUINO */ + #include "AT_CellularDevice.h" namespace mbed { class GEMALTO_CINTERION : public AT_CellularDevice { public: - GEMALTO_CINTERION(FileHandle *fh); - virtual ~GEMALTO_CINTERION(); protected: // AT_CellularDevice - virtual AT_CellularNetwork *open_network_impl(ATHandler &at); - virtual AT_CellularContext *create_context_impl(ATHandler &at, const char *apn); -public: - virtual nsapi_error_t init_module(); + virtual AT_CellularContext *create_context_impl(ATHandler &at, const char *apn, bool cp_req = false, bool nonip_req = false); +protected: virtual uint16_t get_send_delay() const; + virtual nsapi_error_t init(); }; } // namespace mbed diff --git a/features/cellular/framework/targets/GEMALTO/CINTERION/GEMALTO_CINTERION_CellularContext.cpp b/features/cellular/framework/targets/GEMALTO/CINTERION/GEMALTO_CINTERION_CellularContext.cpp index 322f53e1c72..57cc9128bd1 100644 --- a/features/cellular/framework/targets/GEMALTO/CINTERION/GEMALTO_CINTERION_CellularContext.cpp +++ b/features/cellular/framework/targets/GEMALTO/CINTERION/GEMALTO_CINTERION_CellularContext.cpp @@ -17,11 +17,12 @@ #include "GEMALTO_CINTERION_CellularContext.h" #include "GEMALTO_CINTERION_CellularStack.h" #include "GEMALTO_CINTERION_Module.h" +#include "CellularLog.h" namespace mbed { GEMALTO_CINTERION_CellularContext::GEMALTO_CINTERION_CellularContext(ATHandler &at, CellularDevice *device, - const char *apn) : AT_CellularContext(at, device, apn) + const char *apn, bool cp_req, bool nonip_req) : AT_CellularContext(at, device, apn, cp_req, nonip_req) { } @@ -32,19 +33,16 @@ GEMALTO_CINTERION_CellularContext::~GEMALTO_CINTERION_CellularContext() #if !NSAPI_PPP_AVAILABLE NetworkStack *GEMALTO_CINTERION_CellularContext::get_stack() { + if (_pdp_type == NON_IP_PDP_TYPE || _cp_in_use) { + tr_error("Requesting stack for NON-IP context! Should request control plane netif: get_cp_netif()"); + return NULL; + } + if (!_stack) { - _stack = new GEMALTO_CINTERION_CellularStack(_at, _apn, _cid, _ip_stack_type); + _stack = new GEMALTO_CINTERION_CellularStack(_at, _apn, _cid, (nsapi_ip_stack_t)_pdp_type); } return _stack; } #endif // NSAPI_PPP_AVAILABLE -bool GEMALTO_CINTERION_CellularContext::stack_type_supported(nsapi_ip_stack_t requested_stack) -{ - if (GEMALTO_CINTERION_Module::get_model() == GEMALTO_CINTERION_Module::ModelBGS2) { - return (requested_stack == IPV4_STACK); - } - return (requested_stack == IPV4_STACK || requested_stack == IPV6_STACK); -} - } /* namespace mbed */ diff --git a/features/cellular/framework/targets/GEMALTO/CINTERION/GEMALTO_CINTERION_CellularContext.h b/features/cellular/framework/targets/GEMALTO/CINTERION/GEMALTO_CINTERION_CellularContext.h index c77c5af703a..86209eda599 100644 --- a/features/cellular/framework/targets/GEMALTO/CINTERION/GEMALTO_CINTERION_CellularContext.h +++ b/features/cellular/framework/targets/GEMALTO/CINTERION/GEMALTO_CINTERION_CellularContext.h @@ -23,14 +23,13 @@ namespace mbed { class GEMALTO_CINTERION_CellularContext: public AT_CellularContext { public: - GEMALTO_CINTERION_CellularContext(ATHandler &at, CellularDevice *device, const char *apn); + GEMALTO_CINTERION_CellularContext(ATHandler &at, CellularDevice *device, const char *apn, bool cp_req = false, bool nonip_req = false); virtual ~GEMALTO_CINTERION_CellularContext(); protected: #if !NSAPI_PPP_AVAILABLE virtual NetworkStack *get_stack(); #endif // NSAPI_PPP_AVAILABLE - virtual bool stack_type_supported(nsapi_ip_stack_t requested_stack); }; } /* namespace mbed */ diff --git a/features/cellular/framework/targets/GEMALTO/CINTERION/GEMALTO_CINTERION_CellularNetwork.cpp b/features/cellular/framework/targets/GEMALTO/CINTERION/GEMALTO_CINTERION_CellularNetwork.cpp deleted file mode 100644 index 65f944fc1db..00000000000 --- a/features/cellular/framework/targets/GEMALTO/CINTERION/GEMALTO_CINTERION_CellularNetwork.cpp +++ /dev/null @@ -1,49 +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 "GEMALTO_CINTERION_CellularNetwork.h" -#include "GEMALTO_CINTERION_Module.h" - -using namespace mbed; - -GEMALTO_CINTERION_CellularNetwork::GEMALTO_CINTERION_CellularNetwork(ATHandler &atHandler) : AT_CellularNetwork(atHandler) -{ -} - -GEMALTO_CINTERION_CellularNetwork::~GEMALTO_CINTERION_CellularNetwork() -{ -} - -AT_CellularNetwork::RegistrationMode GEMALTO_CINTERION_CellularNetwork::has_registration(RegistrationType reg_type) -{ - if (GEMALTO_CINTERION_Module::get_model() == GEMALTO_CINTERION_Module::ModelEMS31) { - return (reg_type == C_EREG) ? RegistrationModeLAC : RegistrationModeDisable; - } - if (GEMALTO_CINTERION_Module::get_model() == GEMALTO_CINTERION_Module::ModelBGS2) { - if (reg_type == C_GREG) { - return RegistrationModeEnable; - } - return (reg_type == C_REG) ? RegistrationModeLAC : RegistrationModeDisable; - } - return (reg_type == C_REG || reg_type == C_GREG || reg_type == C_EREG) ? RegistrationModeLAC : RegistrationModeDisable; -} - -nsapi_error_t GEMALTO_CINTERION_CellularNetwork::set_access_technology_impl(RadioAccessTechnology opsAct) -{ - _op_act = RAT_UNKNOWN; - return NSAPI_ERROR_UNSUPPORTED; -} diff --git a/features/cellular/framework/targets/GEMALTO/CINTERION/GEMALTO_CINTERION_CellularNetwork.h b/features/cellular/framework/targets/GEMALTO/CINTERION/GEMALTO_CINTERION_CellularNetwork.h deleted file mode 100644 index e01c7e81877..00000000000 --- a/features/cellular/framework/targets/GEMALTO/CINTERION/GEMALTO_CINTERION_CellularNetwork.h +++ /dev/null @@ -1,37 +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. - */ - -#ifndef GEMALTO_CINTERION_CELLULAR_NETWORK_H_ -#define GEMALTO_CINTERION_CELLULAR_NETWORK_H_ - -#include "AT_CellularNetwork.h" - -namespace mbed { - -class GEMALTO_CINTERION_CellularNetwork : public AT_CellularNetwork { -public: - GEMALTO_CINTERION_CellularNetwork(ATHandler &atHandler); - virtual ~GEMALTO_CINTERION_CellularNetwork(); - -protected: - virtual RegistrationMode has_registration(RegistrationType reg_type); - virtual nsapi_error_t set_access_technology_impl(RadioAccessTechnology opsAct); -}; - -} // namespace mbed - -#endif // GEMALTO_CINTERION_CELLULAR_NETWORK_H_ diff --git a/features/cellular/framework/targets/GEMALTO/CINTERION/GEMALTO_CINTERION_CellularStack.cpp b/features/cellular/framework/targets/GEMALTO/CINTERION/GEMALTO_CINTERION_CellularStack.cpp index 1c0128ea914..86cafcbc607 100644 --- a/features/cellular/framework/targets/GEMALTO/CINTERION/GEMALTO_CINTERION_CellularStack.cpp +++ b/features/cellular/framework/targets/GEMALTO/CINTERION/GEMALTO_CINTERION_CellularStack.cpp @@ -39,9 +39,9 @@ GEMALTO_CINTERION_CellularStack::GEMALTO_CINTERION_CellularStack(ATHandler &atHa GEMALTO_CINTERION_CellularStack::~GEMALTO_CINTERION_CellularStack() { - _at.remove_urc_handler("^SIS:"); - _at.remove_urc_handler("^SISW:"); - _at.remove_urc_handler("^SISR:"); + _at.set_urc_handler("^SIS:", 0); + _at.set_urc_handler("^SISW:", 0); + _at.set_urc_handler("^SISR:", 0); } GEMALTO_CINTERION_CellularStack::CellularSocket *GEMALTO_CINTERION_CellularStack::find_socket(int sock_id) diff --git a/features/cellular/framework/targets/GEMALTO/CINTERION/GEMALTO_CINTERION_Module.cpp b/features/cellular/framework/targets/GEMALTO/CINTERION/GEMALTO_CINTERION_Module.cpp index 0f52bfcbe8c..34448bf83ce 100644 --- a/features/cellular/framework/targets/GEMALTO/CINTERION/GEMALTO_CINTERION_Module.cpp +++ b/features/cellular/framework/targets/GEMALTO/CINTERION/GEMALTO_CINTERION_Module.cpp @@ -18,48 +18,69 @@ #include #include "AT_CellularBase.h" +#include "AT_CellularNetwork.h" #include "GEMALTO_CINTERION_Module.h" #include "CellularLog.h" using namespace mbed; -// unsupported features as per ELS61-E2_ATC_V01.000 -static const AT_CellularBase::SupportedFeature unsupported_features_els61[] = { - AT_CellularBase::AT_CGSN_WITH_TYPE, - AT_CellularBase::SUPPORTED_FEATURE_END_MARK +static const intptr_t cellular_properties_els61[AT_CellularBase::PROPERTY_MAX] = { + AT_CellularNetwork::RegistrationModeLAC, // C_EREG + AT_CellularNetwork::RegistrationModeLAC, // C_GREG + AT_CellularNetwork::RegistrationModeLAC, // C_REG + 0, // AT_CGSN_WITH_TYPE + 1, // AT_CGDATA + 1, // AT_CGAUTH + 1, // PROPERTY_IPV4_STACK + 1, // PROPERTY_IPV6_STACK + 0, // PROPERTY_IPV4V6_STACK }; -// unsupported features as per BGS2-W_ATC_V00.100 -static const AT_CellularBase::SupportedFeature unsupported_features_bgs2[] = { - AT_CellularBase::AT_CGSN_WITH_TYPE, - AT_CellularBase::SUPPORTED_FEATURE_END_MARK +static const intptr_t cellular_properties_bgs2[AT_CellularBase::PROPERTY_MAX] = { + AT_CellularNetwork::RegistrationModeDisable, // C_EREG + AT_CellularNetwork::RegistrationModeEnable, // C_GREG + AT_CellularNetwork::RegistrationModeLAC, // C_REG + 0, // AT_CGSN_WITH_TYPE + 1, // AT_CGDATA + 1, // AT_CGAUTH + 1, // PROPERTY_IPV4_STACK + 0, // PROPERTY_IPV6_STACK + 0, // PROPERTY_IPV4V6_STACK + }; -// unsupported features as per EMS31-US_ATC_V4.9.5 -static const AT_CellularBase::SupportedFeature unsupported_features_ems31[] = { - AT_CellularBase::SUPPORTED_FEATURE_END_MARK +static const intptr_t cellular_properties_ems31[AT_CellularBase::PROPERTY_MAX] = { + AT_CellularNetwork::RegistrationModeLAC, // C_EREG + AT_CellularNetwork::RegistrationModeDisable, // C_GREG + AT_CellularNetwork::RegistrationModeDisable, // C_REG + 1, // AT_CGSN_WITH_TYPE + 1, // AT_CGDATA + 1, // AT_CGAUTH + 1, // PROPERTY_IPV4_STACK + 1, // PROPERTY_IPV6_STACK + 0, // PROPERTY_IPV4V6_STACK }; GEMALTO_CINTERION_Module::Model GEMALTO_CINTERION_Module::_model; nsapi_error_t GEMALTO_CINTERION_Module::detect_model(const char *model) { - static const AT_CellularBase::SupportedFeature *unsupported_features; + static const intptr_t *cellular_properties; if (memcmp(model, "ELS61", sizeof("ELS61") - 1) == 0) { _model = ModelELS61; - unsupported_features = unsupported_features_els61; + cellular_properties = cellular_properties_els61; } else if (memcmp(model, "BGS2", sizeof("BGS2") - 1) == 0) { _model = ModelBGS2; - unsupported_features = unsupported_features_bgs2; + cellular_properties = cellular_properties_bgs2; } else if (memcmp(model, "EMS31", sizeof("EMS31") - 1) == 0) { _model = ModelEMS31; - unsupported_features = unsupported_features_ems31; + cellular_properties = cellular_properties_ems31; } else { tr_error("Cinterion model unsupported %s", model); return NSAPI_ERROR_UNSUPPORTED; } tr_info("Cinterion model %s (%d)", model, _model); - AT_CellularBase::set_unsupported_features(unsupported_features); + AT_CellularBase::set_cellular_properties(cellular_properties); return NSAPI_ERROR_OK; } diff --git a/features/cellular/framework/targets/GEMALTO/CINTERION/mbed_lib.json b/features/cellular/framework/targets/GEMALTO/CINTERION/mbed_lib.json new file mode 100644 index 00000000000..132526d15c3 --- /dev/null +++ b/features/cellular/framework/targets/GEMALTO/CINTERION/mbed_lib.json @@ -0,0 +1,29 @@ +{ + "name": "GEMALTO_CINTERION", + "config": { + "tx": { + "help": "TX pin for serial connection. D1 assumed if Arduino Form Factor, needs to be set/overwritten otherwise.", + "value": null + }, + "rx": { + "help": "RX pin for serial connection. D0 assumed if Arduino Form Factor, needs to be set/overwritten otherwise.", + "value": null + }, + "rts": { + "help": "RTS pin for serial connection", + "value": null + }, + "cts": { + "help": "CTS pin for serial connection", + "value": null + }, + "baudrate" : { + "help": "Serial connection baud rate", + "value": 115200 + }, + "provide-default": { + "help": "Provide as default CellularDevice [true/false]", + "value": false + } + } +} diff --git a/features/cellular/framework/targets/GENERIC/GENERIC_AT3GPP/GENERIC_AT3GPP.cpp b/features/cellular/framework/targets/GENERIC/GENERIC_AT3GPP/GENERIC_AT3GPP.cpp new file mode 100644 index 00000000000..a7203fb0c43 --- /dev/null +++ b/features/cellular/framework/targets/GENERIC/GENERIC_AT3GPP/GENERIC_AT3GPP.cpp @@ -0,0 +1,53 @@ +/* + * 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 "GENERIC_AT3GPP.h" +#include "AT_CellularNetwork.h" + +using namespace mbed; + +// by default all properties are supported +static const intptr_t cellular_properties[AT_CellularBase::PROPERTY_MAX] = { + AT_CellularNetwork::RegistrationModeLAC, // C_EREG + AT_CellularNetwork::RegistrationModeLAC, // C_GREG + AT_CellularNetwork::RegistrationModeLAC, // C_REG + 1, // AT_CGSN_WITH_TYPE + 1, // AT_CGDATA + 1, // AT_CGAUTH + 1, // PROPERTY_IPV4_STACK + 1, // PROPERTY_IPV6_STACK + 1, // PROPERTY_IPV4V6_STACK +}; + +GENERIC_AT3GPP::GENERIC_AT3GPP(FileHandle *fh) : AT_CellularDevice(fh) +{ + AT_CellularBase::set_cellular_properties(cellular_properties); +} + +#if MBED_CONF_GENERIC_AT3GPP_PROVIDE_DEFAULT +#include "UARTSerial.h" +CellularDevice *CellularDevice::get_default_instance() +{ + static UARTSerial serial(MBED_CONF_GENERIC_AT3GPP_TX, MBED_CONF_GENERIC_AT3GPP_RX, MBED_CONF_GENERIC_AT3GPP_BAUDRATE); +#if defined (MBED_CONF_GENERIC_AT3GPP_RTS) && defined(MBED_CONF_GENERIC_AT3GPP_CTS) + tr_debug("GENERIC_AT3GPP flow control: RTS %d CTS %d", MBED_CONF_GENERIC_AT3GPP_RTS, MBED_CONF_GENERIC_AT3GPP_CTS); + serial.set_flow_control(SerialBase::RTSCTS, MBED_CONF_GENERIC_AT3GPP_RTS, MBED_CONF_GENERIC_AT3GPP_CTS); +#endif + static GENERIC_AT3GPP device(&serial); + return &device; +} +#endif diff --git a/features/cellular/framework/targets/GENERIC/GENERIC_AT3GPP/GENERIC_AT3GPP.h b/features/cellular/framework/targets/GENERIC/GENERIC_AT3GPP/GENERIC_AT3GPP.h new file mode 100644 index 00000000000..692f735ed37 --- /dev/null +++ b/features/cellular/framework/targets/GENERIC/GENERIC_AT3GPP/GENERIC_AT3GPP.h @@ -0,0 +1,52 @@ +/* + * 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 GENERIC_AT3GPP_H_ +#define GENERIC_AT3GPP_H_ + +#ifdef TARGET_FF_ARDUINO +#ifndef MBED_CONF_GENERIC_AT3GPP_TX +#define MBED_CONF_GENERIC_AT3GPP_TX D1 +#endif +#ifndef MBED_CONF_GENERIC_AT3GPP_RX +#define MBED_CONF_GENERIC_AT3GPP_RX D0 +#endif +#endif /* TARGET_FF_ARDUINO */ + +#include "AT_CellularDevice.h" + +namespace mbed { + +/** + * Generic Cellular module which can be used as a default module when porting new cellular module. + * GENERIC_AT3GPP uses standard 3GPP AT commands (3GPP TS 27.007 V14.5.0 (2017-09)) to communicate with the modem. + * + * GENERIC_AT3GPP can be used as a shield for example on top K64F. + * Cellular example can be used for testing: https://github.com/ARMmbed/mbed-os-example-cellular + * Define in mbed_app.json "target_overrides" correct pins and other setup for your modem. + * + * If new target don't work with GENERIC_AT3GPP then it needs some customizations. + * First thing to try can be checking/modifying cellular_properties array in GENERIC_AT3GPP.cpp, does the module support + * these commands or not? Modify array and if that's not enough then some AT_xxx classes might need to be created and + * methods overridden. Check help how other modules are done what methods they have overridden. Happy porting! + */ +class GENERIC_AT3GPP : public AT_CellularDevice { +public: + GENERIC_AT3GPP(FileHandle *fh); +}; +} // namespace mbed +#endif // GENERIC_AT3GPP_H_ diff --git a/features/cellular/framework/targets/GENERIC/GENERIC_AT3GPP/mbed_lib.json b/features/cellular/framework/targets/GENERIC/GENERIC_AT3GPP/mbed_lib.json new file mode 100644 index 00000000000..ed4b9173ff9 --- /dev/null +++ b/features/cellular/framework/targets/GENERIC/GENERIC_AT3GPP/mbed_lib.json @@ -0,0 +1,29 @@ +{ + "name": "GENERIC_AT3GPP", + "config": { + "tx": { + "help": "TX pin for serial connection. D1 assumed if Arduino Form Factor, needs to be set/overwritten otherwise.", + "value": null + }, + "rx": { + "help": "RX pin for serial connection. D0 assumed if Arduino Form Factor, needs to be set/overwritten otherwise.", + "value": null + }, + "rts": { + "help": "RTS pin for serial connection", + "value": null + }, + "cts": { + "help": "CTS pin for serial connection", + "value": null + }, + "baudrate" : { + "help": "Serial connection baud rate", + "value": 115200 + }, + "provide-default": { + "help": "Provide as default CellularDevice [true/false]", + "value": false + } + } +} diff --git a/features/cellular/framework/targets/MultiTech/DragonflyNano/PPP/SARA4_PPP.cpp b/features/cellular/framework/targets/MultiTech/DragonflyNano/PPP/SARA4_PPP.cpp index c91941fc61b..55fe2df1de2 100644 --- a/features/cellular/framework/targets/MultiTech/DragonflyNano/PPP/SARA4_PPP.cpp +++ b/features/cellular/framework/targets/MultiTech/DragonflyNano/PPP/SARA4_PPP.cpp @@ -17,25 +17,25 @@ #include "SARA4_PPP.h" #include "SARA4_PPP_CellularNetwork.h" -#include "SARA4_PPP_CellularPower.h" -#include "SARA4_PPP_CellularContext.h" using namespace mbed; using namespace events; -static const AT_CellularBase::SupportedFeature unsupported_features[] = { - AT_CellularBase::AT_CGSN_WITH_TYPE, - AT_CellularBase::AT_CGDATA, - AT_CellularBase::SUPPORTED_FEATURE_END_MARK +static const intptr_t cellular_properties[AT_CellularBase::PROPERTY_MAX] = { + AT_CellularNetwork::RegistrationModeDisable,// C_EREG + AT_CellularNetwork::RegistrationModeLAC, // C_GREG + AT_CellularNetwork::RegistrationModeLAC, // C_REG + 0, // AT_CGSN_WITH_TYPE + 0, // AT_CGDATA + 1, // AT_CGAUTH + 1, // PROPERTY_IPV4_STACK + 0, // PROPERTY_IPV6_STACK + 0, // PROPERTY_IPV4V6_STACK }; SARA4_PPP::SARA4_PPP(FileHandle *fh) : AT_CellularDevice(fh) { - AT_CellularBase::set_unsupported_features(unsupported_features); -} - -SARA4_PPP::~SARA4_PPP() -{ + AT_CellularBase::set_cellular_properties(cellular_properties); } AT_CellularNetwork *SARA4_PPP::open_network_impl(ATHandler &at) @@ -43,12 +43,16 @@ AT_CellularNetwork *SARA4_PPP::open_network_impl(ATHandler &at) return new SARA4_PPP_CellularNetwork(at); } -AT_CellularPower *SARA4_PPP::open_power_impl(ATHandler &at) -{ - return new SARA4_PPP_CellularPower(at); -} - -AT_CellularContext *SARA4_PPP::create_context_impl(ATHandler &at, const char *apn) +#if MBED_CONF_SARA4_PPP_PROVIDE_DEFAULT +#include "UARTSerial.h" +CellularDevice *CellularDevice::get_default_instance() { - return new SARA4_PPP_CellularContext(at, this, apn); + static UARTSerial serial(MBED_CONF_SARA4_PPP_TX, MBED_CONF_SARA4_PPP_RX, MBED_CONF_SARA4_PPP_BAUDRATE); +#if defined (MBED_CONF_SARA4_PPP_RTS) && defined (MBED_CONF_SARA4_PPP_CTS) + tr_debug("SARA4_PPP flow control: RTS %d CTS %d", MBED_CONF_SARA4_PPP_RTS, MBED_CONF_SARA4_PPP_CTS); + serial.set_flow_control(SerialBase::RTSCTS, MBED_CONF_SARA4_PPP_RTS, MBED_CONF_SARA4_PPP_CTS); +#endif + static SARA4_PPP device(&serial); + return &device; } +#endif diff --git a/features/cellular/framework/targets/MultiTech/DragonflyNano/PPP/SARA4_PPP.h b/features/cellular/framework/targets/MultiTech/DragonflyNano/PPP/SARA4_PPP.h index 6a9f346d7a1..e4f49e30857 100644 --- a/features/cellular/framework/targets/MultiTech/DragonflyNano/PPP/SARA4_PPP.h +++ b/features/cellular/framework/targets/MultiTech/DragonflyNano/PPP/SARA4_PPP.h @@ -18,6 +18,15 @@ #ifndef SARA4_PPP_H_ #define SARA4_PPP_H_ +#ifdef TARGET_FF_ARDUINO +#ifndef MBED_CONF_SARA4_PPP_TX +#define MBED_CONF_SARA4_PPP_TX D1 +#endif +#ifndef MBED_CONF_SARA4_PPP_RX +#define MBED_CONF_SARA4_PPP_RX D0 +#endif +#endif /* TARGET_FF_ARDUINO */ + #include "AT_CellularDevice.h" namespace mbed { @@ -26,12 +35,9 @@ class SARA4_PPP : public AT_CellularDevice { public: SARA4_PPP(FileHandle *fh); - virtual ~SARA4_PPP(); public: // CellularDevice virtual AT_CellularNetwork *open_network_impl(ATHandler &at); - virtual AT_CellularPower *open_power_impl(ATHandler &at); - virtual AT_CellularContext *create_context_impl(ATHandler &at, const char *apn); }; } // namespace mbed diff --git a/features/cellular/framework/targets/MultiTech/DragonflyNano/PPP/SARA4_PPP_CellularContext.h b/features/cellular/framework/targets/MultiTech/DragonflyNano/PPP/SARA4_PPP_CellularContext.h deleted file mode 100644 index be4a1779120..00000000000 --- a/features/cellular/framework/targets/MultiTech/DragonflyNano/PPP/SARA4_PPP_CellularContext.h +++ /dev/null @@ -1,35 +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. - */ -#ifndef SARA4_PPP_CELLULARCONTEXT_H_ -#define SARA4_PPP_CELLULARCONTEXT_H_ - -#include "AT_CellularContext.h" - -namespace mbed { - -class SARA4_PPP_CellularContext: public AT_CellularContext { -public: - SARA4_PPP_CellularContext(ATHandler &at, CellularDevice *device, const char *apn); - virtual ~SARA4_PPP_CellularContext(); - -protected: - virtual bool stack_type_supported(nsapi_ip_stack_t requested_stack); -}; - -} /* namespace mbed */ - -#endif // SARA4_PPP_CELLULARCONTEXT_H_ diff --git a/features/cellular/framework/targets/MultiTech/DragonflyNano/PPP/SARA4_PPP_CellularNetwork.cpp b/features/cellular/framework/targets/MultiTech/DragonflyNano/PPP/SARA4_PPP_CellularNetwork.cpp index 5e2c891cb8d..a505d6f09c9 100644 --- a/features/cellular/framework/targets/MultiTech/DragonflyNano/PPP/SARA4_PPP_CellularNetwork.cpp +++ b/features/cellular/framework/targets/MultiTech/DragonflyNano/PPP/SARA4_PPP_CellularNetwork.cpp @@ -27,11 +27,6 @@ SARA4_PPP_CellularNetwork::~SARA4_PPP_CellularNetwork() { } -AT_CellularNetwork::RegistrationMode SARA4_PPP_CellularNetwork::has_registration(RegistrationType reg_type) -{ - return (reg_type == C_REG || reg_type == C_GREG) ? RegistrationModeLAC : RegistrationModeDisable; -} - nsapi_error_t SARA4_PPP_CellularNetwork::set_access_technology_impl(RadioAccessTechnology opRat) { _op_act = RAT_CATM1; diff --git a/features/cellular/framework/targets/MultiTech/DragonflyNano/PPP/SARA4_PPP_CellularNetwork.h b/features/cellular/framework/targets/MultiTech/DragonflyNano/PPP/SARA4_PPP_CellularNetwork.h index 123b15d387a..2799bb58e68 100644 --- a/features/cellular/framework/targets/MultiTech/DragonflyNano/PPP/SARA4_PPP_CellularNetwork.h +++ b/features/cellular/framework/targets/MultiTech/DragonflyNano/PPP/SARA4_PPP_CellularNetwork.h @@ -28,8 +28,6 @@ class SARA4_PPP_CellularNetwork : public AT_CellularNetwork { virtual ~SARA4_PPP_CellularNetwork(); protected: - virtual RegistrationMode has_registration(RegistrationType rat); - virtual nsapi_error_t set_access_technology_impl(RadioAccessTechnology opRat); }; diff --git a/features/cellular/framework/targets/MultiTech/DragonflyNano/PPP/mbed_lib.json b/features/cellular/framework/targets/MultiTech/DragonflyNano/PPP/mbed_lib.json new file mode 100644 index 00000000000..8ebfad7a3e1 --- /dev/null +++ b/features/cellular/framework/targets/MultiTech/DragonflyNano/PPP/mbed_lib.json @@ -0,0 +1,29 @@ +{ + "name": "SARA4_PPP", + "config": { + "tx": { + "help": "TX pin for serial connection. D1 assumed if Arduino Form Factor, needs to be set/overwritten otherwise.", + "value": null + }, + "rx": { + "help": "RX pin for serial connection. D0 assumed if Arduino Form Factor, needs to be set/overwritten otherwise.", + "value": null + }, + "rts": { + "help": "RTS pin for serial connection", + "value": null + }, + "cts": { + "help": "CTS pin for serial connection", + "value": null + }, + "baudrate" : { + "help": "Serial connection baud rate", + "value": 115200 + }, + "provide-default": { + "help": "Provide as default CellularDevice [true/false]", + "value": false + } + } +} diff --git a/features/cellular/framework/targets/QUECTEL/BC95/QUECTEL_BC95.cpp b/features/cellular/framework/targets/QUECTEL/BC95/QUECTEL_BC95.cpp index 6a586974787..a76aedccb94 100644 --- a/features/cellular/framework/targets/QUECTEL/BC95/QUECTEL_BC95.cpp +++ b/features/cellular/framework/targets/QUECTEL/BC95/QUECTEL_BC95.cpp @@ -16,33 +16,50 @@ */ #include "QUECTEL_BC95_CellularNetwork.h" -#include "QUECTEL_BC95_CellularPower.h" -#include "QUECTEL_BC95_CellularSIM.h" #include "QUECTEL_BC95_CellularContext.h" +#include "QUECTEL_BC95_CellularInformation.h" #include "QUECTEL_BC95.h" +#include "CellularLog.h" #define CONNECT_DELIM "\r\n" #define CONNECT_BUFFER_SIZE (1280 + 80 + 80) // AT response + sscanf format #define CONNECT_TIMEOUT 8000 -#define MAX_STARTUP_TRIALS 5 -#define MAX_RESET_TRIALS 5 - using namespace events; using namespace mbed; -static const AT_CellularBase::SupportedFeature unsupported_features[] = { - AT_CellularBase::AT_CGAUTH, // BC95_AT_Commands_Manual_V1.9 - AT_CellularBase::SUPPORTED_FEATURE_END_MARK +static const intptr_t cellular_properties[AT_CellularBase::PROPERTY_MAX] = { + AT_CellularNetwork::RegistrationModeLAC, // C_EREG + AT_CellularNetwork::RegistrationModeDisable, // C_GREG + AT_CellularNetwork::RegistrationModeDisable, // C_REG + 1, // AT_CGSN_WITH_TYPE + 1, // AT_CGDATA + 0, // AT_CGAUTH, BC95_AT_Commands_Manual_V1.9 + 1, // PROPERTY_IPV4_STACK + 0, // PROPERTY_IPV6_STACK + 0, // PROPERTY_IPV4V6_STACK }; QUECTEL_BC95::QUECTEL_BC95(FileHandle *fh) : AT_CellularDevice(fh) { - AT_CellularBase::set_unsupported_features(unsupported_features); + AT_CellularBase::set_cellular_properties(cellular_properties); } -QUECTEL_BC95::~QUECTEL_BC95() +nsapi_error_t QUECTEL_BC95::get_sim_state(SimState &state) { + _at->lock(); + _at->flush(); + _at->cmd_start("AT+NCCID?"); + _at->cmd_stop(); + _at->resp_start("+NCCID:"); + if (_at->info_resp()) { + state = SimStateReady; + } else { + tr_warn("SIM not readable."); + state = SimStateUnknown; // SIM may not be ready yet + } + _at->resp_stop(); + return _at->unlock_return_error(); } AT_CellularNetwork *QUECTEL_BC95::open_network_impl(ATHandler &at) @@ -50,17 +67,40 @@ AT_CellularNetwork *QUECTEL_BC95::open_network_impl(ATHandler &at) return new QUECTEL_BC95_CellularNetwork(at); } -AT_CellularPower *QUECTEL_BC95::open_power_impl(ATHandler &at) +AT_CellularContext *QUECTEL_BC95::create_context_impl(ATHandler &at, const char *apn, bool cp_req, bool nonip_req) +{ + return new QUECTEL_BC95_CellularContext(at, this, apn, cp_req, nonip_req); +} + +AT_CellularInformation *QUECTEL_BC95::open_information_impl(ATHandler &at) { - return new QUECTEL_BC95_CellularPower(at); + return new QUECTEL_BC95_CellularInformation(at); } -AT_CellularSIM *QUECTEL_BC95::open_sim_impl(ATHandler &at) +nsapi_error_t QUECTEL_BC95::init() { - return new QUECTEL_BC95_CellularSIM(at); + _at->lock(); + _at->flush(); + _at->cmd_start("AT"); + _at->cmd_stop_read_resp(); + + _at->cmd_start("AT+CMEE="); // verbose responses + _at->write_int(1); + _at->cmd_stop_read_resp(); + + return _at->unlock_return_error(); } -AT_CellularContext *QUECTEL_BC95::create_context_impl(ATHandler &at, const char *apn) +#if MBED_CONF_QUECTEL_BC95_PROVIDE_DEFAULT +#include "UARTSerial.h" +CellularDevice *CellularDevice::get_default_instance() { - return new QUECTEL_BC95_CellularContext(at, this, apn); + static UARTSerial serial(MBED_CONF_QUECTEL_BC95_TX, MBED_CONF_QUECTEL_BC95_RX, MBED_CONF_QUECTEL_BC95_BAUDRATE); +#if defined (MBED_CONF_UBLOX_AT_RTS) && defined(MBED_CONF_UBLOX_AT_CTS) + tr_debug("QUECTEL_BC95 flow control: RTS %d CTS %d", MBED_CONF_QUECTEL_BC95_RTS, MBED_CONF_QUECTEL_BC95_CTS); + serial.set_flow_control(SerialBase::RTSCTS, MBED_CONF_QUECTEL_BC95_RTS, MBED_CONF_QUECTEL_BC95_CTS); +#endif + static QUECTEL_BC95 device(&serial); + return &device; } +#endif diff --git a/features/cellular/framework/targets/QUECTEL/BC95/QUECTEL_BC95.h b/features/cellular/framework/targets/QUECTEL/BC95/QUECTEL_BC95.h index 7dd9937f4ca..84a017ca6f5 100644 --- a/features/cellular/framework/targets/QUECTEL/BC95/QUECTEL_BC95.h +++ b/features/cellular/framework/targets/QUECTEL/BC95/QUECTEL_BC95.h @@ -18,6 +18,15 @@ #ifndef QUECTEL_BC95_H_ #define QUECTEL_BC95_H_ +#ifdef TARGET_FF_ARDUINO +#ifndef MBED_CONF_QUECTEL_BC95_TX +#define MBED_CONF_QUECTEL_BC95_TX D1 +#endif +#ifndef MBED_CONF_QUECTEL_BC95_RX +#define MBED_CONF_QUECTEL_BC95_RX D0 +#endif +#endif /* TARGET_FF_ARDUINO */ + #include "AT_CellularDevice.h" namespace mbed { @@ -25,13 +34,15 @@ namespace mbed { class QUECTEL_BC95 : public AT_CellularDevice { public: QUECTEL_BC95(FileHandle *fh); - virtual ~QUECTEL_BC95(); + +public: // AT_CellularDevice + virtual nsapi_error_t get_sim_state(SimState &state); protected: // AT_CellularDevice virtual AT_CellularNetwork *open_network_impl(ATHandler &at); - virtual AT_CellularPower *open_power_impl(ATHandler &at); - virtual AT_CellularSIM *open_sim_impl(ATHandler &at); - virtual AT_CellularContext *create_context_impl(ATHandler &at, const char *apn); + virtual AT_CellularContext *create_context_impl(ATHandler &at, const char *apn, bool cp_req = false, bool nonip_req = false); + virtual AT_CellularInformation *open_information_impl(ATHandler &at); + virtual nsapi_error_t init(); public: // NetworkInterface void handle_urc(FileHandle *fh); diff --git a/features/cellular/framework/targets/QUECTEL/BC95/QUECTEL_BC95_CellularContext.cpp b/features/cellular/framework/targets/QUECTEL/BC95/QUECTEL_BC95_CellularContext.cpp index f6d5e5a6efa..15d29f08e24 100644 --- a/features/cellular/framework/targets/QUECTEL/BC95/QUECTEL_BC95_CellularContext.cpp +++ b/features/cellular/framework/targets/QUECTEL/BC95/QUECTEL_BC95_CellularContext.cpp @@ -16,11 +16,12 @@ */ #include "QUECTEL_BC95_CellularContext.h" #include "QUECTEL_BC95_CellularStack.h" +#include "CellularLog.h" namespace mbed { -QUECTEL_BC95_CellularContext::QUECTEL_BC95_CellularContext(ATHandler &at, CellularDevice *device, const char *apn) : - AT_CellularContext(at, device, apn) +QUECTEL_BC95_CellularContext::QUECTEL_BC95_CellularContext(ATHandler &at, CellularDevice *device, const char *apn, bool cp_req, bool nonip_req) : + AT_CellularContext(at, device, apn, cp_req, nonip_req) { } @@ -30,15 +31,15 @@ QUECTEL_BC95_CellularContext::~QUECTEL_BC95_CellularContext() NetworkStack *QUECTEL_BC95_CellularContext::get_stack() { + if (_pdp_type == NON_IP_PDP_TYPE || _cp_in_use) { + tr_error("Requesting stack for NON-IP context! Should request control plane netif: get_cp_netif()"); + return NULL; + } + if (!_stack) { - _stack = new QUECTEL_BC95_CellularStack(_at, _cid, _ip_stack_type); + _stack = new QUECTEL_BC95_CellularStack(_at, _cid, (nsapi_ip_stack_t)_pdp_type); } return _stack; } -bool QUECTEL_BC95_CellularContext::stack_type_supported(nsapi_ip_stack_t stack_type) -{ - return stack_type == IPV4_STACK ? true : false; -} - } /* namespace mbed */ diff --git a/features/cellular/framework/targets/QUECTEL/BC95/QUECTEL_BC95_CellularContext.h b/features/cellular/framework/targets/QUECTEL/BC95/QUECTEL_BC95_CellularContext.h index e241275aeba..7c2bac86333 100644 --- a/features/cellular/framework/targets/QUECTEL/BC95/QUECTEL_BC95_CellularContext.h +++ b/features/cellular/framework/targets/QUECTEL/BC95/QUECTEL_BC95_CellularContext.h @@ -23,12 +23,11 @@ namespace mbed { class QUECTEL_BC95_CellularContext: public AT_CellularContext { public: - QUECTEL_BC95_CellularContext(ATHandler &at, CellularDevice *device, const char *apn); + QUECTEL_BC95_CellularContext(ATHandler &at, CellularDevice *device, const char *apn, bool cp_req = false, bool nonip_req = false); virtual ~QUECTEL_BC95_CellularContext(); protected: virtual NetworkStack *get_stack(); - virtual bool stack_type_supported(nsapi_ip_stack_t stack_type); }; } /* namespace mbed */ diff --git a/features/cellular/framework/targets/TELIT/HE910/TELIT_HE910_CellularContext.cpp b/features/cellular/framework/targets/QUECTEL/BC95/QUECTEL_BC95_CellularInformation.cpp similarity index 56% rename from features/cellular/framework/targets/TELIT/HE910/TELIT_HE910_CellularContext.cpp rename to features/cellular/framework/targets/QUECTEL/BC95/QUECTEL_BC95_CellularInformation.cpp index b9ec13eb060..8bc51c5aefc 100644 --- a/features/cellular/framework/targets/TELIT/HE910/TELIT_HE910_CellularContext.cpp +++ b/features/cellular/framework/targets/QUECTEL/BC95/QUECTEL_BC95_CellularInformation.cpp @@ -14,22 +14,28 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -#include "TELIT_HE910_CellularContext.h" +#include "QUECTEL_BC95_CellularInformation.h" namespace mbed { -TELIT_HE910_CellularContext::TELIT_HE910_CellularContext(ATHandler &at, CellularDevice *device, const char *apn) : - AT_CellularContext(at, device, apn) +QUECTEL_BC95_CellularInformation::QUECTEL_BC95_CellularInformation(ATHandler &at) : AT_CellularInformation(at) { } -TELIT_HE910_CellularContext::~TELIT_HE910_CellularContext() +QUECTEL_BC95_CellularInformation::~QUECTEL_BC95_CellularInformation() { } -bool TELIT_HE910_CellularContext::stack_type_supported(nsapi_ip_stack_t stack_type) +// According to BC95_AT_Commands_Manual_V1.9 +nsapi_error_t QUECTEL_BC95_CellularInformation::get_iccid(char *buf, size_t buf_size) { - return stack_type == IPV4_STACK ? true : false; + _at.lock(); + _at.cmd_start("AT+NCCID?"); + _at.cmd_stop(); + _at.resp_start("+NCCID:"); + _at.read_string(buf, buf_size); + _at.resp_stop(); + return _at.unlock_return_error(); } } /* namespace mbed */ diff --git a/features/cellular/framework/targets/QUECTEL/BG96/QUECTEL_BG96_CellularSIM.h b/features/cellular/framework/targets/QUECTEL/BC95/QUECTEL_BC95_CellularInformation.h similarity index 66% rename from features/cellular/framework/targets/QUECTEL/BG96/QUECTEL_BG96_CellularSIM.h rename to features/cellular/framework/targets/QUECTEL/BC95/QUECTEL_BC95_CellularInformation.h index efbcf5b3edc..833649eee9b 100644 --- a/features/cellular/framework/targets/QUECTEL/BG96/QUECTEL_BG96_CellularSIM.h +++ b/features/cellular/framework/targets/QUECTEL/BC95/QUECTEL_BC95_CellularInformation.h @@ -14,23 +14,22 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +#ifndef QUECTEL_BC95_CELLULARINFORMATION_H_ +#define QUECTEL_BC95_CELLULARINFORMATION_H_ -#ifndef QUECTEL_BG96_CELLULAR_SIM_H_ -#define QUECTEL_BG96_CELLULAR_SIM_H_ - -#include "AT_CellularSIM.h" +#include "AT_CellularInformation.h" namespace mbed { -class QUECTEL_BG96_CellularSIM : public AT_CellularSIM { +class QUECTEL_BC95_CellularInformation : public AT_CellularInformation { public: - QUECTEL_BG96_CellularSIM(ATHandler &atHandler); - virtual ~QUECTEL_BG96_CellularSIM(); + QUECTEL_BC95_CellularInformation(ATHandler &at); + virtual ~QUECTEL_BC95_CellularInformation(); -public: //from CellularSIM +public: virtual nsapi_error_t get_iccid(char *buf, size_t buf_size); }; -} // namespace mbed +} /* namespace mbed */ -#endif // QUECTEL_BG96_CELLULAR_SIM_H_ +#endif /* QUECTEL_BC95_CELLULARINFORMATION_H_ */ diff --git a/features/cellular/framework/targets/QUECTEL/BC95/QUECTEL_BC95_CellularNetwork.cpp b/features/cellular/framework/targets/QUECTEL/BC95/QUECTEL_BC95_CellularNetwork.cpp index cfceacc291a..440c7b519e6 100644 --- a/features/cellular/framework/targets/QUECTEL/BC95/QUECTEL_BC95_CellularNetwork.cpp +++ b/features/cellular/framework/targets/QUECTEL/BC95/QUECTEL_BC95_CellularNetwork.cpp @@ -28,11 +28,6 @@ QUECTEL_BC95_CellularNetwork::~QUECTEL_BC95_CellularNetwork() { } -AT_CellularNetwork::RegistrationMode QUECTEL_BC95_CellularNetwork::has_registration(RegistrationType reg_tech) -{ - return (reg_tech == C_EREG) ? RegistrationModeLAC : RegistrationModeDisable; -} - nsapi_error_t QUECTEL_BC95_CellularNetwork::set_access_technology_impl(RadioAccessTechnology opRat) { if (opRat != RAT_NB1) { diff --git a/features/cellular/framework/targets/QUECTEL/BC95/QUECTEL_BC95_CellularNetwork.h b/features/cellular/framework/targets/QUECTEL/BC95/QUECTEL_BC95_CellularNetwork.h index 30ab188d643..7058e2af5ba 100644 --- a/features/cellular/framework/targets/QUECTEL/BC95/QUECTEL_BC95_CellularNetwork.h +++ b/features/cellular/framework/targets/QUECTEL/BC95/QUECTEL_BC95_CellularNetwork.h @@ -29,7 +29,6 @@ class QUECTEL_BC95_CellularNetwork : public AT_CellularNetwork { protected: virtual nsapi_error_t set_access_technology_impl(RadioAccessTechnology opRat); - virtual RegistrationMode has_registration(RegistrationType reg_type); }; } // namespace mbed #endif // QUECTEL_BC95_CELLULAR_NETWORK_H_ diff --git a/features/cellular/framework/targets/QUECTEL/BC95/QUECTEL_BC95_CellularPower.cpp b/features/cellular/framework/targets/QUECTEL/BC95/QUECTEL_BC95_CellularPower.cpp deleted file mode 100644 index fc27d257e89..00000000000 --- a/features/cellular/framework/targets/QUECTEL/BC95/QUECTEL_BC95_CellularPower.cpp +++ /dev/null @@ -1,53 +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 "QUECTEL_BC95_CellularPower.h" - -using namespace mbed; - -QUECTEL_BC95_CellularPower::QUECTEL_BC95_CellularPower(ATHandler &atHandler) : AT_CellularPower(atHandler) -{ - -} - -QUECTEL_BC95_CellularPower::~QUECTEL_BC95_CellularPower() -{ - -} - -nsapi_error_t QUECTEL_BC95_CellularPower::set_at_mode() -{ - _at.lock(); - _at.flush(); - _at.cmd_start("AT"); - _at.cmd_stop_read_resp(); - - _at.cmd_start("AT+CMEE="); // verbose responses - _at.write_int(1); - _at.cmd_stop_read_resp(); - - return _at.unlock_return_error(); -} - -nsapi_error_t QUECTEL_BC95_CellularPower::reset() -{ - _at.lock(); - _at.cmd_start("AT+NRB"); // reset to full power levels - _at.cmd_stop(); - _at.resp_start("REBOOTING", true); - return _at.unlock_return_error(); -} diff --git a/features/cellular/framework/targets/QUECTEL/BC95/QUECTEL_BC95_CellularPower.h b/features/cellular/framework/targets/QUECTEL/BC95/QUECTEL_BC95_CellularPower.h deleted file mode 100644 index a10ab3e5d36..00000000000 --- a/features/cellular/framework/targets/QUECTEL/BC95/QUECTEL_BC95_CellularPower.h +++ /dev/null @@ -1,38 +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. - */ - -#ifndef QUECTEL_BC95_CELLULAR_POWER_H_ -#define QUECTEL_BC95_CELLULAR_POWER_H_ - -#include "AT_CellularPower.h" - -namespace mbed { - -class QUECTEL_BC95_CellularPower : public AT_CellularPower { -public: - QUECTEL_BC95_CellularPower(ATHandler &atHandler); - virtual ~QUECTEL_BC95_CellularPower(); - -public: //from CellularPower - virtual nsapi_error_t set_at_mode(); - - virtual nsapi_error_t reset(); -}; - -} // namespace mbed - -#endif // QUECTEL_BC95_CELLULAR_POWER_H_ diff --git a/features/cellular/framework/targets/QUECTEL/BC95/QUECTEL_BC95_CellularSIM.cpp b/features/cellular/framework/targets/QUECTEL/BC95/QUECTEL_BC95_CellularSIM.cpp deleted file mode 100644 index 6da7e5f554c..00000000000 --- a/features/cellular/framework/targets/QUECTEL/BC95/QUECTEL_BC95_CellularSIM.cpp +++ /dev/null @@ -1,70 +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 "QUECTEL_BC95_CellularSIM.h" -#include "CellularLog.h" - -using namespace mbed; - -QUECTEL_BC95_CellularSIM::QUECTEL_BC95_CellularSIM(ATHandler &atHandler) : AT_CellularSIM(atHandler) -{ - -} - -QUECTEL_BC95_CellularSIM::~QUECTEL_BC95_CellularSIM() -{ - -} - -nsapi_error_t QUECTEL_BC95_CellularSIM::get_sim_state(SimState &state) -{ - _at.lock(); - _at.flush(); - _at.cmd_start("AT+NCCID?"); - _at.cmd_stop(); - _at.resp_start("+NCCID:"); - if (_at.info_resp()) { - state = SimStateReady; - } else { - tr_warn("SIM not readable."); - state = SimStateUnknown; // SIM may not be ready yet - } - _at.resp_stop(); - return _at.unlock_return_error(); -} - -// According to BC95_AT_Commands_Manual_V1.9 -nsapi_error_t QUECTEL_BC95_CellularSIM::get_iccid(char *buf, size_t buf_size) -{ - _at.lock(); - _at.cmd_start("AT+NCCID?"); - _at.cmd_stop(); - _at.resp_start("+NCCID:"); - _at.read_string(buf, buf_size); - _at.resp_stop(); - return _at.unlock_return_error(); -} - -nsapi_error_t QUECTEL_BC95_CellularSIM::change_pin(const char *sim_pin, const char *new_pin) -{ - return NSAPI_ERROR_UNSUPPORTED; -} - -nsapi_error_t QUECTEL_BC95_CellularSIM::set_pin_query(const char *sim_pin, bool query_pin) -{ - return NSAPI_ERROR_UNSUPPORTED; -} diff --git a/features/cellular/framework/targets/QUECTEL/BC95/QUECTEL_BC95_CellularSIM.h b/features/cellular/framework/targets/QUECTEL/BC95/QUECTEL_BC95_CellularSIM.h deleted file mode 100644 index 498be768172..00000000000 --- a/features/cellular/framework/targets/QUECTEL/BC95/QUECTEL_BC95_CellularSIM.h +++ /dev/null @@ -1,39 +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. - */ - -#ifndef QUECTEL_BC95_CELLULAR_SIM_H_ -#define QUECTEL_BC95_CELLULAR_SIM_H_ - -#include "AT_CellularSIM.h" - -namespace mbed { - -class QUECTEL_BC95_CellularSIM : public AT_CellularSIM { -public: - QUECTEL_BC95_CellularSIM(ATHandler &atHandler); - virtual ~QUECTEL_BC95_CellularSIM(); - -public: //from CellularSIM - virtual nsapi_error_t get_sim_state(SimState &state); - virtual nsapi_error_t get_iccid(char *buf, size_t buf_size); - virtual nsapi_error_t change_pin(const char *sim_pin, const char *new_pin); - virtual nsapi_error_t set_pin_query(const char *sim_pin, bool query_pin); -}; - -} // namespace mbed - -#endif // QUECTEL_BC95_CELLULAR_SIM_H_ diff --git a/features/cellular/framework/targets/QUECTEL/BC95/mbed_lib.json b/features/cellular/framework/targets/QUECTEL/BC95/mbed_lib.json new file mode 100644 index 00000000000..d435e5e0850 --- /dev/null +++ b/features/cellular/framework/targets/QUECTEL/BC95/mbed_lib.json @@ -0,0 +1,29 @@ +{ + "name": "QUECTEL_BC95", + "config": { + "tx": { + "help": "TX pin for serial connection. D1 assumed if Arduino Form Factor, needs to be set/overwritten otherwise.", + "value": null + }, + "rx": { + "help": "RX pin for serial connection. D0 assumed if Arduino Form Factor, needs to be set/overwritten otherwise.", + "value": null + }, + "rts": { + "help": "RTS pin for serial connection", + "value": null + }, + "cts": { + "help": "CTS pin for serial connection", + "value": null + }, + "baudrate" : { + "help": "Serial connection baud rate", + "value": 9600 + }, + "provide-default": { + "help": "Provide as default CellularDevice [true/false]", + "value": false + } + } +} diff --git a/features/cellular/framework/targets/QUECTEL/BG96/QUECTEL_BG96.cpp b/features/cellular/framework/targets/QUECTEL/BG96/QUECTEL_BG96.cpp index c63feb28885..84ca203e2c9 100644 --- a/features/cellular/framework/targets/QUECTEL/BG96/QUECTEL_BG96.cpp +++ b/features/cellular/framework/targets/QUECTEL/BG96/QUECTEL_BG96.cpp @@ -18,8 +18,7 @@ #include "QUECTEL_BG96.h" #include "QUECTEL_BG96_CellularNetwork.h" #include "QUECTEL_BG96_CellularStack.h" -#include "QUECTEL_BG96_CellularSIM.h" -#include "QUECTEL_BG96_CellularPower.h" +#include "QUECTEL_BG96_CellularInformation.h" #include "QUECTEL_BG96_CellularContext.h" using namespace mbed; @@ -29,21 +28,24 @@ using namespace events; #define CONNECT_BUFFER_SIZE (1280 + 80 + 80) // AT response + sscanf format #define CONNECT_TIMEOUT 8000 -#define MAX_STARTUP_TRIALS 5 -#define MAX_RESET_TRIALS 5 +#define DEVICE_READY_URC "CPIN:" -static const AT_CellularBase::SupportedFeature unsupported_features[] = { - AT_CellularBase::AT_CGSN_WITH_TYPE, - AT_CellularBase::SUPPORTED_FEATURE_END_MARK +static const intptr_t cellular_properties[AT_CellularBase::PROPERTY_MAX] = { + AT_CellularNetwork::RegistrationModeLAC, // C_EREG + AT_CellularNetwork::RegistrationModeLAC, // C_GREG + AT_CellularNetwork::RegistrationModeLAC, // C_REG + 0, // AT_CGSN_WITH_TYPE + 0, // AT_CGDATA + 1, // AT_CGAUTH + 1, // PROPERTY_IPV4_STACK + 0, // PROPERTY_IPV6_STACK + 0, // PROPERTY_IPV4V6_STACK + 1, // PROPERTY_NON_IP_PDP_TYPE }; QUECTEL_BG96::QUECTEL_BG96(FileHandle *fh) : AT_CellularDevice(fh) { - AT_CellularBase::set_unsupported_features(unsupported_features); -} - -QUECTEL_BG96::~QUECTEL_BG96() -{ + AT_CellularBase::set_cellular_properties(cellular_properties); } AT_CellularNetwork *QUECTEL_BG96::open_network_impl(ATHandler &at) @@ -51,18 +53,31 @@ AT_CellularNetwork *QUECTEL_BG96::open_network_impl(ATHandler &at) return new QUECTEL_BG96_CellularNetwork(at); } -AT_CellularSIM *QUECTEL_BG96::open_sim_impl(ATHandler &at) +AT_CellularContext *QUECTEL_BG96::create_context_impl(ATHandler &at, const char *apn, bool cp_req, bool nonip_req) { - return new QUECTEL_BG96_CellularSIM(at); + return new QUECTEL_BG96_CellularContext(at, this, apn, cp_req, nonip_req); } -AT_CellularPower *QUECTEL_BG96::open_power_impl(ATHandler &at) +AT_CellularInformation *QUECTEL_BG96::open_information_impl(ATHandler &at) { - return new QUECTEL_BG96_CellularPower(at); + return new QUECTEL_BG96_CellularInformation(at); } -AT_CellularContext *QUECTEL_BG96::create_context_impl(ATHandler &at, const char *apn) +void QUECTEL_BG96::set_ready_cb(Callback callback) { - return new QUECTEL_BG96_CellularContext(at, this, apn); + _at->set_urc_handler(DEVICE_READY_URC, callback); } +#if MBED_CONF_QUECTEL_BG96_PROVIDE_DEFAULT +#include "UARTSerial.h" +CellularDevice *CellularDevice::get_default_instance() +{ + static UARTSerial serial(MBED_CONF_QUECTEL_BG96_TX, MBED_CONF_QUECTEL_BG96_RX, MBED_CONF_QUECTEL_BG96_BAUDRATE); +#if defined (MBED_CONF_UBLOX_AT_RTS) && defined(MBED_CONF_UBLOX_AT_CTS) + tr_debug("QUECTEL_BG96 flow control: RTS %d CTS %d", MBED_CONF_QUECTEL_BG96_RTS, MBED_CONF_QUECTEL_BG96_CTS); + serial.set_flow_control(SerialBase::RTSCTS, MBED_CONF_QUECTEL_BG96_RTS, MBED_CONF_QUECTEL_BG96_CTS); +#endif + static QUECTEL_BG96 device(&serial); + return &device; +} +#endif diff --git a/features/cellular/framework/targets/QUECTEL/BG96/QUECTEL_BG96.h b/features/cellular/framework/targets/QUECTEL/BG96/QUECTEL_BG96.h index 86eb63bd4cf..324de491c81 100644 --- a/features/cellular/framework/targets/QUECTEL/BG96/QUECTEL_BG96.h +++ b/features/cellular/framework/targets/QUECTEL/BG96/QUECTEL_BG96.h @@ -18,6 +18,15 @@ #ifndef QUECTEL_BG96_H_ #define QUECTEL_BG96_H_ +#ifdef TARGET_FF_ARDUINO +#ifndef MBED_CONF_QUECTEL_BG96_TX +#define MBED_CONF_QUECTEL_BG96_TX D1 +#endif +#ifndef MBED_CONF_QUECTEL_BG96_RX +#define MBED_CONF_QUECTEL_BG96_RX D0 +#endif +#endif /* TARGET_FF_ARDUINO */ + #include "AT_CellularDevice.h" namespace mbed { @@ -25,13 +34,13 @@ namespace mbed { class QUECTEL_BG96 : public AT_CellularDevice { public: QUECTEL_BG96(FileHandle *fh); - virtual ~QUECTEL_BG96(); protected: // AT_CellularDevice virtual AT_CellularNetwork *open_network_impl(ATHandler &at); - virtual AT_CellularSIM *open_sim_impl(ATHandler &at); - virtual AT_CellularPower *open_power_impl(ATHandler &at); - virtual AT_CellularContext *create_context_impl(ATHandler &at, const char *apn); + virtual AT_CellularContext *create_context_impl(ATHandler &at, const char *apn, bool cp_req = false, bool nonip_req = false); + virtual AT_CellularInformation *open_information_impl(ATHandler &at); + virtual void set_ready_cb(Callback callback); + public: void handle_urc(FileHandle *fh); }; diff --git a/features/cellular/framework/targets/QUECTEL/BG96/QUECTEL_BG96_CellularContext.cpp b/features/cellular/framework/targets/QUECTEL/BG96/QUECTEL_BG96_CellularContext.cpp index 295b0d3af9b..599f3b0a2fa 100644 --- a/features/cellular/framework/targets/QUECTEL/BG96/QUECTEL_BG96_CellularContext.cpp +++ b/features/cellular/framework/targets/QUECTEL/BG96/QUECTEL_BG96_CellularContext.cpp @@ -16,34 +16,55 @@ */ #include "QUECTEL_BG96_CellularContext.h" #include "QUECTEL_BG96_CellularStack.h" +#include "QUECTEL_BG96_ControlPlane_netif.h" +#include "CellularLog.h" + +#include "Semaphore.h" namespace mbed { -QUECTEL_BG96_CellularContext::QUECTEL_BG96_CellularContext(ATHandler &at, CellularDevice *device, const char *apn) : - AT_CellularContext(at, device, apn) -{ -} +// Non-IP context supported only for context id 1 +#define NIDD_PDP_CONTEXT_ID 1 +// Timeout to wait for URC indicating NIDD connection opening +#define NIDD_OPEN_URC_TIMEOUT 3000 -QUECTEL_BG96_CellularContext::~QUECTEL_BG96_CellularContext() +QUECTEL_BG96_CellularContext::QUECTEL_BG96_CellularContext(ATHandler &at, CellularDevice *device, const char *apn, bool cp_req, bool nonip_req) : + AT_CellularContext(at, device, apn, cp_req, nonip_req) { + if (_nonip_req) { + _at.set_urc_handler("+QIND:", mbed::Callback(this, &QUECTEL_BG96_CellularContext::urc_nidd)); + } } -bool QUECTEL_BG96_CellularContext::stack_type_supported(nsapi_ip_stack_t stack_type) +QUECTEL_BG96_CellularContext::~QUECTEL_BG96_CellularContext() { - if (stack_type == IPV4_STACK) { - return true; + if (_nonip_req) { + _at.set_urc_handler("+QIND:", 0); } - return false; } NetworkStack *QUECTEL_BG96_CellularContext::get_stack() { + if (_pdp_type == NON_IP_PDP_TYPE || (_nonip_req && _pdp_type != DEFAULT_PDP_TYPE)) { + tr_error("Requesting stack for NON-IP context! Should request control plane netif: get_cp_netif()"); + return NULL; + } + if (!_stack) { - _stack = new QUECTEL_BG96_CellularStack(_at, _cid, _ip_stack_type); + _stack = new QUECTEL_BG96_CellularStack(_at, _cid, (nsapi_ip_stack_t)_pdp_type); } + return _stack; } +ControlPlane_netif *QUECTEL_BG96_CellularContext::get_cp_netif() +{ + if (!_cp_netif) { + _cp_netif = new QUECTEL_BG96_ControlPlane_netif(_at, _cid); + } + return _cp_netif; +} + nsapi_error_t QUECTEL_BG96_CellularContext::do_user_authentication() { if (_pwd && _uname) { @@ -65,4 +86,110 @@ nsapi_error_t QUECTEL_BG96_CellularContext::do_user_authentication() return NSAPI_ERROR_OK; } +nsapi_error_t QUECTEL_BG96_CellularContext::activate_non_ip_context() +{ + _at.lock(); + + // Open the NIDD connection + _at.cmd_start("AT+QCFGEXT=\"nipd\",1"); + _at.cmd_stop(); + _at.resp_start(); + _at.resp_stop(); + + nsapi_size_or_error_t ret = _at.get_last_error(); + + _at.unlock(); + + if (ret == NSAPI_ERROR_OK) { + _semaphore.wait(NIDD_OPEN_URC_TIMEOUT); + if (_cid == -1) { + return NSAPI_ERROR_NO_CONNECTION; + } + } + + return (ret == NSAPI_ERROR_OK) ? NSAPI_ERROR_OK : NSAPI_ERROR_NO_CONNECTION; +} + +void QUECTEL_BG96_CellularContext::deactivate_non_ip_context() +{ + // Close the NIDD connection + _at.cmd_start("AT+QCFGEXT=\"nipd\",0"); + _at.cmd_stop(); + _at.resp_start(); + _at.resp_stop(); + +} + +void QUECTEL_BG96_CellularContext::urc_nidd() +{ + char nipd_string[6]; + + // skip "nipd" + _at.skip_param(); + _at.read_string(nipd_string, sizeof(nipd_string)); + + if (!strcmp(nipd_string, "recv")) { + _cp_netif->data_received(); + } else if (!strcmp(nipd_string, "open")) { + urc_nidd_open(); + } else if (!strcmp(nipd_string, "close")) { + urc_nidd_close(); + } +} + +void QUECTEL_BG96_CellularContext::urc_nidd_open() +{ + int err = _at.read_int(); + if (!err) { + _is_context_active = true; + _is_context_activated = true; + _cid = NIDD_PDP_CONTEXT_ID; + } else { + tr_error("NIDD connection open failed with error: %d", err); + } + _semaphore.release(); +} + +void QUECTEL_BG96_CellularContext::urc_nidd_close() +{ + set_disconnect(); +} + +nsapi_error_t QUECTEL_BG96_CellularContext::setup_control_plane_opt() +{ + _at.lock(); + + _at.cmd_start("AT+QCFGEXT=\"pdp_type\","); + _at.write_int(NIDD_PDP_CONTEXT_ID); + _at.write_string("Non-IP"); + _at.write_string(_apn); + _at.cmd_stop(); + _at.resp_start(); + _at.resp_stop(); + _at.cmd_start("AT+CFUN=0"); + _at.cmd_stop(); + _at.resp_start(); + _at.resp_stop(); + _at.cmd_start("AT+CFUN=1"); + _at.cmd_stop(); + _at.resp_start(); + _at.resp_stop(); + + // Configure Non-IP outgoing data type - 0 for no exception data + _at.cmd_start("AT+QCFGEXT=\"nipdcfg\",0,"); + _at.write_string(_apn); + _at.cmd_stop(); + _at.resp_start(); + _at.resp_stop(); + + if (_at.get_last_error() == NSAPI_ERROR_OK) { + _cp_in_use = true; + if (_nonip_req) { + _pdp_type = NON_IP_PDP_TYPE; + } + } + + return _at.unlock_return_error(); +} + } /* namespace mbed */ diff --git a/features/cellular/framework/targets/QUECTEL/BG96/QUECTEL_BG96_CellularContext.h b/features/cellular/framework/targets/QUECTEL/BG96/QUECTEL_BG96_CellularContext.h index 363aaa095e6..bf1e54a90c8 100644 --- a/features/cellular/framework/targets/QUECTEL/BG96/QUECTEL_BG96_CellularContext.h +++ b/features/cellular/framework/targets/QUECTEL/BG96/QUECTEL_BG96_CellularContext.h @@ -23,13 +23,22 @@ namespace mbed { class QUECTEL_BG96_CellularContext: public AT_CellularContext { public: - QUECTEL_BG96_CellularContext(ATHandler &at, CellularDevice *device, const char *apn); + QUECTEL_BG96_CellularContext(ATHandler &at, CellularDevice *device, const char *apn, bool cp_req = false, bool nonip_req = false); virtual ~QUECTEL_BG96_CellularContext(); protected: - virtual bool stack_type_supported(nsapi_ip_stack_t stack_type); virtual NetworkStack *get_stack(); + virtual ControlPlane_netif *get_cp_netif(); virtual nsapi_error_t do_user_authentication(); + virtual nsapi_error_t activate_non_ip_context(); + virtual nsapi_error_t setup_control_plane_opt(); + virtual void deactivate_non_ip_context(); + rtos::Semaphore _semaphore; + +private: + void urc_nidd(); + void urc_nidd_open(); + void urc_nidd_close(); }; } /* namespace mbed */ diff --git a/features/cellular/framework/targets/QUECTEL/BG96/QUECTEL_BG96_CellularSIM.cpp b/features/cellular/framework/targets/QUECTEL/BG96/QUECTEL_BG96_CellularInformation.cpp similarity index 72% rename from features/cellular/framework/targets/QUECTEL/BG96/QUECTEL_BG96_CellularSIM.cpp rename to features/cellular/framework/targets/QUECTEL/BG96/QUECTEL_BG96_CellularInformation.cpp index 80cae03db59..c8f4319ce61 100644 --- a/features/cellular/framework/targets/QUECTEL/BG96/QUECTEL_BG96_CellularSIM.cpp +++ b/features/cellular/framework/targets/QUECTEL/BG96/QUECTEL_BG96_CellularInformation.cpp @@ -14,24 +14,20 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +#include "QUECTEL_BG96_CellularInformation.h" -#include "QUECTEL_BG96_CellularSIM.h" -#include "CellularLog.h" +namespace mbed { -using namespace mbed; - -QUECTEL_BG96_CellularSIM::QUECTEL_BG96_CellularSIM(ATHandler &atHandler) : AT_CellularSIM(atHandler) +QUECTEL_BG96_CellularInformation::QUECTEL_BG96_CellularInformation(ATHandler &at) : AT_CellularInformation(at) { - } -QUECTEL_BG96_CellularSIM::~QUECTEL_BG96_CellularSIM() +QUECTEL_BG96_CellularInformation::~QUECTEL_BG96_CellularInformation() { - } // According to BG96_AT_Commands_Manual_V2.0 -nsapi_error_t QUECTEL_BG96_CellularSIM::get_iccid(char *buf, size_t buf_size) +nsapi_error_t QUECTEL_BG96_CellularInformation::get_iccid(char *buf, size_t buf_size) { _at.lock(); _at.cmd_start("AT+QCCID"); @@ -41,3 +37,5 @@ nsapi_error_t QUECTEL_BG96_CellularSIM::get_iccid(char *buf, size_t buf_size) _at.resp_stop(); return _at.unlock_return_error(); } + +} /* namespace mbed */ diff --git a/features/cellular/framework/targets/TELIT/HE910/TELIT_HE910_CellularContext.h b/features/cellular/framework/targets/QUECTEL/BG96/QUECTEL_BG96_CellularInformation.h similarity index 61% rename from features/cellular/framework/targets/TELIT/HE910/TELIT_HE910_CellularContext.h rename to features/cellular/framework/targets/QUECTEL/BG96/QUECTEL_BG96_CellularInformation.h index 774fada8c93..c952eac33e6 100644 --- a/features/cellular/framework/targets/TELIT/HE910/TELIT_HE910_CellularContext.h +++ b/features/cellular/framework/targets/QUECTEL/BG96/QUECTEL_BG96_CellularInformation.h @@ -14,22 +14,22 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -#ifndef TELIT_HE910_CELLULARCONTEXT_H_ -#define TELIT_HE910_CELLULARCONTEXT_H_ +#ifndef QUECTEL_BG96_CELLULARINFORMATION_H_ +#define QUECTEL_BG96_CELLULARINFORMATION_H_ -#include "AT_CellularContext.h" +#include "AT_CellularInformation.h" namespace mbed { -class TELIT_HE910_CellularContext: public AT_CellularContext { +class QUECTEL_BG96_CellularInformation: public AT_CellularInformation { public: - TELIT_HE910_CellularContext(ATHandler &at, CellularDevice *device, const char *apn); - virtual ~TELIT_HE910_CellularContext(); + QUECTEL_BG96_CellularInformation(ATHandler &at); + virtual ~QUECTEL_BG96_CellularInformation(); -protected: - virtual bool stack_type_supported(nsapi_ip_stack_t stack_type); +public: // AT_CellularInformation + virtual nsapi_error_t get_iccid(char *buf, size_t buf_size); }; } /* namespace mbed */ -#endif // TELIT_HE910_CELLULARCONTEXT_H_ +#endif /* QUECTEL_BG96_CELLULARINFORMATION_H_ */ diff --git a/features/cellular/framework/targets/QUECTEL/BG96/QUECTEL_BG96_CellularPower.cpp b/features/cellular/framework/targets/QUECTEL/BG96/QUECTEL_BG96_CellularPower.cpp deleted file mode 100644 index f6e35196a05..00000000000 --- a/features/cellular/framework/targets/QUECTEL/BG96/QUECTEL_BG96_CellularPower.cpp +++ /dev/null @@ -1,36 +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 "QUECTEL_BG96_CellularPower.h" - -#define DEVICE_READY_URC "CPIN:" - -using namespace mbed; - -QUECTEL_BG96_CellularPower::QUECTEL_BG96_CellularPower(ATHandler &atHandler) : AT_CellularPower(atHandler) -{ -} - -nsapi_error_t QUECTEL_BG96_CellularPower::set_device_ready_urc_cb(mbed::Callback callback) -{ - return _at.set_urc_handler(DEVICE_READY_URC, callback); -} - -void QUECTEL_BG96_CellularPower::remove_device_ready_urc_cb(mbed::Callback callback) -{ - _at.remove_urc_handler(DEVICE_READY_URC); -} diff --git a/features/cellular/framework/targets/QUECTEL/BG96/QUECTEL_BG96_CellularPower.h b/features/cellular/framework/targets/QUECTEL/BG96/QUECTEL_BG96_CellularPower.h deleted file mode 100644 index ec7368cbfe5..00000000000 --- a/features/cellular/framework/targets/QUECTEL/BG96/QUECTEL_BG96_CellularPower.h +++ /dev/null @@ -1,36 +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. - */ - -#ifndef QUECTEL_BG96_CELLULAR_POWER_H_ -#define QUECTEL_BG96_CELLULAR_POWER_H_ - -#include "AT_CellularPower.h" - -namespace mbed { - -class QUECTEL_BG96_CellularPower : public AT_CellularPower { -public: - QUECTEL_BG96_CellularPower(ATHandler &atHandler); - -public: //from CellularPower - virtual nsapi_error_t set_device_ready_urc_cb(mbed::Callback callback); - virtual void remove_device_ready_urc_cb(mbed::Callback callback); -}; - -} // namespace mbed - -#endif // QUECTEL_BG96_CELLULAR_POWER_H_ diff --git a/features/cellular/framework/targets/QUECTEL/BG96/QUECTEL_BG96_ControlPlane_netif.cpp b/features/cellular/framework/targets/QUECTEL/BG96/QUECTEL_BG96_ControlPlane_netif.cpp new file mode 100644 index 00000000000..5d2036c76a1 --- /dev/null +++ b/features/cellular/framework/targets/QUECTEL/BG96/QUECTEL_BG96_ControlPlane_netif.cpp @@ -0,0 +1,68 @@ +#include "QUECTEL_BG96_ControlPlane_netif.h" + +namespace mbed { + +QUECTEL_BG96_ControlPlane_netif::QUECTEL_BG96_ControlPlane_netif(ATHandler &at, int cid) : AT_ControlPlane_netif(at, cid) +{} + +nsapi_size_or_error_t QUECTEL_BG96_ControlPlane_netif::send(const void *data, nsapi_size_t size) +{ + _at.lock(); + + _at.cmd_start("AT+QCFGEXT=\"nipds\",0,"); + _at.write_string((char *)data); + _at.write_int(size); + _at.cmd_stop(); + _at.resp_start(); + _at.resp_stop(); + + nsapi_error_t err = _at.get_last_error(); + + _at.unlock(); + + if (err == NSAPI_ERROR_OK) { + return size; + } + + return err; +} + +nsapi_size_or_error_t QUECTEL_BG96_ControlPlane_netif::recv(void *buffer, nsapi_size_t size) +{ + _at.lock(); + + _at.cmd_start("AT+QCFGEXT=\"nipdr\",0"); + _at.cmd_stop(); + _at.resp_start("+QCFGEXT: "); + // skip 3 params: "nipdr",, + _at.skip_param(3); + // get to + int unread_length = _at.read_int(); + _at.resp_stop(); + + if (!unread_length || unread_length == -1) { + _at.unlock(); + return NSAPI_ERROR_WOULD_BLOCK; + } + + _at.cmd_start("AT+QCFGEXT=\"nipdr\","); + _at.write_int(unread_length); + _at.cmd_stop(); + + _at.resp_start("+QCFGEXT:"); + // skip "nipdr" + _at.skip_param(); + int read_length = _at.read_int(); + _at.read_string((char *)buffer, read_length); + _at.resp_stop(); + nsapi_error_t err = _at.get_last_error(); + _at.unlock(); + + if (err == NSAPI_ERROR_OK && read_length) { + return read_length; + } + + return NSAPI_ERROR_WOULD_BLOCK; +} + +} // mbed namespace diff --git a/features/cellular/framework/targets/UBLOX/PPP/UBLOX_PPP_CellularContext.h b/features/cellular/framework/targets/QUECTEL/BG96/QUECTEL_BG96_ControlPlane_netif.h similarity index 58% rename from features/cellular/framework/targets/UBLOX/PPP/UBLOX_PPP_CellularContext.h rename to features/cellular/framework/targets/QUECTEL/BG96/QUECTEL_BG96_ControlPlane_netif.h index 37ac1bb8ee3..917a7428087 100644 --- a/features/cellular/framework/targets/UBLOX/PPP/UBLOX_PPP_CellularContext.h +++ b/features/cellular/framework/targets/QUECTEL/BG96/QUECTEL_BG96_ControlPlane_netif.h @@ -14,22 +14,23 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -#ifndef UBLOX_PPP_CELLULARCONTEXT_H_ -#define UBLOX_PPP_CELLULARCONTEXT_H_ +#ifndef QUECTEL_BG96_CONTROLPLANE_NETIF_H_ +#define QUECTEL_BG96_CONTROLPLANE_NETIF_H_ -#include "AT_CellularContext.h" +#include "AT_ControlPlane_netif.h" namespace mbed { -class UBLOX_PPP_CellularContext: public AT_CellularContext { +class QUECTEL_BG96_ControlPlane_netif: public AT_ControlPlane_netif { public: - UBLOX_PPP_CellularContext(ATHandler &at, CellularDevice *device, const char *apn); - virtual ~UBLOX_PPP_CellularContext(); + QUECTEL_BG96_ControlPlane_netif(ATHandler &at, int cid); + virtual ~QUECTEL_BG96_ControlPlane_netif() {}; -protected: - virtual bool stack_type_supported(nsapi_ip_stack_t stack_type); +// ControlPlane_netif + nsapi_size_or_error_t send(const void *data, nsapi_size_t size); + nsapi_size_or_error_t recv(void *buffer, nsapi_size_t size); }; } /* namespace mbed */ -#endif // UBLOX_PPP_CELLULARCONTEXT_H_ +#endif // QUECTEL_BG96_CONTROLPLANE_NETIF_H_ diff --git a/features/cellular/framework/targets/QUECTEL/BG96/mbed_lib.json b/features/cellular/framework/targets/QUECTEL/BG96/mbed_lib.json new file mode 100644 index 00000000000..9ad92a2ee6c --- /dev/null +++ b/features/cellular/framework/targets/QUECTEL/BG96/mbed_lib.json @@ -0,0 +1,29 @@ +{ + "name": "QUECTEL_BG96", + "config": { + "tx": { + "help": "TX pin for serial connection. D1 assumed if Arduino Form Factor, needs to be set/overwritten otherwise.", + "value": null + }, + "rx": { + "help": "RX pin for serial connection. D0 assumed if Arduino Form Factor, needs to be set/overwritten otherwise.", + "value": null + }, + "rts": { + "help": "RTS pin for serial connection", + "value": null + }, + "cts": { + "help": "CTS pin for serial connection", + "value": null + }, + "baudrate" : { + "help": "Serial connection baud rate", + "value": 115200 + }, + "provide-default": { + "help": "Provide as default CellularDevice [true/false]", + "value": false + } + } +} diff --git a/features/cellular/framework/targets/QUECTEL/UG96/QUECTEL_UG96.cpp b/features/cellular/framework/targets/QUECTEL/UG96/QUECTEL_UG96.cpp index 4e69e0a8ede..695ce9f5025 100644 --- a/features/cellular/framework/targets/QUECTEL/UG96/QUECTEL_UG96.cpp +++ b/features/cellular/framework/targets/QUECTEL/UG96/QUECTEL_UG96.cpp @@ -15,10 +15,10 @@ * limitations under the License. */ +#include "onboard_modem_api.h" #include "QUECTEL_UG96.h" -#include "QUECTEL_UG96_CellularNetwork.h" -#include "QUECTEL_UG96_CellularPower.h" #include "QUECTEL_UG96_CellularContext.h" +#include "AT_CellularNetwork.h" using namespace mbed; using namespace events; @@ -27,28 +27,39 @@ using namespace events; #define CONNECT_BUFFER_SIZE (1280 + 80 + 80) // AT response + sscanf format #define CONNECT_TIMEOUT 8000 -#define MAX_STARTUP_TRIALS 5 -#define MAX_RESET_TRIALS 5 +static const intptr_t cellular_properties[AT_CellularBase::PROPERTY_MAX] = { + AT_CellularNetwork::RegistrationModeDisable,// C_EREG + AT_CellularNetwork::RegistrationModeLAC, // C_GREG + AT_CellularNetwork::RegistrationModeLAC, // C_REG + 1, // AT_CGSN_WITH_TYPE + 1, // AT_CGDATA + 1, // AT_CGAUTH + 1, // PROPERTY_IPV4_STACK + 0, // PROPERTY_IPV6_STACK + 0, // PROPERTY_IPV4V6_STACK +}; QUECTEL_UG96::QUECTEL_UG96(FileHandle *fh) : AT_CellularDevice(fh) { + AT_CellularBase::set_cellular_properties(cellular_properties); } -QUECTEL_UG96::~QUECTEL_UG96() +AT_CellularContext *QUECTEL_UG96::create_context_impl(ATHandler &at, const char *apn, bool cp_req, bool nonip_req) { + return new QUECTEL_UG96_CellularContext(at, this, apn, cp_req, nonip_req); } -AT_CellularNetwork *QUECTEL_UG96::open_network_impl(ATHandler &at) +#if MBED_CONF_QUECTEL_UG96_PROVIDE_DEFAULT +#include "UARTSerial.h" +CellularDevice *CellularDevice::get_default_instance() { - return new QUECTEL_UG96_CellularNetwork(at); + static UARTSerial serial(MBED_CONF_QUECTEL_UG96_TX, MBED_CONF_QUECTEL_UG96_RX, MBED_CONF_QUECTEL_UG96_BAUDRATE); +#if defined (MBED_CONF_QUECTEL_UG96_RTS) && defined (MBED_CONF_QUECTEL_UG96_CTS) + tr_debug("QUECTEL_UG96 flow control: RTS %d CTS %d", MBED_CONF_QUECTEL_UG96_RTS, MBED_CONF_QUECTEL_UG96_CTS); + serial.set_flow_control(SerialBase::RTSCTS, MBED_CONF_QUECTEL_UG96_RTS, MBED_CONF_QUECTEL_UG96_CTS); +#endif + static QUECTEL_UG96 device(&serial); + return &device; } +#endif -AT_CellularPower *QUECTEL_UG96::open_power_impl(ATHandler &at) -{ - return new QUECTEL_UG96_CellularPower(at); -} - -AT_CellularContext *QUECTEL_UG96::create_context_impl(ATHandler &at, const char *apn) -{ - return new QUECTEL_UG96_CellularContext(at, this, apn); -} diff --git a/features/cellular/framework/targets/QUECTEL/UG96/QUECTEL_UG96.h b/features/cellular/framework/targets/QUECTEL/UG96/QUECTEL_UG96.h index 84ad9a2bfa2..fbd47a6e348 100644 --- a/features/cellular/framework/targets/QUECTEL/UG96/QUECTEL_UG96.h +++ b/features/cellular/framework/targets/QUECTEL/UG96/QUECTEL_UG96.h @@ -18,6 +18,15 @@ #ifndef QUECTEL_UG96_H_ #define QUECTEL_UG96_H_ +#ifdef TARGET_FF_ARDUINO +#ifndef MBED_CONF_QUECTEL_UG96_TX +#define MBED_CONF_QUECTEL_UG96_TX D1 +#endif +#ifndef MBED_CONF_QUECTEL_UG96_RX +#define MBED_CONF_QUECTEL_UG96_RX D0 +#endif +#endif /* TARGET_FF_ARDUINO */ + #include "AT_CellularDevice.h" namespace mbed { @@ -33,16 +42,12 @@ namespace mbed { class QUECTEL_UG96 : public AT_CellularDevice { public: QUECTEL_UG96(FileHandle *fh); - virtual ~QUECTEL_UG96(); protected: // AT_CellularDevice - virtual AT_CellularNetwork *open_network_impl(ATHandler &at); - virtual AT_CellularPower *open_power_impl(ATHandler &at); - virtual AT_CellularContext *create_context_impl(ATHandler &at, const char *apn); + virtual AT_CellularContext *create_context_impl(ATHandler &at, const char *apn, bool cp_req = false, bool nonip_req = false); public: // NetworkInterface void handle_urc(FileHandle *fh); - }; } // namespace mbed diff --git a/features/cellular/framework/targets/QUECTEL/UG96/QUECTEL_UG96_CellularContext.cpp b/features/cellular/framework/targets/QUECTEL/UG96/QUECTEL_UG96_CellularContext.cpp index 9f65d78f9ab..7c61b6b25b6 100644 --- a/features/cellular/framework/targets/QUECTEL/UG96/QUECTEL_UG96_CellularContext.cpp +++ b/features/cellular/framework/targets/QUECTEL/UG96/QUECTEL_UG96_CellularContext.cpp @@ -18,8 +18,8 @@ namespace mbed { -QUECTEL_UG96_CellularContext::QUECTEL_UG96_CellularContext(ATHandler &at, CellularDevice *device, const char *apn) : - AT_CellularContext(at, device, apn) +QUECTEL_UG96_CellularContext::QUECTEL_UG96_CellularContext(ATHandler &at, CellularDevice *device, const char *apn, bool cp_req, bool nonip_req) : + AT_CellularContext(at, device, apn, cp_req, nonip_req) { } @@ -27,11 +27,6 @@ QUECTEL_UG96_CellularContext::~QUECTEL_UG96_CellularContext() { } -bool QUECTEL_UG96_CellularContext::stack_type_supported(nsapi_ip_stack_t stack_type) -{ - return stack_type == IPV4_STACK ? true : false; -} - nsapi_error_t QUECTEL_UG96_CellularContext::do_user_authentication() { if (_pwd && _uname) { diff --git a/features/cellular/framework/targets/QUECTEL/UG96/QUECTEL_UG96_CellularContext.h b/features/cellular/framework/targets/QUECTEL/UG96/QUECTEL_UG96_CellularContext.h index 0ec4232071f..1310fef4da0 100644 --- a/features/cellular/framework/targets/QUECTEL/UG96/QUECTEL_UG96_CellularContext.h +++ b/features/cellular/framework/targets/QUECTEL/UG96/QUECTEL_UG96_CellularContext.h @@ -23,11 +23,10 @@ namespace mbed { class QUECTEL_UG96_CellularContext: public AT_CellularContext { public: - QUECTEL_UG96_CellularContext(ATHandler &at, CellularDevice *device, const char *apn); + QUECTEL_UG96_CellularContext(ATHandler &at, CellularDevice *device, const char *apn, bool cp_req = false, bool nonip_req = false); virtual ~QUECTEL_UG96_CellularContext(); protected: - virtual bool stack_type_supported(nsapi_ip_stack_t stack_type); virtual nsapi_error_t do_user_authentication(); }; diff --git a/features/cellular/framework/targets/QUECTEL/UG96/QUECTEL_UG96_CellularNetwork.cpp b/features/cellular/framework/targets/QUECTEL/UG96/QUECTEL_UG96_CellularNetwork.cpp deleted file mode 100644 index 08e7e2fc6de..00000000000 --- a/features/cellular/framework/targets/QUECTEL/UG96/QUECTEL_UG96_CellularNetwork.cpp +++ /dev/null @@ -1,39 +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 "QUECTEL_UG96_CellularNetwork.h" - -using namespace mbed; - -QUECTEL_UG96_CellularNetwork::QUECTEL_UG96_CellularNetwork(ATHandler &atHandler) : AT_CellularNetwork(atHandler) -{ -} - -QUECTEL_UG96_CellularNetwork::~QUECTEL_UG96_CellularNetwork() -{ -} - -AT_CellularNetwork::RegistrationMode QUECTEL_UG96_CellularNetwork::has_registration(RegistrationType reg_type) -{ - return (reg_type == C_REG || reg_type == C_GREG) ? RegistrationModeLAC : RegistrationModeDisable; -} - -nsapi_error_t QUECTEL_UG96_CellularNetwork::set_access_technology_impl(RadioAccessTechnology opRat) -{ - _op_act = RAT_UNKNOWN; - return NSAPI_ERROR_UNSUPPORTED; -} diff --git a/features/cellular/framework/targets/QUECTEL/UG96/QUECTEL_UG96_CellularNetwork.h b/features/cellular/framework/targets/QUECTEL/UG96/QUECTEL_UG96_CellularNetwork.h deleted file mode 100644 index e9708f3f49d..00000000000 --- a/features/cellular/framework/targets/QUECTEL/UG96/QUECTEL_UG96_CellularNetwork.h +++ /dev/null @@ -1,37 +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. - */ - -#ifndef QUECTEL_UG96_CELLULAR_NETWORK_H_ -#define QUECTEL_UG96_CELLULAR_NETWORK_H_ - -#include "AT_CellularNetwork.h" - -namespace mbed { - -class QUECTEL_UG96_CellularNetwork : public AT_CellularNetwork { -public: - QUECTEL_UG96_CellularNetwork(ATHandler &atHandler); - virtual ~QUECTEL_UG96_CellularNetwork(); - -protected: - virtual RegistrationMode has_registration(RegistrationType rat); - virtual nsapi_error_t set_access_technology_impl(RadioAccessTechnology opRat); -}; - -} // namespace mbed - -#endif // QUECTEL_UG96_CELLULAR_NETWORK_H_ diff --git a/features/cellular/framework/targets/QUECTEL/UG96/QUECTEL_UG96_CellularPower.h b/features/cellular/framework/targets/QUECTEL/UG96/QUECTEL_UG96_CellularPower.h deleted file mode 100644 index f28943c97df..00000000000 --- a/features/cellular/framework/targets/QUECTEL/UG96/QUECTEL_UG96_CellularPower.h +++ /dev/null @@ -1,39 +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. - */ - -#ifndef QUECTEL_UG96_CELLULARPOWER_H_ -#define QUECTEL_UG96_CELLULARPOWER_H_ - -#include "AT_CellularPower.h" - -namespace mbed { - -class QUECTEL_UG96_CellularPower : public AT_CellularPower { -public: - QUECTEL_UG96_CellularPower(ATHandler &atHandler); - virtual ~QUECTEL_UG96_CellularPower(); - -public: //from CellularPower - - virtual nsapi_error_t on(); - - virtual nsapi_error_t off(); -}; - -} // namespace mbed - -#endif // QUECTEL_UG96_CELLULARPOWER_H_ diff --git a/features/cellular/framework/targets/QUECTEL/UG96/mbed_lib.json b/features/cellular/framework/targets/QUECTEL/UG96/mbed_lib.json new file mode 100644 index 00000000000..143d761a914 --- /dev/null +++ b/features/cellular/framework/targets/QUECTEL/UG96/mbed_lib.json @@ -0,0 +1,29 @@ +{ + "name": "QUECTEL_UG96", + "config": { + "tx": { + "help": "TX pin for serial connection. D1 assumed if Arduino Form Factor, needs to be set/overwritten otherwise.", + "value": null + }, + "rx": { + "help": "RX pin for serial connection. D0 assumed if Arduino Form Factor, needs to be set/overwritten otherwise.", + "value": null + }, + "rts": { + "help": "RTS pin for serial connection", + "value": null + }, + "cts": { + "help": "CTS pin for serial connection", + "value": null + }, + "baudrate" : { + "help": "Serial connection baud rate", + "value": 115200 + }, + "provide-default": { + "help": "Provide as default CellularDevice [true/false]", + "value": false + } + } +} diff --git a/features/cellular/framework/targets/TELIT/HE910/TELIT_HE910.cpp b/features/cellular/framework/targets/TELIT/HE910/TELIT_HE910.cpp index d3feca74d3d..cd25409d82b 100644 --- a/features/cellular/framework/targets/TELIT/HE910/TELIT_HE910.cpp +++ b/features/cellular/framework/targets/TELIT/HE910/TELIT_HE910.cpp @@ -16,45 +16,56 @@ */ #include "TELIT_HE910.h" -#include "TELIT_HE910_CellularPower.h" -#include "TELIT_HE910_CellularNetwork.h" -#include "TELIT_HE910_CellularContext.h" +#include "AT_CellularNetwork.h" using namespace mbed; using namespace events; -static const AT_CellularBase::SupportedFeature unsupported_features[] = { - AT_CellularBase::AT_CGSN_WITH_TYPE, // HE910/UE910/UL865/UE866 AT Commands Reference Guide Rev. 11-2006-10-14 - AT_CellularBase::AT_CGAUTH, // HE910/UE910/UL865/UE866 AT Commands Reference Guide Rev. 11-2006-10-14 - AT_CellularBase::SUPPORTED_FEATURE_END_MARK +static const intptr_t cellular_properties[AT_CellularBase::PROPERTY_MAX] = { + AT_CellularNetwork::RegistrationModeDisable,// C_EREG + AT_CellularNetwork::RegistrationModeLAC, // C_GREG + AT_CellularNetwork::RegistrationModeLAC, // C_REG + 0, // AT_CGSN_WITH_TYPE + 1, // AT_CGDATA + 0, // AT_CGAUTH + 1, // PROPERTY_IPV4_STACK + 0, // PROPERTY_IPV6_STACK + 0, // PROPERTY_IPV4V6_STACK }; TELIT_HE910::TELIT_HE910(FileHandle *fh) : AT_CellularDevice(fh) { - AT_CellularBase::set_unsupported_features(unsupported_features); + AT_CellularBase::set_cellular_properties(cellular_properties); } -TELIT_HE910::~TELIT_HE910() -{ -} - -AT_CellularNetwork *TELIT_HE910::open_network_impl(ATHandler &at) +uint16_t TELIT_HE910::get_send_delay() const { - return new TELIT_HE910_CellularNetwork(at); + return DEFAULT_DELAY_BETWEEN_AT_COMMANDS; } -AT_CellularPower *TELIT_HE910::open_power_impl(ATHandler &at) +nsapi_error_t TELIT_HE910::init() { - return new TELIT_HE910_CellularPower(at); -} + nsapi_error_t err = AT_CellularDevice::init(); + if (err != NSAPI_ERROR_OK) { + return err; + } + _at->lock(); + _at->cmd_start("AT&K0;&C1;&D0"); + _at->cmd_stop_read_resp(); -AT_CellularContext *TELIT_HE910::create_context_impl(ATHandler &at, const char *apn) -{ - return new TELIT_HE910_CellularContext(at, this, apn); + return _at->unlock_return_error(); } -uint16_t TELIT_HE910::get_send_delay() const +#if MBED_CONF_TELIT_HE910_PROVIDE_DEFAULT +#include "UARTSerial.h" +CellularDevice *CellularDevice::get_default_instance() { - return DEFAULT_DELAY_BETWEEN_AT_COMMANDS; + static UARTSerial serial(MBED_CONF_TELIT_HE910_TX, MBED_CONF_TELIT_HE910_RX, MBED_CONF_TELIT_HE910_BAUDRATE); +#if defined (MBED_CONF_TELIT_HE910_RTS) && defined (MBED_CONF_TELIT_HE910_CTS) + tr_debug("TELIT_HE910 flow control: RTS %d CTS %d", MBED_CONF_TELIT_HE910_RTS, MBED_CONF_TELIT_HE910_CTS); + serial.set_flow_control(SerialBase::RTSCTS, MBED_CONF_TELIT_HE910_RTS, MBED_CONF_TELIT_HE910_CTS); +#endif + static TELIT_HE910 device(&serial); + return &device; } - +#endif diff --git a/features/cellular/framework/targets/TELIT/HE910/TELIT_HE910.h b/features/cellular/framework/targets/TELIT/HE910/TELIT_HE910.h index b09f525608d..4f34637a426 100644 --- a/features/cellular/framework/targets/TELIT/HE910/TELIT_HE910.h +++ b/features/cellular/framework/targets/TELIT/HE910/TELIT_HE910.h @@ -18,6 +18,15 @@ #ifndef CELLULAR_TARGETS_TELIT_HE910_TELIT_HE910_H_ #define CELLULAR_TARGETS_TELIT_HE910_TELIT_HE910_H_ +#ifdef TARGET_FF_ARDUINO +#ifndef MBED_CONF_TELIT_HE910_TX +#define MBED_CONF_TELIT_HE910_TX D1 +#endif +#ifndef MBED_CONF_TELIT_HE910_RX +#define MBED_CONF_TELIT_HE910_RX D0 +#endif +#endif /* TARGET_FF_ARDUINO */ + #include "AT_CellularDevice.h" //the delay between sending AT commands @@ -28,15 +37,10 @@ namespace mbed { class TELIT_HE910 : public AT_CellularDevice { public: TELIT_HE910(FileHandle *fh); - virtual ~TELIT_HE910(); protected: // AT_CellularDevice - virtual AT_CellularNetwork *open_network_impl(ATHandler &at); - virtual AT_CellularPower *open_power_impl(ATHandler &at); - virtual AT_CellularContext *create_context_impl(ATHandler &at, const char *apn); - -public: // from CellularDevice virtual uint16_t get_send_delay() const; + virtual nsapi_error_t init(); }; } // namespace mbed #endif /* CELLULAR_TARGETS_TELIT_HE910_TELIT_HE910_H_ */ diff --git a/features/cellular/framework/targets/TELIT/HE910/TELIT_HE910_CellularNetwork.cpp b/features/cellular/framework/targets/TELIT/HE910/TELIT_HE910_CellularNetwork.cpp deleted file mode 100644 index 5957d26b596..00000000000 --- a/features/cellular/framework/targets/TELIT/HE910/TELIT_HE910_CellularNetwork.cpp +++ /dev/null @@ -1,39 +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 "TELIT_HE910_CellularNetwork.h" - -using namespace mbed; - -TELIT_HE910_CellularNetwork::TELIT_HE910_CellularNetwork(ATHandler &atHandler) : AT_CellularNetwork(atHandler) -{ -} - -TELIT_HE910_CellularNetwork::~TELIT_HE910_CellularNetwork() -{ -} - -AT_CellularNetwork::RegistrationMode TELIT_HE910_CellularNetwork::has_registration(RegistrationType reg_type) -{ - return (reg_type == C_REG || reg_type == C_GREG) ? RegistrationModeLAC : RegistrationModeDisable; -} - -nsapi_error_t TELIT_HE910_CellularNetwork::set_access_technology_impl(RadioAccessTechnology opRat) -{ - _op_act = RAT_UNKNOWN; - return NSAPI_ERROR_UNSUPPORTED; -} diff --git a/features/cellular/framework/targets/TELIT/HE910/TELIT_HE910_CellularNetwork.h b/features/cellular/framework/targets/TELIT/HE910/TELIT_HE910_CellularNetwork.h deleted file mode 100644 index 6cdbba7cd30..00000000000 --- a/features/cellular/framework/targets/TELIT/HE910/TELIT_HE910_CellularNetwork.h +++ /dev/null @@ -1,37 +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. - */ - -#ifndef TELIT_HE910_CELLULAR_NETWORK_H_ -#define TELIT_HE910_CELLULAR_NETWORK_H_ - -#include "AT_CellularNetwork.h" - -namespace mbed { - -class TELIT_HE910_CellularNetwork : public AT_CellularNetwork { -public: - TELIT_HE910_CellularNetwork(ATHandler &atHandler); - virtual ~TELIT_HE910_CellularNetwork(); - -protected: - virtual RegistrationMode has_registration(RegistrationType rat); - virtual nsapi_error_t set_access_technology_impl(RadioAccessTechnology opRat); -}; - -} // namespace mbed - -#endif // TELIT_HE910_CELLULAR_NETWORK_H_ diff --git a/features/cellular/framework/targets/TELIT/HE910/TELIT_HE910_CellularPower.cpp b/features/cellular/framework/targets/TELIT/HE910/TELIT_HE910_CellularPower.cpp deleted file mode 100644 index 68a397743d1..00000000000 --- a/features/cellular/framework/targets/TELIT/HE910/TELIT_HE910_CellularPower.cpp +++ /dev/null @@ -1,67 +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 "TELIT_HE910_CellularPower.h" -// for modem powering up&down -#include "onboard_modem_api.h" - -using namespace mbed; - -TELIT_HE910_CellularPower::TELIT_HE910_CellularPower(ATHandler &atHandler) : AT_CellularPower(atHandler) -{ - -} - -TELIT_HE910_CellularPower::~TELIT_HE910_CellularPower() -{ - -} - -nsapi_error_t TELIT_HE910_CellularPower::on() -{ -#if MODEM_ON_BOARD - ::onboard_modem_init(); - ::onboard_modem_power_up(); -#endif - return NSAPI_ERROR_OK; -} - -nsapi_error_t TELIT_HE910_CellularPower::off() -{ -#if MODEM_ON_BOARD - ::onboard_modem_power_down(); -#endif - return NSAPI_ERROR_OK; -} - -/** - * Set AT command mode. - * @remark must be called after power on to prepare correct AT mode - * @return blocking until success or failure - */ -nsapi_error_t TELIT_HE910_CellularPower::set_at_mode() -{ - nsapi_error_t err = AT_CellularPower::set_at_mode(); - if (err != NSAPI_ERROR_OK) { - return err; - } - _at.lock(); - _at.cmd_start("AT&K0;&C1;&D0"); - _at.cmd_stop_read_resp(); - - return _at.unlock_return_error(); -} diff --git a/features/cellular/framework/targets/TELIT/HE910/TELIT_HE910_CellularPower.h b/features/cellular/framework/targets/TELIT/HE910/TELIT_HE910_CellularPower.h deleted file mode 100644 index e60f1620cec..00000000000 --- a/features/cellular/framework/targets/TELIT/HE910/TELIT_HE910_CellularPower.h +++ /dev/null @@ -1,41 +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. - */ - -#ifndef TELIT_HE910_CELLULAR_POWER_H_ -#define TELIT_HE910_CELLULAR_POWER_H_ - -#include "AT_CellularPower.h" - -namespace mbed { - -class TELIT_HE910_CellularPower : public AT_CellularPower { -public: - TELIT_HE910_CellularPower(ATHandler &atHandler); - virtual ~TELIT_HE910_CellularPower(); - -public: //from CellularPower - - virtual nsapi_error_t on(); - - virtual nsapi_error_t off(); - - virtual nsapi_error_t set_at_mode(); -}; - -} // namespace mbed - -#endif // TELIT_HE910_CELLULAR_POWER_H_ diff --git a/features/cellular/framework/targets/TELIT/HE910/mbed_lib.json b/features/cellular/framework/targets/TELIT/HE910/mbed_lib.json new file mode 100644 index 00000000000..f5c8498ef27 --- /dev/null +++ b/features/cellular/framework/targets/TELIT/HE910/mbed_lib.json @@ -0,0 +1,30 @@ +{ + "name": "TELIT_HE910", + "config": { + "tx": { + "help": "TX pin for serial connection. D1 assumed if Arduino Form Factor, needs to be set/overwritten otherwise.", + "value": null + }, + "rx": { + "help": "RX pin for serial connection. D0 assumed if Arduino Form Factor, needs to be set/overwritten otherwise.", + "value": null + }, + "rts": { + "help": "RTS pin for serial connection", + "value": null + }, + "cts": { + "help": "CTS pin for serial connection", + "value": null + }, + "baudrate" : { + "help": "Serial connection baud rate", + "value": 115200 + }, + "provide-default": { + "help": "Provide as default CellularDevice [true/false]", + "value": false + } + } +} + diff --git a/features/cellular/framework/targets/UBLOX/AT/UBLOX_AT.cpp b/features/cellular/framework/targets/UBLOX/AT/UBLOX_AT.cpp index 86e4b72be39..a2c7d7c023c 100644 --- a/features/cellular/framework/targets/UBLOX/AT/UBLOX_AT.cpp +++ b/features/cellular/framework/targets/UBLOX/AT/UBLOX_AT.cpp @@ -17,28 +17,40 @@ #include "UBLOX_AT.h" #include "UBLOX_AT_CellularNetwork.h" -#include "UBLOX_AT_CellularPower.h" #include "UBLOX_AT_CellularContext.h" using namespace mbed; using namespace events; #ifdef TARGET_UBLOX_C030_R410M -static const AT_CellularBase::SupportedFeature unsupported_features[] = { - AT_CellularBase::AT_CGSN_WITH_TYPE, - AT_CellularBase::SUPPORTED_FEATURE_END_MARK +static const intptr_t cellular_properties[AT_CellularBase::PROPERTY_MAX] = { + AT_CellularNetwork::RegistrationModeDisable,// C_EREG + AT_CellularNetwork::RegistrationModeLAC, // C_GREG + AT_CellularNetwork::RegistrationModeLAC, // C_REG + 0, // AT_CGSN_WITH_TYPE + 1, // AT_CGDATA + 1, // AT_CGAUTH, + 1, // PROPERTY_IPV4_STACK + 0, // PROPERTY_IPV6_STACK + 0, // PROPERTY_IPV4V6_STACK +}; +#else +static const intptr_t cellular_properties[AT_CellularBase::PROPERTY_MAX] = { + AT_CellularNetwork::RegistrationModeDisable,// C_EREG + AT_CellularNetwork::RegistrationModeLAC, // C_GREG + AT_CellularNetwork::RegistrationModeLAC, // C_REG + 1, // AT_CGSN_WITH_TYPE + 1, // AT_CGDATA + 1, // AT_CGAUTH + 1, // PROPERTY_IPV4_STACK + 0, // PROPERTY_IPV6_STACK + 0, // PROPERTY_IPV4V6_STACK }; #endif UBLOX_AT::UBLOX_AT(FileHandle *fh) : AT_CellularDevice(fh) { -#ifdef TARGET_UBLOX_C030_R410M - AT_CellularBase::set_unsupported_features(unsupported_features); -#endif -} - -UBLOX_AT::~UBLOX_AT() -{ + AT_CellularBase::set_cellular_properties(cellular_properties); } AT_CellularNetwork *UBLOX_AT::open_network_impl(ATHandler &at) @@ -46,12 +58,22 @@ AT_CellularNetwork *UBLOX_AT::open_network_impl(ATHandler &at) return new UBLOX_AT_CellularNetwork(at); } -AT_CellularPower *UBLOX_AT::open_power_impl(ATHandler &at) +AT_CellularContext *UBLOX_AT::create_context_impl(ATHandler &at, const char *apn, bool cp_req, bool nonip_req) { - return new UBLOX_AT_CellularPower(at); + return new UBLOX_AT_CellularContext(at, this, apn, cp_req, nonip_req); } -AT_CellularContext *UBLOX_AT::create_context_impl(ATHandler &at, const char *apn) +#if MBED_CONF_UBLOX_AT_PROVIDE_DEFAULT +#include "UARTSerial.h" +CellularDevice *CellularDevice::get_default_instance() { - return new UBLOX_AT_CellularContext(at, this, apn); + static UARTSerial serial(MBED_CONF_UBLOX_AT_TX, MBED_CONF_UBLOX_AT_RX, MBED_CONF_UBLOX_AT_BAUDRATE); +#if defined (MBED_CONF_UBLOX_AT_RTS) && defined(MBED_CONF_UBLOX_AT_CTS) + tr_debug("UBLOX_AT flow control: RTS %d CTS %d", MBED_CONF_UBLOX_AT_RTS, MBED_CONF_UBLOX_AT_CTS); + serial.set_flow_control(SerialBase::RTSCTS, MBED_CONF_UBLOX_AT_RTS, MBED_CONF_UBLOX_AT_CTS); +#endif + static UBLOX_AT device(&serial); + return &device; } +#endif + diff --git a/features/cellular/framework/targets/UBLOX/AT/UBLOX_AT.h b/features/cellular/framework/targets/UBLOX/AT/UBLOX_AT.h index 1ee455a8a83..01950cb482d 100644 --- a/features/cellular/framework/targets/UBLOX/AT/UBLOX_AT.h +++ b/features/cellular/framework/targets/UBLOX/AT/UBLOX_AT.h @@ -18,6 +18,15 @@ #ifndef UBLOX_AT_H_ #define UBLOX_AT_H_ +#ifdef TARGET_FF_ARDUINO +#ifndef MBED_CONF_UBLOX_AT_TX +#define MBED_CONF_UBLOX_AT_TX D1 +#endif +#ifndef MBED_CONF_UBLOX_AT_RX +#define MBED_CONF_UBLOX_AT_RX D0 +#endif +#endif /* TARGET_FF_ARDUINO */ + #include "AT_CellularDevice.h" namespace mbed { @@ -25,12 +34,10 @@ namespace mbed { class UBLOX_AT : public AT_CellularDevice { public: UBLOX_AT(FileHandle *fh); - virtual ~UBLOX_AT(); protected: // AT_CellularDevice virtual AT_CellularNetwork *open_network_impl(ATHandler &at); - virtual AT_CellularPower *open_power_impl(ATHandler &at); - virtual AT_CellularContext *create_context_impl(ATHandler &at, const char *apn); + virtual AT_CellularContext *create_context_impl(ATHandler &at, const char *apn, bool cp_req = false, bool nonip_req = false); public: // NetworkInterface void handle_urc(FileHandle *fh); }; diff --git a/features/cellular/framework/targets/UBLOX/AT/UBLOX_AT_CellularContext.cpp b/features/cellular/framework/targets/UBLOX/AT/UBLOX_AT_CellularContext.cpp index 240710239e1..3d42ddf9328 100644 --- a/features/cellular/framework/targets/UBLOX/AT/UBLOX_AT_CellularContext.cpp +++ b/features/cellular/framework/targets/UBLOX/AT/UBLOX_AT_CellularContext.cpp @@ -17,11 +17,12 @@ #include "UBLOX_AT_CellularContext.h" #include "UBLOX_AT_CellularStack.h" #include "APN_db.h" +#include "CellularLog.h" namespace mbed { -UBLOX_AT_CellularContext::UBLOX_AT_CellularContext(ATHandler &at, CellularDevice *device, const char *apn) : - AT_CellularContext(at, device, apn) +UBLOX_AT_CellularContext::UBLOX_AT_CellularContext(ATHandler &at, CellularDevice *device, const char *apn, bool cp_req, bool nonip_req) : + AT_CellularContext(at, device, apn, cp_req, nonip_req) { // The authentication to use _auth = NSAPI_SECURITY_UNKNOWN; @@ -33,15 +34,15 @@ UBLOX_AT_CellularContext::~UBLOX_AT_CellularContext() NetworkStack *UBLOX_AT_CellularContext::get_stack() { + if (_pdp_type == NON_IP_PDP_TYPE || _cp_in_use) { + tr_error("Requesting stack for NON-IP context! Should request control plane netif: get_cp_netif()"); + return NULL; + } if (!_stack) { - _stack = new UBLOX_AT_CellularStack(_at, _cid, _ip_stack_type); + _stack = new UBLOX_AT_CellularStack(_at, _cid, (nsapi_ip_stack_t)_pdp_type); } - return _stack; -} -bool UBLOX_AT_CellularContext::stack_type_supported(nsapi_ip_stack_t stack_type) -{ - return stack_type == IPV4_STACK ? true : false; + return _stack; } void UBLOX_AT_CellularContext::do_connect() diff --git a/features/cellular/framework/targets/UBLOX/AT/UBLOX_AT_CellularContext.h b/features/cellular/framework/targets/UBLOX/AT/UBLOX_AT_CellularContext.h index d6cd4731b1f..b330abead17 100644 --- a/features/cellular/framework/targets/UBLOX/AT/UBLOX_AT_CellularContext.h +++ b/features/cellular/framework/targets/UBLOX/AT/UBLOX_AT_CellularContext.h @@ -23,7 +23,7 @@ namespace mbed { class UBLOX_AT_CellularContext: public AT_CellularContext { public: - UBLOX_AT_CellularContext(ATHandler &at, CellularDevice *device, const char *apn); + UBLOX_AT_CellularContext(ATHandler &at, CellularDevice *device, const char *apn, bool cp_req = false, bool nonip_req = false); virtual ~UBLOX_AT_CellularContext(); virtual void do_connect(); @@ -31,7 +31,6 @@ class UBLOX_AT_CellularContext: public AT_CellularContext { protected: virtual NetworkStack *get_stack(); - virtual bool stack_type_supported(nsapi_ip_stack_t stack_type); private: diff --git a/features/cellular/framework/targets/UBLOX/AT/UBLOX_AT_CellularNetwork.cpp b/features/cellular/framework/targets/UBLOX/AT/UBLOX_AT_CellularNetwork.cpp index 85b06a4c7e1..7ad5128e8a0 100644 --- a/features/cellular/framework/targets/UBLOX/AT/UBLOX_AT_CellularNetwork.cpp +++ b/features/cellular/framework/targets/UBLOX/AT/UBLOX_AT_CellularNetwork.cpp @@ -31,11 +31,6 @@ UBLOX_AT_CellularNetwork::~UBLOX_AT_CellularNetwork() } } -AT_CellularNetwork::RegistrationMode UBLOX_AT_CellularNetwork::has_registration(RegistrationType reg_type) -{ - return (reg_type == C_REG || reg_type == C_GREG) ? RegistrationModeLAC : RegistrationModeDisable; -} - nsapi_error_t UBLOX_AT_CellularNetwork::set_access_technology_impl(RadioAccessTechnology opRat) { switch (opRat) { diff --git a/features/cellular/framework/targets/UBLOX/AT/UBLOX_AT_CellularNetwork.h b/features/cellular/framework/targets/UBLOX/AT/UBLOX_AT_CellularNetwork.h index ac9ba299f62..b1f49d376c5 100644 --- a/features/cellular/framework/targets/UBLOX/AT/UBLOX_AT_CellularNetwork.h +++ b/features/cellular/framework/targets/UBLOX/AT/UBLOX_AT_CellularNetwork.h @@ -29,7 +29,6 @@ class UBLOX_AT_CellularNetwork : public AT_CellularNetwork { protected: virtual nsapi_error_t set_access_technology_impl(RadioAccessTechnology opRat); - virtual RegistrationMode has_registration(RegistrationType rat); }; } // namespace mbed diff --git a/features/cellular/framework/targets/UBLOX/AT/mbed_lib.json b/features/cellular/framework/targets/UBLOX/AT/mbed_lib.json new file mode 100644 index 00000000000..4db0d2f28e5 --- /dev/null +++ b/features/cellular/framework/targets/UBLOX/AT/mbed_lib.json @@ -0,0 +1,29 @@ +{ + "name": "UBLOX_AT", + "config": { + "tx": { + "help": "TX pin for serial connection. D1 assumed if Arduino Form Factor, needs to be set/overwritten otherwise.", + "value": null + }, + "rx": { + "help": "RX pin for serial connection. D0 assumed if Arduino Form Factor, needs to be set/overwritten otherwise.", + "value": null + }, + "rts": { + "help": "RTS pin for serial connection", + "value": null + }, + "cts": { + "help": "CTS pin for serial connection", + "value": null + }, + "baudrate" : { + "help": "Serial connection baud rate", + "value": 115200 + }, + "provide-default": { + "help": "Provide as default CellularDevice [true/false]", + "value": false + } + } +} diff --git a/features/cellular/framework/targets/UBLOX/PPP/UBLOX_PPP.cpp b/features/cellular/framework/targets/UBLOX/PPP/UBLOX_PPP.cpp index d71c80d63b1..0acc86b7c5b 100644 --- a/features/cellular/framework/targets/UBLOX/PPP/UBLOX_PPP.cpp +++ b/features/cellular/framework/targets/UBLOX/PPP/UBLOX_PPP.cpp @@ -16,42 +16,57 @@ */ #include "UBLOX_PPP.h" -#include "UBLOX_PPP_CellularNetwork.h" -#include "UBLOX_PPP_CellularPower.h" -#include "UBLOX_PPP_CellularContext.h" +#include "AT_CellularNetwork.h" using namespace mbed; using namespace events; #ifdef TARGET_UBLOX_C027 -static const AT_CellularBase::SupportedFeature unsupported_features[] = { - AT_CellularBase::AT_CGSN_WITH_TYPE, - AT_CellularBase::SUPPORTED_FEATURE_END_MARK +static const intptr_t cellular_properties[AT_CellularBase::PROPERTY_MAX] = { + AT_CellularNetwork::RegistrationModeDisable,// C_EREG + AT_CellularNetwork::RegistrationModeLAC, // C_GREG + AT_CellularNetwork::RegistrationModeLAC, // C_REG + 0, // AT_CGSN_WITH_TYPE + 1, // AT_CGDATA + 1, // AT_CGAUTH + 1, // PROPERTY_IPV4_STACK + 0, // PROPERTY_IPV6_STACK + 0, // PROPERTY_IPV4V6_STACK +}; +#else +static const intptr_t cellular_properties[AT_CellularBase::PROPERTY_MAX] = { + AT_CellularNetwork::RegistrationModeDisable,// C_EREG + AT_CellularNetwork::RegistrationModeLAC, // C_GREG + AT_CellularNetwork::RegistrationModeLAC, // C_REG + 1, // AT_CGSN_WITH_TYPE + 1, // AT_CGDATA + 1, // AT_CGAUTH + 1, // PROPERTY_IPV4_STACK + 0, // PROPERTY_IPV6_STACK + 0, // PROPERTY_IPV4V6_STACK }; #endif UBLOX_PPP::UBLOX_PPP(FileHandle *fh) : AT_CellularDevice(fh) { -#ifdef TARGET_UBLOX_C027 - AT_CellularBase::set_unsupported_features(unsupported_features); -#endif + AT_CellularBase::set_cellular_properties(cellular_properties); } -UBLOX_PPP::~UBLOX_PPP() -{ -} +#if MBED_CONF_UBLOX_PPP_PROVIDE_DEFAULT -AT_CellularNetwork *UBLOX_PPP::open_network_impl(ATHandler &at) -{ - return new UBLOX_PPP_CellularNetwork(at); -} - -AT_CellularPower *UBLOX_PPP::open_power_impl(ATHandler &at) -{ - return new UBLOX_PPP_CellularPower(at); -} +#if !NSAPI_PPP_AVAILABLE +#error Must define lwip.ppp-enabled +#endif -AT_CellularContext *UBLOX_PPP::create_context_impl(ATHandler &at, const char *apn) +#include "UARTSerial.h" +CellularDevice *CellularDevice::get_default_instance() { - return new UBLOX_PPP_CellularContext(at, this, apn); + static UARTSerial serial(MBED_CONF_UBLOX_PPP_TX, MBED_CONF_UBLOX_PPP_RX, MBED_CONF_UBLOX_PPP_BAUDRATE); +#if defined (MBED_CONF_UBLOX_AT_RTS) && defined(MBED_CONF_UBLOX_AT_CTS) + tr_debug("UBLOX_PPP flow control: RTS %d CTS %d", MBED_CONF_UBLOX_PPP_RTS, MBED_CONF_UBLOX_PPP_CTS); + serial.set_flow_control(SerialBase::RTSCTS, MBED_CONF_UBLOX_PPP_RTS, MBED_CONF_UBLOX_PPP_CTS); +#endif + static UBLOX_PPP device(&serial); + return &device; } +#endif diff --git a/features/cellular/framework/targets/UBLOX/PPP/UBLOX_PPP.h b/features/cellular/framework/targets/UBLOX/PPP/UBLOX_PPP.h index 33f8ace12bb..882d3066ab0 100644 --- a/features/cellular/framework/targets/UBLOX/PPP/UBLOX_PPP.h +++ b/features/cellular/framework/targets/UBLOX/PPP/UBLOX_PPP.h @@ -18,6 +18,15 @@ #ifndef UBLOX_PPP_H_ #define UBLOX_PPP_H_ +#ifdef TARGET_FF_ARDUINO +#ifndef MBED_CONF_UBLOX_PPP_TX +#define MBED_CONF_UBLOX_PPP_TX D1 +#endif +#ifndef MBED_CONF_UBLOX_PPP_RX +#define MBED_CONF_UBLOX_PPP_RX D0 +#endif +#endif /* TARGET_FF_ARDUINO */ + #include "AT_CellularDevice.h" namespace mbed { @@ -25,12 +34,6 @@ namespace mbed { class UBLOX_PPP : public AT_CellularDevice { public: UBLOX_PPP(FileHandle *fh); - virtual ~UBLOX_PPP(); - -protected: // AT_CellularDevice - virtual AT_CellularNetwork *open_network_impl(ATHandler &at); - virtual AT_CellularPower *open_power_impl(ATHandler &at); - virtual AT_CellularContext *create_context_impl(ATHandler &at, const char *apn); }; } // namespace mbed diff --git a/features/cellular/framework/targets/UBLOX/PPP/UBLOX_PPP_CellularNetwork.cpp b/features/cellular/framework/targets/UBLOX/PPP/UBLOX_PPP_CellularNetwork.cpp deleted file mode 100644 index 9ada418ae36..00000000000 --- a/features/cellular/framework/targets/UBLOX/PPP/UBLOX_PPP_CellularNetwork.cpp +++ /dev/null @@ -1,39 +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 "UBLOX_PPP_CellularNetwork.h" - -using namespace mbed; - -UBLOX_PPP_CellularNetwork::UBLOX_PPP_CellularNetwork(ATHandler &atHandler) : AT_CellularNetwork(atHandler) -{ -} - -UBLOX_PPP_CellularNetwork::~UBLOX_PPP_CellularNetwork() -{ -} - -AT_CellularNetwork::RegistrationMode UBLOX_PPP_CellularNetwork::has_registration(RegistrationType reg_type) -{ - return (reg_type == C_REG || reg_type == C_GREG) ? RegistrationModeLAC : RegistrationModeDisable; -} - -nsapi_error_t UBLOX_PPP_CellularNetwork::set_access_technology_impl(RadioAccessTechnology opRat) -{ - _op_act = RAT_UNKNOWN; - return NSAPI_ERROR_UNSUPPORTED; -} diff --git a/features/cellular/framework/targets/UBLOX/PPP/UBLOX_PPP_CellularNetwork.h b/features/cellular/framework/targets/UBLOX/PPP/UBLOX_PPP_CellularNetwork.h deleted file mode 100644 index 89ea42efad0..00000000000 --- a/features/cellular/framework/targets/UBLOX/PPP/UBLOX_PPP_CellularNetwork.h +++ /dev/null @@ -1,37 +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. - */ - -#ifndef UBLOX_PPP_CELLULAR_NETWORK_H_ -#define UBLOX_PPP_CELLULAR_NETWORK_H_ - -#include "AT_CellularNetwork.h" - -namespace mbed { - -class UBLOX_PPP_CellularNetwork : public AT_CellularNetwork { -public: - UBLOX_PPP_CellularNetwork(ATHandler &atHandler); - virtual ~UBLOX_PPP_CellularNetwork(); - -protected: - virtual RegistrationMode has_registration(RegistrationType rat); - virtual nsapi_error_t set_access_technology_impl(RadioAccessTechnology opRat); -}; - -} // namespace mbed - -#endif // UBLOX_PPP_CELLULAR_NETWORK_H_ diff --git a/features/cellular/framework/targets/UBLOX/PPP/mbed_lib.json b/features/cellular/framework/targets/UBLOX/PPP/mbed_lib.json new file mode 100644 index 00000000000..7c8bae14386 --- /dev/null +++ b/features/cellular/framework/targets/UBLOX/PPP/mbed_lib.json @@ -0,0 +1,29 @@ +{ + "name": "UBLOX_PPP", + "config": { + "tx": { + "help": "TX pin for serial connection. D1 assumed if Arduino Form Factor, needs to be set/overwritten otherwise.", + "value": null + }, + "rx": { + "help": "RX pin for serial connection. D0 assumed if Arduino Form Factor, needs to be set/overwritten otherwise.", + "value": null + }, + "rts": { + "help": "RTS pin for serial connection", + "value": null + }, + "cts": { + "help": "CTS pin for serial connection", + "value": null + }, + "baudrate" : { + "help": "Serial connection baud rate", + "value": 115200 + }, + "provide-default": { + "help": "Provide as default CellularDevice [true/false]", + "value": false + } + } +} diff --git a/features/netsocket/cellular/CellularNonIPSocket.cpp b/features/netsocket/cellular/CellularNonIPSocket.cpp new file mode 100644 index 00000000000..07abb6af29c --- /dev/null +++ b/features/netsocket/cellular/CellularNonIPSocket.cpp @@ -0,0 +1,267 @@ +/* CellularNonIPSocket +#include + * Copyright (c) 2015 ARM Limited + * + * 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 "platform/Callback.h" +#include "CellularNonIPSocket.h" +#include + +using namespace mbed; + +CellularNonIPSocket::CellularNonIPSocket(CellularContext *cellular_context) + : _timeout(osWaitForever), + _readers(0), _writers(0), _pending(0), + _cp_netif(NULL), + _opened(false) +{ + open(cellular_context); +} + +nsapi_error_t CellularNonIPSocket::open(CellularContext *cellular_context) +{ + return open(cellular_context->get_cp_netif()); +} + +CellularNonIPSocket::~CellularNonIPSocket() +{ + close(); +} + +nsapi_error_t CellularNonIPSocket::open(ControlPlane_netif *cp_netif) +{ + if (_opened) { + return NSAPI_ERROR_OK; + } + + _lock.lock(); + + if (_cp_netif != NULL || cp_netif == NULL) { + _lock.unlock(); + return NSAPI_ERROR_PARAMETER; + } + _cp_netif = cp_netif; + + _event = callback(this, &CellularNonIPSocket::event); + _cp_netif->attach(Callback::thunk, &_event); + _opened = true; + + _lock.unlock(); + return NSAPI_ERROR_OK; +} + +nsapi_error_t CellularNonIPSocket::close() +{ + _lock.lock(); + + nsapi_error_t ret = NSAPI_ERROR_OK; + if (!_opened) { + return NSAPI_ERROR_NO_SOCKET; + } + + // Just in case - tell the stack not to callback any more, then remove this socket. + _cp_netif->attach(0, 0); + _opened = false; + _cp_netif = 0; // Invalidate the cp_netif pointer - otherwise open() fails. + + // Wakeup anything in a blocking operation + // on this socket + event(); + + // Wait until all readers and writers are gone + while (_readers || _writers) { + _lock.unlock(); + _event_flag.wait_any(FINISHED_FLAG, osWaitForever); + _lock.lock(); + } + + _lock.unlock(); + + return ret; +} + +nsapi_size_or_error_t CellularNonIPSocket::send(const void *data, nsapi_size_t size) +{ + _lock.lock(); + nsapi_size_or_error_t ret; + + _writers++; + + while (true) { + if (!_opened) { + ret = NSAPI_ERROR_NO_SOCKET; + break; + } + + _pending = 0; + nsapi_size_or_error_t sent = _cp_netif->send(data, size); + if ((0 == _timeout) || (NSAPI_ERROR_WOULD_BLOCK != sent)) { + ret = sent; + break; + } else { + uint32_t flag; + + // Release lock before blocking so other threads + // accessing this object aren't blocked + _lock.unlock(); + flag = _event_flag.wait_any(WRITE_FLAG, _timeout); + _lock.lock(); + + if (flag & osFlagsError) { + // Timeout break + ret = NSAPI_ERROR_WOULD_BLOCK; + break; + } + } + } + + _writers--; + if (!_opened || !_writers) { + _event_flag.set(FINISHED_FLAG); + } + _lock.unlock(); + return ret; +} + +nsapi_size_or_error_t CellularNonIPSocket::recv(void *buffer, nsapi_size_t size) +{ + _lock.lock(); + nsapi_size_or_error_t ret; + + _readers++; + + while (true) { + if (!_opened) { + ret = NSAPI_ERROR_NO_SOCKET; + break; + } + + _pending = 0; + nsapi_size_or_error_t recv = _cp_netif->recv(buffer, size); + + // Non-blocking sockets always return. Blocking only returns when success or errors other than WOULD_BLOCK + if ((0 == _timeout) || (NSAPI_ERROR_WOULD_BLOCK != recv)) { + ret = recv; + break; + } else { + uint32_t flag; + + // Release lock before blocking so other threads + // accessing this object aren't blocked + _lock.unlock(); + printf("\nWAITWAITWAIT\n"); + flag = _event_flag.wait_any(READ_FLAG, _timeout); + _lock.lock(); + + if (flag & osFlagsError) { + // Timeout break + ret = NSAPI_ERROR_WOULD_BLOCK; + break; + } + } + } + + _readers--; + if (!_opened || !_readers) { + _event_flag.set(FINISHED_FLAG); + } + + _lock.unlock(); + return ret; +} + +void CellularNonIPSocket::set_blocking(bool blocking) +{ + set_timeout(blocking ? -1 : 0); +} + +void CellularNonIPSocket::set_timeout(int timeout) +{ + _lock.lock(); + + if (timeout >= 0) { + _timeout = (uint32_t)timeout; + } else { + _timeout = osWaitForever; + } + + _lock.unlock(); +} + +void CellularNonIPSocket::event() +{ + _event_flag.set(READ_FLAG | WRITE_FLAG); + + _pending += 1; + if (_callback && _pending == 1) { + _callback(); + } +} + +void CellularNonIPSocket::sigio(Callback callback) +{ + _lock.lock(); + _callback = callback; + _lock.unlock(); +} + +nsapi_error_t CellularNonIPSocket::connect(const SocketAddress &address) +{ + return NSAPI_ERROR_UNSUPPORTED; +} + +Socket *CellularNonIPSocket::accept(nsapi_error_t *error) +{ + if (error) { + *error = NSAPI_ERROR_UNSUPPORTED; + } + return NULL; +} + +nsapi_error_t CellularNonIPSocket::listen(int backlog) +{ + return NSAPI_ERROR_UNSUPPORTED; +} + +nsapi_size_or_error_t CellularNonIPSocket::sendto(const SocketAddress &address, + const void *data, nsapi_size_t size) +{ + return NSAPI_ERROR_UNSUPPORTED; +} +nsapi_size_or_error_t CellularNonIPSocket::recvfrom(SocketAddress *address, + void *data, nsapi_size_t size) +{ + return NSAPI_ERROR_UNSUPPORTED; +} + +nsapi_error_t CellularNonIPSocket::setsockopt(int level, int optname, const void *optval, unsigned optlen) +{ + return NSAPI_ERROR_UNSUPPORTED; +} + +nsapi_error_t CellularNonIPSocket::getsockopt(int level, int optname, void *optval, unsigned *optlen) +{ + return NSAPI_ERROR_UNSUPPORTED; +} + +nsapi_error_t CellularNonIPSocket::getpeername(SocketAddress *address) +{ + return NSAPI_ERROR_UNSUPPORTED; +} + +nsapi_error_t CellularNonIPSocket::bind(const SocketAddress &address) +{ + return NSAPI_ERROR_UNSUPPORTED; +} diff --git a/features/netsocket/cellular/CellularNonIPSocket.h b/features/netsocket/cellular/CellularNonIPSocket.h new file mode 100644 index 00000000000..798bd613ff6 --- /dev/null +++ b/features/netsocket/cellular/CellularNonIPSocket.h @@ -0,0 +1,150 @@ + +/** \addtogroup netsocket */ +/** @{*/ +/* Socket + * Copyright (c) 2015 ARM Limited + * + * 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 CELLULARNONIPSOCKET_H +#define CELLULARNONIPSOCKET_H + +#include "netsocket/Socket.h" +#include "rtos/Mutex.h" +#include "rtos/EventFlags.h" +#include "Callback.h" +#include "mbed_toolchain.h" +#include "ControlPlane_netif.h" +#include "CellularContext.h" + +//Socket implementation for non ip datagrams over cellular control plane +class CellularNonIPSocket : public Socket { +public: + /** Destroy the socket. + * + * @note Closes socket if it's still open. + */ + virtual ~CellularNonIPSocket(); + + /** Creates and opens a socket on the given cellular context. + * + * @param cellular_context Cellular PDP context over which this socket + * is sending and receiving data. + */ + CellularNonIPSocket(mbed::CellularContext *cellular_context); + + /** Opens a socket on the given cellular context. + * + * @param cellular_context Cellular PDP context over which this socket is sending and + * receiving data. The context has support for providing + * a control plane interface for data delivery. + * @return NSAPI_ERROR_OK on success + * NSAPI_ERROR_PARAMETER otherwise + */ + nsapi_error_t open(mbed::CellularContext *cellular_context); + + /** Opens a socket that will use the given control plane interface for data delivery. + * Attaches the event as callback to the control plane interface. + * + * @param cp_netif Control plane interface for data delivery. + * @return NSAPI_ERROR_OK on success + * NSAPI_ERROR_PARAMETER otherwise + * + */ + nsapi_error_t open(mbed::ControlPlane_netif *cp_netif); + + /** Closes socket + * + * @return NSAPI_ERROR_OK on success + * NSAPI_ERROR_NO_SOCKET otherwise + */ + + virtual nsapi_error_t close(); + + /** Send data over a control plane cellular context. + * + * By default, send blocks until all data is sent. If socket is set to + * nonblocking or times out, a partial amount can be written. + * NSAPI_ERROR_WOULD_BLOCK is returned if no data was written. + * + * @param data Buffer of data to be sent. + * @param size Size of the buffer in bytes. + * @return Number of sent bytes on success, negative error + * code on failure. + */ + virtual nsapi_size_or_error_t send(const void *data, nsapi_size_t size); + + /** Receive data from a socket. + * + * By default, recv blocks until some data is received. If socket is set to + * nonblocking or times out, NSAPI_ERROR_WOULD_BLOCK can be returned to + * indicate no data. + * + * @param data Pointer to buffer for received data. + * @param size Size of the buffer in bytes. + * @return Number of received bytes on success, negative error + * code on failure. + */ + virtual nsapi_size_or_error_t recv(void *data, nsapi_size_t size); + + /** @copydoc Socket::set_blocking + */ + virtual void set_blocking(bool blocking); + + /** @copydoc Socket::set_blocking + */ + virtual void set_timeout(int timeout); + + /** @copydoc Socket::sigio + */ + virtual void sigio(mbed::Callback func); + + // NOT SUPPORTED + virtual nsapi_error_t connect(const SocketAddress &address); + virtual Socket *accept(nsapi_error_t *error = NULL); + virtual nsapi_error_t listen(int backlog = 1); + virtual nsapi_error_t setsockopt(int level, int optname, const void *optval, unsigned optlen); + virtual nsapi_error_t getsockopt(int level, int optname, void *optval, unsigned *optlen); + virtual nsapi_error_t getpeername(SocketAddress *address); + virtual nsapi_size_or_error_t sendto(const SocketAddress &address, + const void *data, nsapi_size_t size); + virtual nsapi_size_or_error_t recvfrom(SocketAddress *address, + void *data, nsapi_size_t size); + virtual nsapi_error_t bind(const SocketAddress &address); + +protected: + CellularNonIPSocket(); + virtual void event(); + + uint32_t _timeout; + mbed::Callback _event; + mbed::Callback _callback; + rtos::EventFlags _event_flag; + rtos::Mutex _lock; + uint8_t _readers; + uint8_t _writers; + volatile unsigned _pending; + + // Event flags + static const int READ_FLAG = 0x1u; + static const int WRITE_FLAG = 0x2u; + static const int FINISHED_FLAG = 0x3u; + + mbed::ControlPlane_netif *_cp_netif; + bool _opened; +}; + +#endif // CELLULARNONIPSOCKET_H + +/** @}*/ diff --git a/features/netsocket/cellular/ControlPlane_netif.h b/features/netsocket/cellular/ControlPlane_netif.h new file mode 100644 index 00000000000..8d1b062f1c0 --- /dev/null +++ b/features/netsocket/cellular/ControlPlane_netif.h @@ -0,0 +1,78 @@ +/* + * 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 CONTROLPLANE_NETIF_H_ +#define CONTROLPLANE_NETIF_H_ + +#include "nsapi_types.h" + +namespace mbed { + +/* Length of the buffer storing data received over control plane */ +#define MAX_CP_DATA_RECV_LEN 2048 + +// TODO: need to make this l3ip compatible +class ControlPlane_netif { +public: + ControlPlane_netif() {} + virtual ~ControlPlane_netif() {} + + /** Send data over cellular control plane + * + * @param cpdata Buffer of data to be sent over control plane connection + * @param cpdata_length Length of data in bytes + * @return Number of sent bytes on success, negative error + * code on failure. + */ + virtual nsapi_size_or_error_t send(const void *cpdata, nsapi_size_t cpdata_length) = 0; + + /** Receive data over cellular control plane + * + * @param cpdata Destination buffer for data received from control plane connection + * @param cpdata_length Length of data in bytes + * @return Number of received bytes on success, negative error + * code on failure. + */ + virtual nsapi_size_or_error_t recv(void *cpdata, nsapi_size_t cpdata_length) = 0; + + /** Register a callback on state change of the socket + * + * The specified callback will be called on state changes such as when + * the socket can recv/send successfully and on when an error + * occurs. The callback may also be called spuriously without reason. + * + * The callback may be called in an interrupt context and should not + * perform expensive operations such as recv/send calls. + * + * @param callback Function to call on state change + * @param data Argument to pass to callback + */ + virtual void attach(void (*callback)(void *), void *data) = 0; + + /** Receives data from the control plane PDP context + * + * This function is called by cellular PDP context when data + * is received from network. It will invoke the callback set + * by the above attach. + * + * @param buffer Buffer containing received data + * @param size Size of data in bytes + */ + virtual void data_received() = 0; +}; + +} // mbed namespace +#endif diff --git a/features/netsocket/cellular/generic_modem_driver/OnboardCellularInterface.cpp b/features/netsocket/cellular/generic_modem_driver/OnboardCellularInterface.cpp deleted file mode 100644 index a1e39a8853b..00000000000 --- a/features/netsocket/cellular/generic_modem_driver/OnboardCellularInterface.cpp +++ /dev/null @@ -1,82 +0,0 @@ -/* Copyright (c) 2017 ARM Limited - * - * 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 "mbed_toolchain.h" -#include "CellularBase.h" -#include "OnboardCellularInterface.h" - -#ifndef CELLULAR_DEVICE - -#if MODEM_ON_BOARD && MODEM_ON_BOARD_UART && NSAPI_PPP_AVAILABLE - -#include "onboard_modem_api.h" - -/** - * OnboardCellularInterface is an on-board specific implementation. - * - */ - -OnboardCellularInterface::OnboardCellularInterface(bool debug) : - UARTCellularInterface(MDMTXD, MDMRXD, MDMDCD, MDMRTS, - MDMCTS, MDMRI, MDMDTR, MDMDSR, - MBED_CONF_PPP_CELL_IFACE_BAUD_RATE, MDM_PIN_POLARITY, debug) -{ -} - -OnboardCellularInterface::~OnboardCellularInterface() -{ -} - -void OnboardCellularInterface::modem_init() -{ - ::onboard_modem_init(); -} - -void OnboardCellularInterface::modem_deinit() -{ - ::onboard_modem_deinit(); -} - -void OnboardCellularInterface::modem_power_up() -{ - ::onboard_modem_power_up(); -} - -void OnboardCellularInterface::modem_power_down() -{ - ::onboard_modem_power_down(); -} -#endif - -#endif // CELLULAR_DEVICE - -#ifdef CELLULAR_DEVICE -MBED_WEAK CellularBase *CellularBase::get_target_default_instance() -{ - return CellularContext::get_default_instance(); -} -#elif defined ONBOARD_CELLULAR_INTERFACE_AVAILABLE -MBED_WEAK CellularBase *CellularBase::get_target_default_instance() -{ - static OnboardCellularInterface cellular; - - return &cellular; -} -#else -MBED_WEAK CellularBase *CellularBase::get_target_default_instance() -{ - return NULL; -} -#endif diff --git a/features/netsocket/cellular/generic_modem_driver/OnboardCellularInterface.h b/features/netsocket/cellular/generic_modem_driver/OnboardCellularInterface.h deleted file mode 100644 index 8aa049b2612..00000000000 --- a/features/netsocket/cellular/generic_modem_driver/OnboardCellularInterface.h +++ /dev/null @@ -1,174 +0,0 @@ -/* Copyright (c) 2017 ARM Limited - * - * 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 ONBOARD_CELLULAR_INTERFACE_ -#define ONBOARD_CELLULAR_INTERFACE_ - -#include "CellularContext.h" -#ifdef CELLULAR_DEVICE -using namespace mbed; -MBED_DEPRECATED_SINCE("mbed-os-5.9", "This API will be deprecated, use CellularBase::get_default_instance() instead.") -class OnboardCellularInterface : public CellularBase { -public: - OnboardCellularInterface(bool debug = false) - { - context = CellularContext::get_default_instance(); - MBED_ASSERT(context != NULL); - CellularDevice *dev = CellularDevice::get_default_instance(); - MBED_ASSERT(dev != NULL); - dev->modem_debug_on(debug); - } -public: // from NetworkInterface - virtual nsapi_error_t set_blocking(bool blocking) - { - return context->set_blocking(blocking); - } - virtual NetworkStack *get_stack() - { - return context->get_stack(); - } - virtual const char *get_ip_address() - { - return context->get_ip_address(); - } - virtual void attach(mbed::Callback status_cb) - { - context->attach(status_cb); - } - virtual nsapi_error_t connect() - { - return context->connect(); - } - virtual nsapi_error_t disconnect() - { - return context->disconnect(); - } - - // from CellularBase - virtual void set_plmn(const char *plmn) - { - context->set_plmn(plmn); - } - virtual void set_sim_pin(const char *sim_pin) - { - context->set_sim_pin(sim_pin); - } - virtual nsapi_error_t connect(const char *sim_pin, const char *apn = 0, const char *uname = 0, - const char *pwd = 0) - { - return context->connect(sim_pin, apn, uname, pwd); - } - virtual void set_credentials(const char *apn, const char *uname = 0, const char *pwd = 0) - { - context->set_credentials(apn, uname, pwd); - } - virtual const char *get_netmask() - { - return context->get_netmask(); - } - virtual const char *get_gateway() - { - return context->get_gateway(); - } - virtual bool is_connected() - { - return context->is_connected(); - } - -private: - CellularContext *context; -}; - -#define ONBOARD_CELLULAR_INTERFACE_AVAILABLE -#elif MODEM_ON_BOARD && MODEM_ON_BOARD_UART && NSAPI_PPP_AVAILABLE - -#include "UARTCellularInterface.h" - -/** OnboardCellularInterface class - * - * @deprecated This API will be deprecated in mbed-os-5.9. Use mbed-os/features/cellular/framework/API/CellularPower.h instead. - * - * This interface serves as the controller/driver for an - * onboard modem implementing onboard_modem_api.h. - * - * Depending on the type of on-board modem, OnboardCellularInterface - * could be derived from different implementation classes. - * Portable applications should only rely on it being a CellularBase. - */ - -class OnboardCellularInterface : public UARTCellularInterface { - -public: - - /** Constructor - * @deprecated This API will be deprecated in mbed-os-5.9. Use mbed-os/features/cellular/framework/API/CellularPower.h instead. - */ - MBED_DEPRECATED_SINCE("mbed-os-5.9", "This API will be deprecated, use mbed-os/features/cellular/framework/API/CellularPower.h instead.") - OnboardCellularInterface(bool debug = false); - - /** Destructor - * @deprecated This API will be deprecated in mbed-os-5.9. Use mbed-os/features/cellular/framework/API/CellularPower.h instead. - */ - MBED_DEPRECATED_SINCE("mbed-os-5.9", "This API will be deprecated, use mbed-os/features/cellular/framework/API/CellularPower.h instead.") - virtual ~OnboardCellularInterface(); - -protected: - /** Sets the modem up for powering on - * - * @deprecated This API will be deprecated in mbed-os-5.9. Use mbed-os/features/cellular/framework/API/CellularPower.h instead. - * - * modem_init() is equivalent to plugging in the device, for example, attaching power and serial port. - * Uses onboard_modem_api.h where the target provides the implementation of onboard_modem_api. - */ - MBED_DEPRECATED_SINCE("mbed-os-5.9", "This API will be deprecated, use mbed-os/features/cellular/framework/API/CellularPower.h instead.") - virtual void modem_init(); - - /** Sets the modem in unplugged state - * - * @deprecated This API will be deprecated in mbed-os-5.9. Use mbed-os/features/cellular/framework/API/CellularPower.h instead. - * - * modem_deinit() will be equivalent to pulling the plug off of the device, in other words, detaching power - * and serial port. This puts the modem in lowest power state. - * Uses onboard_modem_api.h where the target provides the implementation of onboard_modem_api. - */ - MBED_DEPRECATED_SINCE("mbed-os-5.9", "This API will be deprecated, use mbed-os/features/cellular/framework/API/CellularPower.h instead.") - virtual void modem_deinit(); - - /** Powers up the modem - * - * @deprecated This API will be deprecated in mbed-os-5.9. Use mbed-os/features/cellular/framework/API/CellularPower.h instead. - * - * modem_power_up() is equivalent to pressing the soft power button. - * The driver may repeat this if the modem is not responsive to AT commands. - * Uses onboard_modem_api.h where the target provides the implementation of onboard_modem_api. - */ - MBED_DEPRECATED_SINCE("mbed-os-5.9", "This API will be deprecated, use mbed-os/features/cellular/framework/API/CellularPower.h instead.") - virtual void modem_power_up(); - - /** Powers down the modem - * - * @deprecated This API will be deprecated in mbed-os-5.9. Use mbed-os/features/cellular/framework/API/CellularPower.h instead. - * - * modem_power_down() is equivalent to turning off the modem by button press. - * Uses onboard_modem_api.h where the target provides the implementation of onboard_modem_api. - */ - MBED_DEPRECATED_SINCE("mbed-os-5.9", "This API will be deprecated, use mbed-os/features/cellular/framework/API/CellularPower.h instead.") - virtual void modem_power_down(); -}; - -#define ONBOARD_CELLULAR_INTERFACE_AVAILABLE - -#endif //MODEM_ON_BOARD && MODEM_ON_BOARD_UART && NSAPI_PPP_AVAILABLE -#endif //ONBOARD_CELLULAR_INTERFACE_ diff --git a/features/netsocket/cellular/generic_modem_driver/PPPCellularInterface.cpp b/features/netsocket/cellular/generic_modem_driver/PPPCellularInterface.cpp deleted file mode 100644 index 6fb7d466697..00000000000 --- a/features/netsocket/cellular/generic_modem_driver/PPPCellularInterface.cpp +++ /dev/null @@ -1,845 +0,0 @@ -/* Copyright (c) 2017 ARM Limited - * - * 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 "PPPCellularInterface.h" - -#if NSAPI_PPP_AVAILABLE - -#include -#include "nsapi_ppp.h" -#if MBED_CONF_PPP_CELL_IFACE_APN_LOOKUP -#include "APN_db.h" -#endif //MBED_CONF_PPP_CELL_IFACE_APN_LOOKUP -#include "mbed_trace.h" -#define TRACE_GROUP "UCID" - -/** - * PDP (packet data profile) Context - */ -#define CTX "1" - -/** - * Output Enter sequence for the modem , default CR - */ -#define OUTPUT_ENTER_KEY "\r" - -#if MBED_CONF_PPP_CELL_IFACE_AT_PARSER_BUFFER_SIZE -#define AT_PARSER_BUFFER_SIZE MBED_CONF_PPP_CELL_IFACE_AT_PARSER_BUFFER_SIZE //bytes -#else -#define AT_PARSER_BUFFER_SIZE 256 //bytes -#endif //MBED_CONF_PPP_CELL_IFACE_AT_PARSER_BUFFER_SIZE - -#if MBED_CONF_PPP_CELL_IFACE_AT_PARSER_TIMEOUT -#define AT_PARSER_TIMEOUT MBED_CONF_PPP_CELL_IFACE_AT_PARSER_TIMEOUT -#else -#define AT_PARSER_TIMEOUT 8*1000 //miliseconds -#endif //MBED_CONF_PPP_CELL_IFACE_AT_PARSER_TIMEOUT - -static bool initialized; -static bool set_sim_pin_check_request; -static bool change_pin; -static device_info dev_info; - -static void parser_abort(ATCmdParser *at) -{ - at->abort(); -} - -static bool get_CCID(ATCmdParser *at) -{ - // Returns the ICCID (Integrated Circuit Card ID) of the SIM-card. - // ICCID is a serial number identifying the SIM. - bool success = at->send("AT+CCID") && at->recv("+CCID: %20[^\n]\nOK\n", dev_info.ccid); - tr_debug("DevInfo: CCID=%s", dev_info.ccid); - return success; -} - -static bool get_IMSI(ATCmdParser *at) -{ - // International mobile subscriber identification - bool success = at->send("AT+CIMI") && at->recv("%15[^\n]\nOK\n", dev_info.imsi); - tr_debug("DevInfo: IMSI=%s", dev_info.imsi); - return success; -} - -static bool get_IMEI(ATCmdParser *at) -{ - // International mobile equipment identifier - bool success = at->send("AT+CGSN") && at->recv("%15[^\n]\nOK\n", dev_info.imei); - tr_debug("DevInfo: IMEI=%s", dev_info.imei); - return success; -} - -static bool get_MEID(ATCmdParser *at) -{ - // Mobile equipment identifier - bool success = at->send("AT+GSN") - && at->recv("%18[^\n]\nOK\n", dev_info.meid); - tr_debug("DevInfo: MEID=%s", dev_info.meid); - return success; -} - -static bool set_CMGF(ATCmdParser *at) -{ - // Preferred message format - // set AT+CMGF=[mode] , 0 PDU mode, 1 text mode - bool success = at->send("AT+CMGF=1") && at->recv("OK"); - return success; -} - -static bool set_CNMI(ATCmdParser *at) -{ - // New SMS indication configuration - // set AT+CMTI=[mode, index] , 0 PDU mode, 1 text mode - // Multiple URCs for SMS, i.e., CMT, CMTI, UCMT, CBMI, CDSI as DTE could be following any of these SMS formats - bool success = at->send("AT+CNMI=2," CTX) && at->recv("OK"); - return success; -} - -static void CMTI_URC(ATCmdParser *at) -{ - // our CMGF = 1, i.e., text mode. So we expect response in this format: - //+CMTI: ,, - at->recv(": %*u,%*u"); - tr_info("New SMS received"); - -} - -static void CMT_URC(ATCmdParser *at) -{ - // our CMGF = 1, i.e., text mode. So we expect response in this format: - //+CMT: ,[],[,, - //,,,,, - //] - // By default detailed SMS header CSDH=0 , so we are not expecting [,, - //,,,, - char sms[50]; - char service_timestamp[15]; - at->recv(": %49[^\"]\",,%14[^\"]\"\n", sms, service_timestamp); - - tr_info("SMS:%s, %s", service_timestamp, sms); - -} - -static bool set_atd(ATCmdParser *at) -{ - bool success = at->send("ATD*99***" CTX "#") && at->recv("CONNECT"); - - return success; -} - -/** - * Enables or disables SIM pin check lock - */ -static nsapi_error_t do_sim_pin_check(ATCmdParser *at, const char *pin) -{ - bool success; - if (set_sim_pin_check_request) { - /* use the SIM locked */ - success = at->send("AT+CLCK=\"SC\",1,\"%s\"", pin) && at->recv("OK"); - } else { - /* use the SIM unlocked */ - success = at->send("AT+CLCK=\"SC\",0,\"%s\"", pin) && at->recv("OK"); - } - - if (success) { - return NSAPI_ERROR_OK; - } - - return NSAPI_ERROR_AUTH_FAILURE; -} - -/** - * Change the pin code for the SIM card - */ -static nsapi_error_t do_change_sim_pin(ATCmdParser *at, const char *old_pin, const char *new_pin) -{ - /* changes the SIM pin */ - bool success = at->send("AT+CPWD=\"SC\",\"%s\",\"%s\"", old_pin, new_pin) && at->recv("OK"); - if (success) { - return NSAPI_ERROR_OK; - } - - return NSAPI_ERROR_AUTH_FAILURE; -} - -static void set_nwk_reg_status_csd(unsigned int status) -{ - switch (status) { - case CSD_NOT_REGISTERED_NOT_SEARCHING: - case CSD_NOT_REGISTERED_SEARCHING: - break; - case CSD_REGISTERED: - case CSD_REGISTERED_ROAMING: - tr_debug("Registered for circuit switched service"); - break; - case CSD_REGISTRATION_DENIED: - tr_debug("Circuit switched service denied"); - break; - case CSD_UNKNOWN_COVERAGE: - tr_debug("Out of circuit switched service coverage"); - break; - case CSD_SMS_ONLY: - tr_debug("SMS service only"); - break; - case CSD_SMS_ONLY_ROAMING: - tr_debug("SMS service only"); - break; - case CSD_CSFB_NOT_PREFERRED: - tr_debug("Registered for circuit switched service with CSFB not preferred"); - break; - default: - tr_debug("Unknown circuit switched service registration status. %u", status); - break; - } - - dev_info.reg_status_csd = static_cast(status); -} - -static void set_nwk_reg_status_psd(unsigned int status) -{ - switch (status) { - case PSD_NOT_REGISTERED_NOT_SEARCHING: - case PSD_NOT_REGISTERED_SEARCHING: - break; - case PSD_REGISTERED: - case PSD_REGISTERED_ROAMING: - tr_debug("Registered for packet switched service"); - break; - case PSD_REGISTRATION_DENIED: - tr_debug("Packet switched service denied"); - break; - case PSD_UNKNOWN_COVERAGE: - tr_debug("Out of packet switched service coverage"); - break; - case PSD_EMERGENCY_SERVICES_ONLY: - tr_debug("Limited access for packet switched service. Emergency use only."); - break; - default: - tr_debug("Unknown packet switched service registration status. %u", status); - break; - } - - dev_info.reg_status_psd = static_cast(status); -} - -static bool is_registered_csd() -{ - return (dev_info.reg_status_csd == CSD_REGISTERED) || - (dev_info.reg_status_csd == CSD_REGISTERED_ROAMING) || - (dev_info.reg_status_csd == CSD_CSFB_NOT_PREFERRED); -} - -static bool is_registered_psd() -{ - return (dev_info.reg_status_psd == PSD_REGISTERED) || - (dev_info.reg_status_psd == PSD_REGISTERED_ROAMING); -} - -PPPCellularInterface::PPPCellularInterface(FileHandle *fh, bool debug) -{ - _new_pin = NULL; - _pin = NULL; - _at = NULL; - _apn = NULL; - _uname = NULL; - _pwd = NULL; - _fh = fh; - _debug_trace_on = debug; - _stack = DEFAULT_STACK; - _connection_status_cb = NULL; - _connect_status = NSAPI_STATUS_DISCONNECTED; - _connect_is_blocking = true; - dev_info.reg_status_csd = CSD_NOT_REGISTERED_NOT_SEARCHING; - dev_info.reg_status_psd = PSD_NOT_REGISTERED_NOT_SEARCHING; -} - - -PPPCellularInterface::~PPPCellularInterface() -{ - delete _at; -} - -void PPPCellularInterface::enable_hup(bool) -{ - //meant to be overridden -} - -void PPPCellularInterface::modem_init() -{ - //meant to be overridden -} - -void PPPCellularInterface::modem_deinit() -{ - //meant to be overridden -} - -void PPPCellularInterface::modem_power_up() -{ - //meant to be overridden -} - -void PPPCellularInterface::modem_power_down() -{ - //meant to be overridden -} - -void PPPCellularInterface::modem_debug_on(bool on) -{ - _debug_trace_on = on; -} - -void PPPCellularInterface::ppp_status_cb(nsapi_event_t event, intptr_t parameter) -{ - _connect_status = (nsapi_connection_status_t)parameter; - - if (_connection_status_cb) { - _connection_status_cb(event, parameter); - } -} - -/** - * Public API. Sets up the flag for the driver to enable or disable SIM pin check - * at the next boot. - */ -void PPPCellularInterface::set_sim_pin_check(bool check) -{ - set_sim_pin_check_request = check; -} - -/** - * Public API. Sets up the flag for the driver to change pin code for SIM card - */ -void PPPCellularInterface::set_new_sim_pin(const char *new_pin) -{ - change_pin = true; - _new_pin = new_pin; -} - -bool PPPCellularInterface::nwk_registration(uint8_t nwk_type) -{ - bool success = false; - bool registered = false; - - char str[35]; - int retcode; - int retry_counter = 0; - unsigned int reg_status; - - success = nwk_type == PACKET_SWITCHED ? - _at->send("AT+CGREG=0") : - _at->send("AT+CREG=0") && _at->recv("OK\n"); - - success = _at->send("AT+COPS=0") //initiate auto-registration - && _at->recv("OK"); - if (!success) { - tr_error("Modem not responding."); - return false; - } - - //Network search - //If not registered after 60 attempts, i.e., 30 seconds wait, give up - tr_debug("Searching Network ..."); - - while (!registered) { - - if (retry_counter > 60) { - success = false; - goto give_up; - } - - success = nwk_type == PACKET_SWITCHED ? - _at->send("AT+CGREG?") - && _at->recv("+CGREG: %34[^\n]\n", str) - && _at->recv("OK\n") : - _at->send("AT+CREG?") - && _at->recv("+CREG: %34[^\n]\n", str) - && _at->recv("OK\n"); - - retcode = sscanf(str, "%*u,%u", ®_status); - - if (retcode >= 1) { - if (nwk_type == PACKET_SWITCHED) { - set_nwk_reg_status_psd(reg_status); - if (is_registered_psd()) { - registered = true; - } - } else if (nwk_type == CIRCUIT_SWITCHED) { - set_nwk_reg_status_csd(reg_status); - if (is_registered_csd()) { - registered = true; - } - } - } - - if (registered) { - break; - } else { - wait_ms(500); - } - - retry_counter++; - } - -give_up: - return registered; -} - -bool PPPCellularInterface::is_connected() -{ - return (_connect_status == NSAPI_STATUS_GLOBAL_UP || _connect_status == NSAPI_STATUS_LOCAL_UP); -} - -// Get the SIM card going. -nsapi_error_t PPPCellularInterface::initialize_sim_card() -{ - nsapi_error_t nsapi_error = NSAPI_ERROR_AUTH_FAILURE; - int retry_count = 0; - bool done = false; - - /* SIM initialization may take a significant amount, so an error is - * kind of expected. We should retry 10 times until we succeed or timeout. */ - for (retry_count = 0; !done && (retry_count < 10); retry_count++) { - char pinstr[16]; - - if (_at->send("AT+CPIN?") && _at->recv("+CPIN: %15[^\n]\nOK\n", pinstr)) { - if (strcmp(pinstr, "SIM PIN") == 0) { - if (!_at->send("AT+CPIN=\"%s\"", _pin) || !_at->recv("OK")) { - tr_debug("PIN correct"); - nsapi_error = NSAPI_ERROR_OK; - } - } else if (strcmp(pinstr, "READY") == 0) { - tr_debug("SIM Ready"); - nsapi_error = NSAPI_ERROR_OK; - done = true; - } else { - tr_debug("Unexpected response from SIM: \"%s\"", pinstr); - } - } - - /* wait for a second before retry */ - wait_ms(1000); - } - - if (!done) { - tr_error("SIM not ready."); - } - - return nsapi_error; -} - -void PPPCellularInterface::set_sim_pin(const char *pin) -{ - /* overwrite the default pin by user provided pin */ - _pin = pin; -} - -nsapi_error_t PPPCellularInterface::setup_context_and_credentials() -{ - bool success; - - if (!_apn) { - return NSAPI_ERROR_PARAMETER; - } - -#if NSAPI_PPP_IPV4_AVAILABLE && NSAPI_PPP_IPV6_AVAILABLE - const char ipv4v6_pdp_type[] = {"IPV4V6"}; - const char ipv4_pdp_type[] = {"IP"}; - const char *pdp_type = ipv4v6_pdp_type; - _stack = IPV4V6_STACK; -#elif NSAPI_PPP_IPV6_AVAILABLE - const char pdp_type[] = {"IPV6"}; -#elif NSAPI_PPP_IPV4_AVAILABLE - const char pdp_type[] = {"IP"}; -#endif - const char *auth = _uname && _pwd ? "CHAP:" : ""; - -#if NSAPI_PPP_IPV4_AVAILABLE && NSAPI_PPP_IPV6_AVAILABLE -retry_without_dual_stack: -#endif - success = _at->send("AT" - "+FCLASS=0;" // set to connection (ATD) to data mode - "+CGDCONT=" CTX ",\"%s\",\"%s%s\"", - pdp_type, auth, _apn - ) - && _at->recv("OK"); - -#if NSAPI_PPP_IPV4_AVAILABLE && NSAPI_PPP_IPV6_AVAILABLE - if (_stack == IPV4V6_STACK) { - if (!success) { - // fallback to ipv4 - pdp_type = ipv4_pdp_type; - _stack = IPV4_STACK; - goto retry_without_dual_stack; - } - } -#endif - - if (!success) { - _at->recv("OK"); - } - - return success ? NSAPI_ERROR_OK : NSAPI_ERROR_PARAMETER; - -} - -void PPPCellularInterface::set_credentials(const char *apn, const char *uname, - const char *pwd) -{ - _apn = apn; - _uname = uname; - _pwd = pwd; -} - -void PPPCellularInterface::setup_at_parser() -{ - if (_at) { - return; - } - - _at = new ATCmdParser(_fh, OUTPUT_ENTER_KEY, AT_PARSER_BUFFER_SIZE, AT_PARSER_TIMEOUT, - _debug_trace_on ? true : false); - - /* Error cases, out of band handling */ - _at->oob("ERROR", callback(parser_abort, _at)); - _at->oob("+CME ERROR", callback(parser_abort, _at)); - _at->oob("+CMS ERROR", callback(parser_abort, _at)); - _at->oob("NO CARRIER", callback(parser_abort, _at)); - - /* URCs, handled out of band */ - _at->oob("+CMT", callback(CMT_URC, _at)); - _at->oob("+CMTI", callback(CMTI_URC, _at)); -} - -void PPPCellularInterface::shutdown_at_parser() -{ - delete _at; - _at = NULL; -} - -nsapi_error_t PPPCellularInterface::connect(const char *sim_pin, const char *apn, const char *uname, const char *pwd) -{ - if (!sim_pin) { - return NSAPI_ERROR_PARAMETER; - } - - _pin = sim_pin; - - if (apn) { - if (pwd && !uname) { - return NSAPI_ERROR_PARAMETER; - } - set_credentials(apn, uname, pwd); - } - - return connect(); -} - -nsapi_error_t PPPCellularInterface::connect() -{ - nsapi_error_t retcode; - bool success; - bool did_init = false; - const char *apn_config = NULL; - bool user_specified_apn = false; - - /* If the user has specified the APN then use that or, - * if we are not using the APN database, set _apn to - * "internet" as a best guess - */ - if (_apn) { - user_specified_apn = true; - } else { -#ifndef MBED_CONF_PPP_CELL_IFACE_APN_LOOKUP - _apn = "internet"; - user_specified_apn = true; -#endif - } - - if (is_connected()) { - return NSAPI_ERROR_IS_CONNECTED; - } else if (_connect_status == NSAPI_STATUS_CONNECTING) { - return NSAPI_ERROR_ALREADY; - } - - _connect_status = NSAPI_STATUS_CONNECTING; - if (_connection_status_cb) { - _connection_status_cb(NSAPI_EVENT_CONNECTION_STATUS_CHANGE, NSAPI_STATUS_CONNECTING); - } - - do { -retry_init: - - retcode = NSAPI_ERROR_OK; - - /* setup AT parser */ - setup_at_parser(); - - if (!initialized) { - - /* If we have hangup (eg DCD) detection, we don't want it active - * as long as we are using ATCmdParser. - * As soon as we get into data mode, we will turn it back on. */ - enable_hup(false); - - if (!power_up()) { - retcode = NSAPI_ERROR_DEVICE_ERROR; - break; - } - - retcode = initialize_sim_card(); - if (retcode != NSAPI_ERROR_OK) { - break; - } - - success = nwk_registration(PACKET_SWITCHED) //perform network registration - && get_CCID(_at)//get integrated circuit ID of the SIM - && get_IMSI(_at)//get international mobile subscriber information - && get_IMEI(_at)//get international mobile equipment identifier - && get_MEID(_at)//its same as IMEI - && set_CMGF(_at)//set message format for SMS - && set_CNMI(_at);//set new SMS indication - - if (!success) { - retcode = NSAPI_ERROR_NO_CONNECTION; - break; - } - -#if MBED_CONF_PPP_CELL_IFACE_APN_LOOKUP - if (!apn_config) { - apn_config = apnconfig(dev_info.imsi); - } -#endif - - /* Check if user want skip SIM pin checking on boot up */ - if (set_sim_pin_check_request) { - retcode = do_sim_pin_check(_at, _pin); - if (retcode != NSAPI_ERROR_OK) { - break; - } - /* set this request to false, as it is unnecessary to repeat in case of retry */ - set_sim_pin_check_request = false; - } - - /* check if the user requested a sim pin change */ - if (change_pin) { - retcode = do_change_sim_pin(_at, _pin, _new_pin); - if (retcode != NSAPI_ERROR_OK) { - break; - } - /* set this request to false, as it is unnecessary to repeat in case of retry */ - change_pin = false; - } - -#if MBED_CONF_PPP_CELL_IFACE_APN_LOOKUP - if (!user_specified_apn && apn_config) { - _apn = _APN_GET(apn_config); - _uname = _APN_GET(apn_config); - _pwd = _APN_GET(apn_config); - tr_info("Looked up APN %s.", _apn); - } -#endif - - //sets up APN and IP protocol for external PDP context - retcode = setup_context_and_credentials(); - if (retcode != NSAPI_ERROR_OK) { - break; - } - - if (!success) { - shutdown_at_parser(); - retcode = NSAPI_ERROR_NO_CONNECTION; - break; - } - - initialized = true; - did_init = true; - } else { - /* If we were already initialized, we expect to receive NO_CARRIER response - * from the modem as we were kicked out of Data mode */ - _at->recv("NO CARRIER"); - success = _at->send("AT") && _at->recv("OK"); - } - - tr_info("The APN being used is %s.\n", _apn); - - /* Attempt to enter data mode */ - success = set_atd(_at); //enter into Data mode with the modem - if (!success) { - power_down(); - initialized = false; - - /* if we were previously initialized , i.e., not in this particular attempt, - * we want to re-initialize */ - if (!did_init) { - goto retry_init; - } - - /* shutdown AT parser before notifying application of the failure */ - shutdown_at_parser(); - - retcode = NSAPI_ERROR_NO_CONNECTION; - break; - } - - /* This is the success case. - * Save RAM, discard AT Parser as we have entered Data mode. */ - shutdown_at_parser(); - - /* We now want hangup (e.g., DCD) detection if available */ - enable_hup(true); - - /* Initialize PPP - * mbed_ppp_init() is a blocking call, it will block until - * connected, or timeout after 30 seconds*/ - retcode = nsapi_ppp_connect(_fh, callback(this, &PPPCellularInterface::ppp_status_cb), _uname, _pwd, _stack); - if (retcode == NSAPI_ERROR_OK && _connect_is_blocking) { - _connect_status = NSAPI_STATUS_GLOBAL_UP; - } - } while ((_connect_status == NSAPI_STATUS_CONNECTING && _connect_is_blocking) && - apn_config && *apn_config); - - - if (retcode != NSAPI_ERROR_OK) { - _connect_status = NSAPI_STATUS_DISCONNECTED; - if (_connection_status_cb) { - _connection_status_cb(NSAPI_EVENT_CONNECTION_STATUS_CHANGE, NSAPI_STATUS_DISCONNECTED); - } - } - - - return retcode; -} - -/** - * User initiated disconnect - * - * Disconnects from PPP connection only and brings down the underlying network - * interface - */ -nsapi_error_t PPPCellularInterface::disconnect() -{ - return nsapi_ppp_disconnect(_fh); -} - -const char *PPPCellularInterface::get_ip_address() -{ - return nsapi_ppp_get_ip_addr(_fh); -} - -const char *PPPCellularInterface::get_netmask() -{ - return nsapi_ppp_get_netmask(_fh); -} - -const char *PPPCellularInterface::get_gateway() -{ - return nsapi_ppp_get_gw_addr(_fh); -} - -/** Power down modem - * Uses AT command to do it */ -void PPPCellularInterface::power_down() -{ - modem_power_down(); - modem_deinit(); -} - -/** - * Powers up the modem - * - * Enables the GPIO lines to the modem and then wriggles the power line in short pulses. - */ -bool PPPCellularInterface::power_up() -{ - /* Initialize GPIO lines */ - modem_init(); - /* Give modem a little time to settle down */ - wait(0.25); - - bool success = false; - - int retry_count = 0; - while (true) { - modem_power_up(); - /* Modem tends to spit out noise during power up - don't confuse the parser */ - _at->flush(); - /* It is mandatory to avoid sending data to the serial port during the first 200 ms - * of the module startup. Telit_xE910 Global form factor App note. - * Not necessary for all types of modems however. Let's wait just to be on the safe side */ - wait_ms(200); - _at->set_timeout(1000); - if (_at->send("AT") && _at->recv("OK")) { - tr_info("Modem Ready."); - break; - } - - if (++retry_count > 10) { - goto failure; - } - } - - _at->set_timeout(8000); - - /*For more details regarding DCD and DTR circuitry, please refer to Modem AT manual */ - success = _at->send("AT" - "E0;" //turn off modem echoing - "+CMEE=2;"//turn on verbose responses - "&K0"//turn off RTC/CTS handshaking - "+IPR=115200;"//setup baud rate - "&C1;"//set DCD circuit(109), changes in accordance with the carrier detect status - "&D0")//set DTR circuit, we ignore the state change of DTR - && _at->recv("OK"); - - if (!success) { - goto failure; - } - - /* If everything alright, return from here with success*/ - return success; - -failure: - tr_error("Preliminary modem setup failed."); - return false; -} - -/** - * Get a pointer to the underlying network stack - */ -NetworkStack *PPPCellularInterface::get_stack() -{ - return nsapi_ppp_get_stack(); -} - - -void PPPCellularInterface::attach( - Callback status_cb) -{ - _connection_status_cb = status_cb; -} - -nsapi_connection_status_t PPPCellularInterface::get_connection_status() const -{ - return _connect_status; -} - -nsapi_error_t PPPCellularInterface::set_blocking(bool blocking) -{ - return nsapi_ppp_set_blocking(blocking); -} - - - -#endif // NSAPI_PPP_AVAILABLE diff --git a/features/netsocket/cellular/generic_modem_driver/PPPCellularInterface.h b/features/netsocket/cellular/generic_modem_driver/PPPCellularInterface.h deleted file mode 100644 index 525c7c9f9ce..00000000000 --- a/features/netsocket/cellular/generic_modem_driver/PPPCellularInterface.h +++ /dev/null @@ -1,412 +0,0 @@ -/* Copyright (c) 2017 ARM Limited - * - * 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 PPP_CELLULAR_INTERFACE_ -#define PPP_CELLULAR_INTERFACE_ - -#include "CellularBase.h" -#include "platform/ATCmdParser.h" -#include "mbed.h" - -#if NSAPI_PPP_AVAILABLE - -// Forward declaration -class NetworkStack; - -/** - * Network registration status - * UBX-13001820 - AT Commands Example Application Note (Section 4.1.4.5) - */ -typedef enum { - GSM = 0, - COMPACT_GSM = 1, - UTRAN = 2, - EDGE = 3, - HSDPA = 4, - HSUPA = 5, - HSDPA_HSUPA = 6, - LTE = 7 -} radio_access_nwk_type; - -/** - * Used in registration process to tell which type of network - * to connect. - */ -typedef enum { - CIRCUIT_SWITCHED = 0, - PACKET_SWITCHED -} nwk_type; - -/** - * Circuit Switched network registration status (CREG Usage) - * UBX-13001820 - AT Commands Example Application Note (Section 7.10.3) - */ -typedef enum { - CSD_NOT_REGISTERED_NOT_SEARCHING = 0, - CSD_REGISTERED = 1, - CSD_NOT_REGISTERED_SEARCHING = 2, - CSD_REGISTRATION_DENIED = 3, - CSD_UNKNOWN_COVERAGE = 4, - CSD_REGISTERED_ROAMING = 5, - CSD_SMS_ONLY = 6, - CSD_SMS_ONLY_ROAMING = 7, - CSD_CSFB_NOT_PREFERRED = 9 -} nwk_registration_status_csd; - -/** - * Packet Switched network registration status (CGREG Usage) - * UBX-13001820 - AT Commands Example Application Note (Section 18.27.3) - */ -typedef enum { - PSD_NOT_REGISTERED_NOT_SEARCHING = 0, - PSD_REGISTERED = 1, - PSD_NOT_REGISTERED_SEARCHING = 2, - PSD_REGISTRATION_DENIED = 3, - PSD_UNKNOWN_COVERAGE = 4, - PSD_REGISTERED_ROAMING = 5, - PSD_EMERGENCY_SERVICES_ONLY = 8 -} nwk_registration_status_psd; - -typedef struct { - char ccid[20 + 1]; //!< Integrated Circuit Card ID - char imsi[15 + 1]; //!< International Mobile Station Identity - char imei[15 + 1]; //!< International Mobile Equipment Identity - char meid[18 + 1]; //!< Mobile Equipment IDentifier - int flags; - radio_access_nwk_type rat; - nwk_registration_status_csd reg_status_csd; - nwk_registration_status_psd reg_status_psd; -} device_info; - -/** PPPCellularInterface class - * - * @deprecated This API will be deprecated in mbed-os-5.9. Use mbed-os/features/cellular/easy_cellular/EasyCellularConnection.h instead. - * - * This interface serves as the controller/driver for the Cellular - * modems (tested with UBLOX_C027 and MTS_DRAGONFLY_F411RE). - * - * The driver will work with any generic FileHandle, and can be - * derived from in order to provide forms for specific interfaces, as well as - * adding extra power and reset controls alongside the FileHandle. - */ - -class PPPCellularInterface : public CellularBase { - -public: - - /** Constructor for a generic modem, using a single FileHandle for commands and PPP data. - * - * @deprecated This API will be deprecated in mbed-os-5.9. Use mbed-os/features/cellular/easy_cellular/EasyCellularConnection.h instead. - * - * The file handle pointer is not accessed within the constructor, only recorded for later - * use - this permits a derived class to pass a pointer to a not-yet-constructed member object. - */ - MBED_DEPRECATED_SINCE("mbed-os-5.9", "This API will be deprecated, use mbed-os/features/cellular/easy_cellular/EasyCellularConnection.h instead.") - PPPCellularInterface(FileHandle *fh, bool debug = false); - - /** Destructor - * - * @deprecated This API will be deprecated in mbed-os-5.9. Use mbed-os/features/cellular/easy_cellular/EasyCellularConnection.h instead. - */ - MBED_DEPRECATED_SINCE("mbed-os-5.9", "This API will be deprecated, use mbed-os/features/cellular/easy_cellular/EasyCellularConnection.h instead.") - virtual ~PPPCellularInterface(); - - /** Set the Cellular network credentials - * - * @deprecated This API will be deprecated in mbed-os-5.9. Use mbed-os/features/cellular/easy_cellular/EasyCellularConnection.h instead. - * - * Please check documentation of connect() for default behavior of APN settings. - * - * @param apn Access point name - * @param uname optionally, Username - * @param pwd optionally, password - */ - MBED_DEPRECATED_SINCE("mbed-os-5.9", "This API will be deprecated, use mbed-os/features/cellular/easy_cellular/EasyCellularConnection.h instead.") - virtual void set_credentials(const char *apn, const char *uname = 0, - const char *pwd = 0); - - /** Set the pin code for SIM card - * - * @deprecated This API will be deprecated in mbed-os-5.9. Use mbed-os/features/cellular/easy_cellular/EasyCellularConnection.h instead. - * - * @param sim_pin PIN for the SIM card - */ - MBED_DEPRECATED_SINCE("mbed-os-5.9", "This API will be deprecated, use mbed-os/features/cellular/easy_cellular/EasyCellularConnection.h instead.") - virtual void set_sim_pin(const char *sim_pin); - - /** Start the interface - * - * @deprecated This API will be deprecated in mbed-os-5.9. Use mbed-os/features/cellular/easy_cellular/EasyCellularConnection.h instead. - * - * Attempts to connect to a Cellular network. - * This driver is written mainly for data network connections as CellularBase - * is NetworkInterface. That's why connect() call internally calls nwk_registration() - * method with parameter PACKET_SWITCHED network. Circuit switched hook and registration - * process is implemented and left in the driver for future extension/subclass support,e.g., - * an SMS or GPS extension. - * - * @param sim_pin PIN for the SIM card - * @param apn optionally, access point name - * @param uname optionally, Username - * @param pwd optionally, password - * @return NSAPI_ERROR_OK on success, or negative error code on failure - */ - MBED_DEPRECATED_SINCE("mbed-os-5.9", "This API will be deprecated, use mbed-os/features/cellular/easy_cellular/EasyCellularConnection.h instead.") - virtual nsapi_error_t connect(const char *sim_pin, const char *apn = 0, - const char *uname = 0, const char *pwd = 0); - - /** Attempt to connect to the Cellular network - * - * @deprecated This API will be deprecated in mbed-os-5.9. Use mbed-os/features/cellular/easy_cellular/EasyCellularConnection.h instead. - * - * Brings up the network interface. Connects to the Cellular Radio - * network and then brings up the underlying network stack to be used - * by the cellular modem over PPP interface. - * - * If the SIM requires a PIN, and it is not set/invalid, NSAPI_ERROR_AUTH_ERROR is returned. - * For APN setup, default behavior is to use 'internet' as APN string and assuming no authentication - * is required, i.e., username and password are not set. Optionally, a database lookup can be requested - * by turning on the APN database lookup feature. In order to do so, add 'MBED_CONF_PPP_CELL_IFACE_APN_LOOKUP' - * in your mbed_app.json. APN database is by no means exhaustive. It contains a short list of some public - * APNs with publicly available usernames and passwords (if required) in some particular countries only. - * Lookup is done using IMSI (International mobile subscriber identifier). - * Please note that even if 'MBED_CONF_PPP_CELL_IFACE_APN_LOOKUP' config option is set, 'set_credentials()' api still - * gets the precedence. If the aforementioned API is not used and the config option is set but no match is found in - * the lookup table then the driver tries to resort to default APN settings. - * - * Preferred method is to setup APN using 'set_credentials()' API. - * @return 0 on success, negative error code on failure - */ - MBED_DEPRECATED_SINCE("mbed-os-5.9", "This API will be deprecated, use mbed-os/features/cellular/easy_cellular/EasyCellularConnection.h instead.") - virtual nsapi_error_t connect(); - - /** Attempt to disconnect from the network - * - * Brings down the network interface. Shuts down the PPP interface - * of the underlying network stack. Does not bring down the Radio network - * - * @return 0 on success, negative error code on failure - */ - MBED_DEPRECATED_SINCE("mbed-os-5.9", "This API will be deprecated, use mbed-os/features/cellular/easy_cellular/EasyCellularConnection.h instead.") - virtual nsapi_error_t disconnect(); - - /** Adds or removes a SIM facility lock - * - * @deprecated This API will be deprecated in mbed-os-5.9. Use mbed-os/features/cellular/easy_cellular/EasyCellularConnection.h instead. - * - * Can be used to enable or disable SIM pin check at device startup. - * This API sets up flags for the driver which would either enable or disable - * SIM pin checking depending upon the user provided argument while establishing - * connection. It doesn't do anything immediately other than setting up flags. - * - * @param set can be set to true if the SIM pin check is supposed to be enabled - * and vice versa. - */ - MBED_DEPRECATED_SINCE("mbed-os-5.9", "This API will be deprecated, use mbed-os/features/cellular/easy_cellular/EasyCellularConnection.h instead.") - void set_sim_pin_check(bool set); - - /** Change the pin for the SIM card - * - * @deprecated This API will be deprecated in mbed-os-5.9. Use mbed-os/features/cellular/easy_cellular/EasyCellularConnection.h instead. - * - * Provide the new pin for your SIM card with this API. Old pin code will be assumed to - * be set using set_SIM_pin() API. This API have no immediate effect. While establishing - * connection, driver will change the SIM pin for the next boot. - * - * @param new_pin new pin to be used in string format - */ - MBED_DEPRECATED_SINCE("mbed-os-5.9", "This API will be deprecated, use mbed-os/features/cellular/easy_cellular/EasyCellularConnection.h instead.") - void set_new_sim_pin(const char *new_pin); - - /** Check if the connection is currently established or not - * - * @deprecated This API will be deprecated in mbed-os-5.9. Use mbed-os/features/cellular/easy_cellular/EasyCellularConnection.h instead. - * - * @return true/false If the cellular module have successfully acquired a carrier and is - * connected to an external packet data network using PPP, isConnected() - * API returns true and false otherwise. - */ - MBED_DEPRECATED_SINCE("mbed-os-5.9", "This API will be deprecated, use mbed-os/features/cellular/easy_cellular/EasyCellularConnection.h instead.") - virtual bool is_connected(); - - /** Get the local IP address - * - * @deprecated This API will be deprecated in mbed-os-5.9. Use mbed-os/features/cellular/easy_cellular/EasyCellularConnection.h instead. - * - * @return Null-terminated representation of the local IP address - * or null if no IP address has been received - */ - MBED_DEPRECATED_SINCE("mbed-os-5.9", "This API will be deprecated, use mbed-os/features/cellular/easy_cellular/EasyCellularConnection.h instead.") - virtual const char *get_ip_address(); - - /** Get the local network mask - * - * @deprecated This API will be deprecated in mbed-os-5.9. Use mbed-os/features/cellular/easy_cellular/EasyCellularConnection.h instead. - * - * @return Null-terminated representation of the local network mask - * or null if no network mask has been received - */ - MBED_DEPRECATED_SINCE("mbed-os-5.9", "This API will be deprecated, use mbed-os/features/cellular/easy_cellular/EasyCellularConnection.h instead.") - virtual const char *get_netmask(); - - /** Get the local gateways - * - * @deprecated This API will be deprecated in mbed-os-5.9. Use mbed-os/features/cellular/easy_cellular/EasyCellularConnection.h instead. - * - * @return Null-terminated representation of the local gateway - * or null if no network mask has been received - */ - MBED_DEPRECATED_SINCE("mbed-os-5.9", "This API will be deprecated, use mbed-os/features/cellular/easy_cellular/EasyCellularConnection.h instead.") - virtual const char *get_gateway(); - - - /** Turn modem debug traces on - * - * @param on set true to enable debug traces - */ - MBED_DEPRECATED_SINCE("mbed-os-5.9", "This API will be deprecated, use mbed-os/features/cellular/easy_cellular/EasyCellularConnection.h instead.") - void modem_debug_on(bool on); - - /** Register callback for status reporting - * - * @deprecated This API will be deprecated in mbed-os-5.9. Use mbed-os/features/cellular/easy_cellular/EasyCellularConnection.h instead. - * - * @param status_cb The callback for status changes - */ - MBED_DEPRECATED_SINCE("mbed-os-5.9", "This API will be deprecated, use mbed-os/features/cellular/easy_cellular/EasyCellularConnection.h instead.") - virtual void attach(Callback status_cb); - - /** Get the connection status - * - * @deprecated This API will be deprecated in mbed-os-5.9. Use mbed-os/features/cellular/easy_cellular/EasyCellularConnection.h instead. - * - * @return The connection status according to nsapi_connection_status_t - */ - MBED_DEPRECATED_SINCE("mbed-os-5.9", "This API will be deprecated, use mbed-os/features/cellular/easy_cellular/EasyCellularConnection.h instead.") - virtual nsapi_connection_status_t get_connection_status() const; - - /** Set blocking status of connect() which by default should be blocking - * - * @deprecated This API will be deprecated in mbed-os-5.9. Use mbed-os/features/cellular/easy_cellular/EasyCellularConnection.h instead. - * - * @param blocking true if connect is blocking - * @return 0 on success, negative error code on failure - */ - MBED_DEPRECATED_SINCE("mbed-os-5.9", "This API will be deprecated, use mbed-os/features/cellular/easy_cellular/EasyCellularConnection.h instead.") - virtual nsapi_error_t set_blocking(bool blocking); - -private: - FileHandle *_fh; - ATCmdParser *_at; - const char *_new_pin; - const char *_pin; - const char *_apn; - const char *_uname; - const char *_pwd; - bool _debug_trace_on; - nsapi_ip_stack_t _stack; - Callback _connection_status_cb; - nsapi_connection_status_t _connect_status; - bool _connect_is_blocking; - void base_initialization(); - void setup_at_parser(); - void shutdown_at_parser(); - nsapi_error_t initialize_sim_card(); - nsapi_error_t setup_context_and_credentials(); - bool power_up(); - void power_down(); - void ppp_status_cb(nsapi_event_t, intptr_t); - -protected: - /** Enable or disable hang-up detection - * - * When in PPP data pump mode, it is helpful if the FileHandle will signal hang-up via - * POLLHUP, e.g., if the DCD line is deasserted on a UART. During command mode, this - * signaling is not desired. enable_hup() controls whether this function should be - * active. - * - * Meant to be overridden. - */ - virtual void enable_hup(bool enable); - - /** Sets the modem up for powering on - * - * modem_init() is equivalent to plugging in the device, e.g., attaching power and serial port. - * It is meant to be overridden. - * If the modem is on-board, an implementation of onboard_modem_api.h - * will override this. - * If the the modem is a plugged-in device (i.e., a shield type component), the driver deriving from this - * class will override. - */ - virtual void modem_init(); - - /** Sets the modem in unplugged state - * - * modem_deinit() will be equivalent to pulling the plug off of the device, i.e., detaching power - * and serial port. This puts the modem in lowest power state. - * It is meant to be overridden. - * If the modem is on-board, an implementation of onboard_modem_api.h - * will override this. - * If the the modem is a plugged-in device (i.e., a shield type component), the driver deriving from this - * class will override. - */ - virtual void modem_deinit(); - - /** Powers up the modem - * - * modem_power_up() is equivalent to pressing the soft power button. - * The driver may repeat this if the modem is not responsive to AT commands. - * It is meant to be overridden. - * If the modem is on-board, an implementation of onboard_modem_api.h - * will override this. - * If the the modem is a plugged-in device (i.e., a shield type component), the driver deriving from this - * class will override. - */ - virtual void modem_power_up(); - - /** Powers down the modem - * - * modem_power_down() is equivalent to turning off the modem by button press. - * It is meant to be overridden. - * If the modem is on-board, an implementation of onboard_modem_api.h - * will override this. - * If the the modem is a plugged-in device (i.e., a shield type component), the driver deriving from this - * class will override. - */ - virtual void modem_power_down(); - - /** Provide access to the underlying stack - * - * @return The underlying network stack - */ - virtual NetworkStack *get_stack(); - - /** Starts network registration process. - * - * Potential users could be subclasses who are not network interface - * but would like to use CellularBase infrastructure to register - * with a cellular network, e.g., an SMS extension to CellularBase. - * - * @param nwk_type type of network to connect, defaults to packet switched network - * - * @return true if registration is successful - */ - bool nwk_registration(uint8_t nwk_type = PACKET_SWITCHED); - -}; - -#endif //NSAPI_PPP_AVAILABLE - -#endif //PPP_CELLULAR_INTERFACE_ diff --git a/features/netsocket/cellular/generic_modem_driver/TESTS/unit_tests/default/gmd_ut_config_header.h b/features/netsocket/cellular/generic_modem_driver/TESTS/unit_tests/default/gmd_ut_config_header.h deleted file mode 100644 index b59901c553f..00000000000 --- a/features/netsocket/cellular/generic_modem_driver/TESTS/unit_tests/default/gmd_ut_config_header.h +++ /dev/null @@ -1,69 +0,0 @@ -/* mbed Microcontroller Library - * Copyright (c) 2017 ARM Limited - * - * 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_GMD_UT_CONFIG_HEADER_H_ -#define MBED_OS_GMD_UT_CONFIG_HEADER_H_ - -// The number of retries for UDP exchanges -#define NUM_UDP_RETRIES 5 - -// How long to wait for stuff to travel in the async echo tests -#define ASYNC_TEST_WAIT_TIME 10000 - -#define TEST_DATA "_____0000:0123456789012345678901234567890123456789" \ - "01234567890123456789012345678901234567890123456789" \ - "_____0100:0123456789012345678901234567890123456789" \ - "01234567890123456789012345678901234567890123456789" \ - "_____0200:0123456789012345678901234567890123456789" \ - "01234567890123456789012345678901234567890123456789" \ - "_____0300:0123456789012345678901234567890123456789" \ - "01234567890123456789012345678901234567890123456789" \ - "_____0400:0123456789012345678901234567890123456789" \ - "01234567890123456789012345678901234567890123456789" \ - "_____0500:0123456789012345678901234567890123456789" \ - "01234567890123456789012345678901234567890123456789" \ - "_____0600:0123456789012345678901234567890123456789" \ - "01234567890123456789012345678901234567890123456789" \ - "_____0700:0123456789012345678901234567890123456789" \ - "01234567890123456789012345678901234567890123456789" \ - "_____0800:0123456789012345678901234567890123456789" \ - "01234567890123456789012345678901234567890123456789" \ - "_____0900:0123456789012345678901234567890123456789" \ - "01234567890123456789012345678901234567890123456789" \ - "_____1000:0123456789012345678901234567890123456789" \ - "01234567890123456789012345678901234567890123456789" \ - "_____1100:0123456789012345678901234567890123456789" \ - "01234567890123456789012345678901234567890123456789" \ - "_____1200:0123456789012345678901234567890123456789" \ - "01234567890123456789012345678901234567890123456789" \ - "_____1300:0123456789012345678901234567890123456789" \ - "01234567890123456789012345678901234567890123456789" \ - "_____1400:0123456789012345678901234567890123456789" \ - "01234567890123456789012345678901234567890123456789" \ - "_____1500:0123456789012345678901234567890123456789" \ - "01234567890123456789012345678901234567890123456789" \ - "_____1600:0123456789012345678901234567890123456789" \ - "01234567890123456789012345678901234567890123456789" \ - "_____1700:0123456789012345678901234567890123456789" \ - "01234567890123456789012345678901234567890123456789" \ - "_____1800:0123456789012345678901234567890123456789" \ - "01234567890123456789012345678901234567890123456789" \ - "_____1900:0123456789012345678901234567890123456789" \ - "01234567890123456789012345678901234567890123456789" \ - "_____2000:0123456789012345678901234567890123456789" \ - "01234567890123456789012345678901234567890123456789"; - -#endif /* MBED_OS_GMD_UT_CONFIG_HEADER_H_ */ diff --git a/features/netsocket/cellular/generic_modem_driver/TESTS/unit_tests/default/main.cpp b/features/netsocket/cellular/generic_modem_driver/TESTS/unit_tests/default/main.cpp deleted file mode 100644 index 437d8e2e4ad..00000000000 --- a/features/netsocket/cellular/generic_modem_driver/TESTS/unit_tests/default/main.cpp +++ /dev/null @@ -1,435 +0,0 @@ -/* mbed Microcontroller Library - * Copyright (c) 2017 ARM Limited - * - * 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. - */ - -#if !MODEM_ON_BOARD -#error [NOT_SUPPORTED] MODEM_ON_BOARD should be set for this test to be functional -#endif - -#include "mbed.h" -#include "gmd_ut_config_header.h" -#include "greentea-client/test_env.h" -#include "unity.h" -#include "utest.h" -//Add your driver's header file here -#include "OnboardCellularInterface.h" -#include "UDPSocket.h" -#include "TCPSocket.h" -#include "mbed_trace.h" -#define TRACE_GROUP "TEST" - -using namespace utest::v1; - -#if !defined(MBED_CONF_APP_DEFAULT_PIN) -#error [NOT_SUPPORTED] A json configuration file is needed. Skipping this build. -#endif - -/** How to run port verification tests - * - * i) Copy this file in your implementation directory - * e.g., netsocket/cellular/YOUR_IMPLEMENTATION/TESTS/unit_tests/default/ - * ii) Rename OnboardCellularInterface everywhere in this file with your Class - * iii) Make an empty test application with the fork of mbed-os where your implementation resides - * iv) Create a json file in the root directory of your application and copy the contents of - * template_mbed_app.txt into it - * v) Now from the root of your application, enter this command: - * mbed test --compile-list - * Look for the name of of your test suite matching to the directory path - * vi) Run tests with the command: - * mbed test -n YOUR_TEST_SUITE_NAME - * - * For more information on mbed-greentea testing suite, please visit: - * https://docs.mbed.com/docs/mbed-os-handbook/en/latest/advanced/greentea/ - */ - -// Lock for debug prints -static Mutex mtx; - -// An instance of the cellular driver -// change this with the name of your driver -static OnboardCellularInterface driver(true); - -// Test data -static const char test_data[] = TEST_DATA; - -//Private Function prototypes -static nsapi_error_t do_connect(OnboardCellularInterface *iface); -static int fix(int size, int limit); -static void do_udp_echo(UDPSocket *sock, SocketAddress *host_address, int size); -static int send_all(TCPSocket *sock, const char *data, int size); -static void async_cb(bool *callback_triggered); -static void do_tcp_echo_async(TCPSocket *sock, int size, bool *callback_triggered); -static void use_connection(OnboardCellularInterface *driver); -static void drop_connection(OnboardCellularInterface *driver); -static void lock(); -static void unlock(); - -/* - * Verification tests for a successful porting - * These tests must pass: - * - * test_udp_echo() - * test_tcp_echo_async - * test_connect_credentials - * test_connect_preset_credentials - */ - -/** - * Test UDP data exchange - */ -void test_udp_echo() -{ - UDPSocket sock; - SocketAddress host_address; - int x; - int size; - - driver.disconnect(); - TEST_ASSERT(do_connect(&driver) == 0); - - TEST_ASSERT(driver.gethostbyname(MBED_CONF_APP_ECHO_SERVER, &host_address) == 0); - host_address.set_port(MBED_CONF_APP_ECHO_UDP_PORT); - - tr_debug("UDP: Server %s address: %s on port %d.", - MBED_CONF_APP_ECHO_SERVER, host_address.get_ip_address(), - host_address.get_port()); - - TEST_ASSERT(sock.open(&driver) == 0) - - sock.set_timeout(10000); - - // Test min, max, and some random sizes in-between - do_udp_echo(&sock, &host_address, 1); - do_udp_echo(&sock, &host_address, MBED_CONF_APP_UDP_MAX_PACKET_SIZE); - for (x = 0; x < 10; x++) { - size = (rand() % MBED_CONF_APP_UDP_MAX_PACKET_SIZE) + 1; - size = fix(size, MBED_CONF_APP_UDP_MAX_PACKET_SIZE + 1); - do_udp_echo(&sock, &host_address, size); - } - - sock.close(); - - drop_connection(&driver); - - tr_debug("%d UDP packets of size up to %d byte(s) echoed successfully.", x, - MBED_CONF_APP_UDP_MAX_PACKET_SIZE); -} - -/** - * Test TCP data exchange via the asynchronous sigio() mechanism - */ -void test_tcp_echo_async() -{ - TCPSocket sock; - SocketAddress host_address; - bool callback_triggered = false; - int x; - int size; - - driver.disconnect(); - TEST_ASSERT(do_connect(&driver) == 0); - - TEST_ASSERT( - driver.gethostbyname(MBED_CONF_APP_ECHO_SERVER, &host_address) == 0); - host_address.set_port(MBED_CONF_APP_ECHO_TCP_PORT); - - tr_debug("TCP: Server %s address: %s on port %d.", - MBED_CONF_APP_ECHO_SERVER, host_address.get_ip_address(), - host_address.get_port()); - - TEST_ASSERT(sock.open(&driver) == 0) - - // Set up the async callback and set the timeout to zero - sock.sigio(callback(async_cb, &callback_triggered)); - sock.set_timeout(0); - - TEST_ASSERT(sock.connect(host_address) == 0); - // Test min, max, and some random sizes in-between - do_tcp_echo_async(&sock, 1, &callback_triggered); - do_tcp_echo_async(&sock, MBED_CONF_APP_TCP_MAX_PACKET_SIZE, - &callback_triggered); - - sock.close(); - - drop_connection(&driver); - - tr_debug("TCP packets of size up to %d byte(s) echoed asynchronously and successfully.", - MBED_CONF_APP_TCP_MAX_PACKET_SIZE); -} - -/** - * Connect with credentials included in the connect request - */ -void test_connect_credentials() -{ - - driver.disconnect(); - - TEST_ASSERT(do_connect(&driver) == 0); - use_connection(&driver); - drop_connection(&driver); -} - -/** - * Test with credentials preset - */ -void test_connect_preset_credentials() -{ - driver.disconnect(); - driver.set_sim_pin(MBED_CONF_APP_DEFAULT_PIN); - driver.set_credentials(MBED_CONF_APP_APN, MBED_CONF_APP_USERNAME, - MBED_CONF_APP_PASSWORD); - int num_retries = 0; - nsapi_error_t err = NSAPI_ERROR_OK; - while (!driver.is_connected()) { - err = driver.connect(); - if (err == NSAPI_ERROR_OK || num_retries > MBED_CONF_APP_MAX_RETRIES) { - break; - } - } - - TEST_ASSERT(err == 0); - use_connection(&driver); - drop_connection(&driver); -} - -/** - * Setup Test Environment - */ -utest::v1::status_t test_setup(const size_t number_of_cases) -{ - // Setup Greentea with a timeout - GREENTEA_SETUP(600, "default_auto"); - return verbose_test_setup_handler(number_of_cases); -} - -/** - * Array defining test cases - */ -Case cases[] = { Case("UDP echo test", test_udp_echo), -#if MBED_CONF_LWIP_TCP_ENABLED - Case("TCP async echo test", test_tcp_echo_async), -#endif - Case("Connect with credentials", test_connect_credentials), - Case("Connect with preset credentials", test_connect_preset_credentials) - }; - -Specification specification(test_setup, cases); - -/** - * main test harness - */ -int main() -{ - mbed_trace_init(); - - mbed_trace_mutex_wait_function_set(lock); - mbed_trace_mutex_release_function_set(unlock); - - // Run tests - return !Harness::run(specification); -} -/** - * connect to the network - */ -static nsapi_error_t do_connect(OnboardCellularInterface *iface) -{ - int num_retries = 0; - nsapi_error_t err = NSAPI_ERROR_OK; - while (!iface->is_connected()) { - err = driver.connect(MBED_CONF_APP_DEFAULT_PIN, MBED_CONF_APP_APN, - MBED_CONF_APP_USERNAME, MBED_CONF_APP_PASSWORD); - if (err == NSAPI_ERROR_OK || num_retries > MBED_CONF_APP_MAX_RETRIES) { - break; - } - num_retries++; - } - - return err; -} - -/** - * Get a random size for the test packet - */ -static int fix(int size, int limit) -{ - if (size <= 0) { - size = limit / 2; - } else if (size > limit) { - size = limit; - } - return size; -} - -/** - * Do a UDP socket echo test to a given host of a given packet size - */ -static void do_udp_echo(UDPSocket *sock, SocketAddress *host_address, int size) -{ - bool success = false; - void *recv_data = malloc(size); - TEST_ASSERT(recv_data != NULL); - - // Retry this a few times, don't want to fail due to a flaky link - for (int x = 0; !success && (x < NUM_UDP_RETRIES); x++) { - tr_debug("Echo testing UDP packet size %d byte(s), try %d.", size, x + 1); - if ((sock->sendto(*host_address, (void *) test_data, size) == size) - && (sock->recvfrom(host_address, recv_data, size) == size)) { - TEST_ASSERT(memcmp(test_data, recv_data, size) == 0); - success = true; - } - } - TEST_ASSERT(success); - - free(recv_data); -} - -/** - * Send an entire TCP data buffer until done - */ -static int send_all(TCPSocket *sock, const char *data, int size) -{ - int x; - int count = 0; - Timer timer; - - timer.start(); - while ((count < size) && (timer.read_ms() < ASYNC_TEST_WAIT_TIME)) { - x = sock->send(data + count, size - count); - if (x > 0) { - count += x; - tr_debug("%d byte(s) sent, %d left to send.", count, size - count); - } - wait_ms(10); - } - timer.stop(); - - return count; -} - -/** - * The asynchronous callback - */ -static void async_cb(bool *callback_triggered) -{ - - TEST_ASSERT(callback_triggered != NULL); - *callback_triggered = true; -} - -/** - * Do a TCP echo using the asynchronous driver - */ -static void do_tcp_echo_async(TCPSocket *sock, int size, - bool *callback_triggered) -{ - void *recv_data = malloc(size); - int recv_size = 0; - int remaining_size; - int x, y; - Timer timer; - TEST_ASSERT(recv_data != NULL); - - *callback_triggered = false; - tr_debug("Echo testing TCP packet size %d byte(s) async.", size); - TEST_ASSERT(send_all(sock, test_data, size) == size); - // Wait for all the echoed data to arrive - timer.start(); - remaining_size = size; - while ((recv_size < size) && (timer.read_ms() < ASYNC_TEST_WAIT_TIME)) { - if (*callback_triggered) { - *callback_triggered = false; - x = sock->recv((char *) recv_data + recv_size, remaining_size); - if (x > 0) { - recv_size += x; - remaining_size = size - recv_size; - tr_debug("%d byte(s) echoed back so far, %d to go.", recv_size, - remaining_size); - } - } - wait_ms(10); - } - TEST_ASSERT(recv_size == size); - y = memcmp(test_data, recv_data, size); - if (y != 0) { - tr_debug("Sent %d, |%*.*s|", size, size, size, test_data); - tr_debug("Rcvd %d, |%*.*s|", size, size, size, (char *) recv_data); - // We do not assert a failure here because ublox TCP echo server doesn't send - // back original data. It actually constructs a ublox message string. They need to fix it as - // at the minute in case of TCP, their server is not behaving like a echo TCP server. - //TEST_ASSERT(false); - } - timer.stop(); - free(recv_data); -} - -/** - * Use a connection, checking that it is good - * Checks via doing an NTP transaction - */ -static void use_connection(OnboardCellularInterface *driver) -{ - const char *ip_address = driver->get_ip_address(); - const char *net_mask = driver->get_netmask(); - const char *gateway = driver->get_gateway(); - - TEST_ASSERT(driver->is_connected()); - - TEST_ASSERT(ip_address != NULL); - tr_debug("IP address %s.", ip_address); - TEST_ASSERT(net_mask != NULL); - tr_debug("Net mask %s.", net_mask); - TEST_ASSERT(gateway != NULL); - tr_debug("Gateway %s.", gateway); - - UDPSocket sock; - SocketAddress host_address; - - TEST_ASSERT(driver->gethostbyname(MBED_CONF_APP_ECHO_SERVER, &host_address) == 0); - host_address.set_port(MBED_CONF_APP_ECHO_UDP_PORT); - - tr_debug("UDP: Server %s address: %s on port %d.", - MBED_CONF_APP_ECHO_SERVER, host_address.get_ip_address(), - host_address.get_port()); - - TEST_ASSERT(sock.open(driver) == 0) - - sock.set_timeout(10000); - do_udp_echo(&sock, &host_address, 1); - - sock.close(); -} - -/** - * Drop a connection and check that it has dropped - */ -static void drop_connection(OnboardCellularInterface *driver) -{ - TEST_ASSERT(driver->disconnect() == 0); - TEST_ASSERT(!driver->is_connected()); -} - -/** - * Locks for debug prints - */ -static void lock() -{ - mtx.lock(); -} - -static void unlock() -{ - mtx.unlock(); -} diff --git a/features/netsocket/cellular/generic_modem_driver/TESTS/unit_tests/default/template_mbed_app.txt b/features/netsocket/cellular/generic_modem_driver/TESTS/unit_tests/default/template_mbed_app.txt deleted file mode 100644 index e41a4259105..00000000000 --- a/features/netsocket/cellular/generic_modem_driver/TESTS/unit_tests/default/template_mbed_app.txt +++ /dev/null @@ -1,68 +0,0 @@ -{ - "config": { - "platform": { - "help": "The platform for the cellular feature, e.g. UBLOX or MTS_DRAGONFLY", - "value": "UBLOX" - }, - "max-retries": { - "help": "Network can be intermittent, number of retries before giving up", - "value": 3 - }, - "default-pin": { - "help": "The current value of the SIM PIN as a string", - "value": "\"1234\"" - }, - "apn": { - "help": "The APN string to use for this SIM/network, set to 0 if none", - "value": "\"internet\"" - }, - "username": { - "help": "The user name string to use for this APN, set to zero if none", - "value": 0 - }, - "password": { - "help": "The password string to use for this APN, set to 0 if none", - "value": 0 - }, - "echo-server": { - "help": "The URL string of the UDP/TCP echo server to use during testing", - "value": "\"echo.u-blox.com\"" - }, - "echo-udp-port": { - "help": "The port to connect to on echo-server for UDP testing", - "value": 7 - }, - "echo-tcp-port": { - "help": "The port to connect to on echo-server for TCP testing", - "value": 7 - }, - "ntp-server": { - "help": "The URL string of the NTP server to use during testing", - "value": "\"2.pool.ntp.org\"" - }, - "ntp-port": { - "help": "The port to connect to on ntp-server", - "value": 123 - }, - "udp-max-packet-size": { - "help": "The maximum UDP packet size to use", - "value": 508 - }, - "tcp-max-packet-size": { - "help": "The maximum TCP packet size to use", - "value": 1500 - } - }, - "target_overrides": { - "*": { - "lwip.ipv4-enabled": true, - "lwip.ethernet-enabled": false, - "lwip.ppp-enabled": true, - "lwip.tcp-enabled": true, - "platform.stdio-convert-newlines": true, - "platform.stdio-baud-rate": 115200, - "platform.default-serial-baud-rate": 115200, - "mbed-trace.enable": 1 - } - } -} diff --git a/features/netsocket/cellular/generic_modem_driver/UARTCellularInterface.cpp b/features/netsocket/cellular/generic_modem_driver/UARTCellularInterface.cpp deleted file mode 100644 index 17cf64206f2..00000000000 --- a/features/netsocket/cellular/generic_modem_driver/UARTCellularInterface.cpp +++ /dev/null @@ -1,39 +0,0 @@ -/* Copyright (c) 2017 ARM Limited - * - * 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 "UARTCellularInterface.h" - - -#if NSAPI_PPP_AVAILABLE - -UARTCellularInterface::UARTCellularInterface(PinName txd, PinName rxd, PinName dcd, PinName rts, PinName cts, PinName ri, - PinName dtr, PinName dsr, int baud, bool active_high, bool debug) : - PPPCellularInterface(&_serial, debug), - _serial(txd, rxd, baud) -{ - _dcd_pin = dcd; - _active_high = active_high; -} - -UARTCellularInterface::~UARTCellularInterface() -{ - //do nothing -} - -void UARTCellularInterface::enable_hup(bool enable) -{ - _serial.set_data_carrier_detect(enable ? _dcd_pin : NC, _active_high); -} - -#endif // NSAPI_PPP_AVAILABLE diff --git a/features/netsocket/cellular/generic_modem_driver/UARTCellularInterface.h b/features/netsocket/cellular/generic_modem_driver/UARTCellularInterface.h deleted file mode 100644 index b35bfa0a848..00000000000 --- a/features/netsocket/cellular/generic_modem_driver/UARTCellularInterface.h +++ /dev/null @@ -1,77 +0,0 @@ -/* Copyright (c) 2017 ARM Limited - * - * 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 UART_CELLULAR_INTERFACE_ -#define UART_CELLULAR_INTERFACE_ - -#include "PPPCellularInterface.h" -#include "UARTSerial.h" - -#if NSAPI_PPP_AVAILABLE - -/** UARTCellularInterface class - * - * @deprecated This API will be deprecated in mbed-os-5.9. Use mbed-os/features/cellular/easy_cellular/EasyCellularConnection.h instead. - * - * This interface serves as the controller/driver for Cellular - * modems attached via a UART (tested with UBLOX_C027 and MTS_DRAGONFLY_F411RE). - * - * It constructs a FileHandle and passes it back to its base class as well as overrides - * enable_hup() in the base class. - */ - -class UARTCellularInterface : public PPPCellularInterface { - -public: - - /** Constructor - * - * @deprecated This API will be deprecated in mbed-os-5.9. Use mbed-os/features/cellular/easy_cellular/EasyCellularConnection.h instead. - */ - MBED_DEPRECATED_SINCE("mbed-os-5.9", "This API will be deprecated, use mbed-os/features/cellular/easy_cellular/EasyCellularConnection.h instead.") - UARTCellularInterface(PinName tx, PinName rx, PinName dcd = NC, PinName rts = NC, PinName cts = NC, PinName ri = NC, - PinName dtr = NC, PinName dsr = NC, int baud = MBED_CONF_PPP_CELL_IFACE_BAUD_RATE, - bool active_high = false, - bool debug = false); - - /** Destructor - * - * @deprecated This API will be deprecated in mbed-os-5.9. Use mbed-os/features/cellular/easy_cellular/EasyCellularConnection.h instead. - */ - MBED_DEPRECATED_SINCE("mbed-os-5.9", "This API will be deprecated, use mbed-os/features/cellular/easy_cellular/EasyCellularConnection.h instead.") - virtual ~UARTCellularInterface(); - -private: - UARTSerial _serial; - PinName _dcd_pin; - bool _active_high; - -protected: - /** Enable or disable hang-up detection - * - * @deprecated This API will be deprecated in mbed-os-5.9. Use mbed-os/features/cellular/easy_cellular/EasyCellularConnection.h instead. - * - * When in PPP data pump mode, it is helpful if the FileHandle will signal hang-up via - * POLLHUP, e.g., if the DCD line is deasserted on a UART. During command mode, this - * signaling is not desired. enable_hup() controls whether this function should be - * active. - */ - MBED_DEPRECATED_SINCE("mbed-os-5.9", "This API will be deprecated, use mbed-os/features/cellular/easy_cellular/EasyCellularConnection.h instead.") - virtual void enable_hup(bool enable); -}; - -#endif //NSAPI_PPP_AVAILABLE - -#endif //UART_CELLULAR_INTERFACE_ diff --git a/features/netsocket/cellular/generic_modem_driver/mbed_lib.json b/features/netsocket/cellular/mbed_lib.json similarity index 100% rename from features/netsocket/cellular/generic_modem_driver/mbed_lib.json rename to features/netsocket/cellular/mbed_lib.json diff --git a/targets/TARGET_NXP/TARGET_LPC176X/TARGET_UBLOX_C027/ONBOARD_UBLOX_PPP.cpp b/targets/TARGET_NXP/TARGET_LPC176X/TARGET_UBLOX_C027/ONBOARD_UBLOX_PPP.cpp new file mode 100644 index 00000000000..614f07289eb --- /dev/null +++ b/targets/TARGET_NXP/TARGET_LPC176X/TARGET_UBLOX_C027/ONBOARD_UBLOX_PPP.cpp @@ -0,0 +1,63 @@ +/* mbed Microcontroller Library + * Copyright (c) 2018 ARM Limited + * + * 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 "ONBOARD_UBLOX_PPP.h" +#include "cellular/onboard_modem_api.h" +#include "UARTSerial.h" +#include "CellularLog.h" + +using namespace mbed; + +ONBOARD_UBLOX_PPP::ONBOARD_UBLOX_PPP(FileHandle *fh) : UBLOX_PPP(fh) +{ +} + +nsapi_error_t ONBOARD_UBLOX_PPP::hard_power_on() +{ + ::onboard_modem_init(); + return NSAPI_ERROR_OK; +} + +nsapi_error_t ONBOARD_UBLOX_PPP::hard_power_off() +{ + ::onboard_modem_deinit(); + return NSAPI_ERROR_OK; +} + +nsapi_error_t ONBOARD_UBLOX_PPP::soft_power_on() +{ + ::onboard_modem_power_up(); + return NSAPI_ERROR_OK; +} + +nsapi_error_t ONBOARD_UBLOX_PPP::soft_power_off() +{ + ::onboard_modem_power_down(); + return NSAPI_ERROR_OK; +} + +CellularDevice *CellularDevice::get_target_default_instance() +{ + static UARTSerial serial(MDMTXD, MDMRXD, 115200); +#if DEVICE_SERIAL_FC + if (MDMRTS != NC && MDMCTS != NC) { + tr_debug("Modem flow control: RTS %d CTS %d", MDMRTS, MDMCTS); + serial.set_flow_control(SerialBase::RTSCTS, MDMRTS, MDMCTS); + } +#endif + static ONBOARD_UBLOX_PPP device(&serial); + return &device; +} diff --git a/features/cellular/framework/targets/UBLOX/PPP/UBLOX_PPP_CellularPower.h b/targets/TARGET_NXP/TARGET_LPC176X/TARGET_UBLOX_C027/ONBOARD_UBLOX_PPP.h similarity index 56% rename from features/cellular/framework/targets/UBLOX/PPP/UBLOX_PPP_CellularPower.h rename to targets/TARGET_NXP/TARGET_LPC176X/TARGET_UBLOX_C027/ONBOARD_UBLOX_PPP.h index c9d701ca64f..f95f2228c38 100644 --- a/features/cellular/framework/targets/UBLOX/PPP/UBLOX_PPP_CellularPower.h +++ b/targets/TARGET_NXP/TARGET_LPC176X/TARGET_UBLOX_C027/ONBOARD_UBLOX_PPP.h @@ -1,6 +1,5 @@ -/* - * Copyright (c) 2017, Arm Limited and affiliates. - * SPDX-License-Identifier: Apache-2.0 +/* mbed Microcontroller Library + * Copyright (c) 2018 ARM Limited * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -15,25 +14,23 @@ * limitations under the License. */ -#ifndef UBLOX_PPP_CELLULARPOWER_H_ -#define UBLOX_PPP_CELLULARPOWER_H_ +#ifndef ONBOARD_UBLOX_PPP_ +#define ONBOARD_UBLOX_PPP_ -#include "AT_CellularPower.h" +#include "UBLOX_PPP.h" namespace mbed { -class UBLOX_PPP_CellularPower : public AT_CellularPower { +class ONBOARD_UBLOX_PPP : public UBLOX_PPP { public: - UBLOX_PPP_CellularPower(ATHandler &atHandler); - virtual ~UBLOX_PPP_CellularPower(); + ONBOARD_UBLOX_PPP(FileHandle *fh); -public: //from CellularPower - - virtual nsapi_error_t on(); - - virtual nsapi_error_t off(); + virtual nsapi_error_t hard_power_on(); + virtual nsapi_error_t hard_power_off(); + virtual nsapi_error_t soft_power_on(); + virtual nsapi_error_t soft_power_off(); }; } // namespace mbed -#endif // UBLOX_PPP_CELLULARPOWER_H_ +#endif // ONBOARD_UBLOX_PPP_ diff --git a/features/cellular/framework/targets/UBLOX/PPP/UBLOX_PPP_CellularPower.cpp b/targets/TARGET_STM/TARGET_STM32F4/TARGET_MTB_MTS_DRAGONFLY/ONBOARD_TELIT_HE910.cpp similarity index 51% rename from features/cellular/framework/targets/UBLOX/PPP/UBLOX_PPP_CellularPower.cpp rename to targets/TARGET_STM/TARGET_STM32F4/TARGET_MTB_MTS_DRAGONFLY/ONBOARD_TELIT_HE910.cpp index 8cb78aab2fe..d77f21e46c9 100644 --- a/features/cellular/framework/targets/UBLOX/PPP/UBLOX_PPP_CellularPower.cpp +++ b/targets/TARGET_STM/TARGET_STM32F4/TARGET_MTB_MTS_DRAGONFLY/ONBOARD_TELIT_HE910.cpp @@ -1,6 +1,5 @@ -/* - * Copyright (c) 2017, Arm Limited and affiliates. - * SPDX-License-Identifier: Apache-2.0 +/* mbed Microcontroller Library + * Copyright (c) 2018 ARM Limited * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -15,35 +14,43 @@ * limitations under the License. */ -#include "UBLOX_PPP_CellularPower.h" - -#include "onboard_modem_api.h" +#include "cellular/onboard_modem_api.h" +#include "UARTSerial.h" +#include "ONBOARD_TELIT_HE910.h" using namespace mbed; -UBLOX_PPP_CellularPower::UBLOX_PPP_CellularPower(ATHandler &atHandler) : AT_CellularPower(atHandler) +ONBOARD_TELIT_HE910::ONBOARD_TELIT_HE910(FileHandle *fh) : TELIT_HE910(fh) { - } -UBLOX_PPP_CellularPower::~UBLOX_PPP_CellularPower() +nsapi_error_t ONBOARD_TELIT_HE910::hard_power_on() { + ::onboard_modem_init(); + return NSAPI_ERROR_OK; +} +nsapi_error_t ONBOARD_TELIT_HE910::hard_power_off() +{ + ::onboard_modem_deinit(); + return NSAPI_ERROR_OK; } -nsapi_error_t UBLOX_PPP_CellularPower::on() +nsapi_error_t ONBOARD_TELIT_HE910::soft_power_on() { -#if MODEM_ON_BOARD - ::onboard_modem_init(); ::onboard_modem_power_up(); -#endif return NSAPI_ERROR_OK; } -nsapi_error_t UBLOX_PPP_CellularPower::off() +nsapi_error_t ONBOARD_TELIT_HE910::soft_power_off() { -#if MODEM_ON_BOARD ::onboard_modem_power_down(); -#endif return NSAPI_ERROR_OK; } + +CellularDevice *CellularDevice::get_target_default_instance() +{ + static UARTSerial serial(MDMTXD, MDMRXD, 115200); + static ONBOARD_TELIT_HE910 device(&serial); + return &device; +} diff --git a/features/cellular/framework/targets/MultiTech/DragonflyNano/PPP/SARA4_PPP_CellularPower.h b/targets/TARGET_STM/TARGET_STM32F4/TARGET_MTB_MTS_DRAGONFLY/ONBOARD_TELIT_HE910.h similarity index 56% rename from features/cellular/framework/targets/MultiTech/DragonflyNano/PPP/SARA4_PPP_CellularPower.h rename to targets/TARGET_STM/TARGET_STM32F4/TARGET_MTB_MTS_DRAGONFLY/ONBOARD_TELIT_HE910.h index 39abd60fad5..9e08961411a 100644 --- a/features/cellular/framework/targets/MultiTech/DragonflyNano/PPP/SARA4_PPP_CellularPower.h +++ b/targets/TARGET_STM/TARGET_STM32F4/TARGET_MTB_MTS_DRAGONFLY/ONBOARD_TELIT_HE910.h @@ -1,6 +1,5 @@ -/* - * Copyright (c) 2017, Arm Limited and affiliates. - * SPDX-License-Identifier: Apache-2.0 +/* mbed Microcontroller Library + * Copyright (c) 2018 ARM Limited * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -15,25 +14,23 @@ * limitations under the License. */ -#ifndef SARA4_PPP_CELLULARPOWER_H_ -#define SARA4_PPP_CELLULARPOWER_H_ +#ifndef ONBOARD_TELIT_HE910_ +#define ONBOARD_TELIT_HE910_ -#include "AT_CellularPower.h" +#include "TELIT_HE910.h" namespace mbed { -class SARA4_PPP_CellularPower : public AT_CellularPower { +class ONBOARD_TELIT_HE910 : public TELIT_HE910 { public: - SARA4_PPP_CellularPower(ATHandler &atHandler); - virtual ~SARA4_PPP_CellularPower(); + ONBOARD_TELIT_HE910(FileHandle *fh); -public: //from CellularPower - - virtual nsapi_error_t on(); - - virtual nsapi_error_t off(); + virtual nsapi_error_t hard_power_on(); + virtual nsapi_error_t hard_power_off(); + virtual nsapi_error_t soft_power_on(); + virtual nsapi_error_t soft_power_off(); }; } // namespace mbed -#endif // SARA4_PPP_CELLULARPOWER_H_ +#endif // ONBOARD_TELIT_HE910_ diff --git a/targets/TARGET_STM/TARGET_STM32F4/TARGET_MTS_DRAGONFLY_F411RE/ONBOARD_TELIT_HE910.cpp b/targets/TARGET_STM/TARGET_STM32F4/TARGET_MTS_DRAGONFLY_F411RE/ONBOARD_TELIT_HE910.cpp new file mode 100644 index 00000000000..082ac9097fc --- /dev/null +++ b/targets/TARGET_STM/TARGET_STM32F4/TARGET_MTS_DRAGONFLY_F411RE/ONBOARD_TELIT_HE910.cpp @@ -0,0 +1,63 @@ +/* mbed Microcontroller Library + * Copyright (c) 2018 ARM Limited + * + * 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 "cellular/onboard_modem_api.h" +#include "UARTSerial.h" +#include "ONBOARD_TELIT_HE910.h" +#include "CellularLog.h" + +using namespace mbed; + +ONBOARD_TELIT_HE910::ONBOARD_TELIT_HE910(FileHandle *fh) : TELIT_HE910(fh) +{ +} + +nsapi_error_t ONBOARD_TELIT_HE910::hard_power_on() +{ + ::onboard_modem_init(); + return NSAPI_ERROR_OK; +} + +nsapi_error_t ONBOARD_TELIT_HE910::hard_power_off() +{ + ::onboard_modem_deinit(); + return NSAPI_ERROR_OK; +} + +nsapi_error_t ONBOARD_TELIT_HE910::soft_power_on() +{ + ::onboard_modem_power_up(); + return NSAPI_ERROR_OK; +} + +nsapi_error_t ONBOARD_TELIT_HE910::soft_power_off() +{ + ::onboard_modem_power_down(); + return NSAPI_ERROR_OK; +} + +CellularDevice *CellularDevice::get_target_default_instance() +{ + static UARTSerial serial(MDMTXD, MDMRXD, 115200); +#if DEVICE_SERIAL_FC + if (MDMRTS != NC && MDMCTS != NC) { + tr_debug("Modem flow control: RTS %d CTS %d", MDMRTS, MDMCTS); + serial.set_flow_control(SerialBase::RTSCTS, MDMRTS, MDMCTS); + } +#endif + static ONBOARD_TELIT_HE910 device(&serial); + return &device; +} diff --git a/targets/TARGET_STM/TARGET_STM32F4/TARGET_MTS_DRAGONFLY_F411RE/ONBOARD_TELIT_HE910.h b/targets/TARGET_STM/TARGET_STM32F4/TARGET_MTS_DRAGONFLY_F411RE/ONBOARD_TELIT_HE910.h new file mode 100644 index 00000000000..9e08961411a --- /dev/null +++ b/targets/TARGET_STM/TARGET_STM32F4/TARGET_MTS_DRAGONFLY_F411RE/ONBOARD_TELIT_HE910.h @@ -0,0 +1,36 @@ +/* mbed Microcontroller Library + * Copyright (c) 2018 ARM Limited + * + * 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 ONBOARD_TELIT_HE910_ +#define ONBOARD_TELIT_HE910_ + +#include "TELIT_HE910.h" + +namespace mbed { + +class ONBOARD_TELIT_HE910 : public TELIT_HE910 { +public: + ONBOARD_TELIT_HE910(FileHandle *fh); + + virtual nsapi_error_t hard_power_on(); + virtual nsapi_error_t hard_power_off(); + virtual nsapi_error_t soft_power_on(); + virtual nsapi_error_t soft_power_off(); +}; + +} // namespace mbed + +#endif // ONBOARD_TELIT_HE910_ diff --git a/targets/TARGET_STM/TARGET_STM32F4/TARGET_STM32F437xG/TARGET_UBLOX_C030/ONBOARD_UBLOX.cpp b/targets/TARGET_STM/TARGET_STM32F4/TARGET_STM32F437xG/TARGET_UBLOX_C030/ONBOARD_UBLOX.cpp new file mode 100644 index 00000000000..cd6b9223117 --- /dev/null +++ b/targets/TARGET_STM/TARGET_STM32F4/TARGET_STM32F437xG/TARGET_UBLOX_C030/ONBOARD_UBLOX.cpp @@ -0,0 +1,33 @@ +/* mbed Microcontroller Library + * Copyright (c) 2018 ARM Limited + * + * 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 "ONBOARD_UBLOX_AT.h" +#include "ONBOARD_UBLOX_PPP.h" +#include "UARTSerial.h" + +using namespace mbed; + +CellularDevice *CellularDevice::get_target_default_instance() +{ +#if defined(TARGET_UBLOX_C030_N211) || defined(TARGET_UBLOX_C030_R410M) + static UARTSerial serial(MDMTXD, MDMRXD, 115200); + static ONBOARD_UBLOX_AT device(&serial); +#else + static UARTSerial serial(MDMTXD, MDMRXD, 115200); + static ONBOARD_UBLOX_PPP device(&serial); +#endif + return &device; +} diff --git a/features/cellular/framework/targets/UBLOX/AT/UBLOX_AT_CellularPower.cpp b/targets/TARGET_STM/TARGET_STM32F4/TARGET_STM32F437xG/TARGET_UBLOX_C030/ONBOARD_UBLOX_AT.cpp similarity index 60% rename from features/cellular/framework/targets/UBLOX/AT/UBLOX_AT_CellularPower.cpp rename to targets/TARGET_STM/TARGET_STM32F4/TARGET_STM32F437xG/TARGET_UBLOX_C030/ONBOARD_UBLOX_AT.cpp index aaec18f084e..137f9d96303 100644 --- a/features/cellular/framework/targets/UBLOX/AT/UBLOX_AT_CellularPower.cpp +++ b/targets/TARGET_STM/TARGET_STM32F4/TARGET_STM32F437xG/TARGET_UBLOX_C030/ONBOARD_UBLOX_AT.cpp @@ -1,6 +1,5 @@ -/* - * Copyright (c) 2018, Arm Limited and affiliates. - * SPDX-License-Identifier: Apache-2.0 +/* mbed Microcontroller Library + * Copyright (c) 2018 ARM Limited * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -15,33 +14,35 @@ * limitations under the License. */ -#include "UBLOX_AT_CellularPower.h" - -#include "onboard_modem_api.h" +#include "ONBOARD_UBLOX_AT.h" +#include "cellular/onboard_modem_api.h" using namespace mbed; -UBLOX_AT_CellularPower::UBLOX_AT_CellularPower(ATHandler &atHandler) : AT_CellularPower(atHandler) +ONBOARD_UBLOX_AT::ONBOARD_UBLOX_AT(FileHandle *fh) : UBLOX_AT(fh) { } -UBLOX_AT_CellularPower::~UBLOX_AT_CellularPower() +nsapi_error_t ONBOARD_UBLOX_AT::hard_power_on() { + ::onboard_modem_init(); + return NSAPI_ERROR_OK; } -nsapi_error_t UBLOX_AT_CellularPower::on() +nsapi_error_t ONBOARD_UBLOX_AT::hard_power_off() +{ + ::onboard_modem_deinit(); + return NSAPI_ERROR_OK; +} + +nsapi_error_t ONBOARD_UBLOX_AT::soft_power_on() { -#if MODEM_ON_BOARD - ::onboard_modem_init(); ::onboard_modem_power_up(); -#endif return NSAPI_ERROR_OK; } -nsapi_error_t UBLOX_AT_CellularPower::off() +nsapi_error_t ONBOARD_UBLOX_AT::soft_power_off() { -#if MODEM_ON_BOARD ::onboard_modem_power_down(); -#endif return NSAPI_ERROR_OK; } diff --git a/targets/TARGET_STM/TARGET_STM32F4/TARGET_STM32F437xG/TARGET_UBLOX_C030/ONBOARD_UBLOX_AT.h b/targets/TARGET_STM/TARGET_STM32F4/TARGET_STM32F437xG/TARGET_UBLOX_C030/ONBOARD_UBLOX_AT.h new file mode 100644 index 00000000000..43a76ecbac0 --- /dev/null +++ b/targets/TARGET_STM/TARGET_STM32F4/TARGET_STM32F437xG/TARGET_UBLOX_C030/ONBOARD_UBLOX_AT.h @@ -0,0 +1,36 @@ +/* mbed Microcontroller Library + * Copyright (c) 2018 ARM Limited + * + * 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 ONBOARD_UBLOX_AT_ +#define ONBOARD_UBLOX_AT_ + +#include "UBLOX_AT.h" + +namespace mbed { + +class ONBOARD_UBLOX_AT : public UBLOX_AT { +public: + ONBOARD_UBLOX_AT(FileHandle *fh); + + virtual nsapi_error_t hard_power_on(); + virtual nsapi_error_t hard_power_off(); + virtual nsapi_error_t soft_power_on(); + virtual nsapi_error_t soft_power_off(); +}; + +} // namespace mbed + +#endif // ONBOARD_UBLOX_AT_ diff --git a/features/cellular/framework/targets/QUECTEL/UG96/QUECTEL_UG96_CellularPower.cpp b/targets/TARGET_STM/TARGET_STM32F4/TARGET_STM32F437xG/TARGET_UBLOX_C030/ONBOARD_UBLOX_PPP.cpp similarity index 60% rename from features/cellular/framework/targets/QUECTEL/UG96/QUECTEL_UG96_CellularPower.cpp rename to targets/TARGET_STM/TARGET_STM32F4/TARGET_STM32F437xG/TARGET_UBLOX_C030/ONBOARD_UBLOX_PPP.cpp index a7b754cd3c8..f3f18a5219d 100644 --- a/features/cellular/framework/targets/QUECTEL/UG96/QUECTEL_UG96_CellularPower.cpp +++ b/targets/TARGET_STM/TARGET_STM32F4/TARGET_STM32F437xG/TARGET_UBLOX_C030/ONBOARD_UBLOX_PPP.cpp @@ -1,6 +1,5 @@ -/* - * Copyright (c) 2018, Arm Limited and affiliates. - * SPDX-License-Identifier: Apache-2.0 +/* mbed Microcontroller Library + * Copyright (c) 2018 ARM Limited * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -15,35 +14,35 @@ * limitations under the License. */ -#include "QUECTEL_UG96_CellularPower.h" - -#include "onboard_modem_api.h" +#include "ONBOARD_UBLOX_PPP.h" +#include "cellular/onboard_modem_api.h" using namespace mbed; -QUECTEL_UG96_CellularPower::QUECTEL_UG96_CellularPower(ATHandler &atHandler) : AT_CellularPower(atHandler) +ONBOARD_UBLOX_PPP::ONBOARD_UBLOX_PPP(FileHandle *fh) : UBLOX_PPP(fh) { - } -QUECTEL_UG96_CellularPower::~QUECTEL_UG96_CellularPower() +nsapi_error_t ONBOARD_UBLOX_PPP::hard_power_on() { + ::onboard_modem_init(); + return NSAPI_ERROR_OK; +} +nsapi_error_t ONBOARD_UBLOX_PPP::hard_power_off() +{ + ::onboard_modem_deinit(); + return NSAPI_ERROR_OK; } -nsapi_error_t QUECTEL_UG96_CellularPower::on() +nsapi_error_t ONBOARD_UBLOX_PPP::soft_power_on() { -#if MODEM_ON_BOARD - ::onboard_modem_init(); ::onboard_modem_power_up(); -#endif return NSAPI_ERROR_OK; } -nsapi_error_t QUECTEL_UG96_CellularPower::off() +nsapi_error_t ONBOARD_UBLOX_PPP::soft_power_off() { -#if MODEM_ON_BOARD ::onboard_modem_power_down(); -#endif return NSAPI_ERROR_OK; } diff --git a/targets/TARGET_STM/TARGET_STM32F4/TARGET_STM32F437xG/TARGET_UBLOX_C030/ONBOARD_UBLOX_PPP.h b/targets/TARGET_STM/TARGET_STM32F4/TARGET_STM32F437xG/TARGET_UBLOX_C030/ONBOARD_UBLOX_PPP.h new file mode 100644 index 00000000000..f95f2228c38 --- /dev/null +++ b/targets/TARGET_STM/TARGET_STM32F4/TARGET_STM32F437xG/TARGET_UBLOX_C030/ONBOARD_UBLOX_PPP.h @@ -0,0 +1,36 @@ +/* mbed Microcontroller Library + * Copyright (c) 2018 ARM Limited + * + * 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 ONBOARD_UBLOX_PPP_ +#define ONBOARD_UBLOX_PPP_ + +#include "UBLOX_PPP.h" + +namespace mbed { + +class ONBOARD_UBLOX_PPP : public UBLOX_PPP { +public: + ONBOARD_UBLOX_PPP(FileHandle *fh); + + virtual nsapi_error_t hard_power_on(); + virtual nsapi_error_t hard_power_off(); + virtual nsapi_error_t soft_power_on(); + virtual nsapi_error_t soft_power_off(); +}; + +} // namespace mbed + +#endif // ONBOARD_UBLOX_PPP_ diff --git a/features/cellular/framework/targets/MultiTech/DragonflyNano/PPP/SARA4_PPP_CellularPower.cpp b/targets/TARGET_STM/TARGET_STM32F4/TARGET_STM32F439xI/TARGET_WIO_3G/ONBOARD_QUECTEL_BG96.cpp similarity index 50% rename from features/cellular/framework/targets/MultiTech/DragonflyNano/PPP/SARA4_PPP_CellularPower.cpp rename to targets/TARGET_STM/TARGET_STM32F4/TARGET_STM32F439xI/TARGET_WIO_3G/ONBOARD_QUECTEL_BG96.cpp index 7cb16844814..cd2a74cd9a3 100644 --- a/features/cellular/framework/targets/MultiTech/DragonflyNano/PPP/SARA4_PPP_CellularPower.cpp +++ b/targets/TARGET_STM/TARGET_STM32F4/TARGET_STM32F439xI/TARGET_WIO_3G/ONBOARD_QUECTEL_BG96.cpp @@ -1,6 +1,5 @@ -/* - * Copyright (c) 2017, Arm Limited and affiliates. - * SPDX-License-Identifier: Apache-2.0 +/* mbed Microcontroller Library + * Copyright (c) 2018 ARM Limited * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -15,35 +14,44 @@ * limitations under the License. */ -#include "SARA4_PPP_CellularPower.h" +#include "ONBOARD_QUECTEL_BG96.h" -#include "onboard_modem_api.h" +#include "cellular/onboard_modem_api.h" +#include "UARTSerial.h" using namespace mbed; -SARA4_PPP_CellularPower::SARA4_PPP_CellularPower(ATHandler &atHandler) : AT_CellularPower(atHandler) +ONBOARD_QUECTEL_BG96::ONBOARD_QUECTEL_BG96(FileHandle *fh) : QUECTEL_BG96(fh) { - } -SARA4_PPP_CellularPower::~SARA4_PPP_CellularPower() +nsapi_error_t ONBOARD_QUECTEL_BG96::hard_power_on() { + ::onboard_modem_init(); + return NSAPI_ERROR_OK; +} +nsapi_error_t ONBOARD_QUECTEL_BG96::hard_power_off() +{ + ::onboard_modem_deinit(); + return NSAPI_ERROR_OK; } -nsapi_error_t SARA4_PPP_CellularPower::on() +nsapi_error_t ONBOARD_QUECTEL_BG96::soft_power_on() { -#if MODEM_ON_BOARD - ::onboard_modem_init(); ::onboard_modem_power_up(); -#endif return NSAPI_ERROR_OK; } -nsapi_error_t SARA4_PPP_CellularPower::off() +nsapi_error_t ONBOARD_QUECTEL_BG96::soft_power_off() { -#if MODEM_ON_BOARD ::onboard_modem_power_down(); -#endif return NSAPI_ERROR_OK; } + +CellularDevice *CellularDevice::get_target_default_instance() +{ + static UARTSerial serial(MDMTXD, MDMRXD, 115200); + static ONBOARD_QUECTEL_BG96 device(&serial); + return &device; +} diff --git a/targets/TARGET_STM/TARGET_STM32F4/TARGET_STM32F439xI/TARGET_WIO_3G/ONBOARD_QUECTEL_BG96.h b/targets/TARGET_STM/TARGET_STM32F4/TARGET_STM32F439xI/TARGET_WIO_3G/ONBOARD_QUECTEL_BG96.h new file mode 100644 index 00000000000..1416660ec70 --- /dev/null +++ b/targets/TARGET_STM/TARGET_STM32F4/TARGET_STM32F439xI/TARGET_WIO_3G/ONBOARD_QUECTEL_BG96.h @@ -0,0 +1,36 @@ +/* mbed Microcontroller Library + * Copyright (c) 2018 ARM Limited + * + * 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 ONBOARD_QUECTEL_BG96_ +#define ONBOARD_QUECTEL_BG96_ + +#include "QUECTEL_BG96.h" + +namespace mbed { + +class ONBOARD_QUECTEL_BG96 : public QUECTEL_BG96 { +public: + ONBOARD_QUECTEL_BG96(FileHandle *fh); + + virtual nsapi_error_t hard_power_on(); + virtual nsapi_error_t hard_power_off(); + virtual nsapi_error_t soft_power_on(); + virtual nsapi_error_t soft_power_off(); +}; + +} // namespace mbed + +#endif // ONBOARD_QUECTEL_BG96_ diff --git a/targets/TARGET_STM/TARGET_STM32F4/TARGET_STM32F439xI/TARGET_WIO_BG96/ONBOARD_QUECTEL_BG96.cpp b/targets/TARGET_STM/TARGET_STM32F4/TARGET_STM32F439xI/TARGET_WIO_BG96/ONBOARD_QUECTEL_BG96.cpp new file mode 100644 index 00000000000..cd2a74cd9a3 --- /dev/null +++ b/targets/TARGET_STM/TARGET_STM32F4/TARGET_STM32F439xI/TARGET_WIO_BG96/ONBOARD_QUECTEL_BG96.cpp @@ -0,0 +1,57 @@ +/* mbed Microcontroller Library + * Copyright (c) 2018 ARM Limited + * + * 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 "ONBOARD_QUECTEL_BG96.h" + +#include "cellular/onboard_modem_api.h" +#include "UARTSerial.h" + +using namespace mbed; + +ONBOARD_QUECTEL_BG96::ONBOARD_QUECTEL_BG96(FileHandle *fh) : QUECTEL_BG96(fh) +{ +} + +nsapi_error_t ONBOARD_QUECTEL_BG96::hard_power_on() +{ + ::onboard_modem_init(); + return NSAPI_ERROR_OK; +} + +nsapi_error_t ONBOARD_QUECTEL_BG96::hard_power_off() +{ + ::onboard_modem_deinit(); + return NSAPI_ERROR_OK; +} + +nsapi_error_t ONBOARD_QUECTEL_BG96::soft_power_on() +{ + ::onboard_modem_power_up(); + return NSAPI_ERROR_OK; +} + +nsapi_error_t ONBOARD_QUECTEL_BG96::soft_power_off() +{ + ::onboard_modem_power_down(); + return NSAPI_ERROR_OK; +} + +CellularDevice *CellularDevice::get_target_default_instance() +{ + static UARTSerial serial(MDMTXD, MDMRXD, 115200); + static ONBOARD_QUECTEL_BG96 device(&serial); + return &device; +} diff --git a/targets/TARGET_STM/TARGET_STM32F4/TARGET_STM32F439xI/TARGET_WIO_BG96/ONBOARD_QUECTEL_BG96.h b/targets/TARGET_STM/TARGET_STM32F4/TARGET_STM32F439xI/TARGET_WIO_BG96/ONBOARD_QUECTEL_BG96.h new file mode 100644 index 00000000000..1416660ec70 --- /dev/null +++ b/targets/TARGET_STM/TARGET_STM32F4/TARGET_STM32F439xI/TARGET_WIO_BG96/ONBOARD_QUECTEL_BG96.h @@ -0,0 +1,36 @@ +/* mbed Microcontroller Library + * Copyright (c) 2018 ARM Limited + * + * 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 ONBOARD_QUECTEL_BG96_ +#define ONBOARD_QUECTEL_BG96_ + +#include "QUECTEL_BG96.h" + +namespace mbed { + +class ONBOARD_QUECTEL_BG96 : public QUECTEL_BG96 { +public: + ONBOARD_QUECTEL_BG96(FileHandle *fh); + + virtual nsapi_error_t hard_power_on(); + virtual nsapi_error_t hard_power_off(); + virtual nsapi_error_t soft_power_on(); + virtual nsapi_error_t soft_power_off(); +}; + +} // namespace mbed + +#endif // ONBOARD_QUECTEL_BG96_ diff --git a/targets/TARGET_STM/TARGET_STM32L4/TARGET_MTS_DRAGONFLY_L471QG/ONBOARD_SARA4_PPP.cpp b/targets/TARGET_STM/TARGET_STM32L4/TARGET_MTS_DRAGONFLY_L471QG/ONBOARD_SARA4_PPP.cpp new file mode 100644 index 00000000000..029b2569afa --- /dev/null +++ b/targets/TARGET_STM/TARGET_STM32L4/TARGET_MTS_DRAGONFLY_L471QG/ONBOARD_SARA4_PPP.cpp @@ -0,0 +1,63 @@ +/* mbed Microcontroller Library + * Copyright (c) 2018 ARM Limited + * + * 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 "ONBOARD_SARA4_PPP.h" +#include "cellular/onboard_modem_api.h" +#include "UARTSerial.h" +#include "CellularLog.h" + +using namespace mbed; + +ONBOARD_SARA4_PPP::ONBOARD_SARA4_PPP(FileHandle *fh) : SARA4_PPP(fh) +{ +} + +nsapi_error_t ONBOARD_SARA4_PPP::hard_power_on() +{ + ::onboard_modem_init(); + return NSAPI_ERROR_OK; +} + +nsapi_error_t ONBOARD_SARA4_PPP::hard_power_off() +{ + ::onboard_modem_deinit(); + return NSAPI_ERROR_OK; +} + +nsapi_error_t ONBOARD_SARA4_PPP::soft_power_on() +{ + ::onboard_modem_power_up(); + return NSAPI_ERROR_OK; +} + +nsapi_error_t ONBOARD_SARA4_PPP::soft_power_off() +{ + ::onboard_modem_power_down(); + return NSAPI_ERROR_OK; +} + +CellularDevice *CellularDevice::get_target_default_instance() +{ + static UARTSerial serial(MDMTXD, MDMRXD, 115200); +#if DEVICE_SERIAL_FC + if (MDMRTS != NC && MDMCTS != NC) { + tr_debug("Modem flow control: RTS %d CTS %d", MDMRTS, MDMCTS); + serial.set_flow_control(SerialBase::RTSCTS, MDMRTS, MDMCTS); + } +#endif + static ONBOARD_SARA4_PPP device(&serial); + return &device; +} diff --git a/targets/TARGET_STM/TARGET_STM32L4/TARGET_MTS_DRAGONFLY_L471QG/ONBOARD_SARA4_PPP.h b/targets/TARGET_STM/TARGET_STM32L4/TARGET_MTS_DRAGONFLY_L471QG/ONBOARD_SARA4_PPP.h new file mode 100644 index 00000000000..ef20272bbdd --- /dev/null +++ b/targets/TARGET_STM/TARGET_STM32L4/TARGET_MTS_DRAGONFLY_L471QG/ONBOARD_SARA4_PPP.h @@ -0,0 +1,36 @@ +/* mbed Microcontroller Library + * Copyright (c) 2018 ARM Limited + * + * 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 ONBOARD_SARA4_PPP_ +#define ONBOARD_SARA4_PPP_ + +#include "SARA4_PPP.h" + +namespace mbed { + +class ONBOARD_SARA4_PPP : public SARA4_PPP { +public: + ONBOARD_SARA4_PPP(FileHandle *fh); + + virtual nsapi_error_t hard_power_on(); + virtual nsapi_error_t hard_power_off(); + virtual nsapi_error_t soft_power_on(); + virtual nsapi_error_t soft_power_off(); +}; + +} // namespace mbed + +#endif // ONBOARD_SARA4_PPP_ diff --git a/targets/TARGET_STM/TARGET_STM32L4/TARGET_STM32L486xG/TARGET_MTB_ADV_WISE_1570/ONBOARD_QUECTEL_BC95.cpp b/targets/TARGET_STM/TARGET_STM32L4/TARGET_STM32L486xG/TARGET_MTB_ADV_WISE_1570/ONBOARD_QUECTEL_BC95.cpp new file mode 100644 index 00000000000..4bce3631b0c --- /dev/null +++ b/targets/TARGET_STM/TARGET_STM32L4/TARGET_STM32L486xG/TARGET_MTB_ADV_WISE_1570/ONBOARD_QUECTEL_BC95.cpp @@ -0,0 +1,64 @@ +/* mbed Microcontroller Library + * Copyright (c) 2018 ARM Limited + * + * 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 "ONBOARD_QUECTEL_BC95.h" + +#include "cellular/onboard_modem_api.h" +#include "UARTSerial.h" +#include "CellularLog.h" + +using namespace mbed; + +ONBOARD_QUECTEL_BC95::ONBOARD_QUECTEL_BC95(FileHandle *fh) : QUECTEL_BC95(fh) +{ +} + +nsapi_error_t ONBOARD_QUECTEL_BC95::hard_power_on() +{ + ::onboard_modem_init(); + return NSAPI_ERROR_OK; +} + +nsapi_error_t ONBOARD_QUECTEL_BC95::hard_power_off() +{ + ::onboard_modem_deinit(); + return NSAPI_ERROR_OK; +} + +nsapi_error_t ONBOARD_QUECTEL_BC95::soft_power_on() +{ + ::onboard_modem_power_up(); + return NSAPI_ERROR_OK; +} + +nsapi_error_t ONBOARD_QUECTEL_BC95::soft_power_off() +{ + ::onboard_modem_power_down(); + return NSAPI_ERROR_OK; +} + +CellularDevice *CellularDevice::get_target_default_instance() +{ + static UARTSerial serial(MDMTXD, MDMRXD, 9600); +#if DEVICE_SERIAL_FC + if (MDMRTS != NC && MDMCTS != NC) { + tr_debug("Modem flow control: RTS %d CTS %d", MDMRTS, MDMCTS); + serial.set_flow_control(SerialBase::RTSCTS, MDMRTS, MDMCTS); + } +#endif + static ONBOARD_QUECTEL_BC95 device(&serial); + return &device; +} diff --git a/targets/TARGET_STM/TARGET_STM32L4/TARGET_STM32L486xG/TARGET_MTB_ADV_WISE_1570/ONBOARD_QUECTEL_BC95.h b/targets/TARGET_STM/TARGET_STM32L4/TARGET_STM32L486xG/TARGET_MTB_ADV_WISE_1570/ONBOARD_QUECTEL_BC95.h new file mode 100644 index 00000000000..9d91504727d --- /dev/null +++ b/targets/TARGET_STM/TARGET_STM32L4/TARGET_STM32L486xG/TARGET_MTB_ADV_WISE_1570/ONBOARD_QUECTEL_BC95.h @@ -0,0 +1,36 @@ +/* mbed Microcontroller Library + * Copyright (c) 2018 ARM Limited + * + * 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 ONBOARD_QUECTEL_BC95_ +#define ONBOARD_QUECTEL_BC95_ + +#include "QUECTEL_BC95.h" + +namespace mbed { + +class ONBOARD_QUECTEL_BC95 : public QUECTEL_BC95 { +public: + ONBOARD_QUECTEL_BC95(FileHandle *fh); + + virtual nsapi_error_t hard_power_on(); + virtual nsapi_error_t hard_power_off(); + virtual nsapi_error_t soft_power_on(); + virtual nsapi_error_t soft_power_off(); +}; + +} // namespace mbed + +#endif // ONBOARD_QUECTEL_BC95_