Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bridging: minor updates and utility functions #606

Merged
merged 1 commit into from
Feb 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 47 additions & 38 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
# global files
*.o
*.cmd
*.tmp
*.gcda
*.gcno
whitespace_commit_checker.sh

# android
port/android/client
port/android/server
port/android/temp_sensor
Expand All @@ -19,6 +28,7 @@ port/android/libiotivity-lite-server.*
port/android/libiotivity-lite-client-server.*
port/android/*.so

# linux
port/linux/client
port/linux/server
port/linux/temp_sensor
Expand Down Expand Up @@ -47,34 +57,7 @@ port/linux/tests/*
port/linux/obj/
port/linux/onboarding_tool_creds/
port/linux/smart_lock
port/windows/vs2015/.vs/
port/windows/vs2015/Debug/
port/windows/vs2015/Release/
port/windows/vs2015/simpleserver_creds/
port/windows/vs2015/Win32/
port/windows/vs2015/x64/
port/windows/vs2015/onboarding_tool/Debug
port/windows/vs2015/onboarding_tool/x64
port/windows/vs2015/*.VC.opendb
port/windows/vs2015/*.VC.db
port/windows/vs2015/*.vcxproj.user
port/zephyr/outdir/
port/contiki/*.a
port/contiki/*.map
port/contiki/obj_native/
port/contiki/*.native
port/contiki/symbols.*
port/openthread/output
port/esp32/build/
port/esp32/sdkconfig
port/esp32/esp-idf/
port/esp32/sdkconfig.old
*.o
*.cmd
*.tmp
*.gcda
*.gcno
whitespace_commit_checker.sh

port/linux/platformtest
port/linux/storage_test
port/linux/apitest
Expand All @@ -85,7 +68,7 @@ port/linux/securitytest
port/linux/simpleserver_pki
port/linux/stapptest

#creds files
# creds files
port/linux/client_creds
port/linux/server_creds
port/linux/temp_sensor_creds
Expand All @@ -99,19 +82,45 @@ port/linux/multi_device_server_creds
port/linux/server_multithread_linux_creds
port/linux/client_multithread_linux_creds

#service
service/cloud-access/unittest/obj
service/resource-directory/client/unittest/obj
port/windows/vs2015/.vs/
port/windows/vs2015/Debug/
port/windows/vs2015/Release/
port/windows/vs2015/simpleserver_creds/
port/windows/vs2015/Win32/
port/windows/vs2015/x64/
port/windows/vs2015/onboarding_tool/Debug
port/windows/vs2015/onboarding_tool/x64
port/windows/vs2015/*.VC.opendb
port/windows/vs2015/*.VC.db
port/windows/vs2015/*.vcxproj.user

# zephyr
port/zephyr/outdir/

# openthread
port/openthread/output

# esp32
port/esp32/build/
port/esp32/sdkconfig
port/esp32/esp-idf/
port/esp32/sdkconfig.old

#vscode setting files
# vscode setting files
.vscode

.clang-tidy
# eclipse
.settings
.project
.cproject

# cmake build folder
build
build4test

python/plgd_headers.config
# sonarqube
.scannerwork

# eclipse config file
.cproject
.project
# clang-tidy
.clang-tidy
compile_commands.json
14 changes: 8 additions & 6 deletions api/oc_collection.c
Original file line number Diff line number Diff line change
Expand Up @@ -609,13 +609,15 @@ oc_get_next_collection_with_link(const oc_resource_t *resource,
collection = (oc_collection_t *)collection->res.next;
}

while (collection != NULL && collection->res.device == resource->device) {
const oc_link_t *link = (oc_link_t *)oc_list_head(collection->links);
while (link != NULL) {
if (link->resource == resource) {
return collection;
while (collection != NULL) {
if (collection->res.device == resource->device) {
const oc_link_t *link = (oc_link_t *)oc_list_head(collection->links);
while (link != NULL) {
if (link->resource == resource) {
return collection;
}
link = link->next;
}
link = link->next;
}
collection = (oc_collection_t *)collection->res.next;
}
Expand Down
37 changes: 28 additions & 9 deletions api/oc_core_res.c
Original file line number Diff line number Diff line change
Expand Up @@ -312,9 +312,10 @@ oc_create_device_resource(size_t device_count, const char *uri, const char *rt)
#ifdef OC_CLOUD
properties |= OC_OBSERVABLE;
#endif /* OC_CLOUD */
if (oc_strnlen(rt, OC_CHAR_ARRAY_LEN(OCF_D_RT) + 1) ==
OC_CHAR_ARRAY_LEN(OCF_D_RT) &&
strncmp(rt, OCF_D_RT, OC_CHAR_ARRAY_LEN(OCF_D_RT)) == 0) {
oc_string_view_t rtv =
oc_string_view(rt, oc_strnlen(rt, OC_CHAR_ARRAY_LEN(OCF_D_RT) + 1));
if (oc_string_view_is_equal(
rtv, oc_string_view(OCF_D_RT, OC_CHAR_ARRAY_LEN(OCF_D_RT)))) {
oc_core_populate_resource(OCF_D, device_count, uri,
OC_IF_R | OC_IF_BASELINE, OC_IF_R, properties,
oc_core_device_handler, /*put*/ NULL,
Expand Down Expand Up @@ -392,6 +393,21 @@ oc_core_add_new_device(oc_add_new_device_t cfg)
return &g_oc_device_info[device_count];
}

bool
oc_core_get_device_index(oc_uuid_t di, size_t *device)
{
uint32_t device_count = OC_ATOMIC_LOAD32(g_device_count);
for (size_t i = 0; i < device_count; i++) {
if (oc_uuid_is_equal(g_oc_device_info[i].di, di)) {
if (device != NULL) {
*device = i;
}
return true;
}
}
return false;
}

static void
oc_device_bind_rt(size_t device_index, const char *rt)
{
Expand Down Expand Up @@ -436,27 +452,28 @@ oc_device_bind_resource_type(size_t device, const char *type)
oc_device_bind_rt(device, type);
}

void
bool
oc_store_uri(const char *s_uri, oc_string_t *d_uri)
{
size_t s_len = oc_strnlen(s_uri, OC_MAX_STRING_LENGTH);
if (s_len >= OC_MAX_STRING_LENGTH) {
OC_ERR("Invalid URI");
return;
return false;
}

if (s_uri[0] == '/') {
oc_set_string(d_uri, s_uri, s_len);
return;
return true;
}

oc_string_t uri;
oc_alloc_string(&uri, s_len + 2);
memcpy(oc_string(uri) + 1, s_uri, s_len);
(oc_string(uri))[0] = '/';
(oc_string(uri))[s_len + 1] = '\0';
oc_new_string(d_uri, oc_string(uri), oc_string_len(uri));
oc_new_string(d_uri, oc_string(uri), s_len + 1);
oc_free_string(&uri);
return true;
}

static oc_resource_t *
Expand Down Expand Up @@ -653,13 +670,15 @@ core_is_resource_uri(const char *uri, size_t uri_len, const char *r_uri,
uri = &uri[1];
--uri_len;
}
oc_string_view_t uriv = oc_string_view(uri, uri_len);

if (r_uri[0] == '/') {
r_uri = &r_uri[1];
--r_uri_len;
}
oc_string_view_t r_uriv = oc_string_view(r_uri, r_uri_len);

return uri_len == r_uri_len &&
(uri_len == 0 || memcmp(uri, r_uri, uri_len) == 0);
return oc_string_view_is_equal(uriv, r_uriv);
}

int
Expand Down
4 changes: 3 additions & 1 deletion api/oc_core_res_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,10 @@ void oc_core_encode_interfaces_mask(CborEncoder *parent, unsigned iface_mask,
*
* @param s_uri source string (cannot be NULL)
* @param d_uri destination (to be allocated) to store the uri (cannot be NULL)
*
* @return true on success
*/
void oc_store_uri(const char *s_uri, oc_string_t *d_uri) OC_NONNULL();
bool oc_store_uri(const char *s_uri, oc_string_t *d_uri) OC_NONNULL();

/**
* @brief populate resource
Expand Down
7 changes: 7 additions & 0 deletions api/oc_uuid.c
Original file line number Diff line number Diff line change
Expand Up @@ -178,3 +178,10 @@ oc_uuid_is_equal(oc_uuid_t first, oc_uuid_t second)
{
return memcmp(first.id, second.id, OC_UUID_ID_SIZE) == 0;
}

bool
oc_uuid_is_empty(oc_uuid_t uuid)
{
oc_uuid_t nil_uuid = { { 0 } };
return (memcmp(&uuid, &nil_uuid, sizeof(oc_uuid_t)) == 0);
}
39 changes: 38 additions & 1 deletion api/unittest/coreresourcetest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include "tests/gtest/Device.h"
#include "tests/gtest/RepPool.h"
#include "util/oc_macros_internal.h"
#include "util/oc_secure_string_internal.h"

#ifdef OC_HAS_FEATURE_PUSH
#include "api/oc_push_internal.h"
Expand All @@ -46,6 +47,7 @@ static const std::string kOCFSpecVersion{ "ocf.1.0.0" };
static const std::string kOCFDataModelVersion{ "ocf.res.1.0.0" };

static constexpr size_t kDevice1ID{ 0 };
static constexpr size_t kDevice2ID{ 1 };
static constexpr std::string_view kDevice1Name{ "Test Device 1" };
static constexpr std::string_view kDevice2Name{ "Test Device 2" };

Expand Down Expand Up @@ -184,6 +186,25 @@ TEST_F(TestCoreResource, EncodeInterfaces_P)
encodeInterfaces(all_ifs_mask, all_ifstrs, includePrivateInterfaces);
}

TEST_F(TestCoreResource, StoreURI_P)
{
oc_string_t uri;
ASSERT_TRUE(oc_store_uri("oic/d", &uri));
EXPECT_STREQ("/oic/d", oc_string(uri));
oc_free_string(&uri);

ASSERT_TRUE(oc_store_uri("/oic/d", &uri));
EXPECT_STREQ("/oic/d", oc_string(uri));
oc_free_string(&uri);
}

TEST_F(TestCoreResource, StoreURI_F)
{
oc_string_t uri;
std::string tooLong(OC_MAX_STRING_LENGTH + 1, 'a');
ASSERT_FALSE(oc_store_uri(tooLong.c_str(), &uri));
}

class TestCoreResourceWithDevice : public testing::Test {
public:
#if defined(OC_SERVER) && defined(OC_DYNAMIC_ALLOCATION)
Expand Down Expand Up @@ -220,7 +241,7 @@ class TestCoreResourceWithDevice : public testing::Test {
/*uri=*/"/oic/d",
},
{
/*rt=*/"oic.d.test2",
/*rt=*/OCF_D_RT,
/*name=*/std::string(kDevice2Name),
/*spec_version=*/"ocf.1.0.0",
/*data_model_version=*/"ocf.res.1.0.0",
Expand Down Expand Up @@ -436,6 +457,22 @@ TEST_F(TestCoreResourceWithDevice, CoreGetResourceIsVerticalResource_P)
#endif /* OC_SERVER */
}

TEST_F(TestCoreResourceWithDevice, GetDeviceIndex_P)
{
EXPECT_TRUE(
oc_core_get_device_index(oc_core_get_device_info(kDevice1ID)->di, nullptr));
size_t index{};
EXPECT_TRUE(
oc_core_get_device_index(oc_core_get_device_info(kDevice2ID)->di, &index));
EXPECT_EQ(kDevice2ID, index);
}

TEST_F(TestCoreResourceWithDevice, GetDeviceIndex_F)
{
oc_uuid_t invalid{};
EXPECT_FALSE(oc_core_get_device_index(invalid, nullptr));
}

TEST_F(TestCoreResourceWithDevice, SetName_P)
{
std::string name = "new name";
Expand Down
13 changes: 12 additions & 1 deletion api/unittest/uuidtest.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/******************************************************************
*
* Copyright 2018 Samsung Electronics All Rights Reserved.
* Copyright 2024 ETRI All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License"),
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -29,6 +30,16 @@ constexpr const char UUID2[] = "XYZabcdefghijklmnopqrstuvwxyz012";

using uuid_buffer_t = std::array<char, OC_UUID_LEN>;

TEST(UUID, UUIDIsNill)
{
oc_uuid_t uuid{};
EXPECT_TRUE(oc_uuid_is_empty(uuid));

oc_uuid_t uuid2{};
oc_str_to_uuid(UUID, &uuid2);
EXPECT_FALSE(oc_uuid_is_empty(uuid2));
}

TEST(UUID, StrToUUIDTest_P)
{
oc_uuid_t uuid{};
Expand Down Expand Up @@ -227,4 +238,4 @@ TEST(UUIDComparison, NonEmptyUUID)

EXPECT_FALSE(oc_uuid_is_equal(uuid, gen_uuid));
EXPECT_FALSE(oc_uuid_is_equal(gen_uuid, uuid));
}
}
9 changes: 9 additions & 0 deletions include/oc_core_res.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,15 @@ oc_uuid_t *oc_core_get_device_id(size_t device);
*/
oc_device_info_t *oc_core_get_device_info(size_t device);

/**
* @brief retrieve the index of device with given id
*
* @param di device id to be used for search
* @param[out] device device index of the device with the given id
* @return true if found
*/
bool oc_core_get_device_index(oc_uuid_t di, size_t *device);

/**
* @brief retrieve the platform information
*
Expand Down
Loading
Loading