William A. Kennington III | 05c6d5f | 2018-08-16 00:47:58 -0700 | [diff] [blame] | 1 | #pragma once |
William A. Kennington III | 05c6d5f | 2018-08-16 00:47:58 -0700 | [diff] [blame] | 2 | #include <gpioplus/chip.hpp> |
| 3 | #include <gpioplus/internal/fd.hpp> |
Patrick Williams | 7ba248a | 2023-05-10 07:51:30 -0500 | [diff] [blame^] | 4 | |
| 5 | #include <cstdint> |
William A. Kennington III | 12cd254 | 2018-08-22 21:17:56 -0700 | [diff] [blame] | 6 | #include <string_view> |
William A. Kennington III | 05c6d5f | 2018-08-16 00:47:58 -0700 | [diff] [blame] | 7 | #include <vector> |
| 8 | |
| 9 | namespace gpioplus |
| 10 | { |
| 11 | |
William A. Kennington III | 8ebe525 | 2018-08-27 12:39:24 -0700 | [diff] [blame] | 12 | /** @brief Flags to set when taking a handle */ |
William A. Kennington III | 05c6d5f | 2018-08-16 00:47:58 -0700 | [diff] [blame] | 13 | struct HandleFlags |
| 14 | { |
William A. Kennington III | 8ebe525 | 2018-08-27 12:39:24 -0700 | [diff] [blame] | 15 | /** @brief Is the line used for output (otherwise input) */ |
William A. Kennington III | 05c6d5f | 2018-08-16 00:47:58 -0700 | [diff] [blame] | 16 | bool output; |
William A. Kennington III | 8ebe525 | 2018-08-27 12:39:24 -0700 | [diff] [blame] | 17 | /** @brief Is the line value active at low voltage */ |
William A. Kennington III | 05c6d5f | 2018-08-16 00:47:58 -0700 | [diff] [blame] | 18 | bool active_low; |
William A. Kennington III | 8ebe525 | 2018-08-27 12:39:24 -0700 | [diff] [blame] | 19 | /** @brief Is the line an open drain */ |
William A. Kennington III | 05c6d5f | 2018-08-16 00:47:58 -0700 | [diff] [blame] | 20 | bool open_drain; |
William A. Kennington III | 8ebe525 | 2018-08-27 12:39:24 -0700 | [diff] [blame] | 21 | /** @brief Is the line an open source */ |
William A. Kennington III | 05c6d5f | 2018-08-16 00:47:58 -0700 | [diff] [blame] | 22 | bool open_source; |
| 23 | |
William A. Kennington III | 16e7f11 | 2018-08-17 13:15:14 -0700 | [diff] [blame] | 24 | HandleFlags() = default; |
William A. Kennington III | 8ebe525 | 2018-08-27 12:39:24 -0700 | [diff] [blame] | 25 | |
| 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 III | 05c6d5f | 2018-08-16 00:47:58 -0700 | [diff] [blame] | 30 | explicit HandleFlags(LineFlags line_flags); |
William A. Kennington III | 16e7f11 | 2018-08-17 13:15:14 -0700 | [diff] [blame] | 31 | |
William A. Kennington III | 8ebe525 | 2018-08-27 12:39:24 -0700 | [diff] [blame] | 32 | /** @brief Converts this struct to an int bitfield |
| 33 | * |
| 34 | * @return The int bitfield usable by the syscall interface |
| 35 | */ |
William A. Kennington III | 05c6d5f | 2018-08-16 00:47:58 -0700 | [diff] [blame] | 36 | uint32_t toInt() const; |
| 37 | }; |
| 38 | |
Patrick Venture | 526fc7d | 2018-12-19 09:59:29 -0800 | [diff] [blame] | 39 | /** @class HandleInterface |
| 40 | * @brief Handle interface to provide a set of methods required to exist in |
| 41 | * derived objects. |
| 42 | */ |
| 43 | class 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 III | 8ebe525 | 2018-08-27 12:39:24 -0700 | [diff] [blame] | 53 | /** @class Handle |
| 54 | * @brief Handle to a gpio line handle |
| 55 | * @details Provides a c++ interface for gpio handle operations |
| 56 | */ |
Patrick Venture | 526fc7d | 2018-12-19 09:59:29 -0800 | [diff] [blame] | 57 | class Handle : public HandleInterface |
William A. Kennington III | 05c6d5f | 2018-08-16 00:47:58 -0700 | [diff] [blame] | 58 | { |
| 59 | public: |
William A. Kennington III | 8ebe525 | 2018-08-27 12:39:24 -0700 | [diff] [blame] | 60 | /** @brief Per line information used to construct a handle */ |
William A. Kennington III | 05c6d5f | 2018-08-16 00:47:58 -0700 | [diff] [blame] | 61 | struct Line |
| 62 | { |
William A. Kennington III | 8ebe525 | 2018-08-27 12:39:24 -0700 | [diff] [blame] | 63 | /** @brief Offset of the line on the gpio chip */ |
William A. Kennington III | 05c6d5f | 2018-08-16 00:47:58 -0700 | [diff] [blame] | 64 | uint32_t offset; |
William A. Kennington III | 8ebe525 | 2018-08-27 12:39:24 -0700 | [diff] [blame] | 65 | /** @brief Default output value of the line */ |
William A. Kennington III | 05c6d5f | 2018-08-16 00:47:58 -0700 | [diff] [blame] | 66 | uint8_t default_value; |
| 67 | }; |
William A. Kennington III | 8ebe525 | 2018-08-27 12:39:24 -0700 | [diff] [blame] | 68 | |
| 69 | /** @brief Creates a new gpio handle |
William A. Kennington III | 4d40f76 | 2018-09-20 13:23:50 -0700 | [diff] [blame] | 70 | * 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 III | 8ebe525 | 2018-08-27 12:39:24 -0700 | [diff] [blame] | 73 | * |
| 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 III | 05c6d5f | 2018-08-16 00:47:58 -0700 | [diff] [blame] | 80 | Handle(const Chip& chip, const std::vector<Line>& lines, HandleFlags flags, |
William A. Kennington III | 12cd254 | 2018-08-22 21:17:56 -0700 | [diff] [blame] | 81 | std::string_view consumer_label); |
William A. Kennington III | 05c6d5f | 2018-08-16 00:47:58 -0700 | [diff] [blame] | 82 | |
William A. Kennington III | 8ebe525 | 2018-08-27 12:39:24 -0700 | [diff] [blame] | 83 | /** @brief Get the file descriptor used for the handle |
| 84 | * |
| 85 | * @return The gpio handle file descriptor |
| 86 | */ |
William A. Kennington III | 05c6d5f | 2018-08-16 00:47:58 -0700 | [diff] [blame] | 87 | const internal::Fd& getFd() const; |
| 88 | |
William A. Kennington III | 8ebe525 | 2018-08-27 12:39:24 -0700 | [diff] [blame] | 89 | /** @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 Venture | 526fc7d | 2018-12-19 09:59:29 -0800 | [diff] [blame] | 94 | std::vector<uint8_t> getValues() const override; |
William A. Kennington III | 8ebe525 | 2018-08-27 12:39:24 -0700 | [diff] [blame] | 95 | |
| 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 Venture | 526fc7d | 2018-12-19 09:59:29 -0800 | [diff] [blame] | 101 | void getValues(std::vector<uint8_t>& values) const override; |
William A. Kennington III | 8ebe525 | 2018-08-27 12:39:24 -0700 | [diff] [blame] | 102 | |
| 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 Venture | 526fc7d | 2018-12-19 09:59:29 -0800 | [diff] [blame] | 108 | void setValues(const std::vector<uint8_t>& values) const override; |
William A. Kennington III | 05c6d5f | 2018-08-16 00:47:58 -0700 | [diff] [blame] | 109 | |
| 110 | private: |
| 111 | internal::Fd fd; |
| 112 | uint32_t nlines; |
| 113 | }; |
| 114 | |
| 115 | } // namespace gpioplus |