xref: /haiku/src/kits/bluetooth/DiscoveryAgent.cpp (revision cfc3fa87da824bdf593eb8b817a83b6376e77935)
1 /*
2  * Copyright 2007-2008 Oliver Ruiz Dorantes, oliver.ruiz.dorantes_at_gmail.com
3  *
4  * All rights reserved. Distributed under the terms of the MIT License.
5  *
6  */
7 
8 #include <bluetooth/DiscoveryAgent.h>
9 #include <bluetooth/DiscoveryListener.h>
10 #include <bluetooth/RemoteDevice.h>
11 #include <bluetooth/LocalDevice.h>
12 
13 #include <bluetooth/bluetooth_error.h>
14 
15 #include <bluetooth/bluetoothserver_p.h>
16 #include <bluetooth/CommandManager.h>
17 
18 #include <bluetooth/HCI/btHCI_command.h>
19 #include <bluetooth/HCI/btHCI_event.h>
20 
21 
22 #include "KitSupport.h"
23 
24 namespace Bluetooth {
25 
26 
27 RemoteDevicesList
28 DiscoveryAgent::RetrieveDevices(int option)
29 {
30     /* No inquiry process initiated */
31     if (fLastUsedListener == NULL)
32         return NULL;
33 
34     return fLastUsedListener->GetRemoteDevicesList();
35 }
36 
37 
38 status_t
39 DiscoveryAgent::StartInquiry(int accessCode, DiscoveryListener* listener)
40 {
41     return StartInquiry(accessCode, listener, BT_DEFAULT_INQUIRY_TIME);
42 }
43 
44 
45 status_t
46 DiscoveryAgent::StartInquiry(uint32 accessCode, DiscoveryListener* listener, bigtime_t secs)
47 {
48     BMessenger* btsm = NULL;
49 	size_t size;
50 
51     if ((btsm = _RetrieveBluetoothMessenger()) == NULL)
52     	return B_ERROR;
53 
54 	if (secs < 1 || secs > 61 )
55 		return B_TIMED_OUT;
56 
57     void*  startInquiryCommand = NULL;
58 
59     // keep the listener whats the current listener for our inquiry state
60     fLastUsedListener = listener;
61 
62     // Inform the listener who is gonna be its owner LocalDevice
63     // and its discovered devices
64     listener->SetLocalDeviceOwner(fLocalDevice);
65 
66     /* Issue inquiry command */
67     BMessage request(BT_MSG_HANDLE_SIMPLE_REQUEST);
68     BMessage reply;
69 
70     request.AddInt32("hci_id", fLocalDevice->GetID());
71 
72 	startInquiryCommand = buildInquiry(accessCode, secs, BT_MAX_RESPONSES, &size);
73 
74 	// For stating the inquiry
75 	request.AddData("raw command", B_ANY_TYPE, startInquiryCommand, size);
76     request.AddInt16("eventExpected",  HCI_EVENT_CMD_STATUS);
77     request.AddInt16("opcodeExpected", PACK_OPCODE(OGF_LINK_CONTROL, OCF_INQUIRY));
78 
79 	// For getting each discovered message
80     request.AddInt16("eventExpected",  HCI_EVENT_INQUIRY_RESULT);
81 
82 	// For finishing each discovered message
83     request.AddInt16("eventExpected",  HCI_EVENT_INQUIRY_COMPLETE);
84 
85 
86     if (btsm->SendMessage(&request, listener) == B_OK)
87     {
88     	return B_OK;
89     }
90 
91 	return B_ERROR;
92 
93 }
94 
95 
96 status_t
97 DiscoveryAgent::CancelInquiry(DiscoveryListener* listener)
98 {
99     BMessenger* btsm = NULL;
100 
101     if ((btsm = _RetrieveBluetoothMessenger()) == NULL)
102     	return B_ERROR;
103 
104     void* cancelInquiryCommand = NULL;
105     int8  bt_status = BT_ERROR;
106 
107     /* Issue inquiry command */
108     BMessage request(BT_MSG_HANDLE_SIMPLE_REQUEST);
109     BMessage reply;
110 
111     request.AddInt32("hci_id", fLocalDevice->GetID());
112     // TODO: Add the raw command to the BMessage + expected event(s)
113 
114 
115 
116     if (btsm->SendMessage(&request, listener) == B_OK) {
117         if (reply.FindInt8("status", &bt_status ) == B_OK ) {
118             return bt_status;
119         }
120     }
121 
122 	return B_ERROR;
123 }
124 
125 void
126 DiscoveryAgent::SetLocalDeviceOwner(LocalDevice* ld)
127 {
128     fLocalDevice = ld;
129 }
130 
131 DiscoveryAgent::DiscoveryAgent()
132 {
133     fLocalDevice = NULL;
134 }
135 
136 
137 }
138