| 1 | // SPDX-License-Identifier: BSD-2-Clause |
| 2 | /* |
| 3 | * Copyright (C) 2024 Intel Corporation. |
| 4 | * Author Walfred Tedeschi <[email protected]> |
| 5 | * Copyright (C) 2025 Linutronix GmbH |
| 6 | * Author Kurt Kanzenbach <[email protected]> |
| 7 | */ |
| 8 | |
| 9 | #include <errno.h> |
| 10 | #include <inttypes.h> |
| 11 | #include <stdarg.h> |
| 12 | #include <stdio.h> |
| 13 | #include <stdlib.h> |
| 14 | #include <string.h> |
| 15 | #include <time.h> |
| 16 | |
| 17 | #include "app_config.h" |
| 18 | #ifdef WITH_MQTT |
| 19 | #include <mosquitto.h> |
| 20 | #endif |
| 21 | |
| 22 | #include "config.h" |
| 23 | #include "log_mqtt.h" |
| 24 | #include "stat.h" |
| 25 | #include "thread.h" |
| 26 | #include "utils.h" |
| 27 | |
| 28 | #ifndef WITH_MQTT |
| 29 | struct log_mqtt_thread_context *log_mqtt_thread_create(void) |
| 30 | { |
| 31 | return NULL; |
| 32 | } |
| 33 | |
| 34 | void log_mqtt_thread_wait_for_finish(struct log_mqtt_thread_context *thread_context) |
| 35 | { |
| 36 | } |
| 37 | |
| 38 | void log_mqtt_thread_free(struct log_mqtt_thread_context *thread_context) |
| 39 | { |
| 40 | } |
| 41 | |
| 42 | #else |
| 43 | |
| 44 | static struct statistics statistics_per_period[NUM_FRAME_TYPES]; |
| 45 | |
| 46 | static void log_mqtt_add_traffic_class(struct mosquitto *mosq, const char *mqtt_base_topic_name, |
| 47 | struct statistics *stat, const char *tc) |
| 48 | { |
| 49 | char stat_message[4096] = {}; |
| 50 | int result_pub; |
| 51 | |
| 52 | /* Convert stats to json */ |
| 53 | stat_to_json(stat_message, sizeof(stat_message), stat, tc, mqtt_base_topic_name); |
| 54 | |
| 55 | /* Publish */ |
| 56 | result_pub = mosquitto_publish(mosq, NULL, "testbench", strlen(stat_message), stat_message, |
| 57 | 2, false); |
| 58 | if (result_pub != MOSQ_ERR_SUCCESS) |
| 59 | fprintf(stderr, "Error publishing: %s\n", mosquitto_strerror(result_pub)); |
| 60 | } |
| 61 | |
| 62 | static void log_mqtt_on_connect(struct mosquitto *mosq, void *obj, int reason_code) |
| 63 | { |
| 64 | if (reason_code != 0) |
| 65 | mosquitto_disconnect(mosq); |
| 66 | } |
| 67 | |
| 68 | static void *log_mqtt_thread_routine(void *data) |
| 69 | { |
| 70 | uint64_t period_ns = app_config.stats_collection_interval_ns; |
| 71 | struct log_mqtt_thread_context *mqtt_context = data; |
| 72 | int ret, connect_status; |
| 73 | struct timespec time; |
| 74 | |
| 75 | mosquitto_lib_init(); |
| 76 | |
| 77 | mqtt_context->mosq = mosquitto_new(NULL, true, NULL); |
| 78 | if (mqtt_context->mosq == NULL) { |
| 79 | fprintf(stderr, "MQTTLog Error: Out of memory.\n"); |
| 80 | goto err_mqtt_outof_memory; |
| 81 | } |
| 82 | |
| 83 | connect_status = mosquitto_connect(mqtt_context->mosq, app_config.log_mqtt_broker_ip, |
| 84 | app_config.log_mqtt_broker_port, |
| 85 | app_config.log_mqtt_keep_alive_secs); |
| 86 | if (connect_status != MOSQ_ERR_SUCCESS) { |
| 87 | fprintf(stderr, "MQTTLog Error by connect: %s\n", |
| 88 | mosquitto_strerror(connect_status)); |
| 89 | goto err_mqtt_connect; |
| 90 | } |
| 91 | |
| 92 | mosquitto_connect_callback_set(mqtt_context->mosq, log_mqtt_on_connect); |
| 93 | |
| 94 | ret = mosquitto_loop_start(mqtt_context->mosq); |
| 95 | if (ret != MOSQ_ERR_SUCCESS) { |
| 96 | fprintf(stderr, "Log Via MQTT Error: %s\n", mosquitto_strerror(ret)); |
| 97 | goto err_mqtt_start; |
| 98 | } |
| 99 | |
| 100 | /* |
| 101 | * Send the statistics periodically to the MQTT broker. This thread can run with low |
| 102 | * priority to not influence to Application Tasks that much. |
| 103 | */ |
| 104 | ret = clock_gettime(app_config.application_clock_id, &time); |
| 105 | if (ret) { |
| 106 | fprintf(stderr, "Log Via MQTT: clock_gettime() failed: %s!", strerror(errno)); |
| 107 | goto err_time; |
| 108 | } |
| 109 | |
| 110 | while (!mqtt_context->stop) { |
| 111 | int i; |
| 112 | |
| 113 | increment_period(&time, period_ns); |
| 114 | ret = clock_nanosleep(app_config.application_clock_id, TIMER_ABSTIME, &time, NULL); |
| 115 | if (ret) { |
| 116 | pthread_error(ret, "clock_nanosleep() failed"); |
| 117 | goto err_time; |
| 118 | } |
| 119 | |
| 120 | /* Get latest statistics data */ |
| 121 | stat_get_stats_per_period(statistics_per_period, sizeof(statistics_per_period)); |
| 122 | |
| 123 | /* Publish via MQTT */ |
| 124 | for (i = 0; i < NUM_FRAME_TYPES; i++) { |
| 125 | if (config_is_traffic_class_active(stat_frame_type_to_string(i))) |
| 126 | log_mqtt_add_traffic_class( |
| 127 | mqtt_context->mosq, app_config.log_mqtt_measurement_name, |
| 128 | &statistics_per_period[i], stat_frame_type_to_string(i)); |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | return NULL; |
| 133 | |
| 134 | err_mqtt_outof_memory: |
| 135 | err_mqtt_connect: |
| 136 | err_mqtt_start: |
| 137 | err_time: |
| 138 | if (mqtt_context->mosq) |
| 139 | mosquitto_destroy(mqtt_context->mosq); |
| 140 | mosquitto_lib_cleanup(); |
| 141 | return NULL; |
| 142 | } |
| 143 | |
| 144 | struct log_mqtt_thread_context *log_mqtt_thread_create(void) |
| 145 | { |
| 146 | struct log_mqtt_thread_context *mqtt_context; |
| 147 | int ret = 0; |
| 148 | |
| 149 | if (!app_config.log_mqtt) |
| 150 | return NULL; |
| 151 | |
| 152 | mqtt_context = calloc(1, sizeof(*mqtt_context)); |
| 153 | if (!mqtt_context) |
| 154 | return NULL; |
| 155 | |
| 156 | ret = create_rt_thread(&mqtt_context->mqtt_log_task_id, "LoggerGraph", |
| 157 | app_config.log_mqtt_thread_priority, app_config.log_mqtt_thread_cpu, |
| 158 | log_mqtt_thread_routine, mqtt_context); |
| 159 | |
| 160 | if (ret) |
| 161 | goto err_thread; |
| 162 | |
| 163 | return mqtt_context; |
| 164 | |
| 165 | err_thread: |
| 166 | free(mqtt_context); |
| 167 | return NULL; |
| 168 | } |
| 169 | |
| 170 | void log_mqtt_thread_free(struct log_mqtt_thread_context *thread_context) |
| 171 | { |
| 172 | if (!thread_context) |
| 173 | return; |
| 174 | |
| 175 | if (app_config.log_mqtt) { |
| 176 | if (thread_context->mosq) |
| 177 | mosquitto_destroy(thread_context->mosq); |
| 178 | mosquitto_lib_cleanup(); |
| 179 | } |
| 180 | |
| 181 | free(thread_context); |
| 182 | } |
| 183 | |
| 184 | void log_mqtt_thread_stop(struct log_mqtt_thread_context *thread_context) |
| 185 | { |
| 186 | if (!thread_context) |
| 187 | return; |
| 188 | |
| 189 | thread_context->stop = 1; |
| 190 | pthread_join(thread_context->mqtt_log_task_id, NULL); |
| 191 | } |
| 192 | |
| 193 | void log_mqtt_thread_wait_for_finish(struct log_mqtt_thread_context *thread_context) |
| 194 | { |
| 195 | if (!thread_context) |
| 196 | return; |
| 197 | |
| 198 | pthread_join(thread_context->mqtt_log_task_id, NULL); |
| 199 | } |
| 200 | #endif |