summaryrefslogtreecommitdiffstats
path: root/lib/libUPnP/Neptune/Source/System/Win32/NptWin32SerialPort.cpp
blob: 4dfc23a603053e93eaf09c7b91553b160d656451 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
/*****************************************************************
|
|   Neptune - Serial Ports :: Win32 Implementation
|
|   (c) 2001-2007 Gilles Boccon-Gibod
|   Author: Gilles Boccon-Gibod (bok@bok.net)
|
 ****************************************************************/

/*----------------------------------------------------------------------
|   includes
+---------------------------------------------------------------------*/
#include <windows.h>

#include "NptUtils.h"
#include "NptSerialPort.h"
#include "NptStrings.h"
#include "NptLogging.h"

#ifndef TARGET_WINDOWS_STORE
/*----------------------------------------------------------------------
|   NPT_Win32HandletWrapper
+---------------------------------------------------------------------*/
class NPT_Win32HandleWrapper
{
public:
    // constructors and destructor
    NPT_Win32HandleWrapper(HANDLE handle) : m_Handle(handle) {}
    ~NPT_Win32HandleWrapper() {
        CloseHandle(m_Handle);
    }

    // methods
    HANDLE GetHandle() { return m_Handle; }

private:
    // members
    HANDLE m_Handle;
};

typedef NPT_Reference<NPT_Win32HandleWrapper> NPT_Win32HandleReference;

/*----------------------------------------------------------------------
|   NPT_Win32SerialPortStream
+---------------------------------------------------------------------*/
class NPT_Win32SerialPortStream
{
public:
    // constructors and destructor
    NPT_Win32SerialPortStream(NPT_Win32HandleReference handle) :
      m_HandleReference(handle) {}

protected:
    // constructors and destructors
    virtual ~NPT_Win32SerialPortStream() {}

    // members
    NPT_Win32HandleReference m_HandleReference;
};

/*----------------------------------------------------------------------
|   NPT_Win32SerialPortInputStream
+---------------------------------------------------------------------*/
class NPT_Win32SerialPortInputStream : public NPT_InputStream,
                                       private NPT_Win32SerialPortStream
                                
{
public:
    // constructors and destructor
    NPT_Win32SerialPortInputStream(NPT_Win32HandleReference& handle) :
        NPT_Win32SerialPortStream(handle) {}

    // NPT_InputStream methods
    NPT_Result Read(void*     buffer, 
                    NPT_Size  bytes_to_read, 
                    NPT_Size* bytes_read);
    NPT_Result Seek(NPT_Position /* offset */) {
        return NPT_ERROR_NOT_SUPPORTED;
    }
    NPT_Result Tell(NPT_Position& /* offset */) {
        return NPT_ERROR_NOT_SUPPORTED;
    }
    NPT_Result GetSize(NPT_LargeSize& /* size */) {
        return NPT_ERROR_NOT_SUPPORTED;
    }
    NPT_Result GetAvailable(NPT_LargeSize& /* available */) {
        return NPT_ERROR_NOT_SUPPORTED;
    }
};

/*----------------------------------------------------------------------
|   NPT_Win32SerialPortInputStream::Read
+---------------------------------------------------------------------*/
NPT_Result
NPT_Win32SerialPortInputStream::Read(void*     buffer, 
                                     NPT_Size  bytes_to_read, 
                                     NPT_Size* bytes_read)
{
    DWORD nb_read = 0;
    BOOL result = ReadFile(m_HandleReference->GetHandle(), 
                           buffer, 
                           bytes_to_read, 
                           &nb_read, 
                           NULL);
    if (result == TRUE) {
        if (bytes_read) *bytes_read = nb_read;
        return NPT_SUCCESS;
    } else {
        if (bytes_read) *bytes_read = 0;
        return NPT_FAILURE;
    }
}

/*----------------------------------------------------------------------
|   NPT_Win32SerialPortOutputStream
+---------------------------------------------------------------------*/
class NPT_Win32SerialPortOutputStream : public NPT_OutputStream,
                                        private NPT_Win32SerialPortStream
{
public:
    // constructors and destructor
    NPT_Win32SerialPortOutputStream(NPT_Win32HandleReference& handle) :
        NPT_Win32SerialPortStream(handle) {}

    // NPT_InputStream methods
    NPT_Result Write(const void* buffer, 
                     NPT_Size    bytes_to_write, 
                     NPT_Size*   bytes_written);
    NPT_Result Seek(NPT_Position /* offset */) {
        return NPT_ERROR_NOT_SUPPORTED;
    }
    NPT_Result Tell(NPT_Position& /* offset */) {
        return NPT_ERROR_NOT_SUPPORTED;
    }
};

/*----------------------------------------------------------------------
|   NPT_Win32SerialPortOutputStream::Write
+---------------------------------------------------------------------*/
NPT_Result
NPT_Win32SerialPortOutputStream::Write(const void* buffer, 
                                       NPT_Size    bytes_to_write, 
                                       NPT_Size*   bytes_written)
{
    DWORD nb_written = 0;

    BOOL result = WriteFile(m_HandleReference->GetHandle(), 
                            buffer, 
                            bytes_to_write, 
                            &nb_written, 
                            NULL);
    if (result == TRUE) {
        if (bytes_written) *bytes_written = nb_written;
        return NPT_SUCCESS;
    } else {
        if (bytes_written) *bytes_written = 0;
        return NPT_FAILURE;
    }
}

/*----------------------------------------------------------------------
|   NPT_Win32SerialPort
+---------------------------------------------------------------------*/
class NPT_Win32SerialPort: public NPT_SerialPortInterface
{
public:
    // constructors and destructor
    NPT_Win32SerialPort(const char* name);
   ~NPT_Win32SerialPort();

    // NPT_SerialPortInterface methods
    NPT_Result Open(unsigned int              speed, 
                    NPT_SerialPortStopBits    stop_bits = NPT_SERIAL_PORT_STOP_BITS_1,
                    NPT_SerialPortFlowControl flow_control = NPT_SERIAL_PORT_FLOW_CONTROL_NONE,
                    NPT_SerialPortParity      parity = NPT_SERIAL_PORT_PARITY_NONE);
    NPT_Result Close();
    NPT_Result GetInputStream(NPT_InputStreamReference& stream);
    NPT_Result GetOutputStream(NPT_OutputStreamReference& stream);

private:
    // members
    NPT_String               m_Name;
    NPT_Win32HandleReference m_HandleReference;
};

/*----------------------------------------------------------------------
|   NPT_Win32SerialPort::NPT_Win32SerialPort
+---------------------------------------------------------------------*/
NPT_Win32SerialPort::NPT_Win32SerialPort(const char* name) :
    m_Name(name)
{
}

/*----------------------------------------------------------------------
|   NPT_Win32SerialPort::~NPT_Win32SerialPort
+---------------------------------------------------------------------*/
NPT_Win32SerialPort::~NPT_Win32SerialPort()
{
    Close();
}

/*----------------------------------------------------------------------
|   NPT_Win32SerialPort::Open
+---------------------------------------------------------------------*/
NPT_Result
NPT_Win32SerialPort::Open(unsigned int              speed, 
                          NPT_SerialPortStopBits    stop_bits,
                          NPT_SerialPortFlowControl flow_control,
                          NPT_SerialPortParity      parity)
{
    return NPT_FAILURE; // We don't need serial port suppurt
#if 0
    // check if we're already open
    if (!m_HandleReference.IsNull()) {
        return NPT_ERROR_SERIAL_PORT_ALREADY_OPEN;
    }

    HANDLE handle = CreateFile(m_Name,  
                               GENERIC_READ | GENERIC_WRITE, 
                               0, 
                               0, 
                               OPEN_EXISTING,
                               0,
                               0);
    if (handle == INVALID_HANDLE_VALUE) {
        return NPT_ERROR_NO_SUCH_SERIAL_PORT;
    }

    // set the parameters
    DCB dcb;
    NPT_SetMemory(&dcb, 0, sizeof(dcb));
    dcb.DCBlength = sizeof(DCB);
    if (!GetCommState(handle, &dcb)) {
        CloseHandle(handle);
        return NPT_FAILURE;
    }
    dcb.fBinary = TRUE;
    dcb.BaudRate = speed;
    switch (stop_bits) {
        case NPT_SERIAL_PORT_STOP_BITS_1: dcb.StopBits   = ONESTOPBIT; break;
        case NPT_SERIAL_PORT_STOP_BITS_1_5: dcb.StopBits = ONE5STOPBITS; break;
        case NPT_SERIAL_PORT_STOP_BITS_2: dcb.StopBits   = TWOSTOPBITS; break;
    }
    switch (flow_control) {
        case NPT_SERIAL_PORT_FLOW_CONTROL_NONE:
            dcb.fOutX = dcb.fOutxCtsFlow = dcb.fOutxDsrFlow = FALSE;
            dcb.fInX = dcb.fDsrSensitivity = FALSE;
            dcb.fRtsControl = RTS_CONTROL_DISABLE;
            dcb.fDtrControl = DTR_CONTROL_DISABLE;
            break;

        case NPT_SERIAL_PORT_FLOW_CONTROL_HARDWARE:
            dcb.fOutX = dcb.fOutxDsrFlow = FALSE;
            dcb.fOutxCtsFlow = TRUE;
            dcb.fInX = dcb.fDsrSensitivity = FALSE;
            dcb.fRtsControl = RTS_CONTROL_HANDSHAKE;
            dcb.fDtrControl = DTR_CONTROL_DISABLE;
            break;

        case NPT_SERIAL_PORT_FLOW_CONTROL_XON_XOFF:
            dcb.fOutX = TRUE;
            dcb.fOutxCtsFlow = dcb.fOutxDsrFlow = FALSE;
            dcb.fInX = TRUE;
            dcb.fDsrSensitivity = FALSE;
            dcb.fRtsControl = RTS_CONTROL_DISABLE;
            dcb.fDtrControl = DTR_CONTROL_DISABLE;
            break;
    }
    switch (parity) {
        case NPT_SERIAL_PORT_PARITY_NONE: dcb.fParity = FALSE; dcb.Parity = NOPARITY; break;
        case NPT_SERIAL_PORT_PARITY_EVEN: dcb.fParity = TRUE;  dcb.Parity = EVENPARITY; break;
        case NPT_SERIAL_PORT_PARITY_ODD: dcb.fParity  = TRUE;  dcb.Parity = ODDPARITY; break;
        case NPT_SERIAL_PORT_PARITY_MARK: dcb.fParity = TRUE;  dcb.Parity = MARKPARITY; break;
    }
    if (!SetCommState(handle, &dcb)) {
        CloseHandle(handle);
        return NPT_FAILURE;
    }

    // create a reference to the FILE object
    m_HandleReference = new NPT_Win32HandleWrapper(handle);

    return NPT_SUCCESS;
#endif
}

/*----------------------------------------------------------------------
|   NPT_Win32SerialPort::Close
+---------------------------------------------------------------------*/
NPT_Result
NPT_Win32SerialPort::Close()
{
    // release the file reference
    m_HandleReference = NULL;

    return NPT_SUCCESS;
}

/*----------------------------------------------------------------------
|   NPT_Win32SerialPort::GetInputStream
+---------------------------------------------------------------------*/
NPT_Result 
NPT_Win32SerialPort::GetInputStream(NPT_InputStreamReference& stream)
{
    // default value
    stream = NULL;

    // check that the file is open
    if (m_HandleReference.IsNull()) return NPT_ERROR_SERIAL_PORT_NOT_OPEN;

    // create a stream
    stream = new NPT_Win32SerialPortInputStream(m_HandleReference);

    return NPT_SUCCESS;
}

/*----------------------------------------------------------------------
|   NPT_Win32SerialPort::GetOutputStream
+---------------------------------------------------------------------*/
NPT_Result 
NPT_Win32SerialPort::GetOutputStream(NPT_OutputStreamReference& stream)
{
    // default value
    stream = NULL;

    // check that the file is open
    if (m_HandleReference.IsNull()) return NPT_ERROR_SERIAL_PORT_NOT_OPEN;

    // create a stream
    stream = new NPT_Win32SerialPortOutputStream(m_HandleReference);

    return NPT_SUCCESS;
}

/*----------------------------------------------------------------------
|   NPT_SerialPort::NPT_SerialPort
+---------------------------------------------------------------------*/
NPT_SerialPort::NPT_SerialPort(const char* name)
{
    m_Delegate = new NPT_Win32SerialPort(name);
}
#endif // ! TARGET_WINDOWS_STORE