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