libgpiod  1.2
gpiod.hpp
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2 /*
3  * This file is part of libgpiod.
4  *
5  * Copyright (C) 2017-2018 Bartosz Golaszewski <bartekgola@gmail.com>
6  */
7 
8 #ifndef __LIBGPIOD_GPIOD_CXX_HPP__
9 #define __LIBGPIOD_GPIOD_CXX_HPP__
10 
11 #include <gpiod.h>
12 
13 #include <string>
14 #include <vector>
15 #include <memory>
16 #include <bitset>
17 #include <chrono>
18 
19 namespace gpiod {
20 
21 class line;
22 class line_bulk;
23 class line_event;
24 class line_iter;
25 class chip_iter;
26 
39 class chip
40 {
41 public:
42 
46  GPIOD_API chip(void) = default;
47 
53  GPIOD_API chip(const ::std::string& device, int how = OPEN_LOOKUP);
54 
59  GPIOD_API chip(const chip& other) = default;
60 
65  GPIOD_API chip(chip&& other) = default;
66 
72  GPIOD_API chip& operator=(const chip& other) = default;
73 
79  GPIOD_API chip& operator=(chip&& other) = default;
80 
84  GPIOD_API ~chip(void) = default;
85 
94  GPIOD_API void open(const ::std::string &device, int how = OPEN_LOOKUP);
95 
99  GPIOD_API void reset(void) noexcept;
100 
105  GPIOD_API ::std::string name(void) const;
106 
111  GPIOD_API ::std::string label(void) const;
112 
117  GPIOD_API unsigned int num_lines(void) const;
118 
124  GPIOD_API line get_line(unsigned int offset) const;
125 
131  GPIOD_API line find_line(const ::std::string& name) const;
132 
138  GPIOD_API line_bulk get_lines(const ::std::vector<unsigned int>& offsets) const;
139 
144  GPIOD_API line_bulk get_all_lines(void) const;
145 
151  GPIOD_API line_bulk find_lines(const ::std::vector<::std::string>& names) const;
152 
158  GPIOD_API bool operator==(const chip& rhs) const noexcept;
159 
165  GPIOD_API bool operator!=(const chip& rhs) const noexcept;
166 
171  GPIOD_API operator bool(void) const noexcept;
172 
177  GPIOD_API bool operator!(void) const noexcept;
178 
183  enum : int {
194  };
195 
196 private:
197 
198  chip(::gpiod_chip* chip);
199 
200  void throw_if_noref(void) const;
201 
202  ::std::shared_ptr<::gpiod_chip> _m_chip;
203 
204  friend chip_iter;
205  friend line_iter;
206 };
207 
212 {
216  enum : int {
229  };
230 
231  GPIOD_API static const ::std::bitset<32> FLAG_ACTIVE_LOW;
233  GPIOD_API static const ::std::bitset<32> FLAG_OPEN_SOURCE;
235  GPIOD_API static const ::std::bitset<32> FLAG_OPEN_DRAIN;
238  ::std::string consumer;
242  ::std::bitset<32> flags;
244 };
245 
253 class line
254 {
255 public:
256 
260  GPIOD_API line(void);
261 
266  GPIOD_API line(const line& other) = default;
267 
272  GPIOD_API line(line&& other) = default;
273 
279  GPIOD_API line& operator=(const line& other) = default;
280 
286  GPIOD_API line& operator=(line&& other) = default;
287 
291  GPIOD_API ~line(void) = default;
292 
297  GPIOD_API unsigned int offset(void) const;
298 
303  GPIOD_API ::std::string name(void) const;
304 
310  GPIOD_API ::std::string consumer(void) const;
311 
316  GPIOD_API int direction(void) const noexcept;
317 
322  GPIOD_API int active_state(void) const noexcept;
323 
329  GPIOD_API bool is_used(void) const;
330 
335  GPIOD_API bool is_open_drain(void) const;
336 
341  GPIOD_API bool is_open_source(void) const;
342 
348  GPIOD_API void request(const line_request& config, int default_val = 0) const;
349 
353  GPIOD_API void release(void) const;
354 
359  GPIOD_API bool is_requested(void) const;
360 
365  GPIOD_API int get_value(void) const;
366 
371  GPIOD_API void set_value(int val) const;
372 
379  GPIOD_API bool event_wait(const ::std::chrono::nanoseconds& timeout) const;
380 
385  GPIOD_API line_event event_read(void) const;
386 
391  GPIOD_API int event_get_fd(void) const;
392 
397  GPIOD_API const chip& get_chip(void) const;
398 
406  GPIOD_API void reset(void);
407 
413  GPIOD_API bool operator==(const line& rhs) const noexcept;
414 
420  GPIOD_API bool operator!=(const line& rhs) const noexcept;
421 
426  GPIOD_API operator bool(void) const noexcept;
427 
433  GPIOD_API bool operator!(void) const noexcept;
434 
438  enum : int {
443  };
444 
448  enum : int {
453  };
454 
455 private:
456 
457  line(::gpiod_line* line, const chip& owner);
458 
459  void throw_if_null(void) const;
460 
461  ::gpiod_line* _m_line;
462  chip _m_chip;
463 
464  friend chip;
465  friend line_bulk;
466  friend line_iter;
467 };
468 
474 GPIOD_API line find_line(const ::std::string& name);
475 
480 {
481 
485  enum : int {
490  };
491 
492  ::std::chrono::nanoseconds timestamp;
498 };
499 
507 {
508 public:
509 
513  GPIOD_API line_bulk(void) = default;
514 
520  GPIOD_API line_bulk(const ::std::vector<line>& lines);
521 
526  GPIOD_API line_bulk(const line_bulk& other) = default;
527 
532  GPIOD_API line_bulk(line_bulk&& other) = default;
533 
539  GPIOD_API line_bulk& operator=(const line_bulk& other) = default;
540 
546  GPIOD_API line_bulk& operator=(line_bulk&& other) = default;
547 
551  GPIOD_API ~line_bulk(void) = default;
552 
559  GPIOD_API void append(const line& new_line);
560 
566  GPIOD_API line& get(unsigned int offset);
567 
574  GPIOD_API line& operator[](unsigned int offset);
575 
580  GPIOD_API unsigned int size(void) const noexcept;
581 
586  GPIOD_API bool empty(void) const noexcept;
587 
591  GPIOD_API void clear(void);
592 
599  GPIOD_API void request(const line_request& config,
600  const std::vector<int> default_vals = std::vector<int>()) const;
601 
605  GPIOD_API void release(void) const;
606 
612  GPIOD_API ::std::vector<int> get_values(void) const;
613 
619  GPIOD_API void set_values(const ::std::vector<int>& values) const;
620 
628  GPIOD_API line_bulk event_wait(const ::std::chrono::nanoseconds& timeout) const;
629 
634  GPIOD_API operator bool(void) const noexcept;
635 
640  GPIOD_API bool operator!(void) const noexcept;
641 
645  GPIOD_API static const unsigned int MAX_LINES;
646 
650  class iterator
651  {
652  public:
653 
657  GPIOD_API iterator(void) = default;
658 
663  GPIOD_API iterator(const iterator& other) = default;
664 
669  GPIOD_API iterator(iterator&& other) = default;
670 
676  GPIOD_API iterator& operator=(const iterator& other) = default;
677 
683  GPIOD_API iterator& operator=(iterator&& other) = default;
684 
688  GPIOD_API ~iterator(void) = default;
689 
695 
700  GPIOD_API const line& operator*(void) const;
701 
706  GPIOD_API const line* operator->(void) const;
707 
714  GPIOD_API bool operator==(const iterator& rhs) const noexcept;
715 
722  GPIOD_API bool operator!=(const iterator& rhs) const noexcept;
723 
724  private:
725 
726  iterator(const ::std::vector<line>::iterator& it);
727 
728  ::std::vector<line>::iterator _m_iter;
729 
730  friend line_bulk;
731  };
732 
737  GPIOD_API iterator begin(void) noexcept;
738 
743  GPIOD_API iterator end(void) noexcept;
744 
745 private:
746 
747  void throw_if_empty(void) const;
748  void to_line_bulk(::gpiod_line_bulk* bulk) const;
749 
750  ::std::vector<line> _m_bulk;
751 };
752 
760 
766 GPIOD_API chip_iter begin(chip_iter iter) noexcept;
767 
773 GPIOD_API chip_iter end(const chip_iter& iter) noexcept;
774 
779 {
780 public:
781 
785  GPIOD_API chip_iter(void) = default;
786 
791  GPIOD_API chip_iter(const chip_iter& other) = default;
792 
797  GPIOD_API chip_iter(chip_iter&& other) = default;
798 
804  GPIOD_API chip_iter& operator=(const chip_iter& other) = default;
805 
811  GPIOD_API chip_iter& operator=(chip_iter&& other) = default;
812 
816  GPIOD_API ~chip_iter(void) = default;
817 
822  GPIOD_API chip_iter& operator++(void);
823 
828  GPIOD_API const chip& operator*(void) const;
829 
834  GPIOD_API const chip* operator->(void) const;
835 
842  GPIOD_API bool operator==(const chip_iter& rhs) const noexcept;
843 
850  GPIOD_API bool operator!=(const chip_iter& rhs) const noexcept;
851 
852 private:
853 
854  chip_iter(::gpiod_chip_iter* iter);
855 
856  ::std::shared_ptr<::gpiod_chip_iter> _m_iter;
857  chip _m_current;
858 
859  friend chip_iter make_chip_iter(void);
860 };
861 
867 GPIOD_API line_iter begin(line_iter iter) noexcept;
868 
874 GPIOD_API line_iter end(const line_iter& iter) noexcept;
875 
880 {
881 public:
882 
886  GPIOD_API line_iter(void) = default;
887 
892  GPIOD_API line_iter(const chip& owner);
893 
898  GPIOD_API line_iter(const line_iter& other) = default;
899 
904  GPIOD_API line_iter(line_iter&& other) = default;
905 
911  GPIOD_API line_iter& operator=(const line_iter& other) = default;
912 
918  GPIOD_API line_iter& operator=(line_iter&& other) = default;
919 
923  GPIOD_API ~line_iter(void) = default;
924 
929  GPIOD_API line_iter& operator++(void);
930 
935  GPIOD_API const line& operator*(void) const;
936 
941  GPIOD_API const line* operator->(void) const;
942 
949  GPIOD_API bool operator==(const line_iter& rhs) const noexcept;
950 
957  GPIOD_API bool operator!=(const line_iter& rhs) const noexcept;
958 
959 private:
960 
961  ::std::shared_ptr<::gpiod_line_iter> _m_iter;
962  line _m_current;
963 };
964 
969 } /* namespace gpiod */
970 
971 #endif /* __LIBGPIOD_GPIOD_CXX_HPP__ */
Definition: gpiod.hpp:19
Listen for all types of events.
Definition: gpiod.hpp:227
GPIOD_API::std::string name(void) const
Return the name of the chip held by this object.
static GPIOD_API const ::std::bitset< 32 > FLAG_OPEN_SOURCE
The line is an open-source port.
Definition: gpiod.hpp:233
GPIOD_API bool is_open_source(void) const
Check if this line represents an open-source GPIO.
GPIOD_API void append(const line &new_line)
Add a line to this line_bulk object.
GPIOD_API bool is_used(void) const
Check if this line is used by the kernel or other user space process.
GPIOD_API void clear(void)
Remove all lines from this object.
Assume the string is the label of the GPIO chip.
Definition: gpiod.hpp:190
GPIOD_API void release(void) const
Release all lines held by this object.
Stores the configuration for line requests.
Definition: gpiod.hpp:211
GPIOD_API int direction(void) const noexcept
Get current direction of this line.
Line's active state is high.
Definition: gpiod.hpp:451
GPIOD_API line_bulk get_all_lines(void) const
Get all lines exposed by this chip.
GPIOD_API::std::string consumer(void) const
Get the consumer of this line (if any).
GPIOD_API chip_iter make_chip_iter(void)
Create a new chip_iter.
static GPIOD_API const ::std::bitset< 32 > FLAG_OPEN_DRAIN
The line is an open-drain port.
Definition: gpiod.hpp:235
::std::chrono::nanoseconds timestamp
Best estimate of time of event occurrence in nanoseconds.
Definition: gpiod.hpp:492
::std::bitset< 32 > flags
Additional request flags.
Definition: gpiod.hpp:242
GPIOD_API iterator & operator=(const iterator &other)=default
Assignment operator.
Represents a single GPIO line.
Definition: gpiod.hpp:253
GPIOD_API void set_values(const ::std::vector< int > &values) const
Set values of all lines held by this object.
Represents a set of GPIO lines.
Definition: gpiod.hpp:506
Line's direction setting is output.
Definition: gpiod.hpp:441
Assume the string is a path to the GPIO chardev.
Definition: gpiod.hpp:186
#define GPIOD_API
Makes symbol visible.
Definition: gpiod.h:58
GPIOD_API bool operator!(void) const noexcept
Check if this object doesn't hold a reference to a GPIO chip.
GPIOD_API void open(const ::std::string &device, int how=OPEN_LOOKUP)
Open a GPIO chip.
GPIOD_API line_bulk find_lines(const ::std::vector<::std::string > &names) const
Get a set of lines exposed by this chip by their names.
static GPIOD_API const unsigned int MAX_LINES
Max number of lines that this object can hold.
Definition: gpiod.hpp:645
GPIOD_API bool operator!(void) const noexcept
Check if this object doesn't reference any GPIO line.
GPIOD_API int event_get_fd(void) const
Get the event file descriptor associated with this line.
Assume the string is the number of the GPIO chip.
Definition: gpiod.hpp:192
GPIOD_API line(void)
Default constructor.
GPIOD_API line_event event_read(void) const
Read a line event.
int request_type
Type of the request.
Definition: gpiod.hpp:240
Line's direction setting is input.
Definition: gpiod.hpp:439
GPIOD_API bool operator==(const chip &rhs) const noexcept
Equality operator.
Listen for rising edge events.
Definition: gpiod.hpp:225
GPIOD_API bool operator!=(const line &rhs) const noexcept
Check if two line objects reference different GPIO lines.
GPIOD_API line & operator[](unsigned int offset)
Get the line at given offset without bounds checking.
GPIOD_API line & operator=(const line &other)=default
Assignment operator.
Assume the string is the name of the chip.
Definition: gpiod.hpp:188
GPIOD_API void reset(void) noexcept
Reset the internal smart pointer owned by this object.
GPIOD_API ~line(void)=default
Destructor.
Falling edge event.
Definition: gpiod.hpp:488
GPIOD_API bool operator!=(const chip &rhs) const noexcept
Inequality operator.
GPIOD_API line_bulk event_wait(const ::std::chrono::nanoseconds &timeout) const
Poll the set of lines for line events.
GPIOD_API void request(const line_request &config, const std::vector< int > default_vals=std::vector< int >()) const
Request all lines held by this object.
GPIOD_API line_bulk get_lines(const ::std::vector< unsigned int > &offsets) const
Get a set of lines exposed by this chip at given offsets.
Allows to iterate over all lines owned by a GPIO chip.
Definition: gpiod.hpp:879
GPIOD_API iterator(void)=default
Default constructor.
Rising edge event.
Definition: gpiod.hpp:486
GPIOD_API bool operator!=(const iterator &rhs) const noexcept
Check if this operator doesn't point to the same element.
GPIOD_API const line & operator*(void) const
Dereference current element.
GPIOD_API bool operator!(void) const noexcept
Check if this object doesn't hold any lines.
GPIOD_API int get_value(void) const
Read the line value.
GPIOD_API bool is_open_drain(void) const
Check if this line represents an open-drain GPIO.
GPIOD_API ~iterator(void)=default
Destructor.
Line's active state is low.
Definition: gpiod.hpp:449
Represents a GPIO chip.
Definition: gpiod.hpp:39
Iterator for iterating over lines held by line_bulk.
Definition: gpiod.hpp:650
GPIOD_API::std::string name(void) const
Get the name of this line (if any).
GPIOD_API bool is_requested(void) const
Check if this user has ownership of this line.
GPIOD_API::std::string label(void) const
Return the label of the chip held by this object.
Request for reading line values.
Definition: gpiod.hpp:219
Helper structure for storing a set of GPIO line objects.
Definition: gpiod.h:545
static GPIOD_API const ::std::bitset< 32 > FLAG_ACTIVE_LOW
Set the active state to 'low' (high is the default).
Definition: gpiod.hpp:231
Request for driving the GPIO lines.
Definition: gpiod.hpp:221
GPIOD_API chip(void)=default
Default constructor.
::std::string consumer
Consumer name to pass to the request.
Definition: gpiod.hpp:238
GPIOD_API bool operator==(const iterator &rhs) const noexcept
Check if this operator points to the same element.
GPIOD_API const chip & get_chip(void) const
Get the reference to the parent chip.
GPIOD_API iterator & operator++(void)
Advance the iterator by one element.
Listen for falling edge events.
Definition: gpiod.hpp:223
GPIOD_API const line * operator->(void) const
Member access operator.
GPIOD_API bool empty(void) const noexcept
Check if this line_bulk doesn't hold any lines.
GPIOD_API line find_line(const ::std::string &name) const
Get the line exposed by this chip by name.
GPIOD_API ~line_bulk(void)=default
Destructor.
GPIOD_API bool operator==(const line &rhs) const noexcept
Check if two line objects reference the same GPIO line.
line source
Line object referencing the GPIO line on which the event occurred.
Definition: gpiod.hpp:496
Open based on the best guess what the supplied string is.
Definition: gpiod.hpp:184
GPIOD_API line_bulk & operator=(const line_bulk &other)=default
Assignment operator.
GPIOD_API line find_line(const ::std::string &name)
Find a GPIO line by name.
GPIOD_API iterator begin(void) noexcept
Returns an iterator to the first line.
GPIOD_API line get_line(unsigned int offset) const
Get the line exposed by this chip at given offset.
GPIOD_API void release(void) const
Release the line if it was previously requested.
GPIOD_API ~chip(void)=default
Destructor.
GPIOD_API void set_value(int val) const
Set the value of this line.
GPIOD_API iterator end(void) noexcept
Returns an iterator to the element following the last line.
GPIOD_API::std::vector< int > get_values(void) const
Read values from all lines held by this object.
GPIOD_API unsigned int offset(void) const
Get the offset of this line.
GPIOD_API unsigned int num_lines(void) const
Return the number of lines exposed by this chip.
GPIOD_API void request(const line_request &config, int default_val=0) const
Request this line.
GPIOD_API int active_state(void) const noexcept
Get current active state of this line.
int event_type
Type of the event that occurred.
Definition: gpiod.hpp:494
GPIOD_API void reset(void)
Reset the state of this object.
GPIOD_API line_bulk(void)=default
Default constructor.
Describes a single GPIO line event.
Definition: gpiod.hpp:479
GPIOD_API unsigned int size(void) const noexcept
Get the number of lines currently held by this object.
Request for values, don't change the direction.
Definition: gpiod.hpp:217
GPIOD_API bool event_wait(const ::std::chrono::nanoseconds &timeout) const
Wait for an event on this line.
Allows to iterate over all GPIO chips present on the system.
Definition: gpiod.hpp:778
GPIOD_API chip & operator=(const chip &other)=default
Assignment operator.