cec: fixed - commands that were forwarded to clients used the source address of the...
[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-2012 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 "../CECClient.h"
38 #include "../CECProcessor.h"
39 #include "../LibCEC.h"
40 #include "../CECTypeUtils.h"
41 #include "../platform/util/util.h"
42
43 using namespace CEC;
44 using namespace std;
45 using namespace PLATFORM;
46
47 #define LIB_CEC m_busDevice->GetProcessor()->GetLib()
48 #define ToString(p) CCECTypeUtils::ToString(p)
49
50 CCECCommandHandler::CCECCommandHandler(CCECBusDevice *busDevice) :
51 m_busDevice(busDevice),
52 m_processor(m_busDevice->GetProcessor()),
53 m_iTransmitTimeout(CEC_DEFAULT_TRANSMIT_TIMEOUT),
54 m_iTransmitWait(CEC_DEFAULT_TRANSMIT_WAIT),
55 m_iTransmitRetries(CEC_DEFAULT_TRANSMIT_RETRIES),
56 m_bHandlerInited(false),
57 m_bOPTSendDeckStatusUpdateOnActiveSource(false),
58 m_vendorId(CEC_VENDOR_UNKNOWN),
59 m_waitForResponse(new CWaitForResponse),
60 m_iActiveSourcePending(0)
61 {
62 }
63
64 CCECCommandHandler::~CCECCommandHandler(void)
65 {
66 DELETE_AND_NULL(m_waitForResponse);
67 }
68
69 bool CCECCommandHandler::HandleCommand(const cec_command &command)
70 {
71 if (command.opcode_set == 0)
72 return HandlePoll(command);
73
74 int iHandled(CEC_ABORT_REASON_UNRECOGNIZED_OPCODE);
75
76 LIB_CEC->AddCommand(command);
77
78 switch(command.opcode)
79 {
80 case CEC_OPCODE_REPORT_POWER_STATUS:
81 iHandled = HandleReportPowerStatus(command);
82 break;
83 case CEC_OPCODE_CEC_VERSION:
84 iHandled = HandleDeviceCecVersion(command);
85 break;
86 case CEC_OPCODE_SET_MENU_LANGUAGE:
87 iHandled = HandleSetMenuLanguage(command);
88 break;
89 case CEC_OPCODE_GIVE_PHYSICAL_ADDRESS:
90 iHandled = HandleGivePhysicalAddress(command);
91 break;
92 case CEC_OPCODE_GET_MENU_LANGUAGE:
93 iHandled = HandleGiveMenuLanguage(command);
94 break;
95 case CEC_OPCODE_GIVE_OSD_NAME:
96 iHandled = HandleGiveOSDName(command);
97 break;
98 case CEC_OPCODE_GIVE_DEVICE_VENDOR_ID:
99 iHandled = HandleGiveDeviceVendorId(command);
100 break;
101 case CEC_OPCODE_DEVICE_VENDOR_ID:
102 iHandled = HandleDeviceVendorId(command);
103 break;
104 case CEC_OPCODE_VENDOR_COMMAND_WITH_ID:
105 iHandled = HandleDeviceVendorCommandWithId(command);
106 break;
107 case CEC_OPCODE_GIVE_DECK_STATUS:
108 iHandled = HandleGiveDeckStatus(command);
109 break;
110 case CEC_OPCODE_DECK_CONTROL:
111 iHandled = HandleDeckControl(command);
112 break;
113 case CEC_OPCODE_MENU_REQUEST:
114 iHandled = HandleMenuRequest(command);
115 break;
116 case CEC_OPCODE_GIVE_DEVICE_POWER_STATUS:
117 iHandled = HandleGiveDevicePowerStatus(command);
118 break;
119 case CEC_OPCODE_GET_CEC_VERSION:
120 iHandled = HandleGetCecVersion(command);
121 break;
122 case CEC_OPCODE_USER_CONTROL_PRESSED:
123 iHandled = HandleUserControlPressed(command);
124 break;
125 case CEC_OPCODE_USER_CONTROL_RELEASE:
126 iHandled = HandleUserControlRelease(command);
127 break;
128 case CEC_OPCODE_GIVE_AUDIO_STATUS:
129 iHandled = HandleGiveAudioStatus(command);
130 break;
131 case CEC_OPCODE_GIVE_SYSTEM_AUDIO_MODE_STATUS:
132 iHandled = HandleGiveSystemAudioModeStatus(command);
133 break;
134 case CEC_OPCODE_SYSTEM_AUDIO_MODE_REQUEST:
135 iHandled = HandleSystemAudioModeRequest(command);
136 break;
137 case CEC_OPCODE_REPORT_AUDIO_STATUS:
138 iHandled = HandleReportAudioStatus(command);
139 break;
140 case CEC_OPCODE_SYSTEM_AUDIO_MODE_STATUS:
141 iHandled = HandleSystemAudioModeStatus(command);
142 break;
143 case CEC_OPCODE_SET_SYSTEM_AUDIO_MODE:
144 iHandled = HandleSetSystemAudioMode(command);
145 break;
146 case CEC_OPCODE_REQUEST_ACTIVE_SOURCE:
147 iHandled = HandleRequestActiveSource(command);
148 break;
149 case CEC_OPCODE_SET_STREAM_PATH:
150 iHandled = HandleSetStreamPath(command);
151 break;
152 case CEC_OPCODE_ROUTING_CHANGE:
153 iHandled = HandleRoutingChange(command);
154 break;
155 case CEC_OPCODE_ROUTING_INFORMATION:
156 iHandled = HandleRoutingInformation(command);
157 break;
158 case CEC_OPCODE_STANDBY:
159 iHandled = HandleStandby(command);
160 break;
161 case CEC_OPCODE_ACTIVE_SOURCE:
162 iHandled = HandleActiveSource(command);
163 break;
164 case CEC_OPCODE_REPORT_PHYSICAL_ADDRESS:
165 iHandled = HandleReportPhysicalAddress(command);
166 break;
167 case CEC_OPCODE_SET_OSD_NAME:
168 iHandled = HandleSetOSDName(command);
169 break;
170 case CEC_OPCODE_IMAGE_VIEW_ON:
171 iHandled = HandleImageViewOn(command);
172 break;
173 case CEC_OPCODE_TEXT_VIEW_ON:
174 iHandled = HandleTextViewOn(command);
175 break;
176 case CEC_OPCODE_FEATURE_ABORT:
177 iHandled = HandleFeatureAbort(command);
178 break;
179 case CEC_OPCODE_VENDOR_COMMAND:
180 iHandled = HandleVendorCommand(command);
181 break;
182 case CEC_OPCODE_VENDOR_REMOTE_BUTTON_DOWN:
183 iHandled = HandleVendorRemoteButtonDown(command);
184 break;
185 case CEC_OPCODE_VENDOR_REMOTE_BUTTON_UP:
186 iHandled = HandleVendorRemoteButtonUp(command);
187 break;
188 case CEC_OPCODE_PLAY:
189 // libCEC (currently) doesn't need to do anything with this, since player applications handle it
190 // but it should not respond with a feature abort
191 iHandled = COMMAND_HANDLED;
192 break;
193 default:
194 break;
195 }
196
197 if (iHandled == COMMAND_HANDLED)
198 m_waitForResponse->Received((command.opcode == CEC_OPCODE_FEATURE_ABORT && command.parameters.size > 0) ? (cec_opcode)command.parameters[0] : command.opcode);
199 else
200 UnhandledCommand(command, (cec_abort_reason)iHandled);
201
202 return iHandled == COMMAND_HANDLED;
203 }
204
205 int CCECCommandHandler::HandleActiveSource(const cec_command &command)
206 {
207 if (command.parameters.size == 2)
208 {
209 uint16_t iAddress = ((uint16_t)command.parameters[0] << 8) | ((uint16_t)command.parameters[1]);
210 CCECBusDevice *device = m_processor->GetDeviceByPhysicalAddress(iAddress);
211 if (device)
212 {
213 device->MarkAsActiveSource();
214 return COMMAND_HANDLED;
215 }
216 }
217
218 return CEC_ABORT_REASON_INVALID_OPERAND;
219 }
220
221 int CCECCommandHandler::HandleDeckControl(const cec_command &command)
222 {
223 CCECPlaybackDevice *device = CCECBusDevice::AsPlaybackDevice(GetDevice(command.destination));
224 if (device && command.parameters.size > 0)
225 {
226 device->SetDeckControlMode((cec_deck_control_mode) command.parameters[0]);
227 return COMMAND_HANDLED;
228 }
229
230 return CEC_ABORT_REASON_INVALID_OPERAND;
231 }
232
233 int CCECCommandHandler::HandleDeviceCecVersion(const cec_command &command)
234 {
235 if (command.parameters.size == 1)
236 {
237 CCECBusDevice *device = GetDevice(command.initiator);
238 if (device)
239 device->SetCecVersion((cec_version) command.parameters[0]);
240
241 return COMMAND_HANDLED;
242 }
243
244 return CEC_ABORT_REASON_INVALID_OPERAND;
245 }
246
247 int CCECCommandHandler::HandleDeviceVendorCommandWithId(const cec_command & UNUSED(command))
248 {
249 return CEC_ABORT_REASON_INVALID_OPERAND;
250 }
251
252 int CCECCommandHandler::HandleDeviceVendorId(const cec_command &command)
253 {
254 SetVendorId(command);
255 return COMMAND_HANDLED;
256 }
257
258 int CCECCommandHandler::HandleFeatureAbort(const cec_command &command)
259 {
260 if (command.parameters.size == 2 &&
261 (command.parameters[1] == CEC_ABORT_REASON_UNRECOGNIZED_OPCODE ||
262 command.parameters[1] == CEC_ABORT_REASON_REFUSED))
263 m_processor->GetDevice(command.initiator)->SetUnsupportedFeature((cec_opcode)command.parameters[0]);
264 return COMMAND_HANDLED;
265 }
266
267 int CCECCommandHandler::HandleGetCecVersion(const cec_command &command)
268 {
269 if (m_processor->CECInitialised() && m_processor->IsHandledByLibCEC(command.destination))
270 {
271 CCECBusDevice *device = GetDevice(command.destination);
272 if (device && device->TransmitCECVersion(command.initiator))
273 return COMMAND_HANDLED;
274 return CEC_ABORT_REASON_INVALID_OPERAND;
275 }
276
277 return CEC_ABORT_REASON_NOT_IN_CORRECT_MODE_TO_RESPOND;
278 }
279
280 int CCECCommandHandler::HandleGiveAudioStatus(const cec_command &command)
281 {
282 if (m_processor->CECInitialised() && m_processor->IsHandledByLibCEC(command.destination))
283 {
284 CCECAudioSystem *device = CCECBusDevice::AsAudioSystem(GetDevice(command.destination));
285 if (device && device->TransmitAudioStatus(command.initiator))
286 return COMMAND_HANDLED;
287 return CEC_ABORT_REASON_INVALID_OPERAND;
288 }
289
290 return CEC_ABORT_REASON_NOT_IN_CORRECT_MODE_TO_RESPOND;
291 }
292
293 int CCECCommandHandler::HandleGiveDeckStatus(const cec_command &command)
294 {
295 if (m_processor->CECInitialised() && m_processor->IsHandledByLibCEC(command.destination))
296 {
297 CCECPlaybackDevice *device = CCECBusDevice::AsPlaybackDevice(GetDevice(command.destination));
298 if (device && device->TransmitDeckStatus(command.initiator))
299 return COMMAND_HANDLED;
300 return CEC_ABORT_REASON_INVALID_OPERAND;
301 }
302
303 return CEC_ABORT_REASON_NOT_IN_CORRECT_MODE_TO_RESPOND;
304 }
305
306 int CCECCommandHandler::HandleGiveDevicePowerStatus(const cec_command &command)
307 {
308 if (m_processor->CECInitialised() && m_processor->IsHandledByLibCEC(command.destination))
309 {
310 CCECBusDevice *device = GetDevice(command.destination);
311 if (device && device->TransmitPowerState(command.initiator))
312 return COMMAND_HANDLED;
313 return CEC_ABORT_REASON_INVALID_OPERAND;
314 }
315
316 return CEC_ABORT_REASON_INVALID_OPERAND;
317 }
318
319 int CCECCommandHandler::HandleGiveDeviceVendorId(const cec_command &command)
320 {
321 if (m_processor->CECInitialised() && m_processor->IsHandledByLibCEC(command.destination))
322 {
323 CCECBusDevice *device = GetDevice(command.destination);
324 if (device && device->TransmitVendorID(command.initiator))
325 return COMMAND_HANDLED;
326 }
327
328 return CEC_ABORT_REASON_INVALID_OPERAND;
329 }
330
331 int CCECCommandHandler::HandleGiveOSDName(const cec_command &command)
332 {
333 if (m_processor->CECInitialised() && m_processor->IsHandledByLibCEC(command.destination))
334 {
335 CCECBusDevice *device = GetDevice(command.destination);
336 if (device && device->TransmitOSDName(command.initiator))
337 return COMMAND_HANDLED;
338 }
339
340 return CEC_ABORT_REASON_INVALID_OPERAND;
341 }
342
343 int CCECCommandHandler::HandleGivePhysicalAddress(const cec_command &command)
344 {
345 if (m_processor->CECInitialised() && m_processor->IsHandledByLibCEC(command.destination))
346 {
347 CCECBusDevice *device = GetDevice(command.destination);
348 if (device && device->TransmitPhysicalAddress())
349 return COMMAND_HANDLED;
350 return CEC_ABORT_REASON_INVALID_OPERAND;
351 }
352
353 return CEC_ABORT_REASON_NOT_IN_CORRECT_MODE_TO_RESPOND;
354 }
355
356 int CCECCommandHandler::HandleGiveMenuLanguage(const cec_command &command)
357 {
358 if (m_processor->CECInitialised() && m_processor->IsHandledByLibCEC(command.destination))
359 {
360 CCECBusDevice *device = GetDevice(command.destination);
361 if (device && device->TransmitSetMenuLanguage(command.initiator))
362 return COMMAND_HANDLED;
363 return CEC_ABORT_REASON_INVALID_OPERAND;
364 }
365
366 return CEC_ABORT_REASON_NOT_IN_CORRECT_MODE_TO_RESPOND;
367 }
368
369 int CCECCommandHandler::HandleGiveSystemAudioModeStatus(const cec_command &command)
370 {
371 if (m_processor->CECInitialised() && m_processor->IsHandledByLibCEC(command.destination))
372 {
373 CCECAudioSystem *device = CCECBusDevice::AsAudioSystem(GetDevice(command.destination));
374 if (device && device->TransmitSystemAudioModeStatus(command.initiator))
375 return COMMAND_HANDLED;
376 return CEC_ABORT_REASON_INVALID_OPERAND;
377 }
378
379 return CEC_ABORT_REASON_NOT_IN_CORRECT_MODE_TO_RESPOND;
380 }
381
382 int CCECCommandHandler::HandleImageViewOn(const cec_command &command)
383 {
384 m_processor->GetDevice(command.initiator)->MarkAsActiveSource();
385 return COMMAND_HANDLED;
386 }
387
388 int CCECCommandHandler::HandleMenuRequest(const cec_command &command)
389 {
390 if (m_processor->CECInitialised() && m_processor->IsHandledByLibCEC(command.destination))
391 {
392 CCECBusDevice *device = GetDevice(command.destination);
393 if (device)
394 {
395 CCECClient *client = device->GetClient();
396 if (client)
397 {
398 if (command.parameters[0] == CEC_MENU_REQUEST_TYPE_ACTIVATE)
399 {
400 if (client->MenuStateChanged(CEC_MENU_STATE_ACTIVATED) == 1)
401 device->SetMenuState(CEC_MENU_STATE_ACTIVATED);
402 }
403 else if (command.parameters[0] == CEC_MENU_REQUEST_TYPE_DEACTIVATE)
404 {
405 if (client->MenuStateChanged(CEC_MENU_STATE_DEACTIVATED) == 1)
406 device->SetMenuState(CEC_MENU_STATE_DEACTIVATED);
407 }
408 }
409 if (device->TransmitMenuState(command.initiator))
410 return COMMAND_HANDLED;
411 }
412 return CEC_ABORT_REASON_INVALID_OPERAND;
413 }
414
415 return CEC_ABORT_REASON_NOT_IN_CORRECT_MODE_TO_RESPOND;
416 }
417
418 bool CCECCommandHandler::HandlePoll(const cec_command &command)
419 {
420 m_busDevice->HandlePoll(command.destination);
421 return true;
422 }
423
424 int CCECCommandHandler::HandleReportAudioStatus(const cec_command &command)
425 {
426 if (command.parameters.size == 1)
427 {
428 CCECAudioSystem *device = CCECBusDevice::AsAudioSystem(GetDevice(command.initiator));
429 if (device)
430 {
431 device->SetAudioStatus(command.parameters[0]);
432 return COMMAND_HANDLED;
433 }
434 }
435 return CEC_ABORT_REASON_INVALID_OPERAND;
436 }
437
438 int CCECCommandHandler::HandleReportPhysicalAddress(const cec_command &command)
439 {
440 if (command.parameters.size == 3)
441 {
442 uint16_t iNewAddress = ((uint16_t)command.parameters[0] << 8) | ((uint16_t)command.parameters[1]);
443 SetPhysicalAddress(command.initiator, iNewAddress);
444 return COMMAND_HANDLED;
445 }
446 return CEC_ABORT_REASON_INVALID_OPERAND;
447 }
448
449 int CCECCommandHandler::HandleReportPowerStatus(const cec_command &command)
450 {
451 if (command.parameters.size == 1)
452 {
453 CCECBusDevice *device = GetDevice(command.initiator);
454 if (device)
455 {
456 device->SetPowerStatus((cec_power_status) command.parameters[0]);
457 return COMMAND_HANDLED;
458 }
459 }
460 return CEC_ABORT_REASON_INVALID_OPERAND;
461 }
462
463 int CCECCommandHandler::HandleRequestActiveSource(const cec_command &command)
464 {
465 if (m_processor->CECInitialised())
466 {
467 LIB_CEC->AddLog(CEC_LOG_DEBUG, ">> %i requests active source", (uint8_t) command.initiator);
468 m_processor->GetDevice(command.initiator)->SetPowerStatus(CEC_POWER_STATUS_ON);
469
470 vector<CCECBusDevice *> devices;
471 for (size_t iDevicePtr = 0; iDevicePtr < GetMyDevices(devices); iDevicePtr++)
472 devices[iDevicePtr]->TransmitActiveSource();
473 }
474
475 return COMMAND_HANDLED;
476 }
477
478 int CCECCommandHandler::HandleRoutingChange(const cec_command &command)
479 {
480 if (command.parameters.size == 4)
481 {
482 uint16_t iOldAddress = ((uint16_t)command.parameters[0] << 8) | ((uint16_t)command.parameters[1]);
483 uint16_t iNewAddress = ((uint16_t)command.parameters[2] << 8) | ((uint16_t)command.parameters[3]);
484
485 CCECBusDevice *device = GetDevice(command.initiator);
486 if (device)
487 {
488 device->SetStreamPath(iNewAddress, iOldAddress);
489 return COMMAND_HANDLED;
490 }
491 }
492
493 return CEC_ABORT_REASON_INVALID_OPERAND;
494 }
495
496 int CCECCommandHandler::HandleRoutingInformation(const cec_command &command)
497 {
498 if (command.parameters.size == 2)
499 {
500 uint16_t iNewAddress = ((uint16_t)command.parameters[0] << 8) | ((uint16_t)command.parameters[1]);
501 CCECBusDevice *device = m_processor->GetDeviceByPhysicalAddress(iNewAddress);
502 if (device)
503 {
504 device->MarkAsActiveSource();
505 return COMMAND_HANDLED;
506 }
507 }
508
509 return CEC_ABORT_REASON_INVALID_OPERAND;
510 }
511
512 int CCECCommandHandler::HandleSetMenuLanguage(const cec_command &command)
513 {
514 if (command.parameters.size == 3)
515 {
516 CCECBusDevice *device = GetDevice(command.initiator);
517 if (device)
518 {
519 cec_menu_language language;
520 language.device = command.initiator;
521 for (uint8_t iPtr = 0; iPtr < 4; iPtr++)
522 language.language[iPtr] = command.parameters[iPtr];
523 language.language[3] = 0;
524 device->SetMenuLanguage(language);
525 return COMMAND_HANDLED;
526 }
527 }
528
529 return CEC_ABORT_REASON_INVALID_OPERAND;
530 }
531
532 int CCECCommandHandler::HandleSetOSDName(const cec_command &command)
533 {
534 if (command.parameters.size > 0)
535 {
536 CCECBusDevice *device = GetDevice(command.initiator);
537 if (device)
538 {
539 char buf[1024];
540 for (uint8_t iPtr = 0; iPtr < command.parameters.size; iPtr++)
541 buf[iPtr] = (char)command.parameters[iPtr];
542 buf[command.parameters.size] = 0;
543
544 CStdString strName(buf);
545 device->SetOSDName(strName);
546
547 return COMMAND_HANDLED;
548 }
549 }
550
551 return CEC_ABORT_REASON_INVALID_OPERAND;
552 }
553
554 int CCECCommandHandler::HandleSetStreamPath(const cec_command &command)
555 {
556 if (!m_processor->CECInitialised())
557 return CEC_ABORT_REASON_NOT_IN_CORRECT_MODE_TO_RESPOND;
558
559 if (command.parameters.size >= 2)
560 {
561 uint16_t iStreamAddress = ((uint16_t)command.parameters[0] << 8) | ((uint16_t)command.parameters[1]);
562 LIB_CEC->AddLog(CEC_LOG_DEBUG, ">> %s (%x) sets stream path to physical address %04x", ToString(command.initiator), command.initiator, iStreamAddress);
563
564 // a device will only change the stream path when it's powered on
565 m_busDevice->SetPowerStatus(CEC_POWER_STATUS_ON);
566
567 /* one of the device handled by libCEC has been made active */
568 CCECBusDevice *device = GetDeviceByPhysicalAddress(iStreamAddress);
569 if (device && device->IsHandledByLibCEC())
570 {
571 device->ActivateSource();
572 return COMMAND_HANDLED;
573 }
574 }
575
576 return CEC_ABORT_REASON_INVALID_OPERAND;
577 }
578
579 int CCECCommandHandler::HandleSystemAudioModeRequest(const cec_command &command)
580 {
581 if (m_processor->CECInitialised() && m_processor->IsHandledByLibCEC(command.destination))
582 {
583 CCECAudioSystem *device = CCECBusDevice::AsAudioSystem(GetDevice(command.destination));
584 if (device)
585 {
586 if (command.parameters.size >= 2)
587 {
588 device->SetPowerStatus(CEC_POWER_STATUS_ON);
589 device->SetSystemAudioModeStatus(CEC_SYSTEM_AUDIO_STATUS_ON);
590 uint16_t iNewAddress = ((uint16_t)command.parameters[0] << 8) | ((uint16_t)command.parameters[1]);
591 CCECBusDevice *newActiveDevice = GetDeviceByPhysicalAddress(iNewAddress);
592 if (newActiveDevice)
593 newActiveDevice->MarkAsActiveSource();
594 if (device->TransmitSetSystemAudioMode(command.initiator))
595 return COMMAND_HANDLED;
596 }
597 else
598 {
599 device->SetSystemAudioModeStatus(CEC_SYSTEM_AUDIO_STATUS_OFF);
600 if (device->TransmitSetSystemAudioMode(command.initiator))
601 return COMMAND_HANDLED;
602 }
603 }
604 }
605
606 return CEC_ABORT_REASON_NOT_IN_CORRECT_MODE_TO_RESPOND;
607 }
608
609 int CCECCommandHandler::HandleStandby(const cec_command &command)
610 {
611 CCECBusDevice *device = GetDevice(command.initiator);
612 if (device)
613 device->SetPowerStatus(CEC_POWER_STATUS_STANDBY);
614
615 return COMMAND_HANDLED;
616 }
617
618 int CCECCommandHandler::HandleSystemAudioModeStatus(const cec_command &command)
619 {
620 if (command.parameters.size == 1)
621 {
622 CCECAudioSystem *device = CCECBusDevice::AsAudioSystem(GetDevice(command.initiator));
623 if (device)
624 {
625 device->SetSystemAudioModeStatus((cec_system_audio_status)command.parameters[0]);
626 return COMMAND_HANDLED;
627 }
628 }
629
630 return CEC_ABORT_REASON_INVALID_OPERAND;
631 }
632
633 int CCECCommandHandler::HandleSetSystemAudioMode(const cec_command &command)
634 {
635 if (command.parameters.size == 1)
636 {
637 CCECAudioSystem *device = CCECBusDevice::AsAudioSystem(GetDevice(command.initiator));
638 if (device)
639 {
640 device->SetSystemAudioModeStatus((cec_system_audio_status)command.parameters[0]);
641 return COMMAND_HANDLED;
642 }
643 }
644
645 return CEC_ABORT_REASON_INVALID_OPERAND;
646 }
647
648 int CCECCommandHandler::HandleTextViewOn(const cec_command &command)
649 {
650 m_processor->GetDevice(command.initiator)->MarkAsActiveSource();
651 return COMMAND_HANDLED;
652 }
653
654 int CCECCommandHandler::HandleUserControlPressed(const cec_command &command)
655 {
656 if (!m_processor->CECInitialised() ||
657 !m_processor->IsHandledByLibCEC(command.destination))
658 return CEC_ABORT_REASON_NOT_IN_CORRECT_MODE_TO_RESPOND;
659
660 if (command.parameters.size == 0)
661 return CEC_ABORT_REASON_INVALID_OPERAND;
662
663 CCECBusDevice *device = GetDevice(command.destination);
664 if (!device)
665 return CEC_ABORT_REASON_INVALID_OPERAND;
666
667 CCECClient *client = device->GetClient();
668 if (client)
669 client->AddKey();
670
671 if (command.parameters[0] <= CEC_USER_CONTROL_CODE_MAX)
672 client->SetCurrentButton((cec_user_control_code) command.parameters[0]);
673
674 if (command.parameters[0] == CEC_USER_CONTROL_CODE_POWER ||
675 command.parameters[0] == CEC_USER_CONTROL_CODE_POWER_ON_FUNCTION)
676 {
677 bool bPowerOn(true);
678
679 // CEC_USER_CONTROL_CODE_POWER operates as a toggle
680 // assume CEC_USER_CONTROL_CODE_POWER_ON_FUNCTION does not
681 if (command.parameters[0] == CEC_USER_CONTROL_CODE_POWER)
682 {
683 cec_power_status status = device->GetCurrentPowerStatus();
684 bPowerOn = !(status == CEC_POWER_STATUS_ON || status == CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON);
685 }
686
687 if (bPowerOn)
688 {
689 device->ActivateSource();
690 }
691 else
692 {
693 device->MarkAsInactiveSource();
694 device->TransmitInactiveSource();
695 device->SetMenuState(CEC_MENU_STATE_DEACTIVATED);
696 }
697 }
698
699 return COMMAND_HANDLED;
700 }
701
702 int CCECCommandHandler::HandleUserControlRelease(const cec_command &command)
703 {
704 if (!m_processor->CECInitialised() ||
705 !m_processor->IsHandledByLibCEC(command.destination))
706 return CEC_ABORT_REASON_NOT_IN_CORRECT_MODE_TO_RESPOND;
707
708 CCECClient *client = m_processor->GetClient(command.destination);
709 if (client)
710 client->AddKey();
711
712 return COMMAND_HANDLED;
713 }
714
715 int CCECCommandHandler::HandleVendorCommand(const cec_command & UNUSED(command))
716 {
717 return CEC_ABORT_REASON_INVALID_OPERAND;
718 }
719
720 void CCECCommandHandler::UnhandledCommand(const cec_command &command, const cec_abort_reason reason)
721 {
722 if (m_processor->IsHandledByLibCEC(command.destination))
723 {
724 LIB_CEC->AddLog(CEC_LOG_DEBUG, "sending abort with opcode %02x and reason '%s' to %s", command.opcode, ToString(reason), ToString(command.initiator));
725 m_processor->TransmitAbort(command.destination, command.initiator, command.opcode, reason);
726 }
727 }
728
729 size_t CCECCommandHandler::GetMyDevices(vector<CCECBusDevice *> &devices) const
730 {
731 size_t iReturn(0);
732
733 cec_logical_addresses addresses = m_processor->GetLogicalAddresses();
734 for (uint8_t iPtr = CECDEVICE_TV; iPtr < CECDEVICE_BROADCAST; iPtr++)
735 {
736 if (addresses[iPtr])
737 {
738 devices.push_back(GetDevice((cec_logical_address) iPtr));
739 ++iReturn;
740 }
741 }
742
743 return iReturn;
744 }
745
746 CCECBusDevice *CCECCommandHandler::GetDevice(cec_logical_address iLogicalAddress) const
747 {
748 return m_processor->GetDevice(iLogicalAddress);
749 }
750
751 CCECBusDevice *CCECCommandHandler::GetDeviceByPhysicalAddress(uint16_t iPhysicalAddress) const
752 {
753 return m_processor->GetDeviceByPhysicalAddress(iPhysicalAddress);
754 }
755
756 bool CCECCommandHandler::SetVendorId(const cec_command &command)
757 {
758 bool bChanged(false);
759 if (command.parameters.size < 3)
760 {
761 LIB_CEC->AddLog(CEC_LOG_WARNING, "invalid vendor ID received");
762 return bChanged;
763 }
764
765 uint64_t iVendorId = ((uint64_t)command.parameters[0] << 16) +
766 ((uint64_t)command.parameters[1] << 8) +
767 (uint64_t)command.parameters[2];
768
769 CCECBusDevice *device = GetDevice((cec_logical_address) command.initiator);
770 if (device)
771 bChanged = device->SetVendorId(iVendorId);
772 return bChanged;
773 }
774
775 void CCECCommandHandler::SetPhysicalAddress(cec_logical_address iAddress, uint16_t iNewAddress)
776 {
777 if (!m_processor->IsHandledByLibCEC(iAddress))
778 {
779 CCECBusDevice *otherDevice = m_processor->GetDeviceByPhysicalAddress(iNewAddress);
780 CCECClient *client = otherDevice ? otherDevice->GetClient() : NULL;
781
782 CCECBusDevice *device = m_processor->GetDevice(iAddress);
783 if (device)
784 device->SetPhysicalAddress(iNewAddress);
785 else
786 {
787 LIB_CEC->AddLog(CEC_LOG_DEBUG, "device with logical address %X not found", iAddress);
788 }
789
790 /* another device reported the same physical address as ours */
791 if (client)
792 client->ResetPhysicalAddress();
793 }
794 else
795 {
796 LIB_CEC->AddLog(CEC_LOG_DEBUG, "ignore physical address report for device %s (%X) because it's marked as handled by libCEC", ToString(iAddress), iAddress);
797 }
798 }
799
800 bool CCECCommandHandler::PowerOn(const cec_logical_address iInitiator, const cec_logical_address iDestination)
801 {
802 if (iDestination == CECDEVICE_TV)
803 return TransmitImageViewOn(iInitiator, iDestination);
804
805 return TransmitKeypress(iInitiator, iDestination, CEC_USER_CONTROL_CODE_POWER) &&
806 TransmitKeyRelease(iInitiator, iDestination);
807 }
808
809 bool CCECCommandHandler::TransmitImageViewOn(const cec_logical_address iInitiator, const cec_logical_address iDestination)
810 {
811 cec_command command;
812 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_IMAGE_VIEW_ON);
813
814 return Transmit(command);
815 }
816
817 bool CCECCommandHandler::TransmitStandby(const cec_logical_address iInitiator, const cec_logical_address iDestination)
818 {
819 cec_command command;
820 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_STANDBY);
821
822 return Transmit(command);
823 }
824
825 bool CCECCommandHandler::TransmitRequestActiveSource(const cec_logical_address iInitiator, bool bWaitForResponse /* = true */)
826 {
827 cec_command command;
828 cec_command::Format(command, iInitiator, CECDEVICE_BROADCAST, CEC_OPCODE_REQUEST_ACTIVE_SOURCE);
829
830 return Transmit(command, !bWaitForResponse);
831 }
832
833 bool CCECCommandHandler::TransmitRequestCecVersion(const cec_logical_address iInitiator, const cec_logical_address iDestination, bool bWaitForResponse /* = true */)
834 {
835 cec_command command;
836 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_GET_CEC_VERSION);
837
838 return Transmit(command, !bWaitForResponse);
839 }
840
841 bool CCECCommandHandler::TransmitRequestMenuLanguage(const cec_logical_address iInitiator, const cec_logical_address iDestination, bool bWaitForResponse /* = true */)
842 {
843 cec_command command;
844 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_GET_MENU_LANGUAGE);
845
846 return Transmit(command, !bWaitForResponse);
847 }
848
849 bool CCECCommandHandler::TransmitRequestOSDName(const cec_logical_address iInitiator, const cec_logical_address iDestination, bool bWaitForResponse /* = true */)
850 {
851 cec_command command;
852 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_GIVE_OSD_NAME);
853
854 return Transmit(command, !bWaitForResponse);
855 }
856
857 bool CCECCommandHandler::TransmitRequestPhysicalAddress(const cec_logical_address iInitiator, const cec_logical_address iDestination, bool bWaitForResponse /* = true */)
858 {
859 cec_command command;
860 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_GIVE_PHYSICAL_ADDRESS);
861
862 return Transmit(command, !bWaitForResponse);
863 }
864
865 bool CCECCommandHandler::TransmitRequestPowerStatus(const cec_logical_address iInitiator, const cec_logical_address iDestination, bool bWaitForResponse /* = true */)
866 {
867 cec_command command;
868 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_GIVE_DEVICE_POWER_STATUS);
869
870 return Transmit(command, !bWaitForResponse);
871 }
872
873 bool CCECCommandHandler::TransmitRequestVendorId(const cec_logical_address iInitiator, const cec_logical_address iDestination, bool bWaitForResponse /* = true */)
874 {
875 cec_command command;
876 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_GIVE_DEVICE_VENDOR_ID);
877
878 return Transmit(command, !bWaitForResponse);
879 }
880
881 bool CCECCommandHandler::TransmitActiveSource(const cec_logical_address iInitiator, uint16_t iPhysicalAddress)
882 {
883 cec_command command;
884 cec_command::Format(command, iInitiator, CECDEVICE_BROADCAST, CEC_OPCODE_ACTIVE_SOURCE);
885 command.parameters.PushBack((uint8_t) ((iPhysicalAddress >> 8) & 0xFF));
886 command.parameters.PushBack((uint8_t) (iPhysicalAddress & 0xFF));
887
888 return Transmit(command);
889 }
890
891 bool CCECCommandHandler::TransmitCECVersion(const cec_logical_address iInitiator, const cec_logical_address iDestination, cec_version cecVersion)
892 {
893 cec_command command;
894 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_CEC_VERSION);
895 command.parameters.PushBack((uint8_t)cecVersion);
896
897 return Transmit(command);
898 }
899
900 bool CCECCommandHandler::TransmitInactiveSource(const cec_logical_address iInitiator, uint16_t iPhysicalAddress)
901 {
902 cec_command command;
903 cec_command::Format(command, iInitiator, CECDEVICE_TV, CEC_OPCODE_INACTIVE_SOURCE);
904 command.parameters.PushBack((iPhysicalAddress >> 8) & 0xFF);
905 command.parameters.PushBack(iPhysicalAddress & 0xFF);
906
907 return Transmit(command);
908 }
909
910 bool CCECCommandHandler::TransmitMenuState(const cec_logical_address iInitiator, const cec_logical_address iDestination, cec_menu_state menuState)
911 {
912 cec_command command;
913 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_MENU_STATUS);
914 command.parameters.PushBack((uint8_t)menuState);
915
916 return Transmit(command);
917 }
918
919 bool CCECCommandHandler::TransmitOSDName(const cec_logical_address iInitiator, const cec_logical_address iDestination, CStdString strDeviceName)
920 {
921 cec_command command;
922 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_SET_OSD_NAME);
923 for (size_t iPtr = 0; iPtr < strDeviceName.length(); iPtr++)
924 command.parameters.PushBack(strDeviceName.at(iPtr));
925
926 return Transmit(command);
927 }
928
929 bool CCECCommandHandler::TransmitOSDString(const cec_logical_address iInitiator, const cec_logical_address iDestination, cec_display_control duration, const char *strMessage)
930 {
931 cec_command command;
932 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_SET_OSD_STRING);
933 command.parameters.PushBack((uint8_t)duration);
934
935 size_t iLen = strlen(strMessage);
936 if (iLen > 13) iLen = 13;
937
938 for (size_t iPtr = 0; iPtr < iLen; iPtr++)
939 command.parameters.PushBack(strMessage[iPtr]);
940
941 return Transmit(command);
942 }
943
944 bool CCECCommandHandler::TransmitPhysicalAddress(const cec_logical_address iInitiator, uint16_t iPhysicalAddress, cec_device_type type)
945 {
946 cec_command command;
947 cec_command::Format(command, iInitiator, CECDEVICE_BROADCAST, CEC_OPCODE_REPORT_PHYSICAL_ADDRESS);
948 command.parameters.PushBack((uint8_t) ((iPhysicalAddress >> 8) & 0xFF));
949 command.parameters.PushBack((uint8_t) (iPhysicalAddress & 0xFF));
950 command.parameters.PushBack((uint8_t) (type));
951
952 return Transmit(command);
953 }
954
955 bool CCECCommandHandler::TransmitSetMenuLanguage(const cec_logical_address iInitiator, const char lang[3])
956 {
957 cec_command command;
958 command.Format(command, iInitiator, CECDEVICE_BROADCAST, CEC_OPCODE_SET_MENU_LANGUAGE);
959 command.parameters.PushBack((uint8_t) lang[0]);
960 command.parameters.PushBack((uint8_t) lang[1]);
961 command.parameters.PushBack((uint8_t) lang[2]);
962
963 return Transmit(command);
964 }
965
966 bool CCECCommandHandler::TransmitPoll(const cec_logical_address iInitiator, const cec_logical_address iDestination)
967 {
968 cec_command command;
969 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_NONE);
970
971 return Transmit(command);
972 }
973
974 bool CCECCommandHandler::TransmitPowerState(const cec_logical_address iInitiator, const cec_logical_address iDestination, cec_power_status state)
975 {
976 cec_command command;
977 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_REPORT_POWER_STATUS);
978 command.parameters.PushBack((uint8_t) state);
979
980 return Transmit(command);
981 }
982
983 bool CCECCommandHandler::TransmitVendorID(const cec_logical_address iInitiator, uint64_t iVendorId)
984 {
985 cec_command command;
986 cec_command::Format(command, iInitiator, CECDEVICE_BROADCAST, CEC_OPCODE_DEVICE_VENDOR_ID);
987
988 command.parameters.PushBack((uint8_t) (((uint64_t)iVendorId >> 16) & 0xFF));
989 command.parameters.PushBack((uint8_t) (((uint64_t)iVendorId >> 8) & 0xFF));
990 command.parameters.PushBack((uint8_t) ((uint64_t)iVendorId & 0xFF));
991
992 return Transmit(command);
993 }
994
995 bool CCECCommandHandler::TransmitAudioStatus(const cec_logical_address iInitiator, const cec_logical_address iDestination, uint8_t state)
996 {
997 cec_command command;
998 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_REPORT_AUDIO_STATUS);
999 command.parameters.PushBack(state);
1000
1001 return Transmit(command);
1002 }
1003
1004 bool CCECCommandHandler::TransmitSetSystemAudioMode(const cec_logical_address iInitiator, const cec_logical_address iDestination, cec_system_audio_status state)
1005 {
1006 cec_command command;
1007 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_SET_SYSTEM_AUDIO_MODE);
1008 command.parameters.PushBack((uint8_t)state);
1009
1010 return Transmit(command);
1011 }
1012
1013 bool CCECCommandHandler::TransmitSetStreamPath(uint16_t iStreamPath)
1014 {
1015 cec_command command;
1016 cec_command::Format(command, m_busDevice->GetLogicalAddress(), CECDEVICE_BROADCAST, CEC_OPCODE_SET_STREAM_PATH);
1017 command.parameters.PushBack((uint8_t) ((iStreamPath >> 8) & 0xFF));
1018 command.parameters.PushBack((uint8_t) (iStreamPath & 0xFF));
1019
1020 return Transmit(command);
1021 }
1022
1023 bool CCECCommandHandler::TransmitSystemAudioModeStatus(const cec_logical_address iInitiator, const cec_logical_address iDestination, cec_system_audio_status state)
1024 {
1025 cec_command command;
1026 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_SYSTEM_AUDIO_MODE_STATUS);
1027 command.parameters.PushBack((uint8_t)state);
1028
1029 return Transmit(command);
1030 }
1031
1032 bool CCECCommandHandler::TransmitDeckStatus(const cec_logical_address iInitiator, const cec_logical_address iDestination, cec_deck_info state)
1033 {
1034 cec_command command;
1035 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_DECK_STATUS);
1036 command.PushBack((uint8_t)state);
1037
1038 return Transmit(command);
1039 }
1040
1041 bool CCECCommandHandler::TransmitKeypress(const cec_logical_address iInitiator, const cec_logical_address iDestination, cec_user_control_code key, bool bWait /* = true */)
1042 {
1043 cec_command command;
1044 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_USER_CONTROL_PRESSED);
1045 command.parameters.PushBack((uint8_t)key);
1046
1047 return Transmit(command, !bWait);
1048 }
1049
1050 bool CCECCommandHandler::TransmitKeyRelease(const cec_logical_address iInitiator, const cec_logical_address iDestination, bool bWait /* = true */)
1051 {
1052 cec_command command;
1053 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_USER_CONTROL_RELEASE);
1054
1055 return Transmit(command, !bWait);
1056 }
1057
1058 bool CCECCommandHandler::Transmit(cec_command &command, bool bSuppressWait /* = false */)
1059 {
1060 bool bReturn(false);
1061 cec_opcode expectedResponse(cec_command::GetResponseOpcode(command.opcode));
1062 bool bExpectResponse(expectedResponse != CEC_OPCODE_NONE && !bSuppressWait);
1063 command.transmit_timeout = m_iTransmitTimeout;
1064
1065 if (command.initiator == CECDEVICE_UNKNOWN)
1066 {
1067 LIB_CEC->AddLog(CEC_LOG_ERROR, "not transmitting a command without a valid initiator");
1068 return bReturn;
1069 }
1070
1071 {
1072 uint8_t iTries(0), iMaxTries(!command.opcode_set ? 1 : m_iTransmitRetries + 1);
1073 while (!bReturn && ++iTries <= iMaxTries && !m_busDevice->IsUnsupportedFeature(command.opcode))
1074 {
1075 if ((bReturn = m_processor->Transmit(command)) == true)
1076 {
1077 LIB_CEC->AddLog(CEC_LOG_DEBUG, "command transmitted");
1078 if (bExpectResponse)
1079 {
1080 bReturn = m_waitForResponse->Wait(expectedResponse);
1081 LIB_CEC->AddLog(CEC_LOG_DEBUG, bReturn ? "expected response received (%X: %s)" : "expected response not received (%X: %s)", (int)expectedResponse, ToString(expectedResponse));
1082 }
1083 }
1084 }
1085 }
1086
1087 return bReturn;
1088 }
1089
1090 bool CCECCommandHandler::ActivateSource(bool bTransmitDelayedCommandsOnly /* = false */)
1091 {
1092 if (m_busDevice->IsActiveSource() &&
1093 m_busDevice->IsHandledByLibCEC())
1094 {
1095 {
1096 CLockObject lock(m_mutex);
1097 // check if we need to send a delayed source switch
1098 if (bTransmitDelayedCommandsOnly)
1099 {
1100 if (m_iActiveSourcePending == 0 || GetTimeMs() < m_iActiveSourcePending)
1101 return false;
1102
1103 LIB_CEC->AddLog(CEC_LOG_DEBUG, "transmitting delayed activate source command");
1104 }
1105
1106 // clear previous pending active source command
1107 m_iActiveSourcePending = 0;
1108 }
1109
1110 // update the power state and menu state
1111 m_busDevice->SetPowerStatus(CEC_POWER_STATUS_ON);
1112 m_busDevice->SetMenuState(CEC_MENU_STATE_ACTIVATED); // TODO: LG
1113
1114 // power on the TV
1115 bool bActiveSourceFailed = !m_busDevice->TransmitImageViewOn();
1116
1117 // check if we're allowed to switch sources
1118 bool bSourceSwitchAllowed = SourceSwitchAllowed();
1119 if (!bSourceSwitchAllowed)
1120 LIB_CEC->AddLog(CEC_LOG_DEBUG, "source switch is currently not allowed by command handler");
1121
1122 // switch sources (if allowed)
1123 if (!bActiveSourceFailed && bSourceSwitchAllowed)
1124 {
1125 bActiveSourceFailed = !m_busDevice->TransmitActiveSource() ||
1126 !m_busDevice->TransmitMenuState(CECDEVICE_TV);
1127
1128 // update the deck status for playback devices
1129 if (!bActiveSourceFailed)
1130 {
1131 CCECPlaybackDevice *playbackDevice = m_busDevice->AsPlaybackDevice();
1132 if (playbackDevice && SendDeckStatusUpdateOnActiveSource())
1133 bActiveSourceFailed = !playbackDevice->TransmitDeckStatus(CECDEVICE_TV);
1134 }
1135 }
1136
1137 // retry later
1138 if (bActiveSourceFailed || !bSourceSwitchAllowed)
1139 {
1140 LIB_CEC->AddLog(CEC_LOG_DEBUG, "failed to make '%s' the active source. will retry later", m_busDevice->GetLogicalAddressName());
1141 CLockObject lock(m_mutex);
1142 m_iActiveSourcePending = GetTimeMs() + (int64_t)CEC_ACTIVE_SOURCE_SWITCH_RETRY_TIME_MS;
1143 return false;
1144 }
1145
1146 // mark the handler as initialised
1147 CLockObject lock(m_mutex);
1148 m_bHandlerInited = true;
1149 }
1150 return true;
1151 }
1152
1153 void CCECCommandHandler::SignalOpcode(cec_opcode opcode)
1154 {
1155 m_waitForResponse->Received(opcode);
1156 }