Files
palemoon27/ipc/hal/DaemonSocketPDU.cpp
T
roytam1 a312918fb4 import changes from `dev' branch of rmottola/Arctic-Fox:
- Bug 1143925 - Avoid crash for HFP/A2DP manager during restart daemon. r=tzimmermann (5cd0d4b4db)
- Bug 1195608 - [cleanup] Fix too long lines and revise some comments under dom/bluetooth, r=joliu (44db9edaf8)
- Bug 1181512 - Use a single GATT interface, result handler, and notification handler for both GATT client and server operations in bluetooth daemon backend as bluez ipc protocol. r=tzimmermann (d5c1ca04f4)
- Bug 1168266 - Add few member functions of ObexHeaderSet for PBAP. r=shuang (113ef68a3f)
- Bug 1166587 - Check OBEX packet length before accessing it, r=shuang (dc5ca707fa)
- Bug 1180554 - Dispatch events to PBAP event handlers when the PBAP requests comes. r=btian, r=mrbkap (449900a161)
- Bug 1180555 - Handle PBAP replies and pass the results through IPC to PbapManager. r=btian (976b4130f0)
- Bug 1181480 - Add and implement GATT server connection related Web APIs. r=btian, r=mrbkap (996528346d)
- Bug 1195729: Fix ref-counting of Bluetooth result handlers for failed send operations, r=brsun (b40f14f2c5)
- Bug 1197877: Remove |CONVERT| macro from Bluetooth, r=btian (7c3cd72556)
- Bug 1197877: Remove |INIT_AT| macro from Bluetooth, r=btian (fb1b03e313)
- Bug 1187904: Add |DaemonSocketConnector|, r=shuang (8473e149fe)
- Bug 1203092: Add |DaemonSocketResultHandler| base class to HAL IPC, r=shuang (41210e6f1f)
- Bug 1175067 - Switch Bluetooth marionette test set to BT API v2 and disable unsupported tests. r=joliu (307eb1473c)
- Bug 1193379 - Move ipc and tests folders out from dom/bluetooth/bluetooth2. r=joliu (38820c505c)
- Bug 1155063 - Part 1: CallStateChanged accepts an array (idl). r=hsinyi (3ae1af7a7f)
- Bug 1155063 - Part 2: CallStateChanged accepts an array (telephony dom). r=hsinyi (10063343dc)
- Bug 1155063 - Part 3: CallStateChanged accepts an array (bluetooth dom). r=btian (20ef2893b4)
- Bug 1155063 - Part 4: CallStateChanged accepts an array (ipc). r=hsinyi (790c583a05)
- Bug 1155063 - Part 5: CallStateChanged accepts an array (ril). r=hsinyi (960ca6cd0a)
- Bug 1193379 - Rename bluetooth2 folder to common. r=joliu (58e992c298)
- Bug 1193379 - Move backend-neutral files into dom/bluetooth/common. r=joliu (f5769a803b)
- Bug 1203092: Inherit Bluetooth result handlers from |DaemonSocketResultHandler|, r=shuang (cb874fc706)
- Bug 1203092: Remove void pointers from Bluetooth's daemon backend, r=shuang (c78a0507c9)
- Bug 1204801: Move |BluetoothDaemonSetupModule| into separate compilation unit, r=btian (a1ae28c948)
- Bug 1204801: Use enum constants for Bluetooth Service module, r=btian (876e02a69a)
- Bug 1204801: Move |BlutoothSetupResultHandler| to generic backend interface, r=btian (9f12ca6d21)
- Bug 1199110: Add |BluetoothAvrcpManager| to Bluedroid and BlueZ backends, r=shuang (7a897a3436)
- Bug 1205253: Move |BluetoothDaemonCoreModule| into separate compilation unit, r=btian (d443455b07)
- Bug 1205253: Use enum constants for Bluetooth Core module, r=btian (2c69493ea8)
- Bug 1204506: Set 'override' attribute for overloaded Bluetooth backend methods, r=btian (211bded054)
- Bug 825149 - Add NREC feature bit in bluetooth stack. r=shuang (330103a1c0)
- Bug 825149 - Support HFP NREC feature. r=shuang (25e988596c)
- Bug 1192695 - Use pref instead of pre-defined C string as origin of bluetooth app. r=btian (06531c82d2)
- Bug 1195604 - [cleanup] Integrate similar BluetoothResultHandlers into basic one, r=tzimmermann (b87aeaa8c7)
- Bug 1199110: Use mPlayStatus to determine track selection, r=shuang (99f9eebb90)
- Bug 1142417 - Truncate AVRCP meta data strings followed by BTRC_MAX_ATTR_STR_LEN. r=tzimmermann (2f586f70e8)
- Bug 1142132: Move helper classes of A2DP manager into manager's namespace, r=shuang (12af5f6506)
- Bug 1159267: Share Bluedroid's A2DP manager between Bluetooth v1 and v2, r=btian (caa7c3dc56)
- missing part of Bug 1132229: Survive crashes of bluetoothd, r=shuang (708a6351be)
- Bug 1199110: Remove AVRCP support from |BluetoothA2dpManager| and convert callers, r=shuang (284e2804f8)
- CLOSED TREE Bug 1180556 - Pack PBAP replies to OBEX response message and reply to remote device. r=btian (7b02ab708d)
- Bug 1199107 - Access PBAP vCard input stream directly instead of passing it as input parameter, r=shuang (a0fd942bc0)
-  Bug 1199107 - Replace BT_WARNING with BT_LOGR in PBAP manager, r=shuang (ccd44ba040)
- Bug 1199548 - Do not append vCard Body payload in PBAP replies when |MaxListCount| is zero, r=shuang (2c1b8d2a52)
- Bug 1200124 - Correct PBAP order mapping, r=shuang, r=mrbkap (f988c2276e)
- Bug 1193379 - Create dom/bluetooth/common/webapi folder. r=joliu (889546481e)
- Bug 1200124 - Pass order and search key with uint32_t instead of nsString to avoid string mismatch error, r=shuang (caf33a4d28)
- Bug 1195685 - Transform macro |BT_APPEND_NAMED_VALUE| into function, r=shuang (6879f5624d)
- Bug 1141616: Implement |BluetoothService::GetServiceChannel|, r=btian (98b9dd3821)
- Bug 1206686: Use |BluetoothUuid| throughout Bluetooth backend interfaces, r=btian (cd4db645e0)
- Bug 1205577: Use enum constants for Bluetooth Socket module, r=btian (53f3aedd33)
- Bug 1127701 - Fix warning: NS_ENSURE_TRUE(value.isNumber()) failed, r=joliu (e83dc53c8c)
- Bug 1203046 - Call |NRECNotification| once RFCOMM is connected. r=shuang (53cc91861e)
- Bug 1166645 - Implement MAP profile manager connection related function, r=btian (e47ba31157)
- Bug 1203023 - Do not call Reset() for BluetoothMapSmsManager, r=tzimmermann (22bb74f972)
- fix misspatches (96dced2ce7)
- Bug 1141616: Support SDP lookups in |BluetoothServiceBluedroid|, r=btian (09dd2e1a69)
- Bug 1203819 - Fix loop condition in BluetoothPbapManager::PackPropertiesMask, r=shuang (c4fdb58f20)
- Bug 1159267: Share Bluedroid's OPP manager between Bluetooth v1 and v2, r=btian (e72a3dd65b)
- Bug 1141616: Lookup service channel in Bluedroid's OPP manager, r=btian (17b3cc0a65)
- Bug 1166176 - [cleanup] Simplify volume related code in OPP manager, r=shuang (4730023336)
- Bug 1191715 - Store local pointer of thread to shutdwon to avoid reentry crash, f=jesup, r=shuang (7d97509067)
- Bug 1055434 - Do not inform gaia of current blob's sending failure before assigning it, r=shuang (66f268fda5)
- missing bit of Bug 1164425 (59c5844590)
- namespace (5e253af0c7)
2022-03-22 11:49:40 +08:00

194 lines
4.6 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 "DaemonSocketPDU.h"
#include "mozilla/ipc/DaemonSocketConsumer.h"
#include "nsISupportsImpl.h" // for MOZ_COUNT_CTOR, MOZ_COUNT_DTOR
#ifdef CHROMIUM_LOG
#undef CHROMIUM_LOG
#endif
#if defined(MOZ_WIDGET_GONK)
#include <android/log.h>
#define CHROMIUM_LOG(args...) __android_log_print(ANDROID_LOG_INFO, "I/O", args);
#else
#include <stdio.h>
#define IODEBUG true
#define CHROMIUM_LOG(args...) if (IODEBUG) printf(args);
#endif
namespace mozilla {
namespace ipc {
//
// DaemonSocketPDU
//
DaemonSocketPDU::DaemonSocketPDU(uint8_t aService, uint8_t aOpcode,
uint16_t aPayloadSize)
: mConsumer(nullptr)
{
MOZ_COUNT_CTOR_INHERITED(DaemonSocketPDU, UnixSocketIOBuffer);
// Allocate memory
size_t availableSpace = HEADER_SIZE + aPayloadSize;
ResetBuffer(new uint8_t[availableSpace], 0, 0, availableSpace);
// Reserve PDU header
uint8_t* data = Append(HEADER_SIZE);
MOZ_ASSERT(data);
// Setup PDU header
data[OFF_SERVICE] = aService;
data[OFF_OPCODE] = aOpcode;
memcpy(data + OFF_LENGTH, &aPayloadSize, sizeof(aPayloadSize));
}
DaemonSocketPDU::DaemonSocketPDU(size_t aPayloadSize)
: mConsumer(nullptr)
{
MOZ_COUNT_CTOR_INHERITED(DaemonSocketPDU, UnixSocketIOBuffer);
size_t availableSpace = HEADER_SIZE + aPayloadSize;
ResetBuffer(new uint8_t[availableSpace], 0, 0, availableSpace);
}
DaemonSocketPDU::~DaemonSocketPDU()
{
MOZ_COUNT_DTOR_INHERITED(DaemonSocketPDU, UnixSocketIOBuffer);
nsAutoArrayPtr<uint8_t> data(GetBuffer());
ResetBuffer(nullptr, 0, 0, 0);
}
void
DaemonSocketPDU::GetHeader(uint8_t& aService, uint8_t& aOpcode,
uint16_t& aPayloadSize)
{
memcpy(&aService, GetData(OFF_SERVICE), sizeof(aService));
memcpy(&aOpcode, GetData(OFF_OPCODE), sizeof(aOpcode));
memcpy(&aPayloadSize, GetData(OFF_LENGTH), sizeof(aPayloadSize));
}
ssize_t
DaemonSocketPDU::Send(int aFd)
{
struct iovec iv;
memset(&iv, 0, sizeof(iv));
iv.iov_base = GetData(GetLeadingSpace());
iv.iov_len = GetSize();
struct msghdr msg;
memset(&msg, 0, sizeof(msg));
msg.msg_iov = &iv;
msg.msg_iovlen = 1;
msg.msg_control = nullptr;
msg.msg_controllen = 0;
ssize_t res = TEMP_FAILURE_RETRY(sendmsg(aFd, &msg, 0));
if (res < 0) {
MOZ_ASSERT(errno != EBADF); /* internal error */
OnError("sendmsg", errno);
return -1;
}
Consume(res);
if (mConsumer) {
// We successfully sent a PDU, now store the
// result handler in the consumer.
mConsumer->StoreResultHandler(*this);
}
return res;
}
#define CMSGHDR_CONTAINS_FD(_cmsghdr) \
( ((_cmsghdr)->cmsg_level == SOL_SOCKET) && \
((_cmsghdr)->cmsg_type == SCM_RIGHTS) )
ssize_t
DaemonSocketPDU::Receive(int aFd)
{
struct iovec iv;
memset(&iv, 0, sizeof(iv));
iv.iov_base = GetData(0);
iv.iov_len = GetAvailableSpace();
uint8_t cmsgbuf[CMSG_SPACE(sizeof(int))];
struct msghdr msg;
memset(&msg, 0, sizeof(msg));
msg.msg_iov = &iv;
msg.msg_iovlen = 1;
msg.msg_control = cmsgbuf;
msg.msg_controllen = sizeof(cmsgbuf);
ssize_t res = TEMP_FAILURE_RETRY(recvmsg(aFd, &msg, MSG_NOSIGNAL));
if (res < 0) {
MOZ_ASSERT(errno != EBADF); /* internal error */
OnError("recvmsg", errno);
return -1;
}
if (msg.msg_flags & (MSG_CTRUNC | MSG_OOB | MSG_ERRQUEUE)) {
return -1;
}
SetRange(0, res);
struct cmsghdr *chdr = CMSG_FIRSTHDR(&msg);
for (; chdr; chdr = CMSG_NXTHDR(&msg, chdr)) {
if (NS_WARN_IF(!CMSGHDR_CONTAINS_FD(chdr))) {
continue;
}
// Retrieve sent file descriptor. If multiple file descriptors
// have been sent, we close all but the final one.
mReceivedFd = *(static_cast<int*>(CMSG_DATA(chdr)));
}
return res;
}
int
DaemonSocketPDU::AcquireFd()
{
return mReceivedFd.forget();
}
nsresult
DaemonSocketPDU::UpdateHeader()
{
size_t len = GetPayloadSize();
if (len >= MAX_PAYLOAD_LENGTH) {
return NS_ERROR_ILLEGAL_VALUE;
}
uint16_t len16 = static_cast<uint16_t>(len);
memcpy(GetData(OFF_LENGTH), &len16, sizeof(len16));
return NS_OK;
}
size_t
DaemonSocketPDU::GetPayloadSize() const
{
MOZ_ASSERT(GetSize() >= HEADER_SIZE);
return GetSize() - HEADER_SIZE;
}
void
DaemonSocketPDU::OnError(const char* aFunction, int aErrno)
{
CHROMIUM_LOG("%s failed with error %d (%s)",
aFunction, aErrno, strerror(aErrno));
}
}
}