cec: added a callback to handle menu state changes. if the callback method returns...
[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 {
338 device->SetActiveSource();
339 return device->TransmitPhysicalAddress() &&
340 device->TransmitImageViewOn() &&
341 device->TransmitActiveSource();
342 }
343 }
344
345 return false;
346 }
347
348 bool CCECCommandHandler::HandleGiveMenuLanguage(const cec_command &command)
349 {
350 if (m_processor->IsRunning() && m_busDevice->MyLogicalAddressContains(command.destination))
351 {
352 CCECBusDevice *device = GetDevice(command.destination);
353 if (device)
354 return device->TransmitSetMenuLanguage(command.initiator);
355 }
356
357 return false;
358 }
359
360 bool CCECCommandHandler::HandleGiveSystemAudioModeStatus(const cec_command &command)
361 {
362 if (m_processor->IsRunning() && m_busDevice->MyLogicalAddressContains(command.destination))
363 {
364 CCECBusDevice *device = GetDevice(command.destination);
365 if (device && device->GetType() == CEC_DEVICE_TYPE_AUDIO_SYSTEM)
366 return ((CCECAudioSystem *) device)->TransmitSystemAudioModeStatus(command.initiator);
367 }
368
369 return false;
370 }
371
372 bool CCECCommandHandler::HandleImageViewOn(const cec_command &command)
373 {
374 m_processor->m_busDevices[command.initiator]->SetActiveSource();
375 return true;
376 }
377
378 bool CCECCommandHandler::HandleMenuRequest(const cec_command &command)
379 {
380 if (m_processor->IsRunning() && m_busDevice->MyLogicalAddressContains(command.destination))
381 {
382 CCECBusDevice *device = GetDevice(command.destination);
383 if (device)
384 {
385 if (command.parameters[0] == CEC_MENU_REQUEST_TYPE_ACTIVATE)
386 {
387 if (CLibCEC::MenuStateChanged(CEC_MENU_STATE_ACTIVATED) == 1)
388 device->SetMenuState(CEC_MENU_STATE_ACTIVATED);
389 }
390 else if (command.parameters[0] == CEC_MENU_REQUEST_TYPE_DEACTIVATE)
391 {
392 if (CLibCEC::MenuStateChanged(CEC_MENU_STATE_DEACTIVATED) == 1)
393 device->SetMenuState(CEC_MENU_STATE_DEACTIVATED);
394 }
395 return device->TransmitMenuState(command.initiator);
396 }
397 }
398
399 return false;
400 }
401
402 bool CCECCommandHandler::HandlePoll(const cec_command &command)
403 {
404 m_busDevice->HandlePoll(command.destination);
405 return true;
406 }
407
408 bool CCECCommandHandler::HandleReportAudioStatus(const cec_command &command)
409 {
410 if (command.parameters.size == 1)
411 {
412 CCECBusDevice *device = GetDevice(command.initiator);
413 if (device && device->GetType() == CEC_DEVICE_TYPE_AUDIO_SYSTEM)
414 {
415 ((CCECAudioSystem *)device)->SetAudioStatus(command.parameters[0]);
416 return true;
417 }
418 }
419 return false;
420 }
421
422 bool CCECCommandHandler::HandleReportPhysicalAddress(const cec_command &command)
423 {
424 if (command.parameters.size == 3)
425 {
426 uint16_t iNewAddress = ((uint16_t)command.parameters[0] << 8) | ((uint16_t)command.parameters[1]);
427 SetPhysicalAddress(command.initiator, iNewAddress);
428 }
429 return true;
430 }
431
432 bool CCECCommandHandler::HandleReportPowerStatus(const cec_command &command)
433 {
434 if (command.parameters.size == 1)
435 {
436 CCECBusDevice *device = GetDevice(command.initiator);
437 if (device)
438 device->SetPowerStatus((cec_power_status) command.parameters[0]);
439 }
440 return true;
441 }
442
443 bool CCECCommandHandler::HandleRequestActiveSource(const cec_command &command)
444 {
445 if (m_processor->IsRunning())
446 {
447 CLibCEC::AddLog(CEC_LOG_DEBUG, ">> %i requests active source", (uint8_t) command.initiator);
448
449 vector<CCECBusDevice *> devices;
450 for (size_t iDevicePtr = 0; iDevicePtr < GetMyDevices(devices); iDevicePtr++)
451 devices[iDevicePtr]->TransmitActiveSource();
452
453 return true;
454 }
455 return false;
456 }
457
458 bool CCECCommandHandler::HandleRoutingChange(const cec_command &command)
459 {
460 if (command.parameters.size == 4)
461 {
462 uint16_t iOldAddress = ((uint16_t)command.parameters[0] << 8) | ((uint16_t)command.parameters[1]);
463 uint16_t iNewAddress = ((uint16_t)command.parameters[2] << 8) | ((uint16_t)command.parameters[3]);
464
465 CCECBusDevice *device = GetDevice(command.initiator);
466 if (device)
467 device->SetStreamPath(iNewAddress, iOldAddress);
468 }
469 return true;
470 }
471
472 bool CCECCommandHandler::HandleRoutingInformation(const cec_command &command)
473 {
474 if (command.parameters.size == 2)
475 {
476 uint16_t iNewAddress = ((uint16_t)command.parameters[0] << 8) | ((uint16_t)command.parameters[1]);
477 m_processor->SetActiveSource(iNewAddress);
478 }
479
480 return false;
481 }
482
483 bool CCECCommandHandler::HandleSetMenuLanguage(const cec_command &command)
484 {
485 if (command.parameters.size == 3)
486 {
487 CCECBusDevice *device = GetDevice(command.initiator);
488 if (device)
489 {
490 cec_menu_language language;
491 language.device = command.initiator;
492 for (uint8_t iPtr = 0; iPtr < 4; iPtr++)
493 language.language[iPtr] = command.parameters[iPtr];
494 language.language[3] = 0;
495 device->SetMenuLanguage(language);
496 return true;
497 }
498 }
499 return false;
500 }
501
502 bool CCECCommandHandler::HandleSetOSDName(const cec_command &command)
503 {
504 if (command.parameters.size > 0)
505 {
506 CCECBusDevice *device = GetDevice(command.initiator);
507 if (device)
508 {
509 char buf[1024];
510 for (uint8_t iPtr = 0; iPtr < command.parameters.size; iPtr++)
511 buf[iPtr] = (char)command.parameters[iPtr];
512 buf[command.parameters.size] = 0;
513
514 CStdString strName(buf);
515 device->SetOSDName(strName);
516
517 return true;
518 }
519 }
520 return false;
521 }
522
523 bool CCECCommandHandler::HandleSetStreamPath(const cec_command &command)
524 {
525 if (m_processor->IsRunning() && command.parameters.size >= 2)
526 {
527 uint16_t iStreamAddress = ((uint16_t)command.parameters[0] << 8) | ((uint16_t)command.parameters[1]);
528 CLibCEC::AddLog(CEC_LOG_DEBUG, ">> %i sets stream path to physical address %04x", command.initiator, iStreamAddress);
529
530 /* one of the device handled by libCEC has been made active */
531 CCECBusDevice *device = GetDeviceByPhysicalAddress(iStreamAddress);
532 if (device && m_busDevice->MyLogicalAddressContains(device->GetLogicalAddress()))
533 {
534 device->SetActiveSource();
535 device->TransmitImageViewOn();
536 device->TransmitActiveSource();
537
538 device->SetMenuState(CEC_MENU_STATE_ACTIVATED);
539 device->TransmitMenuState(command.initiator);
540 }
541 }
542 return false;
543 }
544
545 bool CCECCommandHandler::HandleSystemAudioModeRequest(const cec_command &command)
546 {
547 if (m_processor->IsRunning() && m_busDevice->MyLogicalAddressContains(command.destination))
548 {
549 CCECBusDevice *device = GetDevice(command.destination);
550 if (device && device->GetType() == CEC_DEVICE_TYPE_AUDIO_SYSTEM)
551 {
552 if (command.parameters.size >= 2)
553 {
554 device->SetPowerStatus(CEC_POWER_STATUS_ON);
555 ((CCECAudioSystem *) device)->SetSystemAudioModeStatus(CEC_SYSTEM_AUDIO_STATUS_ON);
556 uint16_t iNewAddress = ((uint16_t)command.parameters[0] << 8) | ((uint16_t)command.parameters[1]);
557 CCECBusDevice *newActiveDevice = GetDeviceByPhysicalAddress(iNewAddress);
558 if (newActiveDevice)
559 newActiveDevice->SetActiveSource();
560 return ((CCECAudioSystem *) device)->TransmitSetSystemAudioMode(command.initiator);
561 }
562 else
563 {
564 ((CCECAudioSystem *) device)->SetSystemAudioModeStatus(CEC_SYSTEM_AUDIO_STATUS_OFF);
565 return ((CCECAudioSystem *) device)->TransmitSetSystemAudioMode(command.initiator);
566 }
567 }
568 }
569 return false;
570 }
571
572 bool CCECCommandHandler::HandleStandby(const cec_command &command)
573 {
574 CCECBusDevice *device = GetDevice(command.initiator);
575 if (device)
576 device->SetPowerStatus(CEC_POWER_STATUS_STANDBY);
577
578 return true;
579 }
580
581 bool CCECCommandHandler::HandleSystemAudioModeStatus(const cec_command &command)
582 {
583 if (command.parameters.size == 1)
584 {
585 CCECBusDevice *device = GetDevice(command.initiator);
586 if (device && device->GetType() == CEC_DEVICE_TYPE_AUDIO_SYSTEM)
587 {
588 ((CCECAudioSystem *)device)->SetSystemAudioModeStatus((cec_system_audio_status)command.parameters[0]);
589 return true;
590 }
591 }
592
593 return false;
594 }
595
596 bool CCECCommandHandler::HandleSetSystemAudioMode(const cec_command &command)
597 {
598 if (command.parameters.size == 1)
599 {
600 CCECBusDevice *device = GetDevice(command.initiator);
601 if (device && device->GetType() == CEC_DEVICE_TYPE_AUDIO_SYSTEM)
602 {
603 ((CCECAudioSystem *)device)->SetSystemAudioModeStatus((cec_system_audio_status)command.parameters[0]);
604 return true;
605 }
606 }
607
608 return false;
609 }
610
611 bool CCECCommandHandler::HandleTextViewOn(const cec_command &command)
612 {
613 m_processor->m_busDevices[command.initiator]->SetActiveSource();
614 return true;
615 }
616
617 bool CCECCommandHandler::HandleUserControlPressed(const cec_command &command)
618 {
619 if (m_processor->IsRunning() &&
620 m_busDevice->MyLogicalAddressContains(command.destination) &&
621 command.parameters.size > 0)
622 {
623 CLibCEC::AddKey();
624 if (command.parameters[0] <= CEC_USER_CONTROL_CODE_MAX)
625 CLibCEC::SetCurrentButton((cec_user_control_code) command.parameters[0]);
626
627 if (command.parameters[0] == CEC_USER_CONTROL_CODE_POWER ||
628 command.parameters[0] == CEC_USER_CONTROL_CODE_POWER_ON_FUNCTION)
629 {
630 bool bPowerOn(true);
631 CCECBusDevice *device = GetDevice(command.destination);
632 if (!device)
633 return true;
634
635 // CEC_USER_CONTROL_CODE_POWER operates as a toggle
636 // assume CEC_USER_CONTROL_CODE_POWER_ON_FUNCTION does not
637 if (command.parameters[0] == CEC_USER_CONTROL_CODE_POWER)
638 {
639 cec_power_status status = device->GetPowerStatus();
640 bPowerOn = !(status == CEC_POWER_STATUS_ON || status == CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON);
641 }
642
643 if (bPowerOn)
644 {
645 device->SetActiveSource();
646 device->TransmitImageViewOn();
647 device->TransmitActiveSource();
648
649 if (device->GetType() == CEC_DEVICE_TYPE_PLAYBACK_DEVICE ||
650 device->GetType() == CEC_DEVICE_TYPE_RECORDING_DEVICE)
651 ((CCECPlaybackDevice *)device)->TransmitDeckStatus(command.initiator);
652 }
653 else
654 {
655 device->SetInactiveSource();
656 device->TransmitInactiveSource();
657 device->SetMenuState(CEC_MENU_STATE_DEACTIVATED);
658 }
659 }
660
661 return true;
662 }
663 return false;
664 }
665
666 bool CCECCommandHandler::HandleUserControlRelease(const cec_command &command)
667 {
668 if (m_processor->IsRunning() && m_busDevice->MyLogicalAddressContains(command.destination))
669 CLibCEC::AddKey();
670
671 return true;
672 }
673
674 bool CCECCommandHandler::HandleVendorCommand(const cec_command & UNUSED(command))
675 {
676 return true;
677 }
678
679 void CCECCommandHandler::UnhandledCommand(const cec_command &command)
680 {
681 CLibCEC::AddLog(CEC_LOG_DEBUG, "unhandled command with opcode %02x from address %d", command.opcode, command.initiator);
682 }
683
684 size_t CCECCommandHandler::GetMyDevices(vector<CCECBusDevice *> &devices) const
685 {
686 size_t iReturn(0);
687
688 cec_logical_addresses addresses = m_processor->GetLogicalAddresses();
689 for (uint8_t iPtr = 0; iPtr < 16; iPtr++)
690 {
691 if (addresses[iPtr])
692 {
693 devices.push_back(GetDevice((cec_logical_address) iPtr));
694 ++iReturn;
695 }
696 }
697
698 return iReturn;
699 }
700
701 CCECBusDevice *CCECCommandHandler::GetDevice(cec_logical_address iLogicalAddress) const
702 {
703 CCECBusDevice *device = NULL;
704
705 if (iLogicalAddress >= CECDEVICE_TV && iLogicalAddress <= CECDEVICE_BROADCAST)
706 device = m_processor->m_busDevices[iLogicalAddress];
707
708 return device;
709 }
710
711 CCECBusDevice *CCECCommandHandler::GetDeviceByPhysicalAddress(uint16_t iPhysicalAddress) const
712 {
713 return m_processor->GetDeviceByPhysicalAddress(iPhysicalAddress);
714 }
715
716 CCECBusDevice *CCECCommandHandler::GetDeviceByType(cec_device_type type) const
717 {
718 return m_processor->GetDeviceByType(type);
719 }
720
721 bool CCECCommandHandler::SetVendorId(const cec_command &command)
722 {
723 bool bChanged(false);
724 if (command.parameters.size < 3)
725 {
726 CLibCEC::AddLog(CEC_LOG_WARNING, "invalid vendor ID received");
727 return bChanged;
728 }
729
730 uint64_t iVendorId = ((uint64_t)command.parameters[0] << 16) +
731 ((uint64_t)command.parameters[1] << 8) +
732 (uint64_t)command.parameters[2];
733
734 CCECBusDevice *device = GetDevice((cec_logical_address) command.initiator);
735 if (device)
736 bChanged = device->SetVendorId(iVendorId);
737 return bChanged;
738 }
739
740 void CCECCommandHandler::SetPhysicalAddress(cec_logical_address iAddress, uint16_t iNewAddress)
741 {
742 if (!m_busDevice->MyLogicalAddressContains(iAddress))
743 {
744 bool bOurAddress(m_processor->GetPhysicalAddress() == iNewAddress);
745 GetDevice(iAddress)->SetPhysicalAddress(iNewAddress);
746 if (bOurAddress)
747 {
748 /* another device reported the same physical address as ours
749 * since we don't have physical address detection yet, we'll just use the
750 * given address, increased by 0x100 for now */
751 m_processor->SetPhysicalAddress(iNewAddress + 0x100);
752 }
753 }
754 }
755
756 bool CCECCommandHandler::PowerOn(const cec_logical_address iInitiator, const cec_logical_address iDestination)
757 {
758 if (iDestination == CECDEVICE_TV)
759 return TransmitImageViewOn(iInitiator, iDestination);
760
761 return TransmitKeypress(iInitiator, iDestination, CEC_USER_CONTROL_CODE_POWER) &&
762 TransmitKeyRelease(iInitiator, iDestination);
763 }
764
765 bool CCECCommandHandler::TransmitImageViewOn(const cec_logical_address iInitiator, const cec_logical_address iDestination)
766 {
767 cec_command command;
768 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_IMAGE_VIEW_ON);
769
770 return Transmit(command, false);
771 }
772
773 bool CCECCommandHandler::TransmitStandby(const cec_logical_address iInitiator, const cec_logical_address iDestination)
774 {
775 cec_command command;
776 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_STANDBY);
777
778 return Transmit(command, false);
779 }
780
781 bool CCECCommandHandler::TransmitRequestCecVersion(const cec_logical_address iInitiator, const cec_logical_address iDestination, bool bWaitForResponse /* = true */)
782 {
783 cec_command command;
784 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_GET_CEC_VERSION);
785
786 return Transmit(command, bWaitForResponse, CEC_OPCODE_CEC_VERSION);
787 }
788
789 bool CCECCommandHandler::TransmitRequestMenuLanguage(const cec_logical_address iInitiator, const cec_logical_address iDestination, bool bWaitForResponse /* = true */)
790 {
791 cec_command command;
792 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_GET_MENU_LANGUAGE);
793
794 return Transmit(command, bWaitForResponse, CEC_OPCODE_SET_MENU_LANGUAGE);
795 }
796
797 bool CCECCommandHandler::TransmitRequestOSDName(const cec_logical_address iInitiator, const cec_logical_address iDestination, bool bWaitForResponse /* = true */)
798 {
799 cec_command command;
800 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_GIVE_OSD_NAME);
801
802 return Transmit(command, bWaitForResponse, CEC_OPCODE_SET_OSD_NAME);
803 }
804
805 bool CCECCommandHandler::TransmitRequestPhysicalAddress(const cec_logical_address iInitiator, const cec_logical_address iDestination, bool bWaitForResponse /* = true */)
806 {
807 cec_command command;
808 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_GIVE_PHYSICAL_ADDRESS);
809
810 return Transmit(command, bWaitForResponse, CEC_OPCODE_REPORT_PHYSICAL_ADDRESS);
811 }
812
813 bool CCECCommandHandler::TransmitRequestPowerStatus(const cec_logical_address iInitiator, const cec_logical_address iDestination, bool bWaitForResponse /* = true */)
814 {
815 cec_command command;
816 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_GIVE_DEVICE_POWER_STATUS);
817
818 return Transmit(command, bWaitForResponse, CEC_OPCODE_REPORT_POWER_STATUS);
819 }
820
821 bool CCECCommandHandler::TransmitRequestVendorId(const cec_logical_address iInitiator, const cec_logical_address iDestination, bool bWaitForResponse /* = true */)
822 {
823 cec_command command;
824 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_GIVE_DEVICE_VENDOR_ID);
825
826 return Transmit(command, bWaitForResponse, CEC_OPCODE_DEVICE_VENDOR_ID);
827 }
828
829 bool CCECCommandHandler::TransmitActiveSource(const cec_logical_address iInitiator, uint16_t iPhysicalAddress)
830 {
831 cec_command command;
832 cec_command::Format(command, iInitiator, CECDEVICE_BROADCAST, CEC_OPCODE_ACTIVE_SOURCE);
833 command.parameters.PushBack((uint8_t) ((iPhysicalAddress >> 8) & 0xFF));
834 command.parameters.PushBack((uint8_t) (iPhysicalAddress & 0xFF));
835
836 return Transmit(command, false);
837 }
838
839 bool CCECCommandHandler::TransmitCECVersion(const cec_logical_address iInitiator, const cec_logical_address iDestination, cec_version cecVersion)
840 {
841 cec_command command;
842 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_CEC_VERSION);
843 command.parameters.PushBack((uint8_t)cecVersion);
844
845 return Transmit(command, false);
846 }
847
848 bool CCECCommandHandler::TransmitInactiveSource(const cec_logical_address iInitiator, uint16_t iPhysicalAddress)
849 {
850 cec_command command;
851 cec_command::Format(command, iInitiator, CECDEVICE_TV, CEC_OPCODE_INACTIVE_SOURCE);
852 command.parameters.PushBack((iPhysicalAddress >> 8) & 0xFF);
853 command.parameters.PushBack(iPhysicalAddress & 0xFF);
854
855 return Transmit(command, false);
856 }
857
858 bool CCECCommandHandler::TransmitMenuState(const cec_logical_address iInitiator, const cec_logical_address iDestination, cec_menu_state menuState)
859 {
860 cec_command command;
861 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_MENU_STATUS);
862 command.parameters.PushBack((uint8_t)menuState);
863
864 return Transmit(command, false);
865 }
866
867 bool CCECCommandHandler::TransmitOSDName(const cec_logical_address iInitiator, const cec_logical_address iDestination, CStdString strDeviceName)
868 {
869 cec_command command;
870 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_SET_OSD_NAME);
871 for (size_t iPtr = 0; iPtr < strDeviceName.length(); iPtr++)
872 command.parameters.PushBack(strDeviceName.at(iPtr));
873
874 return Transmit(command, false);
875 }
876
877 bool CCECCommandHandler::TransmitOSDString(const cec_logical_address iInitiator, const cec_logical_address iDestination, cec_display_control duration, const char *strMessage)
878 {
879 cec_command command;
880 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_SET_OSD_STRING);
881 command.parameters.PushBack((uint8_t)duration);
882
883 size_t iLen = strlen(strMessage);
884 if (iLen > 13) iLen = 13;
885
886 for (size_t iPtr = 0; iPtr < iLen; iPtr++)
887 command.parameters.PushBack(strMessage[iPtr]);
888
889 return Transmit(command, false);
890 }
891
892 bool CCECCommandHandler::TransmitPhysicalAddress(const cec_logical_address iInitiator, uint16_t iPhysicalAddress, cec_device_type type)
893 {
894 cec_command command;
895 cec_command::Format(command, iInitiator, CECDEVICE_BROADCAST, CEC_OPCODE_REPORT_PHYSICAL_ADDRESS);
896 command.parameters.PushBack((uint8_t) ((iPhysicalAddress >> 8) & 0xFF));
897 command.parameters.PushBack((uint8_t) (iPhysicalAddress & 0xFF));
898 command.parameters.PushBack((uint8_t) (type));
899
900 return Transmit(command, false);
901 }
902
903 bool CCECCommandHandler::TransmitSetMenuLanguage(const cec_logical_address iInitiator, const char lang[3])
904 {
905 cec_command command;
906 command.Format(command, iInitiator, CECDEVICE_BROADCAST, CEC_OPCODE_SET_MENU_LANGUAGE);
907 command.parameters.PushBack((uint8_t) lang[0]);
908 command.parameters.PushBack((uint8_t) lang[1]);
909 command.parameters.PushBack((uint8_t) lang[2]);
910
911 return Transmit(command, false);
912 }
913
914 bool CCECCommandHandler::TransmitPoll(const cec_logical_address iInitiator, const cec_logical_address iDestination)
915 {
916 cec_command command;
917 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_NONE);
918
919 return Transmit(command, false);
920 }
921
922 bool CCECCommandHandler::TransmitPowerState(const cec_logical_address iInitiator, const cec_logical_address iDestination, cec_power_status state)
923 {
924 cec_command command;
925 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_REPORT_POWER_STATUS);
926 command.parameters.PushBack((uint8_t) state);
927
928 return Transmit(command, false);
929 }
930
931 bool CCECCommandHandler::TransmitVendorID(const cec_logical_address iInitiator, uint64_t iVendorId)
932 {
933 cec_command command;
934 cec_command::Format(command, iInitiator, CECDEVICE_BROADCAST, CEC_OPCODE_DEVICE_VENDOR_ID);
935
936 command.parameters.PushBack((uint8_t) (((uint64_t)iVendorId >> 16) & 0xFF));
937 command.parameters.PushBack((uint8_t) (((uint64_t)iVendorId >> 8) & 0xFF));
938 command.parameters.PushBack((uint8_t) ((uint64_t)iVendorId & 0xFF));
939
940 return Transmit(command, false);
941 }
942
943 bool CCECCommandHandler::TransmitAudioStatus(const cec_logical_address iInitiator, const cec_logical_address iDestination, uint8_t state)
944 {
945 cec_command command;
946 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_REPORT_AUDIO_STATUS);
947 command.parameters.PushBack(state);
948
949 return Transmit(command, false);
950 }
951
952 bool CCECCommandHandler::TransmitSetSystemAudioMode(const cec_logical_address iInitiator, const cec_logical_address iDestination, cec_system_audio_status state)
953 {
954 cec_command command;
955 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_SET_SYSTEM_AUDIO_MODE);
956 command.parameters.PushBack((uint8_t)state);
957
958 return Transmit(command, false);
959 }
960
961 bool CCECCommandHandler::TransmitSetStreamPath(uint16_t iStreamPath)
962 {
963 cec_command command;
964 cec_command::Format(command, m_busDevice->GetLogicalAddress(), CECDEVICE_BROADCAST, CEC_OPCODE_SET_STREAM_PATH);
965 command.parameters.PushBack((uint8_t) ((iStreamPath >> 8) & 0xFF));
966 command.parameters.PushBack((uint8_t) (iStreamPath & 0xFF));
967
968 return Transmit(command, false);
969 }
970
971 bool CCECCommandHandler::TransmitSystemAudioModeStatus(const cec_logical_address iInitiator, const cec_logical_address iDestination, cec_system_audio_status state)
972 {
973 cec_command command;
974 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_SYSTEM_AUDIO_MODE_STATUS);
975 command.parameters.PushBack((uint8_t)state);
976
977 return Transmit(command, false);
978 }
979
980 bool CCECCommandHandler::TransmitDeckStatus(const cec_logical_address iInitiator, const cec_logical_address iDestination, cec_deck_info state)
981 {
982 cec_command command;
983 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_DECK_STATUS);
984 command.PushBack((uint8_t)state);
985
986 return Transmit(command, false);
987 }
988
989 bool CCECCommandHandler::TransmitKeypress(const cec_logical_address iInitiator, const cec_logical_address iDestination, cec_user_control_code key, bool bWait /* = true */)
990 {
991 cec_command command;
992 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_USER_CONTROL_PRESSED);
993 command.parameters.PushBack((uint8_t)key);
994
995 return Transmit(command, bWait);
996 }
997
998 bool CCECCommandHandler::TransmitKeyRelease(const cec_logical_address iInitiator, const cec_logical_address iDestination, bool bWait /* = true */)
999 {
1000 cec_command command;
1001 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_USER_CONTROL_RELEASE);
1002
1003 return Transmit(command, bWait);
1004 }
1005
1006 bool CCECCommandHandler::Transmit(cec_command &command, bool bExpectResponse /* = true */, cec_opcode expectedResponse /* = CEC_OPCODE_NONE */)
1007 {
1008 bool bReturn(false);
1009 command.transmit_timeout = m_iTransmitTimeout;
1010
1011 if (command.initiator == CECDEVICE_UNKNOWN)
1012 {
1013 CLibCEC::AddLog(CEC_LOG_ERROR, "not transmitting a command without a valid initiator");
1014 return bReturn;
1015 }
1016
1017 {
1018 uint8_t iTries(0), iMaxTries(command.opcode == CEC_OPCODE_NONE ? 1 : m_iTransmitRetries + 1);
1019 while (!bReturn && ++iTries <= iMaxTries)
1020 {
1021 if ((bReturn = m_processor->Transmit(command)) == true)
1022 {
1023 CLibCEC::AddLog(CEC_LOG_DEBUG, "command transmitted");
1024 if (bExpectResponse)
1025 {
1026 bReturn = m_waitForResponse->Wait(expectedResponse);
1027 CLibCEC::AddLog(CEC_LOG_DEBUG, bReturn ? "expected response received (%X: %s)" : "expected response not received (%X: %s)", (int)expectedResponse, m_processor->ToString(expectedResponse));
1028 }
1029 }
1030 }
1031 }
1032
1033 return bReturn;
1034 }
1035
1036 bool CCECCommandHandler::ActivateSource(void)
1037 {
1038 if (m_busDevice->IsActiveSource() &&
1039 m_busDevice->GetStatus(false) == CEC_DEVICE_STATUS_HANDLED_BY_LIBCEC)
1040 {
1041 m_busDevice->SetPowerStatus(CEC_POWER_STATUS_ON);
1042 m_busDevice->SetMenuState(CEC_MENU_STATE_ACTIVATED);
1043
1044 m_busDevice->TransmitImageViewOn();
1045 m_busDevice->TransmitActiveSource();
1046 m_busDevice->TransmitMenuState(CECDEVICE_TV);
1047 if ((m_busDevice->GetType() == CEC_DEVICE_TYPE_PLAYBACK_DEVICE ||
1048 m_busDevice->GetType() == CEC_DEVICE_TYPE_RECORDING_DEVICE) &&
1049 SendDeckStatusUpdateOnActiveSource())
1050 ((CCECPlaybackDevice *)m_busDevice)->TransmitDeckStatus(CECDEVICE_TV);
1051 m_bHandlerInited = true;
1052 }
1053 return true;
1054 }