Merge remote-tracking branch 'warped-rudi/cubox-stable' into development. Closes #50
[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)
9a54dc82 676 client->SetCurrentButton((cec_user_control_code) command.parameters[0]);
e33e0d75 677
9a54dc82
LOK
678 if (command.parameters[0] == CEC_USER_CONTROL_CODE_POWER ||
679 command.parameters[0] == CEC_USER_CONTROL_CODE_POWER_ON_FUNCTION)
680 {
681 bool bPowerOn(true);
682
683 // CEC_USER_CONTROL_CODE_POWER operates as a toggle
684 // assume CEC_USER_CONTROL_CODE_POWER_ON_FUNCTION does not
685 if (command.parameters[0] == CEC_USER_CONTROL_CODE_POWER)
686 {
687 cec_power_status status = device->GetCurrentPowerStatus();
688 bPowerOn = !(status == CEC_POWER_STATUS_ON || status == CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON);
e9de9629 689 }
e33e0d75 690
9a54dc82
LOK
691 if (bPowerOn)
692 {
693 device->ActivateSource();
694 }
695 else
696 {
697 device->MarkAsInactiveSource();
698 device->TransmitInactiveSource();
699 device->SetMenuState(CEC_MENU_STATE_DEACTIVATED);
700 }
e9de9629 701 }
9a54dc82
LOK
702
703 return COMMAND_HANDLED;
e9de9629
LOK
704}
705
9a54dc82 706int CCECCommandHandler::HandleUserControlRelease(const cec_command &command)
e9de9629 707{
9a54dc82
LOK
708 if (!m_processor->CECInitialised() ||
709 !m_processor->IsHandledByLibCEC(command.destination))
710 return CEC_ABORT_REASON_NOT_IN_CORRECT_MODE_TO_RESPOND;
711
004b8382
LOK
712 CCECClient *client = m_processor->GetClient(command.destination);
713 if (client)
714 client->AddKey();
9a54dc82
LOK
715
716 return COMMAND_HANDLED;
e9de9629
LOK
717}
718
866e7584 719int CCECCommandHandler::HandleVendorCommand(const cec_command & UNUSED(command))
1de6617c 720{
22579015 721 return CEC_ABORT_REASON_INVALID_OPERAND;
1de6617c
LOK
722}
723
9a54dc82 724void CCECCommandHandler::UnhandledCommand(const cec_command &command, const cec_abort_reason reason)
e9de9629 725{
9a54dc82
LOK
726 if (m_processor->IsHandledByLibCEC(command.destination))
727 {
728 LIB_CEC->AddLog(CEC_LOG_DEBUG, "sending abort with opcode %02x and reason '%s' to %s", command.opcode, ToString(reason), ToString(command.initiator));
729 m_processor->TransmitAbort(command.destination, command.initiator, command.opcode, reason);
730 }
0f23c85c
LOK
731}
732
6f14b512 733size_t CCECCommandHandler::GetMyDevices(vector<CCECBusDevice *> &devices) const
8747dd4f 734{
6f14b512 735 size_t iReturn(0);
8747dd4f 736
fcf10e27 737 cec_logical_addresses addresses = m_processor->GetLogicalAddresses();
d2d1660c 738 for (uint8_t iPtr = CECDEVICE_TV; iPtr < CECDEVICE_BROADCAST; iPtr++)
8747dd4f
LOK
739 {
740 if (addresses[iPtr])
741 {
742 devices.push_back(GetDevice((cec_logical_address) iPtr));
743 ++iReturn;
744 }
745 }
746
747 return iReturn;
748}
749
0f23c85c
LOK
750CCECBusDevice *CCECCommandHandler::GetDevice(cec_logical_address iLogicalAddress) const
751{
004b8382 752 return m_processor->GetDevice(iLogicalAddress);
e9de9629 753}
6685ae07
LOK
754
755CCECBusDevice *CCECCommandHandler::GetDeviceByPhysicalAddress(uint16_t iPhysicalAddress) const
756{
fcf10e27 757 return m_processor->GetDeviceByPhysicalAddress(iPhysicalAddress);
6685ae07 758}
181b3475 759
8fa35473 760bool CCECCommandHandler::SetVendorId(const cec_command &command)
181b3475 761{
8fa35473 762 bool bChanged(false);
181b3475
LOK
763 if (command.parameters.size < 3)
764 {
004b8382 765 LIB_CEC->AddLog(CEC_LOG_WARNING, "invalid vendor ID received");
8fa35473 766 return bChanged;
181b3475
LOK
767 }
768
bae71306
LOK
769 uint64_t iVendorId = ((uint64_t)command.parameters[0] << 16) +
770 ((uint64_t)command.parameters[1] << 8) +
181b3475
LOK
771 (uint64_t)command.parameters[2];
772
773 CCECBusDevice *device = GetDevice((cec_logical_address) command.initiator);
774 if (device)
8fa35473
LOK
775 bChanged = device->SetVendorId(iVendorId);
776 return bChanged;
181b3475 777}
5e822b09 778
16b1e052
LOK
779void CCECCommandHandler::SetPhysicalAddress(cec_logical_address iAddress, uint16_t iNewAddress)
780{
004b8382 781 if (!m_processor->IsHandledByLibCEC(iAddress))
16b1e052 782 {
c0152c09
LOK
783 CCECBusDevice *otherDevice = m_processor->GetDeviceByPhysicalAddress(iNewAddress);
784 CCECClient *client = otherDevice ? otherDevice->GetClient() : NULL;
004b8382 785
c0152c09
LOK
786 CCECBusDevice *device = m_processor->GetDevice(iAddress);
787 if (device)
788 device->SetPhysicalAddress(iNewAddress);
789 else
16b1e052 790 {
c0152c09 791 LIB_CEC->AddLog(CEC_LOG_DEBUG, "device with logical address %X not found", iAddress);
16b1e052 792 }
c0152c09
LOK
793
794 /* another device reported the same physical address as ours */
795 if (client)
796 client->ResetPhysicalAddress();
797 }
798 else
799 {
800 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
801 }
802}
855a3a98 803
f4698390
LOK
804bool CCECCommandHandler::PowerOn(const cec_logical_address iInitiator, const cec_logical_address iDestination)
805{
806 if (iDestination == CECDEVICE_TV)
807 return TransmitImageViewOn(iInitiator, iDestination);
808
809 return TransmitKeypress(iInitiator, iDestination, CEC_USER_CONTROL_CODE_POWER) &&
810 TransmitKeyRelease(iInitiator, iDestination);
811}
812
b64db02e 813bool CCECCommandHandler::TransmitImageViewOn(const cec_logical_address iInitiator, const cec_logical_address iDestination)
8fa35473
LOK
814{
815 cec_command command;
ae693aaa 816 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_IMAGE_VIEW_ON);
8fa35473 817
2b44051c 818 return Transmit(command, false, false);
8fa35473
LOK
819}
820
821bool CCECCommandHandler::TransmitStandby(const cec_logical_address iInitiator, const cec_logical_address iDestination)
822{
823 cec_command command;
ae693aaa 824 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_STANDBY);
8fa35473 825
2b44051c 826 return Transmit(command, false, false);
8fa35473
LOK
827}
828
5734016c
LOK
829bool CCECCommandHandler::TransmitRequestActiveSource(const cec_logical_address iInitiator, bool bWaitForResponse /* = true */)
830{
831 cec_command command;
832 cec_command::Format(command, iInitiator, CECDEVICE_BROADCAST, CEC_OPCODE_REQUEST_ACTIVE_SOURCE);
833
2b44051c 834 return Transmit(command, !bWaitForResponse, false);
5734016c
LOK
835}
836
a75e3a5a 837bool CCECCommandHandler::TransmitRequestCecVersion(const cec_logical_address iInitiator, const cec_logical_address iDestination, bool bWaitForResponse /* = true */)
8fa35473
LOK
838{
839 cec_command command;
ae693aaa 840 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_GET_CEC_VERSION);
8fa35473 841
2b44051c 842 return Transmit(command, !bWaitForResponse, false);
8fa35473
LOK
843}
844
a75e3a5a 845bool CCECCommandHandler::TransmitRequestMenuLanguage(const cec_logical_address iInitiator, const cec_logical_address iDestination, bool bWaitForResponse /* = true */)
8fa35473
LOK
846{
847 cec_command command;
ae693aaa 848 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_GET_MENU_LANGUAGE);
8fa35473 849
2b44051c 850 return Transmit(command, !bWaitForResponse, false);
8fa35473
LOK
851}
852
a75e3a5a 853bool CCECCommandHandler::TransmitRequestOSDName(const cec_logical_address iInitiator, const cec_logical_address iDestination, bool bWaitForResponse /* = true */)
8fa35473
LOK
854{
855 cec_command command;
ae693aaa 856 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_GIVE_OSD_NAME);
8fa35473 857
2b44051c 858 return Transmit(command, !bWaitForResponse, false);
8fa35473
LOK
859}
860
a75e3a5a 861bool CCECCommandHandler::TransmitRequestPhysicalAddress(const cec_logical_address iInitiator, const cec_logical_address iDestination, bool bWaitForResponse /* = true */)
8fa35473
LOK
862{
863 cec_command command;
ae693aaa 864 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_GIVE_PHYSICAL_ADDRESS);
8fa35473 865
2b44051c 866 return Transmit(command, !bWaitForResponse, false);
8fa35473
LOK
867}
868
a75e3a5a 869bool CCECCommandHandler::TransmitRequestPowerStatus(const cec_logical_address iInitiator, const cec_logical_address iDestination, bool bWaitForResponse /* = true */)
8fa35473
LOK
870{
871 cec_command command;
ae693aaa 872 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_GIVE_DEVICE_POWER_STATUS);
8fa35473 873
2b44051c 874 return Transmit(command, !bWaitForResponse, false);
8fa35473
LOK
875}
876
a75e3a5a 877bool CCECCommandHandler::TransmitRequestVendorId(const cec_logical_address iInitiator, const cec_logical_address iDestination, bool bWaitForResponse /* = true */)
8fa35473
LOK
878{
879 cec_command command;
ae693aaa 880 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_GIVE_DEVICE_VENDOR_ID);
8fa35473 881
2b44051c 882 return Transmit(command, !bWaitForResponse, false);
8fa35473
LOK
883}
884
2b44051c 885bool CCECCommandHandler::TransmitActiveSource(const cec_logical_address iInitiator, uint16_t iPhysicalAddress, bool bIsReply)
8fa35473
LOK
886{
887 cec_command command;
ae693aaa 888 cec_command::Format(command, iInitiator, CECDEVICE_BROADCAST, CEC_OPCODE_ACTIVE_SOURCE);
8fa35473
LOK
889 command.parameters.PushBack((uint8_t) ((iPhysicalAddress >> 8) & 0xFF));
890 command.parameters.PushBack((uint8_t) (iPhysicalAddress & 0xFF));
891
2b44051c 892 return Transmit(command, false, bIsReply);
8fa35473
LOK
893}
894
2b44051c 895bool CCECCommandHandler::TransmitCECVersion(const cec_logical_address iInitiator, const cec_logical_address iDestination, cec_version cecVersion, bool bIsReply)
8fa35473
LOK
896{
897 cec_command command;
ae693aaa 898 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_CEC_VERSION);
8fa35473
LOK
899 command.parameters.PushBack((uint8_t)cecVersion);
900
2b44051c 901 return Transmit(command, false, bIsReply);
8fa35473
LOK
902}
903
904bool CCECCommandHandler::TransmitInactiveSource(const cec_logical_address iInitiator, uint16_t iPhysicalAddress)
905{
906 cec_command command;
ae693aaa 907 cec_command::Format(command, iInitiator, CECDEVICE_TV, CEC_OPCODE_INACTIVE_SOURCE);
8fa35473
LOK
908 command.parameters.PushBack((iPhysicalAddress >> 8) & 0xFF);
909 command.parameters.PushBack(iPhysicalAddress & 0xFF);
910
2b44051c 911 return Transmit(command, false, false);
8fa35473
LOK
912}
913
2b44051c 914bool CCECCommandHandler::TransmitMenuState(const cec_logical_address iInitiator, const cec_logical_address iDestination, cec_menu_state menuState, bool bIsReply)
8fa35473
LOK
915{
916 cec_command command;
ae693aaa 917 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_MENU_STATUS);
8fa35473
LOK
918 command.parameters.PushBack((uint8_t)menuState);
919
2b44051c 920 return Transmit(command, false, bIsReply);
8fa35473
LOK
921}
922
2b44051c 923bool CCECCommandHandler::TransmitOSDName(const cec_logical_address iInitiator, const cec_logical_address iDestination, std::string strDeviceName, bool bIsReply)
8fa35473
LOK
924{
925 cec_command command;
ae693aaa 926 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_SET_OSD_NAME);
6f14b512 927 for (size_t iPtr = 0; iPtr < strDeviceName.length(); iPtr++)
8fa35473
LOK
928 command.parameters.PushBack(strDeviceName.at(iPtr));
929
2b44051c 930 return Transmit(command, false, bIsReply);
8fa35473
LOK
931}
932
2b44051c 933bool CCECCommandHandler::TransmitOSDString(const cec_logical_address iInitiator, const cec_logical_address iDestination, cec_display_control duration, const char *strMessage, bool bIsReply)
8fa35473
LOK
934{
935 cec_command command;
ae693aaa 936 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_SET_OSD_STRING);
8fa35473
LOK
937 command.parameters.PushBack((uint8_t)duration);
938
6f14b512 939 size_t iLen = strlen(strMessage);
8fa35473
LOK
940 if (iLen > 13) iLen = 13;
941
6f14b512 942 for (size_t iPtr = 0; iPtr < iLen; iPtr++)
8fa35473
LOK
943 command.parameters.PushBack(strMessage[iPtr]);
944
2b44051c 945 return Transmit(command, false, bIsReply);
8fa35473
LOK
946}
947
2b44051c 948bool CCECCommandHandler::TransmitPhysicalAddress(const cec_logical_address iInitiator, uint16_t iPhysicalAddress, cec_device_type type, bool bIsReply)
8fa35473
LOK
949{
950 cec_command command;
ae693aaa 951 cec_command::Format(command, iInitiator, CECDEVICE_BROADCAST, CEC_OPCODE_REPORT_PHYSICAL_ADDRESS);
8fa35473
LOK
952 command.parameters.PushBack((uint8_t) ((iPhysicalAddress >> 8) & 0xFF));
953 command.parameters.PushBack((uint8_t) (iPhysicalAddress & 0xFF));
954 command.parameters.PushBack((uint8_t) (type));
955
2b44051c 956 return Transmit(command, false, bIsReply);
fbdea54c
MK
957}
958
2b44051c 959bool CCECCommandHandler::TransmitSetMenuLanguage(const cec_logical_address iInitiator, const char lang[3], bool bIsReply)
fbdea54c
MK
960{
961 cec_command command;
962 command.Format(command, iInitiator, CECDEVICE_BROADCAST, CEC_OPCODE_SET_MENU_LANGUAGE);
963 command.parameters.PushBack((uint8_t) lang[0]);
964 command.parameters.PushBack((uint8_t) lang[1]);
965 command.parameters.PushBack((uint8_t) lang[2]);
966
2b44051c 967 return Transmit(command, false, bIsReply);
8fa35473
LOK
968}
969
2b44051c 970bool CCECCommandHandler::TransmitPoll(const cec_logical_address iInitiator, const cec_logical_address iDestination, bool bIsReply)
8fa35473
LOK
971{
972 cec_command command;
ae693aaa 973 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_NONE);
8fa35473 974
2b44051c 975 return Transmit(command, false, bIsReply);
8fa35473
LOK
976}
977
2b44051c 978bool CCECCommandHandler::TransmitPowerState(const cec_logical_address iInitiator, const cec_logical_address iDestination, cec_power_status state, bool bIsReply)
8fa35473
LOK
979{
980 cec_command command;
ae693aaa 981 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_REPORT_POWER_STATUS);
8fa35473
LOK
982 command.parameters.PushBack((uint8_t) state);
983
2b44051c 984 return Transmit(command, false, bIsReply);
8fa35473
LOK
985}
986
2b44051c 987bool CCECCommandHandler::TransmitVendorID(const cec_logical_address iInitiator, uint64_t iVendorId, bool bIsReply)
8fa35473
LOK
988{
989 cec_command command;
ae693aaa 990 cec_command::Format(command, iInitiator, CECDEVICE_BROADCAST, CEC_OPCODE_DEVICE_VENDOR_ID);
8fa35473
LOK
991
992 command.parameters.PushBack((uint8_t) (((uint64_t)iVendorId >> 16) & 0xFF));
993 command.parameters.PushBack((uint8_t) (((uint64_t)iVendorId >> 8) & 0xFF));
994 command.parameters.PushBack((uint8_t) ((uint64_t)iVendorId & 0xFF));
995
2b44051c 996 return Transmit(command, false, bIsReply);
8fa35473
LOK
997}
998
2b44051c 999bool CCECCommandHandler::TransmitAudioStatus(const cec_logical_address iInitiator, const cec_logical_address iDestination, uint8_t state, bool bIsReply)
8fa35473
LOK
1000{
1001 cec_command command;
ae693aaa 1002 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_REPORT_AUDIO_STATUS);
8fa35473
LOK
1003 command.parameters.PushBack(state);
1004
2b44051c 1005 return Transmit(command, false, bIsReply);
8fa35473
LOK
1006}
1007
2b44051c 1008bool CCECCommandHandler::TransmitSetSystemAudioMode(const cec_logical_address iInitiator, const cec_logical_address iDestination, cec_system_audio_status state, bool bIsReply)
8fa35473
LOK
1009{
1010 cec_command command;
ae693aaa 1011 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_SET_SYSTEM_AUDIO_MODE);
8fa35473
LOK
1012 command.parameters.PushBack((uint8_t)state);
1013
2b44051c 1014 return Transmit(command, false, bIsReply);
8fa35473
LOK
1015}
1016
2b44051c 1017bool CCECCommandHandler::TransmitSetStreamPath(uint16_t iStreamPath, bool bIsReply)
f42d3e0f 1018{
2b44051c
LOK
1019 if (m_busDevice->GetLogicalAddress() != CECDEVICE_TV)
1020 {
1021 LIB_CEC->AddLog(CEC_LOG_ERROR, "only the TV is allowed to send CEC_OPCODE_SET_STREAM_PATH");
1022 return false;
1023 }
f42d3e0f
LOK
1024 cec_command command;
1025 cec_command::Format(command, m_busDevice->GetLogicalAddress(), CECDEVICE_BROADCAST, CEC_OPCODE_SET_STREAM_PATH);
1026 command.parameters.PushBack((uint8_t) ((iStreamPath >> 8) & 0xFF));
1027 command.parameters.PushBack((uint8_t) (iStreamPath & 0xFF));
1028
2b44051c 1029 return Transmit(command, false, bIsReply);
f42d3e0f
LOK
1030}
1031
2b44051c 1032bool CCECCommandHandler::TransmitSystemAudioModeStatus(const cec_logical_address iInitiator, const cec_logical_address iDestination, cec_system_audio_status state, bool bIsReply)
8fa35473
LOK
1033{
1034 cec_command command;
ae693aaa 1035 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_SYSTEM_AUDIO_MODE_STATUS);
8fa35473
LOK
1036 command.parameters.PushBack((uint8_t)state);
1037
2b44051c 1038 return Transmit(command, false, bIsReply);
8fa35473
LOK
1039}
1040
2b44051c 1041bool CCECCommandHandler::TransmitDeckStatus(const cec_logical_address iInitiator, const cec_logical_address iDestination, cec_deck_info state, bool bIsReply)
8fa35473
LOK
1042{
1043 cec_command command;
ae693aaa 1044 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_DECK_STATUS);
8fa35473
LOK
1045 command.PushBack((uint8_t)state);
1046
2b44051c 1047 return Transmit(command, false, bIsReply);
8fa35473
LOK
1048}
1049
4bec9d79 1050bool CCECCommandHandler::TransmitKeypress(const cec_logical_address iInitiator, const cec_logical_address iDestination, cec_user_control_code key, bool bWait /* = true */)
8fa35473
LOK
1051{
1052 cec_command command;
ae693aaa 1053 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_USER_CONTROL_PRESSED);
8fa35473
LOK
1054 command.parameters.PushBack((uint8_t)key);
1055
2b44051c 1056 return Transmit(command, !bWait, false);
8fa35473
LOK
1057}
1058
4bec9d79 1059bool CCECCommandHandler::TransmitKeyRelease(const cec_logical_address iInitiator, const cec_logical_address iDestination, bool bWait /* = true */)
8fa35473
LOK
1060{
1061 cec_command command;
ae693aaa 1062 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_USER_CONTROL_RELEASE);
8fa35473 1063
2b44051c 1064 return Transmit(command, !bWait, false);
8fa35473
LOK
1065}
1066
2b44051c 1067bool CCECCommandHandler::Transmit(cec_command &command, bool bSuppressWait, bool bIsReply)
8fa35473 1068{
b64db02e 1069 bool bReturn(false);
4fde2fd0
LOK
1070 cec_opcode expectedResponse(cec_command::GetResponseOpcode(command.opcode));
1071 bool bExpectResponse(expectedResponse != CEC_OPCODE_NONE && !bSuppressWait);
ae693aaa 1072 command.transmit_timeout = m_iTransmitTimeout;
ae693aaa 1073
7e4558f1
LOK
1074 if (command.initiator == CECDEVICE_UNKNOWN)
1075 {
004b8382 1076 LIB_CEC->AddLog(CEC_LOG_ERROR, "not transmitting a command without a valid initiator");
7e4558f1
LOK
1077 return bReturn;
1078 }
1079
ae693aaa 1080 {
6c0ccaa5 1081 uint8_t iTries(0), iMaxTries(!command.opcode_set ? 1 : m_iTransmitRetries + 1);
ec15344c 1082 while (!bReturn && ++iTries <= iMaxTries && !m_busDevice->IsUnsupportedFeature(command.opcode))
b64db02e 1083 {
2b44051c 1084 if ((bReturn = m_processor->Transmit(command, bIsReply)) == true)
19cbfa8f 1085 {
004b8382 1086 LIB_CEC->AddLog(CEC_LOG_DEBUG, "command transmitted");
4478bc79 1087 if (bExpectResponse)
24dd566c 1088 {
060a7b5e 1089 bReturn = m_busDevice->WaitForOpcode(expectedResponse);
004b8382 1090 LIB_CEC->AddLog(CEC_LOG_DEBUG, bReturn ? "expected response received (%X: %s)" : "expected response not received (%X: %s)", (int)expectedResponse, ToString(expectedResponse));
24dd566c 1091 }
19cbfa8f 1092 }
b64db02e 1093 }
ae693aaa 1094 }
8fa35473 1095
b64db02e 1096 return bReturn;
8fa35473 1097}
83be0701 1098
aa4c0d34 1099bool CCECCommandHandler::ActivateSource(bool bTransmitDelayedCommandsOnly /* = false */)
83be0701 1100{
c4287bcd 1101 if (m_busDevice->IsActiveSource() &&
aa4c0d34 1102 m_busDevice->IsHandledByLibCEC())
c4287bcd 1103 {
b0015449
LOK
1104 {
1105 CLockObject lock(m_mutex);
aa4c0d34
LOK
1106 // check if we need to send a delayed source switch
1107 if (bTransmitDelayedCommandsOnly)
1108 {
1109 if (m_iActiveSourcePending == 0 || GetTimeMs() < m_iActiveSourcePending)
1110 return false;
1111
1112 LIB_CEC->AddLog(CEC_LOG_DEBUG, "transmitting delayed activate source command");
1113 }
b0015449
LOK
1114 }
1115
aa4c0d34 1116 // update the power state and menu state
c4287bcd 1117 m_busDevice->SetPowerStatus(CEC_POWER_STATUS_ON);
7b01619d
LOK
1118 m_busDevice->SetMenuState(CEC_MENU_STATE_ACTIVATED);
1119
1120 // vendor specific hook
1121 VendorPreActivateSourceHook();
aa4c0d34
LOK
1122
1123 // power on the TV
f81fdbfa 1124 bool bActiveSourceFailed = !m_busDevice->TransmitImageViewOn();
c4287bcd 1125
aa4c0d34
LOK
1126 // check if we're allowed to switch sources
1127 bool bSourceSwitchAllowed = SourceSwitchAllowed();
1128 if (!bSourceSwitchAllowed)
1129 LIB_CEC->AddLog(CEC_LOG_DEBUG, "source switch is currently not allowed by command handler");
dbad810a 1130
aa4c0d34
LOK
1131 // switch sources (if allowed)
1132 if (!bActiveSourceFailed && bSourceSwitchAllowed)
b0015449 1133 {
2b44051c
LOK
1134 bActiveSourceFailed = !m_busDevice->TransmitActiveSource(false) ||
1135 !m_busDevice->TransmitMenuState(CECDEVICE_TV, false);
aa4c0d34
LOK
1136
1137 // update the deck status for playback devices
1138 if (!bActiveSourceFailed)
1139 {
1140 CCECPlaybackDevice *playbackDevice = m_busDevice->AsPlaybackDevice();
1141 if (playbackDevice && SendDeckStatusUpdateOnActiveSource())
2b44051c 1142 bActiveSourceFailed = !playbackDevice->TransmitDeckStatus(CECDEVICE_TV, false);
aa4c0d34 1143 }
dbad810a
LOK
1144 }
1145
aa4c0d34
LOK
1146 // retry later
1147 if (bActiveSourceFailed || !bSourceSwitchAllowed)
dbad810a
LOK
1148 {
1149 LIB_CEC->AddLog(CEC_LOG_DEBUG, "failed to make '%s' the active source. will retry later", m_busDevice->GetLogicalAddressName());
548c81b3
LOK
1150 CLockObject lock(m_mutex);
1151 if (m_iActiveSourcePending == 0)
1152 m_iActiveSourcePending = GetTimeMs() + (int64_t)CEC_ACTIVE_SOURCE_SWITCH_RETRY_TIME_MS;
b0015449
LOK
1153 return false;
1154 }
045b2e29
LOK
1155 else
1156 {
1157 CLockObject lock(m_mutex);
1158 // clear previous pending active source command
1159 m_iActiveSourcePending = 0;
1160 }
aa40e0e2 1161
aa4c0d34
LOK
1162 // mark the handler as initialised
1163 CLockObject lock(m_mutex);
c4287bcd 1164 m_bHandlerInited = true;
83be0701 1165 }
7dc58c9f 1166 return true;
83be0701 1167}
b499cf16 1168
060a7b5e 1169void CCECCommandHandler::ScheduleActivateSource(uint64_t iDelay)
b499cf16 1170{
060a7b5e
LOK
1171 CLockObject lock(m_mutex);
1172 m_iActiveSourcePending = GetTimeMs() + iDelay;
b499cf16 1173}