[libamb] - added value quality, removed deprecated GetFoo call, made updateFrequency...
[profile/ivi/automotive-message-broker.git] / plugins / common / serialport.hpp
1 #ifndef _SERIALPORT_H_
2 #define _SERIALPORT_H_
3
4 #include "abstractio.hpp"
5 #include "debugout.h"
6
7 #include <stdlib.h>
8 #include <stdio.h>
9 #include <unistd.h>
10 #include <sys/types.h>
11 #include <sys/stat.h>
12 #include <fcntl.h>
13 #include <termios.h>
14 #include <errno.h>
15 #include <stdio.h>
16
17 class SerialPort: public AbstractIo
18 {
19 private:
20         speed_t speed;
21
22 public:
23         SerialPort()
24                 :fd(0), speed(B9600)
25         {
26
27         }
28
29         SerialPort(int fileDesc)
30                 :fd(fileDesc)
31         {
32                 speed = B9600;
33
34                 setDescriptor(fd);
35         }
36
37         SerialPort(std::string _tty)
38                 :tty(_tty), fd(0)
39         {
40                 speed = B9600;
41         }
42
43         ~SerialPort()
44         {
45                 close();
46         }
47
48         int setSpeed(int newspeed)
49         {
50                 int ret = 0;
51                 switch(newspeed)
52                 {
53                         case 2400:
54                                 speed = B2400;
55                                 break;
56                         case 4800:
57                                 speed = B4800;
58                                 break;
59                         case 9600:
60                                 speed = B9600;
61                                 break;
62                         case 19200:
63                                 speed = B19200;
64                                 break;
65                         case 38400:
66                                 speed = B38400;
67                                 break;
68                         default:
69                                 ret = -EINVAL;
70                 }
71                 return ret;
72         }
73
74         bool open()
75         {
76                 fd = ::open(tty.c_str(), O_RDWR, O_NOCTTY);
77
78                 return setupDevice();
79         }
80
81         bool isOpen()
82         {
83                 return (fd > 0);
84         }
85
86         int fileDescriptor() { return fd; }
87
88         bool close()
89         {
90                 ::close(fd);
91         }
92
93         std::string read()
94         {
95                 char buff;
96                 std::string result;
97                 int bytesread = 0;
98                 while( bytesread = ::read(fd, &buff, 1) > 0 )
99                 {
100                         result += buff;
101                 }
102
103                 if(bytesread == -1)
104                         perror("Error while reading: ");
105
106                 return result;
107         }
108
109         void write(const std::string & data)
110         {
111                 int written = ::write(fd, data.c_str(), data.length());
112                 if(written == -1)
113                 {
114                         DebugOut(DebugOut::Warning)<<"Unable to write ("<<fd<<")"<<endl;
115                         perror("write error: ");
116                 }
117         }
118
119         void setDescriptor(int d)
120         {
121                 fd = d;
122                 //setupDevice();
123         }
124
125 private: ///methods
126
127         bool setupDevice()
128         {
129                 if(fd == -1)
130                 {
131                         DebugOut()<<"Cannot open serial device."<<endl;
132                         return false;
133                 }
134
135                 struct termios oldtio;
136                 tcgetattr(fd,&oldtio);
137
138                 oldtio.c_cflag |= CS8 | CLOCAL | CREAD;
139
140                 oldtio.c_iflag |= IGNPAR;
141                 oldtio.c_iflag &= ~(ICRNL | IMAXBEL);
142
143
144                 oldtio.c_oflag &= ~OPOST;
145
146                 oldtio.c_lflag |= ECHOE | ECHOK | ECHOCTL | ECHOKE;
147                 oldtio.c_lflag &= ~(ECHO | ICANON | ISIG);
148
149                 //oldtio.c_cc[VEOL]     = '\r';
150
151                 cfsetispeed(&oldtio, speed);
152                 cfsetospeed(&oldtio, speed);
153
154                 tcflush(fd, TCIFLUSH);
155                 tcsetattr(fd, TCSANOW, &oldtio);
156
157                 fcntl(fd,F_SETFL,O_NONBLOCK);
158
159                 return true;
160         }
161
162 private:
163         int fd;
164         std::string tty;
165
166 };
167
168
169 #endif