From 22e11921bbbac2add400eb5124a04187305386bf Mon Sep 17 00:00:00 2001 From: Jake Drahos <j@kedrahos.com> Date: Thu, 27 Oct 2016 12:57:10 -0500 Subject: [PATCH] Finished implementing backend socket. Untested --- groundStation/Makefile | 8 ++-- groundStation/src/communication.c | 8 ++-- groundStation/src/microcart_cli.c | 73 +++++++++++++++++++++++++++---- 3 files changed, 72 insertions(+), 17 deletions(-) diff --git a/groundStation/Makefile b/groundStation/Makefile index f09d3d4d9..ec54cbcc2 100644 --- a/groundStation/Makefile +++ b/groundStation/Makefile @@ -1,8 +1,8 @@ # Declaration of variables GCC=gcc GXX=g++ -CFLAGS= -Wall -std=c99 -g -CPPFLAGS= -Wall -std=c++11 -g +CFLAGS= -Wall -Wpedantic -Wextra -Werror -std=c99 -g -Wno-unused-parameter -Wno-unused-variable -Wno-unused-function +CXXFLAGS= -Wall -Wno-reorder -std=c++11 -g INCLUDES = $(foreach dir, $(INCDIR), -I$(dir)) # Directories @@ -29,13 +29,13 @@ vrpn: vrpn/build # Main target $(EXE): $(OBJECTS) - $(GXX) $(CPPFLAGS) $^ -o $@ $(INCLUDES) $(LIBS) + $(GXX) $(CXXFLAGS) $^ -o $@ $(INCLUDES) $(LIBS) $(COBJECTS) : %.o : %.c $(GCC) $(CFLAGS) -c $< -o $@ $(INCLUDES) $(LIBS) $(CPPOBJECTS) : %.o : %.cpp - $(GCC) $(CPPFLAGS) -c $< -o $@ $(INCLUDES) $(LIBS) + $(GXX) $(CXXFLAGS) -c $< -o $@ $(INCLUDES) $(LIBS) vrpn/build: mkdir -p src/vrpn/build diff --git a/groundStation/src/communication.c b/groundStation/src/communication.c index 305a40598..f9d357928 100644 --- a/groundStation/src/communication.c +++ b/groundStation/src/communication.c @@ -59,7 +59,7 @@ int formatCommand(unsigned char *command, unsigned char **formattedCommand) { float floatValue = 0.0; int intValue = 0; int valid; - metadata_t metadata = {}; + metadata_t metadata; // ---------------------------------------------- if(tokens.numTokens > 1) { @@ -78,7 +78,7 @@ int formatCommand(unsigned char *command, unsigned char **formattedCommand) { printf("%f, %s\n", floatValue, tokens.tokens[1]); - metadata.begin_char = BEGIN_CHAR; + metadata.begin_char = (char) BEGIN_CHAR; metadata.msg_type = registeredCommands[cmdIndex].ID; metadata.msg_subtype = registeredCommands[cmdIndex].subID; metadata.msg_id = msgNum++; @@ -95,7 +95,7 @@ int formatCommand(unsigned char *command, unsigned char **formattedCommand) { return -1; } - metadata.begin_char = BEGIN_CHAR; + metadata.begin_char = (char) BEGIN_CHAR; metadata.msg_type = registeredCommands[cmdIndex].ID; metadata.msg_subtype = registeredCommands[cmdIndex].subID; metadata.msg_id = msgNum++; @@ -107,7 +107,7 @@ int formatCommand(unsigned char *command, unsigned char **formattedCommand) { // Validate the string input (doesn't need to happen) case stringType: - metadata.begin_char = BEGIN_CHAR; + metadata.begin_char = (char) BEGIN_CHAR; metadata.msg_type = registeredCommands[cmdIndex].ID; metadata.msg_subtype = registeredCommands[cmdIndex].subID; metadata.msg_id = msgNum++; diff --git a/groundStation/src/microcart_cli.c b/groundStation/src/microcart_cli.c index 4b1a66c47..db5d24e9e 100644 --- a/groundStation/src/microcart_cli.c +++ b/groundStation/src/microcart_cli.c @@ -2,6 +2,9 @@ * * BlueTooth socket program for passing vrpn data to quad. */ + +#define _GNU_SOURCE + //system includes #include <err.h> #include <stdio.h> @@ -53,6 +56,8 @@ static ssize_t get_client_index(int fd); static char * get_client_buffer(int fd); /* Returns -1 on error */ static int remove_client(int fd); +/* Receive data from client */ +static void client_recv(int fd); /* Thread-safe wrappers */ pthread_mutex_t quadSocketMutex; @@ -76,7 +81,7 @@ unsigned char *respBuf, *commandBuf; int newQuadResponse = 0, newCliInput = 0; // Structures to be used throughout -modular_structs_t structs = {}; +modular_structs_t structs; // Callback to be ran whenever the tracker receives data. // Currently doing much more than it should. It will be slimmed down @@ -207,7 +212,7 @@ int main(int argc, char **argv) for(int fd = 0; fd <= max_fd; ++fd) { if (FD_ISSET(fd, &rfds)) { if (fd == fileno(stdin)) { - unsigned char userCommand[CMD_MAX_LENGTH] = {}; + unsigned char userCommand[CMD_MAX_LENGTH]; read(fileno(stdin), (char *)userCommand, sizeof(userCommand)); unsigned int cmdLen = strlen((char*) userCommand); // if the user simply hit enter then let them try again @@ -246,6 +251,8 @@ int main(int argc, char **argv) safe_fd_set(fd, &rfds, &max_fd); } } + } else if (get_client_index(fd) > -1) { + client_recv(fd); } } } @@ -281,7 +288,7 @@ void sendStartPacket() { metadata_t metadata = { - BEGIN_CHAR, + (char) BEGIN_CHAR, 0x04, 0x01, 0x01, @@ -316,7 +323,7 @@ void sendStartPacket() { void sendVrpnPacket(struct ucart_vrpn_TrackerData *info) { int pSize = sizeof(info) + 8; int n; - char packet[pSize]; + unsigned char packet[pSize]; packet[0] = 0xBE; // BEGIN //PACKET_START_BYTE; packet[1] = 0x04; // UPDATE //'U'; // U for vrpn camera update, C for command packet[2] = 0x00; // N/A @@ -334,7 +341,7 @@ void sendVrpnPacket(struct ucart_vrpn_TrackerData *info) { packet[pSize - 1] = checksum; //PACKET_END_BYTE; - n = writeQuad(packet, pSize); + n = writeQuad((char *) packet, pSize); if(n < 0) { perror("vrpnhandler: ERROR writing to socket"); keepRunning = 0; @@ -357,7 +364,7 @@ void printVrpnData(struct ucart_vrpn_TrackerData * td) { int connectToZybo() { int sock; - struct sockaddr_rc addr = { -1 }; + struct sockaddr_rc addr; // allocate a socket sock = socket(AF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM); @@ -388,8 +395,9 @@ int connectToZybo() { void performCommand(char *cmdName, char * command) { for(int i = 0; i < NUM_COMMANDS; ++i) { - if(startsWith(registeredCommands[i].commandText, command)); + if(startsWith(registeredCommands[i].commandText, command)) { fprintf(stdout, "\r\n You used cmd '%s'\n",registeredCommands[i].commandText); + } } } @@ -482,7 +490,7 @@ static int remove_client(int fd) { char *clientBuffer = get_client_buffer(fd); if(clientBuffer == NULL) return -1; - clientBuffer = '\0'; + clientBuffer[0] = '\0'; client_fds[slot] = -1; return 0; } @@ -495,4 +503,51 @@ static void safe_close_fd(int fd, pthread_mutex_t *mutexLock) { if (pthread_mutex_unlock(mutexLock)) { err(-2, "pthrtead_mutex_unlock (%s:%d):", __FILE__, __LINE__); } -} \ No newline at end of file +} + +static void client_recv(int fd) +{ + char * buffer; + ssize_t len_pre; + buffer = get_client_buffer(fd); + len_pre = strlen(buffer); + + char * cursor; + cursor = buffer + len_pre; + + ssize_t r; + r = read(fd, cursor, CLIENT_BUFFER_SIZE - len_pre - 1); + if (r < 0) { + warn("read (fd: %d)", fd); + } + buffer[len_pre + r] = '\0'; + + + /* Parse buffer and handle commands */ + while (1) { + /* not using strtok because reasons */ + size_t len = strlen(buffer); + ssize_t newline = -1; + for (size_t i = 0; i < len; i++) { + if (buffer[i] == '\n') { + newline = i; + break; + } + } + + /* No newline found. End parsing */ + if (newline == -1) { + break; + } + + buffer[newline] = '\0'; + unsigned char * packet; + formatCommand((unsigned char *) buffer, &packet); + writeQuad((char *) packet, packet[6] << 8); + free(packet); + + char * rest = &buffer[newline] + 1; + /* Delete parsed data and move the rest to the left */ + memmove(buffer, rest, strlen(rest)); + } +} -- GitLab