From 8575b8327f5b9c85b9f7cbb37e04a24cb5a0aefc Mon Sep 17 00:00:00 2001 From: Jozef Kralik Date: Fri, 17 Mar 2023 16:00:39 +0000 Subject: [PATCH] cloud: expose oc_cloud_access_* functions to public API --- api/cloud/oc_cloud_access.c | 27 ++--- api/cloud/oc_cloud_access_internal.h | 74 -------------- api/cloud/oc_cloud_apis.c | 21 ++-- api/cloud/oc_cloud_context.c | 12 ++- api/cloud/oc_cloud_deregister.c | 3 +- api/cloud/oc_cloud_manager.c | 17 ++-- api/cloud/oc_cloud_rd.c | 3 + api/cloud/unittest/cloud_access_test.cpp | 22 +++-- include/oc_cloud.h | 11 +++ include/oc_cloud_access.h | 119 +++++++++++++++++++++++ 10 files changed, 194 insertions(+), 115 deletions(-) create mode 100644 include/oc_cloud_access.h diff --git a/api/cloud/oc_cloud_access.c b/api/cloud/oc_cloud_access.c index 5bc726e29f..de780cc059 100644 --- a/api/cloud/oc_cloud_access.c +++ b/api/cloud/oc_cloud_access.c @@ -22,6 +22,7 @@ #ifdef OC_CLOUD +#include "oc_cloud_access.h" #include "oc_cloud_access_internal.h" #include "oc_cloud_internal.h" #include "oc_core_res.h" @@ -85,9 +86,9 @@ cloud_tls_add_peer(const oc_endpoint_t *endpoint, int selected_identity_cred_id) /* Internal APIs for accessing the OCF Cloud */ bool -cloud_access_register(oc_cloud_access_conf_t conf, const char *auth_provider, - const char *auth_code, const char *uid, - const char *access_token) +oc_cloud_access_register(oc_cloud_access_conf_t conf, const char *auth_provider, + const char *auth_code, const char *uid, + const char *access_token) { if (conf.endpoint == NULL || conf.handler == NULL || ((auth_provider == NULL || auth_code == NULL) && access_token == NULL)) { @@ -171,8 +172,8 @@ cloud_access_deregister_query(const char *uid, const char *access_token, } bool -cloud_access_deregister(oc_cloud_access_conf_t conf, const char *uid, - const char *access_token) +oc_cloud_access_deregister(oc_cloud_access_conf_t conf, const char *uid, + const char *access_token) { if (conf.endpoint == NULL || conf.handler == NULL) { OC_ERR("Error of input parameters"); @@ -248,22 +249,23 @@ cloud_access_login_out(oc_cloud_access_conf_t conf, const char *uid, } bool -cloud_access_login(oc_cloud_access_conf_t conf, const char *uid, - const char *access_token) +oc_cloud_access_login(oc_cloud_access_conf_t conf, const char *uid, + const char *access_token) { return cloud_access_login_out(conf, uid, access_token, /*is_sign_in*/ true); } bool -cloud_access_logout(oc_cloud_access_conf_t conf, const char *uid, - const char *access_token) +oc_cloud_access_logout(oc_cloud_access_conf_t conf, const char *uid, + const char *access_token) { return cloud_access_login_out(conf, uid, access_token, /*is_sign_in*/ false); } bool -cloud_access_refresh_access_token(oc_cloud_access_conf_t conf, const char *uid, - const char *refresh_token) +oc_cloud_access_refresh_access_token(oc_cloud_access_conf_t conf, + const char *auth_provider, const char *uid, + const char *refresh_token) { if (conf.endpoint == NULL || conf.handler == NULL || uid == NULL || refresh_token == NULL) { @@ -296,6 +298,9 @@ cloud_access_refresh_access_token(oc_cloud_access_conf_t conf, const char *uid, oc_rep_start_root_object(); oc_rep_set_text_string(root, uid, uid); oc_rep_set_text_string(root, di, uuid); + if (auth_provider != NULL) { + oc_rep_set_text_string(root, authprovider, auth_provider); + } oc_rep_set_text_string(root, granttype, OC_RSRVD_GRANT_TYPE_REFRESH_TOKEN); oc_rep_set_text_string(root, refreshtoken, refresh_token); oc_rep_end_root_object(); diff --git a/api/cloud/oc_cloud_access_internal.h b/api/cloud/oc_cloud_access_internal.h index 81e9e9136d..a80536baba 100644 --- a/api/cloud/oc_cloud_access_internal.h +++ b/api/cloud/oc_cloud_access_internal.h @@ -31,32 +31,6 @@ extern "C" { #endif -// Common configuration arguments for cloud_access functions -typedef struct oc_cloud_access_conf_t -{ - oc_endpoint_t *endpoint; ///< cloud endpoint - size_t device; ///< index of the device - int selected_identity_cred_id; ///< selected identity certficate id - oc_response_handler_t handler; ///< response callback - void *user_data; ///< data passed to response callback - uint16_t timeout; ///< timeout for response -} oc_cloud_access_conf_t; - -/** - * @brief Send request to register device to cloud. - * - * @param conf cloud access configuration - * @param auth_provider authorization provider - * @param auth_code authorization code - * @param uid user id - * @param access_token access token - * @return true on success - * false otherwise - */ -bool cloud_access_register(oc_cloud_access_conf_t conf, - const char *auth_provider, const char *auth_code, - const char *uid, const char *access_token); - /** * @brief Generate URI query for deregister request. * @@ -65,54 +39,6 @@ bool cloud_access_register(oc_cloud_access_conf_t conf, oc_string_t cloud_access_deregister_query(const char *uid, const char *access_token, size_t device); -/** - * @brief Send request to deregister device from cloud. - * - * The device must be registered and logged in for this call to succeed. - * - * @param conf cloud access configuration - * @param uid user id - * @param access_token access token - * @return true on success - * false otherwise - */ -bool cloud_access_deregister(oc_cloud_access_conf_t conf, const char *uid, - const char *access_token); -/** - * @brief Send request to sign in the device to the cloud. - * - * @param conf cloud access configuration - * @param uid user id - * @param access_token access token - * @return true on success - * false otherwise - */ -bool cloud_access_login(oc_cloud_access_conf_t conf, const char *uid, - const char *access_token); -/** - * @brief Send request to sign out the device to the cloud. - * - * @param conf cloud access configuration - * @param uid user id - * @param access_token access token - * @return true on success - * false otherwise - */ -bool cloud_access_logout(oc_cloud_access_conf_t conf, const char *uid, - const char *access_token); -/** - * @brief Send request to refresh the device access token to the cloud. - * - * @param conf cloud access configuration - * @param uid user id - * @param refresh_token refresh token - * @return true on success - * false otherwise - */ -bool cloud_access_refresh_access_token(oc_cloud_access_conf_t conf, - const char *uid, - const char *refresh_token); - #ifdef __cplusplus } #endif diff --git a/api/cloud/oc_cloud_apis.c b/api/cloud/oc_cloud_apis.c index fb72fb12c3..e4d1280d48 100644 --- a/api/cloud/oc_cloud_apis.c +++ b/api/cloud/oc_cloud_apis.c @@ -25,7 +25,7 @@ #include "oc_api.h" #include "oc_client_state.h" #include "oc_cloud.h" -#include "oc_cloud_access_internal.h" +#include "oc_cloud_access.h" #include "oc_cloud_context_internal.h" #include "oc_cloud_deregister_internal.h" #include "oc_cloud_internal.h" @@ -121,9 +121,9 @@ cloud_register(oc_cloud_context_t *ctx, oc_cloud_cb_t cb, void *data, .user_data = p, .timeout = timeout, }; - if (cloud_access_register(conf, oc_string(ctx->store.auth_provider), NULL, - oc_string(ctx->store.uid), - oc_string(ctx->store.access_token))) { + if (oc_cloud_access_register(conf, oc_string(ctx->store.auth_provider), NULL, + oc_string(ctx->store.uid), + oc_string(ctx->store.access_token))) { ctx->store.cps = OC_CPS_REGISTERING; return 0; } @@ -180,8 +180,8 @@ cloud_login(oc_cloud_context_t *ctx, oc_cloud_cb_t cb, void *data, } conf.endpoint = ctx->cloud_ep; - if (cloud_access_login(conf, oc_string(ctx->store.uid), - oc_string(ctx->store.access_token))) { + if (oc_cloud_access_login(conf, oc_string(ctx->store.uid), + oc_string(ctx->store.access_token))) { return 0; } @@ -266,8 +266,8 @@ cloud_logout(oc_cloud_context_t *ctx, oc_cloud_cb_t cb, void *data, goto error; } conf.endpoint = ctx->cloud_ep; - if (cloud_access_logout(conf, oc_string(ctx->store.uid), - oc_string(ctx->store.access_token))) { + if (oc_cloud_access_logout(conf, oc_string(ctx->store.uid), + oc_string(ctx->store.access_token))) { return 0; } @@ -325,8 +325,9 @@ cloud_refresh_token(oc_cloud_context_t *ctx, oc_cloud_cb_t cb, void *data, goto error; } conf.endpoint = ctx->cloud_ep; - if (cloud_access_refresh_access_token(conf, oc_string(ctx->store.uid), - oc_string(ctx->store.refresh_token))) { + if (oc_cloud_access_refresh_access_token( + conf, oc_string(ctx->store.auth_provider), oc_string(ctx->store.uid), + oc_string(ctx->store.refresh_token))) { return 0; } diff --git a/api/cloud/oc_cloud_context.c b/api/cloud/oc_cloud_context.c index 6d0a662b06..7caf01a3a5 100644 --- a/api/cloud/oc_cloud_context.c +++ b/api/cloud/oc_cloud_context.c @@ -128,6 +128,12 @@ cloud_context_iterate(cloud_context_iterator_cb_t cb, void *user_data) void cloud_context_clear(oc_cloud_context_t *ctx) +{ + oc_cloud_context_clear(ctx, true); +} + +void +oc_cloud_context_clear(oc_cloud_context_t *ctx, bool dump_async) { assert(ctx != NULL); @@ -142,7 +148,11 @@ cloud_context_clear(oc_cloud_context_t *ctx) ctx->store.cps = 0; ctx->selected_identity_cred_id = -1; ctx->keepalive.ping_timeout = 4; - cloud_store_dump_async(&ctx->store); + if (dump_async) { + cloud_store_dump_async(&ctx->store); + } else { + cloud_store_dump(&ctx->store); + } } size_t diff --git a/api/cloud/oc_cloud_deregister.c b/api/cloud/oc_cloud_deregister.c index 778cfb57ea..28f2d73aca 100644 --- a/api/cloud/oc_cloud_deregister.c +++ b/api/cloud/oc_cloud_deregister.c @@ -22,6 +22,7 @@ #include "oc_api.h" #include "oc_cloud_internal.h" +#include "oc_cloud_access.h" #include "oc_cloud_access_internal.h" #include "oc_cloud_context_internal.h" #include "oc_cloud_deregister_internal.h" @@ -186,7 +187,7 @@ cloud_deregister_by_request(cloud_api_param_t *p, uint16_t timeout, } conf.endpoint = ctx->cloud_ep; - if (cloud_access_deregister( + if (oc_cloud_access_deregister( conf, oc_string(ctx->store.uid), useAccessToken ? oc_string(ctx->store.access_token) : NULL)) { return 0; diff --git a/api/cloud/oc_cloud_manager.c b/api/cloud/oc_cloud_manager.c index 30da5f192c..06bdded958 100644 --- a/api/cloud/oc_cloud_manager.c +++ b/api/cloud/oc_cloud_manager.c @@ -23,7 +23,7 @@ #ifdef OC_CLOUD #include "oc_api.h" -#include "oc_cloud_access_internal.h" +#include "oc_cloud_access.h" #include "oc_cloud_context_internal.h" #include "oc_cloud_internal.h" #include "oc_cloud_manager_internal.h" @@ -422,9 +422,9 @@ cloud_manager_register_async(void *data) goto retry; } conf.endpoint = ctx->cloud_ep; - if (!cloud_access_register(conf, oc_string(ctx->store.auth_provider), NULL, - oc_string(ctx->store.uid), - oc_string(ctx->store.access_token))) { + if (!oc_cloud_access_register(conf, oc_string(ctx->store.auth_provider), NULL, + oc_string(ctx->store.uid), + oc_string(ctx->store.access_token))) { OC_ERR("failed to sent register request to cloud"); goto retry; } @@ -639,8 +639,8 @@ cloud_manager_login_async(void *data) goto retry; } conf.endpoint = ctx->cloud_ep; - if (!cloud_access_login(conf, oc_string(ctx->store.uid), - oc_string(ctx->store.access_token))) { + if (!oc_cloud_access_login(conf, oc_string(ctx->store.uid), + oc_string(ctx->store.access_token))) { OC_ERR("failed to sent sign in request to cloud"); goto retry; } @@ -835,8 +835,9 @@ cloud_manager_refresh_token_async(void *data) } conf.endpoint = ctx->cloud_ep; - if (!cloud_access_refresh_access_token(conf, oc_string(ctx->store.uid), - oc_string(ctx->store.refresh_token))) { + if (!oc_cloud_access_refresh_access_token( + conf, oc_string(ctx->store.auth_provider), oc_string(ctx->store.uid), + oc_string(ctx->store.refresh_token))) { goto retry; } diff --git a/api/cloud/oc_cloud_rd.c b/api/cloud/oc_cloud_rd.c index 72ad416aa7..f677e3e963 100644 --- a/api/cloud/oc_cloud_rd.c +++ b/api/cloud/oc_cloud_rd.c @@ -281,6 +281,9 @@ cloud_rd_reset_context(oc_cloud_context_t *ctx) void oc_cloud_delete_resource(oc_resource_t *res) { + if (res == NULL) { + return; + } oc_cloud_context_t *ctx = oc_cloud_get_context(res->device); if (ctx == NULL) { return; diff --git a/api/cloud/unittest/cloud_access_test.cpp b/api/cloud/unittest/cloud_access_test.cpp index 3798dbb49a..558740b1b3 100644 --- a/api/cloud/unittest/cloud_access_test.cpp +++ b/api/cloud/unittest/cloud_access_test.cpp @@ -22,7 +22,7 @@ #include #include "oc_api.h" -#include "oc_cloud_access_internal.h" +#include "oc_cloud_access.h" #include "oc_cloud_internal.h" #include "oc_endpoint.h" @@ -79,8 +79,8 @@ TEST_F(TestCloudAccess, cloud_access_register_p) /*.user_data = */ nullptr, /*.timeout=*/0, }; - bool ret = cloud_access_register(conf, "auth_provider", "auth_code", "uid", - "access_token"); + bool ret = oc_cloud_access_register(conf, "auth_provider", "auth_code", "uid", + "access_token"); // Then EXPECT_TRUE(ret); @@ -97,7 +97,7 @@ TEST_F(TestCloudAccess, cloud_access_register_f) /*.user_data = */ nullptr, /*.timeout=*/0, }; - bool ret = cloud_access_register(conf, nullptr, nullptr, nullptr, nullptr); + bool ret = oc_cloud_access_register(conf, nullptr, nullptr, nullptr, nullptr); // Then EXPECT_FALSE(ret); @@ -114,7 +114,7 @@ TEST_F(TestCloudAccess, cloud_access_login_p) /*.user_data = */ nullptr, /*.timeout=*/0, }; - bool ret = cloud_access_login(conf, "uid", "access_token"); + bool ret = oc_cloud_access_login(conf, "uid", "access_token"); // Then EXPECT_TRUE(ret); @@ -131,7 +131,7 @@ TEST_F(TestCloudAccess, cloud_access_login_f) /*.user_data = */ nullptr, /*.timeout=*/0, }; - bool ret = cloud_access_login(conf, nullptr, nullptr); + bool ret = oc_cloud_access_login(conf, nullptr, nullptr); // Then EXPECT_FALSE(ret); @@ -148,7 +148,7 @@ TEST_F(TestCloudAccess, cloud_access_logout_p) /*.user_data = */ nullptr, /*.timeout=*/0, }; - bool ret = cloud_access_logout(conf, "uid", "access_token"); + bool ret = oc_cloud_access_logout(conf, "uid", "access_token"); // Then EXPECT_TRUE(ret); @@ -165,7 +165,7 @@ TEST_F(TestCloudAccess, cloud_access_logout_f) /*.user_data = */ nullptr, /*.timeout=*/0, }; - bool ret = cloud_access_logout(conf, nullptr, nullptr); + bool ret = oc_cloud_access_logout(conf, nullptr, nullptr); // Then EXPECT_FALSE(ret); @@ -182,7 +182,8 @@ TEST_F(TestCloudAccess, cloud_access_refresh_access_token_p) /*.user_data = */ nullptr, /*.timeout=*/0, }; - bool ret = cloud_access_refresh_access_token(conf, "uid", "refresh_token"); + bool ret = + oc_cloud_access_refresh_access_token(conf, "apn", "uid", "refresh_token"); // Then EXPECT_TRUE(ret); @@ -199,7 +200,8 @@ TEST_F(TestCloudAccess, cloud_access_refresh_access_token_f) /*.user_data = */ nullptr, /*.timeout=*/0, }; - bool ret = cloud_access_refresh_access_token(conf, nullptr, nullptr); + bool ret = + oc_cloud_access_refresh_access_token(conf, nullptr, nullptr, nullptr); // Then EXPECT_FALSE(ret); diff --git a/include/oc_cloud.h b/include/oc_cloud.h index 357fe0a4c3..3dc8b602b0 100644 --- a/include/oc_cloud.h +++ b/include/oc_cloud.h @@ -355,6 +355,17 @@ void oc_cloud_set_keepalive( oc_cloud_context_t *ctx, oc_cloud_on_keepalive_response_cb_t on_keepalive_response, void *user_data); +/** + * @brief Remove cloud context values, disconnect, and stop the cloud manager, + * without releasing the context. + * + * @param ctx Cloud context to clear, must not be NULL. + * @param dump_async If true, store the context to storage in an asynchronous + * manner; otherwise, perform the dump while executing this function. + */ +OC_API +void oc_cloud_context_clear(oc_cloud_context_t *ctx, bool dump_async); + #ifdef __cplusplus } #endif diff --git a/include/oc_cloud_access.h b/include/oc_cloud_access.h new file mode 100644 index 0000000000..55129fb4d5 --- /dev/null +++ b/include/oc_cloud_access.h @@ -0,0 +1,119 @@ +/**************************************************************************** + * + * Copyright (c) 2019 Intel Corporation + * Copyright 2019 Jozef Kralik All Rights Reserved. + * Copyright 2018 Samsung Electronics All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"), + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, + * either express or implied. See the License for the specific + * language governing permissions and limitations under the License. + * + ****************************************************************************/ + +#ifndef OC_CLOUD_ACCESS_H +#define OC_CLOUD_ACCESS_H + +#include "oc_client_state.h" +#include "oc_endpoint.h" + +#include +#include + +#ifdef __cplusplus +extern "C" { +#endif + +// Common configuration arguments for cloud_access functions +typedef struct oc_cloud_access_conf_t +{ + const oc_endpoint_t *endpoint; ///< cloud endpoint + size_t device; ///< index of the device + int selected_identity_cred_id; ///< selected identity certificate id + oc_response_handler_t handler; ///< response callback + void *user_data; ///< data passed to response callback + uint16_t timeout; ///< timeout for response +} oc_cloud_access_conf_t; + +/** + * @brief Send request to register device to cloud. + * + * @param conf cloud access configuration + * @param auth_provider authorization provider + * @param auth_code authorization code + * @param uid user id + * @param access_token access token + * @return true on success + * false otherwise + */ +OC_API +bool oc_cloud_access_register(oc_cloud_access_conf_t conf, + const char *auth_provider, const char *auth_code, + const char *uid, const char *access_token); + +/** + * @brief Send request to deregister device from cloud. + * + * The device must be registered and logged in for this call to succeed. + * + * @param conf cloud access configuration + * @param uid user id + * @param access_token access token + * @return true on success + * false otherwise + */ +OC_API +bool oc_cloud_access_deregister(oc_cloud_access_conf_t conf, const char *uid, + const char *access_token); +/** + * @brief Send request to sign in the device to the cloud. + * + * @param conf cloud access configuration + * @param uid user id + * @param access_token access token + * @return true on success + * false otherwise + */ +OC_API +bool oc_cloud_access_login(oc_cloud_access_conf_t conf, const char *uid, + const char *access_token); +/** + * @brief Send request to sign out the device to the cloud. + * + * @param conf cloud access configuration + * @param uid user id + * @param access_token access token + * @return true on success + * false otherwise + */ +OC_API +bool oc_cloud_access_logout(oc_cloud_access_conf_t conf, const char *uid, + const char *access_token); +/** + * @brief Send request to refresh the device access token to the cloud. + * + * @param conf cloud access configuration + * @param auth_provider authorization provider + * @param uid user id + * @param refresh_token refresh token + * @return true on success + * false otherwise + */ +OC_API +bool oc_cloud_access_refresh_access_token(oc_cloud_access_conf_t conf, + const char *auth_provider, + const char *uid, + const char *refresh_token); + +#ifdef __cplusplus +} +#endif + +#endif /* OC_CLOUD_INTERNAL_H */