xref: /haiku/src/bin/bt_discovery.cpp (revision b06a48ab8f30b45916a9c157b992827779182163)
1 /*
2  * Copyright 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 <stdio.h>
9 #include <stdlib.h>
10 
11 #include <bluetooth/LocalDevice.h>
12 #include <bluetooth/bdaddrUtils.h>
13 
14 #include <bluetooth/DiscoveryAgent.h>
15 #include <bluetooth/DiscoveryListener.h>
16 
17 
18 thread_id mainThread;
19 
20 class simpleDiscoveryListener : public DiscoveryListener {
21 
22 public:
23 
24 simpleDiscoveryListener(LocalDevice *device) : DiscoveryListener()
25 {
26     /* TODO: Should not be needed */
27 	SetLocalDeviceOwner(device);
28 }
29 
30 
31 void
32 DeviceDiscovered(RemoteDevice* btDevice, DeviceClass cod)
33 {
34 	printf("\t%s: Device %s discovered.\n",__FUNCTION__, bdaddrUtils::ToString(btDevice->GetBluetoothAddress()));
35 }
36 
37 
38 void
39 InquiryCompleted(int discType)
40 {
41 
42 	printf("\t%s: Inquiry process has finished ...\n",__FUNCTION__);
43 	(void)send_data(mainThread, discType, NULL, 0);
44 
45 }
46 
47 
48 void
49 InquiryStarted(status_t status)
50 {
51 	printf("\t%s: Inquiry process has started ...\n",__FUNCTION__);
52 }
53 
54 
55 };
56 
57 
58 void
59 DumpInfo(LocalDevice* device)
60 {
61 	DiscoveryAgent* dAgent = device->GetDiscoveryAgent();
62 
63 	if (dAgent == NULL) {
64 		printf("DiscoveryAgent could not be located\n");
65 		return;
66 	}
67 
68 	printf("Discovering for  [LocalDevice] %s\t%s\n\n",
69          (device->GetFriendlyName()).String(),
70           bdaddrUtils::ToString(device->GetBluetoothAddress()));
71 
72 	simpleDiscoveryListener* dListener = new simpleDiscoveryListener(device);
73 
74 	dAgent->StartInquiry(BT_GIAC, dListener);
75 
76 	thread_id sender;
77 	(void)receive_data(&sender, NULL, 0);
78 
79     printf("Retrieving names ...\n");
80 
81     for (int32 index = 0 ; index < dAgent->RetrieveDevices(0).CountItems(); index++ ) {
82 
83         RemoteDevice* rDevice = dAgent->RetrieveDevices(0).ItemAt(index);
84 	    printf("\t%s \t@ %s ...\n", rDevice->GetFriendlyName(true).String(), bdaddrUtils::ToString(rDevice->GetBluetoothAddress()));
85 
86     }
87 
88 }
89 
90 static status_t
91 LocalDeviceError(status_t status)
92 {
93     fprintf(stderr,"No Device/s found");
94 
95     return status;
96 }
97 
98 
99 int
100 main(int argc, char *argv[])
101 {
102 
103 	mainThread = find_thread(NULL);
104 
105         if (argc == 2) {
106             // device specified
107             LocalDevice* device = LocalDevice::GetLocalDevice(atoi(argv[0]));
108             if (device == NULL)
109                return LocalDeviceError(ENODEV);
110 
111             DumpInfo(device);
112 
113         } else if (argc == 1) {
114             // show all devices
115             LocalDevice* device = NULL;
116 
117             printf("Performing discovery for %ld Bluetooth Local Devices ...\n", LocalDevice::GetLocalDeviceCount());
118 
119             for (uint32 index = 0 ; index < LocalDevice::GetLocalDeviceCount() ; index++) {
120 
121                device = LocalDevice::GetLocalDevice();
122                if (device == NULL) {
123                     LocalDeviceError(ENODEV);
124                     continue;
125                }
126                DumpInfo(device);
127 
128             }
129 
130             return B_OK;
131 
132         } else {
133                 fprintf(stderr,"Usage: bt_dev_info [device]\n");
134                 return B_ERROR;
135         }
136 }
137