Debian Codegraph @

src/win32/link_gry2.cc

raw
1 /* Hey EMACS -*- win32-c -*- */
2 /* $Id: link_gry.c 1622 2005-09-10 06:33:36Z roms $ */
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 //!!!!!!!!!!!!!!!!!! Not used, for use of overlapped i/o !!!!!!!!!!!!!!!!!!!!
23
24 /* "Grey TIGraphLink" link cable unit */
25
26 #include <stdio.h>
27 #include <windows.h>
28
29 #include "../ticables.h"
30 #include "../logging.h"
31 #include "../error.h"
32 #include "../gettext.h"
33 #include "../internal.h"
34 #include "detect.h"
35
36 #define hCom (HANDLE)(h->priv)
37
38 static int gry_prepare(CableHandle *h)
39 {
40 const char * device;
41 switch(h->port)
42 {
43 case PORT_1: h->address = 0x3f8; device = "COM1"; break;
44 case PORT_2: h->address = 0x2f8; device = "COM2"; break;
45 case PORT_3: h->address = 0x3e8; device = "COM3"; break;
46 case PORT_4: h->address = 0x3e8; device = "COM4"; break;
47 default: return ERR_ILLEGAL_ARG;
48 }
49
50 if (h->device == NULL)
51 {
52 h->device = strdup(device);
53 }
54
55 return 0;
56 }
57
58 static int gry_open(CableHandle *h)
59 {
60 DCB dcb;
61 BOOL fSuccess;
62 COMMTIMEOUTS cto;
63
64 // Open device
65 h->priv = (void *)CreateFile(h->device, GENERIC_READ | GENERIC_WRITE, 0,
66 NULL, OPEN_EXISTING,
67 FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED /*| FILE_FLAG_NO_BUFFERING*/,
68 NULL);
69 if (hCom == INVALID_HANDLE_VALUE)
70 {
71 ticables_warning("CreateFile");
72 return ERR_GRY_CREATEFILE;
73 }
74
75 // Setup buffer size
76 fSuccess = SetupComm(hCom, 1024, 1024);
77 if (!fSuccess)
78 {
79 ticables_warning("SetupComm");
80 return ERR_GRY_SETUPCOMM;
81 }
82
83 // Retrieve config structure
84 fSuccess = GetCommState(hCom, &dcb);
85 if (!fSuccess)
86 {
87 ticables_warning("GetCommState");
88 return ERR_GRY_GETCOMMSTATE;
89 }
90
91 // Fills the structure with config
92 dcb.BaudRate = CBR_9600; // 9600 bauds
93 dcb.fBinary = TRUE; // Binary mode
94 dcb.fParity = FALSE; // Parity checking disabled
95 dcb.fOutxCtsFlow = FALSE; // No output flow control
96 dcb.fOutxDsrFlow = FALSE; // Idem
97 dcb.fDtrControl = DTR_CONTROL_DISABLE; // Provide power supply
98 dcb.fDsrSensitivity = FALSE; // ignore DSR status
99 dcb.fOutX = FALSE; // no XON/XOFF flow control
100 dcb.fInX = FALSE; // idem
101 dcb.fErrorChar = FALSE; // no replacement
102 dcb.fNull = FALSE; // don't discard null chars
103 dcb.fRtsControl = RTS_CONTROL_ENABLE; // Provide power supply
104 dcb.fAbortOnError = FALSE; // do not report errors
105
106 dcb.ByteSize = 8; // 8 bits
107 dcb.Parity = NOPARITY; // no parity checking
108 dcb.StopBits = ONESTOPBIT; // 1 stop bit
109
110 // Config COM port
111 fSuccess = SetCommState(hCom, &dcb);
112 if (!fSuccess)
113 {
114 ticables_warning("SetCommState");
115 return ERR_GRY_SETCOMMSTATE;
116 }
117
118 // Wait for GrayLink to be ready
119 Sleep(250);
120
121 // Set timeouts
122 fSuccess = GetCommTimeouts(hCom, &cto);
123 if (!fSuccess)
124 {
125 ticables_warning("GetCommTimeouts");
126 return ERR_GRY_GETCOMMTIMEOUT;
127 }
128
129 cto.ReadIntervalTimeout = 0;
130
131 cto.ReadTotalTimeoutMultiplier = 0;
132 cto.ReadTotalTimeoutConstant = 100 * h->timeout;
133
134 cto.WriteTotalTimeoutMultiplier = 0;
135 cto.WriteTotalTimeoutConstant = 100 * h->timeout;
136
137 fSuccess = SetCommTimeouts(hCom, &cto);
138 if (!fSuccess)
139 {
140 ticables_warning("SetCommTimeouts");
141 return ERR_GRY_SETCOMMTIMEOUT;
142 }
143
144 // Monitor receiving of chars
145 fSuccess = SetCommMask(hCom, EV_RXCHAR);
146 if (!fSuccess)
147 {
148 ticables_warning("SetCommMask");
149 return ERR_GRY_SETCOMMMASK;
150 }
151
152 // Flush/Dicard buffers
153 fSuccess = PurgeComm(hCom, PURGE_TXCLEAR | PURGE_RXCLEAR);
154 if (!fSuccess)
155 {
156 ticables_warning("PurgeComm");
157 return ERR_GRY_PURGECOMM;
158 }
159
160 return 0;
161 }
162
163 static int gry_close(CableHandle *h)
164 {
165 if (hCom)
166 {
167 CloseHandle(hCom);
168 hCom = INVALID_HANDLE_VALUE;
169 }
170
171 return 0;
172 }
173
174 static int gry_reset(CableHandle *h)
175 {
176 BOOL fSuccess;
177
178 fSuccess = PurgeComm(hCom, PURGE_TXCLEAR | PURGE_RXCLEAR);
179 if (!fSuccess)
180 {
181 ticables_warning("PurgeComm");
182 return ERR_GRY_PURGECOMM;
183 }
184
185 return 0;
186 }
187
188 static int gry_probe(CableHandle *h)
189 {
190 DWORD status; //MS_CTS_ON or MS_DTR_ON
191
192 EscapeCommFunction(hCom, SETDTR);
193 EscapeCommFunction(hCom, SETRTS);
194 GetCommModemStatus(hCom, &status); // Get MCR values
195 if (status != 0x20)
196 {
197 return ERR_PROBE_FAILED;
198 }
199
200 EscapeCommFunction(hCom, SETDTR);
201 EscapeCommFunction(hCom, CLRRTS);
202 GetCommModemStatus(hCom, &status);
203 if (status != 0x20)
204 {
205 return ERR_PROBE_FAILED;
206 }
207
208 EscapeCommFunction(hCom, CLRDTR);
209 EscapeCommFunction(hCom, CLRRTS);
210 GetCommModemStatus(hCom, &status);
211 if (status != 0x00)
212 {
213 return ERR_PROBE_FAILED;
214 }
215
216 EscapeCommFunction(hCom, CLRDTR);
217 EscapeCommFunction(hCom, SETRTS);
218 GetCommModemStatus(hCom, &status);
219 if (status != 0x00)
220 {
221 return ERR_PROBE_FAILED;
222 }
223
224 EscapeCommFunction(hCom, SETDTR);
225 EscapeCommFunction(hCom, SETRTS);
226 GetCommModemStatus(hCom, &status);
227 if (status != 0x20)
228 {
229 return ERR_PROBE_FAILED;
230 }
231
232 return 0;
233 }
234
235 static int gry_put(CableHandle* h, uint8_t *data, uint32_t len)
236 {
237 BOOL fSuccess;
238 DWORD nBytesWritten;
239 OVERLAPPED ol;
240
241 memset(&ol, 0, sizeof(OVERLAPPED));
242 fSuccess = WriteFile(hCom, data, len, &nBytesWritten, &ol);
243
244 while(HasOverlappedIoCompleted(&ol) == FALSE) Sleep(0);
245
246 fSuccess = GetOverlappedResult(hCom, &ol, &nBytesWritten, FALSE);
247 if (!fSuccess)
248 {
249 ticables_warning("WriteFile");
250 return ERR_WRITE_ERROR;
251 }
252 else if (nBytesWritten == 0)
253 {
254 ticables_warning("WriteFile");
255 return ERR_WRITE_TIMEOUT;
256 }
257 else if (nBytesWritten < len)
258 {
259 ticables_warning("WriteFile");
260 return ERR_WRITE_ERROR;
261 }
262
263 return 0;
264 }
265
266 static int gry_get(CableHandle* h, uint8_t *data, uint32_t len)
267 {
268 BOOL fSuccess;
269 DWORD nBytesRead;
270 OVERLAPPED ol;
271 uint32_t i;
272
273 for(i = 0; i < len;)
274 {
275 memset(&ol, 0, sizeof(OVERLAPPED));
276 fSuccess = ReadFile(hCom, data + i, len - i, &nBytesRead, &ol);
277
278 while(HasOverlappedIoCompleted(&ol) == FALSE) Sleep(0);
279 fSuccess = GetOverlappedResult(hCom, &ol, &nBytesRead, FALSE);
280
281 if (!fSuccess)
282 {
283 ticables_warning("ReadFile");
284 return ERR_READ_ERROR;
285 }
286 else if (nBytesRead == 0)
287 {
288 ticables_warning("ReadFile");
289 return ERR_READ_TIMEOUT;
290 }
291
292 i += nBytesRead;
293 }
294 printf("get : %i %i %i\n", fSuccess, nBytesRead, len);
295
296 return 0;
297 }
298
299 // pb with this code: works if no receiving of chars has been previously done
300 static int gry_check(CableHandle *h, int *status)
301 {
302 BOOL fSuccess;
303 static DWORD dwEvtMask = 0;
304 static OVERLAPPED ol = { 0 };
305
306 static BOOL iop;
307 static BOOL ioPending = FALSE;
308
309 if (ioPending == FALSE)
310 {
311 memset(&ol, 0, sizeof(OVERLAPPED));
312 fSuccess = WaitCommEvent(hCom, &dwEvtMask, &ol);
313
314 ioPending = TRUE;
315 printf("$ (%i)\n", ioPending);
316 }
317 else
318 {
319 if (HasOverlappedIoCompleted(&ol))
320 {
321 if (dwEvtMask & EV_RXCHAR)
322 {
323 *status = STATUS_RX;
324 printf("#\n");
325 ioPending = FALSE;
326 }
327 }
328 }
329
330 return 0;
331 }
332
333 static int gry_timeout(CableHandle *h)
334 {
335 BOOL fSuccess;
336 COMMTIMEOUTS cto;
337
338 cto.ReadIntervalTimeout = 0;
339
340 cto.ReadTotalTimeoutMultiplier = 0;
341 cto.ReadTotalTimeoutConstant = 100 * h->timeout;
342
343 cto.WriteTotalTimeoutMultiplier = 0;
344 cto.WriteTotalTimeoutConstant = 100 * h->timeout;
345
346 fSuccess = SetCommTimeouts(hCom, &cto);
347 if (!fSuccess)
348 {
349 ticables_warning("SetCommTimeouts");
350 return ERR_GRY_SETCOMMTIMEOUT;
351 }
352
353 return 0;
354 }
355
356 static int gry_set_device(CableHandle *h, const char * device)
357 {
358 if (device != NULL)
359 {
360 char * device2 = strdup(device);
361 if (device2 != NULL)
362 {
363 free(h->device);
364 h->device = device2;
365 }
366 else
367 {
368 ticables_warning(_("unable to set device %s.\n"), device);
369 }
370 return 0;
371 }
372 return ERR_ILLEGAL_ARG;
373 }
374
375 extern const CableFncts cable_gry =
376 {
377 CABLE_GRY,
378 "GRY",
379 N_("GrayLink"),
380 N_("GrayLink serial cable"),
381 !0,
382 &gry_prepare,
383 &gry_open, &gry_close, &gry_reset, &gry_probe, &gry_timeout,
384 &gry_put, &gry_get, &gry_check,
385 &noop_set_red_wire, &noop_set_white_wire,
386 &noop_get_red_wire, &noop_get_white_wire,
387 NULL, NULL,
388 &gry_set_device,
389 NULL
390 };