diff --git a/src/btstack.c b/src/btstack.c index eddf8c185..ab06a17e0 100644 --- a/src/btstack.c +++ b/src/btstack.c @@ -34,7 +34,7 @@ int bt_open(){ // BTdaemon socket_connection_register_packet_callback(btstack_packet_handler); - btstack_connection = socket_connection_open_tcp(); + btstack_connection = socket_connection_open_unix(); if (!btstack_connection) return -1; return 0; diff --git a/src/daemon.c b/src/daemon.c index 599f2cd3a..4fc33eae4 100644 --- a/src/daemon.c +++ b/src/daemon.c @@ -198,7 +198,8 @@ int main (int argc, const char * argv[]){ // @TODO: make port and/or socket configurable per config.h // create server - socket_connection_create_tcp(BTSTACK_PORT); + // socket_connection_create_tcp(BTSTACK_PORT); + socket_connection_create_unix(BTSTACK_UNIX); socket_connection_register_packet_callback(daemon_client_handler); // handle CTRL-c diff --git a/src/platform_iphone.m b/src/platform_iphone.m index 0727fa67d..844dbd899 100644 --- a/src/platform_iphone.m +++ b/src/platform_iphone.m @@ -6,6 +6,8 @@ #import "platform_iphone.h" +#ifdef USE_SPRINGBOARD + #ifdef __OBJC__ #import #import @@ -36,4 +38,6 @@ void platform_iphone_status_handler(BLUETOOTH_STATE state){ default: break; } -} \ No newline at end of file +} + +#endif \ No newline at end of file diff --git a/src/socket_connection.c b/src/socket_connection.c index 604ad5f42..b345bf07d 100644 --- a/src/socket_connection.c +++ b/src/socket_connection.c @@ -19,6 +19,7 @@ #include #include #include +#include #include #define MAX_PENDING_CONNECTIONS 10 @@ -232,10 +233,48 @@ int socket_connection_create_tcp(int port){ /** * create socket data_source for unix domain socket - * - * @TODO: implement socket_connection_create_unix - */ + * */ int socket_connection_create_unix(char *path){ + + // create data_source_t + data_source_t *ds = malloc( sizeof(data_source_t)); + if (ds == NULL) return -1; + ds->fd = 0; + ds->process = socket_connection_accept; + + // create tcp socket + if ((ds->fd = socket (AF_UNIX, SOCK_STREAM, 0)) < 0) { + printf ("Error creating socket ...(%s)\n", strerror(errno)); + free(ds); + return -1; + } + + printf ("Socket created\n"); + + struct sockaddr_un addr; + bzero(&addr, sizeof(addr)); + addr.sun_family = AF_UNIX; + strcpy(addr.sun_path, path); + unlink(path); + + const int y = 1; + setsockopt(ds->fd, SOL_SOCKET, SO_REUSEADDR, &y, sizeof(int)); + + if (bind ( ds->fd, (struct sockaddr *) &addr, sizeof (addr) ) ) { + printf ("Error on bind() ...(%s)\n", strerror(errno)); + free(ds); + return -1; + } + + if (listen (ds->fd, MAX_PENDING_CONNECTIONS)) { + printf ("Error on listen() ...(%s)\n", strerror(errno)); + free(ds); + return -1; + } + + run_loop_add_data_source(ds); + + printf ("Server up and running ...\n"); return 0; } @@ -310,4 +349,39 @@ int socket_connection_close_tcp(connection_t * connection){ run_loop_remove_data_source(&connection->ds); free( connection ); return 0; -} \ No newline at end of file +} + + +/** + * create socket connection to BTdaemon + */ +connection_t * socket_connection_open_unix(){ + + int btsocket = socket(AF_UNIX, SOCK_STREAM, 0); + if(btsocket == -1){ + return NULL; + } + + struct sockaddr_un server; + bzero(&server, sizeof(server)); + server.sun_family = AF_UNIX; + strcpy(server.sun_path, BTSTACK_UNIX); + if (connect(btsocket, (struct sockaddr *)&server, sizeof (server)) == -1){ + return NULL; + }; + + return socket_connection_register_new_connection(btsocket); +} + + +/** + * close socket connection to BTdaemon + */ +int socket_connection_close_unix(connection_t * connection){ + if (!connection) return -1; + shutdown(connection->ds.fd, SHUT_RDWR); + run_loop_remove_data_source(&connection->ds); + free( connection ); + return 0; +} + diff --git a/src/socket_connection.h b/src/socket_connection.h index fc5592d40..692a33238 100644 --- a/src/socket_connection.h +++ b/src/socket_connection.h @@ -13,6 +13,9 @@ /** TCP port for BTstack */ #define BTSTACK_PORT 13333 +/** UNIX domain socket for BTstack */ +#define BTSTACK_UNIX "/tmp/BTstack" + /** opaque connection type */ typedef struct connection connection_t; @@ -22,20 +25,36 @@ typedef struct connection connection_t; int socket_connection_create_tcp(int port); /** - * create socket for incoming for unix domain connections + * create socket for incoming unix domain connections */ int socket_connection_create_unix(char *path); -/** - * create socket connection to BTdaemon - */ -connection_t * socket_connection_open_tcp(); - /** * close socket connection to BTdaemon */ int socket_connection_close_tcp(connection_t *connection); +/** + * create TCP socket connection to BTdaemon + */ +connection_t * socket_connection_open_tcp(); + +/** + * close TCP socket connection to BTdaemon + */ +int socket_connection_close_tcp(connection_t *connection); + +/** + * create unix socket connection to BTdaemon + */ +connection_t * socket_connection_open_unix(); + +/** + * close unix connection to BTdaemon + */ +int socket_connection_close_unix(connection_t *connection); + + /** * set packet handler for all auto-accepted connections */