mraa  2.0.0
Low Level Skeleton Library for Communication on GNU/Linux platforms
initio.hpp
1 /*
2  * Author: Mihai Stefanescu <mihai.stefanescu@rinftech.com>
3  * Copyright (c) 2018 Intel Corporation.
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining
6  * a copy of this software and associated documentation files (the
7  * "Software"), to deal in the Software without restriction, including
8  * without limitation the rights to use, copy, modify, merge, publish,
9  * distribute, sublicense, and/or sell copies of the Software, and to
10  * permit persons to whom the Software is furnished to do so, subject to
11  * the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be
14  * included in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  */
24 
25 #pragma once
26 
27 #include "initio.h"
28 #include <stdexcept>
29 #include <string>
30 #include <vector>
31 
32 #include "aio.hpp"
33 #include "gpio.hpp"
34 #include "i2c.hpp"
35 
36 #if !defined(PERIPHERALMAN)
37 #include "iio.hpp"
38 #endif
39 
40 #include "pwm.hpp"
41 #include "spi.hpp"
42 #include "uart.hpp"
43 #include "uart_ow.hpp"
44 
45 namespace mraa
46 {
47 class MraaIo
48 {
49  private:
50  mraa_io_descriptor* descs;
51 
52  public:
53  MraaIo(const std::string& initStr) : descs()
54  {
55  if (mraa_io_init(initStr.c_str(), &descs) != MRAA_SUCCESS) {
56  throw std::runtime_error("mraa_io_init error");
57  }
58 
59  aios.reserve(descs->n_aio);
60  for (int i = 0; i < descs->n_aio; ++i) {
61  aios.emplace_back(descs->aios[i]);
62  }
63 
64  gpios.reserve(descs->n_gpio);
65  for (int i = 0; i < descs->n_gpio; ++i) {
66  gpios.emplace_back(descs->gpios[i]);
67  }
68 
69  i2cs.reserve(descs->n_i2c);
70  for (int i = 0; i < descs->n_i2c; ++i) {
71  i2cs.emplace_back(descs->i2cs[i]);
72  }
73 
74 #if !defined(PERIPHERALMAN)
75  iios.reserve(descs->n_iio);
76  for (int i = 0; i < descs->n_iio; ++i) {
77  iios.emplace_back(descs->iios[i]);
78  }
79 #endif
80 
81  pwms.reserve(descs->n_pwm);
82  for (int i = 0; i < descs->n_pwm; ++i) {
83  pwms.emplace_back(descs->pwms[i]);
84  }
85 
86  spis.reserve(descs->n_spi);
87  for (int i = 0; i < descs->n_spi; ++i) {
88  spis.emplace_back(descs->spis[i]);
89  }
90 
91  uarts.reserve(descs->n_uart);
92  for (int i = 0; i < descs->n_uart; ++i) {
93  uarts.emplace_back(descs->uarts[i]);
94  }
95 
96  uart_ows.reserve(descs->n_uart_ow);
97  for (int i = 0; i < descs->n_uart_ow; ++i) {
98  uart_ows.emplace_back(descs->uart_ows[i]);
99  }
100 
101  if (descs->leftover_str) {
102  leftoverStr = std::string(descs->leftover_str);
103  } else {
104  leftoverStr = std::string("");
105  }
106  }
107 
108  MraaIo() : descs() {}
109 
110  ~MraaIo()
111  {
112  if (descs->leftover_str) {
113  free(descs->leftover_str);
114  }
115 
116  if (descs->n_aio) {
117  free(descs->aios);
118  }
119  if (descs->n_gpio) {
120  free(descs->gpios);
121  }
122  if (descs->n_i2c) {
123  free(descs->i2cs);
124  }
125 #if !defined(PERIPHERALMAN)
126  if (descs->n_iio) {
127  free(descs->iios);
128  }
129 #endif
130  if (descs->n_pwm) {
131  free(descs->pwms);
132  }
133  if (descs->n_spi) {
134  free(descs->spis);
135  }
136  if (descs->n_uart) {
137  free(descs->uarts);
138  }
139  if (descs->n_uart_ow) {
140  free(descs->uart_ows);
141  }
142 
143  /* Finally free the mraa_io_descriptor structure. */
144  free(descs);
145  }
146 
147  public:
148  std::vector<Aio> aios;
149  std::vector<Gpio> gpios;
150  std::vector<I2c> i2cs;
151 #if !defined(PERIPHERALMAN)
152  std::vector<Iio> iios;
153 #endif
154  std::vector<Pwm> pwms;
155  std::vector<Spi> spis;
156  std::vector<Uart> uarts;
157  std::vector<UartOW> uart_ows;
158 
159  private:
160  /* Used exclusively by the UPM library. */
161  std::string leftoverStr;
162 
163  public:
164  /* This is used mainly by sensors that use C structs/functions in C++ code. */
166  getMraaDescriptors()
167  {
168  return descs;
169  }
170 
171  std::string
172  getLeftoverStr()
173  {
174  return leftoverStr;
175  }
176 };
177 }
Definition: initio.hpp:47
I/O initializer.
Definition: types.h:210
mraa_result_t mraa_io_init(const char *strdesc, mraa_io_descriptor **desc)
Definition: aio.hpp:31
Definition: initio.h:53