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