Files
palemoon27/dom/bluetooth/bluedroid/BluetoothMapBMessage.cpp
T
roytam1 e333860065 import changes from `dev' branch of rmottola/Arctic-Fox:
- Bug 1251090 - Shell functions should check whether an off main thread compilation is for a script or a module r=shu (41b89b1232)
- Bug 1222420 - Make AutoFinishGC into a function r=terrence (a8f535528c)
- Bug 1253094, part 4 - Stop using DebugOnly for class/struct members in js/. r=billm (2c790aa526)
- Bug 1080968 - Don't use constexpr on clang-cl when emulating MSVC 2013 (e9cc2d7c7c)
- Bug 1257496 - Add information about cycle count and GC state to our GC stats r=sfink (6c2250bfba)
- Bug 1256178 - Drop the moz prefix from the MozCSSKeyframeRule and MozCSSKeyframesRule interfaces; r=bzbarsky (894855bce1)
- Bug 1249896 - Part 1: Remove unused AndOrLeft, AndOrRight, RelationalLeft, and RelationalRight. r=jwalden (c4db9dfecf)
- Bug 1249896 - Part 2: Remove unused type member from SIMD Int8x16Defnetc. r=bbouvier (7d3d7a4db1)
- Bug 1249896 - Part 3: Add JS namespace for GenericNaN. r=luke (69acc1f3ec)
- Bug 1249896 - Part 4: Avoid declaring variable between cases in switch. r=luke (28e5f94c24)
- Bug 1249896 - Part 5: Remove unused ReportInvalidTrapResult. r=efaust (caa948acac)
- Bug 1249896 - Part 7: Remove unnecessary static from my_ErrorReporter. r=sfink (7f723e079d)
- Bug 1249896 - Part 8: Remove unreachable code from GetLine. r=jwalden (108c857a1a)
- Bug 1249896 - Part 9: Include shell/jsshell.h in js.cpp. r=sfink (638bfc13c4)
- Bug 1253094, part 3 - Stop using DebugOnly for class/struct members in ipc/. r=billm (ab9e014158)
- Bug 1259737 - Make sure BackgroundImpl.cpp's mBoundThread is not used in release builds. r=froydnj (5c278ab398)
- Bug 1254515 - GonkCameraImage.cpp:16:5: error: class 'mozilla::GonkCameraImage' does not have any field named 'mThread' r=reuben (e5cac8cf5b)
- Bug 1250952: Create a join block in case we're in dead code after the condition; r=luke (1d6b706ae5)
- Bug 1249601 - Implement wasm i64 comparisons. r=sunfish (4927f709db)
- Bug 1250165 - Implement wasm i64 bitwise ops. r=luke (5c25322772)
- Bug 1251225 - Implement wasm i64 binary arithmetic operators. r=sunfish (f478fa8b4b)
- Bug 1251392 - Baldr: Implement PopcntI. r=sunfish (79556e947f)
- Bug 1229985 - remove nsAutoArrayPtr; r=erahm (91c49451ca)
- Bug 1251541: Replace |nsAutoArrayPtr<>| with |UniquePtr<[]>| in Bluetooth managers, r=shuang (2c1dc7ca89)
- Bug 1251541: Pass arguments to AVRCP passthrough command as uint8_t, r=shuang (73a094bd26)
- Bug 1251541: Replace |nsAutoArrayPtr<>| with |UniquePtr<[]>| in Bluetooth interfaces, r=shuang (371041f093)
- Bug 1249424 - Remove use of nsAutoArrayPtr from librlz. r=gerald (7cbcfea1b0)
- Bug 1249389 - part 0 - use getter_Transfers in TestStartupCache.cpp; r=erahm (ecb8818959)
- Bug 1249389 - part 1 - change NewObjectInputStreamFromBuffer to take a UniquePtr argument; r=erahm (0bdafa20ae)
- Bug 1249389 - part 2 - change NewBufferFromStorageStream's outparam into a UniquePtr; r=erahm (b27a70ce2d)
- Bug 1249389 - part 3 - assert the size of nsZipItemPtr's storage type; r=erahm (7c55da9ea7)
- Bug 1249389 - part 4 - make StartupCache::GetBuffer take a UniquePtr outparam; r=erahm (84b722479b)
- Bug 1249389 - part 5 - use UniquePtr instead of nsAutoArrayPtr in mozilla::scache::CacheEntry; r=erahm (0fd492ea95)
- Bug 1249389 - part 6 - provide UniquePtr overload for nsIStartupCache::GetBuffer; r=erahm (c27231d945)
- Bug 1249389 - followup - fix compilation bustage in gfxFT2FontList.cpp; r=me (a86a1b0399)
- Bug 1249389 - part 7 - clean up calls to GetBuffer in TestStartupCache; r=erahm (98939334b6)
- Bug 1249369 - use UniquePtr instead of nsAutoArrayPtr in MediaPipeline.cpp; r=jesup (7402a75a8a)
- Bug 1238842 - Add error codes to Gecko by following W3C spec. r=brsun (0dd283f6ba)
- Bug 1253217 - Change filename from BluetoothUuid to BluetoothUuidHelper. r=jocelyn (1946bcac26)
- Bug 1241382 - Revise default value of BluetoothAdapter.address and BluetoothDevice.address to empty string, f=wiwang, r=shuang (d9fdb97743)
- Bug 1223722: Transfer arrays of Bluetooth UUIDs in |BluetoothValue|, r=brsun (344af40591)
- Bug 1222956: Use |BluetoothUuid| in |BluetoothDiscoveryHandle|, r=tzimmermann (40512de386)
- Bug 1250694: Use functions to return static const BluetoothAddress and BluetoothUuid; r=tzimmermann (0c4b20066f)
- Bug 1236561 - part 1 - convert easy cases of nsAutoArrayPtr<T> to UniquePtr<T[]> in dom/bluetooth/; r=btian (31ef8b7b50)
- Bug 1236561 - part 2 - remove ObexHeaderSet::GetAuthChallenge; r=btian (f70daddd53)
- Bug 1236561 - part 3 - make BluetoothConfigurationParameter.mValue a UniquePtr; r=btian (a5a0ac988a)
- Bug 1239979: Add |BluetoothSocket::Accept| method, r=btian (ca92a0ab7c)
- Bug 1239979: Store pointer to Bluetooth socket interface in |BluetoothSocket|, r=btian (7c15e75133)
- Bug 1239979: Cleanup |BluetoothSocket|'s internals when connections close, r=btian (b6ac677d2e)
- Bug 1252841: Convert Bluetooth module to |UniquePtr<>|, r=btian (76d9759afd)
- Bug 1232687: Start bluetoothd with HAL service interface, r=shuang (379a68064f)
- Bug 1223729 - HID Connection Implementation, r=jocelyn, sr=mrbkap (0eeddf875e)
- Bug 1232670: Prepare Bluetooth PDU for sending after checking connection status, r=shuang (699ff14d9b)
- Bug 1252841: Convert Bluetooth daemon interfaces to |UniquePtr<>|, r=brsun (797260c6dc)
2024-02-07 15:25:46 +08:00

418 lines
10 KiB
C++

/* -*- Mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; tab-width: 40 -*- */
/* vim: set ts=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 "BluetoothMapBMessage.h"
#include "base/basictypes.h"
#include "plstr.h"
BEGIN_BLUETOOTH_NAMESPACE
namespace {
static const char kMapCrlf[] = "\r\n";
}
BluetoothMapBMessage::BluetoothMapBMessage(uint8_t* aObexBody, int aLength)
: mReadStatus(false)
, mPartId(0)
, mState(BMSG_PARSING_STATE_INVALID)
, mUnwindState(BMSG_PARSING_STATE_INVALID)
, mEnvelopeLevel(0)
{
const nsCString body = nsCString(reinterpret_cast<const char*>(aObexBody),
aLength);
ProcessDecode(body.get());
}
BluetoothMapBMessage::~BluetoothMapBMessage()
{}
/**
* Read one line by one line.
*
* @param aNextLineStart [in][out] On input, aNextLineStart is the start of
* the current line. On output, aNextLineStart is the start of the next
* line.
* @param aLine [out] The next line read.
*/
static int
ReadLine(const char* & aNextLineStart, nsACString& aLine)
{
aLine.Truncate();
for (;;) {
const char* eol = PL_strpbrk(aNextLineStart, kMapCrlf);
if (!eol) { // Reached end of file before newline
eol = aNextLineStart + strlen(aNextLineStart);
}
aLine.Append(aNextLineStart, eol - aNextLineStart);
if (*eol == '\r') {
++eol;
}
if (*eol == '\n') {
++eol;
}
aNextLineStart = eol;
if (*eol != ' ') {
// not a continuation
return 0;
}
}
}
static bool
ExtractParameter(const nsAutoCString& aCurrLine,
const char* aPattern, nsACString& aParam)
{
aParam.Truncate();
if (aCurrLine.Find(aPattern, false, 0, PL_strlen(aPattern)) == kNotFound) {
return false;
}
int32_t colonPos = aCurrLine.FindChar(':');
aParam = nsDependentCSubstring(aCurrLine, colonPos + 1, aCurrLine.Length());
return true;
}
/*
* |ProcessDecode| parses bMessage based on following MAP 3.1.3 Message Format.
* The state transition follows by BMsgParserState.
* BNF:
* <bmessage-object>::= {
* "BEGIN:BMSG" <CRLF>
* <bmessage-property>
* [<bmessage-originator>]*
* <bmessage-envelope>
* "END:BMSG" <CRLF>
* }
*
* <bmessage-envelope> ::= {
* "BEGIN:BENV" <CRLF>
* [<bmessage-recipient>]*
* <bmessage-envelope> | <bmessage-content>
* "END:BENV" <CRLF>
* }
*
* The state transition follows:
* BEGIN_BMSG->VERSION->STATUS->TYPE->FOLDER->ORIGINATOR->VCARD->ENVELOPE->
* VCARD->RECIPIENT->BODY->BMSG
*/
void
BluetoothMapBMessage::ProcessDecode(const char* aBuf)
{
mState = BMSG_PARSING_STATE_BEGIN_BMSG;
static void (BluetoothMapBMessage::* const ParserActions[])(const
nsAutoCString&) = {
nullptr, /* BMSG_PARSING_STATE_INVALID */
&BluetoothMapBMessage::ParseBeginBmsg,
&BluetoothMapBMessage::ParseVersion,
&BluetoothMapBMessage::ParseStatus,
&BluetoothMapBMessage::ParseType,
&BluetoothMapBMessage::ParseFolder,
&BluetoothMapBMessage::ParseOriginator,
&BluetoothMapBMessage::ParseVCard,
&BluetoothMapBMessage::ParseEnvelope,
&BluetoothMapBMessage::ParseRecipient,
&BluetoothMapBMessage::ParseBody,
&BluetoothMapBMessage::ParseBMsg
};
for (;;) {
nsAutoCString curLine;
if (ReadLine(aBuf, curLine) == -1) {
// Cannot find eol symbol
return;
}
if (curLine.IsEmpty()) {
// Blank line or eof, exit
return;
}
MOZ_ASSERT(mState != BMSG_PARSING_STATE_INVALID);
// Parse bMessage
(this->*(ParserActions[mState]))(curLine);
}
}
void
BluetoothMapBMessage::ParseBeginBmsg(const nsAutoCString& aCurrLine)
{
if (aCurrLine.EqualsLiteral("BEGIN:BMSG")) {
mState = BMSG_PARSING_STATE_VERSION;
}
}
void
BluetoothMapBMessage::ParseVersion(const nsAutoCString& aCurrLine)
{
nsCString version;
if (ExtractParameter(aCurrLine, "VERSION:", version)) {
/* The value for this property shall be "VERSION:1.0",
* based on Message Access Profile 3.1.3.
*/
if (version.EqualsLiteral("1.0")) {
mState = BMSG_PARSING_STATE_STATUS;
}
}
}
void
BluetoothMapBMessage::ParseStatus(const nsAutoCString& aCurrLine)
{
nsCString status;
if (ExtractParameter(aCurrLine, "STATUS:", status)) {
// only READ or UNREAD status
mReadStatus = status.EqualsLiteral("READ");
mState = BMSG_PARSING_STATE_TYPE;
}
}
void
BluetoothMapBMessage::ParseType(const nsAutoCString& aCurrLine)
{
nsCString type;
if (ExtractParameter(aCurrLine, "TYPE:", type)) {
mType = type;
mState = BMSG_PARSING_STATE_FOLDER;
}
}
void
BluetoothMapBMessage::ParseFolder(const nsAutoCString& aCurrLine)
{
nsCString folder;
if (ExtractParameter(aCurrLine, "FOLDER:", folder)) {
mFolderName = folder;
}
mState = BMSG_PARSING_STATE_ORIGINATOR;
}
void
BluetoothMapBMessage::ParseOriginator(const nsAutoCString& aCurrLine)
{
if (aCurrLine.EqualsLiteral("BEGIN:VCARD")) {
mOriginators.AppendElement(new VCard());
// We may parse vCard multiple times
mUnwindState = mState;
mState = BMSG_PARSING_STATE_VCARD;
} else if (aCurrLine.EqualsLiteral("BEGIN:BENV")) {
mState = BMSG_PARSING_STATE_BEGIN_ENVELOPE;
}
}
void
BluetoothMapBMessage::ParseVCard(const nsAutoCString& aCurrLine)
{
if (aCurrLine.EqualsLiteral("END:VCARD")) {
mState = mUnwindState;
return;
}
if (mUnwindState == BMSG_PARSING_STATE_ORIGINATOR &&
!mOriginators.IsEmpty()) {
mOriginators.LastElement()->Parse(aCurrLine);
} else if (mUnwindState == BMSG_PARSING_STATE_RECIPIENT &&
!mRecipients.IsEmpty()) {
mRecipients.LastElement()->Parse(aCurrLine);
}
}
void
BluetoothMapBMessage::ParseEnvelope(const nsAutoCString& aCurrLine)
{
if(!aCurrLine.EqualsLiteral("BEGIN:VCARD") &&
!aCurrLine.EqualsLiteral("BEGIN:BENV")) {
mState = BMSG_PARSING_STATE_BEGIN_BODY;
return;
}
if (aCurrLine.EqualsLiteral("BEGIN:BENV")) {
// nested BENV
// TODO: Check nested BENV envelope level for Email use case
++mEnvelopeLevel;
}
mRecipients.AppendElement(new VCard());
mState = BMSG_PARSING_STATE_VCARD;
// In case there are many recipients
mUnwindState = BMSG_PARSING_STATE_RECIPIENT;
}
void
BluetoothMapBMessage::ParseRecipient(const nsAutoCString& aCurrLine)
{
/* After parsing vCard, check whether it's bMessage BODY or VCARD.
* bmessage-recipient may appear more than once.
*/
if (aCurrLine.EqualsLiteral("BEGIN:BBODY")) {
mState = BMSG_PARSING_STATE_BEGIN_BODY;
} else if (aCurrLine.EqualsLiteral("BEGIN:VCARD")) {
mState = BMSG_PARSING_STATE_VCARD;
}
}
/* |ParseBody| parses based on the following BNF format:
* <bmessage-body-property>::=[<bmessage-body-encoding-property>]
* [<bmessage-body-charset-property>]
* [<bmessage-body-language-property>]
* <bmessage-body-content-length-property>
*
* <bmessage-content>::= {
* "BEGIN:BBODY"<CRLF>
* [<bmessage-body-part-ID> <CRLF>]
* <bmessage-body-property>
* <bmessage-body-content>* <CRLF>
* "END:BBODY"<CRLF>
* }
*/
void
BluetoothMapBMessage::ParseBody(const nsAutoCString& aCurrLine)
{
nsCString param;
if (ExtractParameter(aCurrLine, "PARTID::", param)) {
nsresult rv;
mPartId = param.ToInteger(&rv);
if (NS_FAILED(rv)) {
BT_LOGR("Failed to convert PARTID, error: 0x%x",
static_cast<uint32_t>(rv));
}
} else if (ExtractParameter(aCurrLine, "ENCODING:", param)) {
mEncoding = param;
} else if (ExtractParameter(aCurrLine, "CHARSET:", param)) {
mCharset = param;
} else if (ExtractParameter(aCurrLine, "LANGUAGE:", param)) {
mLanguage = param;
} else if (ExtractParameter(aCurrLine, "LENGTH:", param)) {
mBMsgLength = param;
} else if (aCurrLine.EqualsLiteral("BEGIN:MSG")) {
// Parse <bmessage-body-content>
mState = BMSG_PARSING_STATE_BEGIN_MSG;
}
}
/* |ParseBMsg| parses based on the following BNF format:
* <bmessage-body-content>::={
* "BEGIN:MSG"<CRLF>
* 'message'<CRLF>
* "END:MSG"<CRLF>
* }
*/
void
BluetoothMapBMessage::ParseBMsg(const nsAutoCString& aCurrLine)
{
/* TODO: Support binary message content
* For SMS: currently only UTF-8 is supported for textual content.
*/
if (aCurrLine.EqualsLiteral("END:MSG") ||
aCurrLine.EqualsLiteral("BEGIN:MSG")) {
/* Set state to STATE_BEGIN_MSG due to <bmessage-body-content> may appear
* many times.
*/
mState = BMSG_PARSING_STATE_BEGIN_MSG;
return;
}
mBMsgBody += aCurrLine.get();
// restore <CRLF>
mBMsgBody.AppendLiteral(kMapCrlf);
}
void
BluetoothMapBMessage::GetRecipients(nsTArray<RefPtr<VCard>>& aRecipients)
{
aRecipients = mRecipients;
}
void
BluetoothMapBMessage::GetOriginators(nsTArray<RefPtr<VCard>>& aOriginators)
{
aOriginators = mOriginators;
}
void
BluetoothMapBMessage::GetBody(nsACString& aBody)
{
aBody = mBMsgBody;
}
void
BluetoothMapBMessage::Dump()
{
BT_LOGR("Dump: message body %s", mBMsgBody.get());
BT_LOGR("Dump: read status %s", mReadStatus? "true" : "false");
BT_LOGR("Dump: folder %s", mFolderName.get());
BT_LOGR("Dump encoding %s", mEncoding.get());
BT_LOGR("Dump charset: %s" , mCharset.get());
BT_LOGR("Dump language: %s", mLanguage.get());
BT_LOGR("Dump length: %s", mBMsgLength.get());
BT_LOGR("Dump type: %s ", mType.get());
}
VCard::VCard()
{}
VCard::~VCard()
{}
void
VCard::Parse(const nsAutoCString& aCurrLine)
{
nsCString param;
if (ExtractParameter(aCurrLine, "N:", param)) {
mName = param;
} else if (ExtractParameter(aCurrLine, "FN:", param)) {
mFormattedName = param;
} else if (ExtractParameter(aCurrLine, "TEL:", param)) {
mTelephone = param;
} else if (ExtractParameter(aCurrLine, "Email:", param)) {
mEmail = param;
}
}
void
VCard::GetTelephone(nsACString& aTelephone)
{
aTelephone = mTelephone;
}
void
VCard::GetName(nsACString& aName)
{
aName = mName;
}
void
VCard::GetFormattedName(nsACString& aFormattedName)
{
aFormattedName = mFormattedName;
}
void
VCard::GetEmail(nsACString& aEmail)
{
aEmail = mEmail;
}
void
VCard::Dump()
{
BT_LOGR("Dump: Name %s", mName.get());
BT_LOGR("Dump: FormattedName %s", mFormattedName.get());
BT_LOGR("Dump: Telephone %s", mTelephone.get());
BT_LOGR("Dump: Email %s", mEmail.get());
}
END_BLUETOOTH_NAMESPACE