Skip to content
This repository has been archived by the owner on Feb 5, 2021. It is now read-only.

Commit

Permalink
fix: added minor mqtt conntion handling
Browse files Browse the repository at this point in the history
If the connection is closed by the cloud, the device
will try to reconnect 2 times by default. If this is not
successful the device will reboot. The number of retries
is configurable via the CONFIG_CLOUD_RECONNECT_RETRIES
option.

Signed-off-by: Simen S. Røstad <simen.rostad@nordicsemi.no>
  • Loading branch information
simensrostad committed Mar 25, 2020
1 parent 2898c8b commit e5b6b2c
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
4 changes: 4 additions & 0 deletions Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ config CLOUD_POLL_PRIORITY
int
default 7

config CLOUD_RECONNECT_RETRIES
int "Number of retires after a cloud socket POLLUP"
default 2

endmenu # Cloud socket poll

menu "Cloud codec"
Expand Down
15 changes: 13 additions & 2 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ static struct cloud_backend *cloud_backend;

static bool queued_entries;
static bool cloud_connected;
static int cloud_connect_retries;

static int head_cir_buf;
static int num_queued_entries;
Expand Down Expand Up @@ -619,6 +620,7 @@ void cloud_event_handler(const struct cloud_backend *const backend,
case CLOUD_EVT_CONNECTED:
LOG_INF("CLOUD_EVT_CONNECTED");
cloud_connected = true;
cloud_connect_retries = 0;
cloud_synch();
boot_write_img_confirmed();
k_delayed_work_submit(&mov_timeout_work,
Expand Down Expand Up @@ -718,14 +720,23 @@ void cloud_poll(void)
if ((fds[0].revents & POLLNVAL) == POLLNVAL) {
LOG_ERR("Socket error: POLLNVAL");
LOG_ERR("The cloud socket was unexpectedly closed.");
break;
error_handler(-EIO);
return;
}

if ((fds[0].revents & POLLHUP) == POLLHUP) {
LOG_ERR("Socket error: POLLHUP");
LOG_ERR("Connection was closed by the cloud.");
LOG_ERR("TRYING TO RECONNECT...");
if (cloud_connect_retries >
CONFIG_CLOUD_RECONNECT_RETRIES) {
LOG_ERR("Too many retires, reboot");
error_handler(-EIO);
return;
}
cloud_connect_retries++;
LOG_ERR("cloud connect retries %d",
cloud_connect_retries);
break;
}

if ((fds[0].revents & POLLERR) == POLLERR) {
Expand Down

0 comments on commit e5b6b2c

Please sign in to comment.