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
);
260 return COMMAND_HANDLED
;
263 int CCECCommandHandler::HandleFeatureAbort(const cec_command
&command
)
265 if (command
.parameters
.size
== 2 &&
266 (command
.parameters
[1] == CEC_ABORT_REASON_UNRECOGNIZED_OPCODE
||
267 command
.parameters
[1] == CEC_ABORT_REASON_REFUSED
))
268 m_processor
->GetDevice(command
.initiator
)->SetUnsupportedFeature((cec_opcode
)command
.parameters
[0]);
269 return COMMAND_HANDLED
;
272 int CCECCommandHandler::HandleGetCecVersion(const cec_command
&command
)
274 if (m_processor
->CECInitialised() && m_processor
->IsHandledByLibCEC(command
.destination
))
276 CCECBusDevice
*device
= GetDevice(command
.destination
);
277 if (device
&& device
->TransmitCECVersion(command
.initiator
, true))
278 return COMMAND_HANDLED
;
279 return CEC_ABORT_REASON_INVALID_OPERAND
;
282 return CEC_ABORT_REASON_NOT_IN_CORRECT_MODE_TO_RESPOND
;
285 int CCECCommandHandler::HandleGiveAudioStatus(const cec_command
&command
)
287 if (m_processor
->CECInitialised() && m_processor
->IsHandledByLibCEC(command
.destination
))
289 CCECAudioSystem
*device
= CCECBusDevice::AsAudioSystem(GetDevice(command
.destination
));
290 if (device
&& device
->TransmitAudioStatus(command
.initiator
, true))
291 return COMMAND_HANDLED
;
292 return CEC_ABORT_REASON_INVALID_OPERAND
;
295 return CEC_ABORT_REASON_NOT_IN_CORRECT_MODE_TO_RESPOND
;
298 int CCECCommandHandler::HandleGiveDeckStatus(const cec_command
&command
)
300 if (m_processor
->CECInitialised() && m_processor
->IsHandledByLibCEC(command
.destination
))
302 CCECPlaybackDevice
*device
= CCECBusDevice::AsPlaybackDevice(GetDevice(command
.destination
));
303 if (device
&& device
->TransmitDeckStatus(command
.initiator
, true))
304 return COMMAND_HANDLED
;
305 return CEC_ABORT_REASON_INVALID_OPERAND
;
308 return CEC_ABORT_REASON_NOT_IN_CORRECT_MODE_TO_RESPOND
;
311 int CCECCommandHandler::HandleGiveDevicePowerStatus(const cec_command
&command
)
313 if (m_processor
->CECInitialised() && m_processor
->IsHandledByLibCEC(command
.destination
))
315 CCECBusDevice
*device
= GetDevice(command
.destination
);
316 if (device
&& device
->TransmitPowerState(command
.initiator
, true))
317 return COMMAND_HANDLED
;
318 return CEC_ABORT_REASON_INVALID_OPERAND
;
321 return CEC_ABORT_REASON_INVALID_OPERAND
;
324 int CCECCommandHandler::HandleGiveDeviceVendorId(const cec_command
&command
)
326 if (m_processor
->CECInitialised() && m_processor
->IsHandledByLibCEC(command
.destination
))
328 CCECBusDevice
*device
= GetDevice(command
.destination
);
329 if (device
&& device
->TransmitVendorID(command
.initiator
, true, true))
330 return COMMAND_HANDLED
;
333 return CEC_ABORT_REASON_INVALID_OPERAND
;
336 int CCECCommandHandler::HandleGiveOSDName(const cec_command
&command
)
338 if (m_processor
->CECInitialised() && m_processor
->IsHandledByLibCEC(command
.destination
))
340 CCECBusDevice
*device
= GetDevice(command
.destination
);
341 if (device
&& device
->TransmitOSDName(command
.initiator
, true))
342 return COMMAND_HANDLED
;
345 return CEC_ABORT_REASON_INVALID_OPERAND
;
348 int CCECCommandHandler::HandleGivePhysicalAddress(const cec_command
&command
)
350 if (m_processor
->CECInitialised() && m_processor
->IsHandledByLibCEC(command
.destination
))
352 CCECBusDevice
*device
= GetDevice(command
.destination
);
353 if (device
&& device
->TransmitPhysicalAddress(true))
354 return COMMAND_HANDLED
;
355 return CEC_ABORT_REASON_INVALID_OPERAND
;
358 return CEC_ABORT_REASON_NOT_IN_CORRECT_MODE_TO_RESPOND
;
361 int CCECCommandHandler::HandleGiveMenuLanguage(const cec_command
&command
)
363 if (m_processor
->CECInitialised() && m_processor
->IsHandledByLibCEC(command
.destination
))
365 CCECBusDevice
*device
= GetDevice(command
.destination
);
366 if (device
&& device
->TransmitSetMenuLanguage(command
.initiator
, true))
367 return COMMAND_HANDLED
;
368 return CEC_ABORT_REASON_INVALID_OPERAND
;
371 return CEC_ABORT_REASON_NOT_IN_CORRECT_MODE_TO_RESPOND
;
374 int CCECCommandHandler::HandleGiveSystemAudioModeStatus(const cec_command
&command
)
376 if (m_processor
->CECInitialised() && m_processor
->IsHandledByLibCEC(command
.destination
))
378 CCECAudioSystem
*device
= CCECBusDevice::AsAudioSystem(GetDevice(command
.destination
));
379 if (device
&& device
->TransmitSystemAudioModeStatus(command
.initiator
, true))
380 return COMMAND_HANDLED
;
381 return CEC_ABORT_REASON_INVALID_OPERAND
;
384 return CEC_ABORT_REASON_NOT_IN_CORRECT_MODE_TO_RESPOND
;
387 int CCECCommandHandler::HandleImageViewOn(const cec_command
&command
)
389 CCECBusDevice
*device
= GetDevice(command
.destination
);
390 if (device
&& device
->GetCurrentStatus() == CEC_DEVICE_STATUS_PRESENT
)
392 if (device
->GetCurrentPowerStatus() == CEC_POWER_STATUS_STANDBY
||
393 device
->GetCurrentPowerStatus() == CEC_POWER_STATUS_IN_TRANSITION_ON_TO_STANDBY
)
394 device
->SetPowerStatus(CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON
);
395 CCECBusDevice
* tv
= GetDevice(CECDEVICE_TV
);
397 tv
->OnImageViewOnSent(false);
399 return COMMAND_HANDLED
;
402 int CCECCommandHandler::HandleMenuRequest(const cec_command
&command
)
404 if (m_processor
->CECInitialised() && m_processor
->IsHandledByLibCEC(command
.destination
))
406 CCECBusDevice
*device
= GetDevice(command
.destination
);
409 CCECClient
*client
= device
->GetClient();
412 if (command
.parameters
[0] == CEC_MENU_REQUEST_TYPE_ACTIVATE
)
414 if (client
->MenuStateChanged(CEC_MENU_STATE_ACTIVATED
) == 1)
415 device
->SetMenuState(CEC_MENU_STATE_ACTIVATED
);
417 else if (command
.parameters
[0] == CEC_MENU_REQUEST_TYPE_DEACTIVATE
)
419 if (client
->MenuStateChanged(CEC_MENU_STATE_DEACTIVATED
) == 1)
420 device
->SetMenuState(CEC_MENU_STATE_DEACTIVATED
);
423 if (device
->TransmitMenuState(command
.initiator
, true))
424 return COMMAND_HANDLED
;
426 return CEC_ABORT_REASON_INVALID_OPERAND
;
429 return CEC_ABORT_REASON_NOT_IN_CORRECT_MODE_TO_RESPOND
;
432 bool CCECCommandHandler::HandlePoll(const cec_command
&command
)
434 m_busDevice
->HandlePoll(command
.destination
);
438 int CCECCommandHandler::HandleReportAudioStatus(const cec_command
&command
)
440 if (command
.parameters
.size
== 1)
442 CCECAudioSystem
*device
= CCECBusDevice::AsAudioSystem(GetDevice(command
.initiator
));
445 device
->SetAudioStatus(command
.parameters
[0]);
446 return COMMAND_HANDLED
;
449 return CEC_ABORT_REASON_INVALID_OPERAND
;
452 int CCECCommandHandler::HandleReportPhysicalAddress(const cec_command
&command
)
454 if (command
.parameters
.size
== 3)
456 uint16_t iNewAddress
= ((uint16_t)command
.parameters
[0] << 8) | ((uint16_t)command
.parameters
[1]);
457 SetPhysicalAddress(command
.initiator
, iNewAddress
);
458 return COMMAND_HANDLED
;
460 return CEC_ABORT_REASON_INVALID_OPERAND
;
463 int CCECCommandHandler::HandleReportPowerStatus(const cec_command
&command
)
465 if (command
.parameters
.size
== 1)
467 CCECBusDevice
*device
= GetDevice(command
.initiator
);
470 device
->SetPowerStatus((cec_power_status
) command
.parameters
[0]);
471 return COMMAND_HANDLED
;
474 return CEC_ABORT_REASON_INVALID_OPERAND
;
477 int CCECCommandHandler::HandleRequestActiveSource(const cec_command
&command
)
479 if (m_processor
->CECInitialised())
481 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, ">> %i requests active source", (uint8_t) command
.initiator
);
482 m_processor
->GetDevice(command
.initiator
)->SetPowerStatus(CEC_POWER_STATUS_ON
);
484 vector
<CCECBusDevice
*> devices
;
485 for (size_t iDevicePtr
= 0; iDevicePtr
< GetMyDevices(devices
); iDevicePtr
++)
486 devices
[iDevicePtr
]->TransmitActiveSource(true);
489 return COMMAND_HANDLED
;
492 int CCECCommandHandler::HandleRoutingChange(const cec_command
&command
)
494 if (command
.parameters
.size
== 4)
496 CCECBusDevice
*device
= GetDevice(command
.initiator
);
499 uint16_t iNewAddress
= ((uint16_t)command
.parameters
[2] << 8) | ((uint16_t)command
.parameters
[3]);
500 device
->SetActiveRoute(iNewAddress
);
501 return COMMAND_HANDLED
;
505 return CEC_ABORT_REASON_INVALID_OPERAND
;
508 int CCECCommandHandler::HandleRoutingInformation(const cec_command
&command
)
510 if (command
.parameters
.size
== 2)
512 CCECBusDevice
*device
= GetDevice(command
.initiator
);
515 uint16_t iNewAddress
= ((uint16_t)command
.parameters
[0] << 8) | ((uint16_t)command
.parameters
[1]);
516 device
->SetActiveRoute(iNewAddress
);
517 return COMMAND_HANDLED
;
521 return CEC_ABORT_REASON_INVALID_OPERAND
;
524 int CCECCommandHandler::HandleSetMenuLanguage(const cec_command
&command
)
526 if (command
.parameters
.size
== 3)
528 CCECBusDevice
*device
= GetDevice(command
.initiator
);
531 cec_menu_language language
;
532 language
.device
= command
.initiator
;
533 for (uint8_t iPtr
= 0; iPtr
< 4; iPtr
++)
534 language
.language
[iPtr
] = command
.parameters
[iPtr
];
535 language
.language
[3] = 0;
536 device
->SetMenuLanguage(language
);
537 return COMMAND_HANDLED
;
541 return CEC_ABORT_REASON_INVALID_OPERAND
;
544 int CCECCommandHandler::HandleSetOSDName(const cec_command
&command
)
546 if (command
.parameters
.size
> 0)
548 CCECBusDevice
*device
= GetDevice(command
.initiator
);
552 for (uint8_t iPtr
= 0; iPtr
< command
.parameters
.size
; iPtr
++)
553 buf
[iPtr
] = (char)command
.parameters
[iPtr
];
554 buf
[command
.parameters
.size
] = 0;
556 CStdString
strName(buf
);
557 device
->SetOSDName(strName
);
559 return COMMAND_HANDLED
;
563 return CEC_ABORT_REASON_INVALID_OPERAND
;
566 int CCECCommandHandler::HandleSetStreamPath(const cec_command
&command
)
568 if (!m_processor
->CECInitialised())
569 return CEC_ABORT_REASON_NOT_IN_CORRECT_MODE_TO_RESPOND
;
571 if (command
.parameters
.size
>= 2)
573 uint16_t iStreamAddress
= ((uint16_t)command
.parameters
[0] << 8) | ((uint16_t)command
.parameters
[1]);
574 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, ">> %s (%x) sets stream path to physical address %04x", ToString(command
.initiator
), command
.initiator
, iStreamAddress
);
576 /* one of the device handled by libCEC has been made active */
577 CCECBusDevice
*device
= GetDeviceByPhysicalAddress(iStreamAddress
);
580 if (device
->IsHandledByLibCEC() && !device
->IsActiveSource())
581 device
->ActivateSource();
583 device
->MarkAsActiveSource();
584 return COMMAND_HANDLED
;
588 cec_logical_address previousSource
= m_processor
->GetActiveSource(false);
589 CCECBusDevice
* device
= m_processor
->GetDevice(previousSource
);
590 if (device
&& device
->GetCurrentPhysicalAddress() != iStreamAddress
)
591 device
->MarkAsInactiveSource();
595 return CEC_ABORT_REASON_INVALID_OPERAND
;
598 int CCECCommandHandler::HandleSystemAudioModeRequest(const cec_command
&command
)
600 if (m_processor
->CECInitialised() && m_processor
->IsHandledByLibCEC(command
.destination
))
602 CCECAudioSystem
*device
= CCECBusDevice::AsAudioSystem(GetDevice(command
.destination
));
605 if (command
.parameters
.size
>= 2)
607 device
->SetPowerStatus(CEC_POWER_STATUS_ON
);
608 device
->SetSystemAudioModeStatus(CEC_SYSTEM_AUDIO_STATUS_ON
);
609 uint16_t iNewAddress
= ((uint16_t)command
.parameters
[0] << 8) | ((uint16_t)command
.parameters
[1]);
610 CCECBusDevice
*newActiveDevice
= GetDeviceByPhysicalAddress(iNewAddress
);
612 newActiveDevice
->MarkAsActiveSource();
613 if (device
->TransmitSetSystemAudioMode(command
.initiator
, true))
614 return COMMAND_HANDLED
;
618 device
->SetSystemAudioModeStatus(CEC_SYSTEM_AUDIO_STATUS_OFF
);
619 if (device
->TransmitSetSystemAudioMode(command
.initiator
, true))
620 return COMMAND_HANDLED
;
625 return CEC_ABORT_REASON_NOT_IN_CORRECT_MODE_TO_RESPOND
;
628 int CCECCommandHandler::HandleStandby(const cec_command
&command
)
630 CCECBusDevice
*device
= GetDevice(command
.initiator
);
632 device
->SetPowerStatus(CEC_POWER_STATUS_STANDBY
);
634 return COMMAND_HANDLED
;
637 int CCECCommandHandler::HandleSystemAudioModeStatus(const cec_command
&command
)
639 if (command
.parameters
.size
== 1)
641 CCECAudioSystem
*device
= CCECBusDevice::AsAudioSystem(GetDevice(command
.initiator
));
644 device
->SetSystemAudioModeStatus((cec_system_audio_status
)command
.parameters
[0]);
645 return COMMAND_HANDLED
;
649 return CEC_ABORT_REASON_INVALID_OPERAND
;
652 int CCECCommandHandler::HandleSetSystemAudioMode(const cec_command
&command
)
654 if (command
.parameters
.size
== 1)
656 CCECAudioSystem
*device
= CCECBusDevice::AsAudioSystem(GetDevice(command
.initiator
));
659 device
->SetSystemAudioModeStatus((cec_system_audio_status
)command
.parameters
[0]);
660 return COMMAND_HANDLED
;
664 return CEC_ABORT_REASON_INVALID_OPERAND
;
667 int CCECCommandHandler::HandleTextViewOn(const cec_command
&command
)
669 m_processor
->GetDevice(command
.initiator
)->MarkAsActiveSource();
670 return COMMAND_HANDLED
;
673 int CCECCommandHandler::HandleUserControlPressed(const cec_command
&command
)
675 if (!m_processor
->CECInitialised() ||
676 !m_processor
->IsHandledByLibCEC(command
.destination
))
677 return CEC_ABORT_REASON_NOT_IN_CORRECT_MODE_TO_RESPOND
;
679 if (command
.parameters
.size
== 0)
680 return CEC_ABORT_REASON_INVALID_OPERAND
;
682 CCECBusDevice
*device
= GetDevice(command
.destination
);
684 return CEC_ABORT_REASON_INVALID_OPERAND
;
686 CCECClient
*client
= device
->GetClient();
688 client
->SetCurrentButton((cec_user_control_code
) command
.parameters
[0]);
690 if (command
.parameters
[0] == CEC_USER_CONTROL_CODE_POWER
||
691 command
.parameters
[0] == CEC_USER_CONTROL_CODE_POWER_ON_FUNCTION
||
692 command
.parameters
[0] == CEC_USER_CONTROL_CODE_POWER_TOGGLE_FUNCTION
)
696 // CEC_USER_CONTROL_CODE_POWER and CEC_USER_CONTROL_CODE_POWER_TOGGLE_FUNCTION operate as a toggle
697 // assume CEC_USER_CONTROL_CODE_POWER_ON_FUNCTION does not
698 if (command
.parameters
[0] == CEC_USER_CONTROL_CODE_POWER
||
699 command
.parameters
[0] == CEC_USER_CONTROL_CODE_POWER_TOGGLE_FUNCTION
)
701 cec_power_status status
= device
->GetCurrentPowerStatus();
702 bPowerOn
= !(status
== CEC_POWER_STATUS_ON
|| status
== CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON
);
707 device
->ActivateSource();
711 device
->MarkAsInactiveSource();
712 device
->TransmitInactiveSource();
713 device
->SetMenuState(CEC_MENU_STATE_DEACTIVATED
);
716 else if (command
.parameters
[0] != CEC_USER_CONTROL_CODE_POWER_OFF_FUNCTION
)
718 // we're not marked as active source, but the tv sends keypresses to us, so assume it forgot to activate us
719 if (!device
->IsActiveSource() && command
.initiator
== CECDEVICE_TV
)
720 device
->MarkAsActiveSource();
723 return COMMAND_HANDLED
;
726 int CCECCommandHandler::HandleUserControlRelease(const cec_command
&command
)
728 if (!m_processor
->CECInitialised() ||
729 !m_processor
->IsHandledByLibCEC(command
.destination
))
730 return CEC_ABORT_REASON_NOT_IN_CORRECT_MODE_TO_RESPOND
;
732 CCECClient
*client
= m_processor
->GetClient(command
.destination
);
736 return COMMAND_HANDLED
;
739 int CCECCommandHandler::HandleVendorCommand(const cec_command
& UNUSED(command
))
741 return CEC_ABORT_REASON_INVALID_OPERAND
;
744 int CCECCommandHandler::HandleVendorRemoteButtonDown(const cec_command
& command
)
746 if (command
.parameters
.size
== 0)
747 return CEC_ABORT_REASON_INVALID_OPERAND
;
749 LIB_CEC
->AddLog(CEC_LOG_NOTICE
, "unhandled vendor remote button received with keycode %x", command
.parameters
[0]);
750 return COMMAND_HANDLED
;
753 void CCECCommandHandler::UnhandledCommand(const cec_command
&command
, const cec_abort_reason reason
)
755 if (m_processor
->IsHandledByLibCEC(command
.destination
))
757 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "sending abort with opcode %02x and reason '%s' to %s", command
.opcode
, ToString(reason
), ToString(command
.initiator
));
758 m_processor
->TransmitAbort(command
.destination
, command
.initiator
, command
.opcode
, reason
);
760 if (reason
== CEC_ABORT_REASON_INVALID_OPERAND
)
761 RequestEmailFromCustomer(command
);
765 size_t CCECCommandHandler::GetMyDevices(vector
<CCECBusDevice
*> &devices
) const
769 cec_logical_addresses addresses
= m_processor
->GetLogicalAddresses();
770 for (uint8_t iPtr
= CECDEVICE_TV
; iPtr
< CECDEVICE_BROADCAST
; iPtr
++)
774 devices
.push_back(GetDevice((cec_logical_address
) iPtr
));
782 CCECBusDevice
*CCECCommandHandler::GetDevice(cec_logical_address iLogicalAddress
) const
784 return m_processor
->GetDevice(iLogicalAddress
);
787 CCECBusDevice
*CCECCommandHandler::GetDeviceByPhysicalAddress(uint16_t iPhysicalAddress
) const
789 return m_processor
->GetDeviceByPhysicalAddress(iPhysicalAddress
);
792 bool CCECCommandHandler::SetVendorId(const cec_command
&command
)
794 bool bChanged(false);
795 if (command
.parameters
.size
< 3)
797 LIB_CEC
->AddLog(CEC_LOG_WARNING
, "invalid vendor ID received");
801 uint64_t iVendorId
= ((uint64_t)command
.parameters
[0] << 16) +
802 ((uint64_t)command
.parameters
[1] << 8) +
803 (uint64_t)command
.parameters
[2];
805 CCECBusDevice
*device
= GetDevice((cec_logical_address
) command
.initiator
);
807 bChanged
= device
->SetVendorId(iVendorId
);
811 void CCECCommandHandler::SetPhysicalAddress(cec_logical_address iAddress
, uint16_t iNewAddress
)
813 if (!m_processor
->IsHandledByLibCEC(iAddress
))
815 CCECBusDevice
*otherDevice
= m_processor
->GetDeviceByPhysicalAddress(iNewAddress
);
816 CCECClient
*client
= otherDevice
? otherDevice
->GetClient() : NULL
;
818 CCECBusDevice
*device
= m_processor
->GetDevice(iAddress
);
820 device
->SetPhysicalAddress(iNewAddress
);
822 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "device with logical address %X not found", iAddress
);
824 /* another device reported the same physical address as ours */
827 libcec_parameter param
;
828 param
.paramType
= CEC_PARAMETER_TYPE_STRING
;
829 param
.paramData
= (void*)"Physical address in use by another device. Please verify your settings";
830 client
->Alert(CEC_ALERT_PHYSICAL_ADDRESS_ERROR
, param
);
831 client
->ResetPhysicalAddress();
836 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
);
840 bool CCECCommandHandler::PowerOn(const cec_logical_address iInitiator
, const cec_logical_address iDestination
)
842 if (iDestination
== CECDEVICE_TV
)
843 return TransmitImageViewOn(iInitiator
, iDestination
);
845 return TransmitKeypress(iInitiator
, iDestination
, CEC_USER_CONTROL_CODE_POWER
) &&
846 TransmitKeyRelease(iInitiator
, iDestination
);
849 bool CCECCommandHandler::TransmitImageViewOn(const cec_logical_address iInitiator
, const cec_logical_address iDestination
)
852 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_IMAGE_VIEW_ON
);
854 if (Transmit(command
, false, false))
856 CCECBusDevice
* dest
= m_processor
->GetDevice(iDestination
);
857 if (dest
&& dest
->GetCurrentPowerStatus() != CEC_POWER_STATUS_ON
)
858 dest
->SetPowerStatus(CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON
);
864 bool CCECCommandHandler::TransmitStandby(const cec_logical_address iInitiator
, const cec_logical_address iDestination
)
867 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_STANDBY
);
869 return Transmit(command
, false, false);
872 bool CCECCommandHandler::TransmitRequestActiveSource(const cec_logical_address iInitiator
, bool bWaitForResponse
/* = true */)
875 cec_command::Format(command
, iInitiator
, CECDEVICE_BROADCAST
, CEC_OPCODE_REQUEST_ACTIVE_SOURCE
);
877 return Transmit(command
, !bWaitForResponse
, false);
880 bool CCECCommandHandler::TransmitRequestCecVersion(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, bool bWaitForResponse
/* = true */)
883 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_GET_CEC_VERSION
);
885 return Transmit(command
, !bWaitForResponse
, false);
888 bool CCECCommandHandler::TransmitRequestMenuLanguage(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, bool bWaitForResponse
/* = true */)
891 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_GET_MENU_LANGUAGE
);
893 return Transmit(command
, !bWaitForResponse
, false);
896 bool CCECCommandHandler::TransmitRequestOSDName(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, bool bWaitForResponse
/* = true */)
899 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_GIVE_OSD_NAME
);
901 return Transmit(command
, !bWaitForResponse
, false);
904 bool CCECCommandHandler::TransmitRequestAudioStatus(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, bool bWaitForResponse
/* = true */)
907 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_GIVE_AUDIO_STATUS
);
909 return Transmit(command
, !bWaitForResponse
, false);
912 bool CCECCommandHandler::TransmitRequestPhysicalAddress(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, bool bWaitForResponse
/* = true */)
915 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_GIVE_PHYSICAL_ADDRESS
);
917 return Transmit(command
, !bWaitForResponse
, false);
920 bool CCECCommandHandler::TransmitRequestPowerStatus(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, bool bWaitForResponse
/* = true */)
922 if (iDestination
== CECDEVICE_TV
)
924 int64_t now(GetTimeMs());
925 if (now
- m_iPowerStatusRequested
< REQUEST_POWER_STATUS_TIMEOUT
)
927 m_iPowerStatusRequested
= now
;
930 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "<< requesting power status of '%s' (%X)", m_busDevice
->GetLogicalAddressName(), iDestination
);
933 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_GIVE_DEVICE_POWER_STATUS
);
935 return Transmit(command
, !bWaitForResponse
, false);
938 bool CCECCommandHandler::TransmitRequestVendorId(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, bool bWaitForResponse
/* = true */)
941 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_GIVE_DEVICE_VENDOR_ID
);
943 return Transmit(command
, !bWaitForResponse
, false);
946 bool CCECCommandHandler::TransmitActiveSource(const cec_logical_address iInitiator
, uint16_t iPhysicalAddress
, bool bIsReply
)
949 cec_command::Format(command
, iInitiator
, CECDEVICE_BROADCAST
, CEC_OPCODE_ACTIVE_SOURCE
);
950 command
.parameters
.PushBack((uint8_t) ((iPhysicalAddress
>> 8) & 0xFF));
951 command
.parameters
.PushBack((uint8_t) (iPhysicalAddress
& 0xFF));
953 return Transmit(command
, false, bIsReply
);
956 bool CCECCommandHandler::TransmitCECVersion(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, cec_version cecVersion
, bool bIsReply
)
959 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_CEC_VERSION
);
960 command
.parameters
.PushBack((uint8_t)cecVersion
);
962 return Transmit(command
, false, bIsReply
);
965 bool CCECCommandHandler::TransmitInactiveSource(const cec_logical_address iInitiator
, uint16_t iPhysicalAddress
)
968 cec_command::Format(command
, iInitiator
, CECDEVICE_TV
, CEC_OPCODE_INACTIVE_SOURCE
);
969 command
.parameters
.PushBack((iPhysicalAddress
>> 8) & 0xFF);
970 command
.parameters
.PushBack(iPhysicalAddress
& 0xFF);
972 return Transmit(command
, false, false);
975 bool CCECCommandHandler::TransmitMenuState(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, cec_menu_state menuState
, bool bIsReply
)
978 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_MENU_STATUS
);
979 command
.parameters
.PushBack((uint8_t)menuState
);
981 return Transmit(command
, false, bIsReply
);
984 bool CCECCommandHandler::TransmitOSDName(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, std::string strDeviceName
, bool bIsReply
)
987 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_SET_OSD_NAME
);
988 for (size_t iPtr
= 0; iPtr
< strDeviceName
.length(); iPtr
++)
989 command
.parameters
.PushBack(strDeviceName
.at(iPtr
));
991 return Transmit(command
, false, bIsReply
);
994 bool CCECCommandHandler::TransmitOSDString(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, cec_display_control duration
, const char *strMessage
, bool bIsReply
)
997 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_SET_OSD_STRING
);
998 command
.parameters
.PushBack((uint8_t)duration
);
1000 size_t iLen
= strlen(strMessage
);
1001 if (iLen
> 13) iLen
= 13;
1003 for (size_t iPtr
= 0; iPtr
< iLen
; iPtr
++)
1004 command
.parameters
.PushBack(strMessage
[iPtr
]);
1006 return Transmit(command
, false, bIsReply
);
1009 bool CCECCommandHandler::TransmitPhysicalAddress(const cec_logical_address iInitiator
, uint16_t iPhysicalAddress
, cec_device_type type
, bool bIsReply
)
1011 cec_command command
;
1012 cec_command::Format(command
, iInitiator
, CECDEVICE_BROADCAST
, CEC_OPCODE_REPORT_PHYSICAL_ADDRESS
);
1013 command
.parameters
.PushBack((uint8_t) ((iPhysicalAddress
>> 8) & 0xFF));
1014 command
.parameters
.PushBack((uint8_t) (iPhysicalAddress
& 0xFF));
1015 command
.parameters
.PushBack((uint8_t) (type
));
1017 return Transmit(command
, false, bIsReply
);
1020 bool CCECCommandHandler::TransmitSetMenuLanguage(const cec_logical_address iInitiator
, const char lang
[4], bool bIsReply
)
1022 cec_command command
;
1023 command
.Format(command
, iInitiator
, CECDEVICE_BROADCAST
, CEC_OPCODE_SET_MENU_LANGUAGE
);
1024 command
.parameters
.PushBack((uint8_t) lang
[0]);
1025 command
.parameters
.PushBack((uint8_t) lang
[1]);
1026 command
.parameters
.PushBack((uint8_t) lang
[2]);
1028 return Transmit(command
, false, bIsReply
);
1031 bool CCECCommandHandler::TransmitPoll(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, bool bIsReply
)
1033 cec_command command
;
1034 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_NONE
);
1036 return Transmit(command
, false, bIsReply
);
1039 bool CCECCommandHandler::TransmitPowerState(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, cec_power_status state
, bool bIsReply
)
1041 cec_command command
;
1042 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_REPORT_POWER_STATUS
);
1043 command
.parameters
.PushBack((uint8_t) state
);
1045 return Transmit(command
, false, bIsReply
);
1048 bool CCECCommandHandler::TransmitVendorID(const cec_logical_address iInitiator
, const cec_logical_address
UNUSED(iDestination
), uint64_t iVendorId
, bool bIsReply
)
1050 cec_command command
;
1051 cec_command::Format(command
, iInitiator
, CECDEVICE_BROADCAST
, CEC_OPCODE_DEVICE_VENDOR_ID
);
1053 command
.parameters
.PushBack((uint8_t) (((uint64_t)iVendorId
>> 16) & 0xFF));
1054 command
.parameters
.PushBack((uint8_t) (((uint64_t)iVendorId
>> 8) & 0xFF));
1055 command
.parameters
.PushBack((uint8_t) ((uint64_t)iVendorId
& 0xFF));
1057 return Transmit(command
, false, bIsReply
);
1060 bool CCECCommandHandler::TransmitAudioStatus(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, uint8_t state
, bool bIsReply
)
1062 cec_command command
;
1063 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_REPORT_AUDIO_STATUS
);
1064 command
.parameters
.PushBack(state
);
1066 return Transmit(command
, false, bIsReply
);
1069 bool CCECCommandHandler::TransmitSetSystemAudioMode(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, cec_system_audio_status state
, bool bIsReply
)
1071 cec_command command
;
1072 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_SET_SYSTEM_AUDIO_MODE
);
1073 command
.parameters
.PushBack((uint8_t)state
);
1075 return Transmit(command
, false, bIsReply
);
1078 bool CCECCommandHandler::TransmitSetStreamPath(uint16_t iStreamPath
, bool bIsReply
)
1080 if (m_busDevice
->GetLogicalAddress() != CECDEVICE_TV
)
1082 LIB_CEC
->AddLog(CEC_LOG_ERROR
, "only the TV is allowed to send CEC_OPCODE_SET_STREAM_PATH");
1085 cec_command command
;
1086 cec_command::Format(command
, m_busDevice
->GetLogicalAddress(), CECDEVICE_BROADCAST
, CEC_OPCODE_SET_STREAM_PATH
);
1087 command
.parameters
.PushBack((uint8_t) ((iStreamPath
>> 8) & 0xFF));
1088 command
.parameters
.PushBack((uint8_t) (iStreamPath
& 0xFF));
1090 return Transmit(command
, false, bIsReply
);
1093 bool CCECCommandHandler::TransmitSystemAudioModeStatus(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, cec_system_audio_status state
, bool bIsReply
)
1095 cec_command command
;
1096 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_SYSTEM_AUDIO_MODE_STATUS
);
1097 command
.parameters
.PushBack((uint8_t)state
);
1099 return Transmit(command
, false, bIsReply
);
1102 bool CCECCommandHandler::TransmitDeckStatus(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, cec_deck_info state
, bool bIsReply
)
1104 cec_command command
;
1105 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_DECK_STATUS
);
1106 command
.PushBack((uint8_t)state
);
1108 return Transmit(command
, false, bIsReply
);
1111 bool CCECCommandHandler::TransmitKeypress(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, cec_user_control_code key
, bool bWait
/* = true */)
1113 cec_command command
;
1114 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_USER_CONTROL_PRESSED
);
1115 command
.parameters
.PushBack((uint8_t)key
);
1117 return Transmit(command
, !bWait
, false);
1120 bool CCECCommandHandler::TransmitKeyRelease(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, bool bWait
/* = true */)
1122 cec_command command
;
1123 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_USER_CONTROL_RELEASE
);
1125 return Transmit(command
, !bWait
, false);
1128 bool CCECCommandHandler::Transmit(cec_command
&command
, bool bSuppressWait
, bool bIsReply
)
1130 bool bReturn(false);
1131 cec_opcode
expectedResponse(cec_command::GetResponseOpcode(command
.opcode
));
1132 bool bExpectResponse(expectedResponse
!= CEC_OPCODE_NONE
&& !bSuppressWait
);
1133 command
.transmit_timeout
= m_iTransmitTimeout
;
1135 if (command
.initiator
== CECDEVICE_UNKNOWN
)
1137 LIB_CEC
->AddLog(CEC_LOG_ERROR
, "not transmitting a command without a valid initiator");
1141 // check whether the destination is not marked as not present or handled by libCEC
1142 if (command
.destination
!= CECDEVICE_BROADCAST
&& command
.opcode_set
)
1144 CCECBusDevice
* destinationDevice
= m_processor
->GetDevice(command
.destination
);
1145 cec_bus_device_status status
= destinationDevice
? destinationDevice
->GetStatus() : CEC_DEVICE_STATUS_NOT_PRESENT
;
1146 if (status
== CEC_DEVICE_STATUS_NOT_PRESENT
)
1148 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "not sending command '%s': destination device '%s' marked as not present", ToString(command
.opcode
),ToString(command
.destination
));
1151 else if (status
== CEC_DEVICE_STATUS_HANDLED_BY_LIBCEC
)
1153 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "not sending command '%s': destination device '%s' marked as handled by libCEC", ToString(command
.opcode
),ToString(command
.destination
));
1156 else if (destinationDevice
->IsUnsupportedFeature(command
.opcode
))
1163 uint8_t iTries(0), iMaxTries(m_iTransmitRetries
+ 1);
1164 while (!bReturn
&& ++iTries
<= iMaxTries
)
1166 if ((bReturn
= m_processor
->Transmit(command
, bIsReply
)) == true)
1168 #ifdef CEC_DEBUGGING
1169 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "command transmitted");
1171 if (bExpectResponse
)
1173 bReturn
= m_busDevice
->WaitForOpcode(expectedResponse
);
1174 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, bReturn
? "expected response received (%X: %s)" : "expected response not received (%X: %s)", (int)expectedResponse
, ToString(expectedResponse
));
1183 bool CCECCommandHandler::ActivateSource(bool bTransmitDelayedCommandsOnly
/* = false */)
1185 if (m_busDevice
->IsActiveSource() &&
1186 m_busDevice
->IsHandledByLibCEC())
1189 CLockObject
lock(m_mutex
);
1190 // check if we need to send a delayed source switch
1191 if (bTransmitDelayedCommandsOnly
)
1193 if (m_iActiveSourcePending
== 0 || GetTimeMs() < m_iActiveSourcePending
)
1196 #ifdef CEC_DEBUGGING
1197 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "transmitting delayed activate source command");
1202 // update the power state and menu state
1203 if (!bTransmitDelayedCommandsOnly
)
1205 m_busDevice
->SetPowerStatus(CEC_POWER_STATUS_ON
);
1206 m_busDevice
->SetMenuState(CEC_MENU_STATE_ACTIVATED
);
1209 // vendor specific hook
1210 VendorPreActivateSourceHook();
1213 CCECBusDevice
* tv
= m_processor
->GetDevice(CECDEVICE_TV
);
1214 bool bTvPresent
= (tv
&& tv
->GetStatus() == CEC_DEVICE_STATUS_PRESENT
);
1215 bool bActiveSourceFailed(false);
1217 bActiveSourceFailed
= !m_busDevice
->TransmitImageViewOn();
1219 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "TV not present, not sending 'image view on'");
1221 // check if we're allowed to switch sources
1222 bool bSourceSwitchAllowed
= SourceSwitchAllowed();
1223 if (!bSourceSwitchAllowed
)
1224 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "source switch is currently not allowed by command handler");
1226 // switch sources (if allowed)
1227 if (!bActiveSourceFailed
&& bSourceSwitchAllowed
)
1229 bActiveSourceFailed
= !m_busDevice
->TransmitActiveSource(false);
1230 if (bTvPresent
&& !bActiveSourceFailed
)
1231 bActiveSourceFailed
= !m_busDevice
->TransmitMenuState(CECDEVICE_TV
, false);
1233 // update the deck status for playback devices
1234 if (bTvPresent
&& !bActiveSourceFailed
)
1236 CCECPlaybackDevice
*playbackDevice
= m_busDevice
->AsPlaybackDevice();
1237 if (playbackDevice
&& SendDeckStatusUpdateOnActiveSource())
1238 bActiveSourceFailed
= !playbackDevice
->TransmitDeckStatus(CECDEVICE_TV
, false);
1243 if (bActiveSourceFailed
|| !bSourceSwitchAllowed
)
1245 LIB_CEC
->AddLog(CEC_LOG_DEBUG
, "failed to make '%s' the active source. will retry later", m_busDevice
->GetLogicalAddressName());
1246 int64_t now(GetTimeMs());
1247 CLockObject
lock(m_mutex
);
1248 if (m_iActiveSourcePending
== 0 || m_iActiveSourcePending
< now
)
1249 m_iActiveSourcePending
= now
+ (int64_t)CEC_ACTIVE_SOURCE_SWITCH_RETRY_TIME_MS
;
1254 CLockObject
lock(m_mutex
);
1255 // clear previous pending active source command
1256 m_iActiveSourcePending
= 0;
1259 // mark the handler as initialised
1260 CLockObject
lock(m_mutex
);
1261 m_bHandlerInited
= true;
1266 void CCECCommandHandler::ScheduleActivateSource(uint64_t iDelay
)
1268 CLockObject
lock(m_mutex
);
1269 m_iActiveSourcePending
= GetTimeMs() + iDelay
;
1272 void CCECCommandHandler::RequestEmailFromCustomer(const cec_command
& command
)
1274 bool bInserted(false);
1275 map
<cec_opcode
, vector
<cec_command
> >::iterator it
= m_logsRequested
.find(command
.opcode
);
1276 if (it
!= m_logsRequested
.end())
1278 for (vector
<cec_command
>::const_iterator it2
= it
->second
.begin(); it2
!= it
->second
.end(); it2
++)
1280 // we already logged this one
1281 if ((*it2
).parameters
== command
.parameters
)
1285 it
->second
.push_back(command
);
1291 vector
<cec_command
> commands
;
1292 commands
.push_back(command
);
1293 m_logsRequested
.insert(make_pair(command
.opcode
, commands
));
1296 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)", CCECTypeUtils::ToString(command
).c_str(), CCECTypeUtils::ToString(m_vendorId
), m_vendorId
);