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