cec: don't respond with a poll from the broadcast address when receiving in CSLComman...
[deb_libcec.git] / src / lib / implementations / CECCommandHandler.cpp
... / ...
CommitLineData
1/*
2 * This file is part of the libCEC(R) library.
3 *
4 * libCEC(R) is Copyright (C) 2011-2012 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 "CECCommandHandler.h"
34#include "../devices/CECBusDevice.h"
35#include "../devices/CECAudioSystem.h"
36#include "../devices/CECPlaybackDevice.h"
37#include "../CECClient.h"
38#include "../CECProcessor.h"
39#include "../LibCEC.h"
40#include "../CECTypeUtils.h"
41#include "../platform/util/util.h"
42
43using namespace CEC;
44using namespace std;
45using namespace PLATFORM;
46
47#define LIB_CEC m_busDevice->GetProcessor()->GetLib()
48#define ToString(p) CCECTypeUtils::ToString(p)
49
50CCECCommandHandler::CCECCommandHandler(CCECBusDevice *busDevice) :
51 m_busDevice(busDevice),
52 m_processor(m_busDevice->GetProcessor()),
53 m_iTransmitTimeout(CEC_DEFAULT_TRANSMIT_TIMEOUT),
54 m_iTransmitWait(CEC_DEFAULT_TRANSMIT_WAIT),
55 m_iTransmitRetries(CEC_DEFAULT_TRANSMIT_RETRIES),
56 m_bHandlerInited(false),
57 m_bOPTSendDeckStatusUpdateOnActiveSource(false),
58 m_vendorId(CEC_VENDOR_UNKNOWN),
59 m_waitForResponse(new CWaitForResponse)
60{
61}
62
63CCECCommandHandler::~CCECCommandHandler(void)
64{
65 DELETE_AND_NULL(m_waitForResponse);
66}
67
68bool CCECCommandHandler::HandleCommand(const cec_command &command)
69{
70 if (command.opcode_set == 0)
71 return HandlePoll(command);
72
73 bool bHandled(true);
74
75 CCECClient *client = m_busDevice->GetClient();
76 if (client)
77 client->AddCommand(command);
78
79 switch(command.opcode)
80 {
81 case CEC_OPCODE_REPORT_POWER_STATUS:
82 HandleReportPowerStatus(command);
83 break;
84 case CEC_OPCODE_CEC_VERSION:
85 HandleDeviceCecVersion(command);
86 break;
87 case CEC_OPCODE_SET_MENU_LANGUAGE:
88 HandleSetMenuLanguage(command);
89 break;
90 case CEC_OPCODE_GIVE_PHYSICAL_ADDRESS:
91 if (m_processor->CECInitialised())
92 HandleGivePhysicalAddress(command);
93 break;
94 case CEC_OPCODE_GET_MENU_LANGUAGE:
95 if (m_processor->CECInitialised())
96 HandleGiveMenuLanguage(command);
97 break;
98 case CEC_OPCODE_GIVE_OSD_NAME:
99 if (m_processor->CECInitialised())
100 HandleGiveOSDName(command);
101 break;
102 case CEC_OPCODE_GIVE_DEVICE_VENDOR_ID:
103 if (m_processor->CECInitialised())
104 HandleGiveDeviceVendorId(command);
105 break;
106 case CEC_OPCODE_DEVICE_VENDOR_ID:
107 HandleDeviceVendorId(command);
108 break;
109 case CEC_OPCODE_VENDOR_COMMAND_WITH_ID:
110 HandleDeviceVendorCommandWithId(command);
111 break;
112 case CEC_OPCODE_GIVE_DECK_STATUS:
113 if (m_processor->CECInitialised())
114 HandleGiveDeckStatus(command);
115 break;
116 case CEC_OPCODE_DECK_CONTROL:
117 HandleDeckControl(command);
118 break;
119 case CEC_OPCODE_MENU_REQUEST:
120 if (m_processor->CECInitialised())
121 HandleMenuRequest(command);
122 break;
123 case CEC_OPCODE_GIVE_DEVICE_POWER_STATUS:
124 if (m_processor->CECInitialised())
125 HandleGiveDevicePowerStatus(command);
126 break;
127 case CEC_OPCODE_GET_CEC_VERSION:
128 if (m_processor->CECInitialised())
129 HandleGetCecVersion(command);
130 break;
131 case CEC_OPCODE_USER_CONTROL_PRESSED:
132 if (m_processor->CECInitialised())
133 HandleUserControlPressed(command);
134 break;
135 case CEC_OPCODE_USER_CONTROL_RELEASE:
136 if (m_processor->CECInitialised())
137 HandleUserControlRelease(command);
138 break;
139 case CEC_OPCODE_GIVE_AUDIO_STATUS:
140 if (m_processor->CECInitialised())
141 HandleGiveAudioStatus(command);
142 break;
143 case CEC_OPCODE_GIVE_SYSTEM_AUDIO_MODE_STATUS:
144 if (m_processor->CECInitialised())
145 HandleGiveSystemAudioModeStatus(command);
146 break;
147 case CEC_OPCODE_SYSTEM_AUDIO_MODE_REQUEST:
148 if (m_processor->CECInitialised())
149 HandleSystemAudioModeRequest(command);
150 break;
151 case CEC_OPCODE_REPORT_AUDIO_STATUS:
152 HandleReportAudioStatus(command);
153 break;
154 case CEC_OPCODE_SYSTEM_AUDIO_MODE_STATUS:
155 HandleSystemAudioModeStatus(command);
156 break;
157 case CEC_OPCODE_SET_SYSTEM_AUDIO_MODE:
158 HandleSetSystemAudioMode(command);
159 break;
160 case CEC_OPCODE_REQUEST_ACTIVE_SOURCE:
161 if (m_processor->CECInitialised())
162 HandleRequestActiveSource(command);
163 break;
164 case CEC_OPCODE_SET_STREAM_PATH:
165 HandleSetStreamPath(command);
166 break;
167 case CEC_OPCODE_ROUTING_CHANGE:
168 HandleRoutingChange(command);
169 break;
170 case CEC_OPCODE_ROUTING_INFORMATION:
171 HandleRoutingInformation(command);
172 break;
173 case CEC_OPCODE_STANDBY:
174 if (m_processor->CECInitialised())
175 HandleStandby(command);
176 break;
177 case CEC_OPCODE_ACTIVE_SOURCE:
178 HandleActiveSource(command);
179 break;
180 case CEC_OPCODE_REPORT_PHYSICAL_ADDRESS:
181 HandleReportPhysicalAddress(command);
182 break;
183 case CEC_OPCODE_SET_OSD_NAME:
184 HandleSetOSDName(command);
185 break;
186 case CEC_OPCODE_IMAGE_VIEW_ON:
187 HandleImageViewOn(command);
188 break;
189 case CEC_OPCODE_TEXT_VIEW_ON:
190 HandleTextViewOn(command);
191 break;
192 case CEC_OPCODE_FEATURE_ABORT:
193 HandleFeatureAbort(command);
194 break;
195 case CEC_OPCODE_VENDOR_COMMAND:
196 HandleVendorCommand(command);
197 break;
198 default:
199 bHandled = false;
200 break;
201 }
202
203 if (bHandled)
204 m_waitForResponse->Received((command.opcode == CEC_OPCODE_FEATURE_ABORT && command.parameters.size > 0) ? (cec_opcode)command.parameters[0] : command.opcode);
205 else
206 UnhandledCommand(command);
207
208 return bHandled;
209}
210
211bool CCECCommandHandler::HandleActiveSource(const cec_command &command)
212{
213 if (command.parameters.size == 2)
214 {
215 uint16_t iAddress = ((uint16_t)command.parameters[0] << 8) | ((uint16_t)command.parameters[1]);
216 CCECBusDevice *device = m_processor->GetDeviceByPhysicalAddress(iAddress);
217 if (device)
218 device->MarkAsActiveSource();
219 }
220
221 return true;
222}
223
224bool CCECCommandHandler::HandleDeckControl(const cec_command &command)
225{
226 CCECPlaybackDevice *device = CCECBusDevice::AsPlaybackDevice(GetDevice(command.destination));
227 if (device && command.parameters.size > 0)
228 {
229 device->SetDeckControlMode((cec_deck_control_mode) command.parameters[0]);
230 return true;
231 }
232
233 return false;
234}
235
236bool CCECCommandHandler::HandleDeviceCecVersion(const cec_command &command)
237{
238 if (command.parameters.size == 1)
239 {
240 CCECBusDevice *device = GetDevice(command.initiator);
241 if (device)
242 device->SetCecVersion((cec_version) command.parameters[0]);
243 }
244
245 return true;
246}
247
248bool CCECCommandHandler::HandleDeviceVendorCommandWithId(const cec_command &command)
249{
250 if (m_processor->CECInitialised() && m_processor->IsHandledByLibCEC(command.destination))
251 m_processor->TransmitAbort(command.destination, command.initiator, command.opcode, CEC_ABORT_REASON_REFUSED);
252
253 return true;
254}
255
256bool CCECCommandHandler::HandleDeviceVendorId(const cec_command &command)
257{
258 return SetVendorId(command);
259}
260
261bool CCECCommandHandler::HandleFeatureAbort(const cec_command &command)
262{
263 if (command.parameters.size == 2 &&
264 (command.parameters[1] == CEC_ABORT_REASON_UNRECOGNIZED_OPCODE ||
265 command.parameters[1] == CEC_ABORT_REASON_REFUSED))
266 m_processor->GetDevice(command.initiator)->SetUnsupportedFeature((cec_opcode)command.parameters[0]);
267 return true;
268}
269
270bool CCECCommandHandler::HandleGetCecVersion(const cec_command &command)
271{
272 if (m_processor->CECInitialised() && m_processor->IsHandledByLibCEC(command.destination))
273 {
274 CCECBusDevice *device = GetDevice(command.destination);
275 if (device)
276 return device->TransmitCECVersion(command.initiator);
277 }
278
279 return false;
280}
281
282bool CCECCommandHandler::HandleGiveAudioStatus(const cec_command &command)
283{
284 if (m_processor->CECInitialised() && m_processor->IsHandledByLibCEC(command.destination))
285 {
286 CCECAudioSystem *device = CCECBusDevice::AsAudioSystem(GetDevice(command.destination));
287 if (device)
288 return device->TransmitAudioStatus(command.initiator);
289 }
290
291 return false;
292}
293
294bool CCECCommandHandler::HandleGiveDeckStatus(const cec_command &command)
295{
296 if (m_processor->CECInitialised() && m_processor->IsHandledByLibCEC(command.destination))
297 {
298 CCECPlaybackDevice *device = CCECBusDevice::AsPlaybackDevice(GetDevice(command.destination));
299 if (device)
300 return device->TransmitDeckStatus(command.initiator);
301 }
302
303 return false;
304}
305
306bool CCECCommandHandler::HandleGiveDevicePowerStatus(const cec_command &command)
307{
308 if (m_processor->CECInitialised() && m_processor->IsHandledByLibCEC(command.destination))
309 {
310 CCECBusDevice *device = GetDevice(command.destination);
311 if (device)
312 return device->TransmitPowerState(command.initiator);
313 }
314
315 return false;
316}
317
318bool CCECCommandHandler::HandleGiveDeviceVendorId(const cec_command &command)
319{
320 if (m_processor->CECInitialised() && m_processor->IsHandledByLibCEC(command.destination))
321 {
322 CCECBusDevice *device = GetDevice(command.destination);
323 if (device)
324 return device->TransmitVendorID(command.initiator);
325 }
326
327 return false;
328}
329
330bool CCECCommandHandler::HandleGiveOSDName(const cec_command &command)
331{
332 if (m_processor->CECInitialised() && m_processor->IsHandledByLibCEC(command.destination))
333 {
334 CCECBusDevice *device = GetDevice(command.destination);
335 if (device)
336 return device->TransmitOSDName(command.initiator);
337 }
338
339 return false;
340}
341
342bool CCECCommandHandler::HandleGivePhysicalAddress(const cec_command &command)
343{
344 if (m_processor->CECInitialised() && m_processor->IsHandledByLibCEC(command.destination))
345 {
346 CCECBusDevice *device = GetDevice(command.destination);
347 if (device)
348 return device->TransmitPhysicalAddress();
349 }
350
351 return false;
352}
353
354bool CCECCommandHandler::HandleGiveMenuLanguage(const cec_command &command)
355{
356 if (m_processor->CECInitialised() && m_processor->IsHandledByLibCEC(command.destination))
357 {
358 CCECBusDevice *device = GetDevice(command.destination);
359 if (device)
360 return device->TransmitSetMenuLanguage(command.initiator);
361 }
362
363 return false;
364}
365
366bool CCECCommandHandler::HandleGiveSystemAudioModeStatus(const cec_command &command)
367{
368 if (m_processor->CECInitialised() && m_processor->IsHandledByLibCEC(command.destination))
369 {
370 CCECAudioSystem *device = CCECBusDevice::AsAudioSystem(GetDevice(command.destination));
371 if (device)
372 return device->TransmitSystemAudioModeStatus(command.initiator);
373 }
374
375 return false;
376}
377
378bool CCECCommandHandler::HandleImageViewOn(const cec_command &command)
379{
380 m_processor->GetDevice(command.initiator)->MarkAsActiveSource();
381 return true;
382}
383
384bool CCECCommandHandler::HandleMenuRequest(const cec_command &command)
385{
386 if (m_processor->CECInitialised() && m_processor->IsHandledByLibCEC(command.destination))
387 {
388 CCECBusDevice *device = GetDevice(command.destination);
389 if (device)
390 {
391 CCECClient *client = device->GetClient();
392 if (client)
393 {
394 if (command.parameters[0] == CEC_MENU_REQUEST_TYPE_ACTIVATE)
395 {
396 if (client->MenuStateChanged(CEC_MENU_STATE_ACTIVATED) == 1)
397 device->SetMenuState(CEC_MENU_STATE_ACTIVATED);
398 }
399 else if (command.parameters[0] == CEC_MENU_REQUEST_TYPE_DEACTIVATE)
400 {
401 if (client->MenuStateChanged(CEC_MENU_STATE_DEACTIVATED) == 1)
402 device->SetMenuState(CEC_MENU_STATE_DEACTIVATED);
403 }
404 }
405 return device->TransmitMenuState(command.initiator);
406 }
407 }
408
409 return false;
410}
411
412bool CCECCommandHandler::HandlePoll(const cec_command &command)
413{
414 m_busDevice->HandlePoll(command.destination);
415 return true;
416}
417
418bool CCECCommandHandler::HandleReportAudioStatus(const cec_command &command)
419{
420 if (command.parameters.size == 1)
421 {
422 CCECAudioSystem *device = CCECBusDevice::AsAudioSystem(GetDevice(command.initiator));
423 if (device)
424 {
425 device->SetAudioStatus(command.parameters[0]);
426 return true;
427 }
428 }
429 return false;
430}
431
432bool CCECCommandHandler::HandleReportPhysicalAddress(const cec_command &command)
433{
434 if (command.parameters.size == 3)
435 {
436 uint16_t iNewAddress = ((uint16_t)command.parameters[0] << 8) | ((uint16_t)command.parameters[1]);
437 SetPhysicalAddress(command.initiator, iNewAddress);
438 }
439 return true;
440}
441
442bool CCECCommandHandler::HandleReportPowerStatus(const cec_command &command)
443{
444 if (command.parameters.size == 1)
445 {
446 CCECBusDevice *device = GetDevice(command.initiator);
447 if (device)
448 device->SetPowerStatus((cec_power_status) command.parameters[0]);
449 }
450 return true;
451}
452
453bool CCECCommandHandler::HandleRequestActiveSource(const cec_command &command)
454{
455 if (m_processor->CECInitialised())
456 {
457 LIB_CEC->AddLog(CEC_LOG_DEBUG, ">> %i requests active source", (uint8_t) command.initiator);
458 m_processor->GetDevice(command.initiator)->SetPowerStatus(CEC_POWER_STATUS_ON);
459
460 vector<CCECBusDevice *> devices;
461 for (size_t iDevicePtr = 0; iDevicePtr < GetMyDevices(devices); iDevicePtr++)
462 devices[iDevicePtr]->TransmitActiveSource();
463
464 return true;
465 }
466 return false;
467}
468
469bool CCECCommandHandler::HandleRoutingChange(const cec_command &command)
470{
471 if (command.parameters.size == 4)
472 {
473 uint16_t iOldAddress = ((uint16_t)command.parameters[0] << 8) | ((uint16_t)command.parameters[1]);
474 uint16_t iNewAddress = ((uint16_t)command.parameters[2] << 8) | ((uint16_t)command.parameters[3]);
475
476 CCECBusDevice *device = GetDevice(command.initiator);
477 if (device)
478 device->SetStreamPath(iNewAddress, iOldAddress);
479 }
480 return true;
481}
482
483bool CCECCommandHandler::HandleRoutingInformation(const cec_command &command)
484{
485 if (command.parameters.size == 2)
486 {
487 uint16_t iNewAddress = ((uint16_t)command.parameters[0] << 8) | ((uint16_t)command.parameters[1]);
488 CCECBusDevice *device = m_processor->GetDeviceByPhysicalAddress(iNewAddress);
489 if (device)
490 device->MarkAsActiveSource();
491 }
492
493 return false;
494}
495
496bool CCECCommandHandler::HandleSetMenuLanguage(const cec_command &command)
497{
498 if (command.parameters.size == 3)
499 {
500 CCECBusDevice *device = GetDevice(command.initiator);
501 if (device)
502 {
503 cec_menu_language language;
504 language.device = command.initiator;
505 for (uint8_t iPtr = 0; iPtr < 4; iPtr++)
506 language.language[iPtr] = command.parameters[iPtr];
507 language.language[3] = 0;
508 device->SetMenuLanguage(language);
509 return true;
510 }
511 }
512 return false;
513}
514
515bool CCECCommandHandler::HandleSetOSDName(const cec_command &command)
516{
517 if (command.parameters.size > 0)
518 {
519 CCECBusDevice *device = GetDevice(command.initiator);
520 if (device)
521 {
522 char buf[1024];
523 for (uint8_t iPtr = 0; iPtr < command.parameters.size; iPtr++)
524 buf[iPtr] = (char)command.parameters[iPtr];
525 buf[command.parameters.size] = 0;
526
527 CStdString strName(buf);
528 device->SetOSDName(strName);
529
530 return true;
531 }
532 }
533 return false;
534}
535
536bool CCECCommandHandler::HandleSetStreamPath(const cec_command &command)
537{
538 if (m_processor->CECInitialised() && command.parameters.size >= 2)
539 {
540 uint16_t iStreamAddress = ((uint16_t)command.parameters[0] << 8) | ((uint16_t)command.parameters[1]);
541 LIB_CEC->AddLog(CEC_LOG_DEBUG, ">> %i sets stream path to physical address %04x", command.initiator, iStreamAddress);
542
543 /* one of the device handled by libCEC has been made active */
544 CCECBusDevice *device = GetDeviceByPhysicalAddress(iStreamAddress);
545 if (device && device->IsHandledByLibCEC())
546 device->ActivateSource();
547 }
548 return false;
549}
550
551bool CCECCommandHandler::HandleSystemAudioModeRequest(const cec_command &command)
552{
553 if (m_processor->CECInitialised() && m_processor->IsHandledByLibCEC(command.destination))
554 {
555 CCECAudioSystem *device = CCECBusDevice::AsAudioSystem(GetDevice(command.destination));
556 if (device)
557 {
558 if (command.parameters.size >= 2)
559 {
560 device->SetPowerStatus(CEC_POWER_STATUS_ON);
561 device->SetSystemAudioModeStatus(CEC_SYSTEM_AUDIO_STATUS_ON);
562 uint16_t iNewAddress = ((uint16_t)command.parameters[0] << 8) | ((uint16_t)command.parameters[1]);
563 CCECBusDevice *newActiveDevice = GetDeviceByPhysicalAddress(iNewAddress);
564 if (newActiveDevice)
565 newActiveDevice->MarkAsActiveSource();
566 return device->TransmitSetSystemAudioMode(command.initiator);
567 }
568 else
569 {
570 device->SetSystemAudioModeStatus(CEC_SYSTEM_AUDIO_STATUS_OFF);
571 return device->TransmitSetSystemAudioMode(command.initiator);
572 }
573 }
574 }
575 return false;
576}
577
578bool CCECCommandHandler::HandleStandby(const cec_command &command)
579{
580 CCECBusDevice *device = GetDevice(command.initiator);
581 if (device)
582 device->SetPowerStatus(CEC_POWER_STATUS_STANDBY);
583
584 return true;
585}
586
587bool CCECCommandHandler::HandleSystemAudioModeStatus(const cec_command &command)
588{
589 if (command.parameters.size == 1)
590 {
591 CCECAudioSystem *device = CCECBusDevice::AsAudioSystem(GetDevice(command.initiator));
592 if (device)
593 {
594 device->SetSystemAudioModeStatus((cec_system_audio_status)command.parameters[0]);
595 return true;
596 }
597 }
598
599 return false;
600}
601
602bool CCECCommandHandler::HandleSetSystemAudioMode(const cec_command &command)
603{
604 if (command.parameters.size == 1)
605 {
606 CCECAudioSystem *device = CCECBusDevice::AsAudioSystem(GetDevice(command.initiator));
607 if (device)
608 {
609 device->SetSystemAudioModeStatus((cec_system_audio_status)command.parameters[0]);
610 return true;
611 }
612 }
613
614 return false;
615}
616
617bool CCECCommandHandler::HandleTextViewOn(const cec_command &command)
618{
619 m_processor->GetDevice(command.initiator)->MarkAsActiveSource();
620 return true;
621}
622
623bool CCECCommandHandler::HandleUserControlPressed(const cec_command &command)
624{
625 if (m_processor->CECInitialised() &&
626 m_processor->IsHandledByLibCEC(command.destination) &&
627 command.parameters.size > 0)
628 {
629 CCECBusDevice *device = GetDevice(command.destination);
630 if (!device)
631 return true;
632
633 CCECClient *client = device->GetClient();
634 if (client)
635 client->AddKey();
636
637 if (command.parameters[0] <= CEC_USER_CONTROL_CODE_MAX)
638 client->SetCurrentButton((cec_user_control_code) command.parameters[0]);
639
640 if (command.parameters[0] == CEC_USER_CONTROL_CODE_POWER ||
641 command.parameters[0] == CEC_USER_CONTROL_CODE_POWER_ON_FUNCTION)
642 {
643 bool bPowerOn(true);
644 if (!device)
645 return true;
646
647 // CEC_USER_CONTROL_CODE_POWER operates as a toggle
648 // assume CEC_USER_CONTROL_CODE_POWER_ON_FUNCTION does not
649 if (command.parameters[0] == CEC_USER_CONTROL_CODE_POWER)
650 {
651 cec_power_status status = device->GetCurrentPowerStatus();
652 bPowerOn = !(status == CEC_POWER_STATUS_ON || status == CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON);
653 }
654
655 if (bPowerOn)
656 {
657 device->ActivateSource();
658 }
659 else
660 {
661 device->MarkAsInactiveSource();
662 device->TransmitInactiveSource();
663 device->SetMenuState(CEC_MENU_STATE_DEACTIVATED);
664 }
665 }
666
667 return true;
668 }
669 return false;
670}
671
672bool CCECCommandHandler::HandleUserControlRelease(const cec_command &command)
673{
674 CCECClient *client = m_processor->GetClient(command.destination);
675 if (client)
676 client->AddKey();
677 return true;
678}
679
680bool CCECCommandHandler::HandleVendorCommand(const cec_command & UNUSED(command))
681{
682 return true;
683}
684
685void CCECCommandHandler::UnhandledCommand(const cec_command &command)
686{
687 LIB_CEC->AddLog(CEC_LOG_DEBUG, "unhandled command with opcode %02x from address %d", command.opcode, command.initiator);
688
689 if (m_processor->CECInitialised() && m_processor->IsHandledByLibCEC(command.destination))
690 m_processor->TransmitAbort(m_busDevice->GetLogicalAddress(), command.initiator, command.opcode, CEC_ABORT_REASON_UNRECOGNIZED_OPCODE);
691}
692
693size_t CCECCommandHandler::GetMyDevices(vector<CCECBusDevice *> &devices) const
694{
695 size_t iReturn(0);
696
697 cec_logical_addresses addresses = m_processor->GetLogicalAddresses();
698 for (uint8_t iPtr = CECDEVICE_TV; iPtr < CECDEVICE_BROADCAST; iPtr++)
699 {
700 if (addresses[iPtr])
701 {
702 devices.push_back(GetDevice((cec_logical_address) iPtr));
703 ++iReturn;
704 }
705 }
706
707 return iReturn;
708}
709
710CCECBusDevice *CCECCommandHandler::GetDevice(cec_logical_address iLogicalAddress) const
711{
712 return m_processor->GetDevice(iLogicalAddress);
713}
714
715CCECBusDevice *CCECCommandHandler::GetDeviceByPhysicalAddress(uint16_t iPhysicalAddress) const
716{
717 return m_processor->GetDeviceByPhysicalAddress(iPhysicalAddress);
718}
719
720bool CCECCommandHandler::SetVendorId(const cec_command &command)
721{
722 bool bChanged(false);
723 if (command.parameters.size < 3)
724 {
725 LIB_CEC->AddLog(CEC_LOG_WARNING, "invalid vendor ID received");
726 return bChanged;
727 }
728
729 uint64_t iVendorId = ((uint64_t)command.parameters[0] << 16) +
730 ((uint64_t)command.parameters[1] << 8) +
731 (uint64_t)command.parameters[2];
732
733 CCECBusDevice *device = GetDevice((cec_logical_address) command.initiator);
734 if (device)
735 bChanged = device->SetVendorId(iVendorId);
736 return bChanged;
737}
738
739void CCECCommandHandler::SetPhysicalAddress(cec_logical_address iAddress, uint16_t iNewAddress)
740{
741 if (!m_processor->IsHandledByLibCEC(iAddress))
742 {
743 CCECBusDevice *otherDevice = m_processor->GetDeviceByPhysicalAddress(iNewAddress);
744 CCECClient *client = otherDevice ? otherDevice->GetClient() : NULL;
745
746 CCECBusDevice *device = m_processor->GetDevice(iAddress);
747 if (device)
748 device->SetPhysicalAddress(iNewAddress);
749 else
750 {
751 LIB_CEC->AddLog(CEC_LOG_DEBUG, "device with logical address %X not found", iAddress);
752 }
753
754 /* another device reported the same physical address as ours */
755 if (client)
756 client->ResetPhysicalAddress();
757 }
758 else
759 {
760 LIB_CEC->AddLog(CEC_LOG_DEBUG, "ignore physical address report for device %s (%X) because it's marked as handled by libCEC", ToString(iAddress), iAddress);
761 }
762}
763
764bool CCECCommandHandler::PowerOn(const cec_logical_address iInitiator, const cec_logical_address iDestination)
765{
766 if (iDestination == CECDEVICE_TV)
767 return TransmitImageViewOn(iInitiator, iDestination);
768
769 return TransmitKeypress(iInitiator, iDestination, CEC_USER_CONTROL_CODE_POWER) &&
770 TransmitKeyRelease(iInitiator, iDestination);
771}
772
773bool CCECCommandHandler::TransmitImageViewOn(const cec_logical_address iInitiator, const cec_logical_address iDestination)
774{
775 cec_command command;
776 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_IMAGE_VIEW_ON);
777
778 return Transmit(command);
779}
780
781bool CCECCommandHandler::TransmitStandby(const cec_logical_address iInitiator, const cec_logical_address iDestination)
782{
783 cec_command command;
784 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_STANDBY);
785
786 return Transmit(command);
787}
788
789bool CCECCommandHandler::TransmitRequestActiveSource(const cec_logical_address iInitiator, bool bWaitForResponse /* = true */)
790{
791 cec_command command;
792 cec_command::Format(command, iInitiator, CECDEVICE_BROADCAST, CEC_OPCODE_REQUEST_ACTIVE_SOURCE);
793
794 return Transmit(command, !bWaitForResponse);
795}
796
797bool CCECCommandHandler::TransmitRequestCecVersion(const cec_logical_address iInitiator, const cec_logical_address iDestination, bool bWaitForResponse /* = true */)
798{
799 cec_command command;
800 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_GET_CEC_VERSION);
801
802 return Transmit(command, !bWaitForResponse);
803}
804
805bool CCECCommandHandler::TransmitRequestMenuLanguage(const cec_logical_address iInitiator, const cec_logical_address iDestination, bool bWaitForResponse /* = true */)
806{
807 cec_command command;
808 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_GET_MENU_LANGUAGE);
809
810 return Transmit(command, !bWaitForResponse);
811}
812
813bool CCECCommandHandler::TransmitRequestOSDName(const cec_logical_address iInitiator, const cec_logical_address iDestination, bool bWaitForResponse /* = true */)
814{
815 cec_command command;
816 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_GIVE_OSD_NAME);
817
818 return Transmit(command, !bWaitForResponse);
819}
820
821bool CCECCommandHandler::TransmitRequestPhysicalAddress(const cec_logical_address iInitiator, const cec_logical_address iDestination, bool bWaitForResponse /* = true */)
822{
823 cec_command command;
824 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_GIVE_PHYSICAL_ADDRESS);
825
826 return Transmit(command, !bWaitForResponse);
827}
828
829bool CCECCommandHandler::TransmitRequestPowerStatus(const cec_logical_address iInitiator, const cec_logical_address iDestination, bool bWaitForResponse /* = true */)
830{
831 cec_command command;
832 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_GIVE_DEVICE_POWER_STATUS);
833
834 return Transmit(command, !bWaitForResponse);
835}
836
837bool CCECCommandHandler::TransmitRequestVendorId(const cec_logical_address iInitiator, const cec_logical_address iDestination, bool bWaitForResponse /* = true */)
838{
839 cec_command command;
840 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_GIVE_DEVICE_VENDOR_ID);
841
842 return Transmit(command, !bWaitForResponse);
843}
844
845bool CCECCommandHandler::TransmitActiveSource(const cec_logical_address iInitiator, uint16_t iPhysicalAddress)
846{
847 cec_command command;
848 cec_command::Format(command, iInitiator, CECDEVICE_BROADCAST, CEC_OPCODE_ACTIVE_SOURCE);
849 command.parameters.PushBack((uint8_t) ((iPhysicalAddress >> 8) & 0xFF));
850 command.parameters.PushBack((uint8_t) (iPhysicalAddress & 0xFF));
851
852 return Transmit(command);
853}
854
855bool CCECCommandHandler::TransmitCECVersion(const cec_logical_address iInitiator, const cec_logical_address iDestination, cec_version cecVersion)
856{
857 cec_command command;
858 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_CEC_VERSION);
859 command.parameters.PushBack((uint8_t)cecVersion);
860
861 return Transmit(command);
862}
863
864bool CCECCommandHandler::TransmitInactiveSource(const cec_logical_address iInitiator, uint16_t iPhysicalAddress)
865{
866 cec_command command;
867 cec_command::Format(command, iInitiator, CECDEVICE_TV, CEC_OPCODE_INACTIVE_SOURCE);
868 command.parameters.PushBack((iPhysicalAddress >> 8) & 0xFF);
869 command.parameters.PushBack(iPhysicalAddress & 0xFF);
870
871 return Transmit(command);
872}
873
874bool CCECCommandHandler::TransmitMenuState(const cec_logical_address iInitiator, const cec_logical_address iDestination, cec_menu_state menuState)
875{
876 cec_command command;
877 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_MENU_STATUS);
878 command.parameters.PushBack((uint8_t)menuState);
879
880 return Transmit(command);
881}
882
883bool CCECCommandHandler::TransmitOSDName(const cec_logical_address iInitiator, const cec_logical_address iDestination, CStdString strDeviceName)
884{
885 cec_command command;
886 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_SET_OSD_NAME);
887 for (size_t iPtr = 0; iPtr < strDeviceName.length(); iPtr++)
888 command.parameters.PushBack(strDeviceName.at(iPtr));
889
890 return Transmit(command);
891}
892
893bool CCECCommandHandler::TransmitOSDString(const cec_logical_address iInitiator, const cec_logical_address iDestination, cec_display_control duration, const char *strMessage)
894{
895 cec_command command;
896 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_SET_OSD_STRING);
897 command.parameters.PushBack((uint8_t)duration);
898
899 size_t iLen = strlen(strMessage);
900 if (iLen > 13) iLen = 13;
901
902 for (size_t iPtr = 0; iPtr < iLen; iPtr++)
903 command.parameters.PushBack(strMessage[iPtr]);
904
905 return Transmit(command);
906}
907
908bool CCECCommandHandler::TransmitPhysicalAddress(const cec_logical_address iInitiator, uint16_t iPhysicalAddress, cec_device_type type)
909{
910 cec_command command;
911 cec_command::Format(command, iInitiator, CECDEVICE_BROADCAST, CEC_OPCODE_REPORT_PHYSICAL_ADDRESS);
912 command.parameters.PushBack((uint8_t) ((iPhysicalAddress >> 8) & 0xFF));
913 command.parameters.PushBack((uint8_t) (iPhysicalAddress & 0xFF));
914 command.parameters.PushBack((uint8_t) (type));
915
916 return Transmit(command);
917}
918
919bool CCECCommandHandler::TransmitSetMenuLanguage(const cec_logical_address iInitiator, const char lang[3])
920{
921 cec_command command;
922 command.Format(command, iInitiator, CECDEVICE_BROADCAST, CEC_OPCODE_SET_MENU_LANGUAGE);
923 command.parameters.PushBack((uint8_t) lang[0]);
924 command.parameters.PushBack((uint8_t) lang[1]);
925 command.parameters.PushBack((uint8_t) lang[2]);
926
927 return Transmit(command);
928}
929
930bool CCECCommandHandler::TransmitPoll(const cec_logical_address iInitiator, const cec_logical_address iDestination)
931{
932 cec_command command;
933 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_NONE);
934
935 return Transmit(command);
936}
937
938bool CCECCommandHandler::TransmitPowerState(const cec_logical_address iInitiator, const cec_logical_address iDestination, cec_power_status state)
939{
940 cec_command command;
941 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_REPORT_POWER_STATUS);
942 command.parameters.PushBack((uint8_t) state);
943
944 return Transmit(command);
945}
946
947bool CCECCommandHandler::TransmitVendorID(const cec_logical_address iInitiator, uint64_t iVendorId)
948{
949 cec_command command;
950 cec_command::Format(command, iInitiator, CECDEVICE_BROADCAST, CEC_OPCODE_DEVICE_VENDOR_ID);
951
952 command.parameters.PushBack((uint8_t) (((uint64_t)iVendorId >> 16) & 0xFF));
953 command.parameters.PushBack((uint8_t) (((uint64_t)iVendorId >> 8) & 0xFF));
954 command.parameters.PushBack((uint8_t) ((uint64_t)iVendorId & 0xFF));
955
956 return Transmit(command);
957}
958
959bool CCECCommandHandler::TransmitAudioStatus(const cec_logical_address iInitiator, const cec_logical_address iDestination, uint8_t state)
960{
961 cec_command command;
962 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_REPORT_AUDIO_STATUS);
963 command.parameters.PushBack(state);
964
965 return Transmit(command);
966}
967
968bool CCECCommandHandler::TransmitSetSystemAudioMode(const cec_logical_address iInitiator, const cec_logical_address iDestination, cec_system_audio_status state)
969{
970 cec_command command;
971 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_SET_SYSTEM_AUDIO_MODE);
972 command.parameters.PushBack((uint8_t)state);
973
974 return Transmit(command);
975}
976
977bool CCECCommandHandler::TransmitSetStreamPath(uint16_t iStreamPath)
978{
979 cec_command command;
980 cec_command::Format(command, m_busDevice->GetLogicalAddress(), CECDEVICE_BROADCAST, CEC_OPCODE_SET_STREAM_PATH);
981 command.parameters.PushBack((uint8_t) ((iStreamPath >> 8) & 0xFF));
982 command.parameters.PushBack((uint8_t) (iStreamPath & 0xFF));
983
984 return Transmit(command);
985}
986
987bool CCECCommandHandler::TransmitSystemAudioModeStatus(const cec_logical_address iInitiator, const cec_logical_address iDestination, cec_system_audio_status state)
988{
989 cec_command command;
990 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_SYSTEM_AUDIO_MODE_STATUS);
991 command.parameters.PushBack((uint8_t)state);
992
993 return Transmit(command);
994}
995
996bool CCECCommandHandler::TransmitDeckStatus(const cec_logical_address iInitiator, const cec_logical_address iDestination, cec_deck_info state)
997{
998 cec_command command;
999 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_DECK_STATUS);
1000 command.PushBack((uint8_t)state);
1001
1002 return Transmit(command);
1003}
1004
1005bool CCECCommandHandler::TransmitKeypress(const cec_logical_address iInitiator, const cec_logical_address iDestination, cec_user_control_code key, bool bWait /* = true */)
1006{
1007 cec_command command;
1008 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_USER_CONTROL_PRESSED);
1009 command.parameters.PushBack((uint8_t)key);
1010
1011 return Transmit(command, !bWait);
1012}
1013
1014bool CCECCommandHandler::TransmitKeyRelease(const cec_logical_address iInitiator, const cec_logical_address iDestination, bool bWait /* = true */)
1015{
1016 cec_command command;
1017 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_USER_CONTROL_RELEASE);
1018
1019 return Transmit(command, !bWait);
1020}
1021
1022bool CCECCommandHandler::Transmit(cec_command &command, bool bSuppressWait /* = false */)
1023{
1024 bool bReturn(false);
1025 cec_opcode expectedResponse(cec_command::GetResponseOpcode(command.opcode));
1026 bool bExpectResponse(expectedResponse != CEC_OPCODE_NONE && !bSuppressWait);
1027 command.transmit_timeout = m_iTransmitTimeout;
1028
1029 if (command.initiator == CECDEVICE_UNKNOWN)
1030 {
1031 LIB_CEC->AddLog(CEC_LOG_ERROR, "not transmitting a command without a valid initiator");
1032 return bReturn;
1033 }
1034
1035 {
1036 uint8_t iTries(0), iMaxTries(!command.opcode_set ? 1 : m_iTransmitRetries + 1);
1037 while (!bReturn && ++iTries <= iMaxTries && !m_busDevice->IsUnsupportedFeature(command.opcode))
1038 {
1039 if ((bReturn = m_processor->Transmit(command)) == true)
1040 {
1041 LIB_CEC->AddLog(CEC_LOG_DEBUG, "command transmitted");
1042 if (bExpectResponse)
1043 {
1044 bReturn = m_waitForResponse->Wait(expectedResponse);
1045 LIB_CEC->AddLog(CEC_LOG_DEBUG, bReturn ? "expected response received (%X: %s)" : "expected response not received (%X: %s)", (int)expectedResponse, ToString(expectedResponse));
1046 }
1047 }
1048 }
1049 }
1050
1051 return bReturn;
1052}
1053
1054bool CCECCommandHandler::ActivateSource(void)
1055{
1056 if (m_busDevice->IsActiveSource() &&
1057 m_busDevice->IsHandledByLibCEC())
1058 {
1059 m_busDevice->SetPowerStatus(CEC_POWER_STATUS_ON);
1060 m_busDevice->SetMenuState(CEC_MENU_STATE_ACTIVATED);
1061
1062 m_busDevice->TransmitImageViewOn();
1063 m_busDevice->TransmitActiveSource();
1064 m_busDevice->TransmitMenuState(CECDEVICE_TV);
1065
1066 CCECPlaybackDevice *playbackDevice = m_busDevice->AsPlaybackDevice();
1067 if (playbackDevice && SendDeckStatusUpdateOnActiveSource())
1068 playbackDevice->TransmitDeckStatus(CECDEVICE_TV);
1069 m_bHandlerInited = true;
1070 }
1071 return true;
1072}
1073
1074void CCECCommandHandler::SignalOpcode(cec_opcode opcode)
1075{
1076 m_waitForResponse->Received(opcode);
1077}