2012-02-20 07:15:19 -08: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/. */
|
|
|
|
|
|
|
|
#include "nsDOMClassInfo.h"
|
2012-02-23 07:55:48 -08:00
|
|
|
#include "nsDOMEvent.h"
|
|
|
|
#include "nsThreadUtils.h"
|
|
|
|
#include "nsXPCOMCIDInternal.h"
|
|
|
|
#include "mozilla/LazyIdleThread.h"
|
2012-03-08 15:14:06 -08:00
|
|
|
#include <dlfcn.h>
|
2012-02-23 07:55:48 -08:00
|
|
|
#include "BluetoothAdapter.h"
|
2012-02-20 07:15:19 -08:00
|
|
|
|
2012-03-04 19:54:01 -08:00
|
|
|
USING_BLUETOOTH_NAMESPACE
|
2012-02-23 07:55:48 -08:00
|
|
|
|
2012-03-08 15:14:06 -08: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 07:55:48 -08:00
|
|
|
class ToggleBtResultTask : public nsRunnable
|
2012-02-20 07:15:19 -08:00
|
|
|
{
|
2012-02-23 07:55:48 -08:00
|
|
|
public:
|
2012-03-04 19:54:01 -08:00
|
|
|
ToggleBtResultTask(nsRefPtr<BluetoothAdapter>& adapterPtr, bool result)
|
2012-02-23 07:55:48 -08:00
|
|
|
: mResult(result)
|
|
|
|
{
|
2012-03-04 19:54:01 -08:00
|
|
|
MOZ_ASSERT(!NS_IsMainThread());
|
2012-02-23 07:55:48 -08:00
|
|
|
|
|
|
|
mAdapterPtr.swap(adapterPtr);
|
|
|
|
}
|
|
|
|
|
2012-03-04 19:54:01 -08:00
|
|
|
NS_IMETHOD Run()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
|
|
|
if (!mResult) {
|
|
|
|
//TODO:Bug-731361
|
|
|
|
NS_WARNING("BT firmware loading fails.\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
//mAdapterPtr must be null before returning to prevent the background
|
|
|
|
//thread from racing to release it during the destruction of this runnable.
|
2012-02-23 07:55:48 -08:00
|
|
|
mAdapterPtr->FirePowered();
|
2012-03-04 19:54:01 -08:00
|
|
|
mAdapterPtr = nsnull;
|
2012-02-23 07:55:48 -08:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
nsRefPtr<BluetoothAdapter> mAdapterPtr;
|
2012-03-04 19:54:01 -08:00
|
|
|
bool mResult;
|
2012-02-23 07:55:48 -08:00
|
|
|
};
|
|
|
|
|
|
|
|
class ToggleBtTask : public nsRunnable
|
|
|
|
{
|
|
|
|
public:
|
2012-03-08 15:14:06 -08:00
|
|
|
ToggleBtTask(bool onOff, BluetoothAdapter* adapterPtr)
|
2012-02-23 07:55:48 -08:00
|
|
|
: mOnOff(onOff),
|
2012-03-04 19:54:01 -08:00
|
|
|
mAdapterPtr(adapterPtr)
|
2012-02-23 07:55:48 -08:00
|
|
|
{
|
2012-03-04 19:54:01 -08:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2012-02-23 07:55:48 -08:00
|
|
|
}
|
|
|
|
|
2012-03-04 19:54:01 -08:00
|
|
|
NS_IMETHOD Run()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(!NS_IsMainThread());
|
2012-02-23 07:55:48 -08:00
|
|
|
|
2012-03-08 15:14:06 -08:00
|
|
|
if(!EnsureBluetoothInit()) {
|
|
|
|
NS_ERROR("Failed to load bluedroid library.\n");
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2012-03-04 19:54:01 -08:00
|
|
|
bool result;
|
2012-02-23 07:55:48 -08:00
|
|
|
|
|
|
|
//Toggle BT here
|
2012-03-08 15:14:06 -08:00
|
|
|
|
2012-02-23 07:55:48 -08:00
|
|
|
if (mOnOff) {
|
2012-03-08 15:14:06 -08:00
|
|
|
result = sBluedroidFunctions.bt_enable();
|
2012-02-23 07:55:48 -08:00
|
|
|
} else {
|
2012-03-08 15:14:06 -08:00
|
|
|
result = sBluedroidFunctions.bt_disable();
|
2012-02-23 07:55:48 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Create a result thread and pass it to Main Thread,
|
2012-03-04 19:54:01 -08:00
|
|
|
nsCOMPtr<nsIRunnable> resultRunnable = new ToggleBtResultTask(mAdapterPtr, result);
|
|
|
|
|
|
|
|
if (NS_FAILED(NS_DispatchToMainThread(resultRunnable))) {
|
|
|
|
NS_WARNING("Failed to dispatch to main thread!");
|
|
|
|
}
|
2012-02-23 07:55:48 -08:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
bool mOnOff;
|
2012-03-08 15:14:06 -08:00
|
|
|
nsRefPtr<BluetoothAdapter> mAdapterPtr;
|
2012-02-23 07:55:48 -08:00
|
|
|
};
|
|
|
|
|
2012-03-04 19:54:01 -08:00
|
|
|
DOMCI_DATA(BluetoothAdapter, BluetoothAdapter)
|
2012-02-23 07:55:48 -08:00
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_CLASS(BluetoothAdapter)
|
|
|
|
|
2012-03-04 19:54:01 -08:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_INHERITED(BluetoothAdapter,
|
|
|
|
nsDOMEventTargetHelper)
|
|
|
|
NS_CYCLE_COLLECTION_TRAVERSE_EVENT_HANDLER(powered)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
|
2012-02-20 07:15:19 -08:00
|
|
|
|
2012-03-04 19:54:01 -08:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN_INHERITED(BluetoothAdapter,
|
|
|
|
nsDOMEventTargetHelper)
|
|
|
|
NS_CYCLE_COLLECTION_UNLINK_EVENT_HANDLER(powered)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_END
|
2012-02-23 07:55:48 -08:00
|
|
|
|
2012-03-04 19:54:01 -08:00
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION_INHERITED(BluetoothAdapter)
|
2012-02-20 07:15:19 -08:00
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIDOMBluetoothAdapter)
|
|
|
|
NS_DOM_INTERFACE_MAP_ENTRY_CLASSINFO(BluetoothAdapter)
|
2012-02-23 07:55:48 -08:00
|
|
|
NS_INTERFACE_MAP_END_INHERITING(nsDOMEventTargetHelper)
|
|
|
|
|
|
|
|
NS_IMPL_ADDREF_INHERITED(BluetoothAdapter, nsDOMEventTargetHelper)
|
|
|
|
NS_IMPL_RELEASE_INHERITED(BluetoothAdapter, nsDOMEventTargetHelper)
|
|
|
|
|
2012-03-04 19:54:01 -08:00
|
|
|
BluetoothAdapter::BluetoothAdapter()
|
|
|
|
: mPower(false)
|
2012-02-23 07:55:48 -08:00
|
|
|
{
|
|
|
|
}
|
2012-02-20 07:15:19 -08:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
BluetoothAdapter::GetPower(bool* aPower)
|
|
|
|
{
|
2012-03-08 15:14:06 -08:00
|
|
|
#ifdef MOZ_WIDGET_GONK
|
|
|
|
if(!EnsureBluetoothInit()) {
|
|
|
|
NS_ERROR("Failed to load bluedroid library.\n");
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
*aPower = sBluedroidFunctions.bt_is_enabled();
|
|
|
|
#else
|
2012-02-20 07:15:19 -08:00
|
|
|
*aPower = mPower;
|
2012-03-08 15:14:06 -08:00
|
|
|
#endif
|
2012-02-20 07:15:19 -08:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
BluetoothAdapter::SetPower(bool aPower)
|
|
|
|
{
|
2012-03-08 15:14:06 -08:00
|
|
|
|
|
|
|
#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.
|
|
|
|
if(!EnsureBluetoothInit()) {
|
|
|
|
NS_ERROR("Failed to load bluedroid library.\n");
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
#endif
|
2012-02-23 07:55:48 -08:00
|
|
|
if (mPower != aPower) {
|
|
|
|
mPower = aPower;
|
|
|
|
|
2012-03-08 15:14:06 -08:00
|
|
|
#ifdef MOZ_WIDGET_GONK
|
2012-03-04 19:54:01 -08:00
|
|
|
return ToggleBluetoothAsync();
|
2012-03-08 15:14:06 -08:00
|
|
|
#endif
|
2012-02-23 07:55:48 -08:00
|
|
|
}
|
|
|
|
|
2012-02-20 07:15:19 -08:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2012-02-23 07:55:48 -08:00
|
|
|
|
2012-03-04 19:54:01 -08:00
|
|
|
nsresult
|
2012-02-23 07:55:48 -08:00
|
|
|
BluetoothAdapter::ToggleBluetoothAsync()
|
|
|
|
{
|
|
|
|
if (!mToggleBtThread) {
|
|
|
|
mToggleBtThread = new LazyIdleThread(15000);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIRunnable> r = new ToggleBtTask(mPower, this);
|
|
|
|
|
2012-03-04 19:54:01 -08:00
|
|
|
return mToggleBtThread->Dispatch(r, NS_DISPATCH_NORMAL);
|
2012-02-23 07:55:48 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
BluetoothAdapter::FirePowered()
|
|
|
|
{
|
|
|
|
nsRefPtr<nsDOMEvent> event = new nsDOMEvent(nsnull, nsnull);
|
2012-03-04 19:54:01 -08:00
|
|
|
nsresult rv = event->InitEvent(NS_LITERAL_STRING("powered"), false, false);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
rv = event->SetTrusted(true);
|
2012-02-23 07:55:48 -08:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
bool dummy;
|
|
|
|
rv = DispatchEvent(event, &dummy);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMPL_EVENT_HANDLER(BluetoothAdapter, powered)
|