Debian Codegraph @

src/linux/link_tie.cc

raw
1 /* Hey EMACS -*- linux-c -*- */
2 /* $Id$ */
3
4 /* libticables2 - link cable library, a part of the TiLP project
5 * Copyright (C) 1999-2005 Romain Lievin
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22 /* "TiEmu" virtual link cable unit */
23
24 /*
25 * This unit use two pipes between 2 programs which use this lib.
26 * Convention used: 0 is an emulator and 1 is a linking program.
27 * One pipe is used for transferring information from 0 to 1 and the other
28 * pipe is used for transferring from 1 to 0.
29 */
30
31 #ifdef HAVE_CONFIG_H
32 #include <config.h>
33 #endif
34
35 #include <stdio.h>
36 #include <string.h>
37 #include <fcntl.h>
38 #include <unistd.h>
39 #include <sys/types.h>
40 #include <sys/stat.h>
41 #include <sys/ipc.h>
42 #include <sys/shm.h>
43 #include <errno.h>
44
45 #include "../ticables.h"
46 #include "../logging.h"
47 #include "../error.h"
48 #include "../gettext.h"
49 #include "../internal.h"
50 #include "detect.h"
51
52 #define BUFFER_SIZE 256
53 #define HIGH 666 // upper limit (used for avoiding timeout)
54 #define LOW 333 // lower limit
55
56 static key_t ipc_key;
57 static int shmid;
58 static int* shmaddr;
59 #define ref_cnt (*shmaddr) // counter of instances
60
61 static int rd[2] = { 0 }; // Pipe 0 <- 1 or 1 <- 0
62 static int wr[2] = { 0 }; // Pipe 0 -> 1 or 1 -> 0
63
64 static const char fifo_names[4][256] =
65 {
66 "/tmp/.vlc_1_0", "/tmp/.vlc_0_1",
67 "/tmp/.vlc_0_1", "/tmp/.vlc_1_0"
68 };
69
70 static int shm_check(void)
71 {
72 int shmid_;
73
74 if ((ipc_key = ftok("/tmp", 0x1234)) == -1)
75 {
76 return ERR_TIE_OPEN;
77 }
78 shmid_ = shmget(ipc_key, 1, IPC_CREAT | IPC_EXCL | 0666);
79
80 if ((shmid_ == -1) && (errno == EEXIST))
81 {
82 return 1;
83 }
84 else
85 {
86 shmctl(shmid_, IPC_RMID, NULL);
87 }
88
89 return 0;
90 }
91
92 static int tie_prepare(CableHandle *h)
93 {
94 // in fact, address & device are unused
95 switch(h->port)
96 {
97 case PORT_0: // automatic setting
98 h->address = shm_check();
99 break;
100 case PORT_1: // forced setting, for compatibility
101 case PORT_3:
102 h->address = 0; h->device = strdup("0->1");
103 break;
104 case PORT_2:
105 case PORT_4:
106 h->address = 1; h->device = strdup("1->0");
107 break;
108 default: return ERR_ILLEGAL_ARG;
109 }
110
111 return 0;
112 }
113
114 static int tie_reset(CableHandle *h);
115 static int tie_open(CableHandle *h)
116 {
117 int p = h->address;
118 int newseg = 0;
119
120 if ((ipc_key = ftok("/tmp", 0x1234)) == -1)
121 {
122 return ERR_TIE_OPEN;
123 }
124 shmid = shmget(ipc_key, 1, IPC_CREAT | IPC_EXCL | 0666);
125 if (shmid != -1)
126 {
127 newseg = 1;
128 }
129 else if ((shmid == -1) && (errno == EEXIST))
130 {
131 if ((shmid = shmget(ipc_key, 1, IPC_CREAT | 0666)) < 0)
132 {
133 return ERR_TIE_OPEN;
134 }
135 }
136 if ((shmaddr = (int *)shmat(shmid, NULL, 0)) == (int *)-1)
137 {
138 return ERR_TIE_OPEN;
139 }
140
141 /* Check if the pipes already exist else create them */
142 if (access(fifo_names[0], F_OK) | access(fifo_names[1], F_OK))
143 {
144 if ( mkfifo(fifo_names[0], O_RDONLY | O_WRONLY | O_NONBLOCK | S_IRWXU) == -1
145 || mkfifo(fifo_names[1], O_RDONLY | O_WRONLY | O_NONBLOCK | S_IRWXU) == -1)
146 {
147 return ERR_TIE_OPEN;
148 }
149 }
150
151 /* Open the pipes */
152 // Open the 1->0 pipe in reading
153 if ((rd[p] = open(fifo_names[2 * (p) + 0], O_RDONLY | O_NONBLOCK)) == -1)
154 {
155 ticables_warning(_("error: %s\n"), strerror(errno));
156 return ERR_TIE_OPEN;
157 }
158 // Open the 0->1 pipe in writing (in read mode at first)
159 if ((wr[p] = open(fifo_names[2 * (p) + 1], O_RDONLY | O_NONBLOCK)) == -1)
160 {
161 return ERR_TIE_OPEN;
162 }
163 if ((wr[p] = open(fifo_names[2 * (p) + 1], O_WRONLY | O_NONBLOCK)) == -1)
164 {
165 return ERR_TIE_OPEN;
166 }
167
168 ref_cnt = newseg ? 1 : 2;
169 return tie_reset(h);
170 }
171
172 static int tie_close(CableHandle *h)
173 {
174 int p = h->address;
175
176 if (rd[p])
177 {
178 if (close(rd[p]) == -1)
179 {
180 return ERR_TIE_CLOSE;
181 }
182 rd[p] = 0;
183 }
184 if (wr[p])
185 {
186 if (close(wr[p]) == -1)
187 {
188 return ERR_TIE_CLOSE;
189 }
190 wr[p] = 0;
191 }
192
193 ref_cnt--;
194 if (!ref_cnt)
195 {
196 struct shmid_ds shmid_ds;
197 shmdt(shmaddr);
198 shmctl(shmid, IPC_RMID, &shmid_ds);
199 }
200 else
201 {
202 shmdt(shmaddr);
203 }
204
205 return 0;
206 }
207
208 static int tie_reset(CableHandle *h)
209 {
210 int p = h->address;
211 uint8_t d;
212
213 if (ref_cnt < 2)
214 {
215 return 0;
216 }
217
218 /* Flush the pipe */
219 while(read(rd[p], (void *) (&d), 1) > 0);
220
221 return 0;
222 }
223
224 static int tie_put(CableHandle *h, uint8_t *data, uint32_t len)
225 {
226 int p = h->address;
227 tiTIME clk;
228 struct stat s;
229 ssize_t n = 0;
230
231 if (ref_cnt < 2)
232 {
233 return 0;
234 }
235
236 /* Transfer rate modulation */
237 TO_START(clk);
238 do
239 {
240 if (TO_ELAPSED(clk, h->timeout))
241 {
242 return ERR_WRITE_TIMEOUT;
243 }
244 if (fstat(wr[p], &s) < 0)
245 {
246 return ERR_WRITE_ERROR;
247 }
248 if (s.st_size > HIGH)
249 {
250 n = 0;
251 }
252 else if (s.st_size < LOW)
253 {
254 n = 1;
255 }
256 }
257 while (n <= 0);
258
259 /* Write the data with byte timeout */
260 TO_START(clk);
261 do
262 {
263 n = write(wr[p], (void *)data, len);
264
265 if (TO_ELAPSED(clk, h->timeout))
266 {
267 return ERR_WRITE_TIMEOUT;
268 }
269 }
270 while (n <= 0);
271
272 return 0;
273 }
274
275 static int tie_get(CableHandle *h, uint8_t *data, uint32_t len)
276 {
277 int p = h->address;
278 tiTIME clk;
279 ssize_t i, ret;
280
281 if (ref_cnt < 2)
282 {
283 return 0;
284 }
285
286 for(i = 0; i < len; )
287 {
288 TO_START(clk);
289 do
290 {
291 ret = read(rd[p], (void *)(data + i), len - i);
292
293 if (TO_ELAPSED(clk, h->timeout))
294 {
295 return ERR_READ_TIMEOUT;
296 }
297
298 if (ret == -1 && errno != EAGAIN)
299 {
300 return ERR_READ_ERROR;
301 }
302 }
303 while (ret <= 0);
304
305 i += ret;
306 }
307
308 return 0;
309 }
310
311 static int tie_probe(CableHandle *h)
312 {
313 return shm_check() ? 0 : ERR_PROBE_FAILED;
314 }
315
316 static int tie_check(CableHandle *h, int *status)
317 {
318 int p = h->address;
319 fd_set rdfs;
320 struct timeval tv;
321 int retval;
322
323 *status = STATUS_NONE;
324
325 if (ref_cnt < 2)
326 {
327 return 0;
328 }
329
330 FD_ZERO(&rdfs);
331 FD_SET(rd[p], &rdfs);
332 tv.tv_sec = 0;
333 tv.tv_usec = 0;
334
335 retval = select(rd[p] + 1, &rdfs, NULL, NULL, &tv);
336 switch (retval)
337 {
338 case -1: //error
339 return ERR_READ_ERROR;
340 case 0: //no data
341 return 0;
342 default: // data available
343 *status = STATUS_RX;
344 break;
345 }
346
347 return 0;
348 }
349
350 extern const CableFncts cable_tie =
351 {
352 CABLE_TIE,
353 "TIE",
354 N_("TiEmu"),
355 N_("Virtual link for TiEmu"),
356 0,
357 &tie_prepare,
358 &tie_open, &tie_close, &tie_reset, &tie_probe, NULL,
359 &tie_put, &tie_get, &tie_check,
360 &noop_set_red_wire, &noop_set_white_wire,
361 &noop_get_red_wire, &noop_get_white_wire,
362 NULL, NULL,
363 &noop_set_device,
364 NULL
365 };