diff --git a/src/servers/bluetooth/BluetoothServer.cpp b/src/servers/bluetooth/BluetoothServer.cpp index 09fa164c7f..46b4826bd2 100644 --- a/src/servers/bluetooth/BluetoothServer.cpp +++ b/src/servers/bluetooth/BluetoothServer.cpp @@ -27,9 +27,10 @@ #include #include "BluetoothServer.h" +#include "Debug.h" #include "DeskbarReplicant.h" #include "LocalDeviceImpl.h" -#include "Debug.h" +#include "SDPServer.h" status_t @@ -58,15 +59,14 @@ DispatchEvent(struct hci_event_header* header, int32 code, size_t size) BluetoothServer::BluetoothServer() : - BApplication(BLUETOOTH_SIGNATURE), - fSDPThreadID(-1), - fIsShuttingDown(false) + BApplication(BLUETOOTH_SIGNATURE) { fDeviceManager = new DeviceManager(); fLocalDevicesList.MakeEmpty(); fEventListener2 = new BluetoothPortListener(BT_USERLAND_PORT_NAME, (BluetoothPortListener::port_listener_func)&DispatchEvent); + fSDPServer = new SDPServer(); } @@ -79,13 +79,8 @@ bool BluetoothServer::QuitRequested(void) _RemoveDeskbarIcon(); - // stop the SDP server thread - fIsShuttingDown = true; - - status_t threadReturnStatus; - wait_for_thread(fSDPThreadID, &threadReturnStatus); - TRACE_BT("BluetoothServer server thread exited with: %s\n", - strerror(threadReturnStatus)); + fSDPServer->Stop(); + delete fSDPServer; delete fEventListener2; TRACE_BT("Shutting down bluetooth_server.\n"); @@ -117,16 +112,9 @@ void BluetoothServer::ReadyToRun(void) _InstallDeskbarIcon(); - // Spawn the SDP server thread - fSDPThreadID = spawn_thread(SDPServerThread, "SDP server thread", - B_NORMAL_PRIORITY, this); - -#define _USE_FAKE_SDP_SERVER -#ifdef _USE_FAKE_SDP_SERVER - if (fSDPThreadID <= 0 || resume_thread(fSDPThreadID) != B_OK) { + status_t status = fSDPServer->Start(); + if (status != B_OK) TRACE_BT("BluetoothServer: Failed launching the SDP server thread\n"); - } -#endif } @@ -445,87 +433,6 @@ BluetoothServer::HandleGetProperty(BMessage* message, BMessage* reply) #pragma mark - #endif -int32 -BluetoothServer::SDPServerThread(void* data) -{ - const BluetoothServer* server = (BluetoothServer*)data; - - // Set up the SDP socket - struct sockaddr_l2cap loc_addr = { 0 }; - int socketServer; - int client; - status_t status; - char buffer[512] = ""; - - TRACE_BT("SDP: SDP server thread up...\n"); - - socketServer = socket(PF_BLUETOOTH, SOCK_SEQPACKET, BLUETOOTH_PROTO_L2CAP); - - if (socketServer < 0) { - TRACE_BT("SDP: Could not create server socket ...\n"); - return B_ERROR; - } - - // bind socket to port 0x1001 of the first available - // bluetooth adapter - loc_addr.l2cap_family = AF_BLUETOOTH; - loc_addr.l2cap_bdaddr = BDADDR_ANY; - loc_addr.l2cap_psm = B_HOST_TO_LENDIAN_INT16(1); - loc_addr.l2cap_len = sizeof(struct sockaddr_l2cap); - - status = bind(socketServer, (struct sockaddr*)&loc_addr, - sizeof(struct sockaddr_l2cap)); - - if (status < 0) { - TRACE_BT("SDP: Could not bind server socket (%s)...\n", strerror(status)); - return status; - } - - // setsockopt(sock, SOL_L2CAP, SO_L2CAP_OMTU, &omtu, len ); - // getsockopt(sock, SOL_L2CAP, SO_L2CAP_IMTU, &omtu, &len ); - - // Listen for up to 10 connections - status = listen(socketServer, 10); - - if (status != B_OK) { - TRACE_BT("SDP: Could not listen server socket (%s)...\n", strerror(status)); - return status; - } - - while (!server->fIsShuttingDown) { - - TRACE_BT("SDP: Waiting connection for socket (%s)...\n", strerror(status)); - - uint len = sizeof(struct sockaddr_l2cap); - client = accept(socketServer, (struct sockaddr*)&loc_addr, &len); - - TRACE_BT("SDP: Incomming connection... %d\n", client); - - ssize_t receivedSize; - - do { - receivedSize = recv(client, buffer, 29 , 0); - if (receivedSize < 0) - TRACE_BT("SDP: Error reading client socket\n"); - else { - TRACE_BT("SDP: Received from SDP client: %ld:\n", receivedSize); - for (int i = 0; i < receivedSize ; i++) - TRACE_BT("SDP: %x:", buffer[i]); - - TRACE_BT("\n"); - } - } while (receivedSize >= 0); - - snooze(5000000); - TRACE_BT("SDP: Waiting for next connection...\n"); - } - - // Close the socket - close(socketServer); - - return B_NO_ERROR; -} - void BluetoothServer::ShowWindow(BWindow* pWindow) diff --git a/src/servers/bluetooth/BluetoothServer.h b/src/servers/bluetooth/BluetoothServer.h index 42e96424b8..fc7b8b9a08 100644 --- a/src/servers/bluetooth/BluetoothServer.h +++ b/src/servers/bluetooth/BluetoothServer.h @@ -16,9 +16,10 @@ #include #include -#include "HCIDelegate.h" #include "DeviceManager.h" +#include "HCIDelegate.h" #include "LocalDeviceImpl.h" +#include "SDPServer.h" #include @@ -86,9 +87,7 @@ private: BPoint fCenter; - thread_id fSDPThreadID; - - bool fIsShuttingDown; + SDPServer* fSDPServer; }; #endif diff --git a/src/servers/bluetooth/Jamfile b/src/servers/bluetooth/Jamfile index 8a77d27b30..264eea2076 100644 --- a/src/servers/bluetooth/Jamfile +++ b/src/servers/bluetooth/Jamfile @@ -13,6 +13,7 @@ Application bluetooth_server HCITransportAccessor.cpp LocalDeviceHandler.cpp LocalDeviceImpl.cpp + SDPServer.cpp : be network libbluetooth.so localestub [ TargetLibstdc++ ] ; diff --git a/src/servers/bluetooth/SDPServer.cpp b/src/servers/bluetooth/SDPServer.cpp new file mode 100644 index 0000000000..fa60db0ee9 --- /dev/null +++ b/src/servers/bluetooth/SDPServer.cpp @@ -0,0 +1,168 @@ +/* + * Copyright 2007-2009 Oliver Ruiz Dorantes, oliver.ruiz.dorantes_at_gmail.com + * Copyright 2008 Mika Lindqvist, monni1995_at_gmail.com + * All rights reserved. Distributed under the terms of the MIT License. + */ + +#include +#include + +#include + +#include + +#include "Debug.h" +#include "SDPServer.h" + + +SDPServer::SDPServer() + : + fThreadID(-1), + fServerSocket(-1), + fIsShuttingDown(false) +{ +} + + +SDPServer::~SDPServer() +{ + Stop(); +} + + +status_t +SDPServer::Start() +{ + if (fThreadID > 0) { + // Already running + return B_OK; + } + + fIsShuttingDown = false; + + // Spawn the SDP thread + fThreadID = spawn_thread(_ListenThread, "SDP server thread", B_NORMAL_PRIORITY, this); + if (fThreadID < 0) { + TRACE_BT("SDP: Failed launching the SDP server thread\n"); + return fThreadID; + } + + return resume_thread(fThreadID); +} + + +void +SDPServer::Stop() +{ + if (fThreadID > 0) { + fIsShuttingDown = true; + + // Close the socket to unblock the accept() call + if (fServerSocket > 0) { + close(fServerSocket); + fServerSocket = -1; + } + + status_t threadReturnStatus; + wait_for_thread(fThreadID, &threadReturnStatus); + TRACE_BT("SDP: Server thread exited with: %s\n", strerror(threadReturnStatus)); + + fThreadID = -1; + } +} + + +int32 +SDPServer::_ListenThread(void* data) +{ + SDPServer* server = (SDPServer*)data; + return server->_Run(); +} + + +int32 +SDPServer::_Run() +{ + // Set up the SDP socket + struct sockaddr_l2cap loc_addr = {0}; + status_t status; + char buffer[512] = ""; + + TRACE_BT("SDP: SDP server thread up...\n"); + + fServerSocket = socket(PF_BLUETOOTH, SOCK_SEQPACKET, BLUETOOTH_PROTO_L2CAP); + + if (fServerSocket < 0) { + TRACE_BT("SDP: Could not create server socket ...\n"); + return B_ERROR; + } + + // bind socket to port 0x1001 of the first available + // bluetooth adapter + loc_addr.l2cap_family = AF_BLUETOOTH; + loc_addr.l2cap_bdaddr = BDADDR_ANY; + loc_addr.l2cap_psm = B_HOST_TO_LENDIAN_INT16(1); + loc_addr.l2cap_len = sizeof(struct sockaddr_l2cap); + + status = bind(fServerSocket, (struct sockaddr*)&loc_addr, sizeof(struct sockaddr_l2cap)); + + if (status < 0) { + TRACE_BT("SDP: Could not bind server socket (%s)...\n", strerror(status)); + close(fServerSocket); + fServerSocket = -1; + return status; + } + + // Listen for up to 10 connections + status = listen(fServerSocket, 10); + + if (status != B_OK) { + TRACE_BT("SDP: Could not listen server socket (%s)...\n", strerror(status)); + close(fServerSocket); + fServerSocket = -1; + return status; + } + + while (!fIsShuttingDown) { + + TRACE_BT("SDP: Waiting connection for socket...\n"); + + uint len = sizeof(struct sockaddr_l2cap); + int client = accept(fServerSocket, (struct sockaddr*)&loc_addr, &len); + + // Check if we woke up because of shutdown + if (fIsShuttingDown) { + if (client > 0) + close(client); + break; + } + + if (client < 0) { + TRACE_BT("SDP: Error accepting connection\n"); + continue; + } + + TRACE_BT("SDP: Incoming connection... %d\n", client); + + ssize_t receivedSize; + + do { + receivedSize = recv(client, buffer, 29, 0); + + if (receivedSize < 0) { + TRACE_BT("SDP: Error reading client socket\n"); + } else { + TRACE_BT("SDP: Received from SDP client: %ld:\n", receivedSize); + for (int i = 0; i < receivedSize; i++) + TRACE_BT("SDP: %x:", buffer[i]); + + TRACE_BT("\n"); + } + } while (receivedSize > 0); + + snooze(50000); + TRACE_BT("SDP: Waiting for next connection...\n"); + } + + return B_NO_ERROR; +} diff --git a/src/servers/bluetooth/SDPServer.h b/src/servers/bluetooth/SDPServer.h new file mode 100644 index 0000000000..1f2672f13c --- /dev/null +++ b/src/servers/bluetooth/SDPServer.h @@ -0,0 +1,33 @@ +/* + * Copyright 2007 Oliver Ruiz Dorantes, oliver.ruiz.dorantes_at_gmail.com + * All rights reserved. Distributed under the terms of the MIT License. + */ +#ifndef _SDP_SERVER_H_ +#define _SDP_SERVER_H_ + + +#include "OS.h" +#include "SupportDefs.h" + + +class SDPServer { +public: + + SDPServer(); + ~SDPServer(); + + status_t Start(); + void Stop(); + +private: + static int32 _ListenThread(void* data); + int32 _Run(); + + thread_id fThreadID; + int fServerSocket; + + bool fIsShuttingDown; +}; + + +#endif