blob: 58500fda7a50065ca6f6c1aa98e12565b539e264 [file] [log] [blame]
William A. Kennington III05c6d5f2018-08-16 00:47:58 -07001#pragma once
William A. Kennington III05c6d5f2018-08-16 00:47:58 -07002#include <gpioplus/chip.hpp>
3#include <gpioplus/internal/fd.hpp>
Patrick Williams7ba248a2023-05-10 07:51:30 -05004
5#include <cstdint>
William A. Kennington III12cd2542018-08-22 21:17:56 -07006#include <string_view>
William A. Kennington III05c6d5f2018-08-16 00:47:58 -07007#include <vector>
8
9namespace gpioplus
10{
11
William A. Kennington III8ebe5252018-08-27 12:39:24 -070012/** @brief Flags to set when taking a handle */
William A. Kennington III05c6d5f2018-08-16 00:47:58 -070013struct HandleFlags
14{
William A. Kennington III8ebe5252018-08-27 12:39:24 -070015 /** @brief Is the line used for output (otherwise input) */
William A. Kennington III05c6d5f2018-08-16 00:47:58 -070016 bool output;
William A. Kennington III8ebe5252018-08-27 12:39:24 -070017 /** @brief Is the line value active at low voltage */
William A. Kennington III05c6d5f2018-08-16 00:47:58 -070018 bool active_low;
William A. Kennington III8ebe5252018-08-27 12:39:24 -070019 /** @brief Is the line an open drain */
William A. Kennington III05c6d5f2018-08-16 00:47:58 -070020 bool open_drain;
William A. Kennington III8ebe5252018-08-27 12:39:24 -070021 /** @brief Is the line an open source */
William A. Kennington III05c6d5f2018-08-16 00:47:58 -070022 bool open_source;
23
William A. Kennington III16e7f112018-08-17 13:15:14 -070024 HandleFlags() = default;
William A. Kennington III8ebe5252018-08-27 12:39:24 -070025
26 /** @brief Creates handle flags from the gpio line flags
27 *
28 * @param[in] line_flags - Line flags used for population
29 */
William A. Kennington III05c6d5f2018-08-16 00:47:58 -070030 explicit HandleFlags(LineFlags line_flags);
William A. Kennington III16e7f112018-08-17 13:15:14 -070031
William A. Kennington III8ebe5252018-08-27 12:39:24 -070032 /** @brief Converts this struct to an int bitfield
33 *
34 * @return The int bitfield usable by the syscall interface
35 */
William A. Kennington III05c6d5f2018-08-16 00:47:58 -070036 uint32_t toInt() const;
37};
38
Patrick Venture526fc7d2018-12-19 09:59:29 -080039/** @class HandleInterface
40 * @brief Handle interface to provide a set of methods required to exist in
41 * derived objects.
42 */
43class HandleInterface
44{
45 public:
46 virtual ~HandleInterface() = default;
47
48 virtual std::vector<uint8_t> getValues() const = 0;
49 virtual void getValues(std::vector<uint8_t>& values) const = 0;
50 virtual void setValues(const std::vector<uint8_t>& values) const = 0;
51};
52
William A. Kennington III8ebe5252018-08-27 12:39:24 -070053/** @class Handle
54 * @brief Handle to a gpio line handle
55 * @details Provides a c++ interface for gpio handle operations
56 */
Patrick Venture526fc7d2018-12-19 09:59:29 -080057class Handle : public HandleInterface
William A. Kennington III05c6d5f2018-08-16 00:47:58 -070058{
59 public:
William A. Kennington III8ebe5252018-08-27 12:39:24 -070060 /** @brief Per line information used to construct a handle */
William A. Kennington III05c6d5f2018-08-16 00:47:58 -070061 struct Line
62 {
William A. Kennington III8ebe5252018-08-27 12:39:24 -070063 /** @brief Offset of the line on the gpio chip */
William A. Kennington III05c6d5f2018-08-16 00:47:58 -070064 uint32_t offset;
William A. Kennington III8ebe5252018-08-27 12:39:24 -070065 /** @brief Default output value of the line */
William A. Kennington III05c6d5f2018-08-16 00:47:58 -070066 uint8_t default_value;
67 };
William A. Kennington III8ebe5252018-08-27 12:39:24 -070068
69 /** @brief Creates a new gpio handle
William A. Kennington III4d40f762018-09-20 13:23:50 -070070 * The underlying implementation of the handle is independent of
71 * the provided chip object. It is safe to destroy any of the
72 * provided inputs while this handle is alive.
William A. Kennington III8ebe5252018-08-27 12:39:24 -070073 *
74 * @param[in] chip - The gpio chip which provides the handle
75 * @param[in] lines - A collection of lines the handle provides
76 * @param[in] flags - The flags applied to all lines
77 * @param[in] consumer_label - The functional name of this consumer
78 * @throws std::system_error for underlying syscall failures
79 */
William A. Kennington III05c6d5f2018-08-16 00:47:58 -070080 Handle(const Chip& chip, const std::vector<Line>& lines, HandleFlags flags,
William A. Kennington III12cd2542018-08-22 21:17:56 -070081 std::string_view consumer_label);
William A. Kennington III05c6d5f2018-08-16 00:47:58 -070082
William A. Kennington III8ebe5252018-08-27 12:39:24 -070083 /** @brief Get the file descriptor used for the handle
84 *
85 * @return The gpio handle file descriptor
86 */
William A. Kennington III05c6d5f2018-08-16 00:47:58 -070087 const internal::Fd& getFd() const;
88
William A. Kennington III8ebe5252018-08-27 12:39:24 -070089 /** @brief Get the current values of all associated lines
90 *
91 * @throws std::system_error for underlying syscall failures
92 * @return The values of the gpio lines
93 */
Patrick Venture526fc7d2018-12-19 09:59:29 -080094 std::vector<uint8_t> getValues() const override;
William A. Kennington III8ebe5252018-08-27 12:39:24 -070095
96 /** @brief Gets the current values of all associated lines
97 *
98 * @param[out] values - The values of the gpio lines
99 * @throws std::system_error for underlying syscall failures
100 */
Patrick Venture526fc7d2018-12-19 09:59:29 -0800101 void getValues(std::vector<uint8_t>& values) const override;
William A. Kennington III8ebe5252018-08-27 12:39:24 -0700102
103 /** @brief Sets the current values of all associated lines
104 *
105 * @param[in] values - The new values of the gpio lines
106 * @throws std::system_error for underlying syscall failures
107 */
Patrick Venture526fc7d2018-12-19 09:59:29 -0800108 void setValues(const std::vector<uint8_t>& values) const override;
William A. Kennington III05c6d5f2018-08-16 00:47:58 -0700109
110 private:
111 internal::Fd fd;
112 uint32_t nlines;
113};
114
115} // namespace gpioplus