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