xref: /haiku/src/servers/bluetooth/BluetoothServer.cpp (revision 057d0dc02a89641a28855818f5d2d755f77cbdc6)
130f1d1f3SOliver Ruiz Dorantes /*
230f1d1f3SOliver Ruiz Dorantes  * Copyright 2007 Oliver Ruiz Dorantes, oliver.ruiz.dorantes_at_gmail.com
330f1d1f3SOliver Ruiz Dorantes  *
430f1d1f3SOliver Ruiz Dorantes  * All rights reserved. Distributed under the terms of the MIT License.
530f1d1f3SOliver Ruiz Dorantes  *
630f1d1f3SOliver Ruiz Dorantes  */
730f1d1f3SOliver Ruiz Dorantes 
830f1d1f3SOliver Ruiz Dorantes #include <stdio.h>
930f1d1f3SOliver Ruiz Dorantes #include <fcntl.h>
1030f1d1f3SOliver Ruiz Dorantes #include <unistd.h>
1130f1d1f3SOliver Ruiz Dorantes 
1230f1d1f3SOliver Ruiz Dorantes 
1330f1d1f3SOliver Ruiz Dorantes #include <Entry.h>
1430f1d1f3SOliver Ruiz Dorantes #include <Path.h>
1530f1d1f3SOliver Ruiz Dorantes #include <Message.h>
1630f1d1f3SOliver Ruiz Dorantes #include <Directory.h>
1730f1d1f3SOliver Ruiz Dorantes #include <String.h>
1830f1d1f3SOliver Ruiz Dorantes #include <Roster.h>
1930f1d1f3SOliver Ruiz Dorantes 
2030f1d1f3SOliver Ruiz Dorantes 
2130f1d1f3SOliver Ruiz Dorantes #include <TypeConstants.h>
2230f1d1f3SOliver Ruiz Dorantes #include <syslog.h>
2330f1d1f3SOliver Ruiz Dorantes 
2430f1d1f3SOliver Ruiz Dorantes #include <bluetoothserver_p.h>
2530f1d1f3SOliver Ruiz Dorantes #include <bluetooth/HCI/btHCI_command.h>
2630f1d1f3SOliver Ruiz Dorantes #include <bluetooth/bluetooth_util.h>
2730f1d1f3SOliver Ruiz Dorantes 
2830f1d1f3SOliver Ruiz Dorantes #include "LocalDeviceImpl.h"
2930f1d1f3SOliver Ruiz Dorantes #include "BluetoothServer.h"
3030f1d1f3SOliver Ruiz Dorantes #include "Output.h"
3130f1d1f3SOliver Ruiz Dorantes 
3230f1d1f3SOliver Ruiz Dorantes 
3330f1d1f3SOliver Ruiz Dorantes BluetoothServer::BluetoothServer() : BApplication(BLUETOOTH_SIGNATURE)
3430f1d1f3SOliver Ruiz Dorantes {
3530f1d1f3SOliver Ruiz Dorantes 	Output::Instance()->Run();
3630f1d1f3SOliver Ruiz Dorantes 	Output::Instance()->SetTitle("Bluetooth message gathering");
3730f1d1f3SOliver Ruiz Dorantes 
3830f1d1f3SOliver Ruiz Dorantes 	Output::Instance()->AddTab("General", BLACKBOARD_GENERAL);
3930f1d1f3SOliver Ruiz Dorantes 	Output::Instance()->AddTab("Device Manager", BLACKBOARD_DEVICEMANAGER);
4030f1d1f3SOliver Ruiz Dorantes 	Output::Instance()->AddTab("Events", BLACKBOARD_EVENTS);
4130f1d1f3SOliver Ruiz Dorantes 	Output::Instance()->AddTab("Kit", BLACKBOARD_KIT);
4230f1d1f3SOliver Ruiz Dorantes 
4330f1d1f3SOliver Ruiz Dorantes 
4430f1d1f3SOliver Ruiz Dorantes 	ShowWindow(Output::Instance());
4530f1d1f3SOliver Ruiz Dorantes 
4630f1d1f3SOliver Ruiz Dorantes     fDeviceManager = new DeviceManager();
4730f1d1f3SOliver Ruiz Dorantes 	fLocalDevicesList.MakeEmpty();
4830f1d1f3SOliver Ruiz Dorantes 
4930f1d1f3SOliver Ruiz Dorantes 	fEventListener = spawn_thread(notification_Thread, "BT port listener" , B_DISPLAY_PRIORITY , this);
5030f1d1f3SOliver Ruiz Dorantes 
5130f1d1f3SOliver Ruiz Dorantes 
5230f1d1f3SOliver Ruiz Dorantes }
5330f1d1f3SOliver Ruiz Dorantes 
5430f1d1f3SOliver Ruiz Dorantes bool BluetoothServer::QuitRequested(void)
5530f1d1f3SOliver Ruiz Dorantes {
5630f1d1f3SOliver Ruiz Dorantes 	// Finish quitting
5730f1d1f3SOliver Ruiz Dorantes 	Output::Instance()->Lock();
5830f1d1f3SOliver Ruiz Dorantes 	Output::Instance()->Quit();
5930f1d1f3SOliver Ruiz Dorantes 
6030f1d1f3SOliver Ruiz Dorantes 
6130f1d1f3SOliver Ruiz Dorantes /*	HCIDelegate *hd = NULL;
6230f1d1f3SOliver Ruiz Dorantes 	while ((hd = (HCIDelegate *)fDelegatesList.RemoveItem((int32)0)) !=NULL)
6330f1d1f3SOliver Ruiz Dorantes 		delete hd;
6430f1d1f3SOliver Ruiz Dorantes */
6530f1d1f3SOliver Ruiz Dorantes 
6630f1d1f3SOliver Ruiz Dorantes 	printf("Accepting quitting of the application\n");
6730f1d1f3SOliver Ruiz Dorantes 	return BApplication::QuitRequested();
6830f1d1f3SOliver Ruiz Dorantes }
6930f1d1f3SOliver Ruiz Dorantes 
7030f1d1f3SOliver Ruiz Dorantes void BluetoothServer::ArgvReceived(int32 argc, char **argv)
7130f1d1f3SOliver Ruiz Dorantes {
7230f1d1f3SOliver Ruiz Dorantes 	if (argc>1) {
7330f1d1f3SOliver Ruiz Dorantes 		if (strcmp(argv[1], "--finish") == 0)
7430f1d1f3SOliver Ruiz Dorantes 			PostMessage(B_QUIT_REQUESTED);
7530f1d1f3SOliver Ruiz Dorantes 
7630f1d1f3SOliver Ruiz Dorantes 	    else {
7730f1d1f3SOliver Ruiz Dorantes 
7830f1d1f3SOliver Ruiz Dorantes 
7930f1d1f3SOliver Ruiz Dorantes 	    }
8030f1d1f3SOliver Ruiz Dorantes 	}
8130f1d1f3SOliver Ruiz Dorantes 
8230f1d1f3SOliver Ruiz Dorantes }
8330f1d1f3SOliver Ruiz Dorantes 
8430f1d1f3SOliver Ruiz Dorantes 
8530f1d1f3SOliver Ruiz Dorantes void BluetoothServer::ReadyToRun(void) {
8630f1d1f3SOliver Ruiz Dorantes 
8730f1d1f3SOliver Ruiz Dorantes     fDeviceManager->StartMonitoringDevice("bluetooth/h2generic");
8830f1d1f3SOliver Ruiz Dorantes 
8930f1d1f3SOliver Ruiz Dorantes 
9030f1d1f3SOliver Ruiz Dorantes 	// Launch the notifier thread
9130f1d1f3SOliver Ruiz Dorantes 	if ( resume_thread(fEventListener) != B_OK )
9230f1d1f3SOliver Ruiz Dorantes 	{
9330f1d1f3SOliver Ruiz Dorantes 		Output::Instance()->Post("Bluetooth port listener failed\n", BLACKBOARD_GENERAL);
9430f1d1f3SOliver Ruiz Dorantes 	}
9530f1d1f3SOliver Ruiz Dorantes 
9630f1d1f3SOliver Ruiz Dorantes     Output::Instance()->Post("Bluetooth server Ready\n", BLACKBOARD_GENERAL);
9730f1d1f3SOliver Ruiz Dorantes }
9830f1d1f3SOliver Ruiz Dorantes 
9930f1d1f3SOliver Ruiz Dorantes void BluetoothServer::AppActivated(bool act) {
10030f1d1f3SOliver Ruiz Dorantes 
10130f1d1f3SOliver Ruiz Dorantes 	printf("Activated %d\n",act);
10230f1d1f3SOliver Ruiz Dorantes 
10330f1d1f3SOliver Ruiz Dorantes }
10430f1d1f3SOliver Ruiz Dorantes 
10530f1d1f3SOliver Ruiz Dorantes 
10630f1d1f3SOliver Ruiz Dorantes 
10730f1d1f3SOliver Ruiz Dorantes void BluetoothServer::MessageReceived(BMessage *message)
10830f1d1f3SOliver Ruiz Dorantes {
10930f1d1f3SOliver Ruiz Dorantes 	BMessage reply;
11030f1d1f3SOliver Ruiz Dorantes 	status_t status = B_WOULD_BLOCK; // mark somehow.. do not reply anything
11130f1d1f3SOliver Ruiz Dorantes 
11230f1d1f3SOliver Ruiz Dorantes 	switch(message->what)
11330f1d1f3SOliver Ruiz Dorantes 	{
11430f1d1f3SOliver Ruiz Dorantes 
11530f1d1f3SOliver Ruiz Dorantes 		case BT_MSG_ADD_DEVICE:
11630f1d1f3SOliver Ruiz Dorantes 		{
11730f1d1f3SOliver Ruiz Dorantes 			BString str;
11830f1d1f3SOliver Ruiz Dorantes 	        message->FindString("name", &str);
11930f1d1f3SOliver Ruiz Dorantes 			BPath path(str.String());
12030f1d1f3SOliver Ruiz Dorantes 
12130f1d1f3SOliver Ruiz Dorantes 	  	    (Output::Instance()->Post( str.String(), BLACKBOARD_GENERAL));
12230f1d1f3SOliver Ruiz Dorantes      	    (Output::Instance()->Post(" requested LocalDevice\n", BLACKBOARD_GENERAL));
12330f1d1f3SOliver Ruiz Dorantes 
12430f1d1f3SOliver Ruiz Dorantes 	        LocalDeviceImpl* ldi = LocalDeviceImpl::CreateTransportAccessor(&path);
12530f1d1f3SOliver Ruiz Dorantes 
12630f1d1f3SOliver Ruiz Dorantes 	        if (ldi->GetID() >= 0) {
12730f1d1f3SOliver Ruiz Dorantes                       fLocalDevicesList.AddItem(ldi);
12830f1d1f3SOliver Ruiz Dorantes 					  Output::Instance()->AddTab("Local Device", BLACKBOARD_LD_OFFSET + ldi->GetID());
12930f1d1f3SOliver Ruiz Dorantes 				  	  (Output::Instance()->Post( str.String(), BLACKBOARD_LD_OFFSET + ldi->GetID()));
13030f1d1f3SOliver Ruiz Dorantes 					  (Output::Instance()->Post(" LocalDevice added\n", BLACKBOARD_LD_OFFSET + ldi->GetID()));
13130f1d1f3SOliver Ruiz Dorantes 
13230f1d1f3SOliver Ruiz Dorantes 
13330f1d1f3SOliver Ruiz Dorantes             } else {
13430f1d1f3SOliver Ruiz Dorantes 					  (Output::Instance()->Post("Adding LocalDevice failed\n", BLACKBOARD_GENERAL));
13530f1d1f3SOliver Ruiz Dorantes             }
13630f1d1f3SOliver Ruiz Dorantes 
13730f1d1f3SOliver Ruiz Dorantes             status = B_WOULD_BLOCK;
13830f1d1f3SOliver Ruiz Dorantes         }
13930f1d1f3SOliver Ruiz Dorantes 
14030f1d1f3SOliver Ruiz Dorantes 		case BT_MSG_COUNT_LOCAL_DEVICES:
14130f1d1f3SOliver Ruiz Dorantes 			status = HandleLocalDevicesCount(message, &reply);
14230f1d1f3SOliver Ruiz Dorantes     	break;
14330f1d1f3SOliver Ruiz Dorantes 
144*057d0dc0SOliver Ruiz Dorantes 		case BT_MSG_ACQUIRE_LOCAL_DEVICE:
14530f1d1f3SOliver Ruiz Dorantes 			status = HandleAcquireLocalDevice(message, &reply);
14630f1d1f3SOliver Ruiz Dorantes 		break;
14730f1d1f3SOliver Ruiz Dorantes 
14830f1d1f3SOliver Ruiz Dorantes         case BT_MSG_GET_FRIENDLY_NAME:
14930f1d1f3SOliver Ruiz Dorantes             status = HandleGetFriendlyName(message, &reply);
15030f1d1f3SOliver Ruiz Dorantes         break;
15130f1d1f3SOliver Ruiz Dorantes 
15230f1d1f3SOliver Ruiz Dorantes         case BT_MSG_GET_ADDRESS:
15330f1d1f3SOliver Ruiz Dorantes             status = HandleGetAddress(message, &reply);
15430f1d1f3SOliver Ruiz Dorantes         break;
15530f1d1f3SOliver Ruiz Dorantes 
15630f1d1f3SOliver Ruiz Dorantes         case BT_MSG_HANDLE_SIMPLE_REQUEST:
15730f1d1f3SOliver Ruiz Dorantes             status = HandleSimpleRequest(message, &reply);
15830f1d1f3SOliver Ruiz Dorantes         break;
15930f1d1f3SOliver Ruiz Dorantes 
16030f1d1f3SOliver Ruiz Dorantes 
161009fac99SOliver Ruiz Dorantes 		/* Handle if the bluetooth preferences is running?? */
16230f1d1f3SOliver Ruiz Dorantes 		case B_SOME_APP_LAUNCHED:
16330f1d1f3SOliver Ruiz Dorantes    		{
16430f1d1f3SOliver Ruiz Dorantes 			const char *signature;
16530f1d1f3SOliver Ruiz Dorantes 			// TODO: what's this for?
16630f1d1f3SOliver Ruiz Dorantes 			if (message->FindString("be:signature", &signature)==B_OK) {
16730f1d1f3SOliver Ruiz Dorantes 				printf("input_server : %s\n", signature);
16830f1d1f3SOliver Ruiz Dorantes 				if (strcmp(signature, "application/x-vnd.Be-TSKB")==0) {
16930f1d1f3SOliver Ruiz Dorantes 
17030f1d1f3SOliver Ruiz Dorantes 				}
17130f1d1f3SOliver Ruiz Dorantes 			}
17230f1d1f3SOliver Ruiz Dorantes 			return;
17330f1d1f3SOliver Ruiz Dorantes 		}
17430f1d1f3SOliver Ruiz Dorantes 
17530f1d1f3SOliver Ruiz Dorantes 
17630f1d1f3SOliver Ruiz Dorantes 		default:
17730f1d1f3SOliver Ruiz Dorantes 			BApplication::MessageReceived(message);
17830f1d1f3SOliver Ruiz Dorantes 		break;
17930f1d1f3SOliver Ruiz Dorantes 	}
18030f1d1f3SOliver Ruiz Dorantes 
181009fac99SOliver Ruiz Dorantes 	// Can we reply right now?
182009fac99SOliver Ruiz Dorantes 	// TOD: review this condition
18330f1d1f3SOliver Ruiz Dorantes     if (status != B_WOULD_BLOCK) {
18430f1d1f3SOliver Ruiz Dorantes 	    reply.AddInt32("status", status);
18530f1d1f3SOliver Ruiz Dorantes 	    message->SendReply(&reply);
18630f1d1f3SOliver Ruiz Dorantes 	    printf("Sending reply message\n");
18730f1d1f3SOliver Ruiz Dorantes 	}
18830f1d1f3SOliver Ruiz Dorantes 
18930f1d1f3SOliver Ruiz Dorantes }
19030f1d1f3SOliver Ruiz Dorantes 
19130f1d1f3SOliver Ruiz Dorantes #if 0
19230f1d1f3SOliver Ruiz Dorantes #pragma mark -
19330f1d1f3SOliver Ruiz Dorantes #endif
19430f1d1f3SOliver Ruiz Dorantes 
19530f1d1f3SOliver Ruiz Dorantes LocalDeviceImpl*
19630f1d1f3SOliver Ruiz Dorantes BluetoothServer::LocateDelegateFromMessage(BMessage* message)
19730f1d1f3SOliver Ruiz Dorantes {
19830f1d1f3SOliver Ruiz Dorantes     LocalDeviceImpl* ldi = NULL;
19930f1d1f3SOliver Ruiz Dorantes  	hci_id hid;
20030f1d1f3SOliver Ruiz Dorantes 
20130f1d1f3SOliver Ruiz Dorantes     if (message->FindInt32("hci_id", &hid) == B_OK)
20230f1d1f3SOliver Ruiz Dorantes 	{
20330f1d1f3SOliver Ruiz Dorantes 	    /* Try to find out when a ID was specified */
20430f1d1f3SOliver Ruiz Dorantes 	    int index;
20530f1d1f3SOliver Ruiz Dorantes 
20630f1d1f3SOliver Ruiz Dorantes 	    for (index = 0; index < fLocalDevicesList.CountItems() ; index ++) {
20730f1d1f3SOliver Ruiz Dorantes 
20830f1d1f3SOliver Ruiz Dorantes 		    ldi = fLocalDevicesList.ItemAt(index);
20930f1d1f3SOliver Ruiz Dorantes 		    if (ldi->GetID() == hid)  {
21030f1d1f3SOliver Ruiz Dorantes 		        break;
21130f1d1f3SOliver Ruiz Dorantes 		    }
21230f1d1f3SOliver Ruiz Dorantes         }
21330f1d1f3SOliver Ruiz Dorantes     }
21430f1d1f3SOliver Ruiz Dorantes 
21530f1d1f3SOliver Ruiz Dorantes     return ldi;
21630f1d1f3SOliver Ruiz Dorantes }
21730f1d1f3SOliver Ruiz Dorantes 
21830f1d1f3SOliver Ruiz Dorantes LocalDeviceImpl*
21930f1d1f3SOliver Ruiz Dorantes BluetoothServer::LocateLocalDeviceImpl(hci_id hid)
22030f1d1f3SOliver Ruiz Dorantes {
22130f1d1f3SOliver Ruiz Dorantes 
22230f1d1f3SOliver Ruiz Dorantes     /* Try to find out when a ID was specified */
22330f1d1f3SOliver Ruiz Dorantes     int index;
22430f1d1f3SOliver Ruiz Dorantes 
22530f1d1f3SOliver Ruiz Dorantes     for (index = 0; index < fLocalDevicesList.CountItems() ; index ++) {
22630f1d1f3SOliver Ruiz Dorantes 
22730f1d1f3SOliver Ruiz Dorantes 	    LocalDeviceImpl* ldi = fLocalDevicesList.ItemAt(index);
22830f1d1f3SOliver Ruiz Dorantes 	    if (ldi->GetID() == hid)  {
22930f1d1f3SOliver Ruiz Dorantes 	        return ldi;
23030f1d1f3SOliver Ruiz Dorantes 	    }
23130f1d1f3SOliver Ruiz Dorantes     }
23230f1d1f3SOliver Ruiz Dorantes 
23330f1d1f3SOliver Ruiz Dorantes     return NULL;
23430f1d1f3SOliver Ruiz Dorantes }
23530f1d1f3SOliver Ruiz Dorantes 
23630f1d1f3SOliver Ruiz Dorantes 
23730f1d1f3SOliver Ruiz Dorantes #if 0
23830f1d1f3SOliver Ruiz Dorantes #pragma - Messages reply
23930f1d1f3SOliver Ruiz Dorantes #endif
24030f1d1f3SOliver Ruiz Dorantes 
24130f1d1f3SOliver Ruiz Dorantes status_t
24230f1d1f3SOliver Ruiz Dorantes BluetoothServer::HandleLocalDevicesCount(BMessage* message, BMessage* reply)
24330f1d1f3SOliver Ruiz Dorantes {
24430f1d1f3SOliver Ruiz Dorantes 	return reply->AddInt32("count", fLocalDevicesList.CountItems());
24530f1d1f3SOliver Ruiz Dorantes }
24630f1d1f3SOliver Ruiz Dorantes 
24730f1d1f3SOliver Ruiz Dorantes 
24830f1d1f3SOliver Ruiz Dorantes status_t
24930f1d1f3SOliver Ruiz Dorantes BluetoothServer::HandleAcquireLocalDevice(BMessage* message, BMessage* reply)
25030f1d1f3SOliver Ruiz Dorantes {
25130f1d1f3SOliver Ruiz Dorantes 	hci_id hid;
25230f1d1f3SOliver Ruiz Dorantes 	ssize_t size;
25330f1d1f3SOliver Ruiz Dorantes 	bdaddr_t bdaddr;
25430f1d1f3SOliver Ruiz Dorantes 	LocalDeviceImpl* ldi = NULL;
25530f1d1f3SOliver Ruiz Dorantes     int32 index = 0;
25630f1d1f3SOliver Ruiz Dorantes 
25730f1d1f3SOliver Ruiz Dorantes 	if (message->FindInt32("hci_id", &hid) == B_OK)
25830f1d1f3SOliver Ruiz Dorantes 	{
25930f1d1f3SOliver Ruiz Dorantes 	    Output::Instance()->Post("GetLocalDevice requested with id\n", BLACKBOARD_KIT);
26030f1d1f3SOliver Ruiz Dorantes         ldi = LocateDelegateFromMessage(message);
26130f1d1f3SOliver Ruiz Dorantes 
26230f1d1f3SOliver Ruiz Dorantes 	} else if (message->FindData("bdaddr", B_ANY_TYPE, (const void**)&bdaddr, &size ) == B_OK)
26330f1d1f3SOliver Ruiz Dorantes 	{
26430f1d1f3SOliver Ruiz Dorantes 	    /* Try to find out when the user specified the address */
26530f1d1f3SOliver Ruiz Dorantes 	    Output::Instance()->Post("GetLocalDevice requested with bdaddr\n", BLACKBOARD_KIT);
26630f1d1f3SOliver Ruiz Dorantes 	    for (index = 0; index < fLocalDevicesList.CountItems() ; index ++) {
26730f1d1f3SOliver Ruiz Dorantes 
26830f1d1f3SOliver Ruiz Dorantes 	        bdaddr_t local;
26930f1d1f3SOliver Ruiz Dorantes 		    ldi = fLocalDevicesList.ItemAt(index);
27030f1d1f3SOliver Ruiz Dorantes 		    if ((ldi->GetAddress(&local, message) == B_OK) && bacmp(&local, &bdaddr))  {
27130f1d1f3SOliver Ruiz Dorantes 		        break;
27230f1d1f3SOliver Ruiz Dorantes 		    }
27330f1d1f3SOliver Ruiz Dorantes         }
27430f1d1f3SOliver Ruiz Dorantes 
27530f1d1f3SOliver Ruiz Dorantes 	} else
27630f1d1f3SOliver Ruiz Dorantes 	{
27730f1d1f3SOliver Ruiz Dorantes 	    /* Careless, any device not performing operations will be fine */
27830f1d1f3SOliver Ruiz Dorantes 	    Output::Instance()->Post("GetLocalDevice requested\n", BLACKBOARD_KIT);
27930f1d1f3SOliver Ruiz Dorantes 	    for (index = 0; index < fLocalDevicesList.CountItems() ; index ++) {
28030f1d1f3SOliver Ruiz Dorantes 
28130f1d1f3SOliver Ruiz Dorantes 		    ldi = fLocalDevicesList.ItemAt(index);
28230f1d1f3SOliver Ruiz Dorantes 		    printf("Requesting local device %ld\n", ldi->GetID());
28330f1d1f3SOliver Ruiz Dorantes 		    if (ldi != NULL && ldi->Available())
28430f1d1f3SOliver Ruiz Dorantes 		    {
28530f1d1f3SOliver Ruiz Dorantes   			    printf("dev ours %ld\n", ldi->GetID());
28630f1d1f3SOliver Ruiz Dorantes 		        break;
28730f1d1f3SOliver Ruiz Dorantes 		    }
28830f1d1f3SOliver Ruiz Dorantes 
28930f1d1f3SOliver Ruiz Dorantes         }
29030f1d1f3SOliver Ruiz Dorantes 	}
29130f1d1f3SOliver Ruiz Dorantes 
29230f1d1f3SOliver Ruiz Dorantes     if (index <= fLocalDevicesList.CountItems() && ldi != NULL && ldi->Available())
29330f1d1f3SOliver Ruiz Dorantes 	{
29430f1d1f3SOliver Ruiz Dorantes 	    Output::Instance()->Post("Device acquired\n", BLACKBOARD_KIT);
29530f1d1f3SOliver Ruiz Dorantes 	    ldi->Acquire();
29630f1d1f3SOliver Ruiz Dorantes 	    return reply->AddInt32("hci_id", hid);
29730f1d1f3SOliver Ruiz Dorantes 	}
29830f1d1f3SOliver Ruiz Dorantes 
29930f1d1f3SOliver Ruiz Dorantes 	return B_ERROR;
30030f1d1f3SOliver Ruiz Dorantes 
30130f1d1f3SOliver Ruiz Dorantes }
30230f1d1f3SOliver Ruiz Dorantes 
30330f1d1f3SOliver Ruiz Dorantes 
30430f1d1f3SOliver Ruiz Dorantes status_t
30530f1d1f3SOliver Ruiz Dorantes BluetoothServer::HandleGetFriendlyName(BMessage* message, BMessage* reply)
30630f1d1f3SOliver Ruiz Dorantes {
30730f1d1f3SOliver Ruiz Dorantes     LocalDeviceImpl* ldi = LocateDelegateFromMessage(message);
30830f1d1f3SOliver Ruiz Dorantes     BString name;
30930f1d1f3SOliver Ruiz Dorantes 
31030f1d1f3SOliver Ruiz Dorantes     if (ldi == NULL)
31130f1d1f3SOliver Ruiz Dorantes     	return B_ERROR;
31230f1d1f3SOliver Ruiz Dorantes 
31330f1d1f3SOliver Ruiz Dorantes     /* If the device was ocupied... Autlock?->LocalDeviceImpl will decide */
31430f1d1f3SOliver Ruiz Dorantes     if (ldi->GetFriendlyName(name, DetachCurrentMessage()) == B_OK) {
31530f1d1f3SOliver Ruiz Dorantes 
31630f1d1f3SOliver Ruiz Dorantes         return reply->AddString("friendlyname", name);
31730f1d1f3SOliver Ruiz Dorantes 	}
31830f1d1f3SOliver Ruiz Dorantes 
31930f1d1f3SOliver Ruiz Dorantes 	return B_WOULD_BLOCK;
32030f1d1f3SOliver Ruiz Dorantes }
32130f1d1f3SOliver Ruiz Dorantes 
32230f1d1f3SOliver Ruiz Dorantes 
32330f1d1f3SOliver Ruiz Dorantes status_t
32430f1d1f3SOliver Ruiz Dorantes BluetoothServer::HandleGetAddress(BMessage* message, BMessage* reply)
32530f1d1f3SOliver Ruiz Dorantes {
32630f1d1f3SOliver Ruiz Dorantes     LocalDeviceImpl* ldi = LocateDelegateFromMessage(message);
32730f1d1f3SOliver Ruiz Dorantes     bdaddr_t bdaddr;
32830f1d1f3SOliver Ruiz Dorantes 
32930f1d1f3SOliver Ruiz Dorantes     if (ldi == NULL)
33030f1d1f3SOliver Ruiz Dorantes     	return B_ERROR;
33130f1d1f3SOliver Ruiz Dorantes 
33230f1d1f3SOliver Ruiz Dorantes     /* If the device was ocupied... Autlock?->LocalDeviceImpl will decide */
33330f1d1f3SOliver Ruiz Dorantes 	status_t status = ldi->GetAddress(&bdaddr, DetachCurrentMessage());
33430f1d1f3SOliver Ruiz Dorantes     if ( status == B_OK) {
33530f1d1f3SOliver Ruiz Dorantes 
33630f1d1f3SOliver Ruiz Dorantes         return reply->AddData("bdaddr", B_ANY_TYPE, &bdaddr, sizeof(bdaddr_t));
33730f1d1f3SOliver Ruiz Dorantes 	}
33830f1d1f3SOliver Ruiz Dorantes 
33930f1d1f3SOliver Ruiz Dorantes 	return status;
34030f1d1f3SOliver Ruiz Dorantes }
34130f1d1f3SOliver Ruiz Dorantes 
34230f1d1f3SOliver Ruiz Dorantes 
34330f1d1f3SOliver Ruiz Dorantes status_t
34430f1d1f3SOliver Ruiz Dorantes BluetoothServer::HandleSimpleRequest(BMessage* message, BMessage* reply)
34530f1d1f3SOliver Ruiz Dorantes {
34630f1d1f3SOliver Ruiz Dorantes 
34730f1d1f3SOliver Ruiz Dorantes     LocalDeviceImpl* ldi = LocateDelegateFromMessage(message);
34830f1d1f3SOliver Ruiz Dorantes 	BString propertyRequested;
34930f1d1f3SOliver Ruiz Dorantes 
35030f1d1f3SOliver Ruiz Dorantes 	// Find out if there is a property being requested,
35130f1d1f3SOliver Ruiz Dorantes     if (message->FindString("property", &propertyRequested) == B_OK) {
35230f1d1f3SOliver Ruiz Dorantes     	// Check if the property has been already retrieved
35330f1d1f3SOliver Ruiz Dorantes 
35430f1d1f3SOliver Ruiz Dorantes     	if (ldi->IsPropertyAvailable(propertyRequested)) {
35530f1d1f3SOliver Ruiz Dorantes 
35630f1d1f3SOliver Ruiz Dorantes     	    // Dump everything
35730f1d1f3SOliver Ruiz Dorantes     		reply->AddMessage("properties",ldi->GetPropertiesMessage());
35830f1d1f3SOliver Ruiz Dorantes     		return B_OK;
35930f1d1f3SOliver Ruiz Dorantes     	}
36030f1d1f3SOliver Ruiz Dorantes 
36130f1d1f3SOliver Ruiz Dorantes     }
36230f1d1f3SOliver Ruiz Dorantes 
36330f1d1f3SOliver Ruiz Dorantes 	// we are gonna need issue the command ...
36430f1d1f3SOliver Ruiz Dorantes 	if (ldi->ProcessSimpleRequest(DetachCurrentMessage()) == B_OK)
36530f1d1f3SOliver Ruiz Dorantes 		return B_WOULD_BLOCK;
36630f1d1f3SOliver Ruiz Dorantes 	else
36730f1d1f3SOliver Ruiz Dorantes 		return B_ERROR;
36830f1d1f3SOliver Ruiz Dorantes 
36930f1d1f3SOliver Ruiz Dorantes }
37030f1d1f3SOliver Ruiz Dorantes 
37130f1d1f3SOliver Ruiz Dorantes 
37230f1d1f3SOliver Ruiz Dorantes #if 0
37330f1d1f3SOliver Ruiz Dorantes #pragma mark -
37430f1d1f3SOliver Ruiz Dorantes #endif
37530f1d1f3SOliver Ruiz Dorantes 
37630f1d1f3SOliver Ruiz Dorantes 
37730f1d1f3SOliver Ruiz Dorantes int32
37830f1d1f3SOliver Ruiz Dorantes BluetoothServer::notification_Thread(void* data)
37930f1d1f3SOliver Ruiz Dorantes {
38030f1d1f3SOliver Ruiz Dorantes 	BPortNot notifierd( (BluetoothServer*) data , BT_USERLAND_PORT_NAME);
38130f1d1f3SOliver Ruiz Dorantes 
38230f1d1f3SOliver Ruiz Dorantes 	notifierd.loop();
38330f1d1f3SOliver Ruiz Dorantes 	return B_NO_ERROR;
38430f1d1f3SOliver Ruiz Dorantes }
38530f1d1f3SOliver Ruiz Dorantes 
38630f1d1f3SOliver Ruiz Dorantes int32
38730f1d1f3SOliver Ruiz Dorantes BluetoothServer::sdp_server_Thread(void* data)
38830f1d1f3SOliver Ruiz Dorantes {
38930f1d1f3SOliver Ruiz Dorantes 
39030f1d1f3SOliver Ruiz Dorantes 	return B_NO_ERROR;
39130f1d1f3SOliver Ruiz Dorantes }
39230f1d1f3SOliver Ruiz Dorantes 
39330f1d1f3SOliver Ruiz Dorantes #if 0
39430f1d1f3SOliver Ruiz Dorantes 
39530f1d1f3SOliver Ruiz Dorantes 					struct  {
39630f1d1f3SOliver Ruiz Dorantes 						size_t	size;
39730f1d1f3SOliver Ruiz Dorantes 						struct hci_command_header header;
39830f1d1f3SOliver Ruiz Dorantes 						//struct hci_rp_read_bd_addr body;
39930f1d1f3SOliver Ruiz Dorantes 					} __attribute__ ((packed)) cm1;
40030f1d1f3SOliver Ruiz Dorantes 
40130f1d1f3SOliver Ruiz Dorantes 					struct  {
40230f1d1f3SOliver Ruiz Dorantes 						size_t	size;
40330f1d1f3SOliver Ruiz Dorantes 						struct hci_command_header header;
40430f1d1f3SOliver Ruiz Dorantes 						struct hci_cp_inquiry body;
40530f1d1f3SOliver Ruiz Dorantes 					} __attribute__ ((packed)) cm2;
40630f1d1f3SOliver Ruiz Dorantes 
40730f1d1f3SOliver Ruiz Dorantes 					struct  {
40830f1d1f3SOliver Ruiz Dorantes 						size_t	size;
40930f1d1f3SOliver Ruiz Dorantes 						struct hci_command_header header;
41030f1d1f3SOliver Ruiz Dorantes 						struct hci_remote_name_request body;
41130f1d1f3SOliver Ruiz Dorantes 					} __attribute__ ((packed)) cm3;
41230f1d1f3SOliver Ruiz Dorantes 
41330f1d1f3SOliver Ruiz Dorantes 					struct  {
41430f1d1f3SOliver Ruiz Dorantes 						size_t	size;
41530f1d1f3SOliver Ruiz Dorantes 						struct hci_command_header header;
41630f1d1f3SOliver Ruiz Dorantes 						struct hci_cp_create_conn body;
41730f1d1f3SOliver Ruiz Dorantes 					} __attribute__ ((packed)) cm4;
41830f1d1f3SOliver Ruiz Dorantes 
41930f1d1f3SOliver Ruiz Dorantes 
42030f1d1f3SOliver Ruiz Dorantes 					syslog(LOG_ALERT,BT "Registering device %s\n",path.Path());
42130f1d1f3SOliver Ruiz Dorantes 					fprintf(stderr, "Opening %s\n",path.Path());
42230f1d1f3SOliver Ruiz Dorantes 
42330f1d1f3SOliver Ruiz Dorantes 					cm1.size = sizeof(struct hci_command_header);
42430f1d1f3SOliver Ruiz Dorantes 					cm1.header.opcode = B_HOST_TO_LENDIAN_INT16(hci_opcode_pack(OGF_CONTROL_BASEBAND, OCF_RESET));
42530f1d1f3SOliver Ruiz Dorantes 					cm1.header.clen = 0;
42630f1d1f3SOliver Ruiz Dorantes 					ioctl(fd1, ISSUE_BT_COMMAND, &cm1, sizeof(cm1));
42730f1d1f3SOliver Ruiz Dorantes 
428009fac99SOliver Ruiz Dorantes 					cm1.size = sizeof(struct hci_command_header);
42930f1d1f3SOliver Ruiz Dorantes 					cm1.header.opcode = B_HOST_TO_LENDIAN_INT16(hci_opcode_pack(OGF_CONTROL_BASEBAND, OCF_READ_LOCAL_NAME));
43030f1d1f3SOliver Ruiz Dorantes 					cm1.header.clen = 0;
43130f1d1f3SOliver Ruiz Dorantes 					ioctl(fd1, ISSUE_BT_COMMAND, &cm1, sizeof(cm1));
43230f1d1f3SOliver Ruiz Dorantes 
43330f1d1f3SOliver Ruiz Dorantes 					cm1.size = sizeof(struct hci_command_header);
43430f1d1f3SOliver Ruiz Dorantes 					cm1.header.opcode = B_HOST_TO_LENDIAN_INT16(hci_opcode_pack(OGF_INFORMATIONAL_PARAM, OCF_READ_BD_ADDR));
43530f1d1f3SOliver Ruiz Dorantes 					cm1.header.clen = 0;
43630f1d1f3SOliver Ruiz Dorantes 					ioctl(fd1, ISSUE_BT_COMMAND, &cm1, sizeof(cm1));
43730f1d1f3SOliver Ruiz Dorantes 
43830f1d1f3SOliver Ruiz Dorantes 					cm2.size = sizeof(struct hci_command_header)+sizeof(struct hci_cp_inquiry);
43930f1d1f3SOliver Ruiz Dorantes 					cm2.header.opcode = B_HOST_TO_LENDIAN_INT16(hci_opcode_pack(OGF_LINK_CTL, OCF_INQUIRY));
44030f1d1f3SOliver Ruiz Dorantes 					cm2.body.lap[0] = ((B_BT_GIAC & 0x000000FF));
44130f1d1f3SOliver Ruiz Dorantes 					cm2.body.lap[1] = ((B_BT_GIAC & 0x0000FF00) >> 8);
44230f1d1f3SOliver Ruiz Dorantes 					cm2.body.lap[2] = ((B_BT_GIAC & 0x00FF0000) >> 16);
44330f1d1f3SOliver Ruiz Dorantes 					cm2.body.length = 0x15;
44430f1d1f3SOliver Ruiz Dorantes 					cm2.body.num_rsp = 8;
44530f1d1f3SOliver Ruiz Dorantes 					cm2.header.clen = 5;
44630f1d1f3SOliver Ruiz Dorantes 					ioctl(fd1, ISSUE_BT_COMMAND, &cm2, sizeof(cm1));
447009fac99SOliver Ruiz Dorantes 
44830f1d1f3SOliver Ruiz Dorantes 					snooze(60*1000*1000);*/
44930f1d1f3SOliver Ruiz Dorantes 					cm3.size = sizeof(struct hci_command_header)+sizeof(struct hci_remote_name_request);
45030f1d1f3SOliver Ruiz Dorantes 					cm3.header.opcode = B_HOST_TO_LENDIAN_INT16(hci_opcode_pack(OGF_LINK_CONTROL, OCF_REMOTE_NAME_REQUEST));
45130f1d1f3SOliver Ruiz Dorantes 					cm3.body.bdaddr.b[0] = 0x92;
45230f1d1f3SOliver Ruiz Dorantes 					cm3.body.bdaddr.b[1] = 0xd3;
45330f1d1f3SOliver Ruiz Dorantes 					cm3.body.bdaddr.b[2] = 0xaf;
45430f1d1f3SOliver Ruiz Dorantes 					cm3.body.bdaddr.b[3] = 0xd9;
45530f1d1f3SOliver Ruiz Dorantes 					cm3.body.bdaddr.b[4] = 0x0a;
45630f1d1f3SOliver Ruiz Dorantes 					cm3.body.bdaddr.b[5] = 0x00;
45730f1d1f3SOliver Ruiz Dorantes 					cm3.body.pscan_rep_mode = 1;
45830f1d1f3SOliver Ruiz Dorantes 					cm3.body.clock_offset = 0xc7;
45930f1d1f3SOliver Ruiz Dorantes 					cm3.header.clen = 10;
46030f1d1f3SOliver Ruiz Dorantes 					ioctl(fd1, ISSUE_BT_COMMAND, &cm3, sizeof(cm3));
46130f1d1f3SOliver Ruiz Dorantes 					/*
46230f1d1f3SOliver Ruiz Dorantes 					cm4.size = sizeof(struct hci_command_header)+sizeof(struct hci_cp_create_conn);
46330f1d1f3SOliver Ruiz Dorantes 					cm4.header.opcode = B_HOST_TO_LENDIAN_INT16(hci_opcode_pack(OGF_LINK_CTL, OCF_CREATE_CONN));
46430f1d1f3SOliver Ruiz Dorantes 					cm4.body.bdaddr.b[0] = 0x92;
46530f1d1f3SOliver Ruiz Dorantes 					cm4.body.bdaddr.b[1] = 0xd3;
46630f1d1f3SOliver Ruiz Dorantes 					cm4.body.bdaddr.b[2] = 0xaf;
46730f1d1f3SOliver Ruiz Dorantes 					cm4.body.bdaddr.b[3] = 0xd9;
46830f1d1f3SOliver Ruiz Dorantes 					cm4.body.bdaddr.b[4] = 0x0a;
46930f1d1f3SOliver Ruiz Dorantes 					cm4.body.bdaddr.b[5] = 0x00;
47030f1d1f3SOliver Ruiz Dorantes 					cm4.body.pkt_type = 0xFFFF;
47130f1d1f3SOliver Ruiz Dorantes 					cm4.body.pscan_rep_mode = 1;
47230f1d1f3SOliver Ruiz Dorantes 					cm4.body.pscan_mode = 0;
47330f1d1f3SOliver Ruiz Dorantes 					cm4.body.clock_offset = 0xc7;
47430f1d1f3SOliver Ruiz Dorantes 					cm4.body.role_switch = 1;
47530f1d1f3SOliver Ruiz Dorantes 					cm4.header.clen = 13;
47630f1d1f3SOliver Ruiz Dorantes 					ioctl(fd1, ISSUE_BT_COMMAND, &cm4, sizeof(cm4));
47730f1d1f3SOliver Ruiz Dorantes 					*/
47830f1d1f3SOliver Ruiz Dorantes 				}
47930f1d1f3SOliver Ruiz Dorantes 
48030f1d1f3SOliver Ruiz Dorantes #endif
48130f1d1f3SOliver Ruiz Dorantes 
48230f1d1f3SOliver Ruiz Dorantes void
48330f1d1f3SOliver Ruiz Dorantes BluetoothServer::ShowWindow(BWindow* pWindow)
48430f1d1f3SOliver Ruiz Dorantes {
48530f1d1f3SOliver Ruiz Dorantes 	pWindow->Lock();
48630f1d1f3SOliver Ruiz Dorantes 	if (pWindow->IsHidden())
48730f1d1f3SOliver Ruiz Dorantes 		pWindow->Show();
48830f1d1f3SOliver Ruiz Dorantes 	else
48930f1d1f3SOliver Ruiz Dorantes 		pWindow->Activate();
49030f1d1f3SOliver Ruiz Dorantes 	pWindow->Unlock();
49130f1d1f3SOliver Ruiz Dorantes }
49230f1d1f3SOliver Ruiz Dorantes 
49330f1d1f3SOliver Ruiz Dorantes 
49430f1d1f3SOliver Ruiz Dorantes #if 0
49530f1d1f3SOliver Ruiz Dorantes #pragma mark -
49630f1d1f3SOliver Ruiz Dorantes #endif
49730f1d1f3SOliver Ruiz Dorantes 
49830f1d1f3SOliver Ruiz Dorantes int
49930f1d1f3SOliver Ruiz Dorantes main(int /*argc*/, char** /*argv*/)
50030f1d1f3SOliver Ruiz Dorantes {
50130f1d1f3SOliver Ruiz Dorantes 	setbuf(stdout,NULL);
50230f1d1f3SOliver Ruiz Dorantes 
50330f1d1f3SOliver Ruiz Dorantes 	BluetoothServer* bluetoothServer = new BluetoothServer;
50430f1d1f3SOliver Ruiz Dorantes 
50530f1d1f3SOliver Ruiz Dorantes 	bluetoothServer->Run();
50630f1d1f3SOliver Ruiz Dorantes 	delete bluetoothServer;
50730f1d1f3SOliver Ruiz Dorantes 
50830f1d1f3SOliver Ruiz Dorantes 	return 0;
50930f1d1f3SOliver Ruiz Dorantes }
51030f1d1f3SOliver Ruiz Dorantes 
511