implemented character device class
Stefan Schuermans

Stefan Schuermans commited on 2011-12-11 20:22:55
Showing 2 changed files, with 176 additions and 0 deletions.

... ...
@@ -0,0 +1,117 @@
1
+/* Blinker
2
+   Copyright 2011 Stefan Schuermans <stefan@blinkenarea.org>
3
+   Copyleft GNU public license - http://www.gnu.org/copyleft/gpl.html
4
+   a blinkenarea.org project */
5
+
6
+#include <fcntl.h>
7
+#include <sys/stat.h>
8
+#include <sys/types.h>
9
+#include <string>
10
+#include <strings.h>
11
+#include <termios.h>
12
+#include <unistd.h>
13
+
14
+#include "Device.h"
15
+#include "Io.h"
16
+#include "SerCfg.h"
17
+
18
+namespace Blinker {
19
+
20
+/**
21
+ * @brief constructor
22
+ * @param[in] path path to device
23
+ */
24
+Device::Device(const std::string &path)
25
+{
26
+  // open device
27
+  m_fd = open(path.c_str(), O_CLOEXEC | O_NOCTTY | O_NONBLOCK);
28
+}
29
+
30
+/// destructor
31
+Device::~Device()
32
+{
33
+  // exit if not initialized
34
+  if (m_fd == -1)
35
+    return;
36
+
37
+  // close device
38
+  close(m_fd);
39
+}
40
+
41
+/**
42
+ * @brief set serial port configuration
43
+ * @param[in] serCfg serial port configuration
44
+ * @return if configuration succeeded
45
+ *
46
+ * This function should only be used if device is a serial port.
47
+ */
48
+bool Device::setSerCfg(const SerCfg& serCfg)
49
+{
50
+  struct termios tio;
51
+
52
+  // exit if not initialized
53
+  if (m_fd == -1)
54
+    return false;
55
+
56
+  // set up serial port configuration structure
57
+  bzero(&tio, sizeof(tio));
58
+  tio.c_cflag = CLOCAL | HUPCL | CREAD;
59
+  tio.c_iflag = IGNBRK | IGNPAR;
60
+  tio.c_oflag = 0;
61
+  tio.c_lflag = 0;
62
+  tio.c_cc[VTIME] = 1; // 0.1 sec timeout
63
+  tio.c_cc[VMIN] = 0;  // return on single char read
64
+  switch (serCfg.m_baud) {
65
+    case    300: tio.c_cflag |=    B300; break;
66
+    case    600: tio.c_cflag |=    B600; break;
67
+    case   1200: tio.c_cflag |=   B1200; break;
68
+    case   2400: tio.c_cflag |=   B2400; break;
69
+    case   4800: tio.c_cflag |=   B4800; break;
70
+    case   9600: tio.c_cflag |=   B9600; break;
71
+    case  19200: tio.c_cflag |=  B19200; break;
72
+    case  38400: tio.c_cflag |=  B38400; break;
73
+    case  57600: tio.c_cflag |=  B57600; break;
74
+    case 115200: tio.c_cflag |= B115200; break;
75
+    default: return false; // invalid setting
76
+  }
77
+  switch (serCfg.m_data) {
78
+    case 5: tio.c_cflag |= CS5; break;
79
+    case 6: tio.c_cflag |= CS6; break;
80
+    case 7: tio.c_cflag |= CS7; break;
81
+    case 8: tio.c_cflag |= CS8; break;
82
+    default: return false; // invalid setting
83
+  }
84
+  switch (serCfg.m_parity) {
85
+    case SerCfg::ParityNone: break;
86
+    case SerCfg::ParityEven: tio.c_cflag |= PARENB; break;
87
+    case SerCfg::ParityOdd: tio.c_cflag |= PARENB | PARODD; break;
88
+    default: return false; // invalid setting
89
+  }
90
+  switch (serCfg.m_stop) {
91
+    case 1: break;
92
+    case 2: tio.c_cflag |= CSTOPB; break;
93
+    default: return false; // invalid setting
94
+  }
95
+
96
+  // configure serial port
97
+  return tcsetattr(m_fd, TCSANOW, &tio) != -1;
98
+}
99
+
100
+/**
101
+ * @brief write data to device
102
+ * @param[in] data data to write
103
+ * @return if the data could be written
104
+ */
105
+bool Device::write(const std::string &data)
106
+{
107
+  // exit if not initialized
108
+  if (m_fd == -1)
109
+    return false;
110
+
111
+  // write data
112
+  return ::write(m_fd, data.c_str(), data.size()) == (ssize_t)data.size();
113
+}
114
+
115
+} // namespace Blinker
116
+
117
+
... ...
@@ -0,0 +1,59 @@
1
+/* Blinker
2
+   Copyright 2011 Stefan Schuermans <stefan@blinkenarea.org>
3
+   Copyleft GNU public license - http://www.gnu.org/copyleft/gpl.html
4
+   a blinkenarea.org project */
5
+
6
+#ifndef DEVICE_H
7
+#define DEVICE_H
8
+
9
+#include <string>
10
+
11
+#include "Io.h"
12
+#include "SerCfg.h"
13
+
14
+namespace Blinker {
15
+
16
+/// a character device
17
+class Device: public Io
18
+{
19
+public:
20
+  /**
21
+   * @brief constructor
22
+   * @param[in] path path to device
23
+   */
24
+  Device(const std::string &path);
25
+
26
+  /// destructor
27
+  ~Device();
28
+
29
+private:
30
+  /// copy constructor disabled
31
+  Device(const Device &that);
32
+
33
+  /// assignment operator disabled
34
+  const Device & operator=(const Device &that);
35
+
36
+public:
37
+  /**
38
+   * @brief set serial port configuration
39
+   * @param[in] serCfg serial port configuration
40
+   * @return if configuration succeeded
41
+   *
42
+   * This function should only be used if device is a serial port.
43
+   */
44
+  bool setSerCfg(const SerCfg& serCfg);
45
+
46
+  /**
47
+   * @brief write data to device
48
+   * @param[in] data data to write
49
+   * @return if the data could be written
50
+   */
51
+  bool write(const std::string &data);
52
+
53
+}; // class Device
54
+
55
+} // namespace Blinker
56
+
57
+#endif // #ifndef DEVICE_H
58
+
59
+
0 60