• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024 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 
16 #include "sensor_adapter_impl.h"
17 
18 #include <securec.h>
19 #include <map>
20 
21 #include "nweb_log.h"
22 
23 namespace OHOS::NWeb {
24 
25 std::unordered_map<int32_t, std::shared_ptr<SensorCallbackImpl>> SensorAdapterImpl::sensorCallbackMap;
26 std::mutex SensorAdapterImpl::sensorCallbackMapMutex_;
27 
28 constexpr double NANOSECONDS_IN_SECOND = 1000000000.0;
29 constexpr double DEFAULT_SAMPLE_PERIOD = 200000000.0;
30 
SensorTypeToOhosSensorType(int sensorTypeId)31 SensorTypeId SensorTypeToOhosSensorType(int sensorTypeId)
32 {
33     SensorTypeId ohosSensorTypeId = SENSOR_TYPE_ID_NONE;
34     const static std::map<int32_t, SensorTypeId> TO_OHOS_SENSOR_TYPE_MAP = {
35         {2  /* ACCELEROMETER                     */, SENSOR_TYPE_ID_ACCELEROMETER },
36         {3  /* LINEAR_ACCELERATION               */, SENSOR_TYPE_ID_LINEAR_ACCELERATION },
37         {4  /* GRAVITY                           */, SENSOR_TYPE_ID_GRAVITY },
38         {5  /* GYROSCOPE                         */, SENSOR_TYPE_ID_GYROSCOPE },
39         {6  /* MAGNETOMETER                      */, SENSOR_TYPE_ID_MAGNETIC_FIELD },
40         {8  /* ABSOLUTE_ORIENTATION_EULER_ANGLES}*/, SENSOR_TYPE_ID_ORIENTATION },
41         {9  /* ABSOLUTE_ORIENTATION_QUATERNION}  */, SENSOR_TYPE_ID_ROTATION_VECTOR },
42         {11 /* RELATIVE_ORIENTATION_QUATERNION}  */, SENSOR_TYPE_ID_GAME_ROTATION_VECTOR }
43     };
44     auto checkIter = TO_OHOS_SENSOR_TYPE_MAP.find(sensorTypeId);
45     if (checkIter != TO_OHOS_SENSOR_TYPE_MAP.end()) {
46         ohosSensorTypeId = checkIter->second;
47     }
48     return ohosSensorTypeId;
49 }
50 
SensorTypeToSensorUserName(int sensorTypeId)51 std::string SensorTypeToSensorUserName(int sensorTypeId)
52 {
53     const static std::map<int32_t, std::string> TO_OHOS_SENSOR_USER_NAME_MAP = {
54         {2  /* ACCELEROMETER                     */, "OhosAccelerometerService" },
55         {3  /* LINEAR_ACCELERATION               */, "OhosLinearAccelerometerService" },
56         {4  /* GRAVITY                           */, "OhosGravityService" },
57         {5  /* GYROSCOPE                         */, "OhosCyroscopeService" },
58         {6  /* MAGNETOMETER                      */, "OhosMagnetometerService" },
59         {8  /* ABSOLUTE_ORIENTATION_EULER_ANGLES}*/, "OhosOrientationService" },
60         {9  /* ABSOLUTE_ORIENTATION_QUATERNION}  */, "OhosRotationVectorService"},
61         {11 /* RELATIVE_ORIENTATION_QUATERNION}  */, "OhosGameRotationVectorService" }
62     };
63     std::string userName = "OhosSensorService";
64     auto checkIter = TO_OHOS_SENSOR_USER_NAME_MAP.find(sensorTypeId);
65     if (checkIter != TO_OHOS_SENSOR_USER_NAME_MAP.end()) {
66         userName = checkIter->second;
67     }
68     return userName;
69 }
70 
SensorCallbackImpl(std::shared_ptr<SensorCallbackAdapter> callbackAdapter)71 SensorCallbackImpl::SensorCallbackImpl(
72     std::shared_ptr<SensorCallbackAdapter> callbackAdapter)
73     : callbackAdapter_(callbackAdapter)
74 {}
75 
UpdateOhosSensorData(double timestamp,double value1,double value2,double value3,double value4)76 void SensorCallbackImpl::UpdateOhosSensorData(double timestamp,
77     double value1, double value2, double value3, double value4)
78 {
79     if (callbackAdapter_) {
80         callbackAdapter_->UpdateOhosSensorData(timestamp, value1, value2, value3, value4);
81     }
82 }
83 
IsOhosSensorSupported(int32_t sensorTypeId)84 int32_t SensorAdapterImpl::IsOhosSensorSupported(int32_t sensorTypeId)
85 {
86     int32_t ohosSensorTypeId = SensorTypeToOhosSensorType(sensorTypeId);
87     if (ohosSensorTypeId != SENSOR_TYPE_ID_NONE) {
88         SensorInfo* sensorInfo = nullptr;
89         int32_t count;
90         int ret = GetAllSensors(&sensorInfo, &count);
91         if (ret != SENSOR_SUCCESS || sensorInfo == nullptr || count < 0) {
92             WVLOG_E("IsOhosSensorSupported Error, ret = %{public}d, count = %{public}d.", ret, count);
93             return SENSOR_ERROR;
94         }
95 
96         for (int i = 0; i < count; i++) {
97             if (sensorInfo[i].sensorId == ohosSensorTypeId) {
98                 WVLOG_I("IsOhosSensorSupported SUCCESS, sensorTypeId = %{public}d.", sensorTypeId);
99                 return SENSOR_SUCCESS;
100             }
101         }
102     }
103     WVLOG_E("IsOhosSensorSupported Error, sensorTypeId = %{public}d is invalid.", sensorTypeId);
104     return SENSOR_ERROR;
105 }
106 
GetOhosSensorReportingMode(int32_t sensorTypeId)107 int32_t SensorAdapterImpl::GetOhosSensorReportingMode(int32_t sensorTypeId)
108 {
109     int32_t ohosSensorTypeId = SensorTypeToOhosSensorType(sensorTypeId);
110     int32_t reportingMode = -1;
111     switch (ohosSensorTypeId) {
112         case SENSOR_TYPE_ID_ACCELEROMETER:
113         case SENSOR_TYPE_ID_GRAVITY:
114         case SENSOR_TYPE_ID_LINEAR_ACCELERATION:
115         case SENSOR_TYPE_ID_GYROSCOPE:
116         case SENSOR_TYPE_ID_MAGNETIC_FIELD:
117         case SENSOR_TYPE_ID_ORIENTATION:
118         case SENSOR_TYPE_ID_ROTATION_VECTOR:
119         case SENSOR_TYPE_ID_GAME_ROTATION_VECTOR:
120             reportingMode = SENSOR_DATA_REPORT_CONTINUOUS;
121             break;
122         default:
123             break;
124     }
125     return reportingMode;
126 }
127 
GetOhosSensorDefaultSupportedFrequency(int32_t sensorTypeId)128 double SensorAdapterImpl::GetOhosSensorDefaultSupportedFrequency(int32_t sensorTypeId)
129 {
130     int32_t ohosSensorTypeId = SensorTypeToOhosSensorType(sensorTypeId);
131     double defaultFrequency = 0.0;
132     if (ohosSensorTypeId != SENSOR_TYPE_ID_NONE) {
133         defaultFrequency = NANOSECONDS_IN_SECOND / DEFAULT_SAMPLE_PERIOD;
134     }
135     WVLOG_I("GetOhosSensorDefaultSupportedFrequency sensorTypeId: %{public}d, defaultFrequency: %{public}f",
136         sensorTypeId, defaultFrequency);
137     return defaultFrequency;
138 }
139 
GetOhosSensorMinSupportedFrequency(int32_t sensorTypeId)140 double SensorAdapterImpl::GetOhosSensorMinSupportedFrequency(int32_t sensorTypeId)
141 {
142     int32_t ohosSensorTypeId = SensorTypeToOhosSensorType(sensorTypeId);
143     double minFrequency = 0.0;
144     if (ohosSensorTypeId == SENSOR_TYPE_ID_NONE) {
145         WVLOG_E("GetOhosSensorMinSupportedFrequency Error, sensorTypeId = %{public}d is invalid.", sensorTypeId);
146         return minFrequency;
147     }
148     SensorInfo* sensorInfo = nullptr;
149     int32_t count;
150     int ret = GetAllSensors(&sensorInfo, &count);
151     if (ret != SENSOR_SUCCESS || sensorInfo == nullptr || count < 0) {
152         WVLOG_E("GetOhosSensorMinSupportedFrequency Error, ret = %{public}d, count = %{public}d.", ret, count);
153         return minFrequency;
154     }
155     for (int i = 0; i < count; i++) {
156         if (sensorInfo[i].sensorId == ohosSensorTypeId) {
157             int64_t maxSamplePeriod = sensorInfo[i].maxSamplePeriod;
158             if (maxSamplePeriod > 0) {
159                 minFrequency = NANOSECONDS_IN_SECOND / static_cast<double>(maxSamplePeriod);
160             }
161             break;
162         }
163     }
164     WVLOG_I("GetOhosSensorMinSupportedFrequency sensorTypeId: %{public}d, minFrequency: %{public}f",
165         sensorTypeId, minFrequency);
166     return minFrequency;
167 }
168 
GetOhosSensorMaxSupportedFrequency(int32_t sensorTypeId)169 double SensorAdapterImpl::GetOhosSensorMaxSupportedFrequency(int32_t sensorTypeId)
170 {
171     int32_t ohosSensorTypeId = SensorTypeToOhosSensorType(sensorTypeId);
172     double maxFrequency = 0.0;
173     if (ohosSensorTypeId == SENSOR_TYPE_ID_NONE) {
174         WVLOG_E("GetOhosSensorMaxSupportedFrequency Error, sensorTypeId = %{public}d is invalid.", sensorTypeId);
175         return maxFrequency;
176     }
177     SensorInfo* sensorInfo = nullptr;
178     int32_t count;
179     int ret = GetAllSensors(&sensorInfo, &count);
180     if (ret != SENSOR_SUCCESS || sensorInfo == nullptr || count < 0) {
181         WVLOG_E("GetOhosSensorMaxSupportedFrequency Error, ret = %{public}d, count = %{public}d.", ret, count);
182         return maxFrequency;
183     }
184     for (int i = 0; i < count; i++) {
185         if (sensorInfo[i].sensorId == ohosSensorTypeId) {
186             int64_t minSamplePeriod = sensorInfo[i].minSamplePeriod;
187             if (minSamplePeriod > 0) {
188                 maxFrequency = NANOSECONDS_IN_SECOND / static_cast<double>(minSamplePeriod);
189             }
190             break;
191         }
192     }
193     WVLOG_I("GetOhosSensorMaxSupportedFrequency sensorTypeId: %{public}d, maxFrequency: %{public}f",
194         sensorTypeId, maxFrequency);
195     return maxFrequency;
196 }
197 
handleAccelerometerData(std::shared_ptr<OHOS::NWeb::SensorCallbackImpl> callback,SensorEvent * event)198 void SensorAdapterImpl::handleAccelerometerData(std::shared_ptr<OHOS::NWeb::SensorCallbackImpl> callback,
199     SensorEvent* event)
200 {
201     if ((event == nullptr) || (callback == nullptr)) {
202         WVLOG_E("handleAccelerometerData Error.");
203         return;
204     }
205     AccelData* data = reinterpret_cast<AccelData*>(event->data);
206     if (data != nullptr) {
207         callback->UpdateOhosSensorData(event->timestamp, data->x, data->y, data->z, 0.0f);
208     }
209 }
210 
handleLinearAccelerometerData(std::shared_ptr<OHOS::NWeb::SensorCallbackImpl> callback,SensorEvent * event)211 void SensorAdapterImpl::handleLinearAccelerometerData(std::shared_ptr<OHOS::NWeb::SensorCallbackImpl> callback,
212     SensorEvent* event)
213 {
214     if ((event == nullptr) || (callback == nullptr)) {
215         WVLOG_E("handleLinearAccelerometerData Error.");
216         return;
217     }
218     LinearAccelData* data = reinterpret_cast<LinearAccelData*>(event->data);
219     if (data != nullptr) {
220         callback->UpdateOhosSensorData(event->timestamp, data->x, data->y, data->z, 0.0f);
221     }
222 }
223 
handleGravityData(std::shared_ptr<OHOS::NWeb::SensorCallbackImpl> callback,SensorEvent * event)224 void SensorAdapterImpl::handleGravityData(std::shared_ptr<OHOS::NWeb::SensorCallbackImpl> callback,
225     SensorEvent* event)
226 {
227     if ((event == nullptr) || (callback == nullptr)) {
228         WVLOG_E("handleGravityData Error.");
229         return;
230     }
231     GravityData* data = reinterpret_cast<GravityData*>(event->data);
232     if (data != nullptr) {
233         callback->UpdateOhosSensorData(event->timestamp, data->x, data->y, data->z, 0.0f);
234     }
235 }
236 
handleCyroscopeData(std::shared_ptr<OHOS::NWeb::SensorCallbackImpl> callback,SensorEvent * event)237 void SensorAdapterImpl::handleCyroscopeData(std::shared_ptr<OHOS::NWeb::SensorCallbackImpl> callback,
238     SensorEvent* event)
239 {
240     if ((event == nullptr) || (callback == nullptr)) {
241         WVLOG_E("handleCyroscopeData Error.");
242         return;
243     }
244     GyroscopeData* data = reinterpret_cast<GyroscopeData*>(event->data);
245     if (data != nullptr) {
246         callback->UpdateOhosSensorData(event->timestamp, data->x, data->y, data->z, 0.0f);
247     }
248 }
249 
handleMagnetometerData(std::shared_ptr<OHOS::NWeb::SensorCallbackImpl> callback,SensorEvent * event)250 void SensorAdapterImpl::handleMagnetometerData(std::shared_ptr<OHOS::NWeb::SensorCallbackImpl> callback,
251     SensorEvent* event)
252 {
253     if ((event == nullptr) || (callback == nullptr)) {
254         WVLOG_E("handleMagnetometerData Error.");
255         return;
256     }
257     MagneticFieldData* data = reinterpret_cast<MagneticFieldData*>(event->data);
258     if (data != nullptr) {
259         callback->UpdateOhosSensorData(event->timestamp, data->x, data->y, data->z, 0.0f);
260     }
261 }
262 
handleOrientationData(std::shared_ptr<OHOS::NWeb::SensorCallbackImpl> callback,SensorEvent * event)263 void SensorAdapterImpl::handleOrientationData(std::shared_ptr<OHOS::NWeb::SensorCallbackImpl> callback,
264     SensorEvent* event)
265 {
266     if ((event == nullptr) || (callback == nullptr)) {
267         WVLOG_E("handleOrientationData Error.");
268         return;
269     }
270     OrientationData* data = reinterpret_cast<OrientationData*>(event->data);
271     if (data != nullptr) {
272         callback->UpdateOhosSensorData(event->timestamp, data->beta, data->gamma, data->alpha, 0.0f);
273     }
274 }
275 
handleRotationVectorData(std::shared_ptr<OHOS::NWeb::SensorCallbackImpl> callback,SensorEvent * event)276 void SensorAdapterImpl::handleRotationVectorData(std::shared_ptr<OHOS::NWeb::SensorCallbackImpl> callback,
277     SensorEvent* event)
278 {
279     if ((event == nullptr) || (callback == nullptr)) {
280         WVLOG_E("handleRotationVectorData Error.");
281         return;
282     }
283     RotationVectorData* data = reinterpret_cast<RotationVectorData*>(event->data);
284     if (data != nullptr) {
285         callback->UpdateOhosSensorData(event->timestamp, data->x, data->y, data->z, data->w);
286     }
287 }
288 
handleGameRotationVectorData(std::shared_ptr<OHOS::NWeb::SensorCallbackImpl> callback,SensorEvent * event)289 void SensorAdapterImpl::handleGameRotationVectorData(std::shared_ptr<OHOS::NWeb::SensorCallbackImpl> callback,
290     SensorEvent* event)
291 {
292     if ((event == nullptr) || (callback == nullptr)) {
293         WVLOG_E("handleGameRotationVectorData Error.");
294         return;
295     }
296     GameRotationVectorData* data = reinterpret_cast<GameRotationVectorData*>(event->data);
297     if (data != nullptr) {
298         callback->UpdateOhosSensorData(event->timestamp, data->x, data->y, data->z, data->w);
299     }
300 }
301 
OhosSensorCallback(SensorEvent * event)302 void SensorAdapterImpl::OhosSensorCallback(SensorEvent* event)
303 {
304     if (event == nullptr) {
305         WVLOG_E("SensorEvent Error.");
306         return;
307     }
308     std::shared_ptr<OHOS::NWeb::SensorCallbackImpl> callback = nullptr;
309     {
310         std::lock_guard<std::mutex> lock(sensorCallbackMapMutex_);
311         auto findIter = sensorCallbackMap.find(event->sensorTypeId);
312         if (findIter != sensorCallbackMap.end()) {
313             callback = findIter->second;
314         }
315     }
316     if (callback == nullptr) {
317         WVLOG_E("OhosSensorCallback Error.");
318         return;
319     }
320     switch (event->sensorTypeId) {
321         case SENSOR_TYPE_ID_ACCELEROMETER:
322             handleAccelerometerData(callback, event);
323             break;
324         case SENSOR_TYPE_ID_GRAVITY:
325             handleGravityData(callback, event);
326             break;
327         case SENSOR_TYPE_ID_LINEAR_ACCELERATION:
328             handleLinearAccelerometerData(callback, event);
329             break;
330         case SENSOR_TYPE_ID_GYROSCOPE:
331             handleCyroscopeData(callback, event);
332             break;
333         case SENSOR_TYPE_ID_MAGNETIC_FIELD:
334             handleMagnetometerData(callback, event);
335             break;
336         case SENSOR_TYPE_ID_ORIENTATION:
337             handleOrientationData(callback, event);
338             break;
339         case SENSOR_TYPE_ID_ROTATION_VECTOR:
340             handleRotationVectorData(callback, event);
341             break;
342         case SENSOR_TYPE_ID_GAME_ROTATION_VECTOR:
343             handleGameRotationVectorData(callback, event);
344             break;
345         default:
346             break;
347     }
348 }
349 
SubscribeOhosSensor(int32_t sensorTypeId,int64_t samplingInterval)350 int32_t SensorAdapterImpl::SubscribeOhosSensor(int32_t sensorTypeId, int64_t samplingInterval)
351 {
352     WVLOG_I("SubscribeOhosSensor sensorTypeId: %{public}d", sensorTypeId);
353     if (samplingInterval <= 0) {
354         WVLOG_E("SubscribeOhosSensor error, samplingInterval is invalid.");
355         return SENSOR_PARAMETER_ERROR;
356     }
357     int32_t ohosSensorTypeId = SensorTypeToOhosSensorType(sensorTypeId);
358     if (ohosSensorTypeId == SENSOR_TYPE_ID_NONE) {
359         WVLOG_E("SubscribeOhosSensor error, sensorTypeId is invalid.");
360         return SENSOR_PARAMETER_ERROR;
361     }
362 
363     std::string userName = SensorTypeToSensorUserName(sensorTypeId);
364     int cpyret = strcpy_s(mSensorUser.name, sizeof(mSensorUser.name), userName.c_str());
365     if (cpyret != 0) {
366         WVLOG_E("SubscribeOhosSensor error, call strcpy_s ret = %{public}d.", cpyret);
367     }
368     mSensorUser.userData = nullptr;
369     mSensorUser.callback = &OhosSensorCallback;
370     int32_t ret = SENSOR_SUCCESS;
371     ret = SubscribeSensor(ohosSensorTypeId, &mSensorUser);
372     if (ret != SENSOR_SUCCESS) {
373         WVLOG_E("SubscribeOhosSensor error, call SubscribeSensor ret = %{public}d.", ret);
374         return ret;
375     }
376     ret = SetBatch(ohosSensorTypeId, &mSensorUser, samplingInterval, samplingInterval);
377     if (ret != SENSOR_SUCCESS) {
378         WVLOG_E("SubscribeOhosSensor error, call SetBatch ret = %{public}d.", ret);
379         return ret;
380     }
381     ret = ActivateSensor(ohosSensorTypeId, &mSensorUser);
382     if (ret != SENSOR_SUCCESS) {
383         WVLOG_E("SubscribeOhosSensor error, call ActivateSensor ret = %{public}d.", ret);
384         return ret;
385     }
386     ret = SetMode(ohosSensorTypeId, &mSensorUser, SENSOR_REALTIME_MODE);
387     if (ret != SENSOR_SUCCESS) {
388         WVLOG_E("SubscribeOhosSensor error, call SetMode ret = %{public}d.", ret);
389         return ret;
390     }
391     return SENSOR_SUCCESS;
392 }
393 
RegistOhosSensorCallback(int32_t sensorTypeId,std::shared_ptr<SensorCallbackAdapter> callbackAdapter)394 int32_t SensorAdapterImpl::RegistOhosSensorCallback(int32_t sensorTypeId,
395     std::shared_ptr<SensorCallbackAdapter> callbackAdapter)
396 {
397     int32_t ohosSensorTypeId = SensorTypeToOhosSensorType(sensorTypeId);
398     if (ohosSensorTypeId != SENSOR_TYPE_ID_NONE) {
399         auto callback = std::make_shared<SensorCallbackImpl>(callbackAdapter);
400         std::lock_guard<std::mutex> lock(sensorCallbackMapMutex_);
401         sensorCallbackMap[ohosSensorTypeId] = callback;
402         return SENSOR_SUCCESS;
403     }
404     WVLOG_E("RegistOhosSensorCallback error, sensorTypeId is invalid.");
405     return SENSOR_PARAMETER_ERROR;
406 }
407 
UnsubscribeOhosSensor(int32_t sensorTypeId)408 int32_t SensorAdapterImpl::UnsubscribeOhosSensor(int32_t sensorTypeId)
409 {
410     WVLOG_I("UnsubscribeOhosSensor sensorTypeId: %{public}d.", sensorTypeId);
411     int32_t ohosSensorTypeId = SensorTypeToOhosSensorType(sensorTypeId);
412     if (ohosSensorTypeId != SENSOR_TYPE_ID_NONE) {
413         int32_t ret = DeactivateSensor(ohosSensorTypeId, &mSensorUser);
414         if (ret != SENSOR_SUCCESS) {
415             WVLOG_E("UnsubscribeOhosSensor error, call DeactivateSensor ret = %{public}d.", ret);
416             return ret;
417         }
418         ret = UnsubscribeSensor(ohosSensorTypeId, &mSensorUser);
419         if (ret != SENSOR_SUCCESS) {
420             WVLOG_E("UnsubscribeOhosSensor error, call UnsubscribeSensor ret = %{public}d.", ret);
421             return ret;
422         }
423         std::lock_guard<std::mutex> lock(sensorCallbackMapMutex_);
424         sensorCallbackMap.erase(ohosSensorTypeId);
425         return SENSOR_SUCCESS;
426     }
427     WVLOG_E("UnsubscribeOhosSensor error, sensorTypeId is invalid.");
428     return SENSOR_PARAMETER_ERROR;
429 }
430 } // namespace OHOS::NWeb