1 /* 2 * Copyright 2008 Oliver Ruiz Dorantes, oliver.ruiz.dorantes_at_gmail.com 3 * Copyright 2008 Mika Lindqvist, monni1995_at_gmail.com 4 * All rights reserved. Distributed under the terms of the MIT License. 5 */ 6 7 #include <bluetooth/DeviceClass.h> 8 #include <bluetooth/DiscoveryAgent.h> 9 #include <bluetooth/DiscoveryListener.h> 10 #include <bluetooth/bdaddrUtils.h> 11 #include <bluetooth/LocalDevice.h> 12 #include <bluetooth/RemoteDevice.h> 13 14 #include <bluetooth/HCI/btHCI_command.h> 15 #include <bluetooth/HCI/btHCI_event.h> 16 17 #include <bluetooth/bluetooth_error.h> 18 19 #include <CommandManager.h> 20 #include <bluetoothserver_p.h> 21 22 #include "KitSupport.h" 23 24 25 namespace Bluetooth { 26 27 28 bool 29 RemoteDevice::IsTrustedDevice(void) 30 { 31 return true; 32 } 33 34 35 BString 36 RemoteDevice::GetFriendlyName(bool alwaysAsk) 37 { 38 if (!alwaysAsk) { 39 // Check if the name is already retrieved 40 // TODO: Check if It is known from a KnownDevicesList 41 return BString("Not implemented"); 42 } 43 44 if (fDiscovererLocalDevice == NULL) 45 return BString("#NoOwnerError#Not Valid name"); 46 47 if (fMessenger == NULL) 48 return BString("#ServerNotReady#Not Valid name"); 49 50 void* remoteNameCommand = NULL; 51 size_t size; 52 53 // Issue inquiry command 54 BMessage request(BT_MSG_HANDLE_SIMPLE_REQUEST); 55 BMessage reply; 56 57 request.AddInt32("hci_id", fDiscovererLocalDevice->ID()); 58 59 // Fill the request 60 remoteNameCommand = buildRemoteNameRequest(fBdaddr, fPageRepetitionMode, fClockOffset, &size); 61 62 request.AddData("raw command", B_ANY_TYPE, remoteNameCommand, size); 63 64 request.AddInt16("eventExpected", HCI_EVENT_CMD_STATUS); 65 request.AddInt16("opcodeExpected", PACK_OPCODE(OGF_LINK_CONTROL, OCF_REMOTE_NAME_REQUEST)); 66 67 request.AddInt16("eventExpected", HCI_EVENT_REMOTE_NAME_REQUEST_COMPLETE); 68 69 70 if (fMessenger->SendMessage(&request, &reply) == B_OK) { 71 BString name; 72 int8 status; 73 74 if ((reply.FindInt8("status", &status) == B_OK) && (status == BT_OK)) { 75 76 if ((reply.FindString("friendlyname", &name) == B_OK )) { 77 return name; 78 } else { 79 return BString(""); // should not happen 80 } 81 82 } else { 83 // seems we got a negative event 84 return BString("#CommandFailed#Not Valid name"); 85 } 86 } 87 88 return BString("#NotCompletedRequest#Not Valid name"); 89 } 90 91 92 BString 93 RemoteDevice::GetFriendlyName() 94 { 95 return GetFriendlyName(true); 96 } 97 98 99 bdaddr_t 100 RemoteDevice::GetBluetoothAddress() 101 { 102 return fBdaddr; 103 } 104 105 106 bool 107 RemoteDevice::Equals(RemoteDevice* obj) 108 { 109 bdaddr_t ba = obj->GetBluetoothAddress(); 110 111 return bdaddrUtils::Compare(&fBdaddr, &ba); 112 } 113 114 115 // static RemoteDevice* GetRemoteDevice(Connection conn); 116 117 118 bool 119 RemoteDevice::Authenticate() 120 { 121 return true; 122 } 123 124 125 // bool Authorize(Connection conn); 126 // bool Encrypt(Connection conn, bool on); 127 128 129 bool 130 RemoteDevice::IsAuthenticated() 131 { 132 return true; 133 } 134 135 136 // bool IsAuthorized(Connection conn); 137 138 139 bool 140 RemoteDevice::IsEncrypted() 141 { 142 return true; 143 } 144 145 146 LocalDevice* 147 RemoteDevice::GetLocalDeviceOwner() 148 { 149 return fDiscovererLocalDevice; 150 } 151 152 153 /* Private */ 154 void 155 RemoteDevice::SetLocalDeviceOwner(LocalDevice* ld) 156 { 157 fDiscovererLocalDevice = ld; 158 } 159 160 161 /* Constructor */ 162 RemoteDevice::RemoteDevice(const bdaddr_t address, uint8 record[3]) 163 { 164 fBdaddr = address; 165 fDeviceClass.SetRecord(record); 166 fMessenger = _RetrieveBluetoothMessenger(); 167 } 168 169 170 RemoteDevice::RemoteDevice(const BString& address) 171 { 172 fDeviceClass.SetRecord((uint32)0); 173 fBdaddr = bdaddrUtils::FromString((const char *)address.String()); 174 fMessenger = _RetrieveBluetoothMessenger(); 175 } 176 177 178 RemoteDevice::~RemoteDevice() 179 { 180 delete fMessenger; 181 } 182 183 184 BString 185 RemoteDevice::GetProperty(const char* property) /* Throwing */ 186 { 187 return NULL; 188 } 189 190 191 status_t 192 RemoteDevice::GetProperty(const char* property, uint32* value) /* Throwing */ 193 { 194 return B_ERROR; 195 } 196 197 198 DeviceClass 199 RemoteDevice::GetDeviceClass() 200 { 201 return fDeviceClass; 202 } 203 204 205 } 206