1 /*
2 * Copyright (c) 2024 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 <cstdint>
17 #include <iomanip>
18 #include <sstream>
19 #include "drm_dfx_utils.h"
20 #include "drm_log.h"
21 #include "iservice_registry.h"
22 #include "bundle_mgr_interface.h"
23 #include "bundle_mgr_proxy.h"
24 #include "system_ability_definition.h"
25 
26 namespace OHOS {
27 namespace DrmStandard {
GetClientBundleName(int32_t uid)28 std::string __attribute__((visibility("default"))) GetClientBundleName(int32_t uid)
29 {
30     std::string bundleName = "";
31     auto samgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
32     if (samgr == nullptr) {
33         DRM_ERR_LOG("Get ability manager failed");
34         return bundleName;
35     }
36 
37     sptr<IRemoteObject> object = samgr->GetSystemAbility(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID);
38     if (object == nullptr) {
39         DRM_ERR_LOG("object is NULL.");
40         return bundleName;
41     }
42 
43     sptr<OHOS::AppExecFwk::IBundleMgr> bms = iface_cast<OHOS::AppExecFwk::IBundleMgr>(object);
44     if (bms == nullptr) {
45         DRM_ERR_LOG("bundle manager service is NULL.");
46         return bundleName;
47     }
48 
49     auto result = bms->GetNameForUid(uid, bundleName);
50     if (result != ERR_OK) {
51         DRM_ERR_LOG("GetBundleNameForUid fail");
52         return "";
53     }
54 
55     DRM_INFO_LOG("BundleName:%{public}s", bundleName.c_str());
56 
57     return bundleName;
58 }
59 
CastToHexString(std::vector<uint8_t> binaryData)60 std::string __attribute__((visibility("default"))) CastToHexString(std::vector<uint8_t> binaryData)
61 {
62     std::string hexString;
63     for (uint8_t binary : binaryData) {
64         std::stringstream stream;
65         stream << std::hex << std::setw(minimumDigit) << std::setfill('0') << static_cast<int>(binary);
66         hexString += stream.str();
67     }
68     return hexString;
69 }
70 
CalculateTimeDiff(std::chrono::system_clock::time_point timeBefore,std::chrono::system_clock::time_point timeAfter)71 uint32_t __attribute__((visibility("default"))) CalculateTimeDiff(std::chrono::system_clock::time_point timeBefore,
72     std::chrono::system_clock::time_point timeAfter)
73 {
74     auto duration = timeAfter - timeBefore;
75     auto milliseconds = std::chrono::duration_cast<std::chrono::milliseconds>(duration);
76     return static_cast<uint32_t>(milliseconds.count());
77 }
78 }  // namespace DrmStandard
79 }  // namespace OHOS