Debian Codegraph @

src/win32/link_tie.cc

raw
1 /* Hey EMACS -*- win32-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 /* "TiEmulator" virtual link cable unit */
23
24 /*
25 * This unit uses two circular buffer implemented as shared memory.
26 * Names of shm are exchanged thru two FIFOs between the program which use this library.
27 *
28 * Convention used: 0 is an emulator and 1 is a linking program.
29 * One pipe is used for transferring information from 0 to 1 and the other
30 * pipe is used for transferring from 1 to 0.
31 */
32
33 #include <stdio.h>
34 #include <windows.h>
35
36 #include "../ticables.h"
37 #include "../logging.h"
38 #include "../error.h"
39 #include "../gettext.h"
40 #include "../internal.h"
41 #include "detect.h"
42
43 #define BUFSIZE 256
44
45 static const char cnt_name[] = "TiEmu Virtual Link (2)";
46
47 static const char name[4][256] =
48 {
49 "GtkTiEmu Virtual Link 0", "GtkTiEmu Virtual Link 1",
50 "GtkTiEmu Virtual Link 1", "GtkTiEmu Virtual Link 0"
51 };
52
53 typedef struct
54 {
55 uint8_t buf[BUFSIZE];
56 int start;
57 int end;
58 } LinkBuffer;
59
60 static HANDLE hRefCnt = NULL;
61 static HANDLE hSendBuf = NULL;
62 static HANDLE hRecvBuf = NULL;
63
64 static int *pRefCnt = NULL;
65 static LinkBuffer *pSendBuf = NULL;
66 static LinkBuffer *pRecvBuf = NULL;
67
68 static int shm_check(void)
69 {
70 int ret;
71
72 hRefCnt = CreateFileMapping((HANDLE) (-1), NULL, PAGE_READWRITE, 0, sizeof(int), (LPCTSTR) cnt_name);
73 if (hRefCnt == NULL)
74 {
75 return ERR_TIE_OPENFILEMAPPING;
76 }
77 ret = GetLastError() == ERROR_ALREADY_EXISTS ? 1 : 0;
78 if (GetLastError() != ERROR_ALREADY_EXISTS)
79 {
80 CloseHandle(hRefCnt);
81 }
82
83 return ret ? 1 : 0;
84 }
85
86 static int tie_prepare(CableHandle *h)
87 {
88 // in fact, address & device are unused
89 switch(h->port)
90 {
91 case PORT_0: // automatic setting
92 h->address = shm_check();
93 break;
94
95 case PORT_1: // forced setting, for compatibility
96 case PORT_3:
97 h->address = 0; h->device = strdup("0->1");
98 break;
99
100 case PORT_2:
101 case PORT_4:
102 h->address = 1; h->device = strdup("1->0");
103 break;
104
105 default: return ERR_ILLEGAL_ARG;
106 }
107
108 return 0;
109 }
110
111 static int tie_open(CableHandle *h)
112 {
113 int p = h->address;
114 int ret;
115
116 /* Create shared counter */
117 hRefCnt = CreateFileMapping((HANDLE) (-1), NULL, PAGE_READWRITE, 0, sizeof(int), (LPCTSTR) cnt_name);
118 if (hRefCnt == NULL)
119 {
120 return ERR_TIE_OPENFILEMAPPING;
121 }
122 ret = GetLastError() == ERROR_ALREADY_EXISTS;
123
124 /* Create a FileMapping objects */
125 hSendBuf = CreateFileMapping((HANDLE) (-1), NULL, PAGE_READWRITE, 0, sizeof(LinkBuffer), (LPCTSTR) name[2 * p + 0]);
126 if (hSendBuf == NULL)
127 {
128 return ERR_TIE_OPENFILEMAPPING;
129 }
130
131 hRecvBuf = CreateFileMapping((HANDLE) (-1), NULL, PAGE_READWRITE, 0, sizeof(LinkBuffer), (LPCTSTR) name[2 * p + 1]);
132 if (hRecvBuf == NULL)
133 {
134 return ERR_TIE_OPENFILEMAPPING;
135 }
136
137 /* Map them */
138 pSendBuf = (LinkBuffer *) MapViewOfFile(hSendBuf, FILE_MAP_ALL_ACCESS, 0, 0, sizeof(LinkBuffer));
139 if (pSendBuf == NULL)
140 {
141 return ERR_TIE_MAPVIEWOFFILE;
142 }
143
144 pRecvBuf = (LinkBuffer *) MapViewOfFile(hRecvBuf, FILE_MAP_ALL_ACCESS, 0, 0, sizeof(LinkBuffer));
145 if (pRecvBuf == NULL)
146 {
147 return ERR_TIE_MAPVIEWOFFILE;
148 }
149
150 /* Reset buffers */
151 pSendBuf->start = pSendBuf->end = 0;
152 pRecvBuf->start = pRecvBuf->end = 0;
153
154 /* Increase ref counter */
155 pRefCnt = (int *)MapViewOfFile(hRefCnt, FILE_MAP_ALL_ACCESS, 0, 0, sizeof(int));
156 *pRefCnt = ret ? 2 : 1;
157
158 //printf("pRefCnt = %i\n", *pRefCnt);
159
160 return 0;
161 }
162
163 static int tie_close(CableHandle *h)
164 {
165 /* Close mappings */
166 if (hSendBuf)
167 {
168 UnmapViewOfFile(pSendBuf);
169 pSendBuf = NULL;
170 CloseHandle(hSendBuf);
171 }
172
173 if (hRecvBuf)
174 {
175 UnmapViewOfFile(pRecvBuf);
176 pRecvBuf = NULL;
177 CloseHandle(hRecvBuf);
178 }
179
180 /* Decrease ref counter */
181 if (pRefCnt)
182 {
183 (*pRefCnt)--;
184 if (*pRefCnt == 0)
185 {
186 UnmapViewOfFile(pRecvBuf);
187 CloseHandle(hRefCnt);
188 }
189 }
190 else
191 {
192 ticables_critical("tie_close(): reference counter busted !\n");
193 }
194
195 return 0;
196 }
197
198 static int tie_reset(CableHandle *h)
199 {
200 if (pRefCnt)
201 {
202 if (*pRefCnt < 2)
203 {
204 return 0;
205 }
206
207 if (!hSendBuf || !hRecvBuf)
208 {
209 return 0;
210 }
211
212 if (pSendBuf)
213 {
214 pSendBuf->start = pSendBuf->end = 0;
215 }
216 else
217 {
218 ticables_critical("tie_reset(): send buffer busted !\n");
219 }
220 if (pRecvBuf)
221 {
222 pRecvBuf->start = pRecvBuf->end = 0;
223 }
224 else
225 {
226 ticables_critical("tie_reset(): receive buffer busted !\n");
227 }
228 }
229 else
230 {
231 ticables_critical("tie_reset(): reference counter busted !\n");
232 }
233
234 return 0;
235 }
236
237 static int tie_probe(CableHandle *h)
238 {
239 return shm_check() ? 0 : ERR_PROBE_FAILED;
240 }
241
242 static int tie_put(CableHandle *h, uint8_t *data, uint32_t len)
243 {
244 unsigned int i;
245 tiTIME clk;
246
247 if (pRefCnt)
248 {
249 if (*pRefCnt < 2)
250 {
251 return 0;
252 }
253
254 if (!hSendBuf)
255 {
256 return 0;
257 }
258 if (pSendBuf)
259 {
260 for(i = 0; i < len; i++)
261 {
262 TO_START(clk);
263 do
264 {
265 if (TO_ELAPSED(clk, h->timeout))
266 {
267 return ERR_WRITE_TIMEOUT;
268 }
269 }
270 while (((pSendBuf->end + 1) & (BUFSIZE-1)) == pSendBuf->start);
271
272 pSendBuf->buf[pSendBuf->end] = data[i];
273 pSendBuf->end = (pSendBuf->end + 1) & (BUFSIZE-1);
274 }
275 }
276 else
277 {
278 ticables_critical("tie_put(): send buffer busted !\n");
279 }
280 }
281 else
282 {
283 ticables_critical("tie_put(): reference counter busted !\n");
284 }
285
286 return 0;
287 }
288
289 static int tie_get(CableHandle *h, uint8_t *data, uint32_t len)
290 {
291 unsigned int i;
292 tiTIME clk;
293
294 if (pRefCnt)
295 {
296 if (*pRefCnt < 2)
297 {
298 return 0;
299 }
300
301 if (!hRecvBuf)
302 {
303 return 0;
304 }
305 if (pRecvBuf)
306 {
307 for(i = 0; i < len; i++)
308 {
309 TO_START(clk);
310 do
311 {
312 if (TO_ELAPSED(clk, h->timeout))
313 {
314 return ERR_READ_TIMEOUT;
315 }
316 }
317 while (pRecvBuf->start == pRecvBuf->end);
318
319 data[i] = pRecvBuf->buf[pRecvBuf->start];
320 pRecvBuf->start = (pRecvBuf->start + 1) & (BUFSIZE-1);
321 }
322 }
323 else
324 {
325 ticables_critical("tie_get(): receive buffer busted !\n");
326 }
327 }
328 else
329 {
330 ticables_critical("tie_get(): reference counter busted !\n");
331 }
332
333 return 0;
334 }
335
336 static int tie_check(CableHandle *h, int *status)
337 {
338 if (pRefCnt)
339 {
340 if (*pRefCnt < 2)
341 {
342 return 0;
343 }
344
345 if (!hRecvBuf)
346 {
347 return 0;
348 }
349 if (pRecvBuf)
350 {
351 *status = !(pRecvBuf->start == pRecvBuf->end);
352 }
353 else
354 {
355 ticables_critical("tie_check(): receive buffer busted !\n");
356 }
357 }
358 else
359 {
360 ticables_critical("tie_check(): reference counter busted !\n");
361 }
362
363 return 0;
364 }
365
366 extern const CableFncts cable_tie =
367 {
368 CABLE_TIE,
369 "TIE",
370 N_("TiEmu"),
371 N_("Virtual link for TiEmu"),
372 0,
373 &tie_prepare,
374 &tie_open, &tie_close, &tie_reset, &tie_probe, NULL,
375 &tie_put, &tie_get, &tie_check,
376 &noop_set_red_wire, &noop_set_white_wire,
377 &noop_get_red_wire, &noop_get_white_wire,
378 NULL, NULL,
379 &noop_set_device,
380 NULL
381 };