diff --git a/groundStation/src/backend/backend.c b/groundStation/src/backend/backend.c index f025c009da060244c223012b1e253575344884f7..a57f4366529a245c77f30f5d91a12b84cffc8200 100644 --- a/groundStation/src/backend/backend.c +++ b/groundStation/src/backend/backend.c @@ -341,28 +341,48 @@ void printVrpnData(struct ucart_vrpn_TrackerData * td) { int connectToZybo() { int sock; - int status = 0; + int status = -1; if (getenv(NOQUAD_ENV)) { return 0; } - /* Use bluetooth by default */ - if (!getenv(QUAD_WIFI_ENV)) { - printf("Using BT Settings\n"); - struct sockaddr_rc addr; + /* Use wifi by default */ + if (getenv(QUAD_COMM_ENV)) { + /* Determine if we are using bluetooth or local */ + if(strcmp(getenv(QUAD_COMM_ENV), "local") == 0) { + printf("Using Local Socket Settings\n"); + + struct sockaddr_un remote; + char str[100]; + + if ((sock = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) { + perror("socket"); + exit(1); + } - // allocate a socket - sock = socket(AF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM); + remote.sun_family = AF_UNIX; + char * sock_env = getenv(QUAD_LOCAL_SOCKET); + strcpy(remote.sun_path, sock_env ? sock_env : QUAD_DEFAULT_LOCAL_SOCKET); + printf("Attempting to connect to local socket at '%s'. please be patiend.\n", remote.sun_path); - //set the connection params ie. who to connect to - addr.rc_family = AF_BLUETOOTH; - addr.rc_channel = (uint8_t) QUAD_BT_CHANNEL; - str2ba( QUAD_BT_ADDR, &addr.rc_bdaddr ); - - 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)); + status = connect(sock, (struct sockaddr *)&remote, sizeof(remote)); + } else if (strcmp(getenv(QUAD_COMM_ENV), "bluetooth") == 0) { + printf("Using BT Settings\n"); + struct sockaddr_rc addr; + + // allocate a socket + sock = socket(AF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM); + + //set the connection params ie. who to connect to + addr.rc_family = AF_BLUETOOTH; + addr.rc_channel = (uint8_t) QUAD_BT_CHANNEL; + str2ba( QUAD_BT_ADDR, &addr.rc_bdaddr ); + + 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)); + } } else { printf("Using WIFI settings\n"); struct sockaddr_in addr; diff --git a/groundStation/src/backend/config.h b/groundStation/src/backend/config.h index 38bf5611a009380597fd44732d995bb65535e75a..9306bd65ecf4ca80e60fb118a942e8fff00131d1 100644 --- a/groundStation/src/backend/config.h +++ b/groundStation/src/backend/config.h @@ -11,7 +11,10 @@ // If you are planning on using any of these env vars and you have // exported them with normal user rights. You will need to run the // backend with sudo elevation and with the --preserve-env flag or -E -#define QUAD_WIFI_ENV "UCART_USE_WIFI" +#define QUAD_COMM_ENV "UCART_COMM_CHANNEL" + +#define QUAD_DEFAULT_LOCAL_SOCKET "./virtquad.socket" +#define QUAD_LOCAL_SOCKET "UCART_LOCAL_SOCKET" #define QUAD_IP_ENV "UCART_QUAD_IP" #define QUAD_IP_DEFAULT "192.168.1.1" #define QUAD_PORT_ENV "UCART_QUAD_PORT" diff --git a/quad/src/virt_quad/hw_impl_unix_uart.c b/quad/src/virt_quad/hw_impl_unix_uart.c index f4787939832cd38d781643df1c9cd9d801f311b3..6328eb197729375022eb2d9f691a5d85fe31ce23 100644 --- a/quad/src/virt_quad/hw_impl_unix_uart.c +++ b/quad/src/virt_quad/hw_impl_unix_uart.c @@ -1,13 +1,63 @@ #include "hw_impl_unix.h" +#include <sys/types.h> +#include <sys/socket.h> +#include <stdio.h> +#include <sys/un.h> +#include <err.h> +#include <netinet/in.h> + +#define DEFAULT_SOCKET "../../groundStation/virtquad.socket" +#define SOCKET_ENV "VIRT_QUAD_SOCKET" + +static int backendSocket; +static int sendSocket; int unix_uart_reset(struct UARTDriver *self) { + + + char * backend_socket_path = DEFAULT_SOCKET; + if (getenv(SOCKET_ENV)) { + backend_socket_path = getenv(SOCKET_ENV); + } + /* Unlink if it exists */ + unlink(backend_socket_path); + printf("using socket at '%s'\n", backend_socket_path); + + /* Create socket */ + backendSocket = socket(AF_UNIX, SOCK_STREAM | SOCK_NONBLOCK, 0); + + + /* 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, 1)) { + err(-1, "listen"); + } + + while (1) { + sendSocket = accept(backendSocket, NULL, NULL); + if (sendSocket > 0) + break; + } + + printf(" accpet() returned with %d\n", sendSocket); return 0; } int unix_uart_write(struct UARTDriver *self, unsigned char c) { - return 0; + printf("sending %c\n", c); + return send(sendSocket, &c, 1, 0); } int unix_uart_read(struct UARTDriver *self, unsigned char *c) { - return 0; + int bytes = read(sendSocket, c, 1); + printf("read in %d byte [%x], msg=%d\n", bytes, *c, self->msg); + return bytes; }