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