send our vendor id when the tv sends it's vendor id
[deb_libcec.git] / src / lib / implementations / CECCommandHandler.cpp
CommitLineData
e9de9629
LOK
1/*
2 * This file is part of the libCEC(R) library.
3 *
16f47961 4 * libCEC(R) is Copyright (C) 2011-2013 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]);
d5890d16 212 CCECBusDevice *device = m_processor->GetDevice(command.initiator);
004b8382 213 if (device)
d5890d16
LOK
214 {
215 device->SetPhysicalAddress(iAddress);
004b8382 216 device->MarkAsActiveSource();
d5890d16 217 }
d3caa81b
LOK
218
219 m_processor->GetDevices()->SignalAll(command.opcode);
220 return COMMAND_HANDLED;
be5b0e24
LOK
221 }
222
9a54dc82 223 return CEC_ABORT_REASON_INVALID_OPERAND;
be5b0e24
LOK
224}
225
9a54dc82 226int CCECCommandHandler::HandleDeckControl(const cec_command &command)
a9232a79 227{
aa40e0e2
LOK
228 CCECPlaybackDevice *device = CCECBusDevice::AsPlaybackDevice(GetDevice(command.destination));
229 if (device && command.parameters.size > 0)
a9232a79 230 {
aa40e0e2 231 device->SetDeckControlMode((cec_deck_control_mode) command.parameters[0]);
9a54dc82 232 return COMMAND_HANDLED;
a9232a79
LOK
233 }
234
9a54dc82 235 return CEC_ABORT_REASON_INVALID_OPERAND;
a9232a79
LOK
236}
237
9a54dc82 238int CCECCommandHandler::HandleDeviceCecVersion(const cec_command &command)
6a1c0009
LOK
239{
240 if (command.parameters.size == 1)
241 {
242 CCECBusDevice *device = GetDevice(command.initiator);
243 if (device)
244 device->SetCecVersion((cec_version) command.parameters[0]);
9a54dc82
LOK
245
246 return COMMAND_HANDLED;
6a1c0009
LOK
247 }
248
9a54dc82 249 return CEC_ABORT_REASON_INVALID_OPERAND;
6a1c0009
LOK
250}
251
866e7584 252int CCECCommandHandler::HandleDeviceVendorCommandWithId(const cec_command & UNUSED(command))
e9de9629 253{
22579015 254 return CEC_ABORT_REASON_INVALID_OPERAND;
e9de9629
LOK
255}
256
9a54dc82 257int CCECCommandHandler::HandleDeviceVendorId(const cec_command &command)
e9de9629 258{
9a54dc82 259 SetVendorId(command);
4c52ccd1
LOK
260
261 if (command.initiator == CECDEVICE_TV)
262 {
263 CCECBusDevice* primary = m_processor->GetPrimaryDevice();
264 if (primary)
265 primary->TransmitVendorID(CECDEVICE_BROADCAST, false, false);
266 }
267
9a54dc82 268 return COMMAND_HANDLED;
e9de9629
LOK
269}
270
9a54dc82 271int CCECCommandHandler::HandleFeatureAbort(const cec_command &command)
4d738fe3 272{
69a1a673
LOK
273 if (command.parameters.size == 2 &&
274 (command.parameters[1] == CEC_ABORT_REASON_UNRECOGNIZED_OPCODE ||
275 command.parameters[1] == CEC_ABORT_REASON_REFUSED))
004b8382 276 m_processor->GetDevice(command.initiator)->SetUnsupportedFeature((cec_opcode)command.parameters[0]);
9a54dc82 277 return COMMAND_HANDLED;
4d738fe3
LOK
278}
279
9a54dc82 280int CCECCommandHandler::HandleGetCecVersion(const cec_command &command)
e9de9629 281{
0b8c7eab 282 if (m_processor->CECInitialised() && m_processor->IsHandledByLibCEC(command.destination))
6b72afcd
LOK
283 {
284 CCECBusDevice *device = GetDevice(command.destination);
2b44051c 285 if (device && device->TransmitCECVersion(command.initiator, true))
9a54dc82
LOK
286 return COMMAND_HANDLED;
287 return CEC_ABORT_REASON_INVALID_OPERAND;
6b72afcd 288 }
0f23c85c 289
9a54dc82 290 return CEC_ABORT_REASON_NOT_IN_CORRECT_MODE_TO_RESPOND;
e9de9629
LOK
291}
292
9a54dc82 293int CCECCommandHandler::HandleGiveAudioStatus(const cec_command &command)
a1f8fb1b 294{
0b8c7eab 295 if (m_processor->CECInitialised() && m_processor->IsHandledByLibCEC(command.destination))
6b72afcd 296 {
aa40e0e2 297 CCECAudioSystem *device = CCECBusDevice::AsAudioSystem(GetDevice(command.destination));
2b44051c 298 if (device && device->TransmitAudioStatus(command.initiator, true))
9a54dc82
LOK
299 return COMMAND_HANDLED;
300 return CEC_ABORT_REASON_INVALID_OPERAND;
6b72afcd 301 }
a1f8fb1b 302
9a54dc82 303 return CEC_ABORT_REASON_NOT_IN_CORRECT_MODE_TO_RESPOND;
a1f8fb1b
LOK
304}
305
9a54dc82 306int CCECCommandHandler::HandleGiveDeckStatus(const cec_command &command)
e9de9629 307{
0b8c7eab 308 if (m_processor->CECInitialised() && m_processor->IsHandledByLibCEC(command.destination))
6b72afcd 309 {
aa40e0e2 310 CCECPlaybackDevice *device = CCECBusDevice::AsPlaybackDevice(GetDevice(command.destination));
2b44051c 311 if (device && device->TransmitDeckStatus(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_NOT_IN_CORRECT_MODE_TO_RESPOND;
e9de9629
LOK
317}
318
9a54dc82 319int CCECCommandHandler::HandleGiveDevicePowerStatus(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->TransmitPowerState(command.initiator, true))
9a54dc82
LOK
325 return COMMAND_HANDLED;
326 return CEC_ABORT_REASON_INVALID_OPERAND;
6b72afcd 327 }
0f23c85c 328
9a54dc82 329 return CEC_ABORT_REASON_INVALID_OPERAND;
e9de9629
LOK
330}
331
9a54dc82 332int CCECCommandHandler::HandleGiveDeviceVendorId(const cec_command &command)
e9de9629 333{
0b8c7eab 334 if (m_processor->CECInitialised() && m_processor->IsHandledByLibCEC(command.destination))
6b72afcd
LOK
335 {
336 CCECBusDevice *device = GetDevice(command.destination);
2b44051c 337 if (device && device->TransmitVendorID(command.initiator, true, true))
9a54dc82 338 return COMMAND_HANDLED;
6b72afcd 339 }
0f23c85c 340
9a54dc82 341 return CEC_ABORT_REASON_INVALID_OPERAND;
e9de9629
LOK
342}
343
9a54dc82 344int CCECCommandHandler::HandleGiveOSDName(const cec_command &command)
e9de9629 345{
0b8c7eab 346 if (m_processor->CECInitialised() && m_processor->IsHandledByLibCEC(command.destination))
6b72afcd
LOK
347 {
348 CCECBusDevice *device = GetDevice(command.destination);
2b44051c 349 if (device && device->TransmitOSDName(command.initiator, true))
9a54dc82 350 return COMMAND_HANDLED;
6b72afcd 351 }
0f23c85c 352
9a54dc82 353 return CEC_ABORT_REASON_INVALID_OPERAND;
e9de9629
LOK
354}
355
9a54dc82 356int CCECCommandHandler::HandleGivePhysicalAddress(const cec_command &command)
e9de9629 357{
0b8c7eab 358 if (m_processor->CECInitialised() && m_processor->IsHandledByLibCEC(command.destination))
6b72afcd
LOK
359 {
360 CCECBusDevice *device = GetDevice(command.destination);
2b44051c 361 if (device && device->TransmitPhysicalAddress(true))
9a54dc82
LOK
362 return COMMAND_HANDLED;
363 return CEC_ABORT_REASON_INVALID_OPERAND;
6b72afcd 364 }
09c10b66 365
9a54dc82 366 return CEC_ABORT_REASON_NOT_IN_CORRECT_MODE_TO_RESPOND;
e9de9629
LOK
367}
368
9a54dc82 369int CCECCommandHandler::HandleGiveMenuLanguage(const cec_command &command)
fbdea54c 370{
0b8c7eab 371 if (m_processor->CECInitialised() && m_processor->IsHandledByLibCEC(command.destination))
fbdea54c
MK
372 {
373 CCECBusDevice *device = GetDevice(command.destination);
2b44051c 374 if (device && device->TransmitSetMenuLanguage(command.initiator, true))
9a54dc82
LOK
375 return COMMAND_HANDLED;
376 return CEC_ABORT_REASON_INVALID_OPERAND;
fbdea54c
MK
377 }
378
9a54dc82 379 return CEC_ABORT_REASON_NOT_IN_CORRECT_MODE_TO_RESPOND;
fbdea54c
MK
380}
381
9a54dc82 382int CCECCommandHandler::HandleGiveSystemAudioModeStatus(const cec_command &command)
1a6669b8 383{
0b8c7eab 384 if (m_processor->CECInitialised() && m_processor->IsHandledByLibCEC(command.destination))
1a6669b8 385 {
aa40e0e2 386 CCECAudioSystem *device = CCECBusDevice::AsAudioSystem(GetDevice(command.destination));
2b44051c 387 if (device && device->TransmitSystemAudioModeStatus(command.initiator, true))
9a54dc82
LOK
388 return COMMAND_HANDLED;
389 return CEC_ABORT_REASON_INVALID_OPERAND;
1a6669b8
LOK
390 }
391
9a54dc82 392 return CEC_ABORT_REASON_NOT_IN_CORRECT_MODE_TO_RESPOND;
1a6669b8
LOK
393}
394
9a54dc82 395int CCECCommandHandler::HandleImageViewOn(const cec_command &command)
1a6669b8 396{
2b44051c 397 CCECBusDevice *device = GetDevice(command.destination);
14f56268 398 if (device && device->GetCurrentStatus() == CEC_DEVICE_STATUS_PRESENT)
2b44051c
LOK
399 {
400 if (device->GetCurrentPowerStatus() == CEC_POWER_STATUS_STANDBY ||
401 device->GetCurrentPowerStatus() == CEC_POWER_STATUS_IN_TRANSITION_ON_TO_STANDBY)
402 device->SetPowerStatus(CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON);
14f56268
LOK
403 CCECBusDevice* tv = GetDevice(CECDEVICE_TV);
404 if (tv)
405 tv->OnImageViewOnSent(false);
2b44051c 406 }
9a54dc82 407 return COMMAND_HANDLED;
1a6669b8
LOK
408}
409
9a54dc82 410int CCECCommandHandler::HandleMenuRequest(const cec_command &command)
e9de9629 411{
0b8c7eab 412 if (m_processor->CECInitialised() && m_processor->IsHandledByLibCEC(command.destination))
0f23c85c 413 {
e1804a4e
LOK
414 CCECBusDevice *device = GetDevice(command.destination);
415 if (device)
6b72afcd 416 {
004b8382
LOK
417 CCECClient *client = device->GetClient();
418 if (client)
e1804a4e 419 {
004b8382
LOK
420 if (command.parameters[0] == CEC_MENU_REQUEST_TYPE_ACTIVATE)
421 {
422 if (client->MenuStateChanged(CEC_MENU_STATE_ACTIVATED) == 1)
423 device->SetMenuState(CEC_MENU_STATE_ACTIVATED);
424 }
425 else if (command.parameters[0] == CEC_MENU_REQUEST_TYPE_DEACTIVATE)
426 {
427 if (client->MenuStateChanged(CEC_MENU_STATE_DEACTIVATED) == 1)
428 device->SetMenuState(CEC_MENU_STATE_DEACTIVATED);
429 }
e1804a4e 430 }
2b44051c 431 if (device->TransmitMenuState(command.initiator, true))
9a54dc82 432 return COMMAND_HANDLED;
6b72afcd 433 }
9a54dc82 434 return CEC_ABORT_REASON_INVALID_OPERAND;
15d1a84c
LOK
435 }
436
9a54dc82 437 return CEC_ABORT_REASON_NOT_IN_CORRECT_MODE_TO_RESPOND;
15d1a84c
LOK
438}
439
d297cbd4
LOK
440bool CCECCommandHandler::HandlePoll(const cec_command &command)
441{
a75e3a5a 442 m_busDevice->HandlePoll(command.destination);
d297cbd4
LOK
443 return true;
444}
445
9a54dc82 446int CCECCommandHandler::HandleReportAudioStatus(const cec_command &command)
15d1a84c
LOK
447{
448 if (command.parameters.size == 1)
449 {
aa40e0e2
LOK
450 CCECAudioSystem *device = CCECBusDevice::AsAudioSystem(GetDevice(command.initiator));
451 if (device)
6b72afcd 452 {
aa40e0e2 453 device->SetAudioStatus(command.parameters[0]);
9a54dc82 454 return COMMAND_HANDLED;
6b72afcd 455 }
0f23c85c 456 }
9a54dc82 457 return CEC_ABORT_REASON_INVALID_OPERAND;
e9de9629
LOK
458}
459
9a54dc82 460int CCECCommandHandler::HandleReportPhysicalAddress(const cec_command &command)
907bd60f 461{
0bfce006 462 if (command.parameters.size == 3)
907bd60f
LOK
463 {
464 uint16_t iNewAddress = ((uint16_t)command.parameters[0] << 8) | ((uint16_t)command.parameters[1]);
16b1e052 465 SetPhysicalAddress(command.initiator, iNewAddress);
14004a26
LOK
466
467 if (command.initiator == CECDEVICE_TV)
468 {
469 CCECBusDevice* primary = m_processor->GetPrimaryDevice();
470 if (primary)
471 primary->TransmitPhysicalAddress(false);
472 }
9a54dc82 473 return COMMAND_HANDLED;
907bd60f 474 }
9a54dc82 475 return CEC_ABORT_REASON_INVALID_OPERAND;
907bd60f
LOK
476}
477
9a54dc82 478int CCECCommandHandler::HandleReportPowerStatus(const cec_command &command)
e55f3f70
LOK
479{
480 if (command.parameters.size == 1)
481 {
482 CCECBusDevice *device = GetDevice(command.initiator);
483 if (device)
9a54dc82 484 {
e55f3f70 485 device->SetPowerStatus((cec_power_status) command.parameters[0]);
9a54dc82
LOK
486 return COMMAND_HANDLED;
487 }
e55f3f70 488 }
9a54dc82 489 return CEC_ABORT_REASON_INVALID_OPERAND;
e55f3f70
LOK
490}
491
9a54dc82 492int CCECCommandHandler::HandleRequestActiveSource(const cec_command &command)
e9de9629 493{
0b8c7eab 494 if (m_processor->CECInitialised())
5f316715 495 {
004b8382
LOK
496 LIB_CEC->AddLog(CEC_LOG_DEBUG, ">> %i requests active source", (uint8_t) command.initiator);
497 m_processor->GetDevice(command.initiator)->SetPowerStatus(CEC_POWER_STATUS_ON);
8747dd4f 498
5f316715 499 vector<CCECBusDevice *> devices;
f00ff009 500 for (size_t iDevicePtr = 0; iDevicePtr < GetMyDevices(devices); iDevicePtr++)
2b44051c 501 devices[iDevicePtr]->TransmitActiveSource(true);
5f316715 502 }
9a54dc82
LOK
503
504 return COMMAND_HANDLED;
e9de9629
LOK
505}
506
9a54dc82 507int CCECCommandHandler::HandleRoutingChange(const cec_command &command)
e9de9629
LOK
508{
509 if (command.parameters.size == 4)
510 {
0f23c85c
LOK
511 CCECBusDevice *device = GetDevice(command.initiator);
512 if (device)
9a54dc82 513 {
844eab4e
LOK
514 uint16_t iNewAddress = ((uint16_t)command.parameters[2] << 8) | ((uint16_t)command.parameters[3]);
515 device->SetActiveRoute(iNewAddress);
9a54dc82
LOK
516 return COMMAND_HANDLED;
517 }
e9de9629 518 }
9a54dc82
LOK
519
520 return CEC_ABORT_REASON_INVALID_OPERAND;
e9de9629
LOK
521}
522
9a54dc82 523int CCECCommandHandler::HandleRoutingInformation(const cec_command &command)
907bd60f
LOK
524{
525 if (command.parameters.size == 2)
526 {
844eab4e 527 CCECBusDevice *device = GetDevice(command.initiator);
004b8382 528 if (device)
9a54dc82 529 {
844eab4e
LOK
530 uint16_t iNewAddress = ((uint16_t)command.parameters[0] << 8) | ((uint16_t)command.parameters[1]);
531 device->SetActiveRoute(iNewAddress);
9a54dc82
LOK
532 return COMMAND_HANDLED;
533 }
907bd60f 534 }
15d1a84c 535
9a54dc82 536 return CEC_ABORT_REASON_INVALID_OPERAND;
907bd60f
LOK
537}
538
9a54dc82 539int CCECCommandHandler::HandleSetMenuLanguage(const cec_command &command)
a3269a0a
LOK
540{
541 if (command.parameters.size == 3)
542 {
543 CCECBusDevice *device = GetDevice(command.initiator);
544 if (device)
545 {
546 cec_menu_language language;
547 language.device = command.initiator;
56701628 548 for (uint8_t iPtr = 0; iPtr < 4; iPtr++)
a3269a0a
LOK
549 language.language[iPtr] = command.parameters[iPtr];
550 language.language[3] = 0;
551 device->SetMenuLanguage(language);
9a54dc82 552 return COMMAND_HANDLED;
15d1a84c
LOK
553 }
554 }
9a54dc82
LOK
555
556 return CEC_ABORT_REASON_INVALID_OPERAND;
15d1a84c
LOK
557}
558
9a54dc82 559int CCECCommandHandler::HandleSetOSDName(const cec_command &command)
15d1a84c
LOK
560{
561 if (command.parameters.size > 0)
562 {
563 CCECBusDevice *device = GetDevice(command.initiator);
564 if (device)
565 {
566 char buf[1024];
567 for (uint8_t iPtr = 0; iPtr < command.parameters.size; iPtr++)
568 buf[iPtr] = (char)command.parameters[iPtr];
569 buf[command.parameters.size] = 0;
570
571 CStdString strName(buf);
572 device->SetOSDName(strName);
6b72afcd 573
9a54dc82 574 return COMMAND_HANDLED;
a3269a0a
LOK
575 }
576 }
9a54dc82
LOK
577
578 return CEC_ABORT_REASON_INVALID_OPERAND;
a3269a0a
LOK
579}
580
9a54dc82 581int CCECCommandHandler::HandleSetStreamPath(const cec_command &command)
e9de9629 582{
9a54dc82
LOK
583 if (!m_processor->CECInitialised())
584 return CEC_ABORT_REASON_NOT_IN_CORRECT_MODE_TO_RESPOND;
585
586 if (command.parameters.size >= 2)
e9de9629 587 {
b6c7bc94 588 uint16_t iStreamAddress = ((uint16_t)command.parameters[0] << 8) | ((uint16_t)command.parameters[1]);
bf1b57e0
LOK
589 LIB_CEC->AddLog(CEC_LOG_DEBUG, ">> %s (%x) sets stream path to physical address %04x", ToString(command.initiator), command.initiator, iStreamAddress);
590
72689600
LOK
591 /* one of the device handled by libCEC has been made active */
592 CCECBusDevice *device = GetDeviceByPhysicalAddress(iStreamAddress);
88d5d47f 593 if (device)
9a54dc82 594 {
d306776d 595 if (device->IsHandledByLibCEC() && !device->IsActiveSource())
88d5d47f
LOK
596 device->ActivateSource();
597 else
1e338e95 598 {
88d5d47f 599 device->MarkAsActiveSource();
1e338e95
LOK
600 device->TransmitActiveSource(true);
601 }
9a54dc82
LOK
602 return COMMAND_HANDLED;
603 }
e9de9629 604 }
9a54dc82
LOK
605
606 return CEC_ABORT_REASON_INVALID_OPERAND;
e9de9629
LOK
607}
608
9a54dc82 609int CCECCommandHandler::HandleSystemAudioModeRequest(const cec_command &command)
e5e86c76 610{
0b8c7eab 611 if (m_processor->CECInitialised() && m_processor->IsHandledByLibCEC(command.destination))
e5e86c76 612 {
aa40e0e2
LOK
613 CCECAudioSystem *device = CCECBusDevice::AsAudioSystem(GetDevice(command.destination));
614 if (device)
aa517a0d
LOK
615 {
616 if (command.parameters.size >= 2)
617 {
618 device->SetPowerStatus(CEC_POWER_STATUS_ON);
aa40e0e2 619 device->SetSystemAudioModeStatus(CEC_SYSTEM_AUDIO_STATUS_ON);
aa517a0d
LOK
620 uint16_t iNewAddress = ((uint16_t)command.parameters[0] << 8) | ((uint16_t)command.parameters[1]);
621 CCECBusDevice *newActiveDevice = GetDeviceByPhysicalAddress(iNewAddress);
622 if (newActiveDevice)
004b8382 623 newActiveDevice->MarkAsActiveSource();
2b44051c 624 if (device->TransmitSetSystemAudioMode(command.initiator, true))
9a54dc82 625 return COMMAND_HANDLED;
aa517a0d
LOK
626 }
627 else
628 {
aa40e0e2 629 device->SetSystemAudioModeStatus(CEC_SYSTEM_AUDIO_STATUS_OFF);
2b44051c 630 if (device->TransmitSetSystemAudioMode(command.initiator, true))
9a54dc82 631 return COMMAND_HANDLED;
aa517a0d
LOK
632 }
633 }
e5e86c76 634 }
9a54dc82
LOK
635
636 return CEC_ABORT_REASON_NOT_IN_CORRECT_MODE_TO_RESPOND;
e5e86c76
LOK
637}
638
9a54dc82 639int CCECCommandHandler::HandleStandby(const cec_command &command)
4d6b4433
LOK
640{
641 CCECBusDevice *device = GetDevice(command.initiator);
642 if (device)
643 device->SetPowerStatus(CEC_POWER_STATUS_STANDBY);
6b72afcd 644
9a54dc82 645 return COMMAND_HANDLED;
4d6b4433
LOK
646}
647
9a54dc82 648int CCECCommandHandler::HandleSystemAudioModeStatus(const cec_command &command)
868dc71f 649{
aa517a0d 650 if (command.parameters.size == 1)
868dc71f 651 {
aa40e0e2
LOK
652 CCECAudioSystem *device = CCECBusDevice::AsAudioSystem(GetDevice(command.initiator));
653 if (device)
aa517a0d 654 {
aa40e0e2 655 device->SetSystemAudioModeStatus((cec_system_audio_status)command.parameters[0]);
9a54dc82 656 return COMMAND_HANDLED;
aa517a0d
LOK
657 }
658 }
659
9a54dc82 660 return CEC_ABORT_REASON_INVALID_OPERAND;
aa517a0d
LOK
661}
662
9a54dc82 663int CCECCommandHandler::HandleSetSystemAudioMode(const cec_command &command)
aa517a0d
LOK
664{
665 if (command.parameters.size == 1)
666 {
aa40e0e2
LOK
667 CCECAudioSystem *device = CCECBusDevice::AsAudioSystem(GetDevice(command.initiator));
668 if (device)
aa517a0d 669 {
aa40e0e2 670 device->SetSystemAudioModeStatus((cec_system_audio_status)command.parameters[0]);
9a54dc82 671 return COMMAND_HANDLED;
aa517a0d 672 }
868dc71f
LOK
673 }
674
9a54dc82 675 return CEC_ABORT_REASON_INVALID_OPERAND;
868dc71f
LOK
676}
677
9a54dc82 678int CCECCommandHandler::HandleTextViewOn(const cec_command &command)
cf0ecd85 679{
004b8382 680 m_processor->GetDevice(command.initiator)->MarkAsActiveSource();
9a54dc82 681 return COMMAND_HANDLED;
cf0ecd85
LOK
682}
683
9a54dc82 684int CCECCommandHandler::HandleUserControlPressed(const cec_command &command)
e9de9629 685{
9a54dc82
LOK
686 if (!m_processor->CECInitialised() ||
687 !m_processor->IsHandledByLibCEC(command.destination))
688 return CEC_ABORT_REASON_NOT_IN_CORRECT_MODE_TO_RESPOND;
004b8382 689
9a54dc82
LOK
690 if (command.parameters.size == 0)
691 return CEC_ABORT_REASON_INVALID_OPERAND;
004b8382 692
9a54dc82
LOK
693 CCECBusDevice *device = GetDevice(command.destination);
694 if (!device)
695 return CEC_ABORT_REASON_INVALID_OPERAND;
e33e0d75 696
9a54dc82
LOK
697 CCECClient *client = device->GetClient();
698 if (client)
9a54dc82 699 client->SetCurrentButton((cec_user_control_code) command.parameters[0]);
e33e0d75 700
9a54dc82 701 if (command.parameters[0] == CEC_USER_CONTROL_CODE_POWER ||
117863dd
LOK
702 command.parameters[0] == CEC_USER_CONTROL_CODE_POWER_ON_FUNCTION||
703 command.parameters[0] == CEC_USER_CONTROL_CODE_POWER_TOGGLE_FUNCTION)
9a54dc82
LOK
704 {
705 bool bPowerOn(true);
706
117863dd 707 // CEC_USER_CONTROL_CODE_POWER and CEC_USER_CONTROL_CODE_POWER_TOGGLE_FUNCTION operate as a toggle
9a54dc82 708 // assume CEC_USER_CONTROL_CODE_POWER_ON_FUNCTION does not
117863dd
LOK
709 if (command.parameters[0] == CEC_USER_CONTROL_CODE_POWER ||
710 command.parameters[0] == CEC_USER_CONTROL_CODE_POWER_TOGGLE_FUNCTION)
9a54dc82
LOK
711 {
712 cec_power_status status = device->GetCurrentPowerStatus();
713 bPowerOn = !(status == CEC_POWER_STATUS_ON || status == CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON);
e9de9629 714 }
e33e0d75 715
9a54dc82
LOK
716 if (bPowerOn)
717 {
718 device->ActivateSource();
719 }
720 else
721 {
722 device->MarkAsInactiveSource();
723 device->TransmitInactiveSource();
724 device->SetMenuState(CEC_MENU_STATE_DEACTIVATED);
725 }
e9de9629 726 }
117863dd
LOK
727 else if (command.parameters[0] != CEC_USER_CONTROL_CODE_POWER_OFF_FUNCTION)
728 {
729 // we're not marked as active source, but the tv sends keypresses to us, so assume it forgot to activate us
730 if (!device->IsActiveSource() && command.initiator == CECDEVICE_TV)
ecc633c5 731 device->MarkAsActiveSource();
117863dd 732 }
9a54dc82
LOK
733
734 return COMMAND_HANDLED;
e9de9629
LOK
735}
736
9a54dc82 737int CCECCommandHandler::HandleUserControlRelease(const cec_command &command)
e9de9629 738{
9a54dc82
LOK
739 if (!m_processor->CECInitialised() ||
740 !m_processor->IsHandledByLibCEC(command.destination))
741 return CEC_ABORT_REASON_NOT_IN_CORRECT_MODE_TO_RESPOND;
742
004b8382
LOK
743 CCECClient *client = m_processor->GetClient(command.destination);
744 if (client)
745 client->AddKey();
9a54dc82
LOK
746
747 return COMMAND_HANDLED;
e9de9629
LOK
748}
749
866e7584 750int CCECCommandHandler::HandleVendorCommand(const cec_command & UNUSED(command))
1de6617c 751{
22579015 752 return CEC_ABORT_REASON_INVALID_OPERAND;
1de6617c
LOK
753}
754
5da98c47
LOK
755int CCECCommandHandler::HandleVendorRemoteButtonDown(const cec_command& command)
756{
8add8f38
LOK
757 if (command.parameters.size == 0)
758 return CEC_ABORT_REASON_INVALID_OPERAND;
759
5da98c47
LOK
760 LIB_CEC->AddLog(CEC_LOG_NOTICE, "unhandled vendor remote button received with keycode %x", command.parameters[0]);
761 return COMMAND_HANDLED;
762}
763
9a54dc82 764void CCECCommandHandler::UnhandledCommand(const cec_command &command, const cec_abort_reason reason)
e9de9629 765{
9a54dc82
LOK
766 if (m_processor->IsHandledByLibCEC(command.destination))
767 {
768 LIB_CEC->AddLog(CEC_LOG_DEBUG, "sending abort with opcode %02x and reason '%s' to %s", command.opcode, ToString(reason), ToString(command.initiator));
769 m_processor->TransmitAbort(command.destination, command.initiator, command.opcode, reason);
3ff78be2
LOK
770
771 if (reason == CEC_ABORT_REASON_INVALID_OPERAND)
772 RequestEmailFromCustomer(command);
9a54dc82 773 }
0f23c85c
LOK
774}
775
6f14b512 776size_t CCECCommandHandler::GetMyDevices(vector<CCECBusDevice *> &devices) const
8747dd4f 777{
6f14b512 778 size_t iReturn(0);
8747dd4f 779
fcf10e27 780 cec_logical_addresses addresses = m_processor->GetLogicalAddresses();
d2d1660c 781 for (uint8_t iPtr = CECDEVICE_TV; iPtr < CECDEVICE_BROADCAST; iPtr++)
8747dd4f
LOK
782 {
783 if (addresses[iPtr])
784 {
785 devices.push_back(GetDevice((cec_logical_address) iPtr));
786 ++iReturn;
787 }
788 }
789
790 return iReturn;
791}
792
0f23c85c
LOK
793CCECBusDevice *CCECCommandHandler::GetDevice(cec_logical_address iLogicalAddress) const
794{
004b8382 795 return m_processor->GetDevice(iLogicalAddress);
e9de9629 796}
6685ae07
LOK
797
798CCECBusDevice *CCECCommandHandler::GetDeviceByPhysicalAddress(uint16_t iPhysicalAddress) const
799{
fcf10e27 800 return m_processor->GetDeviceByPhysicalAddress(iPhysicalAddress);
6685ae07 801}
181b3475 802
8fa35473 803bool CCECCommandHandler::SetVendorId(const cec_command &command)
181b3475 804{
8fa35473 805 bool bChanged(false);
181b3475
LOK
806 if (command.parameters.size < 3)
807 {
004b8382 808 LIB_CEC->AddLog(CEC_LOG_WARNING, "invalid vendor ID received");
8fa35473 809 return bChanged;
181b3475
LOK
810 }
811
bae71306
LOK
812 uint64_t iVendorId = ((uint64_t)command.parameters[0] << 16) +
813 ((uint64_t)command.parameters[1] << 8) +
181b3475
LOK
814 (uint64_t)command.parameters[2];
815
816 CCECBusDevice *device = GetDevice((cec_logical_address) command.initiator);
817 if (device)
8fa35473
LOK
818 bChanged = device->SetVendorId(iVendorId);
819 return bChanged;
181b3475 820}
5e822b09 821
16b1e052
LOK
822void CCECCommandHandler::SetPhysicalAddress(cec_logical_address iAddress, uint16_t iNewAddress)
823{
004b8382 824 if (!m_processor->IsHandledByLibCEC(iAddress))
16b1e052 825 {
c0152c09
LOK
826 CCECBusDevice *otherDevice = m_processor->GetDeviceByPhysicalAddress(iNewAddress);
827 CCECClient *client = otherDevice ? otherDevice->GetClient() : NULL;
004b8382 828
c0152c09
LOK
829 CCECBusDevice *device = m_processor->GetDevice(iAddress);
830 if (device)
831 device->SetPhysicalAddress(iNewAddress);
832 else
c0152c09 833 LIB_CEC->AddLog(CEC_LOG_DEBUG, "device with logical address %X not found", iAddress);
c0152c09
LOK
834
835 /* another device reported the same physical address as ours */
836 if (client)
7e312a79
LOK
837 {
838 libcec_parameter param;
839 param.paramType = CEC_PARAMETER_TYPE_STRING;
840 param.paramData = (void*)"Physical address in use by another device. Please verify your settings";
841 client->Alert(CEC_ALERT_PHYSICAL_ADDRESS_ERROR, param);
c0152c09 842 client->ResetPhysicalAddress();
7e312a79 843 }
c0152c09
LOK
844 }
845 else
846 {
847 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
848 }
849}
855a3a98 850
f4698390
LOK
851bool CCECCommandHandler::PowerOn(const cec_logical_address iInitiator, const cec_logical_address iDestination)
852{
853 if (iDestination == CECDEVICE_TV)
854 return TransmitImageViewOn(iInitiator, iDestination);
855
856 return TransmitKeypress(iInitiator, iDestination, CEC_USER_CONTROL_CODE_POWER) &&
857 TransmitKeyRelease(iInitiator, iDestination);
858}
859
b64db02e 860bool CCECCommandHandler::TransmitImageViewOn(const cec_logical_address iInitiator, const cec_logical_address iDestination)
8fa35473
LOK
861{
862 cec_command command;
ae693aaa 863 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_IMAGE_VIEW_ON);
8fa35473 864
8e2366b9
LOK
865 if (Transmit(command, false, false))
866 {
867 CCECBusDevice* dest = m_processor->GetDevice(iDestination);
868 if (dest && dest->GetCurrentPowerStatus() != CEC_POWER_STATUS_ON)
869 dest->SetPowerStatus(CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON);
870 return true;
871 }
872 return false;
8fa35473
LOK
873}
874
875bool CCECCommandHandler::TransmitStandby(const cec_logical_address iInitiator, const cec_logical_address iDestination)
876{
877 cec_command command;
ae693aaa 878 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_STANDBY);
8fa35473 879
2b44051c 880 return Transmit(command, false, false);
8fa35473
LOK
881}
882
5734016c
LOK
883bool CCECCommandHandler::TransmitRequestActiveSource(const cec_logical_address iInitiator, bool bWaitForResponse /* = true */)
884{
885 cec_command command;
886 cec_command::Format(command, iInitiator, CECDEVICE_BROADCAST, CEC_OPCODE_REQUEST_ACTIVE_SOURCE);
887
2b44051c 888 return Transmit(command, !bWaitForResponse, false);
5734016c
LOK
889}
890
a75e3a5a 891bool CCECCommandHandler::TransmitRequestCecVersion(const cec_logical_address iInitiator, const cec_logical_address iDestination, bool bWaitForResponse /* = true */)
8fa35473
LOK
892{
893 cec_command command;
ae693aaa 894 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_GET_CEC_VERSION);
8fa35473 895
2b44051c 896 return Transmit(command, !bWaitForResponse, false);
8fa35473
LOK
897}
898
a75e3a5a 899bool CCECCommandHandler::TransmitRequestMenuLanguage(const cec_logical_address iInitiator, const cec_logical_address iDestination, bool bWaitForResponse /* = true */)
8fa35473
LOK
900{
901 cec_command command;
ae693aaa 902 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_GET_MENU_LANGUAGE);
8fa35473 903
2b44051c 904 return Transmit(command, !bWaitForResponse, false);
8fa35473
LOK
905}
906
a75e3a5a 907bool CCECCommandHandler::TransmitRequestOSDName(const cec_logical_address iInitiator, const cec_logical_address iDestination, bool bWaitForResponse /* = true */)
8fa35473
LOK
908{
909 cec_command command;
ae693aaa 910 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_GIVE_OSD_NAME);
8fa35473 911
2b44051c 912 return Transmit(command, !bWaitForResponse, false);
8fa35473
LOK
913}
914
6d2bc8de
LOK
915bool CCECCommandHandler::TransmitRequestAudioStatus(const cec_logical_address iInitiator, const cec_logical_address iDestination, bool bWaitForResponse /* = true */)
916{
917 cec_command command;
918 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_GIVE_AUDIO_STATUS);
919
920 return Transmit(command, !bWaitForResponse, false);
921}
922
a75e3a5a 923bool CCECCommandHandler::TransmitRequestPhysicalAddress(const cec_logical_address iInitiator, const cec_logical_address iDestination, bool bWaitForResponse /* = true */)
8fa35473
LOK
924{
925 cec_command command;
ae693aaa 926 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_GIVE_PHYSICAL_ADDRESS);
8fa35473 927
2b44051c 928 return Transmit(command, !bWaitForResponse, false);
8fa35473
LOK
929}
930
d9de2aae 931bool CCECCommandHandler::TransmitRequestPowerStatus(const cec_logical_address iInitiator, const cec_logical_address iDestination, bool bUpdate, bool bWaitForResponse /* = true */)
8fa35473 932{
37383dcc
LOK
933 if (iDestination == CECDEVICE_TV)
934 {
935 int64_t now(GetTimeMs());
d9de2aae 936 if (!bUpdate && now - m_iPowerStatusRequested < REQUEST_POWER_STATUS_TIMEOUT)
37383dcc
LOK
937 return true;
938 m_iPowerStatusRequested = now;
939 }
940
941 LIB_CEC->AddLog(CEC_LOG_DEBUG, "<< requesting power status of '%s' (%X)", m_busDevice->GetLogicalAddressName(), iDestination);
942
8fa35473 943 cec_command command;
ae693aaa 944 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_GIVE_DEVICE_POWER_STATUS);
8fa35473 945
2b44051c 946 return Transmit(command, !bWaitForResponse, false);
8fa35473
LOK
947}
948
a75e3a5a 949bool CCECCommandHandler::TransmitRequestVendorId(const cec_logical_address iInitiator, const cec_logical_address iDestination, bool bWaitForResponse /* = true */)
8fa35473
LOK
950{
951 cec_command command;
ae693aaa 952 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_GIVE_DEVICE_VENDOR_ID);
8fa35473 953
2b44051c 954 return Transmit(command, !bWaitForResponse, false);
8fa35473
LOK
955}
956
2b44051c 957bool CCECCommandHandler::TransmitActiveSource(const cec_logical_address iInitiator, uint16_t iPhysicalAddress, bool bIsReply)
8fa35473
LOK
958{
959 cec_command command;
ae693aaa 960 cec_command::Format(command, iInitiator, CECDEVICE_BROADCAST, CEC_OPCODE_ACTIVE_SOURCE);
8fa35473
LOK
961 command.parameters.PushBack((uint8_t) ((iPhysicalAddress >> 8) & 0xFF));
962 command.parameters.PushBack((uint8_t) (iPhysicalAddress & 0xFF));
963
2b44051c 964 return Transmit(command, false, bIsReply);
8fa35473
LOK
965}
966
2b44051c 967bool CCECCommandHandler::TransmitCECVersion(const cec_logical_address iInitiator, const cec_logical_address iDestination, cec_version cecVersion, bool bIsReply)
8fa35473
LOK
968{
969 cec_command command;
ae693aaa 970 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_CEC_VERSION);
8fa35473
LOK
971 command.parameters.PushBack((uint8_t)cecVersion);
972
2b44051c 973 return Transmit(command, false, bIsReply);
8fa35473
LOK
974}
975
976bool CCECCommandHandler::TransmitInactiveSource(const cec_logical_address iInitiator, uint16_t iPhysicalAddress)
977{
978 cec_command command;
ae693aaa 979 cec_command::Format(command, iInitiator, CECDEVICE_TV, CEC_OPCODE_INACTIVE_SOURCE);
8fa35473
LOK
980 command.parameters.PushBack((iPhysicalAddress >> 8) & 0xFF);
981 command.parameters.PushBack(iPhysicalAddress & 0xFF);
982
2b44051c 983 return Transmit(command, false, false);
8fa35473
LOK
984}
985
2b44051c 986bool CCECCommandHandler::TransmitMenuState(const cec_logical_address iInitiator, const cec_logical_address iDestination, cec_menu_state menuState, bool bIsReply)
8fa35473
LOK
987{
988 cec_command command;
ae693aaa 989 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_MENU_STATUS);
8fa35473
LOK
990 command.parameters.PushBack((uint8_t)menuState);
991
2b44051c 992 return Transmit(command, false, bIsReply);
8fa35473
LOK
993}
994
2b44051c 995bool CCECCommandHandler::TransmitOSDName(const cec_logical_address iInitiator, const cec_logical_address iDestination, std::string strDeviceName, bool bIsReply)
8fa35473
LOK
996{
997 cec_command command;
ae693aaa 998 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_SET_OSD_NAME);
6f14b512 999 for (size_t iPtr = 0; iPtr < strDeviceName.length(); iPtr++)
8fa35473
LOK
1000 command.parameters.PushBack(strDeviceName.at(iPtr));
1001
2b44051c 1002 return Transmit(command, false, bIsReply);
8fa35473
LOK
1003}
1004
2b44051c 1005bool CCECCommandHandler::TransmitOSDString(const cec_logical_address iInitiator, const cec_logical_address iDestination, cec_display_control duration, const char *strMessage, bool bIsReply)
8fa35473
LOK
1006{
1007 cec_command command;
ae693aaa 1008 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_SET_OSD_STRING);
8fa35473
LOK
1009 command.parameters.PushBack((uint8_t)duration);
1010
6f14b512 1011 size_t iLen = strlen(strMessage);
8fa35473
LOK
1012 if (iLen > 13) iLen = 13;
1013
6f14b512 1014 for (size_t iPtr = 0; iPtr < iLen; iPtr++)
8fa35473
LOK
1015 command.parameters.PushBack(strMessage[iPtr]);
1016
2b44051c 1017 return Transmit(command, false, bIsReply);
8fa35473
LOK
1018}
1019
2b44051c 1020bool CCECCommandHandler::TransmitPhysicalAddress(const cec_logical_address iInitiator, uint16_t iPhysicalAddress, cec_device_type type, bool bIsReply)
8fa35473
LOK
1021{
1022 cec_command command;
ae693aaa 1023 cec_command::Format(command, iInitiator, CECDEVICE_BROADCAST, CEC_OPCODE_REPORT_PHYSICAL_ADDRESS);
8fa35473
LOK
1024 command.parameters.PushBack((uint8_t) ((iPhysicalAddress >> 8) & 0xFF));
1025 command.parameters.PushBack((uint8_t) (iPhysicalAddress & 0xFF));
1026 command.parameters.PushBack((uint8_t) (type));
1027
2b44051c 1028 return Transmit(command, false, bIsReply);
fbdea54c
MK
1029}
1030
a1ef75fb 1031bool CCECCommandHandler::TransmitSetMenuLanguage(const cec_logical_address iInitiator, const char lang[4], bool bIsReply)
fbdea54c
MK
1032{
1033 cec_command command;
1034 command.Format(command, iInitiator, CECDEVICE_BROADCAST, CEC_OPCODE_SET_MENU_LANGUAGE);
1035 command.parameters.PushBack((uint8_t) lang[0]);
1036 command.parameters.PushBack((uint8_t) lang[1]);
1037 command.parameters.PushBack((uint8_t) lang[2]);
1038
2b44051c 1039 return Transmit(command, false, bIsReply);
8fa35473
LOK
1040}
1041
2b44051c 1042bool CCECCommandHandler::TransmitPoll(const cec_logical_address iInitiator, const cec_logical_address iDestination, bool bIsReply)
8fa35473
LOK
1043{
1044 cec_command command;
ae693aaa 1045 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_NONE);
8fa35473 1046
2b44051c 1047 return Transmit(command, false, bIsReply);
8fa35473
LOK
1048}
1049
2b44051c 1050bool CCECCommandHandler::TransmitPowerState(const cec_logical_address iInitiator, const cec_logical_address iDestination, cec_power_status state, bool bIsReply)
8fa35473
LOK
1051{
1052 cec_command command;
ae693aaa 1053 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_REPORT_POWER_STATUS);
8fa35473
LOK
1054 command.parameters.PushBack((uint8_t) state);
1055
2b44051c 1056 return Transmit(command, false, bIsReply);
8fa35473
LOK
1057}
1058
869ff027 1059bool CCECCommandHandler::TransmitVendorID(const cec_logical_address iInitiator, const cec_logical_address UNUSED(iDestination), uint64_t iVendorId, bool bIsReply)
8fa35473
LOK
1060{
1061 cec_command command;
ae693aaa 1062 cec_command::Format(command, iInitiator, CECDEVICE_BROADCAST, CEC_OPCODE_DEVICE_VENDOR_ID);
8fa35473
LOK
1063
1064 command.parameters.PushBack((uint8_t) (((uint64_t)iVendorId >> 16) & 0xFF));
1065 command.parameters.PushBack((uint8_t) (((uint64_t)iVendorId >> 8) & 0xFF));
1066 command.parameters.PushBack((uint8_t) ((uint64_t)iVendorId & 0xFF));
1067
2b44051c 1068 return Transmit(command, false, bIsReply);
8fa35473
LOK
1069}
1070
2b44051c 1071bool CCECCommandHandler::TransmitAudioStatus(const cec_logical_address iInitiator, const cec_logical_address iDestination, uint8_t state, bool bIsReply)
8fa35473
LOK
1072{
1073 cec_command command;
ae693aaa 1074 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_REPORT_AUDIO_STATUS);
8fa35473
LOK
1075 command.parameters.PushBack(state);
1076
2b44051c 1077 return Transmit(command, false, bIsReply);
8fa35473
LOK
1078}
1079
2b44051c 1080bool CCECCommandHandler::TransmitSetSystemAudioMode(const cec_logical_address iInitiator, const cec_logical_address iDestination, cec_system_audio_status state, bool bIsReply)
8fa35473
LOK
1081{
1082 cec_command command;
ae693aaa 1083 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_SET_SYSTEM_AUDIO_MODE);
8fa35473
LOK
1084 command.parameters.PushBack((uint8_t)state);
1085
2b44051c 1086 return Transmit(command, false, bIsReply);
8fa35473
LOK
1087}
1088
2b44051c 1089bool CCECCommandHandler::TransmitSetStreamPath(uint16_t iStreamPath, bool bIsReply)
f42d3e0f 1090{
2b44051c
LOK
1091 if (m_busDevice->GetLogicalAddress() != CECDEVICE_TV)
1092 {
1093 LIB_CEC->AddLog(CEC_LOG_ERROR, "only the TV is allowed to send CEC_OPCODE_SET_STREAM_PATH");
1094 return false;
1095 }
f42d3e0f
LOK
1096 cec_command command;
1097 cec_command::Format(command, m_busDevice->GetLogicalAddress(), CECDEVICE_BROADCAST, CEC_OPCODE_SET_STREAM_PATH);
1098 command.parameters.PushBack((uint8_t) ((iStreamPath >> 8) & 0xFF));
1099 command.parameters.PushBack((uint8_t) (iStreamPath & 0xFF));
1100
2b44051c 1101 return Transmit(command, false, bIsReply);
f42d3e0f
LOK
1102}
1103
2b44051c 1104bool CCECCommandHandler::TransmitSystemAudioModeStatus(const cec_logical_address iInitiator, const cec_logical_address iDestination, cec_system_audio_status state, bool bIsReply)
8fa35473
LOK
1105{
1106 cec_command command;
ae693aaa 1107 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_SYSTEM_AUDIO_MODE_STATUS);
8fa35473
LOK
1108 command.parameters.PushBack((uint8_t)state);
1109
2b44051c 1110 return Transmit(command, false, bIsReply);
8fa35473
LOK
1111}
1112
2b44051c 1113bool CCECCommandHandler::TransmitDeckStatus(const cec_logical_address iInitiator, const cec_logical_address iDestination, cec_deck_info state, bool bIsReply)
8fa35473
LOK
1114{
1115 cec_command command;
ae693aaa 1116 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_DECK_STATUS);
8fa35473
LOK
1117 command.PushBack((uint8_t)state);
1118
2b44051c 1119 return Transmit(command, false, bIsReply);
8fa35473
LOK
1120}
1121
4bec9d79 1122bool CCECCommandHandler::TransmitKeypress(const cec_logical_address iInitiator, const cec_logical_address iDestination, cec_user_control_code key, bool bWait /* = true */)
8fa35473
LOK
1123{
1124 cec_command command;
ae693aaa 1125 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_USER_CONTROL_PRESSED);
8fa35473
LOK
1126 command.parameters.PushBack((uint8_t)key);
1127
2b44051c 1128 return Transmit(command, !bWait, false);
8fa35473
LOK
1129}
1130
4bec9d79 1131bool CCECCommandHandler::TransmitKeyRelease(const cec_logical_address iInitiator, const cec_logical_address iDestination, bool bWait /* = true */)
8fa35473
LOK
1132{
1133 cec_command command;
ae693aaa 1134 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_USER_CONTROL_RELEASE);
8fa35473 1135
2b44051c 1136 return Transmit(command, !bWait, false);
8fa35473
LOK
1137}
1138
2b44051c 1139bool CCECCommandHandler::Transmit(cec_command &command, bool bSuppressWait, bool bIsReply)
8fa35473 1140{
b64db02e 1141 bool bReturn(false);
4fde2fd0
LOK
1142 cec_opcode expectedResponse(cec_command::GetResponseOpcode(command.opcode));
1143 bool bExpectResponse(expectedResponse != CEC_OPCODE_NONE && !bSuppressWait);
ae693aaa 1144 command.transmit_timeout = m_iTransmitTimeout;
ae693aaa 1145
7e4558f1
LOK
1146 if (command.initiator == CECDEVICE_UNKNOWN)
1147 {
004b8382 1148 LIB_CEC->AddLog(CEC_LOG_ERROR, "not transmitting a command without a valid initiator");
7e4558f1
LOK
1149 return bReturn;
1150 }
1151
fd415a2a
LOK
1152 // check whether the destination is not marked as not present or handled by libCEC
1153 if (command.destination != CECDEVICE_BROADCAST && command.opcode_set)
1154 {
1155 CCECBusDevice* destinationDevice = m_processor->GetDevice(command.destination);
1156 cec_bus_device_status status = destinationDevice ? destinationDevice->GetStatus() : CEC_DEVICE_STATUS_NOT_PRESENT;
1157 if (status == CEC_DEVICE_STATUS_NOT_PRESENT)
1158 {
1159 LIB_CEC->AddLog(CEC_LOG_DEBUG, "not sending command '%s': destination device '%s' marked as not present", ToString(command.opcode),ToString(command.destination));
1160 return bReturn;
1161 }
1162 else if (status == CEC_DEVICE_STATUS_HANDLED_BY_LIBCEC)
1163 {
1164 LIB_CEC->AddLog(CEC_LOG_DEBUG, "not sending command '%s': destination device '%s' marked as handled by libCEC", ToString(command.opcode),ToString(command.destination));
1165 return bReturn;
1166 }
cb339764
LOK
1167 else if (destinationDevice->IsUnsupportedFeature(command.opcode))
1168 {
1169 return true;
1170 }
fd415a2a
LOK
1171 }
1172
ae693aaa 1173 {
d0e533d0 1174 uint8_t iTries(0), iMaxTries(m_iTransmitRetries + 1);
cb339764 1175 while (!bReturn && ++iTries <= iMaxTries)
b64db02e 1176 {
2b44051c 1177 if ((bReturn = m_processor->Transmit(command, bIsReply)) == true)
19cbfa8f 1178 {
b509ba1a 1179#ifdef CEC_DEBUGGING
004b8382 1180 LIB_CEC->AddLog(CEC_LOG_DEBUG, "command transmitted");
b509ba1a 1181#endif
4478bc79 1182 if (bExpectResponse)
24dd566c 1183 {
060a7b5e 1184 bReturn = m_busDevice->WaitForOpcode(expectedResponse);
004b8382 1185 LIB_CEC->AddLog(CEC_LOG_DEBUG, bReturn ? "expected response received (%X: %s)" : "expected response not received (%X: %s)", (int)expectedResponse, ToString(expectedResponse));
24dd566c 1186 }
19cbfa8f 1187 }
b64db02e 1188 }
ae693aaa 1189 }
8fa35473 1190
b64db02e 1191 return bReturn;
8fa35473 1192}
83be0701 1193
aa4c0d34 1194bool CCECCommandHandler::ActivateSource(bool bTransmitDelayedCommandsOnly /* = false */)
83be0701 1195{
c4287bcd 1196 if (m_busDevice->IsActiveSource() &&
aa4c0d34 1197 m_busDevice->IsHandledByLibCEC())
c4287bcd 1198 {
b0015449
LOK
1199 {
1200 CLockObject lock(m_mutex);
aa4c0d34
LOK
1201 // check if we need to send a delayed source switch
1202 if (bTransmitDelayedCommandsOnly)
1203 {
1204 if (m_iActiveSourcePending == 0 || GetTimeMs() < m_iActiveSourcePending)
1205 return false;
1206
37383dcc 1207#ifdef CEC_DEBUGGING
aa4c0d34 1208 LIB_CEC->AddLog(CEC_LOG_DEBUG, "transmitting delayed activate source command");
37383dcc 1209#endif
aa4c0d34 1210 }
b0015449
LOK
1211 }
1212
aa4c0d34 1213 // update the power state and menu state
05529b92
LOK
1214 if (!bTransmitDelayedCommandsOnly)
1215 {
1216 m_busDevice->SetPowerStatus(CEC_POWER_STATUS_ON);
1217 m_busDevice->SetMenuState(CEC_MENU_STATE_ACTIVATED);
1218 }
7b01619d
LOK
1219
1220 // vendor specific hook
1221 VendorPreActivateSourceHook();
aa4c0d34
LOK
1222
1223 // power on the TV
c4a070be
LOK
1224 CCECBusDevice* tv = m_processor->GetDevice(CECDEVICE_TV);
1225 bool bTvPresent = (tv && tv->GetStatus() == CEC_DEVICE_STATUS_PRESENT);
1226 bool bActiveSourceFailed(false);
1227 if (bTvPresent)
d9de2aae 1228 bActiveSourceFailed = !tv->PowerOn(m_busDevice->GetLogicalAddress());
c4a070be
LOK
1229 else
1230 LIB_CEC->AddLog(CEC_LOG_DEBUG, "TV not present, not sending 'image view on'");
c4287bcd 1231
aa4c0d34
LOK
1232 // check if we're allowed to switch sources
1233 bool bSourceSwitchAllowed = SourceSwitchAllowed();
1234 if (!bSourceSwitchAllowed)
1235 LIB_CEC->AddLog(CEC_LOG_DEBUG, "source switch is currently not allowed by command handler");
dbad810a 1236
aa4c0d34
LOK
1237 // switch sources (if allowed)
1238 if (!bActiveSourceFailed && bSourceSwitchAllowed)
b0015449 1239 {
c4a070be
LOK
1240 bActiveSourceFailed = !m_busDevice->TransmitActiveSource(false);
1241 if (bTvPresent && !bActiveSourceFailed)
1242 bActiveSourceFailed = !m_busDevice->TransmitMenuState(CECDEVICE_TV, false);
aa4c0d34
LOK
1243
1244 // update the deck status for playback devices
c4a070be 1245 if (bTvPresent && !bActiveSourceFailed)
aa4c0d34
LOK
1246 {
1247 CCECPlaybackDevice *playbackDevice = m_busDevice->AsPlaybackDevice();
1248 if (playbackDevice && SendDeckStatusUpdateOnActiveSource())
2b44051c 1249 bActiveSourceFailed = !playbackDevice->TransmitDeckStatus(CECDEVICE_TV, false);
aa4c0d34 1250 }
93efea2a
LOK
1251
1252 // update system audio mode for audiosystem devices
1253 if (bTvPresent && !bActiveSourceFailed)
1254 {
1255 CCECAudioSystem* audioDevice = m_busDevice->AsAudioSystem();
1256 if (audioDevice)
1257 bActiveSourceFailed = !audioDevice->TransmitSetSystemAudioMode(CECDEVICE_TV, false);
1258 }
dbad810a
LOK
1259 }
1260
aa4c0d34
LOK
1261 // retry later
1262 if (bActiveSourceFailed || !bSourceSwitchAllowed)
dbad810a
LOK
1263 {
1264 LIB_CEC->AddLog(CEC_LOG_DEBUG, "failed to make '%s' the active source. will retry later", m_busDevice->GetLogicalAddressName());
085df309 1265 int64_t now(GetTimeMs());
548c81b3 1266 CLockObject lock(m_mutex);
085df309
LOK
1267 if (m_iActiveSourcePending == 0 || m_iActiveSourcePending < now)
1268 m_iActiveSourcePending = now + (int64_t)CEC_ACTIVE_SOURCE_SWITCH_RETRY_TIME_MS;
b0015449
LOK
1269 return false;
1270 }
045b2e29
LOK
1271 else
1272 {
1273 CLockObject lock(m_mutex);
1274 // clear previous pending active source command
1275 m_iActiveSourcePending = 0;
1276 }
aa40e0e2 1277
aa4c0d34
LOK
1278 // mark the handler as initialised
1279 CLockObject lock(m_mutex);
c4287bcd 1280 m_bHandlerInited = true;
83be0701 1281 }
7dc58c9f 1282 return true;
83be0701 1283}
b499cf16 1284
060a7b5e 1285void CCECCommandHandler::ScheduleActivateSource(uint64_t iDelay)
b499cf16 1286{
060a7b5e
LOK
1287 CLockObject lock(m_mutex);
1288 m_iActiveSourcePending = GetTimeMs() + iDelay;
b499cf16 1289}
9169d73e
LOK
1290
1291void CCECCommandHandler::RequestEmailFromCustomer(const cec_command& command)
1292{
1293 bool bInserted(false);
1294 map<cec_opcode, vector<cec_command> >::iterator it = m_logsRequested.find(command.opcode);
1295 if (it != m_logsRequested.end())
1296 {
1297 for (vector<cec_command>::const_iterator it2 = it->second.begin(); it2 != it->second.end(); it2++)
1298 {
1299 // we already logged this one
1300 if ((*it2).parameters == command.parameters)
1301 return;
1302 }
1303
1304 it->second.push_back(command);
1305 bInserted = true;
1306 }
1307
1308 if (!bInserted)
1309 {
1310 vector<cec_command> commands;
1311 commands.push_back(command);
1312 m_logsRequested.insert(make_pair(command.opcode, commands));
1313 }
1314
7f913ac9 1315 LIB_CEC->AddLog(CEC_LOG_NOTICE, "Unmapped code detected. Please send an email to support@pulse-eight.com with the following details, and if you pressed a key, tell us which one you pressed, and we'll add support for this it.\nCEC command: %s\nVendor ID: %s (%06x)", ToString(command).c_str(), ToString(m_vendorId), m_vendorId);
9169d73e
LOK
1316}
1317