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