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