• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 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 "ddm_adapter_impl.h"
17 
18 #include <algorithm>
19 
20 #include "devicestatus_define.h"
21 #include "i_dsoftbus_adapter.h"
22 #include "ipc_skeleton.h"
23 #include "ohos_account_kits.h"
24 #include "os_account_manager.h"
25 #include "utility.h"
26 
27 #undef LOG_TAG
28 #define LOG_TAG "DDMAdapterImpl"
29 
30 namespace OHOS {
31 namespace Msdp {
32 namespace DeviceStatus {
33 #define D_DEV_MGR   DistributedHardware::DeviceManager::GetInstance()
34 
~DDMAdapterImpl()35 DDMAdapterImpl::~DDMAdapterImpl()
36 {
37     Disable();
38 }
39 
Enable()40 int32_t DDMAdapterImpl::Enable()
41 {
42     CALL_DEBUG_ENTER;
43     std::lock_guard guard(lock_);
44     std::string pkgName(FI_PKG_NAME);
45     std::string extra;
46     initCb_ = std::make_shared<DmInitCb>();
47 
48     int32_t ret = D_DEV_MGR.InitDeviceManager(pkgName, initCb_);
49     if (ret != 0) {
50         FI_HILOGE("DM::InitDeviceManager fail");
51         goto INIT_FAIL;
52     }
53     boardStateCb_ = std::make_shared<DmBoardStateCb>(shared_from_this());
54 
55     ret = D_DEV_MGR.RegisterDevStateCallback(pkgName, extra, boardStateCb_);
56     if (ret != 0) {
57         FI_HILOGE("DM::RegisterDevStateCallback fail");
58         goto REG_FAIL;
59     }
60     return RET_OK;
61 
62 REG_FAIL:
63     ret = D_DEV_MGR.UnInitDeviceManager(pkgName);
64     if (ret != 0) {
65         FI_HILOGE("DM::UnInitDeviceManager fail");
66     }
67     boardStateCb_.reset();
68 
69 INIT_FAIL:
70     initCb_.reset();
71     return RET_ERR;
72 }
73 
Disable()74 void DDMAdapterImpl::Disable()
75 {
76     CALL_DEBUG_ENTER;
77     std::lock_guard guard(lock_);
78     std::string pkgName(FI_PKG_NAME);
79 
80     if (boardStateCb_ != nullptr) {
81         boardStateCb_.reset();
82         int32_t ret = D_DEV_MGR.UnRegisterDevStateCallback(pkgName);
83         if (ret != 0) {
84             FI_HILOGE("DM::UnRegisterDevStateCallback fail");
85         }
86     }
87     if (initCb_ != nullptr) {
88         initCb_.reset();
89         int32_t ret = D_DEV_MGR.UnInitDeviceManager(pkgName);
90         if (ret != 0) {
91             FI_HILOGE("DM::UnInitDeviceManager fail");
92         }
93     }
94 }
95 
AddBoardObserver(std::shared_ptr<IBoardObserver> observer)96 void DDMAdapterImpl::AddBoardObserver(std::shared_ptr<IBoardObserver> observer)
97 {
98     CALL_DEBUG_ENTER;
99     std::lock_guard guard(lock_);
100     CHKPV(observer);
101     observers_.erase(Observer());
102     observers_.emplace(observer);
103 }
104 
RemoveBoardObserver(std::shared_ptr<IBoardObserver> observer)105 void DDMAdapterImpl::RemoveBoardObserver(std::shared_ptr<IBoardObserver> observer)
106 {
107     CALL_DEBUG_ENTER;
108     std::lock_guard guard(lock_);
109     CHKPV(observer);
110     observers_.erase(Observer());
111     if (auto iter = observers_.find(Observer(observer)); iter != observers_.end()) {
112         observers_.erase(iter);
113     }
114 }
115 
GetTrustedDeviceList(std::vector<DistributedHardware::DmDeviceInfo> & deviceList)116 int32_t DDMAdapterImpl::GetTrustedDeviceList(std::vector<DistributedHardware::DmDeviceInfo> &deviceList)
117 {
118     CALL_INFO_TRACE;
119     deviceList.clear();
120     if (int32_t ret = D_DEV_MGR.GetTrustedDeviceList(FI_PKG_NAME, "", deviceList); ret != RET_OK) {
121         FI_HILOGE("GetTrustedDeviceList failed, ret %{public}d.", ret);
122         return RET_ERR;
123     }
124     return RET_OK;
125 }
126 
CheckSameAccountToLocal(const std::string & networkId)127 bool DDMAdapterImpl::CheckSameAccountToLocal(const std::string &networkId)
128 {
129     CALL_INFO_TRACE;
130     std::vector<int32_t> ids;
131     ErrCode ret = OHOS::AccountSA::OsAccountManager::QueryActiveOsAccountIds(ids);
132     if (ret != ERR_OK || ids.empty()) {
133         FI_HILOGE("Get userId from active Os AccountIds fail, ret : %{public}d", ret);
134         return false;
135     }
136     OHOS::AccountSA::OhosAccountInfo osAccountInfo;
137     ret = OHOS::AccountSA::OhosAccountKits::GetInstance().GetOhosAccountInfo(osAccountInfo);
138     if (ret != 0 || osAccountInfo.uid_ == "") {
139         FI_HILOGE("Get accountId from Ohos account info fail, ret: %{public}d.", ret);
140         return false;
141     }
142     DistributedHardware::DmAccessCaller Caller = {
143         .accountId = osAccountInfo.uid_,
144         .networkId = IDSoftbusAdapter::GetLocalNetworkId(),
145         .userId = ids[0],
146         .tokenId = IPCSkeleton::GetCallingTokenID(),
147     };
148     DistributedHardware::DmAccessCallee Callee = {
149         .networkId = networkId,
150         .peerId = "",
151     };
152     if (D_DEV_MGR.CheckIsSameAccount(Caller, Callee)) {
153             return true;
154         }
155     FI_HILOGI("check same account fail, will try check access Group by hichain");
156     return false;
157 }
158 
CheckSameAccountToLocalWithUid(const std::string & networkId,const int32_t uid)159 bool DDMAdapterImpl::CheckSameAccountToLocalWithUid(const std::string &networkId, const int32_t uid)
160 {
161     CALL_INFO_TRACE;
162     int32_t appUserId = -1;
163     OHOS::AccountSA::OsAccountManager::GetOsAccountLocalIdFromUid(uid, appUserId);
164     FI_HILOGI("GetOsAccountLocalIdFromUid uid:%{private}d, localId:%{private}d", uid, appUserId);
165     bool isForegroundUser = false;
166     int32_t res = OHOS::AccountSA::OsAccountManager::IsOsAccountForeground(appUserId, isForegroundUser);
167     if (res != ERR_OK) {
168         FI_HILOGE("app userId %{private}d is not Foreground, ret:%{public}d", appUserId, res);
169         return false;
170     }
171     if (!isForegroundUser) {
172         FI_HILOGE("app userId is not Foreground");
173         return false;
174     }
175     isForegroundUser = false;
176     std::vector<AccountSA::ForegroundOsAccount> accounts;
177     ErrCode errCode = OHOS::AccountSA::OsAccountManager::GetForegroundOsAccounts(accounts);
178     if (errCode != ERR_OK || accounts.empty()) {
179         FI_HILOGE("GetForegroundOsAccounts fail, ret : %{public}d", errCode);
180         return false;
181     }
182     for (auto account : accounts) {
183         if (account.localId == appUserId) {
184             isForegroundUser = true;
185             break;
186         }
187     }
188     if (!isForegroundUser) {
189         FI_HILOGI("app userId is not Foreground");
190         return false;
191     }
192     return CheckSameAccountToLocal(networkId);
193 }
194 
OnBoardOnline(const std::string & networkId)195 void DDMAdapterImpl::OnBoardOnline(const std::string &networkId)
196 {
197     CALL_DEBUG_ENTER;
198     std::lock_guard guard(lock_);
199     FI_HILOGI("Board \'%{public}s\' is online", Utility::Anonymize(networkId).c_str());
200     std::for_each(observers_.cbegin(), observers_.cend(),
201         [&networkId](const auto &item) {
202             if (auto observer = item.Lock(); observer != nullptr) {
203                 observer->OnBoardOnline(networkId);
204             }
205         });
206 }
207 
OnBoardOffline(const std::string & networkId)208 void DDMAdapterImpl::OnBoardOffline(const std::string &networkId)
209 {
210     CALL_DEBUG_ENTER;
211     std::lock_guard guard(lock_);
212     FI_HILOGI("Board \'%{public}s\' is offline", Utility::Anonymize(networkId).c_str());
213     std::for_each(observers_.cbegin(), observers_.cend(),
214         [&networkId](const auto &item) {
215             if (auto observer = item.Lock(); observer != nullptr) {
216                 observer->OnBoardOffline(networkId);
217             }
218         });
219 }
220 } // namespace DeviceStatus
221 } // namespace Msdp
222 } // namespace OHOS
223