1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/dom/bluetooth/BluetoothUuid.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,88 @@ 1.4 +/* -*- Mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; tab-width: 40 -*- */ 1.5 +/* vim: set ts=2 et sw=2 tw=80: */ 1.6 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.7 + * License, v. 2.0. If a copy of the MPL was not distributed with this file, 1.8 + * You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.9 + 1.10 +#include "BluetoothUuid.h" 1.11 + 1.12 +#include "BluetoothA2dpManager.h" 1.13 +#include "BluetoothHfpManager.h" 1.14 +#include "BluetoothHidManager.h" 1.15 +#include "BluetoothOppManager.h" 1.16 + 1.17 +USING_BLUETOOTH_NAMESPACE 1.18 + 1.19 +void 1.20 +BluetoothUuidHelper::GetString(BluetoothServiceClass aServiceClass, 1.21 + nsAString& aRetUuidStr) 1.22 +{ 1.23 + aRetUuidStr.Truncate(); 1.24 + 1.25 + aRetUuidStr.AppendLiteral("0000"); 1.26 + aRetUuidStr.AppendInt(aServiceClass, 16); 1.27 + aRetUuidStr.AppendLiteral("-0000-1000-8000-00805F9B34FB"); 1.28 +} 1.29 + 1.30 +BluetoothServiceClass 1.31 +BluetoothUuidHelper::GetBluetoothServiceClass(const nsAString& aUuidStr) 1.32 +{ 1.33 + // An example of input UUID string: 0000110D-0000-1000-8000-00805F9B34FB 1.34 + MOZ_ASSERT(aUuidStr.Length() == 36); 1.35 + 1.36 + /** 1.37 + * Extract uuid16 from input UUID string and return a value of enum 1.38 + * BluetoothServiceClass. If we failed to recognize the value, 1.39 + * BluetoothServiceClass::UNKNOWN is returned. 1.40 + */ 1.41 + BluetoothServiceClass retValue = BluetoothServiceClass::UNKNOWN; 1.42 + nsString uuid(Substring(aUuidStr, 4, 4)); 1.43 + 1.44 + nsresult rv; 1.45 + int32_t integer = uuid.ToInteger(&rv, 16); 1.46 + NS_ENSURE_SUCCESS(rv, retValue); 1.47 + 1.48 + return GetBluetoothServiceClass(integer); 1.49 +} 1.50 + 1.51 +BluetoothServiceClass 1.52 +BluetoothUuidHelper::GetBluetoothServiceClass(uint16_t aServiceUuid) 1.53 +{ 1.54 + BluetoothServiceClass retValue = BluetoothServiceClass::UNKNOWN; 1.55 + switch (aServiceUuid) { 1.56 + case BluetoothServiceClass::A2DP: 1.57 + case BluetoothServiceClass::HANDSFREE: 1.58 + case BluetoothServiceClass::HANDSFREE_AG: 1.59 + case BluetoothServiceClass::HEADSET: 1.60 + case BluetoothServiceClass::HEADSET_AG: 1.61 + case BluetoothServiceClass::HID: 1.62 + case BluetoothServiceClass::OBJECT_PUSH: 1.63 + retValue = (BluetoothServiceClass)aServiceUuid; 1.64 + } 1.65 + return retValue; 1.66 +} 1.67 + 1.68 +BluetoothProfileManagerBase* 1.69 +BluetoothUuidHelper::GetBluetoothProfileManager(uint16_t aServiceUuid) 1.70 +{ 1.71 + BluetoothProfileManagerBase* profile; 1.72 + BluetoothServiceClass serviceClass = GetBluetoothServiceClass(aServiceUuid); 1.73 + switch (serviceClass) { 1.74 + case BluetoothServiceClass::HANDSFREE: 1.75 + case BluetoothServiceClass::HEADSET: 1.76 + profile = BluetoothHfpManager::Get(); 1.77 + break; 1.78 + case BluetoothServiceClass::HID: 1.79 + profile = BluetoothHidManager::Get(); 1.80 + break; 1.81 + case BluetoothServiceClass::A2DP: 1.82 + profile = BluetoothA2dpManager::Get(); 1.83 + break; 1.84 + case BluetoothServiceClass::OBJECT_PUSH: 1.85 + profile = BluetoothOppManager::Get(); 1.86 + break; 1.87 + default: 1.88 + profile = nullptr; 1.89 + } 1.90 + return profile; 1.91 +}