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