cec: don't return from the Close() method in CUSBCECAdapterCommunication, because...
[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"
387b6f6f 37#include "../CECProcessor.h"
5477a250 38#include "../LibCEC.h"
e9de9629
LOK
39
40using namespace CEC;
8747dd4f 41using namespace std;
f00ff009 42using namespace PLATFORM;
e9de9629 43
8fa35473
LOK
44CCECCommandHandler::CCECCommandHandler(CCECBusDevice *busDevice) :
45 m_busDevice(busDevice),
46 m_processor(m_busDevice->GetProcessor()),
ae693aaa
LOK
47 m_iTransmitTimeout(CEC_DEFAULT_TRANSMIT_TIMEOUT),
48 m_iTransmitWait(CEC_DEFAULT_TRANSMIT_WAIT),
89a726fa 49 m_iTransmitRetries(CEC_DEFAULT_TRANSMIT_RETRIES),
b64db02e 50 m_bHandlerInited(false),
d79b67d8 51 m_bOPTSendDeckStatusUpdateOnActiveSource(false),
960f33c6 52 m_vendorId(CEC_VENDOR_UNKNOWN),
24dd566c 53 m_waitForResponse(new CWaitForResponse)
e9de9629 54{
8fa35473
LOK
55}
56
57CCECCommandHandler::~CCECCommandHandler(void)
58{
24dd566c 59 delete m_waitForResponse;
e9de9629
LOK
60}
61
62bool CCECCommandHandler::HandleCommand(const cec_command &command)
63{
d297cbd4
LOK
64 if (command.opcode_set == 0)
65 return HandlePoll(command);
66
3e61b350 67 bool bHandled(true);
e9de9629 68
02e7043e 69 CLibCEC::AddCommand(command);
855a3a98 70
6b72afcd 71 switch(command.opcode)
e9de9629 72 {
6b72afcd
LOK
73 case CEC_OPCODE_REPORT_POWER_STATUS:
74 HandleReportPowerStatus(command);
75 break;
76 case CEC_OPCODE_CEC_VERSION:
77 HandleDeviceCecVersion(command);
78 break;
79 case CEC_OPCODE_SET_MENU_LANGUAGE:
80 HandleSetMenuLanguage(command);
81 break;
82 case CEC_OPCODE_GIVE_PHYSICAL_ADDRESS:
3e61b350
LOK
83 if (m_processor->IsInitialised())
84 HandleGivePhysicalAddress(command);
6b72afcd
LOK
85 break;
86 case CEC_OPCODE_GIVE_OSD_NAME:
3e61b350
LOK
87 if (m_processor->IsInitialised())
88 HandleGiveOSDName(command);
6b72afcd
LOK
89 break;
90 case CEC_OPCODE_GIVE_DEVICE_VENDOR_ID:
3e61b350
LOK
91 if (m_processor->IsInitialised())
92 HandleGiveDeviceVendorId(command);
6b72afcd
LOK
93 break;
94 case CEC_OPCODE_DEVICE_VENDOR_ID:
3e61b350 95 HandleDeviceVendorId(command);
6b72afcd
LOK
96 break;
97 case CEC_OPCODE_VENDOR_COMMAND_WITH_ID:
98 HandleDeviceVendorCommandWithId(command);
99 break;
100 case CEC_OPCODE_GIVE_DECK_STATUS:
3e61b350
LOK
101 if (m_processor->IsInitialised())
102 HandleGiveDeckStatus(command);
6b72afcd
LOK
103 break;
104 case CEC_OPCODE_DECK_CONTROL:
105 HandleDeckControl(command);
106 break;
107 case CEC_OPCODE_MENU_REQUEST:
3e61b350
LOK
108 if (m_processor->IsInitialised())
109 HandleMenuRequest(command);
6b72afcd
LOK
110 break;
111 case CEC_OPCODE_GIVE_DEVICE_POWER_STATUS:
3e61b350
LOK
112 if (m_processor->IsInitialised())
113 HandleGiveDevicePowerStatus(command);
6b72afcd
LOK
114 break;
115 case CEC_OPCODE_GET_CEC_VERSION:
3e61b350
LOK
116 if (m_processor->IsInitialised())
117 HandleGetCecVersion(command);
6b72afcd
LOK
118 break;
119 case CEC_OPCODE_USER_CONTROL_PRESSED:
3e61b350
LOK
120 if (m_processor->IsInitialised())
121 HandleUserControlPressed(command);
6b72afcd
LOK
122 break;
123 case CEC_OPCODE_USER_CONTROL_RELEASE:
3e61b350
LOK
124 if (m_processor->IsInitialised())
125 HandleUserControlRelease(command);
6b72afcd
LOK
126 break;
127 case CEC_OPCODE_GIVE_AUDIO_STATUS:
3e61b350
LOK
128 if (m_processor->IsInitialised())
129 HandleGiveAudioStatus(command);
6b72afcd
LOK
130 break;
131 case CEC_OPCODE_GIVE_SYSTEM_AUDIO_MODE_STATUS:
3e61b350
LOK
132 if (m_processor->IsInitialised())
133 HandleGiveSystemAudioModeStatus(command);
6b72afcd
LOK
134 break;
135 case CEC_OPCODE_SYSTEM_AUDIO_MODE_REQUEST:
3e61b350
LOK
136 if (m_processor->IsInitialised())
137 HandleSystemAudioModeRequest(command);
aa517a0d
LOK
138 break;
139 case CEC_OPCODE_REPORT_AUDIO_STATUS:
855a3a98 140 HandleReportAudioStatus(command);
aa517a0d
LOK
141 break;
142 case CEC_OPCODE_SYSTEM_AUDIO_MODE_STATUS:
855a3a98 143 HandleSystemAudioModeStatus(command);
aa517a0d
LOK
144 break;
145 case CEC_OPCODE_SET_SYSTEM_AUDIO_MODE:
855a3a98 146 HandleSetSystemAudioMode(command);
6b72afcd
LOK
147 break;
148 case CEC_OPCODE_REQUEST_ACTIVE_SOURCE:
3e61b350
LOK
149 if (m_processor->IsInitialised())
150 HandleRequestActiveSource(command);
6b72afcd
LOK
151 break;
152 case CEC_OPCODE_SET_STREAM_PATH:
153 HandleSetStreamPath(command);
154 break;
155 case CEC_OPCODE_ROUTING_CHANGE:
156 HandleRoutingChange(command);
157 break;
907bd60f
LOK
158 case CEC_OPCODE_ROUTING_INFORMATION:
159 HandleRoutingInformation(command);
160 break;
6b72afcd 161 case CEC_OPCODE_STANDBY:
3e61b350
LOK
162 if (m_processor->IsInitialised())
163 HandleStandby(command);
6b72afcd
LOK
164 break;
165 case CEC_OPCODE_ACTIVE_SOURCE:
166 HandleActiveSource(command);
167 break;
907bd60f
LOK
168 case CEC_OPCODE_REPORT_PHYSICAL_ADDRESS:
169 HandleReportPhysicalAddress(command);
170 break;
15d1a84c
LOK
171 case CEC_OPCODE_SET_OSD_NAME:
172 HandleSetOSDName(command);
173 break;
1a6669b8
LOK
174 case CEC_OPCODE_IMAGE_VIEW_ON:
175 HandleImageViewOn(command);
176 break;
177 case CEC_OPCODE_TEXT_VIEW_ON:
178 HandleTextViewOn(command);
179 break;
4d738fe3
LOK
180 case CEC_OPCODE_FEATURE_ABORT:
181 HandleFeatureAbort(command);
182 break;
468a1414
LOK
183 case CEC_OPCODE_VENDOR_COMMAND:
184 HandleVendorCommand(command);
185 break;
6b72afcd
LOK
186 default:
187 UnhandledCommand(command);
e9de9629 188 bHandled = false;
6b72afcd 189 break;
e9de9629
LOK
190 }
191
3e61b350 192 if (bHandled)
24dd566c 193 m_waitForResponse->Received((command.opcode == CEC_OPCODE_FEATURE_ABORT && command.parameters.size > 0) ? (cec_opcode)command.parameters[0] : command.opcode);
8fa35473 194
e9de9629
LOK
195 return bHandled;
196}
197
be5b0e24
LOK
198bool CCECCommandHandler::HandleActiveSource(const cec_command &command)
199{
200 if (command.parameters.size == 2)
201 {
202 uint16_t iAddress = ((uint16_t)command.parameters[0] << 8) | ((uint16_t)command.parameters[1]);
37b0c572 203 return m_processor->SetActiveSource(iAddress);
be5b0e24
LOK
204 }
205
206 return true;
207}
208
a9232a79
LOK
209bool CCECCommandHandler::HandleDeckControl(const cec_command &command)
210{
211 CCECBusDevice *device = GetDevice(command.destination);
88e5de6f 212 if (device && (device->GetType() == CEC_DEVICE_TYPE_PLAYBACK_DEVICE || device->GetType() == CEC_DEVICE_TYPE_RECORDING_DEVICE) && command.parameters.size > 0)
a9232a79
LOK
213 {
214 ((CCECPlaybackDevice *) device)->SetDeckControlMode((cec_deck_control_mode) command.parameters[0]);
215 return true;
216 }
217
218 return false;
219}
220
6a1c0009
LOK
221bool CCECCommandHandler::HandleDeviceCecVersion(const cec_command &command)
222{
223 if (command.parameters.size == 1)
224 {
225 CCECBusDevice *device = GetDevice(command.initiator);
226 if (device)
227 device->SetCecVersion((cec_version) command.parameters[0]);
228 }
229
230 return true;
231}
232
e9de9629
LOK
233bool CCECCommandHandler::HandleDeviceVendorCommandWithId(const cec_command &command)
234{
8e57f83c 235 if (m_processor->IsRunning() && m_busDevice->MyLogicalAddressContains(command.destination))
fcf10e27 236 m_processor->TransmitAbort(command.initiator, command.opcode, CEC_ABORT_REASON_REFUSED);
6b72afcd 237
6a1c0009 238 return true;
e9de9629
LOK
239}
240
241bool CCECCommandHandler::HandleDeviceVendorId(const cec_command &command)
242{
8fa35473 243 return SetVendorId(command);
e9de9629
LOK
244}
245
4d738fe3
LOK
246bool CCECCommandHandler::HandleFeatureAbort(const cec_command &command)
247{
69a1a673
LOK
248 if (command.parameters.size == 2 &&
249 (command.parameters[1] == CEC_ABORT_REASON_UNRECOGNIZED_OPCODE ||
250 command.parameters[1] == CEC_ABORT_REASON_REFUSED))
4d738fe3 251 m_processor->m_busDevices[command.initiator]->SetUnsupportedFeature((cec_opcode)command.parameters[0]);
7de6ad36 252 return true;
4d738fe3
LOK
253}
254
e9de9629
LOK
255bool CCECCommandHandler::HandleGetCecVersion(const cec_command &command)
256{
8e57f83c 257 if (m_processor->IsRunning() && m_busDevice->MyLogicalAddressContains(command.destination))
6b72afcd
LOK
258 {
259 CCECBusDevice *device = GetDevice(command.destination);
260 if (device)
261 return device->TransmitCECVersion(command.initiator);
262 }
0f23c85c
LOK
263
264 return false;
e9de9629
LOK
265}
266
a1f8fb1b
LOK
267bool CCECCommandHandler::HandleGiveAudioStatus(const cec_command &command)
268{
8e57f83c 269 if (m_processor->IsRunning() && m_busDevice->MyLogicalAddressContains(command.destination))
6b72afcd
LOK
270 {
271 CCECBusDevice *device = GetDevice(command.destination);
272 if (device && device->GetType() == CEC_DEVICE_TYPE_AUDIO_SYSTEM)
273 return ((CCECAudioSystem *) device)->TransmitAudioStatus(command.initiator);
274 }
a1f8fb1b
LOK
275
276 return false;
277}
278
e9de9629
LOK
279bool CCECCommandHandler::HandleGiveDeckStatus(const cec_command &command)
280{
8e57f83c 281 if (m_processor->IsRunning() && m_busDevice->MyLogicalAddressContains(command.destination))
6b72afcd
LOK
282 {
283 CCECBusDevice *device = GetDevice(command.destination);
284 if (device && (device->GetType() == CEC_DEVICE_TYPE_PLAYBACK_DEVICE || device->GetType() == CEC_DEVICE_TYPE_RECORDING_DEVICE))
285 return ((CCECPlaybackDevice *) device)->TransmitDeckStatus(command.initiator);
286 }
0f23c85c
LOK
287
288 return false;
e9de9629
LOK
289}
290
291bool CCECCommandHandler::HandleGiveDevicePowerStatus(const cec_command &command)
292{
8e57f83c 293 if (m_processor->IsRunning() && m_busDevice->MyLogicalAddressContains(command.destination))
6b72afcd
LOK
294 {
295 CCECBusDevice *device = GetDevice(command.destination);
296 if (device)
297 return device->TransmitPowerState(command.initiator);
298 }
0f23c85c
LOK
299
300 return false;
e9de9629
LOK
301}
302
303bool CCECCommandHandler::HandleGiveDeviceVendorId(const cec_command &command)
304{
8e57f83c 305 if (m_processor->IsRunning() && m_busDevice->MyLogicalAddressContains(command.destination))
6b72afcd
LOK
306 {
307 CCECBusDevice *device = GetDevice(command.destination);
308 if (device)
309 return device->TransmitVendorID(command.initiator);
310 }
0f23c85c
LOK
311
312 return false;
e9de9629
LOK
313}
314
315bool CCECCommandHandler::HandleGiveOSDName(const cec_command &command)
316{
8e57f83c 317 if (m_processor->IsRunning() && m_busDevice->MyLogicalAddressContains(command.destination))
6b72afcd
LOK
318 {
319 CCECBusDevice *device = GetDevice(command.destination);
320 if (device)
321 return device->TransmitOSDName(command.initiator);
322 }
0f23c85c
LOK
323
324 return false;
e9de9629
LOK
325}
326
327bool CCECCommandHandler::HandleGivePhysicalAddress(const cec_command &command)
328{
8e57f83c 329 if (m_processor->IsRunning() && m_busDevice->MyLogicalAddressContains(command.destination))
6b72afcd
LOK
330 {
331 CCECBusDevice *device = GetDevice(command.destination);
332 if (device)
3c20db73
LOK
333 {
334 device->SetActiveSource();
335 return device->TransmitPhysicalAddress() &&
49c8f2e4 336 device->TransmitImageViewOn() &&
3c20db73
LOK
337 device->TransmitActiveSource();
338 }
6b72afcd 339 }
09c10b66
LOK
340
341 return false;
e9de9629
LOK
342}
343
1a6669b8
LOK
344bool CCECCommandHandler::HandleGiveSystemAudioModeStatus(const cec_command &command)
345{
8e57f83c 346 if (m_processor->IsRunning() && m_busDevice->MyLogicalAddressContains(command.destination))
1a6669b8
LOK
347 {
348 CCECBusDevice *device = GetDevice(command.destination);
349 if (device && device->GetType() == CEC_DEVICE_TYPE_AUDIO_SYSTEM)
350 return ((CCECAudioSystem *) device)->TransmitSystemAudioModeStatus(command.initiator);
351 }
352
353 return false;
354}
355
356bool CCECCommandHandler::HandleImageViewOn(const cec_command &command)
357{
37b0c572 358 m_processor->m_busDevices[command.initiator]->SetActiveSource();
1a6669b8
LOK
359 return true;
360}
361
e9de9629
LOK
362bool CCECCommandHandler::HandleMenuRequest(const cec_command &command)
363{
8e57f83c 364 if (m_processor->IsRunning() && m_busDevice->MyLogicalAddressContains(command.destination))
0f23c85c 365 {
6b72afcd
LOK
366 if (command.parameters[0] == CEC_MENU_REQUEST_TYPE_QUERY)
367 {
368 CCECBusDevice *device = GetDevice(command.destination);
369 if (device)
370 return device->TransmitMenuState(command.initiator);
371 }
15d1a84c
LOK
372 }
373
374 return false;
375}
376
d297cbd4
LOK
377bool CCECCommandHandler::HandlePoll(const cec_command &command)
378{
379 m_busDevice->HandlePoll(command.initiator);
380 return true;
381}
382
15d1a84c
LOK
383bool CCECCommandHandler::HandleReportAudioStatus(const cec_command &command)
384{
385 if (command.parameters.size == 1)
386 {
387 CCECBusDevice *device = GetDevice(command.initiator);
388 if (device && device->GetType() == CEC_DEVICE_TYPE_AUDIO_SYSTEM)
6b72afcd 389 {
15d1a84c
LOK
390 ((CCECAudioSystem *)device)->SetAudioStatus(command.parameters[0]);
391 return true;
6b72afcd 392 }
0f23c85c
LOK
393 }
394 return false;
e9de9629
LOK
395}
396
907bd60f
LOK
397bool CCECCommandHandler::HandleReportPhysicalAddress(const cec_command &command)
398{
0bfce006 399 if (command.parameters.size == 3)
907bd60f
LOK
400 {
401 uint16_t iNewAddress = ((uint16_t)command.parameters[0] << 8) | ((uint16_t)command.parameters[1]);
16b1e052 402 SetPhysicalAddress(command.initiator, iNewAddress);
907bd60f
LOK
403 }
404 return true;
405}
406
e55f3f70
LOK
407bool CCECCommandHandler::HandleReportPowerStatus(const cec_command &command)
408{
409 if (command.parameters.size == 1)
410 {
411 CCECBusDevice *device = GetDevice(command.initiator);
412 if (device)
413 device->SetPowerStatus((cec_power_status) command.parameters[0]);
414 }
415 return true;
416}
417
e9de9629
LOK
418bool CCECCommandHandler::HandleRequestActiveSource(const cec_command &command)
419{
8e57f83c 420 if (m_processor->IsRunning())
5f316715 421 {
5477a250 422 CLibCEC::AddLog(CEC_LOG_DEBUG, ">> %i requests active source", (uint8_t) command.initiator);
8747dd4f 423
5f316715 424 vector<CCECBusDevice *> devices;
f00ff009 425 for (size_t iDevicePtr = 0; iDevicePtr < GetMyDevices(devices); iDevicePtr++)
5f316715 426 devices[iDevicePtr]->TransmitActiveSource();
c4098482 427
5f316715
LOK
428 return true;
429 }
430 return false;
e9de9629
LOK
431}
432
433bool CCECCommandHandler::HandleRoutingChange(const cec_command &command)
434{
435 if (command.parameters.size == 4)
436 {
437 uint16_t iOldAddress = ((uint16_t)command.parameters[0] << 8) | ((uint16_t)command.parameters[1]);
438 uint16_t iNewAddress = ((uint16_t)command.parameters[2] << 8) | ((uint16_t)command.parameters[3]);
e9de9629 439
0f23c85c
LOK
440 CCECBusDevice *device = GetDevice(command.initiator);
441 if (device)
9dc04b07 442 device->SetStreamPath(iNewAddress, iOldAddress);
e9de9629
LOK
443 }
444 return true;
445}
446
907bd60f
LOK
447bool CCECCommandHandler::HandleRoutingInformation(const cec_command &command)
448{
449 if (command.parameters.size == 2)
450 {
451 uint16_t iNewAddress = ((uint16_t)command.parameters[0] << 8) | ((uint16_t)command.parameters[1]);
37b0c572 452 m_processor->SetActiveSource(iNewAddress);
907bd60f 453 }
15d1a84c
LOK
454
455 return false;
907bd60f
LOK
456}
457
a3269a0a
LOK
458bool CCECCommandHandler::HandleSetMenuLanguage(const cec_command &command)
459{
460 if (command.parameters.size == 3)
461 {
462 CCECBusDevice *device = GetDevice(command.initiator);
463 if (device)
464 {
465 cec_menu_language language;
466 language.device = command.initiator;
56701628 467 for (uint8_t iPtr = 0; iPtr < 4; iPtr++)
a3269a0a
LOK
468 language.language[iPtr] = command.parameters[iPtr];
469 language.language[3] = 0;
470 device->SetMenuLanguage(language);
15d1a84c
LOK
471 return true;
472 }
473 }
474 return false;
475}
476
477bool CCECCommandHandler::HandleSetOSDName(const cec_command &command)
478{
479 if (command.parameters.size > 0)
480 {
481 CCECBusDevice *device = GetDevice(command.initiator);
482 if (device)
483 {
484 char buf[1024];
485 for (uint8_t iPtr = 0; iPtr < command.parameters.size; iPtr++)
486 buf[iPtr] = (char)command.parameters[iPtr];
487 buf[command.parameters.size] = 0;
488
489 CStdString strName(buf);
490 device->SetOSDName(strName);
6b72afcd 491
15d1a84c 492 return true;
a3269a0a
LOK
493 }
494 }
15d1a84c 495 return false;
a3269a0a
LOK
496}
497
e9de9629
LOK
498bool CCECCommandHandler::HandleSetStreamPath(const cec_command &command)
499{
8e57f83c 500 if (m_processor->IsRunning() && command.parameters.size >= 2)
e9de9629 501 {
b6c7bc94 502 uint16_t iStreamAddress = ((uint16_t)command.parameters[0] << 8) | ((uint16_t)command.parameters[1]);
5477a250 503 CLibCEC::AddLog(CEC_LOG_DEBUG, ">> %i sets stream path to physical address %04x", command.initiator, iStreamAddress);
72689600
LOK
504
505 /* one of the device handled by libCEC has been made active */
506 CCECBusDevice *device = GetDeviceByPhysicalAddress(iStreamAddress);
507 if (device && m_busDevice->MyLogicalAddressContains(device->GetLogicalAddress()))
508 {
509 device->SetActiveSource();
49c8f2e4 510 device->TransmitImageViewOn();
72689600 511 device->TransmitActiveSource();
538d8471
LOK
512
513 device->SetMenuState(CEC_MENU_STATE_ACTIVATED);
514 device->TransmitMenuState(command.initiator);
72689600 515 }
e9de9629 516 }
15d1a84c 517 return false;
e9de9629
LOK
518}
519
aa517a0d 520bool CCECCommandHandler::HandleSystemAudioModeRequest(const cec_command &command)
e5e86c76 521{
8e57f83c 522 if (m_processor->IsRunning() && m_busDevice->MyLogicalAddressContains(command.destination))
e5e86c76
LOK
523 {
524 CCECBusDevice *device = GetDevice(command.destination);
aa517a0d
LOK
525 if (device && device->GetType() == CEC_DEVICE_TYPE_AUDIO_SYSTEM)
526 {
527 if (command.parameters.size >= 2)
528 {
529 device->SetPowerStatus(CEC_POWER_STATUS_ON);
530 ((CCECAudioSystem *) device)->SetSystemAudioModeStatus(CEC_SYSTEM_AUDIO_STATUS_ON);
531 uint16_t iNewAddress = ((uint16_t)command.parameters[0] << 8) | ((uint16_t)command.parameters[1]);
532 CCECBusDevice *newActiveDevice = GetDeviceByPhysicalAddress(iNewAddress);
533 if (newActiveDevice)
37b0c572 534 newActiveDevice->SetActiveSource();
aa517a0d
LOK
535 return ((CCECAudioSystem *) device)->TransmitSetSystemAudioMode(command.initiator);
536 }
537 else
538 {
539 ((CCECAudioSystem *) device)->SetSystemAudioModeStatus(CEC_SYSTEM_AUDIO_STATUS_OFF);
540 return ((CCECAudioSystem *) device)->TransmitSetSystemAudioMode(command.initiator);
541 }
542 }
e5e86c76 543 }
6b72afcd 544 return false;
e5e86c76
LOK
545}
546
4d6b4433
LOK
547bool CCECCommandHandler::HandleStandby(const cec_command &command)
548{
549 CCECBusDevice *device = GetDevice(command.initiator);
550 if (device)
551 device->SetPowerStatus(CEC_POWER_STATUS_STANDBY);
6b72afcd 552
4d6b4433
LOK
553 return true;
554}
555
aa517a0d 556bool CCECCommandHandler::HandleSystemAudioModeStatus(const cec_command &command)
868dc71f 557{
aa517a0d 558 if (command.parameters.size == 1)
868dc71f 559 {
aa517a0d
LOK
560 CCECBusDevice *device = GetDevice(command.initiator);
561 if (device && device->GetType() == CEC_DEVICE_TYPE_AUDIO_SYSTEM)
562 {
563 ((CCECAudioSystem *)device)->SetSystemAudioModeStatus((cec_system_audio_status)command.parameters[0]);
564 return true;
565 }
566 }
567
568 return false;
569}
570
571bool CCECCommandHandler::HandleSetSystemAudioMode(const cec_command &command)
572{
573 if (command.parameters.size == 1)
574 {
575 CCECBusDevice *device = GetDevice(command.initiator);
576 if (device && device->GetType() == CEC_DEVICE_TYPE_AUDIO_SYSTEM)
577 {
578 ((CCECAudioSystem *)device)->SetSystemAudioModeStatus((cec_system_audio_status)command.parameters[0]);
579 return true;
580 }
868dc71f
LOK
581 }
582
583 return false;
584}
585
1a6669b8 586bool CCECCommandHandler::HandleTextViewOn(const cec_command &command)
cf0ecd85 587{
37b0c572 588 m_processor->m_busDevices[command.initiator]->SetActiveSource();
1a6669b8 589 return true;
cf0ecd85
LOK
590}
591
e9de9629
LOK
592bool CCECCommandHandler::HandleUserControlPressed(const cec_command &command)
593{
e33e0d75
MK
594 if (m_processor->IsRunning() &&
595 m_busDevice->MyLogicalAddressContains(command.destination) &&
596 command.parameters.size > 0)
e9de9629 597 {
02e7043e 598 CLibCEC::AddKey();
e9de9629 599 if (command.parameters[0] <= CEC_USER_CONTROL_CODE_MAX)
e33e0d75
MK
600 CLibCEC::SetCurrentButton((cec_user_control_code) command.parameters[0]);
601
602 if (command.parameters[0] == CEC_USER_CONTROL_CODE_POWER ||
603 command.parameters[0] == CEC_USER_CONTROL_CODE_POWER_ON_FUNCTION)
e9de9629 604 {
e33e0d75
MK
605 bool bPowerOn(true);
606 CCECBusDevice *device = GetDevice(command.destination);
607 if (!device)
608 return true;
609
610 // CEC_USER_CONTROL_CODE_POWER operates as a toggle
611 // assume CEC_USER_CONTROL_CODE_POWER_ON_FUNCTION does not
612 if (command.parameters[0] == CEC_USER_CONTROL_CODE_POWER)
68391d4f 613 {
e33e0d75
MK
614 cec_power_status status = device->GetPowerStatus();
615 bPowerOn = !(status == CEC_POWER_STATUS_ON || status == CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON);
616 }
617
618 if (bPowerOn)
619 {
620 device->SetActiveSource();
621 device->TransmitImageViewOn();
622 device->TransmitActiveSource();
623
624 if (device->GetType() == CEC_DEVICE_TYPE_PLAYBACK_DEVICE ||
625 device->GetType() == CEC_DEVICE_TYPE_RECORDING_DEVICE)
626 ((CCECPlaybackDevice *)device)->TransmitDeckStatus(command.initiator);
68391d4f 627 }
d1ab114f
LOK
628 else
629 {
e33e0d75
MK
630 device->SetInactiveSource();
631 device->TransmitInactiveSource();
632 device->SetMenuState(CEC_MENU_STATE_DEACTIVATED);
d1ab114f 633 }
e9de9629 634 }
e33e0d75
MK
635
636 return true;
e9de9629 637 }
15d1a84c 638 return false;
e9de9629
LOK
639}
640
641bool CCECCommandHandler::HandleUserControlRelease(const cec_command &command)
642{
8e57f83c 643 if (m_processor->IsRunning() && m_busDevice->MyLogicalAddressContains(command.destination))
02e7043e 644 CLibCEC::AddKey();
6b72afcd 645
e9de9629
LOK
646 return true;
647}
648
1de6617c
LOK
649bool CCECCommandHandler::HandleVendorCommand(const cec_command & UNUSED(command))
650{
651 return true;
652}
653
e9de9629
LOK
654void CCECCommandHandler::UnhandledCommand(const cec_command &command)
655{
5477a250 656 CLibCEC::AddLog(CEC_LOG_DEBUG, "unhandled command with opcode %02x from address %d", command.opcode, command.initiator);
0f23c85c
LOK
657}
658
6f14b512 659size_t CCECCommandHandler::GetMyDevices(vector<CCECBusDevice *> &devices) const
8747dd4f 660{
6f14b512 661 size_t iReturn(0);
8747dd4f 662
fcf10e27 663 cec_logical_addresses addresses = m_processor->GetLogicalAddresses();
f2198ab5 664 for (uint8_t iPtr = 0; iPtr < 16; iPtr++)
8747dd4f
LOK
665 {
666 if (addresses[iPtr])
667 {
668 devices.push_back(GetDevice((cec_logical_address) iPtr));
669 ++iReturn;
670 }
671 }
672
673 return iReturn;
674}
675
0f23c85c
LOK
676CCECBusDevice *CCECCommandHandler::GetDevice(cec_logical_address iLogicalAddress) const
677{
678 CCECBusDevice *device = NULL;
679
680 if (iLogicalAddress >= CECDEVICE_TV && iLogicalAddress <= CECDEVICE_BROADCAST)
fcf10e27 681 device = m_processor->m_busDevices[iLogicalAddress];
0f23c85c
LOK
682
683 return device;
e9de9629 684}
6685ae07
LOK
685
686CCECBusDevice *CCECCommandHandler::GetDeviceByPhysicalAddress(uint16_t iPhysicalAddress) const
687{
fcf10e27 688 return m_processor->GetDeviceByPhysicalAddress(iPhysicalAddress);
6685ae07 689}
181b3475 690
c4098482
LOK
691CCECBusDevice *CCECCommandHandler::GetDeviceByType(cec_device_type type) const
692{
fcf10e27 693 return m_processor->GetDeviceByType(type);
c4098482
LOK
694}
695
8fa35473 696bool CCECCommandHandler::SetVendorId(const cec_command &command)
181b3475 697{
8fa35473 698 bool bChanged(false);
181b3475
LOK
699 if (command.parameters.size < 3)
700 {
5477a250 701 CLibCEC::AddLog(CEC_LOG_WARNING, "invalid vendor ID received");
8fa35473 702 return bChanged;
181b3475
LOK
703 }
704
bae71306
LOK
705 uint64_t iVendorId = ((uint64_t)command.parameters[0] << 16) +
706 ((uint64_t)command.parameters[1] << 8) +
181b3475
LOK
707 (uint64_t)command.parameters[2];
708
709 CCECBusDevice *device = GetDevice((cec_logical_address) command.initiator);
710 if (device)
8fa35473
LOK
711 bChanged = device->SetVendorId(iVendorId);
712 return bChanged;
181b3475 713}
5e822b09 714
16b1e052
LOK
715void CCECCommandHandler::SetPhysicalAddress(cec_logical_address iAddress, uint16_t iNewAddress)
716{
717 if (!m_busDevice->MyLogicalAddressContains(iAddress))
718 {
fcf10e27 719 bool bOurAddress(m_processor->GetPhysicalAddress() == iNewAddress);
16b1e052
LOK
720 GetDevice(iAddress)->SetPhysicalAddress(iNewAddress);
721 if (bOurAddress)
722 {
723 /* another device reported the same physical address as ours
724 * since we don't have physical address detection yet, we'll just use the
725 * given address, increased by 0x100 for now */
fcf10e27 726 m_processor->SetPhysicalAddress(iNewAddress + 0x100);
16b1e052
LOK
727 }
728 }
729}
855a3a98 730
f4698390
LOK
731bool CCECCommandHandler::PowerOn(const cec_logical_address iInitiator, const cec_logical_address iDestination)
732{
733 if (iDestination == CECDEVICE_TV)
734 return TransmitImageViewOn(iInitiator, iDestination);
735
736 return TransmitKeypress(iInitiator, iDestination, CEC_USER_CONTROL_CODE_POWER) &&
737 TransmitKeyRelease(iInitiator, iDestination);
738}
739
b64db02e 740bool CCECCommandHandler::TransmitImageViewOn(const cec_logical_address iInitiator, const cec_logical_address iDestination)
8fa35473
LOK
741{
742 cec_command command;
ae693aaa 743 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_IMAGE_VIEW_ON);
8fa35473 744
19cbfa8f 745 return Transmit(command, false);
8fa35473
LOK
746}
747
748bool CCECCommandHandler::TransmitStandby(const cec_logical_address iInitiator, const cec_logical_address iDestination)
749{
750 cec_command command;
ae693aaa 751 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_STANDBY);
8fa35473 752
19cbfa8f 753 return Transmit(command, false);
8fa35473
LOK
754}
755
756bool CCECCommandHandler::TransmitRequestCecVersion(const cec_logical_address iInitiator, const cec_logical_address iDestination)
757{
758 cec_command command;
ae693aaa 759 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_GET_CEC_VERSION);
8fa35473 760
5e526919 761 return Transmit(command, true, CEC_OPCODE_CEC_VERSION);
8fa35473
LOK
762}
763
764bool CCECCommandHandler::TransmitRequestMenuLanguage(const cec_logical_address iInitiator, const cec_logical_address iDestination)
765{
766 cec_command command;
ae693aaa 767 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_GET_MENU_LANGUAGE);
8fa35473 768
5e526919 769 return Transmit(command, true, CEC_OPCODE_SET_MENU_LANGUAGE);
8fa35473
LOK
770}
771
772bool CCECCommandHandler::TransmitRequestOSDName(const cec_logical_address iInitiator, const cec_logical_address iDestination)
773{
774 cec_command command;
ae693aaa 775 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_GIVE_OSD_NAME);
8fa35473 776
5e526919 777 return Transmit(command, true, CEC_OPCODE_SET_OSD_NAME);
8fa35473
LOK
778}
779
780bool CCECCommandHandler::TransmitRequestPhysicalAddress(const cec_logical_address iInitiator, const cec_logical_address iDestination)
781{
782 cec_command command;
ae693aaa 783 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_GIVE_PHYSICAL_ADDRESS);
8fa35473 784
5e526919 785 return Transmit(command, true, CEC_OPCODE_REPORT_PHYSICAL_ADDRESS);
8fa35473
LOK
786}
787
788bool CCECCommandHandler::TransmitRequestPowerStatus(const cec_logical_address iInitiator, const cec_logical_address iDestination)
789{
790 cec_command command;
ae693aaa 791 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_GIVE_DEVICE_POWER_STATUS);
8fa35473 792
5e526919 793 return Transmit(command, true, CEC_OPCODE_REPORT_POWER_STATUS);
8fa35473
LOK
794}
795
796bool CCECCommandHandler::TransmitRequestVendorId(const cec_logical_address iInitiator, const cec_logical_address iDestination)
797{
798 cec_command command;
ae693aaa 799 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_GIVE_DEVICE_VENDOR_ID);
8fa35473 800
5e526919 801 return Transmit(command, true, CEC_OPCODE_DEVICE_VENDOR_ID);
8fa35473
LOK
802}
803
804bool CCECCommandHandler::TransmitActiveSource(const cec_logical_address iInitiator, uint16_t iPhysicalAddress)
805{
806 cec_command command;
ae693aaa 807 cec_command::Format(command, iInitiator, CECDEVICE_BROADCAST, CEC_OPCODE_ACTIVE_SOURCE);
8fa35473
LOK
808 command.parameters.PushBack((uint8_t) ((iPhysicalAddress >> 8) & 0xFF));
809 command.parameters.PushBack((uint8_t) (iPhysicalAddress & 0xFF));
810
19cbfa8f 811 return Transmit(command, false);
8fa35473
LOK
812}
813
814bool CCECCommandHandler::TransmitCECVersion(const cec_logical_address iInitiator, const cec_logical_address iDestination, cec_version cecVersion)
815{
816 cec_command command;
ae693aaa 817 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_CEC_VERSION);
8fa35473
LOK
818 command.parameters.PushBack((uint8_t)cecVersion);
819
19cbfa8f 820 return Transmit(command, false);
8fa35473
LOK
821}
822
823bool CCECCommandHandler::TransmitInactiveSource(const cec_logical_address iInitiator, uint16_t iPhysicalAddress)
824{
825 cec_command command;
ae693aaa 826 cec_command::Format(command, iInitiator, CECDEVICE_TV, CEC_OPCODE_INACTIVE_SOURCE);
8fa35473
LOK
827 command.parameters.PushBack((iPhysicalAddress >> 8) & 0xFF);
828 command.parameters.PushBack(iPhysicalAddress & 0xFF);
829
19cbfa8f 830 return Transmit(command, false);
8fa35473
LOK
831}
832
833bool CCECCommandHandler::TransmitMenuState(const cec_logical_address iInitiator, const cec_logical_address iDestination, cec_menu_state menuState)
834{
835 cec_command command;
ae693aaa 836 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_MENU_STATUS);
8fa35473
LOK
837 command.parameters.PushBack((uint8_t)menuState);
838
19cbfa8f 839 return Transmit(command, false);
8fa35473
LOK
840}
841
842bool CCECCommandHandler::TransmitOSDName(const cec_logical_address iInitiator, const cec_logical_address iDestination, CStdString strDeviceName)
843{
844 cec_command command;
ae693aaa 845 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_SET_OSD_NAME);
6f14b512 846 for (size_t iPtr = 0; iPtr < strDeviceName.length(); iPtr++)
8fa35473
LOK
847 command.parameters.PushBack(strDeviceName.at(iPtr));
848
19cbfa8f 849 return Transmit(command, false);
8fa35473
LOK
850}
851
852bool CCECCommandHandler::TransmitOSDString(const cec_logical_address iInitiator, const cec_logical_address iDestination, cec_display_control duration, const char *strMessage)
853{
854 cec_command command;
ae693aaa 855 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_SET_OSD_STRING);
8fa35473
LOK
856 command.parameters.PushBack((uint8_t)duration);
857
6f14b512 858 size_t iLen = strlen(strMessage);
8fa35473
LOK
859 if (iLen > 13) iLen = 13;
860
6f14b512 861 for (size_t iPtr = 0; iPtr < iLen; iPtr++)
8fa35473
LOK
862 command.parameters.PushBack(strMessage[iPtr]);
863
19cbfa8f 864 return Transmit(command, false);
8fa35473
LOK
865}
866
867bool CCECCommandHandler::TransmitPhysicalAddress(const cec_logical_address iInitiator, uint16_t iPhysicalAddress, cec_device_type type)
868{
869 cec_command command;
ae693aaa 870 cec_command::Format(command, iInitiator, CECDEVICE_BROADCAST, CEC_OPCODE_REPORT_PHYSICAL_ADDRESS);
8fa35473
LOK
871 command.parameters.PushBack((uint8_t) ((iPhysicalAddress >> 8) & 0xFF));
872 command.parameters.PushBack((uint8_t) (iPhysicalAddress & 0xFF));
873 command.parameters.PushBack((uint8_t) (type));
874
19cbfa8f 875 return Transmit(command, false);
8fa35473
LOK
876}
877
878bool CCECCommandHandler::TransmitPoll(const cec_logical_address iInitiator, const cec_logical_address iDestination)
879{
880 cec_command command;
ae693aaa 881 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_NONE);
8fa35473 882
ae693aaa 883 return Transmit(command, false);
8fa35473
LOK
884}
885
886bool CCECCommandHandler::TransmitPowerState(const cec_logical_address iInitiator, const cec_logical_address iDestination, cec_power_status state)
887{
888 cec_command command;
ae693aaa 889 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_REPORT_POWER_STATUS);
8fa35473
LOK
890 command.parameters.PushBack((uint8_t) state);
891
19cbfa8f 892 return Transmit(command, false);
8fa35473
LOK
893}
894
895bool CCECCommandHandler::TransmitVendorID(const cec_logical_address iInitiator, uint64_t iVendorId)
896{
897 cec_command command;
ae693aaa 898 cec_command::Format(command, iInitiator, CECDEVICE_BROADCAST, CEC_OPCODE_DEVICE_VENDOR_ID);
8fa35473
LOK
899
900 command.parameters.PushBack((uint8_t) (((uint64_t)iVendorId >> 16) & 0xFF));
901 command.parameters.PushBack((uint8_t) (((uint64_t)iVendorId >> 8) & 0xFF));
902 command.parameters.PushBack((uint8_t) ((uint64_t)iVendorId & 0xFF));
903
19cbfa8f 904 return Transmit(command, false);
8fa35473
LOK
905}
906
907bool CCECCommandHandler::TransmitAudioStatus(const cec_logical_address iInitiator, const cec_logical_address iDestination, uint8_t state)
908{
909 cec_command command;
ae693aaa 910 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_REPORT_AUDIO_STATUS);
8fa35473
LOK
911 command.parameters.PushBack(state);
912
19cbfa8f 913 return Transmit(command, false);
8fa35473
LOK
914}
915
916bool CCECCommandHandler::TransmitSetSystemAudioMode(const cec_logical_address iInitiator, const cec_logical_address iDestination, cec_system_audio_status state)
917{
918 cec_command command;
ae693aaa 919 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_SET_SYSTEM_AUDIO_MODE);
8fa35473
LOK
920 command.parameters.PushBack((uint8_t)state);
921
19cbfa8f 922 return Transmit(command, false);
8fa35473
LOK
923}
924
f42d3e0f
LOK
925bool CCECCommandHandler::TransmitSetStreamPath(uint16_t iStreamPath)
926{
927 cec_command command;
928 cec_command::Format(command, m_busDevice->GetLogicalAddress(), CECDEVICE_BROADCAST, CEC_OPCODE_SET_STREAM_PATH);
929 command.parameters.PushBack((uint8_t) ((iStreamPath >> 8) & 0xFF));
930 command.parameters.PushBack((uint8_t) (iStreamPath & 0xFF));
931
932 return Transmit(command, false);
933}
934
8fa35473
LOK
935bool CCECCommandHandler::TransmitSystemAudioModeStatus(const cec_logical_address iInitiator, const cec_logical_address iDestination, cec_system_audio_status state)
936{
937 cec_command command;
ae693aaa 938 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_SYSTEM_AUDIO_MODE_STATUS);
8fa35473
LOK
939 command.parameters.PushBack((uint8_t)state);
940
19cbfa8f 941 return Transmit(command, false);
8fa35473
LOK
942}
943
944bool CCECCommandHandler::TransmitDeckStatus(const cec_logical_address iInitiator, const cec_logical_address iDestination, cec_deck_info state)
945{
946 cec_command command;
ae693aaa 947 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_DECK_STATUS);
8fa35473
LOK
948 command.PushBack((uint8_t)state);
949
19cbfa8f 950 return Transmit(command, false);
8fa35473
LOK
951}
952
4bec9d79 953bool CCECCommandHandler::TransmitKeypress(const cec_logical_address iInitiator, const cec_logical_address iDestination, cec_user_control_code key, bool bWait /* = true */)
8fa35473
LOK
954{
955 cec_command command;
ae693aaa 956 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_USER_CONTROL_PRESSED);
8fa35473
LOK
957 command.parameters.PushBack((uint8_t)key);
958
4bec9d79 959 return Transmit(command, bWait);
8fa35473
LOK
960}
961
4bec9d79 962bool CCECCommandHandler::TransmitKeyRelease(const cec_logical_address iInitiator, const cec_logical_address iDestination, bool bWait /* = true */)
8fa35473
LOK
963{
964 cec_command command;
ae693aaa 965 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_USER_CONTROL_RELEASE);
8fa35473 966
4bec9d79 967 return Transmit(command, bWait);
8fa35473
LOK
968}
969
446c0cfc 970bool CCECCommandHandler::Transmit(cec_command &command, bool bExpectResponse /* = true */, cec_opcode expectedResponse /* = CEC_OPCODE_NONE */)
8fa35473 971{
b64db02e 972 bool bReturn(false);
ae693aaa 973 command.transmit_timeout = m_iTransmitTimeout;
ae693aaa 974
ae693aaa 975 {
861832f6 976 uint8_t iTries(0), iMaxTries(command.opcode == CEC_OPCODE_NONE ? 1 : m_iTransmitRetries + 1);
f00ff009 977 CLockObject writeLock(m_processor->m_transmitMutex);
19cbfa8f 978 while (!bReturn && ++iTries <= iMaxTries)
b64db02e 979 {
99666519 980 if ((bReturn = m_processor->Transmit(command)) == true)
19cbfa8f 981 {
5477a250 982 CLibCEC::AddLog(CEC_LOG_DEBUG, "command transmitted");
4478bc79 983 if (bExpectResponse)
24dd566c
LOK
984 {
985 bReturn = m_waitForResponse->Wait(expectedResponse);
986 CLibCEC::AddLog(CEC_LOG_DEBUG, bReturn ? "expected response received (%X: %s)" : "expected response not received (%X: %s)", (int)expectedResponse, m_processor->ToString(expectedResponse));
987 }
19cbfa8f 988 }
b64db02e 989 }
ae693aaa 990 }
8fa35473 991
b64db02e 992 return bReturn;
8fa35473 993}
83be0701 994
3e61b350 995bool CCECCommandHandler::ActivateSource(void)
83be0701 996{
c4287bcd
LOK
997 if (m_busDevice->IsActiveSource() &&
998 m_busDevice->GetStatus(false) == CEC_DEVICE_STATUS_HANDLED_BY_LIBCEC)
999 {
1000 m_busDevice->SetPowerStatus(CEC_POWER_STATUS_ON);
1001 m_busDevice->SetMenuState(CEC_MENU_STATE_ACTIVATED);
1002
1003 m_busDevice->TransmitImageViewOn();
1004 m_busDevice->TransmitActiveSource();
1005 m_busDevice->TransmitMenuState(CECDEVICE_TV);
1006 if ((m_busDevice->GetType() == CEC_DEVICE_TYPE_PLAYBACK_DEVICE ||
1007 m_busDevice->GetType() == CEC_DEVICE_TYPE_RECORDING_DEVICE) &&
1008 SendDeckStatusUpdateOnActiveSource())
1009 ((CCECPlaybackDevice *)m_busDevice)->TransmitDeckStatus(CECDEVICE_TV);
1010 m_bHandlerInited = true;
83be0701 1011 }
7dc58c9f 1012 return true;
83be0701 1013}