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

Began backend socket

parent dcb58d3d
No related branches found
No related tags found
No related merge requests found
#ifndef __BACKEND_H
#define __BACKEND_H
#define DEFAULT_SOCKET "/var/run/ucart.socket"
#define SOCKET_ENV "UCART_SOCKET"
#endif
......@@ -9,6 +9,7 @@
#include <unistd.h>
#include <signal.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <sys/select.h>
#include <bluetooth/bluetooth.h>
#include <bluetooth/rfcomm.h>
......@@ -21,6 +22,7 @@
#include "vrpn_tracker.hpp"
#include "type_def.h"
#include "logger.h"
#include "config.h"
#define QUAD_BT_ADDR "00:06:66:64:61:D6"
#define QUAD_BT_CHANNEL 0x01
......@@ -52,6 +54,7 @@ static ssize_t writeQuad(const char * buf, size_t count);
static volatile int keepRunning = 1;
const char *TRACKER_IP = "UAV@192.168.0.120:3883";
static int zyboSocket;
static int backendSocket;
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";
......@@ -92,6 +95,41 @@ int main(int argc, char **argv)
fd_set rfds;
int activity;
int max_fd = 0;
FD_ZERO(&rfds);
/*
* Create backend listening socket
*/
/* Determine socket path */
char * backend_socket_path = DEFAULT_SOCKET;
if (getenv(SOCKET_ENV)) {
backend_socket_path = getenv(SOCKET_ENV);
}
/* Unlink if it exists */
unlink(backend_socket_path);
/* Create socket */
backendSocket = socket(AF_UNIX, SOCK_STREAM | SOCK_NONBLOCK, 0);
if (backendSocket < 0) {
err(-1, "socket");
}
/* Create sockaddr and bind */
struct sockaddr_un sa;
sa.sun_family = AF_UNIX;
strncpy(sa.sun_path, backend_socket_path, 107);
sa.sun_path[107] = '\0';
if (bind(backendSocket, (struct sockaddr *) &sa, sizeof(sa))) {
err(-1, "bind");
}
/* Listen */
if (listen(backendSocket, 16)) {
err(-1, "listen");
}
/* Add to socket set */
safe_fd_set(backendSocket, &rfds, &max_fd);
signal(SIGINT, killHandler);
......@@ -123,8 +161,6 @@ int main(int argc, char **argv)
// writeStringToLog(logHeader);
// clear the fd set
FD_ZERO(&rfds);
// watch for input from stdin (fd 0) to see when it has input
safe_fd_set(fileno(stdin), &rfds, &max_fd);
// watch for input from the zybo socket
......@@ -148,7 +184,7 @@ int main(int argc, char **argv)
if(activity == -1) {
perror("select() ");
} else if (activity) {
for(unsigned int fd = 0; fd <= max_fd; ++fd) {
for(int fd = 0; fd <= max_fd; ++fd) {
if (FD_ISSET(fd, &rfds)) {
if (fd == fileno(stdin)) {
unsigned char userCommand[CMD_MAX_LENGTH] = {};
......@@ -180,6 +216,13 @@ int main(int argc, char **argv)
memset(userCommand, 0, cmdLen);
} else if (fd == zyboSocket) {
} else if (fd == backendSocket) {
int new_fd = 0;
new_fd = accept(backendSocket, NULL, NULL);
if (new_fd < 0) {
warn("accept");
} else {
}
}
}
}
......
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