Fix handling of cps when cloud registration failed
#define PING_DELAY 20
#define PING_DELAY_ON_TIMEOUT 3
-#define MAX_RETRY_COUNT (5)
+#define MAX_RETRY_COUNT (4)
struct oc_memb rep_objects_pool = { sizeof(oc_rep_t), 0, 0, 0, 0 };
@@ -103,11 +103,7 @@ is_refresh_token_retry_over(oc_cloud_context_t *ctx)
static bool
is_retry_over(oc_cloud_context_t *ctx)
{
- if (ctx->retry_count < MAX_RETRY_COUNT)
- return false;
-
- reconnect(ctx);
- return true;
+ return ctx->retry_count >= MAX_RETRY_COUNT;
}
static void
@@ -217,10 +213,16 @@ _register_handler(oc_cloud_context_t *ctx, oc_client_response_t *data)
return 0;
error:
+ if (is_retry_over(ctx)) {
+ if (ctx->store.cps != OC_CPS_FAILED) {
ctx->store.cps = OC_CPS_FAILED;
ctx->store.status |= OC_CLOUD_FAILURE;
- if (ctx->last_error == 0) {
- cloud_set_last_error(ctx, CLOUD_ERROR_RESPONSE);
+ oc_notify_observers(ctx->cloud_conf);
+ }
+ } else if (ctx->store.cps != OC_CPS_REGISTERING) {
+ ctx->store.cps = OC_CPS_REGISTERING;
+ ctx->store.status |= OC_CLOUD_FAILURE;
+ oc_notify_observers(ctx->cloud_conf);
}
return -1;
}
@@ -267,7 +269,6 @@ cloud_register(void *data)
if (ctx->store.status == OC_CLOUD_INITIALIZED) {
OC_DBG("[CM] try register(%d)\n", ctx->retry_count);
- ctx->retry_count++;
if (!is_retry_over(ctx)) {
bool cannotConnect = true;
if (oc_string(ctx->store.ci_server) && conv_cloud_endpoint(ctx) == 0 &&
@@ -283,6 +284,9 @@ cloud_register(void *data)
}
oc_set_delayed_callback(data, cloud_register,
session_timeout[ctx->retry_count]);
+ ctx->retry_count++;
+ } else {
+ reconnect(ctx);
}
}
@@ -375,7 +379,6 @@ cloud_login(void *data)
if (ctx->store.status & OC_CLOUD_REGISTERED) {
OC_DBG("[CM] try login (%d)\n", ctx->retry_count);
- ctx->retry_count++;
if (!is_retry_over(ctx)) {
bool cannotConnect = true;
if (conv_cloud_endpoint(ctx) == 0 &&
@@ -389,6 +392,9 @@ cloud_login(void *data)
}
oc_set_delayed_callback(ctx, cloud_login,
session_timeout[ctx->retry_count]);
+ ctx->retry_count++;
+ } else {
+ reconnect(ctx);
}
}
@@ -554,11 +560,13 @@ send_ping(void *data)
}
OC_DBG("[CM] try send ping(%d)\n", ctx->retry_count);
- ctx->retry_count++;
if (!is_retry_over(ctx)) {
if (!oc_send_ping(false, ctx->cloud_ep, 1, send_ping_handler, ctx)) {
cloud_set_last_error(ctx, CLOUD_ERROR_CONNECT);
}
+ ctx->retry_count++;
+ } else {
+ reconnect(ctx);
}
return OC_EVENT_DONE;
