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