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