formatting
This commit is contained in:
parent
f05d2c985c
commit
60cfcf0bc7
42
command.c
42
command.c
@ -4,27 +4,31 @@
|
||||
|
||||
#include "network.h"
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
int xPlaneSocket;
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
int xPlaneSocket;
|
||||
|
||||
// check argument
|
||||
if (argc != 2) {
|
||||
fprintf(stderr, "Usage: %s <command>\n", argv[0]);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
// check argument
|
||||
if (argc != 2)
|
||||
{
|
||||
fprintf(stderr, "Usage: %s <command>\n", argv[0]);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
// network
|
||||
xPlaneSocket = createSocket(SRC_PORT);
|
||||
if (xPlaneSocket < 0) {
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
// network
|
||||
xPlaneSocket = createSocket(SRC_PORT);
|
||||
if (xPlaneSocket < 0)
|
||||
{
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
// send payload
|
||||
if (sendToXPlane(xPlaneSocket, DEST_SERVER, DEST_PORT, "CMND", argv[1], strlen(argv[1])) < 0) {
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
// send payload
|
||||
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?!)
|
||||
closeSocket(xPlaneSocket);
|
||||
exit(EXIT_SUCCESS);
|
||||
// bye bye (and no, I don't care about error anymore; I'll exit anyway?!)
|
||||
closeSocket(xPlaneSocket);
|
||||
exit(EXIT_SUCCESS);
|
||||
}
|
||||
|
148
network.c
148
network.c
@ -15,86 +15,102 @@
|
||||
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);
|
||||
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;
|
||||
}
|
||||
// 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;
|
||||
}
|
||||
// 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);
|
||||
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);
|
||||
// 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", errno, strerror(errno));
|
||||
return -1;
|
||||
}
|
||||
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", 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));
|
||||
int recvBytes = recv(sock, buf, maxLength + DGRAM_MSG_LENGTH, 0);
|
||||
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?!");
|
||||
return -11;
|
||||
}
|
||||
} else {
|
||||
fprintf(stderr, "Error receiving data (%d): %s", 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));
|
||||
int recvBytes = recv(sock, buf, maxLength + DGRAM_MSG_LENGTH, 0);
|
||||
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?!");
|
||||
return -11;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
fprintf(stderr, "Error receiving data (%d): %s", errno, strerror(errno));
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
int createSocket(int srcPort) {
|
||||
int xPlaneSocket;
|
||||
struct sockaddr_in6 localAddress;
|
||||
int createSocket(int srcPort)
|
||||
{
|
||||
int xPlaneSocket;
|
||||
struct sockaddr_in6 localAddress;
|
||||
|
||||
int err;
|
||||
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;
|
||||
}
|
||||
// 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;
|
||||
// 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;
|
||||
}
|
||||
// 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;
|
||||
return xPlaneSocket;
|
||||
}
|
||||
|
||||
void closeSocket(int sock) {
|
||||
close(sock);
|
||||
void closeSocket(int sock)
|
||||
{
|
||||
close(sock);
|
||||
}
|
||||
|
151
readref.c
151
readref.c
@ -5,83 +5,100 @@
|
||||
|
||||
#include "network.h"
|
||||
|
||||
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[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[400];
|
||||
};
|
||||
|
||||
struct dref_struct_out {
|
||||
int32_t dref_sender_index;
|
||||
float dref_flt_value;
|
||||
struct dref_struct_out
|
||||
{
|
||||
int32_t dref_sender_index;
|
||||
float dref_flt_value;
|
||||
};
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
int xPlaneSocket;
|
||||
char *wtf;
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
int xPlaneSocket;
|
||||
char *wtf;
|
||||
|
||||
// check argument
|
||||
if (argc != 1 && (argc - 1) % 3 != 0) {
|
||||
fprintf(stderr, "Usage: %s [<dataref> <frequency> <id> [<dataref> <frequency> <id> ... ]]\n", argv[0]);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
// check argument
|
||||
if (argc != 1 && (argc - 1) % 3 != 0)
|
||||
{
|
||||
fprintf(stderr, "Usage: %s [<dataref> <frequency> <id> [<dataref> <frequency> <id> ... ]]\n", argv[0]);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
// network
|
||||
xPlaneSocket = createSocket(SRC_PORT);
|
||||
if (xPlaneSocket < 0) {
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
// network
|
||||
xPlaneSocket = createSocket(SRC_PORT);
|
||||
if (xPlaneSocket < 0)
|
||||
{
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
// 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
|
||||
struct dref_struct_in drefToRead = {
|
||||
.dref_freq = strtoimax(argv[i + 1], &wtf, 10),
|
||||
.dref_sender_index = strtoimax(argv[i + 2], &wtf, 10)
|
||||
};
|
||||
memset(drefToRead.dref_string, 0, sizeof(drefToRead.dref_string));
|
||||
strcpy(drefToRead.dref_string, argv[i]);
|
||||
// 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
|
||||
struct dref_struct_in drefToRead = {
|
||||
.dref_freq = strtoimax(argv[i + 1], &wtf, 10),
|
||||
.dref_sender_index = strtoimax(argv[i + 2], &wtf, 10)};
|
||||
memset(drefToRead.dref_string, 0, sizeof(drefToRead.dref_string));
|
||||
strcpy(drefToRead.dref_string, argv[i]);
|
||||
|
||||
// send payload
|
||||
if (sendToXPlane(xPlaneSocket, DEST_SERVER, DEST_PORT, "RREF", (char *)&drefToRead, sizeof(drefToRead)) < 0) {
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
// send payload
|
||||
if (sendToXPlane(xPlaneSocket, DEST_SERVER, DEST_PORT, "RREF", (char *)&drefToRead, sizeof(drefToRead)) < 0)
|
||||
{
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
// memcpy(&drefsToRead[((i - 1) / 3) * 413], &drefToRead, sizeof(drefToRead));
|
||||
}
|
||||
// memcpy(&drefsToRead[((i - 1) / 3) * 413], &drefToRead, sizeof(drefToRead));
|
||||
}
|
||||
|
||||
// read from network
|
||||
char msg[DGRAM_MSG_LENGTH + 1];
|
||||
char payload[RECV_BUFFER];
|
||||
while (1) {
|
||||
int payloadBytes = recvFromXPlane(xPlaneSocket, msg, payload, RECV_BUFFER);
|
||||
if (payloadBytes > 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");
|
||||
if (strcmp(msg, "RREF") == 0) {
|
||||
if ((payloadBytes - 1) % 8 == 0) {
|
||||
// XXX: loop
|
||||
struct dref_struct_out drefRead;
|
||||
memcpy(&drefRead, &payload[1], 8);
|
||||
// read from network
|
||||
char msg[DGRAM_MSG_LENGTH + 1];
|
||||
char payload[RECV_BUFFER];
|
||||
while (1)
|
||||
{
|
||||
int payloadBytes = recvFromXPlane(xPlaneSocket, msg, payload, RECV_BUFFER);
|
||||
if (payloadBytes > 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");
|
||||
if (strcmp(msg, "RREF") == 0)
|
||||
{
|
||||
if ((payloadBytes - 1) % 8 == 0)
|
||||
{
|
||||
// XXX: loop
|
||||
struct dref_struct_out drefRead;
|
||||
memcpy(&drefRead, &payload[1], 8);
|
||||
|
||||
printf("%d: %f\n", drefRead.dref_sender_index, drefRead.dref_flt_value);
|
||||
} else {
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
} else {
|
||||
// unknown to me
|
||||
}
|
||||
} else {
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
}
|
||||
printf("%d: %f\n", drefRead.dref_sender_index, drefRead.dref_flt_value);
|
||||
}
|
||||
else
|
||||
{
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// unknown to me
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
}
|
||||
|
||||
// bye bye (and no, I don't care about error anymore; I'll exit anyway?!)
|
||||
closeSocket(xPlaneSocket);
|
||||
exit(EXIT_SUCCESS);
|
||||
// bye bye (and no, I don't care about error anymore; I'll exit anyway?!)
|
||||
closeSocket(xPlaneSocket);
|
||||
exit(EXIT_SUCCESS);
|
||||
}
|
||||
|
128
test/airspeed.c
128
test/airspeed.c
@ -15,75 +15,91 @@
|
||||
|
||||
#define COMMAND_BUFFER 256
|
||||
|
||||
char* printable(char *str) {
|
||||
char *ret = strdup(str);
|
||||
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';
|
||||
}
|
||||
}
|
||||
for (int i = strlen(ret); i >= 0; i--)
|
||||
{
|
||||
if (ret[i] == '\r' || ret[i] == '\n')
|
||||
{
|
||||
ret[i] = '\0';
|
||||
}
|
||||
}
|
||||
|
||||
return ret;
|
||||
return ret;
|
||||
}
|
||||
|
||||
void sendCommand(int sock, char *cmd) {
|
||||
char bytesToSend[COMMAND_BUFFER];
|
||||
memset(bytesToSend, 0, COMMAND_BUFFER);
|
||||
void sendCommand(int sock, char *cmd)
|
||||
{
|
||||
char bytesToSend[COMMAND_BUFFER];
|
||||
memset(bytesToSend, 0, COMMAND_BUFFER);
|
||||
|
||||
strcpy(&bytesToSend[0], "CMND");
|
||||
strcpy(&bytesToSend[5], cmd);
|
||||
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 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 main(int argc, char *argv[])
|
||||
{
|
||||
int xplaneSocket;
|
||||
struct sockaddr_in6 xplaneAddress;
|
||||
|
||||
int err;
|
||||
int err;
|
||||
|
||||
// check argument
|
||||
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);
|
||||
}
|
||||
// check argument
|
||||
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) {
|
||||
fprintf(stderr, "Error creating socket (%d): %s\n", errno, strerror(errno));
|
||||
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);
|
||||
// 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));
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
// 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));
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
// send payload
|
||||
if (strcmp(argv[1], "up") == 0) {
|
||||
sendCommand(xplaneSocket, "sim/autopilot/airspeed_up");
|
||||
} else if (strcmp(argv[1], "down") == 0) {
|
||||
sendCommand(xplaneSocket, "sim/autopilot/airspeed_down");
|
||||
} else {
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
// send payload
|
||||
if (strcmp(argv[1], "up") == 0)
|
||||
{
|
||||
sendCommand(xplaneSocket, "sim/autopilot/airspeed_up");
|
||||
}
|
||||
else if (strcmp(argv[1], "down") == 0)
|
||||
{
|
||||
sendCommand(xplaneSocket, "sim/autopilot/airspeed_down");
|
||||
}
|
||||
else
|
||||
{
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
// bye bye (and no, I don't care about error anymore; I'll exit anyway?!)
|
||||
close(xplaneSocket);
|
||||
exit(EXIT_SUCCESS);
|
||||
// bye bye (and no, I don't care about error anymore; I'll exit anyway?!)
|
||||
close(xplaneSocket);
|
||||
exit(EXIT_SUCCESS);
|
||||
}
|
||||
|
128
test/heading.c
128
test/heading.c
@ -15,75 +15,91 @@
|
||||
|
||||
#define COMMAND_BUFFER 256
|
||||
|
||||
char* printable(char *str) {
|
||||
char *ret = strdup(str);
|
||||
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';
|
||||
}
|
||||
}
|
||||
for (int i = strlen(ret); i >= 0; i--)
|
||||
{
|
||||
if (ret[i] == '\r' || ret[i] == '\n')
|
||||
{
|
||||
ret[i] = '\0';
|
||||
}
|
||||
}
|
||||
|
||||
return ret;
|
||||
return ret;
|
||||
}
|
||||
|
||||
void sendCommand(int sock, char *cmd) {
|
||||
char bytesToSend[COMMAND_BUFFER];
|
||||
memset(bytesToSend, 0, COMMAND_BUFFER);
|
||||
void sendCommand(int sock, char *cmd)
|
||||
{
|
||||
char bytesToSend[COMMAND_BUFFER];
|
||||
memset(bytesToSend, 0, COMMAND_BUFFER);
|
||||
|
||||
strcpy(&bytesToSend[0], "CMND");
|
||||
strcpy(&bytesToSend[5], cmd);
|
||||
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 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 main(int argc, char *argv[])
|
||||
{
|
||||
int xplaneSocket;
|
||||
struct sockaddr_in6 xplaneAddress;
|
||||
|
||||
int err;
|
||||
int err;
|
||||
|
||||
// check argument
|
||||
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);
|
||||
}
|
||||
// check argument
|
||||
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) {
|
||||
fprintf(stderr, "Error creating socket (%d): %s\n", errno, strerror(errno));
|
||||
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);
|
||||
// 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));
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
// 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));
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
// send payload
|
||||
if (strcmp(argv[1], "up") == 0) {
|
||||
sendCommand(xplaneSocket, "sim/autopilot/heading_up");
|
||||
} else if (strcmp(argv[1], "down") == 0) {
|
||||
sendCommand(xplaneSocket, "sim/autopilot/heading_down");
|
||||
} else {
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
// send payload
|
||||
if (strcmp(argv[1], "up") == 0)
|
||||
{
|
||||
sendCommand(xplaneSocket, "sim/autopilot/heading_up");
|
||||
}
|
||||
else if (strcmp(argv[1], "down") == 0)
|
||||
{
|
||||
sendCommand(xplaneSocket, "sim/autopilot/heading_down");
|
||||
}
|
||||
else
|
||||
{
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
// bye bye (and no, I don't care about error anymore; I'll exit anyway?!)
|
||||
close(xplaneSocket);
|
||||
exit(EXIT_SUCCESS);
|
||||
// bye bye (and no, I don't care about error anymore; I'll exit anyway?!)
|
||||
close(xplaneSocket);
|
||||
exit(EXIT_SUCCESS);
|
||||
}
|
||||
|
31
test/mqtt.c
31
test/mqtt.c
@ -9,36 +9,42 @@
|
||||
|
||||
#include "mqtt.h"
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
int err;
|
||||
struct mosquitto *mqtt;
|
||||
|
||||
|
||||
|
||||
|
||||
err = mosquitto_lib_init();
|
||||
if (err) {
|
||||
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) {
|
||||
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) {
|
||||
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) {
|
||||
if (err)
|
||||
{
|
||||
fprintf(stderr, "Error on connection of type ");
|
||||
if (err == MOSQ_ERR_ERRNO) {
|
||||
if (err == MOSQ_ERR_ERRNO)
|
||||
{
|
||||
fprintf(stderr, "system (%d): %s\n", errno, strerror(errno));
|
||||
} else {
|
||||
}
|
||||
else
|
||||
{
|
||||
fprintf(stderr, "mosquitto (%d): %s\n", err, mosquitto_strerror(err));
|
||||
}
|
||||
exit(EXIT_FAILURE);
|
||||
@ -46,7 +52,8 @@ int main(int argc, char *argv[]) {
|
||||
|
||||
char *foo = "blah";
|
||||
err = mosquitto_publish(mqtt, NULL, "/xplane/foo", strlen(foo), foo, 0, false);
|
||||
if (err) {
|
||||
if (err)
|
||||
{
|
||||
fprintf(stderr, "Error publishing message (%d): %s\n", err, mosquitto_strerror(err));
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user