diff --git a/.gitignore b/.gitignore index b497ff1..d958e7a 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ bin/ -.vscode/ \ No newline at end of file +.vscode/ +config.h diff --git a/Makefile b/Makefile index d062749..9d9dc98 100644 --- a/Makefile +++ b/Makefile @@ -6,7 +6,7 @@ command: gcc -o ${BIN}/command network.c command.c readref: - gcc -o ${BIN}/readref network.c readref.c + gcc -o ${BIN}/readref network.c mqtt.c -lmosquitto readref.c .PHONY: clean dirs clean: diff --git a/mqtt.c b/mqtt.c new file mode 100644 index 0000000..0964fef --- /dev/null +++ b/mqtt.c @@ -0,0 +1,74 @@ +#include + +#include +#include + +// https://mosquitto.org/api/files/mosquitto-h.html +#include + +struct mosquitto *connectMqtt(char *user, char *pass) +{ + int err; + struct mosquitto *mqtt; + + err = mosquitto_lib_init(); + if (err) + { + fprintf(stderr, "Error initalizing mosquitto lib (%d): %s\n", err, mosquitto_strerror(err)); + return NULL; + } + + mqtt = mosquitto_new(user, true, NULL); + if (mqtt == NULL) + { + fprintf(stderr, "Error creating mosquitto instance (%d): %s\n", errno, strerror(errno)); + return NULL; + } + + err = mosquitto_username_pw_set(mqtt, user, pass); + if (err) + { + fprintf(stderr, "Error setting username & password (%d): %s\n", err, mosquitto_strerror(err)); + return NULL; + } + + err = mosquitto_connect(mqtt, "openhab.sugarland.lan", 1883, 10); + if (err) + { + fprintf(stderr, "Error on connection of type "); + if (err == MOSQ_ERR_ERRNO) + { + fprintf(stderr, "system (%d): %s\n", errno, strerror(errno)); + } + else + { + fprintf(stderr, "mosquitto (%d): %s\n", err, mosquitto_strerror(err)); + } + return NULL; + } + + return mqtt; +} + +int sendMqtt(struct mosquitto *mqtt, char *topic, char *payload, int payloadLength) +{ + int err; + + err = mosquitto_publish(mqtt, NULL, topic, payloadLength, payload, 0, false); + if (err) + { + fprintf(stderr, "Error publishing message (%d): %s\n", err, mosquitto_strerror(err)); + return -1; + } + else + { + return 0; + } +} + +void disconnectMqtt(struct mosquitto *mqtt) +{ + // ignore errors + mosquitto_disconnect(mqtt); + mosquitto_destroy(mqtt); +} \ No newline at end of file diff --git a/mqtt.h b/mqtt.h new file mode 100644 index 0000000..212260a --- /dev/null +++ b/mqtt.h @@ -0,0 +1,3 @@ +struct mosquitto * connectMqtt(char *user, char *pass); +int sendMqtt(struct mosquitto *mqtt, char *topic, char *payload, int payloadLength); +void disconnectMqtt(struct mosquitto *mqtt); \ No newline at end of file diff --git a/readref.c b/readref.c index 9d01b5a..201cd2c 100644 --- a/readref.c +++ b/readref.c @@ -4,6 +4,9 @@ #include #include "network.h" +#include "mqtt.h" + +#include "config.h" struct dref_struct_in { @@ -21,6 +24,7 @@ struct dref_struct_out int main(int argc, char *argv[]) { int xPlaneSocket; + struct mosquitto *mqtt; char *wtf; // check argument @@ -37,8 +41,10 @@ int main(int argc, char *argv[]) exit(EXIT_FAILURE); } - // char drefsToRead[413 * ((argc - 1) / 3)]; - // memset(drefsToRead, 0, sizeof(drefsToRead)); + // 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 @@ -53,13 +59,13 @@ int main(int argc, char *argv[]) { 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]; + + char mqttTopic[100], mqttPayload[512]; while (1) { int payloadBytes = recvFromXPlane(xPlaneSocket, msg, payload, RECV_BUFFER); @@ -74,13 +80,21 @@ int main(int argc, char *argv[]) printf("\n"); if (strcmp(msg, "RREF") == 0) { + // XXX: loop throught multiple drefs if ((payloadBytes - 1) % 8 == 0) { - // XXX: loop + 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 { @@ -99,6 +113,7 @@ int main(int argc, char *argv[]) } // bye bye (and no, I don't care about error anymore; I'll exit anyway?!) + disconnectMqtt(mqtt); closeSocket(xPlaneSocket); exit(EXIT_SUCCESS); }