• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 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 #include "bluetooth_opp_stub.h"
16 #include "bluetooth_log.h"
17 
18 namespace OHOS {
19 namespace Bluetooth {
BluetoothOppStub()20 BluetoothOppStub::BluetoothOppStub()
21 {
22     HILOGD("%{public}s start.", __func__);
23     memberFuncMap_[static_cast<uint32_t>(COMMAND_SEND_FILE)] =
24         &BluetoothOppStub::SendFileInner;
25     memberFuncMap_[static_cast<uint32_t>(COMMAND_SET_INCOMING_FILE_CONFIRMATION)] =
26         &BluetoothOppStub::SetIncomingFileConfirmationInner;
27     memberFuncMap_[static_cast<uint32_t>(COMMAND_GET_CURRENT_TRANSFER_INFORMATION)] =
28         &BluetoothOppStub::GetCurrentTransferInformationInner;
29     memberFuncMap_[static_cast<uint32_t>(COMMAND_CANCEL_TRANSFER)] =
30         &BluetoothOppStub::CancelTransferInner;
31     memberFuncMap_[static_cast<uint32_t>(COMMAND_REGISTER_OBSERVER)] =
32         &BluetoothOppStub::RegisterObserverInner;
33     memberFuncMap_[static_cast<uint32_t>(COMMAND_DEREGISTER_OBSERVER)] =
34         &BluetoothOppStub::DeregisterObserverInner;
35     memberFuncMap_[static_cast<uint32_t>(COMMAND_GET_DEVICE_STATE)] =
36         &BluetoothOppStub::GetDeviceStateInner;
37     memberFuncMap_[static_cast<uint32_t>(COMMAND_GET_DEVICES_BY_STATES)] =
38         &BluetoothOppStub::GetDevicesByStatesInner;
39     HILOGI("ends.");
40 }
41 
~BluetoothOppStub()42 BluetoothOppStub::~BluetoothOppStub()
43 {
44     HILOGI("start.");
45     memberFuncMap_.clear();
46 }
47 
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)48 int BluetoothOppStub::OnRemoteRequest(
49     uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
50 {
51     HILOGI("BluetoothOppStub::OnRemoteRequest, cmd = %{public}d, flags= %{public}d", code, option.GetFlags());
52     std::u16string descriptor = BluetoothOppStub::GetDescriptor();
53     std::u16string remoteDescriptor = data.ReadInterfaceToken();
54     if (descriptor != remoteDescriptor) {
55         HILOGE("local descriptor is not equal to remote");
56         return IPC_INVOKER_TRANSLATE_ERR;
57     }
58     auto itFunc = memberFuncMap_.find(code);
59     if (itFunc != memberFuncMap_.end()) {
60         auto memberFunc = itFunc->second;
61         if (memberFunc != nullptr) {
62             return (this->*memberFunc)(data, reply);
63         }
64     }
65     HILOGI("BluetoothHfpHfStub::OnRemoteRequest, default case, need check.");
66     return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
67 }
68 
SendFileInner(MessageParcel & data,MessageParcel & reply)69 ErrCode BluetoothOppStub::SendFileInner(MessageParcel &data, MessageParcel &reply)
70 {
71     std::string device = data.ReadString();
72     bool result;
73     HILOGI("BluetoothOppStub::SendFileInner");
74     std::vector<std::string> filePaths = {};
75     std::vector<std::string> mimeTypes = {};
76     int32_t fileSize = data.ReadInt32();
77     HILOGI("BluetoothOppStub::GetDevicesByStatesInner");
78     for (int32_t i = 0; i < fileSize; i++) {
79         std::string filePath = data.ReadString();
80         filePaths.push_back(filePath);
81     }
82     int32_t mimeSize = data.ReadInt32();
83     for (int32_t i = 0; i < mimeSize; i++) {
84         std::string mimeType = data.ReadString();
85         mimeTypes.push_back(mimeType);
86     }
87 
88     ErrCode ec = SendFile(device, filePaths, mimeTypes, result);
89     if (SUCCEEDED(ec)) {
90         reply.WriteInt32(result);
91     }
92     return ERR_NONE;
93 }
94 
SetIncomingFileConfirmationInner(MessageParcel & data,MessageParcel & reply)95 ErrCode BluetoothOppStub::SetIncomingFileConfirmationInner(MessageParcel &data, MessageParcel &reply)
96 {
97     bool accept = data.ReadBool();
98     bool result;
99     HILOGI("BluetoothOppStub::SetIncomingFileConfirmationInner");
100     ErrCode ec = SetIncomingFileConfirmation(accept, result);
101     if (SUCCEEDED(ec)) {
102         reply.WriteInt32(result);
103     }
104     return ERR_NONE;
105 }
106 
GetCurrentTransferInformationInner(MessageParcel & data,MessageParcel & reply)107 ErrCode BluetoothOppStub::GetCurrentTransferInformationInner(MessageParcel &data, MessageParcel &reply)
108 {
109     HILOGI("BluetoothOppStub::GetCurrentTransferInformationInner");
110     BluetoothIOppTransferInformation oppInformation;
111     ErrCode ec = GetCurrentTransferInformation(oppInformation);
112     if (ec != ERR_OK) {
113         HILOGE("BluetoothOppStub::fail");
114         return ec;
115     }
116     reply.WriteParcelable(&oppInformation);
117     return ERR_NONE;
118 }
119 
CancelTransferInner(MessageParcel & data,MessageParcel & reply)120 ErrCode BluetoothOppStub::CancelTransferInner(MessageParcel &data, MessageParcel &reply)
121 {
122     HILOGI("BluetoothOppStub::CancelTransfer");
123     bool result;
124     ErrCode ec = CancelTransfer(result);
125     if (SUCCEEDED(ec)) {
126         reply.WriteInt32(result);
127     }
128     return ERR_NONE;
129 }
130 
RegisterObserverInner(MessageParcel & data,MessageParcel & reply)131 ErrCode BluetoothOppStub::RegisterObserverInner(MessageParcel &data, MessageParcel &reply)
132 {
133     HILOGI("BluetoothOppStub::RegisterObserverInner");
134     sptr<IRemoteObject> remote = data.ReadRemoteObject();
135     const sptr<IBluetoothOppObserver> observer = OHOS::iface_cast<IBluetoothOppObserver>(remote);
136     RegisterObserver(observer);
137     return NO_ERROR;
138 }
139 
DeregisterObserverInner(MessageParcel & data,MessageParcel & reply)140 ErrCode BluetoothOppStub::DeregisterObserverInner(MessageParcel &data, MessageParcel &reply)
141 {
142     HILOGI("BluetoothOppStub::DeregisterObserverInner");
143     sptr<IRemoteObject> remote = data.ReadRemoteObject();
144     const sptr<IBluetoothOppObserver> observer = OHOS::iface_cast<IBluetoothOppObserver>(remote);
145     DeregisterObserver(observer);
146     return NO_ERROR;
147 }
148 
GetDeviceStateInner(MessageParcel & data,MessageParcel & reply)149 ErrCode BluetoothOppStub::GetDeviceStateInner(MessageParcel &data, MessageParcel &reply)
150 {
151     const BluetoothRawAddress *device = data.ReadParcelable<BluetoothRawAddress>();
152     int result;
153     HILOGI("BluetoothOppStub::GetDeviceStateInner");
154     ErrCode ec = GetDeviceState(*device, result);
155     reply.WriteInt32(ec);
156     if (SUCCEEDED(ec)) {
157         reply.WriteInt32(result);
158     }
159     return ERR_NONE;
160 }
161 
GetDevicesByStatesInner(MessageParcel & data,MessageParcel & reply)162 ErrCode BluetoothOppStub::GetDevicesByStatesInner(MessageParcel &data, MessageParcel &reply)
163 {
164     std::vector<int32_t> states = {};
165     int32_t stateSize = data.ReadInt32();
166     HILOGI("BluetoothOppStub::GetDevicesByStatesInner");
167     for (int i = 0; i < stateSize; i++) {
168         int32_t state = data.ReadInt32();
169         states.push_back(state);
170     }
171     std::vector<BluetoothRawAddress> rawAdds;
172     ErrCode ec = GetDevicesByStates(states, rawAdds);
173     if (ec != ERR_OK) {
174         HILOGE("BluetoothOppStub::fail");
175         return ec;
176     }
177     reply.WriteInt32(rawAdds.size());
178     for (auto rawAdd : rawAdds) {
179         if (!reply.WriteParcelable(&rawAdd)) {
180             HILOGE("BluetoothOppStub:: WriteParcelable fail");
181             return ERR_INVALID_STATE;
182         }
183     }
184     return ERR_NONE;
185 }
186 }  // namespace Bluetooth
187 }  // namespace OHOS