|
| 1 | +//! Sample component demonstrating pubsub with AWS IoT Core using SDK Lite |
| 2 | + |
| 3 | +#include <ggl/buffer.h> |
| 4 | +#include <ggl/error.h> |
| 5 | +#include <ggl/ipc/client.h> |
| 6 | +#include <ggl/sdk.h> |
| 7 | +#include <unistd.h> |
| 8 | +#include <stdbool.h> |
| 9 | +#include <stdio.h> |
| 10 | +#include <stdlib.h> |
| 11 | +#include <time.h> |
| 12 | +#include <string.h> |
| 13 | + |
| 14 | +static int message_counter = 0; |
| 15 | +static int successful_publishes = 0; |
| 16 | +static int failed_publishes = 0; |
| 17 | + |
| 18 | +static void get_timestamp(char *buffer, size_t size) { |
| 19 | + time_t now = time(NULL); |
| 20 | + struct tm *tm_info = gmtime(&now); |
| 21 | + strftime(buffer, size, "%Y-%m-%dT%H:%M:%S.000Z", tm_info); |
| 22 | +} |
| 23 | + |
| 24 | +static void response_handler(void *ctx, GglBuffer topic, GglBuffer payload, GgIpcSubscriptionHandle handle) { |
| 25 | + (void) ctx; |
| 26 | + (void) handle; |
| 27 | + char timestamp[32]; |
| 28 | + get_timestamp(timestamp, sizeof(timestamp)); |
| 29 | + |
| 30 | + printf("[%s] [HelloWorldSDKLite] RECEIVED MESSAGE:\n", timestamp); |
| 31 | + printf(" Topic: [%.*s]\n", (int) topic.len, topic.data); |
| 32 | + printf(" Payload: [%.*s]\n", (int) payload.len, payload.data); |
| 33 | + printf(" Message Length: %zu bytes\n", payload.len); |
| 34 | + fflush(stdout); |
| 35 | +} |
| 36 | + |
| 37 | +static void log_publish_success(const char* topic, const char* message) { |
| 38 | + char timestamp[32]; |
| 39 | + get_timestamp(timestamp, sizeof(timestamp)); |
| 40 | + |
| 41 | + successful_publishes++; |
| 42 | + |
| 43 | + printf("[%s] [HelloWorldSDKLite] PUBLISH SUCCESS #%d:\n", timestamp, successful_publishes); |
| 44 | + printf(" Topic: %s\n", topic); |
| 45 | + printf(" Message: %s\n", message); |
| 46 | + printf(" Total Successful: %d\n", successful_publishes); |
| 47 | + printf(" Total Failed: %d\n", failed_publishes); |
| 48 | + printf(" Success Rate: %.1f%%\n", |
| 49 | + (successful_publishes * 100.0) / (successful_publishes + failed_publishes)); |
| 50 | + printf(" ----------------------------------------\n"); |
| 51 | + fflush(stdout); |
| 52 | +} |
| 53 | + |
| 54 | +static void log_publish_failure(const char* topic, const char* message, GglError error) { |
| 55 | + char timestamp[32]; |
| 56 | + get_timestamp(timestamp, sizeof(timestamp)); |
| 57 | + |
| 58 | + failed_publishes++; |
| 59 | + |
| 60 | + printf("[%s] [HelloWorldSDKLite] PUBLISH FAILED #%d:\n", timestamp, failed_publishes); |
| 61 | + printf(" Topic: %s\n", topic); |
| 62 | + printf(" Message: %s\n", message); |
| 63 | + printf(" Error Code: %d\n", error); |
| 64 | + printf(" Total Successful: %d\n", successful_publishes); |
| 65 | + printf(" Total Failed: %d\n", failed_publishes); |
| 66 | + if (successful_publishes + failed_publishes > 0) { |
| 67 | + printf(" Success Rate: %.1f%%\n", |
| 68 | + (successful_publishes * 100.0) / (successful_publishes + failed_publishes)); |
| 69 | + } |
| 70 | + printf(" ----------------------------------------\n"); |
| 71 | + fflush(stdout); |
| 72 | +} |
| 73 | + |
| 74 | +int main(void) { |
| 75 | + char timestamp[32]; |
| 76 | + get_timestamp(timestamp, sizeof(timestamp)); |
| 77 | + |
| 78 | + setvbuf(stdout, NULL, _IONBF, 0); |
| 79 | + |
| 80 | + printf("[%s] [HelloWorldSDKLite] COMPONENT STARTING\n", timestamp); |
| 81 | + printf(" Component: com.example.HelloWorldSDKLite\n"); |
| 82 | + printf(" Version: 1.0.0\n"); |
| 83 | + printf(" Binary: hello-world-sdk-lite\n"); |
| 84 | + printf(" ========================================\n"); |
| 85 | + fflush(stdout); |
| 86 | + |
| 87 | + ggl_sdk_init(); |
| 88 | + |
| 89 | + GglError ret = ggipc_connect(); |
| 90 | + if (ret != GGL_ERR_OK) { |
| 91 | + get_timestamp(timestamp, sizeof(timestamp)); |
| 92 | + printf("[%s] [HelloWorldSDKLite] FATAL: Failed to connect to GG nucleus (error: %d)\n", timestamp, ret); |
| 93 | + fflush(stderr); |
| 94 | + exit(1); |
| 95 | + } |
| 96 | + |
| 97 | + get_timestamp(timestamp, sizeof(timestamp)); |
| 98 | + printf("[%s] [HelloWorldSDKLite] Connected to GG nucleus successfully\n", timestamp); |
| 99 | + fflush(stdout); |
| 100 | + |
| 101 | + GgIpcSubscriptionHandle sub_handle; |
| 102 | + ret = ggipc_subscribe_to_iot_core(GGL_STR("hello"), 0, &response_handler, NULL, &sub_handle); |
| 103 | + if (ret != GGL_ERR_OK) { |
| 104 | + get_timestamp(timestamp, sizeof(timestamp)); |
| 105 | + printf("[%s] [HelloWorldSDKLite] WARNING: Failed to subscribe to IoT Core topic 'hello' (error: %d)\n", timestamp, ret); |
| 106 | + printf(" Continuing with publish-only mode...\n"); |
| 107 | + fflush(stdout); |
| 108 | + } else { |
| 109 | + get_timestamp(timestamp, sizeof(timestamp)); |
| 110 | + printf("[%s] [HelloWorldSDKLite] Successfully subscribed to IoT Core topic 'hello'\n", timestamp); |
| 111 | + fflush(stdout); |
| 112 | + } |
| 113 | + |
| 114 | + get_timestamp(timestamp, sizeof(timestamp)); |
| 115 | + printf("[%s] [HelloWorldSDKLite] Starting publish loop (15 second intervals)\n", timestamp); |
| 116 | + fflush(stdout); |
| 117 | + |
| 118 | + while (true) { |
| 119 | + message_counter++; |
| 120 | + |
| 121 | + // Create detailed message with metadata |
| 122 | + char message[512]; |
| 123 | + get_timestamp(timestamp, sizeof(timestamp)); |
| 124 | + snprintf(message, sizeof(message), |
| 125 | + "{" |
| 126 | + "\"message\":\"Hello from Greengrass SDK Lite!\"," |
| 127 | + "\"component\":\"com.example.HelloWorldSDKLite\"," |
| 128 | + "\"version\":\"1.0.0\"," |
| 129 | + "\"counter\":%d," |
| 130 | + "\"timestamp\": \"%s\"," |
| 131 | + "\"successful_publishes\":%d," |
| 132 | + "\"failed_publishes\":%d" |
| 133 | + "}", |
| 134 | + message_counter, timestamp, successful_publishes, failed_publishes); |
| 135 | + |
| 136 | + ret = ggipc_publish_to_iot_core(GGL_STR("hello"), (GglBuffer){.data = (uint8_t*)message, .len = strlen(message)}, 0); |
| 137 | + if (ret != GGL_ERR_OK) { |
| 138 | + log_publish_failure("hello", message, ret); |
| 139 | + } else { |
| 140 | + log_publish_success("hello", message); |
| 141 | + } |
| 142 | + |
| 143 | + // Log periodic statistics |
| 144 | + if (message_counter % 10 == 0) { |
| 145 | + get_timestamp(timestamp, sizeof(timestamp)); |
| 146 | + printf("[%s] [HelloWorldSDKLite] PERIODIC STATS:\n", timestamp); |
| 147 | + printf(" Messages Sent: %d\n", message_counter); |
| 148 | + printf(" Successful Publishes: %d\n", successful_publishes); |
| 149 | + printf(" Failed Publishes: %d\n", failed_publishes); |
| 150 | + if (message_counter > 0) { |
| 151 | + printf(" Overall Success Rate: %.1f%%\n", |
| 152 | + (successful_publishes * 100.0) / message_counter); |
| 153 | + } |
| 154 | + printf(" ========================================\n"); |
| 155 | + fflush(stdout); |
| 156 | + } |
| 157 | + |
| 158 | + sleep(15); |
| 159 | + } |
| 160 | +} |
0 commit comments