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