diff options
Diffstat (limited to 'analysis/rtt/src/mqtt_rtt_logger.c')
| -rw-r--r-- | analysis/rtt/src/mqtt_rtt_logger.c | 117 |
1 files changed, 117 insertions, 0 deletions
diff --git a/analysis/rtt/src/mqtt_rtt_logger.c b/analysis/rtt/src/mqtt_rtt_logger.c new file mode 100644 index 0000000..60e5d2d --- /dev/null +++ b/analysis/rtt/src/mqtt_rtt_logger.c | |||
| @@ -0,0 +1,117 @@ | |||
| 1 | // #include <mosquitto.h> | ||
| 2 | // #include <stdio.h> | ||
| 3 | // #include <stdlib.h> | ||
| 4 | // #include <string.h> | ||
| 5 | // #include <sys/time.h> | ||
| 6 | // #include <time.h> | ||
| 7 | // #include <unistd.h> | ||
| 8 | // | ||
| 9 | // #define BROKER_ADDRESS "192.168.1.101" | ||
| 10 | // #define BROKER_PORT 1883 | ||
| 11 | // #define TOPIC "device/echo/in" | ||
| 12 | // #define CSV_FILE "rtt_throughput_log.csv" | ||
| 13 | // #define WINDOW_SEC 1 | ||
| 14 | // #define MAX_TIMES 1000 | ||
| 15 | // | ||
| 16 | // // Kolejka czasów przyjścia wiadomości (dla throughput) | ||
| 17 | // double msg_times[MAX_TIMES]; | ||
| 18 | // int msg_count = 0; | ||
| 19 | // | ||
| 20 | // // Zwraca aktualny czas w milisekundach | ||
| 21 | // long long current_time_ms() { | ||
| 22 | // struct timeval tv; | ||
| 23 | // gettimeofday(&tv, NULL); | ||
| 24 | // return ((long long)tv.tv_sec * 1000) + (tv.tv_usec / 1000); | ||
| 25 | // } | ||
| 26 | // | ||
| 27 | // // Zwraca aktualny czas w sekundach z ułamkiem | ||
| 28 | // double current_time_sec() { | ||
| 29 | // struct timeval tv; | ||
| 30 | // gettimeofday(&tv, NULL); | ||
| 31 | // return (double)tv.tv_sec + (tv.tv_usec / 1e6); | ||
| 32 | // } | ||
| 33 | // | ||
| 34 | // // Aktualizuje throughput window i zwraca liczbę wiadomości w oknie | ||
| 35 | // int update_throughput_window(double now) { | ||
| 36 | // int i, new_count = 0; | ||
| 37 | // for (i = 0; i < msg_count; i++) { | ||
| 38 | // if (now - msg_times[i] < WINDOW_SEC) { | ||
| 39 | // msg_times[new_count++] = msg_times[i]; | ||
| 40 | // } | ||
| 41 | // } | ||
| 42 | // msg_times[new_count++] = now; | ||
| 43 | // msg_count = new_count; | ||
| 44 | // return msg_count; | ||
| 45 | // } | ||
| 46 | // | ||
| 47 | // // Callback po odebraniu wiadomości | ||
| 48 | // void on_message(struct mosquitto *mosq, void *userdata, | ||
| 49 | // const struct mosquitto_message *msg) { | ||
| 50 | // char *payload = (char *)msg->payload; | ||
| 51 | // long long sent_ms = atoll(payload); | ||
| 52 | // long long received_ms = current_time_ms(); | ||
| 53 | // long long rtt = received_ms - sent_ms; | ||
| 54 | // | ||
| 55 | // double now_sec = current_time_sec(); | ||
| 56 | // int throughput = update_throughput_window(now_sec); | ||
| 57 | // | ||
| 58 | // // Timestamp ISO 8601 | ||
| 59 | // time_t now = time(NULL); | ||
| 60 | // struct tm *tm_info = localtime(&now); | ||
| 61 | // char iso_time[32]; | ||
| 62 | // strftime(iso_time, sizeof(iso_time), "%Y-%m-%dT%H:%M:%S", tm_info); | ||
| 63 | // | ||
| 64 | // // Zapisz do CSV | ||
| 65 | // FILE *f = fopen(CSV_FILE, "a"); | ||
| 66 | // if (f) { | ||
| 67 | // fprintf(f, "%s,%lld,%lld,%lld,%d\n", iso_time, sent_ms, received_ms, rtt, | ||
| 68 | // throughput); | ||
| 69 | // fclose(f); | ||
| 70 | // } | ||
| 71 | // | ||
| 72 | // // Wydruk | ||
| 73 | // printf("RTT: %lld ms | Throughput: %d msg/s\n", rtt, throughput); | ||
| 74 | // } | ||
| 75 | // | ||
| 76 | // int main() { | ||
| 77 | // printf("Start programu\n"); | ||
| 78 | // | ||
| 79 | // FILE *f = fopen(CSV_FILE, "w"); | ||
| 80 | // if (!f) { | ||
| 81 | // perror("Nie można otworzyć pliku CSV"); | ||
| 82 | // return 1; | ||
| 83 | // } | ||
| 84 | // fprintf(f, "timestamp,sent_ms,received_ms,rtt_ms,throughput_msg_per_s\n"); | ||
| 85 | // fclose(f); | ||
| 86 | // printf("Plik CSV utworzony\n"); | ||
| 87 | // | ||
| 88 | // mosquitto_lib_init(); | ||
| 89 | // struct mosquitto *mosq = mosquitto_new(NULL, true, NULL); | ||
| 90 | // if (!mosq) { | ||
| 91 | // fprintf(stderr, "Błąd tworzenia klienta MQTT\n"); | ||
| 92 | // return 1; | ||
| 93 | // } | ||
| 94 | // printf("Klient MQTT utworzony\n"); | ||
| 95 | // | ||
| 96 | // mosquitto_message_callback_set(mosq, on_message); | ||
| 97 | // | ||
| 98 | // int rc = mosquitto_connect(mosq, BROKER_ADDRESS, BROKER_PORT, 60); | ||
| 99 | // if (rc != MOSQ_ERR_SUCCESS) { | ||
| 100 | // fprintf(stderr, "Nie można połączyć się z brokerem MQTT, kod błędu: | ||
| 101 | // %d\n", | ||
| 102 | // rc); | ||
| 103 | // return 1; | ||
| 104 | // } | ||
| 105 | // printf("Połączono z brokerem MQTT\n"); | ||
| 106 | // | ||
| 107 | // mosquitto_subscribe(mosq, NULL, TOPIC, 0); | ||
| 108 | // printf("Subskrypcja tematu %s ustawiona\n", TOPIC); | ||
| 109 | // | ||
| 110 | // printf("Oczekiwanie na wiadomości...\n"); | ||
| 111 | // mosquitto_loop_forever(mosq, -1, 1); | ||
| 112 | // | ||
| 113 | // mosquitto_destroy(mosq); | ||
| 114 | // mosquitto_lib_cleanup(); | ||
| 115 | // | ||
| 116 | // return 0; | ||
| 117 | // } | ||
