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 CROS_EC_SENSORS_H
18 #define CROS_EC_SENSORS_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 <hardware/sensors.h>
27 
28 #define IIO_DIR  "/sys/bus/iio/devices/"
29 #define IIO_MAX_NAME_LENGTH 30
30 #define IIO_MAX_BUFF_SIZE 4096
31 #define INT32_CHAR_LEN 12
32 
33 /*
34  * Use sizeof(...) - 1 instead of strlen because clang FORTIFY makes strlen
35  * non-constant.
36  */
37 #define IIO_MAX_DEVICE_NAME_LENGTH (sizeof(IIO_DIR) - 1 + IIO_MAX_NAME_LENGTH)
38 
39 #define CROS_EC_MAX_SAMPLING_PERIOD ((1 << 16) - 2)
40 
41 enum {X, Y, Z, MAX_AXIS};
42 
43 extern const char *cros_ec_sensor_names[];
44 
45 #define CROS_EC_EVENT_FLUSH_FLAG 0x1
46 #define CROS_EC_EVENT_WAKEUP_FLAG 0x2
47 
48 #define CROS_EC_MAX_PHYSICAL_SENSOR 256
49 
50 enum cros_ec_gesture {
51     CROS_EC_SIGMO,
52     CROS_EC_MAX_GESTURE,
53 };
54 
55 
56 /*****************************************************************************/
57 /* from ec_commands.h */
58 struct cros_ec_event {
59     uint8_t sensor_id;
60     uint8_t flags;
61     union {
62         int16_t vector[MAX_AXIS];
63         struct {
64             uint8_t activity;
65             uint8_t state;
66             uint16_t add_info[2];
67         };
68     };
69     uint64_t timestamp;
70 } __packed;
71 
72 enum motionsensor_activity {
73     MOTIONSENSE_ACTIVITY_RESERVED = 0,
74     MOTIONSENSE_ACTIVITY_SIG_MOTION = 1,
75     MOTIONSENSE_MAX_ACTIVITY,
76 };
77 
78 
79 /*****************************************************************************/
80 
81 enum cros_ec_sensor_device {
82     CROS_EC_ACCEL,
83     CROS_EC_GYRO,
84     CROS_EC_MAG,
85     CROS_EC_PROX,
86     CROS_EC_LIGHT,
87     CROS_EC_ACTIVITY,
88     CROS_EC_RING, /* should be the last device */
89     CROS_EC_MAX_DEVICE,
90 };
91 
92 struct cros_ec_sensor_info {
93     /* description of the sensor, as reported to sensorservice. */
94     sensor_t sensor_data;
95     enum cros_ec_sensor_device type;
96     const char *device_name;
97     int64_t sampling_period_ns;
98     int64_t max_report_latency_ns;
99     bool enabled;
100 };
101 
102 struct cros_ec_gesture_info {
103     /* For activities managed by the sensor interface */
104     sensor_t sensor_data;
105     const char *device_name;
106     const char *enable_entry;
107     bool enabled;
108 };
109 
110 /*
111  * To write sysfs parameters: IIO_DIR is appended before path.
112  */
113 int cros_ec_sysfs_set_input_attr(const char *path, const char *attr, const char *value, size_t len);
114 int cros_ec_sysfs_set_input_attr_by_int(const char *path, const char *attr, int value);
115 
116 /*
117  * To read sysfs parameters: IIO_DIR is NOT appended.
118  */
119 int cros_ec_sysfs_get_attr(const char *path, const char *attr, char *output);
120 
121 class CrosECSensor {
122     struct cros_ec_sensor_info *mSensorInfo;
123     size_t mSensorNb;
124     struct cros_ec_gesture_info *mGestureInfo;
125     size_t mGestureNb;
126     char mRingPath[IIO_MAX_DEVICE_NAME_LENGTH];
127     cros_ec_event mEvents[IIO_MAX_BUFF_SIZE];
128     int mDataFd;
129 
130     int processEvent(sensors_event_t* data, const cros_ec_event *event);
131 public:
132     CrosECSensor(
133         struct cros_ec_sensor_info *sensor_info,
134         size_t sensor_nb,
135         struct cros_ec_gesture_info *gesture_info,
136         size_t gesture_nb,
137         const char *ring_device_name,
138         const char *trigger_name);
139     virtual ~CrosECSensor();
140     virtual int getFd(void);
141     int readEvents(sensors_event_t* data, int count);
142 
143     virtual int activate(int handle, int enabled);
144     virtual int batch(int handle, int64_t period_ns, int64_t timeout);
145     virtual int flush(int handle);
146 };
147 
148 #endif  // CROS_EC_SENSORS_H
149