1 /*!
2  * \file serial/impl/windows.h
3  * \author  William Woodall <wjwwood@gmail.com>
4  * \author  John Harrison <ash@greaterthaninfinity.com>
5  * \version 0.1
6  *
7  * \section LICENSE
8  *
9  * The MIT License
10  *
11  * Copyright (c) 2012 William Woodall, John Harrison
12  *
13  * Permission is hereby granted, free of charge, to any person obtaining a
14  * copy of this software and associated documentation files (the "Software"),
15  * to deal in the Software without restriction, including without limitation
16  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
17  * and/or sell copies of the Software, and to permit persons to whom the
18  * Software is furnished to do so, subject to the following conditions:
19  *
20  * The above copyright notice and this permission notice shall be included in
21  * all copies or substantial portions of the Software.
22  *
23  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
26  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
28  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
29  * DEALINGS IN THE SOFTWARE.
30  *
31  * \section DESCRIPTION
32  *
33  * This provides a windows implementation of the Serial class interface.
34  *
35  */
36 
37 #if defined(_WIN32)
38 
39 #ifndef SERIAL_IMPL_WINDOWS_H
40 #define SERIAL_IMPL_WINDOWS_H
41 
42 #include "serial/serial.h"
43 
44 #include "windows.h"
45 
46 namespace serial
47 {
48 using std::invalid_argument;
49 using std::string;
50 using std::wstring;
51 
52 using serial::IOException;
53 using serial::SerialException;
54 
55 class serial::Serial::SerialImpl
56 {
57 public:
58 	SerialImpl(const string &port,
59 			   unsigned long baudrate,
60 			   bytesize_t bytesize,
61 			   parity_t parity,
62 			   stopbits_t stopbits,
63 			   flowcontrol_t flowcontrol);
64 
65 	virtual ~SerialImpl();
66 
67 	void
68 	open();
69 
70 	void
71 	close();
72 
73 	bool
74 	isOpen() const;
75 
76 	size_t
77 	available();
78 
79 	bool
80 	waitReadable(uint32_t timeout);
81 
82 	void
83 	waitByteTimes(size_t count);
84 
85 	size_t
86 	read(uint8_t *buf, size_t size = 1);
87 
88 	size_t
89 	write(const uint8_t *data, size_t length);
90 
91 	void
92 	flush();
93 
94 	void
95 	flushInput();
96 
97 	void
98 	flushOutput();
99 
100 	void
101 	sendBreak(int duration);
102 
103 	void
104 	setBreak(bool level);
105 
106 	void
107 	setRTS(bool level);
108 
109 	void
110 	setDTR(bool level);
111 
112 	bool
113 	waitForChange();
114 
115 	bool
116 	getCTS();
117 
118 	bool
119 	getDSR();
120 
121 	bool
122 	getRI();
123 
124 	bool
125 	getCD();
126 
127 	void
128 	setPort(const string &port);
129 
130 	string
131 	getPort() const;
132 
133 	void
134 	setTimeout(Timeout &timeout);
135 
136 	Timeout
137 	getTimeout() const;
138 
139 	void
140 	setBaudrate(unsigned long baudrate);
141 
142 	unsigned long
143 	getBaudrate() const;
144 
145 	void
146 	setBytesize(bytesize_t bytesize);
147 
148 	bytesize_t
149 	getBytesize() const;
150 
151 	void
152 	setParity(parity_t parity);
153 
154 	parity_t
155 	getParity() const;
156 
157 	void
158 	setStopbits(stopbits_t stopbits);
159 
160 	stopbits_t
161 	getStopbits() const;
162 
163 	void
164 	setFlowcontrol(flowcontrol_t flowcontrol);
165 
166 	flowcontrol_t
167 	getFlowcontrol() const;
168 
169 	void
170 	readLock();
171 
172 	void
173 	readUnlock();
174 
175 	void
176 	writeLock();
177 
178 	void
179 	writeUnlock();
180 
181 protected:
182 	void reconfigurePort();
183 
184 private:
185 	wstring port_;  // Path to the file descriptor
186 	HANDLE fd_;
187 
188 	bool is_open_;
189 
190 	Timeout timeout_;         // Timeout for read operations
191 	unsigned long baudrate_;  // Baudrate
192 
193 	parity_t parity_;            // Parity
194 	bytesize_t bytesize_;        // Size of the bytes
195 	stopbits_t stopbits_;        // Stop Bits
196 	flowcontrol_t flowcontrol_;  // Flow Control
197 
198 	// Mutex used to lock the read functions
199 	HANDLE read_mutex;
200 	// Mutex used to lock the write functions
201 	HANDLE write_mutex;
202 };
203 
204 }  // namespace serial
205 
206 #endif  // SERIAL_IMPL_WINDOWS_H
207 
208 #endif  // if defined(_WIN32)
209