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