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 9, 2024
1 parent e9872ba commit 9314c52
Show file tree
Hide file tree
Showing 19 changed files with 196 additions and 203 deletions.
4 changes: 2 additions & 2 deletions api/client/unittest/clientcbtest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ TEST_F(TestClientCBWithServer, RemoveAsync)

oc_set_delayed_callback(cb, &oc_client_cb_remove_async, 0);
EXPECT_TRUE(oc_has_delayed_callback(cb, &oc_client_cb_remove_async, false));
oc::TestDevice::PoolEventsMsV1(1s);
oc::TestDevice::PoolEventsMsV1(50ms);

EXPECT_FALSE(oc_ri_is_client_cb_valid(cb));
}
Expand All @@ -275,7 +275,7 @@ TEST_F(TestClientCBWithServer, RemoveWithTimeoutAsync)
0);
EXPECT_TRUE(oc_has_delayed_callback(
cb, &oc_client_cb_remove_with_notify_timeout_async, false));
oc::TestDevice::PoolEventsMsV1(1s);
oc::TestDevice::PoolEventsMsV1(50ms);

EXPECT_TRUE(TestClientCB::responseHandlerInvoked);
EXPECT_FALSE(oc_ri_is_client_cb_valid(cb));
Expand Down
26 changes: 3 additions & 23 deletions api/cloud/oc_cloud.c
Original file line number Diff line number Diff line change
Expand Up @@ -158,35 +158,15 @@ cloud_set_cloudconf(oc_cloud_context_t *ctx, const oc_cloud_conf_update_t *data)
data->access_token_len);
}
if (data->ci_server_len > 0) {
oc_set_string(&ctx->store.ci_server, data->ci_server, data->ci_server_len);
oc_cloud_endpoints_reinit(
&ctx->store.ci_servers,
oc_string_view(data->ci_server, data->ci_server_len));
}
if (data->sid_len > 0) {
oc_set_string(&ctx->store.sid, data->sid, data->sid_len);
}
}

void
cloud_set_cloudconf2(oc_cloud_context_t *ctx,
const oc_cloud_conf_update2_t *data)
{
assert(ctx != NULL);
assert(data != NULL);
if (data->ci_servers.endpoints != NULL) {
// TODO
}
if (data->auth_provider.length > 0) {
oc_set_string(&ctx->store.auth_provider, data->auth_provider.data,
data->auth_provider.length);
}
if (data->access_token.length > 0) {
oc_set_string(&ctx->store.access_token, data->access_token.data,
data->access_token.length);
}
if (data->sid.length > 0) {
oc_set_string(&ctx->store.sid, data->sid.data, data->sid.length);
}
}

int
oc_cloud_provision_conf_resource_v1(oc_cloud_context_t *ctx, const char *server,
size_t server_len, const char *access_token,
Expand Down
110 changes: 47 additions & 63 deletions api/cloud/oc_cloud_apis.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@

#include <assert.h>

// cloud_deregister might invoke cloud_refresh_token or cloud_login so we might
// have 2 concurrent allocations per device
// cloud_deregister might invoke oc_cloud_do_refresh_token or oc_cloud_do_login
// so we might have 2 concurrent allocations per device
OC_MEMB(g_api_params, cloud_api_param_t, OC_MAX_NUM_DEVICES * 2);

cloud_api_param_t *
Expand All @@ -59,39 +59,27 @@ free_api_param(cloud_api_param_t *p)
oc_memb_free(&g_api_params, p);
}

// bool
// oc_cloud_set_endpoint(oc_cloud_context_t *ctx)
// {
// assert(ctx->cloud_ep != NULL);
// if (!oc_endpoint_is_empty(ctx->cloud_ep)) {
// return true;
// }

// if (oc_string_to_endpoint(&ctx->store.ci_server, ctx->cloud_ep, NULL) == 0)
// {
// // set device id to cloud endpoint for multiple servers
// ctx->cloud_ep->device = ctx->device;
// return true;
// }

// return false;
// }

int
conv_cloud_endpoint(oc_cloud_context_t *ctx)
bool
oc_cloud_set_endpoint(oc_cloud_context_t *ctx)
{
int ret = 0;
if (ctx->cloud_ep != NULL && oc_endpoint_is_empty(ctx->cloud_ep)) {
ret = oc_string_to_endpoint(&ctx->store.ci_server, ctx->cloud_ep, NULL);
if (ret == 0) {
// set device id to cloud endpoint for multiple servers
ctx->cloud_ep->device = ctx->device;
}
assert(ctx->cloud_ep != NULL);
if (!oc_endpoint_is_empty(ctx->cloud_ep)) {
return true;
}

bool success = false;
const oc_string_t *ep_addr =
oc_cloud_endpoint_selected_address(&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;
success = true;
}
#ifdef OC_DNS_CACHE
oc_dns_clear_cache();
oc_dns_clear_cache();
#endif /* OC_DNS_CACHE */
}
return ret;

return success;
}

#ifdef OC_SECURITY
Expand All @@ -107,8 +95,8 @@ cloud_tls_connected(const oc_endpoint_t *endpoint)
#endif /* OC_SECURITY */

int
cloud_register(oc_cloud_context_t *ctx, oc_cloud_cb_t cb, void *data,
uint16_t timeout)
oc_cloud_do_register(oc_cloud_context_t *ctx, oc_cloud_cb_t cb, void *data,
uint16_t timeout)
{
if ((ctx->store.status & OC_CLOUD_REGISTERED) != 0) {
cb(ctx, ctx->store.status, data);
Expand All @@ -132,13 +120,9 @@ cloud_register(oc_cloud_context_t *ctx, oc_cloud_cb_t cb, void *data,
}

OC_CLOUD_DBG("try register device %zu", ctx->device);
if (oc_string(ctx->store.ci_server) == NULL ||
conv_cloud_endpoint(ctx) != 0) {
cloud_set_last_error(ctx, CLOUD_ERROR_CONNECT);
free_api_param(p);
return -1;
if (!oc_cloud_set_endpoint(ctx)) {
goto error;
}

oc_cloud_access_conf_t conf = {
.endpoint = ctx->cloud_ep,
.device = ctx->device,
Expand All @@ -154,6 +138,7 @@ cloud_register(oc_cloud_context_t *ctx, oc_cloud_cb_t cb, void *data,
return 0;
}

error:
cloud_set_last_error(ctx, CLOUD_ERROR_CONNECT);
free_api_param(p);
return -1;
Expand All @@ -165,12 +150,12 @@ oc_cloud_register(oc_cloud_context_t *ctx, oc_cloud_cb_t cb, void *data)
if (ctx == NULL || cb == NULL) {
return -1;
}
return cloud_register(ctx, cb, data, /*timeout*/ 0);
return oc_cloud_do_register(ctx, cb, data, /*timeout*/ 0);
}

int
cloud_login(oc_cloud_context_t *ctx, oc_cloud_cb_t cb, void *data,
uint16_t timeout)
oc_cloud_do_login(oc_cloud_context_t *ctx, oc_cloud_cb_t cb, void *data,
uint16_t timeout)
{
if ((ctx->store.status & OC_CLOUD_LOGGED_IN) != 0) {
cb(ctx, ctx->store.status, data);
Expand All @@ -194,18 +179,17 @@ cloud_login(oc_cloud_context_t *ctx, oc_cloud_cb_t cb, void *data,
}

OC_CLOUD_DBG("try login device %zu", ctx->device);
if (!oc_cloud_set_endpoint(ctx)) {
goto error;
}
oc_cloud_access_conf_t conf = {
.endpoint = ctx->cloud_ep,
.device = ctx->device,
.selected_identity_cred_id = ctx->selected_identity_cred_id,
.handler = oc_cloud_login_handler,
.user_data = p,
.timeout = timeout,
};
if (conv_cloud_endpoint(ctx) != 0) {
goto error;
}
conf.endpoint = ctx->cloud_ep;

if (oc_cloud_access_login(conf, oc_string(ctx->store.uid),
oc_string(ctx->store.access_token))) {
return 0;
Expand All @@ -223,7 +207,7 @@ oc_cloud_login(oc_cloud_context_t *ctx, oc_cloud_cb_t cb, void *data)
if (ctx == NULL || cb == NULL) {
return -1;
}
return cloud_login(ctx, cb, data, /*timeout*/ 0);
return oc_cloud_do_login(ctx, cb, data, /*timeout*/ 0);
}

int
Expand Down Expand Up @@ -263,8 +247,8 @@ cloud_logout_internal(oc_client_response_t *data)
}

int
cloud_logout(oc_cloud_context_t *ctx, oc_cloud_cb_t cb, void *data,
uint16_t timeout)
oc_cloud_do_logout(oc_cloud_context_t *ctx, oc_cloud_cb_t cb, void *data,
uint16_t timeout)
{
if ((ctx->store.status & OC_CLOUD_LOGGED_IN) == 0) {
OC_CLOUD_ERR("invalid cloud status(%d)", (int)ctx->store.status);
Expand All @@ -281,17 +265,17 @@ cloud_logout(oc_cloud_context_t *ctx, oc_cloud_cb_t cb, void *data,
p->timeout = timeout;

OC_CLOUD_DBG("try logout device %zu", ctx->device);
if (!oc_cloud_set_endpoint(ctx)) {
goto error;
}
oc_cloud_access_conf_t conf = {
.endpoint = ctx->cloud_ep,
.device = ctx->device,
.selected_identity_cred_id = ctx->selected_identity_cred_id,
.handler = cloud_logout_internal,
.user_data = p,
.timeout = timeout,
};
if (conv_cloud_endpoint(ctx) != 0) {
goto error;
}
conf.endpoint = ctx->cloud_ep;
if (oc_cloud_access_logout(conf, oc_string(ctx->store.uid),
oc_string(ctx->store.access_token))) {
return 0;
Expand All @@ -309,7 +293,7 @@ oc_cloud_logout(oc_cloud_context_t *ctx, oc_cloud_cb_t cb, void *data)
if (ctx == NULL || cb == NULL) {
return -1;
}
return cloud_logout(ctx, cb, data, /*timeout*/ 0);
return oc_cloud_do_logout(ctx, cb, data, /*timeout*/ 0);
}

int
Expand All @@ -322,8 +306,8 @@ oc_cloud_deregister(oc_cloud_context_t *ctx, oc_cloud_cb_t cb, void *data)
}

int
cloud_refresh_token(oc_cloud_context_t *ctx, oc_cloud_cb_t cb, void *data,
uint16_t timeout)
oc_cloud_do_refresh_token(oc_cloud_context_t *ctx, oc_cloud_cb_t cb, void *data,
uint16_t timeout)
{
if ((ctx->store.status & OC_CLOUD_REGISTERED) == 0) {
return -1;
Expand All @@ -340,17 +324,17 @@ cloud_refresh_token(oc_cloud_context_t *ctx, oc_cloud_cb_t cb, void *data,
p->timeout = timeout;

OC_CLOUD_DBG("try refresh token for device %zu", ctx->device);
if (!oc_cloud_set_endpoint(ctx)) {
goto error;
}
oc_cloud_access_conf_t conf = {
.endpoint = ctx->cloud_ep,
.device = ctx->device,
.selected_identity_cred_id = ctx->selected_identity_cred_id,
.handler = oc_cloud_refresh_token_handler,
.user_data = p,
.timeout = timeout,
};
if (conv_cloud_endpoint(ctx) != 0) {
goto error;
}
conf.endpoint = ctx->cloud_ep;
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))) {
Expand All @@ -369,7 +353,7 @@ oc_cloud_refresh_token(oc_cloud_context_t *ctx, oc_cloud_cb_t cb, void *data)
if (!ctx || !cb) {
return -1;
}
return cloud_refresh_token(ctx, cb, data, 0);
return oc_cloud_do_refresh_token(ctx, cb, data, 0);
}

int
Expand Down
23 changes: 14 additions & 9 deletions api/cloud/oc_cloud_apis_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,21 @@
extern "C" {
#endif

int conv_cloud_endpoint(oc_cloud_context_t *ctx) OC_NONNULL();
/** @brief Set cloud endpoint from currently selected cloud server address */
bool oc_cloud_set_endpoint(oc_cloud_context_t *ctx) OC_NONNULL();

int cloud_register(oc_cloud_context_t *ctx, oc_cloud_cb_t cb, void *data,
uint16_t timeout) OC_NONNULL(1, 2);
int cloud_login(oc_cloud_context_t *ctx, oc_cloud_cb_t cb, void *data,
uint16_t timeout) OC_NONNULL(1, 2);
int cloud_logout(oc_cloud_context_t *ctx, oc_cloud_cb_t cb, void *data,
uint16_t timeout) OC_NONNULL(1, 2);
int cloud_refresh_token(oc_cloud_context_t *ctx, oc_cloud_cb_t cb, void *data,
uint16_t timeout) OC_NONNULL(1, 2);
/** Execute cloud sign up */
int oc_cloud_do_register(oc_cloud_context_t *ctx, oc_cloud_cb_t cb, void *data,
uint16_t timeout) OC_NONNULL(1, 2);
/** Execute cloud sign in */
int oc_cloud_do_login(oc_cloud_context_t *ctx, oc_cloud_cb_t cb, void *data,
uint16_t timeout) OC_NONNULL(1, 2);
/** Execute cloud sign out */
int oc_cloud_do_logout(oc_cloud_context_t *ctx, oc_cloud_cb_t cb, void *data,
uint16_t timeout) OC_NONNULL(1, 2);
/** Execute refreshing of the cloud access token */
int oc_cloud_do_refresh_token(oc_cloud_context_t *ctx, oc_cloud_cb_t cb,
void *data, uint16_t timeout) OC_NONNULL(1, 2);

/**
* @brief Send a ping over the cloud connected connection
Expand Down
4 changes: 3 additions & 1 deletion api/cloud/oc_cloud_context.c
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,9 @@ oc_cloud_get_at(const oc_cloud_context_t *ctx)
const char *
oc_cloud_get_cis(const oc_cloud_context_t *ctx)
{
return oc_cloud_endpoint_selected_address(&ctx->store.ci_servers).data;
return oc_string_view2(
oc_cloud_endpoint_selected_address(&ctx->store.ci_servers))
.data;
}

const char *
Expand Down
27 changes: 17 additions & 10 deletions api/cloud/oc_cloud_deregister.c
Original file line number Diff line number Diff line change
Expand Up @@ -182,19 +182,25 @@ cloud_deregister_by_request(cloud_api_param_t *p, uint16_t timeout,

oc_cloud_context_t *ctx = p->ctx;
OC_CLOUD_DBG("try deregister device %zu by DELETE request", ctx->device);
if (!oc_cloud_set_endpoint(ctx)) {
goto error;
}
#if OC_INFO_IS_ENABLED
const char *ep_str = "";
oc_string64_t ep = { 0 };
if (oc_endpoint_to_string64(ctx->cloud_ep, &ep)) {
ep_str = oc_string(ep);
}
OC_CLOUD_INFO("Deregistering from %s", ep_str);
#endif /* OC_INFO_IS_ENABLED */
oc_cloud_access_conf_t conf = {
.endpoint = ctx->cloud_ep,
.device = ctx->device,
.selected_identity_cred_id = ctx->selected_identity_cred_id,
.handler = cloud_deregistered_internal,
.user_data = p,
.timeout = timeout,
};
if (oc_string(ctx->store.ci_server) == NULL ||
conv_cloud_endpoint(ctx) != 0) {
goto error;
}
OC_CLOUD_INFO("Deregistering from %s", oc_string(ctx->store.ci_server));
conf.endpoint = ctx->cloud_ep;
if (oc_cloud_access_deregister(
conf, oc_string(ctx->store.uid),
useAccessToken ? oc_string(ctx->store.access_token) : NULL)) {
Expand Down Expand Up @@ -243,7 +249,8 @@ cloud_deregister_refreshed_token_async(void *data)
}

// long access token -> we must login and then deregister without token
if (cloud_login(p->ctx, cloud_deregister_try_logged_in, p, p->timeout) != 0) {
if (oc_cloud_do_login(p->ctx, cloud_deregister_try_logged_in, p,
p->timeout) != 0) {
OC_CLOUD_ERR("Failed to login to cloud for deregister");
free_api_param(p);
cloud_context_clear(p->ctx);
Expand Down Expand Up @@ -321,8 +328,8 @@ cloud_deregister(oc_cloud_context_t *ctx, bool sync, uint16_t timeout,
bool hasRefreshToken = cloud_context_has_refresh_token(ctx) &&
!cloud_context_has_permanent_access_token(ctx);
if (hasRefreshToken) {
if (cloud_refresh_token(ctx, cloud_deregister_try_refreshed_token, p,
timeout) != 0) {
if (oc_cloud_do_refresh_token(ctx, cloud_deregister_try_refreshed_token, p,
timeout) != 0) {
OC_CLOUD_ERR("Failed to refresh token for deregister");
free_api_param(p);
return -1;
Expand All @@ -331,7 +338,7 @@ cloud_deregister(oc_cloud_context_t *ctx, bool sync, uint16_t timeout,
}

// otherwise try full log in
if (cloud_login(ctx, cloud_deregister_try_logged_in, p, timeout) != 0) {
if (oc_cloud_do_login(ctx, cloud_deregister_try_logged_in, p, timeout) != 0) {
OC_CLOUD_ERR("Failed to login to cloud for deregister");
free_api_param(p);
return -1;
Expand Down
Loading

0 comments on commit 9314c52

Please sign in to comment.