• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 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 "report_data_utils.h"
17 
18 #include <dlfcn.h>
19 #include "standby_service_log.h"
20 
21 namespace OHOS {
22 namespace DevStandbyMgr {
23 namespace {
24     const std::string RES_SCHED_SERVICE_SO = "libresschedsvc.z.so";
25 }
26 
ReportDataUtils()27 ReportDataUtils::ReportDataUtils()
28 {
29     LoadUtils();
30 }
31 
~ReportDataUtils()32 ReportDataUtils::~ReportDataUtils()
33 {
34     reportFunc_ = nullptr;
35     if (handle_) {
36         dlclose(handle_);
37         handle_ = nullptr;
38     }
39 }
40 
GetInstance()41 ReportDataUtils& ReportDataUtils::GetInstance()
42 {
43     static ReportDataUtils instance;
44     return instance;
45 }
46 
LoadUtils()47 void ReportDataUtils::LoadUtils()
48 {
49     handle_ = dlopen(RES_SCHED_SERVICE_SO.c_str(), RTLD_NOW);
50     if (!handle_) {
51         STANDBYSERVICE_LOGW("%{public}s load %{public}s failed!", __func__, RES_SCHED_SERVICE_SO.c_str());
52         return;
53     }
54 
55     reportFunc_ = reinterpret_cast<ReportDataFunc>(dlsym(handle_, "ReportDataInProcess"));
56     if (!reportFunc_) {
57         STANDBYSERVICE_LOGW("%{public}s load function:ReportDataInProcess failed!", __func__);
58         dlclose(handle_);
59         return;
60     }
61 }
62 
ReportDataInProcess(uint32_t resType,int64_t value,const nlohmann::json & payload)63 void ReportDataUtils::ReportDataInProcess(uint32_t resType, int64_t value, const nlohmann::json& payload)
64 {
65     if (!reportFunc_) {
66         STANDBYSERVICE_LOGD("%{public}s failed, function nullptr.", __func__);
67         return;
68     }
69     reportFunc_(resType, value, payload);
70 }
71 
72 } // namespace DevStandbyMgr
73 } // namespace OHOS