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
)
44 m_busDevice
= busDevice
;
45 m_processor
= m_busDevice
->GetProcessor();
48 bool CCECCommandHandler::HandleCommand(const cec_command
&command
)
53 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
);
54 m_busDevice
->AddLog(CEC_LOG_NOTICE
, strLog
);
56 m_processor
->AddCommand(command
);
58 switch(command
.opcode
)
60 case CEC_OPCODE_REPORT_POWER_STATUS
:
61 HandleReportPowerStatus(command
);
63 case CEC_OPCODE_CEC_VERSION
:
64 HandleDeviceCecVersion(command
);
66 case CEC_OPCODE_SET_MENU_LANGUAGE
:
67 HandleSetMenuLanguage(command
);
69 case CEC_OPCODE_GIVE_PHYSICAL_ADDRESS
:
70 HandleGivePhysicalAddress(command
);
72 case CEC_OPCODE_GIVE_OSD_NAME
:
73 HandleGiveOSDName(command
);
75 case CEC_OPCODE_GIVE_DEVICE_VENDOR_ID
:
76 HandleGiveDeviceVendorId(command
);
78 case CEC_OPCODE_DEVICE_VENDOR_ID
:
79 HandleDeviceVendorId(command
);
81 case CEC_OPCODE_VENDOR_COMMAND_WITH_ID
:
82 HandleDeviceVendorCommandWithId(command
);
84 case CEC_OPCODE_GIVE_DECK_STATUS
:
85 HandleGiveDeckStatus(command
);
87 case CEC_OPCODE_DECK_CONTROL
:
88 HandleDeckControl(command
);
90 case CEC_OPCODE_MENU_REQUEST
:
91 HandleMenuRequest(command
);
93 case CEC_OPCODE_GIVE_DEVICE_POWER_STATUS
:
94 HandleGiveDevicePowerStatus(command
);
96 case CEC_OPCODE_GET_CEC_VERSION
:
97 HandleGetCecVersion(command
);
99 case CEC_OPCODE_USER_CONTROL_PRESSED
:
100 HandleUserControlPressed(command
);
102 case CEC_OPCODE_USER_CONTROL_RELEASE
:
103 HandleUserControlRelease(command
);
105 case CEC_OPCODE_GIVE_AUDIO_STATUS
:
106 HandleGiveAudioStatus(command
);
108 case CEC_OPCODE_GIVE_SYSTEM_AUDIO_MODE_STATUS
:
109 HandleGiveSystemAudioModeStatus(command
);
111 case CEC_OPCODE_SYSTEM_AUDIO_MODE_REQUEST
:
112 HandleSystemAudioModeRequest(command
);
114 case CEC_OPCODE_REPORT_AUDIO_STATUS
:
115 HandleReportAudioStatus(command
);
117 case CEC_OPCODE_SYSTEM_AUDIO_MODE_STATUS
:
118 HandleSystemAudioModeStatus(command
);
120 case CEC_OPCODE_SET_SYSTEM_AUDIO_MODE
:
121 HandleSetSystemAudioMode(command
);
123 case CEC_OPCODE_REQUEST_ACTIVE_SOURCE
:
124 HandleRequestActiveSource(command
);
126 case CEC_OPCODE_SET_STREAM_PATH
:
127 HandleSetStreamPath(command
);
129 case CEC_OPCODE_ROUTING_CHANGE
:
130 HandleRoutingChange(command
);
132 case CEC_OPCODE_ROUTING_INFORMATION
:
133 HandleRoutingInformation(command
);
135 case CEC_OPCODE_STANDBY
:
136 HandleStandby(command
);
138 case CEC_OPCODE_ACTIVE_SOURCE
:
139 HandleActiveSource(command
);
141 case CEC_OPCODE_REPORT_PHYSICAL_ADDRESS
:
142 HandleReportPhysicalAddress(command
);
144 case CEC_OPCODE_SET_OSD_NAME
:
145 HandleSetOSDName(command
);
147 case CEC_OPCODE_IMAGE_VIEW_ON
:
148 HandleImageViewOn(command
);
150 case CEC_OPCODE_TEXT_VIEW_ON
:
151 HandleTextViewOn(command
);
154 UnhandledCommand(command
);
162 bool CCECCommandHandler::HandleActiveSource(const cec_command
&command
)
164 if (command
.parameters
.size
== 2)
166 uint16_t iAddress
= ((uint16_t)command
.parameters
[0] << 8) | ((uint16_t)command
.parameters
[1]);
167 return m_processor
->SetStreamPath(iAddress
);
173 bool CCECCommandHandler::HandleDeckControl(const cec_command
&command
)
175 CCECBusDevice
*device
= GetDevice(command
.destination
);
176 if (device
&& (device
->GetType() == CEC_DEVICE_TYPE_PLAYBACK_DEVICE
|| device
->GetType() == CEC_DEVICE_TYPE_RECORDING_DEVICE
) && command
.parameters
.size
> 0)
178 ((CCECPlaybackDevice
*) device
)->SetDeckControlMode((cec_deck_control_mode
) command
.parameters
[0]);
185 bool CCECCommandHandler::HandleDeviceCecVersion(const cec_command
&command
)
187 if (command
.parameters
.size
== 1)
189 CCECBusDevice
*device
= GetDevice(command
.initiator
);
191 device
->SetCecVersion((cec_version
) command
.parameters
[0]);
197 bool CCECCommandHandler::HandleDeviceVendorCommandWithId(const cec_command
&command
)
199 if (m_busDevice
->MyLogicalAddressContains(command
.destination
))
200 m_processor
->TransmitAbort(command
.initiator
, command
.opcode
, CEC_ABORT_REASON_REFUSED
);
205 bool CCECCommandHandler::HandleDeviceVendorId(const cec_command
&command
)
207 SetVendorId(command
);
211 bool CCECCommandHandler::HandleGetCecVersion(const cec_command
&command
)
213 if (m_busDevice
->MyLogicalAddressContains(command
.destination
))
215 CCECBusDevice
*device
= GetDevice(command
.destination
);
217 return device
->TransmitCECVersion(command
.initiator
);
223 bool CCECCommandHandler::HandleGiveAudioStatus(const cec_command
&command
)
225 if (m_busDevice
->MyLogicalAddressContains(command
.destination
))
227 CCECBusDevice
*device
= GetDevice(command
.destination
);
228 if (device
&& device
->GetType() == CEC_DEVICE_TYPE_AUDIO_SYSTEM
)
229 return ((CCECAudioSystem
*) device
)->TransmitAudioStatus(command
.initiator
);
235 bool CCECCommandHandler::HandleGiveDeckStatus(const cec_command
&command
)
237 if (m_busDevice
->MyLogicalAddressContains(command
.destination
))
239 CCECBusDevice
*device
= GetDevice(command
.destination
);
240 if (device
&& (device
->GetType() == CEC_DEVICE_TYPE_PLAYBACK_DEVICE
|| device
->GetType() == CEC_DEVICE_TYPE_RECORDING_DEVICE
))
241 return ((CCECPlaybackDevice
*) device
)->TransmitDeckStatus(command
.initiator
);
247 bool CCECCommandHandler::HandleGiveDevicePowerStatus(const cec_command
&command
)
249 if (m_busDevice
->MyLogicalAddressContains(command
.destination
))
251 CCECBusDevice
*device
= GetDevice(command
.destination
);
253 return device
->TransmitPowerState(command
.initiator
);
259 bool CCECCommandHandler::HandleGiveDeviceVendorId(const cec_command
&command
)
261 if (m_busDevice
->MyLogicalAddressContains(command
.destination
))
263 CCECBusDevice
*device
= GetDevice(command
.destination
);
265 return device
->TransmitVendorID(command
.initiator
);
271 bool CCECCommandHandler::HandleGiveOSDName(const cec_command
&command
)
273 if (m_busDevice
->MyLogicalAddressContains(command
.destination
))
275 CCECBusDevice
*device
= GetDevice(command
.destination
);
277 return device
->TransmitOSDName(command
.initiator
);
283 bool CCECCommandHandler::HandleGivePhysicalAddress(const cec_command
&command
)
285 if (m_busDevice
->MyLogicalAddressContains(command
.destination
))
287 CCECBusDevice
*device
= GetDevice(command
.destination
);
289 return device
->TransmitPhysicalAddress();
295 bool CCECCommandHandler::HandleGiveSystemAudioModeStatus(const cec_command
&command
)
297 if (m_busDevice
->MyLogicalAddressContains(command
.destination
))
299 CCECBusDevice
*device
= GetDevice(command
.destination
);
300 if (device
&& device
->GetType() == CEC_DEVICE_TYPE_AUDIO_SYSTEM
)
301 return ((CCECAudioSystem
*) device
)->TransmitSystemAudioModeStatus(command
.initiator
);
307 bool CCECCommandHandler::HandleImageViewOn(const cec_command
&command
)
309 m_processor
->SetActiveSource(command
.initiator
);
313 bool CCECCommandHandler::HandleMenuRequest(const cec_command
&command
)
315 if (m_busDevice
->MyLogicalAddressContains(command
.destination
))
317 if (command
.parameters
[0] == CEC_MENU_REQUEST_TYPE_QUERY
)
319 CCECBusDevice
*device
= GetDevice(command
.destination
);
321 return device
->TransmitMenuState(command
.initiator
);
328 bool CCECCommandHandler::HandleReportAudioStatus(const cec_command
&command
)
330 if (command
.parameters
.size
== 1)
332 CCECBusDevice
*device
= GetDevice(command
.initiator
);
333 if (device
&& device
->GetType() == CEC_DEVICE_TYPE_AUDIO_SYSTEM
)
335 ((CCECAudioSystem
*)device
)->SetAudioStatus(command
.parameters
[0]);
342 bool CCECCommandHandler::HandleReportPhysicalAddress(const cec_command
&command
)
344 if (command
.parameters
.size
== 3)
346 uint16_t iNewAddress
= ((uint16_t)command
.parameters
[0] << 8) | ((uint16_t)command
.parameters
[1]);
347 SetPhysicalAddress(command
.initiator
, iNewAddress
);
352 bool CCECCommandHandler::HandleReportPowerStatus(const cec_command
&command
)
354 if (command
.parameters
.size
== 1)
356 CCECBusDevice
*device
= GetDevice(command
.initiator
);
358 device
->SetPowerStatus((cec_power_status
) command
.parameters
[0]);
363 bool CCECCommandHandler::HandleRequestActiveSource(const cec_command
&command
)
366 strLog
.Format(">> %i requests active source", (uint8_t) command
.initiator
);
367 m_busDevice
->AddLog(CEC_LOG_DEBUG
, strLog
.c_str());
369 vector
<CCECBusDevice
*> devices
;
370 for (int iDevicePtr
= (int)GetMyDevices(devices
)-1; iDevicePtr
>=0; iDevicePtr
--)
371 devices
[iDevicePtr
]->TransmitActiveSource();
376 bool CCECCommandHandler::HandleRoutingChange(const cec_command
&command
)
378 if (command
.parameters
.size
== 4)
380 uint16_t iOldAddress
= ((uint16_t)command
.parameters
[0] << 8) | ((uint16_t)command
.parameters
[1]);
381 uint16_t iNewAddress
= ((uint16_t)command
.parameters
[2] << 8) | ((uint16_t)command
.parameters
[3]);
383 CCECBusDevice
*device
= GetDevice(command
.initiator
);
385 device
->SetStreamPath(iNewAddress
, iOldAddress
);
390 bool CCECCommandHandler::HandleRoutingInformation(const cec_command
&command
)
392 if (command
.parameters
.size
== 2)
394 uint16_t iNewAddress
= ((uint16_t)command
.parameters
[0] << 8) | ((uint16_t)command
.parameters
[1]);
395 m_processor
->SetStreamPath(iNewAddress
);
401 bool CCECCommandHandler::HandleSetMenuLanguage(const cec_command
&command
)
403 if (command
.parameters
.size
== 3)
405 CCECBusDevice
*device
= GetDevice(command
.initiator
);
408 cec_menu_language language
;
409 language
.device
= command
.initiator
;
410 for (uint8_t iPtr
= 0; iPtr
< 4; iPtr
++)
411 language
.language
[iPtr
] = command
.parameters
[iPtr
];
412 language
.language
[3] = 0;
413 device
->SetMenuLanguage(language
);
420 bool CCECCommandHandler::HandleSetOSDName(const cec_command
&command
)
422 if (command
.parameters
.size
> 0)
424 CCECBusDevice
*device
= GetDevice(command
.initiator
);
428 for (uint8_t iPtr
= 0; iPtr
< command
.parameters
.size
; iPtr
++)
429 buf
[iPtr
] = (char)command
.parameters
[iPtr
];
430 buf
[command
.parameters
.size
] = 0;
432 CStdString
strName(buf
);
433 device
->SetOSDName(strName
);
441 bool CCECCommandHandler::HandleSetStreamPath(const cec_command
&command
)
443 if (command
.parameters
.size
>= 2)
445 uint16_t iStreamAddress
= ((uint16_t)command
.parameters
[0] << 8) | ((uint16_t)command
.parameters
[1]);
447 strLog
.Format(">> %i sets stream path to physical address %04x", command
.initiator
, iStreamAddress
);
448 m_busDevice
->AddLog(CEC_LOG_DEBUG
, strLog
.c_str());
450 if (m_processor
->SetStreamPath(iStreamAddress
))
452 CCECBusDevice
*device
= GetDeviceByPhysicalAddress(iStreamAddress
);
455 return device
->TransmitActiveSource() &&
456 device
->TransmitMenuState(command
.initiator
);
463 bool CCECCommandHandler::HandleSystemAudioModeRequest(const cec_command
&command
)
465 if (m_busDevice
->MyLogicalAddressContains(command
.destination
))
467 CCECBusDevice
*device
= GetDevice(command
.destination
);
468 if (device
&& device
->GetType() == CEC_DEVICE_TYPE_AUDIO_SYSTEM
)
470 if (command
.parameters
.size
>= 2)
472 device
->SetPowerStatus(CEC_POWER_STATUS_ON
);
473 ((CCECAudioSystem
*) device
)->SetSystemAudioModeStatus(CEC_SYSTEM_AUDIO_STATUS_ON
);
474 uint16_t iNewAddress
= ((uint16_t)command
.parameters
[0] << 8) | ((uint16_t)command
.parameters
[1]);
475 CCECBusDevice
*newActiveDevice
= GetDeviceByPhysicalAddress(iNewAddress
);
477 m_processor
->SetActiveSource(newActiveDevice
->GetLogicalAddress());
478 return ((CCECAudioSystem
*) device
)->TransmitSetSystemAudioMode(command
.initiator
);
482 ((CCECAudioSystem
*) device
)->SetSystemAudioModeStatus(CEC_SYSTEM_AUDIO_STATUS_OFF
);
483 return ((CCECAudioSystem
*) device
)->TransmitSetSystemAudioMode(command
.initiator
);
490 bool CCECCommandHandler::HandleStandby(const cec_command
&command
)
492 CCECBusDevice
*device
= GetDevice(command
.initiator
);
494 device
->SetPowerStatus(CEC_POWER_STATUS_STANDBY
);
499 bool CCECCommandHandler::HandleSystemAudioModeStatus(const cec_command
&command
)
501 if (command
.parameters
.size
== 1)
503 CCECBusDevice
*device
= GetDevice(command
.initiator
);
504 if (device
&& device
->GetType() == CEC_DEVICE_TYPE_AUDIO_SYSTEM
)
506 ((CCECAudioSystem
*)device
)->SetSystemAudioModeStatus((cec_system_audio_status
)command
.parameters
[0]);
514 bool CCECCommandHandler::HandleSetSystemAudioMode(const cec_command
&command
)
516 if (command
.parameters
.size
== 1)
518 CCECBusDevice
*device
= GetDevice(command
.initiator
);
519 if (device
&& device
->GetType() == CEC_DEVICE_TYPE_AUDIO_SYSTEM
)
521 ((CCECAudioSystem
*)device
)->SetSystemAudioModeStatus((cec_system_audio_status
)command
.parameters
[0]);
529 bool CCECCommandHandler::HandleTextViewOn(const cec_command
&command
)
531 m_processor
->SetActiveSource(command
.initiator
);
535 bool CCECCommandHandler::HandleUserControlPressed(const cec_command
&command
)
537 if (m_busDevice
->MyLogicalAddressContains(command
.destination
) && command
.parameters
.size
> 0)
539 m_processor
->AddKey();
541 if (command
.parameters
[0] <= CEC_USER_CONTROL_CODE_MAX
)
544 strLog
.Format("key pressed: %x", command
.parameters
[0]);
545 m_busDevice
->AddLog(CEC_LOG_DEBUG
, strLog
.c_str());
547 if (command
.parameters
[0] == CEC_USER_CONTROL_CODE_POWER
||
548 command
.parameters
[0] == CEC_USER_CONTROL_CODE_POWER_ON_FUNCTION
)
550 CCECBusDevice
*device
= GetDevice(command
.destination
);
552 device
->SetPowerStatus(CEC_POWER_STATUS_ON
);
555 m_processor
->SetCurrentButton((cec_user_control_code
) command
.parameters
[0]);
562 bool CCECCommandHandler::HandleUserControlRelease(const cec_command
&command
)
564 if (m_busDevice
->MyLogicalAddressContains(command
.destination
))
565 m_processor
->AddKey();
570 void CCECCommandHandler::UnhandledCommand(const cec_command
&command
)
573 strLog
.Format("unhandled command with opcode %02x from address %d", command
.opcode
, command
.initiator
);
574 m_busDevice
->AddLog(CEC_LOG_DEBUG
, strLog
);
577 unsigned int CCECCommandHandler::GetMyDevices(vector
<CCECBusDevice
*> &devices
) const
579 unsigned int iReturn(0);
581 cec_logical_addresses addresses
= m_processor
->GetLogicalAddresses();
582 for (uint8_t iPtr
= 0; iPtr
< 16; iPtr
++)
586 devices
.push_back(GetDevice((cec_logical_address
) iPtr
));
594 CCECBusDevice
*CCECCommandHandler::GetDevice(cec_logical_address iLogicalAddress
) const
596 CCECBusDevice
*device
= NULL
;
598 if (iLogicalAddress
>= CECDEVICE_TV
&& iLogicalAddress
<= CECDEVICE_BROADCAST
)
599 device
= m_processor
->m_busDevices
[iLogicalAddress
];
604 CCECBusDevice
*CCECCommandHandler::GetDeviceByPhysicalAddress(uint16_t iPhysicalAddress
) const
606 return m_processor
->GetDeviceByPhysicalAddress(iPhysicalAddress
);
609 CCECBusDevice
*CCECCommandHandler::GetDeviceByType(cec_device_type type
) const
611 return m_processor
->GetDeviceByType(type
);
614 void CCECCommandHandler::SetVendorId(const cec_command
&command
)
616 if (command
.parameters
.size
< 3)
618 m_busDevice
->AddLog(CEC_LOG_WARNING
, "invalid vendor ID received");
622 uint64_t iVendorId
= ((uint64_t)command
.parameters
[0] << 16) +
623 ((uint64_t)command
.parameters
[1] << 8) +
624 (uint64_t)command
.parameters
[2];
626 CCECBusDevice
*device
= GetDevice((cec_logical_address
) command
.initiator
);
628 device
->SetVendorId(iVendorId
);
631 void CCECCommandHandler::SetPhysicalAddress(cec_logical_address iAddress
, uint16_t iNewAddress
)
633 if (!m_busDevice
->MyLogicalAddressContains(iAddress
))
635 bool bOurAddress(m_processor
->GetPhysicalAddress() == iNewAddress
);
636 GetDevice(iAddress
)->SetPhysicalAddress(iNewAddress
);
639 /* another device reported the same physical address as ours
640 * since we don't have physical address detection yet, we'll just use the
641 * given address, increased by 0x100 for now */
642 m_processor
->SetPhysicalAddress(iNewAddress
+ 0x100);
647 void CCECCommandHandler::HandlePoll(const cec_logical_address iInitiator
, const cec_logical_address iDestination
)
650 strLog
.Format("<< POLL: %s (%x) -> %s (%x)", m_processor
->ToString(iInitiator
), iInitiator
, m_processor
->ToString(iDestination
), iDestination
);
651 m_processor
->AddLog(CEC_LOG_DEBUG
, strLog
);
654 bool CCECCommandHandler::HandleReceiveFailed(void)
656 /* default = error */