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