120 lines
3.2 KiB
C
120 lines
3.2 KiB
C
#include <stdlib.h>
|
|
#include <stdio.h>
|
|
#include <string.h>
|
|
#include <inttypes.h>
|
|
|
|
#include "network.h"
|
|
#include "mqtt.h"
|
|
|
|
#include "config.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;
|
|
struct mosquitto *mqtt;
|
|
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);
|
|
}
|
|
|
|
// mqtt
|
|
mqtt = connectMqtt(MQTT_USER, MQTT_PASS);
|
|
|
|
// request every dref from arguments
|
|
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);
|
|
}
|
|
}
|
|
|
|
// read from network
|
|
char msg[DGRAM_MSG_LENGTH + 1];
|
|
char payload[RECV_BUFFER];
|
|
|
|
char mqttTopic[100], mqttPayload[512];
|
|
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)
|
|
{
|
|
// XXX: loop throught multiple drefs
|
|
if ((payloadBytes - 1) % 8 == 0)
|
|
{
|
|
memset(mqttTopic, 0, sizeof(mqttTopic));
|
|
memset(mqttPayload, 0, sizeof(mqttPayload));
|
|
|
|
struct dref_struct_out drefRead;
|
|
memcpy(&drefRead, &payload[1], 8);
|
|
|
|
printf("%d: %f\n", drefRead.dref_sender_index, drefRead.dref_flt_value);
|
|
|
|
sprintf(mqttTopic, "/xplane/rref/%d", drefRead.dref_sender_index);
|
|
sprintf(mqttPayload, "%f", drefRead.dref_flt_value);
|
|
|
|
sendMqtt(mqtt, mqttTopic, mqttPayload, strlen(mqttPayload));
|
|
}
|
|
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?!)
|
|
disconnectMqtt(mqtt);
|
|
closeSocket(xPlaneSocket);
|
|
exit(EXIT_SUCCESS);
|
|
}
|