From d8fe68679addfcbc496c661140c858deed96e062 Mon Sep 17 00:00:00 2001 From: damage Date: Sat, 17 May 2025 19:56:49 +0200 Subject: [PATCH] simulation of simulator... yeah... --- simulatesimulator/.gitignore | 1 + simulatesimulator/build.sh | 1 + simulatesimulator/main.c | 104 +++++++++++++++++++++++++++++++++++ simulatesimulator/mqtt.c | 93 +++++++++++++++++++++++++++++++ simulatesimulator/mqtt.h | 4 ++ 5 files changed, 203 insertions(+) create mode 100644 simulatesimulator/.gitignore create mode 100755 simulatesimulator/build.sh create mode 100644 simulatesimulator/main.c create mode 100644 simulatesimulator/mqtt.c create mode 100644 simulatesimulator/mqtt.h diff --git a/simulatesimulator/.gitignore b/simulatesimulator/.gitignore new file mode 100644 index 0000000..ba2906d --- /dev/null +++ b/simulatesimulator/.gitignore @@ -0,0 +1 @@ +main diff --git a/simulatesimulator/build.sh b/simulatesimulator/build.sh new file mode 100755 index 0000000..b02bf4b --- /dev/null +++ b/simulatesimulator/build.sh @@ -0,0 +1 @@ +gcc -o main mqtt.c -lmosquitto main.c diff --git a/simulatesimulator/main.c b/simulatesimulator/main.c new file mode 100644 index 0000000..505fac5 --- /dev/null +++ b/simulatesimulator/main.c @@ -0,0 +1,104 @@ +#include +#include +#include +#include +#include + +#include "mqtt.h" +#include "config.h" + +#define MAX_AIRSPEED 385 +#define MAX_MQTT_PAYLOAD_SIZE 512 + +struct mosquitto *mqtt; + +int airspeed = 0; +int heading = 0; + +struct test { + char *foo; +}; + +void onMessage(struct mosquitto *mqtt, void *obj, const struct mosquitto_message *message) { + char *mqttTopic; + char mqttPayload[MAX_MQTT_PAYLOAD_SIZE]; + + if (strcmp(message->topic, "/xplane/meta/rref") == 0) { + //requestRrefFromXPlane(message); + } else if (strcmp(message->topic, "/xplane/meta/cmnd") == 0) { + mqttTopic = NULL; + memset(mqttPayload, 0, sizeof(mqttPayload)); + + printf("CMD: %s\n", (char *)message->payload); + if (strcmp((char *)message->payload, "sim/autopilot/airspeed_up") == 0) { + airspeed++; + if (airspeed > MAX_AIRSPEED) { + airspeed = MAX_AIRSPEED; + } else { + // airspeed changed + mqttTopic = "/xplane/rref/2"; + sprintf(mqttPayload, "%03d", airspeed); + } + printf("Airspeed: %d\n", airspeed); + } else if (strcmp((char *)message->payload, "sim/autopilot/airspeed_down") == 0) { + airspeed--; + if (airspeed < 0) { + airspeed = 0; + } else { + mqttTopic = "/xplane/rref/2"; + sprintf(mqttPayload, "%03d", airspeed); + } + printf("Airspeed: %d\n", airspeed); + } else if (strcmp((char *)message->payload, "sim/autopilot/heading_up") == 0) { + heading++; + if (heading > 360) { + heading = 1; + } + mqttTopic = "/xplane/rref/1"; + sprintf(mqttPayload, "%03d", heading); + printf("Heading: %d\n", heading); + } else if (strcmp((char *)message->payload, "sim/autopilot/heading_down") == 0) { + heading--; + if (heading < 0) { + heading = 359; + } + mqttTopic = "/xplane/rref/1"; + sprintf(mqttPayload, "%03d", heading); + printf("Heading: %d\n", heading); + } else { + printf("unkown command %s\n", (char *)message->payload); + } + + if (mqttTopic != NULL && mqttPayload != NULL) { + sendMqtt(mqtt, mqttTopic, mqttPayload, strlen(mqttPayload)); + } + } else if (strcmp(message->topic, "/xplane/meta/log") == 0) { + printf("Log: %s\n", (char *)message->payload); + } else { + fprintf(stderr, "Unknown topic %s\n", message->topic); + } +} + +void doit(struct test *blah) { + blah->foo = "moep"; +} + +int main() { + struct test bar = { "baz" }; + doit(&bar); + printf("foo is %s\n", bar.foo); + + mqtt = connectMqtt(MQTT_USER1, MQTT_PASS, onMessage); + if (mqtt == NULL) { + return -1; + } + + recvMqtt(mqtt, "/xplane/meta/cmnd"); + + while (1) { + mosquitto_loop(mqtt, 0, 1); + usleep(10 * 1000); + } + + disconnectMqtt(mqtt); +} diff --git a/simulatesimulator/mqtt.c b/simulatesimulator/mqtt.c new file mode 100644 index 0000000..40553e8 --- /dev/null +++ b/simulatesimulator/mqtt.c @@ -0,0 +1,93 @@ +#include + +#include +#include + +// https://mosquitto.org/api/files/mosquitto-h.html +#include + +struct mosquitto *connectMqtt(char *user, char *pass, void (*messageCallback)(struct mosquitto *, void *, const struct mosquitto_message *)) +{ + 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; + } + + // FIXME: I am hard coded :( + 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; + } + + mosquitto_message_callback_set(mqtt, messageCallback); + + 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 to %s (%d): %s\n", topic, err, mosquitto_strerror(err)); + return -1; + } + else + { + return 0; + } +} + +int recvMqtt(struct mosquitto *mqtt, char *topic) +{ + int err; + + err = mosquitto_subscribe(mqtt, NULL, topic, 0); + if (err) { + fprintf(stderr, "Error subscribing to %s (%d): %s", topic, err, mosquitto_strerror(err)); + return -1; + } + else + { + return 0; + } +} + +void disconnectMqtt(struct mosquitto *mqtt) +{ + // ignore errors + mosquitto_disconnect(mqtt); + mosquitto_destroy(mqtt); + mosquitto_lib_cleanup(); +} \ No newline at end of file diff --git a/simulatesimulator/mqtt.h b/simulatesimulator/mqtt.h new file mode 100644 index 0000000..4205426 --- /dev/null +++ b/simulatesimulator/mqtt.h @@ -0,0 +1,4 @@ +struct mosquitto * connectMqtt(char *user, char *pass, void (*messageCallback)(struct mosquitto *, void *, const struct mosquitto_message *)); +int sendMqtt(struct mosquitto *mqtt, char *topic, char *payload, int payloadLength); +int recvMqtt(struct mosquitto *mqtt, char *topic); +void disconnectMqtt(struct mosquitto *mqtt); \ No newline at end of file