Compare commits

...

16 Commits

Author SHA1 Message Date
8c819ba562 added readme and config example
config example might missing config examples... ?! :D
2024-09-15 21:49:17 +02:00
080cfc8230 only send rrefs if value is changed 2024-02-01 23:54:36 +01:00
b3d99fd94b unique mqtt user 2024-02-01 20:51:01 +01:00
ada95d1d9c fixme 2024-01-29 22:16:43 +01:00
b21ce7d6ac send command to xplane received by mqtt 2024-01-24 22:49:28 +01:00
4042ebb93d output all received drefs 2024-01-24 22:29:48 +01:00
8853b3306a rref requestable via mqtt 2024-01-24 22:08:06 +01:00
0ac6418870 send rref to mqtt 2024-01-22 22:00:14 +01:00
60cfcf0bc7 formatting 2024-01-22 21:20:10 +01:00
f05d2c985c mqtt test client 2024-01-22 21:16:50 +01:00
7134969e2a ignore vscode config files 2024-01-22 17:39:59 +01:00
3f73b85e9d readref 2024-01-22 17:35:55 +01:00
ace99dfe93 send and receive simultaneously
values were ok, EDDB has a mag derivation of ~4° *facepalm*
2024-01-21 19:40:19 +01:00
6c04cdf97f requesting and receiving rref
but currently the value is buggy
2024-01-21 19:01:21 +01:00
56bb116ab9 less magic numbers and generic send function 2024-01-21 13:14:47 +01:00
0104e542e1 split network code into extra file 2024-01-21 12:47:01 +01:00
15 changed files with 779 additions and 188 deletions

3
.gitignore vendored Normal file
View File

@ -0,0 +1,3 @@
bin/
.vscode/
config.h

View File

@ -1,9 +1,12 @@
BIN=bin
all: dirs command
all: dirs command readref
command: command.c
gcc -o ${BIN}/command command.c
command:
gcc -o ${BIN}/command network.c command.c
readref:
gcc -o ${BIN}/readref network.c mqtt.c -lmosquitto readref.c -lm
.PHONY: clean dirs
clean:

7
README.md Normal file
View File

@ -0,0 +1,7 @@
## build
```bash
apt install libmosquitto-dev
cp config.h.example config.h
# edit config.h
make
```

View File

@ -2,82 +2,33 @@
#include <stdio.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h> // BSD compatible
#include <arpa/inet.h>
#include <unistd.h>
#include "network.h"
#include <errno.h>
#include <netdb.h>
#define SERVER "::1"
#define PORT 49000
#define COMMAND_BUFFER 256
char* printable(char *str) {
char *ret = strdup(str);
for (int i = strlen(ret); i >= 0; i--) {
if (ret[i] == '\r' || ret[i] == '\n') {
ret[i] = '\0';
}
}
return ret;
}
void sendCommand(int sock, char *cmd) {
char bytesToSend[COMMAND_BUFFER];
memset(bytesToSend, 0, COMMAND_BUFFER);
strcpy(&bytesToSend[0], "CMND");
strcpy(&bytesToSend[5], cmd);
int numBytesSend = send(sock, bytesToSend, 5 + strlen(cmd), 0);
if (numBytesSend >= 0) {
//printf("Send %d bytes to %s:%d: %s\n", numBytesSend, SERVER, PORT, printable(bytesToSend));
} else {
fprintf(stderr, "Failed to send message to %s:%d (%d): %s", SERVER, PORT, errno, strerror(errno));
exit(EXIT_FAILURE);
}
}
int main(int argc, char *argv[]) {
int xplaneSocket;
struct sockaddr_in6 xplaneAddress;
int err;
int main(int argc, char *argv[])
{
int xPlaneSocket;
// check argument
if (argc != 2) {
if (argc != 2)
{
fprintf(stderr, "Usage: %s <command>\n", argv[0]);
exit(EXIT_FAILURE);
}
// create socket
xplaneSocket = socket(AF_INET6, SOCK_DGRAM, 0);
if (xplaneSocket == -1) {
fprintf(stderr, "Error creating socket (%d): %s\n", errno, strerror(errno));
exit(EXIT_FAILURE);
}
// prepare address struct
xplaneAddress.sin6_family = AF_INET6;
xplaneAddress.sin6_port = htons(PORT);
inet_pton(AF_INET6, SERVER, &xplaneAddress.sin6_addr.s6_addr);
// connect to xplane
err = connect(xplaneSocket, (struct sockaddr *)&xplaneAddress, sizeof(xplaneAddress));
if (err) {
fprintf(stderr, "Error connecting %s:%d (%d): %s\n", SERVER, PORT, err, gai_strerror(err));
// network
xPlaneSocket = createSocket(SRC_PORT);
if (xPlaneSocket < 0)
{
exit(EXIT_FAILURE);
}
// send payload
sendCommand(xplaneSocket, argv[1]);
if (sendToXPlane(xPlaneSocket, DEST_SERVER, DEST_PORT, "CMND", argv[1], strlen(argv[1])) < 0)
{
exit(EXIT_FAILURE);
}
// bye bye (and no, I don't care about error anymore; I'll exit anyway?!)
close(xplaneSocket);
closeSocket(xPlaneSocket);
exit(EXIT_SUCCESS);
}

2
config.h.example Normal file
View File

@ -0,0 +1,2 @@
#define MQTT_USER1 "<user>"
#define MQTT_PASS "<pass>"

93
mqtt.c Normal file
View 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
mqtt.h Normal file
View 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);

126
network.c Normal file
View File

@ -0,0 +1,126 @@
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h> // BSD compatible
#include <arpa/inet.h>
#include <unistd.h>
#include <errno.h>
#include <netdb.h>
#include "network.h"
const int DGRAM_MSG_START = 0;
const int DGRAM_PAYLOAD_START = DGRAM_MSG_START + DGRAM_MSG_LENGTH + DGRAM_NULL_LENGTH;
int sendToXPlane(int sock, char *destAddr, int destPort, char *msg, void *payload, int lengthOfPayload)
{
char bytesToSend[SEND_BUFFER];
struct sockaddr_in6 xPlaneAddress;
memset(bytesToSend, 0, SEND_BUFFER);
// msg is always 4 bytes long
if (strlen(msg) != DGRAM_MSG_LENGTH)
{
return -10;
}
// payload shouldn't exceed buffer
if (lengthOfPayload > (SEND_BUFFER - DGRAM_MSG_LENGTH - DGRAM_NULL_LENGTH))
{
return -11;
}
strcpy(&bytesToSend[DGRAM_MSG_START], msg);
memcpy(&bytesToSend[DGRAM_PAYLOAD_START], payload, lengthOfPayload);
// prepare destination address struct
xPlaneAddress.sin6_family = AF_INET6;
xPlaneAddress.sin6_port = htons(destPort);
inet_pton(AF_INET6, destAddr, &xPlaneAddress.sin6_addr.s6_addr);
int numBytesSend = sendto(sock, bytesToSend, DGRAM_MSG_LENGTH + DGRAM_NULL_LENGTH + lengthOfPayload, 0, (struct sockaddr *)&xPlaneAddress, sizeof(xPlaneAddress));
if (numBytesSend >= 0)
{
return numBytesSend;
}
else
{
fprintf(stderr, "Failed to send message (%d): %s\n", errno, strerror(errno));
return -1;
}
}
int recvFromXPlane(int sock, char msg[DGRAM_MSG_LENGTH], void *payload, int maxLength)
{
char buf[maxLength + DGRAM_MSG_LENGTH];
memset(buf, 0, sizeof(buf));
strcpy(msg, "");
int recvBytes = recv(sock, buf, maxLength + DGRAM_MSG_LENGTH, MSG_DONTWAIT);
if (recvBytes >= 0)
{
if (recvBytes >= DGRAM_MSG_LENGTH)
{
memcpy(msg, &buf[0], DGRAM_MSG_LENGTH);
memcpy(payload, &buf[DGRAM_MSG_LENGTH], maxLength);
return recvBytes - DGRAM_MSG_LENGTH;
}
else
{
fprintf(stderr, "Received data without message header?!\n");
return -11;
}
}
else
{
if (errno != EAGAIN)
{
fprintf(stderr, "Error receiving data (%d): %s\n", errno, strerror(errno));
return -1;
}
else
{
return 0;
}
}
}
int createSocket(int srcPort)
{
int xPlaneSocket;
struct sockaddr_in6 localAddress;
int err;
// create socket
xPlaneSocket = socket(AF_INET6, SOCK_DGRAM, 0);
if (xPlaneSocket == -1)
{
fprintf(stderr, "Error creating socket (%d): %s\n", errno, strerror(errno));
return -1;
}
// prepare source address struct
localAddress.sin6_family = AF_INET6;
localAddress.sin6_port = htons(srcPort);
localAddress.sin6_addr = in6addr_any;
// bind to local port
err = bind(xPlaneSocket, (struct sockaddr *)&localAddress, sizeof(localAddress));
if (err)
{
fprintf(stderr, "Error binding [::]:%d (%d): %s\n", srcPort, errno, gai_strerror(errno));
return -1;
}
return xPlaneSocket;
}
void closeSocket(int sock)
{
close(sock);
}

17
network.h Normal file
View File

@ -0,0 +1,17 @@
#define DEST_SERVER "::1"
#define DEST_PORT 49000
#define SRC_PORT 49666
#define SEND_BUFFER 512
#define RECV_BUFFER 256
#define DGRAM_MSG_LENGTH 4
#define DGRAM_NULL_LENGTH 1
extern const int DGRAM_MSG_START;
extern const int DGRAM_PAYLOAD_START;
int sendToXPlane(int sock, char *destAddr, int destPort, char *msg, void *payload, int lengthOfPayload);
int recvFromXPlane(int sock, char msg[4], void *payload, int maxLength);
int createSocket(int srcPort);
void closeSocket(int sock);

241
readref.c Normal file
View File

@ -0,0 +1,241 @@
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <inttypes.h>
#include <math.h>
#include <mosquitto.h>
#include "network.h"
#include "mqtt.h"
#include "config.h"
#define MQTT_RREF_REQUEST_LENGTH_ID 3
#define MQTT_RREF_REQUEST_LENGTH_FREQ 3
#define MQTT_RREF_REQUEST_LENGTH_SEPARATOR 1
#define MQTT_RREF_REQUEST_SEPARATOR ' '
const int MQTT_RREF_REQUEST_START_ID = 0;
const int MQTT_RREF_REQUEST_START_FREQ = MQTT_RREF_REQUEST_START_ID + MQTT_RREF_REQUEST_LENGTH_ID + MQTT_RREF_REQUEST_LENGTH_SEPARATOR;
const int MQTT_RREF_REQUEST_START_RREF = MQTT_RREF_REQUEST_START_FREQ + MQTT_RREF_REQUEST_LENGTH_FREQ + MQTT_RREF_REQUEST_LENGTH_SEPARATOR;
const int MQTT_RREF_REQUEST_MIN_LENGTH = MQTT_RREF_REQUEST_LENGTH_ID + MQTT_RREF_REQUEST_LENGTH_FREQ + (2 * MQTT_RREF_REQUEST_LENGTH_SEPARATOR) + 1; // at least one char for rref
int xPlaneSocket;
struct mosquitto *mqtt;
struct valueCacheElement
{
float value;
bool force;
};
struct valueCacheElement *valueCache = NULL;
int valueCacheCount = 0;
#define XPLANE_RREF_MAX_LENGTH 400
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[XPLANE_RREF_MAX_LENGTH];
};
struct dref_struct_out
{
int32_t dref_sender_index;
float dref_flt_value;
};
int *strtoint(char *in, size_t start, size_t n)
{
int *ret = malloc(sizeof(int));
*ret = 0;
int offset = start + n - 1;
if (offset < 0)
{
return NULL;
}
for (int i = 0; i < n; i++)
{
int position = offset - i;
if (in[position] >= '0' && in[position] <= '9')
{
*ret += (in[position] - '0') * pow(10, i);
}
else
{
return NULL;
}
}
return ret;
}
void requestRrefFromXPlane(const struct mosquitto_message *message)
{
int *index, *frequency;
if (message->payloadlen >= MQTT_RREF_REQUEST_MIN_LENGTH)
{
index = strtoint(message->payload, MQTT_RREF_REQUEST_START_ID, MQTT_RREF_REQUEST_LENGTH_ID);
frequency = strtoint(message->payload, MQTT_RREF_REQUEST_START_FREQ, MQTT_RREF_REQUEST_LENGTH_FREQ);
if (index == NULL || frequency == NULL)
{
return;
}
if (*index < valueCacheCount)
{
valueCache[*index].force = true;
}
// prepare struct to send, make sure padding is with NULL bytes
struct dref_struct_in drefToRead = {
.dref_freq = *frequency,
.dref_sender_index = *index};
memset(drefToRead.dref_string, 0, sizeof(drefToRead.dref_string));
strncpy(drefToRead.dref_string, &((char *)message->payload)[MQTT_RREF_REQUEST_START_RREF], message->payloadlen - MQTT_RREF_REQUEST_START_RREF);
sendToXPlane(xPlaneSocket, DEST_SERVER, DEST_PORT, "RREF", (char *)&drefToRead, sizeof(drefToRead));
}
}
void sendCommandToXPlane(const struct mosquitto_message *message)
{
char *command;
if (message->payloadlen > 0)
{
sendToXPlane(xPlaneSocket, DEST_SERVER, DEST_PORT, "CMND", (char *)message->payload, message->payloadlen);
}
}
void onMessage(struct mosquitto *mqtt, void *obj, const struct mosquitto_message *message)
{
printf("Received message %.*s on topic %s\n", message->payloadlen, (char *)message->payload, message->topic);
if (strcmp(message->topic, "/xplane/meta/rref") == 0)
{
requestRrefFromXPlane(message);
}
else if (strcmp(message->topic, "/xplane/meta/cmnd") == 0)
{
sendCommandToXPlane(message);
}
else
{
fprintf(stderr, "Unkown topic %s\n", message->topic);
}
}
int main()
{
// network
xPlaneSocket = createSocket(SRC_PORT);
if (xPlaneSocket < 0)
{
exit(EXIT_FAILURE);
}
// mqtt
mqtt = connectMqtt(MQTT_USER1, MQTT_PASS, onMessage);
if (mqtt == NULL)
{
exit(EXIT_FAILURE);
}
// subscribe meta channel
recvMqtt(mqtt, "/xplane/meta/#");
// 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)
{
if (strcmp(msg, "RREF") == 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");
// continue if payloadBytes is valid (1 byte + 8 bytes each dref)
if ((payloadBytes - 1) % sizeof(struct dref_struct_out) == 0)
{
// loop throught all provided drefs
for (int i = 1; i < payloadBytes; i += sizeof(struct dref_struct_out))
{
memset(mqttTopic, 0, sizeof(mqttTopic));
memset(mqttPayload, 0, sizeof(mqttPayload));
struct dref_struct_out drefRead;
memcpy(&drefRead, &payload[i], sizeof(struct dref_struct_out));
// add new element to array
if (valueCache == NULL)
{
valueCacheCount = drefRead.dref_sender_index + 10;
valueCache = calloc(valueCacheCount, sizeof(struct valueCacheElement));
}
else if (drefRead.dref_sender_index >= valueCacheCount)
{
valueCacheCount = drefRead.dref_sender_index + 10;
valueCache = reallocarray(valueCache, valueCacheCount, sizeof(struct valueCacheElement));
}
else
{
// size of array is sufficient
}
if (valueCache[drefRead.dref_sender_index].force || valueCache[drefRead.dref_sender_index].value != drefRead.dref_flt_value)
{
valueCache[drefRead.dref_sender_index].value = drefRead.dref_flt_value;
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));
valueCache[drefRead.dref_sender_index].force = false;
}
else
{
// neither changed nor forced
}
}
}
else
{
exit(EXIT_FAILURE);
}
}
else
{
// unknown to me
}
}
else
{
exit(EXIT_FAILURE);
}
mosquitto_loop(mqtt, 0, 1);
}
// bye bye (and no, I don't care about error anymore; I'll exit anyway?!)
disconnectMqtt(mqtt);
closeSocket(xPlaneSocket);
exit(EXIT_SUCCESS);
}

3
test/.gitignore vendored
View File

@ -1,2 +1,5 @@
airspeed
heading
mqtt
mqtt.h
strtoint

View File

@ -15,11 +15,14 @@
#define COMMAND_BUFFER 256
char* printable(char *str) {
char *printable(char *str)
{
char *ret = strdup(str);
for (int i = strlen(ret); i >= 0; i--) {
if (ret[i] == '\r' || ret[i] == '\n') {
for (int i = strlen(ret); i >= 0; i--)
{
if (ret[i] == '\r' || ret[i] == '\n')
{
ret[i] = '\0';
}
}
@ -27,7 +30,8 @@ char* printable(char *str) {
return ret;
}
void sendCommand(int sock, char *cmd) {
void sendCommand(int sock, char *cmd)
{
char bytesToSend[COMMAND_BUFFER];
memset(bytesToSend, 0, COMMAND_BUFFER);
@ -35,29 +39,35 @@ void sendCommand(int sock, char *cmd) {
strcpy(&bytesToSend[5], cmd);
int numBytesSend = send(sock, bytesToSend, 5 + strlen(cmd), 0);
if (numBytesSend >= 0) {
//printf("Send %d bytes to %s:%d: %s\n", numBytesSend, SERVER, PORT, printable(bytesToSend));
} else {
if (numBytesSend >= 0)
{
// printf("Send %d bytes to %s:%d: %s\n", numBytesSend, SERVER, PORT, printable(bytesToSend));
}
else
{
fprintf(stderr, "Failed to send message to %s:%d (%d): %s", SERVER, PORT, errno, strerror(errno));
exit(EXIT_FAILURE);
}
}
int main(int argc, char *argv[]) {
int main(int argc, char *argv[])
{
int xplaneSocket;
struct sockaddr_in6 xplaneAddress;
int err;
// check argument
if (argc != 2 || (strcmp(argv[1], "up") != 0 && strcmp(argv[1], "down") != 0)) {
if (argc != 2 || (strcmp(argv[1], "up") != 0 && strcmp(argv[1], "down") != 0))
{
fprintf(stderr, "Usage: %s <up|down>\n", argv[0]);
exit(EXIT_FAILURE);
}
// create socket
xplaneSocket = socket(AF_INET6, SOCK_DGRAM, 0);
if (xplaneSocket == -1) {
if (xplaneSocket == -1)
{
fprintf(stderr, "Error creating socket (%d): %s\n", errno, strerror(errno));
exit(EXIT_FAILURE);
}
@ -69,17 +79,23 @@ int main(int argc, char *argv[]) {
// connect to xplane
err = connect(xplaneSocket, (struct sockaddr *)&xplaneAddress, sizeof(xplaneAddress));
if (err) {
if (err)
{
fprintf(stderr, "Error connecting %s:%d (%d): %s\n", SERVER, PORT, err, gai_strerror(err));
exit(EXIT_FAILURE);
}
// send payload
if (strcmp(argv[1], "up") == 0) {
if (strcmp(argv[1], "up") == 0)
{
sendCommand(xplaneSocket, "sim/autopilot/airspeed_up");
} else if (strcmp(argv[1], "down") == 0) {
}
else if (strcmp(argv[1], "down") == 0)
{
sendCommand(xplaneSocket, "sim/autopilot/airspeed_down");
} else {
}
else
{
exit(EXIT_FAILURE);
}

View File

@ -15,11 +15,14 @@
#define COMMAND_BUFFER 256
char* printable(char *str) {
char *printable(char *str)
{
char *ret = strdup(str);
for (int i = strlen(ret); i >= 0; i--) {
if (ret[i] == '\r' || ret[i] == '\n') {
for (int i = strlen(ret); i >= 0; i--)
{
if (ret[i] == '\r' || ret[i] == '\n')
{
ret[i] = '\0';
}
}
@ -27,7 +30,8 @@ char* printable(char *str) {
return ret;
}
void sendCommand(int sock, char *cmd) {
void sendCommand(int sock, char *cmd)
{
char bytesToSend[COMMAND_BUFFER];
memset(bytesToSend, 0, COMMAND_BUFFER);
@ -35,29 +39,35 @@ void sendCommand(int sock, char *cmd) {
strcpy(&bytesToSend[5], cmd);
int numBytesSend = send(sock, bytesToSend, 5 + strlen(cmd), 0);
if (numBytesSend >= 0) {
//printf("Send %d bytes to %s:%d: %s\n", numBytesSend, SERVER, PORT, printable(bytesToSend));
} else {
if (numBytesSend >= 0)
{
// printf("Send %d bytes to %s:%d: %s\n", numBytesSend, SERVER, PORT, printable(bytesToSend));
}
else
{
fprintf(stderr, "Failed to send message to %s:%d (%d): %s", SERVER, PORT, errno, strerror(errno));
exit(EXIT_FAILURE);
}
}
int main(int argc, char *argv[]) {
int main(int argc, char *argv[])
{
int xplaneSocket;
struct sockaddr_in6 xplaneAddress;
int err;
// check argument
if (argc != 2 || (strcmp(argv[1], "up") != 0 && strcmp(argv[1], "down") != 0)) {
if (argc != 2 || (strcmp(argv[1], "up") != 0 && strcmp(argv[1], "down") != 0))
{
fprintf(stderr, "Usage: %s <up|down>\n", argv[0]);
exit(EXIT_FAILURE);
}
// create socket
xplaneSocket = socket(AF_INET6, SOCK_DGRAM, 0);
if (xplaneSocket == -1) {
if (xplaneSocket == -1)
{
fprintf(stderr, "Error creating socket (%d): %s\n", errno, strerror(errno));
exit(EXIT_FAILURE);
}
@ -69,17 +79,23 @@ int main(int argc, char *argv[]) {
// connect to xplane
err = connect(xplaneSocket, (struct sockaddr *)&xplaneAddress, sizeof(xplaneAddress));
if (err) {
if (err)
{
fprintf(stderr, "Error connecting %s:%d (%d): %s\n", SERVER, PORT, err, gai_strerror(err));
exit(EXIT_FAILURE);
}
// send payload
if (strcmp(argv[1], "up") == 0) {
if (strcmp(argv[1], "up") == 0)
{
sendCommand(xplaneSocket, "sim/autopilot/heading_up");
} else if (strcmp(argv[1], "down") == 0) {
}
else if (strcmp(argv[1], "down") == 0)
{
sendCommand(xplaneSocket, "sim/autopilot/heading_down");
} else {
}
else
{
exit(EXIT_FAILURE);
}

65
test/mqtt.c Normal file
View File

@ -0,0 +1,65 @@
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
// https://mosquitto.org/api/files/mosquitto-h.html
#include <mosquitto.h>
#include "mqtt.h"
int main(int argc, char *argv[])
{
int err;
struct mosquitto *mqtt;
err = mosquitto_lib_init();
if (err)
{
fprintf(stderr, "Error initalizing mosquitto lib (%d): %s\n", err, mosquitto_strerror(err));
exit(EXIT_FAILURE);
}
mqtt = mosquitto_new(MQTT_USER, true, NULL);
if (mqtt == NULL)
{
fprintf(stderr, "Error creating mosquitto instance (%d): %s\n", errno, strerror(errno));
exit(EXIT_FAILURE);
}
err = mosquitto_username_pw_set(mqtt, MQTT_USER, MQTT_PASS);
if (err)
{
fprintf(stderr, "Error setting username & password (%d): %s\n", err, mosquitto_strerror(err));
exit(EXIT_FAILURE);
}
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));
}
exit(EXIT_FAILURE);
}
char *foo = "blah";
err = mosquitto_publish(mqtt, NULL, "/xplane/foo", strlen(foo), foo, 0, false);
if (err)
{
fprintf(stderr, "Error publishing message (%d): %s\n", err, mosquitto_strerror(err));
exit(EXIT_FAILURE);
}
err = mosquitto_disconnect(mqtt);
// ignore error
mosquitto_destroy(mqtt);
}

44
test/strtoint.c Normal file
View File

@ -0,0 +1,44 @@
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
int *strtoint(char *in, size_t start, size_t n)
{
int *ret = malloc(sizeof(int));
*ret = 0;
int offset = start + n - 1;
if (offset < 0)
{
return NULL;
}
for (int i = 0; i < n; i++) {
int position = offset - i;
printf("foo: %c\n", in[position]);
if (in[position] >= '0' && in[position] <= '9')
{
*ret += (in[position] - '0') * pow(10, i);
}
else
{
return NULL;
}
}
return ret;
}
int main()
{
char *in = "abcd123";
int *ret = strtoint(in, 4, 3);
if (ret != NULL)
{
printf("%s | %d\n", in, *ret);
}
else
{
printf("ret is NULL\n");
}
}