0178ae069fedcef2123231dc5fadd80f408f4521
[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::AddLog(CEC_LOG_NOTICE, ">> %s (%X) -> %s (%X): %s (%2X)", m_processor->ToString(command.initiator), command.initiator, m_processor->ToString(command.destination), command.destination, m_processor->ToString(command.opcode), command.opcode);
71
72 m_processor->AddCommand(command);
73
74 switch(command.opcode)
75 {
76 case CEC_OPCODE_REPORT_POWER_STATUS:
77 HandleReportPowerStatus(command);
78 break;
79 case CEC_OPCODE_CEC_VERSION:
80 HandleDeviceCecVersion(command);
81 break;
82 case CEC_OPCODE_SET_MENU_LANGUAGE:
83 HandleSetMenuLanguage(command);
84 break;
85 case CEC_OPCODE_GIVE_PHYSICAL_ADDRESS:
86 if (m_processor->IsInitialised())
87 HandleGivePhysicalAddress(command);
88 break;
89 case CEC_OPCODE_GIVE_OSD_NAME:
90 if (m_processor->IsInitialised())
91 HandleGiveOSDName(command);
92 break;
93 case CEC_OPCODE_GIVE_DEVICE_VENDOR_ID:
94 if (m_processor->IsInitialised())
95 HandleGiveDeviceVendorId(command);
96 break;
97 case CEC_OPCODE_DEVICE_VENDOR_ID:
98 HandleDeviceVendorId(command);
99 break;
100 case CEC_OPCODE_VENDOR_COMMAND_WITH_ID:
101 HandleDeviceVendorCommandWithId(command);
102 break;
103 case CEC_OPCODE_GIVE_DECK_STATUS:
104 if (m_processor->IsInitialised())
105 HandleGiveDeckStatus(command);
106 break;
107 case CEC_OPCODE_DECK_CONTROL:
108 HandleDeckControl(command);
109 break;
110 case CEC_OPCODE_MENU_REQUEST:
111 if (m_processor->IsInitialised())
112 HandleMenuRequest(command);
113 break;
114 case CEC_OPCODE_GIVE_DEVICE_POWER_STATUS:
115 if (m_processor->IsInitialised())
116 HandleGiveDevicePowerStatus(command);
117 break;
118 case CEC_OPCODE_GET_CEC_VERSION:
119 if (m_processor->IsInitialised())
120 HandleGetCecVersion(command);
121 break;
122 case CEC_OPCODE_USER_CONTROL_PRESSED:
123 if (m_processor->IsInitialised())
124 HandleUserControlPressed(command);
125 break;
126 case CEC_OPCODE_USER_CONTROL_RELEASE:
127 if (m_processor->IsInitialised())
128 HandleUserControlRelease(command);
129 break;
130 case CEC_OPCODE_GIVE_AUDIO_STATUS:
131 if (m_processor->IsInitialised())
132 HandleGiveAudioStatus(command);
133 break;
134 case CEC_OPCODE_GIVE_SYSTEM_AUDIO_MODE_STATUS:
135 if (m_processor->IsInitialised())
136 HandleGiveSystemAudioModeStatus(command);
137 break;
138 case CEC_OPCODE_SYSTEM_AUDIO_MODE_REQUEST:
139 if (m_processor->IsInitialised())
140 HandleSystemAudioModeRequest(command);
141 break;
142 case CEC_OPCODE_REPORT_AUDIO_STATUS:
143 HandleReportAudioStatus(command);
144 break;
145 case CEC_OPCODE_SYSTEM_AUDIO_MODE_STATUS:
146 HandleSystemAudioModeStatus(command);
147 break;
148 case CEC_OPCODE_SET_SYSTEM_AUDIO_MODE:
149 HandleSetSystemAudioMode(command);
150 break;
151 case CEC_OPCODE_REQUEST_ACTIVE_SOURCE:
152 if (m_processor->IsInitialised())
153 HandleRequestActiveSource(command);
154 break;
155 case CEC_OPCODE_SET_STREAM_PATH:
156 HandleSetStreamPath(command);
157 break;
158 case CEC_OPCODE_ROUTING_CHANGE:
159 HandleRoutingChange(command);
160 break;
161 case CEC_OPCODE_ROUTING_INFORMATION:
162 HandleRoutingInformation(command);
163 break;
164 case CEC_OPCODE_STANDBY:
165 if (m_processor->IsInitialised())
166 HandleStandby(command);
167 break;
168 case CEC_OPCODE_ACTIVE_SOURCE:
169 HandleActiveSource(command);
170 break;
171 case CEC_OPCODE_REPORT_PHYSICAL_ADDRESS:
172 HandleReportPhysicalAddress(command);
173 break;
174 case CEC_OPCODE_SET_OSD_NAME:
175 HandleSetOSDName(command);
176 break;
177 case CEC_OPCODE_IMAGE_VIEW_ON:
178 HandleImageViewOn(command);
179 break;
180 case CEC_OPCODE_TEXT_VIEW_ON:
181 HandleTextViewOn(command);
182 break;
183 case CEC_OPCODE_FEATURE_ABORT:
184 HandleFeatureAbort(command);
185 break;
186 case CEC_OPCODE_VENDOR_COMMAND:
187 HandleVendorCommand(command);
188 break;
189 default:
190 UnhandledCommand(command);
191 bHandled = false;
192 break;
193 }
194
195 if (bHandled)
196 {
197 CLockObject lock(m_receiveMutex);
198 if (m_expectedResponse == CEC_OPCODE_NONE ||
199 m_expectedResponse == command.opcode)
200 m_condition.Signal();
201 }
202
203 MarkReady();
204 return bHandled;
205 }
206
207 bool CCECCommandHandler::HandleActiveSource(const cec_command &command)
208 {
209 if (command.parameters.size == 2)
210 {
211 uint16_t iAddress = ((uint16_t)command.parameters[0] << 8) | ((uint16_t)command.parameters[1]);
212 return m_processor->SetActiveSource(iAddress);
213 }
214
215 return true;
216 }
217
218 bool CCECCommandHandler::HandleDeckControl(const cec_command &command)
219 {
220 CCECBusDevice *device = GetDevice(command.destination);
221 if (device && (device->GetType() == CEC_DEVICE_TYPE_PLAYBACK_DEVICE || device->GetType() == CEC_DEVICE_TYPE_RECORDING_DEVICE) && command.parameters.size > 0)
222 {
223 ((CCECPlaybackDevice *) device)->SetDeckControlMode((cec_deck_control_mode) command.parameters[0]);
224 return true;
225 }
226
227 return false;
228 }
229
230 bool CCECCommandHandler::HandleDeviceCecVersion(const cec_command &command)
231 {
232 if (command.parameters.size == 1)
233 {
234 CCECBusDevice *device = GetDevice(command.initiator);
235 if (device)
236 device->SetCecVersion((cec_version) command.parameters[0]);
237 }
238
239 return true;
240 }
241
242 bool CCECCommandHandler::HandleDeviceVendorCommandWithId(const cec_command &command)
243 {
244 if (m_processor->IsStarted() && m_busDevice->MyLogicalAddressContains(command.destination))
245 m_processor->TransmitAbort(command.initiator, command.opcode, CEC_ABORT_REASON_REFUSED);
246
247 return true;
248 }
249
250 bool CCECCommandHandler::HandleDeviceVendorId(const cec_command &command)
251 {
252 return SetVendorId(command);
253 }
254
255 bool CCECCommandHandler::HandleFeatureAbort(const cec_command &command)
256 {
257 if (command.parameters.size == 2)
258 {
259 m_processor->m_busDevices[command.initiator]->SetUnsupportedFeature((cec_opcode)command.parameters[0]);
260 }
261 return true;
262 }
263
264 bool CCECCommandHandler::HandleGetCecVersion(const cec_command &command)
265 {
266 if (m_processor->IsStarted() && m_busDevice->MyLogicalAddressContains(command.destination))
267 {
268 CCECBusDevice *device = GetDevice(command.destination);
269 if (device)
270 return device->TransmitCECVersion(command.initiator);
271 }
272
273 return false;
274 }
275
276 bool CCECCommandHandler::HandleGiveAudioStatus(const cec_command &command)
277 {
278 if (m_processor->IsStarted() && m_busDevice->MyLogicalAddressContains(command.destination))
279 {
280 CCECBusDevice *device = GetDevice(command.destination);
281 if (device && device->GetType() == CEC_DEVICE_TYPE_AUDIO_SYSTEM)
282 return ((CCECAudioSystem *) device)->TransmitAudioStatus(command.initiator);
283 }
284
285 return false;
286 }
287
288 bool CCECCommandHandler::HandleGiveDeckStatus(const cec_command &command)
289 {
290 if (m_processor->IsStarted() && m_busDevice->MyLogicalAddressContains(command.destination))
291 {
292 CCECBusDevice *device = GetDevice(command.destination);
293 if (device && (device->GetType() == CEC_DEVICE_TYPE_PLAYBACK_DEVICE || device->GetType() == CEC_DEVICE_TYPE_RECORDING_DEVICE))
294 return ((CCECPlaybackDevice *) device)->TransmitDeckStatus(command.initiator);
295 }
296
297 return false;
298 }
299
300 bool CCECCommandHandler::HandleGiveDevicePowerStatus(const cec_command &command)
301 {
302 if (m_processor->IsStarted() && m_busDevice->MyLogicalAddressContains(command.destination))
303 {
304 CCECBusDevice *device = GetDevice(command.destination);
305 if (device)
306 return device->TransmitPowerState(command.initiator);
307 }
308
309 return false;
310 }
311
312 bool CCECCommandHandler::HandleGiveDeviceVendorId(const cec_command &command)
313 {
314 if (m_processor->IsStarted() && m_busDevice->MyLogicalAddressContains(command.destination))
315 {
316 CCECBusDevice *device = GetDevice(command.destination);
317 if (device)
318 return device->TransmitVendorID(command.initiator);
319 }
320
321 return false;
322 }
323
324 bool CCECCommandHandler::HandleGiveOSDName(const cec_command &command)
325 {
326 if (m_processor->IsStarted() && m_busDevice->MyLogicalAddressContains(command.destination))
327 {
328 CCECBusDevice *device = GetDevice(command.destination);
329 if (device)
330 return device->TransmitOSDName(command.initiator);
331 }
332
333 return false;
334 }
335
336 bool CCECCommandHandler::HandleGivePhysicalAddress(const cec_command &command)
337 {
338 if (m_processor->IsStarted() && m_busDevice->MyLogicalAddressContains(command.destination))
339 {
340 CCECBusDevice *device = GetDevice(command.destination);
341 if (device)
342 {
343 device->SetActiveSource();
344 return device->TransmitPhysicalAddress() &&
345 device->TransmitActiveSource();
346 }
347 }
348
349 return false;
350 }
351
352 bool CCECCommandHandler::HandleGiveSystemAudioModeStatus(const cec_command &command)
353 {
354 if (m_processor->IsStarted() && m_busDevice->MyLogicalAddressContains(command.destination))
355 {
356 CCECBusDevice *device = GetDevice(command.destination);
357 if (device && device->GetType() == CEC_DEVICE_TYPE_AUDIO_SYSTEM)
358 return ((CCECAudioSystem *) device)->TransmitSystemAudioModeStatus(command.initiator);
359 }
360
361 return false;
362 }
363
364 bool CCECCommandHandler::HandleImageViewOn(const cec_command &command)
365 {
366 m_processor->m_busDevices[command.initiator]->SetActiveSource();
367 return true;
368 }
369
370 bool CCECCommandHandler::HandleMenuRequest(const cec_command &command)
371 {
372 if (m_processor->IsStarted() && m_busDevice->MyLogicalAddressContains(command.destination))
373 {
374 if (command.parameters[0] == CEC_MENU_REQUEST_TYPE_QUERY)
375 {
376 CCECBusDevice *device = GetDevice(command.destination);
377 if (device)
378 return device->TransmitMenuState(command.initiator);
379 }
380 }
381
382 return false;
383 }
384
385 bool CCECCommandHandler::HandleReportAudioStatus(const cec_command &command)
386 {
387 if (command.parameters.size == 1)
388 {
389 CCECBusDevice *device = GetDevice(command.initiator);
390 if (device && device->GetType() == CEC_DEVICE_TYPE_AUDIO_SYSTEM)
391 {
392 ((CCECAudioSystem *)device)->SetAudioStatus(command.parameters[0]);
393 return true;
394 }
395 }
396 return false;
397 }
398
399 bool CCECCommandHandler::HandleReportPhysicalAddress(const cec_command &command)
400 {
401 if (command.parameters.size == 3)
402 {
403 uint16_t iNewAddress = ((uint16_t)command.parameters[0] << 8) | ((uint16_t)command.parameters[1]);
404 SetPhysicalAddress(command.initiator, iNewAddress);
405 }
406 return true;
407 }
408
409 bool CCECCommandHandler::HandleReportPowerStatus(const cec_command &command)
410 {
411 if (command.parameters.size == 1)
412 {
413 CCECBusDevice *device = GetDevice(command.initiator);
414 if (device)
415 device->SetPowerStatus((cec_power_status) command.parameters[0]);
416 }
417 return true;
418 }
419
420 bool CCECCommandHandler::HandleRequestActiveSource(const cec_command &command)
421 {
422 if (m_processor->IsStarted())
423 {
424 CLibCEC::AddLog(CEC_LOG_DEBUG, ">> %i requests active source", (uint8_t) command.initiator);
425
426 vector<CCECBusDevice *> devices;
427 for (size_t iDevicePtr = 0; iDevicePtr < GetMyDevices(devices); iDevicePtr++)
428 devices[iDevicePtr]->TransmitActiveSource();
429
430 return true;
431 }
432 return false;
433 }
434
435 bool CCECCommandHandler::HandleRoutingChange(const cec_command &command)
436 {
437 if (command.parameters.size == 4)
438 {
439 uint16_t iOldAddress = ((uint16_t)command.parameters[0] << 8) | ((uint16_t)command.parameters[1]);
440 uint16_t iNewAddress = ((uint16_t)command.parameters[2] << 8) | ((uint16_t)command.parameters[3]);
441
442 CCECBusDevice *device = GetDevice(command.initiator);
443 if (device)
444 device->SetStreamPath(iNewAddress, iOldAddress);
445 }
446 return true;
447 }
448
449 bool CCECCommandHandler::HandleRoutingInformation(const cec_command &command)
450 {
451 if (command.parameters.size == 2)
452 {
453 uint16_t iNewAddress = ((uint16_t)command.parameters[0] << 8) | ((uint16_t)command.parameters[1]);
454 m_processor->SetActiveSource(iNewAddress);
455 }
456
457 return false;
458 }
459
460 bool CCECCommandHandler::HandleSetMenuLanguage(const cec_command &command)
461 {
462 if (command.parameters.size == 3)
463 {
464 CCECBusDevice *device = GetDevice(command.initiator);
465 if (device)
466 {
467 cec_menu_language language;
468 language.device = command.initiator;
469 for (uint8_t iPtr = 0; iPtr < 4; iPtr++)
470 language.language[iPtr] = command.parameters[iPtr];
471 language.language[3] = 0;
472 device->SetMenuLanguage(language);
473 return true;
474 }
475 }
476 return false;
477 }
478
479 bool CCECCommandHandler::HandleSetOSDName(const cec_command &command)
480 {
481 if (command.parameters.size > 0)
482 {
483 CCECBusDevice *device = GetDevice(command.initiator);
484 if (device)
485 {
486 char buf[1024];
487 for (uint8_t iPtr = 0; iPtr < command.parameters.size; iPtr++)
488 buf[iPtr] = (char)command.parameters[iPtr];
489 buf[command.parameters.size] = 0;
490
491 CStdString strName(buf);
492 device->SetOSDName(strName);
493
494 return true;
495 }
496 }
497 return false;
498 }
499
500 bool CCECCommandHandler::HandleSetStreamPath(const cec_command &command)
501 {
502 if (m_processor->IsStarted() && command.parameters.size >= 2)
503 {
504 uint16_t iStreamAddress = ((uint16_t)command.parameters[0] << 8) | ((uint16_t)command.parameters[1]);
505 CLibCEC::AddLog(CEC_LOG_DEBUG, ">> %i sets stream path to physical address %04x", command.initiator, iStreamAddress);
506
507 /* one of the device handled by libCEC has been made active */
508 CCECBusDevice *device = GetDeviceByPhysicalAddress(iStreamAddress);
509 if (device && m_busDevice->MyLogicalAddressContains(device->GetLogicalAddress()))
510 {
511 device->SetActiveSource();
512 device->TransmitActiveSource();
513 }
514 }
515 return false;
516 }
517
518 bool CCECCommandHandler::HandleSystemAudioModeRequest(const cec_command &command)
519 {
520 if (m_processor->IsStarted() && 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
545 bool 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
554 bool 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
569 bool 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
584 bool CCECCommandHandler::HandleTextViewOn(const cec_command &command)
585 {
586 m_processor->m_busDevices[command.initiator]->SetActiveSource();
587 return true;
588 }
589
590 bool CCECCommandHandler::HandleUserControlPressed(const cec_command &command)
591 {
592 if (m_processor->IsStarted() && m_busDevice->MyLogicalAddressContains(command.destination) && command.parameters.size > 0)
593 {
594 m_processor->AddKey();
595
596 if (command.parameters[0] <= CEC_USER_CONTROL_CODE_MAX)
597 {
598 CLibCEC::AddLog(CEC_LOG_DEBUG, "key pressed: %x", command.parameters[0]);
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 m_processor->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->IsStarted() && m_busDevice->MyLogicalAddressContains(command.destination))
631 m_processor->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 void CCECCommandHandler::HandlePoll(const cec_logical_address iInitiator, const cec_logical_address iDestination)
719 {
720 CLibCEC::AddLog(CEC_LOG_DEBUG, "<< POLL: %s (%x) -> %s (%x)", m_processor->ToString(iInitiator), iInitiator, m_processor->ToString(iDestination), iDestination);
721 }
722
723 bool CCECCommandHandler::HandleReceiveFailed(void)
724 {
725 /* default = error */
726 return true;
727 }
728
729 bool CCECCommandHandler::TransmitImageViewOn(const cec_logical_address iInitiator, const cec_logical_address iDestination)
730 {
731 cec_command command;
732 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_IMAGE_VIEW_ON);
733
734 return Transmit(command, false);
735 }
736
737 bool CCECCommandHandler::TransmitStandby(const cec_logical_address iInitiator, const cec_logical_address iDestination)
738 {
739 cec_command command;
740 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_STANDBY);
741
742 return Transmit(command, false);
743 }
744
745 bool CCECCommandHandler::TransmitRequestCecVersion(const cec_logical_address iInitiator, const cec_logical_address iDestination)
746 {
747 cec_command command;
748 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_GET_CEC_VERSION);
749
750 return Transmit(command, true, CEC_OPCODE_CEC_VERSION);
751 }
752
753 bool CCECCommandHandler::TransmitRequestMenuLanguage(const cec_logical_address iInitiator, const cec_logical_address iDestination)
754 {
755 cec_command command;
756 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_GET_MENU_LANGUAGE);
757
758 return Transmit(command, true, CEC_OPCODE_SET_MENU_LANGUAGE);
759 }
760
761 bool CCECCommandHandler::TransmitRequestOSDName(const cec_logical_address iInitiator, const cec_logical_address iDestination)
762 {
763 cec_command command;
764 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_GIVE_OSD_NAME);
765
766 return Transmit(command, true, CEC_OPCODE_SET_OSD_NAME);
767 }
768
769 bool CCECCommandHandler::TransmitRequestPhysicalAddress(const cec_logical_address iInitiator, const cec_logical_address iDestination)
770 {
771 cec_command command;
772 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_GIVE_PHYSICAL_ADDRESS);
773
774 return Transmit(command, true, CEC_OPCODE_REPORT_PHYSICAL_ADDRESS);
775 }
776
777 bool CCECCommandHandler::TransmitRequestPowerStatus(const cec_logical_address iInitiator, const cec_logical_address iDestination)
778 {
779 cec_command command;
780 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_GIVE_DEVICE_POWER_STATUS);
781
782 return Transmit(command, true, CEC_OPCODE_REPORT_POWER_STATUS);
783 }
784
785 bool CCECCommandHandler::TransmitRequestVendorId(const cec_logical_address iInitiator, const cec_logical_address iDestination)
786 {
787 cec_command command;
788 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_GIVE_DEVICE_VENDOR_ID);
789
790 return Transmit(command, true, CEC_OPCODE_DEVICE_VENDOR_ID);
791 }
792
793 bool CCECCommandHandler::TransmitActiveSource(const cec_logical_address iInitiator, uint16_t iPhysicalAddress)
794 {
795 cec_command command;
796 cec_command::Format(command, iInitiator, CECDEVICE_BROADCAST, CEC_OPCODE_ACTIVE_SOURCE);
797 command.parameters.PushBack((uint8_t) ((iPhysicalAddress >> 8) & 0xFF));
798 command.parameters.PushBack((uint8_t) (iPhysicalAddress & 0xFF));
799
800 return Transmit(command, false);
801 }
802
803 bool CCECCommandHandler::TransmitCECVersion(const cec_logical_address iInitiator, const cec_logical_address iDestination, cec_version cecVersion)
804 {
805 cec_command command;
806 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_CEC_VERSION);
807 command.parameters.PushBack((uint8_t)cecVersion);
808
809 return Transmit(command, false);
810 }
811
812 bool CCECCommandHandler::TransmitInactiveSource(const cec_logical_address iInitiator, uint16_t iPhysicalAddress)
813 {
814 cec_command command;
815 cec_command::Format(command, iInitiator, CECDEVICE_TV, CEC_OPCODE_INACTIVE_SOURCE);
816 command.parameters.PushBack((iPhysicalAddress >> 8) & 0xFF);
817 command.parameters.PushBack(iPhysicalAddress & 0xFF);
818
819 return Transmit(command, false);
820 }
821
822 bool CCECCommandHandler::TransmitMenuState(const cec_logical_address iInitiator, const cec_logical_address iDestination, cec_menu_state menuState)
823 {
824 cec_command command;
825 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_MENU_STATUS);
826 command.parameters.PushBack((uint8_t)menuState);
827
828 return Transmit(command, false);
829 }
830
831 bool CCECCommandHandler::TransmitOSDName(const cec_logical_address iInitiator, const cec_logical_address iDestination, CStdString strDeviceName)
832 {
833 cec_command command;
834 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_SET_OSD_NAME);
835 for (size_t iPtr = 0; iPtr < strDeviceName.length(); iPtr++)
836 command.parameters.PushBack(strDeviceName.at(iPtr));
837
838 return Transmit(command, false);
839 }
840
841 bool CCECCommandHandler::TransmitOSDString(const cec_logical_address iInitiator, const cec_logical_address iDestination, cec_display_control duration, const char *strMessage)
842 {
843 cec_command command;
844 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_SET_OSD_STRING);
845 command.parameters.PushBack((uint8_t)duration);
846
847 size_t iLen = strlen(strMessage);
848 if (iLen > 13) iLen = 13;
849
850 for (size_t iPtr = 0; iPtr < iLen; iPtr++)
851 command.parameters.PushBack(strMessage[iPtr]);
852
853 return Transmit(command, false);
854 }
855
856 bool CCECCommandHandler::TransmitPhysicalAddress(const cec_logical_address iInitiator, uint16_t iPhysicalAddress, cec_device_type type)
857 {
858 cec_command command;
859 cec_command::Format(command, iInitiator, CECDEVICE_BROADCAST, CEC_OPCODE_REPORT_PHYSICAL_ADDRESS);
860 command.parameters.PushBack((uint8_t) ((iPhysicalAddress >> 8) & 0xFF));
861 command.parameters.PushBack((uint8_t) (iPhysicalAddress & 0xFF));
862 command.parameters.PushBack((uint8_t) (type));
863
864 return Transmit(command, false);
865 }
866
867 bool CCECCommandHandler::TransmitPoll(const cec_logical_address iInitiator, const cec_logical_address iDestination)
868 {
869 cec_command command;
870 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_NONE);
871
872 return Transmit(command, false);
873 }
874
875 bool CCECCommandHandler::TransmitPowerState(const cec_logical_address iInitiator, const cec_logical_address iDestination, cec_power_status state)
876 {
877 cec_command command;
878 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_REPORT_POWER_STATUS);
879 command.parameters.PushBack((uint8_t) state);
880
881 return Transmit(command, false);
882 }
883
884 bool CCECCommandHandler::TransmitVendorID(const cec_logical_address iInitiator, uint64_t iVendorId)
885 {
886 cec_command command;
887 cec_command::Format(command, iInitiator, CECDEVICE_BROADCAST, CEC_OPCODE_DEVICE_VENDOR_ID);
888
889 command.parameters.PushBack((uint8_t) (((uint64_t)iVendorId >> 16) & 0xFF));
890 command.parameters.PushBack((uint8_t) (((uint64_t)iVendorId >> 8) & 0xFF));
891 command.parameters.PushBack((uint8_t) ((uint64_t)iVendorId & 0xFF));
892
893 return Transmit(command, false);
894 }
895
896 bool CCECCommandHandler::TransmitAudioStatus(const cec_logical_address iInitiator, const cec_logical_address iDestination, uint8_t state)
897 {
898 cec_command command;
899 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_REPORT_AUDIO_STATUS);
900 command.parameters.PushBack(state);
901
902 return Transmit(command, false);
903 }
904
905 bool CCECCommandHandler::TransmitSetSystemAudioMode(const cec_logical_address iInitiator, const cec_logical_address iDestination, cec_system_audio_status state)
906 {
907 cec_command command;
908 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_SET_SYSTEM_AUDIO_MODE);
909 command.parameters.PushBack((uint8_t)state);
910
911 return Transmit(command, false);
912 }
913
914 bool CCECCommandHandler::TransmitSystemAudioModeStatus(const cec_logical_address iInitiator, const cec_logical_address iDestination, cec_system_audio_status state)
915 {
916 cec_command command;
917 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_SYSTEM_AUDIO_MODE_STATUS);
918 command.parameters.PushBack((uint8_t)state);
919
920 return Transmit(command, false);
921 }
922
923 bool CCECCommandHandler::TransmitDeckStatus(const cec_logical_address iInitiator, const cec_logical_address iDestination, cec_deck_info state)
924 {
925 cec_command command;
926 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_DECK_STATUS);
927 command.PushBack((uint8_t)state);
928
929 return Transmit(command, false);
930 }
931
932 bool CCECCommandHandler::TransmitKeypress(const cec_logical_address iInitiator, const cec_logical_address iDestination, cec_user_control_code key, bool bWait /* = true */)
933 {
934 cec_command command;
935 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_USER_CONTROL_PRESSED);
936 command.parameters.PushBack((uint8_t)key);
937
938 return Transmit(command, bWait);
939 }
940
941 bool CCECCommandHandler::TransmitKeyRelease(const cec_logical_address iInitiator, const cec_logical_address iDestination, bool bWait /* = true */)
942 {
943 cec_command command;
944 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_USER_CONTROL_RELEASE);
945
946 return Transmit(command, bWait);
947 }
948
949 bool CCECCommandHandler::Transmit(cec_command &command, bool bExpectResponse /* = true */, cec_opcode expectedResponse /* = CEC_OPCODE_NONE */)
950 {
951 bool bReturn(false);
952 MarkBusy();
953 command.transmit_timeout = m_iTransmitTimeout;
954
955 {
956 uint8_t iTries(0), iMaxTries(command.opcode == CEC_OPCODE_NONE ? 1 : m_iTransmitRetries + 1);
957 CLockObject writeLock(m_processor->m_transmitMutex);
958 CLockObject receiveLock(m_receiveMutex);
959 ++m_iUseCounter;
960 while (!bReturn && ++iTries <= iMaxTries)
961 {
962 m_expectedResponse = expectedResponse;
963 if (m_processor->Transmit(command))
964 {
965 CLibCEC::AddLog(CEC_LOG_DEBUG, "command transmitted");
966 bReturn = bExpectResponse ?
967 m_condition.Wait(m_receiveMutex, m_iTransmitWait) :
968 true;
969 }
970 }
971 --m_iUseCounter;
972 }
973
974 MarkReady();
975 return bReturn;
976 }
977
978 bool CCECCommandHandler::ActivateSource(void)
979 {
980 if (m_busDevice->GetLogicalAddress() == CECDEVICE_TV)
981 {
982 CCECBusDevice *primary = m_processor->GetPrimaryDevice();
983 primary->SetPowerStatus(CEC_POWER_STATUS_ON);
984 primary->SetMenuState(CEC_MENU_STATE_ACTIVATED);
985
986 if (m_processor->GetPrimaryDevice()->GetPhysicalAddress(false) != 0xffff)
987 {
988 m_processor->SetActiveSource();
989 primary->TransmitMenuState(m_busDevice->GetLogicalAddress());
990 m_bHandlerInited = true;
991 }
992 }
993 return true;
994 }
995
996 void CCECCommandHandler::MarkBusy(void)
997 {
998 CLockObject receiveLock(m_receiveMutex);
999 ++m_iUseCounter;
1000 }
1001
1002 bool CCECCommandHandler::MarkReady(void)
1003 {
1004 CLockObject receiveLock(m_receiveMutex);
1005 return m_iUseCounter > 0 ? (--m_iUseCounter == 0) : true;
1006 }
1007
1008 bool CCECCommandHandler::InUse(void)
1009 {
1010 CLockObject receiveLock(m_receiveMutex);
1011 return m_iUseCounter > 0;
1012 }