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 <cstring>
18
19 #include "securec.h"
20 #include "sensor_errors.h"
21
22 #undef LOG_TAG
23 #define LOG_TAG "CompatibleConnection"
24
25 namespace OHOS {
26 namespace Sensors {
27 using namespace OHOS::HiviewDFX;
28
29 ReportDataCb CompatibleConnection::reportDataCb_ = nullptr;
30 sptr<ReportDataCallback> CompatibleConnection::reportDataCallback_ = nullptr;
ConnectHdi()31 int32_t CompatibleConnection::ConnectHdi()
32 {
33 SEN_HILOGI("Connect hdi success");
34 return ERR_OK;
35 }
36
GetSensorList(std::vector<Sensor> & sensorList)37 int32_t CompatibleConnection::GetSensorList(std::vector<Sensor> &sensorList)
38 {
39 std::vector<SensorInfo> sensorInfos;
40 int32_t ret = hdiServiceImpl_.GetSensorList(sensorInfos);
41 if (ret != 0) {
42 SEN_HILOGE("Get sensor list failed");
43 return ret;
44 }
45 size_t count = sensorInfos.size();
46 if (count > MAX_SENSOR_COUNT) {
47 SEN_HILOGD("SensorInfos size:%{public}zu", count);
48 count = MAX_SENSOR_COUNT;
49 }
50 for (size_t i = 0; i < count; i++) {
51 const std::string sensorName(sensorInfos[i].sensorName);
52 const std::string vendorName(sensorInfos[i].vendorName);
53 const std::string firmwareVersion(sensorInfos[i].firmwareVersion);
54 const std::string hardwareVersion(sensorInfos[i].hardwareVersion);
55 const int32_t sensorId = sensorInfos[i].sensorId;
56 const float maxRange = sensorInfos[i].maxRange;
57 Sensor sensor;
58 sensor.SetSensorId(sensorId);
59 sensor.SetSensorTypeId(sensorId);
60 sensor.SetFirmwareVersion(firmwareVersion);
61 sensor.SetHardwareVersion(hardwareVersion);
62 sensor.SetMaxRange(maxRange);
63 sensor.SetSensorName(sensorName);
64 sensor.SetVendorName(vendorName);
65 sensor.SetResolution(sensorInfos[i].precision);
66 sensor.SetPower(sensorInfos[i].power);
67 sensor.SetMinSamplePeriodNs(sensorInfos[i].minSamplePeriod);
68 sensor.SetMaxSamplePeriodNs(sensorInfos[i].maxSamplePeriod);
69 sensorList.push_back(sensor);
70 }
71 return ERR_OK;
72 }
73
EnableSensor(int32_t sensorId)74 int32_t CompatibleConnection::EnableSensor(int32_t sensorId)
75 {
76 int32_t ret = hdiServiceImpl_.EnableSensor(sensorId);
77 if (ret != 0) {
78 SEN_HILOGE("Enable sensor failed, sensorId:%{public}d", sensorId);
79 return ret;
80 }
81 return ERR_OK;
82 };
83
DisableSensor(int32_t sensorId)84 int32_t CompatibleConnection::DisableSensor(int32_t sensorId)
85 {
86 int32_t ret = hdiServiceImpl_.DisableSensor(sensorId);
87 if (ret != 0) {
88 SEN_HILOGE("Disable sensor failed, sensorId:%{public}d", sensorId);
89 return ret;
90 }
91 return ERR_OK;
92 }
93
SetBatch(int32_t sensorId,int64_t samplingInterval,int64_t reportInterval)94 int32_t CompatibleConnection::SetBatch(int32_t sensorId, int64_t samplingInterval, int64_t reportInterval)
95 {
96 int32_t ret = hdiServiceImpl_.SetBatch(sensorId, samplingInterval, reportInterval);
97 if (ret != 0) {
98 SEN_HILOGE("Set batch failed, sensorId:%{public}d", sensorId);
99 return ret;
100 }
101 return ERR_OK;
102 }
103
SetMode(int32_t sensorId,int32_t mode)104 int32_t CompatibleConnection::SetMode(int32_t sensorId, int32_t mode)
105 {
106 int32_t ret = hdiServiceImpl_.SetMode(sensorId, mode);
107 if (ret != 0) {
108 SEN_HILOGI("Set mode failed, sensorId:%{public}d", sensorId);
109 return ret;
110 }
111 return ERR_OK;
112 }
113
ReportSensorDataCallback(SensorEvent * event)114 void CompatibleConnection::ReportSensorDataCallback(SensorEvent *event)
115 {
116 CHKPV(event);
117 if ((event->dataLen) == 0) {
118 SEN_HILOGE("Event is NULL");
119 return;
120 }
121
122 SensorData sensorData = {
123 .sensorTypeId = event->sensorTypeId,
124 .version = event->version,
125 .timestamp = event->timestamp,
126 .option = event->option,
127 .mode = event->mode,
128 .dataLen = event->dataLen
129 };
130 CHKPV(sensorData.data);
131 errno_t ret = memcpy_s(sensorData.data, sizeof(sensorData.data), event->data, event->dataLen);
132 if (ret != EOK) {
133 SEN_HILOGE("Copy data failed");
134 return;
135 }
136 CHKPV(reportDataCallback_);
137 CHKPV(reportDataCb_);
138 std::unique_lock<std::mutex> lk(ISensorHdiConnection::dataMutex_);
139 (void)(reportDataCallback_->*reportDataCb_)(&sensorData, reportDataCallback_);
140 ISensorHdiConnection::dataReady_.store(true);
141 ISensorHdiConnection::dataCondition_.notify_one();
142 }
143
RegisterDataReport(ReportDataCb cb,sptr<ReportDataCallback> reportDataCallback)144 int32_t CompatibleConnection::RegisterDataReport(ReportDataCb cb, sptr<ReportDataCallback> reportDataCallback)
145 {
146 CHKPR(reportDataCallback, ERR_INVALID_VALUE);
147 int32_t ret = hdiServiceImpl_.Register(ReportSensorDataCallback);
148 if (ret != 0) {
149 SEN_HILOGE("Register is failed");
150 return ret;
151 }
152 reportDataCb_ = cb;
153 reportDataCallback_ = reportDataCallback;
154 return ERR_OK;
155 }
156
DestroyHdiConnection()157 int32_t CompatibleConnection::DestroyHdiConnection()
158 {
159 int32_t ret = hdiServiceImpl_.Unregister();
160 if (ret != 0) {
161 SEN_HILOGE("Unregister is failed");
162 return ret;
163 }
164 return ERR_OK;
165 }
166 } // namespace Sensors
167 } // namespace OHOS