Skip to content

Commit

Permalink
Cloud: Add custom scheduler for improved cloud action handling
Browse files Browse the repository at this point in the history
Enhance scheduling flexibility by enabling the setting of delay
and timeout for actions, including jitter for added randomness.
  • Loading branch information
jkralik authored Jan 15, 2024
1 parent a4ef10f commit c98b759
Show file tree
Hide file tree
Showing 9 changed files with 672 additions and 269 deletions.
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

0 comments on commit c98b759

Please sign in to comment.