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