btrfs: Attempt to fix GCC2 build.
[haiku.git] / src / kits / bluetooth / DiscoveryListener.cpp
blobc8cf32e6d9c297ce753d3b497931906eaefab79e
1 /*
2 * Copyright 2007 Oliver Ruiz Dorantes, oliver.ruiz.dorantes_at_gmail.com
4 * All rights reserved. Distributed under the terms of the MIT License.
6 */
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>
19 #include <Message.h>
21 namespace Bluetooth {
24 /* hooks */
25 void
26 DiscoveryListener::DeviceDiscovered(RemoteDevice* btDevice, DeviceClass cod)
28 CALLED();
32 void
33 DiscoveryListener::InquiryStarted(status_t status)
35 CALLED();
39 void
40 DiscoveryListener::InquiryCompleted(int discType)
42 CALLED();
46 /* private */
48 /* A LocalDevice is always referenced in any request to the
49 * Bluetooth server therefore is going to be needed in any
51 void
52 DiscoveryListener::SetLocalDeviceOwner(LocalDevice* ld)
54 CALLED();
55 fLocalDevice = ld;
59 RemoteDevicesList
60 DiscoveryListener::GetRemoteDevicesList(void)
62 CALLED();
63 return fRemoteDevicesList;
67 void
68 DiscoveryListener::MessageReceived(BMessage* message)
70 CALLED();
71 int8 status;
73 switch (message->what) {
74 case BT_MSG_INQUIRY_DEVICE:
76 const struct inquiry_info* inquiryInfo;
77 ssize_t size;
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();
86 index++) {
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;
102 break;
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());
120 break;
123 case BT_MSG_INQUIRY_STARTED:
124 if (message->FindInt8("status", &status) == B_OK) {
125 fRemoteDevicesList.MakeEmpty();
126 InquiryStarted(status);
128 break;
130 case BT_MSG_INQUIRY_COMPLETED:
131 InquiryCompleted(BT_INQUIRY_COMPLETED);
132 break;
134 case BT_MSG_INQUIRY_TERMINATED: /* inquiry was cancelled */
135 InquiryCompleted(BT_INQUIRY_TERMINATED);
136 break;
138 case BT_MSG_INQUIRY_ERROR:
139 InquiryCompleted(BT_INQUIRY_ERROR);
140 break;
142 default:
143 BLooper::MessageReceived(message);
144 break;
149 DiscoveryListener::DiscoveryListener()
151 BLooper(),
152 fRemoteDevicesList(BT_MAX_RESPONSES)
154 CALLED();
155 // TODO: Make a better handling of the running not running state
156 Run();