cec: reset m_bPowerUpEventReceived in CVLCommandHandler when the device lets us know...
[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
866e7584 249int CCECCommandHandler::HandleDeviceVendorCommandWithId(const cec_command & UNUSED(command))
e9de9629 250{
22579015 251 return CEC_ABORT_REASON_INVALID_OPERAND;
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]);
bf1b57e0
LOK
564 LIB_CEC->AddLog(CEC_LOG_DEBUG, ">> %s (%x) sets stream path to physical address %04x", ToString(command.initiator), command.initiator, iStreamAddress);
565
566 // a device will only change the stream path when it's powered on
567 m_busDevice->SetPowerStatus(CEC_POWER_STATUS_ON);
72689600
LOK
568
569 /* one of the device handled by libCEC has been made active */
570 CCECBusDevice *device = GetDeviceByPhysicalAddress(iStreamAddress);
004b8382 571 if (device && device->IsHandledByLibCEC())
9a54dc82 572 {
004b8382 573 device->ActivateSource();
9a54dc82
LOK
574 return COMMAND_HANDLED;
575 }
e9de9629 576 }
9a54dc82
LOK
577
578 return CEC_ABORT_REASON_INVALID_OPERAND;
e9de9629
LOK
579}
580
9a54dc82 581int CCECCommandHandler::HandleSystemAudioModeRequest(const cec_command &command)
e5e86c76 582{
0b8c7eab 583 if (m_processor->CECInitialised() && m_processor->IsHandledByLibCEC(command.destination))
e5e86c76 584 {
aa40e0e2
LOK
585 CCECAudioSystem *device = CCECBusDevice::AsAudioSystem(GetDevice(command.destination));
586 if (device)
aa517a0d
LOK
587 {
588 if (command.parameters.size >= 2)
589 {
590 device->SetPowerStatus(CEC_POWER_STATUS_ON);
aa40e0e2 591 device->SetSystemAudioModeStatus(CEC_SYSTEM_AUDIO_STATUS_ON);
aa517a0d
LOK
592 uint16_t iNewAddress = ((uint16_t)command.parameters[0] << 8) | ((uint16_t)command.parameters[1]);
593 CCECBusDevice *newActiveDevice = GetDeviceByPhysicalAddress(iNewAddress);
594 if (newActiveDevice)
004b8382 595 newActiveDevice->MarkAsActiveSource();
9a54dc82
LOK
596 if (device->TransmitSetSystemAudioMode(command.initiator))
597 return COMMAND_HANDLED;
aa517a0d
LOK
598 }
599 else
600 {
aa40e0e2 601 device->SetSystemAudioModeStatus(CEC_SYSTEM_AUDIO_STATUS_OFF);
9a54dc82
LOK
602 if (device->TransmitSetSystemAudioMode(command.initiator))
603 return COMMAND_HANDLED;
aa517a0d
LOK
604 }
605 }
e5e86c76 606 }
9a54dc82
LOK
607
608 return CEC_ABORT_REASON_NOT_IN_CORRECT_MODE_TO_RESPOND;
e5e86c76
LOK
609}
610
9a54dc82 611int CCECCommandHandler::HandleStandby(const cec_command &command)
4d6b4433
LOK
612{
613 CCECBusDevice *device = GetDevice(command.initiator);
614 if (device)
615 device->SetPowerStatus(CEC_POWER_STATUS_STANDBY);
6b72afcd 616
9a54dc82 617 return COMMAND_HANDLED;
4d6b4433
LOK
618}
619
9a54dc82 620int CCECCommandHandler::HandleSystemAudioModeStatus(const cec_command &command)
868dc71f 621{
aa517a0d 622 if (command.parameters.size == 1)
868dc71f 623 {
aa40e0e2
LOK
624 CCECAudioSystem *device = CCECBusDevice::AsAudioSystem(GetDevice(command.initiator));
625 if (device)
aa517a0d 626 {
aa40e0e2 627 device->SetSystemAudioModeStatus((cec_system_audio_status)command.parameters[0]);
9a54dc82 628 return COMMAND_HANDLED;
aa517a0d
LOK
629 }
630 }
631
9a54dc82 632 return CEC_ABORT_REASON_INVALID_OPERAND;
aa517a0d
LOK
633}
634
9a54dc82 635int CCECCommandHandler::HandleSetSystemAudioMode(const cec_command &command)
aa517a0d
LOK
636{
637 if (command.parameters.size == 1)
638 {
aa40e0e2
LOK
639 CCECAudioSystem *device = CCECBusDevice::AsAudioSystem(GetDevice(command.initiator));
640 if (device)
aa517a0d 641 {
aa40e0e2 642 device->SetSystemAudioModeStatus((cec_system_audio_status)command.parameters[0]);
9a54dc82 643 return COMMAND_HANDLED;
aa517a0d 644 }
868dc71f
LOK
645 }
646
9a54dc82 647 return CEC_ABORT_REASON_INVALID_OPERAND;
868dc71f
LOK
648}
649
9a54dc82 650int CCECCommandHandler::HandleTextViewOn(const cec_command &command)
cf0ecd85 651{
004b8382 652 m_processor->GetDevice(command.initiator)->MarkAsActiveSource();
9a54dc82 653 return COMMAND_HANDLED;
cf0ecd85
LOK
654}
655
9a54dc82 656int CCECCommandHandler::HandleUserControlPressed(const cec_command &command)
e9de9629 657{
9a54dc82
LOK
658 if (!m_processor->CECInitialised() ||
659 !m_processor->IsHandledByLibCEC(command.destination))
660 return CEC_ABORT_REASON_NOT_IN_CORRECT_MODE_TO_RESPOND;
004b8382 661
9a54dc82
LOK
662 if (command.parameters.size == 0)
663 return CEC_ABORT_REASON_INVALID_OPERAND;
004b8382 664
9a54dc82
LOK
665 CCECBusDevice *device = GetDevice(command.destination);
666 if (!device)
667 return CEC_ABORT_REASON_INVALID_OPERAND;
e33e0d75 668
9a54dc82
LOK
669 CCECClient *client = device->GetClient();
670 if (client)
671 client->AddKey();
e33e0d75 672
9a54dc82
LOK
673 if (command.parameters[0] <= CEC_USER_CONTROL_CODE_MAX)
674 client->SetCurrentButton((cec_user_control_code) command.parameters[0]);
e33e0d75 675
9a54dc82
LOK
676 if (command.parameters[0] == CEC_USER_CONTROL_CODE_POWER ||
677 command.parameters[0] == CEC_USER_CONTROL_CODE_POWER_ON_FUNCTION)
678 {
679 bool bPowerOn(true);
680
681 // CEC_USER_CONTROL_CODE_POWER operates as a toggle
682 // assume CEC_USER_CONTROL_CODE_POWER_ON_FUNCTION does not
683 if (command.parameters[0] == CEC_USER_CONTROL_CODE_POWER)
684 {
685 cec_power_status status = device->GetCurrentPowerStatus();
686 bPowerOn = !(status == CEC_POWER_STATUS_ON || status == CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON);
e9de9629 687 }
e33e0d75 688
9a54dc82
LOK
689 if (bPowerOn)
690 {
691 device->ActivateSource();
692 }
693 else
694 {
695 device->MarkAsInactiveSource();
696 device->TransmitInactiveSource();
697 device->SetMenuState(CEC_MENU_STATE_DEACTIVATED);
698 }
e9de9629 699 }
9a54dc82
LOK
700
701 return COMMAND_HANDLED;
e9de9629
LOK
702}
703
9a54dc82 704int CCECCommandHandler::HandleUserControlRelease(const cec_command &command)
e9de9629 705{
9a54dc82
LOK
706 if (!m_processor->CECInitialised() ||
707 !m_processor->IsHandledByLibCEC(command.destination))
708 return CEC_ABORT_REASON_NOT_IN_CORRECT_MODE_TO_RESPOND;
709
004b8382
LOK
710 CCECClient *client = m_processor->GetClient(command.destination);
711 if (client)
712 client->AddKey();
9a54dc82
LOK
713
714 return COMMAND_HANDLED;
e9de9629
LOK
715}
716
866e7584 717int CCECCommandHandler::HandleVendorCommand(const cec_command & UNUSED(command))
1de6617c 718{
22579015 719 return CEC_ABORT_REASON_INVALID_OPERAND;
1de6617c
LOK
720}
721
9a54dc82 722void CCECCommandHandler::UnhandledCommand(const cec_command &command, const cec_abort_reason reason)
e9de9629 723{
9a54dc82
LOK
724 if (m_processor->IsHandledByLibCEC(command.destination))
725 {
726 LIB_CEC->AddLog(CEC_LOG_DEBUG, "sending abort with opcode %02x and reason '%s' to %s", command.opcode, ToString(reason), ToString(command.initiator));
727 m_processor->TransmitAbort(command.destination, command.initiator, command.opcode, reason);
728 }
0f23c85c
LOK
729}
730
6f14b512 731size_t CCECCommandHandler::GetMyDevices(vector<CCECBusDevice *> &devices) const
8747dd4f 732{
6f14b512 733 size_t iReturn(0);
8747dd4f 734
fcf10e27 735 cec_logical_addresses addresses = m_processor->GetLogicalAddresses();
d2d1660c 736 for (uint8_t iPtr = CECDEVICE_TV; iPtr < CECDEVICE_BROADCAST; iPtr++)
8747dd4f
LOK
737 {
738 if (addresses[iPtr])
739 {
740 devices.push_back(GetDevice((cec_logical_address) iPtr));
741 ++iReturn;
742 }
743 }
744
745 return iReturn;
746}
747
0f23c85c
LOK
748CCECBusDevice *CCECCommandHandler::GetDevice(cec_logical_address iLogicalAddress) const
749{
004b8382 750 return m_processor->GetDevice(iLogicalAddress);
e9de9629 751}
6685ae07
LOK
752
753CCECBusDevice *CCECCommandHandler::GetDeviceByPhysicalAddress(uint16_t iPhysicalAddress) const
754{
fcf10e27 755 return m_processor->GetDeviceByPhysicalAddress(iPhysicalAddress);
6685ae07 756}
181b3475 757
8fa35473 758bool CCECCommandHandler::SetVendorId(const cec_command &command)
181b3475 759{
8fa35473 760 bool bChanged(false);
181b3475
LOK
761 if (command.parameters.size < 3)
762 {
004b8382 763 LIB_CEC->AddLog(CEC_LOG_WARNING, "invalid vendor ID received");
8fa35473 764 return bChanged;
181b3475
LOK
765 }
766
bae71306
LOK
767 uint64_t iVendorId = ((uint64_t)command.parameters[0] << 16) +
768 ((uint64_t)command.parameters[1] << 8) +
181b3475
LOK
769 (uint64_t)command.parameters[2];
770
771 CCECBusDevice *device = GetDevice((cec_logical_address) command.initiator);
772 if (device)
8fa35473
LOK
773 bChanged = device->SetVendorId(iVendorId);
774 return bChanged;
181b3475 775}
5e822b09 776
16b1e052
LOK
777void CCECCommandHandler::SetPhysicalAddress(cec_logical_address iAddress, uint16_t iNewAddress)
778{
004b8382 779 if (!m_processor->IsHandledByLibCEC(iAddress))
16b1e052 780 {
c0152c09
LOK
781 CCECBusDevice *otherDevice = m_processor->GetDeviceByPhysicalAddress(iNewAddress);
782 CCECClient *client = otherDevice ? otherDevice->GetClient() : NULL;
004b8382 783
c0152c09
LOK
784 CCECBusDevice *device = m_processor->GetDevice(iAddress);
785 if (device)
786 device->SetPhysicalAddress(iNewAddress);
787 else
16b1e052 788 {
c0152c09 789 LIB_CEC->AddLog(CEC_LOG_DEBUG, "device with logical address %X not found", iAddress);
16b1e052 790 }
c0152c09
LOK
791
792 /* another device reported the same physical address as ours */
793 if (client)
794 client->ResetPhysicalAddress();
795 }
796 else
797 {
798 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
799 }
800}
855a3a98 801
f4698390
LOK
802bool CCECCommandHandler::PowerOn(const cec_logical_address iInitiator, const cec_logical_address iDestination)
803{
804 if (iDestination == CECDEVICE_TV)
805 return TransmitImageViewOn(iInitiator, iDestination);
806
807 return TransmitKeypress(iInitiator, iDestination, CEC_USER_CONTROL_CODE_POWER) &&
808 TransmitKeyRelease(iInitiator, iDestination);
809}
810
b64db02e 811bool CCECCommandHandler::TransmitImageViewOn(const cec_logical_address iInitiator, const cec_logical_address iDestination)
8fa35473
LOK
812{
813 cec_command command;
ae693aaa 814 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_IMAGE_VIEW_ON);
8fa35473 815
4fde2fd0 816 return Transmit(command);
8fa35473
LOK
817}
818
819bool CCECCommandHandler::TransmitStandby(const cec_logical_address iInitiator, const cec_logical_address iDestination)
820{
821 cec_command command;
ae693aaa 822 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_STANDBY);
8fa35473 823
4fde2fd0 824 return Transmit(command);
8fa35473
LOK
825}
826
5734016c
LOK
827bool CCECCommandHandler::TransmitRequestActiveSource(const cec_logical_address iInitiator, bool bWaitForResponse /* = true */)
828{
829 cec_command command;
830 cec_command::Format(command, iInitiator, CECDEVICE_BROADCAST, CEC_OPCODE_REQUEST_ACTIVE_SOURCE);
831
832 return Transmit(command, !bWaitForResponse);
833}
834
a75e3a5a 835bool CCECCommandHandler::TransmitRequestCecVersion(const cec_logical_address iInitiator, const cec_logical_address iDestination, bool bWaitForResponse /* = true */)
8fa35473
LOK
836{
837 cec_command command;
ae693aaa 838 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_GET_CEC_VERSION);
8fa35473 839
4fde2fd0 840 return Transmit(command, !bWaitForResponse);
8fa35473
LOK
841}
842
a75e3a5a 843bool CCECCommandHandler::TransmitRequestMenuLanguage(const cec_logical_address iInitiator, const cec_logical_address iDestination, bool bWaitForResponse /* = true */)
8fa35473
LOK
844{
845 cec_command command;
ae693aaa 846 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_GET_MENU_LANGUAGE);
8fa35473 847
4fde2fd0 848 return Transmit(command, !bWaitForResponse);
8fa35473
LOK
849}
850
a75e3a5a 851bool CCECCommandHandler::TransmitRequestOSDName(const cec_logical_address iInitiator, const cec_logical_address iDestination, bool bWaitForResponse /* = true */)
8fa35473
LOK
852{
853 cec_command command;
ae693aaa 854 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_GIVE_OSD_NAME);
8fa35473 855
4fde2fd0 856 return Transmit(command, !bWaitForResponse);
8fa35473
LOK
857}
858
a75e3a5a 859bool CCECCommandHandler::TransmitRequestPhysicalAddress(const cec_logical_address iInitiator, const cec_logical_address iDestination, bool bWaitForResponse /* = true */)
8fa35473
LOK
860{
861 cec_command command;
ae693aaa 862 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_GIVE_PHYSICAL_ADDRESS);
8fa35473 863
4fde2fd0 864 return Transmit(command, !bWaitForResponse);
8fa35473
LOK
865}
866
a75e3a5a 867bool CCECCommandHandler::TransmitRequestPowerStatus(const cec_logical_address iInitiator, const cec_logical_address iDestination, bool bWaitForResponse /* = true */)
8fa35473
LOK
868{
869 cec_command command;
ae693aaa 870 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_GIVE_DEVICE_POWER_STATUS);
8fa35473 871
4fde2fd0 872 return Transmit(command, !bWaitForResponse);
8fa35473
LOK
873}
874
a75e3a5a 875bool CCECCommandHandler::TransmitRequestVendorId(const cec_logical_address iInitiator, const cec_logical_address iDestination, bool bWaitForResponse /* = true */)
8fa35473
LOK
876{
877 cec_command command;
ae693aaa 878 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_GIVE_DEVICE_VENDOR_ID);
8fa35473 879
4fde2fd0 880 return Transmit(command, !bWaitForResponse);
8fa35473
LOK
881}
882
883bool CCECCommandHandler::TransmitActiveSource(const cec_logical_address iInitiator, uint16_t iPhysicalAddress)
884{
885 cec_command command;
ae693aaa 886 cec_command::Format(command, iInitiator, CECDEVICE_BROADCAST, CEC_OPCODE_ACTIVE_SOURCE);
8fa35473
LOK
887 command.parameters.PushBack((uint8_t) ((iPhysicalAddress >> 8) & 0xFF));
888 command.parameters.PushBack((uint8_t) (iPhysicalAddress & 0xFF));
889
4fde2fd0 890 return Transmit(command);
8fa35473
LOK
891}
892
893bool CCECCommandHandler::TransmitCECVersion(const cec_logical_address iInitiator, const cec_logical_address iDestination, cec_version cecVersion)
894{
895 cec_command command;
ae693aaa 896 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_CEC_VERSION);
8fa35473
LOK
897 command.parameters.PushBack((uint8_t)cecVersion);
898
4fde2fd0 899 return Transmit(command);
8fa35473
LOK
900}
901
902bool CCECCommandHandler::TransmitInactiveSource(const cec_logical_address iInitiator, uint16_t iPhysicalAddress)
903{
904 cec_command command;
ae693aaa 905 cec_command::Format(command, iInitiator, CECDEVICE_TV, CEC_OPCODE_INACTIVE_SOURCE);
8fa35473
LOK
906 command.parameters.PushBack((iPhysicalAddress >> 8) & 0xFF);
907 command.parameters.PushBack(iPhysicalAddress & 0xFF);
908
4fde2fd0 909 return Transmit(command);
8fa35473
LOK
910}
911
912bool CCECCommandHandler::TransmitMenuState(const cec_logical_address iInitiator, const cec_logical_address iDestination, cec_menu_state menuState)
913{
914 cec_command command;
ae693aaa 915 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_MENU_STATUS);
8fa35473
LOK
916 command.parameters.PushBack((uint8_t)menuState);
917
4fde2fd0 918 return Transmit(command);
8fa35473
LOK
919}
920
921bool CCECCommandHandler::TransmitOSDName(const cec_logical_address iInitiator, const cec_logical_address iDestination, CStdString strDeviceName)
922{
923 cec_command command;
ae693aaa 924 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_SET_OSD_NAME);
6f14b512 925 for (size_t iPtr = 0; iPtr < strDeviceName.length(); iPtr++)
8fa35473
LOK
926 command.parameters.PushBack(strDeviceName.at(iPtr));
927
4fde2fd0 928 return Transmit(command);
8fa35473
LOK
929}
930
931bool CCECCommandHandler::TransmitOSDString(const cec_logical_address iInitiator, const cec_logical_address iDestination, cec_display_control duration, const char *strMessage)
932{
933 cec_command command;
ae693aaa 934 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_SET_OSD_STRING);
8fa35473
LOK
935 command.parameters.PushBack((uint8_t)duration);
936
6f14b512 937 size_t iLen = strlen(strMessage);
8fa35473
LOK
938 if (iLen > 13) iLen = 13;
939
6f14b512 940 for (size_t iPtr = 0; iPtr < iLen; iPtr++)
8fa35473
LOK
941 command.parameters.PushBack(strMessage[iPtr]);
942
4fde2fd0 943 return Transmit(command);
8fa35473
LOK
944}
945
946bool CCECCommandHandler::TransmitPhysicalAddress(const cec_logical_address iInitiator, uint16_t iPhysicalAddress, cec_device_type type)
947{
948 cec_command command;
ae693aaa 949 cec_command::Format(command, iInitiator, CECDEVICE_BROADCAST, CEC_OPCODE_REPORT_PHYSICAL_ADDRESS);
8fa35473
LOK
950 command.parameters.PushBack((uint8_t) ((iPhysicalAddress >> 8) & 0xFF));
951 command.parameters.PushBack((uint8_t) (iPhysicalAddress & 0xFF));
952 command.parameters.PushBack((uint8_t) (type));
953
4fde2fd0 954 return Transmit(command);
fbdea54c
MK
955}
956
957bool CCECCommandHandler::TransmitSetMenuLanguage(const cec_logical_address iInitiator, const char lang[3])
958{
959 cec_command command;
960 command.Format(command, iInitiator, CECDEVICE_BROADCAST, CEC_OPCODE_SET_MENU_LANGUAGE);
961 command.parameters.PushBack((uint8_t) lang[0]);
962 command.parameters.PushBack((uint8_t) lang[1]);
963 command.parameters.PushBack((uint8_t) lang[2]);
964
4fde2fd0 965 return Transmit(command);
8fa35473
LOK
966}
967
968bool CCECCommandHandler::TransmitPoll(const cec_logical_address iInitiator, const cec_logical_address iDestination)
969{
970 cec_command command;
ae693aaa 971 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_NONE);
8fa35473 972
4fde2fd0 973 return Transmit(command);
8fa35473
LOK
974}
975
976bool CCECCommandHandler::TransmitPowerState(const cec_logical_address iInitiator, const cec_logical_address iDestination, cec_power_status state)
977{
978 cec_command command;
ae693aaa 979 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_REPORT_POWER_STATUS);
8fa35473
LOK
980 command.parameters.PushBack((uint8_t) state);
981
4fde2fd0 982 return Transmit(command);
8fa35473
LOK
983}
984
985bool CCECCommandHandler::TransmitVendorID(const cec_logical_address iInitiator, uint64_t iVendorId)
986{
987 cec_command command;
ae693aaa 988 cec_command::Format(command, iInitiator, CECDEVICE_BROADCAST, CEC_OPCODE_DEVICE_VENDOR_ID);
8fa35473
LOK
989
990 command.parameters.PushBack((uint8_t) (((uint64_t)iVendorId >> 16) & 0xFF));
991 command.parameters.PushBack((uint8_t) (((uint64_t)iVendorId >> 8) & 0xFF));
992 command.parameters.PushBack((uint8_t) ((uint64_t)iVendorId & 0xFF));
993
4fde2fd0 994 return Transmit(command);
8fa35473
LOK
995}
996
997bool CCECCommandHandler::TransmitAudioStatus(const cec_logical_address iInitiator, const cec_logical_address iDestination, uint8_t state)
998{
999 cec_command command;
ae693aaa 1000 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_REPORT_AUDIO_STATUS);
8fa35473
LOK
1001 command.parameters.PushBack(state);
1002
4fde2fd0 1003 return Transmit(command);
8fa35473
LOK
1004}
1005
1006bool CCECCommandHandler::TransmitSetSystemAudioMode(const cec_logical_address iInitiator, const cec_logical_address iDestination, cec_system_audio_status state)
1007{
1008 cec_command command;
ae693aaa 1009 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_SET_SYSTEM_AUDIO_MODE);
8fa35473
LOK
1010 command.parameters.PushBack((uint8_t)state);
1011
4fde2fd0 1012 return Transmit(command);
8fa35473
LOK
1013}
1014
f42d3e0f
LOK
1015bool CCECCommandHandler::TransmitSetStreamPath(uint16_t iStreamPath)
1016{
1017 cec_command command;
1018 cec_command::Format(command, m_busDevice->GetLogicalAddress(), CECDEVICE_BROADCAST, CEC_OPCODE_SET_STREAM_PATH);
1019 command.parameters.PushBack((uint8_t) ((iStreamPath >> 8) & 0xFF));
1020 command.parameters.PushBack((uint8_t) (iStreamPath & 0xFF));
1021
4fde2fd0 1022 return Transmit(command);
f42d3e0f
LOK
1023}
1024
8fa35473
LOK
1025bool CCECCommandHandler::TransmitSystemAudioModeStatus(const cec_logical_address iInitiator, const cec_logical_address iDestination, cec_system_audio_status state)
1026{
1027 cec_command command;
ae693aaa 1028 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_SYSTEM_AUDIO_MODE_STATUS);
8fa35473
LOK
1029 command.parameters.PushBack((uint8_t)state);
1030
4fde2fd0 1031 return Transmit(command);
8fa35473
LOK
1032}
1033
1034bool CCECCommandHandler::TransmitDeckStatus(const cec_logical_address iInitiator, const cec_logical_address iDestination, cec_deck_info state)
1035{
1036 cec_command command;
ae693aaa 1037 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_DECK_STATUS);
8fa35473
LOK
1038 command.PushBack((uint8_t)state);
1039
4fde2fd0 1040 return Transmit(command);
8fa35473
LOK
1041}
1042
4bec9d79 1043bool CCECCommandHandler::TransmitKeypress(const cec_logical_address iInitiator, const cec_logical_address iDestination, cec_user_control_code key, bool bWait /* = true */)
8fa35473
LOK
1044{
1045 cec_command command;
ae693aaa 1046 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_USER_CONTROL_PRESSED);
8fa35473
LOK
1047 command.parameters.PushBack((uint8_t)key);
1048
4fde2fd0 1049 return Transmit(command, !bWait);
8fa35473
LOK
1050}
1051
4bec9d79 1052bool CCECCommandHandler::TransmitKeyRelease(const cec_logical_address iInitiator, const cec_logical_address iDestination, bool bWait /* = true */)
8fa35473
LOK
1053{
1054 cec_command command;
ae693aaa 1055 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_USER_CONTROL_RELEASE);
8fa35473 1056
4fde2fd0 1057 return Transmit(command, !bWait);
8fa35473
LOK
1058}
1059
4fde2fd0 1060bool CCECCommandHandler::Transmit(cec_command &command, bool bSuppressWait /* = false */)
8fa35473 1061{
b64db02e 1062 bool bReturn(false);
4fde2fd0
LOK
1063 cec_opcode expectedResponse(cec_command::GetResponseOpcode(command.opcode));
1064 bool bExpectResponse(expectedResponse != CEC_OPCODE_NONE && !bSuppressWait);
ae693aaa 1065 command.transmit_timeout = m_iTransmitTimeout;
ae693aaa 1066
7e4558f1
LOK
1067 if (command.initiator == CECDEVICE_UNKNOWN)
1068 {
004b8382 1069 LIB_CEC->AddLog(CEC_LOG_ERROR, "not transmitting a command without a valid initiator");
7e4558f1
LOK
1070 return bReturn;
1071 }
1072
ae693aaa 1073 {
6c0ccaa5 1074 uint8_t iTries(0), iMaxTries(!command.opcode_set ? 1 : m_iTransmitRetries + 1);
ec15344c 1075 while (!bReturn && ++iTries <= iMaxTries && !m_busDevice->IsUnsupportedFeature(command.opcode))
b64db02e 1076 {
99666519 1077 if ((bReturn = m_processor->Transmit(command)) == true)
19cbfa8f 1078 {
004b8382 1079 LIB_CEC->AddLog(CEC_LOG_DEBUG, "command transmitted");
4478bc79 1080 if (bExpectResponse)
24dd566c
LOK
1081 {
1082 bReturn = m_waitForResponse->Wait(expectedResponse);
004b8382 1083 LIB_CEC->AddLog(CEC_LOG_DEBUG, bReturn ? "expected response received (%X: %s)" : "expected response not received (%X: %s)", (int)expectedResponse, ToString(expectedResponse));
24dd566c 1084 }
19cbfa8f 1085 }
b64db02e 1086 }
ae693aaa 1087 }
8fa35473 1088
b64db02e 1089 return bReturn;
8fa35473 1090}
83be0701 1091
3e61b350 1092bool CCECCommandHandler::ActivateSource(void)
83be0701 1093{
c4287bcd 1094 if (m_busDevice->IsActiveSource() &&
004b8382 1095 m_busDevice->IsHandledByLibCEC())
c4287bcd 1096 {
b0015449
LOK
1097 {
1098 CLockObject lock(m_mutex);
1099 m_bActiveSourcePending = false;
1100 }
1101
c4287bcd
LOK
1102 m_busDevice->SetPowerStatus(CEC_POWER_STATUS_ON);
1103 m_busDevice->SetMenuState(CEC_MENU_STATE_ACTIVATED);
1104
dbad810a
LOK
1105 bool bActiveSourceFailed = !m_busDevice->TransmitImageViewOn() ||
1106 !m_busDevice->TransmitActiveSource() ||
1107 !m_busDevice->TransmitMenuState(CECDEVICE_TV);
1108
1109 if (!bActiveSourceFailed)
b0015449 1110 {
dbad810a
LOK
1111 CCECPlaybackDevice *playbackDevice = m_busDevice->AsPlaybackDevice();
1112 if (playbackDevice && SendDeckStatusUpdateOnActiveSource())
1113 bActiveSourceFailed = !playbackDevice->TransmitDeckStatus(CECDEVICE_TV);
1114 }
1115
1116 if (bActiveSourceFailed)
1117 {
1118 LIB_CEC->AddLog(CEC_LOG_DEBUG, "failed to make '%s' the active source. will retry later", m_busDevice->GetLogicalAddressName());
b0015449
LOK
1119 CLockObject lock(m_mutex);
1120 m_bActiveSourcePending = true;
1121 return false;
1122 }
aa40e0e2 1123
c4287bcd 1124 m_bHandlerInited = true;
83be0701 1125 }
7dc58c9f 1126 return true;
83be0701 1127}
b499cf16
LOK
1128
1129void CCECCommandHandler::SignalOpcode(cec_opcode opcode)
1130{
1131 m_waitForResponse->Received(opcode);
1132}
b0015449
LOK
1133
1134bool CCECCommandHandler::ActiveSourcePending(void)
1135{
1136 CLockObject lock(m_mutex);
1137 return m_bActiveSourcePending;
1138}