1 /*
2  * Copyright (c) 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 "infrared_emitter_controller.h"
17 
18 #include <dlfcn.h>
19 
20 #include "mmi_log.h"
21 
22 #undef MMI_LOG_DOMAIN
23 #define MMI_LOG_DOMAIN MMI_LOG_SERVER
24 #undef MMI_LOG_TAG
25 #define MMI_LOG_TAG "InfraredEmitterController"
26 
27 namespace OHOS {
28 namespace MMI {
29 namespace {
30     const std::string IR_WRAPPER_PATH { "libconsumer_ir_service_1.0.z.so" };
31 }
32 using namespace OHOS::HDI::V1_0;
33 InfraredEmitterController *InfraredEmitterController::instance_ = new (std::nothrow) InfraredEmitterController();
InfraredEmitterController()34 InfraredEmitterController::InfraredEmitterController() {}
35 
~InfraredEmitterController()36 InfraredEmitterController::~InfraredEmitterController()
37 {
38     CALL_DEBUG_ENTER;
39     irInterface_ = nullptr;
40     if (soIrHandle_ != nullptr) {
41         dlclose(soIrHandle_);
42         soIrHandle_ = nullptr;
43     }
44 }
45 
GetInstance()46 InfraredEmitterController *InfraredEmitterController::GetInstance()
47 {
48     return instance_;
49 }
50 
InitInfraredEmitter()51 void InfraredEmitterController::InitInfraredEmitter()
52 {
53     CALL_DEBUG_ENTER;
54     if (irInterface_ != nullptr) {
55         return;
56     }
57     if (soIrHandle_ == nullptr) {
58         soIrHandle_ = dlopen(IR_WRAPPER_PATH.c_str(), RTLD_NOW);
59         if (soIrHandle_ == nullptr) {
60             MMI_HILOGE("Loaded %{public}s failed:%{public}s", IR_WRAPPER_PATH.c_str(), dlerror());
61             return;
62         }
63     }
64     typedef ConsumerIr* (*funCreate_ptr) (void);
65     funCreate_ptr fnCreate = nullptr;
66     fnCreate = (funCreate_ptr)dlsym(soIrHandle_, "ConsumerIrImplGetInstance");
67     const char *dlsymError = dlerror();
68     if (dlsymError != nullptr) {
69         MMI_HILOGE("Loaded ConsumerIrImplGetInstance failed:%{public}s", dlsymError);
70         dlclose(soIrHandle_);
71         soIrHandle_ = nullptr;
72         return;
73     }
74     if (fnCreate == nullptr) {
75         MMI_HILOGE("Loaded ConsumerIrImplGetInstance is null");
76         dlclose(soIrHandle_);
77         soIrHandle_ = nullptr;
78         return;
79     }
80     MMI_HILOGI("Infrared emitter call ConsumerIr:fnCreate begin");
81     irInterface_ = (ConsumerIr *)fnCreate();
82     if (irInterface_ == nullptr) {
83         MMI_HILOGE("Infrared emitter init fail irInterface_ is nullptr");
84         dlclose(soIrHandle_);
85         soIrHandle_ = nullptr;
86         return;
87     }
88 }
89 
Transmit(int64_t carrierFreq,const std::vector<int64_t> pattern)90 bool InfraredEmitterController::Transmit(int64_t carrierFreq, const std::vector<int64_t> pattern)
91 {
92     CALL_DEBUG_ENTER;
93     InitInfraredEmitter();
94     CHKPF(irInterface_);
95     int32_t tempCarrierFreq = carrierFreq;
96     std::vector<int32_t> tempPattern;
97     std::string context = "infraredFrequency:" + std::to_string(tempCarrierFreq) + ";";
98     for (size_t i = 0; i < pattern.size(); i++) {
99         int32_t per = pattern[i];
100         context = context + "index:" + std::to_string(i) + ": pattern:" + std::to_string(per) + ";";
101         tempPattern.push_back(per);
102     }
103     MMI_HILOGI("irInterface_->Transmit params:%{public}s", context.c_str());
104 
105     bool outRet = false;
106     int32_t ret = irInterface_->Transmit(tempCarrierFreq, tempPattern, outRet);
107     MMI_HILOGI("irInterface_->Transmit ret:%{public}d", ret);
108     if (ret < 0) {
109         MMI_HILOGE("Infrared emitter transmit failed:%{public}d", ret);
110         return false;
111     }
112     if (!outRet) {
113         MMI_HILOGE("Infrared emitter transmit out false");
114         return false;
115     }
116     return true;
117 }
118 
GetFrequencies(std::vector<InfraredFrequencyInfo> & frequencyInfo)119 bool InfraredEmitterController::GetFrequencies(std::vector<InfraredFrequencyInfo> &frequencyInfo)
120 {
121     CALL_DEBUG_ENTER;
122     InitInfraredEmitter();
123     if (!irInterface_) {
124         MMI_HILOGE("Infrared emitter not init");
125         return false;
126     }
127     bool outRet = false;
128     std::vector<ConsumerIrFreqRange> outRange;
129     MMI_HILOGI("irInterface_->GetCarrierFreqs");
130     int32_t ret = irInterface_->GetCarrierFreqs(outRet, outRange);
131     MMI_HILOGI("irInterface_->GetCarrierFreqs ret:%{public}d", ret);
132     if (ret < 0) {
133         MMI_HILOGE("Infrared emitter GetCarrierFreqs failed:%{public}d", ret);
134         return false;
135     }
136     if (!outRet) {
137         MMI_HILOGE("Infrared emitter GetCarrierFreqs out false");
138         return false;
139     }
140     std::string context = "size:" + std::to_string(outRange.size()) + ";";
141     for (size_t i = 0; i < outRange.size(); i++) {
142         InfraredFrequencyInfo item;
143         context = context + "index:" + std::to_string(i) + ": per.max:" + std::to_string(outRange[i].max) +
144                   ": per.min:" + std::to_string(outRange[i].min) + ";;";
145         item.max_ = outRange[i].max;
146         item.min_ = outRange[i].min;
147         frequencyInfo.push_back(item);
148     }
149     return true;
150 }
151 } // namespace MMI
152 } // namespace OHOS
153 
154