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