• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022-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 "low_latency.h"
17 
18 #include <cinttypes>
19 
20 #include "res_sched_client.h"
21 #include "res_type.h"
22 
23 #include "constants.h"
24 #include "distributed_hardware_log.h"
25 #include "low_latency_timer.h"
26 
27 namespace OHOS {
28 namespace DistributedHardware {
29 #undef DH_LOG_TAG
30 #define DH_LOG_TAG "LowLatency"
31 IMPLEMENT_SINGLE_INSTANCE(LowLatency);
32 namespace {
33     const std::string LOW_LATENCY_TIMER_ID = "low_latency_timer_id";
34     const std::string LOW_LATENCY_KEY = "identity";
35     constexpr int32_t LOW_LATENCY_DELAY_MS = 50 * 1000;
36     constexpr int32_t MODE_DISABLE = 1;
37     constexpr uint32_t MAX_SWITCH_SIZE = 256;
38 }
39 
LowLatency()40 LowLatency::LowLatency() : lowLatencyTimer_(std::make_shared<LowLatencyTimer>(LOW_LATENCY_TIMER_ID,
41     LOW_LATENCY_DELAY_MS))
42 {
43     DHLOGI("LowLatency ctor!");
44 }
45 
~LowLatency()46 LowLatency::~LowLatency()
47 {
48     DHLOGI("LowLatency dtor!");
49 }
50 
EnableLowLatency(DHType dhType)51 void LowLatency::EnableLowLatency(DHType dhType)
52 {
53     DHLOGI("Start EnableLowLatency dhType: %{public}#X", dhType);
54     if (dhType <= DHType::UNKNOWN || dhType >= DHType::MAX_DH) {
55         DHLOGE("DHType is invalid, dhType: %{public}" PRIu32, (uint32_t)dhType);
56         return;
57     }
58     std::lock_guard<std::mutex> lock(lowLatencyMutex_);
59     DHLOGI("lowLatencySwitchSet size: %{public}zu", lowLatencySwitchSet_.size());
60     if (lowLatencySwitchSet_.empty() && lowLatencyTimer_ != nullptr) {
61         DHLOGD("Open LowLatency dhType: %{public}#X", dhType);
62         lowLatencyTimer_->StartTimer();
63     }
64     if (lowLatencySwitchSet_.size() >= MAX_SWITCH_SIZE) {
65         DHLOGE("lowLatencySwitchSet_ is oversize");
66         return;
67     }
68     lowLatencySwitchSet_.insert(dhType);
69     DHLOGI("End EnableLowLatency dhType: %{public}#X", dhType);
70 }
71 
DisableLowLatency(DHType dhType)72 void LowLatency::DisableLowLatency(DHType dhType)
73 {
74     DHLOGI("Start DisableLowLatency dhType: %{public}#X", dhType);
75     if (dhType <= DHType::UNKNOWN || dhType >= DHType::MAX_DH) {
76         DHLOGE("DHType is invalid, dhType: %{public}" PRIu32, (uint32_t)dhType);
77         return;
78     }
79     std::lock_guard<std::mutex> lock(lowLatencyMutex_);
80     lowLatencySwitchSet_.erase(dhType);
81     if (lowLatencySwitchSet_.empty() && lowLatencyTimer_ != nullptr) {
82         DHLOGD("Close LowLatency dhType: %{public}#X", dhType);
83         lowLatencyTimer_->StopTimer();
84     }
85     DHLOGI("End DisableLowLatency dhType: %{public}#X", dhType);
86 }
87 
CloseLowLatency()88 void LowLatency::CloseLowLatency()
89 {
90     DHLOGI("Shutdown LowLatency");
91     std::lock_guard<std::mutex> lock(lowLatencyMutex_);
92     lowLatencySwitchSet_.clear();
93     // to restore normal latency mode: value = 1
94     OHOS::ResourceSchedule::ResSchedClient::GetInstance().ReportData(
95         OHOS::ResourceSchedule::ResType::RES_TYPE_NETWORK_LATENCY_REQUEST, MODE_DISABLE,
96         {{LOW_LATENCY_KEY, DH_FWK_PKG_NAME}});
97 }
98 } // namespace DistributedHardware
99 } // namespace OHOS