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 "../CECProcessor.h"
41 CCECCommandHandler::CCECCommandHandler(CCECBusDevice
*busDevice
)
43 m_busDevice
= busDevice
;
46 bool CCECCommandHandler::HandleCommand(const cec_command
&command
)
51 strLog
.Format(">> %s (%X) -> %s (%X): %s (%2X)", ToString(command
.initiator
), command
.initiator
, ToString(command
.destination
), command
.destination
, ToString(command
.opcode
), command
.opcode
);
52 m_busDevice
->AddLog(CEC_LOG_NOTICE
, strLog
);
54 if (m_busDevice
->MyLogicalAddressContains(command
.destination
))
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
);
66 /* pass to listeners */
67 m_busDevice
->GetProcessor()->AddCommand(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_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 HandleSetSystemAudioModeRequest(command
);
112 UnhandledCommand(command
);
113 /* pass to listeners */
114 m_busDevice
->GetProcessor()->AddCommand(command
);
119 else if (command
.destination
== CECDEVICE_BROADCAST
)
122 switch (command
.opcode
)
124 case CEC_OPCODE_SET_MENU_LANGUAGE
:
125 HandleSetMenuLanguage(command
);
126 /* pass to listeners */
127 m_busDevice
->GetProcessor()->AddCommand(command
);
129 case CEC_OPCODE_REQUEST_ACTIVE_SOURCE
:
130 HandleRequestActiveSource(command
);
132 case CEC_OPCODE_SET_STREAM_PATH
:
133 HandleSetStreamPath(command
);
135 case CEC_OPCODE_ROUTING_CHANGE
:
136 HandleRoutingChange(command
);
138 case CEC_OPCODE_DEVICE_VENDOR_ID
:
139 HandleDeviceVendorId(command
);
141 case CEC_OPCODE_VENDOR_COMMAND_WITH_ID
:
142 HandleDeviceVendorCommandWithId(command
);
144 case CEC_OPCODE_STANDBY
:
145 HandleStandby(command
);
146 /* pass to listeners */
147 m_busDevice
->GetProcessor()->AddCommand(command
);
150 UnhandledCommand(command
);
151 /* pass to listeners */
152 m_busDevice
->GetProcessor()->AddCommand(command
);
160 strLog
.Format("ignoring frame: we're not at destination %x", command
.destination
);
161 m_busDevice
->AddLog(CEC_LOG_DEBUG
, strLog
.c_str());
168 bool CCECCommandHandler::HandleDeviceCecVersion(const cec_command
&command
)
170 if (command
.parameters
.size
== 1)
172 CCECBusDevice
*device
= GetDevice(command
.initiator
);
174 device
->SetCecVersion((cec_version
) command
.parameters
[0]);
180 bool CCECCommandHandler::HandleDeviceVendorCommandWithId(const cec_command
&command
)
182 SetVendorId(command
);
186 bool CCECCommandHandler::HandleDeviceVendorId(const cec_command
&command
)
188 SetVendorId(command
);
192 bool CCECCommandHandler::HandleGetCecVersion(const cec_command
&command
)
194 CCECBusDevice
*device
= GetDevice(command
.destination
);
196 return device
->TransmitCECVersion(command
.initiator
);
201 bool CCECCommandHandler::HandleGiveAudioStatus(const cec_command
&command
)
203 CCECBusDevice
*device
= GetDevice(command
.destination
);
204 if (device
&& device
->GetType() == CEC_DEVICE_TYPE_AUDIO_SYSTEM
)
205 return ((CCECAudioSystem
*) device
)->TransmitAudioStatus(command
.initiator
);
210 bool CCECCommandHandler::HandleGiveDeckStatus(const cec_command
&command
)
212 CCECBusDevice
*device
= GetDevice(command
.destination
);
214 return device
->TransmitDeckStatus(command
.initiator
);
219 bool CCECCommandHandler::HandleGiveDevicePowerStatus(const cec_command
&command
)
221 CCECBusDevice
*device
= GetDevice(command
.destination
);
223 return device
->TransmitPowerState(command
.initiator
);
228 bool CCECCommandHandler::HandleGiveDeviceVendorId(const cec_command
&command
)
230 CCECBusDevice
*device
= GetDevice(command
.destination
);
232 return device
->TransmitVendorID(command
.initiator
);
237 bool CCECCommandHandler::HandleGiveOSDName(const cec_command
&command
)
239 CCECBusDevice
*device
= GetDevice(command
.destination
);
241 return device
->TransmitOSDName(command
.initiator
);
246 bool CCECCommandHandler::HandleGivePhysicalAddress(const cec_command
&command
)
248 CCECBusDevice
*device
= GetDevice(command
.destination
);
250 return device
->TransmitPhysicalAddress();
255 bool CCECCommandHandler::HandleMenuRequest(const cec_command
&command
)
257 if (command
.parameters
[0] == CEC_MENU_REQUEST_TYPE_QUERY
)
259 CCECBusDevice
*device
= GetDevice(command
.destination
);
261 return device
->TransmitMenuState(command
.initiator
);
266 bool CCECCommandHandler::HandleReportPowerStatus(const cec_command
&command
)
268 if (command
.parameters
.size
== 1)
270 CCECBusDevice
*device
= GetDevice(command
.initiator
);
272 device
->SetPowerStatus((cec_power_status
) command
.parameters
[0]);
277 bool CCECCommandHandler::HandleRequestActiveSource(const cec_command
&command
)
280 strLog
.Format(">> %i requests active source", (uint8_t) command
.initiator
);
281 m_busDevice
->AddLog(CEC_LOG_DEBUG
, strLog
.c_str());
283 vector
<CCECBusDevice
*> devices
;
284 for (int iDevicePtr
= (int)GetMyDevices(devices
)-1; iDevicePtr
>=0; iDevicePtr
--)
285 devices
[iDevicePtr
]->TransmitActiveSource();
289 bool CCECCommandHandler::HandleRoutingChange(const cec_command
&command
)
291 if (command
.parameters
.size
== 4)
293 uint16_t iOldAddress
= ((uint16_t)command
.parameters
[0] << 8) | ((uint16_t)command
.parameters
[1]);
294 uint16_t iNewAddress
= ((uint16_t)command
.parameters
[2] << 8) | ((uint16_t)command
.parameters
[3]);
296 CCECBusDevice
*device
= GetDevice(command
.initiator
);
298 device
->SetStreamPath(iNewAddress
, iOldAddress
);
303 bool CCECCommandHandler::HandleSetMenuLanguage(const cec_command
&command
)
305 if (command
.parameters
.size
== 3)
307 CCECBusDevice
*device
= GetDevice(command
.initiator
);
310 cec_menu_language language
;
311 language
.device
= command
.initiator
;
312 for (uint8_t iPtr
= 0; iPtr
< 4; iPtr
++)
313 language
.language
[iPtr
] = command
.parameters
[iPtr
];
314 language
.language
[3] = 0;
315 device
->SetMenuLanguage(language
);
321 bool CCECCommandHandler::HandleSetStreamPath(const cec_command
&command
)
323 if (command
.parameters
.size
>= 2)
325 int streamaddr
= ((uint16_t)command
.parameters
[0] << 8) | ((uint16_t)command
.parameters
[1]);
327 strLog
.Format(">> %i requests stream path from physical address %04x", command
.initiator
, streamaddr
);
328 m_busDevice
->AddLog(CEC_LOG_DEBUG
, strLog
.c_str());
329 CCECBusDevice
*device
= GetDeviceByPhysicalAddress(streamaddr
);
331 return device
->TransmitActiveSource();
336 bool CCECCommandHandler::HandleSetSystemAudioModeRequest(const cec_command
&command
)
338 if (command
.parameters
.size
>= 1)
340 CCECBusDevice
*device
= GetDevice(command
.destination
);
341 if (device
&& device
->GetType() == CEC_DEVICE_TYPE_AUDIO_SYSTEM
)
342 return ((CCECAudioSystem
*) device
)->SetSystemAudioMode(command
);
347 bool CCECCommandHandler::HandleStandby(const cec_command
&command
)
349 CCECBusDevice
*device
= GetDevice(command
.initiator
);
351 device
->SetPowerStatus(CEC_POWER_STATUS_STANDBY
);
355 bool CCECCommandHandler::HandleGiveSystemAudioModeStatus(const cec_command
&command
)
357 CCECBusDevice
*device
= GetDevice(command
.destination
);
358 if (device
&& device
->GetType() == CEC_DEVICE_TYPE_AUDIO_SYSTEM
)
359 return ((CCECAudioSystem
*) device
)->TransmitSystemAudioModeStatus(command
.initiator
);
364 bool CCECCommandHandler::HandleUserControlPressed(const cec_command
&command
)
366 if (command
.parameters
.size
> 0)
368 m_busDevice
->GetProcessor()->AddKey();
370 if (command
.parameters
[0] <= CEC_USER_CONTROL_CODE_MAX
)
373 strLog
.Format("key pressed: %1x", command
.parameters
[0]);
374 m_busDevice
->AddLog(CEC_LOG_DEBUG
, strLog
.c_str());
376 m_busDevice
->GetProcessor()->SetCurrentButton((cec_user_control_code
) command
.parameters
[0]);
382 bool CCECCommandHandler::HandleUserControlRelease(const cec_command
&command
)
384 m_busDevice
->GetProcessor()->AddKey();
388 void CCECCommandHandler::UnhandledCommand(const cec_command
&command
)
391 strLog
.Format("unhandled command with opcode %02x from address %d", command
.opcode
, command
.initiator
);
392 m_busDevice
->AddLog(CEC_LOG_DEBUG
, strLog
);
395 unsigned int CCECCommandHandler::GetMyDevices(vector
<CCECBusDevice
*> &devices
) const
397 unsigned int iReturn(0);
399 cec_logical_addresses addresses
= m_busDevice
->GetProcessor()->GetLogicalAddresses();
400 for (unsigned int iPtr
= 0; iPtr
< 16; iPtr
++)
404 devices
.push_back(GetDevice((cec_logical_address
) iPtr
));
412 CCECBusDevice
*CCECCommandHandler::GetDevice(cec_logical_address iLogicalAddress
) const
414 CCECBusDevice
*device
= NULL
;
416 if (iLogicalAddress
>= CECDEVICE_TV
&& iLogicalAddress
<= CECDEVICE_BROADCAST
)
417 device
= m_busDevice
->GetProcessor()->m_busDevices
[iLogicalAddress
];
422 CCECBusDevice
*CCECCommandHandler::GetDeviceByPhysicalAddress(uint16_t iPhysicalAddress
) const
424 CCECBusDevice
*device
= NULL
;
426 for (unsigned int iPtr
= 0; iPtr
< 16; iPtr
++)
428 if (m_busDevice
->GetProcessor()->m_busDevices
[iPtr
]->GetPhysicalAddress() == iPhysicalAddress
)
430 device
= m_busDevice
->GetProcessor()->m_busDevices
[iPtr
];
438 void CCECCommandHandler::SetVendorId(const cec_command
&command
)
440 if (command
.parameters
.size
< 3)
442 m_busDevice
->AddLog(CEC_LOG_WARNING
, "invalid vendor ID received");
446 uint64_t iVendorId
= ((uint64_t)command
.parameters
[0] << 16) +
447 ((uint64_t)command
.parameters
[1] << 8) +
448 (uint64_t)command
.parameters
[2];
450 CCECBusDevice
*device
= GetDevice((cec_logical_address
) command
.initiator
);
452 device
->SetVendorId(iVendorId
, command
.parameters
.size
> 3 ? command
.parameters
[3] : 0);
455 const char *CCECCommandHandler::ToString(const cec_logical_address address
)
459 case CECDEVICE_AUDIOSYSTEM
:
461 case CECDEVICE_BROADCAST
:
463 case CECDEVICE_FREEUSE
:
465 case CECDEVICE_PLAYBACKDEVICE1
:
467 case CECDEVICE_PLAYBACKDEVICE2
:
469 case CECDEVICE_PLAYBACKDEVICE3
:
471 case CECDEVICE_RECORDINGDEVICE1
:
473 case CECDEVICE_RECORDINGDEVICE2
:
475 case CECDEVICE_RECORDINGDEVICE3
:
477 case CECDEVICE_RESERVED1
:
479 case CECDEVICE_RESERVED2
:
481 case CECDEVICE_TUNER1
:
483 case CECDEVICE_TUNER2
:
485 case CECDEVICE_TUNER3
:
487 case CECDEVICE_TUNER4
:
496 const char *CCECCommandHandler::ToString(const cec_opcode opcode
)
500 case CEC_OPCODE_ACTIVE_SOURCE
:
501 return "active source";
502 case CEC_OPCODE_IMAGE_VIEW_ON
:
503 return "image view on";
504 case CEC_OPCODE_TEXT_VIEW_ON
:
505 return "text view on";
506 case CEC_OPCODE_INACTIVE_SOURCE
:
507 return "inactive source";
508 case CEC_OPCODE_REQUEST_ACTIVE_SOURCE
:
509 return "request active source";
510 case CEC_OPCODE_ROUTING_CHANGE
:
511 return "routing change";
512 case CEC_OPCODE_ROUTING_INFORMATION
:
513 return "routing information";
514 case CEC_OPCODE_SET_STREAM_PATH
:
515 return "set stream path";
516 case CEC_OPCODE_STANDBY
:
518 case CEC_OPCODE_RECORD_OFF
:
520 case CEC_OPCODE_RECORD_ON
:
522 case CEC_OPCODE_RECORD_STATUS
:
523 return "record status";
524 case CEC_OPCODE_RECORD_TV_SCREEN
:
525 return "record tv screen";
526 case CEC_OPCODE_CLEAR_ANALOGUE_TIMER
:
527 return "clear analogue timer";
528 case CEC_OPCODE_CLEAR_DIGITAL_TIMER
:
529 return "clear digital timer";
530 case CEC_OPCODE_CLEAR_EXTERNAL_TIMER
:
531 return "clear external timer";
532 case CEC_OPCODE_SET_ANALOGUE_TIMER
:
533 return "set analogue timer";
534 case CEC_OPCODE_SET_DIGITAL_TIMER
:
535 return "set digital timer";
536 case CEC_OPCODE_SET_EXTERNAL_TIMER
:
537 return "set external timer";
538 case CEC_OPCODE_SET_TIMER_PROGRAM_TITLE
:
539 return "set timer program title";
540 case CEC_OPCODE_TIMER_CLEARED_STATUS
:
541 return "timer cleared status";
542 case CEC_OPCODE_TIMER_STATUS
:
543 return "timer status";
544 case CEC_OPCODE_CEC_VERSION
:
545 return "cec version";
546 case CEC_OPCODE_GET_CEC_VERSION
:
547 return "get cec version";
548 case CEC_OPCODE_GIVE_PHYSICAL_ADDRESS
:
549 return "give physical address";
550 case CEC_OPCODE_GET_MENU_LANGUAGE
:
551 return "get menu language";
552 case CEC_OPCODE_REPORT_PHYSICAL_ADDRESS
:
553 return "report physical address";
554 case CEC_OPCODE_SET_MENU_LANGUAGE
:
555 return "set menu language";
556 case CEC_OPCODE_DECK_CONTROL
:
557 return "deck control";
558 case CEC_OPCODE_DECK_STATUS
:
559 return "deck status";
560 case CEC_OPCODE_GIVE_DECK_STATUS
:
561 return "give deck status";
562 case CEC_OPCODE_PLAY
:
564 case CEC_OPCODE_GIVE_TUNER_DEVICE_STATUS
:
565 return "give tuner status";
566 case CEC_OPCODE_SELECT_ANALOGUE_SERVICE
:
567 return "select analogue service";
568 case CEC_OPCODE_SELECT_DIGITAL_SERVICE
:
569 return "set digital service";
570 case CEC_OPCODE_TUNER_DEVICE_STATUS
:
571 return "tuner device status";
572 case CEC_OPCODE_TUNER_STEP_DECREMENT
:
573 return "tuner step decrement";
574 case CEC_OPCODE_TUNER_STEP_INCREMENT
:
575 return "tuner step increment";
576 case CEC_OPCODE_DEVICE_VENDOR_ID
:
577 return "device vendor id";
578 case CEC_OPCODE_GIVE_DEVICE_VENDOR_ID
:
579 return "give device vendor id";
580 case CEC_OPCODE_VENDOR_COMMAND
:
581 return "vendor command";
582 case CEC_OPCODE_VENDOR_COMMAND_WITH_ID
:
583 return "vendor command with id";
584 case CEC_OPCODE_VENDOR_REMOTE_BUTTON_DOWN
:
585 return "vendor remote button down";
586 case CEC_OPCODE_VENDOR_REMOTE_BUTTON_UP
:
587 return "vendor remote button up";
588 case CEC_OPCODE_SET_OSD_STRING
:
589 return "set osd string";
590 case CEC_OPCODE_GIVE_OSD_NAME
:
591 return "give osd name";
592 case CEC_OPCODE_SET_OSD_NAME
:
593 return "set osd name";
594 case CEC_OPCODE_MENU_REQUEST
:
595 return "menu request";
596 case CEC_OPCODE_MENU_STATUS
:
597 return "menu status";
598 case CEC_OPCODE_USER_CONTROL_PRESSED
:
599 return "user control pressed";
600 case CEC_OPCODE_USER_CONTROL_RELEASE
:
601 return "user control release";
602 case CEC_OPCODE_GIVE_DEVICE_POWER_STATUS
:
603 return "give device power status";
604 case CEC_OPCODE_REPORT_POWER_STATUS
:
605 return "report power status";
606 case CEC_OPCODE_FEATURE_ABORT
:
607 return "feature abort";
608 case CEC_OPCODE_ABORT
:
610 case CEC_OPCODE_GIVE_AUDIO_STATUS
:
611 return "give audio status";
612 case CEC_OPCODE_GIVE_SYSTEM_AUDIO_MODE_STATUS
:
613 return "give audio mode status";
614 case CEC_OPCODE_REPORT_AUDIO_STATUS
:
615 return "report audio status";
616 case CEC_OPCODE_SET_SYSTEM_AUDIO_MODE
:
617 return "set system audio mode";
618 case CEC_OPCODE_SYSTEM_AUDIO_MODE_REQUEST
:
619 return "system audio mode request";
620 case CEC_OPCODE_SYSTEM_AUDIO_MODE_STATUS
:
621 return "system audio mode status";
622 case CEC_OPCODE_SET_AUDIO_RATE
:
623 return "set audio rate";