bluetooth: Split SdpServer into a different file

This is a refactor, SdpServer will have to do quite a bit of stuff and
it makes sense to split it from bluetooth_server, This does not change
any functionality except removing _USE_FAKE_SDP_SERVER macro which does
not seem to be of any use.

Change-Id: Ibbf2bbe1f59b88d68d2e7523a9a38840295ab050
Reviewed-on: https://review.haiku-os.org/c/haiku/+/10648
Tested-by: Commit checker robot <[email protected]>
Reviewed-by: waddlesplash <[email protected]>
This commit is contained in:
vighnesh-sawant
2026-04-06 13:52:39 +00:00
committed by waddlesplash
parent d910e4b2eb
commit d0b0af0089
5 changed files with 213 additions and 105 deletions
+8 -101
View File
@@ -27,9 +27,10 @@
#include <bluetooth/bluetooth.h>
#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)
+3 -4
View File
@@ -16,9 +16,10 @@
#include <bluetooth/HCI/btHCI_transport.h>
#include <bluetooth/HCI/btHCI_command.h>
#include "HCIDelegate.h"
#include "DeviceManager.h"
#include "HCIDelegate.h"
#include "LocalDeviceImpl.h"
#include "SDPServer.h"
#include <PortListener.h>
@@ -86,9 +87,7 @@ private:
BPoint fCenter;
thread_id fSDPThreadID;
bool fIsShuttingDown;
SDPServer* fSDPServer;
};
#endif
+1
View File
@@ -13,6 +13,7 @@ Application bluetooth_server
HCITransportAccessor.cpp
LocalDeviceHandler.cpp
LocalDeviceImpl.cpp
SDPServer.cpp
: be network libbluetooth.so localestub
[ TargetLibstdc++ ]
;
+168
View File
@@ -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 <stdio.h>
#include <sys/socket.h>
#include <String.h>
#include <bluetooth/L2CAP/btL2CAP.h>
#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;
}
+33
View File
@@ -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