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 #ifndef SENSOR_ADAPTER_IMPL_H
17 #define SENSOR_ADAPTER_IMPL_H
18 
19 #include "sensor_adapter.h"
20 #include "sensor_agent.h"
21 
22 #include <mutex>
23 
24 namespace OHOS::NWeb {
25 
26 class SensorCallbackImpl {
27 public:
28     SensorCallbackImpl(std::shared_ptr<SensorCallbackAdapter> callbackAdapter);
29     ~SensorCallbackImpl() = default;
30 
31     void UpdateOhosSensorData(double timestamp,
32         double value1, double value2, double value3, double value4);
33 private:
34     std::shared_ptr<SensorCallbackAdapter> callbackAdapter_;
35 };
36 
37 class SensorAdapterImpl : public SensorAdapter {
38 public:
39     SensorAdapterImpl() = default;
40     ~SensorAdapterImpl() = default;
41 
42     int32_t IsOhosSensorSupported(int32_t sensorTypeId) override;
43     int32_t GetOhosSensorReportingMode(int32_t sensorTypeId) override;
44     double GetOhosSensorDefaultSupportedFrequency(int32_t sensorTypeId) override;
45     double GetOhosSensorMinSupportedFrequency(int32_t sensorTypeId) override;
46     double GetOhosSensorMaxSupportedFrequency(int32_t sensorTypeId) override;
47     int32_t SubscribeOhosSensor(int32_t sensorTypeId, int64_t samplingInterval) override;
48     int32_t RegistOhosSensorCallback(int32_t sensorTypeId,
49         std::shared_ptr<SensorCallbackAdapter> callbackAdapter) override;
50     int32_t UnsubscribeOhosSensor(int32_t sensorTypeId) override;
51 
52 private:
53     static void OhosSensorCallback(SensorEvent* event);
54     static std::unordered_map<int32_t, std::shared_ptr<SensorCallbackImpl>> sensorCallbackMap;
55     static std::mutex sensorCallbackMapMutex_;
56 
57     static void handleAccelerometerData(std::shared_ptr<OHOS::NWeb::SensorCallbackImpl> callback,
58         SensorEvent* event);
59     static void handleLinearAccelerometerData(std::shared_ptr<OHOS::NWeb::SensorCallbackImpl> callback,
60         SensorEvent* event);
61     static void handleGravityData(std::shared_ptr<OHOS::NWeb::SensorCallbackImpl> callback,
62         SensorEvent* event);
63     static void handleCyroscopeData(std::shared_ptr<OHOS::NWeb::SensorCallbackImpl> callback,
64         SensorEvent* event);
65     static void handleMagnetometerData(std::shared_ptr<OHOS::NWeb::SensorCallbackImpl> callback,
66         SensorEvent* event);
67     static void handleOrientationData(std::shared_ptr<OHOS::NWeb::SensorCallbackImpl> callback,
68         SensorEvent* event);
69     static void handleRotationVectorData(std::shared_ptr<OHOS::NWeb::SensorCallbackImpl> callback,
70         SensorEvent* event);
71     static void handleGameRotationVectorData(std::shared_ptr<OHOS::NWeb::SensorCallbackImpl> callback,
72         SensorEvent* event);
73 
74     SensorUser mSensorUser{};
75 };
76 
77 }
78 #endif // SENSOR_ADAPTER_IMPL_H