From d41d4cdd64b0e41dd0c2b048f856cff197ca41e0 Mon Sep 17 00:00:00 2001 From: Daniel Adam Date: Sat, 11 Jan 2025 14:42:01 +0100 Subject: [PATCH] Add OC_API annotations to public API --- api/cloud/oc_cloud.c | 5 +- api/cloud/unittest/cloud_store_test.cpp | 1 + api/oc_buffer_settings.c | 172 ++++++++++++++++++ api/oc_discovery.c | 6 +- api/oc_helpers.c | 7 +- api/oc_main.c | 148 --------------- api/oc_session_events.c | 1 - .../device-provisioning-client/plgd_dps.c | 7 + .../plgd_dps_manager.c | 1 - api/plgd/unittest/plgdtimetest.cpp | 1 - api/unittest/buffersettingstest.cpp | 90 +++++++++ api/unittest/helperstest.cpp | 83 +++++++++ include/oc_buffer_settings.h | 9 + include/oc_enums.h | 7 +- include/oc_network_monitor.h | 58 +----- include/oc_session_events.h | 55 ++++++ include/oc_signal_event_loop.h | 4 +- include/oc_sp.h | 3 + port/android/ipadapter.c | 5 +- port/esp32/adapter/src/ipadapter.c | 3 + port/esp32/main/CMakeLists.txt | 1 + port/linux/ipadapter.c | 5 +- port/oc_connectivity.h | 20 +- port/oc_dns.h | 64 +++++++ port/oc_storage.h | 21 ++- port/unittest/connectivitytest.cpp | 8 +- port/windows/ipadapter.c | 5 +- port/windows/vs2015/IoTivity-lite.vcxproj | 2 + .../vs2015/IoTivity-lite.vcxproj.filters | 6 + swig/swig_interfaces/oc_buffer_settings.i | 3 +- swig/swig_interfaces/oc_enums.i | 1 + swig/swig_interfaces/oc_session_events.i | 1 + 32 files changed, 555 insertions(+), 248 deletions(-) create mode 100644 api/oc_buffer_settings.c create mode 100644 api/unittest/buffersettingstest.cpp create mode 100644 port/oc_dns.h diff --git a/api/cloud/oc_cloud.c b/api/cloud/oc_cloud.c index f951cf7fb2..008bb1a91c 100644 --- a/api/cloud/oc_cloud.c +++ b/api/cloud/oc_cloud.c @@ -35,12 +35,15 @@ #include "oc_api.h" #include "oc_collection.h" #include "oc_core_res.h" -#include "oc_network_monitor.h" #include "port/oc_assert.h" #include "util/oc_endpoint_address_internal.h" #include "util/oc_mmem_internal.h" #include "util/oc_secure_string_internal.h" +#ifdef OC_NETWORK_MONITOR +#include "oc_network_monitor.h" +#endif /* OC_NETWORK_MONITOR */ + #ifdef OC_SECURITY #include "security/oc_tls_internal.h" #endif /* OC_SECURITY */ diff --git a/api/cloud/unittest/cloud_store_test.cpp b/api/cloud/unittest/cloud_store_test.cpp index ee5df28c16..34161fd9d0 100644 --- a/api/cloud/unittest/cloud_store_test.cpp +++ b/api/cloud/unittest/cloud_store_test.cpp @@ -22,6 +22,7 @@ #include "api/cloud/oc_cloud_resource_internal.h" #include "api/cloud/oc_cloud_store_internal.h" #include "api/oc_event_callback_internal.h" +#include "api/oc_rep_internal.h" #include "api/oc_storage_internal.h" #include "oc_api.h" #include "oc_config.h" diff --git a/api/oc_buffer_settings.c b/api/oc_buffer_settings.c new file mode 100644 index 0000000000..d36e7d01a3 --- /dev/null +++ b/api/oc_buffer_settings.c @@ -0,0 +1,172 @@ +/**************************************************************************** + * + * Copyright (c) 2017 Intel Corporation + * + * 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 "oc_buffer_settings.h" +#include "port/oc_log_internal.h" +#include "util/oc_features.h" + +#include + +#ifdef OC_DYNAMIC_ALLOCATION + +#include "messaging/coap/conf.h" + +#ifdef OC_INOUT_BUFFER_SIZE +static size_t _OC_MTU_SIZE = OC_INOUT_BUFFER_SIZE; +#else /* OC_INOUT_BUFFER_SIZE */ +static size_t _OC_MTU_SIZE = 2048 + COAP_MAX_HEADER_SIZE; +#endif /* !OC_INOUT_BUFFER_SIZE */ + +static size_t _OC_MAX_APP_DATA_SIZE = 7168; +#if defined(OC_APP_DATA_BUFFER_SIZE) || !defined(OC_REP_ENCODING_REALLOC) +static size_t _OC_MIN_APP_DATA_SIZE = 7168; +#else /* OC_APP_DATA_BUFFER_SIZE || !OC_REP_ENCODING_REALLOC */ +static size_t _OC_MIN_APP_DATA_SIZE = 256; +#endif /* !OC_APP_DATA_BUFFER_SIZE && OC_REP_ENCODING_REALLOC */ +static size_t _OC_BLOCK_SIZE = 1024; // FIX + +int +oc_set_mtu_size(size_t mtu_size) +{ + (void)mtu_size; +#ifdef OC_INOUT_BUFFER_SIZE + return -1; +#else /* !OC_INOUT_BUFFER_SIZE */ +#ifdef OC_BLOCK_WISE + if (mtu_size < (COAP_MAX_HEADER_SIZE + 16)) { + return -1; + } +#ifdef OC_OSCORE + _OC_MTU_SIZE = mtu_size + COAP_MAX_HEADER_SIZE; +#else /* OC_OSCORE */ + _OC_MTU_SIZE = mtu_size; +#endif /* !OC_OSCORE */ + mtu_size -= COAP_MAX_HEADER_SIZE; + size_t i; + for (i = 10; i >= 4 && (mtu_size >> i) == 0; i--) + ; + _OC_BLOCK_SIZE = ((size_t)1) << i; +#endif /* OC_BLOCK_WISE */ + return 0; +#endif /* OC_INOUT_BUFFER_SIZE */ +} + +long +oc_get_mtu_size(void) +{ + return (long)_OC_MTU_SIZE; +} + +void +oc_set_max_app_data_size(size_t size) +{ +#ifdef OC_APP_DATA_BUFFER_SIZE + (void)size; +#else /* !OC_APP_DATA_BUFFER_SIZE */ + _OC_MAX_APP_DATA_SIZE = size; +#ifndef OC_REP_ENCODING_REALLOC + _OC_MIN_APP_DATA_SIZE = size; +#endif /* !OC_REP_ENCODING_REALLOC */ +#ifndef OC_BLOCK_WISE + _OC_BLOCK_SIZE = size; + _OC_MTU_SIZE = size + COAP_MAX_HEADER_SIZE; +#endif /* !OC_BLOCK_WISE */ +#endif /* OC_APP_DATA_BUFFER_SIZE */ +} + +long +oc_get_max_app_data_size(void) +{ + return (long)_OC_MAX_APP_DATA_SIZE; +} + +void +oc_set_min_app_data_size(size_t size) +{ +#if defined(OC_APP_DATA_BUFFER_SIZE) || !defined(OC_REP_ENCODING_REALLOC) + (void)size; +#else /* !OC_APP_DATA_BUFFER_SIZE && !OC_REP_ENCODING_REALLOC */ + _OC_MIN_APP_DATA_SIZE = size; +#endif /* OC_APP_DATA_BUFFER_SIZE || !OC_REP_ENCODING_REALLOC */ +} + +long +oc_get_min_app_data_size(void) +{ + return (long)_OC_MIN_APP_DATA_SIZE; +} + +long +oc_get_block_size(void) +{ + return (long)_OC_BLOCK_SIZE; +} + +#else /* !OC_DYNAMIC_ALLOCATION */ + +int +oc_set_mtu_size(size_t mtu_size) +{ + (void)mtu_size; + OC_WRN("Dynamic memory not available"); + return -1; +} + +long +oc_get_mtu_size(void) +{ + OC_WRN("Dynamic memory not available"); + return -1; +} + +void +oc_set_max_app_data_size(size_t size) +{ + (void)size; + OC_WRN("Dynamic memory not available"); +} + +long +oc_get_max_app_data_size(void) +{ + OC_WRN("Dynamic memory not available"); + return -1; +} + +void +oc_set_min_app_data_size(size_t size) +{ + (void)size; + OC_WRN("Dynamic memory not available"); +} + +long +oc_get_min_app_data_size(void) +{ + OC_WRN("Dynamic memory not available"); + return -1; +} + +long +oc_get_block_size(void) +{ + OC_WRN("Dynamic memory not available"); + return -1; +} + +#endif /* OC_DYNAMIC_ALLOCATION */ diff --git a/api/oc_discovery.c b/api/oc_discovery.c index f6484e8095..0b240d9cfa 100644 --- a/api/oc_discovery.c +++ b/api/oc_discovery.c @@ -1241,7 +1241,7 @@ oc_wkcore_discovery_handler(oc_request_t *request, } } - if (rt_request != 0 && rt_device != 0 && + if (rt_request != NULL && rt_device != NULL && strncmp(rt_request, rt_device, rt_len) == 0) { /* request for specific device type */ matches = 1; @@ -1273,7 +1273,9 @@ oc_wkcore_discovery_handler(oc_request_t *request, length += clf_add_line_to_buffer("/oic/res>;"); length += clf_add_line_to_buffer("rt=\"oic.wk.res "); - length += clf_add_str_to_buffer(rt_device, rt_devlen); + if (rt_device != NULL) { + length += clf_add_str_to_buffer(rt_device, rt_devlen); + } length += clf_add_line_to_buffer("\";"); length += clf_add_line_to_buffer("if=\"oic.if.ll oic.if.baseline\";ct=10000"); diff --git a/api/oc_helpers.c b/api/oc_helpers.c index 177ff7db5c..1a63cb38ce 100644 --- a/api/oc_helpers.c +++ b/api/oc_helpers.c @@ -24,6 +24,7 @@ #include "port/oc_random.h" #include "util/oc_macros_internal.h" #include "util/oc_mmem_internal.h" +#include "util/oc_secure_string_internal.h" #include #include #include @@ -141,7 +142,7 @@ oc_set_string(oc_string_t *dst, const char *str, size_t str_len) // oc_free_string oc_new_string(©, str, str_len); oc_free_string(dst); - oc_new_string(dst, oc_string(copy), oc_string_len(copy)); + oc_new_string(dst, oc_string(copy), oc_string_len_unsafe(copy)); oc_free_string(©); #endif /* OC_DYNAMIC_ALLOCATION */ } @@ -311,7 +312,7 @@ bool _oc_copy_string_to_array(oc_string_array_t *ocstringarray, const char str[], size_t index) { - size_t len = strlen(str); + size_t len = oc_strnlen(str, STRING_ARRAY_ITEM_MAX_LEN); if (len >= STRING_ARRAY_ITEM_MAX_LEN) { return false; } @@ -351,7 +352,7 @@ oc_join_string_array(oc_string_array_t *ocstringarray, oc_string_t *ocstring) ++i) { const char *item = (const char *)oc_string_array_get_item(*ocstringarray, i); - size_t item_len = strlen(item); + size_t item_len = oc_strnlen(item, STRING_ARRAY_ITEM_MAX_LEN); if (item_len != 0) { if (len > 0) { oc_string(*ocstring)[len] = ' '; diff --git a/api/oc_main.c b/api/oc_main.c index b3b3f91463..6ba495d2bc 100644 --- a/api/oc_main.c +++ b/api/oc_main.c @@ -98,154 +98,6 @@ oc_get_factory_presets_cb(void) return &g_factory_presets; } -#ifdef OC_DYNAMIC_ALLOCATION -#include "oc_buffer_settings.h" -#ifdef OC_INOUT_BUFFER_SIZE -static size_t _OC_MTU_SIZE = OC_INOUT_BUFFER_SIZE; -#else /* OC_INOUT_BUFFER_SIZE */ -static size_t _OC_MTU_SIZE = 2048 + COAP_MAX_HEADER_SIZE; -#endif /* !OC_INOUT_BUFFER_SIZE */ -#ifdef OC_APP_DATA_BUFFER_SIZE -static size_t _OC_MAX_APP_DATA_SIZE = 7168; -static size_t _OC_MIN_APP_DATA_SIZE = 7168; -#else /* OC_APP_DATA_BUFFER_SIZE */ -static size_t _OC_MAX_APP_DATA_SIZE = 7168; -#ifdef OC_REP_ENCODING_REALLOC -static size_t _OC_MIN_APP_DATA_SIZE = 256; -#else /* OC_REP_ENCODING_REALLOC */ -static size_t _OC_MIN_APP_DATA_SIZE = 7168; -#endif /* !OC_REP_ENCODING_REALLOC */ -#endif /* !OC_APP_DATA_BUFFER_SIZE */ -static size_t _OC_BLOCK_SIZE = 1024; // FIX - -int -oc_set_mtu_size(size_t mtu_size) -{ - (void)mtu_size; -#ifdef OC_INOUT_BUFFER_SIZE - return -1; -#else /* !OC_INOUT_BUFFER_SIZE */ -#ifdef OC_BLOCK_WISE - if (mtu_size < (COAP_MAX_HEADER_SIZE + 16)) { - return -1; - } -#ifdef OC_OSCORE - _OC_MTU_SIZE = mtu_size + COAP_MAX_HEADER_SIZE; -#else /* OC_OSCORE */ - _OC_MTU_SIZE = mtu_size; -#endif /* !OC_OSCORE */ - mtu_size -= COAP_MAX_HEADER_SIZE; - size_t i; - for (i = 10; i >= 4 && (mtu_size >> i) == 0; i--) - ; - _OC_BLOCK_SIZE = ((size_t)1) << i; -#endif /* OC_BLOCK_WISE */ - return 0; -#endif /* OC_INOUT_BUFFER_SIZE */ -} - -long -oc_get_mtu_size(void) -{ - return (long)_OC_MTU_SIZE; -} - -void -oc_set_max_app_data_size(size_t size) -{ -#ifdef OC_APP_DATA_BUFFER_SIZE - (void)size; -#else /* !OC_APP_DATA_BUFFER_SIZE */ - _OC_MAX_APP_DATA_SIZE = size; -#ifndef OC_REP_ENCODING_REALLOC - _OC_MIN_APP_DATA_SIZE = size; -#endif /* !OC_REP_ENCODING_REALLOC */ -#ifndef OC_BLOCK_WISE - _OC_BLOCK_SIZE = size; - _OC_MTU_SIZE = size + COAP_MAX_HEADER_SIZE; -#endif /* !OC_BLOCK_WISE */ -#endif /* OC_APP_DATA_BUFFER_SIZE */ -} - -long -oc_get_max_app_data_size(void) -{ - return (long)_OC_MAX_APP_DATA_SIZE; -} - -void -oc_set_min_app_data_size(size_t size) -{ -#if defined(OC_APP_DATA_BUFFER_SIZE) || !defined(OC_REP_ENCODING_REALLOC) - (void)size; -#else /* !OC_APP_DATA_BUFFER_SIZE && !OC_REP_ENCODING_REALLOC */ - _OC_MIN_APP_DATA_SIZE = size; -#endif /* OC_APP_DATA_BUFFER_SIZE || !OC_REP_ENCODING_REALLOC */ -} - -long -oc_get_min_app_data_size(void) -{ - return (long)_OC_MIN_APP_DATA_SIZE; -} - -long -oc_get_block_size(void) -{ - return (long)_OC_BLOCK_SIZE; -} -#else /* !OC_DYNAMIC_ALLOCATION */ -int -oc_set_mtu_size(size_t mtu_size) -{ - (void)mtu_size; - OC_WRN("Dynamic memory not available"); - return -1; -} - -long -oc_get_mtu_size(void) -{ - OC_WRN("Dynamic memory not available"); - return -1; -} - -void -oc_set_max_app_data_size(size_t size) -{ - (void)size; - OC_WRN("Dynamic memory not available"); -} - -long -oc_get_max_app_data_size(void) -{ - OC_WRN("Dynamic memory not available"); - return -1; -} - -void -oc_set_min_app_data_size(size_t size) -{ - (void)size; - OC_WRN("Dynamic memory not available"); -} - -long -oc_get_min_app_data_size(void) -{ - OC_WRN("Dynamic memory not available"); - return -1; -} - -long -oc_get_block_size(void) -{ - OC_WRN("Dynamic memory not available"); - return -1; -} -#endif /* OC_DYNAMIC_ALLOCATION */ - static void oc_shutdown_all_devices(void) { diff --git a/api/oc_session_events.c b/api/oc_session_events.c index 53caffd914..8623604b00 100644 --- a/api/oc_session_events.c +++ b/api/oc_session_events.c @@ -22,7 +22,6 @@ #include "api/oc_session_events_internal.h" #include "oc_api.h" #include "oc_buffer.h" -#include "oc_network_monitor.h" #include "oc_session_events.h" #include "oc_signal_event_loop.h" #include "port/oc_connectivity_internal.h" diff --git a/api/plgd/device-provisioning-client/plgd_dps.c b/api/plgd/device-provisioning-client/plgd_dps.c index 6d2db25a2b..3345817ada 100644 --- a/api/plgd/device-provisioning-client/plgd_dps.c +++ b/api/plgd/device-provisioning-client/plgd_dps.c @@ -30,7 +30,10 @@ #include "api/oc_tcp_internal.h" #include "oc_certs.h" #include "oc_core_res.h" + +#ifdef OC_NETWORK_MONITOR #include "oc_network_monitor.h" +#endif /* OC_NETWORK_MONITOR */ #include #include @@ -226,13 +229,17 @@ plgd_dps_session_callbacks_deinit(plgd_dps_context_t *ctx) void plgd_dps_interface_callbacks_init(void) { +#ifdef OC_NETWORK_MONITOR oc_add_network_interface_event_callback(dps_interface_event_handler); +#endif /* OC_NETWORK_MONITOR */ } void plgd_dps_interface_callbacks_deinit(void) { +#ifdef OC_NETWORK_MONITOR oc_remove_network_interface_event_callback(dps_interface_event_handler); +#endif /* OC_NETWORK_MONITOR */ } #endif /* OC_SESSION_EVENTS */ diff --git a/api/plgd/device-provisioning-client/plgd_dps_manager.c b/api/plgd/device-provisioning-client/plgd_dps_manager.c index 06c9afebfa..8b51e8346a 100644 --- a/api/plgd/device-provisioning-client/plgd_dps_manager.c +++ b/api/plgd/device-provisioning-client/plgd_dps_manager.c @@ -30,7 +30,6 @@ #include "plgd_dps_internal.h" #include "oc_cred.h" -#include "oc_network_monitor.h" #include "security/oc_cred_util_internal.h" #include "util/oc_list.h" diff --git a/api/plgd/unittest/plgdtimetest.cpp b/api/plgd/unittest/plgdtimetest.cpp index d800004374..d625e3c864 100644 --- a/api/plgd/unittest/plgdtimetest.cpp +++ b/api/plgd/unittest/plgdtimetest.cpp @@ -35,7 +35,6 @@ #include "messaging/coap/transactions_internal.h" #include "oc_acl.h" #include "oc_core_res.h" -#include "oc_network_monitor.h" #include "oc_ri.h" #include "port/oc_log_internal.h" #include "port/oc_network_event_handler_internal.h" diff --git a/api/unittest/buffersettingstest.cpp b/api/unittest/buffersettingstest.cpp new file mode 100644 index 0000000000..0ae98dec0f --- /dev/null +++ b/api/unittest/buffersettingstest.cpp @@ -0,0 +1,90 @@ +/**************************************************************************** + * + * Copyright (c) 2024 plgd.dev s.r.o. + * + * 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 "messaging/coap/conf.h" +#include "oc_buffer_settings.h" +#include "util/oc_features.h" + +#include "gtest/gtest.h" + +#ifdef OC_DYNAMIC_ALLOCATION + +TEST(TestBufferSettings, SetMTUSize) +{ +#ifdef OC_INOUT_BUFFER_SIZE + EXPECT_EQ(-1, oc_set_mtu_size(42)); +#else /* !OC_INOUT_BUFFER_SIZE */ +#ifdef OC_BLOCK_WISE + EXPECT_EQ(-1, oc_set_mtu_size(42)); + EXPECT_EQ(0, oc_set_mtu_size(oc_get_mtu_size())); +#endif /* OC_BLOCK_WISE */ +#endif /* OC_INOUT_BUFFER_SIZE */ +} + +#ifndef OC_APP_DATA_BUFFER_SIZE + +TEST(TestBufferSettings, SetMaxAppDataSize) +{ + auto max_app_size = static_cast(oc_get_max_app_data_size()); + oc_set_max_app_data_size(42); + EXPECT_EQ(42, oc_get_max_app_data_size()); + + oc_set_max_app_data_size(max_app_size); +} + +#ifdef OC_REP_ENCODING_REALLOC + +TEST(TestBufferSettings, SetMinAppDataSize) +{ + auto min_app_size = static_cast(oc_get_min_app_data_size()); + oc_set_min_app_data_size(42); + EXPECT_EQ(42, oc_get_min_app_data_size()); + + oc_set_min_app_data_size(min_app_size); +} + +#endif /* OC_REP_ENCODING_REALLOC */ + +#endif /* !OC_APP_DATA_BUFFER_SIZE */ + +#else /* !OC_DYNAMIC_ALLOCATION */ + +TEST(TestBufferSettings, SetMTUSize) +{ + EXPECT_EQ(-1, oc_set_mtu_size(42)); + EXPECT_EQ(-1, oc_get_mtu_size()); +} + +TEST(TestBufferSettings, SetMaxAppDataSize) +{ + oc_set_max_app_data_size(42); + EXPECT_EQ(-1, oc_get_max_app_data_size()); +} + +TEST(TestBufferSettings, SetMinAppDataSize) +{ + oc_set_min_app_data_size(42); + EXPECT_EQ(-1, oc_get_min_app_data_size()); +} + +TEST(TestBufferSettings, GetBlockSize) +{ + EXPECT_EQ(-1, oc_get_block_size()); +} + +#endif /* OC_DYNAMIC_ALLOCATION */ diff --git a/api/unittest/helperstest.cpp b/api/unittest/helperstest.cpp index ae4f0e0eee..246a18084c 100644 --- a/api/unittest/helperstest.cpp +++ b/api/unittest/helperstest.cpp @@ -256,3 +256,86 @@ TEST(Helpers, RandomBuffer) oc_random_destroy(); } + +// Test error when provided hex string buffer is too small +TEST(Helpers, HexStringTooSmallError) +{ + std::array array{ 0x1f, 0xa0, 0x5b, 0xff }; + std::array hex_str; // Too small to fit the full hex representation + // (8 chars + null terminator) + size_t hex_str_len = hex_str.size(); + int result = oc_conv_byte_array_to_hex_string(array.data(), array.size(), + &hex_str[0], &hex_str_len); + ASSERT_EQ(result, -1); +} + +// Test successful conversion of a byte array to hex string +TEST(Helpers, ConvertByteArrayToHexStringSuccess) +{ + std::array array{ 0x1f, 0xa0, 0x5b, 0xff }; + std::array + hex_str; // Must hold 8 characters (2 per byte) + null terminator + size_t hex_str_len = hex_str.size(); + int result = oc_conv_byte_array_to_hex_string(array.data(), array.size(), + &hex_str[0], &hex_str_len); + ASSERT_EQ(result, 0); + EXPECT_STREQ(hex_str.data(), "1fa05bff"); + EXPECT_EQ(hex_str_len, 8); +} + +// Test empty hex string input +TEST(Helpers, EmptyHexStringError) +{ + std::string hex_str = ""; + std::array array; + size_t array_len = array.size(); + + int result = oc_conv_hex_string_to_byte_array( + hex_str.c_str(), hex_str.length(), &array[0], &array_len); + ASSERT_EQ(result, -1); +} + +// Test when array buffer is too small +TEST(Helpers, ArrayBufferTooSmallError) +{ + std::string hex_str = "1fa05bff"; + std::array + array; // Too small to hold the expected byte array (needs 4 bytes) + size_t array_len = array.size(); + + int result = oc_conv_hex_string_to_byte_array( + hex_str.c_str(), hex_str.length(), &array[0], &array_len); + ASSERT_EQ(result, -1); +} + +// Test successful conversion of an even-length hex string to byte array +TEST(Helpers, EvenLengthHexStringSuccess) +{ + std::string hex_str = "1fa05bff"; + std::array array; + size_t array_len = array.size(); + + int result = oc_conv_hex_string_to_byte_array( + hex_str.c_str(), hex_str.length(), &array[0], &array_len); + + ASSERT_EQ(result, 0); + EXPECT_EQ(array_len, 4); + std::array expected_array = { 0x1f, 0xa0, 0x5b, 0xff }; + EXPECT_EQ(expected_array, array); +} + +// Test successful conversion of an odd-length hex string to byte array +TEST(Helpers, OddLengthHexStringSuccess) +{ + std::string hex_str = "fa05bf"; + std::array array; + size_t array_len = array.size(); + + int result = oc_conv_hex_string_to_byte_array( + hex_str.c_str(), hex_str.length(), &array[0], &array_len); + + ASSERT_EQ(result, 0); + std::array expected_array = { 0xfa, 0x05, 0xbf }; + EXPECT_EQ(array_len, expected_array.size()); + EXPECT_EQ(expected_array, array); +} diff --git a/include/oc_buffer_settings.h b/include/oc_buffer_settings.h index 1f88472a9f..a9735c19a4 100644 --- a/include/oc_buffer_settings.h +++ b/include/oc_buffer_settings.h @@ -21,6 +21,8 @@ #ifndef OC_BUFFER_SETTINGS_H #define OC_BUFFER_SETTINGS_H +#include "oc_export.h" + #include #ifdef __cplusplus @@ -33,6 +35,7 @@ extern "C" { * @param mtu_size size in bytes * @return int 0-success */ +OC_API int oc_set_mtu_size(size_t mtu_size); /** @@ -40,6 +43,7 @@ int oc_set_mtu_size(size_t mtu_size); * * @return long the MTU size in bytes */ +OC_API long oc_get_mtu_size(void); /** @@ -47,6 +51,7 @@ long oc_get_mtu_size(void); * * @param size size in bytes */ +OC_API void oc_set_max_app_data_size(size_t size); /** @@ -54,6 +59,7 @@ void oc_set_max_app_data_size(size_t size); * * @return long size in bytes */ +OC_API long oc_get_max_app_data_size(void); /** @@ -62,6 +68,7 @@ long oc_get_max_app_data_size(void); * * @param size size in bytes */ +OC_API void oc_set_min_app_data_size(size_t size); /** @@ -69,6 +76,7 @@ void oc_set_min_app_data_size(size_t size); * * @return long size in bytes */ +OC_API long oc_get_min_app_data_size(void); /** @@ -76,6 +84,7 @@ long oc_get_min_app_data_size(void); * * @return long the block size in bytes */ +OC_API long oc_get_block_size(void); #ifdef __cplusplus diff --git a/include/oc_enums.h b/include/oc_enums.h index 7a17bec2f2..b36f2a9d39 100644 --- a/include/oc_enums.h +++ b/include/oc_enums.h @@ -27,12 +27,12 @@ extern "C" { #endif +#include "oc_export.h" #include "oc_helpers.h" #include "util/oc_compiler.h" /** * @brief generic enum values for resources that convey string enums - * */ typedef enum oc_enum_t { OC_ENUM_ABORTED = 1, @@ -133,7 +133,6 @@ typedef enum oc_pos_description_t { /** * @brief enum of location tags - * */ typedef enum oc_locn_t { OCF_LOCN_UNKNOWN = 1, @@ -204,6 +203,7 @@ typedef enum oc_locn_t { * @param val the enum value * @return const char* the string */ +OC_API const char *oc_enum_to_str(oc_enum_t val); /** @@ -212,6 +212,7 @@ const char *oc_enum_to_str(oc_enum_t val); * @param pos the enum value of the position description * @return const char* the string */ +OC_API const char *oc_enum_pos_desc_to_str(oc_pos_description_t pos); /** @@ -220,6 +221,7 @@ const char *oc_enum_pos_desc_to_str(oc_pos_description_t pos); * @param locn the location enum value * @return const char* the string */ +OC_API const char *oc_enum_locn_to_str(oc_locn_t locn); /** @@ -230,6 +232,7 @@ const char *oc_enum_locn_to_str(oc_locn_t locn); * NULL) * @return oc_locn_t the location value */ +OC_API oc_locn_t oc_str_to_enum_locn(oc_string_t locn_str, bool *oc_defined) OC_NONNULL(2); diff --git a/include/oc_network_monitor.h b/include/oc_network_monitor.h index 2a291dd360..df986e21ed 100644 --- a/include/oc_network_monitor.h +++ b/include/oc_network_monitor.h @@ -23,8 +23,6 @@ #include "oc_export.h" #include "oc_network_events.h" -#include "oc_session_events.h" -#include "util/oc_compiler.h" #ifdef __cplusplus extern "C" { @@ -37,6 +35,7 @@ extern "C" { * @return 0 on success * @return -1 on error */ +OC_API int oc_add_network_interface_event_callback(interface_event_handler_t cb); /** @@ -46,61 +45,8 @@ int oc_add_network_interface_event_callback(interface_event_handler_t cb); * @return 0 on success * @return -1 on error */ -int oc_remove_network_interface_event_callback(interface_event_handler_t cb); - -/** - * @brief Add the callback to receive session event notifications. - * - * @param cb The callback to be added. Must not be NULL. - * @return 0 on success - * @return -1 on error - * - * @deprecated replaced by oc_add_session_event_callback_v1 in v2.2.5.4 - */ -OC_API -int oc_add_session_event_callback(session_event_handler_t cb) - OC_DEPRECATED("replaced by oc_add_session_event_callback_v1 in v2.2.5.4"); - -/** - * @brief Add the callback to receive session event notifications. - * - * @param cb The callback to be added (cannot be NULL). - * @param user_data user data passed to the callback when invoked - * @return 0 on success - * @return -1 on error - */ -OC_API -int oc_add_session_event_callback_v1(session_event_handler_v1_t cb, - void *user_data); - -/** - * @brief Remove the callback to receive session event notifications. - * @param cb The callback to be removed. Must not be NULL. - * @return 0 on success - * @return -1 on error - * - * @deprecated replaced by oc_remove_session_event_callback_v1 in v2.2.5.4 - */ OC_API -int oc_remove_session_event_callback(session_event_handler_t cb) - OC_DEPRECATED("replaced by oc_remove_session_event_callback_v1 in v2.2.5.4"); - -/** - * @brief Remove the callback with to receive session event notifications. - * - * @param cb The callback to be removed. - * @param user_data user data provided to the callback by - oc_add_session_event_callback_v1 - * @param ignore_user_data ignore user_data and match only the function pointer - * @return 0 on success - * @return OC_ERR_SESSION_EVENT_HANDLER_NOT_FOUND when no match was found - * @return -1 other errors - * - * @see oc_add_session_event_callback_v1 - */ -OC_API -int oc_remove_session_event_callback_v1(session_event_handler_v1_t cb, - void *user_data, bool ignore_user_data); +int oc_remove_network_interface_event_callback(interface_event_handler_t cb); #ifdef __cplusplus } diff --git a/include/oc_session_events.h b/include/oc_session_events.h index e142b45705..8f4daded33 100644 --- a/include/oc_session_events.h +++ b/include/oc_session_events.h @@ -27,6 +27,7 @@ #include "oc_export.h" #include "oc_endpoint.h" +#include "util/oc_compiler.h" #ifdef __cplusplus extern "C" { @@ -76,6 +77,60 @@ typedef void (*session_event_handler_v1_t)(const oc_endpoint_t *endpoint, OC_API void oc_session_events_set_event_delay(int secs); +/** + * @brief Add the callback to receive session event notifications. + * + * @param cb The callback to be added. Must not be NULL. + * @return 0 on success + * @return -1 on error + * + * @deprecated replaced by oc_add_session_event_callback_v1 in v2.2.5.4 + */ +OC_API +int oc_add_session_event_callback(session_event_handler_t cb) + OC_DEPRECATED("replaced by oc_add_session_event_callback_v1 in v2.2.5.4"); + +/** + * @brief Add the callback to receive session event notifications. + * + * @param cb The callback to be added (cannot be NULL). + * @param user_data user data passed to the callback when invoked + * @return 0 on success + * @return -1 on error + */ +OC_API +int oc_add_session_event_callback_v1(session_event_handler_v1_t cb, + void *user_data); + +/** + * @brief Remove the callback to receive session event notifications. + * @param cb The callback to be removed. Must not be NULL. + * @return 0 on success + * @return -1 on error + * + * @deprecated replaced by oc_remove_session_event_callback_v1 in v2.2.5.4 + */ +OC_API +int oc_remove_session_event_callback(session_event_handler_t cb) + OC_DEPRECATED("replaced by oc_remove_session_event_callback_v1 in v2.2.5.4"); + +/** + * @brief Remove the callback with to receive session event notifications. + * + * @param cb The callback to be removed. + * @param user_data user data provided to the callback by + oc_add_session_event_callback_v1 + * @param ignore_user_data ignore user_data and match only the function pointer + * @return 0 on success + * @return OC_ERR_SESSION_EVENT_HANDLER_NOT_FOUND when no match was found + * @return -1 other errors + * + * @see oc_add_session_event_callback_v1 + */ +OC_API +int oc_remove_session_event_callback_v1(session_event_handler_v1_t cb, + void *user_data, bool ignore_user_data); + #ifdef __cplusplus } #endif diff --git a/include/oc_signal_event_loop.h b/include/oc_signal_event_loop.h index f13aa25ad7..2a82c7becc 100644 --- a/include/oc_signal_event_loop.h +++ b/include/oc_signal_event_loop.h @@ -21,14 +21,16 @@ #ifndef OC_SIGNAL_EVENT_LOOP_H #define OC_SIGNAL_EVENT_LOOP_H +#include "oc_export.h" + #ifdef __cplusplus extern "C" { #endif /** * @brief signal event loop - * */ +OC_API void _oc_signal_event_loop(void); #ifdef __cplusplus diff --git a/include/oc_sp.h b/include/oc_sp.h index 4eb520e12c..ddd4c44bfd 100644 --- a/include/oc_sp.h +++ b/include/oc_sp.h @@ -25,6 +25,8 @@ #ifndef OC_SP_H #define OC_SP_H +#include "oc_export.h" + #include #ifdef __cplusplus @@ -126,6 +128,7 @@ typedef enum { * @param[in] mfg_credid the credential ID of the /oic/sec/cred entry containing * the manufactures end-entity certificate */ +OC_API void oc_pki_set_security_profile(size_t device, unsigned supported_profiles, oc_sp_types_t current_profile, int mfg_credid); diff --git a/port/android/ipadapter.c b/port/android/ipadapter.c index 6c68151c64..930820ea3e 100644 --- a/port/android/ipadapter.c +++ b/port/android/ipadapter.c @@ -31,7 +31,6 @@ #include "oc_buffer.h" #include "oc_core_res.h" #include "oc_endpoint.h" -#include "oc_network_monitor.h" #include "port/common/posix/oc_socket_internal.h" #include "port/oc_assert.h" #include "port/oc_connectivity.h" @@ -41,6 +40,10 @@ #include "port/oc_random.h" #include "util/oc_macros_internal.h" +#ifdef OC_NETWORK_MONITOR +#include "oc_network_monitor.h" +#endif /* OC_NETWORK_MONITOR */ + #ifdef OC_SESSION_EVENTS #include "api/oc_session_events_internal.h" #endif /* OC_SESSION_EVENTS */ diff --git a/port/esp32/adapter/src/ipadapter.c b/port/esp32/adapter/src/ipadapter.c index 7f1e72ffb3..76ba3699f4 100644 --- a/port/esp32/adapter/src/ipadapter.c +++ b/port/esp32/adapter/src/ipadapter.c @@ -32,7 +32,10 @@ #include "oc_buffer.h" #include "oc_core_res.h" #include "oc_endpoint.h" + +#ifdef OC_NETWORK_MONITOR #include "oc_network_monitor.h" +#endif /* OC_NETWORK_MONITOR */ #ifdef OC_SESSION_EVENTS #include "api/oc_session_events_internal.h" diff --git a/port/esp32/main/CMakeLists.txt b/port/esp32/main/CMakeLists.txt index 62005ea124..5dfcb7aa03 100644 --- a/port/esp32/main/CMakeLists.txt +++ b/port/esp32/main/CMakeLists.txt @@ -30,6 +30,7 @@ set(sources ${CMAKE_CURRENT_SOURCE_DIR}/../../../api/client/oc_client_cb.c ${CMAKE_CURRENT_SOURCE_DIR}/../../../api/oc_base64.c ${CMAKE_CURRENT_SOURCE_DIR}/../../../api/oc_blockwise.c + ${CMAKE_CURRENT_SOURCE_DIR}/../../../api/oc_buffer_settings.c ${CMAKE_CURRENT_SOURCE_DIR}/../../../api/oc_client_api.c ${CMAKE_CURRENT_SOURCE_DIR}/../../../api/oc_client_role.c ${CMAKE_CURRENT_SOURCE_DIR}/../../../api/oc_con_resource.c diff --git a/port/linux/ipadapter.c b/port/linux/ipadapter.c index 1293346540..c4f3d3a0d4 100644 --- a/port/linux/ipadapter.c +++ b/port/linux/ipadapter.c @@ -27,7 +27,6 @@ #include "oc_buffer.h" #include "oc_core_res.h" #include "oc_endpoint.h" -#include "oc_network_monitor.h" #include "port/common/posix/oc_socket_internal.h" #include "port/oc_assert.h" #include "port/oc_clock.h" @@ -41,6 +40,10 @@ #include "util/oc_features.h" #include "util/oc_macros_internal.h" +#ifdef OC_NETWORK_MONITOR +#include "oc_network_monitor.h" +#endif /* OC_NETWORK_MONITOR */ + #ifdef OC_SESSION_EVENTS #include "api/oc_session_events_internal.h" #endif /* OC_SESSION_EVENTS */ diff --git a/port/oc_connectivity.h b/port/oc_connectivity.h index fb89df95a8..ff7d1b1964 100644 --- a/port/oc_connectivity.h +++ b/port/oc_connectivity.h @@ -29,6 +29,7 @@ #include "oc_export.h" #include "oc_network_events.h" #include "oc_session_events.h" +#include "port/oc_dns.h" #include "util/oc_atomic.h" #include "util/oc_features.h" #include "util/oc_memb.h" @@ -167,25 +168,6 @@ void oc_send_discovery_request(oc_message_t *message); void oc_connectivity_end_session(const oc_endpoint_t *endpoint) OC_DEPRECATED("replaced by oc_close_session in v2.2.5.14"); -#ifdef OC_DNS_LOOKUP -/** - * @brief dns look up - * - * @param domain the url - * @param addr the address - * @param flags the transport flags - * @return int 0 = success - */ -int oc_dns_lookup(const char *domain, oc_string_t *addr, transport_flags flags); -#ifdef OC_DNS_CACHE -/** - * @brief clear the DNS cache - * - */ -void oc_dns_clear_cache(void); -#endif /* OC_DNS_CACHE */ -#endif /* OC_DNS_LOOKUP */ - /** * @brief retrieve list of endpoints for the device * diff --git a/port/oc_dns.h b/port/oc_dns.h new file mode 100644 index 0000000000..2735241e9f --- /dev/null +++ b/port/oc_dns.h @@ -0,0 +1,64 @@ +/**************************************************************************** + * + * Copyright (c) 2016, 2018, 2020 Intel Corporation + * + * 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. + * + ****************************************************************************/ + +/** + * @file + */ + +#ifndef OC_PORT_DNS_H +#define OC_PORT_DNS_H + +#include "oc_endpoint.h" +#include "oc_export.h" +#include "oc_helpers.h" +#include "util/oc_features.h" + +#ifdef __cplusplus +extern "C" { +#endif + +#ifdef OC_DNS_LOOKUP + +/** + * @brief dns look up + * + * @param domain the url + * @param addr the address + * @param flags the transport flags + * @return int 0 = success + */ +OC_API +int oc_dns_lookup(const char *domain, oc_string_t *addr, transport_flags flags); + +#ifdef OC_DNS_CACHE + +/** + * @brief clear the DNS cache + */ +OC_API +void oc_dns_clear_cache(void); + +#endif /* OC_DNS_CACHE */ + +#endif /* OC_DNS_LOOKUP */ + +#ifdef __cplusplus +} +#endif + +#endif /* OC_PORT_DNS_H */ diff --git a/port/oc_storage.h b/port/oc_storage.h index 06dbce8e1e..5129c4757b 100644 --- a/port/oc_storage.h +++ b/port/oc_storage.h @@ -21,6 +21,9 @@ #ifndef OC_PORT_STORAGE_H #define OC_PORT_STORAGE_H +#include "oc_export.h" +#include "util/oc_compiler.h" + #include #include @@ -31,32 +34,36 @@ extern "C" { /** * @brief open the storage * - * @param store the storage path (cannot be NULL) + * @param store the storage path * @return 0 on success * @return <0 on failure */ +OC_API int oc_storage_config(const char *store); /** * @brief read from the storage * - * @param store the path to be read - * @param buf the buffer to store the contents + * @param store the path to be read (cannot be NULL) + * @param buf the buffer to store the contents (cannot be NULL) * @param size amount of bytes to read * @return long amount of bytes read */ -long oc_storage_read(const char *store, uint8_t *buf, size_t size); +OC_API +long oc_storage_read(const char *store, uint8_t *buf, size_t size) OC_NONNULL(); /** * @brief write to storage * - * @param store the store (file path) - * @param buf the buffer to write + * @param store the store (file path, cannot be NULL) + * @param buf the buffer to write (cannot be NULL) * @param size the size of the buffer to write * @return long >= 0 amount of bytes written on success * @return long < 0 on failure */ -long oc_storage_write(const char *store, const uint8_t *buf, size_t size); +OC_API +long oc_storage_write(const char *store, const uint8_t *buf, size_t size) + OC_NONNULL(); #ifdef __cplusplus } diff --git a/port/unittest/connectivitytest.cpp b/port/unittest/connectivitytest.cpp index 18226d632f..ce56e3541b 100644 --- a/port/unittest/connectivitytest.cpp +++ b/port/unittest/connectivitytest.cpp @@ -24,7 +24,6 @@ #include "messaging/coap/signal_internal.h" #include "oc_api.h" #include "oc_buffer.h" -#include "oc_network_monitor.h" #include "port/oc_connectivity.h" #include "port/oc_connectivity_internal.h" #include "port/oc_log_internal.h" @@ -34,6 +33,10 @@ #include "tests/gtest/Device.h" #include "tests/gtest/Endpoint.h" +#ifdef OC_NETWORK_MONITOR +#include "oc_network_monitor.h" +#endif /* OC_NETWORK_MONITOR */ + #include "gtest/gtest.h" #include @@ -157,6 +160,8 @@ TEST(TestConnectivity_wakeup, WakeupInvalidDevice) } #endif /* OC_DYNAMIC_ALLOCATION */ +#ifdef OC_NETWORK_MONITOR + static void interface_event_handler(oc_interface_event_t event) { @@ -183,7 +188,6 @@ TEST_F(TestConnectivity, oc_remove_network_interface_event_callback_fail) -1, oc_remove_network_interface_event_callback(interface_event_handler)); } -#ifdef OC_NETWORK_MONITOR TEST_F(TestConnectivity, handle_network_interface_event_callback) { oc_add_network_interface_event_callback(interface_event_handler); diff --git a/port/windows/ipadapter.c b/port/windows/ipadapter.c index dc18eba180..f943d58f48 100644 --- a/port/windows/ipadapter.c +++ b/port/windows/ipadapter.c @@ -26,7 +26,6 @@ #include "oc_buffer.h" #include "oc_core_res.h" #include "oc_endpoint.h" -#include "oc_network_monitor.h" #include "port/common/posix/oc_socket_internal.h" #include "port/oc_assert.h" #include "port/oc_connectivity.h" @@ -36,6 +35,10 @@ #include "util/oc_list.h" #include "util/oc_memb.h" +#ifdef OC_NETWORK_MONITOR +#include "oc_network_monitor.h" +#endif /* OC_NETWORK_MONITOR */ + #ifdef OC_TCP #include "tcpadapter.h" #endif /* OC_TCP */ diff --git a/port/windows/vs2015/IoTivity-lite.vcxproj b/port/windows/vs2015/IoTivity-lite.vcxproj index 13bcf3042c..97e5943760 100644 --- a/port/windows/vs2015/IoTivity-lite.vcxproj +++ b/port/windows/vs2015/IoTivity-lite.vcxproj @@ -322,6 +322,7 @@ + @@ -354,6 +355,7 @@ + diff --git a/port/windows/vs2015/IoTivity-lite.vcxproj.filters b/port/windows/vs2015/IoTivity-lite.vcxproj.filters index 9446d4cf30..4f68246642 100644 --- a/port/windows/vs2015/IoTivity-lite.vcxproj.filters +++ b/port/windows/vs2015/IoTivity-lite.vcxproj.filters @@ -113,6 +113,9 @@ Core + + Core + Core @@ -616,6 +619,9 @@ Headers + + Port + Core diff --git a/swig/swig_interfaces/oc_buffer_settings.i b/swig/swig_interfaces/oc_buffer_settings.i index 2f136868dd..f3c982aae7 100644 --- a/swig/swig_interfaces/oc_buffer_settings.i +++ b/swig/swig_interfaces/oc_buffer_settings.i @@ -22,4 +22,5 @@ %rename (getMaxAppDataSize) oc_get_max_app_data_size; %rename (getBlockSize) oc_get_block_size; -%include "oc_buffer_settings.h" \ No newline at end of file +#define OC_API +%include "oc_buffer_settings.h" diff --git a/swig/swig_interfaces/oc_enums.i b/swig/swig_interfaces/oc_enums.i index cc03f776f8..f81159bbbd 100644 --- a/swig/swig_interfaces/oc_enums.i +++ b/swig/swig_interfaces/oc_enums.i @@ -32,5 +32,6 @@ %rename(locationToString) oc_enum_locn_to_str; %rename(stringToLocation) oc_str_to_enum_locn; +#define OC_API #define OC_NONNULL(...) %include "oc_enums.h" diff --git a/swig/swig_interfaces/oc_session_events.i b/swig/swig_interfaces/oc_session_events.i index 662b92a7b9..cf788c88f7 100644 --- a/swig/swig_interfaces/oc_session_events.i +++ b/swig/swig_interfaces/oc_session_events.i @@ -39,4 +39,5 @@ void jni_session_events_set_event_delay(int secs) } %} #define OC_API +#define OC_DEPRECATED(...) %include "oc_session_events.h"