1 /*
2 * Copyright (C) 2021-2023 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "bluetooth_bt_uuid.h"
17 #include "bluetooth_errorcode.h"
18 #include "bluetooth_log.h"
19 #include "bluetooth_socket_observer_proxy.h"
20 #include "ipc_types.h"
21 #include "bluetooth_socket_stub.h"
22
23 namespace OHOS {
24 namespace Bluetooth {
BluetoothSocketStub()25 BluetoothSocketStub::BluetoothSocketStub()
26 {
27 HILOGD("%{public}s start.", __func__);
28 memberFuncMap_[static_cast<uint32_t>(BluetoothSocketInterfaceCode::SOCKET_CONNECT)] =
29 &BluetoothSocketStub::ConnectInner;
30 memberFuncMap_[static_cast<uint32_t>(BluetoothSocketInterfaceCode::SOCKET_LISTEN)] =
31 &BluetoothSocketStub::ListenInner;
32 memberFuncMap_[static_cast<uint32_t>(BluetoothSocketInterfaceCode::DEREGISTER_SERVER_OBSERVER)] =
33 &BluetoothSocketStub::DeregisterServerObserverInner;
34 memberFuncMap_[static_cast<uint32_t>(BluetoothSocketInterfaceCode::REGISTER_CLIENT_OBSERVER)] =
35 &BluetoothSocketStub::RegisterClientObserverInner;
36 memberFuncMap_[static_cast<uint32_t>(BluetoothSocketInterfaceCode::DEREGISTER_SERVER_OBSERVER)] =
37 &BluetoothSocketStub::DeregisterClientObserverInner;
38 memberFuncMap_[static_cast<uint32_t>(BluetoothSocketInterfaceCode::SOCKET_UPDATE_COC_PARAMS)] =
39 &BluetoothSocketStub::UpdateCocConnectionParamsInner;
40 memberFuncMap_[static_cast<uint32_t>(BluetoothSocketInterfaceCode::SOCKET_IS_ALLOW_CONNECT)] =
41 &BluetoothSocketStub::IsAllowSocketConnectInner;
42 }
43
~BluetoothSocketStub()44 BluetoothSocketStub::~BluetoothSocketStub()
45 {
46 HILOGD("%{public}s start.", __func__);
47 memberFuncMap_.clear();
48 }
49
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)50 int32_t BluetoothSocketStub::OnRemoteRequest(
51 uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
52 {
53 HILOGD("BluetoothSocketStub::OnRemoteRequest, cmd = %{public}d, flags= %{public}d", code, option.GetFlags());
54
55 std::u16string descriptor = BluetoothSocketStub::GetDescriptor();
56 std::u16string remoteDescriptor = data.ReadInterfaceToken();
57 if (descriptor != remoteDescriptor) {
58 HILOGE("BluetoothSocketStub::OnRemoteRequest, local descriptor is not equal to remote");
59 return ERR_INVALID_STATE;
60 }
61
62 auto itFunc = memberFuncMap_.find(code);
63 if (itFunc != memberFuncMap_.end()) {
64 auto memberFunc = itFunc->second;
65 if (memberFunc != nullptr) {
66 return (this->*memberFunc)(data, reply);
67 }
68 }
69
70 HILOGW("BluetoothHostObserverStub::OnRemoteRequest, default case, need check.");
71 return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
72 }
73
ConnectInner(MessageParcel & data,MessageParcel & reply)74 ErrCode BluetoothSocketStub::ConnectInner(MessageParcel &data, MessageParcel &reply)
75 {
76 HILOGI("ConnectInner starts");
77 std::string addr = data.ReadString();
78 std::shared_ptr<BluetoothUuid> uuid(data.ReadParcelable<BluetoothUuid>());
79 if (uuid == nullptr) {
80 HILOGE("reply writing failed in: %{public}s.", __func__);
81 return ERR_INVALID_VALUE;
82 }
83 ConnectSocketParam param {
84 .addr = addr,
85 .uuid = *uuid,
86 .securityFlag = data.ReadInt32(),
87 .type = data.ReadInt32(),
88 .psm = data.ReadInt32()
89 };
90 int fd = -1;
91 int ret = Connect(param, fd);
92 if (!reply.WriteInt32(ret)) {
93 HILOGE("reply writing failed.");
94 return ERR_INVALID_VALUE;
95 }
96
97 if (ret == NO_ERROR) {
98 if (!reply.WriteFileDescriptor(fd)) {
99 HILOGE("reply writing failed");
100 return ERR_INVALID_VALUE;
101 }
102 }
103 return NO_ERROR;
104 }
105
ListenInner(MessageParcel & data,MessageParcel & reply)106 ErrCode BluetoothSocketStub::ListenInner(MessageParcel &data, MessageParcel &reply)
107 {
108 HILOGI("ListenInner starts");
109 std::string name = data.ReadString();
110 std::shared_ptr<BluetoothUuid> uuid(data.ReadParcelable<BluetoothUuid>());
111 if (uuid == nullptr) {
112 HILOGE("reply writing failed in: %{public}s.", __func__);
113 return ERR_INVALID_VALUE;
114 }
115 ListenSocketParam param {
116 .name = name,
117 .uuid = *uuid,
118 .securityFlag = data.ReadInt32(),
119 .type = data.ReadInt32(),
120 .psm = data.ReadInt32(),
121 .observer = OHOS::iface_cast<IBluetoothServerSocketObserver>(data.ReadRemoteObject())
122 };
123
124 int fd = -1;
125 int ret = Listen(param, fd);
126 if (!reply.WriteInt32(ret)) {
127 HILOGE("reply writing failed.");
128 return ERR_INVALID_VALUE;
129 }
130 if (ret == NO_ERROR) {
131 if (!reply.WriteFileDescriptor(fd)) {
132 HILOGE("reply writing failed");
133 return ERR_INVALID_VALUE;
134 }
135 }
136 return NO_ERROR;
137 }
138
DeregisterServerObserverInner(MessageParcel & data,MessageParcel & reply)139 ErrCode BluetoothSocketStub::DeregisterServerObserverInner(MessageParcel &data, MessageParcel &reply)
140 {
141 HILOGI("DeregisterServerObserverInner starts");
142 sptr<IBluetoothServerSocketObserver> observer =
143 OHOS::iface_cast<IBluetoothServerSocketObserver>(data.ReadRemoteObject());
144 DeregisterServerObserver(observer);
145
146 return NO_ERROR;
147 }
148
RegisterClientObserverInner(MessageParcel & data,MessageParcel & reply)149 ErrCode BluetoothSocketStub::RegisterClientObserverInner(MessageParcel &data, MessageParcel &reply)
150 {
151 return reply.WriteInt32(BT_NO_ERROR);
152 }
153
DeregisterClientObserverInner(MessageParcel & data,MessageParcel & reply)154 ErrCode BluetoothSocketStub::DeregisterClientObserverInner(MessageParcel &data, MessageParcel &reply)
155 {
156 return reply.WriteInt32(BT_NO_ERROR);
157 }
158
UpdateCocConnectionParamsInner(MessageParcel & data,MessageParcel & reply)159 ErrCode BluetoothSocketStub::UpdateCocConnectionParamsInner(MessageParcel &data, MessageParcel &reply)
160 {
161 return reply.WriteInt32(BT_ERR_API_NOT_SUPPORT);
162 }
163
IsAllowSocketConnectInner(MessageParcel & data,MessageParcel & reply)164 ErrCode BluetoothSocketStub::IsAllowSocketConnectInner(MessageParcel &data, MessageParcel &reply)
165 {
166 reply.WriteInt32(BT_NO_ERROR);
167 return reply.WriteBool(true);
168 }
169 } // namespace Bluetooth
170 } // namespace OHOS