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