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