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