xref: /haiku/src/servers/bluetooth/BluetoothServer.cpp (revision 269ebc04b9082e0081f3660bd727d9a6bc4a683d)
130f1d1f3SOliver Ruiz Dorantes /*
2e5da0ec5SOliver Ruiz Dorantes  * Copyright 2007-2009 Oliver Ruiz Dorantes, oliver.ruiz.dorantes_at_gmail.com
3e5da0ec5SOliver Ruiz Dorantes  * Copyright 2008 Mika Lindqvist, monni1995_at_gmail.com
430f1d1f3SOliver Ruiz Dorantes  * All rights reserved. Distributed under the terms of the MIT License.
530f1d1f3SOliver Ruiz Dorantes  */
630f1d1f3SOliver Ruiz Dorantes 
730f1d1f3SOliver Ruiz Dorantes #include <stdio.h>
830f1d1f3SOliver Ruiz Dorantes #include <fcntl.h>
930f1d1f3SOliver Ruiz Dorantes #include <unistd.h>
1061da62d0SOliver Ruiz Dorantes #include <sys/select.h>
1130f1d1f3SOliver Ruiz Dorantes 
1230f1d1f3SOliver Ruiz Dorantes #include <Entry.h>
13111d9460SOliver Ruiz Dorantes #include <Deskbar.h>
1430f1d1f3SOliver Ruiz Dorantes #include <Directory.h>
15e5da0ec5SOliver Ruiz Dorantes #include <Message.h>
16e5da0ec5SOliver Ruiz Dorantes #include <Path.h>
1730f1d1f3SOliver Ruiz Dorantes #include <Roster.h>
18e5da0ec5SOliver Ruiz Dorantes #include <String.h>
1930f1d1f3SOliver Ruiz Dorantes 
2030f1d1f3SOliver Ruiz Dorantes #include <TypeConstants.h>
2130f1d1f3SOliver Ruiz Dorantes #include <syslog.h>
2230f1d1f3SOliver Ruiz Dorantes 
2330f1d1f3SOliver Ruiz Dorantes #include <bluetoothserver_p.h>
2430f1d1f3SOliver Ruiz Dorantes #include <bluetooth/HCI/btHCI_command.h>
2561da62d0SOliver Ruiz Dorantes #include <bluetooth/L2CAP/btL2CAP.h>
2661da62d0SOliver Ruiz Dorantes #include <bluetooth/bluetooth.h>
2730f1d1f3SOliver Ruiz Dorantes 
2830f1d1f3SOliver Ruiz Dorantes #include "BluetoothServer.h"
29111d9460SOliver Ruiz Dorantes #include "DeskbarReplicant.h"
30111d9460SOliver Ruiz Dorantes #include "LocalDeviceImpl.h"
3130f1d1f3SOliver Ruiz Dorantes #include "Output.h"
3230f1d1f3SOliver Ruiz Dorantes 
3330f1d1f3SOliver Ruiz Dorantes 
34c6083519SOliver Ruiz Dorantes status_t
35c6083519SOliver Ruiz Dorantes DispatchEvent(struct hci_event_header* header, int32 code, size_t size)
36c6083519SOliver Ruiz Dorantes {
37c6083519SOliver Ruiz Dorantes 	// we only handle events
38c6083519SOliver Ruiz Dorantes 	if (GET_PORTCODE_TYPE(code)!= BT_EVENT) {
39a1163de8SOliver Ruiz Dorantes 		Output::Instance()->Post("Wrong type frame code", BLACKBOARD_KIT);
40c6083519SOliver Ruiz Dorantes 		return B_OK;
41c6083519SOliver Ruiz Dorantes 	}
42c6083519SOliver Ruiz Dorantes 
43c6083519SOliver Ruiz Dorantes 	// fetch the LocalDevice who belongs this event
44a1163de8SOliver Ruiz Dorantes 	LocalDeviceImpl* lDeviceImplementation = ((BluetoothServer*)be_app)->
45a1163de8SOliver Ruiz Dorantes 		LocateLocalDeviceImpl(GET_PORTCODE_HID(code));
46a1163de8SOliver Ruiz Dorantes 
47c6083519SOliver Ruiz Dorantes 	if (lDeviceImplementation == NULL) {
48a1163de8SOliver Ruiz Dorantes 		Output::Instance()->Post("LocalDevice could not be fetched", BLACKBOARD_KIT);
49c6083519SOliver Ruiz Dorantes 		return B_OK;
50c6083519SOliver Ruiz Dorantes 	}
51c6083519SOliver Ruiz Dorantes 
52c6083519SOliver Ruiz Dorantes 	lDeviceImplementation->HandleEvent(header);
53c6083519SOliver Ruiz Dorantes 
54c6083519SOliver Ruiz Dorantes 	return B_OK;
55c6083519SOliver Ruiz Dorantes }
56c6083519SOliver Ruiz Dorantes 
57c6083519SOliver Ruiz Dorantes 
5861da62d0SOliver Ruiz Dorantes BluetoothServer::BluetoothServer()
5961da62d0SOliver Ruiz Dorantes 	: BApplication(BLUETOOTH_SIGNATURE)
6061da62d0SOliver Ruiz Dorantes 	, fSDPThreadID(-1)
6161da62d0SOliver Ruiz Dorantes 	, fIsShuttingDown(false)
6230f1d1f3SOliver Ruiz Dorantes {
6330f1d1f3SOliver Ruiz Dorantes 	Output::Instance()->Run();
6430f1d1f3SOliver Ruiz Dorantes 	Output::Instance()->SetTitle("Bluetooth message gathering");
6530f1d1f3SOliver Ruiz Dorantes 
6630f1d1f3SOliver Ruiz Dorantes 	Output::Instance()->AddTab("General", BLACKBOARD_GENERAL);
6730f1d1f3SOliver Ruiz Dorantes 	Output::Instance()->AddTab("Device Manager", BLACKBOARD_DEVICEMANAGER);
6830f1d1f3SOliver Ruiz Dorantes 	Output::Instance()->AddTab("Kit", BLACKBOARD_KIT);
6961da62d0SOliver Ruiz Dorantes 	Output::Instance()->AddTab("SDP", BLACKBOARD_SDP);
7030f1d1f3SOliver Ruiz Dorantes 
7130f1d1f3SOliver Ruiz Dorantes 	fDeviceManager = new DeviceManager();
7230f1d1f3SOliver Ruiz Dorantes 	fLocalDevicesList.MakeEmpty();
7330f1d1f3SOliver Ruiz Dorantes 
74c6083519SOliver Ruiz Dorantes 	fEventListener2 = new BluetoothPortListener(BT_USERLAND_PORT_NAME,
75c6083519SOliver Ruiz Dorantes 		(BluetoothPortListener::port_listener_func)&DispatchEvent);
7630f1d1f3SOliver Ruiz Dorantes }
7730f1d1f3SOliver Ruiz Dorantes 
78aeb7f216SOliver Ruiz Dorantes 
7930f1d1f3SOliver Ruiz Dorantes bool BluetoothServer::QuitRequested(void)
8030f1d1f3SOliver Ruiz Dorantes {
81ccf28e4dSOliver Ruiz Dorantes 	LocalDeviceImpl* lDeviceImpl = NULL;
82aeb7f216SOliver Ruiz Dorantes 	while ((lDeviceImpl = (LocalDeviceImpl*)
83aeb7f216SOliver Ruiz Dorantes 		fLocalDevicesList.RemoveItem((int32)0)) != NULL)
84ccf28e4dSOliver Ruiz Dorantes 		delete lDeviceImpl;
8530f1d1f3SOliver Ruiz Dorantes 
86111d9460SOliver Ruiz Dorantes 	_RemoveDeskbarIcon();
87111d9460SOliver Ruiz Dorantes 
88aeb7f216SOliver Ruiz Dorantes 	// stop the SDP server thread
8961da62d0SOliver Ruiz Dorantes 	fIsShuttingDown = true;
90e97e2846SOliver Ruiz Dorantes 
9161da62d0SOliver Ruiz Dorantes 	status_t threadReturnStatus;
9261da62d0SOliver Ruiz Dorantes 	wait_for_thread(fSDPThreadID, &threadReturnStatus);
9361da62d0SOliver Ruiz Dorantes 	printf("SDP server thread exited with: %s\n", strerror(threadReturnStatus));
9461da62d0SOliver Ruiz Dorantes 
95aeb7f216SOliver Ruiz Dorantes 	// Finish quitting
96aeb7f216SOliver Ruiz Dorantes 	Output::Instance()->Lock();
97aeb7f216SOliver Ruiz Dorantes 	Output::Instance()->Quit();
98aeb7f216SOliver Ruiz Dorantes 
99aeb7f216SOliver Ruiz Dorantes 	delete fEventListener2;
10061da62d0SOliver Ruiz Dorantes 	printf("Shutting down bluetooth_server.\n");
101aeb7f216SOliver Ruiz Dorantes 
10230f1d1f3SOliver Ruiz Dorantes 	return BApplication::QuitRequested();
10330f1d1f3SOliver Ruiz Dorantes }
10430f1d1f3SOliver Ruiz Dorantes 
105e5da0ec5SOliver Ruiz Dorantes 
10630f1d1f3SOliver Ruiz Dorantes void BluetoothServer::ArgvReceived(int32 argc, char **argv)
10730f1d1f3SOliver Ruiz Dorantes {
10830f1d1f3SOliver Ruiz Dorantes 	if (argc > 1) {
10930f1d1f3SOliver Ruiz Dorantes 		if (strcmp(argv[1], "--finish") == 0)
11030f1d1f3SOliver Ruiz Dorantes 			PostMessage(B_QUIT_REQUESTED);
11130f1d1f3SOliver Ruiz Dorantes 	}
11230f1d1f3SOliver Ruiz Dorantes 
11330f1d1f3SOliver Ruiz Dorantes }
11430f1d1f3SOliver Ruiz Dorantes 
11530f1d1f3SOliver Ruiz Dorantes 
116e5da0ec5SOliver Ruiz Dorantes void BluetoothServer::ReadyToRun(void)
117e5da0ec5SOliver Ruiz Dorantes {
11861da62d0SOliver Ruiz Dorantes 	ShowWindow(Output::Instance());
11961da62d0SOliver Ruiz Dorantes 
120734ab97cSOliver Ruiz Dorantes 	fDeviceManager->StartMonitoringDevice("bluetooth/h2");
121734ab97cSOliver Ruiz Dorantes 	fDeviceManager->StartMonitoringDevice("bluetooth/h3");
122734ab97cSOliver Ruiz Dorantes 	fDeviceManager->StartMonitoringDevice("bluetooth/h4");
123734ab97cSOliver Ruiz Dorantes 	fDeviceManager->StartMonitoringDevice("bluetooth/h5");
12430f1d1f3SOliver Ruiz Dorantes 
125c6083519SOliver Ruiz Dorantes 	if (fEventListener2->Launch() != B_OK)
126aeb7f216SOliver Ruiz Dorantes 		Output::Instance()->Post("Bluetooth event listener failed\n",
127aeb7f216SOliver Ruiz Dorantes 			BLACKBOARD_GENERAL);
128c6083519SOliver Ruiz Dorantes 	else
129aeb7f216SOliver Ruiz Dorantes 		Output::Instance()->Post("Bluetooth event listener Ready\n",
130aeb7f216SOliver Ruiz Dorantes 			BLACKBOARD_GENERAL);
131111d9460SOliver Ruiz Dorantes 
132111d9460SOliver Ruiz Dorantes 	_InstallDeskbarIcon();
13361da62d0SOliver Ruiz Dorantes 
13461da62d0SOliver Ruiz Dorantes 	// Spawn the SDP server thread
13561da62d0SOliver Ruiz Dorantes 	fSDPThreadID = spawn_thread(SDPServerThread, "SDP server thread",
13661da62d0SOliver Ruiz Dorantes 		B_NORMAL_PRIORITY, this);
13761da62d0SOliver Ruiz Dorantes 
138aeb7f216SOliver Ruiz Dorantes #define _USE_FAKE_SDP_SERVER
139aeb7f216SOliver Ruiz Dorantes #ifdef _USE_FAKE_SDP_SERVER
14061da62d0SOliver Ruiz Dorantes 	if (fSDPThreadID <= 0 || resume_thread(fSDPThreadID) != B_OK) {
14161da62d0SOliver Ruiz Dorantes 		Output::Instance()->Postf(BLACKBOARD_SDP,
14261da62d0SOliver Ruiz Dorantes 			"Failed launching the SDP server thread: %x\n", fSDPThreadID);
14361da62d0SOliver Ruiz Dorantes 	}
144e97e2846SOliver Ruiz Dorantes #endif
14530f1d1f3SOliver Ruiz Dorantes }
14630f1d1f3SOliver Ruiz Dorantes 
14730f1d1f3SOliver Ruiz Dorantes 
148e5da0ec5SOliver Ruiz Dorantes void BluetoothServer::AppActivated(bool act)
149e5da0ec5SOliver Ruiz Dorantes {
15030f1d1f3SOliver Ruiz Dorantes 	printf("Activated %d\n",act);
15130f1d1f3SOliver Ruiz Dorantes }
15230f1d1f3SOliver Ruiz Dorantes 
15330f1d1f3SOliver Ruiz Dorantes 
15430f1d1f3SOliver Ruiz Dorantes void BluetoothServer::MessageReceived(BMessage* message)
15530f1d1f3SOliver Ruiz Dorantes {
15630f1d1f3SOliver Ruiz Dorantes 	BMessage reply;
157ddac4074SOliver Ruiz Dorantes 	status_t status = B_WOULD_BLOCK; // mark somehow to do not reply anything
15830f1d1f3SOliver Ruiz Dorantes 
15930f1d1f3SOliver Ruiz Dorantes 	switch (message->what)
16030f1d1f3SOliver Ruiz Dorantes 	{
16130f1d1f3SOliver Ruiz Dorantes 		case BT_MSG_ADD_DEVICE:
16230f1d1f3SOliver Ruiz Dorantes 		{
16330f1d1f3SOliver Ruiz Dorantes 			BString str;
164aeb7f216SOliver Ruiz Dorantes 
165aeb7f216SOliver Ruiz Dorantes 			message->FindString("name", &str);
166aeb7f216SOliver Ruiz Dorantes 
167e5da0ec5SOliver Ruiz Dorantes 			Output::Instance()->Postf(BLACKBOARD_GENERAL,
168e5da0ec5SOliver Ruiz Dorantes 				"Requested LocalDevice %s\n", str.String());
169aeb7f216SOliver Ruiz Dorantes 
170ecd60ae8SOliver Ruiz Dorantes 			BPath path(str.String());
171ecd60ae8SOliver Ruiz Dorantes 
172ecd60ae8SOliver Ruiz Dorantes 			LocalDeviceImpl* lDeviceImpl
173ecd60ae8SOliver Ruiz Dorantes 				= LocalDeviceImpl::CreateTransportAccessor(&path);
17430f1d1f3SOliver Ruiz Dorantes 
175ccf28e4dSOliver Ruiz Dorantes 			if (lDeviceImpl->GetID() >= 0) {
176ccf28e4dSOliver Ruiz Dorantes 				fLocalDevicesList.AddItem(lDeviceImpl);
177aeb7f216SOliver Ruiz Dorantes 
178aeb7f216SOliver Ruiz Dorantes 				Output::Instance()->AddTab("Local Device",
179aeb7f216SOliver Ruiz Dorantes 					BLACKBOARD_LD(lDeviceImpl->GetID()));
180ccf28e4dSOliver Ruiz Dorantes 				Output::Instance()->Postf(BLACKBOARD_LD(lDeviceImpl->GetID()),
181aeb7f216SOliver Ruiz Dorantes 					"LocalDevice %s id=%x added\n", str.String(),
182aeb7f216SOliver Ruiz Dorantes 					lDeviceImpl->GetID());
183ddac4074SOliver Ruiz Dorantes 
18430f1d1f3SOliver Ruiz Dorantes 			} else {
185aeb7f216SOliver Ruiz Dorantes 				Output::Instance()->Post("Adding LocalDevice hci id invalid\n",
186e5da0ec5SOliver Ruiz Dorantes 					BLACKBOARD_GENERAL);
18730f1d1f3SOliver Ruiz Dorantes 			}
18830f1d1f3SOliver Ruiz Dorantes 
18930f1d1f3SOliver Ruiz Dorantes 			status = B_WOULD_BLOCK;
190d04eb939SOliver Ruiz Dorantes 			/* TODO: This should be by user request only! */
191ccf28e4dSOliver Ruiz Dorantes 			lDeviceImpl->Launch();
192e5da0ec5SOliver Ruiz Dorantes 			break;
193ddac4074SOliver Ruiz Dorantes 		}
194ddac4074SOliver Ruiz Dorantes 
195ddac4074SOliver Ruiz Dorantes 		case BT_MSG_REMOVE_DEVICE:
196ddac4074SOliver Ruiz Dorantes 		{
197ccf28e4dSOliver Ruiz Dorantes 			LocalDeviceImpl* lDeviceImpl = LocateDelegateFromMessage(message);
1986d8521b1SOliver Ruiz Dorantes 			if (lDeviceImpl != NULL) {
199ccf28e4dSOliver Ruiz Dorantes 				fLocalDevicesList.RemoveItem(lDeviceImpl);
200ccf28e4dSOliver Ruiz Dorantes 				delete lDeviceImpl;
2016d8521b1SOliver Ruiz Dorantes 			}
202ddac4074SOliver Ruiz Dorantes 			break;
203ddac4074SOliver Ruiz Dorantes 		}
204ddac4074SOliver Ruiz Dorantes 
20530f1d1f3SOliver Ruiz Dorantes 		case BT_MSG_COUNT_LOCAL_DEVICES:
20630f1d1f3SOliver Ruiz Dorantes 			status = HandleLocalDevicesCount(message, &reply);
20730f1d1f3SOliver Ruiz Dorantes 			break;
208ddac4074SOliver Ruiz Dorantes 
209057d0dc0SOliver Ruiz Dorantes 		case BT_MSG_ACQUIRE_LOCAL_DEVICE:
21030f1d1f3SOliver Ruiz Dorantes 			status = HandleAcquireLocalDevice(message, &reply);
21130f1d1f3SOliver Ruiz Dorantes 			break;
21230f1d1f3SOliver Ruiz Dorantes 
21330f1d1f3SOliver Ruiz Dorantes 		case BT_MSG_HANDLE_SIMPLE_REQUEST:
21430f1d1f3SOliver Ruiz Dorantes 			status = HandleSimpleRequest(message, &reply);
21530f1d1f3SOliver Ruiz Dorantes 			break;
216ddac4074SOliver Ruiz Dorantes 
2173fdaa5bfSOliver Ruiz Dorantes 		case BT_MSG_GET_PROPERTY:
2183fdaa5bfSOliver Ruiz Dorantes 			status = HandleGetProperty(message, &reply);
2193fdaa5bfSOliver Ruiz Dorantes 			break;
2203fdaa5bfSOliver Ruiz Dorantes 
221aeb7f216SOliver Ruiz Dorantes 		// Handle if the bluetooth preferences is running?
22230f1d1f3SOliver Ruiz Dorantes 		case B_SOME_APP_LAUNCHED:
22330f1d1f3SOliver Ruiz Dorantes 		{
22430f1d1f3SOliver Ruiz Dorantes 			const char* signature;
225aeb7f216SOliver Ruiz Dorantes 
22630f1d1f3SOliver Ruiz Dorantes 			if (message->FindString("be:signature", &signature) == B_OK) {
22730f1d1f3SOliver Ruiz Dorantes 				printf("input_server : %s\n", signature);
22830f1d1f3SOliver Ruiz Dorantes 				if (strcmp(signature, "application/x-vnd.Be-TSKB") == 0) {
22930f1d1f3SOliver Ruiz Dorantes 
23030f1d1f3SOliver Ruiz Dorantes 				}
23130f1d1f3SOliver Ruiz Dorantes 			}
23230f1d1f3SOliver Ruiz Dorantes 			return;
23330f1d1f3SOliver Ruiz Dorantes 		}
23430f1d1f3SOliver Ruiz Dorantes 
235111d9460SOliver Ruiz Dorantes 		case BT_MSG_SERVER_SHOW_CONSOLE:
236111d9460SOliver Ruiz Dorantes 			ShowWindow(Output::Instance());
237111d9460SOliver Ruiz Dorantes 			break;
238111d9460SOliver Ruiz Dorantes 
23930f1d1f3SOliver Ruiz Dorantes 		default:
24030f1d1f3SOliver Ruiz Dorantes 			BApplication::MessageReceived(message);
24130f1d1f3SOliver Ruiz Dorantes 			break;
24230f1d1f3SOliver Ruiz Dorantes 	}
24330f1d1f3SOliver Ruiz Dorantes 
244009fac99SOliver Ruiz Dorantes 	// Can we reply right now?
245009fac99SOliver Ruiz Dorantes 	// TOD: review this condition
24630f1d1f3SOliver Ruiz Dorantes 	if (status != B_WOULD_BLOCK) {
24730f1d1f3SOliver Ruiz Dorantes 		reply.AddInt32("status", status);
24830f1d1f3SOliver Ruiz Dorantes 		message->SendReply(&reply);
2496d8521b1SOliver Ruiz Dorantes 		printf("Sending reply message for->\n");
2506d8521b1SOliver Ruiz Dorantes 		message->PrintToStream();
25130f1d1f3SOliver Ruiz Dorantes 	}
25230f1d1f3SOliver Ruiz Dorantes }
25330f1d1f3SOliver Ruiz Dorantes 
254aeb7f216SOliver Ruiz Dorantes 
25530f1d1f3SOliver Ruiz Dorantes #if 0
25630f1d1f3SOliver Ruiz Dorantes #pragma mark -
25730f1d1f3SOliver Ruiz Dorantes #endif
25830f1d1f3SOliver Ruiz Dorantes 
25930f1d1f3SOliver Ruiz Dorantes LocalDeviceImpl*
26030f1d1f3SOliver Ruiz Dorantes BluetoothServer::LocateDelegateFromMessage(BMessage* message)
26130f1d1f3SOliver Ruiz Dorantes {
262ccf28e4dSOliver Ruiz Dorantes 	LocalDeviceImpl* lDeviceImpl = NULL;
26330f1d1f3SOliver Ruiz Dorantes 	hci_id hid;
26430f1d1f3SOliver Ruiz Dorantes 
265e5da0ec5SOliver Ruiz Dorantes 	if (message->FindInt32("hci_id", &hid) == B_OK) {
266aeb7f216SOliver Ruiz Dorantes 		// Try to find out when a ID was specified
26730f1d1f3SOliver Ruiz Dorantes 		int index;
26830f1d1f3SOliver Ruiz Dorantes 		for (index = 0; index < fLocalDevicesList.CountItems(); index ++) {
269ccf28e4dSOliver Ruiz Dorantes 			lDeviceImpl = fLocalDevicesList.ItemAt(index);
270ccf28e4dSOliver Ruiz Dorantes 			if (lDeviceImpl->GetID() == hid)
27130f1d1f3SOliver Ruiz Dorantes 				break;
27230f1d1f3SOliver Ruiz Dorantes 		}
27330f1d1f3SOliver Ruiz Dorantes 	}
27430f1d1f3SOliver Ruiz Dorantes 
275ccf28e4dSOliver Ruiz Dorantes 	return lDeviceImpl;
276e5da0ec5SOliver Ruiz Dorantes 
27730f1d1f3SOliver Ruiz Dorantes }
27830f1d1f3SOliver Ruiz Dorantes 
279aeb7f216SOliver Ruiz Dorantes 
28030f1d1f3SOliver Ruiz Dorantes LocalDeviceImpl*
28130f1d1f3SOliver Ruiz Dorantes BluetoothServer::LocateLocalDeviceImpl(hci_id hid)
28230f1d1f3SOliver Ruiz Dorantes {
283aeb7f216SOliver Ruiz Dorantes 	// Try to find out when a ID was specified
28430f1d1f3SOliver Ruiz Dorantes 	int index;
28530f1d1f3SOliver Ruiz Dorantes 
28630f1d1f3SOliver Ruiz Dorantes 	for (index = 0; index < fLocalDevicesList.CountItems(); index++) {
287ccf28e4dSOliver Ruiz Dorantes 		LocalDeviceImpl* lDeviceImpl = fLocalDevicesList.ItemAt(index);
288ccf28e4dSOliver Ruiz Dorantes 		if (lDeviceImpl->GetID() == hid)
289ccf28e4dSOliver Ruiz Dorantes 			return lDeviceImpl;
29030f1d1f3SOliver Ruiz Dorantes 	}
29130f1d1f3SOliver Ruiz Dorantes 
29230f1d1f3SOliver Ruiz Dorantes 	return NULL;
29330f1d1f3SOliver Ruiz Dorantes }
29430f1d1f3SOliver Ruiz Dorantes 
29530f1d1f3SOliver Ruiz Dorantes 
29630f1d1f3SOliver Ruiz Dorantes #if 0
29730f1d1f3SOliver Ruiz Dorantes #pragma - Messages reply
29830f1d1f3SOliver Ruiz Dorantes #endif
29930f1d1f3SOliver Ruiz Dorantes 
30030f1d1f3SOliver Ruiz Dorantes status_t
30130f1d1f3SOliver Ruiz Dorantes BluetoothServer::HandleLocalDevicesCount(BMessage* message, BMessage* reply)
30230f1d1f3SOliver Ruiz Dorantes {
303ecd60ae8SOliver Ruiz Dorantes 	Output::Instance()->Post("Count Requested\n", BLACKBOARD_KIT);
304ecd60ae8SOliver Ruiz Dorantes 
30530f1d1f3SOliver Ruiz Dorantes 	return reply->AddInt32("count", fLocalDevicesList.CountItems());
30630f1d1f3SOliver Ruiz Dorantes }
30730f1d1f3SOliver Ruiz Dorantes 
30830f1d1f3SOliver Ruiz Dorantes 
30930f1d1f3SOliver Ruiz Dorantes status_t
31030f1d1f3SOliver Ruiz Dorantes BluetoothServer::HandleAcquireLocalDevice(BMessage* message, BMessage* reply)
31130f1d1f3SOliver Ruiz Dorantes {
31230f1d1f3SOliver Ruiz Dorantes 	hci_id hid;
31330f1d1f3SOliver Ruiz Dorantes 	ssize_t size;
31430f1d1f3SOliver Ruiz Dorantes 	bdaddr_t bdaddr;
315ccf28e4dSOliver Ruiz Dorantes 	LocalDeviceImpl* lDeviceImpl = NULL;
3169306af4dSOliver Ruiz Dorantes 	static int32 lastIndex = 0;
31730f1d1f3SOliver Ruiz Dorantes 
318aeb7f216SOliver Ruiz Dorantes 	if (message->FindInt32("hci_id", &hid) == B_OK)	{
319e5da0ec5SOliver Ruiz Dorantes 		Output::Instance()->Post("GetLocalDevice requested with id\n",
320e5da0ec5SOliver Ruiz Dorantes 			BLACKBOARD_KIT);
321ccf28e4dSOliver Ruiz Dorantes 		lDeviceImpl = LocateDelegateFromMessage(message);
32230f1d1f3SOliver Ruiz Dorantes 
323aeb7f216SOliver Ruiz Dorantes 	} else if (message->FindData("bdaddr", B_ANY_TYPE,
324aeb7f216SOliver Ruiz Dorantes 		(const void**)&bdaddr, &size) == B_OK) {
325aeb7f216SOliver Ruiz Dorantes 
326aeb7f216SOliver Ruiz Dorantes 		// Try to find out when the user specified the address
327e5da0ec5SOliver Ruiz Dorantes 		Output::Instance()->Post("GetLocalDevice requested with bdaddr\n",
328e5da0ec5SOliver Ruiz Dorantes 			BLACKBOARD_KIT);
329aeb7f216SOliver Ruiz Dorantes 		for (lastIndex = 0; lastIndex < fLocalDevicesList.CountItems();
330aeb7f216SOliver Ruiz Dorantes 			lastIndex ++) {
3319306af4dSOliver Ruiz Dorantes 			// TODO: Only possible if the property is available
3329306af4dSOliver Ruiz Dorantes 			// bdaddr_t local;
333ccf28e4dSOliver Ruiz Dorantes 			// lDeviceImpl = fLocalDevicesList.ItemAt(lastIndex);
334ccf28e4dSOliver Ruiz Dorantes 			// if ((lDeviceImpl->GetAddress(&local, message) == B_OK)
335e5da0ec5SOliver Ruiz Dorantes 			// 	&& bacmp(&local, &bdaddr)) {
336fbbf64a4SOliver Ruiz Dorantes 			// 	break;
337fbbf64a4SOliver Ruiz Dorantes 			// }
33830f1d1f3SOliver Ruiz Dorantes 		}
33930f1d1f3SOliver Ruiz Dorantes 
340e5da0ec5SOliver Ruiz Dorantes 	} else {
3419306af4dSOliver Ruiz Dorantes 		// Careless, any device not performing operations will be fine
3429306af4dSOliver Ruiz Dorantes 		Output::Instance()->Post("GetLocalDevice plain request\n", BLACKBOARD_KIT);
3439306af4dSOliver Ruiz Dorantes 		// from last assigned till end
344aeb7f216SOliver Ruiz Dorantes 		for (int index = lastIndex + 1;
345aeb7f216SOliver Ruiz Dorantes 			index < fLocalDevicesList.CountItems();	index++) {
346ccf28e4dSOliver Ruiz Dorantes 			lDeviceImpl= fLocalDevicesList.ItemAt(index);
347ccf28e4dSOliver Ruiz Dorantes 			printf("Requesting local device %ld\n", lDeviceImpl->GetID());
348aeb7f216SOliver Ruiz Dorantes 			if (lDeviceImpl != NULL && lDeviceImpl->Available()) {
349aeb7f216SOliver Ruiz Dorantes 				Output::Instance()->Postf(BLACKBOARD_KIT,
350aeb7f216SOliver Ruiz Dorantes 					"Device available: %lx\n", lDeviceImpl->GetID());
3519306af4dSOliver Ruiz Dorantes 				lastIndex = index;
3529306af4dSOliver Ruiz Dorantes 				break;
3539306af4dSOliver Ruiz Dorantes 			}
3549306af4dSOliver Ruiz Dorantes 		}
3559306af4dSOliver Ruiz Dorantes 
3569306af4dSOliver Ruiz Dorantes 		// from starting till last assigned if not yet found
357ccf28e4dSOliver Ruiz Dorantes 		if (lDeviceImpl == NULL) {
3589306af4dSOliver Ruiz Dorantes 			for (int index = 0; index <= lastIndex ; index ++) {
359ccf28e4dSOliver Ruiz Dorantes 				lDeviceImpl = fLocalDevicesList.ItemAt(index);
360ccf28e4dSOliver Ruiz Dorantes 				printf("Requesting local device %ld\n", lDeviceImpl->GetID());
361aeb7f216SOliver Ruiz Dorantes 				if (lDeviceImpl != NULL && lDeviceImpl->Available()) {
362aeb7f216SOliver Ruiz Dorantes 					Output::Instance()->Postf(BLACKBOARD_KIT,
363aeb7f216SOliver Ruiz Dorantes 						"Device available: %lx\n", lDeviceImpl->GetID());
3649306af4dSOliver Ruiz Dorantes 					lastIndex = index;
36530f1d1f3SOliver Ruiz Dorantes 					break;
36630f1d1f3SOliver Ruiz Dorantes 				}
36730f1d1f3SOliver Ruiz Dorantes 			}
36830f1d1f3SOliver Ruiz Dorantes 		}
3699306af4dSOliver Ruiz Dorantes 	}
37030f1d1f3SOliver Ruiz Dorantes 
371aeb7f216SOliver Ruiz Dorantes 	if (lastIndex <= fLocalDevicesList.CountItems() && lDeviceImpl != NULL
372aeb7f216SOliver Ruiz Dorantes 		&& lDeviceImpl->Available()) {
373aeb7f216SOliver Ruiz Dorantes 
374ccf28e4dSOliver Ruiz Dorantes 		hid = lDeviceImpl->GetID();
375ccf28e4dSOliver Ruiz Dorantes 		lDeviceImpl->Acquire();
3769306af4dSOliver Ruiz Dorantes 
3779306af4dSOliver Ruiz Dorantes 		Output::Instance()->Postf(BLACKBOARD_KIT, "Device acquired %lx\n", hid);
37830f1d1f3SOliver Ruiz Dorantes 		return reply->AddInt32("hci_id", hid);
37930f1d1f3SOliver Ruiz Dorantes 	}
38030f1d1f3SOliver Ruiz Dorantes 
38130f1d1f3SOliver Ruiz Dorantes 	return B_ERROR;
38230f1d1f3SOliver Ruiz Dorantes 
38330f1d1f3SOliver Ruiz Dorantes }
38430f1d1f3SOliver Ruiz Dorantes 
38530f1d1f3SOliver Ruiz Dorantes 
38630f1d1f3SOliver Ruiz Dorantes status_t
38730f1d1f3SOliver Ruiz Dorantes BluetoothServer::HandleSimpleRequest(BMessage* message, BMessage* reply)
38830f1d1f3SOliver Ruiz Dorantes {
389ccf28e4dSOliver Ruiz Dorantes 	LocalDeviceImpl* lDeviceImpl = LocateDelegateFromMessage(message);
3906d8521b1SOliver Ruiz Dorantes 	if (lDeviceImpl == NULL) {
3916d8521b1SOliver Ruiz Dorantes 		return B_ERROR;
3926d8521b1SOliver Ruiz Dorantes 	}
3936d8521b1SOliver Ruiz Dorantes 
3943fdaa5bfSOliver Ruiz Dorantes 	const char* propertyRequested;
39530f1d1f3SOliver Ruiz Dorantes 
39630f1d1f3SOliver Ruiz Dorantes 	// Find out if there is a property being requested,
39730f1d1f3SOliver Ruiz Dorantes 	if (message->FindString("property", &propertyRequested) == B_OK) {
39830f1d1f3SOliver Ruiz Dorantes 		// Check if the property has been already retrieved
399ccf28e4dSOliver Ruiz Dorantes 		if (lDeviceImpl->IsPropertyAvailable(propertyRequested)) {
40030f1d1f3SOliver Ruiz Dorantes 			// Dump everything
401ccf28e4dSOliver Ruiz Dorantes 			reply->AddMessage("properties", lDeviceImpl->GetPropertiesMessage());
40230f1d1f3SOliver Ruiz Dorantes 			return B_OK;
40330f1d1f3SOliver Ruiz Dorantes 		}
40430f1d1f3SOliver Ruiz Dorantes 	}
40530f1d1f3SOliver Ruiz Dorantes 
40630f1d1f3SOliver Ruiz Dorantes 	// we are gonna need issue the command ...
407ccf28e4dSOliver Ruiz Dorantes 	if (lDeviceImpl->ProcessSimpleRequest(DetachCurrentMessage()) == B_OK)
40830f1d1f3SOliver Ruiz Dorantes 		return B_WOULD_BLOCK;
409ccf28e4dSOliver Ruiz Dorantes 	else {
410ccf28e4dSOliver Ruiz Dorantes 		lDeviceImpl->Unregister();
41130f1d1f3SOliver Ruiz Dorantes 		return B_ERROR;
412ccf28e4dSOliver Ruiz Dorantes 	}
41330f1d1f3SOliver Ruiz Dorantes 
41430f1d1f3SOliver Ruiz Dorantes }
41530f1d1f3SOliver Ruiz Dorantes 
41630f1d1f3SOliver Ruiz Dorantes 
4173fdaa5bfSOliver Ruiz Dorantes status_t
4183fdaa5bfSOliver Ruiz Dorantes BluetoothServer::HandleGetProperty(BMessage* message, BMessage* reply)
4193fdaa5bfSOliver Ruiz Dorantes {
420ecd60ae8SOliver Ruiz Dorantes 	// User side will look for the reply in a result field and will
421ecd60ae8SOliver Ruiz Dorantes 	// not care about status fields, therefore we return OK in all cases
422ecd60ae8SOliver Ruiz Dorantes 
423ccf28e4dSOliver Ruiz Dorantes 	LocalDeviceImpl* lDeviceImpl = LocateDelegateFromMessage(message);
4246d8521b1SOliver Ruiz Dorantes 	if (lDeviceImpl == NULL) {
4256d8521b1SOliver Ruiz Dorantes 		return B_ERROR;
4266d8521b1SOliver Ruiz Dorantes 	}
4276d8521b1SOliver Ruiz Dorantes 
4283fdaa5bfSOliver Ruiz Dorantes 	const char* propertyRequested;
4293fdaa5bfSOliver Ruiz Dorantes 
4303fdaa5bfSOliver Ruiz Dorantes 	// Find out if there is a property being requested,
4313fdaa5bfSOliver Ruiz Dorantes 	if (message->FindString("property", &propertyRequested) == B_OK) {
4323fdaa5bfSOliver Ruiz Dorantes 
433aeb7f216SOliver Ruiz Dorantes 		Output::Instance()->Postf(BLACKBOARD_LD(lDeviceImpl->GetID()),
434aeb7f216SOliver Ruiz Dorantes 			"Searching %s property...\n", propertyRequested);
4353fdaa5bfSOliver Ruiz Dorantes 
4363fdaa5bfSOliver Ruiz Dorantes 		// Check if the property has been already retrieved
437ccf28e4dSOliver Ruiz Dorantes 		if (lDeviceImpl->IsPropertyAvailable(propertyRequested)) {
438040fb3eaSOliver Ruiz Dorantes 
439040fb3eaSOliver Ruiz Dorantes 			// 1 bytes requests
440dd5df905SOliver Ruiz Dorantes 			if (strcmp(propertyRequested, "hci_version") == 0
441dd5df905SOliver Ruiz Dorantes 				|| strcmp(propertyRequested, "lmp_version") == 0
442dd5df905SOliver Ruiz Dorantes 				|| strcmp(propertyRequested, "sco_mtu") == 0) {
443dd5df905SOliver Ruiz Dorantes 
444aeb7f216SOliver Ruiz Dorantes 				uint8 result = lDeviceImpl->GetPropertiesMessage()->
445aeb7f216SOliver Ruiz Dorantes 					FindInt8(propertyRequested);
4463fdaa5bfSOliver Ruiz Dorantes 				reply->AddInt32("result", result);
447dd5df905SOliver Ruiz Dorantes 
448040fb3eaSOliver Ruiz Dorantes 			// 2 bytes requests
449dd5df905SOliver Ruiz Dorantes 			} else if (strcmp(propertyRequested, "hci_revision") == 0
450dd5df905SOliver Ruiz Dorantes 					|| strcmp(propertyRequested, "lmp_subversion") == 0
451dd5df905SOliver Ruiz Dorantes 					|| strcmp(propertyRequested, "manufacturer") == 0
452dd5df905SOliver Ruiz Dorantes 					|| strcmp(propertyRequested, "acl_mtu") == 0
453dd5df905SOliver Ruiz Dorantes 					|| strcmp(propertyRequested, "acl_max_pkt") == 0
454040fb3eaSOliver Ruiz Dorantes 					|| strcmp(propertyRequested, "sco_max_pkt") == 0
455040fb3eaSOliver Ruiz Dorantes 					|| strcmp(propertyRequested, "packet_type") == 0 ) {
456dd5df905SOliver Ruiz Dorantes 
457aeb7f216SOliver Ruiz Dorantes 				uint16 result = lDeviceImpl->GetPropertiesMessage()->
458aeb7f216SOliver Ruiz Dorantes 					FindInt16(propertyRequested);
4593fdaa5bfSOliver Ruiz Dorantes 				reply->AddInt32("result", result);
460dd5df905SOliver Ruiz Dorantes 
461040fb3eaSOliver Ruiz Dorantes 			// 1 bit requests
462040fb3eaSOliver Ruiz Dorantes 			} else if (strcmp(propertyRequested, "role_switch_capable") == 0
463040fb3eaSOliver Ruiz Dorantes 					|| strcmp(propertyRequested, "encrypt_capable") == 0) {
464040fb3eaSOliver Ruiz Dorantes 
465040fb3eaSOliver Ruiz Dorantes 				bool result = lDeviceImpl->GetPropertiesMessage()->
466040fb3eaSOliver Ruiz Dorantes 					FindBool(propertyRequested);
467040fb3eaSOliver Ruiz Dorantes 
468040fb3eaSOliver Ruiz Dorantes 				reply->AddInt32("result", result);
469040fb3eaSOliver Ruiz Dorantes 
470040fb3eaSOliver Ruiz Dorantes 
471040fb3eaSOliver Ruiz Dorantes 
4723fdaa5bfSOliver Ruiz Dorantes 			} else {
473aeb7f216SOliver Ruiz Dorantes 				Output::Instance()->Postf(BLACKBOARD_LD(lDeviceImpl->GetID()),
474aeb7f216SOliver Ruiz Dorantes 					"Property %s could not be satisfied\n", propertyRequested);
4753fdaa5bfSOliver Ruiz Dorantes 			}
4763fdaa5bfSOliver Ruiz Dorantes 		}
4773fdaa5bfSOliver Ruiz Dorantes 	}
4783fdaa5bfSOliver Ruiz Dorantes 
4793fdaa5bfSOliver Ruiz Dorantes 	return B_OK;
4803fdaa5bfSOliver Ruiz Dorantes }
4813fdaa5bfSOliver Ruiz Dorantes 
482aeb7f216SOliver Ruiz Dorantes 
48330f1d1f3SOliver Ruiz Dorantes #if 0
48430f1d1f3SOliver Ruiz Dorantes #pragma mark -
48530f1d1f3SOliver Ruiz Dorantes #endif
48630f1d1f3SOliver Ruiz Dorantes 
48730f1d1f3SOliver Ruiz Dorantes int32
48861da62d0SOliver Ruiz Dorantes BluetoothServer::SDPServerThread(void* data)
48930f1d1f3SOliver Ruiz Dorantes {
49061da62d0SOliver Ruiz Dorantes 	const BluetoothServer* server = (BluetoothServer*)data;
49161da62d0SOliver Ruiz Dorantes 
492aeb7f216SOliver Ruiz Dorantes 	// Set up the SDP socket
49361da62d0SOliver Ruiz Dorantes 	struct sockaddr_l2cap loc_addr = { 0 };
49461da62d0SOliver Ruiz Dorantes 	int socketServer;
49561da62d0SOliver Ruiz Dorantes 	int client;
49661da62d0SOliver Ruiz Dorantes 	status_t status;
497e97e2846SOliver Ruiz Dorantes 	char buffer[512] = "";
49861da62d0SOliver Ruiz Dorantes 
49961da62d0SOliver Ruiz Dorantes 	Output::Instance()->Postf(BLACKBOARD_SDP, "SDP server thread up...\n");
50061da62d0SOliver Ruiz Dorantes 
501aeb7f216SOliver Ruiz Dorantes 	socketServer = socket(PF_BLUETOOTH, SOCK_STREAM, BLUETOOTH_PROTO_L2CAP);
50261da62d0SOliver Ruiz Dorantes 
50361da62d0SOliver Ruiz Dorantes 	if (socketServer < 0) {
504aeb7f216SOliver Ruiz Dorantes 		Output::Instance()->Post("Could not create server socket ...\n",
505aeb7f216SOliver Ruiz Dorantes 			BLACKBOARD_SDP);
50661da62d0SOliver Ruiz Dorantes 		return B_ERROR;
50761da62d0SOliver Ruiz Dorantes 	}
50861da62d0SOliver Ruiz Dorantes 
50961da62d0SOliver Ruiz Dorantes 	// bind socket to port 0x1001 of the first available
51061da62d0SOliver Ruiz Dorantes 	// bluetooth adapter
51161da62d0SOliver Ruiz Dorantes 	loc_addr.l2cap_family = AF_BLUETOOTH;
512*269ebc04SOliver Tappe 	loc_addr.l2cap_bdaddr = BDADDR_ANY;
513e97e2846SOliver Ruiz Dorantes 	loc_addr.l2cap_psm = B_HOST_TO_LENDIAN_INT16(1);
51461da62d0SOliver Ruiz Dorantes 	loc_addr.l2cap_len = sizeof(struct sockaddr_l2cap);
51561da62d0SOliver Ruiz Dorantes 
516aeb7f216SOliver Ruiz Dorantes 	status = bind(socketServer, (struct sockaddr*)&loc_addr,
517aeb7f216SOliver Ruiz Dorantes 		sizeof(struct sockaddr_l2cap));
51861da62d0SOliver Ruiz Dorantes 
51961da62d0SOliver Ruiz Dorantes 	if (status < 0) {
520aeb7f216SOliver Ruiz Dorantes 		Output::Instance()->Postf(BLACKBOARD_SDP,
521aeb7f216SOliver Ruiz Dorantes 			"Could not bind server socket %d ...\n", status);
52261da62d0SOliver Ruiz Dorantes 		return status;
52361da62d0SOliver Ruiz Dorantes 	}
52461da62d0SOliver Ruiz Dorantes 
52561da62d0SOliver Ruiz Dorantes 	// setsockopt(sock, SOL_L2CAP, SO_L2CAP_OMTU, &omtu, len );
52661da62d0SOliver Ruiz Dorantes 	// getsockopt(sock, SOL_L2CAP, SO_L2CAP_IMTU, &omtu, &len );
52761da62d0SOliver Ruiz Dorantes 
528aeb7f216SOliver Ruiz Dorantes 	// Listen for up to 10 connections
52961da62d0SOliver Ruiz Dorantes 	status = listen(socketServer, 10);
53061da62d0SOliver Ruiz Dorantes 
53161da62d0SOliver Ruiz Dorantes 	if (status != B_OK) {
532aeb7f216SOliver Ruiz Dorantes 		Output::Instance()->Postf(BLACKBOARD_SDP,
533aeb7f216SOliver Ruiz Dorantes 			"Could not listen server socket %d ...\n", status);
53461da62d0SOliver Ruiz Dorantes 		return status;
53561da62d0SOliver Ruiz Dorantes 	}
53661da62d0SOliver Ruiz Dorantes 
53761da62d0SOliver Ruiz Dorantes 	while (!server->fIsShuttingDown) {
53861da62d0SOliver Ruiz Dorantes 
539aeb7f216SOliver Ruiz Dorantes 		Output::Instance()->Postf(BLACKBOARD_SDP,
540aeb7f216SOliver Ruiz Dorantes 			"Waiting connection for socket %d ...\n", socketServer);
54161da62d0SOliver Ruiz Dorantes 
54261da62d0SOliver Ruiz Dorantes 		uint len = sizeof(struct sockaddr_l2cap);
54361da62d0SOliver Ruiz Dorantes 		client = accept(socketServer, (struct sockaddr*)&loc_addr, &len);
54461da62d0SOliver Ruiz Dorantes 
545aeb7f216SOliver Ruiz Dorantes 		Output::Instance()->Postf(BLACKBOARD_SDP,
546aeb7f216SOliver Ruiz Dorantes 			"Incomming connection... %ld\n", client);
54761da62d0SOliver Ruiz Dorantes 
548e97e2846SOliver Ruiz Dorantes 		ssize_t receivedSize;
549e97e2846SOliver Ruiz Dorantes 
550e97e2846SOliver Ruiz Dorantes 		do {
551e97e2846SOliver Ruiz Dorantes 			receivedSize = recv(client, buffer, 29 , 0);
552e97e2846SOliver Ruiz Dorantes 			if (receivedSize < 0)
553aeb7f216SOliver Ruiz Dorantes 				Output::Instance()->Post("Error reading client socket\n",
554aeb7f216SOliver Ruiz Dorantes 					BLACKBOARD_SDP);
555e97e2846SOliver Ruiz Dorantes 			else {
556aeb7f216SOliver Ruiz Dorantes 				Output::Instance()->Postf(BLACKBOARD_SDP,
557aeb7f216SOliver Ruiz Dorantes 					"Received from SDP client: %ld:\n", receivedSize);
558e97e2846SOliver Ruiz Dorantes 				for (int i = 0; i < receivedSize ; i++)
559e97e2846SOliver Ruiz Dorantes 					Output::Instance()->Postf(BLACKBOARD_SDP, "%x:", buffer[i]);
56061da62d0SOliver Ruiz Dorantes 
56161da62d0SOliver Ruiz Dorantes 				Output::Instance()->Post("\n", BLACKBOARD_SDP);
562e97e2846SOliver Ruiz Dorantes 			}
563e97e2846SOliver Ruiz Dorantes 		} while (receivedSize >= 0);
56461da62d0SOliver Ruiz Dorantes 
56561da62d0SOliver Ruiz Dorantes 		snooze(5000000);
566aeb7f216SOliver Ruiz Dorantes 		Output::Instance()->Post("\nWaiting for next connection...\n",
567aeb7f216SOliver Ruiz Dorantes 			BLACKBOARD_SDP);
56861da62d0SOliver Ruiz Dorantes 	}
56961da62d0SOliver Ruiz Dorantes 
570aeb7f216SOliver Ruiz Dorantes 	// Close the socket
57161da62d0SOliver Ruiz Dorantes 	close(socketServer);
57230f1d1f3SOliver Ruiz Dorantes 
57330f1d1f3SOliver Ruiz Dorantes 	return B_NO_ERROR;
57430f1d1f3SOliver Ruiz Dorantes }
57530f1d1f3SOliver Ruiz Dorantes 
57630f1d1f3SOliver Ruiz Dorantes 
57730f1d1f3SOliver Ruiz Dorantes void
57830f1d1f3SOliver Ruiz Dorantes BluetoothServer::ShowWindow(BWindow* pWindow)
57930f1d1f3SOliver Ruiz Dorantes {
58030f1d1f3SOliver Ruiz Dorantes 	pWindow->Lock();
58130f1d1f3SOliver Ruiz Dorantes 	if (pWindow->IsHidden())
58230f1d1f3SOliver Ruiz Dorantes 		pWindow->Show();
58330f1d1f3SOliver Ruiz Dorantes 	else
58430f1d1f3SOliver Ruiz Dorantes 		pWindow->Activate();
58530f1d1f3SOliver Ruiz Dorantes 	pWindow->Unlock();
58630f1d1f3SOliver Ruiz Dorantes }
58730f1d1f3SOliver Ruiz Dorantes 
58830f1d1f3SOliver Ruiz Dorantes 
589111d9460SOliver Ruiz Dorantes void
590111d9460SOliver Ruiz Dorantes BluetoothServer::_InstallDeskbarIcon()
591111d9460SOliver Ruiz Dorantes {
592111d9460SOliver Ruiz Dorantes 	app_info appInfo;
593111d9460SOliver Ruiz Dorantes 	be_app->GetAppInfo(&appInfo);
594111d9460SOliver Ruiz Dorantes 
595111d9460SOliver Ruiz Dorantes 	BDeskbar deskbar;
596111d9460SOliver Ruiz Dorantes 
597111d9460SOliver Ruiz Dorantes 	if (deskbar.HasItem(kDeskbarItemName)) {
598111d9460SOliver Ruiz Dorantes 		_RemoveDeskbarIcon();
599111d9460SOliver Ruiz Dorantes 	}
600111d9460SOliver Ruiz Dorantes 
601111d9460SOliver Ruiz Dorantes 	status_t res = deskbar.AddItem(&appInfo.ref);
602111d9460SOliver Ruiz Dorantes 	if (res != B_OK) {
603111d9460SOliver Ruiz Dorantes 		printf("Failed adding deskbar icon: %ld\n", res);
604111d9460SOliver Ruiz Dorantes 	}
605111d9460SOliver Ruiz Dorantes }
606111d9460SOliver Ruiz Dorantes 
607111d9460SOliver Ruiz Dorantes 
608111d9460SOliver Ruiz Dorantes void
609111d9460SOliver Ruiz Dorantes BluetoothServer::_RemoveDeskbarIcon()
610111d9460SOliver Ruiz Dorantes {
611111d9460SOliver Ruiz Dorantes 	BDeskbar deskbar;
612111d9460SOliver Ruiz Dorantes 	status_t res = deskbar.RemoveItem(kDeskbarItemName);
613111d9460SOliver Ruiz Dorantes 	if (res != B_OK) {
614111d9460SOliver Ruiz Dorantes 		printf("Failed removing Deskbar icon: %ld: \n", res);
615111d9460SOliver Ruiz Dorantes 	}
616111d9460SOliver Ruiz Dorantes }
617111d9460SOliver Ruiz Dorantes 
618aeb7f216SOliver Ruiz Dorantes 
61930f1d1f3SOliver Ruiz Dorantes #if 0
62030f1d1f3SOliver Ruiz Dorantes #pragma mark -
62130f1d1f3SOliver Ruiz Dorantes #endif
62230f1d1f3SOliver Ruiz Dorantes 
62330f1d1f3SOliver Ruiz Dorantes int
62430f1d1f3SOliver Ruiz Dorantes main(int /*argc*/, char** /*argv*/)
62530f1d1f3SOliver Ruiz Dorantes {
62630f1d1f3SOliver Ruiz Dorantes 	setbuf(stdout, NULL);
62730f1d1f3SOliver Ruiz Dorantes 
62830f1d1f3SOliver Ruiz Dorantes 	BluetoothServer* bluetoothServer = new BluetoothServer;
62930f1d1f3SOliver Ruiz Dorantes 
63030f1d1f3SOliver Ruiz Dorantes 	bluetoothServer->Run();
63130f1d1f3SOliver Ruiz Dorantes 	delete bluetoothServer;
63230f1d1f3SOliver Ruiz Dorantes 
63330f1d1f3SOliver Ruiz Dorantes 	return 0;
63430f1d1f3SOliver Ruiz Dorantes }
63530f1d1f3SOliver Ruiz Dorantes 
636