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