Compare commits

...

10 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
14 changed files with 704 additions and 269 deletions

1
.gitignore vendored
View File

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

View File

@ -6,7 +6,7 @@ command:
gcc -o ${BIN}/command network.c command.c gcc -o ${BIN}/command network.c command.c
readref: readref:
gcc -o ${BIN}/readref network.c readref.c gcc -o ${BIN}/readref network.c mqtt.c -lmosquitto readref.c -lm
.PHONY: clean dirs .PHONY: clean dirs
clean: 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

@ -4,23 +4,27 @@
#include "network.h" #include "network.h"
int main(int argc, char *argv[]) { int main(int argc, char *argv[])
{
int xPlaneSocket; int xPlaneSocket;
// check argument // check argument
if (argc != 2) { if (argc != 2)
{
fprintf(stderr, "Usage: %s <command>\n", argv[0]); fprintf(stderr, "Usage: %s <command>\n", argv[0]);
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
// network // network
xPlaneSocket = createSocket(SRC_PORT); xPlaneSocket = createSocket(SRC_PORT);
if (xPlaneSocket < 0) { if (xPlaneSocket < 0)
{
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
// send payload // send payload
if (sendToXPlane(xPlaneSocket, DEST_SERVER, DEST_PORT, "CMND", argv[1], strlen(argv[1])) < 0) { if (sendToXPlane(xPlaneSocket, DEST_SERVER, DEST_PORT, "CMND", argv[1], strlen(argv[1])) < 0)
{
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }

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);

View File

@ -15,18 +15,21 @@
const int DGRAM_MSG_START = 0; const int DGRAM_MSG_START = 0;
const int DGRAM_PAYLOAD_START = DGRAM_MSG_START + DGRAM_MSG_LENGTH + DGRAM_NULL_LENGTH; 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) { int sendToXPlane(int sock, char *destAddr, int destPort, char *msg, void *payload, int lengthOfPayload)
{
char bytesToSend[SEND_BUFFER]; char bytesToSend[SEND_BUFFER];
struct sockaddr_in6 xPlaneAddress; struct sockaddr_in6 xPlaneAddress;
memset(bytesToSend, 0, SEND_BUFFER); memset(bytesToSend, 0, SEND_BUFFER);
// msg is always 4 bytes long // msg is always 4 bytes long
if (strlen(msg) != DGRAM_MSG_LENGTH) { if (strlen(msg) != DGRAM_MSG_LENGTH)
{
return -10; return -10;
} }
// payload shouldn't exceed buffer // payload shouldn't exceed buffer
if (lengthOfPayload > (SEND_BUFFER - DGRAM_MSG_LENGTH - DGRAM_NULL_LENGTH)) { if (lengthOfPayload > (SEND_BUFFER - DGRAM_MSG_LENGTH - DGRAM_NULL_LENGTH))
{
return -11; return -11;
} }
@ -39,35 +42,55 @@ int sendToXPlane(int sock, char *destAddr, int destPort, char *msg, void *payloa
inet_pton(AF_INET6, destAddr, &xPlaneAddress.sin6_addr.s6_addr); 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)); int numBytesSend = sendto(sock, bytesToSend, DGRAM_MSG_LENGTH + DGRAM_NULL_LENGTH + lengthOfPayload, 0, (struct sockaddr *)&xPlaneAddress, sizeof(xPlaneAddress));
if (numBytesSend >= 0) { if (numBytesSend >= 0)
{
return numBytesSend; return numBytesSend;
} else { }
fprintf(stderr, "Failed to send message (%d): %s", errno, strerror(errno)); else
{
fprintf(stderr, "Failed to send message (%d): %s\n", errno, strerror(errno));
return -1; return -1;
} }
} }
int recvFromXPlane(int sock, char msg[DGRAM_MSG_LENGTH], void *payload, int maxLength) { int recvFromXPlane(int sock, char msg[DGRAM_MSG_LENGTH], void *payload, int maxLength)
{
char buf[maxLength + DGRAM_MSG_LENGTH]; char buf[maxLength + DGRAM_MSG_LENGTH];
memset(buf, 0, sizeof(buf)); memset(buf, 0, sizeof(buf));
int recvBytes = recv(sock, buf, maxLength + DGRAM_MSG_LENGTH, 0);
if (recvBytes >= 0) { strcpy(msg, "");
if (recvBytes >= DGRAM_MSG_LENGTH) {
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(msg, &buf[0], DGRAM_MSG_LENGTH);
memcpy(payload, &buf[DGRAM_MSG_LENGTH], maxLength); memcpy(payload, &buf[DGRAM_MSG_LENGTH], maxLength);
return recvBytes - DGRAM_MSG_LENGTH; return recvBytes - DGRAM_MSG_LENGTH;
} else { }
fprintf(stderr, "Received data without message header?!"); else
{
fprintf(stderr, "Received data without message header?!\n");
return -11; return -11;
} }
} else { }
fprintf(stderr, "Error receiving data (%d): %s", errno, strerror(errno)); else
{
if (errno != EAGAIN)
{
fprintf(stderr, "Error receiving data (%d): %s\n", errno, strerror(errno));
return -1; return -1;
} }
else
{
return 0;
}
}
} }
int createSocket(int srcPort) { int createSocket(int srcPort)
{
int xPlaneSocket; int xPlaneSocket;
struct sockaddr_in6 localAddress; struct sockaddr_in6 localAddress;
@ -75,7 +98,8 @@ int createSocket(int srcPort) {
// create socket // create socket
xPlaneSocket = socket(AF_INET6, SOCK_DGRAM, 0); xPlaneSocket = socket(AF_INET6, SOCK_DGRAM, 0);
if (xPlaneSocket == -1) { if (xPlaneSocket == -1)
{
fprintf(stderr, "Error creating socket (%d): %s\n", errno, strerror(errno)); fprintf(stderr, "Error creating socket (%d): %s\n", errno, strerror(errno));
return -1; return -1;
} }
@ -87,7 +111,8 @@ int createSocket(int srcPort) {
// bind to local port // bind to local port
err = bind(xPlaneSocket, (struct sockaddr *)&localAddress, sizeof(localAddress)); err = bind(xPlaneSocket, (struct sockaddr *)&localAddress, sizeof(localAddress));
if (err) { if (err)
{
fprintf(stderr, "Error binding [::]:%d (%d): %s\n", srcPort, errno, gai_strerror(errno)); fprintf(stderr, "Error binding [::]:%d (%d): %s\n", srcPort, errno, gai_strerror(errno));
return -1; return -1;
} }
@ -95,6 +120,7 @@ int createSocket(int srcPort) {
return xPlaneSocket; return xPlaneSocket;
} }
void closeSocket(int sock) { void closeSocket(int sock)
{
close(sock); close(sock);
} }

228
readref.c
View File

@ -2,86 +2,240 @@
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#include <inttypes.h> #include <inttypes.h>
#include <math.h>
#include <mosquitto.h>
#include "network.h" #include "network.h"
#include "mqtt.h"
struct dref_struct_in { #include "config.h"
int32_t dref_freq;
int32_t dref_sender_index; // the index the customer is using to define this dataref #define MQTT_RREF_REQUEST_LENGTH_ID 3
char dref_string[400]; #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 dref_struct_out { 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; int32_t dref_sender_index;
float dref_flt_value; float dref_flt_value;
}; };
int main(int argc, char *argv[]) { int *strtoint(char *in, size_t start, size_t n)
int xPlaneSocket; {
char *wtf; int *ret = malloc(sizeof(int));
*ret = 0;
int offset = start + n - 1;
// check argument if (offset < 0)
if (argc != 1 && (argc - 1) % 3 != 0) { {
fprintf(stderr, "Usage: %s [<dataref> <frequency> <id> [<dataref> <frequency> <id> ... ]]\n", argv[0]); return NULL;
exit(EXIT_FAILURE);
} }
// network for (int i = 0; i < n; i++)
xPlaneSocket = createSocket(SRC_PORT); {
if (xPlaneSocket < 0) { int position = offset - i;
exit(EXIT_FAILURE); 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;
} }
// char drefsToRead[413 * ((argc - 1) / 3)];
// memset(drefsToRead, 0, sizeof(drefsToRead));
for (int i = 1; i < argc; i = i + 3) {
// prepare struct to send, make sure padding is with NULL bytes // prepare struct to send, make sure padding is with NULL bytes
struct dref_struct_in drefToRead = { struct dref_struct_in drefToRead = {
.dref_freq = strtoimax(argv[i + 1], &wtf, 10), .dref_freq = *frequency,
.dref_sender_index = strtoimax(argv[i + 2], &wtf, 10) .dref_sender_index = *index};
};
memset(drefToRead.dref_string, 0, sizeof(drefToRead.dref_string)); memset(drefToRead.dref_string, 0, sizeof(drefToRead.dref_string));
strcpy(drefToRead.dref_string, argv[i]); strncpy(drefToRead.dref_string, &((char *)message->payload)[MQTT_RREF_REQUEST_START_RREF], message->payloadlen - MQTT_RREF_REQUEST_START_RREF);
// send payload sendToXPlane(xPlaneSocket, DEST_SERVER, DEST_PORT, "RREF", (char *)&drefToRead, sizeof(drefToRead));
if (sendToXPlane(xPlaneSocket, DEST_SERVER, DEST_PORT, "RREF", (char *)&drefToRead, sizeof(drefToRead)) < 0) { }
}
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); exit(EXIT_FAILURE);
} }
// memcpy(&drefsToRead[((i - 1) / 3) * 413], &drefToRead, sizeof(drefToRead)); // mqtt
mqtt = connectMqtt(MQTT_USER1, MQTT_PASS, onMessage);
if (mqtt == NULL)
{
exit(EXIT_FAILURE);
} }
// subscribe meta channel
recvMqtt(mqtt, "/xplane/meta/#");
// read from network // read from network
char msg[DGRAM_MSG_LENGTH + 1]; char msg[DGRAM_MSG_LENGTH + 1];
char payload[RECV_BUFFER]; char payload[RECV_BUFFER];
while (1) {
char mqttTopic[100], mqttPayload[512];
while (1)
{
int payloadBytes = recvFromXPlane(xPlaneSocket, msg, payload, RECV_BUFFER); int payloadBytes = recvFromXPlane(xPlaneSocket, msg, payload, RECV_BUFFER);
if (payloadBytes > 0) { if (payloadBytes >= 0)
{
if (strcmp(msg, "RREF") == 0)
{
printf("Received message type %s\n", msg); printf("Received message type %s\n", msg);
printf("Received message data (%d) ", payloadBytes); printf("Received message data (%d) ", payloadBytes);
for (int i = 0; i < payloadBytes; i++) { for (int i = 0; i < payloadBytes; i++)
{
printf("%02x", payload[i]); printf("%02x", payload[i]);
} }
printf("\n"); printf("\n");
if (strcmp(msg, "RREF") == 0) {
if ((payloadBytes - 1) % 8 == 0) {
// XXX: loop
struct dref_struct_out drefRead;
memcpy(&drefRead, &payload[1], 8);
// 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); printf("%d: %f\n", drefRead.dref_sender_index, drefRead.dref_flt_value);
} else {
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); exit(EXIT_FAILURE);
} }
} else { }
else
{
// unknown to me // unknown to me
} }
} else { }
else
{
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
mosquitto_loop(mqtt, 0, 1);
} }
// bye bye (and no, I don't care about error anymore; I'll exit anyway?!) // bye bye (and no, I don't care about error anymore; I'll exit anyway?!)
disconnectMqtt(mqtt);
closeSocket(xPlaneSocket); closeSocket(xPlaneSocket);
exit(EXIT_SUCCESS); exit(EXIT_SUCCESS);
} }

3
test/.gitignore vendored
View File

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

View File

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

View File

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