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