OSDN Git Service

libsensors: make the implementation be compatible with new IIO ABI
[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     unsigned index;
41     unsigned real_bytes;
42     unsigned bytes;
43     unsigned shift;
44     unsigned mask;
45     unsigned is_signed;
46     unsigned enabled;
47 };
48
49 /**
50  * Input device based sensors must inherit this class.
51  * The readEvents loop is already defined by this base class,
52  * inheritors need to define only processEvent for doing sensor-specific
53  * event computations.
54  */
55 class SensorIIODev: public SensorBase{
56
57 private:
58     bool initialized;
59     int device_number;
60     std::stringstream dev_device_name;
61     std::stringstream scan_el_dir;
62     std::stringstream buffer_dir_name;
63     std::vector < SensorIIOChannel > info_array;
64     int num_channels;
65     int buffer_len;
66     int enable_buffer;
67     int file_id;
68     int datum_size;
69     std::string scale_str;
70     std::string offset_str;
71     std::string device_name;
72     std::string channel_prefix_str;
73     float scale;
74     float offset;
75     int retry_count;
76     unsigned char *raw_buffer;
77     int mRefCount;
78
79     int discover();
80     int EnableIIODevice();
81     int GetDir(const std::string& dir, std::vector < std::string >& files);
82     void ListFiles(const std::string& dir);
83     int FindDeviceNumberFromName(const std::string& name, const std::string& prefix);
84
85     int BuildChannelList();
86     int SetUpTrigger(int dev_num);
87     int SetUpBufferLen(int len);
88     int GetSizeFromChannels();
89     int ParseIIODirectory(const std::string& name);
90     int EnableChannels();
91     int AllocateRxBuffer();
92     int FreeRxBuffer();
93
94 protected:
95
96     // Subclasses (e.g. HID devices) may implement "non-streaming"
97     // sensors with a non-constant sample rate, leveraging a
98     // microcontroller "sensor hub" to do the sampling and interrupt
99     // the CPU only on change.  Google allows this in the api by
100     // setting minDelay to 0 in sensor_t.  But if we do that, the
101     // framework has no idea what values can be set so often tries
102     // implausibly high values for "game" mode.  Setting this to
103     // non-zero allows the subclasses to clamp to device-specific
104     // values.
105     int sample_delay_min_ms;
106
107     bool IsDeviceInitialized();
108     int GetDeviceNumber();
109     int SetDataReadyTrigger(int dev_num, bool status);
110     int EnableBuffer(int status);
111     int SetSampleDelay(int dev_num, int rate);
112     int DeviceActivate(int dev_num, int state);
113     double DeviceGetSensitivity(int dev_num);
114     int DeviceSetSensitivity(int dev_num, double value);
115     float GetScaleValue();
116     float GetOffsetValue();
117     int ReadHIDScaleValue(float *scale);
118     int ReadHIDOffsetValue(float *offset);
119     int GetChannelBytesUsedSize(unsigned int channel_no);
120     virtual int processEvent(unsigned char *raw_data, size_t raw_data_len)
121         = 0;
122     virtual int readEvents(sensors_event_t *data, int count);
123     virtual int enable(int enabled);
124     virtual int setDelay(int64_t delay_ns);
125     virtual int setInitialState();
126
127 public:
128     SensorIIODev(const std::string& dev_name, const std::string& scale, const std::string& offset, const std::string& channel_prefix, int retry_cnt = 1);
129
130     // start/stop stream without changing "enabled" status. For slaves.
131     int startStop(int enabled);
132 };
133 #endif