Bug 1207649: Prepare |BluetoothAddress| for general use throughout Bluetooth code, r=brsun

This patch moves |BluetoothAddress| to BluetoothCommon.h, where it is
available for general use. New utility function convert between strings
and addresses. A new hash-key class allowes for using |BluetoothAddress|
as the key in a hash table.
This commit is contained in:
Thomas Zimmermann 2015-09-30 09:42:33 +02:00
Родитель 81d96b3fc9
Коммит 25f47780dd
7 изменённых файлов: 241 добавлений и 4 удалений

Просмотреть файл

@ -32,10 +32,6 @@ enum BluetoothAclState {
ACL_STATE_DISCONNECTED
};
struct BluetoothAddress {
uint8_t mAddr[6];
};
struct BluetoothAvrcpAttributeTextPairs {
BluetoothAvrcpAttributeTextPairs(const uint8_t* aAttr,
const char** aText,

Просмотреть файл

@ -0,0 +1,24 @@
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* 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 "BluetoothCommon.h"
BEGIN_BLUETOOTH_NAMESPACE
//
// |BluetoothAddress|
//
const BluetoothAddress BluetoothAddress::ANY(0x00, 0x00, 0x00,
0x00, 0x00, 0x00);
const BluetoothAddress BluetoothAddress::ALL(0xff, 0xff, 0xff,
0xff, 0xff, 0xff);
const BluetoothAddress BluetoothAddress::LOCAL(0x00, 0x00, 0x00,
0xff, 0xff, 0xff);
END_BLUETOOTH_NAMESPACE

Просмотреть файл

@ -371,6 +371,108 @@ struct BluetoothActivityEnergyInfo {
uint64_t mEnergyUsed; /* a product of mA, V and ms */
};
/**
* |BluetoothAddress| stores the 6-byte MAC address of a Bluetooth
* device. The constants ANY, ALL and LOCAL represent addresses with
* special meaning.
*/
struct BluetoothAddress {
static const BluetoothAddress ANY;
static const BluetoothAddress ALL;
static const BluetoothAddress LOCAL;
uint8_t mAddr[6];
BluetoothAddress()
{
Clear(); // assign ANY
}
MOZ_IMPLICIT BluetoothAddress(const BluetoothAddress&) = default;
BluetoothAddress(uint8_t aAddr0, uint8_t aAddr1,
uint8_t aAddr2, uint8_t aAddr3,
uint8_t aAddr4, uint8_t aAddr5)
{
mAddr[0] = aAddr0;
mAddr[1] = aAddr1;
mAddr[2] = aAddr2;
mAddr[3] = aAddr3;
mAddr[4] = aAddr4;
mAddr[5] = aAddr5;
}
BluetoothAddress& operator=(const BluetoothAddress&) = default;
bool operator==(const BluetoothAddress& aRhs) const
{
return !memcmp(mAddr, aRhs.mAddr, sizeof(mAddr));
}
bool operator!=(const BluetoothAddress& aRhs) const
{
return !operator==(aRhs);
}
/**
* |Clear| assigns an invalid value (i.e., ANY) to the address.
*/
void Clear()
{
operator=(ANY);
}
/*
* Getter and setter methods for the address parts. The figure
* below illustrates the mapping to bytes; from LSB to MSB.
*
* | LAP | UAP | NAP |
* | 0 | 1 | 2 | 3 | 4 | 5 |
*
* See Bluetooth Core Spec 2.1, Sec 1.2.
*/
uint32_t GetLAP() const
{
return (static_cast<uint32_t>(mAddr[0])) |
(static_cast<uint32_t>(mAddr[1]) << 8) |
(static_cast<uint32_t>(mAddr[2]) << 16);
}
void SetLAP(uint32_t aLAP)
{
MOZ_ASSERT(!(aLAP & 0xff000000)); // no top-8 bytes in LAP
mAddr[0] = aLAP;
mAddr[1] = aLAP >> 8;
mAddr[2] = aLAP >> 16;
}
uint8_t GetUAP() const
{
return mAddr[3];
}
void SetUAP(uint8_t aUAP)
{
mAddr[3] = aUAP;
}
uint16_t GetNAP() const
{
return (static_cast<uint16_t>(mAddr[4])) |
(static_cast<uint16_t>(mAddr[5]) << 8);
}
void SetNAP(uint16_t aNAP)
{
mAddr[4] = aNAP;
mAddr[5] = aNAP >> 8;
}
};
struct BluetoothUuid {
uint8_t mUuid[16];

Просмотреть файл

@ -0,0 +1,61 @@
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=2 sts=2 et sw=2 tw=80: */
/* 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/. */
/* This file contains hash-table keys for Bluetooth classes. */
#ifndef mozilla_dom_bluetooth_BluetoothHashKeys_h
#define mozilla_dom_bluetooth_BluetoothHashKeys_h
#include "BluetoothCommon.h"
#include <mozilla/HashFunctions.h>
#include <nsHashKeys.h>
BEGIN_BLUETOOTH_NAMESPACE
/**
* Implements a hash-table key for |BluetoothAddress|.
*/
class BluetoothAddressHashKey : public PLDHashEntryHdr
{
public:
enum {
ALLOW_MEMMOVE = true
};
typedef const BluetoothAddress& KeyType;
typedef const BluetoothAddress* KeyTypePointer;
explicit BluetoothAddressHashKey(KeyTypePointer aKey)
: mValue(*aKey)
{ }
BluetoothAddressHashKey(const BluetoothAddressHashKey& aToCopy)
: mValue(aToCopy.mValue)
{ }
~BluetoothAddressHashKey()
{ }
KeyType GetKey() const
{
return mValue;
}
bool KeyEquals(KeyTypePointer aKey) const
{
return *aKey == mValue;
}
static KeyTypePointer KeyToPointer(KeyType aKey)
{
return &aKey;
}
static PLDHashNumber HashKey(KeyTypePointer aKey)
{
return HashBytes(aKey->mAddr, MOZ_ARRAY_LENGTH(aKey->mAddr));
}
private:
const BluetoothAddress mValue;
};
END_BLUETOOTH_NAMESPACE
#endif // mozilla_dom_bluetooth_BluetoothHashKeys_h

Просмотреть файл

@ -19,6 +19,48 @@
BEGIN_BLUETOOTH_NAMESPACE
void
AddressToString(const BluetoothAddress& aAddress, nsAString& aString)
{
char str[BLUETOOTH_ADDRESS_LENGTH + 1];
int res = snprintf(str, sizeof(str), "%02x:%02x:%02x:%02x:%02x:%02x",
static_cast<int>(aAddress.mAddr[0]),
static_cast<int>(aAddress.mAddr[1]),
static_cast<int>(aAddress.mAddr[2]),
static_cast<int>(aAddress.mAddr[3]),
static_cast<int>(aAddress.mAddr[4]),
static_cast<int>(aAddress.mAddr[5]));
if ((res == EOF) ||
(res < 0) ||
(static_cast<size_t>(res) >= sizeof(str))) {
/* Conversion should have succeeded or (a) we're out of memory, or
* (b) our code is massively broken. We should crash in both cases.
*/
MOZ_CRASH("Failed to convert Bluetooth address to string");
}
aString = NS_ConvertUTF8toUTF16(str);
}
nsresult
StringToAddress(const nsAString& aString, BluetoothAddress& aAddress)
{
int res = sscanf(NS_ConvertUTF16toUTF8(aString).get(),
"%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx",
&aAddress.mAddr[0],
&aAddress.mAddr[1],
&aAddress.mAddr[2],
&aAddress.mAddr[3],
&aAddress.mAddr[4],
&aAddress.mAddr[5]);
if (res < static_cast<ssize_t>(MOZ_ARRAY_LENGTH(aAddress.mAddr))) {
return NS_ERROR_ILLEGAL_VALUE;
}
return NS_OK;
}
void
UuidToString(const BluetoothUuid& aUuid, nsAString& aString)
{

Просмотреть файл

@ -23,6 +23,16 @@ class BluetoothNamedValue;
class BluetoothReplyRunnable;
class BluetoothValue;
//
// Address/String conversion
//
void
AddressToString(const BluetoothAddress& aAddress, nsAString& aString);
nsresult
StringToAddress(const nsAString& aString, BluetoothAddress& aAddress);
//
// BluetoothUuid <-> uuid string conversion
//

Просмотреть файл

@ -16,6 +16,7 @@ if CONFIG['MOZ_B2G_BT']:
]
SOURCES += [
'common/BluetoothCommon.cpp',
'common/BluetoothHidManager.cpp',
'common/BluetoothInterface.cpp',
'common/BluetoothProfileController.cpp',
@ -137,6 +138,7 @@ EXPORTS.mozilla.dom.bluetooth.ipc += [
]
EXPORTS.mozilla.dom.bluetooth += [
'common/BluetoothCommon.h',
'common/BluetoothHashKeys.h',
'common/webapi/BluetoothAdapter.h',
'common/webapi/BluetoothClassOfDevice.h',
'common/webapi/BluetoothDevice.h',