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 "compatible_connection.h"
16
17 #include "securec.h"
18 #include "sensor_errors.h"
19
20 #undef LOG_TAG
21 #define LOG_TAG "CompatibleConnection"
22
23 namespace OHOS {
24 namespace Sensors {
25 using namespace OHOS::HiviewDFX;
26
27 ReportDataCb CompatibleConnection::reportDataCb_ = nullptr;
28 sptr<ReportDataCallback> CompatibleConnection::reportDataCallback_ = nullptr;
ConnectHdi()29 int32_t CompatibleConnection::ConnectHdi()
30 {
31 SEN_HILOGI("Connect hdi success");
32 return ERR_OK;
33 }
34
GetSensorList(std::vector<Sensor> & sensorList)35 int32_t CompatibleConnection::GetSensorList(std::vector<Sensor> &sensorList)
36 {
37 std::vector<SensorInfo> sensorInfos;
38 int32_t ret = hdiServiceImpl_.GetSensorList(sensorInfos);
39 if (ret != 0) {
40 SEN_HILOGE("Get sensor list failed");
41 return ret;
42 }
43 size_t count = sensorInfos.size();
44 if (count > MAX_SENSOR_COUNT) {
45 SEN_HILOGD("SensorInfos size:%{public}zu", count);
46 count = MAX_SENSOR_COUNT;
47 }
48 for (size_t i = 0; i < count; i++) {
49 const std::string sensorName(sensorInfos[i].sensorName);
50 const std::string vendorName(sensorInfos[i].vendorName);
51 const std::string firmwareVersion(sensorInfos[i].firmwareVersion);
52 const std::string hardwareVersion(sensorInfos[i].hardwareVersion);
53 const int32_t sensorId = sensorInfos[i].sensorIndex;
54 const int32_t sensorTypeId = sensorInfos[i].sensorTypeId;
55 const int32_t deviceId = sensorInfos[i].deviceId;
56 const int32_t location = sensorInfos[i].location;
57 const float maxRange = sensorInfos[i].maxRange;
58 Sensor sensor;
59 sensor.SetDeviceId(deviceId);
60 sensor.SetSensorId(sensorId);
61 sensor.SetSensorTypeId(sensorTypeId);
62 sensor.SetLocation(location);
63 sensor.SetFirmwareVersion(firmwareVersion);
64 sensor.SetHardwareVersion(hardwareVersion);
65 sensor.SetMaxRange(maxRange);
66 sensor.SetSensorName(sensorName);
67 sensor.SetVendorName(vendorName);
68 sensor.SetResolution(sensorInfos[i].precision);
69 sensor.SetPower(sensorInfos[i].power);
70 sensor.SetMinSamplePeriodNs(sensorInfos[i].minSamplePeriod);
71 sensor.SetMaxSamplePeriodNs(sensorInfos[i].maxSamplePeriod);
72 sensorList.push_back(sensor);
73 }
74 return ERR_OK;
75 }
76
EnableSensor(const SensorDescription & sensorDesc)77 int32_t CompatibleConnection::EnableSensor(const SensorDescription &sensorDesc)
78 {
79 int32_t ret = hdiServiceImpl_.EnableSensor(sensorDesc);
80 if (ret != 0) {
81 SEN_HILOGE("Enable sensor failed, deviceIndex:%{public}d, sensortypeId:%{public}d, sensorId:%{public}d",
82 sensorDesc.deviceId, sensorDesc.sensorType, sensorDesc.sensorId);
83 return ret;
84 }
85 return ERR_OK;
86 };
87
DisableSensor(const SensorDescription & sensorDesc)88 int32_t CompatibleConnection::DisableSensor(const SensorDescription &sensorDesc)
89 {
90 int32_t ret = hdiServiceImpl_.DisableSensor(sensorDesc);
91 if (ret != 0) {
92 SEN_HILOGE("Disable sensor failed, deviceIndex:%{public}d, sensortypeId:%{public}d, sensorId:%{public}d",
93 sensorDesc.deviceId, sensorDesc.sensorType, sensorDesc.sensorId);
94 return ret;
95 }
96 return ERR_OK;
97 }
98
SetBatch(const SensorDescription & sensorDesc,int64_t samplingInterval,int64_t reportInterval)99 int32_t CompatibleConnection::SetBatch(const SensorDescription &sensorDesc, int64_t samplingInterval,
100 int64_t reportInterval)
101 {
102 int32_t ret = hdiServiceImpl_.SetBatch(sensorDesc, samplingInterval, reportInterval);
103 if (ret != 0) {
104 SEN_HILOGE("Set batch failed, deviceIndex:%{public}d, sensortypeId:%{public}d, sensorId:%{public}d",
105 sensorDesc.deviceId, sensorDesc.sensorType, sensorDesc.sensorId);
106 return ret;
107 }
108 return ERR_OK;
109 }
110
SetMode(const SensorDescription & sensorDesc,int32_t mode)111 int32_t CompatibleConnection::SetMode(const SensorDescription &sensorDesc, int32_t mode)
112 {
113 int32_t ret = hdiServiceImpl_.SetMode(sensorDesc, mode);
114 if (ret != 0) {
115 SEN_HILOGI("Set mode failed, deviceIndex:%{public}d, sensortypeId:%{public}d, sensorId:%{public}d",
116 sensorDesc.deviceId, sensorDesc.sensorType, sensorDesc.sensorId);
117 return ret;
118 }
119 return ERR_OK;
120 }
121
ReportSensorDataCallback(SensorEvent * event)122 void CompatibleConnection::ReportSensorDataCallback(SensorEvent *event)
123 {
124 CHKPV(event);
125 if ((event->dataLen) == 0) {
126 SEN_HILOGE("Event is NULL");
127 return;
128 }
129
130 SensorData sensorData = {
131 .sensorTypeId = event->sensorTypeId,
132 .version = event->version,
133 .timestamp = event->timestamp,
134 .option = event->option,
135 .mode = event->mode,
136 .dataLen = event->dataLen,
137 .deviceId = event->deviceId,
138 .sensorId = event->sensorId,
139 .location = event->location
140 };
141 CHKPV(sensorData.data);
142 errno_t ret = memcpy_s(sensorData.data, sizeof(sensorData.data), event->data, event->dataLen);
143 if (ret != EOK) {
144 SEN_HILOGE("Copy data failed");
145 return;
146 }
147 CHKPV(reportDataCallback_);
148 CHKPV(reportDataCb_);
149 std::unique_lock<std::mutex> lk(ISensorHdiConnection::dataMutex_);
150 (void)(reportDataCallback_->*reportDataCb_)(&sensorData, reportDataCallback_);
151 ISensorHdiConnection::dataReady_.store(true);
152 ISensorHdiConnection::dataCondition_.notify_one();
153 }
154
RegisterDataReport(ReportDataCb cb,sptr<ReportDataCallback> reportDataCallback)155 int32_t CompatibleConnection::RegisterDataReport(ReportDataCb cb, sptr<ReportDataCallback> reportDataCallback)
156 {
157 CHKPR(reportDataCallback, ERR_INVALID_VALUE);
158 int32_t ret = hdiServiceImpl_.Register(ReportSensorDataCallback);
159 if (ret != 0) {
160 SEN_HILOGE("Register is failed");
161 return ret;
162 }
163 reportDataCb_ = cb;
164 reportDataCallback_ = reportDataCallback;
165 return ERR_OK;
166 }
167
DestroyHdiConnection()168 int32_t CompatibleConnection::DestroyHdiConnection()
169 {
170 int32_t ret = hdiServiceImpl_.Unregister();
171 if (ret != 0) {
172 SEN_HILOGE("Unregister is failed");
173 return ret;
174 }
175 return ERR_OK;
176 }
177
GetSensorListByDevice(int32_t deviceId,std::vector<Sensor> & singleDevSensors)178 int32_t CompatibleConnection::GetSensorListByDevice(int32_t deviceId, std::vector<Sensor> &singleDevSensors)
179 {
180 std::vector<SensorInfo> sensorInfos;
181 int32_t ret = hdiServiceImpl_.GetSensorListByDevice(deviceId, sensorInfos);
182 if (ret != 0) {
183 SEN_HILOGE("Get sensor list by device failed");
184 return ret;
185 }
186 size_t count = sensorInfos.size();
187 if (count > MAX_SENSOR_COUNT) {
188 SEN_HILOGD("SensorInfos size:%{public}zu", count);
189 count = MAX_SENSOR_COUNT;
190 }
191 for (size_t i = 0; i < count; i++) {
192 const std::string sensorName(sensorInfos[i].sensorName);
193 const std::string vendorName(sensorInfos[i].vendorName);
194 const std::string firmwareVersion(sensorInfos[i].firmwareVersion);
195 const std::string hardwareVersion(sensorInfos[i].hardwareVersion);
196 const int32_t sensorId = sensorInfos[i].sensorIndex;
197 const int32_t sensorTypeId = sensorInfos[i].sensorTypeId;
198 const int32_t deviceId = sensorInfos[i].deviceId;
199 const int32_t location = sensorInfos[i].location;
200 const float maxRange = sensorInfos[i].maxRange;
201 Sensor sensor;
202 sensor.SetDeviceId(deviceId);
203 sensor.SetSensorId(sensorId);
204 sensor.SetSensorTypeId(sensorTypeId);
205 sensor.SetLocation(location);
206 sensor.SetFirmwareVersion(firmwareVersion);
207 sensor.SetHardwareVersion(hardwareVersion);
208 sensor.SetMaxRange(maxRange);
209 sensor.SetSensorName(sensorName);
210 sensor.SetVendorName(vendorName);
211 sensor.SetResolution(sensorInfos[i].precision);
212 sensor.SetPower(sensorInfos[i].power);
213 sensor.SetMinSamplePeriodNs(sensorInfos[i].minSamplePeriod);
214 sensor.SetMaxSamplePeriodNs(sensorInfos[i].maxSamplePeriod);
215 singleDevSensors.push_back(sensor);
216 }
217 return ERR_OK;
218 }
219
RegSensorPlugCallback(DevicePlugCallback cb)220 int32_t CompatibleConnection::RegSensorPlugCallback(DevicePlugCallback cb)
221 {
222 return ERR_OK;
223 }
224
GetSensorPlugCb()225 DevicePlugCallback CompatibleConnection::GetSensorPlugCb()
226 {
227 return NULL;
228 }
229 } // namespace Sensors
230 } // namespace OHOS