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

util: extend oc_endpoint_addresses_t by custom on change callback #618

Merged
merged 2 commits into from
Apr 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
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
17 changes: 9 additions & 8 deletions .github/workflows/sonar-cloud-analysis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,16 @@ jobs:
include:
# cloud (ipv4+tcp) on, collection create on, push on, rfotm on
- build_args: "-DOC_CLOUD_ENABLED=ON -DOC_COLLECTIONS_IF_CREATE_ENABLED=ON -DOC_PUSH_ENABLED=ON -DOC_RESOURCE_ACCESS_IN_RFOTM_ENABLED=ON"
# security off, ipv4 on, collection create on, push on, rfotm on
# security off, ipv4 on, collection create on, push on
- build_args: "-DOC_SECURITY_ENABLED=OFF -DOC_IPV4_ENABLED=ON -DOC_COLLECTIONS_IF_CREATE_ENABLED=ON -DOC_PUSH_ENABLED=ON"
# ipv6 dns on, oscore off, rep realloc on, json encoder on
- build_args: "-DOC_DNS_LOOKUP_IPV6_ENABLED=ON -DOC_OSCORE_ENABLED=OFF -DOC_REPRESENTATION_REALLOC_ENCODING_ENABLED=ON -DOC_JSON_ENCODER_ENABLED=ON"
# ipv6 dns on, oscore off, rep realloc on, json encoder on, introspection IDD off
- build_args: "-DOC_DNS_LOOKUP_IPV6_ENABLED=ON -DOC_OSCORE_ENABLED=OFF -DOC_REPRESENTATION_REALLOC_ENCODING_ENABLED=ON -DOC_JSON_ENCODER_ENABLED=ON -DOC_IDD_API_ENABLED=OFF"
# cloud (ipv4+tcp) on, dynamic allocation off, rfotm on, push off (because it forces dynamic allocation)
- build_args: "-DOC_CLOUD_ENABLED=ON -DOC_DYNAMIC_ALLOCATION_ENABLED=OFF -DOC_RESOURCE_ACCESS_IN_RFOTM_ENABLED=ON"
# security off, dynamic allocation off, push off (because it forces dynamic allocation), json encoder on
- build_args: "-DOC_SECURITY_ENABLED=OFF -DOC_DYNAMIC_ALLOCATION_ENABLED=OFF -DOC_JSON_ENCODER_ENABLED=ON"
# security off, cloud (ipv4+tcp), collection create on, introspection IDD off
- build_args: "-DOC_SECURITY_ENABLED=OFF -DOC_CLOUD_ENABLED=ON -DOC_COLLECTIONS_IF_CREATE_ENABLED=ON -DOC_PUSH_ENABLED=ON -DOC_IDD_API_ENABLED=OFF"
# security off, cloud (ipv4+tcp), dynamic allocation off, push off (because it forces dynamic allocation)
- build_args: "-DOC_SECURITY_ENABLED=OFF -DOC_CLOUD_ENABLED=ON -DOC_DYNAMIC_ALLOCATION_ENABLED=OFF -DOC_PUSH_ENABLED=OFF"

uses: ./.github/workflows/unit-test-with-cfg.yml
with:
Expand Down 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
118 changes: 83 additions & 35 deletions api/cloud/oc_cloud.c
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@ start_manager(void *user_data)
oc_free_endpoint(ctx->cloud_ep);
ctx->cloud_ep = oc_new_endpoint();
ctx->store.status &= ~OC_CLOUD_LOGGED_IN;
ctx->store.cps = (ctx->store.status & OC_CLOUD_REGISTERED) != 0
? OC_CPS_REGISTERED
: OC_CPS_READYTOREGISTER;
cloud_manager_start(ctx);
cloud_manager_cb(ctx);
return OC_EVENT_DONE;
Expand All @@ -92,8 +95,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 +148,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 +180,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 +191,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 +232,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);
Danielius1922 marked this conversation as resolved.
Show resolved Hide resolved
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 +253,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 +262,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 +273,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 +296,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 +449,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,26 +485,58 @@ 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;
}

void
cloud_deinit_devices(size_t devices)
{
for (size_t device = 0; device < devices; ++device) {
cloud_context_deinit(oc_cloud_get_context(device));
}
}

static bool
cloud_init_device(size_t device)
{
if (cloud_context_init(device) == NULL) {
return false;
}

if (oc_cloud_add_resource(oc_core_get_resource_by_index(OCF_D, device)) !=
0 ||
oc_cloud_add_resource(oc_core_get_resource_by_index(OCF_P, 0)) != 0) {
cloud_context_deinit(oc_cloud_get_context(device));
return false;
}
return true;
}

bool
cloud_init_devices(size_t devices)
{
for (size_t device = 0; device < devices; ++device) {
if (!cloud_init_device(device)) {
cloud_deinit_devices(device);
return false;
}
}
return true;
}

bool
oc_cloud_init(void)
{
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) {
if (cloud_context_init(device) == NULL) {
return false;
}
oc_cloud_add_resource(oc_core_get_resource_by_index(OCF_P, 0));
oc_cloud_add_resource(oc_core_get_resource_by_index(OCF_D, device));
if (!cloud_init_devices(oc_core_get_num_devices())) {
oc_ri_on_delete_resource_remove_callback(oc_cloud_delete_resource);
return false;
}
return true;
}
Expand All @@ -508,7 +556,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
Loading