8c1895894253e0d5e2210da3d9b1e78bf5797205
[deb_libcec.git] / src / lib / implementations / CECCommandHandler.cpp
1 /*
2 * This file is part of the libCEC(R) library.
3 *
4 * libCEC(R) is Copyright (C) 2011-2012 Pulse-Eight Limited. All rights reserved.
5 * libCEC(R) is an original work, containing original code.
6 *
7 * libCEC(R) is a trademark of Pulse-Eight Limited.
8 *
9 * This program is dual-licensed; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22 *
23 *
24 * Alternatively, you can license this library under a commercial license,
25 * please contact Pulse-Eight Licensing for more information.
26 *
27 * For more information contact:
28 * Pulse-Eight Licensing <license@pulse-eight.com>
29 * http://www.pulse-eight.com/
30 * http://www.pulse-eight.net/
31 */
32
33 #include "CECCommandHandler.h"
34 #include "../devices/CECBusDevice.h"
35 #include "../devices/CECAudioSystem.h"
36 #include "../devices/CECPlaybackDevice.h"
37 #include "../CECProcessor.h"
38 #include "../LibCEC.h"
39
40 using namespace CEC;
41 using namespace std;
42 using namespace PLATFORM;
43
44 CCECCommandHandler::CCECCommandHandler(CCECBusDevice *busDevice) :
45 m_busDevice(busDevice),
46 m_processor(m_busDevice->GetProcessor()),
47 m_iTransmitTimeout(CEC_DEFAULT_TRANSMIT_TIMEOUT),
48 m_iTransmitWait(CEC_DEFAULT_TRANSMIT_WAIT),
49 m_iTransmitRetries(CEC_DEFAULT_TRANSMIT_RETRIES),
50 m_bHandlerInited(false),
51 m_expectedResponse(CEC_OPCODE_NONE),
52 m_bOPTSendDeckStatusUpdateOnActiveSource(false),
53 m_vendorId(CEC_VENDOR_UNKNOWN),
54 m_bRcvSignal(false)
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 CLibCEC::AddCommand(command);
70
71 switch(command.opcode)
72 {
73 case CEC_OPCODE_REPORT_POWER_STATUS:
74 HandleReportPowerStatus(command);
75 break;
76 case CEC_OPCODE_CEC_VERSION:
77 HandleDeviceCecVersion(command);
78 break;
79 case CEC_OPCODE_SET_MENU_LANGUAGE:
80 HandleSetMenuLanguage(command);
81 break;
82 case CEC_OPCODE_GIVE_PHYSICAL_ADDRESS:
83 if (m_processor->IsInitialised())
84 HandleGivePhysicalAddress(command);
85 break;
86 case CEC_OPCODE_GIVE_OSD_NAME:
87 if (m_processor->IsInitialised())
88 HandleGiveOSDName(command);
89 break;
90 case CEC_OPCODE_GIVE_DEVICE_VENDOR_ID:
91 if (m_processor->IsInitialised())
92 HandleGiveDeviceVendorId(command);
93 break;
94 case CEC_OPCODE_DEVICE_VENDOR_ID:
95 HandleDeviceVendorId(command);
96 break;
97 case CEC_OPCODE_VENDOR_COMMAND_WITH_ID:
98 HandleDeviceVendorCommandWithId(command);
99 break;
100 case CEC_OPCODE_GIVE_DECK_STATUS:
101 if (m_processor->IsInitialised())
102 HandleGiveDeckStatus(command);
103 break;
104 case CEC_OPCODE_DECK_CONTROL:
105 HandleDeckControl(command);
106 break;
107 case CEC_OPCODE_MENU_REQUEST:
108 if (m_processor->IsInitialised())
109 HandleMenuRequest(command);
110 break;
111 case CEC_OPCODE_GIVE_DEVICE_POWER_STATUS:
112 if (m_processor->IsInitialised())
113 HandleGiveDevicePowerStatus(command);
114 break;
115 case CEC_OPCODE_GET_CEC_VERSION:
116 if (m_processor->IsInitialised())
117 HandleGetCecVersion(command);
118 break;
119 case CEC_OPCODE_USER_CONTROL_PRESSED:
120 if (m_processor->IsInitialised())
121 HandleUserControlPressed(command);
122 break;
123 case CEC_OPCODE_USER_CONTROL_RELEASE:
124 if (m_processor->IsInitialised())
125 HandleUserControlRelease(command);
126 break;
127 case CEC_OPCODE_GIVE_AUDIO_STATUS:
128 if (m_processor->IsInitialised())
129 HandleGiveAudioStatus(command);
130 break;
131 case CEC_OPCODE_GIVE_SYSTEM_AUDIO_MODE_STATUS:
132 if (m_processor->IsInitialised())
133 HandleGiveSystemAudioModeStatus(command);
134 break;
135 case CEC_OPCODE_SYSTEM_AUDIO_MODE_REQUEST:
136 if (m_processor->IsInitialised())
137 HandleSystemAudioModeRequest(command);
138 break;
139 case CEC_OPCODE_REPORT_AUDIO_STATUS:
140 HandleReportAudioStatus(command);
141 break;
142 case CEC_OPCODE_SYSTEM_AUDIO_MODE_STATUS:
143 HandleSystemAudioModeStatus(command);
144 break;
145 case CEC_OPCODE_SET_SYSTEM_AUDIO_MODE:
146 HandleSetSystemAudioMode(command);
147 break;
148 case CEC_OPCODE_REQUEST_ACTIVE_SOURCE:
149 if (m_processor->IsInitialised())
150 HandleRequestActiveSource(command);
151 break;
152 case CEC_OPCODE_SET_STREAM_PATH:
153 HandleSetStreamPath(command);
154 break;
155 case CEC_OPCODE_ROUTING_CHANGE:
156 HandleRoutingChange(command);
157 break;
158 case CEC_OPCODE_ROUTING_INFORMATION:
159 HandleRoutingInformation(command);
160 break;
161 case CEC_OPCODE_STANDBY:
162 if (m_processor->IsInitialised())
163 HandleStandby(command);
164 break;
165 case CEC_OPCODE_ACTIVE_SOURCE:
166 HandleActiveSource(command);
167 break;
168 case CEC_OPCODE_REPORT_PHYSICAL_ADDRESS:
169 HandleReportPhysicalAddress(command);
170 break;
171 case CEC_OPCODE_SET_OSD_NAME:
172 HandleSetOSDName(command);
173 break;
174 case CEC_OPCODE_IMAGE_VIEW_ON:
175 HandleImageViewOn(command);
176 break;
177 case CEC_OPCODE_TEXT_VIEW_ON:
178 HandleTextViewOn(command);
179 break;
180 case CEC_OPCODE_FEATURE_ABORT:
181 HandleFeatureAbort(command);
182 break;
183 case CEC_OPCODE_VENDOR_COMMAND:
184 HandleVendorCommand(command);
185 break;
186 default:
187 UnhandledCommand(command);
188 bHandled = false;
189 break;
190 }
191
192 if (bHandled)
193 {
194 CLockObject lock(m_receiveMutex);
195 if (m_expectedResponse == CEC_OPCODE_NONE ||
196 m_expectedResponse == command.opcode)
197 {
198 m_bRcvSignal = true;
199 m_condition.Signal();
200 }
201 }
202
203 return bHandled;
204 }
205
206 bool 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
217 bool 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
229 bool 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
241 bool 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
249 bool CCECCommandHandler::HandleDeviceVendorId(const cec_command &command)
250 {
251 return SetVendorId(command);
252 }
253
254 bool CCECCommandHandler::HandleFeatureAbort(const cec_command &command)
255 {
256 if (command.parameters.size == 2)
257 {
258 m_processor->m_busDevices[command.initiator]->SetUnsupportedFeature((cec_opcode)command.parameters[0]);
259 }
260 return true;
261 }
262
263 bool CCECCommandHandler::HandleGetCecVersion(const cec_command &command)
264 {
265 if (m_processor->IsRunning() && m_busDevice->MyLogicalAddressContains(command.destination))
266 {
267 CCECBusDevice *device = GetDevice(command.destination);
268 if (device)
269 return device->TransmitCECVersion(command.initiator);
270 }
271
272 return false;
273 }
274
275 bool CCECCommandHandler::HandleGiveAudioStatus(const cec_command &command)
276 {
277 if (m_processor->IsRunning() && m_busDevice->MyLogicalAddressContains(command.destination))
278 {
279 CCECBusDevice *device = GetDevice(command.destination);
280 if (device && device->GetType() == CEC_DEVICE_TYPE_AUDIO_SYSTEM)
281 return ((CCECAudioSystem *) device)->TransmitAudioStatus(command.initiator);
282 }
283
284 return false;
285 }
286
287 bool CCECCommandHandler::HandleGiveDeckStatus(const cec_command &command)
288 {
289 if (m_processor->IsRunning() && m_busDevice->MyLogicalAddressContains(command.destination))
290 {
291 CCECBusDevice *device = GetDevice(command.destination);
292 if (device && (device->GetType() == CEC_DEVICE_TYPE_PLAYBACK_DEVICE || device->GetType() == CEC_DEVICE_TYPE_RECORDING_DEVICE))
293 return ((CCECPlaybackDevice *) device)->TransmitDeckStatus(command.initiator);
294 }
295
296 return false;
297 }
298
299 bool CCECCommandHandler::HandleGiveDevicePowerStatus(const cec_command &command)
300 {
301 if (m_processor->IsRunning() && m_busDevice->MyLogicalAddressContains(command.destination))
302 {
303 CCECBusDevice *device = GetDevice(command.destination);
304 if (device)
305 return device->TransmitPowerState(command.initiator);
306 }
307
308 return false;
309 }
310
311 bool CCECCommandHandler::HandleGiveDeviceVendorId(const cec_command &command)
312 {
313 if (m_processor->IsRunning() && m_busDevice->MyLogicalAddressContains(command.destination))
314 {
315 CCECBusDevice *device = GetDevice(command.destination);
316 if (device)
317 return device->TransmitVendorID(command.initiator);
318 }
319
320 return false;
321 }
322
323 bool CCECCommandHandler::HandleGiveOSDName(const cec_command &command)
324 {
325 if (m_processor->IsRunning() && m_busDevice->MyLogicalAddressContains(command.destination))
326 {
327 CCECBusDevice *device = GetDevice(command.destination);
328 if (device)
329 return device->TransmitOSDName(command.initiator);
330 }
331
332 return false;
333 }
334
335 bool CCECCommandHandler::HandleGivePhysicalAddress(const cec_command &command)
336 {
337 if (m_processor->IsRunning() && m_busDevice->MyLogicalAddressContains(command.destination))
338 {
339 CCECBusDevice *device = GetDevice(command.destination);
340 if (device)
341 {
342 device->SetActiveSource();
343 return device->TransmitPhysicalAddress() &&
344 device->TransmitActiveSource();
345 }
346 }
347
348 return false;
349 }
350
351 bool CCECCommandHandler::HandleGiveSystemAudioModeStatus(const cec_command &command)
352 {
353 if (m_processor->IsRunning() && m_busDevice->MyLogicalAddressContains(command.destination))
354 {
355 CCECBusDevice *device = GetDevice(command.destination);
356 if (device && device->GetType() == CEC_DEVICE_TYPE_AUDIO_SYSTEM)
357 return ((CCECAudioSystem *) device)->TransmitSystemAudioModeStatus(command.initiator);
358 }
359
360 return false;
361 }
362
363 bool CCECCommandHandler::HandleImageViewOn(const cec_command &command)
364 {
365 m_processor->m_busDevices[command.initiator]->SetActiveSource();
366 return true;
367 }
368
369 bool CCECCommandHandler::HandleMenuRequest(const cec_command &command)
370 {
371 if (m_processor->IsRunning() && m_busDevice->MyLogicalAddressContains(command.destination))
372 {
373 if (command.parameters[0] == CEC_MENU_REQUEST_TYPE_QUERY)
374 {
375 CCECBusDevice *device = GetDevice(command.destination);
376 if (device)
377 return device->TransmitMenuState(command.initiator);
378 }
379 }
380
381 return false;
382 }
383
384 bool CCECCommandHandler::HandleReportAudioStatus(const cec_command &command)
385 {
386 if (command.parameters.size == 1)
387 {
388 CCECBusDevice *device = GetDevice(command.initiator);
389 if (device && device->GetType() == CEC_DEVICE_TYPE_AUDIO_SYSTEM)
390 {
391 ((CCECAudioSystem *)device)->SetAudioStatus(command.parameters[0]);
392 return true;
393 }
394 }
395 return false;
396 }
397
398 bool CCECCommandHandler::HandleReportPhysicalAddress(const cec_command &command)
399 {
400 if (command.parameters.size == 3)
401 {
402 uint16_t iNewAddress = ((uint16_t)command.parameters[0] << 8) | ((uint16_t)command.parameters[1]);
403 SetPhysicalAddress(command.initiator, iNewAddress);
404 }
405 return true;
406 }
407
408 bool CCECCommandHandler::HandleReportPowerStatus(const cec_command &command)
409 {
410 if (command.parameters.size == 1)
411 {
412 CCECBusDevice *device = GetDevice(command.initiator);
413 if (device)
414 device->SetPowerStatus((cec_power_status) command.parameters[0]);
415 }
416 return true;
417 }
418
419 bool CCECCommandHandler::HandleRequestActiveSource(const cec_command &command)
420 {
421 if (m_processor->IsRunning())
422 {
423 CLibCEC::AddLog(CEC_LOG_DEBUG, ">> %i requests active source", (uint8_t) command.initiator);
424
425 vector<CCECBusDevice *> devices;
426 for (size_t iDevicePtr = 0; iDevicePtr < GetMyDevices(devices); iDevicePtr++)
427 devices[iDevicePtr]->TransmitActiveSource();
428
429 return true;
430 }
431 return false;
432 }
433
434 bool CCECCommandHandler::HandleRoutingChange(const cec_command &command)
435 {
436 if (command.parameters.size == 4)
437 {
438 uint16_t iOldAddress = ((uint16_t)command.parameters[0] << 8) | ((uint16_t)command.parameters[1]);
439 uint16_t iNewAddress = ((uint16_t)command.parameters[2] << 8) | ((uint16_t)command.parameters[3]);
440
441 CCECBusDevice *device = GetDevice(command.initiator);
442 if (device)
443 device->SetStreamPath(iNewAddress, iOldAddress);
444 }
445 return true;
446 }
447
448 bool CCECCommandHandler::HandleRoutingInformation(const cec_command &command)
449 {
450 if (command.parameters.size == 2)
451 {
452 uint16_t iNewAddress = ((uint16_t)command.parameters[0] << 8) | ((uint16_t)command.parameters[1]);
453 m_processor->SetActiveSource(iNewAddress);
454 }
455
456 return false;
457 }
458
459 bool CCECCommandHandler::HandleSetMenuLanguage(const cec_command &command)
460 {
461 if (command.parameters.size == 3)
462 {
463 CCECBusDevice *device = GetDevice(command.initiator);
464 if (device)
465 {
466 cec_menu_language language;
467 language.device = command.initiator;
468 for (uint8_t iPtr = 0; iPtr < 4; iPtr++)
469 language.language[iPtr] = command.parameters[iPtr];
470 language.language[3] = 0;
471 device->SetMenuLanguage(language);
472 return true;
473 }
474 }
475 return false;
476 }
477
478 bool CCECCommandHandler::HandleSetOSDName(const cec_command &command)
479 {
480 if (command.parameters.size > 0)
481 {
482 CCECBusDevice *device = GetDevice(command.initiator);
483 if (device)
484 {
485 char buf[1024];
486 for (uint8_t iPtr = 0; iPtr < command.parameters.size; iPtr++)
487 buf[iPtr] = (char)command.parameters[iPtr];
488 buf[command.parameters.size] = 0;
489
490 CStdString strName(buf);
491 device->SetOSDName(strName);
492
493 return true;
494 }
495 }
496 return false;
497 }
498
499 bool CCECCommandHandler::HandleSetStreamPath(const cec_command &command)
500 {
501 if (m_processor->IsRunning() && command.parameters.size >= 2)
502 {
503 uint16_t iStreamAddress = ((uint16_t)command.parameters[0] << 8) | ((uint16_t)command.parameters[1]);
504 CLibCEC::AddLog(CEC_LOG_DEBUG, ">> %i sets stream path to physical address %04x", command.initiator, iStreamAddress);
505
506 /* one of the device handled by libCEC has been made active */
507 CCECBusDevice *device = GetDeviceByPhysicalAddress(iStreamAddress);
508 if (device && m_busDevice->MyLogicalAddressContains(device->GetLogicalAddress()))
509 {
510 device->SetActiveSource();
511 device->TransmitActiveSource();
512
513 device->SetMenuState(CEC_MENU_STATE_ACTIVATED);
514 device->TransmitMenuState(command.initiator);
515 }
516 }
517 return false;
518 }
519
520 bool 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
547 bool 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
556 bool 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
571 bool 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
586 bool CCECCommandHandler::HandleTextViewOn(const cec_command &command)
587 {
588 m_processor->m_busDevices[command.initiator]->SetActiveSource();
589 return true;
590 }
591
592 bool 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->TransmitActiveSource();
611
612 if (device->GetType() == CEC_DEVICE_TYPE_PLAYBACK_DEVICE ||
613 device->GetType() == CEC_DEVICE_TYPE_RECORDING_DEVICE)
614 ((CCECPlaybackDevice *)device)->TransmitDeckStatus(command.initiator);
615 }
616 }
617 }
618 else
619 {
620 CLibCEC::SetCurrentButton((cec_user_control_code) command.parameters[0]);
621 }
622 return true;
623 }
624 }
625 return false;
626 }
627
628 bool CCECCommandHandler::HandleUserControlRelease(const cec_command &command)
629 {
630 if (m_processor->IsRunning() && m_busDevice->MyLogicalAddressContains(command.destination))
631 CLibCEC::AddKey();
632
633 return true;
634 }
635
636 bool CCECCommandHandler::HandleVendorCommand(const cec_command & UNUSED(command))
637 {
638 return true;
639 }
640
641 void CCECCommandHandler::UnhandledCommand(const cec_command &command)
642 {
643 CLibCEC::AddLog(CEC_LOG_DEBUG, "unhandled command with opcode %02x from address %d", command.opcode, command.initiator);
644 }
645
646 size_t CCECCommandHandler::GetMyDevices(vector<CCECBusDevice *> &devices) const
647 {
648 size_t iReturn(0);
649
650 cec_logical_addresses addresses = m_processor->GetLogicalAddresses();
651 for (uint8_t iPtr = 0; iPtr < 16; iPtr++)
652 {
653 if (addresses[iPtr])
654 {
655 devices.push_back(GetDevice((cec_logical_address) iPtr));
656 ++iReturn;
657 }
658 }
659
660 return iReturn;
661 }
662
663 CCECBusDevice *CCECCommandHandler::GetDevice(cec_logical_address iLogicalAddress) const
664 {
665 CCECBusDevice *device = NULL;
666
667 if (iLogicalAddress >= CECDEVICE_TV && iLogicalAddress <= CECDEVICE_BROADCAST)
668 device = m_processor->m_busDevices[iLogicalAddress];
669
670 return device;
671 }
672
673 CCECBusDevice *CCECCommandHandler::GetDeviceByPhysicalAddress(uint16_t iPhysicalAddress) const
674 {
675 return m_processor->GetDeviceByPhysicalAddress(iPhysicalAddress);
676 }
677
678 CCECBusDevice *CCECCommandHandler::GetDeviceByType(cec_device_type type) const
679 {
680 return m_processor->GetDeviceByType(type);
681 }
682
683 bool CCECCommandHandler::SetVendorId(const cec_command &command)
684 {
685 bool bChanged(false);
686 if (command.parameters.size < 3)
687 {
688 CLibCEC::AddLog(CEC_LOG_WARNING, "invalid vendor ID received");
689 return bChanged;
690 }
691
692 uint64_t iVendorId = ((uint64_t)command.parameters[0] << 16) +
693 ((uint64_t)command.parameters[1] << 8) +
694 (uint64_t)command.parameters[2];
695
696 CCECBusDevice *device = GetDevice((cec_logical_address) command.initiator);
697 if (device)
698 bChanged = device->SetVendorId(iVendorId);
699 return bChanged;
700 }
701
702 void CCECCommandHandler::SetPhysicalAddress(cec_logical_address iAddress, uint16_t iNewAddress)
703 {
704 if (!m_busDevice->MyLogicalAddressContains(iAddress))
705 {
706 bool bOurAddress(m_processor->GetPhysicalAddress() == iNewAddress);
707 GetDevice(iAddress)->SetPhysicalAddress(iNewAddress);
708 if (bOurAddress)
709 {
710 /* another device reported the same physical address as ours
711 * since we don't have physical address detection yet, we'll just use the
712 * given address, increased by 0x100 for now */
713 m_processor->SetPhysicalAddress(iNewAddress + 0x100);
714 }
715 }
716 }
717
718 bool CCECCommandHandler::TransmitImageViewOn(const cec_logical_address iInitiator, const cec_logical_address iDestination)
719 {
720 cec_command command;
721 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_IMAGE_VIEW_ON);
722
723 return Transmit(command, false);
724 }
725
726 bool CCECCommandHandler::TransmitStandby(const cec_logical_address iInitiator, const cec_logical_address iDestination)
727 {
728 cec_command command;
729 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_STANDBY);
730
731 return Transmit(command, false);
732 }
733
734 bool CCECCommandHandler::TransmitRequestCecVersion(const cec_logical_address iInitiator, const cec_logical_address iDestination)
735 {
736 cec_command command;
737 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_GET_CEC_VERSION);
738
739 return Transmit(command, true, CEC_OPCODE_CEC_VERSION);
740 }
741
742 bool CCECCommandHandler::TransmitRequestMenuLanguage(const cec_logical_address iInitiator, const cec_logical_address iDestination)
743 {
744 cec_command command;
745 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_GET_MENU_LANGUAGE);
746
747 return Transmit(command, true, CEC_OPCODE_SET_MENU_LANGUAGE);
748 }
749
750 bool CCECCommandHandler::TransmitRequestOSDName(const cec_logical_address iInitiator, const cec_logical_address iDestination)
751 {
752 cec_command command;
753 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_GIVE_OSD_NAME);
754
755 return Transmit(command, true, CEC_OPCODE_SET_OSD_NAME);
756 }
757
758 bool CCECCommandHandler::TransmitRequestPhysicalAddress(const cec_logical_address iInitiator, const cec_logical_address iDestination)
759 {
760 cec_command command;
761 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_GIVE_PHYSICAL_ADDRESS);
762
763 return Transmit(command, true, CEC_OPCODE_REPORT_PHYSICAL_ADDRESS);
764 }
765
766 bool CCECCommandHandler::TransmitRequestPowerStatus(const cec_logical_address iInitiator, const cec_logical_address iDestination)
767 {
768 cec_command command;
769 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_GIVE_DEVICE_POWER_STATUS);
770
771 return Transmit(command, true, CEC_OPCODE_REPORT_POWER_STATUS);
772 }
773
774 bool CCECCommandHandler::TransmitRequestVendorId(const cec_logical_address iInitiator, const cec_logical_address iDestination)
775 {
776 cec_command command;
777 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_GIVE_DEVICE_VENDOR_ID);
778
779 return Transmit(command, true, CEC_OPCODE_DEVICE_VENDOR_ID);
780 }
781
782 bool CCECCommandHandler::TransmitActiveSource(const cec_logical_address iInitiator, uint16_t iPhysicalAddress)
783 {
784 cec_command command;
785 cec_command::Format(command, iInitiator, CECDEVICE_BROADCAST, CEC_OPCODE_ACTIVE_SOURCE);
786 command.parameters.PushBack((uint8_t) ((iPhysicalAddress >> 8) & 0xFF));
787 command.parameters.PushBack((uint8_t) (iPhysicalAddress & 0xFF));
788
789 return Transmit(command, false);
790 }
791
792 bool CCECCommandHandler::TransmitCECVersion(const cec_logical_address iInitiator, const cec_logical_address iDestination, cec_version cecVersion)
793 {
794 cec_command command;
795 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_CEC_VERSION);
796 command.parameters.PushBack((uint8_t)cecVersion);
797
798 return Transmit(command, false);
799 }
800
801 bool CCECCommandHandler::TransmitInactiveSource(const cec_logical_address iInitiator, uint16_t iPhysicalAddress)
802 {
803 cec_command command;
804 cec_command::Format(command, iInitiator, CECDEVICE_TV, CEC_OPCODE_INACTIVE_SOURCE);
805 command.parameters.PushBack((iPhysicalAddress >> 8) & 0xFF);
806 command.parameters.PushBack(iPhysicalAddress & 0xFF);
807
808 return Transmit(command, false);
809 }
810
811 bool CCECCommandHandler::TransmitMenuState(const cec_logical_address iInitiator, const cec_logical_address iDestination, cec_menu_state menuState)
812 {
813 cec_command command;
814 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_MENU_STATUS);
815 command.parameters.PushBack((uint8_t)menuState);
816
817 return Transmit(command, false);
818 }
819
820 bool CCECCommandHandler::TransmitOSDName(const cec_logical_address iInitiator, const cec_logical_address iDestination, CStdString strDeviceName)
821 {
822 cec_command command;
823 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_SET_OSD_NAME);
824 for (size_t iPtr = 0; iPtr < strDeviceName.length(); iPtr++)
825 command.parameters.PushBack(strDeviceName.at(iPtr));
826
827 return Transmit(command, false);
828 }
829
830 bool CCECCommandHandler::TransmitOSDString(const cec_logical_address iInitiator, const cec_logical_address iDestination, cec_display_control duration, const char *strMessage)
831 {
832 cec_command command;
833 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_SET_OSD_STRING);
834 command.parameters.PushBack((uint8_t)duration);
835
836 size_t iLen = strlen(strMessage);
837 if (iLen > 13) iLen = 13;
838
839 for (size_t iPtr = 0; iPtr < iLen; iPtr++)
840 command.parameters.PushBack(strMessage[iPtr]);
841
842 return Transmit(command, false);
843 }
844
845 bool CCECCommandHandler::TransmitPhysicalAddress(const cec_logical_address iInitiator, uint16_t iPhysicalAddress, cec_device_type type)
846 {
847 cec_command command;
848 cec_command::Format(command, iInitiator, CECDEVICE_BROADCAST, CEC_OPCODE_REPORT_PHYSICAL_ADDRESS);
849 command.parameters.PushBack((uint8_t) ((iPhysicalAddress >> 8) & 0xFF));
850 command.parameters.PushBack((uint8_t) (iPhysicalAddress & 0xFF));
851 command.parameters.PushBack((uint8_t) (type));
852
853 return Transmit(command, false);
854 }
855
856 bool CCECCommandHandler::TransmitPoll(const cec_logical_address iInitiator, const cec_logical_address iDestination)
857 {
858 cec_command command;
859 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_NONE);
860
861 return Transmit(command, false);
862 }
863
864 bool CCECCommandHandler::TransmitPowerState(const cec_logical_address iInitiator, const cec_logical_address iDestination, cec_power_status state)
865 {
866 cec_command command;
867 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_REPORT_POWER_STATUS);
868 command.parameters.PushBack((uint8_t) state);
869
870 return Transmit(command, false);
871 }
872
873 bool CCECCommandHandler::TransmitVendorID(const cec_logical_address iInitiator, uint64_t iVendorId)
874 {
875 cec_command command;
876 cec_command::Format(command, iInitiator, CECDEVICE_BROADCAST, CEC_OPCODE_DEVICE_VENDOR_ID);
877
878 command.parameters.PushBack((uint8_t) (((uint64_t)iVendorId >> 16) & 0xFF));
879 command.parameters.PushBack((uint8_t) (((uint64_t)iVendorId >> 8) & 0xFF));
880 command.parameters.PushBack((uint8_t) ((uint64_t)iVendorId & 0xFF));
881
882 return Transmit(command, false);
883 }
884
885 bool CCECCommandHandler::TransmitAudioStatus(const cec_logical_address iInitiator, const cec_logical_address iDestination, uint8_t state)
886 {
887 cec_command command;
888 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_REPORT_AUDIO_STATUS);
889 command.parameters.PushBack(state);
890
891 return Transmit(command, false);
892 }
893
894 bool CCECCommandHandler::TransmitSetSystemAudioMode(const cec_logical_address iInitiator, const cec_logical_address iDestination, cec_system_audio_status state)
895 {
896 cec_command command;
897 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_SET_SYSTEM_AUDIO_MODE);
898 command.parameters.PushBack((uint8_t)state);
899
900 return Transmit(command, false);
901 }
902
903 bool CCECCommandHandler::TransmitSetStreamPath(uint16_t iStreamPath)
904 {
905 cec_command command;
906 cec_command::Format(command, m_busDevice->GetLogicalAddress(), CECDEVICE_BROADCAST, CEC_OPCODE_SET_STREAM_PATH);
907 command.parameters.PushBack((uint8_t) ((iStreamPath >> 8) & 0xFF));
908 command.parameters.PushBack((uint8_t) (iStreamPath & 0xFF));
909
910 return Transmit(command, false);
911 }
912
913 bool CCECCommandHandler::TransmitSystemAudioModeStatus(const cec_logical_address iInitiator, const cec_logical_address iDestination, cec_system_audio_status state)
914 {
915 cec_command command;
916 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_SYSTEM_AUDIO_MODE_STATUS);
917 command.parameters.PushBack((uint8_t)state);
918
919 return Transmit(command, false);
920 }
921
922 bool CCECCommandHandler::TransmitDeckStatus(const cec_logical_address iInitiator, const cec_logical_address iDestination, cec_deck_info state)
923 {
924 cec_command command;
925 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_DECK_STATUS);
926 command.PushBack((uint8_t)state);
927
928 return Transmit(command, false);
929 }
930
931 bool CCECCommandHandler::TransmitKeypress(const cec_logical_address iInitiator, const cec_logical_address iDestination, cec_user_control_code key, bool bWait /* = true */)
932 {
933 cec_command command;
934 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_USER_CONTROL_PRESSED);
935 command.parameters.PushBack((uint8_t)key);
936
937 return Transmit(command, bWait);
938 }
939
940 bool CCECCommandHandler::TransmitKeyRelease(const cec_logical_address iInitiator, const cec_logical_address iDestination, bool bWait /* = true */)
941 {
942 cec_command command;
943 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_USER_CONTROL_RELEASE);
944
945 return Transmit(command, bWait);
946 }
947
948 bool CCECCommandHandler::Transmit(cec_command &command, bool bExpectResponse /* = true */, cec_opcode expectedResponse /* = CEC_OPCODE_NONE */)
949 {
950 bool bReturn(false);
951 command.transmit_timeout = m_iTransmitTimeout;
952
953 {
954 uint8_t iTries(0), iMaxTries(command.opcode == CEC_OPCODE_NONE ? 1 : m_iTransmitRetries + 1);
955 CLockObject writeLock(m_processor->m_transmitMutex);
956 CLockObject receiveLock(m_receiveMutex);
957 while (!bReturn && ++iTries <= iMaxTries)
958 {
959 m_expectedResponse = expectedResponse;
960 if ((bReturn = m_processor->Transmit(command)) == true)
961 {
962 CLibCEC::AddLog(CEC_LOG_DEBUG, "command transmitted");
963 if (bExpectResponse)
964 bReturn = m_condition.Wait(m_receiveMutex, m_bRcvSignal, m_iTransmitWait);
965 m_bRcvSignal = false;
966 CLibCEC::AddLog(CEC_LOG_DEBUG, bReturn ? "expected response received" : "expected response not received");
967 }
968 }
969 }
970
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 }