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