1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/dom/bluetooth/bluedroid/BluetoothUtils.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,189 @@ 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 "base/basictypes.h" 1.11 + 1.12 +#include "BluetoothReplyRunnable.h" 1.13 +#include "BluetoothService.h" 1.14 +#include "BluetoothServiceBluedroid.h" 1.15 +#include "BluetoothUtils.h" 1.16 +#include "jsapi.h" 1.17 +#include "mozilla/Scoped.h" 1.18 +#include "mozilla/dom/bluetooth/BluetoothTypes.h" 1.19 +#include "nsContentUtils.h" 1.20 +#include "nsCxPusher.h" 1.21 +#include "nsIScriptContext.h" 1.22 +#include "nsISystemMessagesInternal.h" 1.23 +#include "nsString.h" 1.24 +#include "nsTArray.h" 1.25 +#include "nsServiceManagerUtils.h" 1.26 + 1.27 +BEGIN_BLUETOOTH_NAMESPACE 1.28 + 1.29 +const bt_interface_t* 1.30 +GetBluetoothInterface() 1.31 +{ 1.32 + return BluetoothServiceBluedroid::GetBluetoothInterface(); 1.33 +} 1.34 + 1.35 +void 1.36 +StringToBdAddressType(const nsAString& aBdAddress, 1.37 + bt_bdaddr_t *aRetBdAddressType) 1.38 +{ 1.39 + NS_ConvertUTF16toUTF8 bdAddressUTF8(aBdAddress); 1.40 + const char* str = bdAddressUTF8.get(); 1.41 + 1.42 + for (int i = 0; i < 6; i++) { 1.43 + aRetBdAddressType->address[i] = (uint8_t) strtoul(str, (char **)&str, 16); 1.44 + str++; 1.45 + } 1.46 +} 1.47 + 1.48 +void 1.49 +BdAddressTypeToString(bt_bdaddr_t* aBdAddressType, nsAString& aRetBdAddress) 1.50 +{ 1.51 + uint8_t* addr = aBdAddressType->address; 1.52 + char bdstr[18]; 1.53 + 1.54 + sprintf(bdstr, "%02x:%02x:%02x:%02x:%02x:%02x", 1.55 + (int)addr[0],(int)addr[1],(int)addr[2], 1.56 + (int)addr[3],(int)addr[4],(int)addr[5]); 1.57 + 1.58 + aRetBdAddress = NS_ConvertUTF8toUTF16(bdstr); 1.59 +} 1.60 + 1.61 +bool 1.62 +SetJsObject(JSContext* aContext, 1.63 + const BluetoothValue& aValue, 1.64 + JS::Handle<JSObject*> aObj) 1.65 +{ 1.66 + MOZ_ASSERT(aContext && aObj); 1.67 + 1.68 + if (aValue.type() != BluetoothValue::TArrayOfBluetoothNamedValue) { 1.69 + BT_WARNING("SetJsObject: Invalid parameter type"); 1.70 + return false; 1.71 + } 1.72 + 1.73 + const nsTArray<BluetoothNamedValue>& arr = 1.74 + aValue.get_ArrayOfBluetoothNamedValue(); 1.75 + 1.76 + for (uint32_t i = 0; i < arr.Length(); i++) { 1.77 + JS::Rooted<JS::Value> val(aContext); 1.78 + const BluetoothValue& v = arr[i].value(); 1.79 + 1.80 + switch(v.type()) { 1.81 + case BluetoothValue::TnsString: { 1.82 + JSString* jsData = JS_NewUCStringCopyN(aContext, 1.83 + v.get_nsString().BeginReading(), 1.84 + v.get_nsString().Length()); 1.85 + NS_ENSURE_TRUE(jsData, false); 1.86 + val = STRING_TO_JSVAL(jsData); 1.87 + break; 1.88 + } 1.89 + case BluetoothValue::Tuint32_t: 1.90 + val = INT_TO_JSVAL(v.get_uint32_t()); 1.91 + break; 1.92 + case BluetoothValue::Tbool: 1.93 + val = BOOLEAN_TO_JSVAL(v.get_bool()); 1.94 + break; 1.95 + default: 1.96 + BT_WARNING("SetJsObject: Parameter is not handled"); 1.97 + break; 1.98 + } 1.99 + 1.100 + if (!JS_SetProperty(aContext, aObj, 1.101 + NS_ConvertUTF16toUTF8(arr[i].name()).get(), 1.102 + val)) { 1.103 + BT_WARNING("Failed to set property"); 1.104 + return false; 1.105 + } 1.106 + } 1.107 + 1.108 + return true; 1.109 +} 1.110 + 1.111 +bool 1.112 +BroadcastSystemMessage(const nsAString& aType, 1.113 + const BluetoothValue& aData) 1.114 +{ 1.115 + mozilla::AutoSafeJSContext cx; 1.116 + NS_ASSERTION(!::JS_IsExceptionPending(cx), 1.117 + "Shouldn't get here when an exception is pending!"); 1.118 + 1.119 + nsCOMPtr<nsISystemMessagesInternal> systemMessenger = 1.120 + do_GetService("@mozilla.org/system-message-internal;1"); 1.121 + NS_ENSURE_TRUE(systemMessenger, false); 1.122 + 1.123 + JS::Rooted<JS::Value> value(cx); 1.124 + if (aData.type() == BluetoothValue::TnsString) { 1.125 + JSString* jsData = JS_NewUCStringCopyN(cx, 1.126 + aData.get_nsString().BeginReading(), 1.127 + aData.get_nsString().Length()); 1.128 + value = STRING_TO_JSVAL(jsData); 1.129 + } else if (aData.type() == BluetoothValue::TArrayOfBluetoothNamedValue) { 1.130 + JS::Rooted<JSObject*> obj(cx, JS_NewObject(cx, nullptr, JS::NullPtr(), 1.131 + JS::NullPtr())); 1.132 + if (!obj) { 1.133 + BT_WARNING("Failed to new JSObject for system message!"); 1.134 + return false; 1.135 + } 1.136 + 1.137 + if (!SetJsObject(cx, aData, obj)) { 1.138 + BT_WARNING("Failed to set properties of system message!"); 1.139 + return false; 1.140 + } 1.141 + value = JS::ObjectValue(*obj); 1.142 + } else { 1.143 + BT_WARNING("Not support the unknown BluetoothValue type"); 1.144 + return false; 1.145 + } 1.146 + 1.147 + systemMessenger->BroadcastMessage(aType, value, 1.148 + JS::UndefinedHandleValue); 1.149 + 1.150 + return true; 1.151 +} 1.152 + 1.153 +void 1.154 +DispatchBluetoothReply(BluetoothReplyRunnable* aRunnable, 1.155 + const BluetoothValue& aValue, 1.156 + const nsAString& aErrorStr) 1.157 +{ 1.158 + // Reply will be deleted by the runnable after running on main thread 1.159 + BluetoothReply* reply; 1.160 + if (!aErrorStr.IsEmpty()) { 1.161 + nsString err(aErrorStr); 1.162 + reply = new BluetoothReply(BluetoothReplyError(err)); 1.163 + } else { 1.164 + MOZ_ASSERT(aValue.type() != BluetoothValue::T__None); 1.165 + reply = new BluetoothReply(BluetoothReplySuccess(aValue)); 1.166 + } 1.167 + 1.168 + aRunnable->SetReply(reply); 1.169 + if (NS_FAILED(NS_DispatchToMainThread(aRunnable))) { 1.170 + BT_WARNING("Failed to dispatch to main thread!"); 1.171 + } 1.172 +} 1.173 + 1.174 +void 1.175 +DispatchStatusChangedEvent(const nsAString& aType, 1.176 + const nsAString& aAddress, 1.177 + bool aStatus) 1.178 +{ 1.179 + MOZ_ASSERT(NS_IsMainThread()); 1.180 + 1.181 + InfallibleTArray<BluetoothNamedValue> data; 1.182 + BT_APPEND_NAMED_VALUE(data, "address", nsString(aAddress)); 1.183 + BT_APPEND_NAMED_VALUE(data, "status", aStatus); 1.184 + 1.185 + BluetoothSignal signal(nsString(aType), NS_LITERAL_STRING(KEY_ADAPTER), data); 1.186 + 1.187 + BluetoothService* bs = BluetoothService::Get(); 1.188 + NS_ENSURE_TRUE_VOID(bs); 1.189 + bs->DistributeSignal(signal); 1.190 +} 1.191 + 1.192 +END_BLUETOOTH_NAMESPACE