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