* Copyright (C) 2021-2022 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "bluetooth_gatt_client_stub.h"
#include "bluetooth_log.h"
#include "bluetooth_errorcode.h"
#include "ipc_types.h"
#include "string_ex.h"
#include "permission_manager.h"
#ifdef STUB_FUNC
#undef STUB_FUNC
#endif
#define STUB_FUNC(code, func, perm) BluetoothGattClientInterfaceCode::code, {&BluetoothGattClientStub::func, perm}
namespace OHOS {
namespace Bluetooth {
using namespace OHOS::bluetooth;
const std::map<uint32_t, BluetoothGattClientStub::GattClientStubFuncPerm> BluetoothGattClientStub::memberFuncMap_ = {
{STUB_FUNC(BT_GATT_CLIENT_REGISTER_APP, RegisterApplicationInner,
CHECK_PERM(false, {USE_BLUETOOTH}, {ACCESS_BLUETOOTH}))},
{STUB_FUNC(BT_GATT_CLIENT_DEREGISTER_APP, DeregisterApplicationInner,
CHECK_PERM(false, {USE_BLUETOOTH}, {ACCESS_BLUETOOTH}))},
{STUB_FUNC(BT_GATT_CLIENT_CONNECT, ConnectInner,
CHECK_PERM(false, {USE_BLUETOOTH}, {ACCESS_BLUETOOTH}))},
{STUB_FUNC(BT_GATT_CLIENT_DIS_CONNECT, DisconnectInner,
CHECK_PERM(false, {USE_BLUETOOTH}, {ACCESS_BLUETOOTH}))},
{STUB_FUNC(BT_GATT_CLIENT_DISCOVERY_SERVICES, DiscoveryServicesInner,
CHECK_PERM(false, {USE_BLUETOOTH}, {ACCESS_BLUETOOTH}))},
{STUB_FUNC(BT_GATT_CLIENT_READ_CHARACTERISTIC, ReadCharacteristicInner,
CHECK_PERM(false, {USE_BLUETOOTH}, {ACCESS_BLUETOOTH}))},
{STUB_FUNC(BT_GATT_READ_CHARACTERISTIC_BY_UUID, ReadCharacteristicByUuidInner,
CHECK_PERM(false, {USE_BLUETOOTH}, {ACCESS_BLUETOOTH}))},
{STUB_FUNC(BT_GATT_CLIENT_WRITE_CHARACTERISTIC, WriteCharacteristicInner,
CHECK_PERM(false, {USE_BLUETOOTH}, {ACCESS_BLUETOOTH}))},
{STUB_FUNC(BT_GATT_CLIENT_SIGNED_WRITE_CHARACTERISTIC, SignedWriteCharacteristicInner,
CHECK_PERM(false, {USE_BLUETOOTH}, {ACCESS_BLUETOOTH}))},
{STUB_FUNC(BT_GATT_CLIENT_READ_DESCRIPTOR, ReadDescriptorInner,
CHECK_PERM(false, {USE_BLUETOOTH}, {ACCESS_BLUETOOTH}))},
{STUB_FUNC(BT_GATT_CLIENT_WRITE_DESCRIPTOR, WriteDescriptorInner,
CHECK_PERM(false, {USE_BLUETOOTH}, {ACCESS_BLUETOOTH}))},
{STUB_FUNC(BT_GATT_CLIENT_REQUEST_EXCHANGE_MTU, RequestExchangeMtuInner,
CHECK_PERM(false, {USE_BLUETOOTH}, {ACCESS_BLUETOOTH}))},
{STUB_FUNC(BT_GATT_CLIENT_GET_ALL_DEVICE, GetAllDeviceInner,
CHECK_PERM(false, {}, MULTI_PERM(ACCESS_BLUETOOTH, MANAGE_BLUETOOTH)))},
{STUB_FUNC(BT_GATT_CLIENT_REQUEST_CONNECTION_PRIORITY, RequestConnectionPriorityInner,
CHECK_PERM(false, {}, {ACCESS_BLUETOOTH}))},
{STUB_FUNC(BT_GATT_CLIENT_GET_SERVICES, GetServicesInner,
CHECK_PERM(false, {USE_BLUETOOTH}, {ACCESS_BLUETOOTH}))},
{STUB_FUNC(BT_GATT_CLIENT_REQUEST_FASTEST_CONNECTION, RequestFastestConnInner,
CHECK_PERM(false, {}, MULTI_PERM(ACCESS_BLUETOOTH, MANAGE_BLUETOOTH)))},
{STUB_FUNC(BT_GATT_CLIENT_READ_REMOTE_RSSI_VALUE, ReadRemoteRssiValueInner,
CHECK_PERM(false, {USE_BLUETOOTH}, {ACCESS_BLUETOOTH}))},
{STUB_FUNC(BT_GATT_CLIENT_REQUEST_NOTIFICATION, RequestNotificationInner,
CHECK_PERM(false, {USE_BLUETOOTH}, {ACCESS_BLUETOOTH}))},
{STUB_FUNC(BT_GATT_CLIENT_GET_CONNECTED_STATE, GetConnectedStateInner,
CHECK_PERM(false, {}, {ACCESS_BLUETOOTH}))},
{STUB_FUNC(BT_GATT_CLIENT_SET_PHY, SetPhyInner,
CHECK_PERM(false, {}, {ACCESS_BLUETOOTH}))},
{STUB_FUNC(BT_GATT_CLIENT_READ_PHY, ReadPhyInner,
CHECK_PERM(false, {}, {ACCESS_BLUETOOTH}))},
};
BluetoothGattClientStub::BluetoothGattClientStub()
{}
BluetoothGattClientStub::~BluetoothGattClientStub()
{}
int BluetoothGattClientStub::OnRemoteRequest(
uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
{
CHECK_PERMISSION_AND_EXECUTE_FUNC_RETURN(BluetoothGattClientStub);
}
int32_t BluetoothGattClientStub::RegisterApplicationInner(MessageParcel &data, MessageParcel &reply)
{
HILOGI("BluetoothGattClientStub::RegisterApplicationInner starts");
sptr<IRemoteObject> remote = data.ReadRemoteObject();
const sptr<IBluetoothGattClientCallback> callback = OHOS::iface_cast<IBluetoothGattClientCallback>(remote);
std::shared_ptr<BluetoothRawAddress> addr(data.ReadParcelable<BluetoothRawAddress>());
if (!addr) {
return TRANSACTION_ERR;
}
int32_t transport = data.ReadInt32();
int appId = 0;
int result = RegisterApplication(callback, *addr, transport, appId);
bool resultRet = reply.WriteInt32(result);
bool appIdRet = reply.WriteInt32(appId);
if (!(resultRet && appIdRet)) {
HILOGE("BluetoothGattClientStub: reply writing failed in: %{public}s.", __func__);
return ERR_INVALID_VALUE;
}
return NO_ERROR;
}
int32_t BluetoothGattClientStub::DeregisterApplicationInner(MessageParcel &data, MessageParcel &reply)
{
HILOGI("BluetoothGattClientStub::DeregisterApplicationInner starts");
int32_t appId = data.ReadInt32();
int result = DeregisterApplication(appId);
bool ret = reply.WriteInt32(result);
if (!ret) {
HILOGE("BluetoothGattClientStub: reply writing failed in: %{public}s.", __func__);
return ERR_INVALID_VALUE;
}
return NO_ERROR;
}
int32_t BluetoothGattClientStub::ConnectInner(MessageParcel &data, MessageParcel &reply)
{
HILOGI("BluetoothGattClientStub::ConnectInner starts");
int32_t appId = data.ReadInt32();
bool autoConnect = data.ReadBool();
int result = Connect(appId, autoConnect);
bool ret = reply.WriteInt32(result);
if (!ret) {
HILOGE("BluetoothGattClientStub: reply writing failed in: %{public}s.", __func__);
return ERR_INVALID_VALUE;
}
return NO_ERROR;
}
int32_t BluetoothGattClientStub::DisconnectInner(MessageParcel &data, MessageParcel &reply)
{
HILOGI("BluetoothGattClientStub::DisconnectInner starts");
int32_t appId = data.ReadInt32();
int result = Disconnect(appId);
bool ret = reply.WriteInt32(result);
if (!ret) {
HILOGE("BluetoothGattClientStub: reply writing failed in: %{public}s.", __func__);
return ERR_INVALID_VALUE;
}
return NO_ERROR;
}
int32_t BluetoothGattClientStub::DiscoveryServicesInner(MessageParcel &data, MessageParcel &reply)
{
HILOGI("BluetoothGattClientStub::DiscoveryServicesInner starts");
int32_t appId = data.ReadInt32();
int result = DiscoveryServices(appId);
bool ret = reply.WriteInt32(result);
if (!ret) {
HILOGE("BluetoothGattClientStub: reply writing failed in: %{public}s.", __func__);
return ERR_INVALID_VALUE;
}
return NO_ERROR;
}
int32_t BluetoothGattClientStub::ReadCharacteristicInner(MessageParcel &data, MessageParcel &reply)
{
HILOGI("BluetoothGattClientStub::ReadCharacteristicInner starts");
int32_t appId = data.ReadInt32();
std::shared_ptr<BluetoothGattCharacteristic> characteristic(data.ReadParcelable<BluetoothGattCharacteristic>());
if (!characteristic) {
return TRANSACTION_ERR;
}
int result = ReadCharacteristic(appId, *characteristic);
bool ret = reply.WriteInt32(result);
if (!ret) {
HILOGE("BluetoothGattClientStub: reply writing failed in: %{public}s.", __func__);
return ERR_INVALID_VALUE;
}
return NO_ERROR;
}
int32_t BluetoothGattClientStub::WriteCharacteristicInner(MessageParcel &data, MessageParcel &reply)
{
HILOGI("BluetoothGattClientStub::WriteCharacteristicInner starts");
int32_t appId = data.ReadInt32();
std::shared_ptr<BluetoothGattCharacteristic> characteristic(data.ReadParcelable<BluetoothGattCharacteristic>());
if (!characteristic) {
return TRANSACTION_ERR;
}
bool withoutRespond = data.ReadBool();
bool isWithContext = data.ReadBool();
int result = WriteCharacteristic(appId, characteristic.get(), withoutRespond, isWithContext);
bool ret = reply.WriteInt32(result);
if (!ret) {
HILOGE("BluetoothGattClientStub: reply writing failed in: %{public}s.", __func__);
return ERR_INVALID_VALUE;
}
return NO_ERROR;
}
int32_t BluetoothGattClientStub::SignedWriteCharacteristicInner(MessageParcel &data, MessageParcel &reply)
{
HILOGI("BluetoothGattClientStub::SignedWriteCharacteristicInner starts");
int32_t appId = data.ReadInt32();
std::shared_ptr<BluetoothGattCharacteristic> characteristic(data.ReadParcelable<BluetoothGattCharacteristic>());
if (!characteristic) {
return TRANSACTION_ERR;
}
int result = SignedWriteCharacteristic(appId, characteristic.get());
bool ret = reply.WriteInt32(result);
if (!ret) {
HILOGE("BluetoothGattClientStub: reply writing failed in: %{public}s.", __func__);
return ERR_INVALID_VALUE;
}
return NO_ERROR;
}
int32_t BluetoothGattClientStub::ReadDescriptorInner(MessageParcel &data, MessageParcel &reply)
{
HILOGI("BluetoothGattClientStub::ReadDescriptorInner starts");
int32_t appId = data.ReadInt32();
std::shared_ptr<BluetoothGattDescriptor> descriptor(data.ReadParcelable<BluetoothGattDescriptor>());
if (!descriptor) {
return TRANSACTION_ERR;
}
int result = ReadDescriptor(appId, *descriptor);
bool ret = reply.WriteInt32(result);
if (!ret) {
HILOGE("BluetoothGattClientStub: reply writing failed in: %{public}s.", __func__);
return ERR_INVALID_VALUE;
}
return NO_ERROR;
}
int32_t BluetoothGattClientStub::WriteDescriptorInner(MessageParcel &data, MessageParcel &reply)
{
HILOGI("BluetoothGattClientStub::WriteDescriptorInner starts");
int32_t appId = data.ReadInt32();
std::shared_ptr<BluetoothGattDescriptor> descriptor(data.ReadParcelable<BluetoothGattDescriptor>());
if (!descriptor) {
return TRANSACTION_ERR;
}
int result = WriteDescriptor(appId, descriptor.get());
HILOGI("appId=%{public}d, result=%{public}d", appId, result);
bool ret = reply.WriteInt32(result);
if (!ret) {
HILOGE("BluetoothGattClientStub: reply writing failed in: %{public}s.", __func__);
return ERR_INVALID_VALUE;
}
return NO_ERROR;
}
int32_t BluetoothGattClientStub::RequestExchangeMtuInner(MessageParcel &data, MessageParcel &reply)
{
HILOGI("BluetoothGattClientStub::RequestExchangeMtuInner starts");
int32_t appId = data.ReadInt32();
int32_t mtu = data.ReadInt32();
int result = RequestExchangeMtu(appId, mtu);
HILOGI("appId=%{public}d, mtu=%{public}d, result=%{public}d", appId, mtu, result);
bool ret = reply.WriteInt32(result);
if (!ret) {
HILOGE("BluetoothGattClientStub: reply writing failed in: %{public}s.", __func__);
return ERR_INVALID_VALUE;
}
return NO_ERROR;
}
int32_t BluetoothGattClientStub::GetAllDeviceInner(MessageParcel &data, MessageParcel &reply)
{
HILOGI("BluetoothGattClientStub::GetAllDeviceInner starts");
std::vector<BluetoothGattDevice> device;
GetAllDevice(device);
reply.WriteInt32(device.size());
int num = device.size();
for (int i = 0; i < num; i++) {
bool ret = reply.WriteParcelable(&device[i]);
if (!ret) {
HILOGE("WriteParcelable<GetAllDeviceInner> failed");
return ERR_INVALID_VALUE;
}
}
return NO_ERROR;
}
int32_t BluetoothGattClientStub::RequestConnectionPriorityInner(MessageParcel &data, MessageParcel &reply)
{
HILOGI("BluetoothGattClientStub::RequestConnectionPriorityInner starts");
int32_t appId = data.ReadInt32();
int32_t connPriority = data.ReadInt32();
int result = RequestConnectionPriority(appId, connPriority);
bool ret = reply.WriteInt32(result);
if (!ret) {
HILOGE("BluetoothGattClientStub: reply writing failed in: %{public}s.", __func__);
return ERR_INVALID_VALUE;
}
return NO_ERROR;
}
int32_t BluetoothGattClientStub::GetServicesInner(MessageParcel &data, MessageParcel &reply)
{
HILOGI("BluetoothGattClientStub::GetServicesInner starts");
int32_t appId = data.ReadInt32();
std::vector<BluetoothGattService> service;
int result = GetServices(appId, service);
bool resultRet = reply.WriteInt32(result);
bool sizeRet = reply.WriteInt32(service.size());
if (!(resultRet && sizeRet)) {
HILOGE("Write data fail");
return ERR_INVALID_VALUE;
}
int num = service.size();
for (int i = 0; i < num; i++) {
bool ret = reply.WriteParcelable(&service[i]);
if (!ret) {
HILOGE("WriteParcelable<GetServicesInner> failed");
return ERR_INVALID_VALUE;
}
}
return NO_ERROR;
}
int32_t BluetoothGattClientStub::RequestFastestConnInner(MessageParcel &data, MessageParcel &reply)
{
return NO_ERROR;
}
int32_t BluetoothGattClientStub::ReadRemoteRssiValueInner(MessageParcel &data, MessageParcel &reply)
{
return NO_ERROR;
}
int32_t BluetoothGattClientStub::RequestNotificationInner(MessageParcel &data, MessageParcel &reply)
{
return NO_ERROR;
}
int32_t BluetoothGattClientStub::GetConnectedStateInner(MessageParcel &data, MessageParcel &reply)
{
HILOGI("Not Support");
if (!reply.WriteInt32(BT_ERR_API_NOT_SUPPORT)) {
HILOGE("BluetoothA2dpSrcStub: WriteFrameInner reply writing failed in: %{public}s.", __func__);
return TRANSACTION_ERR;
}
return NO_ERROR;
}
int32_t BluetoothGattClientStub::SetPhyInner(MessageParcel &data, MessageParcel &reply)
{
HILOGI("Not Support");
if (!reply.WriteInt32(BT_ERR_API_NOT_SUPPORT)) {
HILOGE("BluetoothA2dpSrcStub: WriteFrameInner reply writing failed in: %{public}s.", __func__);
return TRANSACTION_ERR;
}
return NO_ERROR;
}
int32_t BluetoothGattClientStub::ReadPhyInner(MessageParcel &data, MessageParcel &reply)
{
HILOGI("Not Support");
if (!reply.WriteInt32(BT_ERR_API_NOT_SUPPORT)) {
HILOGE("BluetoothA2dpSrcStub: WriteFrameInner reply writing failed in: %{public}s.", __func__);
return TRANSACTION_ERR;
}
return NO_ERROR;
}
int32_t BluetoothGattClientStub::ReadCharacteristicByUuidInner(MessageParcel &data, MessageParcel &reply)
{
HILOGI("Not Support");
if (!reply.WriteInt32(BT_ERR_API_NOT_SUPPORT)) {
HILOGE("BluetoothGattClientStub: WriteFrameInner reply writing failed in: %{public}s.", __func__);
return TRANSACTION_ERR;
}
return NO_ERROR;
}
}
}