2 * Copyright 2007 Oliver Ruiz Dorantes, oliver.ruiz.dorantes_at_gmail.com
4 * All rights reserved. Distributed under the terms of the MIT License.
8 #include <bluetooth/DiscoveryAgent.h>
9 #include <bluetooth/DiscoveryListener.h>
10 #include <bluetooth/RemoteDevice.h>
11 #include <bluetooth/DeviceClass.h>
12 #include <bluetooth/bdaddrUtils.h>
13 #include <bluetooth/debug.h>
15 #include <bluetooth/HCI/btHCI_event.h>
17 #include <bluetoothserver_p.h>
26 DiscoveryListener::DeviceDiscovered(RemoteDevice
* btDevice
, DeviceClass cod
)
33 DiscoveryListener::InquiryStarted(status_t status
)
40 DiscoveryListener::InquiryCompleted(int discType
)
48 /* A LocalDevice is always referenced in any request to the
49 * Bluetooth server therefore is going to be needed in any
52 DiscoveryListener::SetLocalDeviceOwner(LocalDevice
* ld
)
60 DiscoveryListener::GetRemoteDevicesList(void)
63 return fRemoteDevicesList
;
68 DiscoveryListener::MessageReceived(BMessage
* message
)
73 switch (message
->what
) {
74 case BT_MSG_INQUIRY_DEVICE
:
76 const struct inquiry_info
* inquiryInfo
;
78 RemoteDevice
* rd
= NULL
;
79 bool duplicatedFound
= false;
81 // TODO: Loop for all inquiryInfo!
82 if (message
->FindData("info", B_ANY_TYPE
, 0,
83 (const void**)&inquiryInfo
, &size
) == B_OK
) {
84 // Skip duplicated replies
85 for (int32 index
= 0 ; index
< fRemoteDevicesList
.CountItems();
87 bdaddr_t b1
= fRemoteDevicesList
.ItemAt(index
)
88 ->GetBluetoothAddress();
90 if (bdaddrUtils::Compare(inquiryInfo
->bdaddr
, b1
)) {
91 // update these values
92 fRemoteDevicesList
.ItemAt(index
)->fPageRepetitionMode
93 = inquiryInfo
->pscan_rep_mode
;
94 fRemoteDevicesList
.ItemAt(index
)->fScanPeriodMode
95 = inquiryInfo
->pscan_period_mode
;
96 fRemoteDevicesList
.ItemAt(index
)->fScanMode
97 = inquiryInfo
->pscan_mode
;
98 fRemoteDevicesList
.ItemAt(index
)->fClockOffset
99 = inquiryInfo
->clock_offset
;
101 duplicatedFound
= true;
106 if (!duplicatedFound
) {
107 rd
= new RemoteDevice(inquiryInfo
->bdaddr
,
108 (uint8
*)inquiryInfo
->dev_class
);
109 fRemoteDevicesList
.AddItem(rd
);
110 // keep all inquiry reported data
111 rd
->SetLocalDeviceOwner(fLocalDevice
);
112 rd
->fPageRepetitionMode
= inquiryInfo
->pscan_rep_mode
;
113 rd
->fScanPeriodMode
= inquiryInfo
->pscan_period_mode
;
114 rd
->fScanMode
= inquiryInfo
->pscan_mode
;
115 rd
->fClockOffset
= inquiryInfo
->clock_offset
;
117 DeviceDiscovered( rd
, rd
->GetDeviceClass());
123 case BT_MSG_INQUIRY_STARTED
:
124 if (message
->FindInt8("status", &status
) == B_OK
) {
125 fRemoteDevicesList
.MakeEmpty();
126 InquiryStarted(status
);
130 case BT_MSG_INQUIRY_COMPLETED
:
131 InquiryCompleted(BT_INQUIRY_COMPLETED
);
134 case BT_MSG_INQUIRY_TERMINATED
: /* inquiry was cancelled */
135 InquiryCompleted(BT_INQUIRY_TERMINATED
);
138 case BT_MSG_INQUIRY_ERROR
:
139 InquiryCompleted(BT_INQUIRY_ERROR
);
143 BLooper::MessageReceived(message
);
149 DiscoveryListener::DiscoveryListener()
152 fRemoteDevicesList(BT_MAX_RESPONSES
)
155 // TODO: Make a better handling of the running not running state