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