This commit is contained in:
damage 2024-01-22 17:35:55 +01:00
parent ace99dfe93
commit 3f73b85e9d

87
readref.c Normal file
View File

@ -0,0 +1,87 @@
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <inttypes.h>
#include "network.h"
struct dref_struct_in {
int32_t dref_freq;
int32_t dref_sender_index; // the index the customer is using to define this dataref
char dref_string[400];
};
struct dref_struct_out {
int32_t dref_sender_index;
float dref_flt_value;
};
int main(int argc, char *argv[]) {
int xPlaneSocket;
char *wtf;
// check argument
if (argc != 1 && (argc - 1) % 3 != 0) {
fprintf(stderr, "Usage: %s [<dataref> <frequency> <id> [<dataref> <frequency> <id> ... ]]\n", argv[0]);
exit(EXIT_FAILURE);
}
// network
xPlaneSocket = createSocket(SRC_PORT);
if (xPlaneSocket < 0) {
exit(EXIT_FAILURE);
}
// char drefsToRead[413 * ((argc - 1) / 3)];
// memset(drefsToRead, 0, sizeof(drefsToRead));
for (int i = 1; i < argc; i = i + 3) {
// prepare struct to send, make sure padding is with NULL bytes
struct dref_struct_in drefToRead = {
.dref_freq = strtoimax(argv[i + 1], &wtf, 10),
.dref_sender_index = strtoimax(argv[i + 2], &wtf, 10)
};
memset(drefToRead.dref_string, 0, sizeof(drefToRead.dref_string));
strcpy(drefToRead.dref_string, argv[i]);
// send payload
if (sendToXPlane(xPlaneSocket, DEST_SERVER, DEST_PORT, "RREF", (char *)&drefToRead, sizeof(drefToRead)) < 0) {
exit(EXIT_FAILURE);
}
// memcpy(&drefsToRead[((i - 1) / 3) * 413], &drefToRead, sizeof(drefToRead));
}
// read from network
char msg[DGRAM_MSG_LENGTH + 1];
char payload[RECV_BUFFER];
while (1) {
int payloadBytes = recvFromXPlane(xPlaneSocket, msg, payload, RECV_BUFFER);
if (payloadBytes > 0) {
printf("Received message type %s\n", msg);
printf("Received message data (%d) ", payloadBytes);
for (int i = 0; i < payloadBytes; i++) {
printf("%02x", payload[i]);
}
printf("\n");
if (strcmp(msg, "RREF") == 0) {
if ((payloadBytes - 1) % 8 == 0) {
// XXX: loop
struct dref_struct_out drefRead;
memcpy(&drefRead, &payload[1], 8);
printf("%d: %f\n", drefRead.dref_sender_index, drefRead.dref_flt_value);
} else {
exit(EXIT_FAILURE);
}
} else {
// unknown to me
}
} else {
exit(EXIT_FAILURE);
}
}
// bye bye (and no, I don't care about error anymore; I'll exit anyway?!)
closeSocket(xPlaneSocket);
exit(EXIT_SUCCESS);
}