~ubuntu-branches/ubuntu/natty/libticables/natty

« back to all changes in this revision

Viewing changes to src/win32/link_gry2.c

  • Committer: Bazaar Package Importer
  • Author(s): Krzysztof Burghardt
  • Date: 2009-05-17 13:34:18 UTC
  • Revision ID: james.westby@ubuntu.com-20090517133418-ad2h95ods2vycb6m
Tags: upstream-1.2.0
ImportĀ upstreamĀ versionĀ 1.2.0

Show diffs side-by-side

added added

removed removed

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