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 }
41 
~BluetoothSocketStub()42 BluetoothSocketStub::~BluetoothSocketStub()
43 {
44     HILOGD("%{public}s start.", __func__);
45     memberFuncMap_.clear();
46 }
47 
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)48 int32_t BluetoothSocketStub::OnRemoteRequest(
49     uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
50 {
51     HILOGD("BluetoothSocketStub::OnRemoteRequest, cmd = %{public}d, flags= %{public}d", code, option.GetFlags());
52 
53     std::u16string descriptor = BluetoothSocketStub::GetDescriptor();
54     std::u16string remoteDescriptor = data.ReadInterfaceToken();
55     if (descriptor != remoteDescriptor) {
56         HILOGE("BluetoothSocketStub::OnRemoteRequest, local descriptor is not equal to remote");
57         return ERR_INVALID_STATE;
58     }
59 
60     auto itFunc = memberFuncMap_.find(code);
61     if (itFunc != memberFuncMap_.end()) {
62         auto memberFunc = itFunc->second;
63         if (memberFunc != nullptr) {
64             return (this->*memberFunc)(data, reply);
65         }
66     }
67 
68     HILOGW("BluetoothHostObserverStub::OnRemoteRequest, default case, need check.");
69     return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
70 }
71 
ConnectInner(MessageParcel & data,MessageParcel & reply)72 ErrCode BluetoothSocketStub::ConnectInner(MessageParcel &data, MessageParcel &reply)
73 {
74     HILOGI("ConnectInner starts");
75     std::string addr = data.ReadString();
76     std::shared_ptr<BluetoothUuid> uuid(data.ReadParcelable<BluetoothUuid>());
77     if (uuid == nullptr) {
78         HILOGE("reply writing failed in: %{public}s.", __func__);
79         return ERR_INVALID_VALUE;
80     }
81     ConnectSocketParam param {
82         .addr = addr,
83         .uuid = *uuid,
84         .securityFlag = data.ReadInt32(),
85         .type = data.ReadInt32(),
86         .psm = data.ReadInt32()
87     };
88     int fd = -1;
89     int ret = Connect(param, fd);
90     if (!reply.WriteInt32(ret)) {
91         HILOGE("reply writing failed.");
92         return ERR_INVALID_VALUE;
93     }
94 
95     if (ret == NO_ERROR) {
96         if (!reply.WriteFileDescriptor(fd)) {
97             HILOGE("reply writing failed");
98             return ERR_INVALID_VALUE;
99         }
100     }
101     return NO_ERROR;
102 }
103 
ListenInner(MessageParcel & data,MessageParcel & reply)104 ErrCode BluetoothSocketStub::ListenInner(MessageParcel &data, MessageParcel &reply)
105 {
106     HILOGI("ListenInner starts");
107     std::string name = data.ReadString();
108     std::shared_ptr<BluetoothUuid> uuid(data.ReadParcelable<BluetoothUuid>());
109     if (uuid == nullptr) {
110         HILOGE("reply writing failed in: %{public}s.", __func__);
111         return ERR_INVALID_VALUE;
112     }
113     ListenSocketParam param {
114         .name = name,
115         .uuid = *uuid,
116         .securityFlag = data.ReadInt32(),
117         .type = data.ReadInt32(),
118         .observer = OHOS::iface_cast<IBluetoothServerSocketObserver>(data.ReadRemoteObject())
119     };
120 
121     int fd = -1;
122     int ret = Listen(param, fd);
123     if (!reply.WriteInt32(ret)) {
124         HILOGE("reply writing failed.");
125         return ERR_INVALID_VALUE;
126     }
127     if (ret == NO_ERROR) {
128         if (!reply.WriteFileDescriptor(fd)) {
129             HILOGE("reply writing failed");
130             return ERR_INVALID_VALUE;
131         }
132     }
133     return NO_ERROR;
134 }
135 
DeregisterServerObserverInner(MessageParcel & data,MessageParcel & reply)136 ErrCode BluetoothSocketStub::DeregisterServerObserverInner(MessageParcel &data, MessageParcel &reply)
137 {
138     HILOGI("DeregisterServerObserverInner starts");
139     sptr<IBluetoothServerSocketObserver> observer =
140         OHOS::iface_cast<IBluetoothServerSocketObserver>(data.ReadRemoteObject());
141     DeregisterServerObserver(observer);
142 
143     return NO_ERROR;
144 }
145 
RegisterClientObserverInner(MessageParcel & data,MessageParcel & reply)146 ErrCode BluetoothSocketStub::RegisterClientObserverInner(MessageParcel &data, MessageParcel &reply)
147 {
148     return reply.WriteInt32(BT_NO_ERROR);
149 }
150 
DeregisterClientObserverInner(MessageParcel & data,MessageParcel & reply)151 ErrCode BluetoothSocketStub::DeregisterClientObserverInner(MessageParcel &data, MessageParcel &reply)
152 {
153     return reply.WriteInt32(BT_NO_ERROR);
154 }
155 
UpdateCocConnectionParamsInner(MessageParcel & data,MessageParcel & reply)156 ErrCode BluetoothSocketStub::UpdateCocConnectionParamsInner(MessageParcel &data, MessageParcel &reply)
157 {
158     return reply.WriteInt32(BT_ERR_API_NOT_SUPPORT);
159 }
160 }  // namespace Bluetooth
161 }  // namespace OHOS