cec: moved cec_adapter_message to CCECAdapterMessage
[deb_libcec.git] / src / lib / AdapterCommunication.cpp
1 /*
2 * This file is part of the libCEC(R) library.
3 *
4 * libCEC(R) is Copyright (C) 2011 Pulse-Eight Limited. All rights reserved.
5 * libCEC(R) is an original work, containing original code.
6 *
7 * libCEC(R) is a trademark of Pulse-Eight Limited.
8 *
9 * This program is dual-licensed; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22 *
23 *
24 * Alternatively, you can license this library under a commercial license,
25 * please contact Pulse-Eight Licensing for more information.
26 *
27 * For more information contact:
28 * Pulse-Eight Licensing <license@pulse-eight.com>
29 * http://www.pulse-eight.com/
30 * http://www.pulse-eight.net/
31 */
32
33 #include "AdapterCommunication.h"
34
35 #include "LibCEC.h"
36 #include "platform/serialport.h"
37 #include "util/StdString.h"
38 #include "platform/timeutils.h"
39
40 using namespace std;
41 using namespace CEC;
42
43 CAdapterCommunication::CAdapterCommunication(CLibCEC *controller) :
44 m_port(NULL),
45 m_controller(controller)
46 {
47 m_port = new CSerialPort;
48 }
49
50 CAdapterCommunication::~CAdapterCommunication(void)
51 {
52 Close();
53
54 if (m_port)
55 {
56 delete m_port;
57 m_port = NULL;
58 }
59 }
60
61 bool CAdapterCommunication::Open(const char *strPort, uint16_t iBaudRate /* = 38400 */, uint32_t iTimeoutMs /* = 10000 */)
62 {
63 CLockObject lock(&m_commMutex);
64 if (!m_port)
65 {
66 m_controller->AddLog(CEC_LOG_ERROR, "port is NULL");
67 return false;
68 }
69
70 if (IsOpen())
71 {
72 m_controller->AddLog(CEC_LOG_ERROR, "port is already open");
73 }
74
75 if (!m_port->Open(strPort, iBaudRate))
76 {
77 CStdString strError;
78 strError.Format("error opening serial port '%s': %s", strPort, m_port->GetError().c_str());
79 m_controller->AddLog(CEC_LOG_ERROR, strError);
80 return false;
81 }
82
83 m_controller->AddLog(CEC_LOG_DEBUG, "connection opened");
84
85 //clear any input bytes
86 uint8_t buff[1024];
87 m_port->Read(buff, sizeof(buff), 500);
88
89 if (CreateThread())
90 {
91 m_controller->AddLog(CEC_LOG_DEBUG, "communication thread created");
92 return true;
93 }
94 else
95 {
96 m_controller->AddLog(CEC_LOG_DEBUG, "could not create a communication thread");
97 }
98
99 return false;
100 }
101
102 void CAdapterCommunication::Close(void)
103 {
104 CLockObject lock(&m_commMutex);
105 StopThread();
106
107 m_rcvCondition.Broadcast();
108 }
109
110 void *CAdapterCommunication::Process(void)
111 {
112 m_controller->AddLog(CEC_LOG_DEBUG, "communication thread started");
113
114 while (!IsStopped())
115 {
116 ReadFromDevice(100);
117 WriteNextCommand();
118 Sleep(5);
119 }
120
121 return NULL;
122 }
123
124 bool CAdapterCommunication::ReadFromDevice(uint32_t iTimeout)
125 {
126 CLockObject lock(&m_commMutex);
127 int32_t iBytesRead;
128 uint8_t buff[1024];
129 if (!m_port)
130 return false;
131
132 iBytesRead = m_port->Read(buff, sizeof(buff), iTimeout);
133 if (iBytesRead < 0 || iBytesRead > 256)
134 {
135 CStdString strError;
136 strError.Format("error reading from serial port: %s", m_port->GetError().c_str());
137 m_controller->AddLog(CEC_LOG_ERROR, strError);
138 return false;
139 }
140 else if (iBytesRead > 0)
141 AddData(buff, (uint8_t) iBytesRead);
142
143 return iBytesRead > 0;
144 }
145
146 void CAdapterCommunication::AddData(uint8_t *data, uint8_t iLen)
147 {
148 CLockObject lock(&m_bufferMutex);
149 for (unsigned int iPtr = 0; iPtr < iLen; iPtr++)
150 m_inBuffer.Push(data[iPtr]);
151
152 m_rcvCondition.Signal();
153 }
154
155 void CAdapterCommunication::WriteNextCommand(void)
156 {
157 CLockObject lock(&m_commMutex);
158 CCECAdapterMessage msg;
159 if (m_outBuffer.Pop(msg))
160 {
161 if (m_port->Write(msg) != (int32_t) msg.size())
162 {
163 CStdString strError;
164 strError.Format("error writing to serial port: %s", m_port->GetError().c_str());
165 m_controller->AddLog(CEC_LOG_ERROR, strError);
166 }
167 else
168 {
169 m_controller->AddLog(CEC_LOG_DEBUG, "command sent");
170 CCondition::Sleep((uint32_t) msg.size() * (uint32_t)24 /*data*/ + (uint32_t)5 /*start bit (4.5 ms)*/);
171 }
172 }
173 }
174
175 bool CAdapterCommunication::Write(const CCECAdapterMessage &data)
176 {
177 m_outBuffer.Push(data);
178 return true;
179 }
180
181 bool CAdapterCommunication::Read(CCECAdapterMessage &msg, uint32_t iTimeout)
182 {
183 CLockObject lock(&m_bufferMutex);
184
185 msg.clear();
186 uint64_t iNow = GetTimeMs();
187 uint64_t iTarget = iNow + iTimeout;
188 bool bGotFullMessage(false);
189 bool bNextIsEscaped(false);
190 bool bGotStart(false);
191
192 while(!bGotFullMessage && iNow < iTarget)
193 {
194 uint8_t buf = 0;
195 if (!m_inBuffer.Pop(buf))
196 {
197 if (!m_rcvCondition.Wait(&m_bufferMutex, iTarget - iNow))
198 return false;
199 }
200
201 if (!bGotStart)
202 {
203 if (buf == MSGSTART)
204 bGotStart = true;
205 continue;
206 }
207 else if (buf == MSGSTART) //we found a msgstart before msgend, this is not right, remove
208 {
209 m_controller->AddLog(CEC_LOG_ERROR, "received MSGSTART before MSGEND");
210 msg.clear();
211 bGotStart = true;
212 }
213
214 if (buf == MSGEND)
215 {
216 bGotFullMessage = true;
217 }
218 else if (bNextIsEscaped)
219 {
220 msg.push_back(buf + (uint8_t)ESCOFFSET);
221 bNextIsEscaped = false;
222 }
223 else if (buf == MSGESC)
224 bNextIsEscaped = true;
225 else
226 msg.push_back(buf);
227 }
228
229 return bGotFullMessage;
230 }
231
232 std::string CAdapterCommunication::GetError(void) const
233 {
234 return m_port->GetError();
235 }
236
237 bool CAdapterCommunication::StartBootloader(void)
238 {
239 if (!IsRunning())
240 return false;
241
242 m_controller->AddLog(CEC_LOG_DEBUG, "starting the bootloader");
243 CCECAdapterMessage output;
244
245 output.push_back(MSGSTART);
246 output.push_escaped(MSGCODE_START_BOOTLOADER);
247 output.push_back(MSGEND);
248
249 if (!Write(output))
250 {
251 m_controller->AddLog(CEC_LOG_ERROR, "could not start the bootloader");
252 return false;
253 }
254 m_controller->AddLog(CEC_LOG_DEBUG, "bootloader start command transmitted");
255 return true;
256 }
257
258 bool CAdapterCommunication::SetAckMask(uint16_t iMask)
259 {
260 if (!IsRunning())
261 return false;
262
263 CStdString strLog;
264 strLog.Format("setting ackmask to %2x", iMask);
265 m_controller->AddLog(CEC_LOG_DEBUG, strLog.c_str());
266
267 CCECAdapterMessage output;
268
269 output.push_back(MSGSTART);
270 output.push_escaped(MSGCODE_SET_ACK_MASK);
271 output.push_escaped(iMask >> 8);
272 output.push_escaped((uint8_t)iMask);
273 output.push_back(MSGEND);
274
275 if (!Write(output))
276 {
277 m_controller->AddLog(CEC_LOG_ERROR, "could not set the ackmask");
278 return false;
279 }
280
281 return true;
282 }
283
284 bool CAdapterCommunication::PingAdapter(void)
285 {
286 if (!IsRunning())
287 return false;
288
289 m_controller->AddLog(CEC_LOG_DEBUG, "sending ping");
290 CCECAdapterMessage output;
291
292 output.push_back(MSGSTART);
293 output.push_escaped(MSGCODE_PING);
294 output.push_back(MSGEND);
295
296 if (!Write(output))
297 {
298 m_controller->AddLog(CEC_LOG_ERROR, "could not send ping command");
299 return false;
300 }
301
302 m_controller->AddLog(CEC_LOG_DEBUG, "ping tranmitted");
303
304 // TODO check for pong
305 return true;
306 }
307
308 bool CAdapterCommunication::IsOpen(void) const
309 {
310 return !IsStopped() && m_port->IsOpen();
311 }