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