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