// #include // #include // #include // #include // #include // #include // #include // // #define BROKER_ADDRESS "192.168.1.101" // #define BROKER_PORT 1883 // #define TOPIC "device/echo/in" // #define CSV_FILE "rtt_throughput_log.csv" // #define WINDOW_SEC 1 // #define MAX_TIMES 1000 // // // Kolejka czasów przyjścia wiadomości (dla throughput) // double msg_times[MAX_TIMES]; // int msg_count = 0; // // // Zwraca aktualny czas w milisekundach // long long current_time_ms() { // struct timeval tv; // gettimeofday(&tv, NULL); // return ((long long)tv.tv_sec * 1000) + (tv.tv_usec / 1000); // } // // // Zwraca aktualny czas w sekundach z ułamkiem // double current_time_sec() { // struct timeval tv; // gettimeofday(&tv, NULL); // return (double)tv.tv_sec + (tv.tv_usec / 1e6); // } // // // Aktualizuje throughput window i zwraca liczbę wiadomości w oknie // int update_throughput_window(double now) { // int i, new_count = 0; // for (i = 0; i < msg_count; i++) { // if (now - msg_times[i] < WINDOW_SEC) { // msg_times[new_count++] = msg_times[i]; // } // } // msg_times[new_count++] = now; // msg_count = new_count; // return msg_count; // } // // // Callback po odebraniu wiadomości // void on_message(struct mosquitto *mosq, void *userdata, // const struct mosquitto_message *msg) { // char *payload = (char *)msg->payload; // long long sent_ms = atoll(payload); // long long received_ms = current_time_ms(); // long long rtt = received_ms - sent_ms; // // double now_sec = current_time_sec(); // int throughput = update_throughput_window(now_sec); // // // Timestamp ISO 8601 // time_t now = time(NULL); // struct tm *tm_info = localtime(&now); // char iso_time[32]; // strftime(iso_time, sizeof(iso_time), "%Y-%m-%dT%H:%M:%S", tm_info); // // // Zapisz do CSV // FILE *f = fopen(CSV_FILE, "a"); // if (f) { // fprintf(f, "%s,%lld,%lld,%lld,%d\n", iso_time, sent_ms, received_ms, rtt, // throughput); // fclose(f); // } // // // Wydruk // printf("RTT: %lld ms | Throughput: %d msg/s\n", rtt, throughput); // } // // int main() { // printf("Start programu\n"); // // FILE *f = fopen(CSV_FILE, "w"); // if (!f) { // perror("Nie można otworzyć pliku CSV"); // return 1; // } // fprintf(f, "timestamp,sent_ms,received_ms,rtt_ms,throughput_msg_per_s\n"); // fclose(f); // printf("Plik CSV utworzony\n"); // // mosquitto_lib_init(); // struct mosquitto *mosq = mosquitto_new(NULL, true, NULL); // if (!mosq) { // fprintf(stderr, "Błąd tworzenia klienta MQTT\n"); // return 1; // } // printf("Klient MQTT utworzony\n"); // // mosquitto_message_callback_set(mosq, on_message); // // int rc = mosquitto_connect(mosq, BROKER_ADDRESS, BROKER_PORT, 60); // if (rc != MOSQ_ERR_SUCCESS) { // fprintf(stderr, "Nie można połączyć się z brokerem MQTT, kod błędu: // %d\n", // rc); // return 1; // } // printf("Połączono z brokerem MQTT\n"); // // mosquitto_subscribe(mosq, NULL, TOPIC, 0); // printf("Subskrypcja tematu %s ustawiona\n", TOPIC); // // printf("Oczekiwanie na wiadomości...\n"); // mosquitto_loop_forever(mosq, -1, 1); // // mosquitto_destroy(mosq); // mosquitto_lib_cleanup(); // // return 0; // }