cec: fixed possible deadlock because of a negative timeout in CAdapterCommunication...
[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"
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),
446c0cfc 51 m_iUseCounter(0),
7b74fdfb 52 m_expectedResponse(CEC_OPCODE_NONE),
d79b67d8 53 m_bOPTSendDeckStatusUpdateOnActiveSource(false),
cf4931be 54 m_vendorId(CEC_VENDOR_UNKNOWN)
e9de9629 55{
8fa35473
LOK
56}
57
58CCECCommandHandler::~CCECCommandHandler(void)
59{
f00ff009
LOK
60 CLockObject lock(m_processor->m_transmitMutex);
61 CLockObject receiveLock(m_receiveMutex);
8fa35473 62 m_condition.Broadcast();
e9de9629
LOK
63}
64
65bool CCECCommandHandler::HandleCommand(const cec_command &command)
66{
3e61b350 67 bool bHandled(true);
e9de9629 68
b64db02e 69 MarkBusy();
5477a250 70 CLibCEC::AddLog(CEC_LOG_NOTICE, ">> %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 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 {
f00ff009 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 {
5477a250 424 CLibCEC::AddLog(CEC_LOG_DEBUG, ">> %i requests active source", (uint8_t) command.initiator);
8747dd4f 425
5f316715 426 vector<CCECBusDevice *> devices;
f00ff009 427 for (size_t iDevicePtr = 0; iDevicePtr < GetMyDevices(devices); iDevicePtr++)
5f316715 428 devices[iDevicePtr]->TransmitActiveSource();
c4098482 429
5f316715
LOK
430 return true;
431 }
432 return false;
e9de9629
LOK
433}
434
435bool CCECCommandHandler::HandleRoutingChange(const cec_command &command)
436{
437 if (command.parameters.size == 4)
438 {
439 uint16_t iOldAddress = ((uint16_t)command.parameters[0] << 8) | ((uint16_t)command.parameters[1]);
440 uint16_t iNewAddress = ((uint16_t)command.parameters[2] << 8) | ((uint16_t)command.parameters[3]);
e9de9629 441
0f23c85c
LOK
442 CCECBusDevice *device = GetDevice(command.initiator);
443 if (device)
9dc04b07 444 device->SetStreamPath(iNewAddress, iOldAddress);
e9de9629
LOK
445 }
446 return true;
447}
448
907bd60f
LOK
449bool CCECCommandHandler::HandleRoutingInformation(const cec_command &command)
450{
451 if (command.parameters.size == 2)
452 {
453 uint16_t iNewAddress = ((uint16_t)command.parameters[0] << 8) | ((uint16_t)command.parameters[1]);
37b0c572 454 m_processor->SetActiveSource(iNewAddress);
907bd60f 455 }
15d1a84c
LOK
456
457 return false;
907bd60f
LOK
458}
459
a3269a0a
LOK
460bool CCECCommandHandler::HandleSetMenuLanguage(const cec_command &command)
461{
462 if (command.parameters.size == 3)
463 {
464 CCECBusDevice *device = GetDevice(command.initiator);
465 if (device)
466 {
467 cec_menu_language language;
468 language.device = command.initiator;
56701628 469 for (uint8_t iPtr = 0; iPtr < 4; iPtr++)
a3269a0a
LOK
470 language.language[iPtr] = command.parameters[iPtr];
471 language.language[3] = 0;
472 device->SetMenuLanguage(language);
15d1a84c
LOK
473 return true;
474 }
475 }
476 return false;
477}
478
479bool CCECCommandHandler::HandleSetOSDName(const cec_command &command)
480{
481 if (command.parameters.size > 0)
482 {
483 CCECBusDevice *device = GetDevice(command.initiator);
484 if (device)
485 {
486 char buf[1024];
487 for (uint8_t iPtr = 0; iPtr < command.parameters.size; iPtr++)
488 buf[iPtr] = (char)command.parameters[iPtr];
489 buf[command.parameters.size] = 0;
490
491 CStdString strName(buf);
492 device->SetOSDName(strName);
6b72afcd 493
15d1a84c 494 return true;
a3269a0a
LOK
495 }
496 }
15d1a84c 497 return false;
a3269a0a
LOK
498}
499
e9de9629
LOK
500bool CCECCommandHandler::HandleSetStreamPath(const cec_command &command)
501{
5f316715 502 if (m_processor->IsStarted() && command.parameters.size >= 2)
e9de9629 503 {
b6c7bc94 504 uint16_t iStreamAddress = ((uint16_t)command.parameters[0] << 8) | ((uint16_t)command.parameters[1]);
5477a250 505 CLibCEC::AddLog(CEC_LOG_DEBUG, ">> %i sets stream path to physical address %04x", command.initiator, iStreamAddress);
72689600
LOK
506
507 /* one of the device handled by libCEC has been made active */
508 CCECBusDevice *device = GetDeviceByPhysicalAddress(iStreamAddress);
509 if (device && m_busDevice->MyLogicalAddressContains(device->GetLogicalAddress()))
510 {
511 device->SetActiveSource();
512 device->TransmitActiveSource();
513 }
e9de9629 514 }
15d1a84c 515 return false;
e9de9629
LOK
516}
517
aa517a0d 518bool CCECCommandHandler::HandleSystemAudioModeRequest(const cec_command &command)
e5e86c76 519{
5f316715 520 if (m_processor->IsStarted() && m_busDevice->MyLogicalAddressContains(command.destination))
e5e86c76
LOK
521 {
522 CCECBusDevice *device = GetDevice(command.destination);
aa517a0d
LOK
523 if (device && device->GetType() == CEC_DEVICE_TYPE_AUDIO_SYSTEM)
524 {
525 if (command.parameters.size >= 2)
526 {
527 device->SetPowerStatus(CEC_POWER_STATUS_ON);
528 ((CCECAudioSystem *) device)->SetSystemAudioModeStatus(CEC_SYSTEM_AUDIO_STATUS_ON);
529 uint16_t iNewAddress = ((uint16_t)command.parameters[0] << 8) | ((uint16_t)command.parameters[1]);
530 CCECBusDevice *newActiveDevice = GetDeviceByPhysicalAddress(iNewAddress);
531 if (newActiveDevice)
37b0c572 532 newActiveDevice->SetActiveSource();
aa517a0d
LOK
533 return ((CCECAudioSystem *) device)->TransmitSetSystemAudioMode(command.initiator);
534 }
535 else
536 {
537 ((CCECAudioSystem *) device)->SetSystemAudioModeStatus(CEC_SYSTEM_AUDIO_STATUS_OFF);
538 return ((CCECAudioSystem *) device)->TransmitSetSystemAudioMode(command.initiator);
539 }
540 }
e5e86c76 541 }
6b72afcd 542 return false;
e5e86c76
LOK
543}
544
4d6b4433
LOK
545bool CCECCommandHandler::HandleStandby(const cec_command &command)
546{
547 CCECBusDevice *device = GetDevice(command.initiator);
548 if (device)
549 device->SetPowerStatus(CEC_POWER_STATUS_STANDBY);
6b72afcd 550
4d6b4433
LOK
551 return true;
552}
553
aa517a0d 554bool CCECCommandHandler::HandleSystemAudioModeStatus(const cec_command &command)
868dc71f 555{
aa517a0d 556 if (command.parameters.size == 1)
868dc71f 557 {
aa517a0d
LOK
558 CCECBusDevice *device = GetDevice(command.initiator);
559 if (device && device->GetType() == CEC_DEVICE_TYPE_AUDIO_SYSTEM)
560 {
561 ((CCECAudioSystem *)device)->SetSystemAudioModeStatus((cec_system_audio_status)command.parameters[0]);
562 return true;
563 }
564 }
565
566 return false;
567}
568
569bool CCECCommandHandler::HandleSetSystemAudioMode(const cec_command &command)
570{
571 if (command.parameters.size == 1)
572 {
573 CCECBusDevice *device = GetDevice(command.initiator);
574 if (device && device->GetType() == CEC_DEVICE_TYPE_AUDIO_SYSTEM)
575 {
576 ((CCECAudioSystem *)device)->SetSystemAudioModeStatus((cec_system_audio_status)command.parameters[0]);
577 return true;
578 }
868dc71f
LOK
579 }
580
581 return false;
582}
583
1a6669b8 584bool CCECCommandHandler::HandleTextViewOn(const cec_command &command)
cf0ecd85 585{
37b0c572 586 m_processor->m_busDevices[command.initiator]->SetActiveSource();
1a6669b8 587 return true;
cf0ecd85
LOK
588}
589
e9de9629
LOK
590bool CCECCommandHandler::HandleUserControlPressed(const cec_command &command)
591{
5f316715 592 if (m_processor->IsStarted() && m_busDevice->MyLogicalAddressContains(command.destination) && command.parameters.size > 0)
e9de9629 593 {
fcf10e27 594 m_processor->AddKey();
e9de9629
LOK
595
596 if (command.parameters[0] <= CEC_USER_CONTROL_CODE_MAX)
597 {
5477a250 598 CLibCEC::AddLog(CEC_LOG_DEBUG, "key pressed: %x", command.parameters[0]);
e9de9629 599
68391d4f
LOK
600 if (command.parameters[0] == CEC_USER_CONTROL_CODE_POWER ||
601 command.parameters[0] == CEC_USER_CONTROL_CODE_POWER_ON_FUNCTION)
602 {
603 CCECBusDevice *device = GetDevice(command.destination);
604 if (device)
3fd2a2b8 605 {
68391d4f 606 device->SetPowerStatus(CEC_POWER_STATUS_ON);
3fd2a2b8
LOK
607 if (device->MyLogicalAddressContains(device->GetLogicalAddress()))
608 {
609 device->SetActiveSource();
610 device->TransmitActiveSource();
611
612 if (device->GetType() == CEC_DEVICE_TYPE_PLAYBACK_DEVICE ||
613 device->GetType() == CEC_DEVICE_TYPE_RECORDING_DEVICE)
614 ((CCECPlaybackDevice *)device)->TransmitDeckStatus(command.initiator);
615 }
616 }
68391d4f 617 }
d1ab114f
LOK
618 else
619 {
620 m_processor->SetCurrentButton((cec_user_control_code) command.parameters[0]);
621 }
15d1a84c 622 return true;
e9de9629
LOK
623 }
624 }
15d1a84c 625 return false;
e9de9629
LOK
626}
627
628bool CCECCommandHandler::HandleUserControlRelease(const cec_command &command)
629{
5f316715 630 if (m_processor->IsStarted() && m_busDevice->MyLogicalAddressContains(command.destination))
fcf10e27 631 m_processor->AddKey();
6b72afcd 632
e9de9629
LOK
633 return true;
634}
635
1de6617c
LOK
636bool CCECCommandHandler::HandleVendorCommand(const cec_command & UNUSED(command))
637{
638 return true;
639}
640
e9de9629
LOK
641void CCECCommandHandler::UnhandledCommand(const cec_command &command)
642{
5477a250 643 CLibCEC::AddLog(CEC_LOG_DEBUG, "unhandled command with opcode %02x from address %d", command.opcode, command.initiator);
0f23c85c
LOK
644}
645
6f14b512 646size_t CCECCommandHandler::GetMyDevices(vector<CCECBusDevice *> &devices) const
8747dd4f 647{
6f14b512 648 size_t iReturn(0);
8747dd4f 649
fcf10e27 650 cec_logical_addresses addresses = m_processor->GetLogicalAddresses();
f2198ab5 651 for (uint8_t iPtr = 0; iPtr < 16; iPtr++)
8747dd4f
LOK
652 {
653 if (addresses[iPtr])
654 {
655 devices.push_back(GetDevice((cec_logical_address) iPtr));
656 ++iReturn;
657 }
658 }
659
660 return iReturn;
661}
662
0f23c85c
LOK
663CCECBusDevice *CCECCommandHandler::GetDevice(cec_logical_address iLogicalAddress) const
664{
665 CCECBusDevice *device = NULL;
666
667 if (iLogicalAddress >= CECDEVICE_TV && iLogicalAddress <= CECDEVICE_BROADCAST)
fcf10e27 668 device = m_processor->m_busDevices[iLogicalAddress];
0f23c85c
LOK
669
670 return device;
e9de9629 671}
6685ae07
LOK
672
673CCECBusDevice *CCECCommandHandler::GetDeviceByPhysicalAddress(uint16_t iPhysicalAddress) const
674{
fcf10e27 675 return m_processor->GetDeviceByPhysicalAddress(iPhysicalAddress);
6685ae07 676}
181b3475 677
c4098482
LOK
678CCECBusDevice *CCECCommandHandler::GetDeviceByType(cec_device_type type) const
679{
fcf10e27 680 return m_processor->GetDeviceByType(type);
c4098482
LOK
681}
682
8fa35473 683bool CCECCommandHandler::SetVendorId(const cec_command &command)
181b3475 684{
8fa35473 685 bool bChanged(false);
181b3475
LOK
686 if (command.parameters.size < 3)
687 {
5477a250 688 CLibCEC::AddLog(CEC_LOG_WARNING, "invalid vendor ID received");
8fa35473 689 return bChanged;
181b3475
LOK
690 }
691
bae71306
LOK
692 uint64_t iVendorId = ((uint64_t)command.parameters[0] << 16) +
693 ((uint64_t)command.parameters[1] << 8) +
181b3475
LOK
694 (uint64_t)command.parameters[2];
695
696 CCECBusDevice *device = GetDevice((cec_logical_address) command.initiator);
697 if (device)
8fa35473
LOK
698 bChanged = device->SetVendorId(iVendorId);
699 return bChanged;
181b3475 700}
5e822b09 701
16b1e052
LOK
702void CCECCommandHandler::SetPhysicalAddress(cec_logical_address iAddress, uint16_t iNewAddress)
703{
704 if (!m_busDevice->MyLogicalAddressContains(iAddress))
705 {
fcf10e27 706 bool bOurAddress(m_processor->GetPhysicalAddress() == iNewAddress);
16b1e052
LOK
707 GetDevice(iAddress)->SetPhysicalAddress(iNewAddress);
708 if (bOurAddress)
709 {
710 /* another device reported the same physical address as ours
711 * since we don't have physical address detection yet, we'll just use the
712 * given address, increased by 0x100 for now */
fcf10e27 713 m_processor->SetPhysicalAddress(iNewAddress + 0x100);
16b1e052
LOK
714 }
715 }
716}
855a3a98
LOK
717
718void CCECCommandHandler::HandlePoll(const cec_logical_address iInitiator, const cec_logical_address iDestination)
719{
5477a250 720 CLibCEC::AddLog(CEC_LOG_DEBUG, "<< POLL: %s (%x) -> %s (%x)", m_processor->ToString(iInitiator), iInitiator, m_processor->ToString(iDestination), iDestination);
855a3a98
LOK
721}
722
723bool CCECCommandHandler::HandleReceiveFailed(void)
724{
725 /* default = error */
726 return true;
727}
8fa35473 728
b64db02e 729bool CCECCommandHandler::TransmitImageViewOn(const cec_logical_address iInitiator, const cec_logical_address iDestination)
8fa35473
LOK
730{
731 cec_command command;
ae693aaa 732 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_IMAGE_VIEW_ON);
8fa35473 733
19cbfa8f 734 return Transmit(command, false);
8fa35473
LOK
735}
736
737bool CCECCommandHandler::TransmitStandby(const cec_logical_address iInitiator, const cec_logical_address iDestination)
738{
739 cec_command command;
ae693aaa 740 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_STANDBY);
8fa35473 741
19cbfa8f 742 return Transmit(command, false);
8fa35473
LOK
743}
744
745bool CCECCommandHandler::TransmitRequestCecVersion(const cec_logical_address iInitiator, const cec_logical_address iDestination)
746{
747 cec_command command;
ae693aaa 748 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_GET_CEC_VERSION);
8fa35473 749
5e526919 750 return Transmit(command, true, CEC_OPCODE_CEC_VERSION);
8fa35473
LOK
751}
752
753bool CCECCommandHandler::TransmitRequestMenuLanguage(const cec_logical_address iInitiator, const cec_logical_address iDestination)
754{
755 cec_command command;
ae693aaa 756 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_GET_MENU_LANGUAGE);
8fa35473 757
5e526919 758 return Transmit(command, true, CEC_OPCODE_SET_MENU_LANGUAGE);
8fa35473
LOK
759}
760
761bool CCECCommandHandler::TransmitRequestOSDName(const cec_logical_address iInitiator, const cec_logical_address iDestination)
762{
763 cec_command command;
ae693aaa 764 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_GIVE_OSD_NAME);
8fa35473 765
5e526919 766 return Transmit(command, true, CEC_OPCODE_SET_OSD_NAME);
8fa35473
LOK
767}
768
769bool CCECCommandHandler::TransmitRequestPhysicalAddress(const cec_logical_address iInitiator, const cec_logical_address iDestination)
770{
771 cec_command command;
ae693aaa 772 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_GIVE_PHYSICAL_ADDRESS);
8fa35473 773
5e526919 774 return Transmit(command, true, CEC_OPCODE_REPORT_PHYSICAL_ADDRESS);
8fa35473
LOK
775}
776
777bool CCECCommandHandler::TransmitRequestPowerStatus(const cec_logical_address iInitiator, const cec_logical_address iDestination)
778{
779 cec_command command;
ae693aaa 780 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_GIVE_DEVICE_POWER_STATUS);
8fa35473 781
5e526919 782 return Transmit(command, true, CEC_OPCODE_REPORT_POWER_STATUS);
8fa35473
LOK
783}
784
785bool CCECCommandHandler::TransmitRequestVendorId(const cec_logical_address iInitiator, const cec_logical_address iDestination)
786{
787 cec_command command;
ae693aaa 788 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_GIVE_DEVICE_VENDOR_ID);
8fa35473 789
5e526919 790 return Transmit(command, true, CEC_OPCODE_DEVICE_VENDOR_ID);
8fa35473
LOK
791}
792
793bool CCECCommandHandler::TransmitActiveSource(const cec_logical_address iInitiator, uint16_t iPhysicalAddress)
794{
795 cec_command command;
ae693aaa 796 cec_command::Format(command, iInitiator, CECDEVICE_BROADCAST, CEC_OPCODE_ACTIVE_SOURCE);
8fa35473
LOK
797 command.parameters.PushBack((uint8_t) ((iPhysicalAddress >> 8) & 0xFF));
798 command.parameters.PushBack((uint8_t) (iPhysicalAddress & 0xFF));
799
19cbfa8f 800 return Transmit(command, false);
8fa35473
LOK
801}
802
803bool CCECCommandHandler::TransmitCECVersion(const cec_logical_address iInitiator, const cec_logical_address iDestination, cec_version cecVersion)
804{
805 cec_command command;
ae693aaa 806 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_CEC_VERSION);
8fa35473
LOK
807 command.parameters.PushBack((uint8_t)cecVersion);
808
19cbfa8f 809 return Transmit(command, false);
8fa35473
LOK
810}
811
812bool CCECCommandHandler::TransmitInactiveSource(const cec_logical_address iInitiator, uint16_t iPhysicalAddress)
813{
814 cec_command command;
ae693aaa 815 cec_command::Format(command, iInitiator, CECDEVICE_TV, CEC_OPCODE_INACTIVE_SOURCE);
8fa35473
LOK
816 command.parameters.PushBack((iPhysicalAddress >> 8) & 0xFF);
817 command.parameters.PushBack(iPhysicalAddress & 0xFF);
818
19cbfa8f 819 return Transmit(command, false);
8fa35473
LOK
820}
821
822bool CCECCommandHandler::TransmitMenuState(const cec_logical_address iInitiator, const cec_logical_address iDestination, cec_menu_state menuState)
823{
824 cec_command command;
ae693aaa 825 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_MENU_STATUS);
8fa35473
LOK
826 command.parameters.PushBack((uint8_t)menuState);
827
19cbfa8f 828 return Transmit(command, false);
8fa35473
LOK
829}
830
831bool CCECCommandHandler::TransmitOSDName(const cec_logical_address iInitiator, const cec_logical_address iDestination, CStdString strDeviceName)
832{
833 cec_command command;
ae693aaa 834 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_SET_OSD_NAME);
6f14b512 835 for (size_t iPtr = 0; iPtr < strDeviceName.length(); iPtr++)
8fa35473
LOK
836 command.parameters.PushBack(strDeviceName.at(iPtr));
837
19cbfa8f 838 return Transmit(command, false);
8fa35473
LOK
839}
840
841bool CCECCommandHandler::TransmitOSDString(const cec_logical_address iInitiator, const cec_logical_address iDestination, cec_display_control duration, const char *strMessage)
842{
843 cec_command command;
ae693aaa 844 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_SET_OSD_STRING);
8fa35473
LOK
845 command.parameters.PushBack((uint8_t)duration);
846
6f14b512 847 size_t iLen = strlen(strMessage);
8fa35473
LOK
848 if (iLen > 13) iLen = 13;
849
6f14b512 850 for (size_t iPtr = 0; iPtr < iLen; iPtr++)
8fa35473
LOK
851 command.parameters.PushBack(strMessage[iPtr]);
852
19cbfa8f 853 return Transmit(command, false);
8fa35473
LOK
854}
855
856bool CCECCommandHandler::TransmitPhysicalAddress(const cec_logical_address iInitiator, uint16_t iPhysicalAddress, cec_device_type type)
857{
858 cec_command command;
ae693aaa 859 cec_command::Format(command, iInitiator, CECDEVICE_BROADCAST, CEC_OPCODE_REPORT_PHYSICAL_ADDRESS);
8fa35473
LOK
860 command.parameters.PushBack((uint8_t) ((iPhysicalAddress >> 8) & 0xFF));
861 command.parameters.PushBack((uint8_t) (iPhysicalAddress & 0xFF));
862 command.parameters.PushBack((uint8_t) (type));
863
19cbfa8f 864 return Transmit(command, false);
8fa35473
LOK
865}
866
867bool CCECCommandHandler::TransmitPoll(const cec_logical_address iInitiator, const cec_logical_address iDestination)
868{
869 cec_command command;
ae693aaa 870 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_NONE);
8fa35473 871
ae693aaa 872 return Transmit(command, false);
8fa35473
LOK
873}
874
875bool CCECCommandHandler::TransmitPowerState(const cec_logical_address iInitiator, const cec_logical_address iDestination, cec_power_status state)
876{
877 cec_command command;
ae693aaa 878 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_REPORT_POWER_STATUS);
8fa35473
LOK
879 command.parameters.PushBack((uint8_t) state);
880
19cbfa8f 881 return Transmit(command, false);
8fa35473
LOK
882}
883
884bool CCECCommandHandler::TransmitVendorID(const cec_logical_address iInitiator, uint64_t iVendorId)
885{
886 cec_command command;
ae693aaa 887 cec_command::Format(command, iInitiator, CECDEVICE_BROADCAST, CEC_OPCODE_DEVICE_VENDOR_ID);
8fa35473
LOK
888
889 command.parameters.PushBack((uint8_t) (((uint64_t)iVendorId >> 16) & 0xFF));
890 command.parameters.PushBack((uint8_t) (((uint64_t)iVendorId >> 8) & 0xFF));
891 command.parameters.PushBack((uint8_t) ((uint64_t)iVendorId & 0xFF));
892
19cbfa8f 893 return Transmit(command, false);
8fa35473
LOK
894}
895
896bool CCECCommandHandler::TransmitAudioStatus(const cec_logical_address iInitiator, const cec_logical_address iDestination, uint8_t state)
897{
898 cec_command command;
ae693aaa 899 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_REPORT_AUDIO_STATUS);
8fa35473
LOK
900 command.parameters.PushBack(state);
901
19cbfa8f 902 return Transmit(command, false);
8fa35473
LOK
903}
904
905bool CCECCommandHandler::TransmitSetSystemAudioMode(const cec_logical_address iInitiator, const cec_logical_address iDestination, cec_system_audio_status state)
906{
907 cec_command command;
ae693aaa 908 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_SET_SYSTEM_AUDIO_MODE);
8fa35473
LOK
909 command.parameters.PushBack((uint8_t)state);
910
19cbfa8f 911 return Transmit(command, false);
8fa35473
LOK
912}
913
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);
3e61b350 952 MarkBusy();
ae693aaa 953 command.transmit_timeout = m_iTransmitTimeout;
ae693aaa 954
ae693aaa 955 {
861832f6 956 uint8_t iTries(0), iMaxTries(command.opcode == CEC_OPCODE_NONE ? 1 : m_iTransmitRetries + 1);
f00ff009
LOK
957 CLockObject writeLock(m_processor->m_transmitMutex);
958 CLockObject receiveLock(m_receiveMutex);
b64db02e 959 ++m_iUseCounter;
19cbfa8f 960 while (!bReturn && ++iTries <= iMaxTries)
b64db02e 961 {
446c0cfc 962 m_expectedResponse = expectedResponse;
19cbfa8f
LOK
963 if (m_processor->Transmit(command))
964 {
5477a250 965 CLibCEC::AddLog(CEC_LOG_DEBUG, "command transmitted");
19cbfa8f 966 bReturn = bExpectResponse ?
f00ff009 967 m_condition.Wait(m_receiveMutex, m_iTransmitWait) :
19cbfa8f
LOK
968 true;
969 }
b64db02e
LOK
970 }
971 --m_iUseCounter;
ae693aaa 972 }
8fa35473 973
3e61b350 974 MarkReady();
b64db02e 975 return bReturn;
8fa35473 976}
83be0701 977
3e61b350 978bool CCECCommandHandler::ActivateSource(void)
83be0701
LOK
979{
980 if (m_busDevice->GetLogicalAddress() == CECDEVICE_TV)
981 {
842262d8 982 CCECBusDevice *primary = m_processor->GetPrimaryDevice();
6b1eea98
LOK
983 primary->SetPowerStatus(CEC_POWER_STATUS_ON);
984 primary->SetMenuState(CEC_MENU_STATE_ACTIVATED);
985
89a726fa
LOK
986 if (m_processor->GetPrimaryDevice()->GetPhysicalAddress(false) != 0xffff)
987 {
988 m_processor->SetActiveSource();
989 primary->TransmitMenuState(m_busDevice->GetLogicalAddress());
990 m_bHandlerInited = true;
991 }
83be0701 992 }
7dc58c9f 993 return true;
83be0701 994}
b64db02e
LOK
995
996void CCECCommandHandler::MarkBusy(void)
997{
f00ff009 998 CLockObject receiveLock(m_receiveMutex);
b64db02e
LOK
999 ++m_iUseCounter;
1000}
1001
1002bool CCECCommandHandler::MarkReady(void)
1003{
f00ff009 1004 CLockObject receiveLock(m_receiveMutex);
b64db02e
LOK
1005 return m_iUseCounter > 0 ? (--m_iUseCounter == 0) : true;
1006}
1007
1008bool CCECCommandHandler::InUse(void)
1009{
f00ff009 1010 CLockObject receiveLock(m_receiveMutex);
b64db02e
LOK
1011 return m_iUseCounter > 0;
1012}