• 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 
152     pimpl->observers_.Register(observer);
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     pimpl->oppService_->DeregisterObserver(*pimpl->observerImp_.get());
178     return ERR_OK;
179 }
180 
GetDevicesByStates(const std::vector<int32_t> & states,std::vector<BluetoothRawAddress> & result)181 ErrCode BluetoothOppServer::GetDevicesByStates(
182     const std::vector<int32_t> &states, std::vector<BluetoothRawAddress>& result)
183 {
184     HILOGI("start");
185     if (PermissionUtils::VerifyUseBluetoothPermission() == PERMISSION_DENIED) {
186         HILOGE("check permission failed");
187         return false;
188     }
189     if (pimpl == nullptr || pimpl->oppService_ == nullptr) {
190         HILOGE("oppService_ is null");
191         return ERR_NO_INIT;
192     }
193 
194     std::vector<bluetooth::RawAddress> serviceDeviceList = pimpl->oppService_->GetDevicesByStates(states);
195     for (auto &device : serviceDeviceList) {
196         BluetoothRawAddress bluetoothDevice(device.GetAddress());
197         result.push_back(bluetoothDevice);
198         HILOGI("%{public}s", GET_ENCRYPT_ADDR(bluetoothDevice));
199     }
200     return ERR_OK;
201 }
202 
GetDeviceState(const BluetoothRawAddress & device,int & result)203 ErrCode BluetoothOppServer::GetDeviceState(const BluetoothRawAddress &device, int& result)
204 {
205     HILOGI("addr:%{public}s, res:%{public}d", GET_ENCRYPT_ADDR(device), result);
206     if (PermissionUtils::VerifyUseBluetoothPermission() == PERMISSION_DENIED) {
207         HILOGE("check permission failed");
208         return BT_FAILURE;
209     }
210     if (pimpl == nullptr || pimpl->oppService_ == nullptr) {
211         HILOGE("oppService_ is null");
212         return ERR_NO_INIT;
213     }
214     result = pimpl->oppService_->GetDeviceState(device);
215     HILOGI("end, result:%{public}d", result);
216     return ERR_OK;
217 }
218 
SendFile(std::string & device,std::vector<std::string> & filePaths,std::vector<std::string> & mimeTypes,bool & result)219 ErrCode BluetoothOppServer::SendFile(std::string &device, std::vector<std::string> &filePaths,
220     std::vector<std::string> &mimeTypes, bool& result)
221 {
222     HILOGI("addr:%{public}s, res:%{public}d", GetEncryptAddr(device).c_str(), result);
223     if (PermissionUtils::VerifyDiscoverBluetoothPermission() == PERMISSION_DENIED) {
224         HILOGE("check permission failed");
225         return ERR_INVALID_VALUE;
226     }
227     if (pimpl == nullptr || pimpl->oppService_ == nullptr) {
228         HILOGE("oppService_ is null");
229         return ERR_NO_INIT;
230     }
231     result = pimpl->oppService_->SendFile(device, filePaths, mimeTypes);
232     HILOGI("end, result:%{public}d", result);
233     return ERR_OK;
234 }
235 
SetIncomingFileConfirmation(bool & accept,bool & result)236 ErrCode BluetoothOppServer::SetIncomingFileConfirmation(bool &accept, bool &result)
237 {
238     HILOGI("accept:%{public}d, res:%{public}d", accept, result);
239     if (PermissionUtils::VerifyDiscoverBluetoothPermission() == PERMISSION_DENIED) {
240         HILOGE("check permission failed");
241         return ERR_INVALID_VALUE;
242     }
243     if (pimpl == nullptr || pimpl->oppService_ == nullptr) {
244         HILOGE("oppService_ is null");
245         return ERR_NO_INIT;
246     }
247     result = pimpl->oppService_->SetIncomingFileConfirmation(accept);
248     HILOGI("end, result:%{public}d", result);
249     return ERR_OK;
250 }
251 
GetCurrentTransferInformation(BluetoothIOppTransferInformation & transferInformation)252 ErrCode BluetoothOppServer::GetCurrentTransferInformation(BluetoothIOppTransferInformation &transferInformation)
253 {
254     HILOGI("start");
255     if (PermissionUtils::VerifyDiscoverBluetoothPermission() == PERMISSION_DENIED) {
256         HILOGE("check permission failed");
257         return ERR_INVALID_VALUE;
258     }
259     if (pimpl == nullptr || pimpl->oppService_ == nullptr) {
260         HILOGE("oppService_ is null");
261         return ERR_NO_INIT;
262     }
263     transferInformation = BluetoothIOppTransferInformation(pimpl->oppService_->GetCurrentTransferInformation());
264     HILOGI("end");
265     return ERR_OK;
266 }
267 
CancelTransfer(bool & result)268 ErrCode BluetoothOppServer::CancelTransfer(bool &result)
269 {
270     HILOGI("res:%{public}d", result);
271     if (PermissionUtils::VerifyDiscoverBluetoothPermission() == PERMISSION_DENIED) {
272         HILOGE("check permission failed");
273         return ERR_INVALID_VALUE;
274     }
275     if (pimpl == nullptr || pimpl->oppService_ == nullptr) {
276         HILOGE("oppService_ is null");
277         return ERR_NO_INIT;
278     }
279     result = pimpl->oppService_->CancelTransfer();
280     HILOGI("end, result:%{public}d", result);
281     return ERR_OK;
282 }
283 }  // namespace Bluetooth
284 }  // namespace OHOS
285