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