Bug 939022 - BluetoothUtils for bluedroid. Patch 2/3: Modify files, r=echou

This commit is contained in:
Ben Tian 2013-12-23 11:24:11 +08:00
parent c232ce69db
commit 6fe5d8ef31
7 changed files with 55 additions and 110 deletions

View File

@ -14,7 +14,6 @@
#include "BluetoothCommon.h"
#include "BluetoothService.h"
#include "BluetoothServiceBluedroid.h"
#include "BluetoothSocket.h"
#include "BluetoothUtils.h"

View File

@ -8,7 +8,6 @@
#include "BluetoothHfpManager.h"
#include "BluetoothProfileController.h"
#include "BluetoothServiceBluedroid.h"
#include "BluetoothUtils.h"
#include "jsapi.h"

View File

@ -11,8 +11,8 @@
#include <sys/socket.h>
#include "base/message_loop.h"
#include "BluetoothServiceBluedroid.h"
#include "BluetoothSocketObserver.h"
#include "BluetoothUtils.h"
#include "mozilla/FileUtils.h"
#include "mozilla/RefPtr.h"
#include "nsThreadUtils.h"

View File

@ -8,6 +8,7 @@
#include "BluetoothReplyRunnable.h"
#include "BluetoothService.h"
#include "BluetoothServiceBluedroid.h"
#include "BluetoothUtils.h"
#include "jsapi.h"
#include "mozilla/Scoped.h"
@ -22,6 +23,38 @@
BEGIN_BLUETOOTH_NAMESPACE
const bt_interface_t*
GetBluetoothInterface()
{
return BluetoothServiceBluedroid::GetBluetoothInterface();
}
void
StringToBdAddressType(const nsAString& aBdAddress,
bt_bdaddr_t *aRetBdAddressType)
{
NS_ConvertUTF16toUTF8 bdAddressUTF8(aBdAddress);
const char* str = bdAddressUTF8.get();
for (int i = 0; i < 6; i++) {
aRetBdAddressType->address[i] = (uint8_t) strtoul(str, (char **)&str, 16);
str++;
}
}
void
BdAddressTypeToString(bt_bdaddr_t* aBdAddressType, nsAString& aRetBdAddress)
{
uint8_t* addr = aBdAddressType->address;
char bdstr[18];
sprintf(bdstr, "%02x:%02x:%02x:%02x:%02x:%02x",
(int)addr[0],(int)addr[1],(int)addr[2],
(int)addr[3],(int)addr[4],(int)addr[5]);
aRetBdAddress = NS_ConvertUTF8toUTF16(bdstr);
}
bool
SetJsObject(JSContext* aContext,
const BluetoothValue& aValue,
@ -72,37 +105,6 @@ SetJsObject(JSContext* aContext,
return true;
}
nsString
GetObjectPathFromAddress(const nsAString& aAdapterPath,
const nsAString& aDeviceAddress)
{
// 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
GetAddressFromObjectPath(const nsAString& aObjectPath)
{
// 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 == (int)address.Length());
address.Cut(0, addressHead);
address.ReplaceChar('_', ':');
return address;
}
bool
BroadcastSystemMessage(const nsAString& aType,
const InfallibleTArray<BluetoothNamedValue>& aData)
@ -154,27 +156,6 @@ DispatchBluetoothReply(BluetoothReplyRunnable* aRunnable,
}
}
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;
}
}
nsCString tmp(nsDependentCSubstring(aAtCommand, begin));
aRetValues.AppendElement(tmp);
}
void
DispatchStatusChangedEvent(const nsAString& aType,
const nsAString& aAddress,

View File

@ -7,6 +7,8 @@
#ifndef mozilla_dom_bluetooth_bluetoothutils_h__
#define mozilla_dom_bluetooth_bluetoothutils_h__
#include <hardware/bluetooth.h>
#include "BluetoothCommon.h"
#include "js/TypeDecls.h"
@ -16,18 +18,22 @@ class BluetoothNamedValue;
class BluetoothValue;
class BluetoothReplyRunnable;
const bt_interface_t*
GetBluetoothInterface();
void
StringToBdAddressType(const nsAString& aBdAddress,
bt_bdaddr_t *aRetBdAddressType);
void
BdAddressTypeToString(bt_bdaddr_t* aBdAddressType,
nsAString& aRetBdAddress);
bool
SetJsObject(JSContext* aContext,
const BluetoothValue& aValue,
JS::Handle<JSObject*> aObj);
nsString
GetObjectPathFromAddress(const nsAString& aAdapterPath,
const nsAString& aDeviceAddress);
nsString
GetAddressFromObjectPath(const nsAString& aObjectPath);
bool
BroadcastSystemMessage(const nsAString& aType,
const InfallibleTArray<BluetoothNamedValue>& aData);
@ -37,10 +43,6 @@ DispatchBluetoothReply(BluetoothReplyRunnable* aRunnable,
const BluetoothValue& aValue,
const nsAString& aErrorStr);
void
ParseAtCommand(const nsACString& aAtCommand, const int aStart,
nsTArray<nsCString>& aRetValues);
void
DispatchStatusChangedEvent(const nsAString& aType,
const nsAString& aDeviceAddress,

View File

@ -237,38 +237,6 @@ IsReady()
return true;
}
const bt_interface_t*
GetBluetoothInterface()
{
return sBtInterface;
}
void
StringToBdAddressType(const nsAString& aBdAddress,
bt_bdaddr_t *aRetBdAddressType)
{
NS_ConvertUTF16toUTF8 bdAddressUTF8(aBdAddress);
const char* str = bdAddressUTF8.get();
for (int i = 0; i < 6; i++) {
aRetBdAddressType->address[i] = (uint8_t) strtoul(str, (char **)&str, 16);
str++;
}
}
void
BdAddressTypeToString(bt_bdaddr_t* aBdAddressType, nsAString& aRetBdAddress)
{
uint8_t* addr = aBdAddressType->address;
bdstr_t bdstr;
sprintf((char*)bdstr, "%02x:%02x:%02x:%02x:%02x:%02x",
(int)addr[0],(int)addr[1],(int)addr[2],
(int)addr[3],(int)addr[4],(int)addr[5]);
aRetBdAddress = NS_ConvertUTF8toUTF16((char*)bdstr);
}
static void
AdapterStateChangeCallback(bt_state_t aStatus)
{
@ -1216,6 +1184,12 @@ ConnectDisconnect(bool aConnect, const nsAString& aDeviceAddress,
}
}
const bt_interface_t*
BluetoothServiceBluedroid::GetBluetoothInterface()
{
return sBtInterface;
}
void
BluetoothServiceBluedroid::Connect(const nsAString& aDeviceAddress,
uint32_t aCod,

View File

@ -12,23 +12,13 @@
#include "BluetoothCommon.h"
#include "BluetoothService.h"
typedef char bdstr_t[18];
const bt_interface_t*
GetBluetoothInterface();
void
StringToBdAddressType(const nsAString& aBdAddress,
bt_bdaddr_t *aRetBdAddressType);
void
BdAddressTypeToString(bt_bdaddr_t* aBdAddressType,
nsAString& aRetBdAddress);
BEGIN_BLUETOOTH_NAMESPACE
class BluetoothServiceBluedroid : public BluetoothService
{
public:
static const bt_interface_t* GetBluetoothInterface();
BluetoothServiceBluedroid();
~BluetoothServiceBluedroid();