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