p8: don't disable controlled mode when switching to monitoring mode
[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)
004b8382 51
060a7b5e
LOK
52CCECCommandHandler::CCECCommandHandler(CCECBusDevice *busDevice,
53 int32_t iTransmitTimeout /* = CEC_DEFAULT_TRANSMIT_TIMEOUT */,
54 int32_t iTransmitWait /* = CEC_DEFAULT_TRANSMIT_WAIT */,
55 int8_t iTransmitRetries /* = CEC_DEFAULT_TRANSMIT_RETRIES */,
56 int64_t iActiveSourcePending /* = 0 */) :
8fa35473
LOK
57 m_busDevice(busDevice),
58 m_processor(m_busDevice->GetProcessor()),
060a7b5e
LOK
59 m_iTransmitTimeout(iTransmitTimeout),
60 m_iTransmitWait(iTransmitWait),
61 m_iTransmitRetries(iTransmitRetries),
b64db02e 62 m_bHandlerInited(false),
d79b67d8 63 m_bOPTSendDeckStatusUpdateOnActiveSource(false),
960f33c6 64 m_vendorId(CEC_VENDOR_UNKNOWN),
060a7b5e 65 m_iActiveSourcePending(iActiveSourcePending)
e9de9629 66{
8fa35473
LOK
67}
68
e9de9629
LOK
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)
060a7b5e 198 m_busDevice->SignalOpcode((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);
2b44051c 272 if (device && device->TransmitCECVersion(command.initiator, true))
9a54dc82
LOK
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));
2b44051c 285 if (device && device->TransmitAudioStatus(command.initiator, true))
9a54dc82
LOK
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));
2b44051c 298 if (device && device->TransmitDeckStatus(command.initiator, true))
9a54dc82
LOK
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);
2b44051c 311 if (device && device->TransmitPowerState(command.initiator, true))
9a54dc82
LOK
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);
2b44051c 324 if (device && device->TransmitVendorID(command.initiator, true, true))
9a54dc82 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);
2b44051c 336 if (device && device->TransmitOSDName(command.initiator, true))
9a54dc82 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);
2b44051c 348 if (device && device->TransmitPhysicalAddress(true))
9a54dc82
LOK
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);
2b44051c 361 if (device && device->TransmitSetMenuLanguage(command.initiator, true))
9a54dc82
LOK
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));
2b44051c 374 if (device && device->TransmitSystemAudioModeStatus(command.initiator, true))
9a54dc82
LOK
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{
2b44051c
LOK
384 CCECBusDevice *device = GetDevice(command.destination);
385 if (device && (device->GetCurrentStatus() == CEC_DEVICE_STATUS_PRESENT ||
386 device->GetCurrentStatus() == CEC_DEVICE_STATUS_HANDLED_BY_LIBCEC))
387 {
388 if (device->GetCurrentPowerStatus() == CEC_POWER_STATUS_STANDBY ||
389 device->GetCurrentPowerStatus() == CEC_POWER_STATUS_IN_TRANSITION_ON_TO_STANDBY)
390 device->SetPowerStatus(CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON);
391 }
9a54dc82 392 return COMMAND_HANDLED;
1a6669b8
LOK
393}
394
9a54dc82 395int CCECCommandHandler::HandleMenuRequest(const cec_command &command)
e9de9629 396{
0b8c7eab 397 if (m_processor->CECInitialised() && m_processor->IsHandledByLibCEC(command.destination))
0f23c85c 398 {
e1804a4e
LOK
399 CCECBusDevice *device = GetDevice(command.destination);
400 if (device)
6b72afcd 401 {
004b8382
LOK
402 CCECClient *client = device->GetClient();
403 if (client)
e1804a4e 404 {
004b8382
LOK
405 if (command.parameters[0] == CEC_MENU_REQUEST_TYPE_ACTIVATE)
406 {
407 if (client->MenuStateChanged(CEC_MENU_STATE_ACTIVATED) == 1)
408 device->SetMenuState(CEC_MENU_STATE_ACTIVATED);
409 }
410 else if (command.parameters[0] == CEC_MENU_REQUEST_TYPE_DEACTIVATE)
411 {
412 if (client->MenuStateChanged(CEC_MENU_STATE_DEACTIVATED) == 1)
413 device->SetMenuState(CEC_MENU_STATE_DEACTIVATED);
414 }
e1804a4e 415 }
2b44051c 416 if (device->TransmitMenuState(command.initiator, true))
9a54dc82 417 return COMMAND_HANDLED;
6b72afcd 418 }
9a54dc82 419 return CEC_ABORT_REASON_INVALID_OPERAND;
15d1a84c
LOK
420 }
421
9a54dc82 422 return CEC_ABORT_REASON_NOT_IN_CORRECT_MODE_TO_RESPOND;
15d1a84c
LOK
423}
424
d297cbd4
LOK
425bool CCECCommandHandler::HandlePoll(const cec_command &command)
426{
a75e3a5a 427 m_busDevice->HandlePoll(command.destination);
d297cbd4
LOK
428 return true;
429}
430
9a54dc82 431int CCECCommandHandler::HandleReportAudioStatus(const cec_command &command)
15d1a84c
LOK
432{
433 if (command.parameters.size == 1)
434 {
aa40e0e2
LOK
435 CCECAudioSystem *device = CCECBusDevice::AsAudioSystem(GetDevice(command.initiator));
436 if (device)
6b72afcd 437 {
aa40e0e2 438 device->SetAudioStatus(command.parameters[0]);
9a54dc82 439 return COMMAND_HANDLED;
6b72afcd 440 }
0f23c85c 441 }
9a54dc82 442 return CEC_ABORT_REASON_INVALID_OPERAND;
e9de9629
LOK
443}
444
9a54dc82 445int CCECCommandHandler::HandleReportPhysicalAddress(const cec_command &command)
907bd60f 446{
0bfce006 447 if (command.parameters.size == 3)
907bd60f
LOK
448 {
449 uint16_t iNewAddress = ((uint16_t)command.parameters[0] << 8) | ((uint16_t)command.parameters[1]);
16b1e052 450 SetPhysicalAddress(command.initiator, iNewAddress);
9a54dc82 451 return COMMAND_HANDLED;
907bd60f 452 }
9a54dc82 453 return CEC_ABORT_REASON_INVALID_OPERAND;
907bd60f
LOK
454}
455
9a54dc82 456int CCECCommandHandler::HandleReportPowerStatus(const cec_command &command)
e55f3f70
LOK
457{
458 if (command.parameters.size == 1)
459 {
460 CCECBusDevice *device = GetDevice(command.initiator);
461 if (device)
9a54dc82 462 {
e55f3f70 463 device->SetPowerStatus((cec_power_status) command.parameters[0]);
9a54dc82
LOK
464 return COMMAND_HANDLED;
465 }
e55f3f70 466 }
9a54dc82 467 return CEC_ABORT_REASON_INVALID_OPERAND;
e55f3f70
LOK
468}
469
9a54dc82 470int CCECCommandHandler::HandleRequestActiveSource(const cec_command &command)
e9de9629 471{
0b8c7eab 472 if (m_processor->CECInitialised())
5f316715 473 {
004b8382
LOK
474 LIB_CEC->AddLog(CEC_LOG_DEBUG, ">> %i requests active source", (uint8_t) command.initiator);
475 m_processor->GetDevice(command.initiator)->SetPowerStatus(CEC_POWER_STATUS_ON);
8747dd4f 476
5f316715 477 vector<CCECBusDevice *> devices;
f00ff009 478 for (size_t iDevicePtr = 0; iDevicePtr < GetMyDevices(devices); iDevicePtr++)
2b44051c 479 devices[iDevicePtr]->TransmitActiveSource(true);
5f316715 480 }
9a54dc82
LOK
481
482 return COMMAND_HANDLED;
e9de9629
LOK
483}
484
9a54dc82 485int CCECCommandHandler::HandleRoutingChange(const cec_command &command)
e9de9629
LOK
486{
487 if (command.parameters.size == 4)
488 {
489 uint16_t iOldAddress = ((uint16_t)command.parameters[0] << 8) | ((uint16_t)command.parameters[1]);
490 uint16_t iNewAddress = ((uint16_t)command.parameters[2] << 8) | ((uint16_t)command.parameters[3]);
e9de9629 491
0f23c85c
LOK
492 CCECBusDevice *device = GetDevice(command.initiator);
493 if (device)
9a54dc82 494 {
9dc04b07 495 device->SetStreamPath(iNewAddress, iOldAddress);
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 {
507 uint16_t iNewAddress = ((uint16_t)command.parameters[0] << 8) | ((uint16_t)command.parameters[1]);
004b8382
LOK
508 CCECBusDevice *device = m_processor->GetDeviceByPhysicalAddress(iNewAddress);
509 if (device)
9a54dc82 510 {
004b8382 511 device->MarkAsActiveSource();
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
571 // a device will only change the stream path when it's powered on
572 m_busDevice->SetPowerStatus(CEC_POWER_STATUS_ON);
72689600
LOK
573
574 /* one of the device handled by libCEC has been made active */
575 CCECBusDevice *device = GetDeviceByPhysicalAddress(iStreamAddress);
004b8382 576 if (device && device->IsHandledByLibCEC())
9a54dc82 577 {
004b8382 578 device->ActivateSource();
9a54dc82
LOK
579 return COMMAND_HANDLED;
580 }
e9de9629 581 }
9a54dc82
LOK
582
583 return CEC_ABORT_REASON_INVALID_OPERAND;
e9de9629
LOK
584}
585
9a54dc82 586int CCECCommandHandler::HandleSystemAudioModeRequest(const cec_command &command)
e5e86c76 587{
0b8c7eab 588 if (m_processor->CECInitialised() && m_processor->IsHandledByLibCEC(command.destination))
e5e86c76 589 {
aa40e0e2
LOK
590 CCECAudioSystem *device = CCECBusDevice::AsAudioSystem(GetDevice(command.destination));
591 if (device)
aa517a0d
LOK
592 {
593 if (command.parameters.size >= 2)
594 {
595 device->SetPowerStatus(CEC_POWER_STATUS_ON);
aa40e0e2 596 device->SetSystemAudioModeStatus(CEC_SYSTEM_AUDIO_STATUS_ON);
aa517a0d
LOK
597 uint16_t iNewAddress = ((uint16_t)command.parameters[0] << 8) | ((uint16_t)command.parameters[1]);
598 CCECBusDevice *newActiveDevice = GetDeviceByPhysicalAddress(iNewAddress);
599 if (newActiveDevice)
004b8382 600 newActiveDevice->MarkAsActiveSource();
2b44051c 601 if (device->TransmitSetSystemAudioMode(command.initiator, true))
9a54dc82 602 return COMMAND_HANDLED;
aa517a0d
LOK
603 }
604 else
605 {
aa40e0e2 606 device->SetSystemAudioModeStatus(CEC_SYSTEM_AUDIO_STATUS_OFF);
2b44051c 607 if (device->TransmitSetSystemAudioMode(command.initiator, true))
9a54dc82 608 return COMMAND_HANDLED;
aa517a0d
LOK
609 }
610 }
e5e86c76 611 }
9a54dc82
LOK
612
613 return CEC_ABORT_REASON_NOT_IN_CORRECT_MODE_TO_RESPOND;
e5e86c76
LOK
614}
615
9a54dc82 616int CCECCommandHandler::HandleStandby(const cec_command &command)
4d6b4433
LOK
617{
618 CCECBusDevice *device = GetDevice(command.initiator);
619 if (device)
620 device->SetPowerStatus(CEC_POWER_STATUS_STANDBY);
6b72afcd 621
9a54dc82 622 return COMMAND_HANDLED;
4d6b4433
LOK
623}
624
9a54dc82 625int CCECCommandHandler::HandleSystemAudioModeStatus(const cec_command &command)
868dc71f 626{
aa517a0d 627 if (command.parameters.size == 1)
868dc71f 628 {
aa40e0e2
LOK
629 CCECAudioSystem *device = CCECBusDevice::AsAudioSystem(GetDevice(command.initiator));
630 if (device)
aa517a0d 631 {
aa40e0e2 632 device->SetSystemAudioModeStatus((cec_system_audio_status)command.parameters[0]);
9a54dc82 633 return COMMAND_HANDLED;
aa517a0d
LOK
634 }
635 }
636
9a54dc82 637 return CEC_ABORT_REASON_INVALID_OPERAND;
aa517a0d
LOK
638}
639
9a54dc82 640int CCECCommandHandler::HandleSetSystemAudioMode(const cec_command &command)
aa517a0d
LOK
641{
642 if (command.parameters.size == 1)
643 {
aa40e0e2
LOK
644 CCECAudioSystem *device = CCECBusDevice::AsAudioSystem(GetDevice(command.initiator));
645 if (device)
aa517a0d 646 {
aa40e0e2 647 device->SetSystemAudioModeStatus((cec_system_audio_status)command.parameters[0]);
9a54dc82 648 return COMMAND_HANDLED;
aa517a0d 649 }
868dc71f
LOK
650 }
651
9a54dc82 652 return CEC_ABORT_REASON_INVALID_OPERAND;
868dc71f
LOK
653}
654
9a54dc82 655int CCECCommandHandler::HandleTextViewOn(const cec_command &command)
cf0ecd85 656{
004b8382 657 m_processor->GetDevice(command.initiator)->MarkAsActiveSource();
9a54dc82 658 return COMMAND_HANDLED;
cf0ecd85
LOK
659}
660
9a54dc82 661int CCECCommandHandler::HandleUserControlPressed(const cec_command &command)
e9de9629 662{
9a54dc82
LOK
663 if (!m_processor->CECInitialised() ||
664 !m_processor->IsHandledByLibCEC(command.destination))
665 return CEC_ABORT_REASON_NOT_IN_CORRECT_MODE_TO_RESPOND;
004b8382 666
9a54dc82
LOK
667 if (command.parameters.size == 0)
668 return CEC_ABORT_REASON_INVALID_OPERAND;
004b8382 669
9a54dc82
LOK
670 CCECBusDevice *device = GetDevice(command.destination);
671 if (!device)
672 return CEC_ABORT_REASON_INVALID_OPERAND;
e33e0d75 673
9a54dc82
LOK
674 CCECClient *client = device->GetClient();
675 if (client)
676 client->AddKey();
e33e0d75 677
9a54dc82
LOK
678 if (command.parameters[0] <= CEC_USER_CONTROL_CODE_MAX)
679 client->SetCurrentButton((cec_user_control_code) command.parameters[0]);
e33e0d75 680
9a54dc82
LOK
681 if (command.parameters[0] == CEC_USER_CONTROL_CODE_POWER ||
682 command.parameters[0] == CEC_USER_CONTROL_CODE_POWER_ON_FUNCTION)
683 {
684 bool bPowerOn(true);
685
686 // CEC_USER_CONTROL_CODE_POWER operates as a toggle
687 // assume CEC_USER_CONTROL_CODE_POWER_ON_FUNCTION does not
688 if (command.parameters[0] == CEC_USER_CONTROL_CODE_POWER)
689 {
690 cec_power_status status = device->GetCurrentPowerStatus();
691 bPowerOn = !(status == CEC_POWER_STATUS_ON || status == CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON);
e9de9629 692 }
e33e0d75 693
9a54dc82
LOK
694 if (bPowerOn)
695 {
696 device->ActivateSource();
697 }
698 else
699 {
700 device->MarkAsInactiveSource();
701 device->TransmitInactiveSource();
702 device->SetMenuState(CEC_MENU_STATE_DEACTIVATED);
703 }
e9de9629 704 }
9a54dc82
LOK
705
706 return COMMAND_HANDLED;
e9de9629
LOK
707}
708
9a54dc82 709int CCECCommandHandler::HandleUserControlRelease(const cec_command &command)
e9de9629 710{
9a54dc82
LOK
711 if (!m_processor->CECInitialised() ||
712 !m_processor->IsHandledByLibCEC(command.destination))
713 return CEC_ABORT_REASON_NOT_IN_CORRECT_MODE_TO_RESPOND;
714
004b8382
LOK
715 CCECClient *client = m_processor->GetClient(command.destination);
716 if (client)
717 client->AddKey();
9a54dc82
LOK
718
719 return COMMAND_HANDLED;
e9de9629
LOK
720}
721
866e7584 722int CCECCommandHandler::HandleVendorCommand(const cec_command & UNUSED(command))
1de6617c 723{
22579015 724 return CEC_ABORT_REASON_INVALID_OPERAND;
1de6617c
LOK
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
2b44051c 821 return Transmit(command, false, false);
8fa35473
LOK
822}
823
824bool CCECCommandHandler::TransmitStandby(const cec_logical_address iInitiator, const cec_logical_address iDestination)
825{
826 cec_command command;
ae693aaa 827 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_STANDBY);
8fa35473 828
2b44051c 829 return Transmit(command, false, false);
8fa35473
LOK
830}
831
5734016c
LOK
832bool CCECCommandHandler::TransmitRequestActiveSource(const cec_logical_address iInitiator, bool bWaitForResponse /* = true */)
833{
834 cec_command command;
835 cec_command::Format(command, iInitiator, CECDEVICE_BROADCAST, CEC_OPCODE_REQUEST_ACTIVE_SOURCE);
836
2b44051c 837 return Transmit(command, !bWaitForResponse, false);
5734016c
LOK
838}
839
a75e3a5a 840bool CCECCommandHandler::TransmitRequestCecVersion(const cec_logical_address iInitiator, const cec_logical_address iDestination, bool bWaitForResponse /* = true */)
8fa35473
LOK
841{
842 cec_command command;
ae693aaa 843 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_GET_CEC_VERSION);
8fa35473 844
2b44051c 845 return Transmit(command, !bWaitForResponse, false);
8fa35473
LOK
846}
847
a75e3a5a 848bool CCECCommandHandler::TransmitRequestMenuLanguage(const cec_logical_address iInitiator, const cec_logical_address iDestination, bool bWaitForResponse /* = true */)
8fa35473
LOK
849{
850 cec_command command;
ae693aaa 851 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_GET_MENU_LANGUAGE);
8fa35473 852
2b44051c 853 return Transmit(command, !bWaitForResponse, false);
8fa35473
LOK
854}
855
a75e3a5a 856bool CCECCommandHandler::TransmitRequestOSDName(const cec_logical_address iInitiator, const cec_logical_address iDestination, bool bWaitForResponse /* = true */)
8fa35473
LOK
857{
858 cec_command command;
ae693aaa 859 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_GIVE_OSD_NAME);
8fa35473 860
2b44051c 861 return Transmit(command, !bWaitForResponse, false);
8fa35473
LOK
862}
863
a75e3a5a 864bool CCECCommandHandler::TransmitRequestPhysicalAddress(const cec_logical_address iInitiator, const cec_logical_address iDestination, bool bWaitForResponse /* = true */)
8fa35473
LOK
865{
866 cec_command command;
ae693aaa 867 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_GIVE_PHYSICAL_ADDRESS);
8fa35473 868
2b44051c 869 return Transmit(command, !bWaitForResponse, false);
8fa35473
LOK
870}
871
a75e3a5a 872bool CCECCommandHandler::TransmitRequestPowerStatus(const cec_logical_address iInitiator, const cec_logical_address iDestination, bool bWaitForResponse /* = true */)
8fa35473
LOK
873{
874 cec_command command;
ae693aaa 875 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_GIVE_DEVICE_POWER_STATUS);
8fa35473 876
2b44051c 877 return Transmit(command, !bWaitForResponse, false);
8fa35473
LOK
878}
879
a75e3a5a 880bool CCECCommandHandler::TransmitRequestVendorId(const cec_logical_address iInitiator, const cec_logical_address iDestination, bool bWaitForResponse /* = true */)
8fa35473
LOK
881{
882 cec_command command;
ae693aaa 883 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_GIVE_DEVICE_VENDOR_ID);
8fa35473 884
2b44051c 885 return Transmit(command, !bWaitForResponse, false);
8fa35473
LOK
886}
887
2b44051c 888bool CCECCommandHandler::TransmitActiveSource(const cec_logical_address iInitiator, uint16_t iPhysicalAddress, bool bIsReply)
8fa35473
LOK
889{
890 cec_command command;
ae693aaa 891 cec_command::Format(command, iInitiator, CECDEVICE_BROADCAST, CEC_OPCODE_ACTIVE_SOURCE);
8fa35473
LOK
892 command.parameters.PushBack((uint8_t) ((iPhysicalAddress >> 8) & 0xFF));
893 command.parameters.PushBack((uint8_t) (iPhysicalAddress & 0xFF));
894
2b44051c 895 return Transmit(command, false, bIsReply);
8fa35473
LOK
896}
897
2b44051c 898bool CCECCommandHandler::TransmitCECVersion(const cec_logical_address iInitiator, const cec_logical_address iDestination, cec_version cecVersion, bool bIsReply)
8fa35473
LOK
899{
900 cec_command command;
ae693aaa 901 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_CEC_VERSION);
8fa35473
LOK
902 command.parameters.PushBack((uint8_t)cecVersion);
903
2b44051c 904 return Transmit(command, false, bIsReply);
8fa35473
LOK
905}
906
907bool CCECCommandHandler::TransmitInactiveSource(const cec_logical_address iInitiator, uint16_t iPhysicalAddress)
908{
909 cec_command command;
ae693aaa 910 cec_command::Format(command, iInitiator, CECDEVICE_TV, CEC_OPCODE_INACTIVE_SOURCE);
8fa35473
LOK
911 command.parameters.PushBack((iPhysicalAddress >> 8) & 0xFF);
912 command.parameters.PushBack(iPhysicalAddress & 0xFF);
913
2b44051c 914 return Transmit(command, false, false);
8fa35473
LOK
915}
916
2b44051c 917bool CCECCommandHandler::TransmitMenuState(const cec_logical_address iInitiator, const cec_logical_address iDestination, cec_menu_state menuState, bool bIsReply)
8fa35473
LOK
918{
919 cec_command command;
ae693aaa 920 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_MENU_STATUS);
8fa35473
LOK
921 command.parameters.PushBack((uint8_t)menuState);
922
2b44051c 923 return Transmit(command, false, bIsReply);
8fa35473
LOK
924}
925
2b44051c 926bool CCECCommandHandler::TransmitOSDName(const cec_logical_address iInitiator, const cec_logical_address iDestination, std::string strDeviceName, bool bIsReply)
8fa35473
LOK
927{
928 cec_command command;
ae693aaa 929 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_SET_OSD_NAME);
6f14b512 930 for (size_t iPtr = 0; iPtr < strDeviceName.length(); iPtr++)
8fa35473
LOK
931 command.parameters.PushBack(strDeviceName.at(iPtr));
932
2b44051c 933 return Transmit(command, false, bIsReply);
8fa35473
LOK
934}
935
2b44051c 936bool CCECCommandHandler::TransmitOSDString(const cec_logical_address iInitiator, const cec_logical_address iDestination, cec_display_control duration, const char *strMessage, bool bIsReply)
8fa35473
LOK
937{
938 cec_command command;
ae693aaa 939 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_SET_OSD_STRING);
8fa35473
LOK
940 command.parameters.PushBack((uint8_t)duration);
941
6f14b512 942 size_t iLen = strlen(strMessage);
8fa35473
LOK
943 if (iLen > 13) iLen = 13;
944
6f14b512 945 for (size_t iPtr = 0; iPtr < iLen; iPtr++)
8fa35473
LOK
946 command.parameters.PushBack(strMessage[iPtr]);
947
2b44051c 948 return Transmit(command, false, bIsReply);
8fa35473
LOK
949}
950
2b44051c 951bool CCECCommandHandler::TransmitPhysicalAddress(const cec_logical_address iInitiator, uint16_t iPhysicalAddress, cec_device_type type, bool bIsReply)
8fa35473
LOK
952{
953 cec_command command;
ae693aaa 954 cec_command::Format(command, iInitiator, CECDEVICE_BROADCAST, CEC_OPCODE_REPORT_PHYSICAL_ADDRESS);
8fa35473
LOK
955 command.parameters.PushBack((uint8_t) ((iPhysicalAddress >> 8) & 0xFF));
956 command.parameters.PushBack((uint8_t) (iPhysicalAddress & 0xFF));
957 command.parameters.PushBack((uint8_t) (type));
958
2b44051c 959 return Transmit(command, false, bIsReply);
fbdea54c
MK
960}
961
2b44051c 962bool CCECCommandHandler::TransmitSetMenuLanguage(const cec_logical_address iInitiator, const char lang[3], bool bIsReply)
fbdea54c
MK
963{
964 cec_command command;
965 command.Format(command, iInitiator, CECDEVICE_BROADCAST, CEC_OPCODE_SET_MENU_LANGUAGE);
966 command.parameters.PushBack((uint8_t) lang[0]);
967 command.parameters.PushBack((uint8_t) lang[1]);
968 command.parameters.PushBack((uint8_t) lang[2]);
969
2b44051c 970 return Transmit(command, false, bIsReply);
8fa35473
LOK
971}
972
2b44051c 973bool CCECCommandHandler::TransmitPoll(const cec_logical_address iInitiator, const cec_logical_address iDestination, bool bIsReply)
8fa35473
LOK
974{
975 cec_command command;
ae693aaa 976 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_NONE);
8fa35473 977
2b44051c 978 return Transmit(command, false, bIsReply);
8fa35473
LOK
979}
980
2b44051c 981bool CCECCommandHandler::TransmitPowerState(const cec_logical_address iInitiator, const cec_logical_address iDestination, cec_power_status state, bool bIsReply)
8fa35473
LOK
982{
983 cec_command command;
ae693aaa 984 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_REPORT_POWER_STATUS);
8fa35473
LOK
985 command.parameters.PushBack((uint8_t) state);
986
2b44051c 987 return Transmit(command, false, bIsReply);
8fa35473
LOK
988}
989
2b44051c 990bool CCECCommandHandler::TransmitVendorID(const cec_logical_address iInitiator, uint64_t iVendorId, bool bIsReply)
8fa35473
LOK
991{
992 cec_command command;
ae693aaa 993 cec_command::Format(command, iInitiator, CECDEVICE_BROADCAST, CEC_OPCODE_DEVICE_VENDOR_ID);
8fa35473
LOK
994
995 command.parameters.PushBack((uint8_t) (((uint64_t)iVendorId >> 16) & 0xFF));
996 command.parameters.PushBack((uint8_t) (((uint64_t)iVendorId >> 8) & 0xFF));
997 command.parameters.PushBack((uint8_t) ((uint64_t)iVendorId & 0xFF));
998
2b44051c 999 return Transmit(command, false, bIsReply);
8fa35473
LOK
1000}
1001
2b44051c 1002bool CCECCommandHandler::TransmitAudioStatus(const cec_logical_address iInitiator, const cec_logical_address iDestination, uint8_t state, bool bIsReply)
8fa35473
LOK
1003{
1004 cec_command command;
ae693aaa 1005 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_REPORT_AUDIO_STATUS);
8fa35473
LOK
1006 command.parameters.PushBack(state);
1007
2b44051c 1008 return Transmit(command, false, bIsReply);
8fa35473
LOK
1009}
1010
2b44051c 1011bool CCECCommandHandler::TransmitSetSystemAudioMode(const cec_logical_address iInitiator, const cec_logical_address iDestination, cec_system_audio_status state, bool bIsReply)
8fa35473
LOK
1012{
1013 cec_command command;
ae693aaa 1014 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_SET_SYSTEM_AUDIO_MODE);
8fa35473
LOK
1015 command.parameters.PushBack((uint8_t)state);
1016
2b44051c 1017 return Transmit(command, false, bIsReply);
8fa35473
LOK
1018}
1019
2b44051c 1020bool CCECCommandHandler::TransmitSetStreamPath(uint16_t iStreamPath, bool bIsReply)
f42d3e0f 1021{
2b44051c
LOK
1022 if (m_busDevice->GetLogicalAddress() != CECDEVICE_TV)
1023 {
1024 LIB_CEC->AddLog(CEC_LOG_ERROR, "only the TV is allowed to send CEC_OPCODE_SET_STREAM_PATH");
1025 return false;
1026 }
f42d3e0f
LOK
1027 cec_command command;
1028 cec_command::Format(command, m_busDevice->GetLogicalAddress(), CECDEVICE_BROADCAST, CEC_OPCODE_SET_STREAM_PATH);
1029 command.parameters.PushBack((uint8_t) ((iStreamPath >> 8) & 0xFF));
1030 command.parameters.PushBack((uint8_t) (iStreamPath & 0xFF));
1031
2b44051c 1032 return Transmit(command, false, bIsReply);
f42d3e0f
LOK
1033}
1034
2b44051c 1035bool CCECCommandHandler::TransmitSystemAudioModeStatus(const cec_logical_address iInitiator, const cec_logical_address iDestination, cec_system_audio_status state, bool bIsReply)
8fa35473
LOK
1036{
1037 cec_command command;
ae693aaa 1038 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_SYSTEM_AUDIO_MODE_STATUS);
8fa35473
LOK
1039 command.parameters.PushBack((uint8_t)state);
1040
2b44051c 1041 return Transmit(command, false, bIsReply);
8fa35473
LOK
1042}
1043
2b44051c 1044bool CCECCommandHandler::TransmitDeckStatus(const cec_logical_address iInitiator, const cec_logical_address iDestination, cec_deck_info state, bool bIsReply)
8fa35473
LOK
1045{
1046 cec_command command;
ae693aaa 1047 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_DECK_STATUS);
8fa35473
LOK
1048 command.PushBack((uint8_t)state);
1049
2b44051c 1050 return Transmit(command, false, bIsReply);
8fa35473
LOK
1051}
1052
4bec9d79 1053bool CCECCommandHandler::TransmitKeypress(const cec_logical_address iInitiator, const cec_logical_address iDestination, cec_user_control_code key, bool bWait /* = true */)
8fa35473
LOK
1054{
1055 cec_command command;
ae693aaa 1056 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_USER_CONTROL_PRESSED);
8fa35473
LOK
1057 command.parameters.PushBack((uint8_t)key);
1058
2b44051c 1059 return Transmit(command, !bWait, false);
8fa35473
LOK
1060}
1061
4bec9d79 1062bool CCECCommandHandler::TransmitKeyRelease(const cec_logical_address iInitiator, const cec_logical_address iDestination, bool bWait /* = true */)
8fa35473
LOK
1063{
1064 cec_command command;
ae693aaa 1065 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_USER_CONTROL_RELEASE);
8fa35473 1066
2b44051c 1067 return Transmit(command, !bWait, false);
8fa35473
LOK
1068}
1069
2b44051c 1070bool CCECCommandHandler::Transmit(cec_command &command, bool bSuppressWait, bool bIsReply)
8fa35473 1071{
b64db02e 1072 bool bReturn(false);
4fde2fd0
LOK
1073 cec_opcode expectedResponse(cec_command::GetResponseOpcode(command.opcode));
1074 bool bExpectResponse(expectedResponse != CEC_OPCODE_NONE && !bSuppressWait);
ae693aaa 1075 command.transmit_timeout = m_iTransmitTimeout;
ae693aaa 1076
7e4558f1
LOK
1077 if (command.initiator == CECDEVICE_UNKNOWN)
1078 {
004b8382 1079 LIB_CEC->AddLog(CEC_LOG_ERROR, "not transmitting a command without a valid initiator");
7e4558f1
LOK
1080 return bReturn;
1081 }
1082
ae693aaa 1083 {
6c0ccaa5 1084 uint8_t iTries(0), iMaxTries(!command.opcode_set ? 1 : m_iTransmitRetries + 1);
ec15344c 1085 while (!bReturn && ++iTries <= iMaxTries && !m_busDevice->IsUnsupportedFeature(command.opcode))
b64db02e 1086 {
2b44051c 1087 if ((bReturn = m_processor->Transmit(command, bIsReply)) == true)
19cbfa8f 1088 {
004b8382 1089 LIB_CEC->AddLog(CEC_LOG_DEBUG, "command transmitted");
4478bc79 1090 if (bExpectResponse)
24dd566c 1091 {
060a7b5e 1092 bReturn = m_busDevice->WaitForOpcode(expectedResponse);
004b8382 1093 LIB_CEC->AddLog(CEC_LOG_DEBUG, bReturn ? "expected response received (%X: %s)" : "expected response not received (%X: %s)", (int)expectedResponse, ToString(expectedResponse));
24dd566c 1094 }
19cbfa8f 1095 }
b64db02e 1096 }
ae693aaa 1097 }
8fa35473 1098
b64db02e 1099 return bReturn;
8fa35473 1100}
83be0701 1101
aa4c0d34 1102bool CCECCommandHandler::ActivateSource(bool bTransmitDelayedCommandsOnly /* = false */)
83be0701 1103{
c4287bcd 1104 if (m_busDevice->IsActiveSource() &&
aa4c0d34 1105 m_busDevice->IsHandledByLibCEC())
c4287bcd 1106 {
b0015449
LOK
1107 {
1108 CLockObject lock(m_mutex);
aa4c0d34
LOK
1109 // check if we need to send a delayed source switch
1110 if (bTransmitDelayedCommandsOnly)
1111 {
1112 if (m_iActiveSourcePending == 0 || GetTimeMs() < m_iActiveSourcePending)
1113 return false;
1114
1115 LIB_CEC->AddLog(CEC_LOG_DEBUG, "transmitting delayed activate source command");
1116 }
1117
1118 // clear previous pending active source command
1119 m_iActiveSourcePending = 0;
b0015449
LOK
1120 }
1121
aa4c0d34 1122 // update the power state and menu state
c4287bcd 1123 m_busDevice->SetPowerStatus(CEC_POWER_STATUS_ON);
7b01619d
LOK
1124 m_busDevice->SetMenuState(CEC_MENU_STATE_ACTIVATED);
1125
1126 // vendor specific hook
1127 VendorPreActivateSourceHook();
aa4c0d34
LOK
1128
1129 // power on the TV
7b01619d
LOK
1130 bool bActiveSourceFailed(false);
1131 if (m_processor->GetDevice(CECDEVICE_TV)->GetPowerStatus(m_busDevice->GetLogicalAddress()) != CEC_POWER_STATUS_ON)
1132 bActiveSourceFailed = !m_busDevice->TransmitImageViewOn();
c4287bcd 1133
aa4c0d34
LOK
1134 // check if we're allowed to switch sources
1135 bool bSourceSwitchAllowed = SourceSwitchAllowed();
1136 if (!bSourceSwitchAllowed)
1137 LIB_CEC->AddLog(CEC_LOG_DEBUG, "source switch is currently not allowed by command handler");
dbad810a 1138
aa4c0d34
LOK
1139 // switch sources (if allowed)
1140 if (!bActiveSourceFailed && bSourceSwitchAllowed)
b0015449 1141 {
2b44051c
LOK
1142 bActiveSourceFailed = !m_busDevice->TransmitActiveSource(false) ||
1143 !m_busDevice->TransmitMenuState(CECDEVICE_TV, false);
aa4c0d34
LOK
1144
1145 // update the deck status for playback devices
1146 if (!bActiveSourceFailed)
1147 {
1148 CCECPlaybackDevice *playbackDevice = m_busDevice->AsPlaybackDevice();
1149 if (playbackDevice && SendDeckStatusUpdateOnActiveSource())
2b44051c 1150 bActiveSourceFailed = !playbackDevice->TransmitDeckStatus(CECDEVICE_TV, false);
aa4c0d34 1151 }
dbad810a
LOK
1152 }
1153
aa4c0d34
LOK
1154 // retry later
1155 if (bActiveSourceFailed || !bSourceSwitchAllowed)
dbad810a
LOK
1156 {
1157 LIB_CEC->AddLog(CEC_LOG_DEBUG, "failed to make '%s' the active source. will retry later", m_busDevice->GetLogicalAddressName());
b0015449 1158 CLockObject lock(m_mutex);
29c4a2d1 1159 m_iActiveSourcePending = GetTimeMs() + (int64_t)CEC_ACTIVE_SOURCE_SWITCH_RETRY_TIME_MS;
b0015449
LOK
1160 return false;
1161 }
aa40e0e2 1162
aa4c0d34
LOK
1163 // mark the handler as initialised
1164 CLockObject lock(m_mutex);
c4287bcd 1165 m_bHandlerInited = true;
83be0701 1166 }
7dc58c9f 1167 return true;
83be0701 1168}
b499cf16 1169
060a7b5e 1170void CCECCommandHandler::ScheduleActivateSource(uint64_t iDelay)
b499cf16 1171{
060a7b5e
LOK
1172 CLockObject lock(m_mutex);
1173 m_iActiveSourcePending = GetTimeMs() + iDelay;
b499cf16 1174}