cec: show the command as string instead of int when an unexpected command was received.
[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 CCECAdapterMessage::CCECAdapterMessage(const cec_command &command)
44 {
45 clear();
46
47 //set ack polarity to high when transmitting to the broadcast address
48 //set ack polarity low when transmitting to any other address
49 push_back(MSGSTART);
50 push_escaped(MSGCODE_TRANSMIT_ACK_POLARITY);
51 if (command.destination == CECDEVICE_BROADCAST)
52 push_escaped(CEC_TRUE);
53 else
54 push_escaped(CEC_FALSE);
55 push_back(MSGEND);
56
57 // add source and destination
58 push_back(MSGSTART);
59 push_escaped(MSGCODE_TRANSMIT);
60 push_back(((uint8_t)command.initiator << 4) + (uint8_t)command.destination);
61 push_back(MSGEND);
62
63 // add opcode
64 push_back(MSGSTART);
65 push_escaped(command.parameters.empty() ? (uint8_t)MSGCODE_TRANSMIT_EOM : (uint8_t)MSGCODE_TRANSMIT);
66 push_back((uint8_t) command.opcode);
67 push_back(MSGEND);
68
69 // add parameters
70 for (int8_t iPtr = 0; iPtr < command.parameters.size; iPtr++)
71 {
72 push_back(MSGSTART);
73
74 if (iPtr == command.parameters.size - 1)
75 push_escaped( MSGCODE_TRANSMIT_EOM);
76 else
77 push_escaped(MSGCODE_TRANSMIT);
78
79 push_escaped(command.parameters[iPtr]);
80
81 push_back(MSGEND);
82 }
83 }
84
85 CCECAdapterMessage &CCECAdapterMessage::operator =(const CCECAdapterMessage &msg)
86 {
87 packet = msg.packet;
88 state = msg.state;
89 return *this;
90 }
91
92 CStdString CCECAdapterMessage::MessageCodeAsString(void) const
93 {
94 CStdString strMsg;
95 switch (message())
96 {
97 case MSGCODE_NOTHING:
98 strMsg = "NOTHING";
99 break;
100 case MSGCODE_PING:
101 strMsg = "PING";
102 break;
103 case MSGCODE_TIMEOUT_ERROR:
104 strMsg = "TIMEOUT";
105 break;
106 case MSGCODE_HIGH_ERROR:
107 strMsg = "HIGH_ERROR";
108 break;
109 case MSGCODE_LOW_ERROR:
110 strMsg = "LOW_ERROR";
111 break;
112 case MSGCODE_FRAME_START:
113 strMsg = "FRAME_START";
114 break;
115 case MSGCODE_FRAME_DATA:
116 strMsg = "FRAME_DATA";
117 break;
118 case MSGCODE_RECEIVE_FAILED:
119 strMsg = "RECEIVE_FAILED";
120 break;
121 case MSGCODE_COMMAND_ACCEPTED:
122 strMsg = "COMMAND_ACCEPTED";
123 break;
124 case MSGCODE_COMMAND_REJECTED:
125 strMsg = "COMMAND_REJECTED";
126 break;
127 case MSGCODE_SET_ACK_MASK:
128 strMsg = "SET_ACK_MASK";
129 break;
130 case MSGCODE_TRANSMIT:
131 strMsg = "TRANSMIT";
132 break;
133 case MSGCODE_TRANSMIT_EOM:
134 strMsg = "TRANSMIT_EOM";
135 break;
136 case MSGCODE_TRANSMIT_IDLETIME:
137 strMsg = "TRANSMIT_IDLETIME";
138 break;
139 case MSGCODE_TRANSMIT_ACK_POLARITY:
140 strMsg = "TRANSMIT_ACK_POLARITY";
141 break;
142 case MSGCODE_TRANSMIT_LINE_TIMEOUT:
143 strMsg = "TRANSMIT_LINE_TIMEOUT";
144 break;
145 case MSGCODE_TRANSMIT_SUCCEEDED:
146 strMsg = "TRANSMIT_SUCCEEDED";
147 break;
148 case MSGCODE_TRANSMIT_FAILED_LINE:
149 strMsg = "TRANSMIT_FAILED_LINE";
150 break;
151 case MSGCODE_TRANSMIT_FAILED_ACK:
152 strMsg = "TRANSMIT_FAILED_ACK";
153 break;
154 case MSGCODE_TRANSMIT_FAILED_TIMEOUT_DATA:
155 strMsg = "TRANSMIT_FAILED_TIMEOUT_DATA";
156 break;
157 case MSGCODE_TRANSMIT_FAILED_TIMEOUT_LINE:
158 strMsg = "TRANSMIT_FAILED_TIMEOUT_LINE";
159 break;
160 case MSGCODE_FIRMWARE_VERSION:
161 strMsg = "FIRMWARE_VERSION";
162 break;
163 case MSGCODE_START_BOOTLOADER:
164 strMsg = "START_BOOTLOADER";
165 break;
166 case MSGCODE_FRAME_EOM:
167 strMsg = "FRAME_EOM";
168 break;
169 case MSGCODE_FRAME_ACK:
170 strMsg = "FRAME_ACK";
171 break;
172 }
173
174 return strMsg;
175 }
176
177 CStdString CCECAdapterMessage::ToString(void) const
178 {
179 CStdString strMsg;
180 strMsg = MessageCodeAsString();
181
182 switch (message())
183 {
184 case MSGCODE_TIMEOUT_ERROR:
185 case MSGCODE_HIGH_ERROR:
186 case MSGCODE_LOW_ERROR:
187 {
188 int iLine = (size() >= 3) ? (at(1) << 8) | at(2) : 0;
189 uint32_t iTime = (size() >= 7) ? (at(3) << 24) | (at(4) << 16) | (at(5) << 8) | at(6) : 0;
190 strMsg.AppendFormat(" line:%i", iLine);
191 strMsg.AppendFormat(" time:%u", iTime);
192 }
193 break;
194 case MSGCODE_FRAME_START:
195 if (size() >= 2)
196 strMsg.AppendFormat(" initiator:%1x destination:%1x ack:%s %s", initiator(), destination(), ack() ? "high" : "low", eom() ? "eom" : "");
197 break;
198 case MSGCODE_FRAME_DATA:
199 if (size() >= 2)
200 strMsg.AppendFormat(" %02x %s", at(1), eom() ? "eom" : "");
201 break;
202 default:
203 break;
204 }
205
206 return strMsg;
207 }
208
209 bool CCECAdapterMessage::is_error(void) const
210 {
211 cec_adapter_messagecode code = message();
212 return (code == MSGCODE_TIMEOUT_ERROR ||
213 code == MSGCODE_HIGH_ERROR ||
214 code == MSGCODE_LOW_ERROR ||
215 code == MSGCODE_RECEIVE_FAILED ||
216 code == MSGCODE_COMMAND_REJECTED ||
217 code == MSGCODE_TRANSMIT_LINE_TIMEOUT ||
218 code == MSGCODE_TRANSMIT_FAILED_LINE ||
219 code == MSGCODE_TRANSMIT_FAILED_ACK ||
220 code == MSGCODE_TRANSMIT_FAILED_TIMEOUT_DATA ||
221 code == MSGCODE_TRANSMIT_FAILED_TIMEOUT_LINE);
222 }
223
224 void CCECAdapterMessage::push_escaped(int16_t byte)
225 {
226 if (byte >= MSGESC && byte != MSGSTART)
227 {
228 push_back(MSGESC);
229 push_back((uint8_t) (byte - ESCOFFSET));
230 }
231 else
232 push_back((uint8_t) byte);
233 }
234
235 CAdapterCommunication::CAdapterCommunication(CLibCEC *controller) :
236 m_port(NULL),
237 m_controller(controller)
238 {
239 m_port = new CSerialPort;
240 }
241
242 CAdapterCommunication::~CAdapterCommunication(void)
243 {
244 Close();
245
246 if (m_port)
247 {
248 delete m_port;
249 m_port = NULL;
250 }
251 }
252
253 bool CAdapterCommunication::Open(const char *strPort, uint16_t iBaudRate /* = 38400 */, uint32_t iTimeoutMs /* = 10000 */)
254 {
255 CLockObject lock(&m_mutex);
256 if (!m_port)
257 {
258 m_controller->AddLog(CEC_LOG_ERROR, "port is NULL");
259 return false;
260 }
261
262 if (IsOpen())
263 {
264 m_controller->AddLog(CEC_LOG_ERROR, "port is already open");
265 }
266
267 if (!m_port->Open(strPort, iBaudRate))
268 {
269 CStdString strError;
270 strError.Format("error opening serial port '%s': %s", strPort, m_port->GetError().c_str());
271 m_controller->AddLog(CEC_LOG_ERROR, strError);
272 return false;
273 }
274
275 m_controller->AddLog(CEC_LOG_DEBUG, "connection opened");
276
277 //clear any input bytes
278 uint8_t buff[1024];
279 m_port->Read(buff, sizeof(buff), 500);
280
281 if (CreateThread())
282 {
283 m_startCondition.Wait(&m_mutex);
284 m_controller->AddLog(CEC_LOG_DEBUG, "communication thread started");
285 return true;
286 }
287 else
288 {
289 m_controller->AddLog(CEC_LOG_DEBUG, "could not create a communication thread");
290 }
291
292 return false;
293 }
294
295 void CAdapterCommunication::Close(void)
296 {
297 CLockObject lock(&m_mutex);
298 m_startCondition.Broadcast();
299 m_rcvCondition.Broadcast();
300 StopThread();
301 }
302
303 void *CAdapterCommunication::Process(void)
304 {
305 {
306 CLockObject lock(&m_mutex);
307 m_startCondition.Signal();
308 }
309
310 while (!IsStopped())
311 {
312 ReadFromDevice(500);
313 WriteNextCommand();
314 Sleep(5);
315 }
316
317 return NULL;
318 }
319
320 bool CAdapterCommunication::ReadFromDevice(uint32_t iTimeout)
321 {
322 int32_t iBytesRead;
323 uint8_t buff[1024];
324 if (!m_port)
325 return false;
326
327 iBytesRead = m_port->Read(buff, sizeof(buff), iTimeout);
328 if (iBytesRead < 0 || iBytesRead > 256)
329 {
330 CStdString strError;
331 strError.Format("error reading from serial port: %s", m_port->GetError().c_str());
332 m_controller->AddLog(CEC_LOG_ERROR, strError);
333 return false;
334 }
335 else if (iBytesRead > 0)
336 AddData(buff, (uint8_t) iBytesRead);
337
338 return iBytesRead > 0;
339 }
340
341 void CAdapterCommunication::AddData(uint8_t *data, uint8_t iLen)
342 {
343 CLockObject lock(&m_mutex);
344 for (unsigned int iPtr = 0; iPtr < iLen; iPtr++)
345 m_inBuffer.Push(data[iPtr]);
346
347 m_rcvCondition.Signal();
348 }
349
350 void CAdapterCommunication::WriteNextCommand(void)
351 {
352 CCECAdapterMessagePtr msg;
353 if (m_outBuffer.Pop(msg))
354 {
355 CLockObject lock(&msg->mutex);
356 if (m_port->Write(msg) != (int32_t) msg->size())
357 {
358 CStdString strError;
359 strError.Format("error writing to serial port: %s", m_port->GetError().c_str());
360 m_controller->AddLog(CEC_LOG_ERROR, strError);
361 msg->state = ADAPTER_MESSAGE_STATE_ERROR;
362 }
363 else
364 {
365 m_controller->AddLog(CEC_LOG_DEBUG, "command sent");
366 CCondition::Sleep((uint32_t) msg->size() * (uint32_t)24 /*data*/ + (uint32_t)5 /*start bit (4.5 ms)*/);
367 msg->state = ADAPTER_MESSAGE_STATE_SENT;
368 }
369 msg->condition.Signal();
370 }
371 }
372
373 bool CAdapterCommunication::Write(CCECAdapterMessagePtr data)
374 {
375 data->state = ADAPTER_MESSAGE_STATE_WAITING;
376 m_outBuffer.Push(data);
377 return true;
378 }
379
380 bool CAdapterCommunication::Read(CCECAdapterMessage &msg, uint32_t iTimeout)
381 {
382 CLockObject lock(&m_mutex);
383
384 msg.clear();
385 uint64_t iNow = GetTimeMs();
386 uint64_t iTarget = iNow + iTimeout;
387 bool bGotFullMessage(false);
388 bool bNextIsEscaped(false);
389 bool bGotStart(false);
390
391 while(!bGotFullMessage && iNow < iTarget)
392 {
393 uint8_t buf = 0;
394 if (!m_inBuffer.Pop(buf))
395 {
396 if (!m_rcvCondition.Wait(&m_mutex, (uint32_t) (iTarget - iNow)))
397 return false;
398 }
399
400 if (!bGotStart)
401 {
402 if (buf == MSGSTART)
403 bGotStart = true;
404 continue;
405 }
406 else if (buf == MSGSTART) //we found a msgstart before msgend, this is not right, remove
407 {
408 m_controller->AddLog(CEC_LOG_ERROR, "received MSGSTART before MSGEND");
409 msg.clear();
410 bGotStart = true;
411 }
412
413 if (buf == MSGEND)
414 {
415 bGotFullMessage = true;
416 }
417 else if (bNextIsEscaped)
418 {
419 msg.push_back(buf + (uint8_t)ESCOFFSET);
420 bNextIsEscaped = false;
421 }
422 else if (buf == MSGESC)
423 bNextIsEscaped = true;
424 else
425 msg.push_back(buf);
426 }
427
428 if (bGotFullMessage)
429 msg.state = ADAPTER_MESSAGE_STATE_RECEIVED;
430
431 return bGotFullMessage;
432 }
433
434 std::string CAdapterCommunication::GetError(void) const
435 {
436 return m_port->GetError();
437 }
438
439 bool CAdapterCommunication::StartBootloader(void)
440 {
441 if (!IsRunning())
442 return false;
443
444 m_controller->AddLog(CEC_LOG_DEBUG, "starting the bootloader");
445 CCECAdapterMessagePtr output(new CCECAdapterMessage);
446
447 output->push_back(MSGSTART);
448 output->push_escaped(MSGCODE_START_BOOTLOADER);
449 output->push_back(MSGEND);
450
451 if (!Write(output))
452 {
453 m_controller->AddLog(CEC_LOG_ERROR, "could not start the bootloader");
454 return false;
455 }
456 m_controller->AddLog(CEC_LOG_DEBUG, "bootloader start command transmitted");
457 return true;
458 }
459
460 bool CAdapterCommunication::SetAckMask(uint16_t iMask)
461 {
462 if (!IsRunning())
463 return false;
464
465 CStdString strLog;
466 strLog.Format("setting ackmask to %2x", iMask);
467 m_controller->AddLog(CEC_LOG_DEBUG, strLog.c_str());
468
469 CCECAdapterMessagePtr output(new CCECAdapterMessage);
470
471 output->push_back(MSGSTART);
472 output->push_escaped(MSGCODE_SET_ACK_MASK);
473 output->push_escaped(iMask >> 8);
474 output->push_escaped((uint8_t)iMask);
475 output->push_back(MSGEND);
476
477 if (!Write(output))
478 {
479 m_controller->AddLog(CEC_LOG_ERROR, "could not set the ackmask");
480 return false;
481 }
482
483 return true;
484 }
485
486 bool CAdapterCommunication::PingAdapter(void)
487 {
488 if (!IsRunning())
489 return false;
490
491 m_controller->AddLog(CEC_LOG_DEBUG, "sending ping");
492 CCECAdapterMessagePtr output(new CCECAdapterMessage);
493
494 output->push_back(MSGSTART);
495 output->push_escaped(MSGCODE_PING);
496 output->push_back(MSGEND);
497
498 if (!Write(output))
499 {
500 m_controller->AddLog(CEC_LOG_ERROR, "could not send ping command");
501 return false;
502 }
503
504 m_controller->AddLog(CEC_LOG_DEBUG, "ping tranmitted");
505
506 // TODO check for pong
507 return true;
508 }
509
510 bool CAdapterCommunication::IsOpen(void) const
511 {
512 return !IsStopped() && m_port->IsOpen() && IsRunning();
513 }