Added the possibility to specify the baudrate of the GPS NMEA device
[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
16 class SerialPort: public AbstractIo
17 {
18 private:
19         speed_t speed;
20
21 public:
22         SerialPort(std::string _tty)
23                 :tty(_tty)
24         {
25                 speed = B9600;
26         }
27
28         ~SerialPort()
29         {
30                 close();
31         }
32
33         int setSpeed(int newspeed)
34         {
35                 int ret = 0;
36                 switch(newspeed)
37                 {
38                         case 2400: 
39                                 speed = B2400;
40                                 break;
41                         case 4800: 
42                                 speed = B4800;
43                                 break;
44                         case 9600: 
45                                 speed = B9600;
46                                 break;
47                         case 19200: 
48                                 speed = B19200;
49                                 break;
50                         case 38400: 
51                                 speed = B38400;
52                                 break;
53                         default:
54                                 ret = -EINVAL;
55                 }
56                 return ret;
57         }
58
59         bool open()
60         {
61                 fd = ::open(tty.c_str(), O_RDWR, O_NOCTTY);
62
63                 if(fd == -1)
64                 {
65                         DebugOut()<<"Cannot open serial device."<<endl;
66                         return false;
67                 }
68
69                 struct termios oldtio;
70                 tcgetattr(fd,&oldtio);
71
72                 oldtio.c_cflag |= CS8 | CLOCAL | CREAD;
73
74                 oldtio.c_iflag |= IGNPAR;
75                 oldtio.c_iflag &= ~(ICRNL | IMAXBEL);
76
77
78                 oldtio.c_oflag &= ~OPOST;
79
80                 oldtio.c_lflag |= ECHOE | ECHOK | ECHOCTL | ECHOKE;
81                 oldtio.c_lflag &= ~(ECHO | ICANON | ISIG);
82
83                 //oldtio.c_cc[VEOL]     = '\r';
84
85                 cfsetispeed(&oldtio, speed);
86                 cfsetospeed(&oldtio, speed);
87
88                 tcflush(fd, TCIFLUSH);
89                 tcsetattr(fd, TCSANOW, &oldtio);
90
91                 fcntl(fd,F_SETFL,O_NONBLOCK);
92
93                 return true;
94         }
95
96         int fileDescriptor() { return fd; }
97
98         bool close()
99         {
100                 ::close(fd);
101         }
102
103         std::string read()
104         {
105                 char buff;
106                 std::string result;
107                 int bytesread = 0;
108                 while( bytesread = ::read(fd,&buff,1) > 0 )
109                 {
110                         result += buff;
111                 }
112
113
114                 if(bytesread == -1)
115                         perror("Error while reading: ");
116
117                 return result;
118         }
119
120         void write(std::string data)
121         {
122                 int written = ::write(fd,data.c_str(),data.length());
123                 if(written == -1)
124                 {
125                         DebugOut()<<"Unable to write"<<endl;
126                 }
127         }
128
129 private:
130         int fd;
131         std::string tty;
132
133 };
134
135
136 #endif