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