Skip to content

Commit

Permalink
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 27, 2024
1 parent 12011d3 commit 36a649e
Show file tree
Hide file tree
Showing 42 changed files with 779 additions and 250 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ctt-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ jobs:

- name: Archive results
if: success() || failure()
uses: actions/upload-artifact@v3
uses: actions/upload-artifact@v4
with:
name: logs
path: results/*.octt
7 changes: 4 additions & 3 deletions .github/workflows/plgd-device-test-with-cfg.yml
Original file line number Diff line number Diff line change
Expand Up @@ -109,12 +109,13 @@ jobs:
docker run --rm --network=host -v `pwd`/${{ env.CERT_PATH }}:/pki_certs ${{ env.TEST_CLOUD_SERVER_IMAGE }} \
-test.parallel 1 -test.v
- name: Get output file name
- name: Generate file name and artifact name
if: ${{ inputs.coverage }}
id: coverage
run: |
SUFFIX=`echo "-DCMAKE_BUILD_TYPE=${{ inputs.build_type }} ${{ inputs.build_args }} ${{ inputs.name }} -DBUILD_TESTING=ON" | sha1sum | cut -f 1 -d ' '`
echo "filename=coverage-plgd-device-${SUFFIX}.json" >> $GITHUB_OUTPUT
echo "artifact=plgd-device-${SUFFIX}-coverage" >> $GITHUB_OUTPUT
- name: Gather coverage data
if: ${{ inputs.coverage }}
Expand All @@ -129,9 +130,9 @@ jobs:
- name: Upload coverage data
if: ${{ inputs.coverage }}
uses: actions/upload-artifact@v3
uses: actions/upload-artifact@v4
with:
name: plgd-device-coverage
name: ${{ steps.coverage.outputs.artifact }}
path: data/coverage/${{ steps.coverage.outputs.filename }}
if-no-files-found: error
retention-days: 1
Expand Down
7 changes: 4 additions & 3 deletions .github/workflows/plgd-hub-test-with-cfg.yml
Original file line number Diff line number Diff line change
Expand Up @@ -103,12 +103,13 @@ jobs:
- name: Run plgd hub tests image
run: docker run --rm --network=host ${{ inputs.hub_args }} ${{ env.TEST_CLOUD_SERVER_IMAGE }}

- name: Get output file name
- name: Generate file name and artifact name
if: ${{ inputs.coverage }}
id: coverage
run: |
SUFFIX=`echo "-DCMAKE_BUILD_TYPE=${{ inputs.build_type }} ${{ inputs.build_args }} ${{ inputs.args }} ${{ inputs.docker_args }} ${{ inputs.hub_args }} ${{ inputs.name }} -DBUILD_TESTING=ON" | sha1sum | cut -f 1 -d ' '`
echo "filename=coverage-plgd-hub-${SUFFIX}.json" >> $GITHUB_OUTPUT
echo "artifact=plgd-hub-${SUFFIX}-coverage" >> $GITHUB_OUTPUT
- name: Gather coverage data
if: ${{ inputs.coverage }}
Expand All @@ -122,9 +123,9 @@ jobs:
- name: Upload coverage data
if: ${{ inputs.coverage }}
uses: actions/upload-artifact@v3
uses: actions/upload-artifact@v4
with:
name: plgd-hub-coverage
name: ${{ steps.coverage.outputs.artifact }}
path: data/coverage/${{ steps.coverage.outputs.filename }}
if-no-files-found: error
retention-days: 1
Expand Down
7 changes: 4 additions & 3 deletions .github/workflows/sonar-cloud-analysis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -113,9 +113,10 @@ jobs:
build-wrapper-linux-x86-64 --out-dir ${{ env.BUILD_WRAPPER_OUT_DIR }} cmake --build build --verbose --target client-server-static --target all
- name: Get coverage from all tests job
uses: actions/download-artifact@v3
uses: actions/download-artifact@v4
with:
path: tools/
merge-multiple: true
path: tools/coverage/

- name: Install gcovr
run: |
Expand All @@ -126,7 +127,7 @@ 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
gcovr --add-tracefile "coverage/*coverage*.json" --sonarqube --output "coverage.xml" --verbose
- name: Run sonar-scanner
env:
Expand Down
7 changes: 4 additions & 3 deletions .github/workflows/unit-test-with-cfg.yml
Original file line number Diff line number Diff line change
Expand Up @@ -119,12 +119,13 @@ jobs:
cd build
ctest --verbose --label-regex "oc-unittest"
- name: Get output file name
- name: Generate file name and artifact name
if: ${{ inputs.coverage }}
id: coverage
run: |
SUFFIX=`echo "-DCMAKE_BUILD_TYPE=${{ inputs.build_type }} ${{ steps.cmake_flags.outputs.compiler }} ${{ inputs.build_args }} -DBUILD_TESTING=ON" | sha1sum | cut -f 1 -d ' '`
echo "filename=coverage-unix-${SUFFIX}.json" >> $GITHUB_OUTPUT
echo "artifact=unit-test-${SUFFIX}-coverage" >> $GITHUB_OUTPUT
- name: Collect coverage data
if: ${{ inputs.coverage }}
Expand All @@ -135,9 +136,9 @@ jobs:
- name: Upload coverage data
if: ${{ inputs.coverage }}
uses: actions/upload-artifact@v3
uses: actions/upload-artifact@v4
with:
name: unit-test-coverage
name: ${{ steps.coverage.outputs.artifact }}
path: tools/${{ steps.coverage.outputs.filename }}
if-no-files-found: error
retention-days: 1
Expand Down
104 changes: 69 additions & 35 deletions api/cloud/oc_cloud.c
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,11 @@ start_manager(void *user_data)
oc_free_endpoint(ctx->cloud_ep);
ctx->cloud_ep = oc_new_endpoint();
ctx->store.status &= ~OC_CLOUD_LOGGED_IN;
if ((ctx->store.status & OC_CLOUD_REGISTERED) != 0) {
ctx->store.cps = OC_CPS_REGISTERED;
} else {
ctx->store.cps = OC_CPS_READYTOREGISTER;
}
cloud_manager_start(ctx);
cloud_manager_cb(ctx);
return OC_EVENT_DONE;
Expand All @@ -92,8 +97,7 @@ cloud_manager_restart(oc_cloud_context_t *ctx)
}
cloud_manager_stop(ctx);
oc_cloud_deregister_stop(ctx);
oc_remove_delayed_callback(ctx, start_manager);
oc_set_delayed_callback(ctx, start_manager, 0);
oc_reset_delayed_callback(ctx, start_manager, 0);
}

static oc_event_callback_retval_t
Expand Down Expand Up @@ -146,6 +150,14 @@ oc_cloud_close_endpoint(const oc_endpoint_t *ep)
}
}

void
oc_cloud_reset_endpoint(oc_cloud_context_t *ctx)
{
oc_cloud_close_endpoint(ctx->cloud_ep);
memset(ctx->cloud_ep, 0, sizeof(oc_endpoint_t));
ctx->cloud_ep_state = OC_SESSION_DISCONNECTED;
}

int
cloud_reset(size_t device, bool force, bool sync, uint16_t timeout)
{
Expand All @@ -170,7 +182,7 @@ cloud_reset(size_t device, bool force, bool sync, uint16_t timeout)
return 0;
}

void
bool
cloud_set_cloudconf(oc_cloud_context_t *ctx, const oc_cloud_conf_update_t *data)
{
assert(ctx != NULL);
Expand All @@ -181,20 +193,17 @@ cloud_set_cloudconf(oc_cloud_context_t *ctx, const oc_cloud_conf_update_t *data)
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 call oc_endpoint_addresses_reinit, because the deinit might
// deallocate oc_string_t values and relocate memory, thus invalidating the
// oc_string_view
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_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");
}
if (!oc_string_is_null_or_empty(data->ci_server) ||
data->ci_servers != NULL) {
return oc_cloud_endpoint_addresses_set(
&ctx->store.ci_servers, data->ci_server, data->sid,
(oc_endpoint_addresses_rep_t){
.uri_key = OC_STRING_VIEW(OC_ENDPOINT_ADDRESS_URI),
.uuid_key = OC_STRING_VIEW(OC_ENDPOINT_ADDRESS_ID),
.servers = data->ci_servers,
});
}
return true;
}

static oc_string_t
Expand Down Expand Up @@ -225,18 +234,17 @@ oc_cloud_provision_conf_resource(oc_cloud_context_t *ctx, const char *server,
oc_uuid_t sid = OCF_COAPCLOUDCONF_DEFAULT_SID;
if (server_id_len > 0 &&
oc_str_to_uuid_v1(server_id, server_id_len, &sid) != OC_UUID_ID_SIZE) {
OC_ERR("invalid sid(%s)", server_id);
OC_CLOUD_ERR("invalid sid(%s)", server_id);
return -1;
}
size_t auth_provider_len = oc_strnlen_s(auth_provider, OC_MAX_STRING_LENGTH);
if (auth_provider_len >= OC_MAX_STRING_LENGTH) {
return -1;
}

oc_cloud_close_endpoint(ctx->cloud_ep);
memset(ctx->cloud_ep, 0, sizeof(oc_endpoint_t));
ctx->cloud_ep_state = OC_SESSION_DISCONNECTED;
oc_cloud_reset_endpoint(ctx);
oc_cloud_store_reinitialize(&ctx->store);
oc_cloud_registration_context_deinit(&ctx->registration_ctx);
cloud_manager_stop(ctx);
oc_cloud_deregister_stop(ctx);

Expand All @@ -247,6 +255,7 @@ oc_cloud_provision_conf_resource(oc_cloud_context_t *ctx, const char *server,
.access_token = &at,
.ci_server = &s,
.sid = sid,
.ci_servers = NULL,
};

const oc_string_t ap =
Expand All @@ -255,7 +264,9 @@ oc_cloud_provision_conf_resource(oc_cloud_context_t *ctx, const char *server,
data.auth_provider = ≈
}

cloud_set_cloudconf(ctx, &data);
if (!cloud_set_cloudconf(ctx, &data)) {
OC_CLOUD_WRN("update of the cloud configuration encountered an error");
}
cloud_rd_reset_context(ctx);

ctx->store.status = OC_CLOUD_INITIALIZED;
Expand All @@ -264,6 +275,8 @@ oc_cloud_provision_conf_resource(oc_cloud_context_t *ctx, const char *server,
oc_cloud_store_dump_async(&ctx->store);

if (ctx->cloud_manager) {
oc_cloud_registration_context_init(&ctx->registration_ctx,
&ctx->store.ci_servers);
oc_cloud_manager_restart(ctx);
}
return 0;
Expand All @@ -285,19 +298,22 @@ oc_cloud_update_by_resource(oc_cloud_context_t *ctx,
// if deregistering or other cloud API was active then closing of the endpoint
// triggers the handler with timeout error, which ensures that/ the operation
// is interrupted
oc_cloud_close_endpoint(ctx->cloud_ep);
memset(ctx->cloud_ep, 0, sizeof(oc_endpoint_t));
ctx->cloud_ep_state = OC_SESSION_DISCONNECTED;
oc_cloud_reset_endpoint(ctx);
oc_cloud_store_reinitialize(&ctx->store);
oc_cloud_registration_context_deinit(&ctx->registration_ctx);
cloud_manager_stop(ctx);
oc_cloud_deregister_stop(ctx);

cloud_set_cloudconf(ctx, data);
if (!cloud_set_cloudconf(ctx, data)) {
OC_CLOUD_WRN("update of the cloud configuration encountered an error");
}
cloud_rd_reset_context(ctx);

ctx->store.status = OC_CLOUD_INITIALIZED;
ctx->store.cps = OC_CPS_READYTOREGISTER;
if (ctx->cloud_manager) {
oc_cloud_registration_context_init(&ctx->registration_ctx,
&ctx->store.ci_servers);
oc_cloud_manager_restart(ctx);
}
}
Expand Down Expand Up @@ -435,6 +451,8 @@ oc_cloud_manager_start(oc_cloud_context_t *ctx, oc_cloud_cb_t cb, void *data)

cloud_manager_start(ctx);
ctx->cloud_manager = true;
oc_cloud_registration_context_init(&ctx->registration_ctx,
&ctx->store.ci_servers);
#ifdef OC_SESSION_EVENTS
oc_remove_session_event_callback_v1(cloud_ep_session_event_handler, ctx,
false);
Expand Down Expand Up @@ -469,22 +487,24 @@ oc_cloud_manager_stop(oc_cloud_context_t *ctx)
cloud_rd_reset_context(ctx);
cloud_manager_stop(ctx);
oc_cloud_store_reinitialize(&ctx->store);
oc_cloud_close_endpoint(ctx->cloud_ep);
memset(ctx->cloud_ep, 0, sizeof(oc_endpoint_t));
ctx->cloud_ep_state = OC_SESSION_DISCONNECTED;
oc_cloud_reset_endpoint(ctx);
oc_cloud_registration_context_deinit(&ctx->registration_ctx);
ctx->cloud_manager = false;

return 0;
}

bool
oc_cloud_init(void)
static bool
cloud_init_for_devices(size_t devices)
{
if (!oc_ri_on_delete_resource_add_callback(oc_cloud_delete_resource)) {
return false;
}
for (size_t device = 0; device < oc_core_get_num_devices(); ++device) {
for (size_t device = 0; device < devices; ++device) {
if (cloud_context_init(device) == NULL) {
for (size_t i = 0; i < device; ++i) {
oc_cloud_context_t *ctx = oc_cloud_get_context(i);
assert(ctx != NULL); // cloud_context_init for these devices succeeded,
// so the context must exist
cloud_context_deinit(ctx);
}
return false;
}
oc_cloud_add_resource(oc_core_get_resource_by_index(OCF_P, 0));
Expand All @@ -493,6 +513,20 @@ oc_cloud_init(void)
return true;
}

bool
oc_cloud_init(void)
{
if (!oc_ri_on_delete_resource_add_callback(oc_cloud_delete_resource)) {
return false;
}
if (!cloud_init_for_devices(oc_core_get_num_devices())) {
oc_ri_on_delete_resource_remove_callback(oc_cloud_delete_resource);
return false;
}

return true;
}

void
oc_cloud_shutdown(void)
{
Expand All @@ -508,7 +542,7 @@ oc_cloud_shutdown(void)
false);
#endif /* OC_SESSION_EVENTS */
cloud_context_deinit(ctx);
OC_CLOUD_DBG("cloud_shutdown for %d", (int)device);
OC_CLOUD_DBG("cloud_shutdown for %zu", device);
}
oc_ri_on_delete_resource_remove_callback(oc_cloud_delete_resource);
}
Expand Down
Loading

0 comments on commit 36a649e

Please sign in to comment.