1 /*
2 * Copyright (c) 2021 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 #include "sensor_hdi_connection.h"
16
17 #include "compatible_connection.h"
18 #include "hdi_connection.h"
19 #include "hitrace_meter.h"
20 #include "sensors_errors.h"
21
22 namespace OHOS {
23 namespace Sensors {
24 using namespace OHOS::HiviewDFX;
25 namespace {
26 constexpr HiLogLabel LABEL = { LOG_CORE, SENSOR_LOG_DOMAIN, "SensorHdiConnection" };
27 constexpr float MAX_RANGE = 9999.0;
28 constexpr float POWER = 20.0;
29 constexpr float RESOLITION = 0.000001;
30 constexpr float MIN_SAMPLE_PERIOD_NS = 100000000;
31 constexpr float MAX_SAMPLE_PERIOD_NS = 1000000000;
32 const std::string VERSION_NAME = "1.0.1";
33 }
34
ConnectHdi()35 int32_t SensorHdiConnection::ConnectHdi()
36 {
37 iSensorHdiConnection_ = std::make_unique<HdiConnection>();
38 int32_t ret = ConnectHdiService();
39 if (ret != ERR_OK) {
40 existColorAndSar_ = false;
41 SEN_HILOGE("Connect hdi service failed, try to connect compatible connection");
42 iSensorHdiConnection_ = std::make_unique<CompatibleConnection>();
43 ret = ConnectHdiService();
44 if (ret != ERR_OK) {
45 SEN_HILOGE("Connect compatible connection failed, ret:%{public}d", ret);
46 return ret;
47 }
48 ret = ConnectCompatibleHdi();
49 if (ret != ERR_OK) {
50 SEN_HILOGE("Connect color and sar compatible connection failed, ret:%{public}d", ret);
51 }
52 return ret;
53 }
54 if (!ExistSensor(sensorList_, SENSOR_TYPE_ID_COLOR) || !ExistSensor(sensorList_, SENSOR_TYPE_ID_SAR)) {
55 existColorAndSar_ = false;
56 ret = ConnectCompatibleHdi();
57 if (ret != ERR_OK) {
58 SEN_HILOGE("Connect color and sar compatible hdi failed, ret:%{public}d", ret);
59 }
60 return ret;
61 }
62 existColorAndSar_ = true;
63 return ret;
64 }
65
ConnectHdiService()66 int32_t SensorHdiConnection::ConnectHdiService()
67 {
68 int32_t ret = iSensorHdiConnection_->ConnectHdi();
69 if (ret != 0) {
70 SEN_HILOGE("Connect hdi service failed");
71 return CONNECT_SENSOR_HDF_ERR;
72 }
73 ret = iSensorHdiConnection_->GetSensorList(sensorList_);
74 if (ret != 0) {
75 SEN_HILOGE("Get sensor list failed");
76 return GET_SENSOR_LIST_ERR;
77 }
78 return ERR_OK;
79 }
80
ConnectCompatibleHdi()81 int32_t SensorHdiConnection::ConnectCompatibleHdi()
82 {
83 if (iSensorCompatibleHdiConnection_ == nullptr) {
84 iSensorCompatibleHdiConnection_ = std::make_unique<CompatibleConnection>();
85 }
86 int32_t ret = iSensorCompatibleHdiConnection_->ConnectHdi();
87 if (ret != ERR_OK) {
88 SEN_HILOGE("Connect hdi compatible service failed");
89 return CONNECT_SENSOR_HDF_ERR;
90 }
91 return ERR_OK;
92 }
93
ExistSensor(const std::vector<Sensor> & sensorList,int32_t sensorId)94 bool SensorHdiConnection::ExistSensor(const std::vector<Sensor>& sensorList, int32_t sensorId)
95 {
96 return std::any_of(sensorList.begin(), sensorList.end(), [sensorId] (const Sensor &sensor) {
97 return sensor.GetSensorId() == sensorId;
98 });
99 }
100
GetSensorList(std::vector<Sensor> & sensorList)101 int32_t SensorHdiConnection::GetSensorList(std::vector<Sensor>& sensorList)
102 {
103 sensorList.assign(sensorList_.begin(), sensorList_.end());
104 if (existColorAndSar_) {
105 return ERR_OK;
106 }
107 Sensor sensorColor;
108 sensorColor.SetSensorId(SENSOR_TYPE_ID_COLOR);
109 sensorColor.SetSensorTypeId(SENSOR_TYPE_ID_COLOR);
110 sensorColor.SetFirmwareVersion(VERSION_NAME);
111 sensorColor.SetHardwareVersion(VERSION_NAME);
112 sensorColor.SetMaxRange(MAX_RANGE);
113 sensorColor.SetSensorName("sensor_color");
114 sensorColor.SetVendorName("default_color");
115 sensorColor.SetResolution(RESOLITION);
116 sensorColor.SetPower(POWER);
117 sensorColor.SetMinSamplePeriodNs(MIN_SAMPLE_PERIOD_NS);
118 sensorColor.SetMaxSamplePeriodNs(MAX_SAMPLE_PERIOD_NS);
119 sensorList.push_back(sensorColor);
120 Sensor sensorSar;
121 sensorSar.SetSensorId(SENSOR_TYPE_ID_SAR);
122 sensorSar.SetSensorTypeId(SENSOR_TYPE_ID_SAR);
123 sensorSar.SetFirmwareVersion(VERSION_NAME);
124 sensorSar.SetHardwareVersion(VERSION_NAME);
125 sensorSar.SetMaxRange(MAX_RANGE);
126 sensorSar.SetSensorName("sensor_sar");
127 sensorSar.SetVendorName("default_sar");
128 sensorSar.SetResolution(RESOLITION);
129 sensorSar.SetPower(POWER);
130 sensorSar.SetMinSamplePeriodNs(MIN_SAMPLE_PERIOD_NS);
131 sensorSar.SetMaxSamplePeriodNs(MAX_SAMPLE_PERIOD_NS);
132 sensorList.push_back(sensorSar);
133 return ERR_OK;
134 }
135
EnableSensor(int32_t sensorId)136 int32_t SensorHdiConnection::EnableSensor(int32_t sensorId)
137 {
138 StartTrace(HITRACE_TAG_SENSORS, "EnableSensor");
139 int32_t ret = ENABLE_SENSOR_ERR;
140 if (!existColorAndSar_ && (sensorId == SENSOR_TYPE_ID_COLOR || sensorId == SENSOR_TYPE_ID_SAR)) {
141 CHKPR(iSensorCompatibleHdiConnection_, ENABLE_SENSOR_ERR);
142 ret = iSensorCompatibleHdiConnection_->EnableSensor(sensorId);
143 FinishTrace(HITRACE_TAG_SENSORS);
144 if (ret != ERR_OK) {
145 SEN_HILOGE("Enable sensor failed in compatible, sensorId:%{public}d", sensorId);
146 return ENABLE_SENSOR_ERR;
147 }
148 return ret;
149 }
150 CHKPR(iSensorHdiConnection_, ENABLE_SENSOR_ERR);
151 ret = iSensorHdiConnection_->EnableSensor(sensorId);
152 FinishTrace(HITRACE_TAG_SENSORS);
153 if (ret != 0) {
154 SEN_HILOGI("Enable sensor failed, sensorId:%{public}d", sensorId);
155 return ENABLE_SENSOR_ERR;
156 }
157 return ret;
158 };
159
DisableSensor(int32_t sensorId)160 int32_t SensorHdiConnection::DisableSensor(int32_t sensorId)
161 {
162 StartTrace(HITRACE_TAG_SENSORS, "DisableSensor");
163 int32_t ret = DISABLE_SENSOR_ERR;
164 if (!existColorAndSar_ && (sensorId == SENSOR_TYPE_ID_COLOR || sensorId == SENSOR_TYPE_ID_SAR)) {
165 CHKPR(iSensorCompatibleHdiConnection_, DISABLE_SENSOR_ERR);
166 ret = iSensorCompatibleHdiConnection_->DisableSensor(sensorId);
167 FinishTrace(HITRACE_TAG_SENSORS);
168 if (ret != ERR_OK) {
169 SEN_HILOGE("Disable sensor failed in compatible, sensorId:%{public}d", sensorId);
170 return DISABLE_SENSOR_ERR;
171 }
172 return ret;
173 }
174 CHKPR(iSensorHdiConnection_, DISABLE_SENSOR_ERR);
175 ret = iSensorHdiConnection_->DisableSensor(sensorId);
176 FinishTrace(HITRACE_TAG_SENSORS);
177 if (ret != 0) {
178 SEN_HILOGI("Disable sensor failed, sensorId:%{public}d", sensorId);
179 return DISABLE_SENSOR_ERR;
180 }
181 return ret;
182 }
183
SetBatch(int32_t sensorId,int64_t samplingInterval,int64_t reportInterval)184 int32_t SensorHdiConnection::SetBatch(int32_t sensorId, int64_t samplingInterval, int64_t reportInterval)
185 {
186 StartTrace(HITRACE_TAG_SENSORS, "SetBatch");
187 int32_t ret = SET_SENSOR_CONFIG_ERR;
188 if (!existColorAndSar_ && (sensorId == SENSOR_TYPE_ID_COLOR || sensorId == SENSOR_TYPE_ID_SAR)) {
189 CHKPR(iSensorCompatibleHdiConnection_, SET_SENSOR_CONFIG_ERR);
190 ret = iSensorCompatibleHdiConnection_->SetBatch(sensorId, samplingInterval, reportInterval);
191 FinishTrace(HITRACE_TAG_SENSORS);
192 if (ret != ERR_OK) {
193 SEN_HILOGI("Set batch failed in compatible, sensorId:%{public}d", sensorId);
194 return SET_SENSOR_CONFIG_ERR;
195 }
196 return ret;
197 }
198 CHKPR(iSensorHdiConnection_, SET_SENSOR_CONFIG_ERR);
199 ret = iSensorHdiConnection_->SetBatch(sensorId, samplingInterval, reportInterval);
200 FinishTrace(HITRACE_TAG_SENSORS);
201 if (ret != 0) {
202 SEN_HILOGI("Set batch failed, sensorId:%{public}d", sensorId);
203 return SET_SENSOR_CONFIG_ERR;
204 }
205 return ret;
206 }
207
SetMode(int32_t sensorId,int32_t mode)208 int32_t SensorHdiConnection::SetMode(int32_t sensorId, int32_t mode)
209 {
210 StartTrace(HITRACE_TAG_SENSORS, "SetMode");
211 CHKPR(iSensorHdiConnection_, SET_SENSOR_MODE_ERR);
212 int32_t ret = iSensorHdiConnection_->SetMode(sensorId, mode);
213 FinishTrace(HITRACE_TAG_SENSORS);
214 if (ret != 0) {
215 SEN_HILOGI("Set mode failed, sensorId:%{public}d", sensorId);
216 return SET_SENSOR_MODE_ERR;
217 }
218 return ret;
219 }
220
RegisterDataReport(ReportDataCb cb,sptr<ReportDataCallback> reportDataCallback)221 int32_t SensorHdiConnection::RegisterDataReport(ReportDataCb cb, sptr<ReportDataCallback> reportDataCallback)
222 {
223 StartTrace(HITRACE_TAG_SENSORS, "RegisterDataReport");
224 CHKPR(iSensorHdiConnection_, REGIST_CALLBACK_ERR);
225 int32_t ret = iSensorHdiConnection_->RegisterDataReport(cb, reportDataCallback);
226 CHKPR(iSensorCompatibleHdiConnection_, REGIST_CALLBACK_ERR);
227 int32_t compatibleRet = iSensorCompatibleHdiConnection_->RegisterDataReport(cb, reportDataCallback);
228 if (compatibleRet != ERR_OK) {
229 SEN_HILOGE("Registe dataReport failed in compatible");
230 }
231 FinishTrace(HITRACE_TAG_SENSORS);
232 if (ret != 0) {
233 SEN_HILOGI("Registe dataReport failed");
234 return REGIST_CALLBACK_ERR;
235 }
236 return ret;
237 }
238
DestroyHdiConnection()239 int32_t SensorHdiConnection::DestroyHdiConnection()
240 {
241 CHKPR(iSensorHdiConnection_, DEVICE_ERR);
242 int32_t ret = iSensorHdiConnection_->DestroyHdiConnection();
243 if (ret != 0) {
244 SEN_HILOGI("Destroy hdi connection failed");
245 return DEVICE_ERR;
246 }
247 CHKPR(iSensorCompatibleHdiConnection_, DEVICE_ERR);
248 int32_t compatibleRet = iSensorCompatibleHdiConnection_->DestroyHdiConnection();
249 if (compatibleRet != ERR_OK) {
250 SEN_HILOGE("Destroy hdi connection failed in compatible");
251 }
252 return ret;
253 }
254 } // namespace Sensors
255 } // namespace OHOS
256