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