OSDN Git Service

libsensors: Added support for Pressure sensor
[android-x86/hardware-intel-libsensors.git] / SensorIIODev.h
1 /*
2  * Copyright (C) 2010-2012 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 ANDROID_SENSOR_IIO_DEV_H
18 #define ANDROID_SENSOR_IIO_DEV_H
19
20 #include <iostream>
21 #include <string>
22 #include <cstring>
23 #include <string>
24 #include <sstream>
25 #include <iostream>
26 #include <fstream>
27 #include <vector>
28 #include <fcntl.h>
29 #include <unistd.h>
30 #include <stdarg.h>
31 #include <poll.h>
32 #include <dirent.h>
33 #include <errno.h>
34 #include "SensorBase.h"
35 #include "Helpers.h"
36
37 // Used by SensorIIO device containers
38 struct SensorIIOChannel{
39     std::string name;
40     float scale;
41     float offset;
42     unsigned index;
43     unsigned real_bytes;
44     unsigned bytes;
45     unsigned shift;
46     unsigned mask;
47     unsigned is_signed;
48     unsigned enabled;
49 };
50
51 /**
52  * Input device based sensors must inherit this class.
53  * The readEvents loop is already defined by this base class,
54  * inheritors need to define only processEvent for doing sensor-specific
55  * event computations.
56  */
57 class SensorIIODev: public SensorBase{
58
59 private:
60     bool initialized;
61     int device_number;
62     std::stringstream dev_device_name;
63     std::stringstream scan_el_dir;
64     std::stringstream buffer_dir_name;
65     std::vector < SensorIIOChannel > info_array;
66     int num_channels;
67     int buffer_len;
68     int enable_buffer;
69     int file_id;
70     int datum_size;
71     std::string unit_expo_str;
72     std::string unit_str;
73     std::string device_name;
74     std::string channel_prefix_str;
75     long unit_expo_value;
76     long units_value;
77     int retry_count;
78     unsigned char *raw_buffer;
79     int mRefCount;
80
81     int discover();
82     int EnableIIODevice();
83     int GetDir(const std::string& dir, std::vector < std::string >& files);
84     void ListFiles(const std::string& dir);
85     int FindDeviceNumberFromName(const std::string& name, const std::string& prefix);
86
87     int BuildChannelList();
88     int SetUpTrigger(int dev_num);
89     int SetUpBufferLen(int len);
90     int GetSizeFromChannels();
91     int ParseIIODirectory(const std::string& name);
92     int EnableChannels();
93     int AllocateRxBuffer();
94     int FreeRxBuffer();
95
96 protected:
97
98     // Subclasses (e.g. HID devices) may implement "non-streaming"
99     // sensors with a non-constant sample rate, leveraging a
100     // microcontroller "sensor hub" to do the sampling and interrupt
101     // the CPU only on change.  Google allows this in the api by
102     // setting minDelay to 0 in sensor_t.  But if we do that, the
103     // framework has no idea what values can be set so often tries
104     // implausibly high values for "game" mode.  Setting this to
105     // non-zero allows the subclasses to clamp to device-specific
106     // values.
107     int sample_delay_min_ms;
108
109     bool IsDeviceInitialized();
110     int GetDeviceNumber();
111     int SetDataReadyTrigger(int dev_num, bool status);
112     int EnableBuffer(int status);
113     int SetSampleDelay(int dev_num, int rate);
114     int DeviceActivate(int dev_num, int state);
115     double DeviceGetSensitivity(int dev_num);
116     int DeviceSetSensitivity(int dev_num, double value);
117     long GetUnitValue();
118     long GetExponentValue();
119     int ReadHIDMeasurmentUnit(long *unit);
120     int ReadHIDExponentValue(long *exponent);
121     int GetChannelBytesUsedSize(unsigned int channel_no);
122     virtual int processEvent(unsigned char *raw_data, size_t raw_data_len)
123         = 0;
124     virtual int readEvents(sensors_event_t *data, int count);
125     virtual int enable(int enabled);
126     virtual int setDelay(int64_t delay_ns);
127     virtual int setInitialState();
128
129 public:
130     SensorIIODev(const std::string& dev_name, const std::string& units, const std::string& exponent, const std::string& channel_prefix);
131     SensorIIODev(const std::string& dev_name, const std::string& units, const std::string& exponent, const std::string& channel_prefix, int retry_cnt);
132
133     // start/stop stream without changing "enabled" status. For slaves.
134     int startStop(int enabled);
135 };
136 #endif