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