1 /*
2  * Copyright (C) 2008-2014 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef ANDROID_CWMCU_SENSOR_H
18 #define ANDROID_CWMCU_SENSOR_H
19 
20 #include <errno.h>
21 #include <stdint.h>
22 #include <sys/cdefs.h>
23 #include <sys/types.h>
24 #include <utils/BitSet.h>
25 
26 #include "InputEventReader.h"
27 #include "sensors.h"
28 #include "SensorBase.h"
29 
30 /*****************************************************************************/
31 
32 // Must match driver copy of .../linux/include/linux/CwMcuSensor.h file
33 typedef enum {
34     CW_ACCELERATION                = 0,
35     CW_MAGNETIC                    = 1,
36     CW_GYRO                        = 2,
37     CW_LIGHT                       = 3,
38     CW_PRESSURE                    = 5,
39     CW_ORIENTATION                 = 6,
40     CW_ROTATIONVECTOR              = 7,
41     CW_LINEARACCELERATION          = 8,
42     CW_GRAVITY                     = 9,
43     CW_MAGNETIC_UNCALIBRATED       = 16,
44     CW_GYROSCOPE_UNCALIBRATED      = 17,
45     CW_GAME_ROTATION_VECTOR        = 18,
46     CW_GEOMAGNETIC_ROTATION_VECTOR = 19,
47     CW_SIGNIFICANT_MOTION          = 20,
48     CW_STEP_DETECTOR               = 21,
49     CW_STEP_COUNTER                = 22,
50     HTC_ANY_MOTION                 = 28,
51     //Above are Firmware supported sensors,
52     CW_ACCELERATION_W                = 32,
53     CW_MAGNETIC_W                    = 33,
54     CW_GYRO_W                        = 34,
55     CW_PRESSURE_W                    = 37,
56     CW_ORIENTATION_W                 = 38,
57     CW_ROTATIONVECTOR_W              = 39,
58     CW_LINEARACCELERATION_W          = 40,
59     CW_GRAVITY_W                     = 41,
60     HTC_WAKE_UP_GESTURE_W            = 42,
61     CW_MAGNETIC_UNCALIBRATED_W       = 48,
62     CW_GYROSCOPE_UNCALIBRATED_W      = 49,
63     CW_GAME_ROTATION_VECTOR_W        = 50,
64     CW_GEOMAGNETIC_ROTATION_VECTOR_W = 51,
65     CW_STEP_DETECTOR_W               = 53,
66     CW_STEP_COUNTER_W                = 54,
67     CW_SENSORS_ID_END,
68     TIME_DIFF_EXHAUSTED              = 97,
69     CW_TIME_BASE                     = 98,
70     CW_META_DATA                     = 99,
71     CW_MAGNETIC_UNCALIBRATED_BIAS    = 100,
72     CW_GYROSCOPE_UNCALIBRATED_BIAS   = 101
73 } CW_SENSORS_ID;
74 
75 #define        SAVE_PATH_ACC                                "/data/misc/AccOffset.txt"
76 #define        SAVE_PATH_MAG                                "/data/misc/cw_calibrator_mag.ini"
77 #define        SAVE_PATH_GYRO                                "/data/system/cw_calibrator_gyro.ini"
78 
79 #define        BOOT_MODE_PATH                                "sys/class/htc_sensorhub/sensor_hub/boot_mode"
80 
81 #define        numSensors        CW_SENSORS_ID_END
82 
83 #define TIMESTAMP_SYNC_CODE        (98)
84 
85 #define PERIODIC_SYNC_TIME_SEC     (5)
86 
87 class CwMcuSensor : public SensorBase {
88 
89         android::BitSet64 mEnabled;
90         InputEventCircularReader mInputReader;
91         sensors_event_t mPendingEvents[numSensors];
92         sensors_event_t mPendingEventsFlush;
93         android::BitSet64 mPendingMask;
94         char fixed_sysfs_path[PATH_MAX];
95         int fixed_sysfs_path_len;
96 
97         float indexToValue(size_t index) const;
98         char mDevPath[PATH_MAX];
99         char mTriggerName[PATH_MAX];
100 
101         float time_slope;
102         int64_t time_offset;
103         uint64_t last_mcu_sync_time;
104         uint64_t last_cpu_sync_time;
105 
106         bool offset_reset[numSensors];
107         uint64_t last_mcu_timestamp[numSensors];
108         uint64_t last_cpu_timestamp[numSensors];
109         pthread_t sync_time_thread;
110 
111         bool init_trigger_done;
112 
113         int sysfs_set_input_attr(const char *attr, char *value, size_t len);
114         int sysfs_set_input_attr_by_int(const char *attr, int value);
115 public:
116         CwMcuSensor();
117         virtual ~CwMcuSensor();
118         virtual int readEvents(sensors_event_t* data, int count);
119         virtual bool hasPendingEvents() const;
120         virtual int setDelay(int32_t handle, int64_t ns);
121         virtual int setEnable(int32_t handle, int enabled);
122         virtual int getEnable(int32_t handle);
123         virtual int batch(int handle, int flags, int64_t period_ns, int64_t timeout);
124         virtual int flush(int handle);
125         bool is_batch_wake_sensor(int32_t handle);
126         int find_sensor(int32_t handle);
127         int find_handle(int32_t sensors_id);
128         void cw_save_calibrator_file(int type, const char * path, int* str);
129         int cw_read_calibrator_file(int type, const char * path, int* str);
130         int processEvent(uint8_t *event);
131         void calculate_rv_4th_element(int sensors_id);
132         void sync_time_thread_in_class(void);
133 };
134 
135 /*****************************************************************************/
136 
137 #endif  // ANDROID_CWMCU_SENSOR_H
138