cec: renamed CCommunication -> CAdapterCommunication
[deb_libcec.git] / src / lib / CECParser.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 "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"
45 #include "AdapterCommunication.h"
46
47 using namespace CEC;
48 using namespace std;
49
50 #define CEC_MAX_RETRY 5
51 #define CEC_BUTTON_TIMEOUT 500
52
53 /*!
54 * ICECDevice implementation
55 */
56 //@{
57 CCECParser::CCECParser(const char *strDeviceName, cec_logical_address iLogicalAddress /* = CECDEVICE_PLAYBACKDEVICE1 */, int iPhysicalAddress /* = CEC_DEFAULT_PHYSICAL_ADDRESS*/) :
58 m_iCurrentButton(CEC_USER_CONTROL_CODE_UNKNOWN),
59 m_physicaladdress(iPhysicalAddress),
60 m_iLogicalAddress(iLogicalAddress),
61 m_strDeviceName(strDeviceName)
62 {
63 m_communication = new CAdapterCommunication(this);
64 }
65
66 CCECParser::~CCECParser(void)
67 {
68 Close();
69 m_communication->Close();
70 delete m_communication;
71 }
72
73 bool CCECParser::Open(const char *strPort, int iTimeoutMs /* = 10000 */)
74 {
75 if (!m_communication)
76 return false;
77
78 if (m_communication->IsOpen())
79 {
80 AddLog(CEC_LOG_ERROR, "connection already open");
81 return false;
82 }
83
84 if (!m_communication->Open(strPort, 38400, iTimeoutMs))
85 {
86 AddLog(CEC_LOG_ERROR, "could not open a connection");
87 return false;
88 }
89
90 if (!SetLogicalAddress(m_iLogicalAddress))
91 {
92 AddLog(CEC_LOG_ERROR, "could not set the logical address");
93 return false;
94 }
95
96 if (CreateThread())
97 return true;
98 else
99 AddLog(CEC_LOG_ERROR, "could not create a processor thread");
100
101 return false;
102 }
103
104 void CCECParser::Close(void)
105 {
106 StopThread();
107 }
108
109 void *CCECParser::Process(void)
110 {
111 AddLog(CEC_LOG_DEBUG, "processor thread started");
112
113 int64_t now = GetTimeMs();
114 while (!m_bStop)
115 {
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();
126
127 now = GetTimeMs();
128 CheckKeypressTimeout(now);
129 CCondition::Sleep(50);
130 }
131
132 AddLog(CEC_LOG_DEBUG, "processor thread terminated");
133 return NULL;
134 }
135
136 bool CCECParser::Ping(void)
137 {
138 if (!IsRunning())
139 return false;
140
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
147 if (!TransmitFormatted(output, false))
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
159 bool CCECParser::StartBootloader(void)
160 {
161 if (!IsRunning())
162 return false;
163
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
170 if (!TransmitFormatted(output, false))
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
180 uint8_t CCECParser::GetSourceDestination(cec_logical_address destination /* = CECDEVICE_BROADCAST */)
181 {
182 return ((uint8_t)m_iLogicalAddress << 4) + (uint8_t)destination;
183 }
184
185 bool CCECParser::PowerOffDevices(cec_logical_address address /* = CECDEVICE_BROADCAST */)
186 {
187 return StandbyDevices(address);
188 }
189
190 bool CCECParser::PowerOnDevices(cec_logical_address address /* = CECDEVICE_TV */)
191 {
192 if (!IsRunning())
193 return false;
194
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
204 bool CCECParser::StandbyDevices(cec_logical_address address /* = CECDEVICE_BROADCAST */)
205 {
206 if (!IsRunning())
207 return false;
208
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
218 bool CCECParser::SetActiveView(void)
219 {
220 if (!IsRunning())
221 return false;
222
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
232 bool CCECParser::SetInactiveView(void)
233 {
234 if (!IsRunning())
235 return false;
236
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
246 bool CCECParser::GetNextLogMessage(cec_log_message *message)
247 {
248 return m_logBuffer.Pop(*message);
249 }
250
251 bool CCECParser::GetNextKeypress(cec_keypress *key)
252 {
253 return IsRunning() ? m_keyBuffer.Pop(*key) : false;
254 }
255
256 bool CCECParser::GetNextCommand(cec_command *command)
257 {
258 return IsRunning() ? m_commandBuffer.Pop(*command) : false;
259 }
260 //@}
261
262 void CCECParser::TransmitAbort(cec_logical_address address, cec_opcode opcode, ECecAbortReason reason /* = CEC_ABORT_REASON_UNRECOGNIZED_OPCODE */)
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
273 void 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
283 void 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
297 void 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
311 void 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
317 void 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
333 void 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
347 void 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
358 bool CCECParser::TransmitFormatted(const cec_frame &data, bool bWaitForAck /* = true */)
359 {
360 CLockObject lock(&m_mutex);
361 if (!m_communication || !m_communication->Write(data))
362 {
363 return false;
364 }
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
376 bool CCECParser::Transmit(const cec_frame &data, bool bWaitForAck /* = true */)
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
417 return TransmitFormatted(output, bWaitForAck);
418 }
419
420 bool CCECParser::WaitForAck(int iTimeout /* = 1000 */)
421 {
422 bool bGotAck(false);
423 bool bError(false);
424
425 int64_t iNow = GetTimeMs();
426 int64_t iTargetTime = iNow + (int64_t) iTimeout;
427
428 while (!bGotAck && !bError && (iTimeout <= 0 || iNow < iTargetTime))
429 {
430 cec_frame msg;
431 while (!bGotAck && !bError && m_communication->Read(msg, iTimeout))
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
481 bool CCECParser::ParseMessage(cec_frame &msg)
482 {
483 bool bReturn(false);
484
485 if (msg.empty())
486 return bReturn;
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)
543 bReturn = true;
544 break;
545 default:
546 break;
547 }
548
549 return bReturn;
550 }
551
552 void 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;
572 cec_opcode opCode = (cec_opcode) m_currentframe[1];
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:
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);
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 }
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 }
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
651 void 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
664 void CCECParser::CheckKeypressTimeout(int64_t now)
665 {
666 if (m_iCurrentButton != CEC_USER_CONTROL_CODE_UNKNOWN && now - m_buttontime > CEC_BUTTON_TIMEOUT)
667 {
668 AddKey();
669 m_iCurrentButton = CEC_USER_CONTROL_CODE_UNKNOWN;
670 }
671 }
672
673 bool CCECParser::SetLogicalAddress(cec_logical_address iLogicalAddress)
674 {
675 CStdString strLog;
676 strLog.Format("setting logical address to %d", iLogicalAddress);
677 AddLog(CEC_LOG_NOTICE, strLog.c_str());
678
679 m_iLogicalAddress = iLogicalAddress;
680 return SetAckMask(0x1 << (uint8_t)m_iLogicalAddress);
681 }
682
683 bool 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
689 cec_frame output;
690
691 output.push_back(MSGSTART);
692 PushEscaped(output, MSGCODE_SET_ACK_MASK);
693 PushEscaped(output, iMask >> 8);
694 PushEscaped(output, (uint8_t)iMask);
695 output.push_back(MSGEND);
696
697 if (!m_communication->Write(output))
698 {
699 AddLog(CEC_LOG_ERROR, "could not set the ackmask");
700 return false;
701 }
702
703 return true;
704 }
705
706 void 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
714 void 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
727 void 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;
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 }
745 }
746
747 int CCECParser::GetMinVersion(void)
748 {
749 return CEC_MIN_VERSION;
750 }
751
752 int CCECParser::GetLibVersion(void)
753 {
754 return CEC_LIB_VERSION;
755 }
756
757 int 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
769 DECLSPEC void * CECCreate(const char *strDeviceName, CEC::cec_logical_address iLogicalAddress /*= CEC::CECDEVICE_PLAYBACKDEVICE1 */, int iPhysicalAddress /* = CEC_DEFAULT_PHYSICAL_ADDRESS */)
770 {
771 return static_cast< void* > (new CCECParser(strDeviceName, iLogicalAddress, iPhysicalAddress));
772 }