1 /*
2  * Copyright (C) 2020 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 #ifndef ANDROID_SENSORS_IIO_UTILS_H
17 #define ANDROID_SENSORS_IIO_UTILS_H
18 
19 #include <android/hardware/sensors/2.1/types.h>
20 #include <dirent.h>
21 #include <linux/ioctl.h>
22 #include <linux/types.h>
23 #include <stdint.h>
24 #include <sys/ioctl.h>
25 #include <functional>
26 
27 namespace android {
28 namespace hardware {
29 namespace sensors {
30 namespace V2_1 {
31 namespace subhal {
32 namespace implementation {
33 
34 using ::android::hardware::sensors::V2_1::SensorType;
35 
36 extern const char* DEFAULT_IIO_DIR;
37 
38 static constexpr auto DEFAULT_IIO_BUFFER_LEN = 2;
39 static constexpr auto DISABLE_CHANNEL = 0;
40 static constexpr auto ENABLE_CHANNEL = 1;
41 
42 struct sensors_supported_hal {
43     std::string name;
44     SensorType type;
45 };
46 
47 struct iio_info_channel {
48     std::string name;
49     uint8_t index;
50     uint8_t storage_bytes;
51     uint8_t bits_used;
52     uint8_t shift;
53     bool big_endian;
54     bool sign;
55 };
56 
57 struct iio_device_data {
58     std::string name;
59     std::string sysfspath;
60     float resolution;
61     float scale;
62     SensorType type;
63     std::vector<iio_info_channel> channelInfo;
64     std::vector<double> sampling_freq_avl;
65     uint8_t iio_dev_num;
66     int64_t max_range;
67 };
68 
69 using DeviceFilterFunction = std::function<bool(iio_device_data*)>;
70 
71 int load_iio_devices(std::string iio_dir, std::vector<iio_device_data>* iio_data,
72                      DeviceFilterFunction filter);
73 int scan_elements(const std::string& device_dir, struct iio_device_data* iio_data);
74 int enable_sensor(const std::string& name, const bool flag);
75 int set_sampling_frequency(const std::string& name, const double frequency);
76 }  // namespace implementation
77 }  // namespace subhal
78 }  // namespace V2_1
79 }  // namespace sensors
80 }  // namespace hardware
81 }  // namespace android
82 #endif
83