• 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 
16 #include "bluetooth_def.h"
17 #include "bluetooth_log.h"
18 #include "bluetooth_utils_server.h"
19 #include "interface_profile.h"
20 #include "interface_profile_opp.h"
21 #include "i_bluetooth_host_observer.h"
22 #include "permission_utils.h"
23 #include "remote_observer_list.h"
24 #include "hilog/log.h"
25 #include "bluetooth_opp_server.h"
26 
27 namespace OHOS {
28 namespace Bluetooth {
29 class BluetoothOppCallback : public bluetooth::IOppObserver {
30 public:
31     BluetoothOppCallback() = default;
32     ~BluetoothOppCallback() override = default;
33 
OnReceiveIncomingFile(const IOppTransferInformation & transferInformation)34     void OnReceiveIncomingFile(const IOppTransferInformation &transferInformation) override
35     {
36         HILOGI("start.");
37         observers_->ForEach([transferInformation](sptr<IBluetoothOppObserver> observer) {
38             observer->OnReceiveIncomingFileChanged(BluetoothIOppTransferInformation(transferInformation));
39         });
40     }
OnTransferStateChange(const IOppTransferInformation & transferInformation)41     void OnTransferStateChange(const IOppTransferInformation &transferInformation) override
42     {
43         HILOGI("start.");
44         observers_->ForEach([transferInformation](sptr<IBluetoothOppObserver> observer) {
45             observer->OnTransferStateChanged(BluetoothIOppTransferInformation(transferInformation));
46         });
47     }
48 
SetObserver(RemoteObserverList<IBluetoothOppObserver> * observers)49     void SetObserver(RemoteObserverList<IBluetoothOppObserver> *observers)
50     {
51         observers_ = observers;
52     }
53 
54 private:
55     RemoteObserverList<IBluetoothOppObserver> *observers_;
56 };
57 
58 struct BluetoothOppServer::impl {
59     impl();
60     ~impl();
61 
62     class SystemStateObserver;
63     std::unique_ptr<SystemStateObserver> systemStateObserver_ = nullptr;
64 
65     RemoteObserverList<IBluetoothOppObserver> observers_;
66     std::unique_ptr<BluetoothOppCallback> observerImp_ = std::make_unique<BluetoothOppCallback>();
67     IProfileOpp *oppService_ = nullptr;
68     std::vector<sptr<IBluetoothOppObserver>> advCallBack_;
69 
GetServicePtrOHOS::Bluetooth::BluetoothOppServer::impl70     IProfileOpp *GetServicePtr()
71     {
72         if (IProfileManager::GetInstance() == nullptr) {
73             HILOGI("IProfileManager::GetInstance() is null");
74             return nullptr;
75         }
76         return static_cast<IProfileOpp *>(
77             IProfileManager::GetInstance()->GetProfileService(PROFILE_NAME_OPP));
78     }
79 };
80 
81 class BluetoothOppServer::impl::SystemStateObserver : public ISystemStateObserver {
82 public:
SystemStateObserver(BluetoothOppServer::impl * pimpl)83     explicit SystemStateObserver(BluetoothOppServer::impl *pimpl) : pimpl_(pimpl) {}
OnSystemStateChange(const BTSystemState state)84     void OnSystemStateChange(const BTSystemState state) override
85     {
86         HILOGI("BTSystemState:%{public}d", state);
87         switch (state) {
88             case BTSystemState::ON:
89                 pimpl_->oppService_ = pimpl_->GetServicePtr();
90                 if (pimpl_->oppService_ != nullptr) {
91                     pimpl_->oppService_->RegisterObserver(*pimpl_->observerImp_.get());
92                 }
93                 break;
94             case BTSystemState::OFF:
95                 pimpl_->oppService_ = nullptr;
96                 break;
97             default:
98                 break;
99         }
100     };
101 
102 private:
103     BluetoothOppServer::impl *pimpl_ = nullptr;
104 };
105 
impl()106 BluetoothOppServer::impl::impl()
107 {
108     HILOGI("enter");
109 }
110 
~impl()111 BluetoothOppServer::impl::~impl()
112 {
113     HILOGI("enter");
114 }
115 
BluetoothOppServer()116 BluetoothOppServer::BluetoothOppServer()
117 {
118     HILOGI("start");
119     pimpl = std::make_unique<impl>();
120     pimpl->observerImp_->SetObserver(&(pimpl->observers_));
121     pimpl->systemStateObserver_ = std::make_unique<impl::SystemStateObserver>(pimpl.get());
122     IAdapterManager::GetInstance()->RegisterSystemStateObserver(*(pimpl->systemStateObserver_));
123 
124     pimpl->oppService_ = pimpl->GetServicePtr();
125     if (pimpl->oppService_ != nullptr) {
126         pimpl->oppService_->RegisterObserver(*pimpl->observerImp_.get());
127     }
128 }
129 
~BluetoothOppServer()130 BluetoothOppServer::~BluetoothOppServer()
131 {
132     HILOGI("start");
133     IAdapterManager::GetInstance()->DeregisterSystemStateObserver(*(pimpl->systemStateObserver_));
134     if (pimpl->oppService_ != nullptr) {
135         pimpl->oppService_->DeregisterObserver(*pimpl->observerImp_.get());
136     }
137 }
138 
RegisterObserver(const sptr<IBluetoothOppObserver> observer)139 ErrCode BluetoothOppServer::RegisterObserver(const sptr<IBluetoothOppObserver> observer)
140 {
141     HILOGI("start");
142 
143     if (observer == nullptr) {
144         HILOGE("observer is null");
145         return ERR_INVALID_VALUE;
146     }
147     if (pimpl == nullptr) {
148         HILOGE("pimpl is null");
149         return ERR_NO_INIT;
150     }
151     auto func = std::bind(&BluetoothOppServer::DeregisterObserver, this, std::placeholders::_1);
152     pimpl->observers_.Register(observer, func);
153     pimpl->advCallBack_.push_back(observer);
154     return ERR_OK;
155 }
156 
DeregisterObserver(const sptr<IBluetoothOppObserver> observer)157 ErrCode BluetoothOppServer::DeregisterObserver(const sptr<IBluetoothOppObserver> observer)
158 {
159     HILOGI("start");
160     if (observer == nullptr) {
161         HILOGE("observer is null");
162         return ERR_INVALID_VALUE;
163     }
164     if (pimpl == nullptr) {
165         HILOGE("pimpl is null");
166         return ERR_NO_INIT;
167     }
168     for (auto iter = pimpl->advCallBack_.begin(); iter != pimpl->advCallBack_.end(); ++iter) {
169         if ((*iter)->AsObject() == observer->AsObject()) {
170             if (pimpl != nullptr) {
171                 pimpl->observers_.Deregister(*iter);
172                 pimpl->advCallBack_.erase(iter);
173                 break;
174             }
175         }
176     }
177     return ERR_OK;
178 }
179 
GetDevicesByStates(const std::vector<int32_t> & states,std::vector<BluetoothRawAddress> & result)180 ErrCode BluetoothOppServer::GetDevicesByStates(
181     const std::vector<int32_t> &states, std::vector<BluetoothRawAddress>& result)
182 {
183     HILOGI("start");
184     if (PermissionUtils::VerifyUseBluetoothPermission() == PERMISSION_DENIED) {
185         HILOGE("check permission failed");
186         return false;
187     }
188     if (pimpl == nullptr || pimpl->oppService_ == nullptr) {
189         HILOGE("oppService_ is null");
190         return ERR_NO_INIT;
191     }
192 
193     std::vector<bluetooth::RawAddress> serviceDeviceList = pimpl->oppService_->GetDevicesByStates(states);
194     for (auto &device : serviceDeviceList) {
195         BluetoothRawAddress bluetoothDevice(device.GetAddress());
196         result.push_back(bluetoothDevice);
197         HILOGI("%{public}s", GET_ENCRYPT_ADDR(bluetoothDevice));
198     }
199     return ERR_OK;
200 }
201 
GetDeviceState(const BluetoothRawAddress & device,int & result)202 ErrCode BluetoothOppServer::GetDeviceState(const BluetoothRawAddress &device, int& result)
203 {
204     HILOGI("addr:%{public}s, res:%{public}d", GET_ENCRYPT_ADDR(device), result);
205     if (PermissionUtils::VerifyUseBluetoothPermission() == PERMISSION_DENIED) {
206         HILOGE("check permission failed");
207         return BT_FAILURE;
208     }
209     if (pimpl == nullptr || pimpl->oppService_ == nullptr) {
210         HILOGE("oppService_ is null");
211         return ERR_NO_INIT;
212     }
213     result = pimpl->oppService_->GetDeviceState(device);
214     HILOGI("end, result:%{public}d", result);
215     return ERR_OK;
216 }
217 
SendFile(std::string & device,std::vector<std::string> & filePaths,std::vector<std::string> & mimeTypes,bool & result)218 ErrCode BluetoothOppServer::SendFile(std::string &device, std::vector<std::string> &filePaths,
219     std::vector<std::string> &mimeTypes, bool& result)
220 {
221     HILOGI("addr:%{public}s, res:%{public}d", GetEncryptAddr(device).c_str(), result);
222     if (PermissionUtils::VerifyDiscoverBluetoothPermission() == PERMISSION_DENIED) {
223         HILOGE("check permission failed");
224         return ERR_INVALID_VALUE;
225     }
226     if (pimpl == nullptr || pimpl->oppService_ == nullptr) {
227         HILOGE("oppService_ is null");
228         return ERR_NO_INIT;
229     }
230     result = pimpl->oppService_->SendFile(RawAddress(device), filePaths, mimeTypes);
231     HILOGI("end, result:%{public}d", result);
232     return ERR_OK;
233 }
234 
SetIncomingFileConfirmation(bool & accept,bool & result)235 ErrCode BluetoothOppServer::SetIncomingFileConfirmation(bool &accept, bool &result)
236 {
237     HILOGI("accept:%{public}d, res:%{public}d", accept, result);
238     if (PermissionUtils::VerifyDiscoverBluetoothPermission() == PERMISSION_DENIED) {
239         HILOGE("check permission failed");
240         return ERR_INVALID_VALUE;
241     }
242     if (pimpl == nullptr || pimpl->oppService_ == nullptr) {
243         HILOGE("oppService_ is null");
244         return ERR_NO_INIT;
245     }
246     result = pimpl->oppService_->SetIncomingFileConfirmation(accept);
247     HILOGI("end, result:%{public}d", result);
248     return ERR_OK;
249 }
250 
GetCurrentTransferInformation(BluetoothIOppTransferInformation & transferInformation)251 ErrCode BluetoothOppServer::GetCurrentTransferInformation(BluetoothIOppTransferInformation &transferInformation)
252 {
253     HILOGI("start");
254     if (PermissionUtils::VerifyDiscoverBluetoothPermission() == PERMISSION_DENIED) {
255         HILOGE("check permission failed");
256         return ERR_INVALID_VALUE;
257     }
258     if (pimpl == nullptr || pimpl->oppService_ == nullptr) {
259         HILOGE("oppService_ is null");
260         return ERR_NO_INIT;
261     }
262     transferInformation = BluetoothIOppTransferInformation(pimpl->oppService_->GetCurrentTransferInformation());
263     HILOGI("end");
264     return ERR_OK;
265 }
266 
CancelTransfer(bool & result)267 ErrCode BluetoothOppServer::CancelTransfer(bool &result)
268 {
269     HILOGI("res:%{public}d", result);
270     if (PermissionUtils::VerifyDiscoverBluetoothPermission() == PERMISSION_DENIED) {
271         HILOGE("check permission failed");
272         return ERR_INVALID_VALUE;
273     }
274     if (pimpl == nullptr || pimpl->oppService_ == nullptr) {
275         HILOGE("oppService_ is null");
276         return ERR_NO_INIT;
277     }
278     result = pimpl->oppService_->CancelTransfer();
279     HILOGI("end, result:%{public}d", result);
280     return ERR_OK;
281 }
282 }  // namespace Bluetooth
283 }  // namespace OHOS
284