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

Thread-safe writes to quad

parent f492b4d3
No related branches found
No related tags found
No related merge requests found
......@@ -23,7 +23,9 @@ OBJECTS = $(CPPOBJECTS) $(COBJECTS)
LIBS= -lpthread -lbluetooth -lvrpn -lquat -Lsrc/vrpn/build -Lsrc/vrpn/build/quat
# Default target
all: logs vrpn/build $(EXE)
all: logs $(EXE)
vrpn: vrpn/build
# Main target
$(EXE): $(OBJECTS)
......
......@@ -3,6 +3,7 @@
* BlueTooth socket program for passing vrpn data to quad.
*/
//system includes
#include <err.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
......@@ -44,15 +45,13 @@ int safe_fd_clr(int , fd_set* , int* );
static void cb(struct ucart_vrpn_TrackerData *);
/* Thread-safe wrappers */
pthread_mutex_t quadSocketMutex, logFileMutex;
ssize_t writeQuad(const char * buf, size_t count);
ssize_t dprintfLog(const char * fmt, ...);
pthread_mutex_t quadSocketMutex;
static ssize_t writeQuad(const char * buf, size_t count);
// global variables
static volatile int keepRunning = 1;
const char *TRACKER_IP = "UAV@192.168.0.120:3883";
int quadlog_file;
int zyboSocket, status, bytes_read;
static int zyboSocket;
struct ucart_vrpn_tracker * tracker = NULL;
const char *logHeader = "";//"#\n#\tDefault log header\n#\tEverything after '#'`s will be printed as is in the processed logs.\n#\n\0";
......@@ -96,6 +95,10 @@ int main(int argc, char **argv)
signal(SIGINT, killHandler);
if (pthread_mutex_lock(&quadSocketMutex)) {
err(-2, "pthrtead_mutex_lock (%s:%d):", __FILE__, __LINE__);
}
// if ((zyboSocket = connectToZybo()) < 0)
// {
// perror("Error connecting to Zybo...");
......@@ -103,12 +106,16 @@ int main(int argc, char **argv)
// free(commandBuf);
// exit(1);
// }
if (pthread_mutex_unlock(&quadSocketMutex)) {
err(-2, "pthrtead_mutex_unlock (%s:%d):", __FILE__, __LINE__);
}
// create vrpnTracker instance
//tracker = ucart_vrpn_tracker_createInstance(TRACKER_IP);
// open the log file
if( (status = createLogFile(argc, argv[1])) < 0)
if(createLogFile(argc, argv[1]))
{
perror("Error creating log file...");
exit(1);
......@@ -162,7 +169,7 @@ int main(int argc, char **argv)
fprintf(stdout, "CLI sees as: %f\n", getFloat(packet, 7));
// Write the command to the control_loop socket
// int n = write(zyboSocket, packet, ((packet[6] << 8) | packet[5]) + 8);
// int n = writeQuad(packet, ((packet[6] << 8) | packet[5]) + 8);
// if(n < 0) {
// fprintf(stdout, "CLI: ERROR writing to socket\n");
// }
......@@ -180,8 +187,16 @@ int main(int argc, char **argv)
}
// ucart_vrpn_tracker_freeInstance(tracker);
if (pthread_mutex_lock(&quadSocketMutex)) {
err(-2, "pthrtead_mutex_lock (%s:%d):", __FILE__, __LINE__);
}
close(zyboSocket);
close(quadlog_file);
if (pthread_mutex_unlock(&quadSocketMutex)) {
err(-2, "pthrtead_mutex_unlock (%s:%d):", __FILE__, __LINE__);
}
closeLogFile();
return 0;
}
......@@ -217,7 +232,7 @@ void sendStartPacket() {
checksum ^= packet[i];
packet[metadata.data_len + 7] = checksum; //PACKET_END_BYTE;
status = write(zyboSocket, &packet, metadata.data_len + 8);
int status = writeQuad((char * ) packet, metadata.data_len + 8);
if (status != 8)
{
perror("Error sending start packet...\n");
......@@ -249,7 +264,7 @@ void sendVrpnPacket(struct ucart_vrpn_TrackerData *info) {
packet[pSize - 1] = checksum; //PACKET_END_BYTE;
n = write(zyboSocket, packet, pSize);
n = writeQuad(packet, pSize);
if(n < 0) {
perror("vrpnhandler: ERROR writing to socket");
keepRunning = 0;
......@@ -284,7 +299,7 @@ int connectToZybo() {
printf("Attempting to connect to zybo. Please be patient...\n");
// blocking call to connect to socket sock ie. zybo board
status = connect(sock, (struct sockaddr *)&addr, sizeof(addr));
int status = connect(sock, (struct sockaddr *)&addr, sizeof(addr));
// connection failed
if(status < 0)
......@@ -335,3 +350,16 @@ int safe_fd_clr(int fd, fd_set* fds, int* max_fd) {
}
return 0;
}
static ssize_t writeQuad(const char * buf, size_t count) {
ssize_t retval;
if (pthread_mutex_lock(&quadSocketMutex)) {
err(-2, "pthrtead_mutex_lock (%s:%d):", __FILE__, __LINE__);
}
retval = writeQuad(buf, count);
if (pthread_mutex_unlock(&quadSocketMutex)) {
err(-2, "pthrtead_mutex_unlock (%s:%d):", __FILE__, __LINE__);
}
return retval;
}
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