simulation of simulator... yeah...
This commit is contained in:
1
simulatesimulator/.gitignore
vendored
Normal file
1
simulatesimulator/.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
||||
main
|
1
simulatesimulator/build.sh
Executable file
1
simulatesimulator/build.sh
Executable file
@ -0,0 +1 @@
|
||||
gcc -o main mqtt.c -lmosquitto main.c
|
104
simulatesimulator/main.c
Normal file
104
simulatesimulator/main.c
Normal file
@ -0,0 +1,104 @@
|
||||
#include <unistd.h>
|
||||
#include <stdio.h>
|
||||
#include <mosquitto.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#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);
|
||||
}
|
93
simulatesimulator/mqtt.c
Normal file
93
simulatesimulator/mqtt.c
Normal file
@ -0,0 +1,93 @@
|
||||
#include <stdio.h>
|
||||
|
||||
#include <errno.h>
|
||||
#include <string.h>
|
||||
|
||||
// https://mosquitto.org/api/files/mosquitto-h.html
|
||||
#include <mosquitto.h>
|
||||
|
||||
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();
|
||||
}
|
4
simulatesimulator/mqtt.h
Normal file
4
simulatesimulator/mqtt.h
Normal file
@ -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);
|
Reference in New Issue
Block a user