2 * This file is part of the libCEC(R) library.
4 * libCEC(R) is Copyright (C) 2011 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/
33 #include "CECCommandHandler.h"
34 #include "../devices/CECBusDevice.h"
35 #include "../devices/CECAudioSystem.h"
36 #include "../devices/CECPlaybackDevice.h"
37 #include "../CECProcessor.h"
42 CCECCommandHandler::CCECCommandHandler(CCECBusDevice
*busDevice
) :
43 m_busDevice(busDevice
),
44 m_processor(m_busDevice
->GetProcessor()),
45 m_iTransmitTimeout(CEC_DEFAULT_TRANSMIT_TIMEOUT
),
46 m_iTransmitWait(CEC_DEFAULT_TRANSMIT_WAIT
),
47 m_iTransmitRetries(CEC_DEFAULT_TRANSMIT_RETRIES
),
48 m_bHandlerInited(false),
50 m_expectedResponse(CEC_OPCODE_NONE
)
54 CCECCommandHandler::~CCECCommandHandler(void)
56 CLockObject
lock(&m_processor
->m_transmitMutex
);
57 CLockObject
receiveLock(&m_receiveMutex
);
58 m_condition
.Broadcast();
61 bool CCECCommandHandler::HandleCommand(const cec_command
&command
)
67 strLog
.Format(">> %s (%X) -> %s (%X): %s (%2X)", m_processor
->ToString(command
.initiator
), command
.initiator
, m_processor
->ToString(command
.destination
), command
.destination
, m_processor
->ToString(command
.opcode
), command
.opcode
);
68 m_busDevice
->AddLog(CEC_LOG_NOTICE
, strLog
);
70 m_processor
->AddCommand(command
);
72 switch(command
.opcode
)
74 case CEC_OPCODE_REPORT_POWER_STATUS
:
75 HandleReportPowerStatus(command
);
77 case CEC_OPCODE_CEC_VERSION
:
78 HandleDeviceCecVersion(command
);
80 case CEC_OPCODE_SET_MENU_LANGUAGE
:
81 HandleSetMenuLanguage(command
);
83 case CEC_OPCODE_GIVE_PHYSICAL_ADDRESS
:
84 if (m_processor
->IsInitialised())
85 HandleGivePhysicalAddress(command
);
87 case CEC_OPCODE_GIVE_OSD_NAME
:
88 if (m_processor
->IsInitialised())
89 HandleGiveOSDName(command
);
91 case CEC_OPCODE_GIVE_DEVICE_VENDOR_ID
:
92 if (m_processor
->IsInitialised())
93 HandleGiveDeviceVendorId(command
);
95 case CEC_OPCODE_DEVICE_VENDOR_ID
:
96 HandleDeviceVendorId(command
);
98 case CEC_OPCODE_VENDOR_COMMAND_WITH_ID
:
99 HandleDeviceVendorCommandWithId(command
);
101 case CEC_OPCODE_GIVE_DECK_STATUS
:
102 if (m_processor
->IsInitialised())
103 HandleGiveDeckStatus(command
);
105 case CEC_OPCODE_DECK_CONTROL
:
106 HandleDeckControl(command
);
108 case CEC_OPCODE_MENU_REQUEST
:
109 if (m_processor
->IsInitialised())
110 HandleMenuRequest(command
);
112 case CEC_OPCODE_GIVE_DEVICE_POWER_STATUS
:
113 if (m_processor
->IsInitialised())
114 HandleGiveDevicePowerStatus(command
);
116 case CEC_OPCODE_GET_CEC_VERSION
:
117 if (m_processor
->IsInitialised())
118 HandleGetCecVersion(command
);
120 case CEC_OPCODE_USER_CONTROL_PRESSED
:
121 if (m_processor
->IsInitialised())
122 HandleUserControlPressed(command
);
124 case CEC_OPCODE_USER_CONTROL_RELEASE
:
125 if (m_processor
->IsInitialised())
126 HandleUserControlRelease(command
);
128 case CEC_OPCODE_GIVE_AUDIO_STATUS
:
129 if (m_processor
->IsInitialised())
130 HandleGiveAudioStatus(command
);
132 case CEC_OPCODE_GIVE_SYSTEM_AUDIO_MODE_STATUS
:
133 if (m_processor
->IsInitialised())
134 HandleGiveSystemAudioModeStatus(command
);
136 case CEC_OPCODE_SYSTEM_AUDIO_MODE_REQUEST
:
137 if (m_processor
->IsInitialised())
138 HandleSystemAudioModeRequest(command
);
140 case CEC_OPCODE_REPORT_AUDIO_STATUS
:
141 HandleReportAudioStatus(command
);
143 case CEC_OPCODE_SYSTEM_AUDIO_MODE_STATUS
:
144 HandleSystemAudioModeStatus(command
);
146 case CEC_OPCODE_SET_SYSTEM_AUDIO_MODE
:
147 HandleSetSystemAudioMode(command
);
149 case CEC_OPCODE_REQUEST_ACTIVE_SOURCE
:
150 if (m_processor
->IsInitialised())
151 HandleRequestActiveSource(command
);
153 case CEC_OPCODE_SET_STREAM_PATH
:
154 HandleSetStreamPath(command
);
156 case CEC_OPCODE_ROUTING_CHANGE
:
157 HandleRoutingChange(command
);
159 case CEC_OPCODE_ROUTING_INFORMATION
:
160 HandleRoutingInformation(command
);
162 case CEC_OPCODE_STANDBY
:
163 if (m_processor
->IsInitialised())
164 HandleStandby(command
);
166 case CEC_OPCODE_ACTIVE_SOURCE
:
167 HandleActiveSource(command
);
169 case CEC_OPCODE_REPORT_PHYSICAL_ADDRESS
:
170 HandleReportPhysicalAddress(command
);
172 case CEC_OPCODE_SET_OSD_NAME
:
173 HandleSetOSDName(command
);
175 case CEC_OPCODE_IMAGE_VIEW_ON
:
176 HandleImageViewOn(command
);
178 case CEC_OPCODE_TEXT_VIEW_ON
:
179 HandleTextViewOn(command
);
181 case CEC_OPCODE_FEATURE_ABORT
:
182 HandleFeatureAbort(command
);
184 case CEC_OPCODE_VENDOR_COMMAND
:
185 HandleVendorCommand(command
);
188 UnhandledCommand(command
);
195 CLockObject
lock(&m_receiveMutex
);
196 if (m_expectedResponse
== CEC_OPCODE_NONE
||
197 m_expectedResponse
== command
.opcode
)
198 m_condition
.Signal();
205 bool CCECCommandHandler::HandleActiveSource(const cec_command
&command
)
207 if (command
.parameters
.size
== 2)
209 uint16_t iAddress
= ((uint16_t)command
.parameters
[0] << 8) | ((uint16_t)command
.parameters
[1]);
210 return m_processor
->SetActiveSource(iAddress
);
216 bool CCECCommandHandler::HandleDeckControl(const cec_command
&command
)
218 CCECBusDevice
*device
= GetDevice(command
.destination
);
219 if (device
&& (device
->GetType() == CEC_DEVICE_TYPE_PLAYBACK_DEVICE
|| device
->GetType() == CEC_DEVICE_TYPE_RECORDING_DEVICE
) && command
.parameters
.size
> 0)
221 ((CCECPlaybackDevice
*) device
)->SetDeckControlMode((cec_deck_control_mode
) command
.parameters
[0]);
228 bool CCECCommandHandler::HandleDeviceCecVersion(const cec_command
&command
)
230 if (command
.parameters
.size
== 1)
232 CCECBusDevice
*device
= GetDevice(command
.initiator
);
234 device
->SetCecVersion((cec_version
) command
.parameters
[0]);
240 bool CCECCommandHandler::HandleDeviceVendorCommandWithId(const cec_command
&command
)
242 if (m_processor
->IsStarted() && m_busDevice
->MyLogicalAddressContains(command
.destination
))
243 m_processor
->TransmitAbort(command
.initiator
, command
.opcode
, CEC_ABORT_REASON_REFUSED
);
248 bool CCECCommandHandler::HandleDeviceVendorId(const cec_command
&command
)
250 return SetVendorId(command
);
253 bool CCECCommandHandler::HandleFeatureAbort(const cec_command
&command
)
255 if (command
.parameters
.size
== 2)
257 m_processor
->m_busDevices
[command
.initiator
]->SetUnsupportedFeature((cec_opcode
)command
.parameters
[0]);
262 bool CCECCommandHandler::HandleGetCecVersion(const cec_command
&command
)
264 if (m_processor
->IsStarted() && m_busDevice
->MyLogicalAddressContains(command
.destination
))
266 CCECBusDevice
*device
= GetDevice(command
.destination
);
268 return device
->TransmitCECVersion(command
.initiator
);
274 bool CCECCommandHandler::HandleGiveAudioStatus(const cec_command
&command
)
276 if (m_processor
->IsStarted() && m_busDevice
->MyLogicalAddressContains(command
.destination
))
278 CCECBusDevice
*device
= GetDevice(command
.destination
);
279 if (device
&& device
->GetType() == CEC_DEVICE_TYPE_AUDIO_SYSTEM
)
280 return ((CCECAudioSystem
*) device
)->TransmitAudioStatus(command
.initiator
);
286 bool CCECCommandHandler::HandleGiveDeckStatus(const cec_command
&command
)
288 if (m_processor
->IsStarted() && m_busDevice
->MyLogicalAddressContains(command
.destination
))
290 CCECBusDevice
*device
= GetDevice(command
.destination
);
291 if (device
&& (device
->GetType() == CEC_DEVICE_TYPE_PLAYBACK_DEVICE
|| device
->GetType() == CEC_DEVICE_TYPE_RECORDING_DEVICE
))
292 return ((CCECPlaybackDevice
*) device
)->TransmitDeckStatus(command
.initiator
);
298 bool CCECCommandHandler::HandleGiveDevicePowerStatus(const cec_command
&command
)
300 if (m_processor
->IsStarted() && m_busDevice
->MyLogicalAddressContains(command
.destination
))
302 CCECBusDevice
*device
= GetDevice(command
.destination
);
304 return device
->TransmitPowerState(command
.initiator
);
310 bool CCECCommandHandler::HandleGiveDeviceVendorId(const cec_command
&command
)
312 if (m_processor
->IsStarted() && m_busDevice
->MyLogicalAddressContains(command
.destination
))
314 CCECBusDevice
*device
= GetDevice(command
.destination
);
316 return device
->TransmitVendorID(command
.initiator
);
322 bool CCECCommandHandler::HandleGiveOSDName(const cec_command
&command
)
324 if (m_processor
->IsStarted() && m_busDevice
->MyLogicalAddressContains(command
.destination
))
326 CCECBusDevice
*device
= GetDevice(command
.destination
);
328 return device
->TransmitOSDName(command
.initiator
);
334 bool CCECCommandHandler::HandleGivePhysicalAddress(const cec_command
&command
)
336 if (m_processor
->IsStarted() && m_busDevice
->MyLogicalAddressContains(command
.destination
))
338 CCECBusDevice
*device
= GetDevice(command
.destination
);
341 device
->SetActiveSource();
342 return device
->TransmitPhysicalAddress() &&
343 device
->TransmitActiveSource();
350 bool CCECCommandHandler::HandleGiveSystemAudioModeStatus(const cec_command
&command
)
352 if (m_processor
->IsStarted() && m_busDevice
->MyLogicalAddressContains(command
.destination
))
354 CCECBusDevice
*device
= GetDevice(command
.destination
);
355 if (device
&& device
->GetType() == CEC_DEVICE_TYPE_AUDIO_SYSTEM
)
356 return ((CCECAudioSystem
*) device
)->TransmitSystemAudioModeStatus(command
.initiator
);
362 bool CCECCommandHandler::HandleImageViewOn(const cec_command
&command
)
364 m_processor
->m_busDevices
[command
.initiator
]->SetActiveSource();
368 bool CCECCommandHandler::HandleMenuRequest(const cec_command
&command
)
370 if (m_processor
->IsStarted() && m_busDevice
->MyLogicalAddressContains(command
.destination
))
372 if (command
.parameters
[0] == CEC_MENU_REQUEST_TYPE_QUERY
)
374 CCECBusDevice
*device
= GetDevice(command
.destination
);
376 return device
->TransmitMenuState(command
.initiator
);
383 bool CCECCommandHandler::HandleReportAudioStatus(const cec_command
&command
)
385 if (command
.parameters
.size
== 1)
387 CCECBusDevice
*device
= GetDevice(command
.initiator
);
388 if (device
&& device
->GetType() == CEC_DEVICE_TYPE_AUDIO_SYSTEM
)
390 ((CCECAudioSystem
*)device
)->SetAudioStatus(command
.parameters
[0]);
397 bool CCECCommandHandler::HandleReportPhysicalAddress(const cec_command
&command
)
399 if (command
.parameters
.size
== 3)
401 uint16_t iNewAddress
= ((uint16_t)command
.parameters
[0] << 8) | ((uint16_t)command
.parameters
[1]);
402 SetPhysicalAddress(command
.initiator
, iNewAddress
);
407 bool CCECCommandHandler::HandleReportPowerStatus(const cec_command
&command
)
409 if (command
.parameters
.size
== 1)
411 CCECBusDevice
*device
= GetDevice(command
.initiator
);
413 device
->SetPowerStatus((cec_power_status
) command
.parameters
[0]);
418 bool CCECCommandHandler::HandleRequestActiveSource(const cec_command
&command
)
420 if (m_processor
->IsStarted())
423 strLog
.Format(">> %i requests active source", (uint8_t) command
.initiator
);
424 m_busDevice
->AddLog(CEC_LOG_DEBUG
, strLog
.c_str());
426 vector
<CCECBusDevice
*> devices
;
427 for (int iDevicePtr
= (int)GetMyDevices(devices
)-1; iDevicePtr
>=0; iDevicePtr
--)
428 devices
[iDevicePtr
]->TransmitActiveSource();
435 bool CCECCommandHandler::HandleRoutingChange(const cec_command
&command
)
437 if (command
.parameters
.size
== 4)
439 uint16_t iOldAddress
= ((uint16_t)command
.parameters
[0] << 8) | ((uint16_t)command
.parameters
[1]);
440 uint16_t iNewAddress
= ((uint16_t)command
.parameters
[2] << 8) | ((uint16_t)command
.parameters
[3]);
442 CCECBusDevice
*device
= GetDevice(command
.initiator
);
444 device
->SetStreamPath(iNewAddress
, iOldAddress
);
449 bool CCECCommandHandler::HandleRoutingInformation(const cec_command
&command
)
451 if (command
.parameters
.size
== 2)
453 uint16_t iNewAddress
= ((uint16_t)command
.parameters
[0] << 8) | ((uint16_t)command
.parameters
[1]);
454 m_processor
->SetActiveSource(iNewAddress
);
460 bool CCECCommandHandler::HandleSetMenuLanguage(const cec_command
&command
)
462 if (command
.parameters
.size
== 3)
464 CCECBusDevice
*device
= GetDevice(command
.initiator
);
467 cec_menu_language language
;
468 language
.device
= command
.initiator
;
469 for (uint8_t iPtr
= 0; iPtr
< 4; iPtr
++)
470 language
.language
[iPtr
] = command
.parameters
[iPtr
];
471 language
.language
[3] = 0;
472 device
->SetMenuLanguage(language
);
479 bool CCECCommandHandler::HandleSetOSDName(const cec_command
&command
)
481 if (command
.parameters
.size
> 0)
483 CCECBusDevice
*device
= GetDevice(command
.initiator
);
487 for (uint8_t iPtr
= 0; iPtr
< command
.parameters
.size
; iPtr
++)
488 buf
[iPtr
] = (char)command
.parameters
[iPtr
];
489 buf
[command
.parameters
.size
] = 0;
491 CStdString
strName(buf
);
492 device
->SetOSDName(strName
);
500 bool CCECCommandHandler::HandleSetStreamPath(const cec_command
&command
)
502 if (m_processor
->IsStarted() && command
.parameters
.size
>= 2)
504 uint16_t iStreamAddress
= ((uint16_t)command
.parameters
[0] << 8) | ((uint16_t)command
.parameters
[1]);
506 strLog
.Format(">> %i sets stream path to physical address %04x", command
.initiator
, iStreamAddress
);
507 m_busDevice
->AddLog(CEC_LOG_DEBUG
, strLog
.c_str());
509 /* one of the device handled by libCEC has been made active */
510 CCECBusDevice
*device
= GetDeviceByPhysicalAddress(iStreamAddress
);
511 if (device
&& m_busDevice
->MyLogicalAddressContains(device
->GetLogicalAddress()))
513 device
->SetActiveSource();
514 device
->TransmitActiveSource();
520 bool CCECCommandHandler::HandleSystemAudioModeRequest(const cec_command
&command
)
522 if (m_processor
->IsStarted() && m_busDevice
->MyLogicalAddressContains(command
.destination
))
524 CCECBusDevice
*device
= GetDevice(command
.destination
);
525 if (device
&& device
->GetType() == CEC_DEVICE_TYPE_AUDIO_SYSTEM
)
527 if (command
.parameters
.size
>= 2)
529 device
->SetPowerStatus(CEC_POWER_STATUS_ON
);
530 ((CCECAudioSystem
*) device
)->SetSystemAudioModeStatus(CEC_SYSTEM_AUDIO_STATUS_ON
);
531 uint16_t iNewAddress
= ((uint16_t)command
.parameters
[0] << 8) | ((uint16_t)command
.parameters
[1]);
532 CCECBusDevice
*newActiveDevice
= GetDeviceByPhysicalAddress(iNewAddress
);
534 newActiveDevice
->SetActiveSource();
535 return ((CCECAudioSystem
*) device
)->TransmitSetSystemAudioMode(command
.initiator
);
539 ((CCECAudioSystem
*) device
)->SetSystemAudioModeStatus(CEC_SYSTEM_AUDIO_STATUS_OFF
);
540 return ((CCECAudioSystem
*) device
)->TransmitSetSystemAudioMode(command
.initiator
);
547 bool CCECCommandHandler::HandleStandby(const cec_command
&command
)
549 CCECBusDevice
*device
= GetDevice(command
.initiator
);
551 device
->SetPowerStatus(CEC_POWER_STATUS_STANDBY
);
556 bool CCECCommandHandler::HandleSystemAudioModeStatus(const cec_command
&command
)
558 if (command
.parameters
.size
== 1)
560 CCECBusDevice
*device
= GetDevice(command
.initiator
);
561 if (device
&& device
->GetType() == CEC_DEVICE_TYPE_AUDIO_SYSTEM
)
563 ((CCECAudioSystem
*)device
)->SetSystemAudioModeStatus((cec_system_audio_status
)command
.parameters
[0]);
571 bool CCECCommandHandler::HandleSetSystemAudioMode(const cec_command
&command
)
573 if (command
.parameters
.size
== 1)
575 CCECBusDevice
*device
= GetDevice(command
.initiator
);
576 if (device
&& device
->GetType() == CEC_DEVICE_TYPE_AUDIO_SYSTEM
)
578 ((CCECAudioSystem
*)device
)->SetSystemAudioModeStatus((cec_system_audio_status
)command
.parameters
[0]);
586 bool CCECCommandHandler::HandleTextViewOn(const cec_command
&command
)
588 m_processor
->m_busDevices
[command
.initiator
]->SetActiveSource();
592 bool CCECCommandHandler::HandleUserControlPressed(const cec_command
&command
)
594 if (m_processor
->IsStarted() && m_busDevice
->MyLogicalAddressContains(command
.destination
) && command
.parameters
.size
> 0)
596 m_processor
->AddKey();
598 if (command
.parameters
[0] <= CEC_USER_CONTROL_CODE_MAX
)
601 strLog
.Format("key pressed: %x", command
.parameters
[0]);
602 m_busDevice
->AddLog(CEC_LOG_DEBUG
, strLog
.c_str());
604 if (command
.parameters
[0] == CEC_USER_CONTROL_CODE_POWER
||
605 command
.parameters
[0] == CEC_USER_CONTROL_CODE_POWER_ON_FUNCTION
)
607 CCECBusDevice
*device
= GetDevice(command
.destination
);
610 device
->SetPowerStatus(CEC_POWER_STATUS_ON
);
611 if (device
->MyLogicalAddressContains(device
->GetLogicalAddress()))
613 device
->SetActiveSource();
614 device
->TransmitActiveSource();
616 if (device
->GetType() == CEC_DEVICE_TYPE_PLAYBACK_DEVICE
||
617 device
->GetType() == CEC_DEVICE_TYPE_RECORDING_DEVICE
)
618 ((CCECPlaybackDevice
*)device
)->TransmitDeckStatus(command
.initiator
);
624 m_processor
->SetCurrentButton((cec_user_control_code
) command
.parameters
[0]);
632 bool CCECCommandHandler::HandleUserControlRelease(const cec_command
&command
)
634 if (m_processor
->IsStarted() && m_busDevice
->MyLogicalAddressContains(command
.destination
))
635 m_processor
->AddKey();
640 void CCECCommandHandler::UnhandledCommand(const cec_command
&command
)
643 strLog
.Format("unhandled command with opcode %02x from address %d", command
.opcode
, command
.initiator
);
644 m_busDevice
->AddLog(CEC_LOG_DEBUG
, strLog
);
647 unsigned int CCECCommandHandler::GetMyDevices(vector
<CCECBusDevice
*> &devices
) const
649 unsigned int iReturn(0);
651 cec_logical_addresses addresses
= m_processor
->GetLogicalAddresses();
652 for (uint8_t iPtr
= 0; iPtr
< 16; iPtr
++)
656 devices
.push_back(GetDevice((cec_logical_address
) iPtr
));
664 CCECBusDevice
*CCECCommandHandler::GetDevice(cec_logical_address iLogicalAddress
) const
666 CCECBusDevice
*device
= NULL
;
668 if (iLogicalAddress
>= CECDEVICE_TV
&& iLogicalAddress
<= CECDEVICE_BROADCAST
)
669 device
= m_processor
->m_busDevices
[iLogicalAddress
];
674 CCECBusDevice
*CCECCommandHandler::GetDeviceByPhysicalAddress(uint16_t iPhysicalAddress
) const
676 return m_processor
->GetDeviceByPhysicalAddress(iPhysicalAddress
);
679 CCECBusDevice
*CCECCommandHandler::GetDeviceByType(cec_device_type type
) const
681 return m_processor
->GetDeviceByType(type
);
684 bool CCECCommandHandler::SetVendorId(const cec_command
&command
)
686 bool bChanged(false);
687 if (command
.parameters
.size
< 3)
689 m_busDevice
->AddLog(CEC_LOG_WARNING
, "invalid vendor ID received");
693 uint64_t iVendorId
= ((uint64_t)command
.parameters
[0] << 16) +
694 ((uint64_t)command
.parameters
[1] << 8) +
695 (uint64_t)command
.parameters
[2];
697 CCECBusDevice
*device
= GetDevice((cec_logical_address
) command
.initiator
);
699 bChanged
= device
->SetVendorId(iVendorId
);
703 void CCECCommandHandler::SetPhysicalAddress(cec_logical_address iAddress
, uint16_t iNewAddress
)
705 if (!m_busDevice
->MyLogicalAddressContains(iAddress
))
707 bool bOurAddress(m_processor
->GetPhysicalAddress() == iNewAddress
);
708 GetDevice(iAddress
)->SetPhysicalAddress(iNewAddress
);
711 /* another device reported the same physical address as ours
712 * since we don't have physical address detection yet, we'll just use the
713 * given address, increased by 0x100 for now */
714 m_processor
->SetPhysicalAddress(iNewAddress
+ 0x100);
719 void CCECCommandHandler::HandlePoll(const cec_logical_address iInitiator
, const cec_logical_address iDestination
)
722 strLog
.Format("<< POLL: %s (%x) -> %s (%x)", m_processor
->ToString(iInitiator
), iInitiator
, m_processor
->ToString(iDestination
), iDestination
);
723 m_processor
->AddLog(CEC_LOG_DEBUG
, strLog
);
726 bool CCECCommandHandler::HandleReceiveFailed(void)
728 /* default = error */
732 bool CCECCommandHandler::TransmitImageViewOn(const cec_logical_address iInitiator
, const cec_logical_address iDestination
)
735 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_IMAGE_VIEW_ON
);
737 return Transmit(command
, false);
740 bool CCECCommandHandler::TransmitStandby(const cec_logical_address iInitiator
, const cec_logical_address iDestination
)
743 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_STANDBY
);
745 return Transmit(command
, false);
748 bool CCECCommandHandler::TransmitRequestCecVersion(const cec_logical_address iInitiator
, const cec_logical_address iDestination
)
751 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_GET_CEC_VERSION
);
753 return Transmit(command
, true, CEC_OPCODE_CEC_VERSION
);
756 bool CCECCommandHandler::TransmitRequestMenuLanguage(const cec_logical_address iInitiator
, const cec_logical_address iDestination
)
759 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_GET_MENU_LANGUAGE
);
761 return Transmit(command
, true, CEC_OPCODE_SET_MENU_LANGUAGE
);
764 bool CCECCommandHandler::TransmitRequestOSDName(const cec_logical_address iInitiator
, const cec_logical_address iDestination
)
767 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_GIVE_OSD_NAME
);
769 return Transmit(command
, true, CEC_OPCODE_SET_OSD_NAME
);
772 bool CCECCommandHandler::TransmitRequestPhysicalAddress(const cec_logical_address iInitiator
, const cec_logical_address iDestination
)
775 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_GIVE_PHYSICAL_ADDRESS
);
777 return Transmit(command
, true, CEC_OPCODE_REPORT_PHYSICAL_ADDRESS
);
780 bool CCECCommandHandler::TransmitRequestPowerStatus(const cec_logical_address iInitiator
, const cec_logical_address iDestination
)
783 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_GIVE_DEVICE_POWER_STATUS
);
785 return Transmit(command
, true, CEC_OPCODE_REPORT_POWER_STATUS
);
788 bool CCECCommandHandler::TransmitRequestVendorId(const cec_logical_address iInitiator
, const cec_logical_address iDestination
)
791 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_GIVE_DEVICE_VENDOR_ID
);
793 return Transmit(command
, true, CEC_OPCODE_DEVICE_VENDOR_ID
);
796 bool CCECCommandHandler::TransmitActiveSource(const cec_logical_address iInitiator
, uint16_t iPhysicalAddress
)
799 cec_command::Format(command
, iInitiator
, CECDEVICE_BROADCAST
, CEC_OPCODE_ACTIVE_SOURCE
);
800 command
.parameters
.PushBack((uint8_t) ((iPhysicalAddress
>> 8) & 0xFF));
801 command
.parameters
.PushBack((uint8_t) (iPhysicalAddress
& 0xFF));
803 return Transmit(command
, false);
806 bool CCECCommandHandler::TransmitCECVersion(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, cec_version cecVersion
)
809 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_CEC_VERSION
);
810 command
.parameters
.PushBack((uint8_t)cecVersion
);
812 return Transmit(command
, false);
815 bool CCECCommandHandler::TransmitInactiveSource(const cec_logical_address iInitiator
, uint16_t iPhysicalAddress
)
818 cec_command::Format(command
, iInitiator
, CECDEVICE_TV
, CEC_OPCODE_INACTIVE_SOURCE
);
819 command
.parameters
.PushBack((iPhysicalAddress
>> 8) & 0xFF);
820 command
.parameters
.PushBack(iPhysicalAddress
& 0xFF);
822 return Transmit(command
, false);
825 bool CCECCommandHandler::TransmitMenuState(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, cec_menu_state menuState
)
828 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_MENU_STATUS
);
829 command
.parameters
.PushBack((uint8_t)menuState
);
831 return Transmit(command
, false);
834 bool CCECCommandHandler::TransmitOSDName(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, CStdString strDeviceName
)
837 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_SET_OSD_NAME
);
838 for (unsigned int iPtr
= 0; iPtr
< strDeviceName
.length(); iPtr
++)
839 command
.parameters
.PushBack(strDeviceName
.at(iPtr
));
841 return Transmit(command
, false);
844 bool CCECCommandHandler::TransmitOSDString(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, cec_display_control duration
, const char *strMessage
)
847 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_SET_OSD_STRING
);
848 command
.parameters
.PushBack((uint8_t)duration
);
850 unsigned int iLen
= strlen(strMessage
);
851 if (iLen
> 13) iLen
= 13;
853 for (unsigned int iPtr
= 0; iPtr
< iLen
; iPtr
++)
854 command
.parameters
.PushBack(strMessage
[iPtr
]);
856 return Transmit(command
, false);
859 bool CCECCommandHandler::TransmitPhysicalAddress(const cec_logical_address iInitiator
, uint16_t iPhysicalAddress
, cec_device_type type
)
862 cec_command::Format(command
, iInitiator
, CECDEVICE_BROADCAST
, CEC_OPCODE_REPORT_PHYSICAL_ADDRESS
);
863 command
.parameters
.PushBack((uint8_t) ((iPhysicalAddress
>> 8) & 0xFF));
864 command
.parameters
.PushBack((uint8_t) (iPhysicalAddress
& 0xFF));
865 command
.parameters
.PushBack((uint8_t) (type
));
867 return Transmit(command
, false);
870 bool CCECCommandHandler::TransmitPoll(const cec_logical_address iInitiator
, const cec_logical_address iDestination
)
873 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_NONE
);
875 return Transmit(command
, false);
878 bool CCECCommandHandler::TransmitPowerState(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, cec_power_status state
)
881 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_REPORT_POWER_STATUS
);
882 command
.parameters
.PushBack((uint8_t) state
);
884 return Transmit(command
, false);
887 bool CCECCommandHandler::TransmitVendorID(const cec_logical_address iInitiator
, uint64_t iVendorId
)
890 cec_command::Format(command
, iInitiator
, CECDEVICE_BROADCAST
, CEC_OPCODE_DEVICE_VENDOR_ID
);
892 command
.parameters
.PushBack((uint8_t) (((uint64_t)iVendorId
>> 16) & 0xFF));
893 command
.parameters
.PushBack((uint8_t) (((uint64_t)iVendorId
>> 8) & 0xFF));
894 command
.parameters
.PushBack((uint8_t) ((uint64_t)iVendorId
& 0xFF));
896 return Transmit(command
, false);
899 bool CCECCommandHandler::TransmitAudioStatus(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, uint8_t state
)
902 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_REPORT_AUDIO_STATUS
);
903 command
.parameters
.PushBack(state
);
905 return Transmit(command
, false);
908 bool CCECCommandHandler::TransmitSetSystemAudioMode(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, cec_system_audio_status state
)
911 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_SET_SYSTEM_AUDIO_MODE
);
912 command
.parameters
.PushBack((uint8_t)state
);
914 return Transmit(command
, false);
917 bool CCECCommandHandler::TransmitSystemAudioModeStatus(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, cec_system_audio_status state
)
920 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_SYSTEM_AUDIO_MODE_STATUS
);
921 command
.parameters
.PushBack((uint8_t)state
);
923 return Transmit(command
, false);
926 bool CCECCommandHandler::TransmitDeckStatus(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, cec_deck_info state
)
929 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_DECK_STATUS
);
930 command
.PushBack((uint8_t)state
);
932 return Transmit(command
, false);
935 bool CCECCommandHandler::TransmitKeypress(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, cec_user_control_code key
, bool bWait
/* = true */)
938 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_USER_CONTROL_PRESSED
);
939 command
.parameters
.PushBack((uint8_t)key
);
941 return Transmit(command
, bWait
);
944 bool CCECCommandHandler::TransmitKeyRelease(const cec_logical_address iInitiator
, const cec_logical_address iDestination
, bool bWait
/* = true */)
947 cec_command::Format(command
, iInitiator
, iDestination
, CEC_OPCODE_USER_CONTROL_RELEASE
);
949 return Transmit(command
, bWait
);
952 bool CCECCommandHandler::Transmit(cec_command
&command
, bool bExpectResponse
/* = true */, cec_opcode expectedResponse
/* = CEC_OPCODE_NONE */)
956 command
.transmit_timeout
= m_iTransmitTimeout
;
959 uint8_t iTries(0), iMaxTries(command
.opcode
== CEC_OPCODE_NONE
? 1 : m_iTransmitRetries
+ 1);
960 CLockObject
writeLock(&m_processor
->m_transmitMutex
);
961 CLockObject
receiveLock(&m_receiveMutex
);
963 while (!bReturn
&& ++iTries
<= iMaxTries
)
965 m_expectedResponse
= expectedResponse
;
966 if (m_processor
->Transmit(command
))
968 m_processor
->AddLog(CEC_LOG_DEBUG
, "command transmitted");
969 bReturn
= bExpectResponse
?
970 m_condition
.Wait(&m_receiveMutex
, m_iTransmitWait
) :
981 bool CCECCommandHandler::ActivateSource(void)
983 if (m_busDevice
->GetLogicalAddress() == CECDEVICE_TV
)
985 CCECBusDevice
*primary
= m_processor
->GetPrimaryDevice();
986 primary
->SetPowerStatus(CEC_POWER_STATUS_ON
);
987 primary
->SetMenuState(CEC_MENU_STATE_ACTIVATED
);
989 if (m_processor
->GetPrimaryDevice()->GetPhysicalAddress(false) != 0xffff)
991 m_processor
->SetActiveSource();
992 primary
->TransmitMenuState(m_busDevice
->GetLogicalAddress());
993 m_bHandlerInited
= true;
999 void CCECCommandHandler::MarkBusy(void)
1001 CLockObject
receiveLock(&m_receiveMutex
);
1005 bool CCECCommandHandler::MarkReady(void)
1007 CLockObject
receiveLock(&m_receiveMutex
);
1008 return m_iUseCounter
> 0 ? (--m_iUseCounter
== 0) : true;
1011 bool CCECCommandHandler::InUse(void)
1013 CLockObject
receiveLock(&m_receiveMutex
);
1014 return m_iUseCounter
> 0;