1 /*
2 * Copyright (c) 2022 Chipsea Technologies (Shenzhen) Corp., 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 "medical_errors.h"
20 #include "medical_log_domain.h"
21
22 namespace OHOS {
23 namespace Sensors {
24 using namespace OHOS::HiviewDFX;
25
26 namespace {
27 constexpr HiLogLabel LABEL = {
28 LOG_CORE, MedicalSensorLogDomain::MEDICAL_SENSOR_HDI_INTERFACE, "MedicalSensor_SensorHdiConnection"
29 };
30 }
31
ConnectHdi()32 int32_t SensorHdiConnection::ConnectHdi()
33 {
34 iSensorHdiConnection_ = std::make_unique<HdiConnection>();
35 int32_t ret = connectHdiService();
36 if (ret != ERR_OK) {
37 HiLog::Error(LABEL, "%{public}s connect hdi service failed, try to connect compatible connection",
38 __func__);
39 iSensorHdiConnection_ = std::make_unique<CompatibleConnection>();
40 ret = connectHdiService();
41 }
42 if (ret != ERR_OK) {
43 HiLog::Error(LABEL, "%{public}s connect hdi failed", __func__);
44 }
45 return ERR_OK;
46 }
47
connectHdiService()48 int32_t SensorHdiConnection::connectHdiService()
49 {
50 int32_t ret = iSensorHdiConnection_->ConnectHdi();
51 if (ret != 0) {
52 HiLog::Error(LABEL, "%{public}s connect hdi service failed", __func__);
53 return CONNECT_SENSOR_HDF_ERR;
54 }
55 ret = iSensorHdiConnection_->GetSensorList(sensorList_);
56 if (ret != 0) {
57 HiLog::Error(LABEL, "%{public}s get sensor list failed", __func__);
58 return GET_SENSOR_LIST_ERR;
59 }
60 return ERR_OK;
61 }
62
GetSensorList(std::vector<MedicalSensor> & sensorList)63 int32_t SensorHdiConnection::GetSensorList(std::vector<MedicalSensor>& sensorList)
64 {
65 sensorList.assign(sensorList_.begin(), sensorList_.end());
66 return ERR_OK;
67 }
68
EnableSensor(int32_t sensorId)69 int32_t SensorHdiConnection::EnableSensor(int32_t sensorId)
70 {
71 int32_t ret = iSensorHdiConnection_->EnableSensor(sensorId);
72 if (ret != 0) {
73 HiLog::Info(LABEL, "%{public}s enable sensor failed, sensorId: %{public}d", __func__, sensorId);
74 return ENABLE_SENSOR_ERR;
75 }
76 return ret;
77 };
78
DisableSensor(int32_t sensorId)79 int32_t SensorHdiConnection::DisableSensor(int32_t sensorId)
80 {
81 int32_t ret = iSensorHdiConnection_->DisableSensor(sensorId);
82 if (ret != 0) {
83 HiLog::Info(LABEL, "%{public}s disable sensor failed, sensorId: %{public}d", __func__, sensorId);
84 return DISABLE_SENSOR_ERR;
85 }
86 return ret;
87 }
88
SetBatch(int32_t sensorId,int64_t samplingInterval,int64_t reportInterval)89 int32_t SensorHdiConnection::SetBatch(int32_t sensorId, int64_t samplingInterval, int64_t reportInterval)
90 {
91 int32_t ret = iSensorHdiConnection_->SetBatch(sensorId, samplingInterval, reportInterval);
92 if (ret != 0) {
93 HiLog::Info(LABEL, "%{public}s set batch failed, sensorId: %{public}d", __func__, sensorId);
94 return SET_SENSOR_CONFIG_ERR;
95 }
96 return ret;
97 }
98
SetMode(int32_t sensorId,int32_t mode)99 int32_t SensorHdiConnection::SetMode(int32_t sensorId, int32_t mode)
100 {
101 int32_t ret = iSensorHdiConnection_->SetMode(sensorId, mode);
102 if (ret != 0) {
103 HiLog::Info(LABEL, "%{public}s set mode failed, sensorId: %{public}d", __func__, sensorId);
104 return SET_SENSOR_MODE_ERR;
105 }
106 return ret;
107 }
108
SetOption(int32_t sensorId,int32_t option)109 int32_t SensorHdiConnection::SetOption(int32_t sensorId, int32_t option)
110 {
111 int32_t ret = iSensorHdiConnection_->SetOption(sensorId, option);
112 if (ret != 0) {
113 HiLog::Info(LABEL, "%{public}s set option failed, sensorId: %{public}d", __func__, sensorId);
114 return SET_SENSOR_OPTION_ERR;
115 }
116 return ret;
117 }
118
RunCommand(int32_t sensorId,int32_t cmd,int32_t params)119 int32_t SensorHdiConnection::RunCommand(int32_t sensorId, int32_t cmd, int32_t params)
120 {
121 int32_t ret = iSensorHdiConnection_->RunCommand(sensorId, cmd, params);
122 if (ret != 0) {
123 HiLog::Info(LABEL, "%{public}s run command failed, sensorId: %{public}d", __func__, sensorId);
124 return RUN_COMMAND_ERR;
125 }
126 return ret;
127 }
128
RegisteDataReport(DataCacheFunc cacheData,sptr<ReportDataCache> reportDataCache)129 int32_t SensorHdiConnection::RegisteDataReport(DataCacheFunc cacheData, sptr<ReportDataCache> reportDataCache)
130 {
131 int32_t ret = iSensorHdiConnection_->RegisteDataReport(cacheData, reportDataCache);
132 if (ret != 0) {
133 HiLog::Info(LABEL, "%{public}s registe dataReport failed", __func__);
134 return REGIST_CALLBACK_ERR;
135 }
136 return ret;
137 }
138
DestroyHdiConnection()139 int32_t SensorHdiConnection::DestroyHdiConnection()
140 {
141 int32_t ret = iSensorHdiConnection_->DestroyHdiConnection();
142 if (ret != 0) {
143 HiLog::Info(LABEL, "%{public}s destroy hdi connectionr failed", __func__);
144 return DEVICE_ERR;
145 }
146 return ret;
147 }
148 } // namespace Sensors
149 } // namespace OHOS
150