diff --git a/UNITTESTS/CMakeLists.txt b/UNITTESTS/CMakeLists.txt index a50455c689..15dcb0681e 100644 --- a/UNITTESTS/CMakeLists.txt +++ b/UNITTESTS/CMakeLists.txt @@ -119,6 +119,13 @@ set(unittest-includes-base "${PROJECT_SOURCE_DIR}/../features/cellular/framework/AT" "${PROJECT_SOURCE_DIR}/../features/cellular/framework" "${PROJECT_SOURCE_DIR}/../features/cellular/framework/common" + "${PROJECT_SOURCE_DIR}/../features/lorawan" + "${PROJECT_SOURCE_DIR}/../features/lorawan/lorastack" + "${PROJECT_SOURCE_DIR}/../features/lorawan/lorastack/mac" + "${PROJECT_SOURCE_DIR}/../features/lorawan/lorastack/phy" + "${PROJECT_SOURCE_DIR}/../features/lorawan/system" + "${PROJECT_SOURCE_DIR}/../features/mbedtls" + "${PROJECT_SOURCE_DIR}/../features/mbedtls/inc" ) # Create a list for test suites. diff --git a/UNITTESTS/features/cellular/framework/AT/at_cellulardevice/unittest.cmake b/UNITTESTS/features/cellular/framework/AT/at_cellulardevice/unittest.cmake index b3771ae7a3..9330d8a8da 100644 --- a/UNITTESTS/features/cellular/framework/AT/at_cellulardevice/unittest.cmake +++ b/UNITTESTS/features/cellular/framework/AT/at_cellulardevice/unittest.cmake @@ -20,17 +20,17 @@ set(unittest-sources # Test files set(unittest-test-sources features/cellular/framework/AT/at_cellulardevice/at_cellulardevicetest.cpp - 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 - stubs/NetworkInterface_stub.cpp - stubs/EventQueue_stub.cpp - stubs/FileHandle_stub.cpp - stubs/mbed_assert_stub.cpp + 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 + stubs/NetworkInterface_stub.cpp + stubs/EventQueue_stub.cpp + stubs/FileHandle_stub.cpp + stubs/mbed_assert_stub.c stubs/CellularDevice_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 4f2be58fbe..0a6d649e74 100644 --- a/UNITTESTS/features/cellular/framework/AT/at_cellularinformation/at_cellularinformationtest.cpp +++ b/UNITTESTS/features/cellular/framework/AT/at_cellularinformation/at_cellularinformationtest.cpp @@ -60,7 +60,7 @@ TEST_F(TestAT_CellularInformation, test_AT_CellularInformation_get_manufacturer) AT_CellularInformation aci(ah); ATHandler_stub::nsapi_error_value = NSAPI_ERROR_OK; - ATHandler_stub::read_string_value = "some"; + ATHandler_stub::read_string_value = (char *)"some"; ATHandler_stub::ssize_value = 4; char buf[8]; @@ -82,7 +82,7 @@ TEST_F(TestAT_CellularInformation, test_AT_CellularInformation_get_model) AT_CellularInformation aci(ah); ATHandler_stub::nsapi_error_value = NSAPI_ERROR_OK; - ATHandler_stub::read_string_value = "model"; + ATHandler_stub::read_string_value = (char *)"model"; ATHandler_stub::ssize_value = 5; char buf[8]; EXPECT_TRUE(NSAPI_ERROR_OK == aci.get_model(buf, 8)); @@ -105,7 +105,7 @@ TEST_F(TestAT_CellularInformation, test_AT_CellularInformation_get_revision) AT_CellularInformation *aci = new AT_CellularInformation(ah); ATHandler_stub::nsapi_error_value = NSAPI_ERROR_OK; - ATHandler_stub::read_string_value = "revision"; + ATHandler_stub::read_string_value = (char *)"revision"; ATHandler_stub::ssize_value = 8; char buf[9]; @@ -129,7 +129,7 @@ TEST_F(TestAT_CellularInformation, test_AT_CellularInformation_get_serial_number AT_CellularInformation aci(ah); ATHandler_stub::nsapi_error_value = NSAPI_ERROR_OK; - ATHandler_stub::read_string_value = "1234567"; + ATHandler_stub::read_string_value = (char *)"1234567"; ATHandler_stub::ssize_value = 7; char buf[8]; @@ -147,7 +147,7 @@ TEST_F(TestAT_CellularInformation, test_AT_CellularInformation_get_serial_number EXPECT_TRUE(strlen(buf) == 0); ATHandler_stub::nsapi_error_value = NSAPI_ERROR_OK; - ATHandler_stub::read_string_value = "1234567"; + ATHandler_stub::read_string_value = (char *)"1234567"; ATHandler_stub::ssize_value = 7; AT_CellularBase_stub::supported_bool = true; EXPECT_TRUE(NSAPI_ERROR_OK == aci.get_serial_number(buf, 8, CellularInformation::IMEI)); diff --git a/UNITTESTS/features/cellular/framework/AT/at_cellularinformation/unittest.cmake b/UNITTESTS/features/cellular/framework/AT/at_cellularinformation/unittest.cmake index 9a0a7435ce..57748f5e83 100644 --- a/UNITTESTS/features/cellular/framework/AT/at_cellularinformation/unittest.cmake +++ b/UNITTESTS/features/cellular/framework/AT/at_cellularinformation/unittest.cmake @@ -20,9 +20,9 @@ set(unittest-sources # Test files set(unittest-test-sources features/cellular/framework/AT//at_cellularinformation/at_cellularinformationtest.cpp - stubs/ATHandler_stub.cpp - stubs/AT_CellularBase_stub.cpp - stubs/EventQueue_stub.cpp - stubs/FileHandle_stub.cpp - stubs/mbed_assert_stub.cpp + stubs/ATHandler_stub.cpp + stubs/AT_CellularBase_stub.cpp + stubs/EventQueue_stub.cpp + stubs/FileHandle_stub.cpp + stubs/mbed_assert_stub.c ) 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 e54a8d2844..a498db66f8 100644 --- a/UNITTESTS/features/cellular/framework/AT/at_cellularnetwork/at_cellularnetworktest.cpp +++ b/UNITTESTS/features/cellular/framework/AT/at_cellularnetwork/at_cellularnetworktest.cpp @@ -252,7 +252,7 @@ TEST_F(TestAT_CellularNetwork, test_AT_CellularNetwork_activate_context) ATHandler_stub::nsapi_error_value = NSAPI_ERROR_OK; ATHandler_stub::bool_value = false; ATHandler_stub::read_string_index = 1; - ATHandler_stub::read_string_table[0] = ""; + ATHandler_stub::read_string_table[0] = (char *)""; ATHandler_stub::int_value = 1; EXPECT_TRUE(NSAPI_ERROR_UNSUPPORTED == cn.activate_context()); @@ -261,7 +261,7 @@ TEST_F(TestAT_CellularNetwork, test_AT_CellularNetwork_activate_context) ATHandler_stub::nsapi_error_value = NSAPI_ERROR_OK; ATHandler_stub::bool_value = false; ATHandler_stub::read_string_index = 1; - ATHandler_stub::read_string_table[0] = "IPV6"; + ATHandler_stub::read_string_table[0] = (char *)"IPV6"; ATHandler_stub::int_value = 1; EXPECT_TRUE(NSAPI_ERROR_NO_CONNECTION == cn.activate_context()); @@ -270,8 +270,8 @@ TEST_F(TestAT_CellularNetwork, test_AT_CellularNetwork_activate_context) ATHandler_stub::nsapi_error_value = NSAPI_ERROR_OK; ATHandler_stub::bool_value = false; ATHandler_stub::read_string_index = 2; - ATHandler_stub::read_string_table[0] = "internet"; - ATHandler_stub::read_string_table[1] = "IP"; + ATHandler_stub::read_string_table[0] = (char *)"internet"; + ATHandler_stub::read_string_table[1] = (char *)"IP"; ATHandler_stub::int_value = 1; EXPECT_TRUE(NSAPI_ERROR_OK == my_cn.set_credentials("apn", CellularNetwork::CHAP, "user", "passwd")); EXPECT_TRUE(NSAPI_ERROR_OK == my_cn.activate_context()); @@ -281,8 +281,8 @@ TEST_F(TestAT_CellularNetwork, test_AT_CellularNetwork_activate_context) ATHandler_stub::nsapi_error_value = NSAPI_ERROR_OK; ATHandler_stub::bool_value = false; ATHandler_stub::read_string_index = 2; - ATHandler_stub::read_string_table[0] = "internet"; - ATHandler_stub::read_string_table[1] = "IPV4V6"; + ATHandler_stub::read_string_table[0] = (char *)"internet"; + ATHandler_stub::read_string_table[1] = (char *)"IPV4V6"; ATHandler_stub::int_value = 1; EXPECT_TRUE(NSAPI_ERROR_OK == my_cn.set_stack_type(IPV4_STACK)); EXPECT_TRUE(NSAPI_ERROR_OK == my_cn.set_credentials("internet")); @@ -293,8 +293,8 @@ TEST_F(TestAT_CellularNetwork, test_AT_CellularNetwork_activate_context) ATHandler_stub::nsapi_error_value = NSAPI_ERROR_OK; ATHandler_stub::bool_value = false; ATHandler_stub::read_string_index = 2; - ATHandler_stub::read_string_table[0] = "internet"; - ATHandler_stub::read_string_table[1] = "IPV6"; + ATHandler_stub::read_string_table[0] = (char *)"internet"; + ATHandler_stub::read_string_table[1] = (char *)"IPV6"; ATHandler_stub::int_value = 1; EXPECT_TRUE(NSAPI_ERROR_OK == my_cnipv6.set_stack_type(IPV6_STACK)); EXPECT_TRUE(NSAPI_ERROR_OK == my_cnipv6.set_credentials("internet")); @@ -305,8 +305,8 @@ TEST_F(TestAT_CellularNetwork, test_AT_CellularNetwork_activate_context) ATHandler_stub::nsapi_error_value = NSAPI_ERROR_OK; ATHandler_stub::bool_value = false; ATHandler_stub::read_string_index = 2; - ATHandler_stub::read_string_table[0] = "internet"; - ATHandler_stub::read_string_table[1] = "IPV4V6"; + ATHandler_stub::read_string_table[0] = (char *)"internet"; + ATHandler_stub::read_string_table[1] = (char *)"IPV4V6"; ATHandler_stub::int_value = 1; EXPECT_TRUE(NSAPI_ERROR_OK == my_cn.set_stack_type(DEFAULT_STACK)); EXPECT_TRUE(NSAPI_ERROR_OK == my_cn.set_credentials("internet")); @@ -317,8 +317,8 @@ TEST_F(TestAT_CellularNetwork, test_AT_CellularNetwork_activate_context) ATHandler_stub::nsapi_error_value = NSAPI_ERROR_OK; ATHandler_stub::bool_value = false; ATHandler_stub::read_string_index = 2; - ATHandler_stub::read_string_table[0] = "internet"; - ATHandler_stub::read_string_table[1] = "IPV4V6"; + ATHandler_stub::read_string_table[0] = (char *)"internet"; + ATHandler_stub::read_string_table[1] = (char *)"IPV4V6"; ATHandler_stub::int_value = 1; EXPECT_TRUE(NSAPI_ERROR_OK == my_cnipv6.set_stack_type(DEFAULT_STACK)); EXPECT_TRUE(NSAPI_ERROR_OK == my_cnipv6.set_credentials("internet")); @@ -329,8 +329,8 @@ TEST_F(TestAT_CellularNetwork, test_AT_CellularNetwork_activate_context) ATHandler_stub::nsapi_error_value = NSAPI_ERROR_OK; ATHandler_stub::bool_value = false; ATHandler_stub::read_string_index = 2; - ATHandler_stub::read_string_table[0] = "internet"; - ATHandler_stub::read_string_table[1] = "IPV6"; + ATHandler_stub::read_string_table[0] = (char *)"internet"; + ATHandler_stub::read_string_table[1] = (char *)"IPV6"; ATHandler_stub::int_value = 1; EXPECT_TRUE(NSAPI_ERROR_OK == my_cnipv6.set_stack_type(DEFAULT_STACK)); EXPECT_TRUE(NSAPI_ERROR_OK == my_cnipv6.set_credentials("internet")); @@ -341,8 +341,8 @@ TEST_F(TestAT_CellularNetwork, test_AT_CellularNetwork_activate_context) ATHandler_stub::nsapi_error_value = NSAPI_ERROR_OK; ATHandler_stub::bool_value = false; ATHandler_stub::read_string_index = 2; - ATHandler_stub::read_string_table[0] = "internet"; - ATHandler_stub::read_string_table[1] = "IP"; + ATHandler_stub::read_string_table[0] = (char *)"internet"; + ATHandler_stub::read_string_table[1] = (char *)"IP"; ATHandler_stub::int_value = 1; EXPECT_TRUE(NSAPI_ERROR_OK == my_cn.set_stack_type(DEFAULT_STACK)); EXPECT_TRUE(NSAPI_ERROR_OK == my_cn.set_credentials("internet")); @@ -795,9 +795,9 @@ TEST_F(TestAT_CellularNetwork, test_AT_CellularNetwork_scan_plmn) ATHandler_stub::read_string_index = 3; - ATHandler_stub::read_string_table[0] = "44444"; - ATHandler_stub::read_string_table[1] = "33333"; - ATHandler_stub::read_string_table[2] = "12345"; + ATHandler_stub::read_string_table[0] = (char *)"44444"; + ATHandler_stub::read_string_table[1] = (char *)"33333"; + ATHandler_stub::read_string_table[2] = (char *)"12345"; ATHandler_stub::int_value = 1; ATHandler_stub::nsapi_error_value = NSAPI_ERROR_OK; ATHandler_stub::info_elem_true_counter = 1; @@ -922,13 +922,13 @@ TEST_F(TestAT_CellularNetwork, test_AT_CellularNetwork_get_pdpcontext_params) ATHandler_stub::int_valid_count_table[0] = 9; ATHandler_stub::read_string_index = 7; - ATHandler_stub::read_string_table[6] = "internet"; - ATHandler_stub::read_string_table[5] = "1.2.3.4.5.6.7.8.9.10.11.112.13.14.15.16.1.2.3.44.55.6.7.8.9.10.11.12.13.14.15.16"; - ATHandler_stub::read_string_table[4] = "23.33.44.1.2.3.55.123.225.34.11.1.0.0.123.234"; - ATHandler_stub::read_string_table[3] = "1.2.3.4"; - ATHandler_stub::read_string_table[2] = "0.255.0.255"; - ATHandler_stub::read_string_table[1] = "25.66.77.88"; - ATHandler_stub::read_string_table[0] = "004.003.002.001"; + ATHandler_stub::read_string_table[6] = (char *)"internet"; + ATHandler_stub::read_string_table[5] = (char *)"1.2.3.4.5.6.7.8.9.10.11.112.13.14.15.16.1.2.3.44.55.6.7.8.9.10.11.12.13.14.15.16"; + ATHandler_stub::read_string_table[4] = (char *)"23.33.44.1.2.3.55.123.225.34.11.1.0.0.123.234"; + ATHandler_stub::read_string_table[3] = (char *)"1.2.3.4"; + ATHandler_stub::read_string_table[2] = (char *)"0.255.0.255"; + ATHandler_stub::read_string_table[1] = (char *)"25.66.77.88"; + ATHandler_stub::read_string_table[0] = (char *)"004.003.002.001"; EXPECT_TRUE(NSAPI_ERROR_OK == cn.get_pdpcontext_params(list)); CellularNetwork::pdpcontext_params_t *params = list.get_head(); @@ -1005,8 +1005,8 @@ TEST_F(TestAT_CellularNetwork, test_AT_CellularNetwork_get_cell_id) EXPECT_TRUE(id == -1); ATHandler_stub::read_string_index = 2; - ATHandler_stub::read_string_table[1] = "00C3"; - ATHandler_stub::read_string_table[0] = "1234FFC1"; //== cellid and in dec: 305463233 + ATHandler_stub::read_string_table[1] = (char *)"00C3"; + ATHandler_stub::read_string_table[0] = (char *)"1234FFC1"; //== cellid and in dec: 305463233 ATHandler_stub::int_value = 1; // Get registration status to modify cell_id CellularNetwork::RegistrationType type; @@ -1043,7 +1043,7 @@ TEST_F(TestAT_CellularNetwork, test_AT_CellularNetwork_get_operator_params) ATHandler_stub::nsapi_error_value = NSAPI_ERROR_OK; ATHandler_stub::int_value = 0; ATHandler_stub::read_string_index = 1; - ATHandler_stub::read_string_table[0] = "12345"; + ATHandler_stub::read_string_table[0] = (char *)"12345"; EXPECT_TRUE(NSAPI_ERROR_OK == cn.get_operator_params(format, ops)); EXPECT_TRUE(format == 0); EXPECT_TRUE(strcmp(ops.op_long, "12345") == 0); @@ -1054,7 +1054,7 @@ TEST_F(TestAT_CellularNetwork, test_AT_CellularNetwork_get_operator_params) ops.op_long[0] = 0; ATHandler_stub::int_value = 1; ATHandler_stub::read_string_index = 1; - ATHandler_stub::read_string_table[0] = "12345"; + ATHandler_stub::read_string_table[0] = (char *)"12345"; EXPECT_TRUE(NSAPI_ERROR_OK == cn.get_operator_params(format, ops)); EXPECT_TRUE(format == 1); EXPECT_TRUE(strlen(ops.op_long) == 0); @@ -1065,7 +1065,7 @@ TEST_F(TestAT_CellularNetwork, test_AT_CellularNetwork_get_operator_params) ops.op_short[0] = 0; ATHandler_stub::int_value = 2; ATHandler_stub::read_string_index = 1; - ATHandler_stub::read_string_table[0] = "12345"; + ATHandler_stub::read_string_table[0] = (char *)"12345"; EXPECT_TRUE(NSAPI_ERROR_OK == cn.get_operator_params(format, ops)); EXPECT_TRUE(format == 2); EXPECT_TRUE(strlen(ops.op_long) == 0); @@ -1097,8 +1097,8 @@ TEST_F(TestAT_CellularNetwork, test_AT_CellularNetwork_get_operator_names) ATHandler_stub::resp_info_true_counter = 1; ATHandler_stub::bool_value = false; ATHandler_stub::read_string_index = 2; - ATHandler_stub::read_string_table[1] = "12345"; - ATHandler_stub::read_string_table[0] = "56789"; + ATHandler_stub::read_string_table[1] = (char *)"12345"; + ATHandler_stub::read_string_table[0] = (char *)"56789"; ATHandler_stub::nsapi_error_value = NSAPI_ERROR_OK; name_list.delete_all(); EXPECT_TRUE(NSAPI_ERROR_OK == cn.get_operator_names(name_list)); diff --git a/UNITTESTS/features/cellular/framework/AT/at_cellularnetwork/unittest.cmake b/UNITTESTS/features/cellular/framework/AT/at_cellularnetwork/unittest.cmake index 194109d8d2..48f7eae3ca 100644 --- a/UNITTESTS/features/cellular/framework/AT/at_cellularnetwork/unittest.cmake +++ b/UNITTESTS/features/cellular/framework/AT/at_cellularnetwork/unittest.cmake @@ -21,14 +21,14 @@ set(unittest-sources # Test files set(unittest-test-sources features/cellular/framework/AT/at_cellularnetwork/at_cellularnetworktest.cpp - stubs/ATHandler_stub.cpp - stubs/AT_CellularBase_stub.cpp - stubs/EventQueue_stub.cpp - stubs/FileHandle_stub.cpp - stubs/NetworkInterface_stub.cpp - stubs/NetworkStack_stub.cpp - stubs/us_ticker_stub.cpp - stubs/mbed_assert_stub.cpp - stubs/SocketAddress_stub.cpp - stubs/randLIB_stub.cpp + stubs/ATHandler_stub.cpp + stubs/AT_CellularBase_stub.cpp + stubs/EventQueue_stub.cpp + stubs/FileHandle_stub.cpp + stubs/NetworkInterface_stub.cpp + stubs/NetworkStack_stub.cpp + stubs/us_ticker_stub.cpp + stubs/mbed_assert_stub.c + stubs/SocketAddress_stub.cpp + stubs/randLIB_stub.cpp ) diff --git a/UNITTESTS/features/cellular/framework/AT/at_cellularpower/unittest.cmake b/UNITTESTS/features/cellular/framework/AT/at_cellularpower/unittest.cmake index bf177affd9..82dc0e9b38 100644 --- a/UNITTESTS/features/cellular/framework/AT/at_cellularpower/unittest.cmake +++ b/UNITTESTS/features/cellular/framework/AT/at_cellularpower/unittest.cmake @@ -24,5 +24,5 @@ set(unittest-test-sources stubs/EventQueue_stub.cpp stubs/FileHandle_stub.cpp stubs/CellularUtil_stub.cpp - stubs/mbed_assert_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 index eb82bc6c9a..e4fe6a1ce1 100644 --- a/UNITTESTS/features/cellular/framework/AT/at_cellularsim/at_cellularsimtest.cpp +++ b/UNITTESTS/features/cellular/framework/AT/at_cellularsim/at_cellularsimtest.cpp @@ -73,7 +73,7 @@ TEST_F(TestAT_CellularSIM, test_AT_CellularSIM_set_pin) EXPECT_TRUE(NSAPI_ERROR_DEVICE_ERROR == sim.set_pin("12")); ATHandler_stub::nsapi_error_value = NSAPI_ERROR_OK; - ATHandler_stub::read_string_value = "READY"; + ATHandler_stub::read_string_value = (char *)"READY"; ATHandler_stub::ssize_value = 5; EXPECT_TRUE(NSAPI_ERROR_OK == sim.set_pin("12")); @@ -131,22 +131,22 @@ TEST_F(TestAT_CellularSIM, test_AT_CellularSIM_get_sim_state) EXPECT_TRUE(NSAPI_ERROR_OK == sim.get_sim_state(state)); EXPECT_TRUE(CellularSIM::SimStateUnknown == state); - ATHandler_stub::read_string_value = "READY"; + 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 = "SIM PIN"; + 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 = "SIM PUK"; + 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 = "SOME CRAP"; + 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); @@ -161,7 +161,7 @@ TEST_F(TestAT_CellularSIM, test_AT_CellularSIM_get_imsi) char imsi[16]; AT_CellularSIM sim(at); ATHandler_stub::nsapi_error_value = NSAPI_ERROR_OK; - ATHandler_stub::read_string_value = "123456789012345"; + 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); @@ -190,7 +190,7 @@ TEST_F(TestAT_CellularSIM, test_AT_CellularSIM_get_iccid) char buf[16]; AT_CellularSIM sim(at); ATHandler_stub::nsapi_error_value = NSAPI_ERROR_OK; - ATHandler_stub::read_string_value = "123456789012345"; + 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); diff --git a/UNITTESTS/features/cellular/framework/AT/at_cellularsim/unittest.cmake b/UNITTESTS/features/cellular/framework/AT/at_cellularsim/unittest.cmake index d3a6709606..7adbfef871 100644 --- a/UNITTESTS/features/cellular/framework/AT/at_cellularsim/unittest.cmake +++ b/UNITTESTS/features/cellular/framework/AT/at_cellularsim/unittest.cmake @@ -25,5 +25,5 @@ set(unittest-test-sources stubs/FileHandle_stub.cpp stubs/CellularUtil_stub.cpp stubs/us_ticker_stub.cpp - stubs/mbed_assert_stub.cpp + stubs/mbed_assert_stub.c ) diff --git a/UNITTESTS/features/cellular/framework/AT/at_cellularsms/unittest.cmake b/UNITTESTS/features/cellular/framework/AT/at_cellularsms/unittest.cmake index e9816c2ea3..616b49e49a 100644 --- a/UNITTESTS/features/cellular/framework/AT/at_cellularsms/unittest.cmake +++ b/UNITTESTS/features/cellular/framework/AT/at_cellularsms/unittest.cmake @@ -25,6 +25,6 @@ set(unittest-test-sources stubs/FileHandle_stub.cpp stubs/CellularUtil_stub.cpp stubs/us_ticker_stub.cpp - stubs/mbed_assert_stub.cpp + stubs/mbed_assert_stub.c stubs/mbed_wait_api_stub.cpp ) diff --git a/UNITTESTS/features/cellular/framework/AT/at_cellularstack/unittest.cmake b/UNITTESTS/features/cellular/framework/AT/at_cellularstack/unittest.cmake index 5087e2f82e..2f1a243e02 100644 --- a/UNITTESTS/features/cellular/framework/AT/at_cellularstack/unittest.cmake +++ b/UNITTESTS/features/cellular/framework/AT/at_cellularstack/unittest.cmake @@ -27,5 +27,5 @@ set(unittest-test-sources stubs/us_ticker_stub.cpp stubs/NetworkStack_stub.cpp stubs/SocketAddress_stub.cpp - stubs/mbed_assert_stub.cpp + stubs/mbed_assert_stub.c ) diff --git a/UNITTESTS/features/cellular/framework/AT/athandler/unittest.cmake b/UNITTESTS/features/cellular/framework/AT/athandler/unittest.cmake index 6fbe35d4aa..25f8f91944 100644 --- a/UNITTESTS/features/cellular/framework/AT/athandler/unittest.cmake +++ b/UNITTESTS/features/cellular/framework/AT/athandler/unittest.cmake @@ -25,7 +25,7 @@ set(unittest-test-sources stubs/FileHandle_stub.cpp stubs/us_ticker_stub.cpp stubs/mbed_wait_api_stub.cpp - stubs/mbed_assert_stub.cpp + stubs/mbed_assert_stub.c stubs/mbed_poll_stub.cpp stubs/Timer_stub.cpp stubs/equeue_stub.c diff --git a/UNITTESTS/features/lorawan/loramac/Test_LoRaMac.cpp b/UNITTESTS/features/lorawan/loramac/Test_LoRaMac.cpp new file mode 100644 index 0000000000..4999da0f1d --- /dev/null +++ b/UNITTESTS/features/lorawan/loramac/Test_LoRaMac.cpp @@ -0,0 +1,560 @@ +/* + * 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 "LoRaMac.h" +#include "LoRaPHY_stub.h" +#include "LoRaMacCrypto_stub.h" +#include "LoRaMacCommand_stub.h" +#include "LoRaWANTimer_stub.h" +#include "EventQueue_stub.h" + +using namespace events; + +class my_phy : public LoRaPHY +{ +public: + my_phy(){}; + + virtual ~my_phy(){}; +}; + +class Test_LoRaMac : public testing::Test { +protected: + LoRaMac *object; + + virtual void SetUp() + { + object = new LoRaMac(); + } + + virtual void TearDown() + { + delete object; + } +}; + +TEST_F(Test_LoRaMac, constructor) +{ + EXPECT_TRUE(object); +} + +void my_cb() +{ + +} + +TEST_F(Test_LoRaMac, initialize) +{ + my_phy phy; + object->bind_phy(phy); + + lorawan_connect_t conn; + uint8_t key[16]; + conn.connection_u.otaa.app_key = key; + conn.connection_u.otaa.app_eui = key; + conn.connection_u.otaa.dev_eui = key; + conn.connection_u.otaa.nb_trials = 2; + object->prepare_join(&conn, true); + + LoRaWANTimer_stub::call_cb_immediately = true; + EXPECT_TRUE(LORAWAN_STATUS_OK == object->initialize(NULL, my_cb)); +} + +TEST_F(Test_LoRaMac, disconnect) +{ + object->disconnect(); +} + +TEST_F(Test_LoRaMac, nwk_joined) +{ + EXPECT_EQ(false, object->nwk_joined()); +} + +TEST_F(Test_LoRaMac, add_channel_plan) +{ + lorawan_channelplan_t plan; + EXPECT_EQ(LORAWAN_STATUS_OK, object->add_channel_plan(plan)); + + object->set_tx_ongoing(true); + EXPECT_EQ(LORAWAN_STATUS_BUSY, object->add_channel_plan(plan)); +} + +TEST_F(Test_LoRaMac, remove_channel_plan) +{ + EXPECT_EQ(LORAWAN_STATUS_OK, object->remove_channel_plan()); + + object->set_tx_ongoing(true); + EXPECT_EQ(LORAWAN_STATUS_BUSY, object->remove_channel_plan()); +} + +TEST_F(Test_LoRaMac, get_channel_plan) +{ + lorawan_channelplan_t plan; + EXPECT_EQ(LORAWAN_STATUS_OK, object->get_channel_plan(plan)); +} + +TEST_F(Test_LoRaMac, remove_single_channel) +{ + EXPECT_EQ(LORAWAN_STATUS_OK, object->remove_single_channel(1)); + + object->set_tx_ongoing(true); + EXPECT_EQ(LORAWAN_STATUS_BUSY, object->remove_single_channel(1)); +} + +TEST_F(Test_LoRaMac, multicast_channel_link) +{ + multicast_params_t p; + + EXPECT_EQ(LORAWAN_STATUS_PARAMETER_INVALID, object->multicast_channel_link(NULL)); + + object->set_tx_ongoing(true); + EXPECT_EQ(LORAWAN_STATUS_BUSY, object->multicast_channel_link(&p)); + + object->set_tx_ongoing(false); + EXPECT_EQ(LORAWAN_STATUS_OK, object->multicast_channel_link(&p)); +} + +TEST_F(Test_LoRaMac, multicast_channel_unlink) +{ + multicast_params_t p; + + EXPECT_EQ(LORAWAN_STATUS_PARAMETER_INVALID, object->multicast_channel_unlink(NULL)); + + object->set_tx_ongoing(true); + EXPECT_EQ(LORAWAN_STATUS_BUSY, object->multicast_channel_unlink(&p)); + + object->set_tx_ongoing(false); + EXPECT_EQ(LORAWAN_STATUS_OK, object->multicast_channel_unlink(&p)); +} + +TEST_F(Test_LoRaMac, send) +{ + loramac_mhdr_t mac_hdr; + uint8_t buf[15]; + mac_hdr.bits.mtype = FRAME_TYPE_DATA_CONFIRMED_UP; + object->send(&mac_hdr, 1, buf, 15); +} + +TEST_F(Test_LoRaMac, get_default_tx_datarate) +{ + object->get_default_tx_datarate(); +} + +TEST_F(Test_LoRaMac, enable_adaptive_datarate) +{ + object->enable_adaptive_datarate(true); +} + +TEST_F(Test_LoRaMac, set_channel_data_rate) +{ + object->set_channel_data_rate(8); +} + +TEST_F(Test_LoRaMac, tx_ongoing) +{ + object->tx_ongoing(); +} + +TEST_F(Test_LoRaMac, set_tx_ongoing) +{ + object->set_tx_ongoing(true); +} + +TEST_F(Test_LoRaMac, reset_ongoing_tx) +{ + object->reset_ongoing_tx(true); +} + +TEST_F(Test_LoRaMac, prepare_ongoing_tx) +{ + uint8_t buf[16]; + object->prepare_ongoing_tx(1, buf, 16, 1, 0); +} + +TEST_F(Test_LoRaMac, send_ongoing_tx) +{ + object->send_ongoing_tx(); +} + +TEST_F(Test_LoRaMac, get_device_class) +{ + object->get_device_class(); +} + +void exp_cb() +{ + +} + +TEST_F(Test_LoRaMac, set_device_class) +{ + object->set_device_class(CLASS_B, exp_cb); +} + +TEST_F(Test_LoRaMac, setup_link_check_request) +{ + object->setup_link_check_request(); +} + +TEST_F(Test_LoRaMac, prepare_join) +{ + lorawan_connect_t conn; + object->prepare_join(&conn, false); + + my_phy phy; + object->bind_phy(phy); + EXPECT_EQ(LORAWAN_STATUS_OK, object->join(false)); + + uint8_t key[16]; + conn.connection_u.otaa.app_key = NULL; + conn.connection_u.otaa.app_eui = NULL; + conn.connection_u.otaa.dev_eui = NULL; + conn.connection_u.otaa.nb_trials = 0; + EXPECT_EQ(LORAWAN_STATUS_PARAMETER_INVALID, object->prepare_join(&conn, true)); + + conn.connection_u.otaa.app_key = key; + conn.connection_u.otaa.app_eui = NULL; + conn.connection_u.otaa.dev_eui = NULL; + conn.connection_u.otaa.nb_trials = 0; + EXPECT_EQ(LORAWAN_STATUS_PARAMETER_INVALID, object->prepare_join(&conn, true)); + + conn.connection_u.otaa.app_key = key; + conn.connection_u.otaa.app_eui = key; + conn.connection_u.otaa.dev_eui = NULL; + conn.connection_u.otaa.nb_trials = 0; + EXPECT_EQ(LORAWAN_STATUS_PARAMETER_INVALID, object->prepare_join(&conn, true)); + + conn.connection_u.otaa.app_key = key; + conn.connection_u.otaa.app_eui = key; + conn.connection_u.otaa.dev_eui = key; + conn.connection_u.otaa.nb_trials = 0; + EXPECT_EQ(LORAWAN_STATUS_PARAMETER_INVALID, object->prepare_join(&conn, true)); + + LoRaPHY_stub::bool_table[0] = false; + LoRaPHY_stub::bool_counter = 0; + conn.connection_u.otaa.app_key = key; + conn.connection_u.otaa.app_eui = key; + conn.connection_u.otaa.dev_eui = key; + conn.connection_u.otaa.nb_trials = 2; + EXPECT_EQ(LORAWAN_STATUS_OK, object->prepare_join(&conn, true)); + + conn.connection_u.abp.dev_addr = 0; + conn.connection_u.abp.nwk_id = 0; + conn.connection_u.abp.nwk_skey = NULL; + conn.connection_u.abp.app_skey = NULL; + EXPECT_EQ(LORAWAN_STATUS_PARAMETER_INVALID, object->prepare_join(&conn, false)); + + conn.connection_u.abp.dev_addr = 1; + conn.connection_u.abp.nwk_id = 0; + conn.connection_u.abp.nwk_skey = NULL; + conn.connection_u.abp.app_skey = NULL; + EXPECT_EQ(LORAWAN_STATUS_PARAMETER_INVALID, object->prepare_join(&conn, false)); + + conn.connection_u.abp.dev_addr = 1; + conn.connection_u.abp.nwk_id = 2; + conn.connection_u.abp.nwk_skey = NULL; + conn.connection_u.abp.app_skey = NULL; + EXPECT_EQ(LORAWAN_STATUS_PARAMETER_INVALID, object->prepare_join(&conn, false)); + + conn.connection_u.abp.dev_addr = 1; + conn.connection_u.abp.nwk_id = 2; + conn.connection_u.abp.nwk_skey = key; + conn.connection_u.abp.app_skey = NULL; + EXPECT_EQ(LORAWAN_STATUS_PARAMETER_INVALID, object->prepare_join(&conn, false)); + + conn.connection_u.abp.dev_addr = 1; + conn.connection_u.abp.nwk_id = 2; + conn.connection_u.abp.nwk_skey = key; + conn.connection_u.abp.app_skey = key; + EXPECT_EQ(LORAWAN_STATUS_OK, object->prepare_join(&conn, false)); + + EXPECT_EQ(LORAWAN_STATUS_OK, object->prepare_join(NULL, false)); +} + +TEST_F(Test_LoRaMac, join) +{ + my_phy phy; + object->bind_phy(phy); + EXPECT_EQ(LORAWAN_STATUS_OK, object->join(false)); + + lorawan_connect_t conn; + uint8_t key[16]; + conn.connection_u.otaa.app_key = key; + conn.connection_u.otaa.app_eui = key; + conn.connection_u.otaa.dev_eui = key; + conn.connection_u.otaa.nb_trials = 2; + object->prepare_join(&conn, true); + EXPECT_EQ(LORAWAN_STATUS_CONNECT_IN_PROGRESS, object->join(true)); +} + +TEST_F(Test_LoRaMac, on_radio_tx_done) +{ + my_phy phy; + object->bind_phy(phy); + object->on_radio_tx_done(100); +} + +TEST_F(Test_LoRaMac, on_radio_rx_done) +{ + uint8_t buf[16]; + object->on_radio_rx_done(buf, 16, 0, 0); +} + +TEST_F(Test_LoRaMac, on_radio_tx_timeout) +{ + object->on_radio_tx_timeout(); +} + +TEST_F(Test_LoRaMac, on_radio_rx_timeout) +{ + object->on_radio_rx_timeout(true); +} + +TEST_F(Test_LoRaMac, continue_joining_process) +{ + my_phy phy; + object->bind_phy(phy); + lorawan_connect_t conn; + uint8_t key[16]; + conn.connection_u.otaa.app_key = key; + conn.connection_u.otaa.app_eui = key; + conn.connection_u.otaa.dev_eui = key; + conn.connection_u.otaa.nb_trials = 2; + object->prepare_join(&conn, true); + object->continue_joining_process(); +} + +TEST_F(Test_LoRaMac, continue_sending_process) +{ + my_phy phy; + object->bind_phy(phy); + object->continue_sending_process(); +} + +TEST_F(Test_LoRaMac, get_mcps_confirmation) +{ + object->get_mcps_confirmation(); +} + +TEST_F(Test_LoRaMac, get_mcps_indication) +{ + object->get_mcps_indication(); +} + +TEST_F(Test_LoRaMac, get_mlme_confirmation) +{ + object->get_mlme_confirmation(); +} + +TEST_F(Test_LoRaMac, get_mlme_indication) +{ + object->get_mlme_indication(); +} + +TEST_F(Test_LoRaMac, post_process_mcps_req) +{ + uint8_t data[16]; + LoRaPHY_stub::bool_counter = 0; + LoRaPHY_stub::bool_table[0] = true; + + my_phy phy; + object->bind_phy(phy); + object->join(false); + + object->prepare_ongoing_tx(1, data, 15, 0x01, 2); + object->send_ongoing_tx(); + object->post_process_mcps_req(); + + LoRaPHY_stub::bool_counter = 0; + object->prepare_ongoing_tx(1, data, 15, 0x02, 2); + object->send_ongoing_tx(); + object->post_process_mcps_req(); + + //_mcps_confirmation.ack_received missing here + uint8_t payload[16] = {}; + LoRaPHY_stub::uint16_value = 5; + payload[0] = FRAME_TYPE_DATA_CONFIRMED_DOWN << 5; + payload[5] = 1 << 5; + + //address != _params.dev_addr + payload[2] = 2; + object->on_radio_rx_done(payload, 16, 0, 0); + object->post_process_mcps_req(); + + payload[2] = 0; + //mic failure + payload[13] = 2; + object->on_radio_rx_done(payload, 16, 0, 0); + object->post_process_mcps_req(); + + payload[13] = 0; + //crypto failure + LoRaMacCrypto_stub::int_table_idx_value = 0; + LoRaMacCrypto_stub::int_table[0] = 4; + LoRaMacCrypto_stub::int_table[1] = 4; +// LoRaPHY_stub::uint16_value = 0; + object->on_radio_rx_done(payload, 16, 0, 0); + object->post_process_mcps_req(); + + //process_mac_commands failure + LoRaMacCommand_stub::status_value = LORAWAN_STATUS_BUSY; + LoRaMacCrypto_stub::int_table[0] = 0; + LoRaMacCrypto_stub::int_table[1] = 0; + payload[7] = 1; + object->on_radio_rx_done(payload, 16, 0, 0); + object->post_process_mcps_req(); + + //FOpts_len != 0 + payload[5] = (1 << 5) + 1; + payload[7] = 0; + LoRaMacCommand_stub::status_value = LORAWAN_STATUS_OK; + payload[0] = FRAME_TYPE_DATA_UNCONFIRMED_DOWN << 5; + + object->on_radio_rx_done(payload, 13, 0, 0); + + //_mac_commands.process_mac_commands fails + LoRaMacCommand_stub::status_value = LORAWAN_STATUS_DATARATE_INVALID; + object->on_radio_rx_done(payload, 13, 0, 0); + + object->post_process_mcps_req(); + + payload[9] = 1; + LoRaMacCommand_stub::status_value = LORAWAN_STATUS_OK; + payload[0] = FRAME_TYPE_PROPRIETARY << 5; + object->on_radio_rx_done(payload, 16, 0, 0); + object->post_process_mcps_req(); + + payload[9] = 0; + payload[5] = 1 << 5; + LoRaMacCommand_stub::status_value = LORAWAN_STATUS_OK; + object->on_radio_rx_done(payload, 16, 0, 0); + object->post_process_mcps_req(); + + LoRaPHY_stub::bool_counter = 0; + object->prepare_ongoing_tx(1, data, 15, 0x04, 2); + object->send_ongoing_tx(); + object->post_process_mcps_req(); + + LoRaPHY_stub::bool_counter = 0; + object->prepare_ongoing_tx(1, data, 15, 0x08, 2); + object->send_ongoing_tx(); + object->post_process_mcps_req(); +} + +TEST_F(Test_LoRaMac, handle_join_accept_frame) +{ + LoRaPHY_stub::bool_counter = 0; + LoRaPHY_stub::bool_table[0] = true; + + my_phy phy; + object->bind_phy(phy); + + uint8_t payload[16] = {}; + LoRaPHY_stub::uint16_value = 5; + payload[0] = FRAME_TYPE_JOIN_ACCEPT << 5; + payload[5] = 1 << 5; + + LoRaMacCrypto_stub::int_table_idx_value = 0; + LoRaMacCrypto_stub::int_table[0] = 4; + LoRaMacCrypto_stub::int_table[1] = 4; + LoRaMacCrypto_stub::int_table[2] = 4; + LoRaMacCrypto_stub::int_table[3] = 4; + object->on_radio_rx_done(payload, 16, 0, 0); + + LoRaMacCrypto_stub::int_table_idx_value = 0; + LoRaMacCrypto_stub::int_table[0] = 0; + object->on_radio_rx_done(payload, 16, 0, 0); + + LoRaMacCrypto_stub::int_table_idx_value = 0; + LoRaMacCrypto_stub::int_table[1] = 0; + object->on_radio_rx_done(payload, 16, 0, 0); + + //mic failure case + payload[13] = 17; + LoRaMacCrypto_stub::int_table_idx_value = 0; + object->on_radio_rx_done(payload, 16, 0, 0); + + payload[13] = 0; + LoRaMacCrypto_stub::int_table_idx_value = 0; + LoRaMacCrypto_stub::int_table[2] = 0; + object->on_radio_rx_done(payload, 16, 0, 0); +} + +TEST_F(Test_LoRaMac, post_process_mcps_ind) +{ + object->post_process_mcps_ind(); +} + +TEST_F(Test_LoRaMac, post_process_mlme_request) +{ + object->post_process_mlme_request(); +} + +TEST_F(Test_LoRaMac, post_process_mlme_ind) +{ + object->post_process_mlme_ind(); +} + +uint8_t batt_cb() +{ + +} + +TEST_F(Test_LoRaMac, set_batterylevel_callback) +{ + object->set_batterylevel_callback(batt_cb); +} + +TEST_F(Test_LoRaMac, get_backoff_timer_event_id) +{ + object->get_backoff_timer_event_id(); +} + +TEST_F(Test_LoRaMac, clear_tx_pipe) +{ + EXPECT_EQ(LORAWAN_STATUS_OK, object->clear_tx_pipe()); //timer id == 0 + + my_phy phy; + object->bind_phy(phy); + + lorawan_connect_t conn; + uint8_t key[16]; + conn.connection_u.otaa.app_key = key; + conn.connection_u.otaa.app_eui = key; + conn.connection_u.otaa.dev_eui = key; + conn.connection_u.otaa.nb_trials = 2; + object->prepare_join(&conn, true); + + EXPECT_TRUE(LORAWAN_STATUS_OK == object->initialize(NULL, my_cb)); + EventQueue_stub::int_value = 0; + EXPECT_EQ(LORAWAN_STATUS_BUSY, object->clear_tx_pipe()); + + EventQueue_stub::int_value = 1; + EXPECT_EQ(LORAWAN_STATUS_OK, object->clear_tx_pipe()); +} + +TEST_F(Test_LoRaMac, get_current_time) +{ + object->get_current_time(); +} + +TEST_F(Test_LoRaMac, get_current_slot) +{ + object->get_current_slot(); +} + diff --git a/UNITTESTS/features/lorawan/loramac/unittest.cmake b/UNITTESTS/features/lorawan/loramac/unittest.cmake new file mode 100644 index 0000000000..aff97c0412 --- /dev/null +++ b/UNITTESTS/features/lorawan/loramac/unittest.cmake @@ -0,0 +1,58 @@ +#[[ + * 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. +]] + +# Unit test suite name +set(TEST_SUITE_NAME "lorawan_LoRaMac") + +# Source files +set(unittest-sources + ../features/lorawan/lorastack/mac/LoRaMac.cpp +) + +# Add test specific include paths +set(unittest-includes ${unittest-includes} + target_h + ../features/lorawan/lorastack/mac +) + +# Test & stub files +set(unittest-test-sources + features/lorawan/loramac/Test_LoRaMac.cpp + stubs/LoRaPHY_stub.cpp + stubs/LoRaWANStack_stub.cpp + stubs/mbed_assert_stub.c + stubs/LoRaMacCrypto_stub.cpp + stubs/LoRaMacChannelPlan_stub.cpp + stubs/LoRaWANTimer_stub.cpp + stubs/LoRaMacCommand_stub.cpp + stubs/EventQueue_stub.cpp + +) + +# defines +set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DMBED_CONF_LORA_ADR_ON=true -DMBED_CONF_LORA_PUBLIC_NETWORK=true -DMBED_CONF_LORA_NB_TRIALS=2 -DMBED_CONF_LORA_DOWNLINK_PREAMBLE_LENGTH=5") +set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DMBED_CONF_LORA_ADR_ON=true -DMBED_CONF_LORA_PUBLIC_NETWORK=true -DMBED_CONF_LORA_NB_TRIALS=2 -DMBED_CONF_LORA_DOWNLINK_PREAMBLE_LENGTH=5") + +set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DMBED_CONF_LORA_DUTY_CYCLE_ON=true -DMBED_CONF_LORA_MAX_SYS_RX_ERROR=10 -DMBED_CONF_LORA_NWKSKEY=\"{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}\"") +set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DMBED_CONF_LORA_DUTY_CYCLE_ON=true -DMBED_CONF_LORA_MAX_SYS_RX_ERROR=10 -DMBED_CONF_LORA_NWKSKEY=\"{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}\"") + +set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DMBED_CONF_LORA_APPSKEY=\"{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}\"") +set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DMBED_CONF_LORA_APPSKEY=\"{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}\"") + +set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DMBED_CONF_LORA_DEVICE_ADDRESS=\"0x00000000\"") +set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DMBED_CONF_LORA_DEVICE_ADDRESS=\"0x00000000\"") + diff --git a/UNITTESTS/features/lorawan/loramacchannelplan/Test_LoRaMacChannelPlan.cpp b/UNITTESTS/features/lorawan/loramacchannelplan/Test_LoRaMacChannelPlan.cpp new file mode 100644 index 0000000000..123d9830a8 --- /dev/null +++ b/UNITTESTS/features/lorawan/loramacchannelplan/Test_LoRaMacChannelPlan.cpp @@ -0,0 +1,168 @@ +/* + * 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 "LoRaMacChannelPlan.h" +#include "LoRaPHY_stub.h" +#include "LoRaPHY.h" + +class my_LoRaPHY : public LoRaPHY +{ +public: + my_LoRaPHY(){}; + + virtual ~my_LoRaPHY(){}; +}; + + +class Test_LoRaMacChannelPlan : public testing::Test { +protected: + LoRaMacChannelPlan *object; + my_LoRaPHY phy; + + virtual void SetUp() + { + object = new LoRaMacChannelPlan(); + object->activate_channelplan_subsystem(&phy); + + LoRaPHY_stub::uint8_value = 0; + LoRaPHY_stub::bool_counter = 0; + LoRaPHY_stub::lorawan_status_value = LORAWAN_STATUS_OK; + LoRaPHY_stub::uint16_value = 0; + memcpy(LoRaPHY_stub::bool_table, "0", 20); + } + + virtual void TearDown() + { + delete object; + } +}; + +TEST_F(Test_LoRaMacChannelPlan, constructor) +{ + EXPECT_TRUE(object); +} + +TEST_F(Test_LoRaMacChannelPlan, set_plan) +{ + lorawan_channelplan_t plan; + LoRaPHY_stub::bool_counter = 0; + LoRaPHY_stub::bool_table[0] = false; + EXPECT_TRUE(object->set_plan(plan) == LORAWAN_STATUS_SERVICE_UNKNOWN); + + plan.nb_channels = 1; + LoRaPHY_stub::bool_counter = 0; + LoRaPHY_stub::bool_table[0] = true; + LoRaPHY_stub::uint8_value = 0; + EXPECT_TRUE(object->set_plan(plan) == LORAWAN_STATUS_PARAMETER_INVALID); + + LoRaPHY_stub::bool_counter = 0; + LoRaPHY_stub::bool_table[0] = true; + LoRaPHY_stub::uint8_value = 10; + LoRaPHY_stub::lorawan_status_value = LORAWAN_STATUS_PARAMETER_INVALID; + EXPECT_TRUE(object->set_plan(plan) == LORAWAN_STATUS_PARAMETER_INVALID); + + LoRaPHY_stub::bool_counter = 0; + LoRaPHY_stub::bool_table[0] = true; + plan.nb_channels = 2; + LoRaPHY_stub::lorawan_status_value = LORAWAN_STATUS_OK; + EXPECT_TRUE(object->set_plan(plan) == LORAWAN_STATUS_OK); +} + +TEST_F(Test_LoRaMacChannelPlan, get_plan) +{ + lorawan_channelplan_t plan; + channel_params_t params; + LoRaPHY_stub::bool_counter = 0; + LoRaPHY_stub::bool_table[0] = false; + EXPECT_TRUE(object->get_plan(plan, ¶ms) == LORAWAN_STATUS_SERVICE_UNKNOWN); + + LoRaPHY_stub::bool_counter = 0; + LoRaPHY_stub::bool_table[0] = true; + LoRaPHY_stub::bool_table[1] = false; + + LoRaPHY_stub::uint8_value = 1; + LoRaPHY_stub::uint16_value = 0xABCD; + EXPECT_TRUE(object->get_plan(plan, ¶ms) == LORAWAN_STATUS_OK); + + LoRaPHY_stub::bool_counter = 0; + LoRaPHY_stub::bool_table[0] = true; + LoRaPHY_stub::bool_table[1] = true; + LoRaPHY_stub::bool_table[2] = false; + loramac_channel_t ch; + plan.channels = &ch; + EXPECT_TRUE(object->get_plan(plan, ¶ms) == LORAWAN_STATUS_OK); +} + +TEST_F(Test_LoRaMacChannelPlan, remove_plan) +{ + LoRaPHY_stub::bool_counter = 0; + LoRaPHY_stub::bool_table[0] = false; + EXPECT_TRUE(object->remove_plan() == LORAWAN_STATUS_SERVICE_UNKNOWN); + + LoRaPHY_stub::uint8_value = 4; + LoRaPHY_stub::bool_counter = 0; + LoRaPHY_stub::bool_table[0] = true; + LoRaPHY_stub::bool_table[1] = true; //first continue + LoRaPHY_stub::bool_table[2] = false; + LoRaPHY_stub::bool_table[3] = false;//second continue + LoRaPHY_stub::bool_table[4] = false; + LoRaPHY_stub::bool_table[5] = true; + LoRaPHY_stub::bool_table[6] = false;//false for remove_single_channel(i) + + EXPECT_TRUE(object->remove_plan() == LORAWAN_STATUS_SERVICE_UNKNOWN); + + LoRaPHY_stub::uint8_value = 3; + LoRaPHY_stub::bool_counter = 0; + LoRaPHY_stub::bool_table[0] = true; + LoRaPHY_stub::bool_table[1] = false; + LoRaPHY_stub::bool_table[2] = true; + LoRaPHY_stub::bool_table[3] = true; + LoRaPHY_stub::bool_table[4] = true; + LoRaPHY_stub::bool_table[5] = true; + LoRaPHY_stub::bool_table[7] = true; + LoRaPHY_stub::bool_table[8] = true; + LoRaPHY_stub::bool_table[9] = true; + LoRaPHY_stub::bool_table[10] = true; + + EXPECT_TRUE(object->remove_plan() == LORAWAN_STATUS_OK); + +} + +TEST_F(Test_LoRaMacChannelPlan, remove_single_channel) +{ + LoRaPHY_stub::bool_counter = 0; + LoRaPHY_stub::bool_table[0] = false; + EXPECT_TRUE(object->remove_single_channel(4) == LORAWAN_STATUS_SERVICE_UNKNOWN); + + LoRaPHY_stub::uint8_value = 2; + LoRaPHY_stub::bool_counter = 0; + LoRaPHY_stub::bool_table[0] = true; + + EXPECT_TRUE(object->remove_single_channel(4) == LORAWAN_STATUS_PARAMETER_INVALID); + + LoRaPHY_stub::bool_counter = 0; + LoRaPHY_stub::bool_table[0] = true; + LoRaPHY_stub::bool_table[1] = false; + EXPECT_TRUE(object->remove_single_channel(1) == LORAWAN_STATUS_PARAMETER_INVALID); + + LoRaPHY_stub::bool_counter = 0; + LoRaPHY_stub::bool_table[0] = true; + LoRaPHY_stub::bool_table[1] = true; + EXPECT_TRUE(object->remove_single_channel(1) == LORAWAN_STATUS_OK); +} + diff --git a/UNITTESTS/features/lorawan/loramacchannelplan/unittest.cmake b/UNITTESTS/features/lorawan/loramacchannelplan/unittest.cmake new file mode 100644 index 0000000000..1f15bf09ed --- /dev/null +++ b/UNITTESTS/features/lorawan/loramacchannelplan/unittest.cmake @@ -0,0 +1,39 @@ +#[[ + * 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. +]] + +# Unit test suite name +set(TEST_SUITE_NAME "lorawan_LoRaMacChannelPlan") + +# Source files +set(unittest-sources + ../features/lorawan/lorastack/mac/LoRaMacChannelPlan.cpp +) + +# Add test specific include paths +set(unittest-includes ${unittest-includes} + target_h + ../features/lorawan/lorastack/mac +) + +# Test & stub files +set(unittest-test-sources + features/lorawan/loramacchannelplan/Test_LoRaMacChannelPlan.cpp + stubs/LoRaPHY_stub.cpp +) + +set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DMBED_CONF_LORA_TX_MAX_SIZE=255") +set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DMBED_CONF_LORA_TX_MAX_SIZE=255") diff --git a/UNITTESTS/features/lorawan/loramaccommand/Test_LoRaMacCommand.cpp b/UNITTESTS/features/lorawan/loramaccommand/Test_LoRaMacCommand.cpp new file mode 100644 index 0000000000..3417a5eba3 --- /dev/null +++ b/UNITTESTS/features/lorawan/loramaccommand/Test_LoRaMacCommand.cpp @@ -0,0 +1,353 @@ +/* + * 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 "LoRaMacCommand.h" + +#include "LoRaPHY_stub.h" + +class my_LoRaPHY : public LoRaPHY +{ +public: + my_LoRaPHY(){}; + + virtual ~my_LoRaPHY(){}; +}; + +uint8_t my_cb() +{ + return 1; +} + +class Test_LoRaMacCommand : public testing::Test { +protected: + LoRaMacCommand *object; + + virtual void SetUp() + { + object = new LoRaMacCommand(); + } + + virtual void TearDown() + { + delete object; + } +}; + +TEST_F(Test_LoRaMacCommand, constructor) +{ + EXPECT_TRUE(object); +} + +TEST_F(Test_LoRaMacCommand, get_mac_cmd_length) +{ + object->add_link_check_req(); + EXPECT_TRUE(object->get_mac_cmd_length() == 1); + object->clear_command_buffer(); + EXPECT_TRUE(object->get_mac_cmd_length() == 0); +} + +TEST_F(Test_LoRaMacCommand, parse_mac_commands_to_repeat) +{ + loramac_mlme_confirm_t mlme; + lora_mac_system_params_t params; + my_LoRaPHY phy; + uint8_t buf[20]; + + object->parse_mac_commands_to_repeat(); + + buf[0] = 2; + buf[1] = 16; + buf[2] = 32; + EXPECT_TRUE(object->process_mac_commands(buf, 0, 3, 0, mlme, params, phy) == LORAWAN_STATUS_OK); + + buf[0] = 3; + LoRaPHY_stub::uint8_value = 7; + LoRaPHY_stub::linkAdrNbBytesParsed = 5; + EXPECT_TRUE(object->process_mac_commands(buf, 0, 5, 0, mlme, params, phy) == LORAWAN_STATUS_OK); + + buf[0] = 4; + buf[1] = 2; + EXPECT_TRUE(object->process_mac_commands(buf, 0, 1, 0, mlme, params, phy) == LORAWAN_STATUS_OK); + + buf[0] = 5; + buf[1] = 2; + buf[2] = 2; + buf[3] = 2; + buf[4] = 2; + buf[5] = 2; + EXPECT_TRUE(object->process_mac_commands(buf, 0, 5, 0, mlme, params, phy) == LORAWAN_STATUS_OK); + + buf[0] = 6; + object->set_batterylevel_callback(my_cb); + EXPECT_TRUE(object->process_mac_commands(buf, 0, 1, 0, mlme, params, phy) == LORAWAN_STATUS_OK); + + buf[0] = 7; + buf[1] = 2; + buf[2] = 2; + buf[3] = 2; + buf[4] = 2; + buf[5] = 2; + EXPECT_TRUE(object->process_mac_commands(buf, 0, 6, 0, mlme, params, phy) == LORAWAN_STATUS_OK); + + buf[0] = 8; + buf[1] = 0; + EXPECT_TRUE(object->process_mac_commands(buf, 0, 2, 0, mlme, params, phy) == LORAWAN_STATUS_OK); + + buf[0] = 9; + buf[1] = 48; + EXPECT_TRUE(object->process_mac_commands(buf, 0, 2, 0, mlme, params, phy) == LORAWAN_STATUS_OK); + + buf[0] = 10; + buf[1] = 2; + buf[1] = 3; + buf[1] = 4; + EXPECT_TRUE(object->process_mac_commands(buf, 0, 4, 0, mlme, params, phy) == LORAWAN_STATUS_OK); + + object->parse_mac_commands_to_repeat(); +} + +TEST_F(Test_LoRaMacCommand, clear_repeat_buffer) +{ + object->clear_repeat_buffer(); +} + +TEST_F(Test_LoRaMacCommand, copy_repeat_commands_to_buffer) +{ + loramac_mlme_confirm_t mlme; + lora_mac_system_params_t params; + my_LoRaPHY phy; + uint8_t buf[20]; + + object->clear_command_buffer(); + buf[0] = 5; + buf[1] = 2; + buf[2] = 2; + buf[3] = 2; + buf[4] = 2; + buf[5] = 2; + EXPECT_TRUE(object->process_mac_commands(buf, 0, 5, 0, mlme, params, phy) == LORAWAN_STATUS_OK); + object->parse_mac_commands_to_repeat(); + + object->clear_command_buffer(); + EXPECT_TRUE(object->get_mac_cmd_length() == 0); + + object->copy_repeat_commands_to_buffer(); + + EXPECT_TRUE(object->get_mac_cmd_length() != 0); +} + +TEST_F(Test_LoRaMacCommand, get_repeat_commands_length) +{ + EXPECT_TRUE(object->get_repeat_commands_length() == 0 ); +} + +TEST_F(Test_LoRaMacCommand, clear_sticky_mac_cmd) +{ + loramac_mlme_confirm_t mlme; + lora_mac_system_params_t params; + my_LoRaPHY phy; + uint8_t buf[20]; + + EXPECT_TRUE(object->has_sticky_mac_cmd() == false); + + object->clear_command_buffer(); + buf[0] = 5; + buf[1] = 2; + buf[2] = 2; + buf[3] = 2; + buf[4] = 2; + buf[5] = 2; + EXPECT_TRUE(object->process_mac_commands(buf, 0, 5, 0, mlme, params, phy) == LORAWAN_STATUS_OK); + + EXPECT_TRUE(object->has_sticky_mac_cmd() == true); + + object->clear_sticky_mac_cmd(); + EXPECT_TRUE(object->has_sticky_mac_cmd() == false); +} + +TEST_F(Test_LoRaMacCommand, has_sticky_mac_cmd) +{ + loramac_mlme_confirm_t mlme; + lora_mac_system_params_t params; + my_LoRaPHY phy; + uint8_t buf[20]; + + EXPECT_TRUE(object->has_sticky_mac_cmd() == false); + + object->clear_command_buffer(); + buf[0] = 5; + buf[1] = 2; + buf[2] = 2; + buf[3] = 2; + buf[4] = 2; + buf[5] = 2; + EXPECT_TRUE(object->process_mac_commands(buf, 0, 5, 0, mlme, params, phy) == LORAWAN_STATUS_OK); + + EXPECT_TRUE(object->has_sticky_mac_cmd() == true); +} + +TEST_F(Test_LoRaMacCommand, process_mac_commands) +{ + loramac_mlme_confirm_t mlme; + lora_mac_system_params_t params; + my_LoRaPHY phy; + uint8_t buf[20]; + EXPECT_TRUE(object->process_mac_commands(NULL, 0, 0, 0, mlme, params, phy) == LORAWAN_STATUS_OK); + + buf[0] = 2; + buf[1] = 16; + buf[2] = 32; + EXPECT_TRUE(object->process_mac_commands(buf, 0, 3, 0, mlme, params, phy) == LORAWAN_STATUS_OK); + + buf[0] = 3; + LoRaPHY_stub::uint8_value = 7; + LoRaPHY_stub::linkAdrNbBytesParsed = 5; + EXPECT_TRUE(object->process_mac_commands(buf, 0, 5, 0, mlme, params, phy) == LORAWAN_STATUS_OK); + + //Overflow add_link_adr_ans function here + object->clear_command_buffer(); + buf[0] = 3; + for (int i=0; i < 64; i++) { + EXPECT_TRUE(object->process_mac_commands(buf, 0, 5, 0, mlme, params, phy) == LORAWAN_STATUS_OK); + } + EXPECT_TRUE(object->process_mac_commands(buf, 0, 5, 0, mlme, params, phy) == LORAWAN_STATUS_LENGTH_ERROR); + + object->clear_command_buffer(); + buf[0] = 4; + buf[1] = 2; + EXPECT_TRUE(object->process_mac_commands(buf, 0, 1, 0, mlme, params, phy) == LORAWAN_STATUS_OK); + + //Overflow add_duty_cycle_ans() + object->clear_command_buffer(); + for (int i=0; i < 128; i++) { + EXPECT_TRUE(object->process_mac_commands(buf, 0, 1, 0, mlme, params, phy) == LORAWAN_STATUS_OK); + } + EXPECT_TRUE(object->process_mac_commands(buf, 0, 1, 0, mlme, params, phy) == LORAWAN_STATUS_LENGTH_ERROR); + + object->clear_command_buffer(); + buf[0] = 5; + buf[1] = 2; + buf[2] = 2; + buf[3] = 2; + buf[4] = 2; + buf[5] = 2; + EXPECT_TRUE(object->process_mac_commands(buf, 0, 5, 0, mlme, params, phy) == LORAWAN_STATUS_OK); + + //Overflow add_rx_param_setup_ans + object->clear_command_buffer(); + LoRaPHY_stub::uint8_value = 7; + for (int i=0; i < 64; i++) { + EXPECT_TRUE(object->process_mac_commands(buf, 0, 5, 0, mlme, params, phy) == LORAWAN_STATUS_OK); + } + EXPECT_TRUE(object->process_mac_commands(buf, 0, 5, 0, mlme, params, phy) == LORAWAN_STATUS_LENGTH_ERROR); + + object->clear_command_buffer(); + buf[0] = 6; + object->set_batterylevel_callback(my_cb); + EXPECT_TRUE(object->process_mac_commands(buf, 0, 1, 0, mlme, params, phy) == LORAWAN_STATUS_OK); + + //overflow add_dev_status_ans + object->clear_command_buffer(); + for (int i=0; i < 42; i++) { + EXPECT_TRUE(object->process_mac_commands(buf, 0, 1, 0, mlme, params, phy) == LORAWAN_STATUS_OK); + } + EXPECT_TRUE(object->process_mac_commands(buf, 0, 1, 0, mlme, params, phy) == LORAWAN_STATUS_LENGTH_ERROR); + + object->clear_command_buffer(); + buf[0] = 7; + buf[1] = 2; + buf[2] = 2; + buf[3] = 2; + buf[4] = 2; + buf[5] = 2; + EXPECT_TRUE(object->process_mac_commands(buf, 0, 6, 0, mlme, params, phy) == LORAWAN_STATUS_OK); + + //Overflow add_new_channel_ans + object->clear_command_buffer(); + LoRaPHY_stub::uint8_value = 7; + for (int i=0; i < 64; i++) { + EXPECT_TRUE(object->process_mac_commands(buf, 0, 6, 0, mlme, params, phy) == LORAWAN_STATUS_OK); + } + EXPECT_TRUE(object->process_mac_commands(buf, 0, 6, 0, mlme, params, phy) == LORAWAN_STATUS_LENGTH_ERROR); + + object->clear_command_buffer(); + buf[0] = 8; + buf[1] = 0; + EXPECT_TRUE(object->process_mac_commands(buf, 0, 2, 0, mlme, params, phy) == LORAWAN_STATUS_OK); + + //Overflow add_rx_timing_setup_ans + object->clear_command_buffer(); + LoRaPHY_stub::uint8_value = 7; + for (int i=0; i < 128; i++) { + EXPECT_TRUE(object->process_mac_commands(buf, 0, 2, 0, mlme, params, phy) == LORAWAN_STATUS_OK); + } + EXPECT_TRUE(object->process_mac_commands(buf, 0, 2, 0, mlme, params, phy) == LORAWAN_STATUS_LENGTH_ERROR); + + object->clear_command_buffer(); + buf[0] = 9; + buf[1] = 48; + EXPECT_TRUE(object->process_mac_commands(buf, 0, 2, 0, mlme, params, phy) == LORAWAN_STATUS_OK); + + //Overflow add_tx_param_setup_ans + LoRaPHY_stub::bool_counter = 0; + LoRaPHY_stub::bool_table[0] = true; + object->clear_command_buffer(); + LoRaPHY_stub::uint8_value = 7; + for (int i=0; i < 128; i++) { + EXPECT_TRUE(object->process_mac_commands(buf, 0, 2, 0, mlme, params, phy) == LORAWAN_STATUS_OK); + LoRaPHY_stub::bool_counter = 0; + } + EXPECT_TRUE(object->process_mac_commands(buf, 0, 2, 0, mlme, params, phy) == LORAWAN_STATUS_LENGTH_ERROR); + + object->clear_command_buffer(); + buf[0] = 10; + buf[1] = 2; + buf[1] = 3; + buf[1] = 4; + EXPECT_TRUE(object->process_mac_commands(buf, 0, 4, 0, mlme, params, phy) == LORAWAN_STATUS_OK); + + //Overflow add_tx_param_setup_ans + LoRaPHY_stub::bool_counter = 0; + LoRaPHY_stub::bool_table[0] = true; + object->clear_command_buffer(); + LoRaPHY_stub::uint8_value = 7; + for (int i=0; i < 64; i++) { + EXPECT_TRUE(object->process_mac_commands(buf, 0, 4, 0, mlme, params, phy) == LORAWAN_STATUS_OK); + LoRaPHY_stub::bool_counter = 0; + } + EXPECT_TRUE(object->process_mac_commands(buf, 0, 4, 0, mlme, params, phy) == LORAWAN_STATUS_LENGTH_ERROR); + + object->clear_command_buffer(); + buf[0] = 80; + EXPECT_TRUE(object->process_mac_commands(buf, 0, 1, 0, mlme, params, phy) == LORAWAN_STATUS_UNSUPPORTED); +} + +TEST_F(Test_LoRaMacCommand, add_link_check_req) +{ + object->add_link_check_req(); + EXPECT_TRUE(object->get_mac_commands_buffer()[0] == 2); + EXPECT_TRUE(object->get_mac_cmd_length() == 1); + object->clear_command_buffer(); + EXPECT_TRUE(object->get_mac_cmd_length() == 0); +} + +TEST_F(Test_LoRaMacCommand, set_batterylevel_callback) +{ + object->set_batterylevel_callback(my_cb); +} + diff --git a/UNITTESTS/features/lorawan/loramaccommand/unittest.cmake b/UNITTESTS/features/lorawan/loramaccommand/unittest.cmake new file mode 100644 index 0000000000..1dc3272977 --- /dev/null +++ b/UNITTESTS/features/lorawan/loramaccommand/unittest.cmake @@ -0,0 +1,38 @@ +#[[ + * 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. +]] + +# Unit test suite name +set(TEST_SUITE_NAME "lorawan_LoRaMacCommand") + +# Source files +set(unittest-sources + ../features/lorawan/lorastack/mac/LoRaMacCommand.cpp +) + +# Add test specific include paths +set(unittest-includes ${unittest-includes} + target_h + ../features/lorawan/lorastack/mac +) + +# Test & stub files +set(unittest-test-sources + features/lorawan/loramaccommand/Test_LoRaMacCommand.cpp + stubs/mbed_assert_stub.c + stubs/LoRaPHY_stub.cpp +) + diff --git a/UNITTESTS/features/lorawan/loramaccrypto/Test_LoRaMacCrypto.cpp b/UNITTESTS/features/lorawan/loramaccrypto/Test_LoRaMacCrypto.cpp new file mode 100644 index 0000000000..1c00938238 --- /dev/null +++ b/UNITTESTS/features/lorawan/loramaccrypto/Test_LoRaMacCrypto.cpp @@ -0,0 +1,178 @@ +/* + * 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 "LoRaMacCrypto.h" + +#include "cipher_stub.h" +#include "cmac_stub.h" +#include "aes_stub.h" + +class Test_LoRaMacCrypto : public testing::Test { +protected: + LoRaMacCrypto *object; + + virtual void SetUp() + { + cipher_stub.info_value = NULL; + cipher_stub.int_zero_counter = 0; + cipher_stub.int_value = 0; + cmac_stub.int_zero_counter = 0; + cmac_stub.int_value = 0; + aes_stub.int_zero_counter = 0; + aes_stub.int_value = 0; + object = new LoRaMacCrypto(); + } + + virtual void TearDown() + { + delete object; + } +}; + +TEST_F(Test_LoRaMacCrypto, constructor) +{ + EXPECT_TRUE(object); + LoRaMacCrypto obj; +} + +TEST_F(Test_LoRaMacCrypto, compute_mic) +{ + EXPECT_TRUE(MBEDTLS_ERR_CIPHER_ALLOC_FAILED == object->compute_mic(NULL, 0, NULL, 0, 0, 0, 0, NULL)); + + mbedtls_cipher_info_t info; + cipher_stub.info_value = &info; + cipher_stub.int_zero_counter = 0; + cipher_stub.int_value = -1; + EXPECT_TRUE(-1 == object->compute_mic(NULL, 0, NULL, 0, 0, 0, 0, NULL)); + + cipher_stub.int_value = 0; + cmac_stub.int_zero_counter = 0; + cmac_stub.int_value = -1; + EXPECT_TRUE(-1 == object->compute_mic(NULL, 0, NULL, 0, 0, 0, 0, NULL)); + + cmac_stub.int_zero_counter = 1; + cmac_stub.int_value = -1; + EXPECT_TRUE(-1 == object->compute_mic(NULL, 0, NULL, 0, 0, 0, 0, NULL)); + + cmac_stub.int_zero_counter = 2; + cmac_stub.int_value = -1; + EXPECT_TRUE(-1 == object->compute_mic(NULL, 0, NULL, 0, 0, 0, 0, NULL)); + + cmac_stub.int_zero_counter = 3; + cmac_stub.int_value = -1; + EXPECT_TRUE(-1 == object->compute_mic(NULL, 0, NULL, 0, 0, 0, 0, NULL)); + + uint32_t mic[16]; + cmac_stub.int_zero_counter = 3; + cmac_stub.int_value = 0; + EXPECT_TRUE(0 == object->compute_mic(NULL, 0, NULL, 0, 0, 0, 0, mic)); + +} + +TEST_F(Test_LoRaMacCrypto, encrypt_payload) +{ + aes_stub.int_zero_counter = 0; + aes_stub.int_value = -1; + EXPECT_TRUE(-1 == object->encrypt_payload(NULL, 0, NULL, 0, 0, 0, 0, NULL)); + + aes_stub.int_zero_counter = 1; + aes_stub.int_value = -2; + uint8_t buf[60]; + uint8_t enc[60]; + EXPECT_TRUE(-2 == object->encrypt_payload(buf, 20, NULL, 0, 0, 0, 0, enc)); + + aes_stub.int_zero_counter = 2; + aes_stub.int_value = -3; + EXPECT_TRUE(-3 == object->encrypt_payload(buf, 20, NULL, 0, 0, 0, 0, enc)); + + aes_stub.int_value = 0; + EXPECT_TRUE(0 == object->encrypt_payload(buf, 20, NULL, 0, 0, 0, 0, enc)); + + EXPECT_TRUE(0 == object->encrypt_payload(buf, 60, NULL, 0, 0, 0, 0, enc)); + + aes_stub.int_zero_counter = 0; + EXPECT_TRUE(0 == object->encrypt_payload(NULL, 0, NULL, 0, 0, 0, 0, NULL)); +} + +TEST_F(Test_LoRaMacCrypto, decrypt_payload) +{ + EXPECT_TRUE(0 == object->decrypt_payload(NULL, 0, NULL, 0, 0, 0, 0, NULL)); +} + +TEST_F(Test_LoRaMacCrypto, compute_join_frame_mic) +{ + uint32_t mic[16]; + EXPECT_TRUE(MBEDTLS_ERR_CIPHER_ALLOC_FAILED == object->compute_join_frame_mic(NULL, 0, NULL, 0, NULL)); + mbedtls_cipher_info_t info; + cipher_stub.info_value = &info; + cipher_stub.int_zero_counter = 0; + cipher_stub.int_value = -1; + EXPECT_TRUE(-1 == object->compute_join_frame_mic(NULL, 0, NULL, 0, NULL)); + + cipher_stub.int_value = 0; + cmac_stub.int_zero_counter = 0; + cmac_stub.int_value = -1; + EXPECT_TRUE(-1 == object->compute_join_frame_mic(NULL, 0, NULL, 0, NULL)); + + cmac_stub.int_zero_counter = 1; + cmac_stub.int_value = -1; + EXPECT_TRUE(-1 == object->compute_join_frame_mic(NULL, 0, NULL, 0, NULL)); + + cmac_stub.int_zero_counter = 2; + cmac_stub.int_value = -1; + EXPECT_TRUE(-1 == object->compute_join_frame_mic(NULL, 0, NULL, 0, NULL)); + + cmac_stub.int_zero_counter = 3; + cmac_stub.int_value = 0; + EXPECT_TRUE(0 == object->compute_join_frame_mic(NULL, 0, NULL, 0, mic)); +} + +TEST_F(Test_LoRaMacCrypto, decrypt_join_frame) +{ + aes_stub.int_zero_counter = 0; + aes_stub.int_value = -1; + EXPECT_TRUE(-1 == object->decrypt_join_frame(NULL, 0, NULL, 0, NULL)); + + aes_stub.int_zero_counter = 1; + aes_stub.int_value = -1; + EXPECT_TRUE(-1 == object->decrypt_join_frame(NULL, 0, NULL, 0, NULL)); + + aes_stub.int_value = 0; + uint8_t buf[60]; + uint8_t enc[60]; + EXPECT_TRUE(0 == object->decrypt_join_frame(buf, 60, NULL, 0, enc)); +} + +TEST_F(Test_LoRaMacCrypto, compute_skeys_for_join_frame) +{ + uint8_t nwk_key[16]; + uint8_t app_key[16]; + uint8_t nonce[16]; + + aes_stub.int_zero_counter = 0; + aes_stub.int_value = -1; + EXPECT_TRUE(-1 == object->compute_skeys_for_join_frame(NULL, 0, nonce, 0, nwk_key, app_key)); + + aes_stub.int_zero_counter = 1; + aes_stub.int_value = -2; + EXPECT_TRUE(-2 == object->compute_skeys_for_join_frame(NULL, 0, nonce, 0, nwk_key, app_key)); + + aes_stub.int_zero_counter = 0; + aes_stub.int_value = 0; + EXPECT_TRUE(0 == object->compute_skeys_for_join_frame(NULL, 0, nonce, 0, nwk_key, app_key)); +} diff --git a/UNITTESTS/features/lorawan/loramaccrypto/unittest.cmake b/UNITTESTS/features/lorawan/loramaccrypto/unittest.cmake new file mode 100644 index 0000000000..e41d668906 --- /dev/null +++ b/UNITTESTS/features/lorawan/loramaccrypto/unittest.cmake @@ -0,0 +1,40 @@ +#[[ + * 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. +]] + +# Unit test suite name +set(TEST_SUITE_NAME "lorawan_LoRaMacCrypto") + +# Source files +set(unittest-sources + ../features/lorawan/lorastack/mac/LoRaMacCrypto.cpp +) + +# Add test specific include paths +set(unittest-includes ${unittest-includes} + target_h + ../features/lorawan/lorastack/mac +) + +# Test & stub files +set(unittest-test-sources + features/lorawan/loramaccrypto/Test_LoRaMacCrypto.cpp + stubs/cipher_stub.c + stubs/aes_stub.c + stubs/cmac_stub.c + +) + diff --git a/UNITTESTS/features/lorawan/loraphy/Test_LoRaPHY.cpp b/UNITTESTS/features/lorawan/loraphy/Test_LoRaPHY.cpp new file mode 100644 index 0000000000..6b7fabaaaf --- /dev/null +++ b/UNITTESTS/features/lorawan/loraphy/Test_LoRaPHY.cpp @@ -0,0 +1,814 @@ +/* + * 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 "LoRaPHY.h" + +#include "LoRaWANTimer_stub.h" + +class my_LoRaPHY : public LoRaPHY +{ +public: + my_LoRaPHY(){phy_params.adr_ack_delay = 1;} + + virtual ~my_LoRaPHY(){} + + loraphy_params_t &get_phy_params() { + return phy_params; + } +}; + +class my_radio : public LoRaRadio +{ +public: + + virtual void init_radio(radio_events_t *events){}; + + virtual void radio_reset(){}; + + virtual void sleep(void){}; + + virtual void standby(void){}; + + virtual void set_rx_config (radio_modems_t modem, uint32_t bandwidth, + uint32_t datarate, uint8_t coderate, + uint32_t bandwidth_afc, uint16_t preamble_len, + uint16_t symb_timeout, bool fix_len, + uint8_t payload_len, + bool crc_on, bool freq_hop_on, uint8_t hop_period, + bool iq_inverted, bool rx_continuous){}; + + virtual void set_tx_config(radio_modems_t modem, int8_t power, uint32_t fdev, + uint32_t bandwidth, uint32_t datarate, + uint8_t coderate, uint16_t preamble_len, + bool fix_len, bool crc_on, bool freq_hop_on, + uint8_t hop_period, bool iq_inverted, uint32_t timeout){}; + + virtual void send(uint8_t *buffer, uint8_t size){}; + + virtual void receive(void){}; + + virtual void set_channel(uint32_t freq){}; + + virtual uint32_t random(void){}; + + virtual uint8_t get_status(void){return uint8_value;}; + + virtual void set_max_payload_length(radio_modems_t modem, uint8_t max){}; + + virtual void set_public_network(bool enable){}; + + virtual uint32_t time_on_air(radio_modems_t modem, uint8_t pkt_len){}; + + virtual bool perform_carrier_sense(radio_modems_t modem, + uint32_t freq, + int16_t rssi_threshold, + uint32_t max_carrier_sense_time){ return bool_value;}; + + virtual void start_cad(void){}; + + virtual bool check_rf_frequency(uint32_t frequency){ return bool_value; }; + + virtual void set_tx_continuous_wave(uint32_t freq, int8_t power, uint16_t time){}; + + virtual void lock(void){}; + + virtual void unlock(void){}; + + bool bool_value; + uint8_t uint8_value; +}; + +class Test_LoRaPHY : public testing::Test { +protected: + my_LoRaPHY *object; + + virtual void SetUp() + { + object = new my_LoRaPHY(); + } + + virtual void TearDown() + { + delete object; + } +}; + +TEST_F(Test_LoRaPHY, initialize) +{ + object->initialize(NULL); +} + +TEST_F(Test_LoRaPHY, set_radio_instance) +{ + my_radio radio; + object->set_radio_instance(radio); +} + +TEST_F(Test_LoRaPHY, put_radio_to_sleep) +{ + my_radio radio; + object->set_radio_instance(radio); + object->put_radio_to_sleep(); +} + +TEST_F(Test_LoRaPHY, put_radio_to_standby) +{ + my_radio radio; + object->set_radio_instance(radio); + object->put_radio_to_standby(); +} + +TEST_F(Test_LoRaPHY, handle_receive) +{ + my_radio radio; + object->set_radio_instance(radio); + object->handle_receive(); +} + +TEST_F(Test_LoRaPHY, handle_send) +{ + my_radio radio; + object->set_radio_instance(radio); + object->handle_send(NULL, 0); +} + +TEST_F(Test_LoRaPHY, setup_public_network_mode) +{ + my_radio radio; + channel_params_t p; + object->get_phy_params().channels.channel_list = &p; + object->set_radio_instance(radio); + object->setup_public_network_mode(false); +} + +TEST_F(Test_LoRaPHY, get_radio_rng) +{ + my_radio radio; + object->set_radio_instance(radio); + EXPECT_TRUE(0 != object->get_radio_rng()); +} + +TEST_F(Test_LoRaPHY, calculate_backoff) +{ + channel_params_t p[1]; + p[0].band = 0; + object->get_phy_params().channels.channel_list = p; + band_t b[1]; + object->get_phy_params().bands.table = b; + object->calculate_backoff(false, false, false, 0, 10, 12); + + object->calculate_backoff(false, true, false, 0, 3600000 + 10, 12); + + object->calculate_backoff(false, false, true, 0, 3600000 + 36000000 + 10, 12); +} + +TEST_F(Test_LoRaPHY, mask_bit_test) +{ + uint16_t buf; + EXPECT_TRUE(!object->mask_bit_test(&buf, 0)); +} + +TEST_F(Test_LoRaPHY, mask_bit_set) +{ + uint16_t buf; + object->mask_bit_set(&buf, 3); +} + +TEST_F(Test_LoRaPHY, mask_bit_clear) +{ + uint16_t buf; + object->mask_bit_clear(&buf, 0); +} + +TEST_F(Test_LoRaPHY, request_new_channel) +{ + channel_params_t p; + EXPECT_TRUE(0 == object->request_new_channel(1, &p)); + + p.frequency = 0; + object->get_phy_params().custom_channelplans_supported = true; + uint16_t list; + object->get_phy_params().channels.default_mask = &list; + channel_params_t pp; + object->get_phy_params().channels.channel_list = &pp; + EXPECT_TRUE(0 == object->request_new_channel(1, &p)); + + //Default + p.frequency = 2; + EXPECT_TRUE(0 == object->request_new_channel(1, &p)); + + //Freq & DR invalid + object->get_phy_params().max_channel_cnt = 2; + EXPECT_TRUE(0 == object->request_new_channel(1, &p)); + + //Freq invalid + pp.frequency = 0; + object->get_phy_params().default_max_datarate = 1; + object->get_phy_params().max_tx_datarate = 8; + p.dr_range.fields.max = 2; + p.dr_range.fields.min = 0; + object->get_phy_params().default_channel_cnt = 3; + EXPECT_TRUE(2 == object->request_new_channel(0, &p)); + + //DR invalid + pp.frequency = 2; + p.band = 0; + object->get_phy_params().bands.size = 1; + band_t b; + object->get_phy_params().bands.table = &b; + b.higher_band_freq = 5; + b.lower_band_freq = 1; + p.dr_range.fields.max = 12; + p.dr_range.fields.min = 1; + EXPECT_TRUE(1 == object->request_new_channel(0, &p)); + + //STATUS_OK + p.dr_range.fields.max = 2; + uint16_t list2[16]; + p.dr_range.fields.min = 0; + object->get_phy_params().channels.mask = list2; + EXPECT_TRUE(3 == object->request_new_channel(0, &p)); +} + +TEST_F(Test_LoRaPHY, set_last_tx_done) +{ + channel_params_t p[1]; + p[0].band = 0; + object->get_phy_params().channels.channel_list = p; + band_t b[1]; + object->get_phy_params().bands.table = b; + object->set_last_tx_done(0, false, 0); + + object->set_last_tx_done(0, true, 0); +} + +TEST_F(Test_LoRaPHY, restore_default_channels) +{ + channel_params_t p[1]; + p[0].band = 0; + object->get_phy_params().channels.channel_list = p; + uint16_t m, dm; + object->get_phy_params().channels.mask_size = 1; + object->get_phy_params().channels.default_mask = &dm; + object->get_phy_params().channels.mask = &m; + object->restore_default_channels(); +} + +TEST_F(Test_LoRaPHY, apply_cf_list) +{ + uint8_t list[16]; + object->apply_cf_list(list, 0); + + object->get_phy_params().cflist_supported = true; + object->apply_cf_list(list, 0); + + object->get_phy_params().default_channel_cnt = 2; + object->get_phy_params().cflist_channel_cnt = 0; + object->get_phy_params().max_channel_cnt = 3; + + uint16_t mask[8]; + channel_params_t p[8]; + object->get_phy_params().channels.default_mask = mask; + object->get_phy_params().channels.mask = mask; + object->get_phy_params().channels.channel_list = p; + object->apply_cf_list(list, 16); + + list[1] = 15; + object->get_phy_params().cflist_channel_cnt = 1; + object->apply_cf_list(list, 16); +} + +TEST_F(Test_LoRaPHY, get_next_ADR) +{ + int8_t i = 0; + int8_t j = 0; + uint32_t ctr = 0; + object->get_phy_params().min_tx_datarate = 0; + EXPECT_TRUE(!object->get_next_ADR(false, i, j, ctr)); + + i = 1; + object->get_phy_params().adr_ack_limit = 3; + EXPECT_TRUE(!object->get_next_ADR(false, i, j, ctr)); + + object->get_phy_params().adr_ack_limit = 3; + ctr = 4; + object->get_phy_params().max_tx_power = 2; + object->get_phy_params().adr_ack_delay = 1; + EXPECT_TRUE(object->get_next_ADR(true, i, j, ctr)); + + ctr = 5; + object->get_phy_params().adr_ack_delay = 2; + EXPECT_TRUE(!object->get_next_ADR(true, i, j, ctr)); +} + +TEST_F(Test_LoRaPHY, rx_config) +{ + my_radio radio; + object->set_radio_instance(radio); + uint8_t list; + object->get_phy_params().datarates.table = &list; + uint8_t list2; + object->get_phy_params().payloads_with_repeater.table = &list2; + rx_config_params_t p; + p.datarate = 0; + p.rx_slot = RX_SLOT_WIN_1; + channel_params_t pp[1]; + object->get_phy_params().channels.channel_list = pp; + pp[0].rx1_frequency = 2; + p.channel = 0; + uint8_t tab[8]; + object->get_phy_params().payloads.table = tab; + object->get_phy_params().payloads_with_repeater.table = tab; + EXPECT_TRUE(object->rx_config(&p)); + + p.datarate = DR_7; + p.is_repeater_supported = true; + object->get_phy_params().fsk_supported = true; + EXPECT_TRUE(object->rx_config(&p)); +} + +TEST_F(Test_LoRaPHY, compute_rx_win_params) +{ + uint32_t list[1]; + list[0] = 0; + object->get_phy_params().bandwidths.table = list; + uint8_t list2; + object->get_phy_params().datarates.table = &list2; + rx_config_params_t p; + object->compute_rx_win_params(0, 0, 0, &p); + + p.datarate = 0; + list[0] = 125000; + object->compute_rx_win_params(0, 0, 0, &p); + + list[0] = 250000; + object->compute_rx_win_params(0, 0, 0, &p); + + list[0] = 500000; + object->get_phy_params().fsk_supported = true; + object->get_phy_params().max_rx_datarate = 0; + object->compute_rx_win_params(0, 0, 0, &p); +} + +TEST_F(Test_LoRaPHY, tx_config) +{ + band_t b; + object->get_phy_params().bands.table = &b; + channel_params_t pp; + pp.band=0; + object->get_phy_params().channels.channel_list = &pp; + uint32_t list = 0; + object->get_phy_params().bandwidths.table = &list; + uint8_t list2; + object->get_phy_params().datarates.table = &list2; + my_radio radio; + object->set_radio_instance(radio); + tx_config_params_t p; + p.channel=0; + int8_t i; + lorawan_time_t t; + object->tx_config(&p, &i, &t); + + p.datarate = 8; + object->get_phy_params().max_tx_datarate = 8; + object->tx_config(&p, &i, &t); +} + +TEST_F(Test_LoRaPHY, link_ADR_request) +{ + adr_req_params_t p; + uint8_t b[100]; + p.payload = b; + b[0] = 0x03; + b[1] = 1; + b[2] = 0; + b[3] = 0; + b[4] = 1 << 4; + b[5] = 0x03; + b[6] = 1; + b[7] = 1; + b[8] = 1; + b[9] = 6 << 4; + b[10] = 0x03; + b[11] = 1; + b[12] = 0xff; + b[13] = 0xff; + b[14] = 0; + b[15] = 0; + p.payload_size = 16; + int8_t i, j; + uint8_t k, l; + uint8_t t[5]; + t[0] = 0; + object->get_phy_params().datarates.size = 1; + object->get_phy_params().datarates.table = t; + //Test without ADR payload does not make sense here. + + object->get_phy_params().max_channel_cnt = 2; + channel_params_t li[4]; + object->get_phy_params().channels.channel_list = li; + li[0].frequency = 0; + li[1].frequency = 5; + EXPECT_TRUE(4 == object->link_ADR_request(&p, &i, &j, &k, &l)); + + t[0] = 3; + //verify adr with p.adr_enabled = false + EXPECT_TRUE(0 == object->link_ADR_request(&p, &i, &j, &k, &l)); + + p.current_nb_rep = 0; + EXPECT_TRUE(0 == object->link_ADR_request(&p, &i, &j, &k, &l)); + + p.adr_enabled = true; + li[0].dr_range.value = 0xff; + object->get_phy_params().min_tx_datarate = DR_3; + object->get_phy_params().max_tx_datarate = DR_8; + + //verify adr with status != 0 + EXPECT_TRUE(0 == object->link_ADR_request(&p, &i, &j, &k, &l)); + + object->get_phy_params().max_tx_power = 2; + object->get_phy_params().min_tx_power = 6; + //verify adr with status != 0 + EXPECT_TRUE(4 == object->link_ADR_request(&p, &i, &j, &k, &l)); + + object->get_phy_params().min_tx_datarate = DR_0; + li[0].dr_range.value = 0xf0; + EXPECT_TRUE(6 == object->link_ADR_request(&p, &i, &j, &k, &l)); + + li[1].dr_range.fields.min = DR_0; + li[1].dr_range.fields.max = DR_13; + b[4] = 6 << 4; + p.payload_size = 5; + EXPECT_TRUE(7 == object->link_ADR_request(&p, &i, &j, &k, &l)); + + uint16_t mask[2]; + object->get_phy_params().channels.mask = mask; + object->get_phy_params().channels.mask_size = 2; + EXPECT_TRUE(7 == object->link_ADR_request(&p, &i, &j, &k, &l)); + + li[0].dr_range.value = 0xff; + object->get_phy_params().max_channel_cnt = 0; + EXPECT_TRUE(5 == object->link_ADR_request(&p, &i, &j, &k, &l)); + + b[0] = 0x03; + b[1] = 1; + b[2] = 0; + b[3] = 0; + b[4] = 0; + t[0] = 0; + object->get_phy_params().datarates.size = 1; + object->get_phy_params().datarates.table = t; + //Test without ADR payload does not make sense here. + + object->get_phy_params().max_channel_cnt = 2; + li[0].frequency = 0; + li[1].frequency = 5; + EXPECT_TRUE(4 == object->link_ADR_request(&p, &i, &j, &k, &l)); +} + +TEST_F(Test_LoRaPHY, accept_rx_param_setup_req) +{ + my_radio radio; + object->set_radio_instance(radio); + rx_param_setup_req_t req; + EXPECT_TRUE(0 == object->accept_rx_param_setup_req(&req)); +} + +TEST_F(Test_LoRaPHY, accept_tx_param_setup_req) +{ + my_radio radio; + object->set_radio_instance(radio); + object->get_phy_params().accept_tx_param_setup_req = true; + EXPECT_TRUE(object->accept_tx_param_setup_req(0, 0)); +} + +TEST_F(Test_LoRaPHY, dl_channel_request) +{ + EXPECT_TRUE(0 == object->dl_channel_request(0, 0)); + + object->get_phy_params().dl_channel_req_supported = true; + object->get_phy_params().bands.size = 1; + band_t t[1]; + object->get_phy_params().bands.table = t; + channel_params_t p[4]; + object->get_phy_params().channels.channel_list = p; + + p[0].frequency = 0; + EXPECT_TRUE(0 == object->dl_channel_request(0, 1)); + + t[0].higher_band_freq = 19; + t[0].lower_band_freq = 0; + p[0].frequency = 1; + EXPECT_TRUE(3 == object->dl_channel_request(0, 1)); +} + +TEST_F(Test_LoRaPHY, get_alternate_DR) +{ + EXPECT_TRUE(0 == object->get_alternate_DR(0)); + + object->get_phy_params().default_max_datarate = 5; + object->get_phy_params().min_tx_datarate = 4; + EXPECT_TRUE(5 == object->get_alternate_DR(1)); + + object->get_phy_params().default_max_datarate = 6; + object->get_phy_params().min_tx_datarate = 4; + EXPECT_TRUE(5 == object->get_alternate_DR(2)); +} + +TEST_F(Test_LoRaPHY, set_next_channel) +{ + channel_selection_params_t p; + uint8_t ch; + lorawan_time_t t1; + lorawan_time_t t2; + p.aggregate_timeoff = 10000; + EXPECT_TRUE(LORAWAN_STATUS_DUTYCYCLE_RESTRICTED == object->set_next_channel(&p, &ch, &t1, &t2)); + + uint16_t list[16]; + list[4] = 1; + memcpy(list, "\0", 16); + object->get_phy_params().channels.mask = list; + object->get_phy_params().channels.mask_size = 1; + p.aggregate_timeoff = 10000; + EXPECT_TRUE(LORAWAN_STATUS_DUTYCYCLE_RESTRICTED == object->set_next_channel(&p, &ch, &t1, &t2)); + + LoRaWANTimer_stub::time_value = 20000; + EXPECT_TRUE(LORAWAN_STATUS_NO_CHANNEL_FOUND == object->set_next_channel(&p, &ch, &t1, &t2)); + + p.joined = false; + p.dc_enabled = false; + band_t b[4]; + object->get_phy_params().bands.size = 2; + object->get_phy_params().bands.table = &b; + b[0].off_time = 0; + b[1].off_time = 9999999; + list[4] = 0; + object->get_phy_params().channels.mask_size = 128; + p.current_datarate = DR_1; + object->get_phy_params().max_channel_cnt = 4; + EXPECT_TRUE(LORAWAN_STATUS_NO_CHANNEL_FOUND == object->set_next_channel(&p, &ch, &t1, &t2)); + + p.dc_enabled = true; + EXPECT_TRUE(LORAWAN_STATUS_NO_CHANNEL_FOUND == object->set_next_channel(&p, &ch, &t1, &t2)); + + list[4] = 1; + p.joined = true; + p.dc_enabled = false; + channel_params_t l[4]; + l[0].dr_range.value = 0xff; + l[1].dr_range.value = 0xff; + l[2].dr_range.value = 0xf0; + l[3].dr_range.value = 0xf0; + l[2].band = 2; + l[3].band = 3; + object->get_phy_params().channels.channel_list = l; + list[0] = 0xFF; + b[2].off_time = 9999999; + b[3].off_time = 0; + EXPECT_TRUE(LORAWAN_STATUS_OK == object->set_next_channel(&p, &ch, &t1, &t2)); + + b[0].off_time = 10000; + LoRaWANTimer_stub::time_value = 2000; + p.aggregate_timeoff = 1000; + p.dc_enabled = true; + EXPECT_TRUE(LORAWAN_STATUS_OK == object->set_next_channel(&p, &ch, &t1, &t2)); +} + +TEST_F(Test_LoRaPHY, add_channel) +{ + uint16_t list[16]; + object->get_phy_params().channels.mask = list; + object->get_phy_params().channels.default_mask = list; + channel_params_t p; + EXPECT_TRUE(LORAWAN_STATUS_PARAMETER_INVALID == object->add_channel(&p, 0)); + + object->get_phy_params().custom_channelplans_supported = true; + object->get_phy_params().max_channel_cnt = 2; + object->get_phy_params().min_tx_datarate = 0; + object->get_phy_params().max_tx_datarate = 13; + p.dr_range.fields.min = 6; + p.dr_range.fields.max = 1; + EXPECT_TRUE(LORAWAN_STATUS_FREQ_AND_DR_INVALID == object->add_channel(&p, 0)); +} + +TEST_F(Test_LoRaPHY, remove_channel) +{ + channel_params_t pp; + pp.band=0; + object->get_phy_params().channels.channel_list = &pp; + uint16_t list[16]; + list[0] = 1; + object->get_phy_params().channels.mask = list; + object->get_phy_params().channels.default_mask = list; + EXPECT_TRUE(false == object->remove_channel(0)); + + list[0] = 0; + EXPECT_TRUE(false == object->remove_channel(0)); + + object->get_phy_params().channels.mask_size = 1; + object->get_phy_params().max_channel_cnt = 0; + EXPECT_TRUE(false == object->remove_channel(0)); + + object->get_phy_params().max_channel_cnt = 1; + EXPECT_TRUE(true == object->remove_channel(0)); +} + +TEST_F(Test_LoRaPHY, set_tx_cont_mode) +{ + channel_params_t pp; + pp.band=0; + object->get_phy_params().channels.channel_list = &pp; + band_t b; + object->get_phy_params().bands.table = &b; + my_radio radio; + object->set_radio_instance(radio); + + cw_mode_params_t p; + p.max_eirp = 0; + p.channel=0; + object->set_tx_cont_mode(&p); + + p.max_eirp = 1; + p.antenna_gain = 1; + object->set_tx_cont_mode(&p, 1); +} + +TEST_F(Test_LoRaPHY, apply_DR_offset) +{ + EXPECT_TRUE(0 == object->apply_DR_offset(0, 0)); + + object->get_phy_params().min_tx_datarate = 1; + EXPECT_TRUE(1 == object->apply_DR_offset(0, 2)); +} + +TEST_F(Test_LoRaPHY, reset_to_default_values) +{ + loramac_protocol_params p; + object->reset_to_default_values(&p); + + object->reset_to_default_values(&p, true); +} + +TEST_F(Test_LoRaPHY, get_next_lower_tx_datarate) +{ + EXPECT_TRUE(DR_0 == object->get_next_lower_tx_datarate(DR_2)); + + object->get_phy_params().ul_dwell_time_setting = 1; + object->get_phy_params().dwell_limit_datarate = DR_1; + EXPECT_TRUE(DR_1 == object->get_next_lower_tx_datarate(DR_2)); +} + +TEST_F(Test_LoRaPHY, get_minimum_rx_datarate) +{ + EXPECT_TRUE(DR_0 == object->get_minimum_rx_datarate()); + + object->get_phy_params().dl_dwell_time_setting = 1; + object->get_phy_params().dwell_limit_datarate = DR_1; + EXPECT_TRUE(DR_1 == object->get_minimum_rx_datarate()); +} + +TEST_F(Test_LoRaPHY, get_minimum_tx_datarate) +{ + EXPECT_TRUE(DR_0 == object->get_minimum_tx_datarate()); + + object->get_phy_params().ul_dwell_time_setting = 1; + object->get_phy_params().dwell_limit_datarate = DR_1; + EXPECT_TRUE(DR_1 == object->get_minimum_tx_datarate()); +} + +TEST_F(Test_LoRaPHY, get_default_tx_datarate) +{ + EXPECT_TRUE(0 == object->get_default_tx_datarate()); +} + +TEST_F(Test_LoRaPHY, get_default_max_tx_datarate) +{ + EXPECT_TRUE(DR_0 == object->get_default_max_tx_datarate()); +} + +TEST_F(Test_LoRaPHY, get_default_tx_power) +{ + EXPECT_TRUE(0 == object->get_default_tx_power()); +} + +TEST_F(Test_LoRaPHY, get_max_payload) +{ + uint8_t list=8; + object->get_phy_params().payloads.table = &list; + object->get_phy_params().payloads_with_repeater.table = &list; + EXPECT_TRUE(8 == object->get_max_payload(0)); + + EXPECT_TRUE(8 == object->get_max_payload(0, true)); +} + +TEST_F(Test_LoRaPHY, get_maximum_frame_counter_gap) +{ + EXPECT_TRUE(0 == object->get_maximum_frame_counter_gap()); +} + +TEST_F(Test_LoRaPHY, get_ack_timeout) +{ + EXPECT_TRUE(0 == object->get_ack_timeout()); +} + +TEST_F(Test_LoRaPHY, get_default_rx2_frequency) +{ + EXPECT_TRUE(0 == object->get_default_rx2_frequency()); +} + +TEST_F(Test_LoRaPHY, get_default_rx2_datarate) +{ + EXPECT_TRUE(0 == object->get_default_rx2_datarate()); +} + +TEST_F(Test_LoRaPHY, get_channel_mask) +{ + EXPECT_TRUE(0 == object->get_channel_mask()); + EXPECT_TRUE(0 == object->get_channel_mask(true)); +} + +TEST_F(Test_LoRaPHY, get_max_nb_channels) +{ + EXPECT_TRUE(0 == object->get_max_nb_channels()); +} + +TEST_F(Test_LoRaPHY, get_phy_channels) +{ + EXPECT_TRUE(0 == object->get_phy_channels()); +} + +TEST_F(Test_LoRaPHY, is_custom_channel_plan_supported) +{ + EXPECT_TRUE(false == object->is_custom_channel_plan_supported()); +} + +TEST_F(Test_LoRaPHY, verify_rx_datarate) +{ + EXPECT_TRUE(false == object->verify_rx_datarate(0)); + + object->get_phy_params().datarates.size = 1; + uint8_t t[1]; + t[0] = 2; + object->get_phy_params().datarates.table = t; + object->get_phy_params().dl_dwell_time_setting = 0; + + EXPECT_TRUE(true == object->verify_rx_datarate(0)); + + object->get_phy_params().dl_dwell_time_setting = 1; + object->get_phy_params().min_rx_datarate = 0; + + EXPECT_TRUE(true == object->verify_rx_datarate(0)); +} + +TEST_F(Test_LoRaPHY, verify_tx_datarate) +{ + EXPECT_TRUE(false == object->verify_tx_datarate(0)); + + object->get_phy_params().datarates.size = 1; + uint8_t t[1]; + t[0] = 2; + object->get_phy_params().datarates.table = t; + object->get_phy_params().ul_dwell_time_setting = 0; + EXPECT_TRUE(true == object->verify_tx_datarate(0)); + + object->get_phy_params().ul_dwell_time_setting = 1; + EXPECT_TRUE(true == object->verify_tx_datarate(0)); + + object->get_phy_params().ul_dwell_time_setting = 1; + EXPECT_TRUE(true == object->verify_tx_datarate(0, true)); +} + +TEST_F(Test_LoRaPHY, verify_tx_power) +{ + EXPECT_TRUE(true == object->verify_tx_power(0)); +} + +TEST_F(Test_LoRaPHY, verify_duty_cycle) +{ + EXPECT_TRUE(true == object->verify_duty_cycle(false)); + + EXPECT_TRUE(false == object->verify_duty_cycle(true)); +} + +TEST_F(Test_LoRaPHY, verify_nb_join_trials) +{ + EXPECT_TRUE(false == object->verify_nb_join_trials(0)); + EXPECT_TRUE(true == object->verify_nb_join_trials(100)); +} + + diff --git a/UNITTESTS/features/lorawan/loraphy/unittest.cmake b/UNITTESTS/features/lorawan/loraphy/unittest.cmake new file mode 100644 index 0000000000..c1abde4e46 --- /dev/null +++ b/UNITTESTS/features/lorawan/loraphy/unittest.cmake @@ -0,0 +1,41 @@ +#[[ + * Copyright (c) 2018, Arm Limited and affiliates + * SPDX-License-Identifier: Apache-2.0 + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. +]] + +# Unit test suite name +set(TEST_SUITE_NAME "lorawan_LoRaPHY") + +# Source files +set(unittest-sources + ../features/lorawan/lorastack/phy/LoRaPHY.cpp +) + +# Add test specific include paths +set(unittest-includes ${unittest-includes} + target_h + ../features/lorawan/lorastack/phy +) + +# Test & stub files +set(unittest-test-sources + features/lorawan/loraphy/Test_LoRaPHY.cpp + stubs/LoRaWANTimer_stub.cpp + stubs/mbed_assert_stub.c +) + + +set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DMBED_CONF_LORA_UPLINK_PREAMBLE_LENGTH=8 -DMBED_CONF_LORA_DUTY_CYCLE_ON_JOIN=true") +set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DMBED_CONF_LORA_UPLINK_PREAMBLE_LENGTH=8 -DMBED_CONF_LORA_DUTY_CYCLE_ON_JOIN=true") diff --git a/UNITTESTS/features/lorawan/loraphyas923/Test_LoRaPHYAS923.cpp b/UNITTESTS/features/lorawan/loraphyas923/Test_LoRaPHYAS923.cpp new file mode 100644 index 0000000000..98a361810d --- /dev/null +++ b/UNITTESTS/features/lorawan/loraphyas923/Test_LoRaPHYAS923.cpp @@ -0,0 +1,143 @@ +/* + * 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 "LoRaPHYAS923.h" + +#include "LoRaPHY_stub.h" + +class my_radio : public LoRaRadio +{ +public: + + virtual void init_radio(radio_events_t *events){}; + + virtual void radio_reset(){}; + + virtual void sleep(void){}; + + virtual void standby(void){}; + + virtual void set_rx_config (radio_modems_t modem, uint32_t bandwidth, + uint32_t datarate, uint8_t coderate, + uint32_t bandwidth_afc, uint16_t preamble_len, + uint16_t symb_timeout, bool fix_len, + uint8_t payload_len, + bool crc_on, bool freq_hop_on, uint8_t hop_period, + bool iq_inverted, bool rx_continuous){}; + + virtual void set_tx_config(radio_modems_t modem, int8_t power, uint32_t fdev, + uint32_t bandwidth, uint32_t datarate, + uint8_t coderate, uint16_t preamble_len, + bool fix_len, bool crc_on, bool freq_hop_on, + uint8_t hop_period, bool iq_inverted, uint32_t timeout){}; + + virtual void send(uint8_t *buffer, uint8_t size){}; + + virtual void receive(void){}; + + virtual void set_channel(uint32_t freq){}; + + virtual uint32_t random(void){}; + + virtual uint8_t get_status(void){return uint8_value;}; + + virtual void set_max_payload_length(radio_modems_t modem, uint8_t max){}; + + virtual void set_public_network(bool enable){}; + + virtual uint32_t time_on_air(radio_modems_t modem, uint8_t pkt_len){}; + + virtual bool perform_carrier_sense(radio_modems_t modem, + uint32_t freq, + int16_t rssi_threshold, + uint32_t max_carrier_sense_time){ return bool_value;}; + + virtual void start_cad(void){}; + + virtual bool check_rf_frequency(uint32_t frequency){ return bool_value; }; + + virtual void set_tx_continuous_wave(uint32_t freq, int8_t power, uint16_t time){}; + + virtual void lock(void){}; + + virtual void unlock(void){}; + + bool bool_value; + uint8_t uint8_value; +}; + +class Test_LoRaPHYAS923 : public testing::Test { +protected: + LoRaPHYAS923 *object; + my_radio radio; + + virtual void SetUp() + { + LoRaPHY_stub::radio = &radio; + object = new LoRaPHYAS923(); + } + + virtual void TearDown() + { + LoRaPHY_stub::radio = NULL; + delete object; + } +}; + +TEST_F(Test_LoRaPHYAS923, constructor) +{ + EXPECT_TRUE(object); +} + +TEST_F(Test_LoRaPHYAS923, get_alternate_DR) +{ + EXPECT_TRUE(2 == object->get_alternate_DR(1)); +} + +TEST_F(Test_LoRaPHYAS923, set_next_channel) +{ + channel_selection_params_t next_channel; + lorawan_time_t backoff_time = 0; + lorawan_time_t time = 0; + uint8_t ch = 1; + + next_channel.aggregate_timeoff = 0; + LoRaPHY_stub::uint8_value = 0; + EXPECT_TRUE(LORAWAN_STATUS_NO_CHANNEL_FOUND == object->set_next_channel(&next_channel, &ch, &backoff_time, &time)); + + next_channel.aggregate_timeoff = 1; + radio.bool_value = false; + EXPECT_TRUE(LORAWAN_STATUS_DUTYCYCLE_RESTRICTED == object->set_next_channel(&next_channel, &ch, &backoff_time, &time)); + + next_channel.aggregate_timeoff = 0; + LoRaPHY_stub::uint8_value = 1; + EXPECT_TRUE(LORAWAN_STATUS_NO_FREE_CHANNEL_FOUND == object->set_next_channel(&next_channel, &ch, &backoff_time, &time)); + + radio.bool_value = true; + EXPECT_TRUE(LORAWAN_STATUS_OK == object->set_next_channel(&next_channel, &ch, &backoff_time, &time)); +} + +TEST_F(Test_LoRaPHYAS923, apply_DR_offset) +{ + //0, 1, 2, 3, 4, 5, -1, -2 + for (int i=0; i < 8; i++) { + uint8_t val = i>5?5:2; + EXPECT_TRUE(object->apply_DR_offset(0, i)); + } +} + diff --git a/UNITTESTS/features/lorawan/loraphyas923/unittest.cmake b/UNITTESTS/features/lorawan/loraphyas923/unittest.cmake new file mode 100644 index 0000000000..b4648ab904 --- /dev/null +++ b/UNITTESTS/features/lorawan/loraphyas923/unittest.cmake @@ -0,0 +1,40 @@ +#[[ + * 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. +]] + +# Unit test suite name +set(TEST_SUITE_NAME "lorawan_LoRaPHYAS923") + +# Source files +set(unittest-sources + ../features/lorawan/lorastack/phy/LoRaPHYAS923.cpp +) + +# Add test specific include paths +set(unittest-includes ${unittest-includes} + target_h + ../features/lorawan/lorastack/phy +) + +# Test & stub files +set(unittest-test-sources + features/lorawan/loraphyas923/Test_LoRaPHYAS923.cpp + stubs/LoRaPHY_stub.cpp + stubs/LoRaWANTimer_stub.cpp + stubs/mbed_assert_stub.c + +) + diff --git a/UNITTESTS/features/lorawan/loraphyau915/Test_LoRaPHYAU915.cpp b/UNITTESTS/features/lorawan/loraphyau915/Test_LoRaPHYAU915.cpp new file mode 100644 index 0000000000..0cdde0f104 --- /dev/null +++ b/UNITTESTS/features/lorawan/loraphyau915/Test_LoRaPHYAU915.cpp @@ -0,0 +1,231 @@ +/* + * Copyright (c) 2018, Arm Limited and affiliates + * SPDX-License-Identifier: Apache-2.0 + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "gtest/gtest.h" +#include "LoRaPHYAU915.h" + +#include "LoRaPHY_stub.h" + +class my_radio : public LoRaRadio +{ +public: + + virtual void init_radio(radio_events_t *events){}; + + virtual void radio_reset(){}; + + virtual void sleep(void){}; + + virtual void standby(void){}; + + virtual void set_rx_config (radio_modems_t modem, uint32_t bandwidth, + uint32_t datarate, uint8_t coderate, + uint32_t bandwidth_afc, uint16_t preamble_len, + uint16_t symb_timeout, bool fix_len, + uint8_t payload_len, + bool crc_on, bool freq_hop_on, uint8_t hop_period, + bool iq_inverted, bool rx_continuous){}; + + virtual void set_tx_config(radio_modems_t modem, int8_t power, uint32_t fdev, + uint32_t bandwidth, uint32_t datarate, + uint8_t coderate, uint16_t preamble_len, + bool fix_len, bool crc_on, bool freq_hop_on, + uint8_t hop_period, bool iq_inverted, uint32_t timeout){}; + + virtual void send(uint8_t *buffer, uint8_t size){}; + + virtual void receive(void){}; + + virtual void set_channel(uint32_t freq){}; + + virtual uint32_t random(void){}; + + virtual uint8_t get_status(void){return uint8_value;}; + + virtual void set_max_payload_length(radio_modems_t modem, uint8_t max){}; + + virtual void set_public_network(bool enable){}; + + virtual uint32_t time_on_air(radio_modems_t modem, uint8_t pkt_len){}; + + virtual bool perform_carrier_sense(radio_modems_t modem, + uint32_t freq, + int16_t rssi_threshold, + uint32_t max_carrier_sense_time){ return bool_value;}; + + virtual void start_cad(void){}; + + virtual bool check_rf_frequency(uint32_t frequency){ return bool_value; }; + + virtual void set_tx_continuous_wave(uint32_t freq, int8_t power, uint16_t time){}; + + virtual void lock(void){}; + + virtual void unlock(void){}; + + bool bool_value; + uint8_t uint8_value; +}; + +class Test_LoRaPHYAU915 : public testing::Test { +protected: + LoRaPHYAU915 *object; + my_radio radio; + + virtual void SetUp() + { + LoRaPHY_stub::radio = &radio; + object = new LoRaPHYAU915(); + } + + virtual void TearDown() + { + LoRaPHY_stub::radio = NULL; + delete object; + } +}; + +TEST_F(Test_LoRaPHYAU915, constructor) +{ + EXPECT_TRUE(object); +} + +TEST_F(Test_LoRaPHYAU915, rx_config) +{ + rx_config_params_t p; + + radio.uint8_value = 1; + EXPECT_TRUE(!object->rx_config(&p)); + + radio.uint8_value = 0; + p.is_repeater_supported = true; + EXPECT_TRUE(object->rx_config(&p)); + + p.is_repeater_supported = false; + EXPECT_TRUE(object->rx_config(&p)); +} + +TEST_F(Test_LoRaPHYAU915, tx_config) +{ + tx_config_params_t p; + int8_t tx; + lorawan_time_t time; + p.tx_power = 9; + EXPECT_TRUE(object->tx_config(&p, &tx, &time)); +} + +TEST_F(Test_LoRaPHYAU915, link_ADR_request) +{ + adr_req_params_t params; + int8_t dr_out; + int8_t tx_power_out; + uint8_t nb_rep_out; + uint8_t nb_bytes_parsed; + + LoRaPHY_stub::uint8_value = 1; + LoRaPHY_stub::ch_mask_value = 6; + LoRaPHY_stub::adr_parse_count = 2; + EXPECT_TRUE(1 == object->link_ADR_request(¶ms, &dr_out, &tx_power_out, &nb_rep_out, &nb_bytes_parsed)); + + LoRaPHY_stub::adr_parse_count = 2; + LoRaPHY_stub::ch_mask_value = 7; + EXPECT_TRUE(1 == object->link_ADR_request(¶ms, &dr_out, &tx_power_out, &nb_rep_out, &nb_bytes_parsed)); + + LoRaPHY_stub::adr_parse_count = 2; + LoRaPHY_stub::ch_mask_value = 5; + LoRaPHY_stub::uint8_value = 6; + EXPECT_TRUE(6 == object->link_ADR_request(¶ms, &dr_out, &tx_power_out, &nb_rep_out, &nb_bytes_parsed)); + + LoRaPHY_stub::adr_parse_count = 2; + LoRaPHY_stub::ch_mask_value = 66; + LoRaPHY_stub::uint8_value = 7; + EXPECT_TRUE(7 == object->link_ADR_request(¶ms, &dr_out, &tx_power_out, &nb_rep_out, &nb_bytes_parsed)); +} + +TEST_F(Test_LoRaPHYAU915, accept_rx_param_setup_req) +{ + rx_param_setup_req_t p; + radio.bool_value = false; + EXPECT_TRUE(0 == object->accept_rx_param_setup_req(&p)); + + radio.bool_value = true; + p.frequency = 923300000 - 1; + EXPECT_TRUE(0 == object->accept_rx_param_setup_req(&p)); + + radio.bool_value = true; + p.frequency = 927500000 + 1; + p.datarate = 6; + LoRaPHY_stub::bool_counter = 0; + LoRaPHY_stub::bool_table[0] = true; + EXPECT_TRUE(2 == object->accept_rx_param_setup_req(&p)); + + radio.bool_value = true; + p.frequency = 923300000 + 600000; + LoRaPHY_stub::bool_counter = 0; + LoRaPHY_stub::bool_table[0] = true; + LoRaPHY_stub::bool_table[1] = true; + EXPECT_TRUE(7 == object->accept_rx_param_setup_req(&p)); +} + +TEST_F(Test_LoRaPHYAU915, get_alternate_DR) +{ + EXPECT_TRUE(0 == object->get_alternate_DR(0)); + + EXPECT_TRUE(6 == object->get_alternate_DR(1)); +} + +TEST_F(Test_LoRaPHYAU915, set_next_channel) +{ + channel_selection_params_t params; + uint8_t channel; + lorawan_time_t time; + lorawan_time_t timeoff; + + params.current_datarate = 6; + params.aggregate_timeoff = 0; + LoRaPHY_stub::uint8_value = 0; + EXPECT_TRUE(LORAWAN_STATUS_NO_CHANNEL_FOUND == object->set_next_channel(¶ms, &channel, &time, &timeoff)); + + radio.bool_value = false; + params.aggregate_timeoff = 1; + EXPECT_TRUE(LORAWAN_STATUS_DUTYCYCLE_RESTRICTED == object->set_next_channel(¶ms, &channel, &time, &timeoff)); + + params.aggregate_timeoff = 0; + LoRaPHY_stub::uint8_value = 1; + EXPECT_TRUE(LORAWAN_STATUS_OK == object->set_next_channel(¶ms, &channel, &time, &timeoff)); +} + +TEST_F(Test_LoRaPHYAU915, apply_DR_offset) +{ +// { DR_8, DR_8, DR_8, DR_8, DR_8, DR_8 }, // DR_0 +// { DR_9, DR_8, DR_8, DR_8, DR_8, DR_8 }, // DR_1 +// { DR_10, DR_9, DR_8, DR_8, DR_8, DR_8 }, // DR_2 +// { DR_11, DR_10, DR_9, DR_8, DR_8, DR_8 }, // DR_3 +// { DR_12, DR_11, DR_10, DR_9, DR_8, DR_8 }, // DR_4 +// { DR_13, DR_12, DR_11, DR_10, DR_9, DR_8 }, // DR_5 +// { DR_13, DR_13, DR_12, DR_11, DR_10, DR_9 }, // DR_6 + + for (int i = 0; i < 7; i++) { + for (int j=0; j < 6; j++ ) { + uint8_t val = 8 + i; + val -= j; + if (val > 13) val = 13; + if (val < 8) val = 8; + EXPECT_TRUE(val == object->apply_DR_offset(i, j)); + } + } +} diff --git a/UNITTESTS/features/lorawan/loraphyau915/unittest.cmake b/UNITTESTS/features/lorawan/loraphyau915/unittest.cmake new file mode 100644 index 0000000000..abc6c4752c --- /dev/null +++ b/UNITTESTS/features/lorawan/loraphyau915/unittest.cmake @@ -0,0 +1,44 @@ +#[[ + * 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. +]] + +# Unit test suite name +set(TEST_SUITE_NAME "lorawan_LoRaPHYAU915") + +# Source files +set(unittest-sources + ../features/lorawan/lorastack/phy/LoRaPHYAU915.cpp +) + +# Add test specific include paths +set(unittest-includes ${unittest-includes} + target_h + ../features/lorawan/lorastack/phy +) + +# Test & stub files +set(unittest-test-sources + features/lorawan/loraphyau915/Test_LoRaPHYAU915.cpp + stubs/LoRaPHY_stub.cpp + stubs/LoRaWANTimer_stub.cpp + stubs/mbed_assert_stub.c + +) + +set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DMBED_CONF_LORA_FSB_MASK=\"{0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0x00FF}\"") +set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DMBED_CONF_LORA_FSB_MASK=\"{0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0x00FF}\"") + + diff --git a/UNITTESTS/features/lorawan/loraphycn470/Test_LoRaPHYCN470.cpp b/UNITTESTS/features/lorawan/loraphycn470/Test_LoRaPHYCN470.cpp new file mode 100644 index 0000000000..983dec935e --- /dev/null +++ b/UNITTESTS/features/lorawan/loraphycn470/Test_LoRaPHYCN470.cpp @@ -0,0 +1,199 @@ +/* + * 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 "LoRaPHYCN470.h" + +#include "LoRaPHY_stub.h" + +class my_radio : public LoRaRadio +{ +public: + + virtual void init_radio(radio_events_t *events){}; + + virtual void radio_reset(){}; + + virtual void sleep(void){}; + + virtual void standby(void){}; + + virtual void set_rx_config (radio_modems_t modem, uint32_t bandwidth, + uint32_t datarate, uint8_t coderate, + uint32_t bandwidth_afc, uint16_t preamble_len, + uint16_t symb_timeout, bool fix_len, + uint8_t payload_len, + bool crc_on, bool freq_hop_on, uint8_t hop_period, + bool iq_inverted, bool rx_continuous){}; + + virtual void set_tx_config(radio_modems_t modem, int8_t power, uint32_t fdev, + uint32_t bandwidth, uint32_t datarate, + uint8_t coderate, uint16_t preamble_len, + bool fix_len, bool crc_on, bool freq_hop_on, + uint8_t hop_period, bool iq_inverted, uint32_t timeout){}; + + virtual void send(uint8_t *buffer, uint8_t size){}; + + virtual void receive(void){}; + + virtual void set_channel(uint32_t freq){}; + + virtual uint32_t random(void){}; + + virtual uint8_t get_status(void){return uint8_value;}; + + virtual void set_max_payload_length(radio_modems_t modem, uint8_t max){}; + + virtual void set_public_network(bool enable){}; + + virtual uint32_t time_on_air(radio_modems_t modem, uint8_t pkt_len){}; + + virtual bool perform_carrier_sense(radio_modems_t modem, + uint32_t freq, + int16_t rssi_threshold, + uint32_t max_carrier_sense_time){ return bool_value;}; + + virtual void start_cad(void){}; + + virtual bool check_rf_frequency(uint32_t frequency){ return bool_value; }; + + virtual void set_tx_continuous_wave(uint32_t freq, int8_t power, uint16_t time){}; + + virtual void lock(void){}; + + virtual void unlock(void){}; + + bool bool_value; + uint8_t uint8_value; +}; + +class Test_LoRaPHYCN470 : public testing::Test { +protected: + LoRaPHYCN470 *object; + my_radio radio; + + virtual void SetUp() + { + + LoRaPHY_stub::radio = &radio; + object = new LoRaPHYCN470(); + } + + virtual void TearDown() + { + + LoRaPHY_stub::radio = NULL; + delete object; + } +}; + +TEST_F(Test_LoRaPHYCN470, constructor) +{ + EXPECT_TRUE(object); +} + +TEST_F(Test_LoRaPHYCN470, set_next_channel) +{ + channel_selection_params_t params; + uint8_t channel; + lorawan_time_t time; + lorawan_time_t timeoff; + + params.current_datarate = 4; + params.aggregate_timeoff = 0; + LoRaPHY_stub::uint8_value = 0; + EXPECT_TRUE(LORAWAN_STATUS_NO_CHANNEL_FOUND == object->set_next_channel(¶ms, &channel, &time, &timeoff)); + + radio.bool_value = false; + params.aggregate_timeoff = 1; + EXPECT_TRUE(LORAWAN_STATUS_DUTYCYCLE_RESTRICTED == object->set_next_channel(¶ms, &channel, &time, &timeoff)); + + params.aggregate_timeoff = 0; + LoRaPHY_stub::uint8_value = 1; + EXPECT_TRUE(LORAWAN_STATUS_OK == object->set_next_channel(¶ms, &channel, &time, &timeoff)); +} + +TEST_F(Test_LoRaPHYCN470, rx_config) +{ + rx_config_params_t p; + + radio.uint8_value = 1; + EXPECT_TRUE(!object->rx_config(&p)); + + radio.uint8_value = 0; + p.is_repeater_supported = true; + EXPECT_TRUE(object->rx_config(&p)); + + p.is_repeater_supported = false; + EXPECT_TRUE(object->rx_config(&p)); +} + +TEST_F(Test_LoRaPHYCN470, tx_config) +{ + tx_config_params_t p; + int8_t tx; + lorawan_time_t time; + p.tx_power = 9; + EXPECT_TRUE(object->tx_config(&p, &tx, &time)); +} + +TEST_F(Test_LoRaPHYCN470, link_ADR_request) +{ + adr_req_params_t params; + int8_t dr_out; + int8_t tx_power_out; + uint8_t nb_rep_out; + uint8_t nb_bytes_parsed; + + LoRaPHY_stub::uint8_value = 1; + LoRaPHY_stub::ch_mask_value = 6; + LoRaPHY_stub::adr_parse_count = 2; + EXPECT_TRUE(1 == object->link_ADR_request(¶ms, &dr_out, &tx_power_out, &nb_rep_out, &nb_bytes_parsed)); + + LoRaPHY_stub::adr_parse_count = 2; + LoRaPHY_stub::ch_mask_value = 7; + EXPECT_TRUE(1 == object->link_ADR_request(¶ms, &dr_out, &tx_power_out, &nb_rep_out, &nb_bytes_parsed)); + + LoRaPHY_stub::adr_parse_count = 2; + LoRaPHY_stub::ch_mask_value = 5; + LoRaPHY_stub::uint8_value = 6; + EXPECT_TRUE(6 == object->link_ADR_request(¶ms, &dr_out, &tx_power_out, &nb_rep_out, &nb_bytes_parsed)); + + LoRaPHY_stub::adr_parse_count = 2; + LoRaPHY_stub::ch_mask_value = 66; + LoRaPHY_stub::uint8_value = 7; + EXPECT_TRUE(7 == object->link_ADR_request(¶ms, &dr_out, &tx_power_out, &nb_rep_out, &nb_bytes_parsed)); +} + +TEST_F(Test_LoRaPHYCN470, accept_rx_param_setup_req) +{ + rx_param_setup_req_t p; + EXPECT_TRUE(0 == object->accept_rx_param_setup_req(&p)); + + radio.bool_value = true; + p.frequency = 923300000 - 1; + EXPECT_TRUE(0 == object->accept_rx_param_setup_req(&p)); + + radio.bool_value = true; + p.frequency = 927500000 + 1; + p.datarate = 6; + LoRaPHY_stub::bool_counter = 0; + LoRaPHY_stub::bool_table[0] = true; + EXPECT_TRUE(2 == object->accept_rx_param_setup_req(&p)); +} + + diff --git a/UNITTESTS/features/lorawan/loraphycn470/unittest.cmake b/UNITTESTS/features/lorawan/loraphycn470/unittest.cmake new file mode 100644 index 0000000000..6c6746f189 --- /dev/null +++ b/UNITTESTS/features/lorawan/loraphycn470/unittest.cmake @@ -0,0 +1,40 @@ +#[[ + * 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. +]] + +# Unit test suite name +set(TEST_SUITE_NAME "lorawan_LoRaPHYCN470") + +# Source files +set(unittest-sources + ../features/lorawan/lorastack/phy/LoRaPHYCN470.cpp +) + +# Add test specific include paths +set(unittest-includes ${unittest-includes} + target_h + ../features/lorawan/lorastack/phy +) + +# Test & stub files +set(unittest-test-sources + features/lorawan/loraphycn470/Test_LoRaPHYCN470.cpp + stubs/LoRaPHY_stub.cpp + stubs/LoRaWANTimer_stub.cpp + stubs/mbed_assert_stub.c + +) + diff --git a/UNITTESTS/features/lorawan/loraphycn779/Test_LoRaPHYCN779.cpp b/UNITTESTS/features/lorawan/loraphycn779/Test_LoRaPHYCN779.cpp new file mode 100644 index 0000000000..0d729b4c87 --- /dev/null +++ b/UNITTESTS/features/lorawan/loraphycn779/Test_LoRaPHYCN779.cpp @@ -0,0 +1,40 @@ +/* + * 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 "LoRaPHYCN779.h" + +class Test_LoRaPHYCN779 : public testing::Test { +protected: + LoRaPHYCN779 *object; + + virtual void SetUp() + { + object = new LoRaPHYCN779(); + } + + virtual void TearDown() + { + delete object; + } +}; + +TEST_F(Test_LoRaPHYCN779, constructor) +{ + EXPECT_TRUE(object); +} + diff --git a/UNITTESTS/features/lorawan/loraphycn779/unittest.cmake b/UNITTESTS/features/lorawan/loraphycn779/unittest.cmake new file mode 100644 index 0000000000..d0ccd7fece --- /dev/null +++ b/UNITTESTS/features/lorawan/loraphycn779/unittest.cmake @@ -0,0 +1,42 @@ +#[[ + * 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. +]] + +# Unit test suite name +set(TEST_SUITE_NAME "lorawan_LoRaPHYCN779") + +# Source files +set(unittest-sources + ../features/lorawan/lorastack/phy/LoRaPHYCN779.cpp +) + +# Add test specific include paths +set(unittest-includes ${unittest-includes} + target_h + ../features/lorawan/lorastack/phy +) + +# Test & stub files +set(unittest-test-sources + features/lorawan/loraphycn779/Test_LoRaPHYCN779.cpp + stubs/LoRaPHY_stub.cpp + stubs/mbed_assert_stub.c + +) + + +set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DMBED_CONF_LORA_FSB_MASK_CHINA=\"{0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0x00FF}\"") +set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DMBED_CONF_LORA_FSB_MASK_CHINA=\"{0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0x00FF}\"") diff --git a/UNITTESTS/features/lorawan/loraphyeu433/Test_LoRaPHYEU433.cpp b/UNITTESTS/features/lorawan/loraphyeu433/Test_LoRaPHYEU433.cpp new file mode 100644 index 0000000000..1981943a63 --- /dev/null +++ b/UNITTESTS/features/lorawan/loraphyeu433/Test_LoRaPHYEU433.cpp @@ -0,0 +1,40 @@ +/* + * 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 "LoRaPHYEU433.h" + +class Test_LoRaPHYEU433 : public testing::Test { +protected: + LoRaPHYEU433 *object; + + virtual void SetUp() + { + object = new LoRaPHYEU433(); + } + + virtual void TearDown() + { + delete object; + } +}; + +TEST_F(Test_LoRaPHYEU433, constructor) +{ + EXPECT_TRUE(object); +} + diff --git a/UNITTESTS/features/lorawan/loraphyeu433/unittest.cmake b/UNITTESTS/features/lorawan/loraphyeu433/unittest.cmake new file mode 100644 index 0000000000..5966a3ee60 --- /dev/null +++ b/UNITTESTS/features/lorawan/loraphyeu433/unittest.cmake @@ -0,0 +1,39 @@ +#[[ + * 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. +]] + +# Unit test suite name +set(TEST_SUITE_NAME "lorawan_LoRaPHYEU433") + +# Source files +set(unittest-sources + ../features/lorawan/lorastack/phy/LoRaPHYEU433.cpp +) + +# Add test specific include paths +set(unittest-includes ${unittest-includes} + target_h + ../features/lorawan/lorastack/phy +) + +# Test & stub files +set(unittest-test-sources + features/lorawan/loraphyeu433/Test_LoRaPHYEU433.cpp + stubs/LoRaPHY_stub.cpp + stubs/mbed_assert_stub.c + +) + diff --git a/UNITTESTS/features/lorawan/loraphyeu868/Test_LoRaPHYEU868.cpp b/UNITTESTS/features/lorawan/loraphyeu868/Test_LoRaPHYEU868.cpp new file mode 100644 index 0000000000..f216c12016 --- /dev/null +++ b/UNITTESTS/features/lorawan/loraphyeu868/Test_LoRaPHYEU868.cpp @@ -0,0 +1,40 @@ +/* + * 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 "LoRaPHYEU868.h" + +class Test_LoRaPHYEU868 : public testing::Test { +protected: + LoRaPHYEU868 *object; + + virtual void SetUp() + { + object = new LoRaPHYEU868(); + } + + virtual void TearDown() + { + delete object; + } +}; + +TEST_F(Test_LoRaPHYEU868, constructor) +{ + EXPECT_TRUE(object); +} + diff --git a/UNITTESTS/features/lorawan/loraphyeu868/unittest.cmake b/UNITTESTS/features/lorawan/loraphyeu868/unittest.cmake new file mode 100644 index 0000000000..1398a52257 --- /dev/null +++ b/UNITTESTS/features/lorawan/loraphyeu868/unittest.cmake @@ -0,0 +1,39 @@ +#[[ + * 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. +]] + +# Unit test suite name +set(TEST_SUITE_NAME "lorawan_LoRaPHYEU868") + +# Source files +set(unittest-sources + ../features/lorawan/lorastack/phy/LoRaPHYEU868.cpp +) + +# Add test specific include paths +set(unittest-includes ${unittest-includes} + target_h + ../features/lorawan/lorastack/phy +) + +# Test & stub files +set(unittest-test-sources + features/lorawan/loraphyeu868/Test_LoRaPHYEU868.cpp + stubs/LoRaPHY_stub.cpp + stubs/mbed_assert_stub.c + +) + diff --git a/UNITTESTS/features/lorawan/loraphyin865/Test_LoRaPHYIN865.cpp b/UNITTESTS/features/lorawan/loraphyin865/Test_LoRaPHYIN865.cpp new file mode 100644 index 0000000000..ecb12115f4 --- /dev/null +++ b/UNITTESTS/features/lorawan/loraphyin865/Test_LoRaPHYIN865.cpp @@ -0,0 +1,47 @@ +/* + * 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 "LoRaPHYIN865.h" + +class Test_LoRaPHYIN865 : public testing::Test { +protected: + LoRaPHYIN865 *object; + + virtual void SetUp() + { + object = new LoRaPHYIN865(); + } + + virtual void TearDown() + { + delete object; + } +}; + +TEST_F(Test_LoRaPHYIN865, constructor) +{ + EXPECT_TRUE(object); +} + +TEST_F(Test_LoRaPHYIN865, apply_DR_offset) +{ + EXPECT_TRUE(0 == object->apply_DR_offset(0, 0)); +} + + + diff --git a/UNITTESTS/features/lorawan/loraphyin865/unittest.cmake b/UNITTESTS/features/lorawan/loraphyin865/unittest.cmake new file mode 100644 index 0000000000..2fa34a6f73 --- /dev/null +++ b/UNITTESTS/features/lorawan/loraphyin865/unittest.cmake @@ -0,0 +1,39 @@ +#[[ + * 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. +]] + +# Unit test suite name +set(TEST_SUITE_NAME "lorawan_LoRaPHYIN865") + +# Source files +set(unittest-sources + ../features/lorawan/lorastack/phy/LoRaPHYIN865.cpp +) + +# Add test specific include paths +set(unittest-includes ${unittest-includes} + target_h + ../features/lorawan/lorastack/phy +) + +# Test & stub files +set(unittest-test-sources + features/lorawan/loraphyin865/Test_LoRaPHYIN865.cpp + stubs/LoRaPHY_stub.cpp + stubs/mbed_assert_stub.c + +) + diff --git a/UNITTESTS/features/lorawan/loraphykr920/Test_LoRaPHYKR920.cpp b/UNITTESTS/features/lorawan/loraphykr920/Test_LoRaPHYKR920.cpp new file mode 100644 index 0000000000..5efab34c77 --- /dev/null +++ b/UNITTESTS/features/lorawan/loraphykr920/Test_LoRaPHYKR920.cpp @@ -0,0 +1,156 @@ +/* + * 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 "LoRaPHYKR920.h" +#include "LoRaPHY_stub.h" +#include "LoRaRadio.h" + +class my_radio : public LoRaRadio +{ +public: + + virtual void init_radio(radio_events_t *events){}; + + virtual void radio_reset(){}; + + virtual void sleep(void){}; + + virtual void standby(void){}; + + virtual void set_rx_config (radio_modems_t modem, uint32_t bandwidth, + uint32_t datarate, uint8_t coderate, + uint32_t bandwidth_afc, uint16_t preamble_len, + uint16_t symb_timeout, bool fix_len, + uint8_t payload_len, + bool crc_on, bool freq_hop_on, uint8_t hop_period, + bool iq_inverted, bool rx_continuous){}; + + virtual void set_tx_config(radio_modems_t modem, int8_t power, uint32_t fdev, + uint32_t bandwidth, uint32_t datarate, + uint8_t coderate, uint16_t preamble_len, + bool fix_len, bool crc_on, bool freq_hop_on, + uint8_t hop_period, bool iq_inverted, uint32_t timeout){}; + + virtual void send(uint8_t *buffer, uint8_t size){}; + + virtual void receive(void){}; + + virtual void set_channel(uint32_t freq){}; + + virtual uint32_t random(void){}; + + virtual uint8_t get_status(void){}; + + virtual void set_max_payload_length(radio_modems_t modem, uint8_t max){}; + + virtual void set_public_network(bool enable){}; + + virtual uint32_t time_on_air(radio_modems_t modem, uint8_t pkt_len){}; + + virtual bool perform_carrier_sense(radio_modems_t modem, + uint32_t freq, + int16_t rssi_threshold, + uint32_t max_carrier_sense_time){ return bool_value;}; + + virtual void start_cad(void){}; + + virtual bool check_rf_frequency(uint32_t frequency){ return bool_value; }; + + virtual void set_tx_continuous_wave(uint32_t freq, int8_t power, uint16_t time){}; + + virtual void lock(void){}; + + virtual void unlock(void){}; + + bool bool_value; +}; + +class Test_LoRaPHYKR920 : public testing::Test { +protected: + LoRaPHYKR920 *object; + my_radio radio; + + virtual void SetUp() + { + LoRaPHY_stub::radio = &radio; + object = new LoRaPHYKR920(); + } + + virtual void TearDown() + { + LoRaPHY_stub::radio = NULL; + delete object; + } +}; + +TEST_F(Test_LoRaPHYKR920, constructor) +{ + EXPECT_TRUE(object); +} + +TEST_F(Test_LoRaPHYKR920, verify_frequency_for_band) +{ + radio.bool_value = false; + EXPECT_TRUE(false == object->verify_frequency_for_band(0, 0)); + + radio.bool_value = true; + EXPECT_TRUE(false == object->verify_frequency_for_band(0, 0)); + + EXPECT_TRUE(true == object->verify_frequency_for_band(921100000, 0)); +} + +TEST_F(Test_LoRaPHYKR920, tx_config) +{ + tx_config_params_t tx_config; + int8_t tx_power = 0; + lorawan_time_t time; + + tx_config.tx_power = 9; + EXPECT_TRUE(true == object->tx_config(&tx_config, &tx_power, &time)); +} + +TEST_F(Test_LoRaPHYKR920, set_next_channel) +{ + channel_selection_params_t next_channel; + lorawan_time_t backoff_time = 0; + lorawan_time_t time = 0; + uint8_t ch = 1; + + next_channel.aggregate_timeoff = 0; + LoRaPHY_stub::uint8_value = 0; + EXPECT_TRUE(LORAWAN_STATUS_NO_CHANNEL_FOUND == object->set_next_channel(&next_channel, &ch, &backoff_time, &time)); + + next_channel.aggregate_timeoff = 1; + radio.bool_value = false; + EXPECT_TRUE(LORAWAN_STATUS_DUTYCYCLE_RESTRICTED == object->set_next_channel(&next_channel, &ch, &backoff_time, &time)); + + next_channel.aggregate_timeoff = 0; + LoRaPHY_stub::uint8_value = 1; + EXPECT_TRUE(LORAWAN_STATUS_NO_FREE_CHANNEL_FOUND == object->set_next_channel(&next_channel, &ch, &backoff_time, &time)); + + radio.bool_value = true; + EXPECT_TRUE(LORAWAN_STATUS_OK == object->set_next_channel(&next_channel, &ch, &backoff_time, &time)); +} + +TEST_F(Test_LoRaPHYKR920, set_tx_cont_mode) +{ + cw_mode_params_t params; + params.tx_power = 9; + object->set_tx_cont_mode(¶ms, 0); +} + diff --git a/UNITTESTS/features/lorawan/loraphykr920/unittest.cmake b/UNITTESTS/features/lorawan/loraphykr920/unittest.cmake new file mode 100644 index 0000000000..cc32c13f02 --- /dev/null +++ b/UNITTESTS/features/lorawan/loraphykr920/unittest.cmake @@ -0,0 +1,40 @@ +#[[ + * 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. +]] + +# Unit test suite name +set(TEST_SUITE_NAME "lorawan_LoRaPHYKR920") + +# Source files +set(unittest-sources + ../features/lorawan/lorastack/phy/LoRaPHYKR920.cpp +) + +# Add test specific include paths +set(unittest-includes ${unittest-includes} + target_h + ../features/lorawan/lorastack/phy +) + +# Test & stub files +set(unittest-test-sources + features/lorawan/loraphykr920/Test_LoRaPHYKR920.cpp + stubs/LoRaPHY_stub.cpp + stubs/LoRaWANTimer_stub.cpp + stubs/mbed_assert_stub.c + +) + diff --git a/UNITTESTS/features/lorawan/loraphyus915/Test_LoRaPHYUS915.cpp b/UNITTESTS/features/lorawan/loraphyus915/Test_LoRaPHYUS915.cpp new file mode 100644 index 0000000000..4bd793f703 --- /dev/null +++ b/UNITTESTS/features/lorawan/loraphyus915/Test_LoRaPHYUS915.cpp @@ -0,0 +1,235 @@ +/* + * 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 "LoRaPHYUS915.h" +#include "LoRaPHY_stub.h" + +class my_radio : public LoRaRadio +{ +public: + + virtual void init_radio(radio_events_t *events){}; + + virtual void radio_reset(){}; + + virtual void sleep(void){}; + + virtual void standby(void){}; + + virtual void set_rx_config (radio_modems_t modem, uint32_t bandwidth, + uint32_t datarate, uint8_t coderate, + uint32_t bandwidth_afc, uint16_t preamble_len, + uint16_t symb_timeout, bool fix_len, + uint8_t payload_len, + bool crc_on, bool freq_hop_on, uint8_t hop_period, + bool iq_inverted, bool rx_continuous){}; + + virtual void set_tx_config(radio_modems_t modem, int8_t power, uint32_t fdev, + uint32_t bandwidth, uint32_t datarate, + uint8_t coderate, uint16_t preamble_len, + bool fix_len, bool crc_on, bool freq_hop_on, + uint8_t hop_period, bool iq_inverted, uint32_t timeout){}; + + virtual void send(uint8_t *buffer, uint8_t size){}; + + virtual void receive(void){}; + + virtual void set_channel(uint32_t freq){}; + + virtual uint32_t random(void){}; + + virtual uint8_t get_status(void){return uint8_value;}; + + virtual void set_max_payload_length(radio_modems_t modem, uint8_t max){}; + + virtual void set_public_network(bool enable){}; + + virtual uint32_t time_on_air(radio_modems_t modem, uint8_t pkt_len){}; + + virtual bool perform_carrier_sense(radio_modems_t modem, + uint32_t freq, + int16_t rssi_threshold, + uint32_t max_carrier_sense_time){ return bool_value;}; + + virtual void start_cad(void){}; + + virtual bool check_rf_frequency(uint32_t frequency){ return bool_value; }; + + virtual void set_tx_continuous_wave(uint32_t freq, int8_t power, uint16_t time){}; + + virtual void lock(void){}; + + virtual void unlock(void){}; + + bool bool_value; + uint8_t uint8_value; +}; + +class Test_LoRaPHYUS915 : public testing::Test { +protected: + LoRaPHYUS915 *object; + my_radio radio; + + virtual void SetUp() + { + LoRaPHY_stub::radio = &radio; + object = new LoRaPHYUS915(); + } + + virtual void TearDown() + { + LoRaPHY_stub::radio = NULL; + delete object; + } +}; + +TEST_F(Test_LoRaPHYUS915, constructor) +{ + EXPECT_TRUE(object); +} + +TEST_F(Test_LoRaPHYUS915, restore_default_channels) +{ + object->restore_default_channels(); +} + +TEST_F(Test_LoRaPHYUS915, rx_config) +{ + rx_config_params_t p; + + radio.uint8_value = 1; + EXPECT_TRUE(!object->rx_config(&p)); + + radio.uint8_value = 0; + p.is_repeater_supported = true; + EXPECT_TRUE(object->rx_config(&p)); + + p.is_repeater_supported = false; + EXPECT_TRUE(object->rx_config(&p)); +} + +TEST_F(Test_LoRaPHYUS915, tx_config) +{ + tx_config_params_t p; + int8_t tx; + lorawan_time_t time; + EXPECT_TRUE(object->tx_config(&p, &tx, &time)); +} + +TEST_F(Test_LoRaPHYUS915, link_ADR_request) +{ + adr_req_params_t params; + int8_t dr_out; + int8_t tx_power_out; + uint8_t nb_rep_out; + uint8_t nb_bytes_parsed; + + EXPECT_TRUE(0 == object->link_ADR_request(¶ms, &dr_out, &tx_power_out, &nb_rep_out, &nb_bytes_parsed)); + + LoRaPHY_stub::uint8_value = 1; + LoRaPHY_stub::ch_mask_value = 6; + LoRaPHY_stub::adr_parse_count = 2; + EXPECT_TRUE(1 == object->link_ADR_request(¶ms, &dr_out, &tx_power_out, &nb_rep_out, &nb_bytes_parsed)); + + LoRaPHY_stub::adr_parse_count = 2; + LoRaPHY_stub::ch_mask_value = 7; + EXPECT_TRUE(1 == object->link_ADR_request(¶ms, &dr_out, &tx_power_out, &nb_rep_out, &nb_bytes_parsed)); + + LoRaPHY_stub::adr_parse_count = 2; + LoRaPHY_stub::ch_mask_value = 5; + LoRaPHY_stub::uint8_value = 6; + EXPECT_TRUE(6 == object->link_ADR_request(¶ms, &dr_out, &tx_power_out, &nb_rep_out, &nb_bytes_parsed)); + + LoRaPHY_stub::adr_parse_count = 2; + LoRaPHY_stub::ch_mask_value = 66; + LoRaPHY_stub::uint8_value = 7; + EXPECT_TRUE(7 == object->link_ADR_request(¶ms, &dr_out, &tx_power_out, &nb_rep_out, &nb_bytes_parsed)); +} + +TEST_F(Test_LoRaPHYUS915, accept_rx_param_setup_req) +{ + rx_param_setup_req_t p; + EXPECT_TRUE(0 == object->accept_rx_param_setup_req(&p)); + + radio.bool_value = true; + p.frequency = 923300000 - 1; + EXPECT_TRUE(0 == object->accept_rx_param_setup_req(&p)); + + radio.bool_value = true; + p.frequency = 927500000 + 1; + p.datarate = 6; + LoRaPHY_stub::bool_counter = 0; + LoRaPHY_stub::bool_table[0] = true; + EXPECT_TRUE(2 == object->accept_rx_param_setup_req(&p)); +} + +TEST_F(Test_LoRaPHYUS915, get_alternate_DR) +{ + EXPECT_TRUE(0 == object->get_alternate_DR(0)); + + EXPECT_TRUE(4 == object->get_alternate_DR(1)); +} + +TEST_F(Test_LoRaPHYUS915, set_next_channel) +{ + channel_selection_params_t params; + uint8_t channel; + lorawan_time_t time; + lorawan_time_t timeoff; + + params.current_datarate = 4; + params.aggregate_timeoff = 0; + LoRaPHY_stub::uint8_value = 0; + EXPECT_TRUE(LORAWAN_STATUS_NO_CHANNEL_FOUND == object->set_next_channel(¶ms, &channel, &time, &timeoff)); + + radio.bool_value = false; + params.aggregate_timeoff = 1; + EXPECT_TRUE(LORAWAN_STATUS_DUTYCYCLE_RESTRICTED == object->set_next_channel(¶ms, &channel, &time, &timeoff)); + + params.aggregate_timeoff = 0; + LoRaPHY_stub::uint8_value = 1; + EXPECT_TRUE(LORAWAN_STATUS_OK == object->set_next_channel(¶ms, &channel, &time, &timeoff)); +} + +TEST_F(Test_LoRaPHYUS915, apply_DR_offset) +{ + //10, 9, 8, 8 + //11, 10, 9, 8 + //12, 11, 10, 9 + //13, 12, 11, 10 + //13, 13, 12, 11 + + for (int i = 0; i < 5; i++) { + for (int j=0; j < 4; j++ ) { + uint8_t val = 10 + i; + val -= j; + if (val > 13) val = 13; + if (val < 8) val = 8; + EXPECT_TRUE(val == object->apply_DR_offset(i, j)); + } + } +} + +TEST_F(Test_LoRaPHYUS915, set_tx_cont_mode) +{ + cw_mode_params_t p; + object->set_tx_cont_mode(&p, 0); + + p.datarate = 4; + object->set_tx_cont_mode(&p, 0); +} diff --git a/UNITTESTS/features/lorawan/loraphyus915/unittest.cmake b/UNITTESTS/features/lorawan/loraphyus915/unittest.cmake new file mode 100644 index 0000000000..f181da5e3f --- /dev/null +++ b/UNITTESTS/features/lorawan/loraphyus915/unittest.cmake @@ -0,0 +1,42 @@ +#[[ + * 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. +]] + +# Unit test suite name +set(TEST_SUITE_NAME "lorawan_LoRaPHYUS915") + +# Source files +set(unittest-sources + ../features/lorawan/lorastack/phy/LoRaPHYUS915.cpp +) + +# Add test specific include paths +set(unittest-includes ${unittest-includes} + target_h + ../features/lorawan/lorastack/phy +) + +# Test & stub files +set(unittest-test-sources + features/lorawan/loraphyus915/Test_LoRaPHYUS915.cpp + stubs/LoRaPHY_stub.cpp + stubs/LoRaWANTimer_stub.cpp + stubs/mbed_assert_stub.c + +) + +set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DMBED_CONF_LORA_FSB_MASK=\"{0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0x00FF}\"") +set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DMBED_CONF_LORA_FSB_MASK=\"{0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0x00FF}\"") diff --git a/UNITTESTS/features/lorawan/lorawaninterface/Test_LoRaWANInterface.cpp b/UNITTESTS/features/lorawan/lorawaninterface/Test_LoRaWANInterface.cpp new file mode 100644 index 0000000000..d02c9d68f7 --- /dev/null +++ b/UNITTESTS/features/lorawan/lorawaninterface/Test_LoRaWANInterface.cpp @@ -0,0 +1,229 @@ +/* + * 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 "LoRaWANInterface.h" + +class my_radio : public LoRaRadio +{ +public: + + virtual void init_radio(radio_events_t *events){}; + + virtual void radio_reset(){}; + + virtual void sleep(void){}; + + virtual void standby(void){}; + + virtual void set_rx_config (radio_modems_t modem, uint32_t bandwidth, + uint32_t datarate, uint8_t coderate, + uint32_t bandwidth_afc, uint16_t preamble_len, + uint16_t symb_timeout, bool fix_len, + uint8_t payload_len, + bool crc_on, bool freq_hop_on, uint8_t hop_period, + bool iq_inverted, bool rx_continuous){}; + + virtual void set_tx_config(radio_modems_t modem, int8_t power, uint32_t fdev, + uint32_t bandwidth, uint32_t datarate, + uint8_t coderate, uint16_t preamble_len, + bool fix_len, bool crc_on, bool freq_hop_on, + uint8_t hop_period, bool iq_inverted, uint32_t timeout){}; + + virtual void send(uint8_t *buffer, uint8_t size){}; + + virtual void receive(void){}; + + virtual void set_channel(uint32_t freq){}; + + virtual uint32_t random(void){}; + + virtual uint8_t get_status(void){}; + + virtual void set_max_payload_length(radio_modems_t modem, uint8_t max){}; + + virtual void set_public_network(bool enable){}; + + virtual uint32_t time_on_air(radio_modems_t modem, uint8_t pkt_len){}; + + virtual bool perform_carrier_sense(radio_modems_t modem, + uint32_t freq, + int16_t rssi_threshold, + uint32_t max_carrier_sense_time){}; + + virtual void start_cad(void){}; + + virtual bool check_rf_frequency(uint32_t frequency){}; + + virtual void set_tx_continuous_wave(uint32_t freq, int8_t power, uint16_t time){}; + + virtual void lock(void){}; + + virtual void unlock(void){}; +}; + +class my_LoRaPHY : public LoRaPHY +{ +public: + my_LoRaPHY(){}; + + virtual ~my_LoRaPHY(){}; +}; + +class Test_LoRaWANInterface : public testing::Test { +protected: + LoRaWANInterface *object; + my_radio radio; + + virtual void SetUp() + { + object = new LoRaWANInterface(radio); + } + + virtual void TearDown() + { + delete object; + } +}; + +TEST_F(Test_LoRaWANInterface, constructor) +{ + EXPECT_TRUE(object); + + my_radio radio; + my_LoRaPHY phy; + LoRaWANInterface object(radio, phy); +} + +TEST_F(Test_LoRaWANInterface, initialize) +{ + EXPECT_TRUE(LORAWAN_STATUS_OK == object->initialize(NULL)); +} + +TEST_F(Test_LoRaWANInterface, connect) +{ + EXPECT_TRUE(LORAWAN_STATUS_OK == object->connect()); + + lorawan_connect_t conn; + EXPECT_TRUE(LORAWAN_STATUS_OK == object->connect(conn)); +} + +TEST_F(Test_LoRaWANInterface, disconnect) +{ + EXPECT_TRUE(LORAWAN_STATUS_OK == object->disconnect()); +} + +TEST_F(Test_LoRaWANInterface, add_link_check_request) +{ + EXPECT_TRUE(LORAWAN_STATUS_OK == object->add_link_check_request()); +} + +TEST_F(Test_LoRaWANInterface, remove_link_check_request) +{ + object->remove_link_check_request(); +} + +TEST_F(Test_LoRaWANInterface, set_datarate) +{ + EXPECT_TRUE(LORAWAN_STATUS_OK == object->set_datarate(1)); +} + +TEST_F(Test_LoRaWANInterface, enable_adaptive_datarate) +{ + EXPECT_TRUE(LORAWAN_STATUS_OK == object->enable_adaptive_datarate()); +} + +TEST_F(Test_LoRaWANInterface, disable_adaptive_datarate) +{ + object->disable_adaptive_datarate(); +} + +TEST_F(Test_LoRaWANInterface, set_confirmed_msg_retries) +{ + EXPECT_TRUE(LORAWAN_STATUS_OK == object->set_confirmed_msg_retries(1)); +} + +TEST_F(Test_LoRaWANInterface, set_channel_plan) +{ + lorawan_channelplan_t plan; + EXPECT_TRUE(LORAWAN_STATUS_OK == object->set_channel_plan(plan)); +} + +TEST_F(Test_LoRaWANInterface, get_channel_plan) +{ + lorawan_channelplan_t plan; + EXPECT_TRUE(LORAWAN_STATUS_OK == object->get_channel_plan(plan)); +} + +TEST_F(Test_LoRaWANInterface, remove_channel_plan) +{ + EXPECT_TRUE(LORAWAN_STATUS_OK == object->remove_channel_plan()); +} + +TEST_F(Test_LoRaWANInterface, remove_channel) +{ + EXPECT_TRUE(LORAWAN_STATUS_OK == object->remove_channel(1)); +} + +TEST_F(Test_LoRaWANInterface, send) +{ + EXPECT_TRUE(0 == object->send(1, NULL, 0, 0)); +} + +TEST_F(Test_LoRaWANInterface, receive) +{ + EXPECT_TRUE(0 == object->receive(1, NULL, 0, 0)); + + uint8_t port; + int flags; + EXPECT_TRUE(0 == object->receive(NULL, 0, port, flags)); +} + +TEST_F(Test_LoRaWANInterface, add_app_callbacks) +{ + lorawan_app_callbacks_t cbs; + EXPECT_TRUE(LORAWAN_STATUS_OK == object->add_app_callbacks(&cbs)); +} + +TEST_F(Test_LoRaWANInterface, set_device_class) +{ + object->set_device_class(CLASS_A); +} + +TEST_F(Test_LoRaWANInterface, get_tx_metadata) +{ + lorawan_tx_metadata data; + EXPECT_TRUE(LORAWAN_STATUS_OK == object->get_tx_metadata(data)); +} + +TEST_F(Test_LoRaWANInterface, get_rx_metadata) +{ + lorawan_rx_metadata data; + EXPECT_TRUE(LORAWAN_STATUS_OK == object->get_rx_metadata(data)); +} + +TEST_F(Test_LoRaWANInterface, get_backoff_metadata) +{ + int i; + EXPECT_TRUE(LORAWAN_STATUS_OK == object->get_backoff_metadata(i)); +} + +TEST_F(Test_LoRaWANInterface, cancel_sending) +{ + EXPECT_TRUE(LORAWAN_STATUS_OK == object->cancel_sending()); +} + diff --git a/UNITTESTS/features/lorawan/lorawaninterface/unittest.cmake b/UNITTESTS/features/lorawan/lorawaninterface/unittest.cmake new file mode 100644 index 0000000000..9e123a1c5e --- /dev/null +++ b/UNITTESTS/features/lorawan/lorawaninterface/unittest.cmake @@ -0,0 +1,51 @@ +#[[ + * 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. +]] + +# Unit test suite name +set(TEST_SUITE_NAME "lorawan_LoRaWANInterface") + +# Source files +set(unittest-sources + ../features/lorawan/LoRaWANInterface.cpp +) + +# Add test specific include paths +set(unittest-includes ${unittest-includes} + target_h + ../features/lorawan +) + +# Test & stub files +set(unittest-test-sources + features/lorawan/lorawaninterface/Test_LoRaWANInterface.cpp + stubs/LoRaPHY_stub.cpp + stubs/LoRaWANStack_stub.cpp + stubs/LoRaMac_stub.cpp + stubs/mbed_assert_stub.c + stubs/LoRaMacCrypto_stub.cpp + stubs/LoRaMacChannelPlan_stub.cpp + stubs/LoRaWANTimer_stub.cpp + stubs/LoRaMacCommand_stub.cpp + stubs/LoRaPHYEU868_stub.cpp +) + +# defines +set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DMBED_CONF_LORA_PHY=\"EU868\"") +set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DMBED_CONF_LORA_PHY=\"EU868\"") + + + diff --git a/UNITTESTS/features/lorawan/lorawanstack/Test_LoRaWANStack.cpp b/UNITTESTS/features/lorawan/lorawanstack/Test_LoRaWANStack.cpp new file mode 100644 index 0000000000..43be749beb --- /dev/null +++ b/UNITTESTS/features/lorawan/lorawanstack/Test_LoRaWANStack.cpp @@ -0,0 +1,865 @@ +/* + * 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 "LoRaWANStack.h" +#include "EventQueue.h" + +#include "LoRaPHY_stub.h" +#include "LoRaMac_stub.h" +#include "equeue_stub.h" +#include "lorawan_data_structures.h" + +static uint8_t batt_level = 0; + +using namespace events; + +class my_LoRaPHY : public LoRaPHY +{ +public: + my_LoRaPHY(){}; + + virtual ~my_LoRaPHY(){}; +}; + +uint8_t my_cb() +{ + return 1; +} + +class my_radio : public LoRaRadio +{ +public: + radio_events_t *_ev; + + virtual void init_radio(radio_events_t *events){_ev = events;}; + + virtual void radio_reset(){}; + + virtual void sleep(void){}; + + virtual void standby(void){}; + + virtual void set_rx_config (radio_modems_t modem, uint32_t bandwidth, + uint32_t datarate, uint8_t coderate, + uint32_t bandwidth_afc, uint16_t preamble_len, + uint16_t symb_timeout, bool fix_len, + uint8_t payload_len, + bool crc_on, bool freq_hop_on, uint8_t hop_period, + bool iq_inverted, bool rx_continuous){}; + + virtual void set_tx_config(radio_modems_t modem, int8_t power, uint32_t fdev, + uint32_t bandwidth, uint32_t datarate, + uint8_t coderate, uint16_t preamble_len, + bool fix_len, bool crc_on, bool freq_hop_on, + uint8_t hop_period, bool iq_inverted, uint32_t timeout){}; + + virtual void send(uint8_t *buffer, uint8_t size){}; + + virtual void receive(void){}; + + virtual void set_channel(uint32_t freq){}; + + virtual uint32_t random(void){}; + + virtual uint8_t get_status(void){return uint8_value;}; + + virtual void set_max_payload_length(radio_modems_t modem, uint8_t max){}; + + virtual void set_public_network(bool enable){}; + + virtual uint32_t time_on_air(radio_modems_t modem, uint8_t pkt_len){}; + + virtual bool perform_carrier_sense(radio_modems_t modem, + uint32_t freq, + int16_t rssi_threshold, + uint32_t max_carrier_sense_time){ return bool_value;}; + + virtual void start_cad(void){}; + + virtual bool check_rf_frequency(uint32_t frequency){ return bool_value; }; + + virtual void set_tx_continuous_wave(uint32_t freq, int8_t power, uint16_t time){}; + + virtual void lock(void){}; + + virtual void unlock(void){}; + + bool bool_value; + uint8_t uint8_value; +}; + + + +class Test_LoRaWANStack : public testing::Test { +protected: + LoRaWANStack *object; + + virtual void SetUp() + { + LoRaMac_stub::status_value = LORAWAN_STATUS_OK; + object = new LoRaWANStack(); + } + + virtual void TearDown() + { + delete object; + } +}; + +TEST_F(Test_LoRaWANStack, constructor) +{ + EXPECT_TRUE(object); +} + +TEST_F(Test_LoRaWANStack, bind_phy_and_radio_driver) +{ + my_radio radio; + my_LoRaPHY phy; + object->bind_phy_and_radio_driver(radio, phy); +} + +TEST_F(Test_LoRaWANStack, initialize_mac_layer) +{ + EXPECT_TRUE(LORAWAN_STATUS_PARAMETER_INVALID == object->initialize_mac_layer(NULL)); + + EventQueue queue; + EXPECT_TRUE(LORAWAN_STATUS_OK == object->initialize_mac_layer(&queue)); + EXPECT_TRUE(LORAWAN_STATUS_OK == object->initialize_mac_layer(&queue)); + + //Visit callback + if (LoRaMac_stub::_scheduling_failure_handler) { + LoRaMac_stub::_scheduling_failure_handler.call(); + } +} + +void events_cb(lorawan_event_t ev) +{ + +} + +void lc_resp(uint8_t a, uint8_t b) +{ + +} + +uint8_t batt_lvl() +{ + return batt_level; +} + +TEST_F(Test_LoRaWANStack, set_lora_callbacks) +{ + lorawan_app_callbacks_t cb; + EXPECT_TRUE(LORAWAN_STATUS_NOT_INITIALIZED == object->set_lora_callbacks(&cb)); + + EventQueue queue; + EXPECT_TRUE(LORAWAN_STATUS_OK == object->initialize_mac_layer(&queue)); + + EXPECT_TRUE(LORAWAN_STATUS_PARAMETER_INVALID == object->set_lora_callbacks(NULL)); + + cb.events = NULL; + EXPECT_TRUE(LORAWAN_STATUS_PARAMETER_INVALID == object->set_lora_callbacks(&cb)); + + cb.events = events_cb; + cb.link_check_resp = lc_resp; + cb.battery_level = batt_lvl; + EXPECT_TRUE(LORAWAN_STATUS_OK == object->set_lora_callbacks(&cb)); +} + +TEST_F(Test_LoRaWANStack, connect) +{ + EXPECT_TRUE(LORAWAN_STATUS_NOT_INITIALIZED == object->connect()); + + EventQueue queue; + EXPECT_TRUE(LORAWAN_STATUS_OK == object->initialize_mac_layer(&queue)); + + LoRaMac_stub::status_value = LORAWAN_STATUS_BUSY; + EXPECT_TRUE(LORAWAN_STATUS_BUSY == object->connect()); + + LoRaMac_stub::status_value = LORAWAN_STATUS_OK; + EXPECT_TRUE(LORAWAN_STATUS_OK == object->connect()); + + //_ctrl_flags & CONN_IN_PROGRESS_FLAG + EXPECT_TRUE(LORAWAN_STATUS_BUSY == object->connect()); + + my_radio radio; + my_LoRaPHY phy; + object->bind_phy_and_radio_driver(radio, phy); + + struct equeue_event ptr; + equeue_stub.void_ptr = &ptr; + equeue_stub.call_cb_immediately = true; + loramac_mcps_confirm_t conf; + LoRaMac_stub::mcps_conf_ptr = &conf; + radio._ev->tx_done(); + + loramac_mcps_indication_t ind; + LoRaMac_stub::mcps_ind_ptr = &ind; + + loramac_mlme_confirm_t mlme; + LoRaMac_stub::mlme_conf_ptr = &mlme; + mlme.pending = true; + mlme.req_type = MLME_JOIN; + mlme.status = LORAMAC_EVENT_INFO_STATUS_OK; + LoRaMac_stub::bool_value = false; + radio._ev->rx_done(NULL, 0, 0, 0); + + //_ctrl_flags & CONNECTED_FLAG + EXPECT_TRUE(LORAWAN_STATUS_ALREADY_CONNECTED == object->connect()); + + //Visit rx_interrupt_handler's first if + radio._ev->rx_done(NULL, 65535, 0, 0); +} + +TEST_F(Test_LoRaWANStack, connect_args) +{ + lorawan_connect_t conn; + EXPECT_TRUE(LORAWAN_STATUS_NOT_INITIALIZED == object->connect(conn)); + + EventQueue queue; + EXPECT_TRUE(LORAWAN_STATUS_OK == object->initialize_mac_layer(&queue)); + + conn.connect_type = lorawan_connect_type_t(8); + EXPECT_TRUE(LORAWAN_STATUS_PARAMETER_INVALID == object->connect(conn)); + + LoRaMac_stub::status_value = LORAWAN_STATUS_BUSY; + conn.connect_type = LORAWAN_CONNECTION_OTAA; + EXPECT_TRUE(LORAWAN_STATUS_BUSY == object->connect(conn)); + + LoRaMac_stub::status_value = LORAWAN_STATUS_OK; + EXPECT_TRUE(LORAWAN_STATUS_OK == object->connect(conn)); + + //_ctrl_flags & CONN_IN_PROGRESS_FLAG + EXPECT_TRUE(LORAWAN_STATUS_BUSY == object->connect(conn)); + + object->shutdown(); + conn.connect_type = LORAWAN_CONNECTION_ABP; + EXPECT_TRUE(LORAWAN_STATUS_OK == object->connect(conn)); + + //_ctrl_flags & CONNECTED_FLAG + EXPECT_TRUE(LORAWAN_STATUS_ALREADY_CONNECTED == object->connect(conn)); +} + +TEST_F(Test_LoRaWANStack, add_channels) +{ + lorawan_channelplan_t plan; + EXPECT_TRUE(LORAWAN_STATUS_NOT_INITIALIZED == object->add_channels(plan)); + + EventQueue queue; + EXPECT_TRUE(LORAWAN_STATUS_OK == object->initialize_mac_layer(&queue)); + + LoRaMac_stub::status_value = LORAWAN_STATUS_OK; + EXPECT_TRUE(LORAWAN_STATUS_OK == object->add_channels(plan)); +} + +TEST_F(Test_LoRaWANStack, remove_a_channel) +{ + EXPECT_TRUE(LORAWAN_STATUS_NOT_INITIALIZED == object->remove_a_channel(1)); + + EventQueue queue; + EXPECT_TRUE(LORAWAN_STATUS_OK == object->initialize_mac_layer(&queue)); + + LoRaMac_stub::status_value = LORAWAN_STATUS_OK; + EXPECT_TRUE(LORAWAN_STATUS_OK == object->remove_a_channel(1)); +} + +TEST_F(Test_LoRaWANStack, drop_channel_list) +{ + EXPECT_TRUE(LORAWAN_STATUS_NOT_INITIALIZED == object->drop_channel_list()); + + EventQueue queue; + EXPECT_TRUE(LORAWAN_STATUS_OK == object->initialize_mac_layer(&queue)); + + LoRaMac_stub::status_value = LORAWAN_STATUS_OK; + EXPECT_TRUE(LORAWAN_STATUS_OK == object->drop_channel_list()); +} + +TEST_F(Test_LoRaWANStack, get_enabled_channels) +{ + lorawan_channelplan_t plan; + EXPECT_TRUE(LORAWAN_STATUS_NOT_INITIALIZED == object->get_enabled_channels(plan)); + + EventQueue queue; + EXPECT_TRUE(LORAWAN_STATUS_OK == object->initialize_mac_layer(&queue)); + + LoRaMac_stub::status_value = LORAWAN_STATUS_OK; + EXPECT_TRUE(LORAWAN_STATUS_OK == object->get_enabled_channels(plan)); +} + +TEST_F(Test_LoRaWANStack, set_confirmed_msg_retry) +{ + EXPECT_TRUE(LORAWAN_STATUS_NOT_INITIALIZED == object->set_confirmed_msg_retry(1)); + + EventQueue queue; + EXPECT_TRUE(LORAWAN_STATUS_OK == object->initialize_mac_layer(&queue)); + + EXPECT_TRUE(LORAWAN_STATUS_PARAMETER_INVALID == object->set_confirmed_msg_retry(255)); + + LoRaMac_stub::status_value = LORAWAN_STATUS_OK; + EXPECT_TRUE(LORAWAN_STATUS_OK == object->set_confirmed_msg_retry(1)); +} + +TEST_F(Test_LoRaWANStack, set_channel_data_rate) +{ + EXPECT_TRUE(LORAWAN_STATUS_NOT_INITIALIZED == object->set_channel_data_rate(4)); + + EventQueue queue; + EXPECT_TRUE(LORAWAN_STATUS_OK == object->initialize_mac_layer(&queue)); + + LoRaMac_stub::status_value = LORAWAN_STATUS_OK; + EXPECT_TRUE(LORAWAN_STATUS_OK == object->set_channel_data_rate(4)); +} + +TEST_F(Test_LoRaWANStack, enable_adaptive_datarate) +{ + EXPECT_TRUE(LORAWAN_STATUS_NOT_INITIALIZED == object->enable_adaptive_datarate(false)); + + EventQueue queue; + EXPECT_TRUE(LORAWAN_STATUS_OK == object->initialize_mac_layer(&queue)); + + LoRaMac_stub::status_value = LORAWAN_STATUS_OK; + EXPECT_TRUE(LORAWAN_STATUS_OK == object->enable_adaptive_datarate(false)); +} + +TEST_F(Test_LoRaWANStack, handle_tx) +{ + EXPECT_TRUE(LORAWAN_STATUS_NOT_INITIALIZED == object->handle_tx(0, NULL, 0, 0, true, false)); + + EventQueue queue; + EXPECT_TRUE(LORAWAN_STATUS_OK == object->initialize_mac_layer(&queue)); + + LoRaMac_stub::status_value = LORAWAN_STATUS_OK; + EXPECT_TRUE(LORAWAN_STATUS_PARAMETER_INVALID == object->handle_tx(0, NULL, 0, 0, false, false)); + + lorawan_app_callbacks_t cb; + cb.events = events_cb; + cb.link_check_resp = lc_resp; + cb.battery_level = batt_lvl; + EXPECT_TRUE(LORAWAN_STATUS_OK == object->set_lora_callbacks(&cb)); + EXPECT_TRUE(LORAWAN_STATUS_OK == object->set_link_check_request()); + + LoRaMac_stub::status_value = LORAWAN_STATUS_OK; + EXPECT_TRUE(LORAWAN_STATUS_NO_ACTIVE_SESSIONS == object->handle_tx(0, NULL, 0, 0, true, false)); + + lorawan_connect_t conn; + conn.connect_type = LORAWAN_CONNECTION_ABP; + EXPECT_TRUE(LORAWAN_STATUS_OK == object->connect(conn)); + + LoRaMac_stub::bool_value = false; + EXPECT_TRUE(LORAWAN_STATUS_NO_NETWORK_JOINED == object->handle_tx(0, NULL, 0, 0, true, false)); + + LoRaMac_stub::bool_value = true; + EXPECT_TRUE(LORAWAN_STATUS_WOULD_BLOCK == object->handle_tx(0, NULL, 0, 0, true, false)); + + LoRaMac_stub::bool_false_counter = 1; + LoRaMac_stub::bool_value = true; + //set_application_port fails + EXPECT_TRUE(LORAWAN_STATUS_PORT_INVALID == object->handle_tx(0, NULL, 0, 0, true, false)); + + LoRaMac_stub::bool_false_counter = 1; + LoRaMac_stub::bool_value = true; + //Wrong flags -> LORAWAN_STATUS_PARAMETER_INVALID + EXPECT_TRUE(LORAWAN_STATUS_PARAMETER_INVALID == object->handle_tx(1, NULL, 0, 0x04, true, false)); + + LoRaMac_stub::bool_false_counter = 1; + //Actual sending + EXPECT_TRUE(LORAWAN_STATUS_OK == object->handle_tx(1, NULL, 0, 0x08, true, false)); + +} + +TEST_F(Test_LoRaWANStack, handle_rx) +{ + uint8_t port; + int flags; + EXPECT_TRUE(LORAWAN_STATUS_NOT_INITIALIZED == object->handle_rx(NULL, 0, port, flags, false)); + + EventQueue queue; + EXPECT_TRUE(LORAWAN_STATUS_OK == object->initialize_mac_layer(&queue)); + + LoRaMac_stub::status_value = LORAWAN_STATUS_OK; + EXPECT_TRUE(LORAWAN_STATUS_NO_ACTIVE_SESSIONS == object->handle_rx(NULL, 0, port, flags, false)); + + lorawan_connect_t conn; + conn.connect_type = LORAWAN_CONNECTION_ABP; + EXPECT_TRUE(LORAWAN_STATUS_OK == object->connect(conn)); + EXPECT_TRUE(LORAWAN_STATUS_WOULD_BLOCK == object->handle_rx(NULL, 0, port, flags, false)); + + //Prepare ready for receive state + lorawan_app_callbacks_t cb; + cb.events = events_cb; + cb.link_check_resp = lc_resp; + cb.battery_level = batt_lvl; + EXPECT_TRUE(LORAWAN_STATUS_OK == object->set_lora_callbacks(&cb)); + + my_radio radio; + my_LoRaPHY phy; + object->bind_phy_and_radio_driver(radio, phy); + + struct equeue_event ptr; + equeue_stub.void_ptr = &ptr; + equeue_stub.call_cb_immediately = true; + loramac_mcps_confirm_t conf; + LoRaMac_stub::mcps_conf_ptr = &conf; + radio._ev->tx_done(); + + loramac_mcps_indication_t ind; + LoRaMac_stub::mcps_ind_ptr = &ind; + + loramac_mlme_confirm_t mlme; + LoRaMac_stub::mlme_conf_ptr = &mlme; + mlme.pending = false; + mlme.req_type = MLME_JOIN; + mlme.status = LORAMAC_EVENT_INFO_STATUS_OK; + LoRaMac_stub::bool_value = true; + conf.req_type = MCPS_PROPRIETARY; + + ind.pending = true; + LoRaMac_stub::dev_class_value = CLASS_C; + + loramac_mlme_indication_t mlme_ind; + mlme_ind.pending = false; + LoRaMac_stub::mlme_ind_ptr = &mlme_ind; + + uint8_t ind_buf[150]; + for (int i= 0; i < 110; i++) { + ind_buf[i] = i; + } + ind.buffer = ind_buf; + ind.buffer_size = 150; + ind.type = MCPS_UNCONFIRMED; + radio._ev->rx_done(NULL, 0, 0, 0); + + //data == NULL || LENGTH == 0 (2 cases) + EXPECT_TRUE(LORAWAN_STATUS_PARAMETER_INVALID == object->handle_rx(NULL, 0, port, flags, false)); + uint8_t data[50]; + EXPECT_TRUE(LORAWAN_STATUS_PARAMETER_INVALID == object->handle_rx(data, 0, port, flags, false)); + + //validate_params returns Would block + port = 43; + EXPECT_TRUE(LORAWAN_STATUS_WOULD_BLOCK == object->handle_rx(data, 50, port, flags, true)); + + ind.type = MCPS_CONFIRMED; + radio._ev->rx_done(NULL, 0, 0, 0); + EXPECT_TRUE(LORAWAN_STATUS_WOULD_BLOCK == object->handle_rx(data, 50, port, flags, true)); + //Call again to visit send_automatic_uplink_message error case + LoRaMac_stub::bool_true_counter = 1; + ind.type = MCPS_CONFIRMED; + ind.status = LORAMAC_EVENT_INFO_STATUS_ERROR; + LoRaMac_stub::bool_value = false; + radio._ev->rx_done(NULL, 0, 0, 0); + + ind.status = LORAMAC_EVENT_INFO_STATUS_OK; + + LoRaMac_stub::bool_value = true; + //convert_to_msg_flag cases + ind.fpending_status = true; + ind.type = MCPS_PROPRIETARY; + radio._ev->rx_done(NULL, 0, 0, 0); + EXPECT_TRUE(LORAWAN_STATUS_WOULD_BLOCK == object->handle_rx(data, 50, port, flags, true)); + + ind.type = MCPS_MULTICAST; + radio._ev->rx_done(NULL, 0, 0, 0); + EXPECT_TRUE(LORAWAN_STATUS_WOULD_BLOCK == object->handle_rx(data, 50, port, flags, true)); + + ind.type = MCPS_UNCONFIRMED; + radio._ev->rx_done(NULL, 0, 0, 0); + + //read not complete + EXPECT_TRUE(50 == object->handle_rx(data, 50, port, flags, false)); + EXPECT_EQ(10, data[10]); + + EXPECT_TRUE(50 == object->handle_rx(data, 50, port, flags, false)); + EXPECT_EQ(60, data[10]); + + //read complete + EXPECT_TRUE(50 == object->handle_rx(data, 50, port, flags, false)); + EXPECT_EQ(100, data[0]); + + //read can fit the buffer + for (int i= 0; i < 110; i++) { + ind_buf[i] = i; + } + ind.buffer = ind_buf; + ind.buffer_size = 50; + ind.type = mcps_type_t(66); + radio._ev->rx_done(NULL, 0, 0, 0); + EXPECT_TRUE(50 == object->handle_rx(data, 50, port, flags, false)); + EXPECT_EQ(10, data[10]); +} + +TEST_F(Test_LoRaWANStack, set_link_check_request) +{ + EXPECT_TRUE(LORAWAN_STATUS_NOT_INITIALIZED == object->set_link_check_request()); + + EventQueue queue; + EXPECT_TRUE(LORAWAN_STATUS_OK == object->initialize_mac_layer(&queue)); + + LoRaMac_stub::status_value = LORAWAN_STATUS_OK; + EXPECT_TRUE(LORAWAN_STATUS_PARAMETER_INVALID == object->set_link_check_request()); + + lorawan_app_callbacks_t cb; + cb.events = events_cb; + cb.link_check_resp = lc_resp; + cb.battery_level = batt_lvl; + EXPECT_TRUE(LORAWAN_STATUS_OK == object->set_lora_callbacks(&cb)); + EXPECT_TRUE(LORAWAN_STATUS_OK == object->set_link_check_request()); +} + +TEST_F(Test_LoRaWANStack, remove_link_check_request) +{ + object->remove_link_check_request(); +} + +TEST_F(Test_LoRaWANStack, shutdown) +{ + EXPECT_TRUE(LORAWAN_STATUS_NOT_INITIALIZED == object->shutdown()); + + EventQueue queue; + EXPECT_TRUE(LORAWAN_STATUS_OK == object->initialize_mac_layer(&queue)); + + EXPECT_TRUE(LORAWAN_STATUS_DEVICE_OFF == object->shutdown()); +} + +TEST_F(Test_LoRaWANStack, set_device_class) +{ + EXPECT_TRUE(LORAWAN_STATUS_NOT_INITIALIZED == object->set_device_class(CLASS_A)); + + EventQueue queue; + EXPECT_TRUE(LORAWAN_STATUS_OK == object->initialize_mac_layer(&queue)); + + LoRaMac_stub::status_value = LORAWAN_STATUS_OK; + EXPECT_TRUE(LORAWAN_STATUS_UNSUPPORTED == object->set_device_class(CLASS_B)); + + EXPECT_TRUE(LORAWAN_STATUS_OK == object->set_device_class(CLASS_A)); + + //Visit callback + if (LoRaMac_stub::_ack_expiry_handler_for_class_c) { + LoRaMac_stub::_ack_expiry_handler_for_class_c.call(); + } +} + +TEST_F(Test_LoRaWANStack, acquire_tx_metadata) +{ + lorawan_tx_metadata data; + EXPECT_TRUE(LORAWAN_STATUS_NOT_INITIALIZED == object->acquire_tx_metadata(data)); + + EventQueue queue; + EXPECT_TRUE(LORAWAN_STATUS_OK == object->initialize_mac_layer(&queue)); + + LoRaMac_stub::status_value = LORAWAN_STATUS_OK; + // stale = true; + EXPECT_TRUE(LORAWAN_STATUS_METADATA_NOT_AVAILABLE == object->acquire_tx_metadata(data)); + + // stale = false; + my_radio radio; + my_LoRaPHY phy; + object->bind_phy_and_radio_driver(radio, phy); + + struct equeue_event ptr; + equeue_stub.void_ptr = &ptr; + equeue_stub.call_cb_immediately = true; + loramac_mcps_confirm_t conf; + LoRaMac_stub::mcps_conf_ptr = &conf; + radio._ev->tx_done(); + + EXPECT_TRUE(LORAWAN_STATUS_OK == object->acquire_tx_metadata(data)); +} + +TEST_F(Test_LoRaWANStack, acquire_rx_metadata) +{ + lorawan_rx_metadata data; + EXPECT_TRUE(LORAWAN_STATUS_NOT_INITIALIZED == object->acquire_rx_metadata(data)); + + EventQueue queue; + EXPECT_TRUE(LORAWAN_STATUS_OK == object->initialize_mac_layer(&queue)); + + LoRaMac_stub::status_value = LORAWAN_STATUS_OK; + // stale = true; + EXPECT_TRUE(LORAWAN_STATUS_METADATA_NOT_AVAILABLE == object->acquire_rx_metadata(data)); + + // stale = false; + my_radio radio; + my_LoRaPHY phy; + object->bind_phy_and_radio_driver(radio, phy); + + struct equeue_event ptr; + equeue_stub.void_ptr = &ptr; + equeue_stub.call_cb_immediately = true; + loramac_mcps_confirm_t conf; + LoRaMac_stub::mcps_conf_ptr = &conf; + radio._ev->tx_done(); + + loramac_mcps_indication_t ind; + LoRaMac_stub::mcps_ind_ptr = &ind; + + loramac_mlme_confirm_t mlme; + LoRaMac_stub::mlme_conf_ptr = &mlme; + mlme.pending = true; + mlme.req_type = MLME_JOIN; + + //Visit mlme_confirm_handler here also + mlme.status = LORAMAC_EVENT_INFO_STATUS_CRYPTO_FAIL; + LoRaMac_stub::bool_value = false; + radio._ev->rx_done(NULL, 0, 0, 0); + + mlme.status = LORAMAC_EVENT_INFO_STATUS_TX_TIMEOUT; + radio._ev->rx_done(NULL, 0, 0, 0); + + mlme.status = LORAMAC_EVENT_INFO_STATUS_RX1_TIMEOUT; + LoRaMac_stub::slot_value = RX_SLOT_WIN_2; + radio._ev->rx_done(NULL, 0, 0, 0); + + lorawan_app_callbacks_t cb; + cb.events = events_cb; + cb.link_check_resp = lc_resp; + cb.battery_level = batt_lvl; + EXPECT_TRUE(LORAWAN_STATUS_OK == object->set_lora_callbacks(&cb)); + mlme.req_type = MLME_LINK_CHECK; + mlme.status = LORAMAC_EVENT_INFO_STATUS_OK; + radio._ev->rx_done(NULL, 0, 0, 0); + + EXPECT_TRUE(LORAWAN_STATUS_OK == object->acquire_rx_metadata(data)); +} + +TEST_F(Test_LoRaWANStack, acquire_backoff_metadata) +{ + int b; + EXPECT_TRUE(LORAWAN_STATUS_NOT_INITIALIZED == object->acquire_backoff_metadata(b)); + + EventQueue queue; + EXPECT_TRUE(LORAWAN_STATUS_OK == object->initialize_mac_layer(&queue)); + + LoRaMac_stub::int_value = 2; + EXPECT_TRUE(LORAWAN_STATUS_OK == object->acquire_backoff_metadata(b)); + + LoRaMac_stub::int_value = 0; + EXPECT_TRUE(LORAWAN_STATUS_METADATA_NOT_AVAILABLE == object->acquire_backoff_metadata(b)); +} + +TEST_F(Test_LoRaWANStack, stop_sending) +{ + EXPECT_TRUE(LORAWAN_STATUS_NOT_INITIALIZED == object->stop_sending()); + + EventQueue queue; + EXPECT_TRUE(LORAWAN_STATUS_OK == object->initialize_mac_layer(&queue)); + + LoRaMac_stub::status_value = LORAWAN_STATUS_DEVICE_OFF; + EXPECT_TRUE(LORAWAN_STATUS_BUSY == object->stop_sending()); + + LoRaMac_stub::status_value = LORAWAN_STATUS_OK; + EXPECT_TRUE(LORAWAN_STATUS_OK == object->stop_sending()); +} + +TEST_F(Test_LoRaWANStack, lock) +{ + object->lock(); +} + +TEST_F(Test_LoRaWANStack, unlock) +{ + object->unlock(); +} + +TEST_F(Test_LoRaWANStack, interrupt_functions) +{ + lorawan_connect_t conn; + EventQueue queue; + EXPECT_TRUE(LORAWAN_STATUS_OK == object->initialize_mac_layer(&queue)); + + my_radio radio; + my_LoRaPHY phy; + object->bind_phy_and_radio_driver(radio, phy); + + struct equeue_event ptr; + equeue_stub.void_ptr = &ptr; + equeue_stub.call_cb_immediately = true; + loramac_mcps_confirm_t conf; + LoRaMac_stub::mcps_conf_ptr = &conf; + radio._ev->tx_done(); + + loramac_mcps_indication_t ind; + LoRaMac_stub::mcps_ind_ptr = &ind; + + loramac_mlme_confirm_t mlme; + LoRaMac_stub::mlme_conf_ptr = &mlme; + mlme.pending = true; + mlme.req_type = MLME_JOIN; + mlme.status = LORAMAC_EVENT_INFO_STATUS_OK; + LoRaMac_stub::bool_value = false; + radio._ev->rx_done(NULL, 0, 0, 0); + + radio._ev->rx_done(NULL, 0, 0, 0); + + radio._ev->rx_error(); + LoRaMac_stub::slot_value = RX_SLOT_WIN_2; + radio._ev->rx_error(); + + conf.req_type = MCPS_UNCONFIRMED; + LoRaMac_stub::bool_value = true; + radio._ev->rx_error(); + + conf.req_type = MCPS_CONFIRMED; + radio._ev->rx_error(); + + LoRaMac_stub::bool_value = false; + + LoRaMac_stub::slot_value = RX_SLOT_WIN_1; + radio._ev->rx_timeout(); + + radio._ev->tx_timeout(); + + object->shutdown(); + conn.connect_type = LORAWAN_CONNECTION_OTAA; + object->connect(conn); + LoRaMac_stub::status_value = LORAWAN_STATUS_OK; + object->connect(conn); + radio._ev->tx_timeout(); +} + +TEST_F(Test_LoRaWANStack, process_transmission) +{ + EventQueue queue; + EXPECT_TRUE(LORAWAN_STATUS_OK == object->initialize_mac_layer(&queue)); + + lorawan_app_callbacks_t cb; + cb.events = events_cb; + cb.link_check_resp = lc_resp; + cb.battery_level = batt_lvl; + EXPECT_TRUE(LORAWAN_STATUS_OK == object->set_lora_callbacks(&cb)); + + my_radio radio; + my_LoRaPHY phy; + object->bind_phy_and_radio_driver(radio, phy); + + object->connect(); + + struct equeue_event ptr; + equeue_stub.void_ptr = &ptr; + equeue_stub.call_cb_immediately = true; + loramac_mcps_confirm_t conf; + LoRaMac_stub::mcps_conf_ptr = &conf; + radio._ev->tx_done(); + + loramac_mcps_indication_t ind; + LoRaMac_stub::mcps_ind_ptr = &ind; + + loramac_mlme_confirm_t mlme; + LoRaMac_stub::mlme_conf_ptr = &mlme; + mlme.pending = true; + mlme.req_type = MLME_JOIN; + mlme.status = LORAMAC_EVENT_INFO_STATUS_OK; + LoRaMac_stub::bool_value = false; + radio._ev->rx_done(NULL, 0, 0, 0); + + LoRaMac_stub::bool_value = true; + conf.req_type = MCPS_PROPRIETARY; + LoRaMac_stub::bool_false_counter = 1; + LoRaMac_stub::dev_class_value = CLASS_A; + object->handle_tx(1, NULL, 0, MSG_UNCONFIRMED_FLAG, true, false); + radio._ev->tx_done(); + + LoRaMac_stub::bool_false_counter = 1; + LoRaMac_stub::dev_class_value = CLASS_A; + object->handle_tx(1, NULL, 0, MSG_UNCONFIRMED_FLAG, true, false); + radio._ev->tx_done(); + + LoRaMac_stub::bool_false_counter = 1; + LoRaMac_stub::dev_class_value = CLASS_C; + object->handle_tx(1, NULL, 0, MSG_UNCONFIRMED_FLAG, true, false); + radio._ev->tx_done(); + + LoRaMac_stub::bool_false_counter = 1; + conf.req_type = MCPS_CONFIRMED; + object->handle_tx(1, NULL, 0, MSG_UNCONFIRMED_FLAG, true, false); + radio._ev->tx_done(); +} + +TEST_F(Test_LoRaWANStack, process_reception) +{ + EventQueue queue; + EXPECT_TRUE(LORAWAN_STATUS_OK == object->initialize_mac_layer(&queue)); + + //Prepare ready for receive state + lorawan_app_callbacks_t cb; + cb.events = events_cb; + cb.link_check_resp = lc_resp; + cb.battery_level = batt_lvl; + EXPECT_TRUE(LORAWAN_STATUS_OK == object->set_lora_callbacks(&cb)); + + my_radio radio; + my_LoRaPHY phy; + object->bind_phy_and_radio_driver(radio, phy); + + struct equeue_event ptr; + equeue_stub.void_ptr = &ptr; + equeue_stub.call_cb_immediately = true; + loramac_mcps_confirm_t conf; + LoRaMac_stub::mcps_conf_ptr = &conf; + radio._ev->tx_done(); + + loramac_mcps_indication_t ind; + LoRaMac_stub::mcps_ind_ptr = &ind; + + loramac_mlme_confirm_t mlme; + LoRaMac_stub::mlme_conf_ptr = &mlme; + mlme.pending = false; + mlme.req_type = MLME_JOIN; + mlme.status = LORAMAC_EVENT_INFO_STATUS_OK; + LoRaMac_stub::bool_value = true; + conf.req_type = MCPS_PROPRIETARY; + + ind.pending = true; + LoRaMac_stub::dev_class_value = CLASS_C; + + loramac_mlme_indication_t mlme_ind; + mlme_ind.pending = false; + LoRaMac_stub::mlme_ind_ptr = &mlme_ind; + + uint8_t ind_buf[150]; + for (int i= 0; i < 110; i++) { + ind_buf[i] = i; + } + ind.buffer = ind_buf; + ind.buffer_size = 150; + + //_loramac.get_mcps_confirmation()->req_type == MCPS_CONFIRMED + conf.req_type = MCPS_CONFIRMED; + conf.status = LORAMAC_EVENT_INFO_STATUS_TX_TIMEOUT; + radio._ev->rx_done(NULL, 0, 0, 0); + + ind.is_ack_recvd = false; + LoRaMac_stub::bool_true_counter = 1; + LoRaMac_stub::bool_value = false; + LoRaMac_stub::slot_value = RX_SLOT_WIN_2; + conf.status = LORAMAC_EVENT_INFO_STATUS_TX_DR_PAYLOAD_SIZE_ERROR; + radio._ev->rx_done(NULL, 0, 0, 0); + + conf.req_type = MCPS_UNCONFIRMED; + LoRaMac_stub::dev_class_value = CLASS_A; + LoRaMac_stub::bool_value = true; + mlme_ind.pending = true; + mlme_ind.indication_type = MLME_SCHEDULE_UPLINK; + conf.status = LORAMAC_EVENT_INFO_STATUS_ERROR; + radio._ev->rx_done(NULL, 0, 0, 0); + + ind.is_ack_recvd = true; + conf.req_type = MCPS_CONFIRMED; + conf.status = LORAMAC_EVENT_INFO_STATUS_TX_TIMEOUT; + radio._ev->rx_done(NULL, 0, 0, 0); +} + diff --git a/UNITTESTS/features/lorawan/lorawanstack/unittest.cmake b/UNITTESTS/features/lorawan/lorawanstack/unittest.cmake new file mode 100644 index 0000000000..8c97d36268 --- /dev/null +++ b/UNITTESTS/features/lorawan/lorawanstack/unittest.cmake @@ -0,0 +1,56 @@ +#[[ + * 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. +]] + +# Unit test suite name +set(TEST_SUITE_NAME "lorawan_LoRaWANStack") + +# Source files +set(unittest-sources + ../features/lorawan/LoRaWANStack.cpp +) + +# Add test specific include paths +set(unittest-includes ${unittest-includes} + target_h + ../features/lorawan +) + +# Test & stub files +set(unittest-test-sources + features/lorawan/lorawanstack/Test_LoRaWANStack.cpp + stubs/LoRaPHY_stub.cpp + stubs/LoRaMac_stub.cpp + stubs/mbed_assert_stub.c + stubs/LoRaMacCrypto_stub.cpp + stubs/LoRaMacChannelPlan_stub.cpp + stubs/LoRaWANTimer_stub.cpp + stubs/LoRaMacCommand_stub.cpp + stubs/EventQueue_stub.cpp + stubs/equeue_stub.c + +) + +# defines +set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DMBED_CONF_LORA_OVER_THE_AIR_ACTIVATION=true -DMBED_CONF_LORA_DEVICE_EUI=\"{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}\"") +set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DMBED_CONF_LORA_OVER_THE_AIR_ACTIVATION=true -DMBED_CONF_LORA_DEVICE_EUI=\"{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}\"") + +set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DMBED_CONF_LORA_AUTOMATIC_UPLINK_MESSAGE=true") +set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DMBED_CONF_LORA_AUTOMATIC_UPLINK_MESSAGE=true") + +set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DMBED_CONF_LORA_APPLICATION_EUI=\"{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}\" -DMBED_CONF_LORA_APPLICATION_KEY=\"{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}\"") +set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DMBED_CONF_LORA_APPLICATION_EUI=\"{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}\" -DMBED_CONF_LORA_APPLICATION_KEY=\"{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}\"") + diff --git a/UNITTESTS/features/lorawan/lorawantimer/Test_LoRaWANTimer.cpp b/UNITTESTS/features/lorawan/lorawantimer/Test_LoRaWANTimer.cpp new file mode 100644 index 0000000000..80a98460ce --- /dev/null +++ b/UNITTESTS/features/lorawan/lorawantimer/Test_LoRaWANTimer.cpp @@ -0,0 +1,84 @@ +/* + * 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 "LoRaWANTimer.h" + +using namespace events; + +class Test_LoRaWANTimer : public testing::Test { +protected: + LoRaWANTimeHandler *object; + EventQueue *queue; + + virtual void SetUp() + { + queue = new EventQueue(3, NULL); + object = new LoRaWANTimeHandler(); + object->activate_timer_subsystem(queue); + } + + virtual void TearDown() + { + delete object; + delete queue; + } +}; + +TEST_F(Test_LoRaWANTimer, constructor) +{ + EXPECT_TRUE(object); +} + +TEST_F(Test_LoRaWANTimer, get_current_time) +{ + lorawan_time_t tt = object->get_current_time(); + EXPECT_TRUE(0 == tt); +} + +TEST_F(Test_LoRaWANTimer, get_elapsed_time) +{ + lorawan_time_t tt = object->get_elapsed_time(0); + EXPECT_TRUE(0 == tt); +} + +void my_callback() +{ +} + +TEST_F(Test_LoRaWANTimer, init) +{ + timer_event_t ev; + object->init(ev, my_callback); +} + +TEST_F(Test_LoRaWANTimer, start) +{ + timer_event_t ev; + object->start(ev, 10); +} + +TEST_F(Test_LoRaWANTimer, stop) +{ + timer_event_t ev; + ev.timer_id = 4; + object->stop(ev); + EXPECT_TRUE(ev.timer_id == 0); +} + + + diff --git a/UNITTESTS/features/lorawan/lorawantimer/unittest.cmake b/UNITTESTS/features/lorawan/lorawantimer/unittest.cmake new file mode 100644 index 0000000000..48764961fc --- /dev/null +++ b/UNITTESTS/features/lorawan/lorawantimer/unittest.cmake @@ -0,0 +1,39 @@ +#[[ + * 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. +]] + +# Unit test suite name +set(TEST_SUITE_NAME "lorawan_LoRaWANTimer") + +# Source files +set(unittest-sources + ../features/lorawan/system/LoRaWANTimer.cpp +) + +# Add test specific include paths +set(unittest-includes ${unittest-includes} + target_h + ../features/lorawan/system +) + +# Test & stub files +set(unittest-test-sources + features/lorawan/lorawantimer/Test_LoRaWANTimer.cpp + stubs/EventQueue_stub.cpp + stubs/mbed_assert_stub.c + stubs/equeue_stub.c +) + diff --git a/UNITTESTS/features/netsocket/NetworkStack/test_NetworkStack.cpp b/UNITTESTS/features/netsocket/NetworkStack/test_NetworkStack.cpp index 2b18f2b466..6ae21c633c 100644 --- a/UNITTESTS/features/netsocket/NetworkStack/test_NetworkStack.cpp +++ b/UNITTESTS/features/netsocket/NetworkStack/test_NetworkStack.cpp @@ -21,6 +21,8 @@ #include "events/EventQueue.h" #include +#include "equeue_stub.h" + // Control nsapi stub return value. See stubs/nsapi_dns_stub.cpp extern nsapi_error_t nsapi_stub_return_value; @@ -186,6 +188,9 @@ TEST_F(TestNetworkStack, gethostbyname_async_delay) SocketAddress a; stack->ip_address = std::string("127.0.0.1"); SetUpQueue(); + struct equeue_event ptr; + equeue_stub.void_ptr = &ptr; + equeue_stub.call_cb_immediately = true; EXPECT_EQ(stack->gethostbyname_async("localhost", mbed::callback(my_callback), NSAPI_UNSPEC), NSAPI_ERROR_DNS_FAILURE); EXPECT_EQ(callin_callback(1, mbed::callback(noarg_callback)), NSAPI_ERROR_OK); EXPECT_TRUE(noarg_callback_is_called); @@ -197,6 +202,9 @@ TEST_F(TestNetworkStack, gethostbyname_async) SocketAddress a; stack->ip_address = std::string("127.0.0.1"); SetUpQueue(); + struct equeue_event ptr; + equeue_stub.void_ptr = &ptr; + equeue_stub.call_cb_immediately = true; EXPECT_EQ(stack->gethostbyname_async("localhost", mbed::callback(my_callback), NSAPI_UNSPEC), NSAPI_ERROR_DNS_FAILURE); EXPECT_EQ(callin_callback(0, mbed::callback(noarg_callback)), NSAPI_ERROR_OK); EXPECT_TRUE(noarg_callback_is_called); diff --git a/UNITTESTS/stubs/ATCmdParser_stub.cpp b/UNITTESTS/stubs/ATCmdParser_stub.cpp index d9157ec848..233dd039dc 100644 --- a/UNITTESTS/stubs/ATCmdParser_stub.cpp +++ b/UNITTESTS/stubs/ATCmdParser_stub.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2017, Arm Limited and affiliates. + * Copyright (c) , Arm Limited and affiliates. * SPDX-License-Identifier: Apache-2.0 * * Licensed under the Apache License, Version 2.0 (the "License"); diff --git a/UNITTESTS/stubs/ATHandler_stub.cpp b/UNITTESTS/stubs/ATHandler_stub.cpp index 7c65253af3..599d48b812 100644 --- a/UNITTESTS/stubs/ATHandler_stub.cpp +++ b/UNITTESTS/stubs/ATHandler_stub.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2017, Arm Limited and affiliates. + * Copyright (c) , Arm Limited and affiliates. * SPDX-License-Identifier: Apache-2.0 * * Licensed under the Apache License, Version 2.0 (the "License"); diff --git a/UNITTESTS/stubs/ATHandler_stub.h b/UNITTESTS/stubs/ATHandler_stub.h index 3341aa4ced..a3b1cfb5e8 100644 --- a/UNITTESTS/stubs/ATHandler_stub.h +++ b/UNITTESTS/stubs/ATHandler_stub.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2017, Arm Limited and affiliates. + * Copyright (c) , Arm Limited and affiliates. * SPDX-License-Identifier: Apache-2.0 * * Licensed under the Apache License, Version 2.0 (the "License"); diff --git a/UNITTESTS/stubs/AT_CellularBase_stub.cpp b/UNITTESTS/stubs/AT_CellularBase_stub.cpp index e1f8526f5f..179c2e42e1 100644 --- a/UNITTESTS/stubs/AT_CellularBase_stub.cpp +++ b/UNITTESTS/stubs/AT_CellularBase_stub.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2017, Arm Limited and affiliates. + * Copyright (c) , Arm Limited and affiliates. * SPDX-License-Identifier: Apache-2.0 * * Licensed under the Apache License, Version 2.0 (the "License"); diff --git a/UNITTESTS/stubs/AT_CellularBase_stub.h b/UNITTESTS/stubs/AT_CellularBase_stub.h index f7d117d192..ec32d8eb28 100644 --- a/UNITTESTS/stubs/AT_CellularBase_stub.h +++ b/UNITTESTS/stubs/AT_CellularBase_stub.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2017, Arm Limited and affiliates. + * Copyright (c) , Arm Limited and affiliates. * SPDX-License-Identifier: Apache-2.0 * * Licensed under the Apache License, Version 2.0 (the "License"); diff --git a/UNITTESTS/stubs/AT_CellularDevice_stub.cpp b/UNITTESTS/stubs/AT_CellularDevice_stub.cpp index 2010018ced..9f6749a8f8 100644 --- a/UNITTESTS/stubs/AT_CellularDevice_stub.cpp +++ b/UNITTESTS/stubs/AT_CellularDevice_stub.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2017, Arm Limited and affiliates. + * Copyright (c) , Arm Limited and affiliates. * SPDX-License-Identifier: Apache-2.0 * * Licensed under the Apache License, Version 2.0 (the "License"); diff --git a/UNITTESTS/stubs/AT_CellularInformation_stub.cpp b/UNITTESTS/stubs/AT_CellularInformation_stub.cpp index a41b3080d5..ae35f29bde 100644 --- a/UNITTESTS/stubs/AT_CellularInformation_stub.cpp +++ b/UNITTESTS/stubs/AT_CellularInformation_stub.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2018, Arm Limited and affiliates. + * Copyright (c) , Arm Limited and affiliates. * SPDX-License-Identifier: Apache-2.0 * * Licensed under the Apache License, Version 2.0 (the "License"); diff --git a/UNITTESTS/stubs/AT_CellularMultiplexer_stub.cpp b/UNITTESTS/stubs/AT_CellularMultiplexer_stub.cpp index c397d5fb38..21ea7e9f54 100644 --- a/UNITTESTS/stubs/AT_CellularMultiplexer_stub.cpp +++ b/UNITTESTS/stubs/AT_CellularMultiplexer_stub.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2017, Arm Limited and affiliates. + * Copyright (c) , Arm Limited and affiliates. * SPDX-License-Identifier: Apache-2.0 * * Licensed under the Apache License, Version 2.0 (the "License"); diff --git a/UNITTESTS/stubs/AT_CellularNetwork_stub.cpp b/UNITTESTS/stubs/AT_CellularNetwork_stub.cpp index c4ba878439..b28c8ebb9e 100644 --- a/UNITTESTS/stubs/AT_CellularNetwork_stub.cpp +++ b/UNITTESTS/stubs/AT_CellularNetwork_stub.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2017, Arm Limited and affiliates. + * Copyright (c) , Arm Limited and affiliates. * SPDX-License-Identifier: Apache-2.0 * * Licensed under the Apache License, Version 2.0 (the "License"); diff --git a/UNITTESTS/stubs/AT_CellularPower_stub.cpp b/UNITTESTS/stubs/AT_CellularPower_stub.cpp index 3459088ce5..56d8a4ca87 100644 --- a/UNITTESTS/stubs/AT_CellularPower_stub.cpp +++ b/UNITTESTS/stubs/AT_CellularPower_stub.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2017, Arm Limited and affiliates. + * Copyright (c) , Arm Limited and affiliates. * SPDX-License-Identifier: Apache-2.0 * * Licensed under the Apache License, Version 2.0 (the "License"); diff --git a/UNITTESTS/stubs/AT_CellularSIM_stub.cpp b/UNITTESTS/stubs/AT_CellularSIM_stub.cpp index fc4be35b94..afab1012bd 100644 --- a/UNITTESTS/stubs/AT_CellularSIM_stub.cpp +++ b/UNITTESTS/stubs/AT_CellularSIM_stub.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2017, Arm Limited and affiliates. + * Copyright (c) , Arm Limited and affiliates. * SPDX-License-Identifier: Apache-2.0 * * Licensed under the Apache License, Version 2.0 (the "License"); diff --git a/UNITTESTS/stubs/AT_CellularSMS_stub.cpp b/UNITTESTS/stubs/AT_CellularSMS_stub.cpp index 66456d7295..c43d4b2c82 100644 --- a/UNITTESTS/stubs/AT_CellularSMS_stub.cpp +++ b/UNITTESTS/stubs/AT_CellularSMS_stub.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2017, Arm Limited and affiliates. + * Copyright (c) , Arm Limited and affiliates. * SPDX-License-Identifier: Apache-2.0 * * Licensed under the Apache License, Version 2.0 (the "License"); diff --git a/UNITTESTS/stubs/AT_CellularStack_stub.cpp b/UNITTESTS/stubs/AT_CellularStack_stub.cpp index 640d8c1b68..a4dd04732c 100644 --- a/UNITTESTS/stubs/AT_CellularStack_stub.cpp +++ b/UNITTESTS/stubs/AT_CellularStack_stub.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2017, Arm Limited and affiliates. + * Copyright (c) , Arm Limited and affiliates. * SPDX-License-Identifier: Apache-2.0 * * Licensed under the Apache License, Version 2.0 (the "License"); diff --git a/UNITTESTS/stubs/BufferedBlockDevice_stub.cpp b/UNITTESTS/stubs/BufferedBlockDevice_stub.cpp index 9c9fabea1f..ce7e2aba88 100644 --- a/UNITTESTS/stubs/BufferedBlockDevice_stub.cpp +++ b/UNITTESTS/stubs/BufferedBlockDevice_stub.cpp @@ -1,5 +1,6 @@ -/* mbed Microcontroller Library - * Copyright (c) 2018 ARM Limited +/* + * 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. diff --git a/UNITTESTS/stubs/CellularUtil_stub.cpp b/UNITTESTS/stubs/CellularUtil_stub.cpp index d4bcaf4916..7de3907310 100644 --- a/UNITTESTS/stubs/CellularUtil_stub.cpp +++ b/UNITTESTS/stubs/CellularUtil_stub.cpp @@ -14,6 +14,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ + #include "CellularUtil.h" #include #include diff --git a/UNITTESTS/stubs/ChainingBlockDevice_stub.cpp b/UNITTESTS/stubs/ChainingBlockDevice_stub.cpp index 51134b3553..b31de2eec2 100644 --- a/UNITTESTS/stubs/ChainingBlockDevice_stub.cpp +++ b/UNITTESTS/stubs/ChainingBlockDevice_stub.cpp @@ -1,5 +1,6 @@ -/* mbed Microcontroller Library - * Copyright (c) 2017 ARM Limited +/* + * 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. @@ -74,7 +75,7 @@ bd_size_t ChainingBlockDevice::get_erase_size() const bd_size_t ChainingBlockDevice::get_erase_size(bd_addr_t addr) const { - return 0; + return 0; } int ChainingBlockDevice::get_erase_value() const diff --git a/UNITTESTS/stubs/EventFlags_stub.cpp b/UNITTESTS/stubs/EventFlags_stub.cpp index 9c20b882f4..82b1f48f62 100644 --- a/UNITTESTS/stubs/EventFlags_stub.cpp +++ b/UNITTESTS/stubs/EventFlags_stub.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2017, Arm Limited and affiliates. + * Copyright (c) , Arm Limited and affiliates. * SPDX-License-Identifier: Apache-2.0 * * Licensed under the Apache License, Version 2.0 (the "License"); diff --git a/UNITTESTS/stubs/EventQueue_stub.cpp b/UNITTESTS/stubs/EventQueue_stub.cpp index e3436647b7..abbfc8ac21 100644 --- a/UNITTESTS/stubs/EventQueue_stub.cpp +++ b/UNITTESTS/stubs/EventQueue_stub.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2017, Arm Limited and affiliates. + * Copyright (c) , Arm Limited and affiliates. * SPDX-License-Identifier: Apache-2.0 * * Licensed under the Apache License, Version 2.0 (the "License"); @@ -17,9 +17,13 @@ #include "EventQueue.h" #include "Callback.h" +#include "EventQueue_stub.h" using namespace mbed; +int EventQueue_stub::int_value = 0; +unsigned EventQueue_stub::unsigned_value = 0; + namespace events { EventQueue::EventQueue(unsigned event_size, unsigned char *event_pointer) @@ -40,13 +44,18 @@ void EventQueue::break_dispatch() unsigned EventQueue::tick() { - return 0; + return EventQueue_stub::unsigned_value; } void EventQueue::cancel(int id) { } +int EventQueue::time_left(int id) +{ + return EventQueue_stub::int_value; +} + void EventQueue::background(Callback update) { } diff --git a/UNITTESTS/stubs/mbed_assert_stub.cpp b/UNITTESTS/stubs/EventQueue_stub.h similarity index 86% rename from UNITTESTS/stubs/mbed_assert_stub.cpp rename to UNITTESTS/stubs/EventQueue_stub.h index 341881a7f5..4dfb6ac567 100644 --- a/UNITTESTS/stubs/mbed_assert_stub.cpp +++ b/UNITTESTS/stubs/EventQueue_stub.h @@ -15,10 +15,11 @@ * limitations under the License. */ -#include "mbed_assert.h" +#include -void mbed_assert_internal(const char *expr, const char *file, int line) +namespace EventQueue_stub { - +extern int int_value; +extern unsigned unsigned_value; } diff --git a/UNITTESTS/stubs/ExhaustibleBlockDevice_stub.cpp b/UNITTESTS/stubs/ExhaustibleBlockDevice_stub.cpp index 36cdb1cfac..845da43e6d 100644 --- a/UNITTESTS/stubs/ExhaustibleBlockDevice_stub.cpp +++ b/UNITTESTS/stubs/ExhaustibleBlockDevice_stub.cpp @@ -1,5 +1,6 @@ -/* mbed Microcontroller Library - * Copyright (c) 2017 ARM Limited +/* + * 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. diff --git a/UNITTESTS/stubs/FileHandle_stub.h b/UNITTESTS/stubs/FileHandle_stub.h index ef68572c1e..41fb365ea6 100644 --- a/UNITTESTS/stubs/FileHandle_stub.h +++ b/UNITTESTS/stubs/FileHandle_stub.h @@ -14,6 +14,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ + #ifndef __FILE_HANDLE_STUB_H__ #define __FILE_HANDLE_STUB_H__ diff --git a/UNITTESTS/stubs/FlashSimBlockDevice_stub.cpp b/UNITTESTS/stubs/FlashSimBlockDevice_stub.cpp index bbc9db57ce..4f0e368981 100644 --- a/UNITTESTS/stubs/FlashSimBlockDevice_stub.cpp +++ b/UNITTESTS/stubs/FlashSimBlockDevice_stub.cpp @@ -1,5 +1,6 @@ -/* mbed Microcontroller Library - * Copyright (c) 2018 ARM Limited +/* + * 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. diff --git a/UNITTESTS/stubs/HeapBlockDevice_stub.cpp b/UNITTESTS/stubs/HeapBlockDevice_stub.cpp index afa6e9bbbc..dd39ffc28c 100644 --- a/UNITTESTS/stubs/HeapBlockDevice_stub.cpp +++ b/UNITTESTS/stubs/HeapBlockDevice_stub.cpp @@ -1,5 +1,6 @@ -/* mbed Microcontroller Library - * Copyright (c) 2017 ARM Limited +/* + * 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. diff --git a/UNITTESTS/stubs/Kernel_stub.cpp b/UNITTESTS/stubs/Kernel_stub.cpp index ecfa1d80af..0050f4481a 100644 --- a/UNITTESTS/stubs/Kernel_stub.cpp +++ b/UNITTESTS/stubs/Kernel_stub.cpp @@ -14,6 +14,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ + #include "Kernel.h" namespace rtos { diff --git a/UNITTESTS/stubs/LoRaMacChannelPlan_stub.cpp b/UNITTESTS/stubs/LoRaMacChannelPlan_stub.cpp new file mode 100644 index 0000000000..e4ebeeb849 --- /dev/null +++ b/UNITTESTS/stubs/LoRaMacChannelPlan_stub.cpp @@ -0,0 +1,52 @@ +/* + * 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 "LoRaMacChannelPlan.h" + +LoRaMacChannelPlan::LoRaMacChannelPlan() : _lora_phy(NULL) +{ +} + +LoRaMacChannelPlan::~LoRaMacChannelPlan() +{ +} + +void LoRaMacChannelPlan::activate_channelplan_subsystem(LoRaPHY *phy) +{ +} + +lorawan_status_t LoRaMacChannelPlan::set_plan(const lorawan_channelplan_t& plan) +{ + return LORAWAN_STATUS_OK; +} + +lorawan_status_t LoRaMacChannelPlan::get_plan(lorawan_channelplan_t& plan, + const channel_params_t* channel_list) +{ + return LORAWAN_STATUS_OK; +} + +lorawan_status_t LoRaMacChannelPlan::remove_plan() +{ + return LORAWAN_STATUS_OK; +} + +lorawan_status_t LoRaMacChannelPlan::remove_single_channel(uint8_t channel_id) +{ + return LORAWAN_STATUS_OK; +} + diff --git a/UNITTESTS/stubs/LoRaMacCommand_stub.cpp b/UNITTESTS/stubs/LoRaMacCommand_stub.cpp new file mode 100644 index 0000000000..4e67194d36 --- /dev/null +++ b/UNITTESTS/stubs/LoRaMacCommand_stub.cpp @@ -0,0 +1,134 @@ +/* + * 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 "LoRaMacCommand.h" +#include "LoRaMac.h" + +#include "LoRaMacCommand_stub.h" + +lorawan_status_t LoRaMacCommand_stub::status_value = LORAWAN_STATUS_OK; +bool LoRaMacCommand_stub::bool_value = false; +uint8_t LoRaMacCommand_stub::uint8_value = 0; +int32_t LoRaMacCommand_stub::int32_value = 0; + +LoRaMacCommand::LoRaMacCommand() +{ +} + +void LoRaMacCommand::clear_command_buffer() +{ +} + +uint8_t LoRaMacCommand::get_mac_cmd_length() const +{ + return LoRaMacCommand_stub::uint8_value; +} + +uint8_t *LoRaMacCommand::get_mac_commands_buffer() +{ + return &LoRaMacCommand_stub::uint8_value; +} + +void LoRaMacCommand::parse_mac_commands_to_repeat() +{ +} + + +void LoRaMacCommand::clear_repeat_buffer() +{ +} + +void LoRaMacCommand::copy_repeat_commands_to_buffer() +{ +} + +uint8_t LoRaMacCommand::get_repeat_commands_length() const +{ + return LoRaMacCommand_stub::uint8_value; +} + +void LoRaMacCommand::clear_sticky_mac_cmd() +{ +} + +bool LoRaMacCommand::has_sticky_mac_cmd() const +{ + return LoRaMacCommand_stub::bool_value; +} + +lorawan_status_t LoRaMacCommand::process_mac_commands(const uint8_t *payload, uint8_t mac_index, + uint8_t commands_size, uint8_t snr, + loramac_mlme_confirm_t& mlme_conf, + lora_mac_system_params_t &mac_sys_params, + LoRaPHY &lora_phy) +{ + return LoRaMacCommand_stub::status_value; +} + +int32_t LoRaMacCommand::cmd_buffer_remaining() const +{ + return LoRaMacCommand_stub::int32_value; +} + +void LoRaMacCommand::set_batterylevel_callback(mbed::Callback battery_level) +{ +} + +lorawan_status_t LoRaMacCommand::add_link_check_req() +{ + return LoRaMacCommand_stub::status_value; +} + +lorawan_status_t LoRaMacCommand::add_link_adr_ans(uint8_t status) +{ + return LoRaMacCommand_stub::status_value; +} + +lorawan_status_t LoRaMacCommand::add_duty_cycle_ans() +{ + return LoRaMacCommand_stub::status_value; +} + +lorawan_status_t LoRaMacCommand::add_rx_param_setup_ans(uint8_t status) +{ + return LoRaMacCommand_stub::status_value; +} + +lorawan_status_t LoRaMacCommand::add_dev_status_ans(uint8_t battery, uint8_t margin) +{ + return LoRaMacCommand_stub::status_value; +} + +lorawan_status_t LoRaMacCommand::add_new_channel_ans(uint8_t status) +{ + return LoRaMacCommand_stub::status_value; +} + +lorawan_status_t LoRaMacCommand::add_rx_timing_setup_ans() +{ + return LoRaMacCommand_stub::status_value; +} + +lorawan_status_t LoRaMacCommand::add_tx_param_setup_ans() +{ + return LoRaMacCommand_stub::status_value; +} + +lorawan_status_t LoRaMacCommand::add_dl_channel_ans(uint8_t status) +{ + return LoRaMacCommand_stub::status_value; +} diff --git a/UNITTESTS/stubs/LoRaMacCommand_stub.h b/UNITTESTS/stubs/LoRaMacCommand_stub.h new file mode 100644 index 0000000000..c7ce5f3716 --- /dev/null +++ b/UNITTESTS/stubs/LoRaMacCommand_stub.h @@ -0,0 +1,28 @@ +/* + * 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 +#include "lorawan_types.h" + +namespace LoRaMacCommand_stub +{ +extern lorawan_status_t status_value; +extern bool bool_value; +extern uint8_t uint8_value; +extern int32_t int32_value; +} diff --git a/UNITTESTS/stubs/LoRaMacCrypto_stub.cpp b/UNITTESTS/stubs/LoRaMacCrypto_stub.cpp new file mode 100644 index 0000000000..b4b272d23c --- /dev/null +++ b/UNITTESTS/stubs/LoRaMacCrypto_stub.cpp @@ -0,0 +1,66 @@ +/* + * 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 +#include + +#include "LoRaMacCrypto.h" +#include "system/lorawan_data_structures.h" + +#include "LoRaMacCrypto_stub.h" + +int LoRaMacCrypto_stub::int_table_idx_value = 0; +int LoRaMacCrypto_stub::int_table[20] = {}; + +LoRaMacCrypto::LoRaMacCrypto() +{ +} + +int LoRaMacCrypto::compute_mic(const uint8_t *, uint16_t , const uint8_t *, uint32_t, uint32_t, + uint8_t dir, uint32_t, uint32_t *) +{ + return LoRaMacCrypto_stub::int_table[LoRaMacCrypto_stub::int_table_idx_value++]; +} + +int LoRaMacCrypto::encrypt_payload(const uint8_t *, uint16_t , const uint8_t *, uint32_t, uint32_t, + uint8_t , uint32_t , uint8_t *) +{ + return LoRaMacCrypto_stub::int_table[LoRaMacCrypto_stub::int_table_idx_value++]; +} + +int LoRaMacCrypto::decrypt_payload(const uint8_t *, uint16_t , const uint8_t *, uint32_t, uint32_t, + uint8_t , uint32_t , uint8_t *) +{ + return LoRaMacCrypto_stub::int_table[LoRaMacCrypto_stub::int_table_idx_value++]; +} + +int LoRaMacCrypto::compute_join_frame_mic(const uint8_t *, uint16_t , const uint8_t *, uint32_t, uint32_t *) +{ + return LoRaMacCrypto_stub::int_table[LoRaMacCrypto_stub::int_table_idx_value++]; +} + +int LoRaMacCrypto::decrypt_join_frame(const uint8_t *in, uint16_t size, const uint8_t *, uint32_t, uint8_t *out) +{ + memcpy(out, in, size); + return LoRaMacCrypto_stub::int_table[LoRaMacCrypto_stub::int_table_idx_value++]; +} + +int LoRaMacCrypto::compute_skeys_for_join_frame(const uint8_t *, uint32_t, const uint8_t *, uint16_t , + uint8_t *, uint8_t *) +{ + return LoRaMacCrypto_stub::int_table[LoRaMacCrypto_stub::int_table_idx_value++]; +} diff --git a/UNITTESTS/stubs/LoRaMacCrypto_stub.h b/UNITTESTS/stubs/LoRaMacCrypto_stub.h new file mode 100644 index 0000000000..4a3a568efa --- /dev/null +++ b/UNITTESTS/stubs/LoRaMacCrypto_stub.h @@ -0,0 +1,25 @@ +/* + * 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 + +namespace LoRaMacCrypto_stub +{ +extern int int_table_idx_value; +extern int int_table[20]; +} diff --git a/UNITTESTS/stubs/LoRaMac_stub.cpp b/UNITTESTS/stubs/LoRaMac_stub.cpp new file mode 100644 index 0000000000..b9b5139bea --- /dev/null +++ b/UNITTESTS/stubs/LoRaMac_stub.cpp @@ -0,0 +1,458 @@ +/* + * 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 +#include "LoRaMac.h" +#include "LoRaMac_stub.h" + +using namespace events; +using namespace mbed; + +int LoRaMac_stub::bool_false_counter = 0; +int LoRaMac_stub::bool_true_counter = 0; +bool LoRaMac_stub::bool_value = false; +int LoRaMac_stub::int_value = 0; +rx_slot_t LoRaMac_stub::slot_value = RX_SLOT_WIN_1; +lorawan_status_t LoRaMac_stub::status_value = LORAWAN_STATUS_OK; +loramac_mcps_confirm_t *LoRaMac_stub::mcps_conf_ptr = NULL; +loramac_mcps_indication_t *LoRaMac_stub::mcps_ind_ptr = NULL; +loramac_mlme_confirm_t *LoRaMac_stub::mlme_conf_ptr = NULL; +loramac_mlme_indication_t *LoRaMac_stub::mlme_ind_ptr = NULL; +device_class_t LoRaMac_stub::dev_class_value = CLASS_A; +mbed::Callback LoRaMac_stub::_ack_expiry_handler_for_class_c = NULL; +mbed::Callback LoRaMac_stub::_scheduling_failure_handler = NULL; + + +LoRaMac::LoRaMac() + : _lora_time(), + _lora_phy(NULL), + _mac_commands(), + _channel_plan(), + _lora_crypto(), + _ev_queue(NULL), + _mcps_indication(), + _mcps_confirmation(), + _mlme_indication(), + _mlme_confirmation(), + _is_nwk_joined(false), + _continuous_rx2_window_open(false), + _device_class(CLASS_A) +{} + +LoRaMac::~LoRaMac() +{ +} + +const loramac_mcps_confirm_t *LoRaMac::get_mcps_confirmation() const +{ + return LoRaMac_stub::mcps_conf_ptr; +} + +const loramac_mcps_indication_t *LoRaMac::get_mcps_indication() const +{ + return LoRaMac_stub::mcps_ind_ptr; +} + +const loramac_mlme_confirm_t *LoRaMac::get_mlme_confirmation() const +{ + return LoRaMac_stub::mlme_conf_ptr; +} + +const loramac_mlme_indication_t *LoRaMac::get_mlme_indication() const +{ + return LoRaMac_stub::mlme_ind_ptr; +} + +void LoRaMac::post_process_mlme_request() +{ +} + +void LoRaMac::post_process_mcps_req() +{ +} + +void LoRaMac::post_process_mcps_ind() +{ +} + +void LoRaMac::post_process_mlme_ind() +{ +} + +lorawan_time_t LoRaMac::get_current_time(void) +{ +} + +rx_slot_t LoRaMac::get_current_slot(void) +{ + return LoRaMac_stub::slot_value; +} + +void LoRaMac::handle_join_accept_frame(const uint8_t *payload, uint16_t size) +{ +} + +void LoRaMac::check_frame_size(uint16_t size) +{ +} + +bool LoRaMac::message_integrity_check(const uint8_t *const payload, + const uint16_t size, + uint8_t *const ptr_pos, + uint32_t address, + uint32_t *downlink_counter, + const uint8_t *nwk_skey) +{ + if (LoRaMac_stub::bool_false_counter) { + LoRaMac_stub::bool_false_counter--; + return false; + } + if (LoRaMac_stub::bool_true_counter) { + LoRaMac_stub::bool_true_counter--; + return true; + } + + return LoRaMac_stub::bool_value; +} + +void LoRaMac::extract_data_and_mac_commands(const uint8_t *payload, + uint16_t size, + uint8_t fopts_len, + uint8_t *nwk_skey, + uint8_t *app_skey, + uint32_t address, + uint32_t downlink_counter, + int16_t rssi, + int8_t snr) +{ +} + +void LoRaMac::extract_mac_commands_only(const uint8_t *payload, + int8_t snr, + uint8_t fopts_len) +{ +} + +void LoRaMac::handle_data_frame(const uint8_t *const payload, + const uint16_t size, + uint8_t ptr_pos, + uint8_t msg_type, + int16_t rssi, + int8_t snr) +{ +} + +void LoRaMac::set_batterylevel_callback(mbed::Callback battery_level) +{ +} + +void LoRaMac::on_radio_tx_done(lorawan_time_t timestamp) +{ +} + +void LoRaMac::on_radio_rx_done(const uint8_t *const payload, uint16_t size, + int16_t rssi, int8_t snr) +{ +} + +void LoRaMac::on_radio_tx_timeout(void) +{ +} + +void LoRaMac::on_radio_rx_timeout(bool is_timeout) +{ +} + +bool LoRaMac::continue_joining_process() +{ + if (LoRaMac_stub::bool_false_counter) { + LoRaMac_stub::bool_false_counter--; + return false; + } + if (LoRaMac_stub::bool_true_counter) { + LoRaMac_stub::bool_true_counter--; + return true; + } + return LoRaMac_stub::bool_value; +} + +bool LoRaMac::continue_sending_process() +{ + if (LoRaMac_stub::bool_false_counter) { + LoRaMac_stub::bool_false_counter--; + return false; + } + if (LoRaMac_stub::bool_true_counter) { + LoRaMac_stub::bool_true_counter--; + return true; + } + return LoRaMac_stub::bool_value; +} + +lorawan_status_t LoRaMac::send_join_request() +{ + return LoRaMac_stub::status_value; +} + +lorawan_status_t LoRaMac::handle_retransmission() +{ + return LoRaMac_stub::status_value; +} + +void LoRaMac::on_backoff_timer_expiry(void) +{ +} + +void LoRaMac::open_rx1_window(void) +{ +} + +void LoRaMac::open_rx2_window() +{ +} + +void LoRaMac::on_ack_timeout_timer_event(void) +{ +} + +bool LoRaMac::validate_payload_length(uint16_t length, + int8_t datarate, + uint8_t fopts_len) +{ + if (LoRaMac_stub::bool_false_counter) { + LoRaMac_stub::bool_false_counter--; + return false; + } + if (LoRaMac_stub::bool_true_counter) { + LoRaMac_stub::bool_true_counter--; + return true; + } + return LoRaMac_stub::bool_value; +} + +void LoRaMac::set_mlme_schedule_ul_indication(void) +{ +} + +// This is not actual transmission. It just schedules a message in response +// to MCPS request +lorawan_status_t LoRaMac::send(loramac_mhdr_t *machdr, const uint8_t fport, + const void *fbuffer, uint16_t fbuffer_size) +{ + return LoRaMac_stub::status_value; +} + +int LoRaMac::get_backoff_timer_event_id(void) +{ + return LoRaMac_stub::int_value; +} + +lorawan_status_t LoRaMac::clear_tx_pipe(void) +{ + return LoRaMac_stub::status_value; +} + +lorawan_status_t LoRaMac::schedule_tx() +{ + return LoRaMac_stub::status_value; +} + +void LoRaMac::calculate_backOff(uint8_t channel) +{ +} + +void LoRaMac::reset_mac_parameters(void) +{ +} + +uint8_t LoRaMac::get_default_tx_datarate() +{ + return 0; +} + +void LoRaMac::enable_adaptive_datarate(bool adr_enabled) +{ +} + +lorawan_status_t LoRaMac::set_channel_data_rate(uint8_t data_rate) +{ + return LoRaMac_stub::status_value; +} + +bool LoRaMac::tx_ongoing() +{ + if (LoRaMac_stub::bool_false_counter) { + LoRaMac_stub::bool_false_counter--; + return false; + } + if (LoRaMac_stub::bool_true_counter) { + LoRaMac_stub::bool_true_counter--; + return true; + } + return LoRaMac_stub::bool_value; +} + +void LoRaMac::set_tx_ongoing(bool ongoing) +{ +} + +void LoRaMac::reset_ongoing_tx(bool reset_pending) +{ +} + +int16_t LoRaMac::prepare_ongoing_tx(const uint8_t port, + const uint8_t *const data, + uint16_t length, + uint8_t flags, + uint8_t num_retries) +{ + return 0; +} + +lorawan_status_t LoRaMac::send_ongoing_tx() +{ + return LoRaMac_stub::status_value; +} + +device_class_t LoRaMac::get_device_class() const +{ + return LoRaMac_stub::dev_class_value; +} + +void LoRaMac::set_device_class(const device_class_t &device_class, + mbed::Callbackack_expiry_handler) +{ + LoRaMac_stub::_ack_expiry_handler_for_class_c = ack_expiry_handler; +} + +void LoRaMac::setup_link_check_request() +{ +} + +lorawan_status_t LoRaMac::prepare_join(const lorawan_connect_t *params, bool is_otaa) +{ + return LoRaMac_stub::status_value; +} + +lorawan_status_t LoRaMac::join(bool is_otaa) +{ + return LoRaMac_stub::status_value; +} + +static void memcpy_convert_endianess(uint8_t *dst, + const uint8_t *src, + uint16_t size) +{ +} + +lorawan_status_t LoRaMac::prepare_frame(loramac_mhdr_t *machdr, + loramac_frame_ctrl_t *fctrl, + const uint8_t fport, + const void *fbuffer, + uint16_t fbuffer_size) +{ + return LoRaMac_stub::status_value; +} + +lorawan_status_t LoRaMac::send_frame_on_channel(uint8_t channel) +{ + return LoRaMac_stub::status_value; +} + +void LoRaMac::reset_mcps_confirmation() +{ +} + +void LoRaMac::reset_mlme_confirmation() +{ +} + +void LoRaMac::reset_mcps_indication() +{ +} + +void LoRaMac::set_tx_continuous_wave(uint8_t channel, int8_t datarate, int8_t tx_power, + float max_eirp, float antenna_gain, uint16_t timeout) +{ +} + +lorawan_status_t LoRaMac::initialize(EventQueue *queue, + mbed::Callbackscheduling_failure_handler) +{ + LoRaMac_stub::_scheduling_failure_handler = scheduling_failure_handler; + return LoRaMac_stub::status_value; +} + +void LoRaMac::disconnect() +{ +} + +uint8_t LoRaMac::get_max_possible_tx_size(uint8_t fopts_len) +{ + return 0; +} + +bool LoRaMac::nwk_joined() +{ + if (LoRaMac_stub::bool_false_counter) { + LoRaMac_stub::bool_false_counter--; + return false; + } + if (LoRaMac_stub::bool_true_counter) { + LoRaMac_stub::bool_true_counter--; + return true; + } + return LoRaMac_stub::bool_value; +} + +void LoRaMac::set_nwk_joined(bool joined) +{ +} + +lorawan_status_t LoRaMac::add_channel_plan(const lorawan_channelplan_t &plan) +{ + return LoRaMac_stub::status_value; +} + +lorawan_status_t LoRaMac::remove_channel_plan() +{ + return LoRaMac_stub::status_value; +} + +lorawan_status_t LoRaMac::get_channel_plan(lorawan_channelplan_t &plan) +{ + return LoRaMac_stub::status_value; +} + +lorawan_status_t LoRaMac::remove_single_channel(uint8_t id) +{ + return LoRaMac_stub::status_value; +} + +lorawan_status_t LoRaMac::multicast_channel_link(multicast_params_t *channel_param) +{ + return LoRaMac_stub::status_value; +} + +lorawan_status_t LoRaMac::multicast_channel_unlink(multicast_params_t *channel_param) +{ + return LoRaMac_stub::status_value; +} + +void LoRaMac::bind_phy(LoRaPHY &phy) +{ +} diff --git a/UNITTESTS/stubs/LoRaMac_stub.h b/UNITTESTS/stubs/LoRaMac_stub.h new file mode 100644 index 0000000000..f1bbd10517 --- /dev/null +++ b/UNITTESTS/stubs/LoRaMac_stub.h @@ -0,0 +1,40 @@ +/* + * 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 "stdint.h" +#include "stdbool.h" +#include + +#include "lorawan_types.h" +#include "lorawan_data_structures.h" + +namespace LoRaMac_stub +{ +extern bool bool_value; +extern int bool_false_counter; +extern int bool_true_counter; +extern int int_value; +extern rx_slot_t slot_value; +extern lorawan_status_t status_value; +extern loramac_mcps_confirm_t *mcps_conf_ptr; +extern loramac_mcps_indication_t *mcps_ind_ptr; +extern loramac_mlme_confirm_t *mlme_conf_ptr; +extern loramac_mlme_indication_t *mlme_ind_ptr; +extern device_class_t dev_class_value; +extern mbed::Callback _ack_expiry_handler_for_class_c; +extern mbed::Callback _scheduling_failure_handler; +} diff --git a/UNITTESTS/stubs/LoRaPHYEU868_stub.cpp b/UNITTESTS/stubs/LoRaPHYEU868_stub.cpp new file mode 100644 index 0000000000..a425f30a12 --- /dev/null +++ b/UNITTESTS/stubs/LoRaPHYEU868_stub.cpp @@ -0,0 +1,28 @@ +/* + * 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 "LoRaPHYEU868.h" +#include "lora_phy_ds.h" + +LoRaPHYEU868::LoRaPHYEU868() +{ +} + +LoRaPHYEU868::~LoRaPHYEU868() +{ +} + diff --git a/UNITTESTS/stubs/LoRaPHY_stub.cpp b/UNITTESTS/stubs/LoRaPHY_stub.cpp new file mode 100644 index 0000000000..978cf1dd0b --- /dev/null +++ b/UNITTESTS/stubs/LoRaPHY_stub.cpp @@ -0,0 +1,413 @@ +/* + * 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 +#include +#include +#include + +#include "LoRaPHY.h" +#include "LoRaPHY_stub.h" + +LoRaRadio *LoRaPHY_stub::radio = NULL; +uint32_t LoRaPHY_stub::uint32_value = 0; +uint16_t LoRaPHY_stub::uint16_value = 0; +uint8_t LoRaPHY_stub::uint8_value = 0; +int8_t LoRaPHY_stub::int8_value = 0; +int LoRaPHY_stub::int_value = 0; +double LoRaPHY_stub::double_value = 0; +lorawan_status_t LoRaPHY_stub::lorawan_status_value = LORAWAN_STATUS_OK; +channel_params_t *LoRaPHY_stub::channel_params_ptr = NULL; +uint8_t LoRaPHY_stub::bool_counter = 0; +bool LoRaPHY_stub::bool_table[20] = {}; +uint8_t LoRaPHY_stub::linkAdrNbBytesParsed = 0; +uint8_t LoRaPHY_stub::ch_mask_value = 0; +uint8_t LoRaPHY_stub::adr_parse_count = 0; + +LoRaPHY::LoRaPHY() + : _radio(LoRaPHY_stub::radio) +{ +} + +LoRaPHY::~LoRaPHY() +{ +} + +void LoRaPHY::initialize(LoRaWANTimeHandler *lora_time) +{ +} + +bool LoRaPHY::mask_bit_test(const uint16_t *mask, unsigned bit) +{ + return LoRaPHY_stub::bool_table[LoRaPHY_stub::bool_counter++]; +} + +void LoRaPHY::mask_bit_set(uint16_t *mask, unsigned bit) +{ +} + +void LoRaPHY::mask_bit_clear(uint16_t *mask, unsigned bit) +{ +} + +void LoRaPHY::set_radio_instance(LoRaRadio &radio) +{ +} + +void LoRaPHY::put_radio_to_sleep() +{ +} + +void LoRaPHY::put_radio_to_standby() +{ +} + +void LoRaPHY::setup_public_network_mode(bool set) +{ +} + +void LoRaPHY::handle_receive(void) +{ +} + +uint32_t LoRaPHY::get_radio_rng() +{ + return LoRaPHY_stub::uint32_value; +} + +void LoRaPHY::handle_send(uint8_t *buf, uint8_t size) +{ +} + +uint8_t LoRaPHY::request_new_channel(int8_t channel_id, channel_params_t *new_channel) +{ + return LoRaPHY_stub::uint8_value; +} + +int32_t LoRaPHY::get_random(int32_t min, int32_t max) +{ + return LoRaPHY_stub::uint32_value; +} + +bool LoRaPHY::verify_channel_DR(uint16_t *channel_mask, int8_t dr) +{ + return LoRaPHY_stub::bool_table[LoRaPHY_stub::bool_counter++]; +} + +bool LoRaPHY::val_in_range(int8_t value, int8_t min, int8_t max) +{ + return LoRaPHY_stub::bool_table[LoRaPHY_stub::bool_counter++]; +} + +bool LoRaPHY::disable_channel(uint16_t *channel_mask, uint8_t id, + uint8_t max_channels_num) +{ + return LoRaPHY_stub::bool_table[LoRaPHY_stub::bool_counter++]; +} + +uint8_t LoRaPHY::count_bits(uint16_t mask, uint8_t nbBits) +{ + return LoRaPHY_stub::uint8_value; +} + +uint8_t LoRaPHY::num_active_channels(uint16_t *channel_mask, uint8_t start_idx, + uint8_t stop_idx) +{ + return LoRaPHY_stub::uint8_value; +} + +void LoRaPHY::copy_channel_mask(uint16_t *dest_mask, uint16_t *src_mask, uint8_t len) +{ + if ((dest_mask != NULL) && (src_mask != NULL)) { + for (uint8_t i = 0; i < len; i++) { + dest_mask[i] = src_mask[i]; + } + } +} + +void LoRaPHY::set_last_tx_done(uint8_t channel, bool joined, lorawan_time_t last_tx_done_time) +{ +} + +lorawan_time_t LoRaPHY::update_band_timeoff(bool joined, bool duty_cycle, + band_t *bands, uint8_t nb_bands) +{ + return LoRaPHY_stub::uint32_value; +} + +uint8_t LoRaPHY::parse_link_ADR_req(const uint8_t *payload, + link_adr_params_t *params) +{ + params->ch_mask_ctrl = LoRaPHY_stub::ch_mask_value; + + if (LoRaPHY_stub::adr_parse_count) { + return --LoRaPHY_stub::adr_parse_count; + } + + return LoRaPHY_stub::uint8_value; +} + +uint8_t LoRaPHY::verify_link_ADR_req(verify_adr_params_t *verify_params, + int8_t *dr, int8_t *tx_pow, uint8_t *nb_rep) +{ + return LoRaPHY_stub::uint8_value; +} + +void LoRaPHY::get_rx_window_params(double t_symb, uint8_t min_rx_symb, + uint32_t rx_error, uint32_t wakeup_time, + uint32_t *window_timeout, int32_t *window_offset) +{ +} + +int8_t LoRaPHY::compute_tx_power(int8_t tx_power_idx, float max_eirp, + float antenna_gain) +{ + return LoRaPHY_stub::int8_value; +} + + +int8_t LoRaPHY::get_next_lower_dr(int8_t dr, int8_t min_dr) +{ + return LoRaPHY_stub::int8_value; +} + +uint8_t LoRaPHY::get_bandwidth(uint8_t dr) +{ + return LoRaPHY_stub::uint8_value; +} + +uint8_t LoRaPHY::enabled_channel_count(uint8_t datarate, + const uint16_t *channel_mask, + uint8_t *channel_indices, + uint8_t *delayTx) +{ + return LoRaPHY_stub::uint8_value; +} + +bool LoRaPHY::is_datarate_supported(const int8_t datarate) const +{ + return LoRaPHY_stub::bool_table[LoRaPHY_stub::bool_counter++]; +} + +void LoRaPHY::reset_to_default_values(loramac_protocol_params *params, bool init) +{ +} + +int8_t LoRaPHY::get_next_lower_tx_datarate(int8_t datarate) +{ + return LoRaPHY_stub::int8_value; +} + +uint8_t LoRaPHY::get_minimum_rx_datarate() +{ + return LoRaPHY_stub::uint8_value; +} + +uint8_t LoRaPHY::get_minimum_tx_datarate() +{ + return LoRaPHY_stub::uint8_value; +} + +uint8_t LoRaPHY::get_default_tx_datarate() +{ + return LoRaPHY_stub::uint8_value; +} + +uint8_t LoRaPHY::get_default_max_tx_datarate() +{ + return LoRaPHY_stub::uint8_value; +} + +uint8_t LoRaPHY::get_default_tx_power() +{ + return LoRaPHY_stub::uint8_value; +} + +uint8_t LoRaPHY::get_max_payload(uint8_t datarate, bool use_repeater) +{ + return LoRaPHY_stub::uint8_value; +} + +uint16_t LoRaPHY::get_maximum_frame_counter_gap() +{ + return LoRaPHY_stub::uint16_value; +} + +uint32_t LoRaPHY::get_ack_timeout() +{ + return LoRaPHY_stub::uint32_value; +} + +uint32_t LoRaPHY::get_default_rx2_frequency() +{ + return LoRaPHY_stub::uint32_value; +} + +uint8_t LoRaPHY::get_default_rx2_datarate() +{ + return phy_params.rx_window2_datarate; +} + +uint16_t *LoRaPHY::get_channel_mask(bool get_default) +{ + return &LoRaPHY_stub::uint16_value; +} + +uint8_t LoRaPHY::get_max_nb_channels() +{ + return LoRaPHY_stub::uint8_value; +} + +channel_params_t *LoRaPHY::get_phy_channels() +{ + return LoRaPHY_stub::channel_params_ptr; +} + +bool LoRaPHY::is_custom_channel_plan_supported() +{ + return LoRaPHY_stub::bool_table[LoRaPHY_stub::bool_counter++]; +} + +void LoRaPHY::restore_default_channels() +{ +} + +bool LoRaPHY::verify_rx_datarate(uint8_t datarate) +{ + return LoRaPHY_stub::bool_table[LoRaPHY_stub::bool_counter++]; +} + +bool LoRaPHY::verify_tx_datarate(uint8_t datarate, bool use_default) +{ + return LoRaPHY_stub::bool_table[LoRaPHY_stub::bool_counter++]; +} + +bool LoRaPHY::verify_tx_power(uint8_t tx_power) +{ + return LoRaPHY_stub::bool_table[LoRaPHY_stub::bool_counter++]; +} + +bool LoRaPHY::verify_duty_cycle(bool cycle) +{ + return LoRaPHY_stub::bool_table[LoRaPHY_stub::bool_counter++]; +} + +bool LoRaPHY::verify_nb_join_trials(uint8_t nb_join_trials) +{ + return LoRaPHY_stub::bool_table[LoRaPHY_stub::bool_counter++]; +} + +void LoRaPHY::apply_cf_list(const uint8_t *payload, uint8_t size) +{ +} + + +bool LoRaPHY::get_next_ADR(bool restore_channel_mask, int8_t &dr_out, + int8_t &tx_power_out, uint32_t &adr_ack_cnt) +{ + return LoRaPHY_stub::bool_table[LoRaPHY_stub::bool_counter++]; +} + +void LoRaPHY::compute_rx_win_params(int8_t datarate, uint8_t min_rx_symbols, + uint32_t rx_error, + rx_config_params_t *rx_conf_params) +{ +} + +bool LoRaPHY::rx_config(rx_config_params_t *rx_conf) +{ + return LoRaPHY_stub::bool_table[LoRaPHY_stub::bool_counter++]; +} + +bool LoRaPHY::tx_config(tx_config_params_t *tx_conf, int8_t *tx_power, + lorawan_time_t *tx_toa) +{ + return LoRaPHY_stub::bool_table[LoRaPHY_stub::bool_counter++]; +} + +uint8_t LoRaPHY::link_ADR_request(adr_req_params_t *link_adr_req, + int8_t *dr_out, int8_t *tx_power_out, + uint8_t *nb_rep_out, uint8_t *nb_bytes_processed) +{ + *nb_bytes_processed = LoRaPHY_stub::linkAdrNbBytesParsed; + return LoRaPHY_stub::uint8_value; +} + +uint8_t LoRaPHY::accept_rx_param_setup_req(rx_param_setup_req_t *params) +{ + return LoRaPHY_stub::uint8_value; +} + +bool LoRaPHY::accept_tx_param_setup_req(uint8_t ul_dwell_time, uint8_t dl_dwell_time) +{ + return LoRaPHY_stub::bool_table[LoRaPHY_stub::bool_counter++]; +} + +int LoRaPHY::lookup_band_for_frequency(uint32_t freq) const +{ + return LoRaPHY_stub::int_value; +} + +bool LoRaPHY::verify_frequency_for_band(uint32_t freq, uint8_t band) const +{ + return LoRaPHY_stub::bool_table[LoRaPHY_stub::bool_counter++]; +} + +uint8_t LoRaPHY::dl_channel_request(uint8_t channel_id, uint32_t rx1_frequency) +{ + return LoRaPHY_stub::uint8_value; +} + +int8_t LoRaPHY::get_alternate_DR(uint8_t nb_trials) +{ + return LoRaPHY_stub::int8_value; +} + +void LoRaPHY::calculate_backoff(bool joined, bool last_tx_was_join_req, bool dc_enabled, uint8_t channel, + lorawan_time_t elapsed_time, lorawan_time_t tx_toa) +{ +} + +lorawan_status_t LoRaPHY::set_next_channel(channel_selection_params_t *params, + uint8_t *channel, lorawan_time_t *time, + lorawan_time_t *aggregate_timeoff) +{ + return LoRaPHY_stub::lorawan_status_value; +} + +lorawan_status_t LoRaPHY::add_channel(const channel_params_t *new_channel, + uint8_t id) +{ + return LoRaPHY_stub::lorawan_status_value; +} + +bool LoRaPHY::remove_channel(uint8_t channel_id) +{ + return LoRaPHY_stub::bool_table[LoRaPHY_stub::bool_counter++]; +} + +void LoRaPHY::set_tx_cont_mode(cw_mode_params_t *params, uint32_t given_frequency) +{ +} + +uint8_t LoRaPHY::apply_DR_offset(int8_t dr, int8_t dr_offset) +{ + return LoRaPHY_stub::uint8_value; +} + + diff --git a/UNITTESTS/stubs/LoRaPHY_stub.h b/UNITTESTS/stubs/LoRaPHY_stub.h new file mode 100644 index 0000000000..12a4e589bf --- /dev/null +++ b/UNITTESTS/stubs/LoRaPHY_stub.h @@ -0,0 +1,40 @@ +/* + * 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 "stdint.h" +#include "stdbool.h" +#include + +#include "lorawan_types.h" + +namespace LoRaPHY_stub +{ +extern LoRaRadio *radio; +extern uint8_t bool_counter; +extern bool bool_table[20]; +extern uint32_t uint32_value; +extern uint8_t uint8_value; +extern int8_t int8_value; +extern int int_value; +extern double double_value; +extern uint16_t uint16_value; +extern lorawan_status_t lorawan_status_value; +extern channel_params_t* channel_params_ptr; +extern uint8_t linkAdrNbBytesParsed; +extern uint8_t ch_mask_value; +extern uint8_t adr_parse_count; +} diff --git a/UNITTESTS/stubs/LoRaWANStack_stub.cpp b/UNITTESTS/stubs/LoRaWANStack_stub.cpp new file mode 100644 index 0000000000..628ea0faf6 --- /dev/null +++ b/UNITTESTS/stubs/LoRaWANStack_stub.cpp @@ -0,0 +1,298 @@ +/* + * 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 +#include + +#include "LoRaWANStack.h" + +using namespace mbed; +using namespace events; + +/***************************************************************************** + * Constructor * + ****************************************************************************/ +LoRaWANStack::LoRaWANStack() + : _loramac(), + _device_current_state(DEVICE_STATE_NOT_INITIALIZED), + _lw_session(), + _tx_msg(), + _rx_msg(), + _tx_metadata(), + _rx_metadata(), + _num_retry(1), + _ctrl_flags(0), + _app_port(12), + _link_check_requested(false), + _automatic_uplink_ongoing(false), + _ready_for_rx(true), + _queue(NULL) +{ +} + +void LoRaWANStack::bind_phy_and_radio_driver(LoRaRadio &radio, LoRaPHY &phy) +{ +} + +lorawan_status_t LoRaWANStack::initialize_mac_layer(EventQueue *queue) +{ + return LORAWAN_STATUS_OK; +} + +lorawan_status_t LoRaWANStack::set_lora_callbacks(const lorawan_app_callbacks_t *callbacks) +{ + return LORAWAN_STATUS_OK; +} + +lorawan_status_t LoRaWANStack::connect() +{ + return LORAWAN_STATUS_OK; +} + +lorawan_status_t LoRaWANStack::connect(const lorawan_connect_t &connect) +{ + return LORAWAN_STATUS_OK; +} + +lorawan_status_t LoRaWANStack::add_channels(const lorawan_channelplan_t &channel_plan) +{ + return LORAWAN_STATUS_OK; +} + +lorawan_status_t LoRaWANStack::remove_a_channel(uint8_t channel_id) +{ + return LORAWAN_STATUS_OK; +} + +lorawan_status_t LoRaWANStack::drop_channel_list() +{ + return LORAWAN_STATUS_OK; +} + +lorawan_status_t LoRaWANStack::get_enabled_channels(lorawan_channelplan_t &channel_plan) +{ + return LORAWAN_STATUS_OK; +} + +lorawan_status_t LoRaWANStack::set_confirmed_msg_retry(uint8_t count) +{ + return LORAWAN_STATUS_OK; +} + +lorawan_status_t LoRaWANStack::set_channel_data_rate(uint8_t data_rate) +{ + return LORAWAN_STATUS_OK; +} + +lorawan_status_t LoRaWANStack::enable_adaptive_datarate(bool adr_enabled) +{ + return LORAWAN_STATUS_OK; +} + +lorawan_status_t LoRaWANStack::stop_sending(void) +{ + return LORAWAN_STATUS_OK; +} + +int16_t LoRaWANStack::handle_tx(const uint8_t port, const uint8_t *data, + uint16_t length, uint8_t flags, + bool null_allowed, bool allow_port_0) +{ + return 0; +} + +int16_t LoRaWANStack::handle_rx(uint8_t *data, uint16_t length, uint8_t &port, int &flags, bool validate_params) +{ + return 0; +} + +lorawan_status_t LoRaWANStack::set_link_check_request() +{ + return LORAWAN_STATUS_OK; +} + +void LoRaWANStack::remove_link_check_request() +{ +} + +lorawan_status_t LoRaWANStack::shutdown() +{ + return LORAWAN_STATUS_OK; +} + +lorawan_status_t LoRaWANStack::set_device_class(const device_class_t &device_class) +{ + return LORAWAN_STATUS_OK; +} + +lorawan_status_t LoRaWANStack::acquire_tx_metadata(lorawan_tx_metadata &tx_metadata) +{ + return LORAWAN_STATUS_OK; +} + +lorawan_status_t LoRaWANStack::acquire_rx_metadata(lorawan_rx_metadata &metadata) +{ + return LORAWAN_STATUS_OK; +} + +lorawan_status_t LoRaWANStack::acquire_backoff_metadata(int &backoff) +{ + return LORAWAN_STATUS_OK; +} + +/***************************************************************************** + * Interrupt handlers * + ****************************************************************************/ +void LoRaWANStack::tx_interrupt_handler(void) +{ +} + +void LoRaWANStack::rx_interrupt_handler(const uint8_t *payload, uint16_t size, + int16_t rssi, int8_t snr) +{ +} + +void LoRaWANStack::rx_error_interrupt_handler(void) +{ +} + +void LoRaWANStack::tx_timeout_interrupt_handler(void) +{ +} + +void LoRaWANStack::rx_timeout_interrupt_handler(void) +{ +} + +void LoRaWANStack::process_transmission_timeout() +{ +} + +void LoRaWANStack::process_transmission(void) +{ +} + +void LoRaWANStack::handle_ack_expiry_for_class_c(void) +{ +} + +void LoRaWANStack::handle_scheduling_failure(void) +{ +} + +void LoRaWANStack::process_reception(const uint8_t *const payload, uint16_t size, + int16_t rssi, int8_t snr) +{ +} + +void LoRaWANStack::process_reception_timeout(bool is_timeout) +{ +} + +void LoRaWANStack::make_tx_metadata_available(void) +{ +} + +void LoRaWANStack::make_rx_metadata_available(void) +{ +} + +bool LoRaWANStack::is_port_valid(const uint8_t port, bool allow_port_0) +{ + return true; +} + +lorawan_status_t LoRaWANStack::set_application_port(const uint8_t port, bool allow_port_0) +{ + return LORAWAN_STATUS_OK; +} + +void LoRaWANStack::state_machine_run_to_completion() +{ +} + +void LoRaWANStack::send_event_to_application(const lorawan_event_t event) const +{ +} + +void LoRaWANStack::send_automatic_uplink_message(const uint8_t port) +{ +} + +int LoRaWANStack::convert_to_msg_flag(const mcps_type_t type) +{ + return 0; +} + +lorawan_status_t LoRaWANStack::handle_connect(bool is_otaa) +{ + return LORAWAN_STATUS_OK; +} + +void LoRaWANStack::mlme_indication_handler() +{ +} + +void LoRaWANStack::mlme_confirm_handler() +{ +} + +void LoRaWANStack::mcps_confirm_handler() +{ +} + +void LoRaWANStack::mcps_indication_handler() +{ +} + +lorawan_status_t LoRaWANStack::state_controller(device_states_t new_state) +{ + return LORAWAN_STATUS_OK; +} + +void LoRaWANStack::process_shutdown_state(lorawan_status_t &op_status) +{ +} + +void LoRaWANStack::process_status_check_state() +{ +} + +void LoRaWANStack::process_scheduling_state(lorawan_status_t &op_status) +{ +} + +void LoRaWANStack::process_joining_state(lorawan_status_t &op_status) +{ +} + +void LoRaWANStack::process_connected_state() +{ +} + +void LoRaWANStack::process_connecting_state(lorawan_status_t &op_status) +{ +} + +void LoRaWANStack::process_idle_state(lorawan_status_t &op_status) +{ +} + +void LoRaWANStack::process_uninitialized_state(lorawan_status_t &op_status) +{ +} + diff --git a/UNITTESTS/stubs/LoRaWANTimer_stub.cpp b/UNITTESTS/stubs/LoRaWANTimer_stub.cpp new file mode 100644 index 0000000000..a66381c45f --- /dev/null +++ b/UNITTESTS/stubs/LoRaWANTimer_stub.cpp @@ -0,0 +1,64 @@ +/* + * 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 "mbed_assert.h" +#include "LoRaWANTimer.h" + +#include "LoRaWANTimer_stub.h" + +lorawan_time_t LoRaWANTimer_stub::time_value = 0; +bool LoRaWANTimer_stub::call_cb_immediately = false; + +LoRaWANTimeHandler::LoRaWANTimeHandler() + : _queue(NULL) +{ +} + +LoRaWANTimeHandler::~LoRaWANTimeHandler() +{ +} + +void LoRaWANTimeHandler::activate_timer_subsystem(events::EventQueue *queue) +{ +} + +lorawan_time_t LoRaWANTimeHandler::get_current_time( void ) +{ + return LoRaWANTimer_stub::time_value; +} + +lorawan_time_t LoRaWANTimeHandler::get_elapsed_time(lorawan_time_t saved_time) +{ + return LoRaWANTimer_stub::time_value; +} + +void LoRaWANTimeHandler::init(timer_event_t &obj, mbed::Callback callback) +{ + if (callback && LoRaWANTimer_stub::call_cb_immediately) { + callback(); + } + obj.timer_id = 1; +} + +void LoRaWANTimeHandler::start(timer_event_t &obj, const uint32_t timeout) +{ +} + +void LoRaWANTimeHandler::stop(timer_event_t &obj) +{ + obj.timer_id = 0; +} diff --git a/UNITTESTS/stubs/LoRaWANTimer_stub.h b/UNITTESTS/stubs/LoRaWANTimer_stub.h new file mode 100644 index 0000000000..54a0f66633 --- /dev/null +++ b/UNITTESTS/stubs/LoRaWANTimer_stub.h @@ -0,0 +1,24 @@ +/* + * 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 "LoRaWANTimer.h" + +namespace LoRaWANTimer_stub +{ +extern lorawan_time_t time_value; +extern bool call_cb_immediately; +} diff --git a/UNITTESTS/stubs/MBRBlockDevice_stub.cpp b/UNITTESTS/stubs/MBRBlockDevice_stub.cpp index 1e12f59cee..9759f749c9 100644 --- a/UNITTESTS/stubs/MBRBlockDevice_stub.cpp +++ b/UNITTESTS/stubs/MBRBlockDevice_stub.cpp @@ -1,5 +1,6 @@ -/* mbed Microcontroller Library - * Copyright (c) 2017 ARM Limited +/* + * 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. diff --git a/UNITTESTS/stubs/NetworkInterface_stub.cpp b/UNITTESTS/stubs/NetworkInterface_stub.cpp index 0e8eaa15a9..cfe6bdaa79 100644 --- a/UNITTESTS/stubs/NetworkInterface_stub.cpp +++ b/UNITTESTS/stubs/NetworkInterface_stub.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2015, Arm Limited and affiliates. + * Copyright (c) 2017, Arm Limited and affiliates. * SPDX-License-Identifier: Apache-2.0 * * Licensed under the Apache License, Version 2.0 (the "License"); diff --git a/UNITTESTS/stubs/NetworkStack_stub.cpp b/UNITTESTS/stubs/NetworkStack_stub.cpp index 372812effb..2d3a451a3c 100644 --- a/UNITTESTS/stubs/NetworkStack_stub.cpp +++ b/UNITTESTS/stubs/NetworkStack_stub.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2015, Arm Limited and affiliates. + * Copyright (c) 2017, Arm Limited and affiliates. * SPDX-License-Identifier: Apache-2.0 * * Licensed under the Apache License, Version 2.0 (the "License"); diff --git a/UNITTESTS/stubs/ObservingBlockDevice_stub.cpp b/UNITTESTS/stubs/ObservingBlockDevice_stub.cpp index ffd9450a83..550bf722a3 100644 --- a/UNITTESTS/stubs/ObservingBlockDevice_stub.cpp +++ b/UNITTESTS/stubs/ObservingBlockDevice_stub.cpp @@ -1,23 +1,18 @@ -/* mbed Microcontroller Library - * Copyright (c) 2017-2017 ARM Limited +/* + * Copyright (c) 2017, Arm Limited and affiliates. + * SPDX-License-Identifier: Apache-2.0 * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: + * 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 * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. + * http://www.apache.org/licenses/LICENSE-2.0 * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. + * 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 "ObservingBlockDevice.h" diff --git a/UNITTESTS/stubs/ProfilingBlockDevice_stub.cpp b/UNITTESTS/stubs/ProfilingBlockDevice_stub.cpp index df41cb1da5..52a94fa5b1 100644 --- a/UNITTESTS/stubs/ProfilingBlockDevice_stub.cpp +++ b/UNITTESTS/stubs/ProfilingBlockDevice_stub.cpp @@ -1,5 +1,6 @@ -/* mbed Microcontroller Library - * Copyright (c) 2017 ARM Limited +/* + * 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. diff --git a/UNITTESTS/stubs/ReadOnlyBlockDevice_stub.cpp b/UNITTESTS/stubs/ReadOnlyBlockDevice_stub.cpp index 13e20d934e..17a42915bb 100644 --- a/UNITTESTS/stubs/ReadOnlyBlockDevice_stub.cpp +++ b/UNITTESTS/stubs/ReadOnlyBlockDevice_stub.cpp @@ -1,23 +1,18 @@ -/* mbed Microcontroller Library - * Copyright (c) 2017-2017 ARM Limited +/* + * Copyright (c) , Arm Limited and affiliates. + * SPDX-License-Identifier: Apache-2.0 * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: + * 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 * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. + * http://www.apache.org/licenses/LICENSE-2.0 * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. + * 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 "ReadOnlyBlockDevice.h" diff --git a/UNITTESTS/stubs/SlicingBlockDevice_stub.cpp b/UNITTESTS/stubs/SlicingBlockDevice_stub.cpp index 6b4fbabdcf..d0b75ee194 100644 --- a/UNITTESTS/stubs/SlicingBlockDevice_stub.cpp +++ b/UNITTESTS/stubs/SlicingBlockDevice_stub.cpp @@ -1,5 +1,6 @@ -/* mbed Microcontroller Library - * Copyright (c) 2017 ARM Limited +/* + * 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. @@ -19,7 +20,7 @@ SlicingBlockDevice::SlicingBlockDevice(BlockDevice *bd, bd_addr_t start, bd_addr_t stop) { - + } int SlicingBlockDevice::init() diff --git a/UNITTESTS/stubs/SocketAddress_stub.cpp b/UNITTESTS/stubs/SocketAddress_stub.cpp index 9ed53f03cf..874e5ce667 100644 --- a/UNITTESTS/stubs/SocketAddress_stub.cpp +++ b/UNITTESTS/stubs/SocketAddress_stub.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2015, Arm Limited and affiliates. + * Copyright (c) , Arm Limited and affiliates. * SPDX-License-Identifier: Apache-2.0 * * Licensed under the Apache License, Version 2.0 (the "License"); diff --git a/UNITTESTS/stubs/Timer_stub.h b/UNITTESTS/stubs/Timer_stub.h index 699fdd0c53..a97333fe63 100644 --- a/UNITTESTS/stubs/Timer_stub.h +++ b/UNITTESTS/stubs/Timer_stub.h @@ -14,6 +14,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ + #ifndef TIMER_STUB_H #define TIMER_STUB_H diff --git a/UNITTESTS/stubs/aes_stub.c b/UNITTESTS/stubs/aes_stub.c new file mode 100644 index 0000000000..fad4b276f1 --- /dev/null +++ b/UNITTESTS/stubs/aes_stub.c @@ -0,0 +1,232 @@ +/* + * 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. + */ + + +#if !defined(MBEDTLS_CONFIG_FILE) +#include "mbedtls/config.h" +#else +#include MBEDTLS_CONFIG_FILE +#endif + +#include + +#include "mbedtls/aes.h" + +#include "aes_stub.h" +aes_stub_def aes_stub; + + +void mbedtls_aes_init( mbedtls_aes_context *ctx ) +{ +} + +void mbedtls_aes_free( mbedtls_aes_context *ctx ) +{ +} + +#if defined(MBEDTLS_CIPHER_MODE_XTS) +void mbedtls_aes_xts_init( mbedtls_aes_xts_context *ctx ) +{ +} + +void mbedtls_aes_xts_free( mbedtls_aes_xts_context *ctx ) +{ +} +#endif + +#if !defined(MBEDTLS_AES_SETKEY_ENC_ALT) +int mbedtls_aes_setkey_enc( mbedtls_aes_context *ctx, const unsigned char *key, + unsigned int keybits ) +{ + if (aes_stub.int_zero_counter) { + aes_stub.int_zero_counter--; + return 0; + } + return aes_stub.int_value; +} +#endif + +#if !defined(MBEDTLS_AES_SETKEY_DEC_ALT) +int mbedtls_aes_setkey_dec( mbedtls_aes_context *ctx, const unsigned char *key, + unsigned int keybits ) +{ + if (aes_stub.int_zero_counter) { + aes_stub.int_zero_counter--; + return 0; + } + return aes_stub.int_value; +} +#endif + +#if defined(MBEDTLS_CIPHER_MODE_XTS) +int mbedtls_aes_xts_setkey_enc( mbedtls_aes_xts_context *ctx, + const unsigned char *key, + unsigned int keybits) +{ + if (aes_stub.int_zero_counter) { + aes_stub.int_zero_counter--; + return 0; + } + return aes_stub.int_value; +} + +int mbedtls_aes_xts_setkey_dec( mbedtls_aes_xts_context *ctx, + const unsigned char *key, + unsigned int keybits) +{ + if (aes_stub.int_zero_counter) { + aes_stub.int_zero_counter--; + return 0; + } + return aes_stub.int_value; +} +#endif + +int mbedtls_internal_aes_encrypt( mbedtls_aes_context *ctx, + const unsigned char input[16], + unsigned char output[16] ) +{ + if (aes_stub.int_zero_counter) { + aes_stub.int_zero_counter--; + return 0; + } + return aes_stub.int_value; +} + +void mbedtls_aes_encrypt( mbedtls_aes_context *ctx, + const unsigned char input[16], + unsigned char output[16] ) +{ +} + +int mbedtls_internal_aes_decrypt( mbedtls_aes_context *ctx, + const unsigned char input[16], + unsigned char output[16] ) +{ + if (aes_stub.int_zero_counter) { + aes_stub.int_zero_counter--; + return 0; + } + return aes_stub.int_value; +} + +void mbedtls_aes_decrypt( mbedtls_aes_context *ctx, + const unsigned char input[16], + unsigned char output[16] ) +{ +} + +int mbedtls_aes_crypt_ecb( mbedtls_aes_context *ctx, + int mode, + const unsigned char input[16], + unsigned char output[16] ) +{ + if (aes_stub.int_zero_counter) { + aes_stub.int_zero_counter--; + return 0; + } + return aes_stub.int_value; +} + +int mbedtls_aes_crypt_cbc( mbedtls_aes_context *ctx, + int mode, + size_t length, + unsigned char iv[16], + const unsigned char *input, + unsigned char *output ) +{ + if (aes_stub.int_zero_counter) { + aes_stub.int_zero_counter--; + return 0; + } + return aes_stub.int_value; +} + + +#if defined(MBEDTLS_CIPHER_MODE_XTS) +int mbedtls_aes_crypt_xts( mbedtls_aes_xts_context *ctx, + int mode, + size_t length, + const unsigned char data_unit[16], + const unsigned char *input, + unsigned char *output ) +{ + if (aes_stub.int_zero_counter) { + aes_stub.int_zero_counter--; + return 0; + } + return aes_stub.int_value; +} +#endif + +int mbedtls_aes_crypt_cfb128( mbedtls_aes_context *ctx, + int mode, + size_t length, + size_t *iv_off, + unsigned char iv[16], + const unsigned char *input, + unsigned char *output ) +{ + if (aes_stub.int_zero_counter) { + aes_stub.int_zero_counter--; + return 0; + } + return aes_stub.int_value; +} + +int mbedtls_aes_crypt_cfb8( mbedtls_aes_context *ctx, + int mode, + size_t length, + unsigned char iv[16], + const unsigned char *input, + unsigned char *output ) +{ + if (aes_stub.int_zero_counter) { + aes_stub.int_zero_counter--; + return 0; + } + return aes_stub.int_value; +} + +int mbedtls_aes_crypt_ofb( mbedtls_aes_context *ctx, + size_t length, + size_t *iv_off, + unsigned char iv[16], + const unsigned char *input, + unsigned char *output ) +{ + if (aes_stub.int_zero_counter) { + aes_stub.int_zero_counter--; + return 0; + } + return aes_stub.int_value; +} + +int mbedtls_aes_crypt_ctr( mbedtls_aes_context *ctx, + size_t length, + size_t *nc_off, + unsigned char nonce_counter[16], + unsigned char stream_block[16], + const unsigned char *input, + unsigned char *output ) +{ + if (aes_stub.int_zero_counter) { + aes_stub.int_zero_counter--; + return 0; + } + return aes_stub.int_value; +} diff --git a/UNITTESTS/stubs/aes_stub.h b/UNITTESTS/stubs/aes_stub.h new file mode 100644 index 0000000000..c361b0a031 --- /dev/null +++ b/UNITTESTS/stubs/aes_stub.h @@ -0,0 +1,26 @@ +/* + * 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 + +typedef struct { + int int_value; + uint8_t int_zero_counter; +} aes_stub_def; + +extern aes_stub_def aes_stub; diff --git a/UNITTESTS/stubs/cipher_stub.c b/UNITTESTS/stubs/cipher_stub.c new file mode 100644 index 0000000000..e55a685193 --- /dev/null +++ b/UNITTESTS/stubs/cipher_stub.c @@ -0,0 +1,213 @@ +/* + * 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. + */ + +#if !defined(MBEDTLS_CONFIG_FILE) +#include "mbedtls/config.h" +#else +#include MBEDTLS_CONFIG_FILE +#endif + +#include "mbedtls/cipher.h" +#include "mbedtls/cipher_internal.h" +#include "mbedtls/platform_util.h" + +#include +#include + +#if defined(MBEDTLS_GCM_C) +#include "mbedtls/gcm.h" +#endif + +#if defined(MBEDTLS_CCM_C) +#include "mbedtls/ccm.h" +#endif + +#if defined(MBEDTLS_CMAC_C) +#include "mbedtls/cmac.h" +#endif + +#include "cipher_stub.h" + +cipher_stub_def cipher_stub; + +const int *mbedtls_cipher_list( void ) +{ + return cipher_stub.int_ptr; +} + +const mbedtls_cipher_info_t *mbedtls_cipher_info_from_type( const mbedtls_cipher_type_t cipher_type ) +{ + return cipher_stub.info_value; +} + +const mbedtls_cipher_info_t *mbedtls_cipher_info_from_string( const char *cipher_name ) +{ + return cipher_stub.info_value; +} + +const mbedtls_cipher_info_t *mbedtls_cipher_info_from_values( const mbedtls_cipher_id_t cipher_id, + int key_bitlen, + const mbedtls_cipher_mode_t mode ) +{ + return cipher_stub.info_value; +} + +void mbedtls_cipher_init( mbedtls_cipher_context_t *ctx ) +{ +} + +void mbedtls_cipher_free( mbedtls_cipher_context_t *ctx ) +{ +} + +int mbedtls_cipher_setup( mbedtls_cipher_context_t *ctx, const mbedtls_cipher_info_t *cipher_info ) +{ + if (cipher_stub.int_zero_counter) { + cipher_stub.int_zero_counter--; + return 0; + } + return cipher_stub.int_value; +} + +int mbedtls_cipher_setkey( mbedtls_cipher_context_t *ctx, const unsigned char *key, + int key_bitlen, const mbedtls_operation_t operation ) +{ + if (cipher_stub.int_zero_counter) { + cipher_stub.int_zero_counter--; + return 0; + } + return cipher_stub.int_value; +} + +int mbedtls_cipher_set_iv( mbedtls_cipher_context_t *ctx, + const unsigned char *iv, size_t iv_len ) +{ + if (cipher_stub.int_zero_counter) { + cipher_stub.int_zero_counter--; + return 0; + } + return cipher_stub.int_value; +} + +int mbedtls_cipher_reset( mbedtls_cipher_context_t *ctx ) +{ + if (cipher_stub.int_zero_counter) { + cipher_stub.int_zero_counter--; + return 0; + } + return cipher_stub.int_value; +} + +int mbedtls_cipher_update_ad( mbedtls_cipher_context_t *ctx, + const unsigned char *ad, size_t ad_len ) +{ + if (cipher_stub.int_zero_counter) { + cipher_stub.int_zero_counter--; + return 0; + } + return cipher_stub.int_value; +} + +int mbedtls_cipher_update( mbedtls_cipher_context_t *ctx, const unsigned char *input, + size_t ilen, unsigned char *output, size_t *olen ) +{ + if (cipher_stub.int_zero_counter) { + cipher_stub.int_zero_counter--; + return 0; + } + return cipher_stub.int_value; +} + +int mbedtls_cipher_finish( mbedtls_cipher_context_t *ctx, + unsigned char *output, size_t *olen ) +{ + if (cipher_stub.int_zero_counter) { + cipher_stub.int_zero_counter--; + return 0; + } + return cipher_stub.int_value; +} + +int mbedtls_cipher_set_padding_mode( mbedtls_cipher_context_t *ctx, mbedtls_cipher_padding_t mode ) +{ + if (cipher_stub.int_zero_counter) { + cipher_stub.int_zero_counter--; + return 0; + } + return cipher_stub.int_value; +} + +int mbedtls_cipher_write_tag( mbedtls_cipher_context_t *ctx, + unsigned char *tag, size_t tag_len ) +{ + if (cipher_stub.int_zero_counter) { + cipher_stub.int_zero_counter--; + return 0; + } + return cipher_stub.int_value; +} + +int mbedtls_cipher_check_tag( mbedtls_cipher_context_t *ctx, + const unsigned char *tag, size_t tag_len ) +{ + if (cipher_stub.int_zero_counter) { + cipher_stub.int_zero_counter--; + return 0; + } + return cipher_stub.int_value; +} + +int mbedtls_cipher_crypt( mbedtls_cipher_context_t *ctx, + const unsigned char *iv, size_t iv_len, + const unsigned char *input, size_t ilen, + unsigned char *output, size_t *olen ) +{ + if (cipher_stub.int_zero_counter) { + cipher_stub.int_zero_counter--; + return 0; + } + return cipher_stub.int_value; +} + +int mbedtls_cipher_auth_encrypt( mbedtls_cipher_context_t *ctx, + const unsigned char *iv, size_t iv_len, + const unsigned char *ad, size_t ad_len, + const unsigned char *input, size_t ilen, + unsigned char *output, size_t *olen, + unsigned char *tag, size_t tag_len ) +{ + if (cipher_stub.int_zero_counter) { + cipher_stub.int_zero_counter--; + return 0; + } + return cipher_stub.int_value; +} + +int mbedtls_cipher_auth_decrypt( mbedtls_cipher_context_t *ctx, + const unsigned char *iv, size_t iv_len, + const unsigned char *ad, size_t ad_len, + const unsigned char *input, size_t ilen, + unsigned char *output, size_t *olen, + const unsigned char *tag, size_t tag_len ) +{ + if (cipher_stub.int_zero_counter) { + cipher_stub.int_zero_counter--; + return 0; + } + return cipher_stub.int_value; +} + diff --git a/UNITTESTS/stubs/cipher_stub.h b/UNITTESTS/stubs/cipher_stub.h new file mode 100644 index 0000000000..9a16f04666 --- /dev/null +++ b/UNITTESTS/stubs/cipher_stub.h @@ -0,0 +1,28 @@ +/* + * 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 +#include "mbedtls/cipher.h" + +typedef struct { + mbedtls_cipher_info_t *info_value; + int int_value; + int *int_ptr; + uint8_t int_zero_counter; +} cipher_stub_def; + +extern cipher_stub_def cipher_stub; diff --git a/UNITTESTS/stubs/cmac_stub.c b/UNITTESTS/stubs/cmac_stub.c new file mode 100644 index 0000000000..382579fe1a --- /dev/null +++ b/UNITTESTS/stubs/cmac_stub.c @@ -0,0 +1,94 @@ +/* + * 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. + */ + +#if !defined(MBEDTLS_CONFIG_FILE) +#include "mbedtls/config.h" +#else +#include MBEDTLS_CONFIG_FILE +#endif + + +#include "mbedtls/cmac.h" +#include "mbedtls/platform_util.h" +#include + +#include "cmac_stub.h" + +cmac_stub_def cmac_stub; + +int mbedtls_cipher_cmac_starts( mbedtls_cipher_context_t *ctx, + const unsigned char *key, size_t keybits ) +{ + if (cmac_stub.int_zero_counter) { + cmac_stub.int_zero_counter--; + return 0; + } + return cmac_stub.int_value; +} + +int mbedtls_cipher_cmac_update( mbedtls_cipher_context_t *ctx, + const unsigned char *input, size_t ilen ) +{ + if (cmac_stub.int_zero_counter) { + cmac_stub.int_zero_counter--; + return 0; + } + return cmac_stub.int_value; +} + +int mbedtls_cipher_cmac_finish( mbedtls_cipher_context_t *ctx, + unsigned char *output ) +{ + if (cmac_stub.int_zero_counter) { + cmac_stub.int_zero_counter--; + return 0; + } + return cmac_stub.int_value; +} + +int mbedtls_cipher_cmac_reset( mbedtls_cipher_context_t *ctx ) +{ + if (cmac_stub.int_zero_counter) { + cmac_stub.int_zero_counter--; + return 0; + } + return cmac_stub.int_value; +} + +int mbedtls_cipher_cmac( const mbedtls_cipher_info_t *cipher_info, + const unsigned char *key, size_t keylen, + const unsigned char *input, size_t ilen, + unsigned char *output ) +{ + if (cmac_stub.int_zero_counter) { + cmac_stub.int_zero_counter--; + return 0; + } + return cmac_stub.int_value; +} + +int mbedtls_aes_cmac_prf_128( const unsigned char *key, size_t key_length, + const unsigned char *input, size_t in_len, + unsigned char *output ) +{ + if (cmac_stub.int_zero_counter) { + cmac_stub.int_zero_counter--; + return 0; + } + return cmac_stub.int_value; +} + diff --git a/UNITTESTS/stubs/cmac_stub.h b/UNITTESTS/stubs/cmac_stub.h new file mode 100644 index 0000000000..4134f9d6cf --- /dev/null +++ b/UNITTESTS/stubs/cmac_stub.h @@ -0,0 +1,26 @@ +/* + * 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 + +typedef struct { + int int_value; + uint8_t int_zero_counter; +} cmac_stub_def; + +extern cmac_stub_def cmac_stub; diff --git a/UNITTESTS/stubs/equeue_stub.c b/UNITTESTS/stubs/equeue_stub.c index cbda94c36b..4fea449ab1 100644 --- a/UNITTESTS/stubs/equeue_stub.c +++ b/UNITTESTS/stubs/equeue_stub.c @@ -18,6 +18,10 @@ #include "equeue.h" #include +#include "equeue_stub.h" + +equeue_stub_def equeue_stub; + int equeue_create(equeue_t *queue, size_t size) { return 0; @@ -55,7 +59,7 @@ int equeue_call_every(equeue_t *queue, int ms, void (*cb)(void *), void *data) void *equeue_alloc(equeue_t *queue, size_t size) { - return malloc(size); + return equeue_stub.void_ptr; } void equeue_dealloc(equeue_t *queue, void *event) @@ -80,11 +84,10 @@ void equeue_event_dtor(void *event, void (*dtor)(void *)) int equeue_post(equeue_t *queue, void (*cb)(void *), void *event) { - if (cb) - { - cb(event); - free(event); - return 1; //Fake ID for calling cancel + struct equeue_event *e = (struct equeue_event*)event - 1; + if (cb && equeue_stub.call_cb_immediately) { + cb(e + 1); + return 1; } return 0; } diff --git a/UNITTESTS/stubs/equeue_stub.h b/UNITTESTS/stubs/equeue_stub.h new file mode 100644 index 0000000000..648d920058 --- /dev/null +++ b/UNITTESTS/stubs/equeue_stub.h @@ -0,0 +1,26 @@ +/* + * 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 "stdint.h" +#include "stdbool.h" + +typedef struct { + void *void_ptr; + bool call_cb_immediately; +} equeue_stub_def; + +extern equeue_stub_def equeue_stub; diff --git a/UNITTESTS/stubs/ip4tos_stub.c b/UNITTESTS/stubs/ip4tos_stub.c index 35957fe232..c45c1383bb 100644 --- a/UNITTESTS/stubs/ip4tos_stub.c +++ b/UNITTESTS/stubs/ip4tos_stub.c @@ -1,18 +1,20 @@ /* - * Copyright (c) 2014-2018 ARM Limited. All rights reserved. + * 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. + * + * 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 + * 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. + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ + #include #include #include "common_functions.h" diff --git a/UNITTESTS/stubs/mbed_assert_stub.c b/UNITTESTS/stubs/mbed_assert_stub.c index 9029a6192d..afe6fb3ff3 100644 --- a/UNITTESTS/stubs/mbed_assert_stub.c +++ b/UNITTESTS/stubs/mbed_assert_stub.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2017, Arm Limited and affiliates. + * Copyright (c) , Arm Limited and affiliates. * SPDX-License-Identifier: Apache-2.0 * * Licensed under the Apache License, Version 2.0 (the "License"); diff --git a/UNITTESTS/stubs/mbed_critical_stub.c b/UNITTESTS/stubs/mbed_critical_stub.c index f812764382..ca08e252d4 100644 --- a/UNITTESTS/stubs/mbed_critical_stub.c +++ b/UNITTESTS/stubs/mbed_critical_stub.c @@ -1,16 +1,16 @@ /* - * Copyright (c) 2015-2016, ARM Limited, All Rights Reserved + * 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. + * 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 + * 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. + * 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. */ diff --git a/UNITTESTS/stubs/mbed_poll_stub.cpp b/UNITTESTS/stubs/mbed_poll_stub.cpp index 1914399d2a..4db534ed10 100644 --- a/UNITTESTS/stubs/mbed_poll_stub.cpp +++ b/UNITTESTS/stubs/mbed_poll_stub.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) , Arm Limited and affiliates. + * Copyright (c) 2017, Arm Limited and affiliates. * SPDX-License-Identifier: Apache-2.0 * * Licensed under the Apache License, Version 2.0 (the "License"); diff --git a/UNITTESTS/stubs/mbed_poll_stub.h b/UNITTESTS/stubs/mbed_poll_stub.h index 6c0a4327a9..930766e840 100644 --- a/UNITTESTS/stubs/mbed_poll_stub.h +++ b/UNITTESTS/stubs/mbed_poll_stub.h @@ -1,5 +1,5 @@ /* - * Copyright (c) , Arm Limited and affiliates. + * Copyright (c) 2017, Arm Limited and affiliates. * SPDX-License-Identifier: Apache-2.0 * * Licensed under the Apache License, Version 2.0 (the "License"); @@ -14,6 +14,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ + #ifndef __MBED_POLL_STUB_H__ #define __MBED_POLL_STUB_H__ diff --git a/UNITTESTS/stubs/mbed_wait_api_stub.cpp b/UNITTESTS/stubs/mbed_wait_api_stub.cpp index f8b240544d..68a265308c 100644 --- a/UNITTESTS/stubs/mbed_wait_api_stub.cpp +++ b/UNITTESTS/stubs/mbed_wait_api_stub.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) , Arm Limited and affiliates. + * Copyright (c) 2017, Arm Limited and affiliates. * SPDX-License-Identifier: Apache-2.0 * * Licensed under the Apache License, Version 2.0 (the "License"); diff --git a/UNITTESTS/stubs/nvic_wrapper_stub.c b/UNITTESTS/stubs/nvic_wrapper_stub.c index 636f05bae5..9cd88195c0 100644 --- a/UNITTESTS/stubs/nvic_wrapper_stub.c +++ b/UNITTESTS/stubs/nvic_wrapper_stub.c @@ -1,3 +1,20 @@ +/* + * 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 "nvic_wrapper.h" void NVIC_SystemReset(void) {} diff --git a/UNITTESTS/stubs/randLIB_stub.c b/UNITTESTS/stubs/randLIB_stub.c index 97154cf2ee..e9fd486adb 100644 --- a/UNITTESTS/stubs/randLIB_stub.c +++ b/UNITTESTS/stubs/randLIB_stub.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2014-2017, Arm Limited and affiliates. + * Copyright (c) 2017, Arm Limited and affiliates. * SPDX-License-Identifier: Apache-2.0 * * Licensed under the Apache License, Version 2.0 (the "License"); @@ -14,6 +14,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ + #include #include #include diff --git a/UNITTESTS/stubs/randLIB_stub.cpp b/UNITTESTS/stubs/randLIB_stub.cpp index 61cd893d0e..28989c444e 100644 --- a/UNITTESTS/stubs/randLIB_stub.cpp +++ b/UNITTESTS/stubs/randLIB_stub.cpp @@ -14,6 +14,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ + #include "randLIB.h" void randLIB_seed_random(void) diff --git a/UNITTESTS/stubs/stoip4_stub.c b/UNITTESTS/stubs/stoip4_stub.c index d22c20e23e..9cb8036577 100644 --- a/UNITTESTS/stubs/stoip4_stub.c +++ b/UNITTESTS/stubs/stoip4_stub.c @@ -1,18 +1,20 @@ /* - * Copyright (c) 2014-2018 ARM Limited. All rights reserved. + * 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. + * + * 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 + * 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. + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ + #include #include #include diff --git a/features/lorawan/LoRaWANStack.cpp b/features/lorawan/LoRaWANStack.cpp index a73512ebbc..1b28e7c9d7 100644 --- a/features/lorawan/LoRaWANStack.cpp +++ b/features/lorawan/LoRaWANStack.cpp @@ -380,35 +380,31 @@ int16_t LoRaWANStack::handle_rx(uint8_t *data, uint16_t length, uint8_t &port, i uint16_t base_size = _rx_msg.msg.mcps_indication.buffer_size; bool read_complete = false; + if (_rx_msg.pending_size == 0) { + _rx_msg.pending_size = _rx_msg.msg.mcps_indication.buffer_size; + _rx_msg.prev_read_size = 0; + } + // check the length of received message whether we can fit into user // buffer completely or not - if (_rx_msg.msg.mcps_indication.buffer_size > length - && _rx_msg.prev_read_size == 0) { - // we can't fit into user buffer. Invoke counter measures - _rx_msg.pending_size = _rx_msg.msg.mcps_indication.buffer_size - length; + if (_rx_msg.prev_read_size == 0 && _rx_msg.msg.mcps_indication.buffer_size <= length) { + memcpy(data, base_ptr, base_size); + read_complete = true; + } else if (_rx_msg.pending_size > length) { + _rx_msg.pending_size = _rx_msg.pending_size - length; base_size = length; - _rx_msg.prev_read_size = base_size; - memcpy(data, base_ptr, base_size); - } else if (_rx_msg.prev_read_size == 0) { - _rx_msg.pending_size = 0; - _rx_msg.prev_read_size = 0; - memcpy(data, base_ptr, base_size); + memcpy(data, base_ptr + _rx_msg.prev_read_size, base_size); + _rx_msg.prev_read_size += base_size; + } else { + base_size = _rx_msg.pending_size; + memcpy(data, base_ptr + _rx_msg.prev_read_size, base_size); read_complete = true; } - // If its the pending read then we should copy only the remaining part of - // the buffer. Due to checks above, in case of a pending read, this block - // will be the only one to get invoked - if (_rx_msg.pending_size > 0 && _rx_msg.prev_read_size > 0) { - memcpy(data, base_ptr + _rx_msg.prev_read_size, base_size); - } - - // we are done handing over received buffer to user. check if there is - // anything pending. If not, memset the buffer to zero and indicate - // that no read is in progress if (read_complete) { _rx_msg.msg.mcps_indication.buffer = NULL; _rx_msg.msg.mcps_indication.buffer_size = 0; + _rx_msg.pending_size = 0; _rx_msg.receive_ready = false; } @@ -1067,9 +1063,9 @@ lorawan_status_t LoRaWANStack::state_controller(device_states_t new_state) process_shutdown_state(status); break; default: - tr_debug("state_controller: Unknown state!"); - status = LORAWAN_STATUS_SERVICE_UNKNOWN; - break; + //Because this is internal function only coding error causes this + tr_error("Unknown state: %d:", new_state); + MBED_ASSERT(false); } return status; diff --git a/features/lorawan/lorastack/mac/LoRaMac.cpp b/features/lorawan/lorastack/mac/LoRaMac.cpp index b6a9ff238f..43dbec93e8 100644 --- a/features/lorawan/lorastack/mac/LoRaMac.cpp +++ b/features/lorawan/lorastack/mac/LoRaMac.cpp @@ -1721,21 +1721,6 @@ void LoRaMac::reset_mcps_indication() _mcps_indication.status = LORAMAC_EVENT_INFO_STATUS_ERROR; } -void LoRaMac::set_tx_continuous_wave(uint8_t channel, int8_t datarate, int8_t tx_power, - float max_eirp, float antenna_gain, uint16_t timeout) -{ - cw_mode_params_t continuous_wave; - - continuous_wave.channel = channel; - continuous_wave.datarate = datarate; - continuous_wave.tx_power = tx_power; - continuous_wave.max_eirp = max_eirp; - continuous_wave.antenna_gain = antenna_gain; - continuous_wave.timeout = timeout; - - _lora_phy->set_tx_cont_mode(&continuous_wave); -} - lorawan_status_t LoRaMac::initialize(EventQueue *queue, mbed::Callbackscheduling_failure_handler) { diff --git a/features/lorawan/lorastack/mac/LoRaMacCommand.cpp b/features/lorawan/lorastack/mac/LoRaMacCommand.cpp index 596d0b8720..87afc3409f 100644 --- a/features/lorawan/lorastack/mac/LoRaMacCommand.cpp +++ b/features/lorawan/lorastack/mac/LoRaMacCommand.cpp @@ -33,7 +33,6 @@ SPDX-License-Identifier: BSD-3-Clause */ static const uint8_t max_eirp_table[] = { 8, 10, 12, 13, 14, 16, 18, 20, 21, 24, 26, 27, 29, 30, 33, 36 }; - LoRaMacCommand::LoRaMacCommand() { sticky_mac_cmd = false; @@ -93,8 +92,9 @@ void LoRaMacCommand::parse_mac_commands_to_repeat() case MOTE_MAC_LINK_CHECK_REQ: { // 0 byte payload break; } - default: - break; + default: { + MBED_ASSERT(false); + } } } @@ -190,8 +190,7 @@ lorawan_status_t LoRaMacCommand::process_mac_commands(const uint8_t *payload, ui rx_param_setup_req_t rxParamSetupReq; rxParamSetupReq.dr_offset = (payload[mac_index] >> 4) & 0x07; - rxParamSetupReq.datarate = payload[mac_index] & 0x0F; - mac_index++; + rxParamSetupReq.datarate = payload[mac_index++] & 0x0F; rxParamSetupReq.frequency = (uint32_t) payload[mac_index++]; rxParamSetupReq.frequency |= (uint32_t) payload[mac_index++] << 8; @@ -405,7 +404,7 @@ lorawan_status_t LoRaMacCommand::add_tx_param_setup_ans() lorawan_status_t LoRaMacCommand::add_dl_channel_ans(uint8_t status) { lorawan_status_t ret = LORAWAN_STATUS_LENGTH_ERROR; - if (cmd_buffer_remaining() > 0) { + if (cmd_buffer_remaining() > 1) { mac_cmd_buffer[mac_cmd_buf_idx++] = MOTE_MAC_DL_CHANNEL_ANS; // Status: Uplink frequency exists, Channel frequency OK mac_cmd_buffer[mac_cmd_buf_idx++] = status; diff --git a/features/lorawan/lorastack/mac/LoRaMacCommand.h b/features/lorawan/lorastack/mac/LoRaMacCommand.h index aec31566c4..7b7d730950 100644 --- a/features/lorawan/lorastack/mac/LoRaMacCommand.h +++ b/features/lorawan/lorastack/mac/LoRaMacCommand.h @@ -76,7 +76,7 @@ public: uint8_t *get_mac_commands_buffer(); /** - * @brief Parses the MAC commands which must be resent. + * @brief Parses the MAC commands which must be re-sent. */ void parse_mac_commands_to_repeat(); diff --git a/features/lorawan/lorastack/mac/LoRaMacCrypto.cpp b/features/lorawan/lorastack/mac/LoRaMacCrypto.cpp index f2afc8dcf5..10099fcae3 100644 --- a/features/lorawan/lorastack/mac/LoRaMacCrypto.cpp +++ b/features/lorawan/lorastack/mac/LoRaMacCrypto.cpp @@ -33,13 +33,7 @@ #if defined(MBEDTLS_CMAC_C) && defined(MBEDTLS_AES_C) && defined(MBEDTLS_CIPHER_C) LoRaMacCrypto::LoRaMacCrypto() - : mic_block_b0(), - computed_mic(), - a_block(), - s_block() { - mic_block_b0[0] = 0x49; - a_block[0] = 0x01; } int LoRaMacCrypto::compute_mic(const uint8_t *buffer, uint16_t size, @@ -47,8 +41,12 @@ int LoRaMacCrypto::compute_mic(const uint8_t *buffer, uint16_t size, uint32_t address, uint8_t dir, uint32_t seq_counter, uint32_t *mic) { + uint8_t computed_mic[16] = {}; + uint8_t mic_block_b0[16] = {}; int ret = 0; + mic_block_b0[0] = 0x49; + mic_block_b0[5] = dir; mic_block_b0[6] = (address) & 0xFF; @@ -95,7 +93,8 @@ int LoRaMacCrypto::compute_mic(const uint8_t *buffer, uint16_t size, ret = MBEDTLS_ERR_CIPHER_ALLOC_FAILED; } -exit: mbedtls_cipher_free(aes_cmac_ctx); +exit: + mbedtls_cipher_free(aes_cmac_ctx); return ret; } @@ -108,12 +107,15 @@ int LoRaMacCrypto::encrypt_payload(const uint8_t *buffer, uint16_t size, uint8_t bufferIndex = 0; uint16_t ctr = 1; int ret = 0; + uint8_t a_block[16] = {}; + uint8_t s_block[16] = {}; mbedtls_aes_init(&aes_ctx); ret = mbedtls_aes_setkey_enc(&aes_ctx, key, key_length); if (0 != ret) goto exit; + a_block[0] = 0x01; a_block[5] = dir; a_block[6] = (address) & 0xFF; @@ -153,7 +155,8 @@ int LoRaMacCrypto::encrypt_payload(const uint8_t *buffer, uint16_t size, } } -exit: mbedtls_aes_free(&aes_ctx); +exit: + mbedtls_aes_free(&aes_ctx); return ret; } @@ -170,6 +173,7 @@ int LoRaMacCrypto::compute_join_frame_mic(const uint8_t *buffer, uint16_t size, const uint8_t *key, uint32_t key_length, uint32_t *mic) { + uint8_t computed_mic[16] = {}; int ret = 0; mbedtls_cipher_init(aes_cmac_ctx); @@ -199,7 +203,8 @@ int LoRaMacCrypto::compute_join_frame_mic(const uint8_t *buffer, uint16_t size, ret = MBEDTLS_ERR_CIPHER_ALLOC_FAILED; } -exit: mbedtls_cipher_free(aes_cmac_ctx); +exit: + mbedtls_cipher_free(aes_cmac_ctx); return ret; } @@ -226,7 +231,8 @@ int LoRaMacCrypto::decrypt_join_frame(const uint8_t *buffer, uint16_t size, dec_buffer + 16); } -exit: mbedtls_aes_free(&aes_ctx); +exit: + mbedtls_aes_free(&aes_ctx); return ret; } @@ -258,7 +264,8 @@ int LoRaMacCrypto::compute_skeys_for_join_frame(const uint8_t *key, uint32_t key memcpy(nonce + 7, p_dev_nonce, 2); ret = mbedtls_aes_crypt_ecb(&aes_ctx, MBEDTLS_AES_ENCRYPT, nonce, app_skey); - exit: mbedtls_aes_free(&aes_ctx); +exit: + mbedtls_aes_free(&aes_ctx); return ret; } #else diff --git a/features/lorawan/lorastack/mac/LoRaMacCrypto.h b/features/lorawan/lorastack/mac/LoRaMacCrypto.h index b74729280e..09fa1c19f0 100644 --- a/features/lorawan/lorastack/mac/LoRaMacCrypto.h +++ b/features/lorawan/lorastack/mac/LoRaMacCrypto.h @@ -147,24 +147,6 @@ public: uint8_t *nwk_skey, uint8_t *app_skey); private: - /** - * MIC field computation initial data - */ - uint8_t mic_block_b0[16]; - - /** - * Contains the computed MIC field. - * - * \remark Only the 4 first bytes are used - */ - uint8_t computed_mic[16]; - - /** - * Encryption aBlock and sBlock - */ - uint8_t a_block[16]; - uint8_t s_block[16]; - /** * AES computation context variable */ diff --git a/features/lorawan/lorastack/phy/LoRaPHY.cpp b/features/lorawan/lorastack/phy/LoRaPHY.cpp index 7a8462fa8e..6a6b5869b1 100644 --- a/features/lorawan/lorastack/phy/LoRaPHY.cpp +++ b/features/lorawan/lorastack/phy/LoRaPHY.cpp @@ -164,19 +164,18 @@ int32_t LoRaPHY::get_random(int32_t min, int32_t max) return (int32_t) rand() % (max - min + 1) + min; } -bool LoRaPHY::verify_channel_DR(uint8_t nb_channels, uint16_t *channel_mask, - int8_t dr, int8_t min_dr, int8_t max_dr, - channel_params_t *channels) +bool LoRaPHY::verify_channel_DR(uint16_t *channel_mask, int8_t dr) { - if (val_in_range(dr, min_dr, max_dr) == 0) { + if (val_in_range(dr, phy_params.min_tx_datarate, + phy_params.max_tx_datarate) == 0) { return false; } for (uint8_t i = 0; i < phy_params.max_channel_cnt; i++) { if (mask_bit_test(channel_mask, i)) { // Check datarate validity for enabled channels - if (val_in_range(dr, (channels[i].dr_range.fields.min & 0x0F), - (channels[i].dr_range.fields.max & 0x0F))) { + if (val_in_range(dr, (phy_params.channels.channel_list[i].dr_range.fields.min & 0x0F), + (phy_params.channels.channel_list[i].dr_range.fields.max & 0x0F))) { // At least 1 channel has been found we can return OK. return true; } @@ -248,45 +247,6 @@ void LoRaPHY::copy_channel_mask(uint16_t *dest_mask, uint16_t *src_mask, uint8_t } } -void LoRaPHY::intersect_channel_mask(const uint16_t *source, - uint16_t *destination, uint8_t size) -{ - if (!source || !destination || size == 0) { - return; - } - - for (uint8_t i = 0; i < size; i++) { - destination[i] &= source[i]; - } -} - -void LoRaPHY::fill_channel_mask_with_fsb(const uint16_t *expectation, - const uint16_t *fsb_mask, - uint16_t *destination, - uint8_t size) -{ - if (!expectation || !fsb_mask || !destination || size == 0) { - return; - } - - for (uint8_t i = 0; i < size; i++) { - destination[i] = expectation[i] & fsb_mask[i]; - } - -} - -void LoRaPHY::fill_channel_mask_with_value(uint16_t *channel_mask, - uint16_t value, uint8_t size) -{ - if (!channel_mask || size == 0) { - return; - } - - for (uint8_t i = 0; i < size; i++) { - channel_mask[i] = value; - } -} - void LoRaPHY::set_last_tx_done(uint8_t channel, bool joined, lorawan_time_t last_tx_done_time) { band_t *band_table = (band_t *) phy_params.bands.table; @@ -396,16 +356,13 @@ uint8_t LoRaPHY::verify_link_ADR_req(verify_adr_params_t *verify_params, if (status != 0) { // Verify channel datarate - if (verify_channel_DR(phy_params.max_channel_cnt, verify_params->channel_mask, - datarate, phy_params.min_tx_datarate, - phy_params.max_tx_datarate, phy_params.channels.channel_list) - == false) { + if (verify_channel_DR(verify_params->channel_mask, datarate) == false) { status &= 0xFD; // Datarate KO } // Verify tx power if (val_in_range(tx_power, phy_params.max_tx_power, - phy_params.min_tx_power) == 0) { + phy_params.min_tx_power) == false) { // Verify if the maximum TX power is exceeded if (phy_params.max_tx_power > tx_power) { // Apply maximum TX power. Accept TX power. @@ -1140,8 +1097,8 @@ uint8_t LoRaPHY::dl_channel_request(uint8_t channel_id, uint32_t rx1_frequency) uint8_t status = 0x03; // Verify if the frequency is supported - uint8_t band = lookup_band_for_frequency(rx1_frequency); - if (verify_frequency_for_band(rx1_frequency, band) == false) { + int band = lookup_band_for_frequency(rx1_frequency); + if (band < 0) { status &= 0xFE; } @@ -1343,7 +1300,7 @@ lorawan_status_t LoRaPHY::add_channel(const channel_params_t *new_channel, // Default channels don't accept all values if (id < phy_params.default_channel_cnt) { // Validate the datarate range for min: must be DR_0 - if (new_channel->dr_range.fields.min > phy_params.min_tx_datarate) { + if (new_channel->dr_range.fields.min != DR_0) { dr_invalid = true; } diff --git a/features/lorawan/lorastack/phy/LoRaPHY.h b/features/lorawan/lorastack/phy/LoRaPHY.h index 5b4ed32b8b..42c45b9575 100644 --- a/features/lorawan/lorastack/phy/LoRaPHY.h +++ b/features/lorawan/lorastack/phy/LoRaPHY.h @@ -535,26 +535,6 @@ public: //Verifiers protected: LoRaPHY(); - /** - * Sets the intersection of source and destination channel masks - * into the destination. - */ - void intersect_channel_mask(const uint16_t *source, uint16_t *destination, - uint8_t size); - - /** - * Fills channel mask array based upon the provided FSB mask - */ - void fill_channel_mask_with_fsb(const uint16_t *expectation, - const uint16_t *fsb_mask, - uint16_t *channel_mask, uint8_t size); - - /** - * Fills channel mask array with a given value - */ - void fill_channel_mask_with_value(uint16_t *channel_mask, - uint16_t value, uint8_t size); - /** * Looks up corresponding band for a frequency. Returns -1 if not in any band. */ @@ -573,8 +553,7 @@ protected: /** * Verifies, if a datarate is available on an active channel. */ - bool verify_channel_DR(uint8_t nbChannels, uint16_t* channelsMask, int8_t dr, - int8_t minDr, int8_t maxDr, channel_params_t* channels); + bool verify_channel_DR(uint16_t* channelsMask, int8_t dr); /** * Disables a channel in a given channels mask. @@ -615,16 +594,6 @@ protected: uint8_t verify_link_ADR_req(verify_adr_params_t* verify_params, int8_t* dr, int8_t* tx_pow, uint8_t* nb_rep); - /** - * Computes the symbol time for LoRa modulation. - */ - double compute_symb_timeout_lora(uint8_t phy_dr, uint32_t bandwidth ); - - /** - * Computes the symbol time for FSK modulation. - */ - double compute_symb_timeout_fsk(uint8_t phy_dr); - /** * Computes the RX window timeout and the RX window offset. */ @@ -658,6 +627,18 @@ protected: bool is_datarate_supported(const int8_t datarate) const; +private: + + /** + * Computes the symbol time for LoRa modulation. + */ + double compute_symb_timeout_lora(uint8_t phy_dr, uint32_t bandwidth ); + + /** + * Computes the symbol time for FSK modulation. + */ + double compute_symb_timeout_fsk(uint8_t phy_dr); + protected: LoRaRadio *_radio; LoRaWANTimeHandler *_lora_time; diff --git a/features/lorawan/lorastack/phy/LoRaPHYAU915.cpp b/features/lorawan/lorastack/phy/LoRaPHYAU915.cpp index 357d382844..97d80016f4 100644 --- a/features/lorawan/lorastack/phy/LoRaPHYAU915.cpp +++ b/features/lorawan/lorastack/phy/LoRaPHYAU915.cpp @@ -615,10 +615,32 @@ lorawan_status_t LoRaPHYAU915::set_next_channel(channel_selection_params_t* next uint8_t LoRaPHYAU915::apply_DR_offset(int8_t dr, int8_t dr_offset) { - int8_t datarate = datarate_offsets_AU915[dr][dr_offset]; - - if (datarate < 0) { - datarate = DR_0; - } - return datarate; + return datarate_offsets_AU915[dr][dr_offset]; +} + +void LoRaPHYAU915::intersect_channel_mask(const uint16_t *source, + uint16_t *destination, uint8_t size) +{ + for (uint8_t i = 0; i < size; i++) { + destination[i] &= source[i]; + } +} + +void LoRaPHYAU915::fill_channel_mask_with_fsb(const uint16_t *expectation, + const uint16_t *fsb_mask, + uint16_t *destination, + uint8_t size) +{ + for (uint8_t i = 0; i < size; i++) { + destination[i] = expectation[i] & fsb_mask[i]; + } + +} + +void LoRaPHYAU915::fill_channel_mask_with_value(uint16_t *channel_mask, + uint16_t value, uint8_t size) +{ + for (uint8_t i = 0; i < size; i++) { + channel_mask[i] = value; + } } diff --git a/features/lorawan/lorastack/phy/LoRaPHYAU915.h b/features/lorawan/lorastack/phy/LoRaPHYAU915.h index 1db1ff227c..27add455d9 100644 --- a/features/lorawan/lorastack/phy/LoRaPHYAU915.h +++ b/features/lorawan/lorastack/phy/LoRaPHYAU915.h @@ -76,6 +76,28 @@ public: virtual uint8_t apply_DR_offset(int8_t dr, int8_t dr_offset); +private: + + /** + * Sets the intersection of source and destination channel masks + * into the destination. + */ + void intersect_channel_mask(const uint16_t *source, uint16_t *destination, + uint8_t size); + + /** + * Fills channel mask array based upon the provided FSB mask + */ + void fill_channel_mask_with_fsb(const uint16_t *expectation, + const uint16_t *fsb_mask, + uint16_t *channel_mask, uint8_t size); + + /** + * Fills channel mask array with a given value + */ + void fill_channel_mask_with_value(uint16_t *channel_mask, + uint16_t value, uint8_t size); + private: /*! diff --git a/features/lorawan/lorastack/phy/LoRaPHYUS915.cpp b/features/lorawan/lorastack/phy/LoRaPHYUS915.cpp index a531de055b..95d52d212d 100644 --- a/features/lorawan/lorastack/phy/LoRaPHYUS915.cpp +++ b/features/lorawan/lorastack/phy/LoRaPHYUS915.cpp @@ -676,11 +676,33 @@ void LoRaPHYUS915::set_tx_cont_mode(cw_mode_params_t* params, uint32_t given_fre uint8_t LoRaPHYUS915::apply_DR_offset(int8_t dr, int8_t dr_offset) { - int8_t datarate = datarate_offsets_US915[dr][dr_offset]; + return datarate_offsets_US915[dr][dr_offset]; +} - if (datarate < 0) { - datarate = DR_0; + +void LoRaPHYUS915::intersect_channel_mask(const uint16_t *source, + uint16_t *destination, uint8_t size) +{ + for (uint8_t i = 0; i < size; i++) { + destination[i] &= source[i]; + } +} + +void LoRaPHYUS915::fill_channel_mask_with_fsb(const uint16_t *expectation, + const uint16_t *fsb_mask, + uint16_t *destination, + uint8_t size) +{ + for (uint8_t i = 0; i < size; i++) { + destination[i] = expectation[i] & fsb_mask[i]; } - return datarate; +} + +void LoRaPHYUS915::fill_channel_mask_with_value(uint16_t *channel_mask, + uint16_t value, uint8_t size) +{ + for (uint8_t i = 0; i < size; i++) { + channel_mask[i] = value; + } } diff --git a/features/lorawan/lorastack/phy/LoRaPHYUS915.h b/features/lorawan/lorastack/phy/LoRaPHYUS915.h index 2c883ae69d..577611ba9c 100644 --- a/features/lorawan/lorastack/phy/LoRaPHYUS915.h +++ b/features/lorawan/lorastack/phy/LoRaPHYUS915.h @@ -80,6 +80,26 @@ public: private: + /** + * Sets the intersection of source and destination channel masks + * into the destination. + */ + void intersect_channel_mask(const uint16_t *source, uint16_t *destination, + uint8_t size); + + /** + * Fills channel mask array based upon the provided FSB mask + */ + void fill_channel_mask_with_fsb(const uint16_t *expectation, + const uint16_t *fsb_mask, + uint16_t *channel_mask, uint8_t size); + + /** + * Fills channel mask array with a given value + */ + void fill_channel_mask_with_value(uint16_t *channel_mask, + uint16_t value, uint8_t size); + int8_t limit_tx_power(int8_t tx_power, int8_t max_band_tx_power, int8_t datarate);