cec: renamed CCommunication -> CAdapterCommunication
[deb_libcec.git] / src / lib / CECParser.cpp
CommitLineData
abbca718
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
33#include "CECParser.h"
34
35#include <algorithm>
36#include <cstdio>
37#include <cstdlib>
38#include <cstring>
39#include <sys/stat.h>
40#include "util/StdString.h"
41#include "libPlatform/serialport.h"
42#include "util/threads.h"
43#include "util/timeutils.h"
44#include "CECDetect.h"
828682d3 45#include "AdapterCommunication.h"
abbca718
LOK
46
47using namespace CEC;
48using namespace std;
49
50#define CEC_MAX_RETRY 5
390b42d6 51#define CEC_BUTTON_TIMEOUT 500
abbca718
LOK
52
53/*!
54 * ICECDevice implementation
55 */
56//@{
df7339c6 57CCECParser::CCECParser(const char *strDeviceName, cec_logical_address iLogicalAddress /* = CECDEVICE_PLAYBACKDEVICE1 */, int iPhysicalAddress /* = CEC_DEFAULT_PHYSICAL_ADDRESS*/) :
abbca718 58 m_iCurrentButton(CEC_USER_CONTROL_CODE_UNKNOWN),
df7339c6
LOK
59 m_physicaladdress(iPhysicalAddress),
60 m_iLogicalAddress(iLogicalAddress),
60fa4578 61 m_strDeviceName(strDeviceName)
abbca718 62{
828682d3 63 m_communication = new CAdapterCommunication(this);
abbca718
LOK
64}
65
66CCECParser::~CCECParser(void)
67{
5f39c4d8 68 Close();
a8f0bd18
LOK
69 m_communication->Close();
70 delete m_communication;
abbca718
LOK
71}
72
73bool CCECParser::Open(const char *strPort, int iTimeoutMs /* = 10000 */)
74{
a8f0bd18
LOK
75 if (!m_communication)
76 return false;
abbca718 77
a8f0bd18 78 if (m_communication->IsOpen())
abbca718 79 {
a8f0bd18
LOK
80 AddLog(CEC_LOG_ERROR, "connection already open");
81 return false;
abbca718
LOK
82 }
83
a8f0bd18
LOK
84 if (!m_communication->Open(strPort, 38400, iTimeoutMs))
85 {
86 AddLog(CEC_LOG_ERROR, "could not open a connection");
87 return false;
88 }
abbca718 89
a8f0bd18 90 if (!SetLogicalAddress(m_iLogicalAddress))
abbca718 91 {
a8f0bd18
LOK
92 AddLog(CEC_LOG_ERROR, "could not set the logical address");
93 return false;
abbca718
LOK
94 }
95
60fa4578 96 if (CreateThread())
a8f0bd18 97 return true;
a8f0bd18 98 else
a8f0bd18 99 AddLog(CEC_LOG_ERROR, "could not create a processor thread");
abbca718 100
a8f0bd18 101 return false;
abbca718
LOK
102}
103
5f39c4d8 104void CCECParser::Close(void)
f99bc831 105{
60fa4578 106 StopThread();
f99bc831
LOK
107}
108
60fa4578 109void *CCECParser::Process(void)
abbca718 110{
60fa4578 111 AddLog(CEC_LOG_DEBUG, "processor thread started");
abbca718 112
abbca718 113 int64_t now = GetTimeMs();
60fa4578 114 while (!m_bStop)
abbca718 115 {
60fa4578
LOK
116 bool bParseFrame(false);
117 {
118 CLockObject lock(&m_mutex);
119 cec_frame msg;
120 if (!m_bStop && m_communication->IsOpen() && m_communication->Read(msg, CEC_BUTTON_TIMEOUT))
121 bParseFrame = ParseMessage(msg);
122 }
123
124 if (bParseFrame)
125 ParseCurrentFrame();
abbca718 126
abbca718
LOK
127 now = GetTimeMs();
128 CheckKeypressTimeout(now);
129 CCondition::Sleep(50);
130 }
131
a8f0bd18 132 AddLog(CEC_LOG_DEBUG, "processor thread terminated");
60fa4578 133 return NULL;
abbca718
LOK
134}
135
136bool CCECParser::Ping(void)
137{
60fa4578 138 if (!IsRunning())
f99bc831
LOK
139 return false;
140
abbca718
LOK
141 AddLog(CEC_LOG_DEBUG, "sending ping");
142 cec_frame output;
143 output.push_back(MSGSTART);
144 PushEscaped(output, MSGCODE_PING);
145 output.push_back(MSGEND);
146
390b42d6 147 if (!TransmitFormatted(output, false))
abbca718
LOK
148 {
149 AddLog(CEC_LOG_ERROR, "could not send ping command");
150 return false;
151 }
152
153 AddLog(CEC_LOG_DEBUG, "ping tranmitted");
154
155 // TODO check for pong
156 return true;
157}
158
159bool CCECParser::StartBootloader(void)
160{
60fa4578 161 if (!IsRunning())
f99bc831
LOK
162 return false;
163
abbca718
LOK
164 AddLog(CEC_LOG_DEBUG, "starting the bootloader");
165 cec_frame output;
166 output.push_back(MSGSTART);
167 PushEscaped(output, MSGCODE_START_BOOTLOADER);
168 output.push_back(MSGEND);
169
390b42d6 170 if (!TransmitFormatted(output, false))
abbca718
LOK
171 {
172 AddLog(CEC_LOG_ERROR, "could not start the bootloader");
173 return false;
174 }
175
176 AddLog(CEC_LOG_DEBUG, "bootloader start command transmitted");
177 return true;
178}
179
180uint8_t CCECParser::GetSourceDestination(cec_logical_address destination /* = CECDEVICE_BROADCAST */)
181{
182 return ((uint8_t)m_iLogicalAddress << 4) + (uint8_t)destination;
183}
184
185bool CCECParser::PowerOffDevices(cec_logical_address address /* = CECDEVICE_BROADCAST */)
186{
8148859e 187 return StandbyDevices(address);
abbca718
LOK
188}
189
8148859e 190bool CCECParser::PowerOnDevices(cec_logical_address address /* = CECDEVICE_TV */)
abbca718 191{
60fa4578 192 if (!IsRunning())
f99bc831
LOK
193 return false;
194
abbca718
LOK
195 CStdString strLog;
196 strLog.Format("powering on devices with logical address %d", (int8_t)address);
197 AddLog(CEC_LOG_DEBUG, strLog.c_str());
198 cec_frame frame;
199 frame.push_back(GetSourceDestination(address));
200 frame.push_back(CEC_OPCODE_TEXT_VIEW_ON);
201 return Transmit(frame);
202}
203
204bool CCECParser::StandbyDevices(cec_logical_address address /* = CECDEVICE_BROADCAST */)
205{
60fa4578 206 if (!IsRunning())
f99bc831
LOK
207 return false;
208
abbca718
LOK
209 CStdString strLog;
210 strLog.Format("putting all devices with logical address %d in standby mode", (int8_t)address);
211 AddLog(CEC_LOG_DEBUG, strLog.c_str());
212 cec_frame frame;
213 frame.push_back(GetSourceDestination(address));
214 frame.push_back(CEC_OPCODE_STANDBY);
215 return Transmit(frame);
216}
217
218bool CCECParser::SetActiveView(void)
219{
60fa4578 220 if (!IsRunning())
f99bc831
LOK
221 return false;
222
abbca718
LOK
223 AddLog(CEC_LOG_DEBUG, "setting active view");
224 cec_frame frame;
225 frame.push_back(GetSourceDestination(CECDEVICE_BROADCAST));
226 frame.push_back(CEC_OPCODE_ACTIVE_SOURCE);
227 frame.push_back((m_physicaladdress >> 8) & 0xFF);
228 frame.push_back(m_physicaladdress & 0xFF);
229 return Transmit(frame);
230}
231
232bool CCECParser::SetInactiveView(void)
233{
60fa4578 234 if (!IsRunning())
f99bc831
LOK
235 return false;
236
abbca718
LOK
237 AddLog(CEC_LOG_DEBUG, "setting inactive view");
238 cec_frame frame;
239 frame.push_back(GetSourceDestination(CECDEVICE_BROADCAST));
240 frame.push_back(CEC_OPCODE_INACTIVE_SOURCE);
241 frame.push_back((m_physicaladdress >> 8) & 0xFF);
242 frame.push_back(m_physicaladdress & 0xFF);
243 return Transmit(frame);
244}
245
246bool CCECParser::GetNextLogMessage(cec_log_message *message)
247{
a8f0bd18 248 return m_logBuffer.Pop(*message);
abbca718
LOK
249}
250
251bool CCECParser::GetNextKeypress(cec_keypress *key)
252{
60fa4578 253 return IsRunning() ? m_keyBuffer.Pop(*key) : false;
abbca718 254}
825ddb96
LOK
255
256bool CCECParser::GetNextCommand(cec_command *command)
257{
60fa4578 258 return IsRunning() ? m_commandBuffer.Pop(*command) : false;
825ddb96 259}
abbca718
LOK
260//@}
261
825ddb96 262void CCECParser::TransmitAbort(cec_logical_address address, cec_opcode opcode, ECecAbortReason reason /* = CEC_ABORT_REASON_UNRECOGNIZED_OPCODE */)
abbca718
LOK
263{
264 AddLog(CEC_LOG_DEBUG, "transmitting abort message");
265 cec_frame frame;
266 frame.push_back(GetSourceDestination(address));
267 frame.push_back(CEC_OPCODE_FEATURE_ABORT);
268 frame.push_back(opcode);
269 frame.push_back(reason);
270 Transmit(frame);
271}
272
273void CCECParser::ReportCECVersion(cec_logical_address address /* = CECDEVICE_TV */)
274{
275 cec_frame frame;
276 AddLog(CEC_LOG_NOTICE, "reporting CEC version as 1.3a");
277 frame.push_back(GetSourceDestination(address));
278 frame.push_back(CEC_OPCODE_CEC_VERSION);
279 frame.push_back(CEC_VERSION_1_3A);
280 Transmit(frame);
281}
282
283void CCECParser::ReportPowerState(cec_logical_address address /*= CECDEVICE_TV */, bool bOn /* = true */)
284{
285 cec_frame frame;
286 if (bOn)
287 AddLog(CEC_LOG_NOTICE, "reporting \"On\" power status");
288 else
289 AddLog(CEC_LOG_NOTICE, "reporting \"Off\" power status");
290
291 frame.push_back(GetSourceDestination(address));
292 frame.push_back(CEC_OPCODE_REPORT_POWER_STATUS);
293 frame.push_back(bOn ? CEC_POWER_STATUS_ON : CEC_POWER_STATUS_STANDBY);
294 Transmit(frame);
295}
296
297void CCECParser::ReportMenuState(cec_logical_address address /* = CECDEVICE_TV */, bool bActive /* = true */)
298{
299 cec_frame frame;
300 if (bActive)
301 AddLog(CEC_LOG_NOTICE, "reporting menu state as active");
302 else
303 AddLog(CEC_LOG_NOTICE, "reporting menu state as inactive");
304
305 frame.push_back(GetSourceDestination(address));
306 frame.push_back(CEC_OPCODE_MENU_STATUS);
307 frame.push_back(bActive ? CEC_MENU_STATE_ACTIVATED : CEC_MENU_STATE_DEACTIVATED);
308 Transmit(frame);
309}
310
311void CCECParser::ReportVendorID(cec_logical_address address /* = CECDEVICE_TV */)
312{
313 AddLog(CEC_LOG_NOTICE, "vendor ID requested, feature abort");
314 TransmitAbort(address, CEC_OPCODE_GIVE_DEVICE_VENDOR_ID);
315}
316
317void CCECParser::ReportOSDName(cec_logical_address address /* = CECDEVICE_TV */)
318{
319 cec_frame frame;
320 const char *osdname = m_strDeviceName.c_str();
321 CStdString strLog;
322 strLog.Format("reporting OSD name as %s", osdname);
323 AddLog(CEC_LOG_NOTICE, strLog.c_str());
324 frame.push_back(GetSourceDestination(address));
325 frame.push_back(CEC_OPCODE_SET_OSD_NAME);
326
327 for (unsigned int i = 0; i < strlen(osdname); i++)
328 frame.push_back(osdname[i]);
329
330 Transmit(frame);
331}
332
333void CCECParser::ReportPhysicalAddress(void)
334{
335 cec_frame frame;
336 CStdString strLog;
337 strLog.Format("reporting physical address as %04x", m_physicaladdress);
338 AddLog(CEC_LOG_NOTICE, strLog.c_str());
339 frame.push_back(GetSourceDestination(CECDEVICE_BROADCAST));
340 frame.push_back(CEC_OPCODE_REPORT_PHYSICAL_ADDRESS);
341 frame.push_back((m_physicaladdress >> 8) & 0xFF);
342 frame.push_back(m_physicaladdress & 0xFF);
343 frame.push_back(CEC_DEVICE_TYPE_PLAYBACK_DEVICE);
344 Transmit(frame);
345}
346
347void CCECParser::BroadcastActiveSource(void)
348{
349 cec_frame frame;
350 AddLog(CEC_LOG_NOTICE, "broadcasting active source");
351 frame.push_back(GetSourceDestination(CECDEVICE_BROADCAST));
352 frame.push_back(CEC_OPCODE_ACTIVE_SOURCE);
353 frame.push_back((m_physicaladdress >> 8) & 0xFF);
354 frame.push_back(m_physicaladdress & 0xFF);
355 Transmit(frame);
356}
357
390b42d6 358bool CCECParser::TransmitFormatted(const cec_frame &data, bool bWaitForAck /* = true */)
abbca718 359{
60fa4578 360 CLockObject lock(&m_mutex);
3fcd38dc 361 if (!m_communication || !m_communication->Write(data))
60fa4578 362 {
abbca718 363 return false;
60fa4578 364 }
abbca718
LOK
365
366 CCondition::Sleep((int) data.size() * 24 /*data*/ + 5 /*start bit (4.5 ms)*/ + 50 /* to be on the safe side */);
367 if (bWaitForAck && !WaitForAck())
368 {
369 AddLog(CEC_LOG_DEBUG, "did not receive ACK");
370 return false;
371 }
372
373 return true;
374}
375
390b42d6 376bool CCECParser::Transmit(const cec_frame &data, bool bWaitForAck /* = true */)
abbca718
LOK
377{
378 CStdString txStr = "transmit ";
379 for (unsigned int i = 0; i < data.size(); i++)
380 txStr.AppendFormat(" %02x", data[i]);
381 AddLog(CEC_LOG_DEBUG, txStr.c_str());
382
383 if (data.empty())
384 {
385 AddLog(CEC_LOG_WARNING, "transmit buffer is empty");
386 return false;
387 }
388
389 cec_frame output;
390
391 //set ack polarity to high when transmitting to the broadcast address
392 //set ack polarity low when transmitting to any other address
393 output.push_back(MSGSTART);
394 PushEscaped(output, MSGCODE_TRANSMIT_ACK_POLARITY);
395
396 if ((data[0] & 0xF) == 0xF)
397 PushEscaped(output, CEC_TRUE);
398 else
399 PushEscaped(output, CEC_FALSE);
400
401 output.push_back(MSGEND);
402
403 for (unsigned int i = 0; i < data.size(); i++)
404 {
405 output.push_back(MSGSTART);
406
407 if (i == data.size() - 1)
408 PushEscaped(output, MSGCODE_TRANSMIT_EOM);
409 else
410 PushEscaped(output, MSGCODE_TRANSMIT);
411
412 PushEscaped(output, data[i]);
413
414 output.push_back(MSGEND);
415 }
416
390b42d6 417 return TransmitFormatted(output, bWaitForAck);
abbca718
LOK
418}
419
390b42d6 420bool CCECParser::WaitForAck(int iTimeout /* = 1000 */)
abbca718
LOK
421{
422 bool bGotAck(false);
423 bool bError(false);
424
425 int64_t iNow = GetTimeMs();
390b42d6 426 int64_t iTargetTime = iNow + (int64_t) iTimeout;
abbca718
LOK
427
428 while (!bGotAck && !bError && (iTimeout <= 0 || iNow < iTargetTime))
429 {
abbca718 430 cec_frame msg;
a8f0bd18 431 while (!bGotAck && !bError && m_communication->Read(msg, iTimeout))
abbca718
LOK
432 {
433 uint8_t iCode = msg[0] & ~(MSGCODE_FRAME_EOM | MSGCODE_FRAME_ACK);
434
435 switch (iCode)
436 {
437 case MSGCODE_COMMAND_ACCEPTED:
438 AddLog(CEC_LOG_DEBUG, "MSGCODE_COMMAND_ACCEPTED");
439 break;
440 case MSGCODE_TRANSMIT_SUCCEEDED:
441 AddLog(CEC_LOG_DEBUG, "MSGCODE_TRANSMIT_SUCCEEDED");
442 // TODO
443 bGotAck = true;
444 break;
445 case MSGCODE_RECEIVE_FAILED:
446 AddLog(CEC_LOG_WARNING, "MSGCODE_RECEIVE_FAILED");
447 bError = true;
448 break;
449 case MSGCODE_COMMAND_REJECTED:
450 AddLog(CEC_LOG_WARNING, "MSGCODE_COMMAND_REJECTED");
451 bError = true;
452 break;
453 case MSGCODE_TRANSMIT_FAILED_LINE:
454 AddLog(CEC_LOG_WARNING, "MSGCODE_TRANSMIT_FAILED_LINE");
455 bError = true;
456 break;
457 case MSGCODE_TRANSMIT_FAILED_ACK:
458 AddLog(CEC_LOG_WARNING, "MSGCODE_TRANSMIT_FAILED_ACK");
459 bError = true;
460 break;
461 case MSGCODE_TRANSMIT_FAILED_TIMEOUT_DATA:
462 AddLog(CEC_LOG_WARNING, "MSGCODE_TRANSMIT_FAILED_TIMEOUT_DATA");
463 bError = true;
464 break;
465 case MSGCODE_TRANSMIT_FAILED_TIMEOUT_LINE:
466 AddLog(CEC_LOG_WARNING, "MSGCODE_TRANSMIT_FAILED_TIMEOUT_LINE");
467 bError = true;
468 break;
469 default:
470 m_frameBuffer.Push(msg);
471 bGotAck = (msg[0] & MSGCODE_FRAME_ACK) != 0;
472 break;
473 }
474 iNow = GetTimeMs();
475 }
476 }
477
478 return bGotAck && !bError;
479}
480
60fa4578 481bool CCECParser::ParseMessage(cec_frame &msg)
abbca718 482{
60fa4578
LOK
483 bool bReturn(false);
484
abbca718 485 if (msg.empty())
60fa4578 486 return bReturn;
abbca718
LOK
487
488 CStdString logStr;
489 uint8_t iCode = msg[0] & ~(MSGCODE_FRAME_EOM | MSGCODE_FRAME_ACK);
490 bool bEom = (msg[0] & MSGCODE_FRAME_EOM) != 0;
491 bool bAck = (msg[0] & MSGCODE_FRAME_ACK) != 0;
492
493 switch(iCode)
494 {
495 case MSGCODE_NOTHING:
496 AddLog(CEC_LOG_DEBUG, "MSGCODE_NOTHING");
497 break;
498 case MSGCODE_TIMEOUT_ERROR:
499 case MSGCODE_HIGH_ERROR:
500 case MSGCODE_LOW_ERROR:
501 {
502 if (iCode == MSGCODE_TIMEOUT_ERROR)
503 logStr = "MSGCODE_TIMEOUT";
504 else if (iCode == MSGCODE_HIGH_ERROR)
505 logStr = "MSGCODE_HIGH_ERROR";
506 else
507 logStr = "MSGCODE_LOW_ERROR";
508
509 int iLine = (msg.size() >= 3) ? (msg[1] << 8) | (msg[2]) : 0;
510 uint32_t iTime = (msg.size() >= 7) ? (msg[3] << 24) | (msg[4] << 16) | (msg[5] << 8) | (msg[6]) : 0;
511 logStr.AppendFormat(" line:%i", iLine);
512 logStr.AppendFormat(" time:%u", iTime);
513 AddLog(CEC_LOG_WARNING, logStr.c_str());
514 }
515 break;
516 case MSGCODE_FRAME_START:
517 {
518 logStr = "MSGCODE_FRAME_START";
519 m_currentframe.clear();
520 if (msg.size() >= 2)
521 {
522 int iInitiator = msg[1] >> 4;
523 int iDestination = msg[1] & 0xF;
524 logStr.AppendFormat(" initiator:%u destination:%u ack:%s %s", iInitiator, iDestination, bAck ? "high" : "low", bEom ? "eom" : "");
525
526 m_currentframe.push_back(msg[1]);
527 }
528 AddLog(CEC_LOG_DEBUG, logStr.c_str());
529 }
530 break;
531 case MSGCODE_FRAME_DATA:
532 {
533 logStr = "MSGCODE_FRAME_DATA";
534 if (msg.size() >= 2)
535 {
536 uint8_t iData = msg[1];
537 logStr.AppendFormat(" %02x", iData);
538 m_currentframe.push_back(iData);
539 }
540 AddLog(CEC_LOG_DEBUG, logStr.c_str());
541 }
542 if (bEom)
60fa4578 543 bReturn = true;
abbca718
LOK
544 break;
545 default:
546 break;
547 }
60fa4578
LOK
548
549 return bReturn;
abbca718
LOK
550}
551
552void CCECParser::ParseCurrentFrame(void)
553{
554 uint8_t initiator = m_currentframe[0] >> 4;
555 uint8_t destination = m_currentframe[0] & 0xF;
556
557 CStdString dataStr;
558 dataStr.Format("received frame: initiator: %u destination: %u", initiator, destination);
559
560 if (m_currentframe.size() > 1)
561 {
562 dataStr += " data:";
563 for (unsigned int i = 1; i < m_currentframe.size(); i++)
564 dataStr.AppendFormat(" %02x", m_currentframe[i]);
565 }
566 AddLog(CEC_LOG_DEBUG, dataStr.c_str());
567
568 if (m_currentframe.size() <= 1)
569 return;
570
571 vector<uint8_t> tx;
825ddb96 572 cec_opcode opCode = (cec_opcode) m_currentframe[1];
abbca718
LOK
573 if (destination == (uint16_t) m_iLogicalAddress)
574 {
575 switch(opCode)
576 {
577 case CEC_OPCODE_GIVE_PHYSICAL_ADDRESS:
578 ReportPhysicalAddress();
579 SetActiveView();
580 break;
581 case CEC_OPCODE_GIVE_OSD_NAME:
582 ReportOSDName((cec_logical_address)initiator);
583 break;
584 case CEC_OPCODE_GIVE_DEVICE_VENDOR_ID:
585 ReportVendorID((cec_logical_address)initiator);
586 break;
587 case CEC_OPCODE_MENU_REQUEST:
588 ReportMenuState((cec_logical_address)initiator);
589 break;
590 case CEC_OPCODE_GIVE_DEVICE_POWER_STATUS:
591 ReportPowerState((cec_logical_address)initiator);
592 break;
593 case CEC_OPCODE_GET_CEC_VERSION:
594 ReportCECVersion((cec_logical_address)initiator);
595 break;
596 case CEC_OPCODE_USER_CONTROL_PRESSED:
597 if (m_currentframe.size() > 2)
598 {
599 AddKey();
600
601 if (m_currentframe[2] <= CEC_USER_CONTROL_CODE_MAX)
602 {
603 m_iCurrentButton = (cec_user_control_code) m_currentframe[2];
604 m_buttontime = GetTimeMs();
605 }
606 }
607 break;
608 case CEC_OPCODE_USER_CONTROL_RELEASE:
609 AddKey();
610 break;
611 default:
825ddb96
LOK
612 cec_frame params = m_currentframe;
613 params.erase(params.begin(), params.begin() + 2);
614 AddCommand((cec_logical_address) initiator, (cec_logical_address) destination, opCode, &params);
abbca718
LOK
615 break;
616 }
617 }
618 else if (destination == (uint8_t) CECDEVICE_BROADCAST)
619 {
620 if (opCode == CEC_OPCODE_REQUEST_ACTIVE_SOURCE)
621 {
622 BroadcastActiveSource();
623 }
624 else if (opCode == CEC_OPCODE_SET_STREAM_PATH)
625 {
626 if (m_currentframe.size() >= 4)
627 {
628 int streamaddr = ((int)m_currentframe[2] << 8) | ((int)m_currentframe[3]);
629 CStdString strLog;
630 strLog.Format("%i requests stream path from physical address %04x", initiator, streamaddr);
631 AddLog(CEC_LOG_DEBUG, strLog.c_str());
632 if (streamaddr == m_physicaladdress)
633 BroadcastActiveSource();
634 }
635 }
c49c485b
LOK
636 else
637 {
638 cec_frame params = m_currentframe;
639 params.erase(params.begin(), params.begin() + 2);
640 AddCommand((cec_logical_address) initiator, (cec_logical_address) destination, opCode, &params);
641 }
abbca718
LOK
642 }
643 else
644 {
645 CStdString strLog;
646 strLog.Format("ignoring frame: destination: %u != %u", destination, (uint16_t)m_iLogicalAddress);
647 AddLog(CEC_LOG_DEBUG, strLog.c_str());
648 }
649}
650
abbca718
LOK
651void CCECParser::PushEscaped(cec_frame &vec, uint8_t byte)
652{
653 if (byte >= MSGESC && byte != MSGSTART)
654 {
655 vec.push_back(MSGESC);
656 vec.push_back(byte - ESCOFFSET);
657 }
658 else
659 {
660 vec.push_back(byte);
661 }
662}
663
664void CCECParser::CheckKeypressTimeout(int64_t now)
665{
390b42d6 666 if (m_iCurrentButton != CEC_USER_CONTROL_CODE_UNKNOWN && now - m_buttontime > CEC_BUTTON_TIMEOUT)
abbca718
LOK
667 {
668 AddKey();
669 m_iCurrentButton = CEC_USER_CONTROL_CODE_UNKNOWN;
670 }
671}
672
6dfe9213 673bool CCECParser::SetLogicalAddress(cec_logical_address iLogicalAddress)
abbca718
LOK
674{
675 CStdString strLog;
6dfe9213 676 strLog.Format("setting logical address to %d", iLogicalAddress);
abbca718
LOK
677 AddLog(CEC_LOG_NOTICE, strLog.c_str());
678
6dfe9213
LOK
679 m_iLogicalAddress = iLogicalAddress;
680 return SetAckMask(0x1 << (uint8_t)m_iLogicalAddress);
681}
682
683bool CCECParser::SetAckMask(uint16_t iMask)
684{
685 CStdString strLog;
686 strLog.Format("setting ackmask to %2x", iMask);
687 AddLog(CEC_LOG_DEBUG, strLog.c_str());
688
abbca718 689 cec_frame output;
abbca718 690
6dfe9213 691 output.push_back(MSGSTART);
abbca718 692 PushEscaped(output, MSGCODE_SET_ACK_MASK);
6dfe9213 693 PushEscaped(output, iMask >> 8);
bcd03b37 694 PushEscaped(output, (uint8_t)iMask);
abbca718
LOK
695 output.push_back(MSGEND);
696
3fcd38dc 697 if (!m_communication->Write(output))
abbca718 698 {
a8f0bd18 699 AddLog(CEC_LOG_ERROR, "could not set the ackmask");
abbca718
LOK
700 return false;
701 }
702
703 return true;
704}
705
706void CCECParser::AddLog(cec_log_level level, const string &strMessage)
707{
708 cec_log_message message;
709 message.level = level;
710 message.message.assign(strMessage.c_str());
711 m_logBuffer.Push(message);
712}
713
714void CCECParser::AddKey(void)
715{
716 if (m_iCurrentButton != CEC_USER_CONTROL_CODE_UNKNOWN)
717 {
718 cec_keypress key;
719 key.duration = (unsigned int) (GetTimeMs() - m_buttontime);
720 key.keycode = m_iCurrentButton;
721 m_keyBuffer.Push(key);
722 m_iCurrentButton = CEC_USER_CONTROL_CODE_UNKNOWN;
723 m_buttontime = 0;
724 }
725}
726
825ddb96
LOK
727void CCECParser::AddCommand(cec_logical_address source, cec_logical_address destination, cec_opcode opcode, cec_frame *parameters)
728{
729 cec_command command;
730 command.source = source;
731 command.destination = destination;
732 command.opcode = opcode;
733 if (parameters)
734 command.parameters = *parameters;
c49c485b
LOK
735 if (m_commandBuffer.Push(command))
736 {
737 CStdString strDebug;
738 strDebug.Format("stored command '%d' in the command buffer. buffer size = %d", opcode, m_commandBuffer.Size());
739 AddLog(CEC_LOG_DEBUG, strDebug);
740 }
741 else
742 {
743 AddLog(CEC_LOG_WARNING, "command buffer is full");
744 }
825ddb96
LOK
745}
746
abbca718
LOK
747int CCECParser::GetMinVersion(void)
748{
749 return CEC_MIN_VERSION;
750}
751
752int CCECParser::GetLibVersion(void)
753{
754 return CEC_LIB_VERSION;
755}
756
757int CCECParser::FindDevices(std::vector<cec_device> &deviceList, const char *strDevicePath /* = NULL */)
758{
759 CStdString strDebug;
760 if (strDevicePath)
761 strDebug.Format("trying to autodetect the com port for device path '%s'", strDevicePath);
762 else
763 strDebug.Format("trying to autodetect all CEC adapters");
764 AddLog(CEC_LOG_DEBUG, strDebug);
765
766 return CCECDetect::FindDevices(deviceList, strDevicePath);
767}
768
bcd03b37 769DECLSPEC void * CECCreate(const char *strDeviceName, CEC::cec_logical_address iLogicalAddress /*= CEC::CECDEVICE_PLAYBACKDEVICE1 */, int iPhysicalAddress /* = CEC_DEFAULT_PHYSICAL_ADDRESS */)
abbca718 770{
df7339c6 771 return static_cast< void* > (new CCECParser(strDeviceName, iLogicalAddress, iPhysicalAddress));
abbca718 772}