• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023-2025 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 "drv_bundle_state_callback.h"
17 
18 #include <want.h>
19 #include <element_name.h>
20 
21 #include "system_ability_definition.h"
22 #include "iservice_registry.h"
23 #include "bundle_constants.h"
24 #include "os_account_manager.h"
25 #include  "pkg_db_helper.h"
26 
27 #include "hdf_log.h"
28 #include "edm_errors.h"
29 #include "hilog_wrapper.h"
30 #include "hitrace_meter.h"
31 #include "bus_extension_core.h"
32 #include "accesstoken_kit.h"
33 #include <unordered_map>
34 #include <pthread.h>
35 #include <thread>
36 #include "driver_report_sys_event.h"
37 #include "usb_driver_info.h"
38 #include <memory.h>
39 namespace OHOS {
40 namespace ExternalDeviceManager {
41 using namespace std;
42 using namespace OHOS;
43 using namespace OHOS::AAFwk;
44 using namespace OHOS::AppExecFwk;
45 using namespace OHOS::ExternalDeviceManager;
46 using namespace OHOS::Security::AccessToken;
47 
48 constexpr uint64_t LABEL = HITRACE_TAG_OHOS;
49 const string DRV_INFO_BUS = "bus";
50 const string DRV_INFO_VENDOR = "vendor";
51 const string DRV_INFO_DESC = "description";
52 const string DRV_INFO_LAUNCHONBIND = "launchonbind";
53 const string DRV_INFO_ALLOW_ACCESSED = "ohos.permission.ACCESS_DDK_ALLOWED";
54 
55 static constexpr const char *BUNDLE_RESET_TASK_NAME = "DRIVER_INFO_RESET";
56 static constexpr const char *BUNDLE_UPDATE_TASK_NAME = "DRIVER_INFO_UPDATE";
57 static constexpr const char *GET_DRIVERINFO_TASK_NAME = "GET_DRIVERINFO_ASYNC";
58 
GetBundleSize(const std::string & bundleName)59 std::string DrvBundleStateCallback::GetBundleSize(const std::string &bundleName)
60 {
61     std::string bundleSize = "";
62     int32_t userId = GetCurrentActiveUserId();
63     std::vector<int64_t> bundleStats;
64     std::lock_guard<std::mutex> lock(bundleMgrMutex_);
65     if (!GetBundleMgrProxy()) {
66         EDM_LOGE(MODULE_PKG_MGR, "%{public}s: failed to GetBundleMgrProxy", __func__);
67         return bundleSize;
68     }
69     if (!bundleMgr_->GetBundleStats(bundleName, userId, bundleStats)) {
70         EDM_LOGE(MODULE_PKG_MGR, "GetBundleStats failed");
71         return bundleSize;
72     }
73     if (!bundleStats.empty()) {
74         bundleSize.append(std::to_string(bundleStats[0]));
75     }
76     return bundleSize;
77 }
78 
ChangeValue(DriverInfo & tmpDrvInfo,const map<string,string> & metadata)79 void DrvBundleStateCallback::ChangeValue(DriverInfo &tmpDrvInfo, const map<string, string> &metadata)
80 {
81     for (auto data : metadata) {
82         if (data.first == DRV_INFO_BUS) {
83             tmpDrvInfo.bus_ = data.second;
84         }
85         if (data.first == DRV_INFO_VENDOR) {
86             tmpDrvInfo.vendor_ = data.second;
87         }
88         if (data.first == DRV_INFO_DESC) {
89             tmpDrvInfo.description_ = data.second;
90         }
91         if (LowerStr(data.first) == DRV_INFO_LAUNCHONBIND) {
92             tmpDrvInfo.launchOnBind_ = (data.second == "true");
93         }
94         if (data.first == DRV_INFO_ALLOW_ACCESSED) {
95             tmpDrvInfo.accessAllowed_ = (data.second == "true");
96         }
97     }
98 }
99 
ParseToPkgInfoTables(const std::vector<ExtensionAbilityInfo> & driverInfos,std::vector<PkgInfoTable> & pkgInfoTables)100 void DrvBundleStateCallback::ParseToPkgInfoTables(const std::vector<ExtensionAbilityInfo> &driverInfos,
101     std::vector<PkgInfoTable> &pkgInfoTables)
102 {
103     std::unordered_map<std::string, std::string> bundlesSize;
104     shared_ptr<IBusExtension> extInstance = nullptr;
105     for (const auto &driverInfo : driverInfos) {
106         if (driverInfo.type != ExtensionAbilityType::DRIVER || driverInfo.metadata.empty()) {
107             continue;
108         }
109         DriverInfo tmpDrvInfo(driverInfo.bundleName, driverInfo.name);
110         map<string, string> metaMap;
111         for (auto meta : driverInfo.metadata) {
112             metaMap.emplace(meta.name, meta.value);
113         }
114         ChangeValue(tmpDrvInfo, metaMap);
115         extInstance = BusExtensionCore::GetInstance().GetBusExtensionByName(tmpDrvInfo.GetBusName());
116         if (extInstance == nullptr) {
117             EDM_LOGE(MODULE_PKG_MGR, "GetBusExtensionByName failed, bus:%{public}s", tmpDrvInfo.bus_.c_str());
118             continue;
119         }
120         tmpDrvInfo.driverInfoExt_ = extInstance->ParseDriverInfo(metaMap);
121         if (tmpDrvInfo.driverInfoExt_ == nullptr) {
122             EDM_LOGE(MODULE_PKG_MGR, "ParseDriverInfo null");
123             continue;
124         }
125 
126         if (bundlesSize.find(driverInfo.bundleName) == bundlesSize.end()) {
127             std::string bundleSize = GetBundleSize(driverInfo.bundleName);
128             bundlesSize.emplace(driverInfo.bundleName, bundleSize);
129         }
130 
131         tmpDrvInfo.driverSize_ = bundlesSize[driverInfo.bundleName];
132         tmpDrvInfo.version_ = driverInfo.applicationInfo.versionName;
133         string driverInfoStr;
134         if (tmpDrvInfo.Serialize(driverInfoStr) != EDM_OK) {
135             EDM_LOGE(MODULE_PKG_MGR, "Serialize driverInfo faild");
136             continue;
137         }
138 
139         PkgInfoTable pkgInfo = CreatePkgInfoTable(driverInfo, driverInfoStr);
140         pkgInfoTables.emplace_back(pkgInfo);
141     }
142 }
143 
CreatePkgInfoTable(const ExtensionAbilityInfo & driverInfo,string driverInfoStr)144 PkgInfoTable DrvBundleStateCallback::CreatePkgInfoTable(const ExtensionAbilityInfo &driverInfo, string driverInfoStr)
145 {
146     PkgInfoTable pkgInfo = {
147         .driverUid = driverInfo.name + "-" + std::to_string(driverInfo.applicationInfo.accessTokenId),
148         .bundleAbility = driverInfo.bundleName + "-" + driverInfo.name,
149         .bundleName = driverInfo.bundleName,
150         .driverName = driverInfo.name,
151         .driverInfo = driverInfoStr
152     };
153     HapTokenInfo hapTokenInfo;
154     if (AccessTokenKit::GetHapTokenInfo(driverInfo.applicationInfo.accessTokenId, hapTokenInfo)
155         == AccessTokenKitRet::RET_SUCCESS) {
156         pkgInfo.userId = hapTokenInfo.userID;
157         pkgInfo.appIndex = hapTokenInfo.instIndex;
158     }
159     return pkgInfo;
160 }
161 
DrvBundleStateCallback()162 DrvBundleStateCallback::DrvBundleStateCallback()
163 {
164     stiching.clear();
165     stiching += "-";
166 };
167 
DrvBundleStateCallback(shared_future<int32_t> bmsFuture,shared_future<int32_t> accountFuture,shared_future<int32_t> commEventFuture)168 DrvBundleStateCallback::DrvBundleStateCallback(shared_future<int32_t> bmsFuture, shared_future<int32_t> accountFuture,
169     shared_future<int32_t> commEventFuture): DrvBundleStateCallback()
170 {
171     bmsFuture_ = bmsFuture;
172     accountFuture_ = accountFuture;
173     commEventFuture_ = commEventFuture;
174 }
175 
~DrvBundleStateCallback()176 DrvBundleStateCallback::~DrvBundleStateCallback()
177 {
178     return;
179 };
180 
PrintTest()181 void DrvBundleStateCallback::PrintTest()
182 {
183     std::vector<std::string> allBundleAbilityNames;
184     std::shared_ptr<PkgDbHelper> helper = PkgDbHelper::GetInstance();
185     int32_t ret = helper->QueryAllSize(allBundleAbilityNames);
186     if (ret <= 0) {
187         EDM_LOGE(MODULE_PKG_MGR, "QueryAllSize failed");
188         return;
189     }
190     cout << "allBundleAbilityNames_ size = " << allBundleAbilityNames.size() << endl;
191 }
192 
OnBundleStateChanged(const uint8_t installType,const int32_t resultCode,const std::string & resultMsg,const std::string & bundleName)193 void DrvBundleStateCallback::OnBundleStateChanged(const uint8_t installType, const int32_t resultCode,
194     const std::string &resultMsg, const std::string &bundleName)
195 {
196     EDM_LOGI(MODULE_PKG_MGR, "OnBundleStateChanged");
197     return;
198 };
199 
200 /**
201     * @brief Called when a new application package has been installed on the device.
202     * @param bundleName Indicates the name of the bundle whose state has been installed.
203     * @param userId Indicates the id of the bundle whose state has been installed.
204     */
OnBundleAdded(const std::string & bundleName,const int userId)205 void DrvBundleStateCallback::OnBundleAdded(const std::string &bundleName, const int userId)
206 {
207     EDM_LOGI(MODULE_PKG_MGR, "OnBundleAdded");
208     StartTrace(LABEL, "OnBundleAdded");
209     if (!IsCurrentUserId(userId)) {
210         return;
211     }
212     std::vector<ExtensionAbilityInfo> driverInfos;
213     if (!QueryDriverInfos(bundleName, userId, driverInfos) || driverInfos.empty()) {
214         return;
215     }
216 
217     if (!UpdateToRdb(driverInfos, bundleName)) {
218         EDM_LOGE(MODULE_PKG_MGR, "OnBundleAdded error");
219     }
220     FinishTrace(LABEL);
221     ReportBundleSysEvent(driverInfos, bundleName, "BUNDLE_ADD");
222 }
223 /**
224     * @brief Called when a new application package has been Updated on the device.
225     * @param bundleName Indicates the name of the bundle whose state has been Updated.
226     * @param userId Indicates the id of the bundle whose state has been Updated.
227     */
OnBundleUpdated(const std::string & bundleName,const int userId)228 void DrvBundleStateCallback::OnBundleUpdated(const std::string &bundleName, const int userId)
229 {
230     EDM_LOGI(MODULE_PKG_MGR, "OnBundleUpdated");
231     StartTrace(LABEL, "OnBundleUpdated");
232     if (!IsCurrentUserId(userId)) {
233         return;
234     }
235     std::vector<ExtensionAbilityInfo> driverInfos;
236     if (!QueryDriverInfos(bundleName, userId, driverInfos)) {
237         return;
238     }
239 
240     if (driverInfos.empty()) {
241         OnBundleDrvRemoved(bundleName);
242         return;
243     }
244 
245     if (!UpdateToRdb(driverInfos, bundleName)) {
246         EDM_LOGE(MODULE_PKG_MGR, "OnBundleUpdated error");
247     }
248     FinishTrace(LABEL);
249     ReportBundleSysEvent(driverInfos, bundleName, "BUNDLE_UPDATE");
250 }
251 
252 /**
253     * @brief Called when a new application package has been Removed on the device.
254     * @param bundleName Indicates the name of the bundle whose state has been Removed.
255     * @param userId Indicates the id of the bundle whose state has been Removed.
256     */
OnBundleRemoved(const std::string & bundleName,const int userId)257 void DrvBundleStateCallback::OnBundleRemoved(const std::string &bundleName, const int userId)
258 {
259     EDM_LOGI(MODULE_PKG_MGR, "OnBundleRemoved");
260     StartTrace(LABEL, "OnBundleRemoved");
261     if (!IsCurrentUserId(userId)) {
262         return;
263     }
264     std::vector<ExtensionAbilityInfo> driverInfos;
265     (void)QueryDriverInfos(bundleName, userId, driverInfos);
266     ReportBundleSysEvent(driverInfos, bundleName, "BUNDLE_REMOVED");
267     OnBundleDrvRemoved(bundleName);
268     FinishTrace(LABEL);
269 }
270 
AsObject()271 sptr<IRemoteObject> DrvBundleStateCallback::AsObject()
272 {
273     return nullptr;
274 }
275 
ResetInitOnce()276 void DrvBundleStateCallback::ResetInitOnce()
277 {
278     std::lock_guard<std::mutex> lock(initOnceMutex_);
279     initOnce = false;
280 }
281 
ResetMatchedBundles(const int32_t userId)282 void DrvBundleStateCallback::ResetMatchedBundles(const int32_t userId)
283 {
284     if (bundleUpdateCallback_ == nullptr) {
285         EDM_LOGE(MODULE_PKG_MGR, "DrvBundleStateCallback::ResetMatchedBundles bundleUpdateCallback_ is null");
286         return;
287     }
288     std::thread taskThread([userId, this]() {
289         bundleUpdateCallback_->OnBundlesReseted(userId);
290     });
291     pthread_setname_np(taskThread.native_handle(), BUNDLE_RESET_TASK_NAME);
292     taskThread.detach();
293 }
294 
GetAllDriverInfos()295 bool DrvBundleStateCallback::GetAllDriverInfos()
296 {
297     std::lock_guard<std::mutex> lock(initOnceMutex_);
298     if (initOnce) {
299         EDM_LOGI(MODULE_PKG_MGR, "GetAllDriverInfos has inited");
300         return true;
301     }
302 
303     std::vector<ExtensionAbilityInfo> driverInfos;
304     int32_t userId = GetCurrentActiveUserId();
305     if (userId == Constants::INVALID_USERID) {
306         EDM_LOGI(MODULE_PKG_MGR, "GetCurrentActiveUserId userId is invalid");
307         return false;
308     }
309     EDM_LOGI(MODULE_PKG_MGR, "QueryExtensionAbilityInfos userId:%{public}d", userId);
310     bundleMgrMutex_.lock();
311     if (!GetBundleMgrProxy()) {
312         EDM_LOGE(MODULE_PKG_MGR, "%{public}s: failed to GetBundleMgrProxy", __func__);
313         bundleMgrMutex_.unlock();
314         return false;
315     }
316     bundleMgr_->QueryExtensionAbilityInfos(ExtensionAbilityType::DRIVER, userId, driverInfos);
317     bundleMgrMutex_.unlock();
318     if (!UpdateToRdb(driverInfos, "")) {
319         EDM_LOGE(MODULE_PKG_MGR, "UpdateToRdb failed");
320         return false;
321     }
322     initOnce = true;
323     return true;
324 }
325 
GetAllDriverInfosAsync()326 void DrvBundleStateCallback::GetAllDriverInfosAsync()
327 {
328     EDM_LOGI(MODULE_PKG_MGR, "GetAllDriverInfosAsync enter");
329     std::thread taskThread([this]() {
330         bmsFuture_.wait();
331         accountFuture_.wait();
332         if (!GetAllDriverInfos()) {
333             EDM_LOGE(MODULE_PKG_MGR, "GetAllDriverInfos failed");
334         }
335     });
336     pthread_setname_np(taskThread.native_handle(), GET_DRIVERINFO_TASK_NAME);
337     taskThread.detach();
338 }
339 
GetStiching()340 string DrvBundleStateCallback::GetStiching()
341 {
342     return stiching;
343 }
344 
CheckBundleMgrProxyPermission()345 bool DrvBundleStateCallback::CheckBundleMgrProxyPermission()
346 {
347     // check permission
348     std::lock_guard<std::mutex> lock(bundleMgrMutex_);
349     if (!GetBundleMgrProxy()) {
350         EDM_LOGE(MODULE_PKG_MGR, "%{public}s: failed to GetBundleMgrProxy", __func__);
351         return false;
352     }
353     if (!bundleMgr_->VerifySystemApi(Constants::INVALID_API_VERSION)) {
354         EDM_LOGE(MODULE_PKG_MGR, "non-system app calling system api");
355         return false;
356     }
357     if (!bundleMgr_->VerifyCallingPermission(Constants::LISTEN_BUNDLE_CHANGE)) {
358         EDM_LOGE(MODULE_PKG_MGR, "register bundle status callback failed due to lack of permission");
359         return false;
360     }
361     return true;
362 }
363 
QueryDriverInfos(const std::string & bundleName,const int userId,std::vector<ExtensionAbilityInfo> & driverInfos)364 bool DrvBundleStateCallback::QueryDriverInfos(const std::string &bundleName, const int userId,
365     std::vector<ExtensionAbilityInfo> &driverInfos)
366 {
367     if (bundleName.empty()) {
368         EDM_LOGE(MODULE_PKG_MGR, "%{public}s: BundleName is empty", __func__);
369         return false;
370     }
371 
372     std::lock_guard<std::mutex> lock(bundleMgrMutex_);
373     if (!GetBundleMgrProxy()) {
374         EDM_LOGE(MODULE_PKG_MGR, "%{public}s: failed to GetBundleMgrProxy", __func__);
375         return false;
376     }
377 
378     BundleInfo tmpBundleInfo;
379     int32_t flags = static_cast<int32_t>(GetBundleInfoFlag::GET_BUNDLE_INFO_WITH_EXTENSION_ABILITY) + \
380                     static_cast<int32_t>(GetBundleInfoFlag::GET_BUNDLE_INFO_WITH_APPLICATION) + \
381                     static_cast<int32_t>(GetBundleInfoFlag::GET_BUNDLE_INFO_WITH_METADATA);
382     if (!(bundleMgr_->GetBundleInfo(bundleName, flags, tmpBundleInfo, userId))) {
383         EDM_LOGE(MODULE_PKG_MGR, "GetBundleInfo err");
384         return false;
385     }
386 
387     for (auto &extensionInfo : tmpBundleInfo.extensionInfos) {
388         if (extensionInfo.type == ExtensionAbilityType::DRIVER) {
389             extensionInfo.applicationInfo = tmpBundleInfo.applicationInfo;
390             driverInfos.emplace_back(extensionInfo);
391         }
392     }
393     return true;
394 }
395 
ClearDriverInfo(DriverInfo & tmpDrvInfo)396 void DrvBundleStateCallback::ClearDriverInfo(DriverInfo &tmpDrvInfo)
397 {
398     tmpDrvInfo.bus_.clear();
399     tmpDrvInfo.vendor_.clear();
400     tmpDrvInfo.version_.clear();
401     tmpDrvInfo.driverInfoExt_ = nullptr;
402 }
403 
UpdateToRdb(const std::vector<ExtensionAbilityInfo> & driverInfos,const std::string & bundleName)404 bool DrvBundleStateCallback::UpdateToRdb(const std::vector<ExtensionAbilityInfo> &driverInfos,
405     const std::string &bundleName)
406 {
407     std::vector<PkgInfoTable> pkgInfoTables;
408     ParseToPkgInfoTables(driverInfos, pkgInfoTables);
409     std::shared_ptr<PkgDbHelper> helper = PkgDbHelper::GetInstance();
410     if (helper->AddOrUpdatePkgInfo(pkgInfoTables, bundleName) < PKG_OK) {
411         EDM_LOGE(MODULE_PKG_MGR, "add or update failed,bundleName:%{public}s", bundleName.c_str());
412         return false;
413     }
414 
415     if (bundleUpdateCallback_ != nullptr) {
416         std::thread taskThread([bundleName, this]() {
417             if (bundleUpdateCallback_ == nullptr) {
418                 EDM_LOGE(MODULE_PKG_MGR, "UpdateToRdb bundleUpdateCallback_ is nullptr");
419                 return;
420             }
421             bundleUpdateCallback_->OnBundlesUpdated(bundleName);
422         });
423         pthread_setname_np(taskThread.native_handle(), BUNDLE_UPDATE_TASK_NAME);
424         taskThread.detach();
425     }
426     return true;
427 }
428 
GetCurrentActiveUserId()429 int32_t DrvBundleStateCallback::GetCurrentActiveUserId()
430 {
431     int32_t localId;
432     int32_t ret = AccountSA::OsAccountManager::GetForegroundOsAccountLocalId(localId);
433     if (ret != 0) {
434         EDM_LOGE(MODULE_PKG_MGR, "GetForegroundOsAccountLocalId failed ret:%{public}d", ret);
435         return Constants::INVALID_USERID;
436     }
437     return localId;
438 }
439 
IsCurrentUserId(const int userId)440 bool DrvBundleStateCallback::IsCurrentUserId(const int userId)
441 {
442     return GetCurrentActiveUserId() == userId;
443 }
444 
GetBundleMgrProxy()445 bool DrvBundleStateCallback::GetBundleMgrProxy()
446 {
447     if (bundleMgr_ == nullptr) {
448         auto systemAbilityManager = OHOS::SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
449         if (systemAbilityManager == nullptr) {
450             EDM_LOGE(MODULE_PKG_MGR, "%{public}s: GetSystemAbilityManager is null", __func__);
451             return false;
452         }
453         auto remoteObj = systemAbilityManager->GetSystemAbility(OHOS::BUNDLE_MGR_SERVICE_SYS_ABILITY_ID);
454         if (remoteObj == nullptr) {
455             EDM_LOGE(MODULE_PKG_MGR, "%{public}s: GetSystemAbility is null", __func__);
456             return false;
457         }
458         bundleMgr_ = OHOS::iface_cast<IBundleMgr>(remoteObj);
459         if (bundleMgr_ == nullptr) {
460             EDM_LOGE(MODULE_PKG_MGR, "%{public}s: iface_cast get null", __func__);
461             return false;
462         }
463         bmsDeathRecipient_ = new (std::nothrow) BundleMgrDeathRecipient([this]() {
464             ResetBundleMgr();
465         });
466         if (bmsDeathRecipient_ == nullptr) {
467             EDM_LOGE(MODULE_PKG_MGR, "%{public}s: failed to create BundleMgrDeathRecipient", __func__);
468             bundleMgr_ = nullptr;
469             return false;
470         }
471         remoteObj->AddDeathRecipient(bmsDeathRecipient_);
472     }
473     return true;
474 }
475 
OnBundleDrvRemoved(const std::string & bundleName)476 void DrvBundleStateCallback::OnBundleDrvRemoved(const std::string &bundleName)
477 {
478     std::shared_ptr<PkgDbHelper> helper = PkgDbHelper::GetInstance();
479 
480     int32_t ret = helper->DeleteRightRecord(bundleName);
481     if (ret < 0) {
482         EDM_LOGE(MODULE_PKG_MGR, "delete failed: %{public}s", bundleName.c_str());
483         return;
484     }
485     if (bundleUpdateCallback_ != nullptr) {
486         std::thread taskThread([bundleName, this]() {
487             if (bundleUpdateCallback_ == nullptr) {
488                 EDM_LOGE(MODULE_PKG_MGR, "OnBundleDrvRemoved bundleUpdateCallback_ is nullptr");
489                 return;
490             }
491             bundleUpdateCallback_->OnBundlesUpdated(bundleName);
492         });
493         pthread_setname_np(taskThread.native_handle(), BUNDLE_UPDATE_TASK_NAME);
494         taskThread.detach();
495     }
496 }
497 
ResetBundleMgr()498 void DrvBundleStateCallback::ResetBundleMgr()
499 {
500     std::lock_guard<std::mutex> lock(bundleMgrMutex_);
501     if (bundleMgr_ != nullptr && bundleMgr_->AsObject()) {
502         bundleMgr_->AsObject()->RemoveDeathRecipient(bmsDeathRecipient_);
503     }
504     bundleMgr_ = nullptr;
505 }
506 
ReportBundleSysEvent(const std::vector<ExtensionAbilityInfo> & driverInfos,const std::string & bundleName,std::string driverEventName)507 void DrvBundleStateCallback::ReportBundleSysEvent(const std::vector<ExtensionAbilityInfo> &driverInfos,
508     const std::string &bundleName, std::string driverEventName)
509 {
510     EDM_LOGI(MODULE_BUS_USB,  "ReportBundleSysEvent begin");
511     std::vector<PkgInfoTable> pkgInfoTables;
512     ParseToPkgInfoTables(driverInfos, pkgInfoTables);
513     for (const auto &pkgInfoTable : pkgInfoTables) {
514         if (pkgInfoTable.bundleName == bundleName) {
515             DriverInfo tmpDrvInfo;
516             if (tmpDrvInfo.UnSerialize(pkgInfoTable.driverInfo) != EDM_OK) {
517                 EDM_LOGE(MODULE_PKG_MGR, "Unserialize driverInfo faild");
518                 return;
519             }
520             shared_ptr<UsbDriverInfo> usbDriverInfo = make_shared<UsbDriverInfo>();
521             if (usbDriverInfo == nullptr) {
522                 EDM_LOGE(MODULE_BUS_USB,  "creat UsbDriverInfo obj fail\n");
523                 return;
524             }
525             usbDriverInfo = std::static_pointer_cast<UsbDriverInfo>(tmpDrvInfo.driverInfoExt_);
526             if (usbDriverInfo == nullptr) {
527                 EDM_LOGE(MODULE_BUS_USB,  "static_pointer_cast UsbDriverInfo fail\n");
528                 return;
529             }
530             uint32_t versionCode = ParseVersionCode(driverInfos, bundleName);
531             std::vector<uint16_t> productIds = usbDriverInfo->GetProductIds();
532             std::vector<uint16_t> vendorIds = usbDriverInfo->GetVendorIds();
533             std::string pids = ParseIdVector(productIds);
534             std::string vids = ParseIdVector(vendorIds);
535             ExtDevReportSysEvent::ReportDriverPackageCycleMangeSysEvent(pkgInfoTable, pids, vids,
536                 versionCode, driverEventName);
537         }
538     }
539 }
540 
ParseIdVector(std::vector<uint16_t> ids)541 std::string DrvBundleStateCallback::ParseIdVector(std::vector<uint16_t> ids)
542 {
543     if (ids.size() < 1) {
544         return "";
545     }
546     std::string str = "";
547     auto it = ids.begin();
548     for (uint16_t id : ids) {
549         if (it + 1 == ids.end()) {
550             std::string copy = std::to_string(id);
551             str.append(copy);
552         } else {
553             std::string copy = std::to_string(id);
554             str.append(copy);
555             str.append(",");
556         }
557     }
558     return str;
559 }
560 
ParseVersionCode(const std::vector<ExtensionAbilityInfo> & driverInfos,const std::string & bundleName)561 int DrvBundleStateCallback::ParseVersionCode(const std::vector<ExtensionAbilityInfo> &driverInfos,
562     const std::string &bundleName)
563 {
564     uint32_t versionCode = 0;
565     for (const auto &driverInfo : driverInfos) {
566         if (driverInfo.bundleName == bundleName) {
567             versionCode = driverInfo.applicationInfo.versionCode;
568             break;
569         }
570     }
571     return versionCode;
572 }
573 }
574 }