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