Enrico Granata | 60a818d | 2019-05-09 09:56:09 -0700 | [diff] [blame] | 1 | // Copyright 2019 The Chromium OS Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
Enrico Granata | 51cdb94 | 2019-06-18 16:40:17 -0700 | [diff] [blame] | 5 | #ifndef LIBMEMS_IIO_CONTEXT_H_ |
| 6 | #define LIBMEMS_IIO_CONTEXT_H_ |
Enrico Granata | 60a818d | 2019-05-09 09:56:09 -0700 | [diff] [blame] | 7 | |
Harvey Yang | 9260b66 | 2019-08-12 15:48:03 +0800 | [diff] [blame] | 8 | #include <iio.h> |
| 9 | |
Gwendal Grignou | a144647 | 2020-06-30 18:00:05 -0700 | [diff] [blame] | 10 | #include <map> |
| 11 | #include <memory> |
Enrico Granata | 60a818d | 2019-05-09 09:56:09 -0700 | [diff] [blame] | 12 | #include <string> |
Harvey Yang | 9260b66 | 2019-08-12 15:48:03 +0800 | [diff] [blame] | 13 | #include <vector> |
Enrico Granata | 60a818d | 2019-05-09 09:56:09 -0700 | [diff] [blame] | 14 | |
| 15 | #include <base/macros.h> |
| 16 | |
Enrico Granata | 51cdb94 | 2019-06-18 16:40:17 -0700 | [diff] [blame] | 17 | #include "libmems/export.h" |
| 18 | |
| 19 | namespace libmems { |
Enrico Granata | 60a818d | 2019-05-09 09:56:09 -0700 | [diff] [blame] | 20 | |
| 21 | class IioDevice; |
| 22 | |
| 23 | // The IioContext is the root of the tree of IIO devices on the system. |
| 24 | // A context is - at its core - a container of devices, which can be |
Harvey Yang | 9260b66 | 2019-08-12 15:48:03 +0800 | [diff] [blame] | 25 | // retrieved via GetDevicesByName, GetDeviceById and GetAllDevices, providing |
| 26 | // the devices's name, id or nothing as input. |
Enrico Granata | 51cdb94 | 2019-06-18 16:40:17 -0700 | [diff] [blame] | 27 | class LIBMEMS_EXPORT IioContext { |
Enrico Granata | 60a818d | 2019-05-09 09:56:09 -0700 | [diff] [blame] | 28 | public: |
| 29 | virtual ~IioContext() = default; |
| 30 | |
Harvey Yang | 9260b66 | 2019-08-12 15:48:03 +0800 | [diff] [blame] | 31 | // Returns the iio_context object underlying this object, if any is available. |
| 32 | // Returns nullptr if no iio_device exists. |
| 33 | virtual iio_context* GetCurrentContext() const = 0; |
| 34 | |
Enrico Granata | 60a818d | 2019-05-09 09:56:09 -0700 | [diff] [blame] | 35 | // libiio loads the device list at context creation time, and does not |
| 36 | // have a way to update it as new devices appear on the system. |
| 37 | // This is a helper that allows a rescan of the system to find new devices |
| 38 | // dynamically at runtime. It should be called after any actions that cause |
| 39 | // new devices of interest to show up. |
| 40 | virtual void Reload() = 0; |
| 41 | |
Harvey Yang | 6698c86 | 2019-09-16 17:24:38 +0800 | [diff] [blame] | 42 | // Sets |timeout| in milliseconds for I/O operations, mainly for reading |
| 43 | // events. Sets |timeout| as 0 to specify that no timeout should occur. |
| 44 | // Default for network/unix_socket backend: 5000 milliseconds. |
| 45 | // Default for local backend: 1000 millisecond. |
| 46 | // Returns true if success. |
| 47 | virtual bool SetTimeout(uint32_t timeout) = 0; |
| 48 | |
Harvey Yang | 9260b66 | 2019-08-12 15:48:03 +0800 | [diff] [blame] | 49 | // Returns IioDevices as a vector given the device's name. Only devices with |
| 50 | // id having "iio:device" as the prefix would be available. |
| 51 | // Returns an empty vector if no device can be found. |
| 52 | // The device objects are guaranteed to stay valid as long as this context |
| 53 | // object is valid. |
| 54 | virtual std::vector<IioDevice*> GetDevicesByName(const std::string& name) = 0; |
| 55 | |
| 56 | // Returns an IioDevice given the device's ID by int. Real id in string would |
| 57 | // be "iio:device|id|". |
| 58 | // Returns nullptr if the device cannot be found. The |
| 59 | // device object is guaranteed to stay valid as long as this context object is |
| 60 | // valid. |
| 61 | virtual IioDevice* GetDeviceById(int id) = 0; |
| 62 | |
| 63 | // Returns all IioDevices as a vector. Only devices with id having |
| 64 | // "iio:device" as the prefix would be available. |
| 65 | // Returns an empty vector if no device can be found. |
| 66 | // The device objects are guaranteed to stay valid as long as this context |
| 67 | // object is valid. |
| 68 | virtual std::vector<IioDevice*> GetAllDevices() = 0; |
| 69 | |
| 70 | // Returns triggers as a vector given the trigger's name. |
| 71 | // Returns an empty vector if no device can be found. |
| 72 | // The trigger objects are guaranteed to stay valid as long as this context |
| 73 | // object is valid. |
| 74 | virtual std::vector<IioDevice*> GetTriggersByName( |
| 75 | const std::string& name) = 0; |
| 76 | |
| 77 | // Returns an IioDevice given the trigger's ID by int. Real id in string would |
| 78 | // be "trigger|id|". If |id| is -1, trigger iio_sysfs_trigger is returned. |
| 79 | // Returns nullptr if the device cannot be found. The |
| 80 | // device object is guaranteed to stay valid as long as this context object is |
| 81 | // valid. |
| 82 | virtual IioDevice* GetTriggerById(int id) = 0; |
| 83 | |
| 84 | // Returns all triggers as a vector. |
| 85 | // Returns an empty vector if no device can be found. |
| 86 | // The device objects are guaranteed to stay valid as long as this context |
| 87 | // object is valid. |
| 88 | virtual std::vector<IioDevice*> GetAllTriggers() = 0; |
Enrico Granata | 60a818d | 2019-05-09 09:56:09 -0700 | [diff] [blame] | 89 | |
| 90 | protected: |
| 91 | IioContext() = default; |
Qijiang Fan | 6bc59e1 | 2020-11-11 02:51:06 +0900 | [diff] [blame^] | 92 | IioContext(const IioContext&) = delete; |
| 93 | IioContext& operator=(const IioContext&) = delete; |
Enrico Granata | 60a818d | 2019-05-09 09:56:09 -0700 | [diff] [blame] | 94 | }; |
| 95 | |
Enrico Granata | 51cdb94 | 2019-06-18 16:40:17 -0700 | [diff] [blame] | 96 | } // namespace libmems |
Enrico Granata | 60a818d | 2019-05-09 09:56:09 -0700 | [diff] [blame] | 97 | |
Enrico Granata | 51cdb94 | 2019-06-18 16:40:17 -0700 | [diff] [blame] | 98 | #endif // LIBMEMS_IIO_CONTEXT_H_ |