2012-02-20 15:15:19 +00:00
|
|
|
/* -*- Mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; tab-width: 40 -*- */
|
|
|
|
/* vim: set ts=2 et sw=2 tw=40: */
|
|
|
|
/* 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-03-27 23:17:35 +00:00
|
|
|
#include "BluetoothAdapter.h"
|
|
|
|
|
2012-02-20 15:15:19 +00:00
|
|
|
#include "nsDOMClassInfo.h"
|
2012-02-23 15:55:48 +00:00
|
|
|
#include "nsDOMEvent.h"
|
|
|
|
#include "nsThreadUtils.h"
|
|
|
|
#include "nsXPCOMCIDInternal.h"
|
|
|
|
#include "mozilla/LazyIdleThread.h"
|
2012-03-27 23:17:35 +00:00
|
|
|
#include "mozilla/Util.h"
|
2012-03-08 23:14:06 +00:00
|
|
|
#include <dlfcn.h>
|
2012-03-27 23:17:35 +00:00
|
|
|
|
|
|
|
static void
|
|
|
|
FireEnabled(bool aResult, nsIDOMDOMRequest* aDomRequest)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIDOMRequestService> rs = do_GetService("@mozilla.org/dom/dom-request-service;1");
|
|
|
|
|
|
|
|
if (!rs) {
|
|
|
|
NS_WARNING("No DOMRequest Service!");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
mozilla::DebugOnly<nsresult> rv = aResult ?
|
|
|
|
rs->FireSuccess(aDomRequest, JSVAL_VOID) :
|
|
|
|
rs->FireError(aDomRequest,
|
|
|
|
NS_LITERAL_STRING("Bluetooth firmware loading failed"));
|
|
|
|
|
|
|
|
NS_WARN_IF_FALSE(NS_SUCCEEDED(rv), "Bluetooth firmware loading failed");
|
|
|
|
}
|
2012-02-20 15:15:19 +00:00
|
|
|
|
2012-03-05 03:54:01 +00:00
|
|
|
USING_BLUETOOTH_NAMESPACE
|
2012-02-23 15:55:48 +00:00
|
|
|
|
2012-03-08 23:14:06 +00:00
|
|
|
static struct BluedroidFunctions {
|
|
|
|
bool initialized;
|
|
|
|
bool tried_initialization;
|
|
|
|
|
|
|
|
BluedroidFunctions() :
|
|
|
|
initialized(false),
|
|
|
|
tried_initialization(false)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
int (* bt_enable)();
|
|
|
|
int (* bt_disable)();
|
|
|
|
int (* bt_is_enabled)();
|
|
|
|
} sBluedroidFunctions;
|
|
|
|
|
|
|
|
static bool EnsureBluetoothInit() {
|
|
|
|
if (sBluedroidFunctions.tried_initialization)
|
|
|
|
{
|
|
|
|
return sBluedroidFunctions.initialized;
|
|
|
|
}
|
|
|
|
|
|
|
|
sBluedroidFunctions.initialized = false;
|
|
|
|
sBluedroidFunctions.tried_initialization = true;
|
|
|
|
|
|
|
|
void* handle = dlopen("libbluedroid.so", RTLD_LAZY);
|
|
|
|
|
|
|
|
if(!handle) {
|
|
|
|
NS_ERROR("Failed to open libbluedroid.so, bluetooth cannot run");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
sBluedroidFunctions.bt_enable = (int (*)())dlsym(handle, "bt_enable");
|
|
|
|
if(sBluedroidFunctions.bt_enable == NULL) {
|
|
|
|
NS_ERROR("Failed to attach bt_enable function");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
sBluedroidFunctions.bt_disable = (int (*)())dlsym(handle, "bt_disable");
|
|
|
|
if(sBluedroidFunctions.bt_disable == NULL) {
|
|
|
|
NS_ERROR("Failed to attach bt_disable function");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
sBluedroidFunctions.bt_is_enabled = (int (*)())dlsym(handle, "bt_is_enabled");
|
|
|
|
if(sBluedroidFunctions.bt_is_enabled == NULL) {
|
|
|
|
NS_ERROR("Failed to attach bt_is_enabled function");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
sBluedroidFunctions.initialized = true;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-02-23 15:55:48 +00:00
|
|
|
class ToggleBtResultTask : public nsRunnable
|
2012-02-20 15:15:19 +00:00
|
|
|
{
|
2012-02-23 15:55:48 +00:00
|
|
|
public:
|
2012-03-27 23:17:35 +00:00
|
|
|
ToggleBtResultTask(nsRefPtr<BluetoothAdapter>& adapterPtr,
|
|
|
|
nsCOMPtr<nsIDOMDOMRequest>& req,
|
|
|
|
bool enabled,
|
|
|
|
bool result)
|
|
|
|
: mResult(result),
|
|
|
|
mEnabled(enabled)
|
2012-02-23 15:55:48 +00:00
|
|
|
{
|
2012-03-05 03:54:01 +00:00
|
|
|
MOZ_ASSERT(!NS_IsMainThread());
|
2012-02-23 15:55:48 +00:00
|
|
|
|
2012-03-27 23:17:35 +00:00
|
|
|
mDOMRequest.swap(req);
|
2012-02-23 15:55:48 +00:00
|
|
|
mAdapterPtr.swap(adapterPtr);
|
|
|
|
}
|
|
|
|
|
2012-03-05 03:54:01 +00:00
|
|
|
NS_IMETHOD Run()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
2012-03-27 23:17:35 +00:00
|
|
|
// Update bt power status to BluetoothAdapter only if loading bluetooth
|
|
|
|
// firmware succeeds.
|
|
|
|
if (mResult) {
|
|
|
|
mAdapterPtr->SetEnabledInternal(mEnabled);
|
2012-03-05 03:54:01 +00:00
|
|
|
}
|
2012-03-27 23:17:35 +00:00
|
|
|
|
|
|
|
FireEnabled(mResult, mDOMRequest);
|
|
|
|
|
2012-03-05 03:54:01 +00:00
|
|
|
//mAdapterPtr must be null before returning to prevent the background
|
|
|
|
//thread from racing to release it during the destruction of this runnable.
|
|
|
|
mAdapterPtr = nsnull;
|
2012-03-27 23:17:35 +00:00
|
|
|
mDOMRequest = nsnull;
|
2012-02-23 15:55:48 +00:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
nsRefPtr<BluetoothAdapter> mAdapterPtr;
|
2012-03-27 23:17:35 +00:00
|
|
|
nsCOMPtr<nsIDOMDOMRequest> mDOMRequest;
|
|
|
|
bool mEnabled;
|
2012-03-05 03:54:01 +00:00
|
|
|
bool mResult;
|
2012-02-23 15:55:48 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
class ToggleBtTask : public nsRunnable
|
|
|
|
{
|
|
|
|
public:
|
2012-03-27 23:17:35 +00:00
|
|
|
ToggleBtTask(bool enabled, nsIDOMDOMRequest* req, BluetoothAdapter* adapterPtr)
|
|
|
|
: mEnabled(enabled),
|
|
|
|
mDOMRequest(req),
|
2012-03-05 03:54:01 +00:00
|
|
|
mAdapterPtr(adapterPtr)
|
2012-02-23 15:55:48 +00:00
|
|
|
{
|
2012-03-05 03:54:01 +00:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2012-02-23 15:55:48 +00:00
|
|
|
}
|
|
|
|
|
2012-03-05 03:54:01 +00:00
|
|
|
NS_IMETHOD Run()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(!NS_IsMainThread());
|
2012-02-23 15:55:48 +00:00
|
|
|
|
2012-03-27 23:17:35 +00:00
|
|
|
bool result;
|
|
|
|
|
|
|
|
#ifdef MOZ_WIDGET_GONK
|
|
|
|
// Platform specific check for gonk until object is divided in
|
|
|
|
// different implementations per platform. Linux doesn't require
|
|
|
|
// bluetooth firmware loading, but code should work otherwise.
|
2012-03-08 23:14:06 +00:00
|
|
|
if(!EnsureBluetoothInit()) {
|
|
|
|
NS_ERROR("Failed to load bluedroid library.\n");
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2012-03-27 23:17:35 +00:00
|
|
|
// return 1 if it's enabled, 0 if it's disabled, and -1 on error
|
|
|
|
int isEnabled = sBluedroidFunctions.bt_is_enabled();
|
2012-03-08 23:14:06 +00:00
|
|
|
|
2012-03-27 23:17:35 +00:00
|
|
|
if ((isEnabled == 1 && mEnabled) || (isEnabled == 0 && !mEnabled)) {
|
|
|
|
result = true;
|
|
|
|
} else if (isEnabled < 0) {
|
|
|
|
result = false;
|
|
|
|
} else if (mEnabled) {
|
|
|
|
result = (sBluedroidFunctions.bt_enable() == 0) ? true : false;
|
2012-02-23 15:55:48 +00:00
|
|
|
} else {
|
2012-03-27 23:17:35 +00:00
|
|
|
result = (sBluedroidFunctions.bt_disable() == 0) ? true : false;
|
2012-02-23 15:55:48 +00:00
|
|
|
}
|
2012-03-27 23:17:35 +00:00
|
|
|
#else
|
|
|
|
result = true;
|
|
|
|
NS_WARNING("No bluetooth support in this build configuration, faking a success event instead");
|
|
|
|
#endif
|
2012-02-23 15:55:48 +00:00
|
|
|
|
|
|
|
// Create a result thread and pass it to Main Thread,
|
2012-03-27 23:17:35 +00:00
|
|
|
nsCOMPtr<nsIRunnable> resultRunnable = new ToggleBtResultTask(mAdapterPtr, mDOMRequest, mEnabled, result);
|
2012-03-05 03:54:01 +00:00
|
|
|
|
|
|
|
if (NS_FAILED(NS_DispatchToMainThread(resultRunnable))) {
|
|
|
|
NS_WARNING("Failed to dispatch to main thread!");
|
|
|
|
}
|
2012-02-23 15:55:48 +00:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2012-03-27 23:17:35 +00:00
|
|
|
bool mEnabled;
|
2012-03-08 23:14:06 +00:00
|
|
|
nsRefPtr<BluetoothAdapter> mAdapterPtr;
|
2012-03-27 23:17:35 +00:00
|
|
|
nsCOMPtr<nsIDOMDOMRequest> mDOMRequest;
|
2012-02-23 15:55:48 +00:00
|
|
|
};
|
|
|
|
|
2012-03-05 03:54:01 +00:00
|
|
|
DOMCI_DATA(BluetoothAdapter, BluetoothAdapter)
|
2012-02-23 15:55:48 +00:00
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_CLASS(BluetoothAdapter)
|
|
|
|
|
2012-03-05 03:54:01 +00:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_INHERITED(BluetoothAdapter,
|
|
|
|
nsDOMEventTargetHelper)
|
2012-03-27 23:17:35 +00:00
|
|
|
NS_CYCLE_COLLECTION_TRAVERSE_EVENT_HANDLER(enabled)
|
2012-03-05 03:54:01 +00:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
|
2012-02-20 15:15:19 +00:00
|
|
|
|
2012-03-05 03:54:01 +00:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN_INHERITED(BluetoothAdapter,
|
|
|
|
nsDOMEventTargetHelper)
|
2012-03-27 23:17:35 +00:00
|
|
|
NS_CYCLE_COLLECTION_UNLINK_EVENT_HANDLER(enabled)
|
2012-03-05 03:54:01 +00:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_END
|
2012-02-23 15:55:48 +00:00
|
|
|
|
2012-03-05 03:54:01 +00:00
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION_INHERITED(BluetoothAdapter)
|
2012-02-20 15:15:19 +00:00
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIDOMBluetoothAdapter)
|
|
|
|
NS_DOM_INTERFACE_MAP_ENTRY_CLASSINFO(BluetoothAdapter)
|
2012-02-23 15:55:48 +00:00
|
|
|
NS_INTERFACE_MAP_END_INHERITING(nsDOMEventTargetHelper)
|
|
|
|
|
|
|
|
NS_IMPL_ADDREF_INHERITED(BluetoothAdapter, nsDOMEventTargetHelper)
|
|
|
|
NS_IMPL_RELEASE_INHERITED(BluetoothAdapter, nsDOMEventTargetHelper)
|
|
|
|
|
2012-03-27 23:17:35 +00:00
|
|
|
BluetoothAdapter::BluetoothAdapter(nsPIDOMWindow *aWindow)
|
2012-02-23 15:55:48 +00:00
|
|
|
{
|
2012-03-27 23:17:35 +00:00
|
|
|
BindToOwner(aWindow);
|
2012-02-23 15:55:48 +00:00
|
|
|
}
|
2012-02-20 15:15:19 +00:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-03-27 23:17:35 +00:00
|
|
|
BluetoothAdapter::SetEnabled(bool aEnabled, nsIDOMDOMRequest** aDomRequest)
|
2012-02-20 15:15:19 +00:00
|
|
|
{
|
2012-03-27 23:17:35 +00:00
|
|
|
nsCOMPtr<nsIDOMRequestService> rs = do_GetService("@mozilla.org/dom/dom-request-service;1");
|
2012-02-20 15:15:19 +00:00
|
|
|
|
2012-03-27 23:17:35 +00:00
|
|
|
if (!rs) {
|
|
|
|
NS_ERROR("No DOMRequest Service!");
|
2012-03-08 23:14:06 +00:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
2012-02-23 15:55:48 +00:00
|
|
|
|
2012-03-27 23:17:35 +00:00
|
|
|
nsCOMPtr<nsIDOMDOMRequest> request;
|
|
|
|
nsresult rv = rs->CreateRequest(GetOwner(), getter_AddRefs(request));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2012-02-23 15:55:48 +00:00
|
|
|
if (!mToggleBtThread) {
|
|
|
|
mToggleBtThread = new LazyIdleThread(15000);
|
|
|
|
}
|
|
|
|
|
2012-03-27 23:17:35 +00:00
|
|
|
nsCOMPtr<nsIRunnable> r = new ToggleBtTask(aEnabled, request, this);
|
2012-03-05 03:54:01 +00:00
|
|
|
|
2012-03-27 23:17:35 +00:00
|
|
|
rv = mToggleBtThread->Dispatch(r, NS_DISPATCH_NORMAL);
|
2012-02-23 15:55:48 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2012-03-27 23:17:35 +00:00
|
|
|
request.forget(aDomRequest);
|
2012-02-23 15:55:48 +00:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-03-27 23:17:35 +00:00
|
|
|
NS_IMETHODIMP
|
|
|
|
BluetoothAdapter::GetEnabled(bool* aEnabled)
|
|
|
|
{
|
|
|
|
*aEnabled = mEnabled;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|