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