Assorted whitespace cleanup and typo fixes.
[haiku.git] / src / bin / bt_discovery.cpp
blob7ce9af47bee28a42e12a850b82ed3e2152f36aaa
1 /*
2 * Copyright 2008 Oliver Ruiz Dorantes, oliver.ruiz.dorantes_at_gmail.com
3 * All rights reserved. Distributed under the terms of the MIT License.
4 */
6 #include <stdio.h>
7 #include <stdlib.h>
9 #include <bluetooth/bdaddrUtils.h>
10 #include <bluetooth/LocalDevice.h>
12 #include <bluetooth/DeviceClass.h>
13 #include <bluetooth/DiscoveryAgent.h>
14 #include <bluetooth/DiscoveryListener.h>
16 thread_id mainThread;
18 class SimpleDiscoveryListener : public DiscoveryListener {
20 public:
22 SimpleDiscoveryListener()
23 : DiscoveryListener()
29 void
30 DeviceDiscovered(RemoteDevice* btDevice, DeviceClass cod)
32 BString classString;
34 printf("\t%s: Device %s discovered.\n",__FUNCTION__,
35 bdaddrUtils::ToString(btDevice->GetBluetoothAddress()));
37 cod.GetServiceClass(classString);
38 classString << " |";
39 cod.GetMajorDeviceClass(classString);
40 classString << " |";
41 cod.GetMinorDeviceClass(classString);
43 printf("\t\t%s: \n", classString.String());
47 void
48 InquiryCompleted(int discType)
50 printf("\t%s: Inquiry process has finished ...\n",__FUNCTION__);
51 (void)send_data(mainThread, discType, NULL, 0);
55 void
56 InquiryStarted(status_t status)
58 printf("\t%s: Inquiry process has started ...\n",__FUNCTION__);
65 void
66 DumpInfo(LocalDevice* device)
68 DiscoveryAgent* dAgent = device->GetDiscoveryAgent();
70 if (dAgent == NULL) {
71 printf("DiscoveryAgent could not be located\n");
72 return;
75 printf("Discovering for [LocalDevice] %s\t%s\n\n",
76 (device->GetFriendlyName()).String(),
77 bdaddrUtils::ToString(device->GetBluetoothAddress()));
79 SimpleDiscoveryListener* dListener = new SimpleDiscoveryListener();
81 dAgent->StartInquiry(BT_GIAC, dListener);
83 thread_id sender;
84 (void)receive_data(&sender, NULL, 0);
86 printf("Retrieving names ...\n");
88 for (int32 index = 0 ; index < dAgent->RetrieveDevices(0).CountItems(); index++ ) {
90 RemoteDevice* rDevice = dAgent->RetrieveDevices(0).ItemAt(index);
91 printf("\t%s \t@ %s ...\n", rDevice->GetFriendlyName(true).String(),
92 bdaddrUtils::ToString(rDevice->GetBluetoothAddress()));
99 static status_t
100 LocalDeviceError(status_t status)
102 fprintf(stderr,"No Device/s found");
104 return status;
109 main(int argc, char *argv[])
111 mainThread = find_thread(NULL);
113 if (argc == 2) {
114 // device specified
115 LocalDevice* device = LocalDevice::GetLocalDevice(atoi(argv[0]));
116 if (device == NULL)
117 return LocalDeviceError(ENODEV);
119 DumpInfo(device);
121 } else if (argc == 1) {
122 // show all devices
123 LocalDevice* device = NULL;
125 printf("Performing discovery for %ld Bluetooth Local Devices ...\n",
126 LocalDevice::GetLocalDeviceCount());
128 for (uint32 index = 0 ; index < LocalDevice::GetLocalDeviceCount() ; index++) {
130 device = LocalDevice::GetLocalDevice();
131 if (device == NULL) {
132 LocalDeviceError(ENODEV);
133 continue;
135 DumpInfo(device);
139 return B_OK;
141 } else {
142 fprintf(stderr,"Usage: bt_dev_info [device]\n");
143 return B_ERROR;