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