]> git.sesse.net Git - casparcg/blob - server/io/SerialPort.h
b8996396df4d362627305c6c113378cf63691111
[casparcg] / server / io / SerialPort.h
1 /*\r
2 * copyright (c) 2010 Sveriges Television AB <info@casparcg.com>\r
3 *\r
4 *  This file is part of CasparCG.\r
5 *\r
6 *    CasparCG is free software: you can redistribute it and/or modify\r
7 *    it under the terms of the GNU General Public License as published by\r
8 *    the Free Software Foundation, either version 3 of the License, or\r
9 *    (at your option) any later version.\r
10 *\r
11 *    CasparCG is distributed in the hope that it will be useful,\r
12 *    but WITHOUT ANY WARRANTY; without even the implied warranty of\r
13 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
14 *    GNU General Public License for more details.\r
15 \r
16 *    You should have received a copy of the GNU General Public License\r
17 *    along with CasparCG.  If not, see <http://www.gnu.org/licenses/>.\r
18 *\r
19 */\r
20  \r
21 #pragma once\r
22 \r
23 #include <string>\r
24 #include <queue>\r
25 #include "..\utils\thread.h"\r
26 #include "..\utils\lockable.h"\r
27 #include "ProtocolStrategy.h"\r
28 #include "..\controller.h"\r
29 #include "..\utils\databuffer.h"\r
30 \r
31 namespace caspar {\r
32 namespace IO {\r
33 \r
34 class SerialPort : public caspar::IController\r
35 {\r
36         class SerialPortClientInfo;\r
37         typedef std::tr1::shared_ptr<SerialPortClientInfo> SerialPortClientInfoPtr;\r
38         friend class SerialPortClientInfo;\r
39 \r
40         class Listener;\r
41         typedef std::tr1::shared_ptr<Listener> ListenerPtr;\r
42         friend class Listener;\r
43 \r
44         class Writer;\r
45         typedef std::tr1::shared_ptr<Writer> WriterPtr;\r
46         friend class Writer;\r
47 \r
48         SerialPort(const SerialPort&);\r
49         SerialPort& operator=(const SerialPort&);\r
50 \r
51 public:\r
52         explicit SerialPort(DWORD baudRate, BYTE parity, BYTE dataBits, BYTE stopBits, const tstring& portName);\r
53         virtual ~SerialPort();\r
54 \r
55         bool Start();\r
56         void Stop();\r
57 \r
58         void SetProtocolStrategy(ProtocolStrategyPtr pPS) {\r
59                 pProtocolStrategy_ = pPS;\r
60         }\r
61 \r
62 private:\r
63         void Setup();\r
64         void Write(const tstring&);\r
65 \r
66         ProtocolStrategyPtr             pProtocolStrategy_;\r
67         tstring portName_;\r
68         HANDLE hPort_;\r
69         DCB commSettings_;\r
70 \r
71         utils::Thread listenerThread_;\r
72         ListenerPtr pListener_;\r
73 \r
74         utils::Thread writerThread_;\r
75         WriterPtr pWriter_;\r
76 \r
77         SerialPortClientInfoPtr pClientInfo_;\r
78 \r
79 // Listener\r
80 /////////////\r
81         class Listener : public utils::IRunnable\r
82         {\r
83                 friend SerialPort;\r
84 \r
85                 Listener(const Listener&);\r
86                 Listener& operator=(const Listener&);\r
87 \r
88         public:\r
89                 explicit Listener(SerialPort* pSerialPort, ProtocolStrategyPtr pPS);\r
90                 virtual ~Listener();\r
91 \r
92                 void Run(HANDLE stopEvent);\r
93                 bool OnUnhandledException(const std::exception& ex) throw();\r
94 \r
95         private:\r
96                 void ProcessStatusEvent(DWORD eventMask);\r
97                 void DoRead();\r
98                 bool OnRead(int numBytesRead);\r
99 \r
100                 static const int WaitTimeout;\r
101                 static const int InputBufferSize;\r
102 \r
103                 SerialPort*                             pPort_;\r
104                 ProtocolStrategyPtr             pProtocolStrategy_;\r
105 \r
106                 utils::Event            overlappedReadEvent_;\r
107                 OVERLAPPED      overlappedRead_;\r
108                 bool            bWaitingOnRead_;\r
109 \r
110                 char*           pInputBuffer_;\r
111                 int                     queuedChars_;\r
112                 utils::DataBuffer<wchar_t> wideRecvBuffer_;\r
113         };\r
114 \r
115 // Writer\r
116 /////////////\r
117         class Writer : public utils::IRunnable, private utils::LockableObject\r
118         {\r
119         public:\r
120                 Writer(HANDLE port);\r
121                 virtual ~Writer() \r
122                 {}\r
123 \r
124         public:\r
125                 void push_back(const tstring&);\r
126 \r
127                 void Run(HANDLE stopEvent);\r
128                 bool OnUnhandledException(const std::exception& ex) throw();\r
129 \r
130         private:\r
131                 utils::Event newStringEvent_;\r
132                 std::queue<tstring> sendQueue_;\r
133                 HANDLE port_;\r
134         };\r
135 \r
136 // ClientInfo\r
137 ///////////////\r
138         class SerialPortClientInfo : public caspar::IO::ClientInfo\r
139         {\r
140                 SerialPort* pSerialPort_;\r
141         public:\r
142                 SerialPortClientInfo(SerialPort* pSP) : pSerialPort_(pSP) {\r
143                 }\r
144                 virtual ~SerialPortClientInfo() \r
145                 {}\r
146 \r
147                 void Send(const tstring& data);\r
148                 void Disconnect() \r
149                 {}\r
150         };\r
151 };\r
152 \r
153 typedef std::tr1::shared_ptr<SerialPort> SerialPortPtr;\r
154 \r
155 }       //namespace IO\r
156 }       //namespace caspar