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