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

Cloud: Add custom scheduler for improved cloud action handling #583

Merged
merged 7 commits into from
Jan 15, 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
23 changes: 13 additions & 10 deletions api/cloud/oc_cloud.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

#include "api/oc_ri_internal.h"
#include "api/oc_ri_server_internal.h"
#include "api/oc_server_api_internal.h"
#include "oc_api.h"
#include "oc_cloud_context_internal.h"
#include "oc_cloud_deregister_internal.h"
Expand Down Expand Up @@ -79,6 +80,10 @@ start_manager(void *user_data)
static void
cloud_manager_restart(oc_cloud_context_t *ctx)
{
if (!ctx->cloud_manager) {
OC_CLOUD_ERR("cloud manager is not running");
return;
}
cloud_manager_stop(ctx);
cloud_deregister_stop(ctx);
oc_remove_delayed_callback(ctx, start_manager);
Expand Down Expand Up @@ -229,11 +234,6 @@ cloud_update_by_resource(oc_cloud_context_t *ctx,
ctx->store.cps = OC_CPS_READYTOREGISTER;
if (ctx->cloud_manager) {
oc_cloud_manager_restart(ctx);
return;
}

if (oc_cloud_manager_start(ctx, ctx->callback, ctx->user_data) != 0) {
OC_CLOUD_ERR("cannot start cloud manager");
}
}

Expand All @@ -255,9 +255,9 @@ cloud_ep_session_event_handler(const oc_endpoint_t *endpoint,
}

ctx->cloud_ep_state = state;
if (ctx->cloud_ep_state == OC_SESSION_DISCONNECTED && ctx->cloud_manager) {
if (ctx->cloud_ep_state == OC_SESSION_DISCONNECTED) {
OC_CLOUD_INFO("Session disconnected");
if ((ctx->store.status & OC_CLOUD_REGISTERED) != 0) {
if ((ctx->store.status & OC_CLOUD_REGISTERED) != 0 && ctx->cloud_manager) {
cloud_manager_restart(ctx);
}
}
Expand All @@ -270,7 +270,7 @@ static void
cloud_interface_up_event_handler(oc_cloud_context_t *ctx, void *user_data)
{
(void)user_data;
if (ctx->store.status == OC_CLOUD_INITIALIZED) {
if (ctx->store.status == OC_CLOUD_INITIALIZED && ctx->cloud_manager) {
cloud_manager_restart(ctx);
}
}
Expand Down Expand Up @@ -329,6 +329,10 @@ cloud_is_deregistering(const oc_cloud_context_t *ctx)
void
oc_cloud_manager_restart(oc_cloud_context_t *ctx)
{
if (!ctx->cloud_manager) {
OC_CLOUD_ERR("cloud manager is not running");
return;
}
OC_CLOUD_DBG("oc_cloud_manager_restart");
#ifdef OC_SESSION_EVENTS
if (ctx->cloud_ep_state == OC_SESSION_CONNECTED) {
Expand All @@ -339,8 +343,7 @@ oc_cloud_manager_restart(oc_cloud_context_t *ctx)
}
}
#endif /* OC_SESSION_EVENTS */
oc_remove_delayed_callback(ctx, restart_manager);
oc_set_delayed_callback(ctx, restart_manager, 0);
oc_reset_delayed_callback(ctx, restart_manager, 0);
}

int
Expand Down
15 changes: 15 additions & 0 deletions api/cloud/oc_cloud_apis.c
Original file line number Diff line number Diff line change
Expand Up @@ -389,4 +389,19 @@ cloud_send_ping(const oc_endpoint_t *endpoint, uint16_t timeout_seconds,
timeout_seconds, handler, LOW_QOS, user_data);
}

const char *
oc_cloud_action_to_str(oc_cloud_action_t action)
{
switch (action) {
case OC_CLOUD_ACTION_REGISTER:
return OC_CLOUD_ACTION_REGISTER_STR;
case OC_CLOUD_ACTION_LOGIN:
return OC_CLOUD_ACTION_LOGIN_STR;
case OC_CLOUD_ACTION_REFRESH_TOKEN:
return OC_CLOUD_ACTION_REFRESH_TOKEN_STR;
default:
return OC_CLOUD_ACTION_UNKNOWN_STR;
}
}

#endif /* OC_CLOUD */
9 changes: 9 additions & 0 deletions api/cloud/oc_cloud_context.c
Original file line number Diff line number Diff line change
Expand Up @@ -210,4 +210,13 @@ oc_cloud_set_keepalive(
ctx->keepalive.user_data = user_data;
}

void
oc_cloud_set_schedule_action(oc_cloud_context_t *ctx,
oc_cloud_schedule_action_cb_t on_schedule_action,
void *user_data)
{
ctx->schedule_action.on_schedule_action = on_schedule_action;
ctx->schedule_action.user_data = user_data;
}

#endif /* OC_CLOUD */
5 changes: 5 additions & 0 deletions api/cloud/oc_cloud_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ extern "C" {
*/
#define RD_PUBLISH_TTL_UNLIMITED 0

#define OC_CLOUD_ACTION_REGISTER_STR "register"
#define OC_CLOUD_ACTION_LOGIN_STR "login"
#define OC_CLOUD_ACTION_REFRESH_TOKEN_STR "refreshtoken"
#define OC_CLOUD_ACTION_UNKNOWN_STR "unknown"

typedef struct cloud_conf_update_t
{
const char *access_token; /**< Access Token resolved with an auth code. */
Expand Down
Loading