15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Copyright (c) 2012 The Chromium Authors. All rights reserved. 25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be 35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// found in the LICENSE file. 45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 51320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "extensions/browser/api/bluetooth/bluetooth_api_utils.h" 65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 7868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "base/strings/utf_string_conversions.h" 85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/values.h" 92a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "device/bluetooth/bluetooth_adapter.h" 105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "device/bluetooth/bluetooth_device.h" 111320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "extensions/common/api/bluetooth.h" 125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 131320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tuccinamespace bluetooth = extensions::core_api::bluetooth; 14a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) 15a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)using device::BluetoothDevice; 16a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)using bluetooth::VendorIdSource; 17a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) 18a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)namespace { 19a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) 20a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)bool ConvertVendorIDSourceToApi(const BluetoothDevice::VendorIDSource& input, 21a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) bluetooth::VendorIdSource* output) { 22a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) switch (input) { 23a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) case BluetoothDevice::VENDOR_ID_UNKNOWN: 24a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) *output = bluetooth::VENDOR_ID_SOURCE_NONE; 25a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) return true; 26a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) case BluetoothDevice::VENDOR_ID_BLUETOOTH: 27a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) *output = bluetooth::VENDOR_ID_SOURCE_BLUETOOTH; 28a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) return true; 29a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) case BluetoothDevice::VENDOR_ID_USB: 30a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) *output = bluetooth::VENDOR_ID_SOURCE_USB; 31a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) return true; 32a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) default: 33a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) NOTREACHED(); 34a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) return false; 35a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) } 36a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)} 37a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) 38a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)bool ConvertDeviceTypeToApi(const BluetoothDevice::DeviceType& input, 39a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) bluetooth::DeviceType* output) { 40a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) switch (input) { 41a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) case BluetoothDevice::DEVICE_UNKNOWN: 42a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) *output = bluetooth::DEVICE_TYPE_NONE; 43a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) return true; 44a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) case BluetoothDevice::DEVICE_COMPUTER: 45a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) *output = bluetooth::DEVICE_TYPE_COMPUTER; 46a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) return true; 47a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) case BluetoothDevice::DEVICE_PHONE: 48a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) *output = bluetooth::DEVICE_TYPE_PHONE; 49a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) return true; 50a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) case BluetoothDevice::DEVICE_MODEM: 51a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) *output = bluetooth::DEVICE_TYPE_MODEM; 52a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) return true; 53a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) case BluetoothDevice::DEVICE_AUDIO: 54a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) *output = bluetooth::DEVICE_TYPE_AUDIO; 55a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) return true; 56a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) case BluetoothDevice::DEVICE_CAR_AUDIO: 57a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) *output = bluetooth::DEVICE_TYPE_CARAUDIO; 58a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) return true; 59a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) case BluetoothDevice::DEVICE_VIDEO: 60a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) *output = bluetooth::DEVICE_TYPE_VIDEO; 61a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) return true; 62a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) case BluetoothDevice::DEVICE_PERIPHERAL: 63a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) *output = bluetooth::DEVICE_TYPE_PERIPHERAL; 64a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) return true; 65a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) case BluetoothDevice::DEVICE_JOYSTICK: 66a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) *output = bluetooth::DEVICE_TYPE_JOYSTICK; 67a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) return true; 68a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) case BluetoothDevice::DEVICE_GAMEPAD: 69a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) *output = bluetooth::DEVICE_TYPE_GAMEPAD; 70a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) return true; 71a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) case BluetoothDevice::DEVICE_KEYBOARD: 72a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) *output = bluetooth::DEVICE_TYPE_KEYBOARD; 73a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) return true; 74a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) case BluetoothDevice::DEVICE_MOUSE: 75a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) *output = bluetooth::DEVICE_TYPE_MOUSE; 76a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) return true; 77a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) case BluetoothDevice::DEVICE_TABLET: 78a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) *output = bluetooth::DEVICE_TYPE_TABLET; 79a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) return true; 80a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) case BluetoothDevice::DEVICE_KEYBOARD_MOUSE_COMBO: 81a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) *output = bluetooth::DEVICE_TYPE_KEYBOARDMOUSECOMBO; 82a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) return true; 83a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) default: 84a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) return false; 85a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) } 86a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)} 87a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) 88a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)} // namespace 89a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) 905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace extensions { 911320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tuccinamespace core_api { 925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace bluetooth { 935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void BluetoothDeviceToApiDevice(const device::BluetoothDevice& device, 955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) Device* out) { 96c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) out->address = device.GetAddress(); 975d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) out->name.reset(new std::string(base::UTF16ToUTF8(device.GetName()))); 98a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) out->device_class.reset(new int(device.GetBluetoothClass())); 99a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) 100a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) // Only include the Device ID members when one exists for the device, and 101a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) // always include all or none. 102a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) if (ConvertVendorIDSourceToApi(device.GetVendorIDSource(), 103a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) &(out->vendor_id_source)) && 104a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) out->vendor_id_source != VENDOR_ID_SOURCE_NONE) { 105a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) out->vendor_id.reset(new int(device.GetVendorID())); 106a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) out->product_id.reset(new int(device.GetProductID())); 107a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) out->device_id.reset(new int(device.GetDeviceID())); 108a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) } 109a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) 110a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) ConvertDeviceTypeToApi(device.GetDeviceType(), &(out->type)); 111a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) 112c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) out->paired.reset(new bool(device.IsPaired())); 113c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) out->connected.reset(new bool(device.IsConnected())); 11423730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles) 1155c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu int rssi = device.GetRSSI(); 1165c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu if (rssi != BluetoothDevice::kUnknownPower) 1175c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu out->rssi.reset(new int(rssi)); 1185c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu 1195c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu if (*out->connected) { 1205c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu int current_transmit_power = device.GetCurrentHostTransmitPower(); 1215c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu if (current_transmit_power != BluetoothDevice::kUnknownPower) 1225c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu out->current_host_transmit_power.reset(new int(current_transmit_power)); 1235c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu int maximum_transmit_power = device.GetMaximumHostTransmitPower(); 1245c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu if (maximum_transmit_power != BluetoothDevice::kUnknownPower) 1255c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu out->maximum_host_transmit_power.reset(new int(maximum_transmit_power)); 1265c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu } 1275c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu 128c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch std::vector<std::string>* string_uuids = new std::vector<std::string>(); 129c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch const device::BluetoothDevice::UUIDList& uuids = device.GetUUIDs(); 130c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch for (device::BluetoothDevice::UUIDList::const_iterator iter = uuids.begin(); 131c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch iter != uuids.end(); ++iter) 132c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch string_uuids->push_back(iter->canonical_value()); 133c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch out->uuids.reset(string_uuids); 1345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 1355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1362a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void PopulateAdapterState(const device::BluetoothAdapter& adapter, 1372a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) AdapterState* out) { 1382a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) out->discovering = adapter.IsDiscovering(); 1392a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) out->available = adapter.IsPresent(); 1402a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) out->powered = adapter.IsPowered(); 141c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) out->name = adapter.GetName(); 142c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) out->address = adapter.GetAddress(); 1435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 1445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} // namespace bluetooth 1461320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci} // namespace core_api 1475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} // namespace extensions 148