cec: handle bus scan from LG TVs
[deb_libcec.git] / src / lib / implementations / CECCommandHandler.cpp
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"
34 #include "../devices/CECBusDevice.h"
35 #include "../devices/CECAudioSystem.h"
36 #include "../devices/CECPlaybackDevice.h"
37 #include "../CECProcessor.h"
38
39 using namespace CEC;
40 using namespace std;
41
42 CCECCommandHandler::CCECCommandHandler(CCECBusDevice *busDevice)
43 {
44 m_busDevice = busDevice;
45 m_processor = m_busDevice->GetProcessor();
46 }
47
48 bool CCECCommandHandler::HandleCommand(const cec_command &command)
49 {
50 bool bHandled(true);
51
52 CStdString strLog;
53 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);
54 m_busDevice->AddLog(CEC_LOG_NOTICE, strLog);
55
56 m_processor->AddCommand(command);
57
58 switch(command.opcode)
59 {
60 case CEC_OPCODE_REPORT_POWER_STATUS:
61 HandleReportPowerStatus(command);
62 break;
63 case CEC_OPCODE_CEC_VERSION:
64 HandleDeviceCecVersion(command);
65 break;
66 case CEC_OPCODE_SET_MENU_LANGUAGE:
67 HandleSetMenuLanguage(command);
68 break;
69 case CEC_OPCODE_GIVE_PHYSICAL_ADDRESS:
70 HandleGivePhysicalAddress(command);
71 break;
72 case CEC_OPCODE_GIVE_OSD_NAME:
73 HandleGiveOSDName(command);
74 break;
75 case CEC_OPCODE_GIVE_DEVICE_VENDOR_ID:
76 HandleGiveDeviceVendorId(command);
77 break;
78 case CEC_OPCODE_DEVICE_VENDOR_ID:
79 HandleDeviceVendorId(command);
80 break;
81 case CEC_OPCODE_VENDOR_COMMAND_WITH_ID:
82 HandleDeviceVendorCommandWithId(command);
83 break;
84 case CEC_OPCODE_GIVE_DECK_STATUS:
85 HandleGiveDeckStatus(command);
86 break;
87 case CEC_OPCODE_DECK_CONTROL:
88 HandleDeckControl(command);
89 break;
90 case CEC_OPCODE_MENU_REQUEST:
91 HandleMenuRequest(command);
92 break;
93 case CEC_OPCODE_GIVE_DEVICE_POWER_STATUS:
94 HandleGiveDevicePowerStatus(command);
95 break;
96 case CEC_OPCODE_GET_CEC_VERSION:
97 HandleGetCecVersion(command);
98 break;
99 case CEC_OPCODE_USER_CONTROL_PRESSED:
100 HandleUserControlPressed(command);
101 break;
102 case CEC_OPCODE_USER_CONTROL_RELEASE:
103 HandleUserControlRelease(command);
104 break;
105 case CEC_OPCODE_GIVE_AUDIO_STATUS:
106 HandleGiveAudioStatus(command);
107 break;
108 case CEC_OPCODE_GIVE_SYSTEM_AUDIO_MODE_STATUS:
109 HandleGiveSystemAudioModeStatus(command);
110 break;
111 case CEC_OPCODE_SYSTEM_AUDIO_MODE_REQUEST:
112 HandleSystemAudioModeRequest(command);
113 break;
114 case CEC_OPCODE_REPORT_AUDIO_STATUS:
115 HandleReportAudioStatus(command);
116 break;
117 case CEC_OPCODE_SYSTEM_AUDIO_MODE_STATUS:
118 HandleSystemAudioModeStatus(command);
119 break;
120 case CEC_OPCODE_SET_SYSTEM_AUDIO_MODE:
121 HandleSetSystemAudioMode(command);
122 break;
123 case CEC_OPCODE_REQUEST_ACTIVE_SOURCE:
124 HandleRequestActiveSource(command);
125 break;
126 case CEC_OPCODE_SET_STREAM_PATH:
127 HandleSetStreamPath(command);
128 break;
129 case CEC_OPCODE_ROUTING_CHANGE:
130 HandleRoutingChange(command);
131 break;
132 case CEC_OPCODE_ROUTING_INFORMATION:
133 HandleRoutingInformation(command);
134 break;
135 case CEC_OPCODE_STANDBY:
136 HandleStandby(command);
137 break;
138 case CEC_OPCODE_ACTIVE_SOURCE:
139 HandleActiveSource(command);
140 break;
141 case CEC_OPCODE_REPORT_PHYSICAL_ADDRESS:
142 HandleReportPhysicalAddress(command);
143 break;
144 case CEC_OPCODE_SET_OSD_NAME:
145 HandleSetOSDName(command);
146 break;
147 case CEC_OPCODE_IMAGE_VIEW_ON:
148 HandleImageViewOn(command);
149 break;
150 case CEC_OPCODE_TEXT_VIEW_ON:
151 HandleTextViewOn(command);
152 break;
153 default:
154 UnhandledCommand(command);
155 bHandled = false;
156 break;
157 }
158
159 return bHandled;
160 }
161
162 bool CCECCommandHandler::HandleActiveSource(const cec_command &command)
163 {
164 if (command.parameters.size == 2)
165 {
166 uint16_t iAddress = ((uint16_t)command.parameters[0] << 8) | ((uint16_t)command.parameters[1]);
167 return m_processor->SetStreamPath(iAddress);
168 }
169
170 return true;
171 }
172
173 bool CCECCommandHandler::HandleDeckControl(const cec_command &command)
174 {
175 CCECBusDevice *device = GetDevice(command.destination);
176 if (device && (device->GetType() == CEC_DEVICE_TYPE_PLAYBACK_DEVICE || device->GetType() == CEC_DEVICE_TYPE_RECORDING_DEVICE) && command.parameters.size > 0)
177 {
178 ((CCECPlaybackDevice *) device)->SetDeckControlMode((cec_deck_control_mode) command.parameters[0]);
179 return true;
180 }
181
182 return false;
183 }
184
185 bool CCECCommandHandler::HandleDeviceCecVersion(const cec_command &command)
186 {
187 if (command.parameters.size == 1)
188 {
189 CCECBusDevice *device = GetDevice(command.initiator);
190 if (device)
191 device->SetCecVersion((cec_version) command.parameters[0]);
192 }
193
194 return true;
195 }
196
197 bool CCECCommandHandler::HandleDeviceVendorCommandWithId(const cec_command &command)
198 {
199 if (m_busDevice->MyLogicalAddressContains(command.destination))
200 m_processor->TransmitAbort(command.initiator, command.opcode, CEC_ABORT_REASON_REFUSED);
201
202 return true;
203 }
204
205 bool CCECCommandHandler::HandleDeviceVendorId(const cec_command &command)
206 {
207 SetVendorId(command);
208 return true;
209 }
210
211 bool CCECCommandHandler::HandleGetCecVersion(const cec_command &command)
212 {
213 if (m_busDevice->MyLogicalAddressContains(command.destination))
214 {
215 CCECBusDevice *device = GetDevice(command.destination);
216 if (device)
217 return device->TransmitCECVersion(command.initiator);
218 }
219
220 return false;
221 }
222
223 bool CCECCommandHandler::HandleGiveAudioStatus(const cec_command &command)
224 {
225 if (m_busDevice->MyLogicalAddressContains(command.destination))
226 {
227 CCECBusDevice *device = GetDevice(command.destination);
228 if (device && device->GetType() == CEC_DEVICE_TYPE_AUDIO_SYSTEM)
229 return ((CCECAudioSystem *) device)->TransmitAudioStatus(command.initiator);
230 }
231
232 return false;
233 }
234
235 bool CCECCommandHandler::HandleGiveDeckStatus(const cec_command &command)
236 {
237 if (m_busDevice->MyLogicalAddressContains(command.destination))
238 {
239 CCECBusDevice *device = GetDevice(command.destination);
240 if (device && (device->GetType() == CEC_DEVICE_TYPE_PLAYBACK_DEVICE || device->GetType() == CEC_DEVICE_TYPE_RECORDING_DEVICE))
241 return ((CCECPlaybackDevice *) device)->TransmitDeckStatus(command.initiator);
242 }
243
244 return false;
245 }
246
247 bool CCECCommandHandler::HandleGiveDevicePowerStatus(const cec_command &command)
248 {
249 if (m_busDevice->MyLogicalAddressContains(command.destination))
250 {
251 CCECBusDevice *device = GetDevice(command.destination);
252 if (device)
253 return device->TransmitPowerState(command.initiator);
254 }
255
256 return false;
257 }
258
259 bool CCECCommandHandler::HandleGiveDeviceVendorId(const cec_command &command)
260 {
261 if (m_busDevice->MyLogicalAddressContains(command.destination))
262 {
263 CCECBusDevice *device = GetDevice(command.destination);
264 if (device)
265 return device->TransmitVendorID(command.initiator);
266 }
267
268 return false;
269 }
270
271 bool CCECCommandHandler::HandleGiveOSDName(const cec_command &command)
272 {
273 if (m_busDevice->MyLogicalAddressContains(command.destination))
274 {
275 CCECBusDevice *device = GetDevice(command.destination);
276 if (device)
277 return device->TransmitOSDName(command.initiator);
278 }
279
280 return false;
281 }
282
283 bool CCECCommandHandler::HandleGivePhysicalAddress(const cec_command &command)
284 {
285 if (m_busDevice->MyLogicalAddressContains(command.destination))
286 {
287 CCECBusDevice *device = GetDevice(command.destination);
288 if (device)
289 return device->TransmitPhysicalAddress();
290 }
291
292 return false;
293 }
294
295 bool CCECCommandHandler::HandleGiveSystemAudioModeStatus(const cec_command &command)
296 {
297 if (m_busDevice->MyLogicalAddressContains(command.destination))
298 {
299 CCECBusDevice *device = GetDevice(command.destination);
300 if (device && device->GetType() == CEC_DEVICE_TYPE_AUDIO_SYSTEM)
301 return ((CCECAudioSystem *) device)->TransmitSystemAudioModeStatus(command.initiator);
302 }
303
304 return false;
305 }
306
307 bool CCECCommandHandler::HandleImageViewOn(const cec_command &command)
308 {
309 m_processor->SetActiveSource(command.initiator);
310 return true;
311 }
312
313 bool CCECCommandHandler::HandleMenuRequest(const cec_command &command)
314 {
315 if (m_busDevice->MyLogicalAddressContains(command.destination))
316 {
317 if (command.parameters[0] == CEC_MENU_REQUEST_TYPE_QUERY)
318 {
319 CCECBusDevice *device = GetDevice(command.destination);
320 if (device)
321 return device->TransmitMenuState(command.initiator);
322 }
323 }
324
325 return false;
326 }
327
328 bool CCECCommandHandler::HandleReportAudioStatus(const cec_command &command)
329 {
330 if (command.parameters.size == 1)
331 {
332 CCECBusDevice *device = GetDevice(command.initiator);
333 if (device && device->GetType() == CEC_DEVICE_TYPE_AUDIO_SYSTEM)
334 {
335 ((CCECAudioSystem *)device)->SetAudioStatus(command.parameters[0]);
336 return true;
337 }
338 }
339 return false;
340 }
341
342 bool CCECCommandHandler::HandleReportPhysicalAddress(const cec_command &command)
343 {
344 if (command.parameters.size == 3)
345 {
346 uint16_t iNewAddress = ((uint16_t)command.parameters[0] << 8) | ((uint16_t)command.parameters[1]);
347 SetPhysicalAddress(command.initiator, iNewAddress);
348 }
349 return true;
350 }
351
352 bool CCECCommandHandler::HandleReportPowerStatus(const cec_command &command)
353 {
354 if (command.parameters.size == 1)
355 {
356 CCECBusDevice *device = GetDevice(command.initiator);
357 if (device)
358 device->SetPowerStatus((cec_power_status) command.parameters[0]);
359 }
360 return true;
361 }
362
363 bool CCECCommandHandler::HandleRequestActiveSource(const cec_command &command)
364 {
365 CStdString strLog;
366 strLog.Format(">> %i requests active source", (uint8_t) command.initiator);
367 m_busDevice->AddLog(CEC_LOG_DEBUG, strLog.c_str());
368
369 vector<CCECBusDevice *> devices;
370 for (int iDevicePtr = (int)GetMyDevices(devices)-1; iDevicePtr >=0; iDevicePtr--)
371 devices[iDevicePtr]->TransmitActiveSource();
372
373 return true;
374 }
375
376 bool CCECCommandHandler::HandleRoutingChange(const cec_command &command)
377 {
378 if (command.parameters.size == 4)
379 {
380 uint16_t iOldAddress = ((uint16_t)command.parameters[0] << 8) | ((uint16_t)command.parameters[1]);
381 uint16_t iNewAddress = ((uint16_t)command.parameters[2] << 8) | ((uint16_t)command.parameters[3]);
382
383 CCECBusDevice *device = GetDevice(command.initiator);
384 if (device)
385 device->SetStreamPath(iNewAddress, iOldAddress);
386 }
387 return true;
388 }
389
390 bool CCECCommandHandler::HandleRoutingInformation(const cec_command &command)
391 {
392 if (command.parameters.size == 2)
393 {
394 uint16_t iNewAddress = ((uint16_t)command.parameters[0] << 8) | ((uint16_t)command.parameters[1]);
395 m_processor->SetStreamPath(iNewAddress);
396 }
397
398 return false;
399 }
400
401 bool CCECCommandHandler::HandleSetMenuLanguage(const cec_command &command)
402 {
403 if (command.parameters.size == 3)
404 {
405 CCECBusDevice *device = GetDevice(command.initiator);
406 if (device)
407 {
408 cec_menu_language language;
409 language.device = command.initiator;
410 for (uint8_t iPtr = 0; iPtr < 4; iPtr++)
411 language.language[iPtr] = command.parameters[iPtr];
412 language.language[3] = 0;
413 device->SetMenuLanguage(language);
414 return true;
415 }
416 }
417 return false;
418 }
419
420 bool CCECCommandHandler::HandleSetOSDName(const cec_command &command)
421 {
422 if (command.parameters.size > 0)
423 {
424 CCECBusDevice *device = GetDevice(command.initiator);
425 if (device)
426 {
427 char buf[1024];
428 for (uint8_t iPtr = 0; iPtr < command.parameters.size; iPtr++)
429 buf[iPtr] = (char)command.parameters[iPtr];
430 buf[command.parameters.size] = 0;
431
432 CStdString strName(buf);
433 device->SetOSDName(strName);
434
435 return true;
436 }
437 }
438 return false;
439 }
440
441 bool CCECCommandHandler::HandleSetStreamPath(const cec_command &command)
442 {
443 if (command.parameters.size >= 2)
444 {
445 uint16_t iStreamAddress = ((uint16_t)command.parameters[0] << 8) | ((uint16_t)command.parameters[1]);
446 CStdString strLog;
447 strLog.Format(">> %i sets stream path to physical address %04x", command.initiator, iStreamAddress);
448 m_busDevice->AddLog(CEC_LOG_DEBUG, strLog.c_str());
449
450 if (m_processor->SetStreamPath(iStreamAddress))
451 {
452 CCECBusDevice *device = GetDeviceByPhysicalAddress(iStreamAddress);
453 if (device)
454 {
455 return device->TransmitActiveSource() &&
456 device->TransmitMenuState(command.initiator);
457 }
458 }
459 }
460 return false;
461 }
462
463 bool CCECCommandHandler::HandleSystemAudioModeRequest(const cec_command &command)
464 {
465 if (m_busDevice->MyLogicalAddressContains(command.destination))
466 {
467 CCECBusDevice *device = GetDevice(command.destination);
468 if (device && device->GetType() == CEC_DEVICE_TYPE_AUDIO_SYSTEM)
469 {
470 if (command.parameters.size >= 2)
471 {
472 device->SetPowerStatus(CEC_POWER_STATUS_ON);
473 ((CCECAudioSystem *) device)->SetSystemAudioModeStatus(CEC_SYSTEM_AUDIO_STATUS_ON);
474 uint16_t iNewAddress = ((uint16_t)command.parameters[0] << 8) | ((uint16_t)command.parameters[1]);
475 CCECBusDevice *newActiveDevice = GetDeviceByPhysicalAddress(iNewAddress);
476 if (newActiveDevice)
477 m_processor->SetActiveSource(newActiveDevice->GetLogicalAddress());
478 return ((CCECAudioSystem *) device)->TransmitSetSystemAudioMode(command.initiator);
479 }
480 else
481 {
482 ((CCECAudioSystem *) device)->SetSystemAudioModeStatus(CEC_SYSTEM_AUDIO_STATUS_OFF);
483 return ((CCECAudioSystem *) device)->TransmitSetSystemAudioMode(command.initiator);
484 }
485 }
486 }
487 return false;
488 }
489
490 bool CCECCommandHandler::HandleStandby(const cec_command &command)
491 {
492 CCECBusDevice *device = GetDevice(command.initiator);
493 if (device)
494 device->SetPowerStatus(CEC_POWER_STATUS_STANDBY);
495
496 return true;
497 }
498
499 bool CCECCommandHandler::HandleSystemAudioModeStatus(const cec_command &command)
500 {
501 if (command.parameters.size == 1)
502 {
503 CCECBusDevice *device = GetDevice(command.initiator);
504 if (device && device->GetType() == CEC_DEVICE_TYPE_AUDIO_SYSTEM)
505 {
506 ((CCECAudioSystem *)device)->SetSystemAudioModeStatus((cec_system_audio_status)command.parameters[0]);
507 return true;
508 }
509 }
510
511 return false;
512 }
513
514 bool CCECCommandHandler::HandleSetSystemAudioMode(const cec_command &command)
515 {
516 if (command.parameters.size == 1)
517 {
518 CCECBusDevice *device = GetDevice(command.initiator);
519 if (device && device->GetType() == CEC_DEVICE_TYPE_AUDIO_SYSTEM)
520 {
521 ((CCECAudioSystem *)device)->SetSystemAudioModeStatus((cec_system_audio_status)command.parameters[0]);
522 return true;
523 }
524 }
525
526 return false;
527 }
528
529 bool CCECCommandHandler::HandleTextViewOn(const cec_command &command)
530 {
531 m_processor->SetActiveSource(command.initiator);
532 return true;
533 }
534
535 bool CCECCommandHandler::HandleUserControlPressed(const cec_command &command)
536 {
537 if (m_busDevice->MyLogicalAddressContains(command.destination) && command.parameters.size > 0)
538 {
539 m_processor->AddKey();
540
541 if (command.parameters[0] <= CEC_USER_CONTROL_CODE_MAX)
542 {
543 CStdString strLog;
544 strLog.Format("key pressed: %x", command.parameters[0]);
545 m_busDevice->AddLog(CEC_LOG_DEBUG, strLog.c_str());
546
547 if (command.parameters[0] == CEC_USER_CONTROL_CODE_POWER ||
548 command.parameters[0] == CEC_USER_CONTROL_CODE_POWER_ON_FUNCTION)
549 {
550 CCECBusDevice *device = GetDevice(command.destination);
551 if (device)
552 device->SetPowerStatus(CEC_POWER_STATUS_ON);
553 }
554
555 m_processor->SetCurrentButton((cec_user_control_code) command.parameters[0]);
556 return true;
557 }
558 }
559 return false;
560 }
561
562 bool CCECCommandHandler::HandleUserControlRelease(const cec_command &command)
563 {
564 if (m_busDevice->MyLogicalAddressContains(command.destination))
565 m_processor->AddKey();
566
567 return true;
568 }
569
570 void CCECCommandHandler::UnhandledCommand(const cec_command &command)
571 {
572 CStdString strLog;
573 strLog.Format("unhandled command with opcode %02x from address %d", command.opcode, command.initiator);
574 m_busDevice->AddLog(CEC_LOG_DEBUG, strLog);
575 }
576
577 unsigned int CCECCommandHandler::GetMyDevices(vector<CCECBusDevice *> &devices) const
578 {
579 unsigned int iReturn(0);
580
581 cec_logical_addresses addresses = m_processor->GetLogicalAddresses();
582 for (uint8_t iPtr = 0; iPtr < 16; iPtr++)
583 {
584 if (addresses[iPtr])
585 {
586 devices.push_back(GetDevice((cec_logical_address) iPtr));
587 ++iReturn;
588 }
589 }
590
591 return iReturn;
592 }
593
594 CCECBusDevice *CCECCommandHandler::GetDevice(cec_logical_address iLogicalAddress) const
595 {
596 CCECBusDevice *device = NULL;
597
598 if (iLogicalAddress >= CECDEVICE_TV && iLogicalAddress <= CECDEVICE_BROADCAST)
599 device = m_processor->m_busDevices[iLogicalAddress];
600
601 return device;
602 }
603
604 CCECBusDevice *CCECCommandHandler::GetDeviceByPhysicalAddress(uint16_t iPhysicalAddress) const
605 {
606 return m_processor->GetDeviceByPhysicalAddress(iPhysicalAddress);
607 }
608
609 CCECBusDevice *CCECCommandHandler::GetDeviceByType(cec_device_type type) const
610 {
611 return m_processor->GetDeviceByType(type);
612 }
613
614 void CCECCommandHandler::SetVendorId(const cec_command &command)
615 {
616 if (command.parameters.size < 3)
617 {
618 m_busDevice->AddLog(CEC_LOG_WARNING, "invalid vendor ID received");
619 return;
620 }
621
622 uint64_t iVendorId = ((uint64_t)command.parameters[0] << 16) +
623 ((uint64_t)command.parameters[1] << 8) +
624 (uint64_t)command.parameters[2];
625
626 CCECBusDevice *device = GetDevice((cec_logical_address) command.initiator);
627 if (device)
628 device->SetVendorId(iVendorId);
629 }
630
631 void CCECCommandHandler::SetPhysicalAddress(cec_logical_address iAddress, uint16_t iNewAddress)
632 {
633 if (!m_busDevice->MyLogicalAddressContains(iAddress))
634 {
635 bool bOurAddress(m_processor->GetPhysicalAddress() == iNewAddress);
636 GetDevice(iAddress)->SetPhysicalAddress(iNewAddress);
637 if (bOurAddress)
638 {
639 /* another device reported the same physical address as ours
640 * since we don't have physical address detection yet, we'll just use the
641 * given address, increased by 0x100 for now */
642 m_processor->SetPhysicalAddress(iNewAddress + 0x100);
643 }
644 }
645 }
646
647 void CCECCommandHandler::HandlePoll(const cec_logical_address iInitiator, const cec_logical_address iDestination)
648 {
649 CStdString strLog;
650 strLog.Format("<< POLL: %s (%x) -> %s (%x)", m_processor->ToString(iInitiator), iInitiator, m_processor->ToString(iDestination), iDestination);
651 m_processor->AddLog(CEC_LOG_DEBUG, strLog);
652 }
653
654 bool CCECCommandHandler::HandleReceiveFailed(void)
655 {
656 /* default = error */
657 return true;
658 }