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