186aceda9SOliver Ruiz Dorantes /*
286aceda9SOliver Ruiz Dorantes * Copyright 2007 Oliver Ruiz Dorantes, oliver.ruiz.dorantes_at_gmail.com
386aceda9SOliver Ruiz Dorantes * All rights reserved. Distributed under the terms of the MIT License.
486aceda9SOliver Ruiz Dorantes */
586aceda9SOliver Ruiz Dorantes
6*19733b44SAlexander von Gluck IV
7b0410a22SOliver Ruiz Dorantes #include <bluetooth/DiscoveryAgent.h>
886aceda9SOliver Ruiz Dorantes #include <bluetooth/DiscoveryListener.h>
986aceda9SOliver Ruiz Dorantes #include <bluetooth/RemoteDevice.h>
1086aceda9SOliver Ruiz Dorantes #include <bluetooth/DeviceClass.h>
11b0410a22SOliver Ruiz Dorantes #include <bluetooth/bdaddrUtils.h>
12fef016caSFredrik Modéen #include <bluetooth/debug.h>
1386aceda9SOliver Ruiz Dorantes
14d581ede6SOliver Ruiz Dorantes #include <bluetooth/HCI/btHCI_event.h>
15d581ede6SOliver Ruiz Dorantes
16b8c8202dSOliver Ruiz Dorantes #include <bluetoothserver_p.h>
17b8c8202dSOliver Ruiz Dorantes
1886aceda9SOliver Ruiz Dorantes #include <Message.h>
1986aceda9SOliver Ruiz Dorantes
20*19733b44SAlexander von Gluck IV
2186aceda9SOliver Ruiz Dorantes namespace Bluetooth {
2286aceda9SOliver Ruiz Dorantes
2386aceda9SOliver Ruiz Dorantes
2486aceda9SOliver Ruiz Dorantes /* hooks */
2586aceda9SOliver Ruiz Dorantes void
DeviceDiscovered(RemoteDevice * btDevice,DeviceClass cod)26d581ede6SOliver Ruiz Dorantes DiscoveryListener::DeviceDiscovered(RemoteDevice* btDevice, DeviceClass cod)
2786aceda9SOliver Ruiz Dorantes {
28fef016caSFredrik Modéen CALLED();
2986aceda9SOliver Ruiz Dorantes }
3086aceda9SOliver Ruiz Dorantes
3186aceda9SOliver Ruiz Dorantes
3286aceda9SOliver Ruiz Dorantes void
InquiryStarted(status_t status)33b8c8202dSOliver Ruiz Dorantes DiscoveryListener::InquiryStarted(status_t status)
34b8c8202dSOliver Ruiz Dorantes {
35fef016caSFredrik Modéen CALLED();
36b8c8202dSOliver Ruiz Dorantes }
37b8c8202dSOliver Ruiz Dorantes
38b8c8202dSOliver Ruiz Dorantes
39b8c8202dSOliver Ruiz Dorantes void
InquiryCompleted(int discType)4086aceda9SOliver Ruiz Dorantes DiscoveryListener::InquiryCompleted(int discType)
4186aceda9SOliver Ruiz Dorantes {
42fef016caSFredrik Modéen CALLED();
4386aceda9SOliver Ruiz Dorantes }
4486aceda9SOliver Ruiz Dorantes
4586aceda9SOliver Ruiz Dorantes
4686aceda9SOliver Ruiz Dorantes /* private */
4786aceda9SOliver Ruiz Dorantes
48d581ede6SOliver Ruiz Dorantes /* A LocalDevice is always referenced in any request to the
49b9068dbfSOliver Ruiz Dorantes * Bluetooth server therefore is going to be needed in any
50b9068dbfSOliver Ruiz Dorantes */
51d581ede6SOliver Ruiz Dorantes void
SetLocalDeviceOwner(LocalDevice * ld)52d581ede6SOliver Ruiz Dorantes DiscoveryListener::SetLocalDeviceOwner(LocalDevice* ld)
53d581ede6SOliver Ruiz Dorantes {
54fef016caSFredrik Modéen CALLED();
55d581ede6SOliver Ruiz Dorantes fLocalDevice = ld;
56d581ede6SOliver Ruiz Dorantes }
57d581ede6SOliver Ruiz Dorantes
58d581ede6SOliver Ruiz Dorantes
59d581ede6SOliver Ruiz Dorantes RemoteDevicesList
GetRemoteDevicesList(void)60d581ede6SOliver Ruiz Dorantes DiscoveryListener::GetRemoteDevicesList(void)
61d581ede6SOliver Ruiz Dorantes {
62fef016caSFredrik Modéen CALLED();
63d581ede6SOliver Ruiz Dorantes return fRemoteDevicesList;
6486aceda9SOliver Ruiz Dorantes }
6586aceda9SOliver Ruiz Dorantes
66b8c8202dSOliver Ruiz Dorantes
6786aceda9SOliver Ruiz Dorantes void
MessageReceived(BMessage * message)6886aceda9SOliver Ruiz Dorantes DiscoveryListener::MessageReceived(BMessage* message)
6986aceda9SOliver Ruiz Dorantes {
70fef016caSFredrik Modéen CALLED();
71d581ede6SOliver Ruiz Dorantes int8 status;
72d581ede6SOliver Ruiz Dorantes
73b9068dbfSOliver Ruiz Dorantes switch (message->what) {
74b8c8202dSOliver Ruiz Dorantes case BT_MSG_INQUIRY_DEVICE:
75d581ede6SOliver Ruiz Dorantes {
76d581ede6SOliver Ruiz Dorantes const struct inquiry_info* inquiryInfo;
77d581ede6SOliver Ruiz Dorantes ssize_t size;
78b0410a22SOliver Ruiz Dorantes RemoteDevice* rd = NULL;
79b0410a22SOliver Ruiz Dorantes bool duplicatedFound = false;
8086aceda9SOliver Ruiz Dorantes
818dc33083SOliver Ruiz Dorantes // TODO: Loop for all inquiryInfo!
82269ebc04SOliver Tappe if (message->FindData("info", B_ANY_TYPE, 0,
83269ebc04SOliver Tappe (const void**)&inquiryInfo, &size) == B_OK) {
84b0410a22SOliver Ruiz Dorantes // Skip duplicated replies
85269ebc04SOliver Tappe for (int32 index = 0 ; index < fRemoteDevicesList.CountItems();
86269ebc04SOliver Tappe index++) {
87269ebc04SOliver Tappe bdaddr_t b1 = fRemoteDevicesList.ItemAt(index)
88269ebc04SOliver Tappe ->GetBluetoothAddress();
89b0410a22SOliver Ruiz Dorantes
90269ebc04SOliver Tappe if (bdaddrUtils::Compare(inquiryInfo->bdaddr, b1)) {
91fae5a1d3SOliver Ruiz Dorantes // update these values
92269ebc04SOliver Tappe fRemoteDevicesList.ItemAt(index)->fPageRepetitionMode
93269ebc04SOliver Tappe = inquiryInfo->pscan_rep_mode;
94269ebc04SOliver Tappe fRemoteDevicesList.ItemAt(index)->fScanPeriodMode
95269ebc04SOliver Tappe = inquiryInfo->pscan_period_mode;
96269ebc04SOliver Tappe fRemoteDevicesList.ItemAt(index)->fScanMode
97269ebc04SOliver Tappe = inquiryInfo->pscan_mode;
98269ebc04SOliver Tappe fRemoteDevicesList.ItemAt(index)->fClockOffset
99269ebc04SOliver Tappe = inquiryInfo->clock_offset;
100fae5a1d3SOliver Ruiz Dorantes
101b0410a22SOliver Ruiz Dorantes duplicatedFound = true;
102b0410a22SOliver Ruiz Dorantes break;
103b0410a22SOliver Ruiz Dorantes }
104b0410a22SOliver Ruiz Dorantes }
105b0410a22SOliver Ruiz Dorantes
106b0410a22SOliver Ruiz Dorantes if (!duplicatedFound) {
107269ebc04SOliver Tappe rd = new RemoteDevice(inquiryInfo->bdaddr,
108269ebc04SOliver Tappe (uint8*)inquiryInfo->dev_class);
109b0410a22SOliver Ruiz Dorantes fRemoteDevicesList.AddItem(rd);
110fae5a1d3SOliver Ruiz Dorantes // keep all inquiry reported data
111b0410a22SOliver Ruiz Dorantes rd->SetLocalDeviceOwner(fLocalDevice);
112fae5a1d3SOliver Ruiz Dorantes rd->fPageRepetitionMode = inquiryInfo->pscan_rep_mode;
113fae5a1d3SOliver Ruiz Dorantes rd->fScanPeriodMode = inquiryInfo->pscan_period_mode;
114fae5a1d3SOliver Ruiz Dorantes rd->fScanMode = inquiryInfo->pscan_mode;
115fae5a1d3SOliver Ruiz Dorantes rd->fClockOffset = inquiryInfo->clock_offset;
116fae5a1d3SOliver Ruiz Dorantes
11733fe7b32SOliver Ruiz Dorantes DeviceDiscovered( rd, rd->GetDeviceClass());
118b0410a22SOliver Ruiz Dorantes }
119d581ede6SOliver Ruiz Dorantes }
120d581ede6SOliver Ruiz Dorantes break;
121b9068dbfSOliver Ruiz Dorantes }
122d581ede6SOliver Ruiz Dorantes
123d581ede6SOliver Ruiz Dorantes case BT_MSG_INQUIRY_STARTED:
124d581ede6SOliver Ruiz Dorantes if (message->FindInt8("status", &status) == B_OK) {
125b0410a22SOliver Ruiz Dorantes fRemoteDevicesList.MakeEmpty();
126d581ede6SOliver Ruiz Dorantes InquiryStarted(status);
127d581ede6SOliver Ruiz Dorantes }
12886aceda9SOliver Ruiz Dorantes break;
12986aceda9SOliver Ruiz Dorantes
130b8c8202dSOliver Ruiz Dorantes case BT_MSG_INQUIRY_COMPLETED:
131d581ede6SOliver Ruiz Dorantes InquiryCompleted(BT_INQUIRY_COMPLETED);
13286aceda9SOliver Ruiz Dorantes break;
133b9068dbfSOliver Ruiz Dorantes
134d581ede6SOliver Ruiz Dorantes case BT_MSG_INQUIRY_TERMINATED: /* inquiry was cancelled */
135d581ede6SOliver Ruiz Dorantes InquiryCompleted(BT_INQUIRY_TERMINATED);
13686aceda9SOliver Ruiz Dorantes break;
137b9068dbfSOliver Ruiz Dorantes
138b8c8202dSOliver Ruiz Dorantes case BT_MSG_INQUIRY_ERROR:
139d581ede6SOliver Ruiz Dorantes InquiryCompleted(BT_INQUIRY_ERROR);
14086aceda9SOliver Ruiz Dorantes break;
14186aceda9SOliver Ruiz Dorantes
14286aceda9SOliver Ruiz Dorantes default:
14386aceda9SOliver Ruiz Dorantes BLooper::MessageReceived(message);
14486aceda9SOliver Ruiz Dorantes break;
14586aceda9SOliver Ruiz Dorantes }
14686aceda9SOliver Ruiz Dorantes }
14786aceda9SOliver Ruiz Dorantes
148d581ede6SOliver Ruiz Dorantes
DiscoveryListener()149269ebc04SOliver Tappe DiscoveryListener::DiscoveryListener()
150269ebc04SOliver Tappe :
151269ebc04SOliver Tappe BLooper(),
152269ebc04SOliver Tappe fRemoteDevicesList(BT_MAX_RESPONSES)
153d581ede6SOliver Ruiz Dorantes {
154fef016caSFredrik Modéen CALLED();
155078958f5SOliver Ruiz Dorantes // TODO: Make a better handling of the running not running state
156078958f5SOliver Ruiz Dorantes Run();
157d581ede6SOliver Ruiz Dorantes }
158d581ede6SOliver Ruiz Dorantes
159*19733b44SAlexander von Gluck IV } /* end namespace Bluetooth */
160