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