chiark / gitweb /
*** empty log message ***
[sympathy.git] / src / serial.c
1 /*
2  * serial.c:
3  *
4  * Copyright (c) 2008 James McKenzie <james@fishsoup.dhs.org>,
5  * All rights reserved.
6  *
7  */
8
9 static char rcsid[] = "$Id$";
10
11 /*
12  * $Log$
13  * Revision 1.10  2008/02/24 00:47:14  james
14  * *** empty log message ***
15  *
16  * Revision 1.9  2008/02/24 00:42:53  james
17  * *** empty log message ***
18  *
19  * Revision 1.8  2008/02/23 13:05:58  staffcvs
20  * *** empty log message ***
21  *
22  * Revision 1.7  2008/02/15 23:52:12  james
23  * *** empty log message ***
24  *
25  * Revision 1.6  2008/02/15 19:51:30  james
26  * *** empty log message ***
27  *
28  * Revision 1.5  2008/02/15 19:09:00  james
29  * *** empty log message ***
30  *
31  * Revision 1.4  2008/02/15 16:48:56  james
32  * *** empty log message ***
33  *
34  * Revision 1.3  2008/02/15 03:32:07  james
35  * *** empty log message ***
36  *
37  * Revision 1.2  2008/02/14 16:21:17  james
38  * *** empty log message ***
39  *
40  * Revision 1.1  2008/02/14 12:51:14  james
41  * *** empty log message ***
42  *
43  * Revision 1.4  2008/02/14 10:39:14  james
44  * *** empty log message ***
45  *
46  * Revision 1.3  2008/02/13 09:12:21  james
47  * *** empty log message ***
48  *
49  * Revision 1.2  2008/02/12 22:36:46  james
50  * *** empty log message ***
51  *
52  * Revision 1.1  2008/02/09 15:47:28  james
53  * *** empty log message ***
54  *
55  * Revision 1.2  2008/02/07 11:11:14  staffcvs
56  * *** empty log message ***
57  *
58  * Revision 1.1  2008/02/07 01:02:52  james
59  * *** empty log message ***
60  *
61  * Revision 1.3  2008/02/06 17:53:28  james
62  * *** empty log message ***
63  *
64  * Revision 1.2  2008/02/04 02:05:06  james
65  * *** empty log message ***
66  *
67  * Revision 1.1  2008/02/04 01:32:39  james
68  * *** empty log message ***
69  *
70  */
71
72 #include "project.h"
73 #include <pwd.h>
74 #include <dirent.h>
75 #include <sys/stat.h>
76
77
78 typedef struct
79 {
80   TTY_SIGNATURE;
81   Serial_lock *lock;
82   int fd;
83 } Serial;
84
85
86 static void
87 serial_close (TTY * _t)
88 {
89   Serial *t = (Serial *) _t;
90
91   if (!t)
92     return;
93
94   tcflush (t->fd, TCIOFLUSH);
95   close (t->fd);
96   free (t);
97 }
98
99
100 static int
101 serial_read (TTY * _t, void *buf, int len)
102 {
103   Serial *t = (Serial *) _t;
104   int red, done = 0;
105
106   t->blocked = serial_lock_check (t->lock);
107
108   if (t->blocked)
109     return 0;
110
111   do
112     {
113
114       red = wrap_read (t->fd, buf, len);
115       if (red < 0)
116         return -1;
117       if (!red)
118         return done;
119
120       buf += red;
121       len -= red;
122       done += red;
123     }
124   while (len);
125
126
127   return done;
128 }
129
130
131 static int
132 serial_write (TTY * _t, void *buf, int len)
133 {
134   int writ, done = 0;
135   Serial *t = (Serial *) _t;
136
137   t->blocked = serial_lock_check (t->lock);
138   if (t->blocked)
139     return 0;
140
141   do
142     {
143
144       writ = wrap_write (t->fd, buf, len);
145       if (writ < 0)
146         return -1;
147       if (!writ)
148         sleep (1);
149
150       buf += writ;
151       len -= writ;
152       done += writ;
153     }
154   while (len);
155
156
157   return done;
158 }
159
160 TTY *
161 serial_open (char *path, int lock_mode)
162 {
163   Serial *t;
164   pid_t child;
165   char name[1024];
166   struct winsize winsize = { 0 };
167   struct termios termios;
168   int fd;
169   Serial_lock *l;
170
171   l = serial_lock_new (path, lock_mode);
172   if (!l)
173     return NULL;
174
175
176   fd = open (path, O_RDWR | O_NOCTTY | O_NONBLOCK);
177
178   set_nonblocking (fd);
179
180
181   if (tcgetattr (fd, &termios))
182     {
183       close (fd);
184       return NULL;
185     }
186   default_termios (&termios);
187
188   if (tcsetattr (fd, TCSANOW, &termios))
189     {
190       close (fd);
191       return NULL;
192     }
193
194   t = (Serial *) malloc (sizeof (Serial));
195
196   t->lock = l;
197
198   strncpy (t->name, path, sizeof (t->name));
199   t->name[sizeof (t->name) - 1] = 0;
200
201   t->recv = serial_read;
202   t->xmit = serial_write;
203   t->close = serial_close;
204   t->fd = fd;
205   t->rfd = t->fd;
206   t->wfd = t->fd;
207   t->size.x = VT102_COLS;
208   t->size.y = VT102_ROWS;
209   t->blocked = serial_lock_check (t->lock);
210   t->hanging_up = 0;
211
212   return (TTY *) t;
213 }