libgpiod  1.4.1
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 <bitset>
12 #include <chrono>
13 #include <gpiod.h>
14 #include <memory>
15 #include <string>
16 #include <vector>
17 
18 namespace gpiod {
19 
20 class line;
21 class line_bulk;
22 class line_event;
23 class line_iter;
24 class chip_iter;
25 
38 class chip
39 {
40 public:
41 
45  GPIOD_API chip(void) = default;
46 
52  GPIOD_API chip(const ::std::string& device, int how = OPEN_LOOKUP);
53 
58  GPIOD_API chip(const chip& other) = default;
59 
64  GPIOD_API chip(chip&& other) = default;
65 
71  GPIOD_API chip& operator=(const chip& other) = default;
72 
78  GPIOD_API chip& operator=(chip&& other) = default;
79 
83  GPIOD_API ~chip(void) = default;
84 
93  GPIOD_API void open(const ::std::string &device, int how = OPEN_LOOKUP);
94 
98  GPIOD_API void reset(void) noexcept;
99 
104  GPIOD_API ::std::string name(void) const;
105 
110  GPIOD_API ::std::string label(void) const;
111 
116  GPIOD_API unsigned int num_lines(void) const;
117 
123  GPIOD_API line get_line(unsigned int offset) const;
124 
130  GPIOD_API line find_line(const ::std::string& name) const;
131 
137  GPIOD_API line_bulk get_lines(const ::std::vector<unsigned int>& offsets) const;
138 
143  GPIOD_API line_bulk get_all_lines(void) const;
144 
150  GPIOD_API line_bulk find_lines(const ::std::vector<::std::string>& names) const;
151 
157  GPIOD_API bool operator==(const chip& rhs) const noexcept;
158 
164  GPIOD_API bool operator!=(const chip& rhs) const noexcept;
165 
170  GPIOD_API explicit operator bool(void) const noexcept;
171 
176  GPIOD_API bool operator!(void) const noexcept;
177 
182  enum : int {
193  };
194 
195 private:
196 
197  chip(::gpiod_chip* chip);
198 
199  void throw_if_noref(void) const;
200 
201  ::std::shared_ptr<::gpiod_chip> _m_chip;
202 
203  friend chip_iter;
204  friend line_iter;
205 };
206 
211 {
215  enum : int {
216  DIRECTION_AS_IS = 1,
218  DIRECTION_INPUT,
220  DIRECTION_OUTPUT,
222  EVENT_FALLING_EDGE,
224  EVENT_RISING_EDGE,
226  EVENT_BOTH_EDGES,
228  };
229 
230  GPIOD_API static const ::std::bitset<32> FLAG_ACTIVE_LOW;
232  GPIOD_API static const ::std::bitset<32> FLAG_OPEN_SOURCE;
234  GPIOD_API static const ::std::bitset<32> FLAG_OPEN_DRAIN;
237  ::std::string consumer;
241  ::std::bitset<32> flags;
243 };
244 
252 class line
253 {
254 public:
255 
259  GPIOD_API line(void);
260 
265  GPIOD_API line(const line& other) = default;
266 
271  GPIOD_API line(line&& other) = default;
272 
278  GPIOD_API line& operator=(const line& other) = default;
279 
285  GPIOD_API line& operator=(line&& other) = default;
286 
290  GPIOD_API ~line(void) = default;
291 
296  GPIOD_API unsigned int offset(void) const;
297 
302  GPIOD_API ::std::string name(void) const;
303 
309  GPIOD_API ::std::string consumer(void) const;
310 
315  GPIOD_API int direction(void) const noexcept;
316 
321  GPIOD_API int active_state(void) const noexcept;
322 
328  GPIOD_API bool is_used(void) const;
329 
334  GPIOD_API bool is_open_drain(void) const;
335 
340  GPIOD_API bool is_open_source(void) const;
341 
347  GPIOD_API void request(const line_request& config, int default_val = 0) const;
348 
352  GPIOD_API void release(void) const;
353 
358  GPIOD_API bool is_requested(void) const;
359 
364  GPIOD_API int get_value(void) const;
365 
370  GPIOD_API void set_value(int val) const;
371 
378  GPIOD_API bool event_wait(const ::std::chrono::nanoseconds& timeout) const;
379 
384  GPIOD_API line_event event_read(void) const;
385 
390  GPIOD_API int event_get_fd(void) const;
391 
396  GPIOD_API const chip& get_chip(void) const;
397 
405  GPIOD_API void reset(void);
406 
412  GPIOD_API bool operator==(const line& rhs) const noexcept;
413 
419  GPIOD_API bool operator!=(const line& rhs) const noexcept;
420 
425  GPIOD_API explicit operator bool(void) const noexcept;
426 
432  GPIOD_API bool operator!(void) const noexcept;
433 
437  enum : int {
438  DIRECTION_INPUT = 1,
440  DIRECTION_OUTPUT,
442  };
443 
447  enum : int {
448  ACTIVE_LOW = 1,
450  ACTIVE_HIGH,
452  };
453 
454 private:
455 
456  line(::gpiod_line* line, const chip& owner);
457 
458  void throw_if_null(void) const;
459 
460  ::gpiod_line* _m_line;
461  chip _m_chip;
462 
463  friend chip;
464  friend line_bulk;
465  friend line_iter;
466 };
467 
473 GPIOD_API line find_line(const ::std::string& name);
474 
479 {
480 
484  enum : int {
485  RISING_EDGE = 1,
487  FALLING_EDGE,
489  };
490 
491  ::std::chrono::nanoseconds timestamp;
497 };
498 
506 {
507 public:
508 
512  GPIOD_API line_bulk(void) = default;
513 
519  GPIOD_API line_bulk(const ::std::vector<line>& lines);
520 
525  GPIOD_API line_bulk(const line_bulk& other) = default;
526 
531  GPIOD_API line_bulk(line_bulk&& other) = default;
532 
538  GPIOD_API line_bulk& operator=(const line_bulk& other) = default;
539 
545  GPIOD_API line_bulk& operator=(line_bulk&& other) = default;
546 
550  GPIOD_API ~line_bulk(void) = default;
551 
558  GPIOD_API void append(const line& new_line);
559 
565  GPIOD_API line& get(unsigned int offset);
566 
573  GPIOD_API line& operator[](unsigned int offset);
574 
579  GPIOD_API unsigned int size(void) const noexcept;
580 
585  GPIOD_API bool empty(void) const noexcept;
586 
590  GPIOD_API void clear(void);
591 
598  GPIOD_API void request(const line_request& config,
599  const std::vector<int> default_vals = std::vector<int>()) const;
600 
604  GPIOD_API void release(void) const;
605 
611  GPIOD_API ::std::vector<int> get_values(void) const;
612 
618  GPIOD_API void set_values(const ::std::vector<int>& values) const;
619 
627  GPIOD_API line_bulk event_wait(const ::std::chrono::nanoseconds& timeout) const;
628 
633  GPIOD_API explicit operator bool(void) const noexcept;
634 
639  GPIOD_API bool operator!(void) const noexcept;
640 
644  GPIOD_API static const unsigned int MAX_LINES;
645 
649  class iterator
650  {
651  public:
652 
656  GPIOD_API iterator(void) = default;
657 
662  GPIOD_API iterator(const iterator& other) = default;
663 
668  GPIOD_API iterator(iterator&& other) = default;
669 
675  GPIOD_API iterator& operator=(const iterator& other) = default;
676 
682  GPIOD_API iterator& operator=(iterator&& other) = default;
683 
687  GPIOD_API ~iterator(void) = default;
688 
693  GPIOD_API iterator& operator++(void);
694 
699  GPIOD_API const line& operator*(void) const;
700 
705  GPIOD_API const line* operator->(void) const;
706 
713  GPIOD_API bool operator==(const iterator& rhs) const noexcept;
714 
721  GPIOD_API bool operator!=(const iterator& rhs) const noexcept;
722 
723  private:
724 
725  iterator(const ::std::vector<line>::iterator& it);
726 
727  ::std::vector<line>::iterator _m_iter;
728 
729  friend line_bulk;
730  };
731 
736  GPIOD_API iterator begin(void) noexcept;
737 
742  GPIOD_API iterator end(void) noexcept;
743 
744 private:
745 
746  void throw_if_empty(void) const;
747  void to_line_bulk(::gpiod_line_bulk* bulk) const;
748 
749  ::std::vector<line> _m_bulk;
750 };
751 
759 
765 GPIOD_API chip_iter begin(chip_iter iter) noexcept;
766 
772 GPIOD_API chip_iter end(const chip_iter& iter) noexcept;
773 
778 {
779 public:
780 
784  GPIOD_API chip_iter(void) = default;
785 
790  GPIOD_API chip_iter(const chip_iter& other) = default;
791 
796  GPIOD_API chip_iter(chip_iter&& other) = default;
797 
803  GPIOD_API chip_iter& operator=(const chip_iter& other) = default;
804 
810  GPIOD_API chip_iter& operator=(chip_iter&& other) = default;
811 
815  GPIOD_API ~chip_iter(void) = default;
816 
821  GPIOD_API chip_iter& operator++(void);
822 
827  GPIOD_API const chip& operator*(void) const;
828 
833  GPIOD_API const chip* operator->(void) const;
834 
841  GPIOD_API bool operator==(const chip_iter& rhs) const noexcept;
842 
849  GPIOD_API bool operator!=(const chip_iter& rhs) const noexcept;
850 
851 private:
852 
853  chip_iter(::gpiod_chip_iter* iter);
854 
855  ::std::shared_ptr<::gpiod_chip_iter> _m_iter;
856  chip _m_current;
857 
858  friend chip_iter make_chip_iter(void);
859 };
860 
866 GPIOD_API line_iter begin(line_iter iter) noexcept;
867 
873 GPIOD_API line_iter end(const line_iter& iter) noexcept;
874 
879 {
880 public:
881 
885  GPIOD_API line_iter(void) = default;
886 
891  GPIOD_API line_iter(const chip& owner);
892 
897  GPIOD_API line_iter(const line_iter& other) = default;
898 
903  GPIOD_API line_iter(line_iter&& other) = default;
904 
910  GPIOD_API line_iter& operator=(const line_iter& other) = default;
911 
917  GPIOD_API line_iter& operator=(line_iter&& other) = default;
918 
922  GPIOD_API ~line_iter(void) = default;
923 
928  GPIOD_API line_iter& operator++(void);
929 
934  GPIOD_API const line& operator*(void) const;
935 
940  GPIOD_API const line* operator->(void) const;
941 
948  GPIOD_API bool operator==(const line_iter& rhs) const noexcept;
949 
956  GPIOD_API bool operator!=(const line_iter& rhs) const noexcept;
957 
958 private:
959 
960  ::std::shared_ptr<::gpiod_line_iter> _m_iter;
961  line _m_current;
962 };
963 
968 } /* namespace gpiod */
969 
970 #endif /* __LIBGPIOD_GPIOD_CXX_HPP__ */
Definition: gpiod.hpp:18
static GPIOD_API const ::std::bitset< 32 > FLAG_OPEN_SOURCE
The line is an open-source port.
Definition: gpiod.hpp:232
GPIOD_API line find_line(const ::std::string &name) const
Get the line exposed by this chip by name.
Stores the configuration for line requests.
Definition: gpiod.hpp:210
GPIOD_API line_bulk get_all_lines(void) const
Get all lines exposed by this chip.
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:234
::std::chrono::nanoseconds timestamp
Best estimate of time of event occurrence in nanoseconds.
Definition: gpiod.hpp:491
::std::bitset< 32 > flags
Additional request flags.
Definition: gpiod.hpp:241
Represents a single GPIO line.
Definition: gpiod.hpp:252
Represents a set of GPIO lines.
Definition: gpiod.hpp:505
Assume the string is a path to the GPIO chardev.
Definition: gpiod.hpp:185
#define GPIOD_API
Makes symbol visible.
Definition: gpiod.h:58
GPIOD_API bool operator!(void) const noexcept
Check if this object doesn&#39;t hold a reference to a GPIO chip.
GPIOD_API void open(const ::std::string &device, int how=OPEN_LOOKUP)
Open a GPIO chip.
static GPIOD_API const unsigned int MAX_LINES
Max number of lines that this object can hold.
Definition: gpiod.hpp:644
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.
int request_type
Type of the request.
Definition: gpiod.hpp:239
GPIOD_API bool operator==(const chip &rhs) const noexcept
Equality operator.
GPIOD_API void reset(void) noexcept
Reset the internal smart pointer owned by this object.
GPIOD_API bool operator!=(const chip &rhs) const noexcept
Inequality operator.
Allows to iterate over all lines owned by a GPIO chip.
Definition: gpiod.hpp:878
GPIOD_API line get_line(unsigned int offset) const
Get the line exposed by this chip at given offset.
GPIOD_API line_iter end(const line_iter &iter) noexcept
Support for range-based loops for line iterators.
Represents a GPIO chip.
Definition: gpiod.hpp:38
Iterator for iterating over lines held by line_bulk.
Definition: gpiod.hpp:649
Assume the string is the number of the GPIO chip.
Definition: gpiod.hpp:191
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 &#39;low&#39; (high is the default).
Definition: gpiod.hpp:230
GPIOD_API chip(void)=default
Default constructor.
::std::string consumer
Consumer name to pass to the request.
Definition: gpiod.hpp:237
GPIOD_API ::std::string label(void) const
Return the label of the chip held by this object.
line source
Line object referencing the GPIO line on which the event occurred.
Definition: gpiod.hpp:495
GPIOD_API ~chip(void)=default
Destructor.
Assume the string is the label of the GPIO chip.
Definition: gpiod.hpp:189
Assume the string is the name of the chip.
Definition: gpiod.hpp:187
GPIOD_API unsigned int num_lines(void) const
Return the number of lines exposed by this chip.
int event_type
Type of the event that occurred.
Definition: gpiod.hpp:493
GPIOD_API line_iter begin(line_iter iter) noexcept
Support for range-based loops for line iterators.
Describes a single GPIO line event.
Definition: gpiod.hpp:478
Allows to iterate over all GPIO chips present on the system.
Definition: gpiod.hpp:777
GPIOD_API ::std::string name(void) const
Return the name of the chip held by this object.
GPIOD_API chip & operator=(const chip &other)=default
Assignment operator.
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.
Open based on the best guess what the supplied string is.
Definition: gpiod.hpp:183