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(">> %x -> %x: %s (%2x)", command
.initiator
, 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 m_busDevice
->GetProcessor()->AddCommand(command
);
68 case CEC_OPCODE_GIVE_PHYSICAL_ADDRESS
:
69 HandleGivePhysicalAddress(command
);
71 case CEC_OPCODE_GIVE_OSD_NAME
:
72 HandleGiveOSDName(command
);
74 case CEC_OPCODE_GIVE_DEVICE_VENDOR_ID
:
75 HandleGiveDeviceVendorId(command
);
77 case CEC_OPCODE_DEVICE_VENDOR_ID
:
78 HandleDeviceVendorId(command
);
80 case CEC_OPCODE_VENDOR_COMMAND_WITH_ID
:
81 HandleDeviceVendorCommandWithId(command
);
83 case CEC_OPCODE_GIVE_DECK_STATUS
:
84 HandleGiveDeckStatus(command
);
86 case CEC_OPCODE_MENU_REQUEST
:
87 HandleMenuRequest(command
);
89 case CEC_OPCODE_GIVE_DEVICE_POWER_STATUS
:
90 HandleGiveDevicePowerStatus(command
);
92 case CEC_OPCODE_GET_CEC_VERSION
:
93 HandleGetCecVersion(command
);
95 case CEC_OPCODE_USER_CONTROL_PRESSED
:
96 HandleUserControlPressed(command
);
98 case CEC_OPCODE_USER_CONTROL_RELEASE
:
99 HandleUserControlRelease(command
);
101 case CEC_OPCODE_GIVE_AUDIO_STATUS
:
102 HandleGiveAudioStatus(command
);
104 case CEC_OPCODE_GIVE_SYSTEM_AUDIO_MODE_STATUS
:
105 HandleGiveSystemAudioModeStatus(command
);
107 case CEC_OPCODE_SYSTEM_AUDIO_MODE_REQUEST
:
108 HandleSetSystemAudioModeRequest(command
);
111 UnhandledCommand(command
);
112 m_busDevice
->GetProcessor()->AddCommand(command
);
117 else if (command
.destination
== CECDEVICE_BROADCAST
)
120 switch (command
.opcode
)
122 case CEC_OPCODE_SET_MENU_LANGUAGE
:
123 HandleSetMenuLanguage(command
);
124 m_busDevice
->GetProcessor()->AddCommand(command
);
126 case CEC_OPCODE_REQUEST_ACTIVE_SOURCE
:
127 HandleRequestActiveSource(command
);
129 case CEC_OPCODE_SET_STREAM_PATH
:
130 HandleSetStreamPath(command
);
131 m_busDevice
->GetProcessor()->AddCommand(command
);
133 case CEC_OPCODE_ROUTING_CHANGE
:
134 HandleRoutingChange(command
);
135 m_busDevice
->GetProcessor()->AddCommand(command
);
137 case CEC_OPCODE_DEVICE_VENDOR_ID
:
138 HandleDeviceVendorId(command
);
140 case CEC_OPCODE_VENDOR_COMMAND_WITH_ID
:
141 HandleDeviceVendorCommandWithId(command
);
144 UnhandledCommand(command
);
145 m_busDevice
->GetProcessor()->AddCommand(command
);
153 strLog
.Format("ignoring frame: we're not at destination %x", command
.destination
);
154 m_busDevice
->AddLog(CEC_LOG_DEBUG
, strLog
.c_str());
161 bool CCECCommandHandler::HandleDeviceCecVersion(const cec_command
&command
)
163 if (command
.parameters
.size
== 1)
165 CCECBusDevice
*device
= GetDevice(command
.initiator
);
167 device
->SetCecVersion((cec_version
) command
.parameters
[0]);
173 bool CCECCommandHandler::HandleDeviceVendorCommandWithId(const cec_command
&command
)
175 SetVendorId(command
);
179 bool CCECCommandHandler::HandleDeviceVendorId(const cec_command
&command
)
181 SetVendorId(command
);
185 bool CCECCommandHandler::HandleGetCecVersion(const cec_command
&command
)
187 CCECBusDevice
*device
= GetDevice(command
.destination
);
189 return device
->TransmitCECVersion(command
.initiator
);
194 bool CCECCommandHandler::HandleGiveAudioStatus(const cec_command
&command
)
196 CCECBusDevice
*device
= GetDevice(command
.destination
);
197 if (device
&& device
->GetType() == CEC_DEVICE_TYPE_AUDIO_SYSTEM
)
198 return ((CCECAudioSystem
*) device
)->TransmitAudioStatus(command
.initiator
);
203 bool CCECCommandHandler::HandleGiveDeckStatus(const cec_command
&command
)
205 CCECBusDevice
*device
= GetDevice(command
.destination
);
207 return device
->TransmitDeckStatus(command
.initiator
);
212 bool CCECCommandHandler::HandleGiveDevicePowerStatus(const cec_command
&command
)
214 CCECBusDevice
*device
= GetDevice(command
.destination
);
216 return device
->TransmitPowerState(command
.initiator
);
221 bool CCECCommandHandler::HandleGiveDeviceVendorId(const cec_command
&command
)
223 CCECBusDevice
*device
= GetDevice(command
.destination
);
225 return device
->TransmitVendorID(command
.initiator
);
230 bool CCECCommandHandler::HandleGiveOSDName(const cec_command
&command
)
232 CCECBusDevice
*device
= GetDevice(command
.destination
);
234 return device
->TransmitOSDName(command
.initiator
);
239 bool CCECCommandHandler::HandleGivePhysicalAddress(const cec_command
&command
)
241 CCECBusDevice
*device
= GetDevice(command
.destination
);
243 return device
->TransmitPhysicalAddress();
248 bool CCECCommandHandler::HandleMenuRequest(const cec_command
&command
)
250 if (command
.parameters
[0] == CEC_MENU_REQUEST_TYPE_QUERY
)
252 CCECBusDevice
*device
= GetDevice(command
.destination
);
254 return device
->TransmitMenuState(command
.initiator
);
259 bool CCECCommandHandler::HandleReportPowerStatus(const cec_command
&command
)
261 if (command
.parameters
.size
== 1)
263 CCECBusDevice
*device
= GetDevice(command
.initiator
);
265 device
->SetPowerStatus((cec_power_status
) command
.parameters
[0]);
270 bool CCECCommandHandler::HandleRequestActiveSource(const cec_command
&command
)
273 strLog
.Format(">> %i requests active source", (uint8_t) command
.initiator
);
274 m_busDevice
->AddLog(CEC_LOG_DEBUG
, strLog
.c_str());
276 vector
<CCECBusDevice
*> devices
;
277 for (int iDevicePtr
= (int)GetMyDevices(devices
)-1; iDevicePtr
>=0; iDevicePtr
--)
278 devices
[iDevicePtr
]->TransmitActiveSource();
282 bool CCECCommandHandler::HandleRoutingChange(const cec_command
&command
)
284 if (command
.parameters
.size
== 4)
286 uint16_t iOldAddress
= ((uint16_t)command
.parameters
[0] << 8) | ((uint16_t)command
.parameters
[1]);
287 uint16_t iNewAddress
= ((uint16_t)command
.parameters
[2] << 8) | ((uint16_t)command
.parameters
[3]);
289 CCECBusDevice
*device
= GetDevice(command
.initiator
);
291 device
->SetStreamPath(iNewAddress
, iOldAddress
);
296 bool CCECCommandHandler::HandleSetMenuLanguage(const cec_command
&command
)
298 if (command
.parameters
.size
== 3)
300 CCECBusDevice
*device
= GetDevice(command
.initiator
);
303 cec_menu_language language
;
304 language
.device
= command
.initiator
;
305 for (uint8_t iPtr
= 0; iPtr
< 4; iPtr
++)
306 language
.language
[iPtr
] = command
.parameters
[iPtr
];
307 language
.language
[3] = 0;
308 device
->SetMenuLanguage(language
);
314 bool CCECCommandHandler::HandleSetStreamPath(const cec_command
&command
)
316 if (command
.parameters
.size
>= 2)
318 int streamaddr
= ((uint16_t)command
.parameters
[0] << 8) | ((uint16_t)command
.parameters
[1]);
320 strLog
.Format(">> %i requests stream path from physical address %04x", command
.initiator
, streamaddr
);
321 m_busDevice
->AddLog(CEC_LOG_DEBUG
, strLog
.c_str());
322 CCECBusDevice
*device
= GetDeviceByPhysicalAddress(streamaddr
);
324 return device
->TransmitActiveSource();
329 bool CCECCommandHandler::HandleSetSystemAudioModeRequest(const cec_command
&command
)
331 if (command
.parameters
.size
>= 1)
333 CCECBusDevice
*device
= GetDevice(command
.destination
);
334 if (device
&& device
->GetType() == CEC_DEVICE_TYPE_AUDIO_SYSTEM
)
335 return ((CCECAudioSystem
*) device
)->SetSystemAudioMode(command
);
340 bool CCECCommandHandler::HandleGiveSystemAudioModeStatus(const cec_command
&command
)
342 CCECBusDevice
*device
= GetDevice(command
.destination
);
343 if (device
&& device
->GetType() == CEC_DEVICE_TYPE_AUDIO_SYSTEM
)
344 return ((CCECAudioSystem
*) device
)->TransmitSystemAudioModeStatus(command
.initiator
);
349 bool CCECCommandHandler::HandleUserControlPressed(const cec_command
&command
)
351 if (command
.parameters
.size
> 0)
353 m_busDevice
->GetProcessor()->AddKey();
355 if (command
.parameters
[0] <= CEC_USER_CONTROL_CODE_MAX
)
358 strLog
.Format("key pressed: %1x", command
.parameters
[0]);
359 m_busDevice
->AddLog(CEC_LOG_DEBUG
, strLog
.c_str());
361 m_busDevice
->GetProcessor()->SetCurrentButton((cec_user_control_code
) command
.parameters
[0]);
367 bool CCECCommandHandler::HandleUserControlRelease(const cec_command
&command
)
369 m_busDevice
->GetProcessor()->AddKey();
373 void CCECCommandHandler::UnhandledCommand(const cec_command
&command
)
376 strLog
.Format("unhandled command with opcode %02x from address %d", command
.opcode
, command
.initiator
);
377 m_busDevice
->AddLog(CEC_LOG_DEBUG
, strLog
);
380 unsigned int CCECCommandHandler::GetMyDevices(vector
<CCECBusDevice
*> &devices
) const
382 unsigned int iReturn(0);
384 cec_logical_addresses addresses
= m_busDevice
->GetProcessor()->GetLogicalAddresses();
385 for (unsigned int iPtr
= 0; iPtr
< 16; iPtr
++)
389 devices
.push_back(GetDevice((cec_logical_address
) iPtr
));
397 CCECBusDevice
*CCECCommandHandler::GetDevice(cec_logical_address iLogicalAddress
) const
399 CCECBusDevice
*device
= NULL
;
401 if (iLogicalAddress
>= CECDEVICE_TV
&& iLogicalAddress
<= CECDEVICE_BROADCAST
)
402 device
= m_busDevice
->GetProcessor()->m_busDevices
[iLogicalAddress
];
407 CCECBusDevice
*CCECCommandHandler::GetDeviceByPhysicalAddress(uint16_t iPhysicalAddress
) const
409 CCECBusDevice
*device
= NULL
;
411 for (unsigned int iPtr
= 0; iPtr
< 16; iPtr
++)
413 if (m_busDevice
->GetProcessor()->m_busDevices
[iPtr
]->GetPhysicalAddress() == iPhysicalAddress
)
415 device
= m_busDevice
->GetProcessor()->m_busDevices
[iPtr
];
423 void CCECCommandHandler::SetVendorId(const cec_command
&command
)
425 if (command
.parameters
.size
< 3)
427 m_busDevice
->AddLog(CEC_LOG_WARNING
, "invalid vendor ID received");
431 uint64_t iVendorId
= ((uint64_t)command
.parameters
[0] << 16) +
432 ((uint64_t)command
.parameters
[1] << 8) +
433 (uint64_t)command
.parameters
[2];
435 CCECBusDevice
*device
= GetDevice((cec_logical_address
) command
.initiator
);
437 device
->SetVendorId(iVendorId
, command
.parameters
.size
> 3 ? command
.parameters
[3] : 0);
440 const char *CCECCommandHandler::ToString(const cec_opcode opcode
)
444 case CEC_OPCODE_ACTIVE_SOURCE
:
445 return "active source";
446 case CEC_OPCODE_IMAGE_VIEW_ON
:
447 return "image view on";
448 case CEC_OPCODE_TEXT_VIEW_ON
:
449 return "text view on";
450 case CEC_OPCODE_INACTIVE_SOURCE
:
451 return "inactive source";
452 case CEC_OPCODE_REQUEST_ACTIVE_SOURCE
:
453 return "request active source";
454 case CEC_OPCODE_ROUTING_CHANGE
:
455 return "routing change";
456 case CEC_OPCODE_ROUTING_INFORMATION
:
457 return "routing information";
458 case CEC_OPCODE_SET_STREAM_PATH
:
459 return "set stream path";
460 case CEC_OPCODE_STANDBY
:
462 case CEC_OPCODE_RECORD_OFF
:
464 case CEC_OPCODE_RECORD_ON
:
466 case CEC_OPCODE_RECORD_STATUS
:
467 return "record status";
468 case CEC_OPCODE_RECORD_TV_SCREEN
:
469 return "record tv screen";
470 case CEC_OPCODE_CLEAR_ANALOGUE_TIMER
:
471 return "clear analogue timer";
472 case CEC_OPCODE_CLEAR_DIGITAL_TIMER
:
473 return "clear digital timer";
474 case CEC_OPCODE_CLEAR_EXTERNAL_TIMER
:
475 return "clear external timer";
476 case CEC_OPCODE_SET_ANALOGUE_TIMER
:
477 return "set analogue timer";
478 case CEC_OPCODE_SET_DIGITAL_TIMER
:
479 return "set digital timer";
480 case CEC_OPCODE_SET_EXTERNAL_TIMER
:
481 return "set external timer";
482 case CEC_OPCODE_SET_TIMER_PROGRAM_TITLE
:
483 return "set timer program title";
484 case CEC_OPCODE_TIMER_CLEARED_STATUS
:
485 return "timer cleared status";
486 case CEC_OPCODE_TIMER_STATUS
:
487 return "timer status";
488 case CEC_OPCODE_CEC_VERSION
:
489 return "cec version";
490 case CEC_OPCODE_GET_CEC_VERSION
:
491 return "get cec version";
492 case CEC_OPCODE_GIVE_PHYSICAL_ADDRESS
:
493 return "give physical address";
494 case CEC_OPCODE_GET_MENU_LANGUAGE
:
495 return "get menu language";
496 case CEC_OPCODE_REPORT_PHYSICAL_ADDRESS
:
497 return "report physical address";
498 case CEC_OPCODE_SET_MENU_LANGUAGE
:
499 return "set menu language";
500 case CEC_OPCODE_DECK_CONTROL
:
501 return "deck control";
502 case CEC_OPCODE_DECK_STATUS
:
503 return "deck status";
504 case CEC_OPCODE_GIVE_DECK_STATUS
:
505 return "give deck status";
506 case CEC_OPCODE_PLAY
:
508 case CEC_OPCODE_GIVE_TUNER_DEVICE_STATUS
:
509 return "give tuner status";
510 case CEC_OPCODE_SELECT_ANALOGUE_SERVICE
:
511 return "select analogue service";
512 case CEC_OPCODE_SELECT_DIGITAL_SERVICE
:
513 return "set digital service";
514 case CEC_OPCODE_TUNER_DEVICE_STATUS
:
515 return "tuner device status";
516 case CEC_OPCODE_TUNER_STEP_DECREMENT
:
517 return "tuner step decrement";
518 case CEC_OPCODE_TUNER_STEP_INCREMENT
:
519 return "tuner step increment";
520 case CEC_OPCODE_DEVICE_VENDOR_ID
:
521 return "device vendor id";
522 case CEC_OPCODE_GIVE_DEVICE_VENDOR_ID
:
523 return "give device vendor id";
524 case CEC_OPCODE_VENDOR_COMMAND
:
525 return "vendor command";
526 case CEC_OPCODE_VENDOR_COMMAND_WITH_ID
:
527 return "vendor command with id";
528 case CEC_OPCODE_VENDOR_REMOTE_BUTTON_DOWN
:
529 return "vendor remote button down";
530 case CEC_OPCODE_VENDOR_REMOTE_BUTTON_UP
:
531 return "vendor remote button up";
532 case CEC_OPCODE_SET_OSD_STRING
:
533 return "set osd string";
534 case CEC_OPCODE_GIVE_OSD_NAME
:
535 return "give osd name";
536 case CEC_OPCODE_SET_OSD_NAME
:
537 return "set osd name";
538 case CEC_OPCODE_MENU_REQUEST
:
539 return "menu request";
540 case CEC_OPCODE_MENU_STATUS
:
541 return "menu status";
542 case CEC_OPCODE_USER_CONTROL_PRESSED
:
543 return "user control pressed";
544 case CEC_OPCODE_USER_CONTROL_RELEASE
:
545 return "user control release";
546 case CEC_OPCODE_GIVE_DEVICE_POWER_STATUS
:
547 return "give device power status";
548 case CEC_OPCODE_REPORT_POWER_STATUS
:
549 return "report power status";
550 case CEC_OPCODE_FEATURE_ABORT
:
551 return "feature abort";
552 case CEC_OPCODE_ABORT
:
554 case CEC_OPCODE_GIVE_AUDIO_STATUS
:
555 return "give audio status";
556 case CEC_OPCODE_GIVE_SYSTEM_AUDIO_MODE_STATUS
:
557 return "give audio mode status";
558 case CEC_OPCODE_REPORT_AUDIO_STATUS
:
559 return "report audio status";
560 case CEC_OPCODE_SET_SYSTEM_AUDIO_MODE
:
561 return "set system audio mode";
562 case CEC_OPCODE_SYSTEM_AUDIO_MODE_REQUEST
:
563 return "system audio mode request";
564 case CEC_OPCODE_SYSTEM_AUDIO_MODE_STATUS
:
565 return "system audio mode status";
566 case CEC_OPCODE_SET_AUDIO_RATE
:
567 return "set audio rate";