• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 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 <cinttypes>
17 
18 #include "constants.h"
19 #include "dh_utils_tool.h"
20 #include "distributed_hardware_errno.h"
21 #include "distributed_hardware_log.h"
22 #include "low_latency_listener.h"
23 
24 namespace OHOS {
25 namespace DistributedHardware {
26 #undef DH_LOG_TAG
27 #define DH_LOG_TAG "LowLatencyListener"
28 
LowLatencyListener()29 LowLatencyListener::LowLatencyListener()
30 {
31     DHLOGI("LowLatencyListener ctor!");
32 }
33 
~LowLatencyListener()34 LowLatencyListener::~LowLatencyListener()
35 {
36     DHLOGI("LowLatencyListener dtor!");
37 }
38 
OnMessage(const DHTopic topic,const std::string & message)39 void LowLatencyListener::OnMessage(const DHTopic topic, const std::string& message)
40 {
41     (void) topic;
42     (void) message;
43 #ifdef DHARDWARE_LOW_LATENCY
44     if (topic <= DHTopic::TOPIC_MIN || topic >= DHTopic::TOPIC_MAX) {
45         DHLOGE("Topic is invalid, topic: %" PRIu32, (uint32_t)topic);
46         return;
47     }
48     if (message.size() == 0 || message.size() > MAX_MESSAGE_LEN) {
49         DHLOGE("Message is invalid");
50         return;
51     }
52     nlohmann::json jsonObj = nlohmann::json::parse(message, nullptr, false);
53     if (jsonObj.is_discarded()) {
54         DHLOGE("jsonStr parse failed");
55         return;
56     }
57     if (!IsUInt32(jsonObj, DH_TYPE)) {
58         DHLOGE("The DH_TYPE key is invalid");
59         return;
60     }
61     if (!IsBool(jsonObj, LOW_LATENCY_ENABLE)) {
62         DHLOGE("The LOW_LATENCY_ENABLE key is invalid");
63         return;
64     }
65     DHType dhType = jsonObj[DH_TYPE];
66     bool isEnable = jsonObj[LOW_LATENCY_ENABLE];
67     if (isEnable) {
68         LowLatency::GetInstance().EnableLowLatency(dhType);
69     } else {
70         LowLatency::GetInstance().DisableLowLatency(dhType);
71     }
72 #endif
73 }
74 
AsObject()75 sptr<IRemoteObject> LowLatencyListener::AsObject()
76 {
77     return nullptr;
78 }
79 } // namespace DistributedHardware
80 } // namespace OHOS
81