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