Mostly minor fixes up until version 0.8.10.
[irreco.git] / irtrans / irserver / src / linuxserio.c
blobafc9adf12c41deabc0a3f91865aa6d25d83288d7
1 /*
2 * Copyright (c) 2007, IRTrans GmbH
3 * All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 * * Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * * Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 * * Neither the name of IRTrans GmbH nor the
13 * names of its contributors may be used to endorse or promote products
14 * derived from this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED BY IRTrans GmbH ``AS IS'' AND ANY
17 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 * DISCLAIMED. IN NO EVENT SHALL IRTrans GmbH BE LIABLE FOR ANY
20 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 #ifdef LINUX
32 #include <sys/time.h>
33 #include <sys/types.h>
34 #include <stdint.h>
35 #include <unistd.h>
36 #include <stdio.h>
37 #include <termios.h>
38 #include <sys/stat.h>
39 #include <fcntl.h>
40 #include <sys/ioctl.h>
41 #include <sys/socket.h>
42 #include <netinet/in.h>
43 #include <sys/un.h>
44 #include <arpa/inet.h>
45 #include <stdlib.h>
47 #include "remote.h"
48 #include "errcode.h"
49 #include "lowlevel.h"
50 #include "serio.h"
51 #include "global.h"
54 #define BAUDRATE B38400
56 char SerialDevice[256];
57 extern char baudrate[10];
59 int hCom;
61 void msSleep (int time)
63 struct timeval tv;
65 tv.tv_sec = time / 1000;
66 tv.tv_usec = (time % 1000) * 1000;
68 select (0,NULL,NULL,NULL,&tv);
71 void WriteSerialString (byte pnt[],int len)
73 int res,stat;
75 res = write (hCom,pnt,len);
76 if (res != len) {
77 log_print ("IRTrans Connection lost. Aborting ...\n",LOG_FATAL);
78 exit (-1);
82 int ReadSerialString (byte pnt[],int len,word timeout)
84 int bytes,total = 0;
85 struct timeval tv;
86 fd_set fs;
88 while (total < len) {
89 FD_ZERO (&fs);
90 FD_SET (hCom,&fs);
91 tv.tv_sec = timeout / 1000;
92 tv.tv_usec = (timeout % 1000) * 1000;
93 bytes = select (hCom+1,&fs,NULL,NULL,&tv);
94 if (!bytes) return (total);
95 bytes = read (hCom,pnt+total,len-total);
96 total += bytes;
98 return (total);
101 int WriteSerialStringEx (DEVICEINFO *dev,byte pnt[],int len)
103 int res,stat;
105 res = write (dev->io.comport,pnt,len);
106 if (res != len) {
107 if (dev->io.if_type == IF_USB && !(mode_flag & NO_RECONNECT)) {
108 close (dev->io.comport);
109 while (stat) {
110 stat = OpenSerialPortEx (dev->io.node,&dev->io.comport,0);
111 if (stat) sleep (10);
114 else {
115 log_print ("IRTrans Connection lost. Aborting ...\n",LOG_FATAL);
116 exit (-1);
118 return (ERR_TIMEOUT);
120 return (0);
124 int ReadSerialStringEx (DEVICEINFO *dev,byte pnt[],int len,word timeout)
126 int bytes,total = 0;
127 struct timeval tv;
128 fd_set fs;
130 while (total < len) {
131 FD_ZERO (&fs);
132 FD_SET (dev->io.comport,&fs);
133 tv.tv_sec = timeout / 1000;
134 tv.tv_usec = (timeout % 1000) * 1000;
135 bytes = select (dev->io.comport+1,&fs,NULL,NULL,&tv);
136 if (!bytes) return (total);
137 bytes = read (dev->io.comport,pnt+total,len-total);
138 total += bytes;
140 return (total);
143 void FlushCom ()
149 void FlushComEx(HANDLE fp)
151 int bytes;
152 struct timeval tv;
153 fd_set fs;
154 char dummy[256];
156 FD_ZERO (&fs);
157 FD_SET (fp,&fs);
158 tv.tv_sec = 0;
159 tv.tv_usec = 10000;
160 bytes = select (fp+1,&fs,NULL,NULL,&tv);
161 if (!bytes) return;
162 bytes = read (fp,dummy,256);
167 int OpenSerialPort(char Pname[])
169 int parnum = 0,res,flg;
170 struct termios portterm;
172 strcpy (SerialDevice,Pname);
173 if ((hCom = open(Pname, O_RDWR | O_NOCTTY)) < 0) return (ERR_OPEN);
175 if (!isatty(hCom)) {
176 close(hCom);
177 return (ERR_OPEN);
180 #ifndef DBOX
181 if (flock(hCom, LOCK_EX | LOCK_NB) < 0) {
182 close(hCom);
183 return (ERR_FLOCK);
185 #endif
187 portterm.c_cflag = CS8 | CREAD | CLOCAL;
189 portterm.c_cc[VMIN] = 1;
190 portterm.c_cc[VTIME] = 0;
192 cfsetispeed(&portterm, BAUDRATE);
193 cfsetospeed(&portterm, BAUDRATE);
195 portterm.c_lflag = 0;
197 portterm.c_iflag = IGNBRK;
198 portterm.c_oflag = 0;
201 tcflush(hCom, TCIOFLUSH);
202 if (tcsetattr(hCom, TCSANOW, &portterm) < 0) {
203 close(hCom);
204 return (ERR_STTY);
206 msSleep (1000);
208 tcflush(hCom, TCIOFLUSH);
210 return (0);
215 int OpenSerialPortEx (char Pname[],int *port,int wait)
217 int res,flg;
218 struct termios portterm;
219 if ((*port = open(Pname, O_RDWR | O_NOCTTY)) < 0) return (ERR_OPEN);
221 if (!isatty(*port)) {
222 close(*port);
223 return (ERR_OPEN);
226 #ifndef DBOX
227 if (flock(*port, LOCK_EX | LOCK_NB) < 0) {
228 close(*port);
229 return (ERR_FLOCK);
231 #endif
233 portterm.c_cflag = CS8 | CREAD | CLOCAL;
235 portterm.c_cc[VMIN] = 1;
236 portterm.c_cc[VTIME] = 0;
239 if (!strcmp (baudrate,"4800")) {
240 cfsetispeed(&portterm, B4800);
241 cfsetospeed(&portterm, B4800);
244 else if (!strcmp (baudrate,"9600")) {
245 cfsetispeed(&portterm, B9600);
246 cfsetospeed(&portterm, B9600);
249 else if (!strcmp (baudrate,"19200")) {
250 cfsetispeed(&portterm, B19200);
251 cfsetospeed(&portterm, B19200);
253 else {
254 cfsetispeed(&portterm, BAUDRATE);
255 cfsetospeed(&portterm, BAUDRATE);
258 portterm.c_lflag = 0;
260 portterm.c_iflag = IGNBRK;
261 portterm.c_oflag = 0;
264 tcflush(*port, TCIOFLUSH);
265 if (tcsetattr(*port, TCSANOW, &portterm) < 0) {
266 close(*port);
267 return (ERR_STTY);
269 msSleep (1000);
271 tcflush(*port, TCIOFLUSH);
273 return (0);
277 #ifdef DBOX
279 tcflush (int fd,int mode)
281 char st[1024];
282 do {
283 } while (ReadSerialString (st,1000,10) == 1000);
286 #endif
289 #endif