log unhandled vendor remote keycodes in the log, so they get logged without debugging...
[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 CCECBusDevice *device = m_processor->GetDeviceByPhysicalAddress(iAddress);
213 if (device)
214 {
215 device->MarkAsActiveSource();
216 return COMMAND_HANDLED;
217 }
218 }
219
220 return CEC_ABORT_REASON_INVALID_OPERAND;
221 }
222
223 int CCECCommandHandler::HandleDeckControl(const cec_command &command)
224 {
225 CCECPlaybackDevice *device = CCECBusDevice::AsPlaybackDevice(GetDevice(command.destination));
226 if (device && command.parameters.size > 0)
227 {
228 device->SetDeckControlMode((cec_deck_control_mode) command.parameters[0]);
229 return COMMAND_HANDLED;
230 }
231
232 return CEC_ABORT_REASON_INVALID_OPERAND;
233 }
234
235 int CCECCommandHandler::HandleDeviceCecVersion(const cec_command &command)
236 {
237 if (command.parameters.size == 1)
238 {
239 CCECBusDevice *device = GetDevice(command.initiator);
240 if (device)
241 device->SetCecVersion((cec_version) command.parameters[0]);
242
243 return COMMAND_HANDLED;
244 }
245
246 return CEC_ABORT_REASON_INVALID_OPERAND;
247 }
248
249 int CCECCommandHandler::HandleDeviceVendorCommandWithId(const cec_command & UNUSED(command))
250 {
251 return CEC_ABORT_REASON_INVALID_OPERAND;
252 }
253
254 int CCECCommandHandler::HandleDeviceVendorId(const cec_command &command)
255 {
256 SetVendorId(command);
257 return COMMAND_HANDLED;
258 }
259
260 int CCECCommandHandler::HandleFeatureAbort(const cec_command &command)
261 {
262 if (command.parameters.size == 2 &&
263 (command.parameters[1] == CEC_ABORT_REASON_UNRECOGNIZED_OPCODE ||
264 command.parameters[1] == CEC_ABORT_REASON_REFUSED))
265 m_processor->GetDevice(command.initiator)->SetUnsupportedFeature((cec_opcode)command.parameters[0]);
266 return COMMAND_HANDLED;
267 }
268
269 int CCECCommandHandler::HandleGetCecVersion(const cec_command &command)
270 {
271 if (m_processor->CECInitialised() && m_processor->IsHandledByLibCEC(command.destination))
272 {
273 CCECBusDevice *device = GetDevice(command.destination);
274 if (device && device->TransmitCECVersion(command.initiator, true))
275 return COMMAND_HANDLED;
276 return CEC_ABORT_REASON_INVALID_OPERAND;
277 }
278
279 return CEC_ABORT_REASON_NOT_IN_CORRECT_MODE_TO_RESPOND;
280 }
281
282 int CCECCommandHandler::HandleGiveAudioStatus(const cec_command &command)
283 {
284 if (m_processor->CECInitialised() && m_processor->IsHandledByLibCEC(command.destination))
285 {
286 CCECAudioSystem *device = CCECBusDevice::AsAudioSystem(GetDevice(command.destination));
287 if (device && device->TransmitAudioStatus(command.initiator, true))
288 return COMMAND_HANDLED;
289 return CEC_ABORT_REASON_INVALID_OPERAND;
290 }
291
292 return CEC_ABORT_REASON_NOT_IN_CORRECT_MODE_TO_RESPOND;
293 }
294
295 int 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 && device->TransmitDeckStatus(command.initiator, true))
301 return COMMAND_HANDLED;
302 return CEC_ABORT_REASON_INVALID_OPERAND;
303 }
304
305 return CEC_ABORT_REASON_NOT_IN_CORRECT_MODE_TO_RESPOND;
306 }
307
308 int CCECCommandHandler::HandleGiveDevicePowerStatus(const cec_command &command)
309 {
310 if (m_processor->CECInitialised() && m_processor->IsHandledByLibCEC(command.destination))
311 {
312 CCECBusDevice *device = GetDevice(command.destination);
313 if (device && device->TransmitPowerState(command.initiator, true))
314 return COMMAND_HANDLED;
315 return CEC_ABORT_REASON_INVALID_OPERAND;
316 }
317
318 return CEC_ABORT_REASON_INVALID_OPERAND;
319 }
320
321 int CCECCommandHandler::HandleGiveDeviceVendorId(const cec_command &command)
322 {
323 if (m_processor->CECInitialised() && m_processor->IsHandledByLibCEC(command.destination))
324 {
325 CCECBusDevice *device = GetDevice(command.destination);
326 if (device && device->TransmitVendorID(command.initiator, true, true))
327 return COMMAND_HANDLED;
328 }
329
330 return CEC_ABORT_REASON_INVALID_OPERAND;
331 }
332
333 int CCECCommandHandler::HandleGiveOSDName(const cec_command &command)
334 {
335 if (m_processor->CECInitialised() && m_processor->IsHandledByLibCEC(command.destination))
336 {
337 CCECBusDevice *device = GetDevice(command.destination);
338 if (device && device->TransmitOSDName(command.initiator, true))
339 return COMMAND_HANDLED;
340 }
341
342 return CEC_ABORT_REASON_INVALID_OPERAND;
343 }
344
345 int CCECCommandHandler::HandleGivePhysicalAddress(const cec_command &command)
346 {
347 if (m_processor->CECInitialised() && m_processor->IsHandledByLibCEC(command.destination))
348 {
349 CCECBusDevice *device = GetDevice(command.destination);
350 if (device && device->TransmitPhysicalAddress(true))
351 return COMMAND_HANDLED;
352 return CEC_ABORT_REASON_INVALID_OPERAND;
353 }
354
355 return CEC_ABORT_REASON_NOT_IN_CORRECT_MODE_TO_RESPOND;
356 }
357
358 int CCECCommandHandler::HandleGiveMenuLanguage(const cec_command &command)
359 {
360 if (m_processor->CECInitialised() && m_processor->IsHandledByLibCEC(command.destination))
361 {
362 CCECBusDevice *device = GetDevice(command.destination);
363 if (device && device->TransmitSetMenuLanguage(command.initiator, true))
364 return COMMAND_HANDLED;
365 return CEC_ABORT_REASON_INVALID_OPERAND;
366 }
367
368 return CEC_ABORT_REASON_NOT_IN_CORRECT_MODE_TO_RESPOND;
369 }
370
371 int CCECCommandHandler::HandleGiveSystemAudioModeStatus(const cec_command &command)
372 {
373 if (m_processor->CECInitialised() && m_processor->IsHandledByLibCEC(command.destination))
374 {
375 CCECAudioSystem *device = CCECBusDevice::AsAudioSystem(GetDevice(command.destination));
376 if (device && device->TransmitSystemAudioModeStatus(command.initiator, true))
377 return COMMAND_HANDLED;
378 return CEC_ABORT_REASON_INVALID_OPERAND;
379 }
380
381 return CEC_ABORT_REASON_NOT_IN_CORRECT_MODE_TO_RESPOND;
382 }
383
384 int CCECCommandHandler::HandleImageViewOn(const cec_command &command)
385 {
386 CCECBusDevice *device = GetDevice(command.destination);
387 if (device && (device->GetCurrentStatus() == CEC_DEVICE_STATUS_PRESENT ||
388 device->GetCurrentStatus() == CEC_DEVICE_STATUS_HANDLED_BY_LIBCEC))
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 }
394 return COMMAND_HANDLED;
395 }
396
397 int CCECCommandHandler::HandleMenuRequest(const cec_command &command)
398 {
399 if (m_processor->CECInitialised() && m_processor->IsHandledByLibCEC(command.destination))
400 {
401 CCECBusDevice *device = GetDevice(command.destination);
402 if (device)
403 {
404 CCECClient *client = device->GetClient();
405 if (client)
406 {
407 if (command.parameters[0] == CEC_MENU_REQUEST_TYPE_ACTIVATE)
408 {
409 if (client->MenuStateChanged(CEC_MENU_STATE_ACTIVATED) == 1)
410 device->SetMenuState(CEC_MENU_STATE_ACTIVATED);
411 }
412 else if (command.parameters[0] == CEC_MENU_REQUEST_TYPE_DEACTIVATE)
413 {
414 if (client->MenuStateChanged(CEC_MENU_STATE_DEACTIVATED) == 1)
415 device->SetMenuState(CEC_MENU_STATE_DEACTIVATED);
416 }
417 }
418 if (device->TransmitMenuState(command.initiator, true))
419 return COMMAND_HANDLED;
420 }
421 return CEC_ABORT_REASON_INVALID_OPERAND;
422 }
423
424 return CEC_ABORT_REASON_NOT_IN_CORRECT_MODE_TO_RESPOND;
425 }
426
427 bool CCECCommandHandler::HandlePoll(const cec_command &command)
428 {
429 m_busDevice->HandlePoll(command.destination);
430 return true;
431 }
432
433 int CCECCommandHandler::HandleReportAudioStatus(const cec_command &command)
434 {
435 if (command.parameters.size == 1)
436 {
437 CCECAudioSystem *device = CCECBusDevice::AsAudioSystem(GetDevice(command.initiator));
438 if (device)
439 {
440 device->SetAudioStatus(command.parameters[0]);
441 return COMMAND_HANDLED;
442 }
443 }
444 return CEC_ABORT_REASON_INVALID_OPERAND;
445 }
446
447 int CCECCommandHandler::HandleReportPhysicalAddress(const cec_command &command)
448 {
449 if (command.parameters.size == 3)
450 {
451 uint16_t iNewAddress = ((uint16_t)command.parameters[0] << 8) | ((uint16_t)command.parameters[1]);
452 SetPhysicalAddress(command.initiator, iNewAddress);
453 return COMMAND_HANDLED;
454 }
455 return CEC_ABORT_REASON_INVALID_OPERAND;
456 }
457
458 int CCECCommandHandler::HandleReportPowerStatus(const cec_command &command)
459 {
460 if (command.parameters.size == 1)
461 {
462 CCECBusDevice *device = GetDevice(command.initiator);
463 if (device)
464 {
465 device->SetPowerStatus((cec_power_status) command.parameters[0]);
466 return COMMAND_HANDLED;
467 }
468 }
469 return CEC_ABORT_REASON_INVALID_OPERAND;
470 }
471
472 int CCECCommandHandler::HandleRequestActiveSource(const cec_command &command)
473 {
474 if (m_processor->CECInitialised())
475 {
476 LIB_CEC->AddLog(CEC_LOG_DEBUG, ">> %i requests active source", (uint8_t) command.initiator);
477 m_processor->GetDevice(command.initiator)->SetPowerStatus(CEC_POWER_STATUS_ON);
478
479 vector<CCECBusDevice *> devices;
480 for (size_t iDevicePtr = 0; iDevicePtr < GetMyDevices(devices); iDevicePtr++)
481 devices[iDevicePtr]->TransmitActiveSource(true);
482 }
483
484 return COMMAND_HANDLED;
485 }
486
487 int CCECCommandHandler::HandleRoutingChange(const cec_command &command)
488 {
489 if (command.parameters.size == 4)
490 {
491 CCECBusDevice *device = GetDevice(command.initiator);
492 if (device)
493 {
494 uint16_t iNewAddress = ((uint16_t)command.parameters[2] << 8) | ((uint16_t)command.parameters[3]);
495 device->SetActiveRoute(iNewAddress);
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 CCECBusDevice *device = GetDevice(command.initiator);
508 if (device)
509 {
510 uint16_t iNewAddress = ((uint16_t)command.parameters[0] << 8) | ((uint16_t)command.parameters[1]);
511 device->SetActiveRoute(iNewAddress);
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 /* one of the device handled by libCEC has been made active */
572 CCECBusDevice *device = GetDeviceByPhysicalAddress(iStreamAddress);
573 if (device && device->IsHandledByLibCEC())
574 {
575 device->ActivateSource();
576 return COMMAND_HANDLED;
577 }
578 }
579
580 return CEC_ABORT_REASON_INVALID_OPERAND;
581 }
582
583 int CCECCommandHandler::HandleSystemAudioModeRequest(const cec_command &command)
584 {
585 if (m_processor->CECInitialised() && m_processor->IsHandledByLibCEC(command.destination))
586 {
587 CCECAudioSystem *device = CCECBusDevice::AsAudioSystem(GetDevice(command.destination));
588 if (device)
589 {
590 if (command.parameters.size >= 2)
591 {
592 device->SetPowerStatus(CEC_POWER_STATUS_ON);
593 device->SetSystemAudioModeStatus(CEC_SYSTEM_AUDIO_STATUS_ON);
594 uint16_t iNewAddress = ((uint16_t)command.parameters[0] << 8) | ((uint16_t)command.parameters[1]);
595 CCECBusDevice *newActiveDevice = GetDeviceByPhysicalAddress(iNewAddress);
596 if (newActiveDevice)
597 newActiveDevice->MarkAsActiveSource();
598 if (device->TransmitSetSystemAudioMode(command.initiator, true))
599 return COMMAND_HANDLED;
600 }
601 else
602 {
603 device->SetSystemAudioModeStatus(CEC_SYSTEM_AUDIO_STATUS_OFF);
604 if (device->TransmitSetSystemAudioMode(command.initiator, true))
605 return COMMAND_HANDLED;
606 }
607 }
608 }
609
610 return CEC_ABORT_REASON_NOT_IN_CORRECT_MODE_TO_RESPOND;
611 }
612
613 int CCECCommandHandler::HandleStandby(const cec_command &command)
614 {
615 CCECBusDevice *device = GetDevice(command.initiator);
616 if (device)
617 device->SetPowerStatus(CEC_POWER_STATUS_STANDBY);
618
619 return COMMAND_HANDLED;
620 }
621
622 int CCECCommandHandler::HandleSystemAudioModeStatus(const cec_command &command)
623 {
624 if (command.parameters.size == 1)
625 {
626 CCECAudioSystem *device = CCECBusDevice::AsAudioSystem(GetDevice(command.initiator));
627 if (device)
628 {
629 device->SetSystemAudioModeStatus((cec_system_audio_status)command.parameters[0]);
630 return COMMAND_HANDLED;
631 }
632 }
633
634 return CEC_ABORT_REASON_INVALID_OPERAND;
635 }
636
637 int CCECCommandHandler::HandleSetSystemAudioMode(const cec_command &command)
638 {
639 if (command.parameters.size == 1)
640 {
641 CCECAudioSystem *device = CCECBusDevice::AsAudioSystem(GetDevice(command.initiator));
642 if (device)
643 {
644 device->SetSystemAudioModeStatus((cec_system_audio_status)command.parameters[0]);
645 return COMMAND_HANDLED;
646 }
647 }
648
649 return CEC_ABORT_REASON_INVALID_OPERAND;
650 }
651
652 int CCECCommandHandler::HandleTextViewOn(const cec_command &command)
653 {
654 m_processor->GetDevice(command.initiator)->MarkAsActiveSource();
655 return COMMAND_HANDLED;
656 }
657
658 int CCECCommandHandler::HandleUserControlPressed(const cec_command &command)
659 {
660 if (!m_processor->CECInitialised() ||
661 !m_processor->IsHandledByLibCEC(command.destination))
662 return CEC_ABORT_REASON_NOT_IN_CORRECT_MODE_TO_RESPOND;
663
664 if (command.parameters.size == 0)
665 return CEC_ABORT_REASON_INVALID_OPERAND;
666
667 CCECBusDevice *device = GetDevice(command.destination);
668 if (!device)
669 return CEC_ABORT_REASON_INVALID_OPERAND;
670
671 CCECClient *client = device->GetClient();
672 if (client)
673 client->SetCurrentButton((cec_user_control_code) command.parameters[0]);
674
675 if (command.parameters[0] == CEC_USER_CONTROL_CODE_POWER ||
676 command.parameters[0] == CEC_USER_CONTROL_CODE_POWER_ON_FUNCTION)
677 {
678 bool bPowerOn(true);
679
680 // CEC_USER_CONTROL_CODE_POWER operates as a toggle
681 // assume CEC_USER_CONTROL_CODE_POWER_ON_FUNCTION does not
682 if (command.parameters[0] == CEC_USER_CONTROL_CODE_POWER)
683 {
684 cec_power_status status = device->GetCurrentPowerStatus();
685 bPowerOn = !(status == CEC_POWER_STATUS_ON || status == CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON);
686 }
687
688 if (bPowerOn)
689 {
690 device->ActivateSource();
691 }
692 else
693 {
694 device->MarkAsInactiveSource();
695 device->TransmitInactiveSource();
696 device->SetMenuState(CEC_MENU_STATE_DEACTIVATED);
697 }
698 }
699
700 return COMMAND_HANDLED;
701 }
702
703 int CCECCommandHandler::HandleUserControlRelease(const cec_command &command)
704 {
705 if (!m_processor->CECInitialised() ||
706 !m_processor->IsHandledByLibCEC(command.destination))
707 return CEC_ABORT_REASON_NOT_IN_CORRECT_MODE_TO_RESPOND;
708
709 CCECClient *client = m_processor->GetClient(command.destination);
710 if (client)
711 client->AddKey();
712
713 return COMMAND_HANDLED;
714 }
715
716 int CCECCommandHandler::HandleVendorCommand(const cec_command & UNUSED(command))
717 {
718 return CEC_ABORT_REASON_INVALID_OPERAND;
719 }
720
721 int CCECCommandHandler::HandleVendorRemoteButtonDown(const cec_command& command)
722 {
723 LIB_CEC->AddLog(CEC_LOG_NOTICE, "unhandled vendor remote button received with keycode %x", command.parameters[0]);
724 return COMMAND_HANDLED;
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 if (Transmit(command, false, false))
822 {
823 CCECBusDevice* dest = m_processor->GetDevice(iDestination);
824 if (dest && dest->GetCurrentPowerStatus() != CEC_POWER_STATUS_ON)
825 dest->SetPowerStatus(CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON);
826 return true;
827 }
828 return false;
829 }
830
831 bool CCECCommandHandler::TransmitStandby(const cec_logical_address iInitiator, const cec_logical_address iDestination)
832 {
833 cec_command command;
834 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_STANDBY);
835
836 return Transmit(command, false, false);
837 }
838
839 bool CCECCommandHandler::TransmitRequestActiveSource(const cec_logical_address iInitiator, bool bWaitForResponse /* = true */)
840 {
841 cec_command command;
842 cec_command::Format(command, iInitiator, CECDEVICE_BROADCAST, CEC_OPCODE_REQUEST_ACTIVE_SOURCE);
843
844 return Transmit(command, !bWaitForResponse, false);
845 }
846
847 bool CCECCommandHandler::TransmitRequestCecVersion(const cec_logical_address iInitiator, const cec_logical_address iDestination, bool bWaitForResponse /* = true */)
848 {
849 cec_command command;
850 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_GET_CEC_VERSION);
851
852 return Transmit(command, !bWaitForResponse, false);
853 }
854
855 bool CCECCommandHandler::TransmitRequestMenuLanguage(const cec_logical_address iInitiator, const cec_logical_address iDestination, bool bWaitForResponse /* = true */)
856 {
857 cec_command command;
858 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_GET_MENU_LANGUAGE);
859
860 return Transmit(command, !bWaitForResponse, false);
861 }
862
863 bool CCECCommandHandler::TransmitRequestOSDName(const cec_logical_address iInitiator, const cec_logical_address iDestination, bool bWaitForResponse /* = true */)
864 {
865 cec_command command;
866 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_GIVE_OSD_NAME);
867
868 return Transmit(command, !bWaitForResponse, false);
869 }
870
871 bool CCECCommandHandler::TransmitRequestPhysicalAddress(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_GIVE_PHYSICAL_ADDRESS);
875
876 return Transmit(command, !bWaitForResponse, false);
877 }
878
879 bool CCECCommandHandler::TransmitRequestPowerStatus(const cec_logical_address iInitiator, const cec_logical_address iDestination, bool bWaitForResponse /* = true */)
880 {
881 if (iDestination == CECDEVICE_TV)
882 {
883 int64_t now(GetTimeMs());
884 if (now - m_iPowerStatusRequested < REQUEST_POWER_STATUS_TIMEOUT)
885 return true;
886 m_iPowerStatusRequested = now;
887 }
888
889 LIB_CEC->AddLog(CEC_LOG_DEBUG, "<< requesting power status of '%s' (%X)", m_busDevice->GetLogicalAddressName(), iDestination);
890
891 cec_command command;
892 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_GIVE_DEVICE_POWER_STATUS);
893
894 return Transmit(command, !bWaitForResponse, false);
895 }
896
897 bool CCECCommandHandler::TransmitRequestVendorId(const cec_logical_address iInitiator, const cec_logical_address iDestination, bool bWaitForResponse /* = true */)
898 {
899 cec_command command;
900 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_GIVE_DEVICE_VENDOR_ID);
901
902 return Transmit(command, !bWaitForResponse, false);
903 }
904
905 bool CCECCommandHandler::TransmitActiveSource(const cec_logical_address iInitiator, uint16_t iPhysicalAddress, bool bIsReply)
906 {
907 cec_command command;
908 cec_command::Format(command, iInitiator, CECDEVICE_BROADCAST, CEC_OPCODE_ACTIVE_SOURCE);
909 command.parameters.PushBack((uint8_t) ((iPhysicalAddress >> 8) & 0xFF));
910 command.parameters.PushBack((uint8_t) (iPhysicalAddress & 0xFF));
911
912 return Transmit(command, false, bIsReply);
913 }
914
915 bool CCECCommandHandler::TransmitCECVersion(const cec_logical_address iInitiator, const cec_logical_address iDestination, cec_version cecVersion, bool bIsReply)
916 {
917 cec_command command;
918 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_CEC_VERSION);
919 command.parameters.PushBack((uint8_t)cecVersion);
920
921 return Transmit(command, false, bIsReply);
922 }
923
924 bool CCECCommandHandler::TransmitInactiveSource(const cec_logical_address iInitiator, uint16_t iPhysicalAddress)
925 {
926 cec_command command;
927 cec_command::Format(command, iInitiator, CECDEVICE_TV, CEC_OPCODE_INACTIVE_SOURCE);
928 command.parameters.PushBack((iPhysicalAddress >> 8) & 0xFF);
929 command.parameters.PushBack(iPhysicalAddress & 0xFF);
930
931 return Transmit(command, false, false);
932 }
933
934 bool CCECCommandHandler::TransmitMenuState(const cec_logical_address iInitiator, const cec_logical_address iDestination, cec_menu_state menuState, bool bIsReply)
935 {
936 cec_command command;
937 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_MENU_STATUS);
938 command.parameters.PushBack((uint8_t)menuState);
939
940 return Transmit(command, false, bIsReply);
941 }
942
943 bool CCECCommandHandler::TransmitOSDName(const cec_logical_address iInitiator, const cec_logical_address iDestination, std::string strDeviceName, bool bIsReply)
944 {
945 cec_command command;
946 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_SET_OSD_NAME);
947 for (size_t iPtr = 0; iPtr < strDeviceName.length(); iPtr++)
948 command.parameters.PushBack(strDeviceName.at(iPtr));
949
950 return Transmit(command, false, bIsReply);
951 }
952
953 bool CCECCommandHandler::TransmitOSDString(const cec_logical_address iInitiator, const cec_logical_address iDestination, cec_display_control duration, const char *strMessage, bool bIsReply)
954 {
955 cec_command command;
956 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_SET_OSD_STRING);
957 command.parameters.PushBack((uint8_t)duration);
958
959 size_t iLen = strlen(strMessage);
960 if (iLen > 13) iLen = 13;
961
962 for (size_t iPtr = 0; iPtr < iLen; iPtr++)
963 command.parameters.PushBack(strMessage[iPtr]);
964
965 return Transmit(command, false, bIsReply);
966 }
967
968 bool CCECCommandHandler::TransmitPhysicalAddress(const cec_logical_address iInitiator, uint16_t iPhysicalAddress, cec_device_type type, bool bIsReply)
969 {
970 cec_command command;
971 cec_command::Format(command, iInitiator, CECDEVICE_BROADCAST, CEC_OPCODE_REPORT_PHYSICAL_ADDRESS);
972 command.parameters.PushBack((uint8_t) ((iPhysicalAddress >> 8) & 0xFF));
973 command.parameters.PushBack((uint8_t) (iPhysicalAddress & 0xFF));
974 command.parameters.PushBack((uint8_t) (type));
975
976 return Transmit(command, false, bIsReply);
977 }
978
979 bool CCECCommandHandler::TransmitSetMenuLanguage(const cec_logical_address iInitiator, const char lang[3], bool bIsReply)
980 {
981 cec_command command;
982 command.Format(command, iInitiator, CECDEVICE_BROADCAST, CEC_OPCODE_SET_MENU_LANGUAGE);
983 command.parameters.PushBack((uint8_t) lang[0]);
984 command.parameters.PushBack((uint8_t) lang[1]);
985 command.parameters.PushBack((uint8_t) lang[2]);
986
987 return Transmit(command, false, bIsReply);
988 }
989
990 bool CCECCommandHandler::TransmitPoll(const cec_logical_address iInitiator, const cec_logical_address iDestination, bool bIsReply)
991 {
992 cec_command command;
993 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_NONE);
994
995 return Transmit(command, false, bIsReply);
996 }
997
998 bool CCECCommandHandler::TransmitPowerState(const cec_logical_address iInitiator, const cec_logical_address iDestination, cec_power_status state, bool bIsReply)
999 {
1000 cec_command command;
1001 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_REPORT_POWER_STATUS);
1002 command.parameters.PushBack((uint8_t) state);
1003
1004 return Transmit(command, false, bIsReply);
1005 }
1006
1007 bool CCECCommandHandler::TransmitVendorID(const cec_logical_address iInitiator, uint64_t iVendorId, bool bIsReply)
1008 {
1009 cec_command command;
1010 cec_command::Format(command, iInitiator, CECDEVICE_BROADCAST, CEC_OPCODE_DEVICE_VENDOR_ID);
1011
1012 command.parameters.PushBack((uint8_t) (((uint64_t)iVendorId >> 16) & 0xFF));
1013 command.parameters.PushBack((uint8_t) (((uint64_t)iVendorId >> 8) & 0xFF));
1014 command.parameters.PushBack((uint8_t) ((uint64_t)iVendorId & 0xFF));
1015
1016 return Transmit(command, false, bIsReply);
1017 }
1018
1019 bool CCECCommandHandler::TransmitAudioStatus(const cec_logical_address iInitiator, const cec_logical_address iDestination, uint8_t state, bool bIsReply)
1020 {
1021 cec_command command;
1022 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_REPORT_AUDIO_STATUS);
1023 command.parameters.PushBack(state);
1024
1025 return Transmit(command, false, bIsReply);
1026 }
1027
1028 bool CCECCommandHandler::TransmitSetSystemAudioMode(const cec_logical_address iInitiator, const cec_logical_address iDestination, cec_system_audio_status state, bool bIsReply)
1029 {
1030 cec_command command;
1031 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_SET_SYSTEM_AUDIO_MODE);
1032 command.parameters.PushBack((uint8_t)state);
1033
1034 return Transmit(command, false, bIsReply);
1035 }
1036
1037 bool CCECCommandHandler::TransmitSetStreamPath(uint16_t iStreamPath, bool bIsReply)
1038 {
1039 if (m_busDevice->GetLogicalAddress() != CECDEVICE_TV)
1040 {
1041 LIB_CEC->AddLog(CEC_LOG_ERROR, "only the TV is allowed to send CEC_OPCODE_SET_STREAM_PATH");
1042 return false;
1043 }
1044 cec_command command;
1045 cec_command::Format(command, m_busDevice->GetLogicalAddress(), CECDEVICE_BROADCAST, CEC_OPCODE_SET_STREAM_PATH);
1046 command.parameters.PushBack((uint8_t) ((iStreamPath >> 8) & 0xFF));
1047 command.parameters.PushBack((uint8_t) (iStreamPath & 0xFF));
1048
1049 return Transmit(command, false, bIsReply);
1050 }
1051
1052 bool CCECCommandHandler::TransmitSystemAudioModeStatus(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_SYSTEM_AUDIO_MODE_STATUS);
1056 command.parameters.PushBack((uint8_t)state);
1057
1058 return Transmit(command, false, bIsReply);
1059 }
1060
1061 bool CCECCommandHandler::TransmitDeckStatus(const cec_logical_address iInitiator, const cec_logical_address iDestination, cec_deck_info state, bool bIsReply)
1062 {
1063 cec_command command;
1064 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_DECK_STATUS);
1065 command.PushBack((uint8_t)state);
1066
1067 return Transmit(command, false, bIsReply);
1068 }
1069
1070 bool CCECCommandHandler::TransmitKeypress(const cec_logical_address iInitiator, const cec_logical_address iDestination, cec_user_control_code key, bool bWait /* = true */)
1071 {
1072 cec_command command;
1073 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_USER_CONTROL_PRESSED);
1074 command.parameters.PushBack((uint8_t)key);
1075
1076 return Transmit(command, !bWait, false);
1077 }
1078
1079 bool CCECCommandHandler::TransmitKeyRelease(const cec_logical_address iInitiator, const cec_logical_address iDestination, bool bWait /* = true */)
1080 {
1081 cec_command command;
1082 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_USER_CONTROL_RELEASE);
1083
1084 return Transmit(command, !bWait, false);
1085 }
1086
1087 bool CCECCommandHandler::Transmit(cec_command &command, bool bSuppressWait, bool bIsReply)
1088 {
1089 bool bReturn(false);
1090 cec_opcode expectedResponse(cec_command::GetResponseOpcode(command.opcode));
1091 bool bExpectResponse(expectedResponse != CEC_OPCODE_NONE && !bSuppressWait);
1092 command.transmit_timeout = m_iTransmitTimeout;
1093
1094 if (command.initiator == CECDEVICE_UNKNOWN)
1095 {
1096 LIB_CEC->AddLog(CEC_LOG_ERROR, "not transmitting a command without a valid initiator");
1097 return bReturn;
1098 }
1099
1100 // check whether the destination is not marked as not present or handled by libCEC
1101 if (command.destination != CECDEVICE_BROADCAST && command.opcode_set)
1102 {
1103 CCECBusDevice* destinationDevice = m_processor->GetDevice(command.destination);
1104 cec_bus_device_status status = destinationDevice ? destinationDevice->GetStatus() : CEC_DEVICE_STATUS_NOT_PRESENT;
1105 if (status == CEC_DEVICE_STATUS_NOT_PRESENT)
1106 {
1107 LIB_CEC->AddLog(CEC_LOG_DEBUG, "not sending command '%s': destination device '%s' marked as not present", ToString(command.opcode),ToString(command.destination));
1108 return bReturn;
1109 }
1110 else if (status == CEC_DEVICE_STATUS_HANDLED_BY_LIBCEC)
1111 {
1112 LIB_CEC->AddLog(CEC_LOG_DEBUG, "not sending command '%s': destination device '%s' marked as handled by libCEC", ToString(command.opcode),ToString(command.destination));
1113 return bReturn;
1114 }
1115 }
1116
1117 {
1118 uint8_t iTries(0), iMaxTries(!command.opcode_set ? 1 : m_iTransmitRetries + 1);
1119 while (!bReturn && ++iTries <= iMaxTries && !m_busDevice->IsUnsupportedFeature(command.opcode))
1120 {
1121 if ((bReturn = m_processor->Transmit(command, bIsReply)) == true)
1122 {
1123 #ifdef CEC_DEBUGGING
1124 LIB_CEC->AddLog(CEC_LOG_DEBUG, "command transmitted");
1125 #endif
1126 if (bExpectResponse)
1127 {
1128 bReturn = m_busDevice->WaitForOpcode(expectedResponse);
1129 LIB_CEC->AddLog(CEC_LOG_DEBUG, bReturn ? "expected response received (%X: %s)" : "expected response not received (%X: %s)", (int)expectedResponse, ToString(expectedResponse));
1130 }
1131 }
1132 }
1133 }
1134
1135 return bReturn;
1136 }
1137
1138 bool CCECCommandHandler::ActivateSource(bool bTransmitDelayedCommandsOnly /* = false */)
1139 {
1140 if (m_busDevice->IsActiveSource() &&
1141 m_busDevice->IsHandledByLibCEC())
1142 {
1143 {
1144 CLockObject lock(m_mutex);
1145 // check if we need to send a delayed source switch
1146 if (bTransmitDelayedCommandsOnly)
1147 {
1148 if (m_iActiveSourcePending == 0 || GetTimeMs() < m_iActiveSourcePending)
1149 return false;
1150
1151 #ifdef CEC_DEBUGGING
1152 LIB_CEC->AddLog(CEC_LOG_DEBUG, "transmitting delayed activate source command");
1153 #endif
1154 }
1155 }
1156
1157 // update the power state and menu state
1158 if (!bTransmitDelayedCommandsOnly)
1159 {
1160 m_busDevice->SetPowerStatus(CEC_POWER_STATUS_ON);
1161 m_busDevice->SetMenuState(CEC_MENU_STATE_ACTIVATED);
1162 }
1163
1164 // vendor specific hook
1165 VendorPreActivateSourceHook();
1166
1167 // power on the TV
1168 CCECBusDevice* tv = m_processor->GetDevice(CECDEVICE_TV);
1169 bool bTvPresent = (tv && tv->GetStatus() == CEC_DEVICE_STATUS_PRESENT);
1170 bool bActiveSourceFailed(false);
1171 if (bTvPresent)
1172 {
1173 if (tv->GetCurrentPowerStatus() != CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON)
1174 bActiveSourceFailed = !m_busDevice->TransmitImageViewOn();
1175 }
1176 else
1177 LIB_CEC->AddLog(CEC_LOG_DEBUG, "TV not present, not sending 'image view on'");
1178
1179 // check if we're allowed to switch sources
1180 bool bSourceSwitchAllowed = SourceSwitchAllowed();
1181 if (!bSourceSwitchAllowed)
1182 LIB_CEC->AddLog(CEC_LOG_DEBUG, "source switch is currently not allowed by command handler");
1183
1184 // switch sources (if allowed)
1185 if (!bActiveSourceFailed && bSourceSwitchAllowed)
1186 {
1187 bActiveSourceFailed = !m_busDevice->TransmitActiveSource(false);
1188 if (bTvPresent && !bActiveSourceFailed)
1189 bActiveSourceFailed = !m_busDevice->TransmitMenuState(CECDEVICE_TV, false);
1190
1191 // update the deck status for playback devices
1192 if (bTvPresent && !bActiveSourceFailed)
1193 {
1194 CCECPlaybackDevice *playbackDevice = m_busDevice->AsPlaybackDevice();
1195 if (playbackDevice && SendDeckStatusUpdateOnActiveSource())
1196 bActiveSourceFailed = !playbackDevice->TransmitDeckStatus(CECDEVICE_TV, false);
1197 }
1198 }
1199
1200 // retry later
1201 if (bActiveSourceFailed || !bSourceSwitchAllowed)
1202 {
1203 LIB_CEC->AddLog(CEC_LOG_DEBUG, "failed to make '%s' the active source. will retry later", m_busDevice->GetLogicalAddressName());
1204 int64_t now(GetTimeMs());
1205 CLockObject lock(m_mutex);
1206 if (m_iActiveSourcePending == 0 || m_iActiveSourcePending < now)
1207 m_iActiveSourcePending = now + (int64_t)CEC_ACTIVE_SOURCE_SWITCH_RETRY_TIME_MS;
1208 return false;
1209 }
1210 else
1211 {
1212 CLockObject lock(m_mutex);
1213 // clear previous pending active source command
1214 m_iActiveSourcePending = 0;
1215 }
1216
1217 // mark the handler as initialised
1218 CLockObject lock(m_mutex);
1219 m_bHandlerInited = true;
1220 }
1221 return true;
1222 }
1223
1224 void CCECCommandHandler::ScheduleActivateSource(uint64_t iDelay)
1225 {
1226 CLockObject lock(m_mutex);
1227 m_iActiveSourcePending = GetTimeMs() + iDelay;
1228 }