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