2012-07-31 21:53:04 -07:00
|
|
|
/* -*- Mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; tab-width: 40 -*- */
|
2012-08-16 19:53:45 -07:00
|
|
|
/* vim: set ts=2 et sw=2 tw=80: */
|
2012-07-31 21:53:04 -07:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
|
|
|
* You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
2012-10-01 00:44:48 -07:00
|
|
|
#include "base/basictypes.h"
|
|
|
|
|
2012-11-18 23:10:38 -08:00
|
|
|
#include "BluetoothReplyRunnable.h"
|
2012-10-03 02:06:40 -07:00
|
|
|
#include "BluetoothUtils.h"
|
2012-07-31 21:53:04 -07:00
|
|
|
#include "jsapi.h"
|
|
|
|
#include "mozilla/Scoped.h"
|
2012-10-01 00:44:48 -07:00
|
|
|
#include "mozilla/dom/bluetooth/BluetoothTypes.h"
|
2012-10-03 02:06:40 -07:00
|
|
|
#include "nsContentUtils.h"
|
2013-05-22 09:05:27 -07:00
|
|
|
#include "nsCxPusher.h"
|
2013-01-28 22:52:39 -08:00
|
|
|
#include "nsIScriptContext.h"
|
2012-10-03 02:06:40 -07:00
|
|
|
#include "nsISystemMessagesInternal.h"
|
|
|
|
#include "nsString.h"
|
2012-11-18 23:10:38 -08:00
|
|
|
#include "nsTArray.h"
|
2012-10-03 02:06:40 -07:00
|
|
|
|
2012-11-18 23:10:38 -08:00
|
|
|
BEGIN_BLUETOOTH_NAMESPACE
|
2012-07-31 21:53:04 -07:00
|
|
|
|
2012-10-01 00:44:48 -07:00
|
|
|
bool
|
2012-11-18 23:10:38 -08:00
|
|
|
SetJsObject(JSContext* aContext,
|
2013-01-28 22:52:39 -08:00
|
|
|
const BluetoothValue& aValue,
|
|
|
|
JSObject* aObj)
|
2012-10-01 00:44:48 -07:00
|
|
|
{
|
2013-01-28 22:52:39 -08:00
|
|
|
MOZ_ASSERT(aContext && aObj);
|
2012-10-01 00:44:48 -07:00
|
|
|
|
2013-02-06 04:07:27 -08:00
|
|
|
if (aValue.type() != BluetoothValue::TArrayOfBluetoothNamedValue) {
|
|
|
|
NS_WARNING("SetJsObject: Invalid parameter type");
|
|
|
|
return false;
|
|
|
|
}
|
2013-01-28 22:52:39 -08:00
|
|
|
|
2013-02-06 04:07:27 -08:00
|
|
|
const nsTArray<BluetoothNamedValue>& arr =
|
|
|
|
aValue.get_ArrayOfBluetoothNamedValue();
|
2013-01-28 22:52:39 -08:00
|
|
|
|
2013-02-06 04:07:27 -08:00
|
|
|
for (uint32_t i = 0; i < arr.Length(); i++) {
|
2013-04-02 16:06:14 -07:00
|
|
|
JS::Value val;
|
2013-02-06 04:07:27 -08:00
|
|
|
const BluetoothValue& v = arr[i].value();
|
|
|
|
JSString* jsData;
|
2013-01-28 22:52:39 -08:00
|
|
|
|
2013-02-06 04:07:27 -08:00
|
|
|
switch(v.type()) {
|
|
|
|
case BluetoothValue::TnsString:
|
2013-02-18 00:02:23 -08:00
|
|
|
jsData = JS_NewUCStringCopyN(aContext,
|
|
|
|
v.get_nsString().BeginReading(),
|
|
|
|
v.get_nsString().Length());
|
2013-02-06 04:07:27 -08:00
|
|
|
NS_ENSURE_TRUE(jsData, false);
|
|
|
|
val = STRING_TO_JSVAL(jsData);
|
|
|
|
break;
|
|
|
|
case BluetoothValue::Tuint32_t:
|
|
|
|
val = INT_TO_JSVAL(v.get_uint32_t());
|
|
|
|
break;
|
|
|
|
case BluetoothValue::Tbool:
|
|
|
|
val = BOOLEAN_TO_JSVAL(v.get_bool());
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
NS_WARNING("SetJsObject: Parameter is not handled");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!JS_SetProperty(aContext, aObj,
|
|
|
|
NS_ConvertUTF16toUTF8(arr[i].name()).get(),
|
|
|
|
&val)) {
|
|
|
|
NS_WARNING("Failed to set property");
|
|
|
|
return false;
|
2013-01-28 22:52:39 -08:00
|
|
|
}
|
2012-10-01 00:44:48 -07:00
|
|
|
}
|
2013-01-28 22:52:39 -08:00
|
|
|
|
2012-10-01 00:44:48 -07:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsString
|
2012-11-18 23:10:38 -08:00
|
|
|
GetObjectPathFromAddress(const nsAString& aAdapterPath,
|
|
|
|
const nsAString& aDeviceAddress)
|
2012-10-01 00:44:48 -07:00
|
|
|
{
|
|
|
|
// The object path would be like /org/bluez/2906/hci0/dev_00_23_7F_CB_B4_F1,
|
|
|
|
// and the adapter path would be the first part of the object path, according
|
|
|
|
// to the example above, it's /org/bluez/2906/hci0.
|
|
|
|
nsString devicePath(aAdapterPath);
|
|
|
|
devicePath.AppendLiteral("/dev_");
|
|
|
|
devicePath.Append(aDeviceAddress);
|
|
|
|
devicePath.ReplaceChar(':', '_');
|
|
|
|
return devicePath;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsString
|
2012-11-18 23:10:38 -08:00
|
|
|
GetAddressFromObjectPath(const nsAString& aObjectPath)
|
2012-10-01 00:44:48 -07:00
|
|
|
{
|
|
|
|
// The object path would be like /org/bluez/2906/hci0/dev_00_23_7F_CB_B4_F1,
|
|
|
|
// and the adapter path would be the first part of the object path, according
|
|
|
|
// to the example above, it's /org/bluez/2906/hci0.
|
|
|
|
nsString address(aObjectPath);
|
|
|
|
int addressHead = address.RFind("/") + 5;
|
|
|
|
|
|
|
|
MOZ_ASSERT(addressHead + BLUETOOTH_ADDRESS_LENGTH == address.Length());
|
|
|
|
|
|
|
|
address.Cut(0, addressHead);
|
|
|
|
address.ReplaceChar('_', ':');
|
|
|
|
|
|
|
|
return address;
|
|
|
|
}
|
|
|
|
|
2012-10-03 02:06:40 -07:00
|
|
|
bool
|
2012-11-18 23:10:38 -08:00
|
|
|
BroadcastSystemMessage(const nsAString& aType,
|
|
|
|
const InfallibleTArray<BluetoothNamedValue>& aData)
|
2012-10-03 02:06:40 -07:00
|
|
|
{
|
2013-05-22 09:05:27 -07:00
|
|
|
mozilla::AutoSafeJSContext cx;
|
2012-10-03 02:06:40 -07:00
|
|
|
NS_ASSERTION(!::JS_IsExceptionPending(cx),
|
|
|
|
"Shouldn't get here when an exception is pending!");
|
|
|
|
|
|
|
|
JSObject* obj = JS_NewObject(cx, NULL, NULL, NULL);
|
|
|
|
if (!obj) {
|
|
|
|
NS_WARNING("Failed to new JSObject for system message!");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-01-28 22:52:39 -08:00
|
|
|
if (!SetJsObject(cx, aData, obj)) {
|
2012-10-03 02:06:40 -07:00
|
|
|
NS_WARNING("Failed to set properties of system message!");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsISystemMessagesInternal> systemMessenger =
|
|
|
|
do_GetService("@mozilla.org/system-message-internal;1");
|
2013-01-28 22:52:39 -08:00
|
|
|
NS_ENSURE_TRUE(systemMessenger, false);
|
2012-10-03 02:06:40 -07:00
|
|
|
|
|
|
|
systemMessenger->BroadcastMessage(aType, OBJECT_TO_JSVAL(obj));
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-11-18 23:10:38 -08:00
|
|
|
void
|
|
|
|
DispatchBluetoothReply(BluetoothReplyRunnable* aRunnable,
|
|
|
|
const BluetoothValue& aValue,
|
|
|
|
const nsAString& aErrorStr)
|
|
|
|
{
|
|
|
|
// Reply will be deleted by the runnable after running on main thread
|
|
|
|
BluetoothReply* reply;
|
|
|
|
if (!aErrorStr.IsEmpty()) {
|
|
|
|
nsString err(aErrorStr);
|
|
|
|
reply = new BluetoothReply(BluetoothReplyError(err));
|
|
|
|
} else {
|
|
|
|
MOZ_ASSERT(aValue.type() != BluetoothValue::T__None);
|
|
|
|
reply = new BluetoothReply(BluetoothReplySuccess(aValue));
|
|
|
|
}
|
|
|
|
|
|
|
|
aRunnable->SetReply(reply);
|
|
|
|
if (NS_FAILED(NS_DispatchToMainThread(aRunnable))) {
|
|
|
|
NS_WARNING("Failed to dispatch to main thread!");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-17 07:24:35 -08:00
|
|
|
void
|
|
|
|
ParseAtCommand(const nsACString& aAtCommand, const int aStart,
|
|
|
|
nsTArray<nsCString>& aRetValues)
|
|
|
|
{
|
|
|
|
int length = aAtCommand.Length();
|
|
|
|
int begin = aStart;
|
|
|
|
|
|
|
|
for (int i = aStart; i < length; ++i) {
|
|
|
|
// Use ',' as separator
|
|
|
|
if (aAtCommand[i] == ',') {
|
|
|
|
nsCString tmp(nsDependentCSubstring(aAtCommand, begin, i - begin));
|
|
|
|
aRetValues.AppendElement(tmp);
|
|
|
|
|
|
|
|
begin = i + 1;
|
|
|
|
}
|
|
|
|
}
|
2012-11-18 23:10:38 -08:00
|
|
|
|
2012-12-17 07:24:35 -08:00
|
|
|
nsCString tmp(nsDependentCSubstring(aAtCommand, begin));
|
|
|
|
aRetValues.AppendElement(tmp);
|
|
|
|
}
|
|
|
|
|
|
|
|
END_BLUETOOTH_NAMESPACE
|