cec: safe deletes for members
[deb_libcec.git] / src / lib / implementations / CECCommandHandler.cpp
1 /*
2 * This file is part of the libCEC(R) library.
3 *
4 * libCEC(R) is Copyright (C) 2011-2012 Pulse-Eight Limited. All rights reserved.
5 * libCEC(R) is an original work, containing original code.
6 *
7 * libCEC(R) is a trademark of Pulse-Eight Limited.
8 *
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.
13 *
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.
18 *
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.
22 *
23 *
24 * Alternatively, you can license this library under a commercial license,
25 * please contact Pulse-Eight Licensing for more information.
26 *
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/
31 */
32
33 #include "CECCommandHandler.h"
34 #include "../devices/CECBusDevice.h"
35 #include "../devices/CECAudioSystem.h"
36 #include "../devices/CECPlaybackDevice.h"
37 #include "../CECClient.h"
38 #include "../CECProcessor.h"
39 #include "../LibCEC.h"
40 #include "../platform/util/util.h"
41
42 using namespace CEC;
43 using namespace std;
44 using namespace PLATFORM;
45
46 #define LIB_CEC m_busDevice->GetProcessor()->GetLib()
47 #define ToString(p) LIB_CEC->ToString(p)
48
49 CCECCommandHandler::CCECCommandHandler(CCECBusDevice *busDevice) :
50 m_busDevice(busDevice),
51 m_processor(m_busDevice->GetProcessor()),
52 m_iTransmitTimeout(CEC_DEFAULT_TRANSMIT_TIMEOUT),
53 m_iTransmitWait(CEC_DEFAULT_TRANSMIT_WAIT),
54 m_iTransmitRetries(CEC_DEFAULT_TRANSMIT_RETRIES),
55 m_bHandlerInited(false),
56 m_bOPTSendDeckStatusUpdateOnActiveSource(false),
57 m_vendorId(CEC_VENDOR_UNKNOWN),
58 m_waitForResponse(new CWaitForResponse)
59 {
60 }
61
62 CCECCommandHandler::~CCECCommandHandler(void)
63 {
64 DELETE_AND_NULL(m_waitForResponse);
65 }
66
67 bool CCECCommandHandler::HandleCommand(const cec_command &command)
68 {
69 if (command.opcode_set == 0)
70 return HandlePoll(command);
71
72 bool bHandled(true);
73
74 CCECClient *client = m_busDevice->GetClient();
75 if (client)
76 client->AddCommand(command);
77
78 switch(command.opcode)
79 {
80 case CEC_OPCODE_REPORT_POWER_STATUS:
81 HandleReportPowerStatus(command);
82 break;
83 case CEC_OPCODE_CEC_VERSION:
84 HandleDeviceCecVersion(command);
85 break;
86 case CEC_OPCODE_SET_MENU_LANGUAGE:
87 HandleSetMenuLanguage(command);
88 break;
89 case CEC_OPCODE_GIVE_PHYSICAL_ADDRESS:
90 if (m_processor->CECInitialised())
91 HandleGivePhysicalAddress(command);
92 break;
93 case CEC_OPCODE_GET_MENU_LANGUAGE:
94 if (m_processor->CECInitialised())
95 HandleGiveMenuLanguage(command);
96 break;
97 case CEC_OPCODE_GIVE_OSD_NAME:
98 if (m_processor->CECInitialised())
99 HandleGiveOSDName(command);
100 break;
101 case CEC_OPCODE_GIVE_DEVICE_VENDOR_ID:
102 if (m_processor->CECInitialised())
103 HandleGiveDeviceVendorId(command);
104 break;
105 case CEC_OPCODE_DEVICE_VENDOR_ID:
106 HandleDeviceVendorId(command);
107 break;
108 case CEC_OPCODE_VENDOR_COMMAND_WITH_ID:
109 HandleDeviceVendorCommandWithId(command);
110 break;
111 case CEC_OPCODE_GIVE_DECK_STATUS:
112 if (m_processor->CECInitialised())
113 HandleGiveDeckStatus(command);
114 break;
115 case CEC_OPCODE_DECK_CONTROL:
116 HandleDeckControl(command);
117 break;
118 case CEC_OPCODE_MENU_REQUEST:
119 if (m_processor->CECInitialised())
120 HandleMenuRequest(command);
121 break;
122 case CEC_OPCODE_GIVE_DEVICE_POWER_STATUS:
123 if (m_processor->CECInitialised())
124 HandleGiveDevicePowerStatus(command);
125 break;
126 case CEC_OPCODE_GET_CEC_VERSION:
127 if (m_processor->CECInitialised())
128 HandleGetCecVersion(command);
129 break;
130 case CEC_OPCODE_USER_CONTROL_PRESSED:
131 if (m_processor->CECInitialised())
132 HandleUserControlPressed(command);
133 break;
134 case CEC_OPCODE_USER_CONTROL_RELEASE:
135 if (m_processor->CECInitialised())
136 HandleUserControlRelease(command);
137 break;
138 case CEC_OPCODE_GIVE_AUDIO_STATUS:
139 if (m_processor->CECInitialised())
140 HandleGiveAudioStatus(command);
141 break;
142 case CEC_OPCODE_GIVE_SYSTEM_AUDIO_MODE_STATUS:
143 if (m_processor->CECInitialised())
144 HandleGiveSystemAudioModeStatus(command);
145 break;
146 case CEC_OPCODE_SYSTEM_AUDIO_MODE_REQUEST:
147 if (m_processor->CECInitialised())
148 HandleSystemAudioModeRequest(command);
149 break;
150 case CEC_OPCODE_REPORT_AUDIO_STATUS:
151 HandleReportAudioStatus(command);
152 break;
153 case CEC_OPCODE_SYSTEM_AUDIO_MODE_STATUS:
154 HandleSystemAudioModeStatus(command);
155 break;
156 case CEC_OPCODE_SET_SYSTEM_AUDIO_MODE:
157 HandleSetSystemAudioMode(command);
158 break;
159 case CEC_OPCODE_REQUEST_ACTIVE_SOURCE:
160 if (m_processor->CECInitialised())
161 HandleRequestActiveSource(command);
162 break;
163 case CEC_OPCODE_SET_STREAM_PATH:
164 HandleSetStreamPath(command);
165 break;
166 case CEC_OPCODE_ROUTING_CHANGE:
167 HandleRoutingChange(command);
168 break;
169 case CEC_OPCODE_ROUTING_INFORMATION:
170 HandleRoutingInformation(command);
171 break;
172 case CEC_OPCODE_STANDBY:
173 if (m_processor->CECInitialised())
174 HandleStandby(command);
175 break;
176 case CEC_OPCODE_ACTIVE_SOURCE:
177 HandleActiveSource(command);
178 break;
179 case CEC_OPCODE_REPORT_PHYSICAL_ADDRESS:
180 HandleReportPhysicalAddress(command);
181 break;
182 case CEC_OPCODE_SET_OSD_NAME:
183 HandleSetOSDName(command);
184 break;
185 case CEC_OPCODE_IMAGE_VIEW_ON:
186 HandleImageViewOn(command);
187 break;
188 case CEC_OPCODE_TEXT_VIEW_ON:
189 HandleTextViewOn(command);
190 break;
191 case CEC_OPCODE_FEATURE_ABORT:
192 HandleFeatureAbort(command);
193 break;
194 case CEC_OPCODE_VENDOR_COMMAND:
195 HandleVendorCommand(command);
196 break;
197 default:
198 bHandled = false;
199 break;
200 }
201
202 if (bHandled)
203 m_waitForResponse->Received((command.opcode == CEC_OPCODE_FEATURE_ABORT && command.parameters.size > 0) ? (cec_opcode)command.parameters[0] : command.opcode);
204 else
205 UnhandledCommand(command);
206
207 return bHandled;
208 }
209
210 bool CCECCommandHandler::HandleActiveSource(const cec_command &command)
211 {
212 if (command.parameters.size == 2)
213 {
214 uint16_t iAddress = ((uint16_t)command.parameters[0] << 8) | ((uint16_t)command.parameters[1]);
215 CCECBusDevice *device = m_processor->GetDeviceByPhysicalAddress(iAddress);
216 if (device)
217 device->MarkAsActiveSource();
218 }
219
220 return true;
221 }
222
223 bool CCECCommandHandler::HandleDeckControl(const cec_command &command)
224 {
225 CCECPlaybackDevice *device = CCECBusDevice::AsPlaybackDevice(GetDevice(command.destination));
226 if (device && command.parameters.size > 0)
227 {
228 device->SetDeckControlMode((cec_deck_control_mode) command.parameters[0]);
229 return true;
230 }
231
232 return false;
233 }
234
235 bool CCECCommandHandler::HandleDeviceCecVersion(const cec_command &command)
236 {
237 if (command.parameters.size == 1)
238 {
239 CCECBusDevice *device = GetDevice(command.initiator);
240 if (device)
241 device->SetCecVersion((cec_version) command.parameters[0]);
242 }
243
244 return true;
245 }
246
247 bool CCECCommandHandler::HandleDeviceVendorCommandWithId(const cec_command &command)
248 {
249 if (m_processor->CECInitialised() && m_processor->IsHandledByLibCEC(command.destination))
250 m_processor->TransmitAbort(command.destination, command.initiator, command.opcode, CEC_ABORT_REASON_REFUSED);
251
252 return true;
253 }
254
255 bool CCECCommandHandler::HandleDeviceVendorId(const cec_command &command)
256 {
257 return SetVendorId(command);
258 }
259
260 bool CCECCommandHandler::HandleFeatureAbort(const cec_command &command)
261 {
262 if (command.parameters.size == 2 &&
263 (command.parameters[1] == CEC_ABORT_REASON_UNRECOGNIZED_OPCODE ||
264 command.parameters[1] == CEC_ABORT_REASON_REFUSED))
265 m_processor->GetDevice(command.initiator)->SetUnsupportedFeature((cec_opcode)command.parameters[0]);
266 return true;
267 }
268
269 bool CCECCommandHandler::HandleGetCecVersion(const cec_command &command)
270 {
271 if (m_processor->CECInitialised() && m_processor->IsHandledByLibCEC(command.destination))
272 {
273 CCECBusDevice *device = GetDevice(command.destination);
274 if (device)
275 return device->TransmitCECVersion(command.initiator);
276 }
277
278 return false;
279 }
280
281 bool CCECCommandHandler::HandleGiveAudioStatus(const cec_command &command)
282 {
283 if (m_processor->CECInitialised() && m_processor->IsHandledByLibCEC(command.destination))
284 {
285 CCECAudioSystem *device = CCECBusDevice::AsAudioSystem(GetDevice(command.destination));
286 if (device)
287 return device->TransmitAudioStatus(command.initiator);
288 }
289
290 return false;
291 }
292
293 bool CCECCommandHandler::HandleGiveDeckStatus(const cec_command &command)
294 {
295 if (m_processor->CECInitialised() && m_processor->IsHandledByLibCEC(command.destination))
296 {
297 CCECPlaybackDevice *device = CCECBusDevice::AsPlaybackDevice(GetDevice(command.destination));
298 if (device)
299 return device->TransmitDeckStatus(command.initiator);
300 }
301
302 return false;
303 }
304
305 bool CCECCommandHandler::HandleGiveDevicePowerStatus(const cec_command &command)
306 {
307 if (m_processor->CECInitialised() && m_processor->IsHandledByLibCEC(command.destination))
308 {
309 CCECBusDevice *device = GetDevice(command.destination);
310 if (device)
311 return device->TransmitPowerState(command.initiator);
312 }
313
314 return false;
315 }
316
317 bool CCECCommandHandler::HandleGiveDeviceVendorId(const cec_command &command)
318 {
319 if (m_processor->CECInitialised() && m_processor->IsHandledByLibCEC(command.destination))
320 {
321 CCECBusDevice *device = GetDevice(command.destination);
322 if (device)
323 return device->TransmitVendorID(command.initiator);
324 }
325
326 return false;
327 }
328
329 bool CCECCommandHandler::HandleGiveOSDName(const cec_command &command)
330 {
331 if (m_processor->CECInitialised() && m_processor->IsHandledByLibCEC(command.destination))
332 {
333 CCECBusDevice *device = GetDevice(command.destination);
334 if (device)
335 return device->TransmitOSDName(command.initiator);
336 }
337
338 return false;
339 }
340
341 bool CCECCommandHandler::HandleGivePhysicalAddress(const cec_command &command)
342 {
343 if (m_processor->CECInitialised() && m_processor->IsHandledByLibCEC(command.destination))
344 {
345 CCECBusDevice *device = GetDevice(command.destination);
346 if (device)
347 return device->TransmitPhysicalAddress();
348 }
349
350 return false;
351 }
352
353 bool CCECCommandHandler::HandleGiveMenuLanguage(const cec_command &command)
354 {
355 if (m_processor->CECInitialised() && m_processor->IsHandledByLibCEC(command.destination))
356 {
357 CCECBusDevice *device = GetDevice(command.destination);
358 if (device)
359 return device->TransmitSetMenuLanguage(command.initiator);
360 }
361
362 return false;
363 }
364
365 bool CCECCommandHandler::HandleGiveSystemAudioModeStatus(const cec_command &command)
366 {
367 if (m_processor->CECInitialised() && m_processor->IsHandledByLibCEC(command.destination))
368 {
369 CCECAudioSystem *device = CCECBusDevice::AsAudioSystem(GetDevice(command.destination));
370 if (device)
371 return device->TransmitSystemAudioModeStatus(command.initiator);
372 }
373
374 return false;
375 }
376
377 bool CCECCommandHandler::HandleImageViewOn(const cec_command &command)
378 {
379 m_processor->GetDevice(command.initiator)->MarkAsActiveSource();
380 return true;
381 }
382
383 bool CCECCommandHandler::HandleMenuRequest(const cec_command &command)
384 {
385 if (m_processor->CECInitialised() && m_processor->IsHandledByLibCEC(command.destination))
386 {
387 CCECBusDevice *device = GetDevice(command.destination);
388 if (device)
389 {
390 CCECClient *client = device->GetClient();
391 if (client)
392 {
393 if (command.parameters[0] == CEC_MENU_REQUEST_TYPE_ACTIVATE)
394 {
395 if (client->MenuStateChanged(CEC_MENU_STATE_ACTIVATED) == 1)
396 device->SetMenuState(CEC_MENU_STATE_ACTIVATED);
397 }
398 else if (command.parameters[0] == CEC_MENU_REQUEST_TYPE_DEACTIVATE)
399 {
400 if (client->MenuStateChanged(CEC_MENU_STATE_DEACTIVATED) == 1)
401 device->SetMenuState(CEC_MENU_STATE_DEACTIVATED);
402 }
403 }
404 return device->TransmitMenuState(command.initiator);
405 }
406 }
407
408 return false;
409 }
410
411 bool CCECCommandHandler::HandlePoll(const cec_command &command)
412 {
413 m_busDevice->HandlePoll(command.destination);
414 return true;
415 }
416
417 bool CCECCommandHandler::HandleReportAudioStatus(const cec_command &command)
418 {
419 if (command.parameters.size == 1)
420 {
421 CCECAudioSystem *device = CCECBusDevice::AsAudioSystem(GetDevice(command.initiator));
422 if (device)
423 {
424 device->SetAudioStatus(command.parameters[0]);
425 return true;
426 }
427 }
428 return false;
429 }
430
431 bool CCECCommandHandler::HandleReportPhysicalAddress(const cec_command &command)
432 {
433 if (command.parameters.size == 3)
434 {
435 uint16_t iNewAddress = ((uint16_t)command.parameters[0] << 8) | ((uint16_t)command.parameters[1]);
436 SetPhysicalAddress(command.initiator, iNewAddress);
437 }
438 return true;
439 }
440
441 bool CCECCommandHandler::HandleReportPowerStatus(const cec_command &command)
442 {
443 if (command.parameters.size == 1)
444 {
445 CCECBusDevice *device = GetDevice(command.initiator);
446 if (device)
447 device->SetPowerStatus((cec_power_status) command.parameters[0]);
448 }
449 return true;
450 }
451
452 bool CCECCommandHandler::HandleRequestActiveSource(const cec_command &command)
453 {
454 if (m_processor->CECInitialised())
455 {
456 LIB_CEC->AddLog(CEC_LOG_DEBUG, ">> %i requests active source", (uint8_t) command.initiator);
457 m_processor->GetDevice(command.initiator)->SetPowerStatus(CEC_POWER_STATUS_ON);
458
459 vector<CCECBusDevice *> devices;
460 for (size_t iDevicePtr = 0; iDevicePtr < GetMyDevices(devices); iDevicePtr++)
461 devices[iDevicePtr]->TransmitActiveSource();
462
463 return true;
464 }
465 return false;
466 }
467
468 bool CCECCommandHandler::HandleRoutingChange(const cec_command &command)
469 {
470 if (command.parameters.size == 4)
471 {
472 uint16_t iOldAddress = ((uint16_t)command.parameters[0] << 8) | ((uint16_t)command.parameters[1]);
473 uint16_t iNewAddress = ((uint16_t)command.parameters[2] << 8) | ((uint16_t)command.parameters[3]);
474
475 CCECBusDevice *device = GetDevice(command.initiator);
476 if (device)
477 device->SetStreamPath(iNewAddress, iOldAddress);
478 }
479 return true;
480 }
481
482 bool CCECCommandHandler::HandleRoutingInformation(const cec_command &command)
483 {
484 if (command.parameters.size == 2)
485 {
486 uint16_t iNewAddress = ((uint16_t)command.parameters[0] << 8) | ((uint16_t)command.parameters[1]);
487 CCECBusDevice *device = m_processor->GetDeviceByPhysicalAddress(iNewAddress);
488 if (device)
489 device->MarkAsActiveSource();
490 }
491
492 return false;
493 }
494
495 bool CCECCommandHandler::HandleSetMenuLanguage(const cec_command &command)
496 {
497 if (command.parameters.size == 3)
498 {
499 CCECBusDevice *device = GetDevice(command.initiator);
500 if (device)
501 {
502 cec_menu_language language;
503 language.device = command.initiator;
504 for (uint8_t iPtr = 0; iPtr < 4; iPtr++)
505 language.language[iPtr] = command.parameters[iPtr];
506 language.language[3] = 0;
507 device->SetMenuLanguage(language);
508 return true;
509 }
510 }
511 return false;
512 }
513
514 bool CCECCommandHandler::HandleSetOSDName(const cec_command &command)
515 {
516 if (command.parameters.size > 0)
517 {
518 CCECBusDevice *device = GetDevice(command.initiator);
519 if (device)
520 {
521 char buf[1024];
522 for (uint8_t iPtr = 0; iPtr < command.parameters.size; iPtr++)
523 buf[iPtr] = (char)command.parameters[iPtr];
524 buf[command.parameters.size] = 0;
525
526 CStdString strName(buf);
527 device->SetOSDName(strName);
528
529 return true;
530 }
531 }
532 return false;
533 }
534
535 bool CCECCommandHandler::HandleSetStreamPath(const cec_command &command)
536 {
537 if (m_processor->CECInitialised() && command.parameters.size >= 2)
538 {
539 uint16_t iStreamAddress = ((uint16_t)command.parameters[0] << 8) | ((uint16_t)command.parameters[1]);
540 LIB_CEC->AddLog(CEC_LOG_DEBUG, ">> %i sets stream path to physical address %04x", command.initiator, iStreamAddress);
541
542 /* one of the device handled by libCEC has been made active */
543 CCECBusDevice *device = GetDeviceByPhysicalAddress(iStreamAddress);
544 if (device && device->IsHandledByLibCEC())
545 device->ActivateSource();
546 }
547 return false;
548 }
549
550 bool CCECCommandHandler::HandleSystemAudioModeRequest(const cec_command &command)
551 {
552 if (m_processor->CECInitialised() && m_processor->IsHandledByLibCEC(command.destination))
553 {
554 CCECAudioSystem *device = CCECBusDevice::AsAudioSystem(GetDevice(command.destination));
555 if (device)
556 {
557 if (command.parameters.size >= 2)
558 {
559 device->SetPowerStatus(CEC_POWER_STATUS_ON);
560 device->SetSystemAudioModeStatus(CEC_SYSTEM_AUDIO_STATUS_ON);
561 uint16_t iNewAddress = ((uint16_t)command.parameters[0] << 8) | ((uint16_t)command.parameters[1]);
562 CCECBusDevice *newActiveDevice = GetDeviceByPhysicalAddress(iNewAddress);
563 if (newActiveDevice)
564 newActiveDevice->MarkAsActiveSource();
565 return device->TransmitSetSystemAudioMode(command.initiator);
566 }
567 else
568 {
569 device->SetSystemAudioModeStatus(CEC_SYSTEM_AUDIO_STATUS_OFF);
570 return device->TransmitSetSystemAudioMode(command.initiator);
571 }
572 }
573 }
574 return false;
575 }
576
577 bool CCECCommandHandler::HandleStandby(const cec_command &command)
578 {
579 CCECBusDevice *device = GetDevice(command.initiator);
580 if (device)
581 device->SetPowerStatus(CEC_POWER_STATUS_STANDBY);
582
583 return true;
584 }
585
586 bool CCECCommandHandler::HandleSystemAudioModeStatus(const cec_command &command)
587 {
588 if (command.parameters.size == 1)
589 {
590 CCECAudioSystem *device = CCECBusDevice::AsAudioSystem(GetDevice(command.initiator));
591 if (device)
592 {
593 device->SetSystemAudioModeStatus((cec_system_audio_status)command.parameters[0]);
594 return true;
595 }
596 }
597
598 return false;
599 }
600
601 bool CCECCommandHandler::HandleSetSystemAudioMode(const cec_command &command)
602 {
603 if (command.parameters.size == 1)
604 {
605 CCECAudioSystem *device = CCECBusDevice::AsAudioSystem(GetDevice(command.initiator));
606 if (device)
607 {
608 device->SetSystemAudioModeStatus((cec_system_audio_status)command.parameters[0]);
609 return true;
610 }
611 }
612
613 return false;
614 }
615
616 bool CCECCommandHandler::HandleTextViewOn(const cec_command &command)
617 {
618 m_processor->GetDevice(command.initiator)->MarkAsActiveSource();
619 return true;
620 }
621
622 bool CCECCommandHandler::HandleUserControlPressed(const cec_command &command)
623 {
624 if (m_processor->CECInitialised() &&
625 m_processor->IsHandledByLibCEC(command.destination) &&
626 command.parameters.size > 0)
627 {
628 CCECBusDevice *device = GetDevice(command.destination);
629 if (!device)
630 return true;
631
632 CCECClient *client = device->GetClient();
633 if (client)
634 client->AddKey();
635
636 if (command.parameters[0] <= CEC_USER_CONTROL_CODE_MAX)
637 client->SetCurrentButton((cec_user_control_code) command.parameters[0]);
638
639 if (command.parameters[0] == CEC_USER_CONTROL_CODE_POWER ||
640 command.parameters[0] == CEC_USER_CONTROL_CODE_POWER_ON_FUNCTION)
641 {
642 bool bPowerOn(true);
643 if (!device)
644 return true;
645
646 // CEC_USER_CONTROL_CODE_POWER operates as a toggle
647 // assume CEC_USER_CONTROL_CODE_POWER_ON_FUNCTION does not
648 if (command.parameters[0] == CEC_USER_CONTROL_CODE_POWER)
649 {
650 cec_power_status status = device->GetCurrentPowerStatus();
651 bPowerOn = !(status == CEC_POWER_STATUS_ON || status == CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON);
652 }
653
654 if (bPowerOn)
655 {
656 device->ActivateSource();
657 }
658 else
659 {
660 device->MarkAsInactiveSource();
661 device->TransmitInactiveSource();
662 device->SetMenuState(CEC_MENU_STATE_DEACTIVATED);
663 }
664 }
665
666 return true;
667 }
668 return false;
669 }
670
671 bool CCECCommandHandler::HandleUserControlRelease(const cec_command &command)
672 {
673 CCECClient *client = m_processor->GetClient(command.destination);
674 if (client)
675 client->AddKey();
676 return true;
677 }
678
679 bool CCECCommandHandler::HandleVendorCommand(const cec_command & UNUSED(command))
680 {
681 return true;
682 }
683
684 void CCECCommandHandler::UnhandledCommand(const cec_command &command)
685 {
686 LIB_CEC->AddLog(CEC_LOG_DEBUG, "unhandled command with opcode %02x from address %d", command.opcode, command.initiator);
687
688 if (m_processor->CECInitialised() && m_processor->IsHandledByLibCEC(command.destination))
689 m_processor->TransmitAbort(m_busDevice->GetLogicalAddress(), command.initiator, command.opcode, CEC_ABORT_REASON_UNRECOGNIZED_OPCODE);
690 }
691
692 size_t CCECCommandHandler::GetMyDevices(vector<CCECBusDevice *> &devices) const
693 {
694 size_t iReturn(0);
695
696 cec_logical_addresses addresses = m_processor->GetLogicalAddresses();
697 for (uint8_t iPtr = CECDEVICE_TV; iPtr < CECDEVICE_BROADCAST; iPtr++)
698 {
699 if (addresses[iPtr])
700 {
701 devices.push_back(GetDevice((cec_logical_address) iPtr));
702 ++iReturn;
703 }
704 }
705
706 return iReturn;
707 }
708
709 CCECBusDevice *CCECCommandHandler::GetDevice(cec_logical_address iLogicalAddress) const
710 {
711 return m_processor->GetDevice(iLogicalAddress);
712 }
713
714 CCECBusDevice *CCECCommandHandler::GetDeviceByPhysicalAddress(uint16_t iPhysicalAddress) const
715 {
716 return m_processor->GetDeviceByPhysicalAddress(iPhysicalAddress);
717 }
718
719 bool CCECCommandHandler::SetVendorId(const cec_command &command)
720 {
721 bool bChanged(false);
722 if (command.parameters.size < 3)
723 {
724 LIB_CEC->AddLog(CEC_LOG_WARNING, "invalid vendor ID received");
725 return bChanged;
726 }
727
728 uint64_t iVendorId = ((uint64_t)command.parameters[0] << 16) +
729 ((uint64_t)command.parameters[1] << 8) +
730 (uint64_t)command.parameters[2];
731
732 CCECBusDevice *device = GetDevice((cec_logical_address) command.initiator);
733 if (device)
734 bChanged = device->SetVendorId(iVendorId);
735 return bChanged;
736 }
737
738 void CCECCommandHandler::SetPhysicalAddress(cec_logical_address iAddress, uint16_t iNewAddress)
739 {
740 if (!m_processor->IsHandledByLibCEC(iAddress))
741 {
742 CCECBusDevice *otherDevice = m_processor->GetDeviceByPhysicalAddress(iNewAddress);
743 CCECClient *client = otherDevice ? otherDevice->GetClient() : NULL;
744
745 CCECBusDevice *device = m_processor->GetDevice(iAddress);
746 if (device)
747 device->SetPhysicalAddress(iNewAddress);
748 else
749 {
750 LIB_CEC->AddLog(CEC_LOG_DEBUG, "device with logical address %X not found", iAddress);
751 }
752
753 /* another device reported the same physical address as ours */
754 if (client)
755 client->ResetPhysicalAddress();
756 }
757 else
758 {
759 LIB_CEC->AddLog(CEC_LOG_DEBUG, "ignore physical address report for device %s (%X) because it's marked as handled by libCEC", ToString(iAddress), iAddress);
760 }
761 }
762
763 bool CCECCommandHandler::PowerOn(const cec_logical_address iInitiator, const cec_logical_address iDestination)
764 {
765 if (iDestination == CECDEVICE_TV)
766 return TransmitImageViewOn(iInitiator, iDestination);
767
768 return TransmitKeypress(iInitiator, iDestination, CEC_USER_CONTROL_CODE_POWER) &&
769 TransmitKeyRelease(iInitiator, iDestination);
770 }
771
772 bool CCECCommandHandler::TransmitImageViewOn(const cec_logical_address iInitiator, const cec_logical_address iDestination)
773 {
774 cec_command command;
775 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_IMAGE_VIEW_ON);
776
777 return Transmit(command);
778 }
779
780 bool CCECCommandHandler::TransmitStandby(const cec_logical_address iInitiator, const cec_logical_address iDestination)
781 {
782 cec_command command;
783 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_STANDBY);
784
785 return Transmit(command);
786 }
787
788 bool CCECCommandHandler::TransmitRequestActiveSource(const cec_logical_address iInitiator, bool bWaitForResponse /* = true */)
789 {
790 cec_command command;
791 cec_command::Format(command, iInitiator, CECDEVICE_BROADCAST, CEC_OPCODE_REQUEST_ACTIVE_SOURCE);
792
793 return Transmit(command, !bWaitForResponse);
794 }
795
796 bool CCECCommandHandler::TransmitRequestCecVersion(const cec_logical_address iInitiator, const cec_logical_address iDestination, bool bWaitForResponse /* = true */)
797 {
798 cec_command command;
799 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_GET_CEC_VERSION);
800
801 return Transmit(command, !bWaitForResponse);
802 }
803
804 bool CCECCommandHandler::TransmitRequestMenuLanguage(const cec_logical_address iInitiator, const cec_logical_address iDestination, bool bWaitForResponse /* = true */)
805 {
806 cec_command command;
807 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_GET_MENU_LANGUAGE);
808
809 return Transmit(command, !bWaitForResponse);
810 }
811
812 bool CCECCommandHandler::TransmitRequestOSDName(const cec_logical_address iInitiator, const cec_logical_address iDestination, bool bWaitForResponse /* = true */)
813 {
814 cec_command command;
815 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_GIVE_OSD_NAME);
816
817 return Transmit(command, !bWaitForResponse);
818 }
819
820 bool CCECCommandHandler::TransmitRequestPhysicalAddress(const cec_logical_address iInitiator, const cec_logical_address iDestination, bool bWaitForResponse /* = true */)
821 {
822 cec_command command;
823 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_GIVE_PHYSICAL_ADDRESS);
824
825 return Transmit(command, !bWaitForResponse);
826 }
827
828 bool CCECCommandHandler::TransmitRequestPowerStatus(const cec_logical_address iInitiator, const cec_logical_address iDestination, bool bWaitForResponse /* = true */)
829 {
830 cec_command command;
831 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_GIVE_DEVICE_POWER_STATUS);
832
833 return Transmit(command, !bWaitForResponse);
834 }
835
836 bool CCECCommandHandler::TransmitRequestVendorId(const cec_logical_address iInitiator, const cec_logical_address iDestination, bool bWaitForResponse /* = true */)
837 {
838 cec_command command;
839 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_GIVE_DEVICE_VENDOR_ID);
840
841 return Transmit(command, !bWaitForResponse);
842 }
843
844 bool CCECCommandHandler::TransmitActiveSource(const cec_logical_address iInitiator, uint16_t iPhysicalAddress)
845 {
846 cec_command command;
847 cec_command::Format(command, iInitiator, CECDEVICE_BROADCAST, CEC_OPCODE_ACTIVE_SOURCE);
848 command.parameters.PushBack((uint8_t) ((iPhysicalAddress >> 8) & 0xFF));
849 command.parameters.PushBack((uint8_t) (iPhysicalAddress & 0xFF));
850
851 return Transmit(command);
852 }
853
854 bool CCECCommandHandler::TransmitCECVersion(const cec_logical_address iInitiator, const cec_logical_address iDestination, cec_version cecVersion)
855 {
856 cec_command command;
857 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_CEC_VERSION);
858 command.parameters.PushBack((uint8_t)cecVersion);
859
860 return Transmit(command);
861 }
862
863 bool CCECCommandHandler::TransmitInactiveSource(const cec_logical_address iInitiator, uint16_t iPhysicalAddress)
864 {
865 cec_command command;
866 cec_command::Format(command, iInitiator, CECDEVICE_TV, CEC_OPCODE_INACTIVE_SOURCE);
867 command.parameters.PushBack((iPhysicalAddress >> 8) & 0xFF);
868 command.parameters.PushBack(iPhysicalAddress & 0xFF);
869
870 return Transmit(command);
871 }
872
873 bool CCECCommandHandler::TransmitMenuState(const cec_logical_address iInitiator, const cec_logical_address iDestination, cec_menu_state menuState)
874 {
875 cec_command command;
876 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_MENU_STATUS);
877 command.parameters.PushBack((uint8_t)menuState);
878
879 return Transmit(command);
880 }
881
882 bool CCECCommandHandler::TransmitOSDName(const cec_logical_address iInitiator, const cec_logical_address iDestination, CStdString strDeviceName)
883 {
884 cec_command command;
885 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_SET_OSD_NAME);
886 for (size_t iPtr = 0; iPtr < strDeviceName.length(); iPtr++)
887 command.parameters.PushBack(strDeviceName.at(iPtr));
888
889 return Transmit(command);
890 }
891
892 bool CCECCommandHandler::TransmitOSDString(const cec_logical_address iInitiator, const cec_logical_address iDestination, cec_display_control duration, const char *strMessage)
893 {
894 cec_command command;
895 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_SET_OSD_STRING);
896 command.parameters.PushBack((uint8_t)duration);
897
898 size_t iLen = strlen(strMessage);
899 if (iLen > 13) iLen = 13;
900
901 for (size_t iPtr = 0; iPtr < iLen; iPtr++)
902 command.parameters.PushBack(strMessage[iPtr]);
903
904 return Transmit(command);
905 }
906
907 bool CCECCommandHandler::TransmitPhysicalAddress(const cec_logical_address iInitiator, uint16_t iPhysicalAddress, cec_device_type type)
908 {
909 cec_command command;
910 cec_command::Format(command, iInitiator, CECDEVICE_BROADCAST, CEC_OPCODE_REPORT_PHYSICAL_ADDRESS);
911 command.parameters.PushBack((uint8_t) ((iPhysicalAddress >> 8) & 0xFF));
912 command.parameters.PushBack((uint8_t) (iPhysicalAddress & 0xFF));
913 command.parameters.PushBack((uint8_t) (type));
914
915 return Transmit(command);
916 }
917
918 bool CCECCommandHandler::TransmitSetMenuLanguage(const cec_logical_address iInitiator, const char lang[3])
919 {
920 cec_command command;
921 command.Format(command, iInitiator, CECDEVICE_BROADCAST, CEC_OPCODE_SET_MENU_LANGUAGE);
922 command.parameters.PushBack((uint8_t) lang[0]);
923 command.parameters.PushBack((uint8_t) lang[1]);
924 command.parameters.PushBack((uint8_t) lang[2]);
925
926 return Transmit(command);
927 }
928
929 bool CCECCommandHandler::TransmitPoll(const cec_logical_address iInitiator, const cec_logical_address iDestination)
930 {
931 cec_command command;
932 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_NONE);
933
934 return Transmit(command);
935 }
936
937 bool CCECCommandHandler::TransmitPowerState(const cec_logical_address iInitiator, const cec_logical_address iDestination, cec_power_status state)
938 {
939 cec_command command;
940 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_REPORT_POWER_STATUS);
941 command.parameters.PushBack((uint8_t) state);
942
943 return Transmit(command);
944 }
945
946 bool CCECCommandHandler::TransmitVendorID(const cec_logical_address iInitiator, uint64_t iVendorId)
947 {
948 cec_command command;
949 cec_command::Format(command, iInitiator, CECDEVICE_BROADCAST, CEC_OPCODE_DEVICE_VENDOR_ID);
950
951 command.parameters.PushBack((uint8_t) (((uint64_t)iVendorId >> 16) & 0xFF));
952 command.parameters.PushBack((uint8_t) (((uint64_t)iVendorId >> 8) & 0xFF));
953 command.parameters.PushBack((uint8_t) ((uint64_t)iVendorId & 0xFF));
954
955 return Transmit(command);
956 }
957
958 bool CCECCommandHandler::TransmitAudioStatus(const cec_logical_address iInitiator, const cec_logical_address iDestination, uint8_t state)
959 {
960 cec_command command;
961 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_REPORT_AUDIO_STATUS);
962 command.parameters.PushBack(state);
963
964 return Transmit(command);
965 }
966
967 bool CCECCommandHandler::TransmitSetSystemAudioMode(const cec_logical_address iInitiator, const cec_logical_address iDestination, cec_system_audio_status state)
968 {
969 cec_command command;
970 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_SET_SYSTEM_AUDIO_MODE);
971 command.parameters.PushBack((uint8_t)state);
972
973 return Transmit(command);
974 }
975
976 bool CCECCommandHandler::TransmitSetStreamPath(uint16_t iStreamPath)
977 {
978 cec_command command;
979 cec_command::Format(command, m_busDevice->GetLogicalAddress(), CECDEVICE_BROADCAST, CEC_OPCODE_SET_STREAM_PATH);
980 command.parameters.PushBack((uint8_t) ((iStreamPath >> 8) & 0xFF));
981 command.parameters.PushBack((uint8_t) (iStreamPath & 0xFF));
982
983 return Transmit(command);
984 }
985
986 bool CCECCommandHandler::TransmitSystemAudioModeStatus(const cec_logical_address iInitiator, const cec_logical_address iDestination, cec_system_audio_status state)
987 {
988 cec_command command;
989 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_SYSTEM_AUDIO_MODE_STATUS);
990 command.parameters.PushBack((uint8_t)state);
991
992 return Transmit(command);
993 }
994
995 bool CCECCommandHandler::TransmitDeckStatus(const cec_logical_address iInitiator, const cec_logical_address iDestination, cec_deck_info state)
996 {
997 cec_command command;
998 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_DECK_STATUS);
999 command.PushBack((uint8_t)state);
1000
1001 return Transmit(command);
1002 }
1003
1004 bool CCECCommandHandler::TransmitKeypress(const cec_logical_address iInitiator, const cec_logical_address iDestination, cec_user_control_code key, bool bWait /* = true */)
1005 {
1006 cec_command command;
1007 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_USER_CONTROL_PRESSED);
1008 command.parameters.PushBack((uint8_t)key);
1009
1010 return Transmit(command, !bWait);
1011 }
1012
1013 bool CCECCommandHandler::TransmitKeyRelease(const cec_logical_address iInitiator, const cec_logical_address iDestination, bool bWait /* = true */)
1014 {
1015 cec_command command;
1016 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_USER_CONTROL_RELEASE);
1017
1018 return Transmit(command, !bWait);
1019 }
1020
1021 bool CCECCommandHandler::Transmit(cec_command &command, bool bSuppressWait /* = false */)
1022 {
1023 bool bReturn(false);
1024 cec_opcode expectedResponse(cec_command::GetResponseOpcode(command.opcode));
1025 bool bExpectResponse(expectedResponse != CEC_OPCODE_NONE && !bSuppressWait);
1026 command.transmit_timeout = m_iTransmitTimeout;
1027
1028 if (command.initiator == CECDEVICE_UNKNOWN)
1029 {
1030 LIB_CEC->AddLog(CEC_LOG_ERROR, "not transmitting a command without a valid initiator");
1031 return bReturn;
1032 }
1033
1034 {
1035 uint8_t iTries(0), iMaxTries(!command.opcode_set ? 1 : m_iTransmitRetries + 1);
1036 while (!bReturn && ++iTries <= iMaxTries && !m_busDevice->IsUnsupportedFeature(command.opcode))
1037 {
1038 if ((bReturn = m_processor->Transmit(command)) == true)
1039 {
1040 LIB_CEC->AddLog(CEC_LOG_DEBUG, "command transmitted");
1041 if (bExpectResponse)
1042 {
1043 bReturn = m_waitForResponse->Wait(expectedResponse);
1044 LIB_CEC->AddLog(CEC_LOG_DEBUG, bReturn ? "expected response received (%X: %s)" : "expected response not received (%X: %s)", (int)expectedResponse, ToString(expectedResponse));
1045 }
1046 }
1047 }
1048 }
1049
1050 return bReturn;
1051 }
1052
1053 bool CCECCommandHandler::ActivateSource(void)
1054 {
1055 if (m_busDevice->IsActiveSource() &&
1056 m_busDevice->IsHandledByLibCEC())
1057 {
1058 m_busDevice->SetPowerStatus(CEC_POWER_STATUS_ON);
1059 m_busDevice->SetMenuState(CEC_MENU_STATE_ACTIVATED);
1060
1061 m_busDevice->TransmitImageViewOn();
1062 m_busDevice->TransmitActiveSource();
1063 m_busDevice->TransmitMenuState(CECDEVICE_TV);
1064
1065 CCECPlaybackDevice *playbackDevice = m_busDevice->AsPlaybackDevice();
1066 if (playbackDevice && SendDeckStatusUpdateOnActiveSource())
1067 playbackDevice->TransmitDeckStatus(CECDEVICE_TV);
1068 m_bHandlerInited = true;
1069 }
1070 return true;
1071 }
1072
1073 void CCECCommandHandler::SignalOpcode(cec_opcode opcode)
1074 {
1075 m_waitForResponse->Received(opcode);
1076 }