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