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
;
47 bool CCECCommandHandler::HandleCommand(const cec_command
&command
)
52 strLog
.Format(">> %s (%X) -> %s (%X): %s (%2X)", m_busDevice
->GetProcessor()->ToString(command
.initiator
), command
.initiator
, m_busDevice
->GetProcessor()->ToString(command
.destination
), command
.destination
, m_busDevice
->GetProcessor()->ToString(command
.opcode
), command
.opcode
);
53 m_busDevice
->AddLog(CEC_LOG_NOTICE
, strLog
);
55 switch(command
.opcode
)
57 case CEC_OPCODE_REPORT_POWER_STATUS
:
58 HandleReportPowerStatus(command
);
60 case CEC_OPCODE_CEC_VERSION
:
61 HandleDeviceCecVersion(command
);
63 case CEC_OPCODE_SET_MENU_LANGUAGE
:
64 HandleSetMenuLanguage(command
);
66 case CEC_OPCODE_GIVE_PHYSICAL_ADDRESS
:
67 HandleGivePhysicalAddress(command
);
69 case CEC_OPCODE_GIVE_OSD_NAME
:
70 HandleGiveOSDName(command
);
72 case CEC_OPCODE_GIVE_DEVICE_VENDOR_ID
:
73 HandleGiveDeviceVendorId(command
);
75 case CEC_OPCODE_DEVICE_VENDOR_ID
:
76 HandleDeviceVendorId(command
);
78 case CEC_OPCODE_VENDOR_COMMAND_WITH_ID
:
79 HandleDeviceVendorCommandWithId(command
);
81 case CEC_OPCODE_GIVE_DECK_STATUS
:
82 HandleGiveDeckStatus(command
);
84 case CEC_OPCODE_DECK_CONTROL
:
85 HandleDeckControl(command
);
87 case CEC_OPCODE_MENU_REQUEST
:
88 HandleMenuRequest(command
);
90 case CEC_OPCODE_GIVE_DEVICE_POWER_STATUS
:
91 HandleGiveDevicePowerStatus(command
);
93 case CEC_OPCODE_GET_CEC_VERSION
:
94 HandleGetCecVersion(command
);
96 case CEC_OPCODE_USER_CONTROL_PRESSED
:
97 HandleUserControlPressed(command
);
99 case CEC_OPCODE_USER_CONTROL_RELEASE
:
100 HandleUserControlRelease(command
);
102 case CEC_OPCODE_GIVE_AUDIO_STATUS
:
103 HandleGiveAudioStatus(command
);
105 case CEC_OPCODE_GIVE_SYSTEM_AUDIO_MODE_STATUS
:
106 HandleGiveSystemAudioModeStatus(command
);
108 case CEC_OPCODE_SYSTEM_AUDIO_MODE_REQUEST
:
109 HandleSystemAudioModeRequest(command
);
111 case CEC_OPCODE_REPORT_AUDIO_STATUS
:
112 HandleReportAudioStatus(command
);//YYY
114 case CEC_OPCODE_SYSTEM_AUDIO_MODE_STATUS
:
115 HandleSystemAudioModeStatus(command
);//YYY
117 case CEC_OPCODE_SET_SYSTEM_AUDIO_MODE
:
118 HandleSetSystemAudioMode(command
);//YYY
120 case CEC_OPCODE_REQUEST_ACTIVE_SOURCE
:
121 HandleRequestActiveSource(command
);
123 case CEC_OPCODE_SET_STREAM_PATH
:
124 HandleSetStreamPath(command
);
126 case CEC_OPCODE_ROUTING_CHANGE
:
127 HandleRoutingChange(command
);
129 case CEC_OPCODE_ROUTING_INFORMATION
:
130 HandleRoutingInformation(command
);
132 case CEC_OPCODE_STANDBY
:
133 HandleStandby(command
);
135 case CEC_OPCODE_ACTIVE_SOURCE
:
136 HandleActiveSource(command
);
138 case CEC_OPCODE_REPORT_PHYSICAL_ADDRESS
:
139 HandleReportPhysicalAddress(command
);
141 case CEC_OPCODE_SET_OSD_NAME
:
142 HandleSetOSDName(command
);
144 case CEC_OPCODE_IMAGE_VIEW_ON
:
145 HandleImageViewOn(command
);
147 case CEC_OPCODE_TEXT_VIEW_ON
:
148 HandleTextViewOn(command
);
151 UnhandledCommand(command
);
156 m_busDevice
->GetProcessor()->AddCommand(command
);
160 bool CCECCommandHandler::HandleActiveSource(const cec_command
&command
)
162 if (command
.parameters
.size
== 2)
164 uint16_t iAddress
= ((uint16_t)command
.parameters
[0] << 8) | ((uint16_t)command
.parameters
[1]);
165 return m_busDevice
->GetProcessor()->SetStreamPath(iAddress
);
171 bool CCECCommandHandler::HandleDeckControl(const cec_command
&command
)
173 CCECBusDevice
*device
= GetDevice(command
.destination
);
174 if (device
&& (device
->GetType() == CEC_DEVICE_TYPE_PLAYBACK_DEVICE
|| device
->GetType() == CEC_DEVICE_TYPE_RECORDING_DEVICE
) && command
.parameters
.size
> 0)
176 ((CCECPlaybackDevice
*) device
)->SetDeckControlMode((cec_deck_control_mode
) command
.parameters
[0]);
183 bool CCECCommandHandler::HandleDeviceCecVersion(const cec_command
&command
)
185 if (command
.parameters
.size
== 1)
187 CCECBusDevice
*device
= GetDevice(command
.initiator
);
189 device
->SetCecVersion((cec_version
) command
.parameters
[0]);
195 bool CCECCommandHandler::HandleDeviceVendorCommandWithId(const cec_command
&command
)
197 if (m_busDevice
->MyLogicalAddressContains(command
.destination
))
198 m_busDevice
->GetProcessor()->TransmitAbort(command
.initiator
, command
.opcode
, CEC_ABORT_REASON_REFUSED
);
203 bool CCECCommandHandler::HandleDeviceVendorId(const cec_command
&command
)
205 SetVendorId(command
);
209 bool CCECCommandHandler::HandleGetCecVersion(const cec_command
&command
)
211 if (m_busDevice
->MyLogicalAddressContains(command
.destination
))
213 CCECBusDevice
*device
= GetDevice(command
.destination
);
215 return device
->TransmitCECVersion(command
.initiator
);
221 bool CCECCommandHandler::HandleGiveAudioStatus(const cec_command
&command
)
223 if (m_busDevice
->MyLogicalAddressContains(command
.destination
))
225 CCECBusDevice
*device
= GetDevice(command
.destination
);
226 if (device
&& device
->GetType() == CEC_DEVICE_TYPE_AUDIO_SYSTEM
)
227 return ((CCECAudioSystem
*) device
)->TransmitAudioStatus(command
.initiator
);
233 bool CCECCommandHandler::HandleGiveDeckStatus(const cec_command
&command
)
235 if (m_busDevice
->MyLogicalAddressContains(command
.destination
))
237 CCECBusDevice
*device
= GetDevice(command
.destination
);
238 if (device
&& (device
->GetType() == CEC_DEVICE_TYPE_PLAYBACK_DEVICE
|| device
->GetType() == CEC_DEVICE_TYPE_RECORDING_DEVICE
))
239 return ((CCECPlaybackDevice
*) device
)->TransmitDeckStatus(command
.initiator
);
245 bool CCECCommandHandler::HandleGiveDevicePowerStatus(const cec_command
&command
)
247 if (m_busDevice
->MyLogicalAddressContains(command
.destination
))
249 CCECBusDevice
*device
= GetDevice(command
.destination
);
251 return device
->TransmitPowerState(command
.initiator
);
257 bool CCECCommandHandler::HandleGiveDeviceVendorId(const cec_command
&command
)
259 if (m_busDevice
->MyLogicalAddressContains(command
.destination
))
261 CCECBusDevice
*device
= GetDevice(command
.destination
);
263 return device
->TransmitVendorID(command
.initiator
);
269 bool CCECCommandHandler::HandleGiveOSDName(const cec_command
&command
)
271 if (m_busDevice
->MyLogicalAddressContains(command
.destination
))
273 CCECBusDevice
*device
= GetDevice(command
.destination
);
275 return device
->TransmitOSDName(command
.initiator
);
281 bool CCECCommandHandler::HandleGivePhysicalAddress(const cec_command
&command
)
283 if (m_busDevice
->MyLogicalAddressContains(command
.destination
))
285 CCECBusDevice
*device
= GetDevice(command
.destination
);
287 return device
->TransmitPhysicalAddress();
293 bool CCECCommandHandler::HandleGiveSystemAudioModeStatus(const cec_command
&command
)
295 if (m_busDevice
->MyLogicalAddressContains(command
.destination
))
297 CCECBusDevice
*device
= GetDevice(command
.destination
);
298 if (device
&& device
->GetType() == CEC_DEVICE_TYPE_AUDIO_SYSTEM
)
299 return ((CCECAudioSystem
*) device
)->TransmitSystemAudioModeStatus(command
.initiator
);
305 bool CCECCommandHandler::HandleImageViewOn(const cec_command
&command
)
307 m_busDevice
->GetProcessor()->SetActiveSource(command
.initiator
);
311 bool CCECCommandHandler::HandleMenuRequest(const cec_command
&command
)
313 if (m_busDevice
->MyLogicalAddressContains(command
.destination
))
315 if (command
.parameters
[0] == CEC_MENU_REQUEST_TYPE_QUERY
)
317 CCECBusDevice
*device
= GetDevice(command
.destination
);
319 return device
->TransmitMenuState(command
.initiator
);
326 bool CCECCommandHandler::HandleReportAudioStatus(const cec_command
&command
)
328 if (command
.parameters
.size
== 1)
330 CCECBusDevice
*device
= GetDevice(command
.initiator
);
331 if (device
&& device
->GetType() == CEC_DEVICE_TYPE_AUDIO_SYSTEM
)
333 ((CCECAudioSystem
*)device
)->SetAudioStatus(command
.parameters
[0]);
340 bool CCECCommandHandler::HandleReportPhysicalAddress(const cec_command
&command
)
342 if (command
.parameters
.size
== 3)
344 uint16_t iNewAddress
= ((uint16_t)command
.parameters
[0] << 8) | ((uint16_t)command
.parameters
[1]);
345 SetPhysicalAddress(command
.initiator
, iNewAddress
);
350 bool CCECCommandHandler::HandleReportPowerStatus(const cec_command
&command
)
352 if (command
.parameters
.size
== 1)
354 CCECBusDevice
*device
= GetDevice(command
.initiator
);
356 device
->SetPowerStatus((cec_power_status
) command
.parameters
[0]);
361 bool CCECCommandHandler::HandleRequestActiveSource(const cec_command
&command
)
364 strLog
.Format(">> %i requests active source", (uint8_t) command
.initiator
);
365 m_busDevice
->AddLog(CEC_LOG_DEBUG
, strLog
.c_str());
367 vector
<CCECBusDevice
*> devices
;
368 for (int iDevicePtr
= (int)GetMyDevices(devices
)-1; iDevicePtr
>=0; iDevicePtr
--)
369 devices
[iDevicePtr
]->TransmitActiveSource();
374 bool CCECCommandHandler::HandleRoutingChange(const cec_command
&command
)
376 if (command
.parameters
.size
== 4)
378 uint16_t iOldAddress
= ((uint16_t)command
.parameters
[0] << 8) | ((uint16_t)command
.parameters
[1]);
379 uint16_t iNewAddress
= ((uint16_t)command
.parameters
[2] << 8) | ((uint16_t)command
.parameters
[3]);
381 CCECBusDevice
*device
= GetDevice(command
.initiator
);
383 device
->SetStreamPath(iNewAddress
, iOldAddress
);
388 bool CCECCommandHandler::HandleRoutingInformation(const cec_command
&command
)
390 if (command
.parameters
.size
== 2)
392 uint16_t iNewAddress
= ((uint16_t)command
.parameters
[0] << 8) | ((uint16_t)command
.parameters
[1]);
393 m_busDevice
->GetProcessor()->SetStreamPath(iNewAddress
);
399 bool CCECCommandHandler::HandleSetMenuLanguage(const cec_command
&command
)
401 if (command
.parameters
.size
== 3)
403 CCECBusDevice
*device
= GetDevice(command
.initiator
);
406 cec_menu_language language
;
407 language
.device
= command
.initiator
;
408 for (uint8_t iPtr
= 0; iPtr
< 4; iPtr
++)
409 language
.language
[iPtr
] = command
.parameters
[iPtr
];
410 language
.language
[3] = 0;
411 device
->SetMenuLanguage(language
);
418 bool CCECCommandHandler::HandleSetOSDName(const cec_command
&command
)
420 if (command
.parameters
.size
> 0)
422 CCECBusDevice
*device
= GetDevice(command
.initiator
);
426 for (uint8_t iPtr
= 0; iPtr
< command
.parameters
.size
; iPtr
++)
427 buf
[iPtr
] = (char)command
.parameters
[iPtr
];
428 buf
[command
.parameters
.size
] = 0;
430 CStdString
strName(buf
);
431 device
->SetOSDName(strName
);
439 bool CCECCommandHandler::HandleSetStreamPath(const cec_command
&command
)
441 if (command
.parameters
.size
>= 2)
443 uint16_t iStreamAddress
= ((uint16_t)command
.parameters
[0] << 8) | ((uint16_t)command
.parameters
[1]);
445 strLog
.Format(">> %i sets stream path to physical address %04x", command
.initiator
, iStreamAddress
);
446 m_busDevice
->AddLog(CEC_LOG_DEBUG
, strLog
.c_str());
448 if (m_busDevice
->GetProcessor()->SetStreamPath(iStreamAddress
))
450 CCECBusDevice
*device
= GetDeviceByPhysicalAddress(iStreamAddress
);
453 return device
->TransmitActiveSource() &&
454 device
->TransmitMenuState(command
.initiator
);
461 bool CCECCommandHandler::HandleSystemAudioModeRequest(const cec_command
&command
)
463 if (m_busDevice
->MyLogicalAddressContains(command
.destination
))
465 CCECBusDevice
*device
= GetDevice(command
.destination
);
466 if (device
&& device
->GetType() == CEC_DEVICE_TYPE_AUDIO_SYSTEM
)
468 if (command
.parameters
.size
>= 2)
470 device
->SetPowerStatus(CEC_POWER_STATUS_ON
);
471 ((CCECAudioSystem
*) device
)->SetSystemAudioModeStatus(CEC_SYSTEM_AUDIO_STATUS_ON
);
472 uint16_t iNewAddress
= ((uint16_t)command
.parameters
[0] << 8) | ((uint16_t)command
.parameters
[1]);
473 CCECBusDevice
*newActiveDevice
= GetDeviceByPhysicalAddress(iNewAddress
);
475 m_busDevice
->GetProcessor()->SetActiveSource(newActiveDevice
->GetLogicalAddress());
476 return ((CCECAudioSystem
*) device
)->TransmitSetSystemAudioMode(command
.initiator
);
480 ((CCECAudioSystem
*) device
)->SetSystemAudioModeStatus(CEC_SYSTEM_AUDIO_STATUS_OFF
);
481 return ((CCECAudioSystem
*) device
)->TransmitSetSystemAudioMode(command
.initiator
);
488 bool CCECCommandHandler::HandleStandby(const cec_command
&command
)
490 CCECBusDevice
*device
= GetDevice(command
.initiator
);
492 device
->SetPowerStatus(CEC_POWER_STATUS_STANDBY
);
497 bool CCECCommandHandler::HandleSystemAudioModeStatus(const cec_command
&command
)
499 if (command
.parameters
.size
== 1)
501 CCECBusDevice
*device
= GetDevice(command
.initiator
);
502 if (device
&& device
->GetType() == CEC_DEVICE_TYPE_AUDIO_SYSTEM
)
504 ((CCECAudioSystem
*)device
)->SetSystemAudioModeStatus((cec_system_audio_status
)command
.parameters
[0]);
512 bool CCECCommandHandler::HandleSetSystemAudioMode(const cec_command
&command
)
514 if (command
.parameters
.size
== 1)
516 CCECBusDevice
*device
= GetDevice(command
.initiator
);
517 if (device
&& device
->GetType() == CEC_DEVICE_TYPE_AUDIO_SYSTEM
)
519 ((CCECAudioSystem
*)device
)->SetSystemAudioModeStatus((cec_system_audio_status
)command
.parameters
[0]);
527 bool CCECCommandHandler::HandleTextViewOn(const cec_command
&command
)
529 m_busDevice
->GetProcessor()->SetActiveSource(command
.initiator
);
533 bool CCECCommandHandler::HandleUserControlPressed(const cec_command
&command
)
535 if (m_busDevice
->MyLogicalAddressContains(command
.destination
) && command
.parameters
.size
> 0)
537 m_busDevice
->GetProcessor()->AddKey();
539 if (command
.parameters
[0] <= CEC_USER_CONTROL_CODE_MAX
)
542 strLog
.Format("key pressed: %x", command
.parameters
[0]);
543 m_busDevice
->AddLog(CEC_LOG_DEBUG
, strLog
.c_str());
545 if (command
.parameters
[0] == CEC_USER_CONTROL_CODE_POWER
||
546 command
.parameters
[0] == CEC_USER_CONTROL_CODE_POWER_ON_FUNCTION
)
548 CCECBusDevice
*device
= GetDevice(command
.destination
);
550 device
->SetPowerStatus(CEC_POWER_STATUS_ON
);
553 m_busDevice
->GetProcessor()->SetCurrentButton((cec_user_control_code
) command
.parameters
[0]);
560 bool CCECCommandHandler::HandleUserControlRelease(const cec_command
&command
)
562 if (m_busDevice
->MyLogicalAddressContains(command
.destination
))
563 m_busDevice
->GetProcessor()->AddKey();
568 void CCECCommandHandler::UnhandledCommand(const cec_command
&command
)
571 strLog
.Format("unhandled command with opcode %02x from address %d", command
.opcode
, command
.initiator
);
572 m_busDevice
->AddLog(CEC_LOG_DEBUG
, strLog
);
575 unsigned int CCECCommandHandler::GetMyDevices(vector
<CCECBusDevice
*> &devices
) const
577 unsigned int iReturn(0);
579 cec_logical_addresses addresses
= m_busDevice
->GetProcessor()->GetLogicalAddresses();
580 for (uint8_t iPtr
= 0; iPtr
< 16; iPtr
++)
584 devices
.push_back(GetDevice((cec_logical_address
) iPtr
));
592 CCECBusDevice
*CCECCommandHandler::GetDevice(cec_logical_address iLogicalAddress
) const
594 CCECBusDevice
*device
= NULL
;
596 if (iLogicalAddress
>= CECDEVICE_TV
&& iLogicalAddress
<= CECDEVICE_BROADCAST
)
597 device
= m_busDevice
->GetProcessor()->m_busDevices
[iLogicalAddress
];
602 CCECBusDevice
*CCECCommandHandler::GetDeviceByPhysicalAddress(uint16_t iPhysicalAddress
) const
604 return m_busDevice
->GetProcessor()->GetDeviceByPhysicalAddress(iPhysicalAddress
);
607 CCECBusDevice
*CCECCommandHandler::GetDeviceByType(cec_device_type type
) const
609 return m_busDevice
->GetProcessor()->GetDeviceByType(type
);
612 void CCECCommandHandler::SetVendorId(const cec_command
&command
)
614 if (command
.parameters
.size
< 3)
616 m_busDevice
->AddLog(CEC_LOG_WARNING
, "invalid vendor ID received");
620 uint64_t iVendorId
= ((uint64_t)command
.parameters
[0] << 16) +
621 ((uint64_t)command
.parameters
[1] << 8) +
622 (uint64_t)command
.parameters
[2];
624 CCECBusDevice
*device
= GetDevice((cec_logical_address
) command
.initiator
);
626 device
->SetVendorId(iVendorId
);
629 void CCECCommandHandler::SetPhysicalAddress(cec_logical_address iAddress
, uint16_t iNewAddress
)
631 if (!m_busDevice
->MyLogicalAddressContains(iAddress
))
633 bool bOurAddress(m_busDevice
->GetProcessor()->GetPhysicalAddress() == iNewAddress
);
634 GetDevice(iAddress
)->SetPhysicalAddress(iNewAddress
);
637 /* another device reported the same physical address as ours
638 * since we don't have physical address detection yet, we'll just use the
639 * given address, increased by 0x100 for now */
640 m_busDevice
->GetProcessor()->SetPhysicalAddress(iNewAddress
+ 0x100);