Skip to content

Commit

Permalink
fixup! util: extend oc_endpoint_addresses_t by custom on change callback
Browse files Browse the repository at this point in the history
  • Loading branch information
Danielius1922 committed Mar 25, 2024
1 parent 0c91a89 commit 343e01a
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 4 deletions.
4 changes: 3 additions & 1 deletion api/cloud/oc_cloud_context.c
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,9 @@ oc_cloud_registration_context_init(oc_cloud_registration_context_t *regctx,
// limit the number of server changes to the number of servers at startup
// minus one (since the initial address is already used)
size_t server_count = oc_endpoint_addresses_size(servers);
regctx->remaining_server_changes = server_count > 0 ? server_count - 1 : 0;
assert(server_count <= UINT8_MAX);
regctx->remaining_server_changes =
(uint8_t)(server_count > 0 ? server_count - 1 : 0);
regctx->server_changed = false;
}

Expand Down
5 changes: 5 additions & 0 deletions swig/swig_interfaces/oc_cloud.i
Original file line number Diff line number Diff line change
Expand Up @@ -712,6 +712,11 @@ void jni_cloud_context_clear(oc_cloud_context_t *ctx, bool dump_async)
%ignore cloud_context_has_permanent_access_token;
%ignore cloud_context_clear_access_token;
%ignore cloud_context_has_refresh_token;

%ignore oc_cloud_registration_context_t;
%ignore oc_cloud_registration_context_init;
%ignore oc_cloud_registration_context_deinit;
%ignore oc_cloud_context_t::registration_ctx;
%include "api/cloud/oc_cloud_context_internal.h"

%ignore oc_cloud_add_server_address;
Expand Down
10 changes: 9 additions & 1 deletion swig/swig_interfaces/oc_endpoint_address.i
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
%ignore oc_endpoint_address_metadata_t;
%ignore oc_endpoint_address_metadata_view_t;
%ignore oc_endpoint_address_view_t;
%ignore oc_endpoint_address_view;
%ignore oc_endpoint_address_make_view_with_uuid;
%ignore oc_endpoint_address_make_view_with_name;

Expand Down Expand Up @@ -87,6 +88,11 @@

/*******************Begin oc_endpoint_address_internal.h****************************/

%ignore oc_endpoint_address_metadata_type_t;
%ignore oc_endpoint_address_encode;
%ignore on_selected_endpoint_address_change_fn_t;
%ignore oc_endpoint_addresses_on_selected_change_t;

%ignore oc_endpoint_addresses_t;
%ignore oc_endpoint_addresses_init;
%ignore oc_endpoint_addresses_deinit;
Expand All @@ -104,11 +110,13 @@
%ignore oc_endpoint_addresses_select_by_uri;
%ignore oc_endpoint_addresses_select_next;
%ignore oc_endpoint_addresses_is_selected;
%ignore oc_endpoint_addresses_selected;
%ignore oc_endpoint_addresses_selected_uri;
%ignore oc_endpoint_addresses_selected_uuid;
%ignore oc_endpoint_addresses_selected_name;
%ignore oc_endpoint_addresses_encode;

%ignore oc_endpoint_addresses_set_on_selected_change;
%ignore oc_endpoint_addresses_get_on_selected_change;

%include "util/oc_endpoint_address_internal.h"

Expand Down
2 changes: 1 addition & 1 deletion util/oc_endpoint_address.c
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ oc_endpoint_addresses_reinit(oc_endpoint_addresses_t *eas,
}

void
oc_endpoint_adddresses_set_on_selected_change(
oc_endpoint_addresses_set_on_selected_change(
oc_endpoint_addresses_t *eas, on_selected_endpoint_address_change_fn_t cb,
void *data)
{
Expand Down
2 changes: 1 addition & 1 deletion util/oc_endpoint_address_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ bool oc_endpoint_addresses_reinit(oc_endpoint_addresses_t *eas,
OC_NONNULL(1);

/** Set the callback invoked when the selected endpoint address changes */
void oc_endpoint_adddresses_set_on_selected_change(
void oc_endpoint_addresses_set_on_selected_change(
oc_endpoint_addresses_t *eas, on_selected_endpoint_address_change_fn_t cb,
void *data) OC_NONNULL(1, 2);

Expand Down
15 changes: 15 additions & 0 deletions util/unittest/endpoint_address_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,16 @@ TEST_F(TestEndpointAddresses, Select)
{
oc_endpoint_addresses_t &ea{ getAddresses() };

auto on_selected_change = oc_endpoint_addresses_get_on_selected_change(&ea);
ASSERT_EQ(nullptr, on_selected_change.cb);
ASSERT_EQ(nullptr, on_selected_change.cb_data);
bool selected_changed = false;
oc_endpoint_addresses_set_on_selected_change(
&ea, [](void *data) { *static_cast<bool *>(data) = true; },
&selected_changed);

oc_endpoint_addresses_select_next(&ea);
EXPECT_FALSE(selected_changed);
auto *selected_addr = oc_endpoint_addresses_selected_uri(&ea);
EXPECT_EQ(nullptr, selected_addr);
auto *selected_uuid = oc_endpoint_addresses_selected_uuid(&ea);
Expand All @@ -395,6 +404,7 @@ TEST_F(TestEndpointAddresses, Select)

// when adding to an empty list, the first added item is automatically
// selected
EXPECT_TRUE(selected_changed);
EXPECT_TRUE(oc_endpoint_addresses_is_selected(&ea, uri1));
selected_addr = oc_endpoint_addresses_selected_uri(&ea);
ASSERT_NE(nullptr, selected_addr);
Expand All @@ -407,8 +417,10 @@ TEST_F(TestEndpointAddresses, Select)
ASSERT_EQ(nullptr, selected_name);

// non-existing URI shouldn't change the selection
selected_changed = false;
EXPECT_FALSE(
oc_endpoint_addresses_select_by_uri(&ea, OC_STRING_VIEW("/fail")));
EXPECT_FALSE(selected_changed);
EXPECT_FALSE(oc_endpoint_addresses_is_selected(&ea, OC_STRING_VIEW("/fail")));
EXPECT_TRUE(oc_endpoint_addresses_is_selected(&ea, uri1));
selected_addr = oc_endpoint_addresses_selected_uri(&ea);
Expand Down Expand Up @@ -453,12 +465,15 @@ TEST_F(TestEndpointAddresses, Select)
EXPECT_STREQ(name3.data, oc_string(*selected_name));

// rotate back to uri1
selected_changed = false;
oc_endpoint_addresses_select_next(&ea);
#else /* !OC_DYNAMIC_ALLOCATION */
// the list has a single element, so the selection should stay at uri1
selected_changed = false;
oc_endpoint_addresses_select_next(&ea);
#endif /* OC_DYNAMIC_ALLOCATION */

EXPECT_TRUE(selected_changed);
EXPECT_TRUE(oc_endpoint_addresses_is_selected(&ea, uri1));
selected_addr = oc_endpoint_addresses_selected_uri(&ea);
ASSERT_NE(nullptr, selected_addr);
Expand Down

0 comments on commit 343e01a

Please sign in to comment.