1/*
2 * Copyright (C) 2015 Intel Corporation
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 SENSOR_HPP
18#define SENSOR_HPP
19
20/**
21 * The default sensor __attribute__((constructor)) priority is represented by
22 * the first available priority value. The [0, 100] ones are used by the system
23 * implementation.
24 */
25#define DEFAULT_SENSOR_CONSTRUCTOR_PRIORITY 101
26
27#include "AcquisitionThread.hpp"
28
29struct sensors_event_t;
30class AcquisitionThread;
31
32/**
33 * Sensor representation class
34 *
35 * It supports sensor enabling/disabling, changing the sensor's parameters
36 * and event reading.
37 */
38class Sensor {
39  public:
40    /**
41     * Sensor constructor
42     */
43    Sensor();
44
45    /**
46     * Sensor destructor
47     */
48    virtual ~Sensor();
49
50    /**
51     * Activate the sensor
52     * @param handle sensor identifier
53     * @param enabled 1 for enabling and 0 for disabling
54     * @return 0 on success and a negative error number otherwise
55     */
56    virtual int activate(int handle, int enabled);
57
58    /**
59     * Set delay
60     * @param handle sensor identifier
61     * @param ns the sampling period at which the sensor should run, in nanoseconds
62     * @return 0 on success and a negative error number otherwise
63     */
64    virtual int setDelay(int handle, int64_t ns);
65
66    /**
67     * Poll for events
68     * @param data where to store the events
69     * @param count the number of events returned must be <= to the count
70     * @return number of events returned in data on success and a negative error number otherwise
71     */
72    virtual int pollEvents(sensors_event_t* data, int count) = 0;
73
74    /**
75     * Sets a sensor’s parameters, including sampling frequency and maximum report latency
76     * @param handle sensor identifier
77     * @param flags currently unused
78     * @param period_ns the sampling period at which the sensor should run, in nanoseconds
79     * @param timeout the maximum time by which events can be delayed before being reported
80     *          through the HAL, in nanoseconds
81     * @return 0 on success and a negative error number otherwise
82     */
83    virtual int batch(int handle, int flags, int64_t period_ns, int64_t timeout);
84
85    /**
86     * Add a flush complete event to the end of the hardware FIFO for the specified sensor and flushes the FIFO
87     * @param handle sensor identifier
88     * @return 0 on success and a negative error number otherwise
89     */
90    virtual int flush(int handle);
91
92    /**
93     * Read and store an event
94     * @param event where to store the event
95     * @return true on success and a false otherwise
96     */
97    virtual bool readOneEvent(sensors_event_t *event);
98
99    /**
100     * Get sensor identifier
101     * @return sensor handle
102     */
103    int getHandle() { return handle; }
104
105    /**
106     * Get sensor type
107     * @return sensor type
108     */
109    int getType() { return type; }
110
111    /**
112     * Get sensor delay in nanoseconds
113     * @return sensor delay
114     */
115    int64_t getDelay() { return delay; }
116
117    /**
118     * Gravitational acceleration constant in m/s^2
119     */
120    static const float kGravitationalAcceleration;
121
122  protected:
123    /**
124     * Enable or disable the associated acquisition thread
125     * @param pollFd poll file descriptor
126     * @param handle sensor identifier
127     * @param enabled 1 for enabling and 0 for disabling
128     * @return 0 on success and a negative error number otherwise
129     */
130    virtual int activateAcquisitionThread(int pollFd, int handle, int enabled);
131
132    AcquisitionThread *acquisitionThread;
133    int handle, type;
134    int64_t delay;
135};
136
137#endif  // SENSOR_HPP
138