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