formatting
This commit is contained in:
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);
|
||||
}
|
||||
|
Reference in New Issue
Block a user