blob: 85da5015e1aa132019327de1a109b255f581b31a [file] [log] [blame]
Enrico Granata60a818d2019-05-09 09:56:09 -07001// 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 Granata51cdb942019-06-18 16:40:17 -07005#ifndef LIBMEMS_IIO_CONTEXT_H_
6#define LIBMEMS_IIO_CONTEXT_H_
Enrico Granata60a818d2019-05-09 09:56:09 -07007
Harvey Yang9260b662019-08-12 15:48:03 +08008#include <iio.h>
9
Gwendal Grignoua1446472020-06-30 18:00:05 -070010#include <map>
11#include <memory>
Enrico Granata60a818d2019-05-09 09:56:09 -070012#include <string>
Harvey Yang9260b662019-08-12 15:48:03 +080013#include <vector>
Enrico Granata60a818d2019-05-09 09:56:09 -070014
15#include <base/macros.h>
16
Enrico Granata51cdb942019-06-18 16:40:17 -070017#include "libmems/export.h"
18
19namespace libmems {
Enrico Granata60a818d2019-05-09 09:56:09 -070020
21class 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 Yang9260b662019-08-12 15:48:03 +080025// retrieved via GetDevicesByName, GetDeviceById and GetAllDevices, providing
26// the devices's name, id or nothing as input.
Enrico Granata51cdb942019-06-18 16:40:17 -070027class LIBMEMS_EXPORT IioContext {
Enrico Granata60a818d2019-05-09 09:56:09 -070028 public:
29 virtual ~IioContext() = default;
30
Harvey Yang9260b662019-08-12 15:48:03 +080031 // 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 Granata60a818d2019-05-09 09:56:09 -070035 // 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 Yang6698c862019-09-16 17:24:38 +080042 // 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 Yang9260b662019-08-12 15:48:03 +080049 // 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 Granata60a818d2019-05-09 09:56:09 -070089
90 protected:
91 IioContext() = default;
Qijiang Fan6bc59e12020-11-11 02:51:06 +090092 IioContext(const IioContext&) = delete;
93 IioContext& operator=(const IioContext&) = delete;
Enrico Granata60a818d2019-05-09 09:56:09 -070094};
95
Enrico Granata51cdb942019-06-18 16:40:17 -070096} // namespace libmems
Enrico Granata60a818d2019-05-09 09:56:09 -070097
Enrico Granata51cdb942019-06-18 16:40:17 -070098#endif // LIBMEMS_IIO_CONTEXT_H_