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].sensorId;
54 const float maxRange = sensorInfos[i].maxRange;
55 Sensor sensor;
56 sensor.SetSensorId(sensorId);
57 sensor.SetSensorTypeId(sensorId);
58 sensor.SetFirmwareVersion(firmwareVersion);
59 sensor.SetHardwareVersion(hardwareVersion);
60 sensor.SetMaxRange(maxRange);
61 sensor.SetSensorName(sensorName);
62 sensor.SetVendorName(vendorName);
63 sensor.SetResolution(sensorInfos[i].precision);
64 sensor.SetPower(sensorInfos[i].power);
65 sensor.SetMinSamplePeriodNs(sensorInfos[i].minSamplePeriod);
66 sensor.SetMaxSamplePeriodNs(sensorInfos[i].maxSamplePeriod);
67 sensorList.push_back(sensor);
68 }
69 return ERR_OK;
70 }
71
EnableSensor(int32_t sensorId)72 int32_t CompatibleConnection::EnableSensor(int32_t sensorId)
73 {
74 int32_t ret = hdiServiceImpl_.EnableSensor(sensorId);
75 if (ret != 0) {
76 SEN_HILOGE("Enable sensor failed, sensorId:%{public}d", sensorId);
77 return ret;
78 }
79 return ERR_OK;
80 };
81
DisableSensor(int32_t sensorId)82 int32_t CompatibleConnection::DisableSensor(int32_t sensorId)
83 {
84 int32_t ret = hdiServiceImpl_.DisableSensor(sensorId);
85 if (ret != 0) {
86 SEN_HILOGE("Disable sensor failed, sensorId:%{public}d", sensorId);
87 return ret;
88 }
89 return ERR_OK;
90 }
91
SetBatch(int32_t sensorId,int64_t samplingInterval,int64_t reportInterval)92 int32_t CompatibleConnection::SetBatch(int32_t sensorId, int64_t samplingInterval, int64_t reportInterval)
93 {
94 int32_t ret = hdiServiceImpl_.SetBatch(sensorId, samplingInterval, reportInterval);
95 if (ret != 0) {
96 SEN_HILOGE("Set batch failed, sensorId:%{public}d", sensorId);
97 return ret;
98 }
99 return ERR_OK;
100 }
101
SetMode(int32_t sensorId,int32_t mode)102 int32_t CompatibleConnection::SetMode(int32_t sensorId, int32_t mode)
103 {
104 int32_t ret = hdiServiceImpl_.SetMode(sensorId, mode);
105 if (ret != 0) {
106 SEN_HILOGI("Set mode failed, sensorId:%{public}d", sensorId);
107 return ret;
108 }
109 return ERR_OK;
110 }
111
ReportSensorDataCallback(SensorEvent * event)112 void CompatibleConnection::ReportSensorDataCallback(SensorEvent *event)
113 {
114 CHKPV(event);
115 if ((event->dataLen) == 0) {
116 SEN_HILOGE("Event is NULL");
117 return;
118 }
119
120 SensorData sensorData = {
121 .sensorTypeId = event->sensorTypeId,
122 .version = event->version,
123 .timestamp = event->timestamp,
124 .option = event->option,
125 .mode = event->mode,
126 .dataLen = event->dataLen
127 };
128 CHKPV(sensorData.data);
129 errno_t ret = memcpy_s(sensorData.data, sizeof(sensorData.data), event->data, event->dataLen);
130 if (ret != EOK) {
131 SEN_HILOGE("Copy data failed");
132 return;
133 }
134 CHKPV(reportDataCallback_);
135 CHKPV(reportDataCb_);
136 std::unique_lock<std::mutex> lk(ISensorHdiConnection::dataMutex_);
137 (void)(reportDataCallback_->*reportDataCb_)(&sensorData, reportDataCallback_);
138 ISensorHdiConnection::dataReady_.store(true);
139 ISensorHdiConnection::dataCondition_.notify_one();
140 }
141
RegisterDataReport(ReportDataCb cb,sptr<ReportDataCallback> reportDataCallback)142 int32_t CompatibleConnection::RegisterDataReport(ReportDataCb cb, sptr<ReportDataCallback> reportDataCallback)
143 {
144 CHKPR(reportDataCallback, ERR_INVALID_VALUE);
145 int32_t ret = hdiServiceImpl_.Register(ReportSensorDataCallback);
146 if (ret != 0) {
147 SEN_HILOGE("Register is failed");
148 return ret;
149 }
150 reportDataCb_ = cb;
151 reportDataCallback_ = reportDataCallback;
152 return ERR_OK;
153 }
154
DestroyHdiConnection()155 int32_t CompatibleConnection::DestroyHdiConnection()
156 {
157 int32_t ret = hdiServiceImpl_.Unregister();
158 if (ret != 0) {
159 SEN_HILOGE("Unregister is failed");
160 return ret;
161 }
162 return ERR_OK;
163 }
164 } // namespace Sensors
165 } // namespace OHOS