Skip to content

Commit

Permalink
Send fleet status update on MQTT reconnect (#587)
Browse files Browse the repository at this point in the history
  • Loading branch information
sameerzuberi authored Oct 30, 2024
1 parent 800f0f2 commit df61550
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions iotcored/src/mqtt.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
#include <core_mqtt_config.h>
#include <core_mqtt_serializer.h>
#include <ggl/backoff.h>
#include <ggl/bump_alloc.h>
#include <ggl/core_bus/client.h>
#include <ggl/object.h>
#include <pthread.h>
#include <string.h>
#include <sys/time.h>
Expand Down Expand Up @@ -127,6 +130,7 @@ static GglError establish_connection(void *ctx) {
}

noreturn static void *mqtt_recv_thread_fn(void *arg) {
bool reconnect = false;
while (true) {
// Connect to IoT core with backoff between 10ms->10s.
GglError err
Expand All @@ -143,6 +147,32 @@ noreturn static void *mqtt_recv_thread_fn(void *arg) {
_Exit(1);
}

// Send a fleet status update on reconnection
if (reconnect) {
static uint8_t buffer[10 * sizeof(GglObject)] = { 0 };
GglMap args = GGL_MAP({ GGL_STR("trigger"),
GGL_OBJ_BUF(GGL_STR("RECONNECT")) });
GglBumpAlloc alloc = ggl_bump_alloc_init(GGL_BUF(buffer));
GglObject result;

GglError ret = ggl_call(
GGL_STR("/aws/ggl/gg-fleet-statusd"),
GGL_STR("send_fleet_status_update"),
args,
NULL,
&alloc.alloc,
&result
);

if (ret != 0) {
GGL_LOGE(
"Failed to call send_fleet_status_update on MQTT "
"reconnection: %d.",
ret
);
}
}

MQTTStatus_t mqtt_ret;
MQTTContext_t *ctx = arg;
do {
Expand All @@ -159,6 +189,10 @@ noreturn static void *mqtt_recv_thread_fn(void *arg) {
// Remove all subscriptions. This will force the callers to
// resubscribe.
iotcored_unregister_all_subs();

// Set reconnect flag. Future connections will send a fleet status
// update
reconnect = true;
}
}

Expand Down

0 comments on commit df61550

Please sign in to comment.