• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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         .observer = OHOS::iface_cast<IBluetoothServerSocketObserver>(data.ReadRemoteObject())
121     };
122 
123     int fd = -1;
124     int ret = Listen(param, fd);
125     if (!reply.WriteInt32(ret)) {
126         HILOGE("reply writing failed.");
127         return ERR_INVALID_VALUE;
128     }
129     if (ret == NO_ERROR) {
130         if (!reply.WriteFileDescriptor(fd)) {
131             HILOGE("reply writing failed");
132             return ERR_INVALID_VALUE;
133         }
134     }
135     return NO_ERROR;
136 }
137 
DeregisterServerObserverInner(MessageParcel & data,MessageParcel & reply)138 ErrCode BluetoothSocketStub::DeregisterServerObserverInner(MessageParcel &data, MessageParcel &reply)
139 {
140     HILOGI("DeregisterServerObserverInner starts");
141     sptr<IBluetoothServerSocketObserver> observer =
142         OHOS::iface_cast<IBluetoothServerSocketObserver>(data.ReadRemoteObject());
143     DeregisterServerObserver(observer);
144 
145     return NO_ERROR;
146 }
147 
RegisterClientObserverInner(MessageParcel & data,MessageParcel & reply)148 ErrCode BluetoothSocketStub::RegisterClientObserverInner(MessageParcel &data, MessageParcel &reply)
149 {
150     return reply.WriteInt32(BT_NO_ERROR);
151 }
152 
DeregisterClientObserverInner(MessageParcel & data,MessageParcel & reply)153 ErrCode BluetoothSocketStub::DeregisterClientObserverInner(MessageParcel &data, MessageParcel &reply)
154 {
155     return reply.WriteInt32(BT_NO_ERROR);
156 }
157 
UpdateCocConnectionParamsInner(MessageParcel & data,MessageParcel & reply)158 ErrCode BluetoothSocketStub::UpdateCocConnectionParamsInner(MessageParcel &data, MessageParcel &reply)
159 {
160     return reply.WriteInt32(BT_ERR_API_NOT_SUPPORT);
161 }
162 
IsAllowSocketConnectInner(MessageParcel & data,MessageParcel & reply)163 ErrCode BluetoothSocketStub::IsAllowSocketConnectInner(MessageParcel &data, MessageParcel &reply)
164 {
165     reply.WriteInt32(BT_NO_ERROR);
166     return reply.WriteBool(true);
167 }
168 }  // namespace Bluetooth
169 }  // namespace OHOS