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