Skip to content

Commit

Permalink
fixup! cloud: add support for multiple servers for a single cloud
Browse files Browse the repository at this point in the history
  • Loading branch information
Danielius1922 committed Feb 20, 2024
1 parent 423e972 commit cdfa775
Show file tree
Hide file tree
Showing 24 changed files with 988 additions and 430 deletions.
46 changes: 24 additions & 22 deletions .github/workflows/sonar-cloud-analysis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,26 +44,26 @@ jobs:
build_type: Debug
coverage: true

plgd-device-tests:
strategy:
fail-fast: false
matrix:
include:
- name: cloud-server
build_args: ""
- name: cloud-server-discovery-resource-observable-access-in-RFOTM-rep-realloc
build_args: "-DOC_DISCOVERY_RESOURCE_OBSERVABLE_ENABLED=ON -DOC_RESOURCE_ACCESS_IN_RFOTM_ENABLED=ON -DOC_REPRESENTATION_REALLOC_ENCODING_ENABLED=ON"
# try with SHA384
cert_signature_algorithm: ECDSA-SHA384
cert_elliptic_curve: P384
uses: ./.github/workflows/plgd-device-test-with-cfg.yml
with:
name: ${{ matrix.name }}
build_args: "-DOC_COLLECTIONS_IF_CREATE_ENABLED=ON -DOC_MNT_ENABLED=ON -DOC_OSCORE_ENABLED=OFF -DPLGD_DEV_TIME_ENABLED=ON -DOC_ETAG_ENABLED=ON -DOC_COVERAGE_ENABLED=ON -DOC_SOFTWARE_UPDATE_ENABLED=ON ${{ matrix.build_args }}"
build_type: Debug
cert_signature_algorithm: ${{ matrix.cert_signature_algorithm }}
cert_elliptic_curve: ${{ matrix.cert_elliptic_curve }}
coverage: true
# plgd-device-tests:
# strategy:
# fail-fast: false
# matrix:
# include:
# - name: cloud-server
# build_args: ""
# - name: cloud-server-discovery-resource-observable-access-in-RFOTM-rep-realloc
# build_args: "-DOC_DISCOVERY_RESOURCE_OBSERVABLE_ENABLED=ON -DOC_RESOURCE_ACCESS_IN_RFOTM_ENABLED=ON -DOC_REPRESENTATION_REALLOC_ENCODING_ENABLED=ON"
# # try with SHA384
# cert_signature_algorithm: ECDSA-SHA384
# cert_elliptic_curve: P384
# uses: ./.github/workflows/plgd-device-test-with-cfg.yml
# with:
# name: ${{ matrix.name }}
# build_args: "-DOC_COLLECTIONS_IF_CREATE_ENABLED=ON -DOC_MNT_ENABLED=ON -DOC_OSCORE_ENABLED=OFF -DPLGD_DEV_TIME_ENABLED=ON -DOC_ETAG_ENABLED=ON -DOC_COVERAGE_ENABLED=ON -DOC_SOFTWARE_UPDATE_ENABLED=ON ${{ matrix.build_args }}"
# build_type: Debug
# cert_signature_algorithm: ${{ matrix.cert_signature_algorithm }}
# cert_elliptic_curve: ${{ matrix.cert_elliptic_curve }}
# coverage: true

plgd-hub-tests:
strategy:
Expand All @@ -90,7 +90,8 @@ jobs:
runs-on: ubuntu-22.04
env:
BUILD_WRAPPER_OUT_DIR: build_wrapper_output_directory # Directory where build-wrapper output will be placed
needs: [unit-tests, plgd-device-tests, plgd-hub-tests]
# needs: [unit-tests, plgd-device-tests, plgd-hub-tests]
needs: [unit-tests, plgd-hub-tests]
steps:
- name: Checkout
uses: actions/checkout@v4
Expand Down Expand Up @@ -126,7 +127,8 @@ jobs:
run: |
cd tools
# ls -lR .
gcovr --add-tracefile "unit-test-coverage/*coverage*.json" --add-tracefile "plgd-device-coverage/*coverage*.json" --add-tracefile "plgd-hub-coverage/*coverage*.json" --sonarqube --output "coverage.xml" --verbose
# --add-tracefile "plgd-device-coverage/*coverage*.json"
gcovr --add-tracefile "unit-test-coverage/*coverage*.json" --add-tracefile "plgd-hub-coverage/*coverage*.json" --sonarqube --output "coverage.xml" --verbose
- name: Run sonar-scanner
env:
Expand Down
72 changes: 44 additions & 28 deletions api/cloud/oc_cloud.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include "api/cloud/oc_cloud_log_internal.h"
#include "api/cloud/oc_cloud_manager_internal.h"
#include "api/cloud/oc_cloud_rd_internal.h"
#include "api/cloud/oc_cloud_resource_internal.h"
#include "api/cloud/oc_cloud_store_internal.h"
#include "api/oc_ri_internal.h"
#include "api/oc_ri_server_internal.h"
Expand All @@ -36,6 +37,7 @@
#include "oc_core_res.h"
#include "oc_network_monitor.h"
#include "port/oc_assert.h"
#include "util/oc_mmem_internal.h"
#include "util/oc_secure_string_internal.h"

#ifdef OC_SECURITY
Expand Down Expand Up @@ -172,25 +174,31 @@ cloud_set_cloudconf(oc_cloud_context_t *ctx, const oc_cloud_conf_update_t *data)
{
assert(ctx != NULL);
assert(data != NULL);
if (data->auth_provider_len > 0) {
oc_set_string(&ctx->store.auth_provider, data->auth_provider,
data->auth_provider_len);
}
if (data->access_token_len > 0) {
oc_set_string(&ctx->store.access_token, data->access_token,
data->access_token_len);
}
if (data->ci_server_len > 0 &&
!oc_cloud_endpoints_reinit(
&ctx->store.ci_servers,
oc_string_view(data->ci_server, data->ci_server_len))) {
OC_WRN("Failed to reinitialize cloud server endpoints");
}
if (data->sid_len > 0) {
oc_set_string(&ctx->store.sid, data->sid, data->sid_len);
if (!oc_string_is_null_or_empty(data->access_token)) {
oc_copy_string(&ctx->store.access_token, data->access_token);
}
if (!oc_string_is_null_or_empty(data->auth_provider)) {
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
// 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_string_view_t cis = oc_string_view2(data->ci_server);
if (!oc_cloud_endpoints_init(&ctx->store.ci_servers, cis, data->sid)) {
OC_WRN("Failed to reinitialize cloud server endpoints");
}
}
}

static oc_string_t
cloud_cstring_to_temp_oc_string(const char *str, size_t len)
{
return OC_MMEM((void *)str, len + 1, NULL); // +1 for null terminator
}

int
oc_cloud_provision_conf_resource(oc_cloud_context_t *ctx, const char *server,
const char *access_token,
Expand All @@ -205,12 +213,14 @@ oc_cloud_provision_conf_resource(oc_cloud_context_t *ctx, const char *server,
if (server_len >= OC_MAX_STRING_LENGTH) {
return -1;
}
size_t access_token_len = oc_strnlen_s(access_token, OC_MAX_STRING_LENGTH);
size_t access_token_len = oc_strnlen(access_token, OC_MAX_STRING_LENGTH);
if (access_token_len >= OC_MAX_STRING_LENGTH) {
return -1;
}
size_t server_id_len = oc_strnlen_s(server_id, OC_MAX_STRING_LENGTH);
if (server_id_len >= OC_MAX_STRING_LENGTH) {
size_t server_id_len = oc_strnlen(server_id, OC_UUID_LEN);
oc_uuid_t sid = { 0 };
if (server_id_len != (OC_UUID_LEN - 1) ||
oc_str_to_uuid_v1(server_id, server_id_len, &sid) < 0) {
return -1;
}
size_t auth_provider_len = oc_strnlen_s(auth_provider, OC_MAX_STRING_LENGTH);
Expand All @@ -225,16 +235,21 @@ oc_cloud_provision_conf_resource(oc_cloud_context_t *ctx, const char *server,
cloud_manager_stop(ctx);
oc_cloud_deregister_stop(ctx);

const oc_string_t at =
cloud_cstring_to_temp_oc_string(access_token, access_token_len);
const oc_string_t s = cloud_cstring_to_temp_oc_string(server, server_len);
oc_cloud_conf_update_t data = {
.access_token = access_token,
.access_token_len = access_token_len,
.ci_server = server,
.ci_server_len = server_len,
.sid = server_id,
.sid_len = server_id_len,
.auth_provider = auth_provider,
.auth_provider_len = auth_provider_len,
.access_token = &at,
.ci_server = &s,
.sid = sid,
};

const oc_string_t ap =
cloud_cstring_to_temp_oc_string(auth_provider, auth_provider_len);
if (auth_provider != NULL) {
data.auth_provider = &ap;
}

cloud_set_cloudconf(ctx, &data);
cloud_rd_reset_context(ctx);

Expand All @@ -253,7 +268,8 @@ void
oc_cloud_update_by_resource(oc_cloud_context_t *ctx,
const oc_cloud_conf_update_t *data)
{
if (data->ci_server_len == 0) {
assert(data->ci_server != NULL);
if (oc_string_is_empty(data->ci_server)) {
OC_CLOUD_DBG("got forced deregister via provisioning of empty cis");
if (cloud_reset(ctx->device, false, false, CLOUD_DEREGISTER_TIMEOUT) != 0) {
OC_CLOUD_DBG("reset failed");
Expand Down
27 changes: 17 additions & 10 deletions api/cloud/oc_cloud_context.c
Original file line number Diff line number Diff line change
Expand Up @@ -144,10 +144,13 @@ oc_cloud_get_cis(const oc_cloud_context_t *ctx)
.data;
}

const char *
const oc_uuid_t *
oc_cloud_get_sid(const oc_cloud_context_t *ctx)
{
return oc_string(ctx->store.sid);
if (ctx->store.ci_servers.selected == NULL) {
return NULL;
}
return &ctx->store.ci_servers.selected->id;
}

const char *
Expand Down Expand Up @@ -255,18 +258,18 @@ oc_cloud_set_schedule_action(oc_cloud_context_t *ctx,
ctx->schedule_action.user_data = user_data;
}

bool
oc_cloud_add_server(oc_cloud_context_t *ctx, const char *uri, size_t uri_len)
oc_cloud_endpoint_t *
oc_cloud_add_server(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)) != NULL;
oc_string_view(uri, uri_len), sid);
}

bool
oc_cloud_remove_server(oc_cloud_context_t *ctx, const char *uri, size_t uri_len)
oc_cloud_remove_server(oc_cloud_context_t *ctx, const oc_cloud_endpoint_t *ce)
{
return oc_cloud_endpoint_remove(&ctx->store.ci_servers,
oc_string_view(uri, uri_len));
return oc_cloud_endpoint_remove(&ctx->store.ci_servers, ce);
}

void
Expand All @@ -276,12 +279,16 @@ oc_cloud_iterate_servers(const oc_cloud_context_t *ctx,
oc_cloud_endpoints_iterate(&ctx->store.ci_servers, fn, data);
}

void
bool
oc_cloud_select_server(oc_cloud_context_t *ctx,
const oc_cloud_endpoint_t *server)
{
assert(oc_list_has_item(ctx->store.ci_servers.endpoints, server));
if (!oc_list_has_item(ctx->store.ci_servers.endpoints, server)) {
return false;
}
ctx->store.ci_servers.selected = server;
// TODO: dump to storage on selection change
return true;
}

#endif /* OC_CLOUD */
Loading

0 comments on commit cdfa775

Please sign in to comment.