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