cec: changed all Handle...() commands to return a cec_abort_reason and send the corre...
[deb_libcec.git] / src / lib / implementations / CECCommandHandler.cpp
CommitLineData
e9de9629
LOK
1/*
2 * This file is part of the libCEC(R) library.
3 *
b492c10e 4 * libCEC(R) is Copyright (C) 2011-2012 Pulse-Eight Limited. All rights reserved.
e9de9629
LOK
5 * libCEC(R) is an original work, containing original code.
6 *
7 * libCEC(R) is a trademark of Pulse-Eight Limited.
8 *
9 * This program is dual-licensed; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22 *
23 *
24 * Alternatively, you can license this library under a commercial license,
25 * please contact Pulse-Eight Licensing for more information.
26 *
27 * For more information contact:
28 * Pulse-Eight Licensing <license@pulse-eight.com>
29 * http://www.pulse-eight.com/
30 * http://www.pulse-eight.net/
31 */
32
33#include "CECCommandHandler.h"
eafa9d46 34#include "../devices/CECBusDevice.h"
a1f8fb1b 35#include "../devices/CECAudioSystem.h"
28089abc 36#include "../devices/CECPlaybackDevice.h"
004b8382 37#include "../CECClient.h"
387b6f6f 38#include "../CECProcessor.h"
5477a250 39#include "../LibCEC.h"
0d800fe5 40#include "../CECTypeUtils.h"
c9d15485 41#include "../platform/util/util.h"
e9de9629
LOK
42
43using namespace CEC;
8747dd4f 44using namespace std;
f00ff009 45using namespace PLATFORM;
e9de9629 46
004b8382 47#define LIB_CEC m_busDevice->GetProcessor()->GetLib()
0d800fe5 48#define ToString(p) CCECTypeUtils::ToString(p)
004b8382 49
8fa35473
LOK
50CCECCommandHandler::CCECCommandHandler(CCECBusDevice *busDevice) :
51 m_busDevice(busDevice),
52 m_processor(m_busDevice->GetProcessor()),
ae693aaa
LOK
53 m_iTransmitTimeout(CEC_DEFAULT_TRANSMIT_TIMEOUT),
54 m_iTransmitWait(CEC_DEFAULT_TRANSMIT_WAIT),
89a726fa 55 m_iTransmitRetries(CEC_DEFAULT_TRANSMIT_RETRIES),
b64db02e 56 m_bHandlerInited(false),
d79b67d8 57 m_bOPTSendDeckStatusUpdateOnActiveSource(false),
960f33c6 58 m_vendorId(CEC_VENDOR_UNKNOWN),
b0015449
LOK
59 m_waitForResponse(new CWaitForResponse),
60 m_bActiveSourcePending(false)
e9de9629 61{
8fa35473
LOK
62}
63
64CCECCommandHandler::~CCECCommandHandler(void)
65{
c9d15485 66 DELETE_AND_NULL(m_waitForResponse);
e9de9629
LOK
67}
68
69bool CCECCommandHandler::HandleCommand(const cec_command &command)
70{
d297cbd4
LOK
71 if (command.opcode_set == 0)
72 return HandlePoll(command);
73
9a54dc82 74 int iHandled(CEC_ABORT_REASON_UNRECOGNIZED_OPCODE);
e9de9629 75
004b8382
LOK
76 CCECClient *client = m_busDevice->GetClient();
77 if (client)
78 client->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)
24dd566c 200 m_waitForResponse->Received((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]);
004b8382
LOK
212 CCECBusDevice *device = m_processor->GetDeviceByPhysicalAddress(iAddress);
213 if (device)
9a54dc82 214 {
004b8382 215 device->MarkAsActiveSource();
9a54dc82
LOK
216 return COMMAND_HANDLED;
217 }
be5b0e24
LOK
218 }
219
9a54dc82 220 return CEC_ABORT_REASON_INVALID_OPERAND;
be5b0e24
LOK
221}
222
9a54dc82 223int CCECCommandHandler::HandleDeckControl(const cec_command &command)
a9232a79 224{
aa40e0e2
LOK
225 CCECPlaybackDevice *device = CCECBusDevice::AsPlaybackDevice(GetDevice(command.destination));
226 if (device && command.parameters.size > 0)
a9232a79 227 {
aa40e0e2 228 device->SetDeckControlMode((cec_deck_control_mode) command.parameters[0]);
9a54dc82 229 return COMMAND_HANDLED;
a9232a79
LOK
230 }
231
9a54dc82 232 return CEC_ABORT_REASON_INVALID_OPERAND;
a9232a79
LOK
233}
234
9a54dc82 235int CCECCommandHandler::HandleDeviceCecVersion(const cec_command &command)
6a1c0009
LOK
236{
237 if (command.parameters.size == 1)
238 {
239 CCECBusDevice *device = GetDevice(command.initiator);
240 if (device)
241 device->SetCecVersion((cec_version) command.parameters[0]);
9a54dc82
LOK
242
243 return COMMAND_HANDLED;
6a1c0009
LOK
244 }
245
9a54dc82 246 return CEC_ABORT_REASON_INVALID_OPERAND;
6a1c0009
LOK
247}
248
9a54dc82 249int CCECCommandHandler::HandleDeviceVendorCommandWithId(const cec_command &command)
e9de9629 250{
9a54dc82 251 return CEC_ABORT_REASON_REFUSED;
e9de9629
LOK
252}
253
9a54dc82 254int CCECCommandHandler::HandleDeviceVendorId(const cec_command &command)
e9de9629 255{
9a54dc82
LOK
256 SetVendorId(command);
257 return COMMAND_HANDLED;
e9de9629
LOK
258}
259
9a54dc82 260int CCECCommandHandler::HandleFeatureAbort(const cec_command &command)
4d738fe3 261{
69a1a673
LOK
262 if (command.parameters.size == 2 &&
263 (command.parameters[1] == CEC_ABORT_REASON_UNRECOGNIZED_OPCODE ||
264 command.parameters[1] == CEC_ABORT_REASON_REFUSED))
004b8382 265 m_processor->GetDevice(command.initiator)->SetUnsupportedFeature((cec_opcode)command.parameters[0]);
9a54dc82 266 return COMMAND_HANDLED;
4d738fe3
LOK
267}
268
9a54dc82 269int CCECCommandHandler::HandleGetCecVersion(const cec_command &command)
e9de9629 270{
0b8c7eab 271 if (m_processor->CECInitialised() && m_processor->IsHandledByLibCEC(command.destination))
6b72afcd
LOK
272 {
273 CCECBusDevice *device = GetDevice(command.destination);
9a54dc82
LOK
274 if (device && device->TransmitCECVersion(command.initiator))
275 return COMMAND_HANDLED;
276 return CEC_ABORT_REASON_INVALID_OPERAND;
6b72afcd 277 }
0f23c85c 278
9a54dc82 279 return CEC_ABORT_REASON_NOT_IN_CORRECT_MODE_TO_RESPOND;
e9de9629
LOK
280}
281
9a54dc82 282int CCECCommandHandler::HandleGiveAudioStatus(const cec_command &command)
a1f8fb1b 283{
0b8c7eab 284 if (m_processor->CECInitialised() && m_processor->IsHandledByLibCEC(command.destination))
6b72afcd 285 {
aa40e0e2 286 CCECAudioSystem *device = CCECBusDevice::AsAudioSystem(GetDevice(command.destination));
9a54dc82
LOK
287 if (device && device->TransmitAudioStatus(command.initiator))
288 return COMMAND_HANDLED;
289 return CEC_ABORT_REASON_INVALID_OPERAND;
6b72afcd 290 }
a1f8fb1b 291
9a54dc82 292 return CEC_ABORT_REASON_NOT_IN_CORRECT_MODE_TO_RESPOND;
a1f8fb1b
LOK
293}
294
9a54dc82 295int CCECCommandHandler::HandleGiveDeckStatus(const cec_command &command)
e9de9629 296{
0b8c7eab 297 if (m_processor->CECInitialised() && m_processor->IsHandledByLibCEC(command.destination))
6b72afcd 298 {
aa40e0e2 299 CCECPlaybackDevice *device = CCECBusDevice::AsPlaybackDevice(GetDevice(command.destination));
9a54dc82
LOK
300 if (device && device->TransmitDeckStatus(command.initiator))
301 return COMMAND_HANDLED;
302 return CEC_ABORT_REASON_INVALID_OPERAND;
6b72afcd 303 }
0f23c85c 304
9a54dc82 305 return CEC_ABORT_REASON_NOT_IN_CORRECT_MODE_TO_RESPOND;
e9de9629
LOK
306}
307
9a54dc82 308int CCECCommandHandler::HandleGiveDevicePowerStatus(const cec_command &command)
e9de9629 309{
0b8c7eab 310 if (m_processor->CECInitialised() && m_processor->IsHandledByLibCEC(command.destination))
6b72afcd
LOK
311 {
312 CCECBusDevice *device = GetDevice(command.destination);
9a54dc82
LOK
313 if (device && device->TransmitPowerState(command.initiator))
314 return COMMAND_HANDLED;
315 return CEC_ABORT_REASON_INVALID_OPERAND;
6b72afcd 316 }
0f23c85c 317
9a54dc82 318 return CEC_ABORT_REASON_INVALID_OPERAND;
e9de9629
LOK
319}
320
9a54dc82 321int CCECCommandHandler::HandleGiveDeviceVendorId(const cec_command &command)
e9de9629 322{
0b8c7eab 323 if (m_processor->CECInitialised() && m_processor->IsHandledByLibCEC(command.destination))
6b72afcd
LOK
324 {
325 CCECBusDevice *device = GetDevice(command.destination);
9a54dc82
LOK
326 if (device && device->TransmitVendorID(command.initiator))
327 return COMMAND_HANDLED;
6b72afcd 328 }
0f23c85c 329
9a54dc82 330 return CEC_ABORT_REASON_INVALID_OPERAND;
e9de9629
LOK
331}
332
9a54dc82 333int CCECCommandHandler::HandleGiveOSDName(const cec_command &command)
e9de9629 334{
0b8c7eab 335 if (m_processor->CECInitialised() && m_processor->IsHandledByLibCEC(command.destination))
6b72afcd
LOK
336 {
337 CCECBusDevice *device = GetDevice(command.destination);
9a54dc82
LOK
338 if (device && device->TransmitOSDName(command.initiator))
339 return COMMAND_HANDLED;
6b72afcd 340 }
0f23c85c 341
9a54dc82 342 return CEC_ABORT_REASON_INVALID_OPERAND;
e9de9629
LOK
343}
344
9a54dc82 345int CCECCommandHandler::HandleGivePhysicalAddress(const cec_command &command)
e9de9629 346{
0b8c7eab 347 if (m_processor->CECInitialised() && m_processor->IsHandledByLibCEC(command.destination))
6b72afcd
LOK
348 {
349 CCECBusDevice *device = GetDevice(command.destination);
9a54dc82
LOK
350 if (device && device->TransmitPhysicalAddress())
351 return COMMAND_HANDLED;
352 return CEC_ABORT_REASON_INVALID_OPERAND;
6b72afcd 353 }
09c10b66 354
9a54dc82 355 return CEC_ABORT_REASON_NOT_IN_CORRECT_MODE_TO_RESPOND;
e9de9629
LOK
356}
357
9a54dc82 358int CCECCommandHandler::HandleGiveMenuLanguage(const cec_command &command)
fbdea54c 359{
0b8c7eab 360 if (m_processor->CECInitialised() && m_processor->IsHandledByLibCEC(command.destination))
fbdea54c
MK
361 {
362 CCECBusDevice *device = GetDevice(command.destination);
9a54dc82
LOK
363 if (device && device->TransmitSetMenuLanguage(command.initiator))
364 return COMMAND_HANDLED;
365 return CEC_ABORT_REASON_INVALID_OPERAND;
fbdea54c
MK
366 }
367
9a54dc82 368 return CEC_ABORT_REASON_NOT_IN_CORRECT_MODE_TO_RESPOND;
fbdea54c
MK
369}
370
9a54dc82 371int CCECCommandHandler::HandleGiveSystemAudioModeStatus(const cec_command &command)
1a6669b8 372{
0b8c7eab 373 if (m_processor->CECInitialised() && m_processor->IsHandledByLibCEC(command.destination))
1a6669b8 374 {
aa40e0e2 375 CCECAudioSystem *device = CCECBusDevice::AsAudioSystem(GetDevice(command.destination));
9a54dc82
LOK
376 if (device && device->TransmitSystemAudioModeStatus(command.initiator))
377 return COMMAND_HANDLED;
378 return CEC_ABORT_REASON_INVALID_OPERAND;
1a6669b8
LOK
379 }
380
9a54dc82 381 return CEC_ABORT_REASON_NOT_IN_CORRECT_MODE_TO_RESPOND;
1a6669b8
LOK
382}
383
9a54dc82 384int CCECCommandHandler::HandleImageViewOn(const cec_command &command)
1a6669b8 385{
004b8382 386 m_processor->GetDevice(command.initiator)->MarkAsActiveSource();
9a54dc82 387 return COMMAND_HANDLED;
1a6669b8
LOK
388}
389
9a54dc82 390int CCECCommandHandler::HandleMenuRequest(const cec_command &command)
e9de9629 391{
0b8c7eab 392 if (m_processor->CECInitialised() && m_processor->IsHandledByLibCEC(command.destination))
0f23c85c 393 {
e1804a4e
LOK
394 CCECBusDevice *device = GetDevice(command.destination);
395 if (device)
6b72afcd 396 {
004b8382
LOK
397 CCECClient *client = device->GetClient();
398 if (client)
e1804a4e 399 {
004b8382
LOK
400 if (command.parameters[0] == CEC_MENU_REQUEST_TYPE_ACTIVATE)
401 {
402 if (client->MenuStateChanged(CEC_MENU_STATE_ACTIVATED) == 1)
403 device->SetMenuState(CEC_MENU_STATE_ACTIVATED);
404 }
405 else if (command.parameters[0] == CEC_MENU_REQUEST_TYPE_DEACTIVATE)
406 {
407 if (client->MenuStateChanged(CEC_MENU_STATE_DEACTIVATED) == 1)
408 device->SetMenuState(CEC_MENU_STATE_DEACTIVATED);
409 }
e1804a4e 410 }
9a54dc82
LOK
411 if (device->TransmitMenuState(command.initiator))
412 return COMMAND_HANDLED;
6b72afcd 413 }
9a54dc82 414 return CEC_ABORT_REASON_INVALID_OPERAND;
15d1a84c
LOK
415 }
416
9a54dc82 417 return CEC_ABORT_REASON_NOT_IN_CORRECT_MODE_TO_RESPOND;
15d1a84c
LOK
418}
419
d297cbd4
LOK
420bool CCECCommandHandler::HandlePoll(const cec_command &command)
421{
a75e3a5a 422 m_busDevice->HandlePoll(command.destination);
d297cbd4
LOK
423 return true;
424}
425
9a54dc82 426int CCECCommandHandler::HandleReportAudioStatus(const cec_command &command)
15d1a84c
LOK
427{
428 if (command.parameters.size == 1)
429 {
aa40e0e2
LOK
430 CCECAudioSystem *device = CCECBusDevice::AsAudioSystem(GetDevice(command.initiator));
431 if (device)
6b72afcd 432 {
aa40e0e2 433 device->SetAudioStatus(command.parameters[0]);
9a54dc82 434 return COMMAND_HANDLED;
6b72afcd 435 }
0f23c85c 436 }
9a54dc82 437 return CEC_ABORT_REASON_INVALID_OPERAND;
e9de9629
LOK
438}
439
9a54dc82 440int CCECCommandHandler::HandleReportPhysicalAddress(const cec_command &command)
907bd60f 441{
0bfce006 442 if (command.parameters.size == 3)
907bd60f
LOK
443 {
444 uint16_t iNewAddress = ((uint16_t)command.parameters[0] << 8) | ((uint16_t)command.parameters[1]);
16b1e052 445 SetPhysicalAddress(command.initiator, iNewAddress);
9a54dc82 446 return COMMAND_HANDLED;
907bd60f 447 }
9a54dc82 448 return CEC_ABORT_REASON_INVALID_OPERAND;
907bd60f
LOK
449}
450
9a54dc82 451int CCECCommandHandler::HandleReportPowerStatus(const cec_command &command)
e55f3f70
LOK
452{
453 if (command.parameters.size == 1)
454 {
455 CCECBusDevice *device = GetDevice(command.initiator);
456 if (device)
9a54dc82 457 {
e55f3f70 458 device->SetPowerStatus((cec_power_status) command.parameters[0]);
9a54dc82
LOK
459 return COMMAND_HANDLED;
460 }
e55f3f70 461 }
9a54dc82 462 return CEC_ABORT_REASON_INVALID_OPERAND;
e55f3f70
LOK
463}
464
9a54dc82 465int CCECCommandHandler::HandleRequestActiveSource(const cec_command &command)
e9de9629 466{
0b8c7eab 467 if (m_processor->CECInitialised())
5f316715 468 {
004b8382
LOK
469 LIB_CEC->AddLog(CEC_LOG_DEBUG, ">> %i requests active source", (uint8_t) command.initiator);
470 m_processor->GetDevice(command.initiator)->SetPowerStatus(CEC_POWER_STATUS_ON);
8747dd4f 471
5f316715 472 vector<CCECBusDevice *> devices;
f00ff009 473 for (size_t iDevicePtr = 0; iDevicePtr < GetMyDevices(devices); iDevicePtr++)
5f316715 474 devices[iDevicePtr]->TransmitActiveSource();
5f316715 475 }
9a54dc82
LOK
476
477 return COMMAND_HANDLED;
e9de9629
LOK
478}
479
9a54dc82 480int CCECCommandHandler::HandleRoutingChange(const cec_command &command)
e9de9629
LOK
481{
482 if (command.parameters.size == 4)
483 {
484 uint16_t iOldAddress = ((uint16_t)command.parameters[0] << 8) | ((uint16_t)command.parameters[1]);
485 uint16_t iNewAddress = ((uint16_t)command.parameters[2] << 8) | ((uint16_t)command.parameters[3]);
e9de9629 486
0f23c85c
LOK
487 CCECBusDevice *device = GetDevice(command.initiator);
488 if (device)
9a54dc82 489 {
9dc04b07 490 device->SetStreamPath(iNewAddress, iOldAddress);
9a54dc82
LOK
491 return COMMAND_HANDLED;
492 }
e9de9629 493 }
9a54dc82
LOK
494
495 return CEC_ABORT_REASON_INVALID_OPERAND;
e9de9629
LOK
496}
497
9a54dc82 498int CCECCommandHandler::HandleRoutingInformation(const cec_command &command)
907bd60f
LOK
499{
500 if (command.parameters.size == 2)
501 {
502 uint16_t iNewAddress = ((uint16_t)command.parameters[0] << 8) | ((uint16_t)command.parameters[1]);
004b8382
LOK
503 CCECBusDevice *device = m_processor->GetDeviceByPhysicalAddress(iNewAddress);
504 if (device)
9a54dc82 505 {
004b8382 506 device->MarkAsActiveSource();
9a54dc82
LOK
507 return COMMAND_HANDLED;
508 }
907bd60f 509 }
15d1a84c 510
9a54dc82 511 return CEC_ABORT_REASON_INVALID_OPERAND;
907bd60f
LOK
512}
513
9a54dc82 514int CCECCommandHandler::HandleSetMenuLanguage(const cec_command &command)
a3269a0a
LOK
515{
516 if (command.parameters.size == 3)
517 {
518 CCECBusDevice *device = GetDevice(command.initiator);
519 if (device)
520 {
521 cec_menu_language language;
522 language.device = command.initiator;
56701628 523 for (uint8_t iPtr = 0; iPtr < 4; iPtr++)
a3269a0a
LOK
524 language.language[iPtr] = command.parameters[iPtr];
525 language.language[3] = 0;
526 device->SetMenuLanguage(language);
9a54dc82 527 return COMMAND_HANDLED;
15d1a84c
LOK
528 }
529 }
9a54dc82
LOK
530
531 return CEC_ABORT_REASON_INVALID_OPERAND;
15d1a84c
LOK
532}
533
9a54dc82 534int CCECCommandHandler::HandleSetOSDName(const cec_command &command)
15d1a84c
LOK
535{
536 if (command.parameters.size > 0)
537 {
538 CCECBusDevice *device = GetDevice(command.initiator);
539 if (device)
540 {
541 char buf[1024];
542 for (uint8_t iPtr = 0; iPtr < command.parameters.size; iPtr++)
543 buf[iPtr] = (char)command.parameters[iPtr];
544 buf[command.parameters.size] = 0;
545
546 CStdString strName(buf);
547 device->SetOSDName(strName);
6b72afcd 548
9a54dc82 549 return COMMAND_HANDLED;
a3269a0a
LOK
550 }
551 }
9a54dc82
LOK
552
553 return CEC_ABORT_REASON_INVALID_OPERAND;
a3269a0a
LOK
554}
555
9a54dc82 556int CCECCommandHandler::HandleSetStreamPath(const cec_command &command)
e9de9629 557{
9a54dc82
LOK
558 if (!m_processor->CECInitialised())
559 return CEC_ABORT_REASON_NOT_IN_CORRECT_MODE_TO_RESPOND;
560
561 if (command.parameters.size >= 2)
e9de9629 562 {
b6c7bc94 563 uint16_t iStreamAddress = ((uint16_t)command.parameters[0] << 8) | ((uint16_t)command.parameters[1]);
004b8382 564 LIB_CEC->AddLog(CEC_LOG_DEBUG, ">> %i sets stream path to physical address %04x", command.initiator, iStreamAddress);
72689600
LOK
565
566 /* one of the device handled by libCEC has been made active */
567 CCECBusDevice *device = GetDeviceByPhysicalAddress(iStreamAddress);
004b8382 568 if (device && device->IsHandledByLibCEC())
9a54dc82 569 {
004b8382 570 device->ActivateSource();
9a54dc82
LOK
571 return COMMAND_HANDLED;
572 }
e9de9629 573 }
9a54dc82
LOK
574
575 return CEC_ABORT_REASON_INVALID_OPERAND;
e9de9629
LOK
576}
577
9a54dc82 578int CCECCommandHandler::HandleSystemAudioModeRequest(const cec_command &command)
e5e86c76 579{
0b8c7eab 580 if (m_processor->CECInitialised() && m_processor->IsHandledByLibCEC(command.destination))
e5e86c76 581 {
aa40e0e2
LOK
582 CCECAudioSystem *device = CCECBusDevice::AsAudioSystem(GetDevice(command.destination));
583 if (device)
aa517a0d
LOK
584 {
585 if (command.parameters.size >= 2)
586 {
587 device->SetPowerStatus(CEC_POWER_STATUS_ON);
aa40e0e2 588 device->SetSystemAudioModeStatus(CEC_SYSTEM_AUDIO_STATUS_ON);
aa517a0d
LOK
589 uint16_t iNewAddress = ((uint16_t)command.parameters[0] << 8) | ((uint16_t)command.parameters[1]);
590 CCECBusDevice *newActiveDevice = GetDeviceByPhysicalAddress(iNewAddress);
591 if (newActiveDevice)
004b8382 592 newActiveDevice->MarkAsActiveSource();
9a54dc82
LOK
593 if (device->TransmitSetSystemAudioMode(command.initiator))
594 return COMMAND_HANDLED;
aa517a0d
LOK
595 }
596 else
597 {
aa40e0e2 598 device->SetSystemAudioModeStatus(CEC_SYSTEM_AUDIO_STATUS_OFF);
9a54dc82
LOK
599 if (device->TransmitSetSystemAudioMode(command.initiator))
600 return COMMAND_HANDLED;
aa517a0d
LOK
601 }
602 }
e5e86c76 603 }
9a54dc82
LOK
604
605 return CEC_ABORT_REASON_NOT_IN_CORRECT_MODE_TO_RESPOND;
e5e86c76
LOK
606}
607
9a54dc82 608int CCECCommandHandler::HandleStandby(const cec_command &command)
4d6b4433
LOK
609{
610 CCECBusDevice *device = GetDevice(command.initiator);
611 if (device)
612 device->SetPowerStatus(CEC_POWER_STATUS_STANDBY);
6b72afcd 613
9a54dc82 614 return COMMAND_HANDLED;
4d6b4433
LOK
615}
616
9a54dc82 617int CCECCommandHandler::HandleSystemAudioModeStatus(const cec_command &command)
868dc71f 618{
aa517a0d 619 if (command.parameters.size == 1)
868dc71f 620 {
aa40e0e2
LOK
621 CCECAudioSystem *device = CCECBusDevice::AsAudioSystem(GetDevice(command.initiator));
622 if (device)
aa517a0d 623 {
aa40e0e2 624 device->SetSystemAudioModeStatus((cec_system_audio_status)command.parameters[0]);
9a54dc82 625 return COMMAND_HANDLED;
aa517a0d
LOK
626 }
627 }
628
9a54dc82 629 return CEC_ABORT_REASON_INVALID_OPERAND;
aa517a0d
LOK
630}
631
9a54dc82 632int CCECCommandHandler::HandleSetSystemAudioMode(const cec_command &command)
aa517a0d
LOK
633{
634 if (command.parameters.size == 1)
635 {
aa40e0e2
LOK
636 CCECAudioSystem *device = CCECBusDevice::AsAudioSystem(GetDevice(command.initiator));
637 if (device)
aa517a0d 638 {
aa40e0e2 639 device->SetSystemAudioModeStatus((cec_system_audio_status)command.parameters[0]);
9a54dc82 640 return COMMAND_HANDLED;
aa517a0d 641 }
868dc71f
LOK
642 }
643
9a54dc82 644 return CEC_ABORT_REASON_INVALID_OPERAND;
868dc71f
LOK
645}
646
9a54dc82 647int CCECCommandHandler::HandleTextViewOn(const cec_command &command)
cf0ecd85 648{
004b8382 649 m_processor->GetDevice(command.initiator)->MarkAsActiveSource();
9a54dc82 650 return COMMAND_HANDLED;
cf0ecd85
LOK
651}
652
9a54dc82 653int CCECCommandHandler::HandleUserControlPressed(const cec_command &command)
e9de9629 654{
9a54dc82
LOK
655 if (!m_processor->CECInitialised() ||
656 !m_processor->IsHandledByLibCEC(command.destination))
657 return CEC_ABORT_REASON_NOT_IN_CORRECT_MODE_TO_RESPOND;
004b8382 658
9a54dc82
LOK
659 if (command.parameters.size == 0)
660 return CEC_ABORT_REASON_INVALID_OPERAND;
004b8382 661
9a54dc82
LOK
662 CCECBusDevice *device = GetDevice(command.destination);
663 if (!device)
664 return CEC_ABORT_REASON_INVALID_OPERAND;
e33e0d75 665
9a54dc82
LOK
666 CCECClient *client = device->GetClient();
667 if (client)
668 client->AddKey();
e33e0d75 669
9a54dc82
LOK
670 if (command.parameters[0] <= CEC_USER_CONTROL_CODE_MAX)
671 client->SetCurrentButton((cec_user_control_code) command.parameters[0]);
e33e0d75 672
9a54dc82
LOK
673 if (command.parameters[0] == CEC_USER_CONTROL_CODE_POWER ||
674 command.parameters[0] == CEC_USER_CONTROL_CODE_POWER_ON_FUNCTION)
675 {
676 bool bPowerOn(true);
677
678 // CEC_USER_CONTROL_CODE_POWER operates as a toggle
679 // assume CEC_USER_CONTROL_CODE_POWER_ON_FUNCTION does not
680 if (command.parameters[0] == CEC_USER_CONTROL_CODE_POWER)
681 {
682 cec_power_status status = device->GetCurrentPowerStatus();
683 bPowerOn = !(status == CEC_POWER_STATUS_ON || status == CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON);
e9de9629 684 }
e33e0d75 685
9a54dc82
LOK
686 if (bPowerOn)
687 {
688 device->ActivateSource();
689 }
690 else
691 {
692 device->MarkAsInactiveSource();
693 device->TransmitInactiveSource();
694 device->SetMenuState(CEC_MENU_STATE_DEACTIVATED);
695 }
e9de9629 696 }
9a54dc82
LOK
697
698 return COMMAND_HANDLED;
e9de9629
LOK
699}
700
9a54dc82 701int CCECCommandHandler::HandleUserControlRelease(const cec_command &command)
e9de9629 702{
9a54dc82
LOK
703 if (!m_processor->CECInitialised() ||
704 !m_processor->IsHandledByLibCEC(command.destination))
705 return CEC_ABORT_REASON_NOT_IN_CORRECT_MODE_TO_RESPOND;
706
004b8382
LOK
707 CCECClient *client = m_processor->GetClient(command.destination);
708 if (client)
709 client->AddKey();
9a54dc82
LOK
710
711 return COMMAND_HANDLED;
e9de9629
LOK
712}
713
9a54dc82 714int CCECCommandHandler::HandleVendorCommand(const cec_command &command)
1de6617c 715{
9a54dc82 716 return CEC_ABORT_REASON_REFUSED;
1de6617c
LOK
717}
718
9a54dc82 719void CCECCommandHandler::UnhandledCommand(const cec_command &command, const cec_abort_reason reason)
e9de9629 720{
9a54dc82
LOK
721 if (m_processor->IsHandledByLibCEC(command.destination))
722 {
723 LIB_CEC->AddLog(CEC_LOG_DEBUG, "sending abort with opcode %02x and reason '%s' to %s", command.opcode, ToString(reason), ToString(command.initiator));
724 m_processor->TransmitAbort(command.destination, command.initiator, command.opcode, reason);
725 }
0f23c85c
LOK
726}
727
6f14b512 728size_t CCECCommandHandler::GetMyDevices(vector<CCECBusDevice *> &devices) const
8747dd4f 729{
6f14b512 730 size_t iReturn(0);
8747dd4f 731
fcf10e27 732 cec_logical_addresses addresses = m_processor->GetLogicalAddresses();
d2d1660c 733 for (uint8_t iPtr = CECDEVICE_TV; iPtr < CECDEVICE_BROADCAST; iPtr++)
8747dd4f
LOK
734 {
735 if (addresses[iPtr])
736 {
737 devices.push_back(GetDevice((cec_logical_address) iPtr));
738 ++iReturn;
739 }
740 }
741
742 return iReturn;
743}
744
0f23c85c
LOK
745CCECBusDevice *CCECCommandHandler::GetDevice(cec_logical_address iLogicalAddress) const
746{
004b8382 747 return m_processor->GetDevice(iLogicalAddress);
e9de9629 748}
6685ae07
LOK
749
750CCECBusDevice *CCECCommandHandler::GetDeviceByPhysicalAddress(uint16_t iPhysicalAddress) const
751{
fcf10e27 752 return m_processor->GetDeviceByPhysicalAddress(iPhysicalAddress);
6685ae07 753}
181b3475 754
8fa35473 755bool CCECCommandHandler::SetVendorId(const cec_command &command)
181b3475 756{
8fa35473 757 bool bChanged(false);
181b3475
LOK
758 if (command.parameters.size < 3)
759 {
004b8382 760 LIB_CEC->AddLog(CEC_LOG_WARNING, "invalid vendor ID received");
8fa35473 761 return bChanged;
181b3475
LOK
762 }
763
bae71306
LOK
764 uint64_t iVendorId = ((uint64_t)command.parameters[0] << 16) +
765 ((uint64_t)command.parameters[1] << 8) +
181b3475
LOK
766 (uint64_t)command.parameters[2];
767
768 CCECBusDevice *device = GetDevice((cec_logical_address) command.initiator);
769 if (device)
8fa35473
LOK
770 bChanged = device->SetVendorId(iVendorId);
771 return bChanged;
181b3475 772}
5e822b09 773
16b1e052
LOK
774void CCECCommandHandler::SetPhysicalAddress(cec_logical_address iAddress, uint16_t iNewAddress)
775{
004b8382 776 if (!m_processor->IsHandledByLibCEC(iAddress))
16b1e052 777 {
c0152c09
LOK
778 CCECBusDevice *otherDevice = m_processor->GetDeviceByPhysicalAddress(iNewAddress);
779 CCECClient *client = otherDevice ? otherDevice->GetClient() : NULL;
004b8382 780
c0152c09
LOK
781 CCECBusDevice *device = m_processor->GetDevice(iAddress);
782 if (device)
783 device->SetPhysicalAddress(iNewAddress);
784 else
16b1e052 785 {
c0152c09 786 LIB_CEC->AddLog(CEC_LOG_DEBUG, "device with logical address %X not found", iAddress);
16b1e052 787 }
c0152c09
LOK
788
789 /* another device reported the same physical address as ours */
790 if (client)
791 client->ResetPhysicalAddress();
792 }
793 else
794 {
795 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
796 }
797}
855a3a98 798
f4698390
LOK
799bool CCECCommandHandler::PowerOn(const cec_logical_address iInitiator, const cec_logical_address iDestination)
800{
801 if (iDestination == CECDEVICE_TV)
802 return TransmitImageViewOn(iInitiator, iDestination);
803
804 return TransmitKeypress(iInitiator, iDestination, CEC_USER_CONTROL_CODE_POWER) &&
805 TransmitKeyRelease(iInitiator, iDestination);
806}
807
b64db02e 808bool CCECCommandHandler::TransmitImageViewOn(const cec_logical_address iInitiator, const cec_logical_address iDestination)
8fa35473
LOK
809{
810 cec_command command;
ae693aaa 811 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_IMAGE_VIEW_ON);
8fa35473 812
4fde2fd0 813 return Transmit(command);
8fa35473
LOK
814}
815
816bool CCECCommandHandler::TransmitStandby(const cec_logical_address iInitiator, const cec_logical_address iDestination)
817{
818 cec_command command;
ae693aaa 819 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_STANDBY);
8fa35473 820
4fde2fd0 821 return Transmit(command);
8fa35473
LOK
822}
823
5734016c
LOK
824bool CCECCommandHandler::TransmitRequestActiveSource(const cec_logical_address iInitiator, bool bWaitForResponse /* = true */)
825{
826 cec_command command;
827 cec_command::Format(command, iInitiator, CECDEVICE_BROADCAST, CEC_OPCODE_REQUEST_ACTIVE_SOURCE);
828
829 return Transmit(command, !bWaitForResponse);
830}
831
a75e3a5a 832bool CCECCommandHandler::TransmitRequestCecVersion(const cec_logical_address iInitiator, const cec_logical_address iDestination, bool bWaitForResponse /* = true */)
8fa35473
LOK
833{
834 cec_command command;
ae693aaa 835 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_GET_CEC_VERSION);
8fa35473 836
4fde2fd0 837 return Transmit(command, !bWaitForResponse);
8fa35473
LOK
838}
839
a75e3a5a 840bool CCECCommandHandler::TransmitRequestMenuLanguage(const cec_logical_address iInitiator, const cec_logical_address iDestination, bool bWaitForResponse /* = true */)
8fa35473
LOK
841{
842 cec_command command;
ae693aaa 843 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_GET_MENU_LANGUAGE);
8fa35473 844
4fde2fd0 845 return Transmit(command, !bWaitForResponse);
8fa35473
LOK
846}
847
a75e3a5a 848bool CCECCommandHandler::TransmitRequestOSDName(const cec_logical_address iInitiator, const cec_logical_address iDestination, bool bWaitForResponse /* = true */)
8fa35473
LOK
849{
850 cec_command command;
ae693aaa 851 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_GIVE_OSD_NAME);
8fa35473 852
4fde2fd0 853 return Transmit(command, !bWaitForResponse);
8fa35473
LOK
854}
855
a75e3a5a 856bool CCECCommandHandler::TransmitRequestPhysicalAddress(const cec_logical_address iInitiator, const cec_logical_address iDestination, bool bWaitForResponse /* = true */)
8fa35473
LOK
857{
858 cec_command command;
ae693aaa 859 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_GIVE_PHYSICAL_ADDRESS);
8fa35473 860
4fde2fd0 861 return Transmit(command, !bWaitForResponse);
8fa35473
LOK
862}
863
a75e3a5a 864bool CCECCommandHandler::TransmitRequestPowerStatus(const cec_logical_address iInitiator, const cec_logical_address iDestination, bool bWaitForResponse /* = true */)
8fa35473
LOK
865{
866 cec_command command;
ae693aaa 867 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_GIVE_DEVICE_POWER_STATUS);
8fa35473 868
4fde2fd0 869 return Transmit(command, !bWaitForResponse);
8fa35473
LOK
870}
871
a75e3a5a 872bool CCECCommandHandler::TransmitRequestVendorId(const cec_logical_address iInitiator, const cec_logical_address iDestination, bool bWaitForResponse /* = true */)
8fa35473
LOK
873{
874 cec_command command;
ae693aaa 875 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_GIVE_DEVICE_VENDOR_ID);
8fa35473 876
4fde2fd0 877 return Transmit(command, !bWaitForResponse);
8fa35473
LOK
878}
879
880bool CCECCommandHandler::TransmitActiveSource(const cec_logical_address iInitiator, uint16_t iPhysicalAddress)
881{
882 cec_command command;
ae693aaa 883 cec_command::Format(command, iInitiator, CECDEVICE_BROADCAST, CEC_OPCODE_ACTIVE_SOURCE);
8fa35473
LOK
884 command.parameters.PushBack((uint8_t) ((iPhysicalAddress >> 8) & 0xFF));
885 command.parameters.PushBack((uint8_t) (iPhysicalAddress & 0xFF));
886
4fde2fd0 887 return Transmit(command);
8fa35473
LOK
888}
889
890bool CCECCommandHandler::TransmitCECVersion(const cec_logical_address iInitiator, const cec_logical_address iDestination, cec_version cecVersion)
891{
892 cec_command command;
ae693aaa 893 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_CEC_VERSION);
8fa35473
LOK
894 command.parameters.PushBack((uint8_t)cecVersion);
895
4fde2fd0 896 return Transmit(command);
8fa35473
LOK
897}
898
899bool CCECCommandHandler::TransmitInactiveSource(const cec_logical_address iInitiator, uint16_t iPhysicalAddress)
900{
901 cec_command command;
ae693aaa 902 cec_command::Format(command, iInitiator, CECDEVICE_TV, CEC_OPCODE_INACTIVE_SOURCE);
8fa35473
LOK
903 command.parameters.PushBack((iPhysicalAddress >> 8) & 0xFF);
904 command.parameters.PushBack(iPhysicalAddress & 0xFF);
905
4fde2fd0 906 return Transmit(command);
8fa35473
LOK
907}
908
909bool CCECCommandHandler::TransmitMenuState(const cec_logical_address iInitiator, const cec_logical_address iDestination, cec_menu_state menuState)
910{
911 cec_command command;
ae693aaa 912 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_MENU_STATUS);
8fa35473
LOK
913 command.parameters.PushBack((uint8_t)menuState);
914
4fde2fd0 915 return Transmit(command);
8fa35473
LOK
916}
917
918bool CCECCommandHandler::TransmitOSDName(const cec_logical_address iInitiator, const cec_logical_address iDestination, CStdString strDeviceName)
919{
920 cec_command command;
ae693aaa 921 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_SET_OSD_NAME);
6f14b512 922 for (size_t iPtr = 0; iPtr < strDeviceName.length(); iPtr++)
8fa35473
LOK
923 command.parameters.PushBack(strDeviceName.at(iPtr));
924
4fde2fd0 925 return Transmit(command);
8fa35473
LOK
926}
927
928bool CCECCommandHandler::TransmitOSDString(const cec_logical_address iInitiator, const cec_logical_address iDestination, cec_display_control duration, const char *strMessage)
929{
930 cec_command command;
ae693aaa 931 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_SET_OSD_STRING);
8fa35473
LOK
932 command.parameters.PushBack((uint8_t)duration);
933
6f14b512 934 size_t iLen = strlen(strMessage);
8fa35473
LOK
935 if (iLen > 13) iLen = 13;
936
6f14b512 937 for (size_t iPtr = 0; iPtr < iLen; iPtr++)
8fa35473
LOK
938 command.parameters.PushBack(strMessage[iPtr]);
939
4fde2fd0 940 return Transmit(command);
8fa35473
LOK
941}
942
943bool CCECCommandHandler::TransmitPhysicalAddress(const cec_logical_address iInitiator, uint16_t iPhysicalAddress, cec_device_type type)
944{
945 cec_command command;
ae693aaa 946 cec_command::Format(command, iInitiator, CECDEVICE_BROADCAST, CEC_OPCODE_REPORT_PHYSICAL_ADDRESS);
8fa35473
LOK
947 command.parameters.PushBack((uint8_t) ((iPhysicalAddress >> 8) & 0xFF));
948 command.parameters.PushBack((uint8_t) (iPhysicalAddress & 0xFF));
949 command.parameters.PushBack((uint8_t) (type));
950
4fde2fd0 951 return Transmit(command);
fbdea54c
MK
952}
953
954bool CCECCommandHandler::TransmitSetMenuLanguage(const cec_logical_address iInitiator, const char lang[3])
955{
956 cec_command command;
957 command.Format(command, iInitiator, CECDEVICE_BROADCAST, CEC_OPCODE_SET_MENU_LANGUAGE);
958 command.parameters.PushBack((uint8_t) lang[0]);
959 command.parameters.PushBack((uint8_t) lang[1]);
960 command.parameters.PushBack((uint8_t) lang[2]);
961
4fde2fd0 962 return Transmit(command);
8fa35473
LOK
963}
964
965bool CCECCommandHandler::TransmitPoll(const cec_logical_address iInitiator, const cec_logical_address iDestination)
966{
967 cec_command command;
ae693aaa 968 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_NONE);
8fa35473 969
4fde2fd0 970 return Transmit(command);
8fa35473
LOK
971}
972
973bool CCECCommandHandler::TransmitPowerState(const cec_logical_address iInitiator, const cec_logical_address iDestination, cec_power_status state)
974{
975 cec_command command;
ae693aaa 976 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_REPORT_POWER_STATUS);
8fa35473
LOK
977 command.parameters.PushBack((uint8_t) state);
978
4fde2fd0 979 return Transmit(command);
8fa35473
LOK
980}
981
982bool CCECCommandHandler::TransmitVendorID(const cec_logical_address iInitiator, uint64_t iVendorId)
983{
984 cec_command command;
ae693aaa 985 cec_command::Format(command, iInitiator, CECDEVICE_BROADCAST, CEC_OPCODE_DEVICE_VENDOR_ID);
8fa35473
LOK
986
987 command.parameters.PushBack((uint8_t) (((uint64_t)iVendorId >> 16) & 0xFF));
988 command.parameters.PushBack((uint8_t) (((uint64_t)iVendorId >> 8) & 0xFF));
989 command.parameters.PushBack((uint8_t) ((uint64_t)iVendorId & 0xFF));
990
4fde2fd0 991 return Transmit(command);
8fa35473
LOK
992}
993
994bool CCECCommandHandler::TransmitAudioStatus(const cec_logical_address iInitiator, const cec_logical_address iDestination, uint8_t state)
995{
996 cec_command command;
ae693aaa 997 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_REPORT_AUDIO_STATUS);
8fa35473
LOK
998 command.parameters.PushBack(state);
999
4fde2fd0 1000 return Transmit(command);
8fa35473
LOK
1001}
1002
1003bool CCECCommandHandler::TransmitSetSystemAudioMode(const cec_logical_address iInitiator, const cec_logical_address iDestination, cec_system_audio_status state)
1004{
1005 cec_command command;
ae693aaa 1006 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_SET_SYSTEM_AUDIO_MODE);
8fa35473
LOK
1007 command.parameters.PushBack((uint8_t)state);
1008
4fde2fd0 1009 return Transmit(command);
8fa35473
LOK
1010}
1011
f42d3e0f
LOK
1012bool CCECCommandHandler::TransmitSetStreamPath(uint16_t iStreamPath)
1013{
1014 cec_command command;
1015 cec_command::Format(command, m_busDevice->GetLogicalAddress(), CECDEVICE_BROADCAST, CEC_OPCODE_SET_STREAM_PATH);
1016 command.parameters.PushBack((uint8_t) ((iStreamPath >> 8) & 0xFF));
1017 command.parameters.PushBack((uint8_t) (iStreamPath & 0xFF));
1018
4fde2fd0 1019 return Transmit(command);
f42d3e0f
LOK
1020}
1021
8fa35473
LOK
1022bool CCECCommandHandler::TransmitSystemAudioModeStatus(const cec_logical_address iInitiator, const cec_logical_address iDestination, cec_system_audio_status state)
1023{
1024 cec_command command;
ae693aaa 1025 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_SYSTEM_AUDIO_MODE_STATUS);
8fa35473
LOK
1026 command.parameters.PushBack((uint8_t)state);
1027
4fde2fd0 1028 return Transmit(command);
8fa35473
LOK
1029}
1030
1031bool CCECCommandHandler::TransmitDeckStatus(const cec_logical_address iInitiator, const cec_logical_address iDestination, cec_deck_info state)
1032{
1033 cec_command command;
ae693aaa 1034 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_DECK_STATUS);
8fa35473
LOK
1035 command.PushBack((uint8_t)state);
1036
4fde2fd0 1037 return Transmit(command);
8fa35473
LOK
1038}
1039
4bec9d79 1040bool CCECCommandHandler::TransmitKeypress(const cec_logical_address iInitiator, const cec_logical_address iDestination, cec_user_control_code key, bool bWait /* = true */)
8fa35473
LOK
1041{
1042 cec_command command;
ae693aaa 1043 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_USER_CONTROL_PRESSED);
8fa35473
LOK
1044 command.parameters.PushBack((uint8_t)key);
1045
4fde2fd0 1046 return Transmit(command, !bWait);
8fa35473
LOK
1047}
1048
4bec9d79 1049bool CCECCommandHandler::TransmitKeyRelease(const cec_logical_address iInitiator, const cec_logical_address iDestination, bool bWait /* = true */)
8fa35473
LOK
1050{
1051 cec_command command;
ae693aaa 1052 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_USER_CONTROL_RELEASE);
8fa35473 1053
4fde2fd0 1054 return Transmit(command, !bWait);
8fa35473
LOK
1055}
1056
4fde2fd0 1057bool CCECCommandHandler::Transmit(cec_command &command, bool bSuppressWait /* = false */)
8fa35473 1058{
b64db02e 1059 bool bReturn(false);
4fde2fd0
LOK
1060 cec_opcode expectedResponse(cec_command::GetResponseOpcode(command.opcode));
1061 bool bExpectResponse(expectedResponse != CEC_OPCODE_NONE && !bSuppressWait);
ae693aaa 1062 command.transmit_timeout = m_iTransmitTimeout;
ae693aaa 1063
7e4558f1
LOK
1064 if (command.initiator == CECDEVICE_UNKNOWN)
1065 {
004b8382 1066 LIB_CEC->AddLog(CEC_LOG_ERROR, "not transmitting a command without a valid initiator");
7e4558f1
LOK
1067 return bReturn;
1068 }
1069
ae693aaa 1070 {
6c0ccaa5 1071 uint8_t iTries(0), iMaxTries(!command.opcode_set ? 1 : m_iTransmitRetries + 1);
ec15344c 1072 while (!bReturn && ++iTries <= iMaxTries && !m_busDevice->IsUnsupportedFeature(command.opcode))
b64db02e 1073 {
99666519 1074 if ((bReturn = m_processor->Transmit(command)) == true)
19cbfa8f 1075 {
004b8382 1076 LIB_CEC->AddLog(CEC_LOG_DEBUG, "command transmitted");
4478bc79 1077 if (bExpectResponse)
24dd566c
LOK
1078 {
1079 bReturn = m_waitForResponse->Wait(expectedResponse);
004b8382 1080 LIB_CEC->AddLog(CEC_LOG_DEBUG, bReturn ? "expected response received (%X: %s)" : "expected response not received (%X: %s)", (int)expectedResponse, ToString(expectedResponse));
24dd566c 1081 }
19cbfa8f 1082 }
b64db02e 1083 }
ae693aaa 1084 }
8fa35473 1085
b64db02e 1086 return bReturn;
8fa35473 1087}
83be0701 1088
3e61b350 1089bool CCECCommandHandler::ActivateSource(void)
83be0701 1090{
c4287bcd 1091 if (m_busDevice->IsActiveSource() &&
004b8382 1092 m_busDevice->IsHandledByLibCEC())
c4287bcd 1093 {
b0015449
LOK
1094 {
1095 CLockObject lock(m_mutex);
1096 m_bActiveSourcePending = false;
1097 }
1098
c4287bcd
LOK
1099 m_busDevice->SetPowerStatus(CEC_POWER_STATUS_ON);
1100 m_busDevice->SetMenuState(CEC_MENU_STATE_ACTIVATED);
1101
dbad810a
LOK
1102 bool bActiveSourceFailed = !m_busDevice->TransmitImageViewOn() ||
1103 !m_busDevice->TransmitActiveSource() ||
1104 !m_busDevice->TransmitMenuState(CECDEVICE_TV);
1105
1106 if (!bActiveSourceFailed)
b0015449 1107 {
dbad810a
LOK
1108 CCECPlaybackDevice *playbackDevice = m_busDevice->AsPlaybackDevice();
1109 if (playbackDevice && SendDeckStatusUpdateOnActiveSource())
1110 bActiveSourceFailed = !playbackDevice->TransmitDeckStatus(CECDEVICE_TV);
1111 }
1112
1113 if (bActiveSourceFailed)
1114 {
1115 LIB_CEC->AddLog(CEC_LOG_DEBUG, "failed to make '%s' the active source. will retry later", m_busDevice->GetLogicalAddressName());
b0015449
LOK
1116 CLockObject lock(m_mutex);
1117 m_bActiveSourcePending = true;
1118 return false;
1119 }
aa40e0e2 1120
c4287bcd 1121 m_bHandlerInited = true;
83be0701 1122 }
7dc58c9f 1123 return true;
83be0701 1124}
b499cf16
LOK
1125
1126void CCECCommandHandler::SignalOpcode(cec_opcode opcode)
1127{
1128 m_waitForResponse->Received(opcode);
1129}
b0015449
LOK
1130
1131bool CCECCommandHandler::ActiveSourcePending(void)
1132{
1133 CLockObject lock(m_mutex);
1134 return m_bActiveSourcePending;
1135}