1 /*
2  * Copyright (c) 2022-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 "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     constexpr int32_t LOW_LATENCY_DELAY_MS = 50 * 1000;
35 }
36 
LowLatency()37 LowLatency::LowLatency() : lowLatencyTimer_(std::make_shared<LowLatencyTimer>(LOW_LATENCY_TIMER_ID,
38     LOW_LATENCY_DELAY_MS))
39 {
40     DHLOGI("LowLatency ctor!");
41 }
42 
~LowLatency()43 LowLatency::~LowLatency()
44 {
45     DHLOGI("LowLatency dtor!");
46 }
47 
EnableLowLatency(DHType dhType)48 void LowLatency::EnableLowLatency(DHType dhType)
49 {
50     DHLOGI("Start EnableLowLatency dhType: %{public}#X", dhType);
51     if (dhType <= DHType::UNKNOWN || dhType >= DHType::MAX_DH) {
52         DHLOGE("DHType is invalid, dhType: %{public}" PRIu32, (uint32_t)dhType);
53         return;
54     }
55     std::lock_guard<std::mutex> lock(lowLatencyMutex_);
56     DHLOGI("lowLatencySwitchSet size: %{public}zu", lowLatencySwitchSet_.size());
57     if (lowLatencySwitchSet_.empty() && lowLatencyTimer_ != nullptr) {
58         DHLOGD("Open LowLatency dhType: %{public}#X", dhType);
59         lowLatencyTimer_->StartTimer();
60     }
61     if (lowLatencySwitchSet_.size() >= MAX_SWITCH_SIZE) {
62         DHLOGE("lowLatencySwitchSet_ is oversize");
63         return;
64     }
65     lowLatencySwitchSet_.insert(dhType);
66     DHLOGI("End EnableLowLatency dhType: %{public}#X", dhType);
67 }
68 
DisableLowLatency(DHType dhType)69 void LowLatency::DisableLowLatency(DHType dhType)
70 {
71     DHLOGI("Start DisableLowLatency dhType: %{public}#X", dhType);
72     if (dhType <= DHType::UNKNOWN || dhType >= DHType::MAX_DH) {
73         DHLOGE("DHType is invalid, dhType: %{public}" PRIu32, (uint32_t)dhType);
74         return;
75     }
76     std::lock_guard<std::mutex> lock(lowLatencyMutex_);
77     lowLatencySwitchSet_.erase(dhType);
78     if (lowLatencySwitchSet_.empty() && lowLatencyTimer_ != nullptr) {
79         DHLOGD("Close LowLatency dhType: %{public}#X", dhType);
80         lowLatencyTimer_->StopTimer();
81     }
82     DHLOGI("End DisableLowLatency dhType: %{public}#X", dhType);
83 }
84 
CloseLowLatency()85 void LowLatency::CloseLowLatency()
86 {
87     DHLOGI("Shutdown LowLatency");
88     std::lock_guard<std::mutex> lock(lowLatencyMutex_);
89     lowLatencySwitchSet_.clear();
90     // to restore normal latency mode: value = 1
91     OHOS::ResourceSchedule::ResSchedClient::GetInstance().ReportData(
92         OHOS::ResourceSchedule::ResType::RES_TYPE_NETWORK_LATENCY_REQUEST, MODE_DISABLE,
93         {{LOW_LATENCY_KEY, DH_FWK_PKG_NAME}});
94 }
95 } // namespace DistributedHardware
96 } // namespace OHOS