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