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