cec: fixed - only the key release events were sent, not keypresses, which appeared...
[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 Pulse-Eight Limited. All rights reserved.
5 * libCEC(R) is an original work, containing original code.
6 *
7 * libCEC(R) is a trademark of Pulse-Eight Limited.
8 *
9 * This program is dual-licensed; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22 *
23 *
24 * Alternatively, you can license this library under a commercial license,
25 * please contact Pulse-Eight Licensing for more information.
26 *
27 * For more information contact:
28 * Pulse-Eight Licensing <license@pulse-eight.com>
29 * http://www.pulse-eight.com/
30 * http://www.pulse-eight.net/
31 */
32
33 #include "CECCommandHandler.h"
34 #include "../devices/CECBusDevice.h"
35 #include "../devices/CECAudioSystem.h"
36 #include "../devices/CECPlaybackDevice.h"
37 #include "../CECProcessor.h"
38 #include "../LibCEC.h"
39
40 using namespace CEC;
41 using namespace std;
42 using namespace PLATFORM;
43
44 CCECCommandHandler::CCECCommandHandler(CCECBusDevice *busDevice) :
45 m_busDevice(busDevice),
46 m_processor(m_busDevice->GetProcessor()),
47 m_iTransmitTimeout(CEC_DEFAULT_TRANSMIT_TIMEOUT),
48 m_iTransmitWait(CEC_DEFAULT_TRANSMIT_WAIT),
49 m_iTransmitRetries(CEC_DEFAULT_TRANSMIT_RETRIES),
50 m_bHandlerInited(false),
51 m_iUseCounter(0),
52 m_expectedResponse(CEC_OPCODE_NONE),
53 m_bOPTSendDeckStatusUpdateOnActiveSource(false),
54 m_vendorId(CEC_VENDOR_UNKNOWN)
55 {
56 }
57
58 CCECCommandHandler::~CCECCommandHandler(void)
59 {
60 CLockObject lock(m_processor->m_transmitMutex);
61 CLockObject receiveLock(m_receiveMutex);
62 m_condition.Broadcast();
63 }
64
65 bool CCECCommandHandler::HandleCommand(const cec_command &command)
66 {
67 bool bHandled(true);
68
69 MarkBusy();
70 CLibCEC::AddCommand(command);
71
72 switch(command.opcode)
73 {
74 case CEC_OPCODE_REPORT_POWER_STATUS:
75 HandleReportPowerStatus(command);
76 break;
77 case CEC_OPCODE_CEC_VERSION:
78 HandleDeviceCecVersion(command);
79 break;
80 case CEC_OPCODE_SET_MENU_LANGUAGE:
81 HandleSetMenuLanguage(command);
82 break;
83 case CEC_OPCODE_GIVE_PHYSICAL_ADDRESS:
84 if (m_processor->IsInitialised())
85 HandleGivePhysicalAddress(command);
86 break;
87 case CEC_OPCODE_GIVE_OSD_NAME:
88 if (m_processor->IsInitialised())
89 HandleGiveOSDName(command);
90 break;
91 case CEC_OPCODE_GIVE_DEVICE_VENDOR_ID:
92 if (m_processor->IsInitialised())
93 HandleGiveDeviceVendorId(command);
94 break;
95 case CEC_OPCODE_DEVICE_VENDOR_ID:
96 HandleDeviceVendorId(command);
97 break;
98 case CEC_OPCODE_VENDOR_COMMAND_WITH_ID:
99 HandleDeviceVendorCommandWithId(command);
100 break;
101 case CEC_OPCODE_GIVE_DECK_STATUS:
102 if (m_processor->IsInitialised())
103 HandleGiveDeckStatus(command);
104 break;
105 case CEC_OPCODE_DECK_CONTROL:
106 HandleDeckControl(command);
107 break;
108 case CEC_OPCODE_MENU_REQUEST:
109 if (m_processor->IsInitialised())
110 HandleMenuRequest(command);
111 break;
112 case CEC_OPCODE_GIVE_DEVICE_POWER_STATUS:
113 if (m_processor->IsInitialised())
114 HandleGiveDevicePowerStatus(command);
115 break;
116 case CEC_OPCODE_GET_CEC_VERSION:
117 if (m_processor->IsInitialised())
118 HandleGetCecVersion(command);
119 break;
120 case CEC_OPCODE_USER_CONTROL_PRESSED:
121 if (m_processor->IsInitialised())
122 HandleUserControlPressed(command);
123 break;
124 case CEC_OPCODE_USER_CONTROL_RELEASE:
125 if (m_processor->IsInitialised())
126 HandleUserControlRelease(command);
127 break;
128 case CEC_OPCODE_GIVE_AUDIO_STATUS:
129 if (m_processor->IsInitialised())
130 HandleGiveAudioStatus(command);
131 break;
132 case CEC_OPCODE_GIVE_SYSTEM_AUDIO_MODE_STATUS:
133 if (m_processor->IsInitialised())
134 HandleGiveSystemAudioModeStatus(command);
135 break;
136 case CEC_OPCODE_SYSTEM_AUDIO_MODE_REQUEST:
137 if (m_processor->IsInitialised())
138 HandleSystemAudioModeRequest(command);
139 break;
140 case CEC_OPCODE_REPORT_AUDIO_STATUS:
141 HandleReportAudioStatus(command);
142 break;
143 case CEC_OPCODE_SYSTEM_AUDIO_MODE_STATUS:
144 HandleSystemAudioModeStatus(command);
145 break;
146 case CEC_OPCODE_SET_SYSTEM_AUDIO_MODE:
147 HandleSetSystemAudioMode(command);
148 break;
149 case CEC_OPCODE_REQUEST_ACTIVE_SOURCE:
150 if (m_processor->IsInitialised())
151 HandleRequestActiveSource(command);
152 break;
153 case CEC_OPCODE_SET_STREAM_PATH:
154 HandleSetStreamPath(command);
155 break;
156 case CEC_OPCODE_ROUTING_CHANGE:
157 HandleRoutingChange(command);
158 break;
159 case CEC_OPCODE_ROUTING_INFORMATION:
160 HandleRoutingInformation(command);
161 break;
162 case CEC_OPCODE_STANDBY:
163 if (m_processor->IsInitialised())
164 HandleStandby(command);
165 break;
166 case CEC_OPCODE_ACTIVE_SOURCE:
167 HandleActiveSource(command);
168 break;
169 case CEC_OPCODE_REPORT_PHYSICAL_ADDRESS:
170 HandleReportPhysicalAddress(command);
171 break;
172 case CEC_OPCODE_SET_OSD_NAME:
173 HandleSetOSDName(command);
174 break;
175 case CEC_OPCODE_IMAGE_VIEW_ON:
176 HandleImageViewOn(command);
177 break;
178 case CEC_OPCODE_TEXT_VIEW_ON:
179 HandleTextViewOn(command);
180 break;
181 case CEC_OPCODE_FEATURE_ABORT:
182 HandleFeatureAbort(command);
183 break;
184 case CEC_OPCODE_VENDOR_COMMAND:
185 HandleVendorCommand(command);
186 break;
187 default:
188 UnhandledCommand(command);
189 bHandled = false;
190 break;
191 }
192
193 if (bHandled)
194 {
195 CLockObject lock(m_receiveMutex);
196 if (m_expectedResponse == CEC_OPCODE_NONE ||
197 m_expectedResponse == command.opcode)
198 m_condition.Signal();
199 }
200
201 MarkReady();
202 return bHandled;
203 }
204
205 bool CCECCommandHandler::HandleActiveSource(const cec_command &command)
206 {
207 if (command.parameters.size == 2)
208 {
209 uint16_t iAddress = ((uint16_t)command.parameters[0] << 8) | ((uint16_t)command.parameters[1]);
210 return m_processor->SetActiveSource(iAddress);
211 }
212
213 return true;
214 }
215
216 bool CCECCommandHandler::HandleDeckControl(const cec_command &command)
217 {
218 CCECBusDevice *device = GetDevice(command.destination);
219 if (device && (device->GetType() == CEC_DEVICE_TYPE_PLAYBACK_DEVICE || device->GetType() == CEC_DEVICE_TYPE_RECORDING_DEVICE) && command.parameters.size > 0)
220 {
221 ((CCECPlaybackDevice *) device)->SetDeckControlMode((cec_deck_control_mode) command.parameters[0]);
222 return true;
223 }
224
225 return false;
226 }
227
228 bool CCECCommandHandler::HandleDeviceCecVersion(const cec_command &command)
229 {
230 if (command.parameters.size == 1)
231 {
232 CCECBusDevice *device = GetDevice(command.initiator);
233 if (device)
234 device->SetCecVersion((cec_version) command.parameters[0]);
235 }
236
237 return true;
238 }
239
240 bool CCECCommandHandler::HandleDeviceVendorCommandWithId(const cec_command &command)
241 {
242 if (m_processor->IsStarted() && m_busDevice->MyLogicalAddressContains(command.destination))
243 m_processor->TransmitAbort(command.initiator, command.opcode, CEC_ABORT_REASON_REFUSED);
244
245 return true;
246 }
247
248 bool CCECCommandHandler::HandleDeviceVendorId(const cec_command &command)
249 {
250 return SetVendorId(command);
251 }
252
253 bool CCECCommandHandler::HandleFeatureAbort(const cec_command &command)
254 {
255 if (command.parameters.size == 2)
256 {
257 m_processor->m_busDevices[command.initiator]->SetUnsupportedFeature((cec_opcode)command.parameters[0]);
258 }
259 return true;
260 }
261
262 bool CCECCommandHandler::HandleGetCecVersion(const cec_command &command)
263 {
264 if (m_processor->IsStarted() && m_busDevice->MyLogicalAddressContains(command.destination))
265 {
266 CCECBusDevice *device = GetDevice(command.destination);
267 if (device)
268 return device->TransmitCECVersion(command.initiator);
269 }
270
271 return false;
272 }
273
274 bool CCECCommandHandler::HandleGiveAudioStatus(const cec_command &command)
275 {
276 if (m_processor->IsStarted() && m_busDevice->MyLogicalAddressContains(command.destination))
277 {
278 CCECBusDevice *device = GetDevice(command.destination);
279 if (device && device->GetType() == CEC_DEVICE_TYPE_AUDIO_SYSTEM)
280 return ((CCECAudioSystem *) device)->TransmitAudioStatus(command.initiator);
281 }
282
283 return false;
284 }
285
286 bool CCECCommandHandler::HandleGiveDeckStatus(const cec_command &command)
287 {
288 if (m_processor->IsStarted() && m_busDevice->MyLogicalAddressContains(command.destination))
289 {
290 CCECBusDevice *device = GetDevice(command.destination);
291 if (device && (device->GetType() == CEC_DEVICE_TYPE_PLAYBACK_DEVICE || device->GetType() == CEC_DEVICE_TYPE_RECORDING_DEVICE))
292 return ((CCECPlaybackDevice *) device)->TransmitDeckStatus(command.initiator);
293 }
294
295 return false;
296 }
297
298 bool CCECCommandHandler::HandleGiveDevicePowerStatus(const cec_command &command)
299 {
300 if (m_processor->IsStarted() && m_busDevice->MyLogicalAddressContains(command.destination))
301 {
302 CCECBusDevice *device = GetDevice(command.destination);
303 if (device)
304 return device->TransmitPowerState(command.initiator);
305 }
306
307 return false;
308 }
309
310 bool CCECCommandHandler::HandleGiveDeviceVendorId(const cec_command &command)
311 {
312 if (m_processor->IsStarted() && m_busDevice->MyLogicalAddressContains(command.destination))
313 {
314 CCECBusDevice *device = GetDevice(command.destination);
315 if (device)
316 return device->TransmitVendorID(command.initiator);
317 }
318
319 return false;
320 }
321
322 bool CCECCommandHandler::HandleGiveOSDName(const cec_command &command)
323 {
324 if (m_processor->IsStarted() && m_busDevice->MyLogicalAddressContains(command.destination))
325 {
326 CCECBusDevice *device = GetDevice(command.destination);
327 if (device)
328 return device->TransmitOSDName(command.initiator);
329 }
330
331 return false;
332 }
333
334 bool CCECCommandHandler::HandleGivePhysicalAddress(const cec_command &command)
335 {
336 if (m_processor->IsStarted() && m_busDevice->MyLogicalAddressContains(command.destination))
337 {
338 CCECBusDevice *device = GetDevice(command.destination);
339 if (device)
340 {
341 device->SetActiveSource();
342 return device->TransmitPhysicalAddress() &&
343 device->TransmitActiveSource();
344 }
345 }
346
347 return false;
348 }
349
350 bool CCECCommandHandler::HandleGiveSystemAudioModeStatus(const cec_command &command)
351 {
352 if (m_processor->IsStarted() && 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
362 bool CCECCommandHandler::HandleImageViewOn(const cec_command &command)
363 {
364 m_processor->m_busDevices[command.initiator]->SetActiveSource();
365 return true;
366 }
367
368 bool CCECCommandHandler::HandleMenuRequest(const cec_command &command)
369 {
370 if (m_processor->IsStarted() && 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
383 bool 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
397 bool 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
407 bool 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
418 bool CCECCommandHandler::HandleRequestActiveSource(const cec_command &command)
419 {
420 if (m_processor->IsStarted())
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
433 bool 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
447 bool 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
458 bool 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
477 bool 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
498 bool CCECCommandHandler::HandleSetStreamPath(const cec_command &command)
499 {
500 if (m_processor->IsStarted() && 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->TransmitActiveSource();
511 }
512 }
513 return false;
514 }
515
516 bool CCECCommandHandler::HandleSystemAudioModeRequest(const cec_command &command)
517 {
518 if (m_processor->IsStarted() && m_busDevice->MyLogicalAddressContains(command.destination))
519 {
520 CCECBusDevice *device = GetDevice(command.destination);
521 if (device && device->GetType() == CEC_DEVICE_TYPE_AUDIO_SYSTEM)
522 {
523 if (command.parameters.size >= 2)
524 {
525 device->SetPowerStatus(CEC_POWER_STATUS_ON);
526 ((CCECAudioSystem *) device)->SetSystemAudioModeStatus(CEC_SYSTEM_AUDIO_STATUS_ON);
527 uint16_t iNewAddress = ((uint16_t)command.parameters[0] << 8) | ((uint16_t)command.parameters[1]);
528 CCECBusDevice *newActiveDevice = GetDeviceByPhysicalAddress(iNewAddress);
529 if (newActiveDevice)
530 newActiveDevice->SetActiveSource();
531 return ((CCECAudioSystem *) device)->TransmitSetSystemAudioMode(command.initiator);
532 }
533 else
534 {
535 ((CCECAudioSystem *) device)->SetSystemAudioModeStatus(CEC_SYSTEM_AUDIO_STATUS_OFF);
536 return ((CCECAudioSystem *) device)->TransmitSetSystemAudioMode(command.initiator);
537 }
538 }
539 }
540 return false;
541 }
542
543 bool CCECCommandHandler::HandleStandby(const cec_command &command)
544 {
545 CCECBusDevice *device = GetDevice(command.initiator);
546 if (device)
547 device->SetPowerStatus(CEC_POWER_STATUS_STANDBY);
548
549 return true;
550 }
551
552 bool CCECCommandHandler::HandleSystemAudioModeStatus(const cec_command &command)
553 {
554 if (command.parameters.size == 1)
555 {
556 CCECBusDevice *device = GetDevice(command.initiator);
557 if (device && device->GetType() == CEC_DEVICE_TYPE_AUDIO_SYSTEM)
558 {
559 ((CCECAudioSystem *)device)->SetSystemAudioModeStatus((cec_system_audio_status)command.parameters[0]);
560 return true;
561 }
562 }
563
564 return false;
565 }
566
567 bool CCECCommandHandler::HandleSetSystemAudioMode(const cec_command &command)
568 {
569 if (command.parameters.size == 1)
570 {
571 CCECBusDevice *device = GetDevice(command.initiator);
572 if (device && device->GetType() == CEC_DEVICE_TYPE_AUDIO_SYSTEM)
573 {
574 ((CCECAudioSystem *)device)->SetSystemAudioModeStatus((cec_system_audio_status)command.parameters[0]);
575 return true;
576 }
577 }
578
579 return false;
580 }
581
582 bool CCECCommandHandler::HandleTextViewOn(const cec_command &command)
583 {
584 m_processor->m_busDevices[command.initiator]->SetActiveSource();
585 return true;
586 }
587
588 bool CCECCommandHandler::HandleUserControlPressed(const cec_command &command)
589 {
590 if (m_processor->IsStarted() && m_busDevice->MyLogicalAddressContains(command.destination) && command.parameters.size > 0)
591 {
592 CLibCEC::AddKey();
593
594 if (command.parameters[0] <= CEC_USER_CONTROL_CODE_MAX)
595 {
596 if (command.parameters[0] == CEC_USER_CONTROL_CODE_POWER ||
597 command.parameters[0] == CEC_USER_CONTROL_CODE_POWER_ON_FUNCTION)
598 {
599 CCECBusDevice *device = GetDevice(command.destination);
600 if (device)
601 {
602 device->SetPowerStatus(CEC_POWER_STATUS_ON);
603 if (device->MyLogicalAddressContains(device->GetLogicalAddress()))
604 {
605 device->SetActiveSource();
606 device->TransmitActiveSource();
607
608 if (device->GetType() == CEC_DEVICE_TYPE_PLAYBACK_DEVICE ||
609 device->GetType() == CEC_DEVICE_TYPE_RECORDING_DEVICE)
610 ((CCECPlaybackDevice *)device)->TransmitDeckStatus(command.initiator);
611 }
612 }
613 }
614 else
615 {
616 CLibCEC::SetCurrentButton((cec_user_control_code) command.parameters[0]);
617 }
618 return true;
619 }
620 }
621 return false;
622 }
623
624 bool CCECCommandHandler::HandleUserControlRelease(const cec_command &command)
625 {
626 if (m_processor->IsStarted() && m_busDevice->MyLogicalAddressContains(command.destination))
627 CLibCEC::AddKey();
628
629 return true;
630 }
631
632 bool CCECCommandHandler::HandleVendorCommand(const cec_command & UNUSED(command))
633 {
634 return true;
635 }
636
637 void CCECCommandHandler::UnhandledCommand(const cec_command &command)
638 {
639 CLibCEC::AddLog(CEC_LOG_DEBUG, "unhandled command with opcode %02x from address %d", command.opcode, command.initiator);
640 }
641
642 size_t CCECCommandHandler::GetMyDevices(vector<CCECBusDevice *> &devices) const
643 {
644 size_t iReturn(0);
645
646 cec_logical_addresses addresses = m_processor->GetLogicalAddresses();
647 for (uint8_t iPtr = 0; iPtr < 16; iPtr++)
648 {
649 if (addresses[iPtr])
650 {
651 devices.push_back(GetDevice((cec_logical_address) iPtr));
652 ++iReturn;
653 }
654 }
655
656 return iReturn;
657 }
658
659 CCECBusDevice *CCECCommandHandler::GetDevice(cec_logical_address iLogicalAddress) const
660 {
661 CCECBusDevice *device = NULL;
662
663 if (iLogicalAddress >= CECDEVICE_TV && iLogicalAddress <= CECDEVICE_BROADCAST)
664 device = m_processor->m_busDevices[iLogicalAddress];
665
666 return device;
667 }
668
669 CCECBusDevice *CCECCommandHandler::GetDeviceByPhysicalAddress(uint16_t iPhysicalAddress) const
670 {
671 return m_processor->GetDeviceByPhysicalAddress(iPhysicalAddress);
672 }
673
674 CCECBusDevice *CCECCommandHandler::GetDeviceByType(cec_device_type type) const
675 {
676 return m_processor->GetDeviceByType(type);
677 }
678
679 bool CCECCommandHandler::SetVendorId(const cec_command &command)
680 {
681 bool bChanged(false);
682 if (command.parameters.size < 3)
683 {
684 CLibCEC::AddLog(CEC_LOG_WARNING, "invalid vendor ID received");
685 return bChanged;
686 }
687
688 uint64_t iVendorId = ((uint64_t)command.parameters[0] << 16) +
689 ((uint64_t)command.parameters[1] << 8) +
690 (uint64_t)command.parameters[2];
691
692 CCECBusDevice *device = GetDevice((cec_logical_address) command.initiator);
693 if (device)
694 bChanged = device->SetVendorId(iVendorId);
695 return bChanged;
696 }
697
698 void CCECCommandHandler::SetPhysicalAddress(cec_logical_address iAddress, uint16_t iNewAddress)
699 {
700 if (!m_busDevice->MyLogicalAddressContains(iAddress))
701 {
702 bool bOurAddress(m_processor->GetPhysicalAddress() == iNewAddress);
703 GetDevice(iAddress)->SetPhysicalAddress(iNewAddress);
704 if (bOurAddress)
705 {
706 /* another device reported the same physical address as ours
707 * since we don't have physical address detection yet, we'll just use the
708 * given address, increased by 0x100 for now */
709 m_processor->SetPhysicalAddress(iNewAddress + 0x100);
710 }
711 }
712 }
713
714 void CCECCommandHandler::HandlePoll(const cec_logical_address iInitiator, const cec_logical_address iDestination)
715 {
716 CLibCEC::AddLog(CEC_LOG_DEBUG, "<< POLL: %s (%x) -> %s (%x)", m_processor->ToString(iInitiator), iInitiator, m_processor->ToString(iDestination), iDestination);
717 }
718
719 bool CCECCommandHandler::HandleReceiveFailed(void)
720 {
721 /* default = error */
722 return true;
723 }
724
725 bool CCECCommandHandler::TransmitImageViewOn(const cec_logical_address iInitiator, const cec_logical_address iDestination)
726 {
727 cec_command command;
728 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_IMAGE_VIEW_ON);
729
730 return Transmit(command, false);
731 }
732
733 bool CCECCommandHandler::TransmitStandby(const cec_logical_address iInitiator, const cec_logical_address iDestination)
734 {
735 cec_command command;
736 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_STANDBY);
737
738 return Transmit(command, false);
739 }
740
741 bool CCECCommandHandler::TransmitRequestCecVersion(const cec_logical_address iInitiator, const cec_logical_address iDestination)
742 {
743 cec_command command;
744 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_GET_CEC_VERSION);
745
746 return Transmit(command, true, CEC_OPCODE_CEC_VERSION);
747 }
748
749 bool CCECCommandHandler::TransmitRequestMenuLanguage(const cec_logical_address iInitiator, const cec_logical_address iDestination)
750 {
751 cec_command command;
752 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_GET_MENU_LANGUAGE);
753
754 return Transmit(command, true, CEC_OPCODE_SET_MENU_LANGUAGE);
755 }
756
757 bool CCECCommandHandler::TransmitRequestOSDName(const cec_logical_address iInitiator, const cec_logical_address iDestination)
758 {
759 cec_command command;
760 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_GIVE_OSD_NAME);
761
762 return Transmit(command, true, CEC_OPCODE_SET_OSD_NAME);
763 }
764
765 bool CCECCommandHandler::TransmitRequestPhysicalAddress(const cec_logical_address iInitiator, const cec_logical_address iDestination)
766 {
767 cec_command command;
768 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_GIVE_PHYSICAL_ADDRESS);
769
770 return Transmit(command, true, CEC_OPCODE_REPORT_PHYSICAL_ADDRESS);
771 }
772
773 bool CCECCommandHandler::TransmitRequestPowerStatus(const cec_logical_address iInitiator, const cec_logical_address iDestination)
774 {
775 cec_command command;
776 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_GIVE_DEVICE_POWER_STATUS);
777
778 return Transmit(command, true, CEC_OPCODE_REPORT_POWER_STATUS);
779 }
780
781 bool CCECCommandHandler::TransmitRequestVendorId(const cec_logical_address iInitiator, const cec_logical_address iDestination)
782 {
783 cec_command command;
784 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_GIVE_DEVICE_VENDOR_ID);
785
786 return Transmit(command, true, CEC_OPCODE_DEVICE_VENDOR_ID);
787 }
788
789 bool CCECCommandHandler::TransmitActiveSource(const cec_logical_address iInitiator, uint16_t iPhysicalAddress)
790 {
791 cec_command command;
792 cec_command::Format(command, iInitiator, CECDEVICE_BROADCAST, CEC_OPCODE_ACTIVE_SOURCE);
793 command.parameters.PushBack((uint8_t) ((iPhysicalAddress >> 8) & 0xFF));
794 command.parameters.PushBack((uint8_t) (iPhysicalAddress & 0xFF));
795
796 return Transmit(command, false);
797 }
798
799 bool CCECCommandHandler::TransmitCECVersion(const cec_logical_address iInitiator, const cec_logical_address iDestination, cec_version cecVersion)
800 {
801 cec_command command;
802 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_CEC_VERSION);
803 command.parameters.PushBack((uint8_t)cecVersion);
804
805 return Transmit(command, false);
806 }
807
808 bool CCECCommandHandler::TransmitInactiveSource(const cec_logical_address iInitiator, uint16_t iPhysicalAddress)
809 {
810 cec_command command;
811 cec_command::Format(command, iInitiator, CECDEVICE_TV, CEC_OPCODE_INACTIVE_SOURCE);
812 command.parameters.PushBack((iPhysicalAddress >> 8) & 0xFF);
813 command.parameters.PushBack(iPhysicalAddress & 0xFF);
814
815 return Transmit(command, false);
816 }
817
818 bool CCECCommandHandler::TransmitMenuState(const cec_logical_address iInitiator, const cec_logical_address iDestination, cec_menu_state menuState)
819 {
820 cec_command command;
821 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_MENU_STATUS);
822 command.parameters.PushBack((uint8_t)menuState);
823
824 return Transmit(command, false);
825 }
826
827 bool CCECCommandHandler::TransmitOSDName(const cec_logical_address iInitiator, const cec_logical_address iDestination, CStdString strDeviceName)
828 {
829 cec_command command;
830 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_SET_OSD_NAME);
831 for (size_t iPtr = 0; iPtr < strDeviceName.length(); iPtr++)
832 command.parameters.PushBack(strDeviceName.at(iPtr));
833
834 return Transmit(command, false);
835 }
836
837 bool CCECCommandHandler::TransmitOSDString(const cec_logical_address iInitiator, const cec_logical_address iDestination, cec_display_control duration, const char *strMessage)
838 {
839 cec_command command;
840 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_SET_OSD_STRING);
841 command.parameters.PushBack((uint8_t)duration);
842
843 size_t iLen = strlen(strMessage);
844 if (iLen > 13) iLen = 13;
845
846 for (size_t iPtr = 0; iPtr < iLen; iPtr++)
847 command.parameters.PushBack(strMessage[iPtr]);
848
849 return Transmit(command, false);
850 }
851
852 bool CCECCommandHandler::TransmitPhysicalAddress(const cec_logical_address iInitiator, uint16_t iPhysicalAddress, cec_device_type type)
853 {
854 cec_command command;
855 cec_command::Format(command, iInitiator, CECDEVICE_BROADCAST, CEC_OPCODE_REPORT_PHYSICAL_ADDRESS);
856 command.parameters.PushBack((uint8_t) ((iPhysicalAddress >> 8) & 0xFF));
857 command.parameters.PushBack((uint8_t) (iPhysicalAddress & 0xFF));
858 command.parameters.PushBack((uint8_t) (type));
859
860 return Transmit(command, false);
861 }
862
863 bool CCECCommandHandler::TransmitPoll(const cec_logical_address iInitiator, const cec_logical_address iDestination)
864 {
865 cec_command command;
866 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_NONE);
867
868 return Transmit(command, false);
869 }
870
871 bool CCECCommandHandler::TransmitPowerState(const cec_logical_address iInitiator, const cec_logical_address iDestination, cec_power_status state)
872 {
873 cec_command command;
874 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_REPORT_POWER_STATUS);
875 command.parameters.PushBack((uint8_t) state);
876
877 return Transmit(command, false);
878 }
879
880 bool CCECCommandHandler::TransmitVendorID(const cec_logical_address iInitiator, uint64_t iVendorId)
881 {
882 cec_command command;
883 cec_command::Format(command, iInitiator, CECDEVICE_BROADCAST, CEC_OPCODE_DEVICE_VENDOR_ID);
884
885 command.parameters.PushBack((uint8_t) (((uint64_t)iVendorId >> 16) & 0xFF));
886 command.parameters.PushBack((uint8_t) (((uint64_t)iVendorId >> 8) & 0xFF));
887 command.parameters.PushBack((uint8_t) ((uint64_t)iVendorId & 0xFF));
888
889 return Transmit(command, false);
890 }
891
892 bool CCECCommandHandler::TransmitAudioStatus(const cec_logical_address iInitiator, const cec_logical_address iDestination, uint8_t state)
893 {
894 cec_command command;
895 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_REPORT_AUDIO_STATUS);
896 command.parameters.PushBack(state);
897
898 return Transmit(command, false);
899 }
900
901 bool CCECCommandHandler::TransmitSetSystemAudioMode(const cec_logical_address iInitiator, const cec_logical_address iDestination, cec_system_audio_status state)
902 {
903 cec_command command;
904 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_SET_SYSTEM_AUDIO_MODE);
905 command.parameters.PushBack((uint8_t)state);
906
907 return Transmit(command, false);
908 }
909
910 bool CCECCommandHandler::TransmitSystemAudioModeStatus(const cec_logical_address iInitiator, const cec_logical_address iDestination, cec_system_audio_status state)
911 {
912 cec_command command;
913 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_SYSTEM_AUDIO_MODE_STATUS);
914 command.parameters.PushBack((uint8_t)state);
915
916 return Transmit(command, false);
917 }
918
919 bool CCECCommandHandler::TransmitDeckStatus(const cec_logical_address iInitiator, const cec_logical_address iDestination, cec_deck_info state)
920 {
921 cec_command command;
922 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_DECK_STATUS);
923 command.PushBack((uint8_t)state);
924
925 return Transmit(command, false);
926 }
927
928 bool CCECCommandHandler::TransmitKeypress(const cec_logical_address iInitiator, const cec_logical_address iDestination, cec_user_control_code key, bool bWait /* = true */)
929 {
930 cec_command command;
931 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_USER_CONTROL_PRESSED);
932 command.parameters.PushBack((uint8_t)key);
933
934 return Transmit(command, bWait);
935 }
936
937 bool CCECCommandHandler::TransmitKeyRelease(const cec_logical_address iInitiator, const cec_logical_address iDestination, bool bWait /* = true */)
938 {
939 cec_command command;
940 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_USER_CONTROL_RELEASE);
941
942 return Transmit(command, bWait);
943 }
944
945 bool CCECCommandHandler::Transmit(cec_command &command, bool bExpectResponse /* = true */, cec_opcode expectedResponse /* = CEC_OPCODE_NONE */)
946 {
947 bool bReturn(false);
948 MarkBusy();
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 ++m_iUseCounter;
956 while (!bReturn && ++iTries <= iMaxTries)
957 {
958 m_expectedResponse = expectedResponse;
959 if (m_processor->Transmit(command))
960 {
961 CLibCEC::AddLog(CEC_LOG_DEBUG, "command transmitted");
962 bReturn = bExpectResponse ?
963 m_condition.Wait(m_receiveMutex, m_iTransmitWait) :
964 true;
965 }
966 }
967 --m_iUseCounter;
968 }
969
970 MarkReady();
971 return bReturn;
972 }
973
974 bool CCECCommandHandler::ActivateSource(void)
975 {
976 if (m_busDevice->GetLogicalAddress() == CECDEVICE_TV)
977 {
978 CCECBusDevice *primary = m_processor->GetPrimaryDevice();
979 primary->SetPowerStatus(CEC_POWER_STATUS_ON);
980 primary->SetMenuState(CEC_MENU_STATE_ACTIVATED);
981
982 if (m_processor->GetPrimaryDevice()->GetPhysicalAddress(false) != 0xffff)
983 {
984 m_processor->SetActiveSource();
985 primary->TransmitMenuState(m_busDevice->GetLogicalAddress());
986 m_bHandlerInited = true;
987 }
988 }
989 return true;
990 }
991
992 void CCECCommandHandler::MarkBusy(void)
993 {
994 CLockObject receiveLock(m_receiveMutex);
995 ++m_iUseCounter;
996 }
997
998 bool CCECCommandHandler::MarkReady(void)
999 {
1000 CLockObject receiveLock(m_receiveMutex);
1001 return m_iUseCounter > 0 ? (--m_iUseCounter == 0) : true;
1002 }
1003
1004 bool CCECCommandHandler::InUse(void)
1005 {
1006 CLockObject receiveLock(m_receiveMutex);
1007 return m_iUseCounter > 0;
1008 }