Skip to content

Commit

Permalink
util: create oc_endpoint_addresses_t
Browse files Browse the repository at this point in the history
oc_endpoint_addresess_t is generalized oc_cloud_endpoints_t
  • Loading branch information
Danielius1922 authored and Daniel Adam committed Mar 19, 2024
1 parent 7a89f4c commit 12011d3
Show file tree
Hide file tree
Showing 37 changed files with 2,169 additions and 1,268 deletions.
14 changes: 8 additions & 6 deletions api/cloud/oc_cloud.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
#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"

Expand Down Expand Up @@ -113,7 +114,7 @@ oc_cloud_set_endpoint(oc_cloud_context_t *ctx)

bool success = false;
const oc_string_t *ep_addr =
oc_cloud_endpoint_selected_address(&ctx->store.ci_servers);
oc_endpoint_addresses_selected_uri(&ctx->store.ci_servers);
if (oc_string_to_endpoint(ep_addr, ctx->cloud_ep, NULL) == 0) {
// set device id to cloud endpoint for multiple servers
ctx->cloud_ep->device = ctx->device;
Expand Down Expand Up @@ -181,13 +182,14 @@ cloud_set_cloudconf(oc_cloud_context_t *ctx, const oc_cloud_conf_update_t *data)
oc_copy_string(&ctx->store.auth_provider, data->auth_provider);
}
if (!oc_string_is_null_or_empty(data->ci_server)) {
// cannot oc_cloud_endpoints_reinit, because the deinit might deallocate
// oc_string_t values and relocate memory, thus invalidating the
// cannot call oc_endpoint_addresses_reinit, because the deinit might
// deallocate oc_string_t values and relocate memory, thus invalidating the
// oc_string_view
oc_cloud_endpoints_deinit(&ctx->store.ci_servers);
// oc_cloud_endpoints_init only allocates, so the oc_string_view_t is valid
oc_endpoint_addresses_deinit(&ctx->store.ci_servers);
// oc_cloud_endpoint_addresses_init only allocates, so the oc_string_view_t
// is valid
oc_string_view_t cis = oc_string_view2(data->ci_server);
if (!oc_cloud_endpoints_init(
if (!oc_cloud_endpoint_addresses_init(
&ctx->store.ci_servers, ctx->store.ci_servers.on_selected_change,
ctx->store.ci_servers.on_selected_change_data, cis, data->sid)) {
OC_WRN("Failed to reinitialize cloud server endpoints");
Expand Down
46 changes: 24 additions & 22 deletions api/cloud/oc_cloud_context.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#include "oc_core_res.h"
#include "oc_endpoint.h"
#include "oc_session_events.h"
#include "util/oc_endpoint_address_internal.h"

#ifdef OC_SECURITY
#include "security/oc_pstat_internal.h"
Expand Down Expand Up @@ -73,7 +74,7 @@ reinitialize_cloud_storage(oc_cloud_context_t *ctx)
return;
}
OC_CLOUD_DBG("reinitializing cloud context in storage");
oc_cloud_store_initialize(&ctx->store, cloud_on_server_change, ctx);
oc_cloud_store_reinitialize(&ctx->store);
if (oc_cloud_store_dump(&ctx->store) < 0) {
OC_CLOUD_ERR("failed to dump cloud store");
}
Expand All @@ -93,6 +94,7 @@ cloud_context_init(size_t device)
ctx->cloud_ep_state = OC_SESSION_DISCONNECTED;
ctx->cloud_ep = oc_new_endpoint();
ctx->selected_identity_cred_id = -1;
oc_cloud_store_initialize(&ctx->store, cloud_on_server_change, ctx);
oc_cloud_store_load(&ctx->store);
ctx->store.status &=
~(OC_CLOUD_LOGGED_IN | OC_CLOUD_TOKEN_EXPIRY | OC_CLOUD_REFRESHED_TOKEN |
Expand Down Expand Up @@ -178,16 +180,13 @@ oc_cloud_get_user_id(const oc_cloud_context_t *ctx)
const oc_string_t *
oc_cloud_get_server_uri(const oc_cloud_context_t *ctx)
{
return oc_cloud_endpoint_selected_address(&ctx->store.ci_servers);
return oc_endpoint_addresses_selected_uri(&ctx->store.ci_servers);
}

const oc_uuid_t *
oc_cloud_get_server_id(const oc_cloud_context_t *ctx)
{
if (ctx->store.ci_servers.selected == NULL) {
return NULL;
}
return &ctx->store.ci_servers.selected->id;
return oc_endpoint_addresses_selected_uuid(&ctx->store.ci_servers);
}

const oc_endpoint_t *
Expand Down Expand Up @@ -313,38 +312,41 @@ oc_cloud_set_schedule_action(oc_cloud_context_t *ctx,
ctx->schedule_action.user_data = user_data;
}

oc_cloud_endpoint_t *
oc_cloud_add_server(oc_cloud_context_t *ctx, const char *uri, size_t uri_len,
oc_uuid_t sid)
oc_endpoint_address_t *
oc_cloud_add_server_address(oc_cloud_context_t *ctx, const char *uri,
size_t uri_len, oc_uuid_t sid)
{
return oc_cloud_endpoint_add(&ctx->store.ci_servers,
oc_string_view(uri, uri_len), sid);
return oc_endpoint_addresses_add(
&ctx->store.ci_servers,
oc_endpoint_address_make_view_with_uuid(oc_string_view(uri, uri_len), sid));
}

bool
oc_cloud_remove_server(oc_cloud_context_t *ctx, const oc_cloud_endpoint_t *ce)
oc_cloud_remove_server_address(oc_cloud_context_t *ctx,
const oc_endpoint_address_t *ea)
{
return oc_cloud_endpoint_remove(&ctx->store.ci_servers, ce);
return oc_endpoint_addresses_remove(&ctx->store.ci_servers, ea);
}

void
oc_cloud_iterate_servers(const oc_cloud_context_t *ctx,
oc_cloud_endpoints_iterate_fn_t fn, void *data)
oc_cloud_iterate_server_addresses(const oc_cloud_context_t *ctx,
oc_endpoint_addresses_iterate_fn_t fn,
void *data)
{
oc_cloud_endpoints_iterate(&ctx->store.ci_servers, fn, data);
oc_endpoint_addresses_iterate(&ctx->store.ci_servers, fn, data);
}

bool
oc_cloud_select_server(oc_cloud_context_t *ctx,
const oc_cloud_endpoint_t *server)
oc_cloud_select_server_address(oc_cloud_context_t *ctx,
const oc_endpoint_address_t *ea)
{
return oc_cloud_endpoint_select(&ctx->store.ci_servers, server);
return oc_endpoint_addresses_select(&ctx->store.ci_servers, ea);
}

const oc_cloud_endpoint_t *
oc_cloud_selected_server(const oc_cloud_context_t *ctx)
const oc_endpoint_address_t *
oc_cloud_selected_server_address(const oc_cloud_context_t *ctx)
{
return ctx->store.ci_servers.selected;
return oc_endpoint_addresses_selected(&ctx->store.ci_servers);
}

#endif /* OC_CLOUD */
1 change: 0 additions & 1 deletion api/cloud/oc_cloud_context_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
#ifndef OC_CLOUD_CONTEXT_INTERNAL_H
#define OC_CLOUD_CONTEXT_INTERNAL_H

#include "api/cloud/oc_cloud_endpoint_internal.h"
#include "api/cloud/oc_cloud_store_internal.h"
#include "oc_cloud.h"
#include "util/oc_compiler.h"
Expand Down
Loading

0 comments on commit 12011d3

Please sign in to comment.