1 #include <fcntl.h>
2 #include <linux/gpio.h>
3
4 #include <gpioplus/chip.hpp>
5
6 #include <cstring>
7 #include <string>
8 #include <system_error>
9
10 namespace gpioplus
11 {
12
LineFlags(uint32_t flags)13 LineFlags::LineFlags(uint32_t flags) :
14 kernel(flags & GPIOLINE_FLAG_KERNEL), output(flags & GPIOLINE_FLAG_IS_OUT),
15 active_low(flags & GPIOLINE_FLAG_ACTIVE_LOW),
16 open_drain(flags & GPIOLINE_FLAG_OPEN_DRAIN),
17 open_source(flags & GPIOLINE_FLAG_OPEN_SOURCE)
18 {}
19
Chip(unsigned id,const internal::Sys * sys)20 Chip::Chip(unsigned id, const internal::Sys* sys) :
21 fd(std::string{"/dev/gpiochip"}.append(std::to_string(id)).c_str(),
22 O_RDONLY | O_CLOEXEC, sys)
23 {}
24
getChipInfo() const25 ChipInfo Chip::getChipInfo() const
26 {
27 struct gpiochip_info info;
28 memset(&info, 0, sizeof(info));
29
30 int r = fd.getSys()->gpio_get_chipinfo(*fd, &info);
31 if (r < 0)
32 {
33 throw std::system_error(-r, std::generic_category(),
34 "gpio_get_chipinfo");
35 }
36
37 return ChipInfo{info.name, info.label, info.lines};
38 }
39
getLineInfo(uint32_t offset) const40 LineInfo Chip::getLineInfo(uint32_t offset) const
41 {
42 struct gpioline_info info;
43 memset(&info, 0, sizeof(info));
44 info.line_offset = offset;
45
46 int r = fd.getSys()->gpio_get_lineinfo(*fd, &info);
47 if (r < 0)
48 {
49 throw std::system_error(-r, std::generic_category(),
50 "gpio_get_lineinfo");
51 }
52
53 return LineInfo{info.flags, info.name, info.consumer};
54 }
55
getFd() const56 const internal::Fd& Chip::getFd() const
57 {
58 return fd;
59 }
60
61 } // namespace gpioplus
62