Skip to content
Snippets Groups Projects
Unverified Commit af685ab9 authored by Jake Drahos's avatar Jake Drahos
Browse files

Changed how packets are received

parent c3434452
No related branches found
No related tags found
No related merge requests found
...@@ -69,7 +69,7 @@ static int remove_client(int fd); ...@@ -69,7 +69,7 @@ static int remove_client(int fd);
/* Receive data from client */ /* Receive data from client */
static void client_recv(int fd); static void client_recv(int fd);
/* Receive data from quad */ /* Receive data from quad */
static void quad_recv(const char * buf, size_t len); static void quad_recv();
/* Checks to see if socket has disconnected. Returns 1 on disconnect, else returns 0 */ /* Checks to see if socket has disconnected. Returns 1 on disconnect, else returns 0 */
static int wasDisconnected(int fd); static int wasDisconnected(int fd);
...@@ -103,7 +103,7 @@ fd_set rfds_master; ...@@ -103,7 +103,7 @@ fd_set rfds_master;
int max_fd = 0; int max_fd = 0;
pthread_mutex_t quadResponseMutex, cliInputMutex ; pthread_mutex_t quadResponseMutex, cliInputMutex ;
char *respBuf, *commandBuf; unsigned char *commandBuf;
int newQuadResponse = 0, newCliInput = 0; int newQuadResponse = 0, newCliInput = 0;
// Structures to be used throughout // Structures to be used throughout
...@@ -178,7 +178,6 @@ int main(int argc, char **argv) ...@@ -178,7 +178,6 @@ int main(int argc, char **argv)
if ((zyboSocket = connectToZybo()) < 0) if ((zyboSocket = connectToZybo()) < 0)
{ {
perror("Error connecting to Quad..."); perror("Error connecting to Quad...");
free(respBuf);
free(commandBuf); free(commandBuf);
exit(1); exit(1);
} }
...@@ -218,7 +217,6 @@ int main(int argc, char **argv) ...@@ -218,7 +217,6 @@ int main(int argc, char **argv)
.tv_usec = 0 .tv_usec = 0
}; };
respBuf = calloc(CMD_MAX_LENGTH, sizeof(unsigned char));
sleep(3); sleep(3);
while(keepRunning) while(keepRunning)
{ {
...@@ -240,33 +238,7 @@ int main(int argc, char **argv) ...@@ -240,33 +238,7 @@ int main(int argc, char **argv)
*/ */
} else if (fd == zyboSocket) { } else if (fd == zyboSocket) {
printf("recieving from quad\n"); printf("recieving from quad\n");
quad_recv();
/**
* Read the response from the control loop
*/
int available;
ioctl(fd, FIONREAD, &available);
if (available < 12) {
continue;
}
int respLen = readQuad(respBuf, CMD_MAX_LENGTH);
if(respLen <= 0) {
perror("ERROR reading from quad...\n");
}
//int id = getInt((unsigned char *)respBuf, 7);
//findTimeDiff(id);
quad_recv(respBuf, respLen);
// if(respLen == 11) {
// int id = getInt((unsigned char *)respBuf, 7);
// findTimeDiff(id);
// printf("respLen = %d : id = %d'\n", respLen, id);
// for(int i = 0; i <= respLen -1; ++i)
// printf("%x ", (unsigned char)respBuf[i]);
// printf("'\n");
// }
memset(respBuf, 0, respLen);
} else if (fd == backendSocket) { } else if (fd == backendSocket) {
int new_fd = 0; int new_fd = 0;
new_fd = accept(backendSocket, NULL, NULL); new_fd = accept(backendSocket, NULL, NULL);
...@@ -693,41 +665,77 @@ static void client_recv(int fd) { ...@@ -693,41 +665,77 @@ static void client_recv(int fd) {
} }
} }
static void quad_recv(const char * buf, size_t len) { static void quad_recv() {
/* Check to see which command we are receiving. If it is one that needs to be passed on /* Check to see which command we are receiving. If it is one that needs to be passed on
onto the clients, do so. onto the clients, do so.
*/ */
char packet[len];
int validPacket; int validPacket;
unsigned char *data; unsigned char *data;
metadata_t metadata; metadata_t metadata;
static unsigned char respBuf[2048];
static size_t respBufLen;
/**
* Read the response from the control loop
*/
int respLen = readQuad((char *) respBuf + respBufLen,
CMD_MAX_LENGTH);
if(respLen <= 0) {
perror("ERROR reading from quad...\n");
return;
}
respBufLen += respLen;
if (respBufLen < 8) {
/* not long enough yet */
return;
}
memcpy(packet, buf, len);
// Validate the message is correctly formatted // Validate the message is correctly formatted
validPacket = parse_packet((unsigned char *) packet, &data, &metadata); validPacket = parse_packet((unsigned char *) respBuf, &data, &metadata);
if(validPacket != 0) { if (validPacket == -1) {
warnx("Could not recognize packet from quad.\n"); warnx("Doesn't have start byte.");
/* nuke packet */
respBufLen = 0;
return; return;
} }
int datalen = (packet[6] << 8) | (packet[5]); /* Get datalen */
char * cmdText = MessageTypes[(int)metadata.msg_type].subtypes[(int)metadata.msg_subtype].cmdText; size_t datalen = metadata.data_len;
float value = getFloat((unsigned char *)packet, 7); if (datalen > CMD_MAX_LENGTH - 8) {
printf("Quad : %s, %lf\n", cmdText, value); /* Very invalid packet. Nuke that shit */
/* respBufLen = 0;
Assuming the quad sends the correct info.. This hasn't been tested yet due to a lack of return;
quad software. We can check how to format by the cmdText and pass to every client. }
*/
char buffer[1048]; if (respBufLen < datalen + 8) {
sprintf(buffer, "%s %lf\n", cmdText, value); /* Packet not yet fully read */
return;
for(int fd = 0; fd <= max_fd; ++fd) { }
if (get_client_index(fd) > -1) {
write(fd, buffer, datalen + 8); if (validPacket == 0) {
/* At least enough data read to check checksum, and it was good!*/
char * cmdText = MessageTypes[(int)metadata.msg_type].subtypes[(int)metadata.msg_subtype].cmdText;
float value = getFloat((unsigned char *)respBuf, 7);
printf("Quad : %s, %lf\n", cmdText, value);
/*
Assuming the quad sends the correct info.. This hasn't been tested yet due to a lack of
quad software. We can check how to format by the cmdText and pass to every client.
*/
char buffer[1048];
sprintf(buffer, "%s %lf\n", cmdText, value);
for(int fd = 0; fd <= max_fd; ++fd) {
if (get_client_index(fd) > -1) {
write(fd, buffer, datalen + 8);
}
} }
} else {
warnx("Checksum mismatch!");
} }
memmove(respBuf, respBuf + datalen + 8, respBufLen - (datalen + 8));
respBufLen -= datalen + 8;
} }
static int wasDisconnected(int fd) { static int wasDisconnected(int fd) {
......
...@@ -236,8 +236,10 @@ int parse_packet(unsigned char * packet, unsigned char ** data, metadata_t * met ...@@ -236,8 +236,10 @@ int parse_packet(unsigned char * packet, unsigned char ** data, metadata_t * met
} }
// compare checksum // compare checksum
if(packet_checksum != calculated_checksum) if(packet_checksum != calculated_checksum) {
fprintf(stderr, "Checksums did not match (Parse Packet): 0x%02x\t0x%02x\n", packet_checksum, calculated_checksum); fprintf(stderr, "Checksums did not match (Parse Packet): 0x%02x\t0x%02x\n", packet_checksum, calculated_checksum);
return -2;
}
return 0; return 0;
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment