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