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 switch(command
.opcode
)
58 case CEC_OPCODE_REPORT_POWER_STATUS
:
59 HandleReportPowerStatus(command
);
61 case CEC_OPCODE_CEC_VERSION
:
62 HandleDeviceCecVersion(command
);
64 case CEC_OPCODE_SET_MENU_LANGUAGE
:
65 HandleSetMenuLanguage(command
);
67 case CEC_OPCODE_GIVE_PHYSICAL_ADDRESS
:
68 HandleGivePhysicalAddress(command
);
70 case CEC_OPCODE_GIVE_OSD_NAME
:
71 HandleGiveOSDName(command
);
73 case CEC_OPCODE_GIVE_DEVICE_VENDOR_ID
:
74 HandleGiveDeviceVendorId(command
);
76 case CEC_OPCODE_DEVICE_VENDOR_ID
:
77 HandleDeviceVendorId(command
);
79 case CEC_OPCODE_VENDOR_COMMAND_WITH_ID
:
80 HandleDeviceVendorCommandWithId(command
);
82 case CEC_OPCODE_GIVE_DECK_STATUS
:
83 HandleGiveDeckStatus(command
);
85 case CEC_OPCODE_DECK_CONTROL
:
86 HandleDeckControl(command
);
88 case CEC_OPCODE_MENU_REQUEST
:
89 HandleMenuRequest(command
);
91 case CEC_OPCODE_GIVE_DEVICE_POWER_STATUS
:
92 HandleGiveDevicePowerStatus(command
);
94 case CEC_OPCODE_GET_CEC_VERSION
:
95 HandleGetCecVersion(command
);
97 case CEC_OPCODE_USER_CONTROL_PRESSED
:
98 HandleUserControlPressed(command
);
100 case CEC_OPCODE_USER_CONTROL_RELEASE
:
101 HandleUserControlRelease(command
);
103 case CEC_OPCODE_GIVE_AUDIO_STATUS
:
104 HandleGiveAudioStatus(command
);
106 case CEC_OPCODE_GIVE_SYSTEM_AUDIO_MODE_STATUS
:
107 HandleGiveSystemAudioModeStatus(command
);
109 case CEC_OPCODE_SYSTEM_AUDIO_MODE_REQUEST
:
110 HandleSystemAudioModeRequest(command
);
112 case CEC_OPCODE_REPORT_AUDIO_STATUS
:
113 HandleReportAudioStatus(command
);//YYY
115 case CEC_OPCODE_SYSTEM_AUDIO_MODE_STATUS
:
116 HandleSystemAudioModeStatus(command
);//YYY
118 case CEC_OPCODE_SET_SYSTEM_AUDIO_MODE
:
119 HandleSetSystemAudioMode(command
);//YYY
121 case CEC_OPCODE_REQUEST_ACTIVE_SOURCE
:
122 HandleRequestActiveSource(command
);
124 case CEC_OPCODE_SET_STREAM_PATH
:
125 HandleSetStreamPath(command
);
127 case CEC_OPCODE_ROUTING_CHANGE
:
128 HandleRoutingChange(command
);
130 case CEC_OPCODE_ROUTING_INFORMATION
:
131 HandleRoutingInformation(command
);
133 case CEC_OPCODE_STANDBY
:
134 HandleStandby(command
);
136 case CEC_OPCODE_ACTIVE_SOURCE
:
137 HandleActiveSource(command
);
139 case CEC_OPCODE_REPORT_PHYSICAL_ADDRESS
:
140 HandleReportPhysicalAddress(command
);
142 case CEC_OPCODE_SET_OSD_NAME
:
143 HandleSetOSDName(command
);
145 case CEC_OPCODE_IMAGE_VIEW_ON
:
146 HandleImageViewOn(command
);
148 case CEC_OPCODE_TEXT_VIEW_ON
:
149 HandleTextViewOn(command
);
152 UnhandledCommand(command
);
157 m_processor
->AddCommand(command
);
161 bool CCECCommandHandler::HandleActiveSource(const cec_command
&command
)
163 if (command
.parameters
.size
== 2)
165 uint16_t iAddress
= ((uint16_t)command
.parameters
[0] << 8) | ((uint16_t)command
.parameters
[1]);
166 return m_processor
->SetStreamPath(iAddress
);
172 bool CCECCommandHandler::HandleDeckControl(const cec_command
&command
)
174 CCECBusDevice
*device
= GetDevice(command
.destination
);
175 if (device
&& (device
->GetType() == CEC_DEVICE_TYPE_PLAYBACK_DEVICE
|| device
->GetType() == CEC_DEVICE_TYPE_RECORDING_DEVICE
) && command
.parameters
.size
> 0)
177 ((CCECPlaybackDevice
*) device
)->SetDeckControlMode((cec_deck_control_mode
) command
.parameters
[0]);
184 bool CCECCommandHandler::HandleDeviceCecVersion(const cec_command
&command
)
186 if (command
.parameters
.size
== 1)
188 CCECBusDevice
*device
= GetDevice(command
.initiator
);
190 device
->SetCecVersion((cec_version
) command
.parameters
[0]);
196 bool CCECCommandHandler::HandleDeviceVendorCommandWithId(const cec_command
&command
)
198 if (m_busDevice
->MyLogicalAddressContains(command
.destination
))
199 m_processor
->TransmitAbort(command
.initiator
, command
.opcode
, CEC_ABORT_REASON_REFUSED
);
204 bool CCECCommandHandler::HandleDeviceVendorId(const cec_command
&command
)
206 SetVendorId(command
);
210 bool CCECCommandHandler::HandleGetCecVersion(const cec_command
&command
)
212 if (m_busDevice
->MyLogicalAddressContains(command
.destination
))
214 CCECBusDevice
*device
= GetDevice(command
.destination
);
216 return device
->TransmitCECVersion(command
.initiator
);
222 bool CCECCommandHandler::HandleGiveAudioStatus(const cec_command
&command
)
224 if (m_busDevice
->MyLogicalAddressContains(command
.destination
))
226 CCECBusDevice
*device
= GetDevice(command
.destination
);
227 if (device
&& device
->GetType() == CEC_DEVICE_TYPE_AUDIO_SYSTEM
)
228 return ((CCECAudioSystem
*) device
)->TransmitAudioStatus(command
.initiator
);
234 bool CCECCommandHandler::HandleGiveDeckStatus(const cec_command
&command
)
236 if (m_busDevice
->MyLogicalAddressContains(command
.destination
))
238 CCECBusDevice
*device
= GetDevice(command
.destination
);
239 if (device
&& (device
->GetType() == CEC_DEVICE_TYPE_PLAYBACK_DEVICE
|| device
->GetType() == CEC_DEVICE_TYPE_RECORDING_DEVICE
))
240 return ((CCECPlaybackDevice
*) device
)->TransmitDeckStatus(command
.initiator
);
246 bool CCECCommandHandler::HandleGiveDevicePowerStatus(const cec_command
&command
)
248 if (m_busDevice
->MyLogicalAddressContains(command
.destination
))
250 CCECBusDevice
*device
= GetDevice(command
.destination
);
252 return device
->TransmitPowerState(command
.initiator
);
258 bool CCECCommandHandler::HandleGiveDeviceVendorId(const cec_command
&command
)
260 if (m_busDevice
->MyLogicalAddressContains(command
.destination
))
262 CCECBusDevice
*device
= GetDevice(command
.destination
);
264 return device
->TransmitVendorID(command
.initiator
);
270 bool CCECCommandHandler::HandleGiveOSDName(const cec_command
&command
)
272 if (m_busDevice
->MyLogicalAddressContains(command
.destination
))
274 CCECBusDevice
*device
= GetDevice(command
.destination
);
276 return device
->TransmitOSDName(command
.initiator
);
282 bool CCECCommandHandler::HandleGivePhysicalAddress(const cec_command
&command
)
284 if (m_busDevice
->MyLogicalAddressContains(command
.destination
))
286 CCECBusDevice
*device
= GetDevice(command
.destination
);
288 return device
->TransmitPhysicalAddress();
294 bool CCECCommandHandler::HandleGiveSystemAudioModeStatus(const cec_command
&command
)
296 if (m_busDevice
->MyLogicalAddressContains(command
.destination
))
298 CCECBusDevice
*device
= GetDevice(command
.destination
);
299 if (device
&& device
->GetType() == CEC_DEVICE_TYPE_AUDIO_SYSTEM
)
300 return ((CCECAudioSystem
*) device
)->TransmitSystemAudioModeStatus(command
.initiator
);
306 bool CCECCommandHandler::HandleImageViewOn(const cec_command
&command
)
308 m_processor
->SetActiveSource(command
.initiator
);
312 bool CCECCommandHandler::HandleMenuRequest(const cec_command
&command
)
314 if (m_busDevice
->MyLogicalAddressContains(command
.destination
))
316 if (command
.parameters
[0] == CEC_MENU_REQUEST_TYPE_QUERY
)
318 CCECBusDevice
*device
= GetDevice(command
.destination
);
320 return device
->TransmitMenuState(command
.initiator
);
327 bool CCECCommandHandler::HandleReportAudioStatus(const cec_command
&command
)
329 if (command
.parameters
.size
== 1)
331 CCECBusDevice
*device
= GetDevice(command
.initiator
);
332 if (device
&& device
->GetType() == CEC_DEVICE_TYPE_AUDIO_SYSTEM
)
334 ((CCECAudioSystem
*)device
)->SetAudioStatus(command
.parameters
[0]);
341 bool CCECCommandHandler::HandleReportPhysicalAddress(const cec_command
&command
)
343 if (command
.parameters
.size
== 3)
345 uint16_t iNewAddress
= ((uint16_t)command
.parameters
[0] << 8) | ((uint16_t)command
.parameters
[1]);
346 SetPhysicalAddress(command
.initiator
, iNewAddress
);
351 bool CCECCommandHandler::HandleReportPowerStatus(const cec_command
&command
)
353 if (command
.parameters
.size
== 1)
355 CCECBusDevice
*device
= GetDevice(command
.initiator
);
357 device
->SetPowerStatus((cec_power_status
) command
.parameters
[0]);
362 bool CCECCommandHandler::HandleRequestActiveSource(const cec_command
&command
)
365 strLog
.Format(">> %i requests active source", (uint8_t) command
.initiator
);
366 m_busDevice
->AddLog(CEC_LOG_DEBUG
, strLog
.c_str());
368 vector
<CCECBusDevice
*> devices
;
369 for (int iDevicePtr
= (int)GetMyDevices(devices
)-1; iDevicePtr
>=0; iDevicePtr
--)
370 devices
[iDevicePtr
]->TransmitActiveSource();
375 bool CCECCommandHandler::HandleRoutingChange(const cec_command
&command
)
377 if (command
.parameters
.size
== 4)
379 uint16_t iOldAddress
= ((uint16_t)command
.parameters
[0] << 8) | ((uint16_t)command
.parameters
[1]);
380 uint16_t iNewAddress
= ((uint16_t)command
.parameters
[2] << 8) | ((uint16_t)command
.parameters
[3]);
382 CCECBusDevice
*device
= GetDevice(command
.initiator
);
384 device
->SetStreamPath(iNewAddress
, iOldAddress
);
389 bool CCECCommandHandler::HandleRoutingInformation(const cec_command
&command
)
391 if (command
.parameters
.size
== 2)
393 uint16_t iNewAddress
= ((uint16_t)command
.parameters
[0] << 8) | ((uint16_t)command
.parameters
[1]);
394 m_processor
->SetStreamPath(iNewAddress
);
400 bool CCECCommandHandler::HandleSetMenuLanguage(const cec_command
&command
)
402 if (command
.parameters
.size
== 3)
404 CCECBusDevice
*device
= GetDevice(command
.initiator
);
407 cec_menu_language language
;
408 language
.device
= command
.initiator
;
409 for (uint8_t iPtr
= 0; iPtr
< 4; iPtr
++)
410 language
.language
[iPtr
] = command
.parameters
[iPtr
];
411 language
.language
[3] = 0;
412 device
->SetMenuLanguage(language
);
419 bool CCECCommandHandler::HandleSetOSDName(const cec_command
&command
)
421 if (command
.parameters
.size
> 0)
423 CCECBusDevice
*device
= GetDevice(command
.initiator
);
427 for (uint8_t iPtr
= 0; iPtr
< command
.parameters
.size
; iPtr
++)
428 buf
[iPtr
] = (char)command
.parameters
[iPtr
];
429 buf
[command
.parameters
.size
] = 0;
431 CStdString
strName(buf
);
432 device
->SetOSDName(strName
);
440 bool CCECCommandHandler::HandleSetStreamPath(const cec_command
&command
)
442 if (command
.parameters
.size
>= 2)
444 uint16_t iStreamAddress
= ((uint16_t)command
.parameters
[0] << 8) | ((uint16_t)command
.parameters
[1]);
446 strLog
.Format(">> %i sets stream path to physical address %04x", command
.initiator
, iStreamAddress
);
447 m_busDevice
->AddLog(CEC_LOG_DEBUG
, strLog
.c_str());
449 if (m_processor
->SetStreamPath(iStreamAddress
))
451 CCECBusDevice
*device
= GetDeviceByPhysicalAddress(iStreamAddress
);
454 return device
->TransmitActiveSource() &&
455 device
->TransmitMenuState(command
.initiator
);
462 bool CCECCommandHandler::HandleSystemAudioModeRequest(const cec_command
&command
)
464 if (m_busDevice
->MyLogicalAddressContains(command
.destination
))
466 CCECBusDevice
*device
= GetDevice(command
.destination
);
467 if (device
&& device
->GetType() == CEC_DEVICE_TYPE_AUDIO_SYSTEM
)
469 if (command
.parameters
.size
>= 2)
471 device
->SetPowerStatus(CEC_POWER_STATUS_ON
);
472 ((CCECAudioSystem
*) device
)->SetSystemAudioModeStatus(CEC_SYSTEM_AUDIO_STATUS_ON
);
473 uint16_t iNewAddress
= ((uint16_t)command
.parameters
[0] << 8) | ((uint16_t)command
.parameters
[1]);
474 CCECBusDevice
*newActiveDevice
= GetDeviceByPhysicalAddress(iNewAddress
);
476 m_processor
->SetActiveSource(newActiveDevice
->GetLogicalAddress());
477 return ((CCECAudioSystem
*) device
)->TransmitSetSystemAudioMode(command
.initiator
);
481 ((CCECAudioSystem
*) device
)->SetSystemAudioModeStatus(CEC_SYSTEM_AUDIO_STATUS_OFF
);
482 return ((CCECAudioSystem
*) device
)->TransmitSetSystemAudioMode(command
.initiator
);
489 bool CCECCommandHandler::HandleStandby(const cec_command
&command
)
491 CCECBusDevice
*device
= GetDevice(command
.initiator
);
493 device
->SetPowerStatus(CEC_POWER_STATUS_STANDBY
);
498 bool CCECCommandHandler::HandleSystemAudioModeStatus(const cec_command
&command
)
500 if (command
.parameters
.size
== 1)
502 CCECBusDevice
*device
= GetDevice(command
.initiator
);
503 if (device
&& device
->GetType() == CEC_DEVICE_TYPE_AUDIO_SYSTEM
)
505 ((CCECAudioSystem
*)device
)->SetSystemAudioModeStatus((cec_system_audio_status
)command
.parameters
[0]);
513 bool CCECCommandHandler::HandleSetSystemAudioMode(const cec_command
&command
)
515 if (command
.parameters
.size
== 1)
517 CCECBusDevice
*device
= GetDevice(command
.initiator
);
518 if (device
&& device
->GetType() == CEC_DEVICE_TYPE_AUDIO_SYSTEM
)
520 ((CCECAudioSystem
*)device
)->SetSystemAudioModeStatus((cec_system_audio_status
)command
.parameters
[0]);
528 bool CCECCommandHandler::HandleTextViewOn(const cec_command
&command
)
530 m_processor
->SetActiveSource(command
.initiator
);
534 bool CCECCommandHandler::HandleUserControlPressed(const cec_command
&command
)
536 if (m_busDevice
->MyLogicalAddressContains(command
.destination
) && command
.parameters
.size
> 0)
538 m_processor
->AddKey();
540 if (command
.parameters
[0] <= CEC_USER_CONTROL_CODE_MAX
)
543 strLog
.Format("key pressed: %x", command
.parameters
[0]);
544 m_busDevice
->AddLog(CEC_LOG_DEBUG
, strLog
.c_str());
546 if (command
.parameters
[0] == CEC_USER_CONTROL_CODE_POWER
||
547 command
.parameters
[0] == CEC_USER_CONTROL_CODE_POWER_ON_FUNCTION
)
549 CCECBusDevice
*device
= GetDevice(command
.destination
);
551 device
->SetPowerStatus(CEC_POWER_STATUS_ON
);
554 m_processor
->SetCurrentButton((cec_user_control_code
) command
.parameters
[0]);
561 bool CCECCommandHandler::HandleUserControlRelease(const cec_command
&command
)
563 if (m_busDevice
->MyLogicalAddressContains(command
.destination
))
564 m_processor
->AddKey();
569 void CCECCommandHandler::UnhandledCommand(const cec_command
&command
)
572 strLog
.Format("unhandled command with opcode %02x from address %d", command
.opcode
, command
.initiator
);
573 m_busDevice
->AddLog(CEC_LOG_DEBUG
, strLog
);
576 unsigned int CCECCommandHandler::GetMyDevices(vector
<CCECBusDevice
*> &devices
) const
578 unsigned int iReturn(0);
580 cec_logical_addresses addresses
= m_processor
->GetLogicalAddresses();
581 for (uint8_t iPtr
= 0; iPtr
< 16; iPtr
++)
585 devices
.push_back(GetDevice((cec_logical_address
) iPtr
));
593 CCECBusDevice
*CCECCommandHandler::GetDevice(cec_logical_address iLogicalAddress
) const
595 CCECBusDevice
*device
= NULL
;
597 if (iLogicalAddress
>= CECDEVICE_TV
&& iLogicalAddress
<= CECDEVICE_BROADCAST
)
598 device
= m_processor
->m_busDevices
[iLogicalAddress
];
603 CCECBusDevice
*CCECCommandHandler::GetDeviceByPhysicalAddress(uint16_t iPhysicalAddress
) const
605 return m_processor
->GetDeviceByPhysicalAddress(iPhysicalAddress
);
608 CCECBusDevice
*CCECCommandHandler::GetDeviceByType(cec_device_type type
) const
610 return m_processor
->GetDeviceByType(type
);
613 void CCECCommandHandler::SetVendorId(const cec_command
&command
)
615 if (command
.parameters
.size
< 3)
617 m_busDevice
->AddLog(CEC_LOG_WARNING
, "invalid vendor ID received");
621 uint64_t iVendorId
= ((uint64_t)command
.parameters
[0] << 16) +
622 ((uint64_t)command
.parameters
[1] << 8) +
623 (uint64_t)command
.parameters
[2];
625 CCECBusDevice
*device
= GetDevice((cec_logical_address
) command
.initiator
);
627 device
->SetVendorId(iVendorId
);
630 void CCECCommandHandler::SetPhysicalAddress(cec_logical_address iAddress
, uint16_t iNewAddress
)
632 if (!m_busDevice
->MyLogicalAddressContains(iAddress
))
634 bool bOurAddress(m_processor
->GetPhysicalAddress() == iNewAddress
);
635 GetDevice(iAddress
)->SetPhysicalAddress(iNewAddress
);
638 /* another device reported the same physical address as ours
639 * since we don't have physical address detection yet, we'll just use the
640 * given address, increased by 0x100 for now */
641 m_processor
->SetPhysicalAddress(iNewAddress
+ 0x100);