gpio.cpp
Go to the documentation of this file.
1 /*
2  * BSD 3-Clause License
3  *
4  * Copyright (c) 2019, Analog Devices, Inc.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright notice, this
11  * list of conditions and the following disclaimer.
12  *
13  * 2. Redistributions in binary form must reproduce the above copyright notice,
14  * this list of conditions and the following disclaimer in the documentation
15  * and/or other materials provided with the distribution.
16  *
17  * 3. Neither the name of the copyright holder nor the names of its
18  * contributors may be used to endorse or promote products derived from
19  * this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32 #include "gpio.h"
33 #ifdef USE_GLOG
34 #include <glog/logging.h>
35 #else
36 #include <aditof/log.h>
37 #endif
38 
39 #include <errno.h>
40 #include <fcntl.h>
41 #include <linux/gpio.h>
42 #include <sys/ioctl.h>
43 #include <unistd.h>
44 
45 using namespace aditof;
46 Gpio::Gpio(const std::string &charDeviceName, int gpioNumber)
47  : m_charDevName{charDeviceName}, m_lineHandle{-1},
48  m_gpioNumber(gpioNumber) {}
49 
50 int Gpio::open(int openType) {
51  int ret;
52  int fd;
53 
54  fd = ::open(m_charDevName.c_str(), O_RDONLY);
55  if (fd <= 0) {
56  LOG(ERROR) << "Failed to open gpio!";
57  return errno;
58  }
59 
60  struct gpiohandle_request request;
61  request.lineoffsets[0] = m_gpioNumber;
62  request.flags = openType;
63  request.lines = 1;
64  ret = ioctl(fd, GPIO_GET_LINEHANDLE_IOCTL, &request);
65  ::close(fd);
66  if (ret == -1) {
67  LOG(ERROR) << "ioctl failed with error: " << errno;
68  return errno;
69  }
70  m_lineHandle = request.fd;
71 
72  return ret;
73 }
74 
75 int Gpio::openForWrite() { return this->open(GPIOHANDLE_REQUEST_OUTPUT); }
76 
77 int Gpio::openForRead() { return this->open(GPIOHANDLE_REQUEST_INPUT); }
78 
79 int Gpio::close() {
80  int ret = 0;
81 
82  if (m_lineHandle != -1) {
83  ret = ::close(m_lineHandle);
84  m_lineHandle = -1;
85  }
86 
87  return ret;
88 }
89 
91  struct gpiohandle_data data;
92  int ret;
93 
94  if (m_lineHandle == -1) {
95  LOG(ERROR) << "The Gpio object is not initialized!";
96  return -EBADFD;
97  }
98 
99  data.values[0] = value;
100  ret = ioctl(m_lineHandle, GPIOHANDLE_GET_LINE_VALUES_IOCTL, &data);
101  if (ret == -1) {
102  return errno;
103  }
104  value = data.values[0];
105 
106  return ret;
107 }
108 
110  struct gpiohandle_data data;
111  int ret;
112 
113  if (m_lineHandle == -1) {
114  LOG(ERROR) << "The Gpio object is not initialized!";
115  return -EBADFD;
116  }
117 
118  data.values[0] = value;
119  ret = ioctl(m_lineHandle, GPIOHANDLE_SET_LINE_VALUES_IOCTL, &data);
120  if (ret == -1) {
121  return errno;
122  }
123 
124  return ret;
125 }
ERROR
const int ERROR
Definition: log_severity.h:60
log.h
aditof::Gpio::m_lineHandle
int m_lineHandle
Definition: gpio.h:85
aditof::Gpio::openForRead
int openForRead()
Definition: gpio.cpp:77
string
GLsizei const GLchar *const * string
Definition: glcorearb.h:3083
errno
int errno
aditof::Gpio::m_charDevName
std::string m_charDevName
Definition: gpio.h:84
aditof::Gpio::m_gpioNumber
int m_gpioNumber
Definition: gpio.h:86
aditof::Gpio::open
int open(int openType)
Definition: gpio.cpp:50
aditof::Gpio::close
int close()
Definition: gpio.cpp:79
aditof
Namespace aditof.
Definition: adsd_errs.h:40
aditof::Gpio::readValue
int readValue(int &value)
Definition: gpio.cpp:90
LOG
#define LOG(x)
Definition: sdk/include/aditof/log.h:72
aditof::Gpio::openForWrite
int openForWrite()
Definition: gpio.cpp:75
gpio.h
data
GLint GLenum GLsizei GLsizei GLsizei GLint GLsizei const GLvoid * data
Definition: glcorearb.h:2879
aditof::Gpio::writeValue
int writeValue(int value)
Definition: gpio.cpp:109
aditof::Gpio::Gpio
Gpio(const std::string &charDeviceName, int gpioNumber)
Definition: gpio.cpp:46
value
GLsizei const GLfloat * value
Definition: glcorearb.h:3093


libaditof
Author(s):
autogenerated on Wed May 21 2025 02:06:53