2 * This file is part of the libCEC(R) library.
4 * libCEC(R) is Copyright (C) 2011-2013 Pulse-Eight Limited. All rights reserved.
5 * libCEC(R) is an original work, containing original code.
7 * libCEC(R) is a trademark of Pulse-Eight Limited.
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.
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.
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.
24 * Alternatively, you can license this library under a commercial license,
25 * please contact Pulse-Eight Licensing for more information.
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/
34 #include "CECCommandHandler.h"
36 #include "lib/devices/CECBusDevice.h"
37 #include "lib/devices/CECAudioSystem.h"
38 #include "lib/devices/CECPlaybackDevice.h"
39 #include "lib/CECClient.h"
40 #include "lib/CECProcessor.h"
41 #include "lib/LibCEC.h"
42 #include "lib/CECTypeUtils.h"
43 #include "lib/platform/util/util.h"
47 using namespace PLATFORM
;
49 #define LIB_CEC m_busDevice->GetProcessor()->GetLib()
50 #define ToString(p) CCECTypeUtils::ToString(p)
51 #define REQUEST_POWER_STATUS_TIMEOUT 5000
53 CCECCommandHandler::CCECCommandHandler(CCECBusDevice
*busDevice
,
54 int32_t iTransmitTimeout
/* = CEC_DEFAULT_TRANSMIT_TIMEOUT */,
55 int32_t iTransmitWait
/* = CEC_DEFAULT_TRANSMIT_WAIT */,
56 int8_t iTransmitRetries
/* = CEC_DEFAULT_TRANSMIT_RETRIES */,
57 int64_t iActiveSourcePending
/* = 0 */) :
58 m_busDevice(busDevice
),
59 m_processor(m_busDevice
->GetProcessor()),
60 m_iTransmitTimeout(iTransmitTimeout
),
61 m_iTransmitWait(iTransmitWait
),
62 m_iTransmitRetries(iTransmitRetries
),
63 m_bHandlerInited(false),
64 m_bOPTSendDeckStatusUpdateOnActiveSource(false),
65 m_vendorId(CEC_VENDOR_UNKNOWN
),
66 m_iActiveSourcePending(iActiveSourcePending
),
67 m_iPowerStatusRequested(0)
71 bool CCECCommandHandler::HandleCommand(const cec_command
&command
)
73 if (command
.opcode_set
== 0)
74 return HandlePoll(command
);
76 int iHandled(CEC_ABORT_REASON_UNRECOGNIZED_OPCODE
);
78 LIB_CEC
->AddCommand(command
);
80 switch(command
.opcode
)
82 case CEC_OPCODE_REPORT_POWER_STATUS
:
83 iHandled
= HandleReportPowerStatus(command
);
85 case CEC_OPCODE_CEC_VERSION
:
86 iHandled
= HandleDeviceCecVersion(command
);
88 case CEC_OPCODE_SET_MENU_LANGUAGE
:
89 iHandled
= HandleSetMenuLanguage(command
);
91 case CEC_OPCODE_GIVE_PHYSICAL_ADDRESS
:
92 iHandled
= HandleGivePhysicalAddress(command
);
94 case CEC_OPCODE_GET_MENU_LANGUAGE
:
95 iHandled
= HandleGiveMenuLanguage(command
);
97 case CEC_OPCODE_GIVE_OSD_NAME
:
98 iHandled
= HandleGiveOSDName(command
);
100 case CEC_OPCODE_GIVE_DEVICE_VENDOR_ID
:
101 iHandled
= HandleGiveDeviceVendorId(command
);
103 case CEC_OPCODE_DEVICE_VENDOR_ID
:
104 iHandled
= HandleDeviceVendorId(command
);
106 case CEC_OPCODE_VENDOR_COMMAND_WITH_ID
:
107 iHandled
= HandleDeviceVendorCommandWithId(command
);
109 case CEC_OPCODE_GIVE_DECK_STATUS
:
110 iHandled
= HandleGiveDeckStatus(command
);
112 case CEC_OPCODE_DECK_CONTROL
:
113 iHandled
= HandleDeckControl(command
);
115 case CEC_OPCODE_MENU_REQUEST
:
116 iHandled
= HandleMenuRequest(command
);
118 case CEC_OPCODE_GIVE_DEVICE_POWER_STATUS
:
119 iHandled
= HandleGiveDevicePowerStatus(command
);
121 case CEC_OPCODE_GET_CEC_VERSION
:
122 iHandled
= HandleGetCecVersion(command
);
124 case CEC_OPCODE_USER_CONTROL_PRESSED
:
125 iHandled
= HandleUserControlPressed(command
);
127 case CEC_OPCODE_USER_CONTROL_RELEASE
:
128 iHandled
= HandleUserControlRelease(command
);
130 case CEC_OPCODE_GIVE_AUDIO_STATUS
:
131 iHandled
= HandleGiveAudioStatus(command
);
133 case CEC_OPCODE_GIVE_SYSTEM_AUDIO_MODE_STATUS
:
134 iHandled
= HandleGiveSystemAudioModeStatus(command
);
136 case CEC_OPCODE_SYSTEM_AUDIO_MODE_REQUEST
:
137 iHandled
= HandleSystemAudioModeRequest(command
);
139 case CEC_OPCODE_REPORT_AUDIO_STATUS
:
140 iHandled
= HandleReportAudioStatus(command
);
142 case CEC_OPCODE_SYSTEM_AUDIO_MODE_STATUS
:
143 iHandled
= HandleSystemAudioModeStatus(command
);
145 case CEC_OPCODE_SET_SYSTEM_AUDIO_MODE
:
146 iHandled
= HandleSetSystemAudioMode(command
);
148 case CEC_OPCODE_REQUEST_ACTIVE_SOURCE
:
149 iHandled
= HandleRequestActiveSource(command
);
151 case CEC_OPCODE_SET_STREAM_PATH
:
152 iHandled
= HandleSetStreamPath(command
);
154 case CEC_OPCODE_ROUTING_CHANGE
:
155 iHandled
= HandleRoutingChange(command
);
157 case CEC_OPCODE_ROUTING_INFORMATION
:
158 iHandled
= HandleRoutingInformation(command
);
160 case CEC_OPCODE_STANDBY
:
161 iHandled
= HandleStandby(command
);
163 case CEC_OPCODE_ACTIVE_SOURCE
:
164 iHandled
= HandleActiveSource(command
);
166 case CEC_OPCODE_REPORT_PHYSICAL_ADDRESS
:
167 iHandled
= HandleReportPhysicalAddress(command
);
169 case CEC_OPCODE_SET_OSD_NAME
:
170 iHandled
= HandleSetOSDName(command
);
172 case CEC_OPCODE_IMAGE_VIEW_ON
:
173 iHandled
= HandleImageViewOn(command
);
175 case CEC_OPCODE_TEXT_VIEW_ON
:
176 iHandled
= HandleTextViewOn(command
);
178 case CEC_OPCODE_FEATURE_ABORT
:
179 iHandled
= HandleFeatureAbort(command
);
181 case CEC_OPCODE_VENDOR_COMMAND
:
182 iHandled
= HandleVendorCommand(command
);
184 case CEC_OPCODE_VENDOR_REMOTE_BUTTON_DOWN
:
185 iHandled
= HandleVendorRemoteButtonDown(command
);
187 case CEC_OPCODE_VENDOR_REMOTE_BUTTON_UP
:
188 iHandled
= HandleVendorRemoteButtonUp(command
);
190 case CEC_OPCODE_PLAY
:
191 // libCEC (currently) doesn't need to do anything with this, since player applications handle it
192 // but it should not respond with a feature abort
193 iHandled
= COMMAND_HANDLED
;
199 if (iHandled
== COMMAND_HANDLED
)
200 m_busDevice
->SignalOpcode((command
.opcode
== CEC_OPCODE_FEATURE_ABORT
&& command
.parameters
.size
> 0) ? (cec_opcode
)command
.parameters
[0] : command
.opcode
);
202 UnhandledCommand(command
, (cec_abort_reason
)iHandled
);
204 return iHandled
== COMMAND_HANDLED
;
207 int CCECCommandHandler::HandleActiveSource(const cec_command
&command
)
209 if (command
.parameters
.size
== 2)
211 uint16_t iAddress
= ((uint16_t)command
.parameters
[0] << 8) | ((uint16_t)command
.parameters
[1]);
212 CCECBusDevice
*device
= m_processor
->GetDevice(command
.initiator
);
215 device
->SetPhysicalAddress(iAddress
);
216 device
->MarkAsActiveSource();
219 m_processor
->GetDevices()->SignalAll(command
.opcode
);
220 return COMMAND_HANDLED
;
223 return CEC_ABORT_REASON_INVALID_OPERAND
;
226 int CCECCommandHandler::HandleDeckControl(const cec_command
&command
)
228 CCECPlaybackDevice
*device
= CCECBusDevice::AsPlaybackDevice(GetDevice(command
.destination
));
229 if (device
&& command
.parameters
.size
> 0)
231 device
->SetDeckControlMode((cec_deck_control_mode
) command
.parameters
[0]);
232 return COMMAND_HANDLED
;
235 return CEC_ABORT_REASON_INVALID_OPERAND
;
238 int CCECCommandHandler::HandleDeviceCecVersion(const cec_command
&command
)
240 if (command
.parameters
.size
== 1)
242 CCECBusDevice
*device
= GetDevice(command
.initiator
);
244 device
->SetCecVersion((cec_version
) command
.parameters
[0]);
246 return COMMAND_HANDLED
;
249 return CEC_ABORT_REASON_INVALID_OPERAND
;
252 int CCECCommandHandler::HandleDeviceVendorCommandWithId(const cec_command
& UNUSED(command
))
254 return CEC_ABORT_REASON_INVALID_OPERAND
;
257 int CCECCommandHandler::HandleDeviceVendorId(const cec_command
&command
)
259 SetVendorId(command
);
261 if (command
.initiator
== CECDEVICE_TV
)
263 CCECBusDevice
* primary
= m_processor
->GetPrimaryDevice();
265 primary
->TransmitVendorID(CECDEVICE_BROADCAST
, false, false);
268 return COMMAND_HANDLED
;
271 int CCECCommandHandler::HandleFeatureAbort(const cec_command
&command
)
273 if (command
.parameters
.size
== 2 &&
274 (command
.parameters
[1] == CEC_ABORT_REASON_UNRECOGNIZED_OPCODE
||
275 command
.parameters
[1] == CEC_ABORT_REASON_REFUSED
))
276 m_processor
->GetDevice(command
.initiator
)->SetUnsupportedFeature((cec_opcode
)command
.parameters
[0]);
277 return COMMAND_HANDLED
;
280 int CCECCommandHandler::HandleGetCecVersion(const cec_command
&command
)
282 if (m_processor
->CECInitialised() && m_processor
->IsHandledByLibCEC(command
.destination
))
284 CCECBusDevice
*device
= GetDevice(command
.destination
);
285 if (device
&& device
->TransmitCECVersion(command
.initiator
, true))
286 return COMMAND_HANDLED
;
287 return CEC_ABORT_REASON_INVALID_OPERAND
;
290 return CEC_ABORT_REASON_NOT_IN_CORRECT_MODE_TO_RESPOND
;
293 int CCECCommandHandler::HandleGiveAudioStatus(const cec_command
&command
)
295 if (m_processor
->CECInitialised() && m_processor
->IsHandledByLibCEC(command
.destination
))
297 CCECAudioSystem
*device
= CCECBusDevice::AsAudioSystem(GetDevice(command
.destination
));
298 if (device
&& device
->TransmitAudioStatus(command
.initiator
, true))
299 return COMMAND_HANDLED
;
300 return CEC_ABORT_REASON_INVALID_OPERAND
;
303 return CEC_ABORT_REASON_NOT_IN_CORRECT_MODE_TO_RESPOND
;
306 int CCECCommandHandler::HandleGiveDeckStatus(const cec_command
&command
)
308 if (m_processor
->CECInitialised() && m_processor
->IsHandledByLibCEC(command
.destination
))
310 CCECPlaybackDevice
*device
= CCECBusDevice::AsPlaybackDevice(GetDevice(command
.destination
));
311 if (device
&& device
->TransmitDeckStatus(command
.initiator
, true))
312 return COMMAND_HANDLED
;
313 return CEC_ABORT_REASON_INVALID_OPERAND
;
316 return CEC_ABORT_REASON_NOT_IN_CORRECT_MODE_TO_RESPOND
;
319 int CCECCommandHandler::HandleGiveDevicePowerStatus(const cec_command
&command
)
321 if (m_processor
->CECInitialised() && m_processor
->IsHandledByLibCEC(command
.destination
))
323 CCECBusDevice
*device
= GetDevice(command
.destination
);
324 if (device
&& device
->TransmitPowerState(command
.initiator
, true))
325 return COMMAND_HANDLED
;
326 return CEC_ABORT_REASON_INVALID_OPERAND
;
329 return CEC_ABORT_REASON_INVALID_OPERAND
;
332 int CCECCommandHandler::HandleGiveDeviceVendorId(const cec_command
&command
)
334 if (m_processor
->CECInitialised() && m_processor
->IsHandledByLibCEC(command
.destination
))
336 CCECBusDevice
*device
= GetDevice(command
.destination
);
337 if (device
&& device
->TransmitVendorID(command
.initiator
, true, true))
338 return COMMAND_HANDLED
;
341 return CEC_ABORT_REASON_INVALID_OPERAND
;
344 int CCECCommandHandler::HandleGiveOSDName(const cec_command
&command
)
346 if (m_processor
->CECInitialised() && m_processor
->IsHandledByLibCEC(command
.destination
))
348 CCECBusDevice
*device
= GetDevice(command
.destination
);
349 if (device
&& device
->TransmitOSDName(command
.initiator
, true))
350 return COMMAND_HANDLED
;
353 return CEC_ABORT_REASON_INVALID_OPERAND
;
356 int CCECCommandHandler::HandleGivePhysicalAddress(const cec_command
&command
)
358 if (m_processor
->CECInitialised() && m_processor
->IsHandledByLibCEC(command
.destination
))
360 CCECBusDevice
*device
= GetDevice(command
.destination
);
361 if (device
&& device
->TransmitPhysicalAddress(true))
362 return COMMAND_HANDLED
;
363 return CEC_ABORT_REASON_INVALID_OPERAND
;
366 return CEC_ABORT_REASON_NOT_IN_CORRECT_MODE_TO_RESPOND
;
369 int CCECCommandHandler::HandleGiveMenuLanguage(const cec_command
&command
)
371 if (m_processor
->CECInitialised() && m_processor
->IsHandledByLibCEC(command
.destination
))
373 CCECBusDevice
*device
= GetDevice(command
.destination
);
374 if (device
&& device
->TransmitSetMenuLanguage(command
.initiator
, true))
375 return COMMAND_HANDLED
;
376 return CEC_ABORT_REASON_INVALID_OPERAND
;
379 return CEC_ABORT_REASON_NOT_IN_CORRECT_MODE_TO_RESPOND
;
382 int CCECCommandHandler::HandleGiveSystemAudioModeStatus(const cec_command
&command
)
384 if (m_processor
->CECInitialised() && m_processor
->IsHandledByLibCEC(command
.destination
))
386 CCECAudioSystem
*device
= CCECBusDevice::AsAudioSystem(GetDevice(command
.destination
));
387 if (device
&& device
->TransmitSystemAudioModeStatus(command
.initiator
, true))
388 return COMMAND_HANDLED
;
389 return CEC_ABORT_REASON_INVALID_OPERAND
;
392 return CEC_ABORT_REASON_NOT_IN_CORRECT_MODE_TO_RESPOND
;
395 int CCECCommandHandler::HandleImageViewOn(const cec_command
&command
)
397 CCECBusDevice
*device
= GetDevice(command
.destination
);
398 if (device
&& device
->GetCurrentStatus() == CEC_DEVICE_STATUS_PRESENT
)
400 if (device
->GetCurrentPowerStatus() == CEC_POWER_STATUS_STANDBY
||
401 device
->GetCurrentPowerStatus() == CEC_POWER_STATUS_IN_TRANSITION_ON_TO_STANDBY
)
402 device
->SetPowerStatus(CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON
);
403 CCECBusDevice
* tv
= GetDevice(CECDEVICE_TV
);
405 tv
->OnImageViewOnSent(false);
407 return COMMAND_HANDLED
;
410 int CCECCommandHandler::HandleMenuRequest(const cec_command
&command
)
412 if (m_processor
->CECInitialised() && m_processor
->IsHandledByLibCEC(command
.destination
))
414 CCECBusDevice
*device
= GetDevice(command
.destination
);
417 CCECClient
*client
= device
->GetClient();
420 if (command
.parameters
[0] == CEC_MENU_REQUEST_TYPE_ACTIVATE
)
422 if (client
->MenuStateChanged(CEC_MENU_STATE_ACTIVATED
) == 1)
423 device
->SetMenuState(CEC_MENU_STATE_ACTIVATED
);
425 else if (command
.parameters
[0] == CEC_MENU_REQUEST_TYPE_DEACTIVATE
)
427 if (client
->MenuStateChanged(CEC_MENU_STATE_DEACTIVATED
) == 1)
428 device
->SetMenuState(CEC_MENU_STATE_DEACTIVATED
);
431 if (device
->TransmitMenuState(command
.initiator
, true))
432 return COMMAND_HANDLED
;
434 return CEC_ABORT_REASON_INVALID_OPERAND
;
437 return CEC_ABORT_REASON_NOT_IN_CORRECT_MODE_TO_RESPOND
;
440 bool CCECCommandHandler::HandlePoll(const cec_command
&command
)
442 m_busDevice
->HandlePoll(command
.destination
);
446 int CCECCommandHandler::HandleReportAudioStatus(const cec_command
&command
)
448 if (command
.parameters
.size
== 1)
450 CCECAudioSystem
*device
= CCECBusDevice::AsAudioSystem(GetDevice(command
.initiator
));
453 device
->SetAudioStatus(command
.parameters
[0]);
454 return COMMAND_HANDLED
;
457 return CEC_ABORT_REASON_INVALID_OPERAND
;
460 int CCECCommandHandler::HandleReportPhysicalAddress(const cec_command
&command
)
462 if (command
.parameters
.size
== 3)
464 uint16_t iNewAddress
= ((uint16_t)command
.parameters
[0] << 8) | ((uint16_t)command
.parameters
[1]);
465 SetPhysicalAddress(command
.initiator
, iNewAddress
);
467 if (command
.initiator
== CECDEVICE_TV
)
469 CCECBusDevice
* primary
= m_processor
->GetPrimaryDevice();
471 primary
->TransmitPhysicalAddress(false);
473 return COMMAND_HANDLED
;
475 return CEC_ABORT_REASON_INVALID_OPERAND
;
478 int CCECCommandHandler::HandleReportPowerStatus(const cec_command
&command
)
480 if (command
.parameters
.size
== 1)
482 CCECBusDevice
*device
= GetDevice(command
.initiator
);
485 device
->SetPowerStatus((cec_power_status
) command
.parameters
[0]);
486 return COMMAND_HANDLED
;
489 return CEC_ABORT_REASON_INVALID_OPERAND
;
492 int CCECCommandHandler::HandleRequestActiveSource(const cec_command
&command
)
494 if (m_processor
->CECInitialised())
496 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, ">> %i requests active source", (uint8_t) command
.initiator
);
497 m_processor
->GetDevice(command
.initiator
)->SetPowerStatus(CEC_POWER_STATUS_ON
);
499 vector
<CCECBusDevice
*> devices
;
500 for (size_t iDevicePtr
= 0; iDevicePtr
< GetMyDevices(devices
); iDevicePtr
++)
501 devices
[iDevicePtr
]->TransmitActiveSource(true);
504 return COMMAND_HANDLED
;
507 int CCECCommandHandler::HandleRoutingChange(const cec_command
&command
)
509 if (command
.parameters
.size
== 4)
511 CCECBusDevice
*device
= GetDevice(command
.initiator
);
514 uint16_t iNewAddress
= ((uint16_t)command
.parameters
[2] << 8) | ((uint16_t)command
.parameters
[3]);
515 device
->SetActiveRoute(iNewAddress
);
516 return COMMAND_HANDLED
;
520 return CEC_ABORT_REASON_INVALID_OPERAND
;
523 int CCECCommandHandler::HandleRoutingInformation(const cec_command
&command
)
525 if (command
.parameters
.size
== 2)
527 CCECBusDevice
*device
= GetDevice(command
.initiator
);
530 uint16_t iNewAddress
= ((uint16_t)command
.parameters
[0] << 8) | ((uint16_t)command
.parameters
[1]);
531 device
->SetActiveRoute(iNewAddress
);
532 return COMMAND_HANDLED
;
536 return CEC_ABORT_REASON_INVALID_OPERAND
;
539 int CCECCommandHandler::HandleSetMenuLanguage(const cec_command
&command
)
541 if (command
.parameters
.size
== 3)
543 CCECBusDevice
*device
= GetDevice(command
.initiator
);
546 cec_menu_language language
;
547 language
.device
= command
.initiator
;
548 for (uint8_t iPtr
= 0; iPtr
< 4; iPtr
++)
549 language
.language
[iPtr
] = command
.parameters
[iPtr
];
550 language
.language
[3] = 0;
551 device
->SetMenuLanguage(language
);
552 return COMMAND_HANDLED
;
556 return CEC_ABORT_REASON_INVALID_OPERAND
;
559 int CCECCommandHandler::HandleSetOSDName(const cec_command
&command
)
561 if (command
.parameters
.size
> 0)
563 CCECBusDevice
*device
= GetDevice(command
.initiator
);
567 for (uint8_t iPtr
= 0; iPtr
< command
.parameters
.size
; iPtr
++)
568 buf
[iPtr
] = (char)command
.parameters
[iPtr
];
569 buf
[command
.parameters
.size
] = 0;
571 CStdString
strName(buf
);
572 device
->SetOSDName(strName
);
574 return COMMAND_HANDLED
;
578 return CEC_ABORT_REASON_INVALID_OPERAND
;
581 int CCECCommandHandler::HandleSetStreamPath(const cec_command
&command
)
583 if (!m_processor
->CECInitialised())
584 return CEC_ABORT_REASON_NOT_IN_CORRECT_MODE_TO_RESPOND
;
586 if (command
.parameters
.size
>= 2)
588 uint16_t iStreamAddress
= ((uint16_t)command
.parameters
[0] << 8) | ((uint16_t)command
.parameters
[1]);
589 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, ">> %s (%x) sets stream path to physical address %04x", ToString(command
.initiator
), command
.initiator
, iStreamAddress
);
591 /* one of the device handled by libCEC has been made active */
592 CCECBusDevice
*device
= GetDeviceByPhysicalAddress(iStreamAddress
);
595 if (device
->IsHandledByLibCEC())
597 if (!device
->IsActiveSource())
598 device
->ActivateSource();
601 device
->MarkAsActiveSource();
602 device
->TransmitActiveSource(true);
605 return COMMAND_HANDLED
;
609 return CEC_ABORT_REASON_INVALID_OPERAND
;
612 int CCECCommandHandler::HandleSystemAudioModeRequest(const cec_command
&command
)
614 if (m_processor
->CECInitialised() && m_processor
->IsHandledByLibCEC(command
.destination
))
616 CCECAudioSystem
*device
= CCECBusDevice::AsAudioSystem(GetDevice(command
.destination
));
619 if (command
.parameters
.size
>= 2)
621 device
->SetPowerStatus(CEC_POWER_STATUS_ON
);
622 device
->SetSystemAudioModeStatus(CEC_SYSTEM_AUDIO_STATUS_ON
);
623 uint16_t iNewAddress
= ((uint16_t)command
.parameters
[0] << 8) | ((uint16_t)command
.parameters
[1]);
624 CCECBusDevice
*newActiveDevice
= GetDeviceByPhysicalAddress(iNewAddress
);
626 newActiveDevice
->MarkAsActiveSource();
627 if (device
->TransmitSetSystemAudioMode(command
.initiator
, true))
628 return COMMAND_HANDLED
;
632 device
->SetSystemAudioModeStatus(CEC_SYSTEM_AUDIO_STATUS_OFF
);
633 if (device
->TransmitSetSystemAudioMode(command
.initiator
, true))
634 return COMMAND_HANDLED
;
639 return CEC_ABORT_REASON_NOT_IN_CORRECT_MODE_TO_RESPOND
;
642 int CCECCommandHandler::HandleStandby(const cec_command
&command
)
644 CCECBusDevice
*device
= GetDevice(command
.initiator
);
646 device
->SetPowerStatus(CEC_POWER_STATUS_STANDBY
);
648 return COMMAND_HANDLED
;
651 int CCECCommandHandler::HandleSystemAudioModeStatus(const cec_command
&command
)
653 if (command
.parameters
.size
== 1)
655 CCECAudioSystem
*device
= CCECBusDevice::AsAudioSystem(GetDevice(command
.initiator
));
658 device
->SetSystemAudioModeStatus((cec_system_audio_status
)command
.parameters
[0]);
659 return COMMAND_HANDLED
;
663 return CEC_ABORT_REASON_INVALID_OPERAND
;
666 int CCECCommandHandler::HandleSetSystemAudioMode(const cec_command
&command
)
668 if (command
.parameters
.size
== 1)
670 CCECAudioSystem
*device
= CCECBusDevice::AsAudioSystem(GetDevice(command
.initiator
));
673 device
->SetSystemAudioModeStatus((cec_system_audio_status
)command
.parameters
[0]);
674 return COMMAND_HANDLED
;
678 return CEC_ABORT_REASON_INVALID_OPERAND
;
681 int CCECCommandHandler::HandleTextViewOn(const cec_command
&command
)
683 m_processor
->GetDevice(command
.initiator
)->MarkAsActiveSource();
684 return COMMAND_HANDLED
;
687 int CCECCommandHandler::HandleUserControlPressed(const cec_command
&command
)
689 if (!m_processor
->CECInitialised() ||
690 !m_processor
->IsHandledByLibCEC(command
.destination
))
691 return CEC_ABORT_REASON_NOT_IN_CORRECT_MODE_TO_RESPOND
;
693 if (command
.parameters
.size
== 0)
694 return CEC_ABORT_REASON_INVALID_OPERAND
;
696 CCECBusDevice
*device
= GetDevice(command
.destination
);
698 return CEC_ABORT_REASON_INVALID_OPERAND
;
700 CCECClient
*client
= device
->GetClient();
702 client
->SetCurrentButton((cec_user_control_code
) command
.parameters
[0]);
704 if (command
.parameters
[0] == CEC_USER_CONTROL_CODE_POWER
||
705 command
.parameters
[0] == CEC_USER_CONTROL_CODE_POWER_ON_FUNCTION
||
706 command
.parameters
[0] == CEC_USER_CONTROL_CODE_POWER_TOGGLE_FUNCTION
)
710 // CEC_USER_CONTROL_CODE_POWER and CEC_USER_CONTROL_CODE_POWER_TOGGLE_FUNCTION operate as a toggle
711 // assume CEC_USER_CONTROL_CODE_POWER_ON_FUNCTION does not
712 if (command
.parameters
[0] == CEC_USER_CONTROL_CODE_POWER
||
713 command
.parameters
[0] == CEC_USER_CONTROL_CODE_POWER_TOGGLE_FUNCTION
)
715 cec_power_status status
= device
->GetCurrentPowerStatus();
716 bPowerOn
= !(status
== CEC_POWER_STATUS_ON
|| status
== CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON
);
721 device
->ActivateSource();
725 device
->MarkAsInactiveSource();
726 device
->TransmitInactiveSource();
727 device
->SetMenuState(CEC_MENU_STATE_DEACTIVATED
);
730 else if (command
.parameters
[0] != CEC_USER_CONTROL_CODE_POWER_OFF_FUNCTION
)
732 // we're not marked as active source, but the tv sends keypresses to us, so assume it forgot to activate us
733 if (!device
->IsActiveSource() && command
.initiator
== CECDEVICE_TV
)
734 device
->MarkAsActiveSource();
737 return COMMAND_HANDLED
;
740 int CCECCommandHandler::HandleUserControlRelease(const cec_command
&command
)
742 if (!m_processor
->CECInitialised() ||
743 !m_processor
->IsHandledByLibCEC(command
.destination
))
744 return CEC_ABORT_REASON_NOT_IN_CORRECT_MODE_TO_RESPOND
;
746 CCECClient
*client
= m_processor
->GetClient(command
.destination
);
750 return COMMAND_HANDLED
;
753 int CCECCommandHandler::HandleVendorCommand(const cec_command
& UNUSED(command
))
755 return CEC_ABORT_REASON_INVALID_OPERAND
;
758 int CCECCommandHandler::HandleVendorRemoteButtonDown(const cec_command
& command
)
760 if (command
.parameters
.size
== 0)
761 return CEC_ABORT_REASON_INVALID_OPERAND
;
763 LIB_CEC
->AddLog(CEC_LOG_NOTICE
, "unhandled vendor remote button received with keycode %x", command
.parameters
[0]);
764 return COMMAND_HANDLED
;
767 void CCECCommandHandler::UnhandledCommand(const cec_command
&command
, const cec_abort_reason reason
)
769 if (m_processor
->IsHandledByLibCEC(command
.destination
))
771 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "sending abort with opcode %02x and reason '%s' to %s", command
.opcode
, ToString(reason
), ToString(command
.initiator
));
772 m_processor
->TransmitAbort(command
.destination
, command
.initiator
, command
.opcode
, reason
);
774 if (reason
== CEC_ABORT_REASON_INVALID_OPERAND
)
775 RequestEmailFromCustomer(command
);
779 size_t CCECCommandHandler::GetMyDevices(vector
<CCECBusDevice
*> &devices
) const
783 cec_logical_addresses addresses
= m_processor
->GetLogicalAddresses();
784 for (uint8_t iPtr
= CECDEVICE_TV
; iPtr
< CECDEVICE_BROADCAST
; iPtr
++)
788 devices
.push_back(GetDevice((cec_logical_address
) iPtr
));
796 CCECBusDevice
*CCECCommandHandler::GetDevice(cec_logical_address iLogicalAddress
) const
798 return m_processor
->GetDevice(iLogicalAddress
);
801 CCECBusDevice
*CCECCommandHandler::GetDeviceByPhysicalAddress(uint16_t iPhysicalAddress
) const
803 return m_processor
->GetDeviceByPhysicalAddress(iPhysicalAddress
);
806 bool CCECCommandHandler::SetVendorId(const cec_command
&command
)
808 bool bChanged(false);
809 if (command
.parameters
.size
< 3)
811 LIB_CEC
->AddLog(CEC_LOG_WARNING
, "invalid vendor ID received");
815 uint64_t iVendorId
= ((uint64_t)command
.parameters
[0] << 16) +
816 ((uint64_t)command
.parameters
[1] << 8) +
817 (uint64_t)command
.parameters
[2];
819 CCECBusDevice
*device
= GetDevice((cec_logical_address
) command
.initiator
);
821 bChanged
= device
->SetVendorId(iVendorId
);
825 void CCECCommandHandler::SetPhysicalAddress(cec_logical_address iAddress
, uint16_t iNewAddress
)
827 if (!m_processor
->IsHandledByLibCEC(iAddress
))
829 CCECBusDevice
*otherDevice
= m_processor
->GetDeviceByPhysicalAddress(iNewAddress
);
830 CCECClient
*client
= otherDevice
? otherDevice
->GetClient() : NULL
;
832 CCECBusDevice
*device
= m_processor
->GetDevice(iAddress
);
834 device
->SetPhysicalAddress(iNewAddress
);
836 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "device with logical address %X not found", iAddress
);
838 /* another device reported the same physical address as ours */
841 libcec_parameter param
;
842 param
.paramType
= CEC_PARAMETER_TYPE_STRING
;
843 param
.paramData
= (void*)"Physical address in use by another device. Please verify your settings";
844 client
->Alert(CEC_ALERT_PHYSICAL_ADDRESS_ERROR
, param
);
845 client
->ResetPhysicalAddress();
850 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
);
854 bool CCECCommandHandler::PowerOn(const cec_logical_address iInitiator
, const cec_logical_address iDestination
)
856 if (iDestination
== CECDEVICE_TV
)
857 return TransmitImageViewOn(iInitiator
, iDestination
);
859 return TransmitKeypress(iInitiator
, iDestination
, CEC_USER_CONTROL_CODE_POWER
) &&
860 TransmitKeyRelease(iInitiator
, iDestination
);
863 bool CCECCommandHandler::TransmitImageViewOn(const cec_logical_address iInitiator
, const cec_logical_address iDestination
)
866 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_IMAGE_VIEW_ON
);
868 if (Transmit(command
, false, false))
870 CCECBusDevice
* dest
= m_processor
->GetDevice(iDestination
);
871 if (dest
&& dest
->GetCurrentPowerStatus() != CEC_POWER_STATUS_ON
)
872 dest
->SetPowerStatus(CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON
);
878 bool CCECCommandHandler::TransmitStandby(const cec_logical_address iInitiator
, const cec_logical_address iDestination
)
881 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_STANDBY
);
883 return Transmit(command
, false, false);
886 bool CCECCommandHandler::TransmitRequestActiveSource(const cec_logical_address iInitiator
, bool bWaitForResponse
/* = true */)
889 cec_command::Format(command
, iInitiator
, CECDEVICE_BROADCAST
, CEC_OPCODE_REQUEST_ACTIVE_SOURCE
);
891 return Transmit(command
, !bWaitForResponse
, false);
894 bool CCECCommandHandler::TransmitRequestCecVersion(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, bool bWaitForResponse
/* = true */)
897 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_GET_CEC_VERSION
);
899 return Transmit(command
, !bWaitForResponse
, false);
902 bool CCECCommandHandler::TransmitRequestMenuLanguage(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, bool bWaitForResponse
/* = true */)
905 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_GET_MENU_LANGUAGE
);
907 return Transmit(command
, !bWaitForResponse
, false);
910 bool CCECCommandHandler::TransmitRequestOSDName(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, bool bWaitForResponse
/* = true */)
913 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_GIVE_OSD_NAME
);
915 return Transmit(command
, !bWaitForResponse
, false);
918 bool CCECCommandHandler::TransmitRequestAudioStatus(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, bool bWaitForResponse
/* = true */)
921 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_GIVE_AUDIO_STATUS
);
923 return Transmit(command
, !bWaitForResponse
, false);
926 bool CCECCommandHandler::TransmitRequestPhysicalAddress(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, bool bWaitForResponse
/* = true */)
929 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_GIVE_PHYSICAL_ADDRESS
);
931 return Transmit(command
, !bWaitForResponse
, false);
934 bool CCECCommandHandler::TransmitRequestPowerStatus(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, bool bUpdate
, bool bWaitForResponse
/* = true */)
936 if (iDestination
== CECDEVICE_TV
)
938 int64_t now(GetTimeMs());
939 if (!bUpdate
&& now
- m_iPowerStatusRequested
< REQUEST_POWER_STATUS_TIMEOUT
)
941 m_iPowerStatusRequested
= now
;
944 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "<< requesting power status of '%s' (%X)", m_busDevice
->GetLogicalAddressName(), iDestination
);
947 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_GIVE_DEVICE_POWER_STATUS
);
949 return Transmit(command
, !bWaitForResponse
, false);
952 bool CCECCommandHandler::TransmitRequestVendorId(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, bool bWaitForResponse
/* = true */)
955 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_GIVE_DEVICE_VENDOR_ID
);
957 return Transmit(command
, !bWaitForResponse
, false);
960 bool CCECCommandHandler::TransmitActiveSource(const cec_logical_address iInitiator
, uint16_t iPhysicalAddress
, bool bIsReply
)
963 cec_command::Format(command
, iInitiator
, CECDEVICE_BROADCAST
, CEC_OPCODE_ACTIVE_SOURCE
);
964 command
.parameters
.PushBack((uint8_t) ((iPhysicalAddress
>> 8) & 0xFF));
965 command
.parameters
.PushBack((uint8_t) (iPhysicalAddress
& 0xFF));
967 return Transmit(command
, false, bIsReply
);
970 bool CCECCommandHandler::TransmitCECVersion(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, cec_version cecVersion
, bool bIsReply
)
973 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_CEC_VERSION
);
974 command
.parameters
.PushBack((uint8_t)cecVersion
);
976 return Transmit(command
, false, bIsReply
);
979 bool CCECCommandHandler::TransmitInactiveSource(const cec_logical_address iInitiator
, uint16_t iPhysicalAddress
)
982 cec_command::Format(command
, iInitiator
, CECDEVICE_TV
, CEC_OPCODE_INACTIVE_SOURCE
);
983 command
.parameters
.PushBack((iPhysicalAddress
>> 8) & 0xFF);
984 command
.parameters
.PushBack(iPhysicalAddress
& 0xFF);
986 return Transmit(command
, false, false);
989 bool CCECCommandHandler::TransmitMenuState(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, cec_menu_state menuState
, bool bIsReply
)
992 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_MENU_STATUS
);
993 command
.parameters
.PushBack((uint8_t)menuState
);
995 return Transmit(command
, false, bIsReply
);
998 bool CCECCommandHandler::TransmitOSDName(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, std::string strDeviceName
, bool bIsReply
)
1000 cec_command command
;
1001 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_SET_OSD_NAME
);
1002 for (size_t iPtr
= 0; iPtr
< strDeviceName
.length(); iPtr
++)
1003 command
.parameters
.PushBack(strDeviceName
.at(iPtr
));
1005 return Transmit(command
, false, bIsReply
);
1008 bool CCECCommandHandler::TransmitOSDString(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, cec_display_control duration
, const char *strMessage
, bool bIsReply
)
1010 cec_command command
;
1011 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_SET_OSD_STRING
);
1012 command
.parameters
.PushBack((uint8_t)duration
);
1014 size_t iLen
= strlen(strMessage
);
1015 if (iLen
> 13) iLen
= 13;
1017 for (size_t iPtr
= 0; iPtr
< iLen
; iPtr
++)
1018 command
.parameters
.PushBack(strMessage
[iPtr
]);
1020 return Transmit(command
, false, bIsReply
);
1023 bool CCECCommandHandler::TransmitPhysicalAddress(const cec_logical_address iInitiator
, uint16_t iPhysicalAddress
, cec_device_type type
, bool bIsReply
)
1025 cec_command command
;
1026 cec_command::Format(command
, iInitiator
, CECDEVICE_BROADCAST
, CEC_OPCODE_REPORT_PHYSICAL_ADDRESS
);
1027 command
.parameters
.PushBack((uint8_t) ((iPhysicalAddress
>> 8) & 0xFF));
1028 command
.parameters
.PushBack((uint8_t) (iPhysicalAddress
& 0xFF));
1029 command
.parameters
.PushBack((uint8_t) (type
));
1031 return Transmit(command
, false, bIsReply
);
1034 bool CCECCommandHandler::TransmitSetMenuLanguage(const cec_logical_address iInitiator
, const char lang
[4], bool bIsReply
)
1036 cec_command command
;
1037 command
.Format(command
, iInitiator
, CECDEVICE_BROADCAST
, CEC_OPCODE_SET_MENU_LANGUAGE
);
1038 command
.parameters
.PushBack((uint8_t) lang
[0]);
1039 command
.parameters
.PushBack((uint8_t) lang
[1]);
1040 command
.parameters
.PushBack((uint8_t) lang
[2]);
1042 return Transmit(command
, false, bIsReply
);
1045 bool CCECCommandHandler::TransmitPoll(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, bool bIsReply
)
1047 cec_command command
;
1048 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_NONE
);
1050 return Transmit(command
, false, bIsReply
);
1053 bool CCECCommandHandler::TransmitPowerState(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, cec_power_status state
, bool bIsReply
)
1055 cec_command command
;
1056 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_REPORT_POWER_STATUS
);
1057 command
.parameters
.PushBack((uint8_t) state
);
1059 return Transmit(command
, false, bIsReply
);
1062 bool CCECCommandHandler::TransmitVendorID(const cec_logical_address iInitiator
, const cec_logical_address
UNUSED(iDestination
), uint64_t iVendorId
, bool bIsReply
)
1064 cec_command command
;
1065 cec_command::Format(command
, iInitiator
, CECDEVICE_BROADCAST
, CEC_OPCODE_DEVICE_VENDOR_ID
);
1067 command
.parameters
.PushBack((uint8_t) (((uint64_t)iVendorId
>> 16) & 0xFF));
1068 command
.parameters
.PushBack((uint8_t) (((uint64_t)iVendorId
>> 8) & 0xFF));
1069 command
.parameters
.PushBack((uint8_t) ((uint64_t)iVendorId
& 0xFF));
1071 return Transmit(command
, false, bIsReply
);
1074 bool CCECCommandHandler::TransmitAudioStatus(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, uint8_t state
, bool bIsReply
)
1076 cec_command command
;
1077 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_REPORT_AUDIO_STATUS
);
1078 command
.parameters
.PushBack(state
);
1080 return Transmit(command
, false, bIsReply
);
1083 bool CCECCommandHandler::TransmitSetSystemAudioMode(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, cec_system_audio_status state
, bool bIsReply
)
1085 cec_command command
;
1086 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_SET_SYSTEM_AUDIO_MODE
);
1087 command
.parameters
.PushBack((uint8_t)state
);
1089 return Transmit(command
, false, bIsReply
);
1092 bool CCECCommandHandler::TransmitSetStreamPath(uint16_t iStreamPath
, bool bIsReply
)
1094 if (m_busDevice
->GetLogicalAddress() != CECDEVICE_TV
)
1096 LIB_CEC
->AddLog(CEC_LOG_ERROR
, "only the TV is allowed to send CEC_OPCODE_SET_STREAM_PATH");
1099 cec_command command
;
1100 cec_command::Format(command
, m_busDevice
->GetLogicalAddress(), CECDEVICE_BROADCAST
, CEC_OPCODE_SET_STREAM_PATH
);
1101 command
.parameters
.PushBack((uint8_t) ((iStreamPath
>> 8) & 0xFF));
1102 command
.parameters
.PushBack((uint8_t) (iStreamPath
& 0xFF));
1104 return Transmit(command
, false, bIsReply
);
1107 bool CCECCommandHandler::TransmitSystemAudioModeStatus(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, cec_system_audio_status state
, bool bIsReply
)
1109 cec_command command
;
1110 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_SYSTEM_AUDIO_MODE_STATUS
);
1111 command
.parameters
.PushBack((uint8_t)state
);
1113 return Transmit(command
, false, bIsReply
);
1116 bool CCECCommandHandler::TransmitDeckStatus(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, cec_deck_info state
, bool bIsReply
)
1118 cec_command command
;
1119 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_DECK_STATUS
);
1120 command
.PushBack((uint8_t)state
);
1122 return Transmit(command
, false, bIsReply
);
1125 bool CCECCommandHandler::TransmitKeypress(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, cec_user_control_code key
, bool bWait
/* = true */)
1127 cec_command command
;
1128 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_USER_CONTROL_PRESSED
);
1129 command
.parameters
.PushBack((uint8_t)key
);
1131 return Transmit(command
, !bWait
, false);
1134 bool CCECCommandHandler::TransmitKeyRelease(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, bool bWait
/* = true */)
1136 cec_command command
;
1137 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_USER_CONTROL_RELEASE
);
1139 return Transmit(command
, !bWait
, false);
1142 bool CCECCommandHandler::Transmit(cec_command
&command
, bool bSuppressWait
, bool bIsReply
)
1144 bool bReturn(false);
1145 cec_opcode
expectedResponse(cec_command::GetResponseOpcode(command
.opcode
));
1146 bool bExpectResponse(expectedResponse
!= CEC_OPCODE_NONE
&& !bSuppressWait
);
1147 command
.transmit_timeout
= m_iTransmitTimeout
;
1149 if (command
.initiator
== CECDEVICE_UNKNOWN
)
1151 LIB_CEC
->AddLog(CEC_LOG_ERROR
, "not transmitting a command without a valid initiator");
1155 // check whether the destination is not marked as not present or handled by libCEC
1156 if (command
.destination
!= CECDEVICE_BROADCAST
&& command
.opcode_set
)
1158 CCECBusDevice
* destinationDevice
= m_processor
->GetDevice(command
.destination
);
1159 cec_bus_device_status status
= destinationDevice
? destinationDevice
->GetStatus() : CEC_DEVICE_STATUS_NOT_PRESENT
;
1160 if (status
== CEC_DEVICE_STATUS_NOT_PRESENT
)
1162 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "not sending command '%s': destination device '%s' marked as not present", ToString(command
.opcode
),ToString(command
.destination
));
1165 else if (status
== CEC_DEVICE_STATUS_HANDLED_BY_LIBCEC
)
1167 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "not sending command '%s': destination device '%s' marked as handled by libCEC", ToString(command
.opcode
),ToString(command
.destination
));
1170 else if (destinationDevice
->IsUnsupportedFeature(command
.opcode
))
1177 uint8_t iTries(0), iMaxTries(m_iTransmitRetries
+ 1);
1178 while (!bReturn
&& ++iTries
<= iMaxTries
)
1180 if ((bReturn
= m_processor
->Transmit(command
, bIsReply
)) == true)
1182 #ifdef CEC_DEBUGGING
1183 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "command transmitted");
1185 if (bExpectResponse
)
1187 bReturn
= m_busDevice
->WaitForOpcode(expectedResponse
);
1188 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, bReturn
? "expected response received (%X: %s)" : "expected response not received (%X: %s)", (int)expectedResponse
, ToString(expectedResponse
));
1197 bool CCECCommandHandler::ActivateSource(bool bTransmitDelayedCommandsOnly
/* = false */)
1199 if (m_busDevice
->IsActiveSource() &&
1200 m_busDevice
->IsHandledByLibCEC())
1203 CLockObject
lock(m_mutex
);
1204 // check if we need to send a delayed source switch
1205 if (bTransmitDelayedCommandsOnly
)
1207 if (m_iActiveSourcePending
== 0 || GetTimeMs() < m_iActiveSourcePending
)
1210 #ifdef CEC_DEBUGGING
1211 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "transmitting delayed activate source command");
1216 // update the power state and menu state
1217 if (!bTransmitDelayedCommandsOnly
)
1219 m_busDevice
->SetPowerStatus(CEC_POWER_STATUS_ON
);
1220 m_busDevice
->SetMenuState(CEC_MENU_STATE_ACTIVATED
);
1223 // vendor specific hook
1224 VendorPreActivateSourceHook();
1227 CCECBusDevice
* tv
= m_processor
->GetDevice(CECDEVICE_TV
);
1228 bool bTvPresent
= (tv
&& tv
->GetStatus() == CEC_DEVICE_STATUS_PRESENT
);
1229 bool bActiveSourceFailed(false);
1231 bActiveSourceFailed
= !tv
->PowerOn(m_busDevice
->GetLogicalAddress());
1233 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "TV not present, not sending 'image view on'");
1235 // check if we're allowed to switch sources
1236 bool bSourceSwitchAllowed
= SourceSwitchAllowed();
1237 if (!bSourceSwitchAllowed
)
1238 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "source switch is currently not allowed by command handler");
1240 // switch sources (if allowed)
1241 if (!bActiveSourceFailed
&& bSourceSwitchAllowed
)
1243 bActiveSourceFailed
= !m_busDevice
->TransmitActiveSource(false);
1244 if (bTvPresent
&& !bActiveSourceFailed
)
1245 bActiveSourceFailed
= !m_busDevice
->TransmitMenuState(CECDEVICE_TV
, false);
1247 // update the deck status for playback devices
1248 if (bTvPresent
&& !bActiveSourceFailed
)
1250 CCECPlaybackDevice
*playbackDevice
= m_busDevice
->AsPlaybackDevice();
1251 if (playbackDevice
&& SendDeckStatusUpdateOnActiveSource())
1252 bActiveSourceFailed
= !playbackDevice
->TransmitDeckStatus(CECDEVICE_TV
, false);
1255 // update system audio mode for audiosystem devices
1256 if (bTvPresent
&& !bActiveSourceFailed
)
1258 CCECAudioSystem
* audioDevice
= m_busDevice
->AsAudioSystem();
1260 bActiveSourceFailed
= !audioDevice
->TransmitSetSystemAudioMode(CECDEVICE_TV
, false);
1265 if (bActiveSourceFailed
|| !bSourceSwitchAllowed
)
1267 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "failed to make '%s' the active source. will retry later", m_busDevice
->GetLogicalAddressName());
1268 int64_t now(GetTimeMs());
1269 CLockObject
lock(m_mutex
);
1270 if (m_iActiveSourcePending
== 0 || m_iActiveSourcePending
< now
)
1271 m_iActiveSourcePending
= now
+ (int64_t)CEC_ACTIVE_SOURCE_SWITCH_RETRY_TIME_MS
;
1276 CLockObject
lock(m_mutex
);
1277 // clear previous pending active source command
1278 m_iActiveSourcePending
= 0;
1281 // mark the handler as initialised
1282 CLockObject
lock(m_mutex
);
1283 m_bHandlerInited
= true;
1288 void CCECCommandHandler::ScheduleActivateSource(uint64_t iDelay
)
1290 CLockObject
lock(m_mutex
);
1291 m_iActiveSourcePending
= GetTimeMs() + iDelay
;
1294 void CCECCommandHandler::RequestEmailFromCustomer(const cec_command
& command
)
1296 bool bInserted(false);
1297 map
<cec_opcode
, vector
<cec_command
> >::iterator it
= m_logsRequested
.find(command
.opcode
);
1298 if (it
!= m_logsRequested
.end())
1300 for (vector
<cec_command
>::const_iterator it2
= it
->second
.begin(); it2
!= it
->second
.end(); it2
++)
1302 // we already logged this one
1303 if ((*it2
).parameters
== command
.parameters
)
1307 it
->second
.push_back(command
);
1313 vector
<cec_command
> commands
;
1314 commands
.push_back(command
);
1315 m_logsRequested
.insert(make_pair(command
.opcode
, commands
));
1318 LIB_CEC
->AddLog(CEC_LOG_NOTICE
, "Unmapped code detected. Please send an email to support@pulse-eight.com with the following details, and if you pressed a key, tell us which one you pressed, and we'll add support for this it.\nCEC command: %s\nVendor ID: %s (%06x)", ToString(command
).c_str(), ToString(m_vendorId
), m_vendorId
);