• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021-2022 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_socket_stub.h"
17 #include "bluetooth_bt_uuid.h"
18 #include "bluetooth_log.h"
19 #include "ipc_types.h"
20 
21 namespace OHOS {
22 namespace Bluetooth {
BluetoothSocketStub()23 BluetoothSocketStub::BluetoothSocketStub()
24 {
25     HILOGD("%{public}s start.", __func__);
26     memberFuncMap_[static_cast<uint32_t>(BluetoothSocketInterfaceCode::SOCKET_CONNECT)] =
27         &BluetoothSocketStub::ConnectInner;
28     memberFuncMap_[static_cast<uint32_t>(BluetoothSocketInterfaceCode::SOCKET_LISTEN)] =
29         &BluetoothSocketStub::ListenInner;
30 }
31 
~BluetoothSocketStub()32 BluetoothSocketStub::~BluetoothSocketStub()
33 {
34     HILOGD("%{public}s start.", __func__);
35     memberFuncMap_.clear();
36 }
37 
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)38 int32_t BluetoothSocketStub::OnRemoteRequest(
39     uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
40 {
41     HILOGD("BluetoothSocketStub::OnRemoteRequest, cmd = %{public}d, flags= %{public}d", code, option.GetFlags());
42 
43     std::u16string descriptor = BluetoothSocketStub::GetDescriptor();
44     std::u16string remoteDescriptor = data.ReadInterfaceToken();
45     if (descriptor != remoteDescriptor) {
46         HILOGE("BluetoothSocketStub::OnRemoteRequest, local descriptor is not equal to remote");
47         return ERR_INVALID_STATE;
48     }
49 
50     auto itFunc = memberFuncMap_.find(code);
51     if (itFunc != memberFuncMap_.end()) {
52         auto memberFunc = itFunc->second;
53         if (memberFunc != nullptr) {
54             return (this->*memberFunc)(data, reply);
55         }
56     }
57 
58     HILOGW("BluetoothHostObserverStub::OnRemoteRequest, default case, need check.");
59     return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
60 }
61 
ConnectInner(MessageParcel & data,MessageParcel & reply)62 ErrCode BluetoothSocketStub::ConnectInner(MessageParcel &data, MessageParcel &reply)
63 {
64     HILOGI("ConnectInner starts");
65     std::string addr = data.ReadString();
66     std::shared_ptr<BluetoothUuid> uuid(data.ReadParcelable<BluetoothUuid>());
67     if (uuid == nullptr) {
68         HILOGE("reply writing failed in: %{public}s.", __func__);
69         return ERR_INVALID_VALUE;
70     }
71     ConnectSocketParam param {
72         .addr = addr,
73         .uuid = *uuid,
74         .securityFlag = data.ReadInt32(),
75         .type = data.ReadInt32(),
76         .psm = data.ReadInt32()
77 
78     };
79     int fd = -1;
80     int ret = Connect(param, fd);
81     if (!reply.WriteInt32(ret)) {
82         HILOGE("reply writing failed.");
83         return ERR_INVALID_VALUE;
84     }
85 
86     if (ret == NO_ERROR) {
87         if (!reply.WriteFileDescriptor(fd)) {
88             HILOGE("reply writing failed");
89             return ERR_INVALID_VALUE;
90         }
91     }
92     return NO_ERROR;
93 }
94 
ListenInner(MessageParcel & data,MessageParcel & reply)95 ErrCode BluetoothSocketStub::ListenInner(MessageParcel &data, MessageParcel &reply)
96 {
97     HILOGI("ListenInner starts");
98     std::string name = data.ReadString();
99     std::shared_ptr<BluetoothUuid> uuid(data.ReadParcelable<BluetoothUuid>());
100     if (uuid == nullptr) {
101         HILOGE("reply writing failed in: %{public}s.", __func__);
102         return ERR_INVALID_VALUE;
103     }
104     ListenSocketParam param {
105         name,
106         *uuid,
107         data.ReadInt32(),
108         data.ReadInt32()
109     };
110 
111     int fd = -1;
112     int ret = Listen(param, fd);
113     if (!reply.WriteInt32(ret)) {
114         HILOGE("reply writing failed.");
115         return ERR_INVALID_VALUE;
116     }
117     if (ret == NO_ERROR) {
118         if (!reply.WriteFileDescriptor(fd)) {
119             HILOGE("reply writing failed");
120             return ERR_INVALID_VALUE;
121         }
122     }
123     return NO_ERROR;
124 }
125 }  // namespace Bluetooth
126 }  // namespace OHOS