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