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 <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: %{public}" PRIu32, (uint32_t)topic);
46         return;
47     }
48     if (!IsMessageLengthValid(message)) {
49         return;
50     }
51     cJSON *jsonObj = cJSON_Parse(message.c_str());
52     if (jsonObj == NULL) {
53         DHLOGE("jsonStr parse failed");
54         return;
55     }
56     if (!IsUInt32(jsonObj, DH_TYPE)) {
57         DHLOGE("The DH_TYPE key is invalid!");
58         cJSON_Delete(jsonObj);
59         return;
60     }
61     if (!IsBool(jsonObj, LOW_LATENCY_ENABLE)) {
62         DHLOGE("The LOW_LATENCY_ENABLE key is invalid!");
63         cJSON_Delete(jsonObj);
64         return;
65     }
66     DHType dhType = (DHType)cJSON_GetObjectItem(jsonObj, DH_TYPE.c_str())->valuedouble;
67     cJSON *isEnable = cJSON_GetObjectItem(jsonObj, LOW_LATENCY_ENABLE.c_str());
68     if (cJSON_IsTrue(isEnable)) {
69         LowLatency::GetInstance().EnableLowLatency(dhType);
70     } else {
71         LowLatency::GetInstance().DisableLowLatency(dhType);
72     }
73     cJSON_Delete(jsonObj);
74 #endif
75 }
76 
AsObject()77 sptr<IRemoteObject> LowLatencyListener::AsObject()
78 {
79     return nullptr;
80 }
81 } // namespace DistributedHardware
82 } // namespace OHOS
83