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