diff --git a/iotcored/src/mqtt.c b/iotcored/src/mqtt.c index 893c477f6..0b3c8dea5 100644 --- a/iotcored/src/mqtt.c +++ b/iotcored/src/mqtt.c @@ -15,6 +15,9 @@ #include #include #include +#include +#include +#include #include #include #include @@ -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 @@ -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 { @@ -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; } }