btrfs: Attempt to fix GCC2 build.
[haiku.git] / src / kits / bluetooth / DiscoveryAgent.cpp
blobab092eafc182337de844f5d63502904ed4524b87
1 /*
2 * Copyright 2007-2008 Oliver Ruiz Dorantes, oliver.ruiz.dorantes_at_gmail.com
3 * All rights reserved. Distributed under the terms of the MIT License.
4 */
6 #include <bluetooth/bluetooth_error.h>
7 #include <bluetooth/DiscoveryAgent.h>
8 #include <bluetooth/DiscoveryListener.h>
9 #include <bluetooth/LocalDevice.h>
10 #include <bluetooth/RemoteDevice.h>
11 #include <bluetooth/debug.h>
13 #include <bluetooth/HCI/btHCI_command.h>
14 #include <bluetooth/HCI/btHCI_event.h>
16 #include <bluetoothserver_p.h>
17 #include <CommandManager.h>
19 #include "KitSupport.h"
21 namespace Bluetooth {
24 RemoteDevicesList
25 DiscoveryAgent::RetrieveDevices(int option)
27 CALLED();
28 // No inquiry process initiated
29 if (fLastUsedListener == NULL)
30 return RemoteDevicesList();
32 return fLastUsedListener->GetRemoteDevicesList();
36 status_t
37 DiscoveryAgent::StartInquiry(int accessCode, DiscoveryListener* listener)
39 CALLED();
40 return StartInquiry(accessCode, listener, GetInquiryTime());
44 status_t
45 DiscoveryAgent::StartInquiry(uint32 accessCode, DiscoveryListener* listener, bigtime_t secs)
47 CALLED();
48 size_t size;
50 if (fMessenger == NULL)
51 return B_ERROR;
53 if (secs < 1 || secs > 61 )
54 return B_TIMED_OUT;
56 void* startInquiryCommand = NULL;
58 // keep the listener whats the current listener for our inquiry state
59 fLastUsedListener = listener;
61 // Inform the listener who is gonna be its owner LocalDevice
62 // and its discovered devices
63 listener->SetLocalDeviceOwner(fLocalDevice);
65 /* Issue inquiry command */
66 BMessage request(BT_MSG_HANDLE_SIMPLE_REQUEST);
67 BMessage reply;
69 request.AddInt32("hci_id", fLocalDevice->ID());
71 startInquiryCommand = buildInquiry(accessCode, secs, BT_MAX_RESPONSES, &size);
73 // For stating the inquiry
74 request.AddData("raw command", B_ANY_TYPE, startInquiryCommand, size);
75 request.AddInt16("eventExpected", HCI_EVENT_CMD_STATUS);
76 request.AddInt16("opcodeExpected", PACK_OPCODE(OGF_LINK_CONTROL, OCF_INQUIRY));
78 // For getting each discovered message
79 request.AddInt16("eventExpected", HCI_EVENT_INQUIRY_RESULT);
81 // For finishing each discovered message
82 request.AddInt16("eventExpected", HCI_EVENT_INQUIRY_COMPLETE);
85 if (fMessenger->SendMessage(&request, listener) == B_OK)
87 return B_OK;
90 return B_ERROR;
95 status_t
96 DiscoveryAgent::CancelInquiry(DiscoveryListener* listener)
98 CALLED();
99 size_t size;
101 if (fMessenger == NULL)
102 return B_ERROR;
104 void* cancelInquiryCommand = NULL;
105 int8 bt_status = BT_ERROR;
107 /* Issue inquiry command */
108 BMessage request(BT_MSG_HANDLE_SIMPLE_REQUEST);
109 BMessage reply;
111 request.AddInt32("hci_id", fLocalDevice->ID());
113 cancelInquiryCommand = buildInquiryCancel(&size);
114 request.AddData("raw command", B_ANY_TYPE, cancelInquiryCommand, size);
115 request.AddInt16("eventExpected", HCI_EVENT_CMD_STATUS);
116 request.AddInt16("opcodeExpected", PACK_OPCODE(OGF_LINK_CONTROL, OCF_INQUIRY_CANCEL));
118 if (fMessenger->SendMessage(&request, &reply) == B_OK) {
119 if (reply.FindInt8("status", &bt_status ) == B_OK ) {
120 return bt_status;
124 return B_ERROR;
128 void
129 DiscoveryAgent::SetLocalDeviceOwner(LocalDevice* ld)
131 CALLED();
132 fLocalDevice = ld;
136 DiscoveryAgent::DiscoveryAgent(LocalDevice* ld)
138 CALLED();
139 fLocalDevice = ld;
140 fMessenger = _RetrieveBluetoothMessenger();
144 DiscoveryAgent::~DiscoveryAgent()
146 CALLED();
147 delete fMessenger;