cec: only send a power on command when a device isn't on already and set the power...
[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 "../CECProcessor.h"
37
38 using namespace CEC;
39 using namespace std;
40
41 CCECCommandHandler::CCECCommandHandler(CCECBusDevice *busDevice)
42 {
43 m_busDevice = busDevice;
44 }
45
46 bool CCECCommandHandler::HandleCommand(const cec_command &command)
47 {
48 bool bHandled(true);
49
50 CStdString strLog;
51 strLog.Format(">> %s (%X) -> %s (%X): %s (%2X)", ToString(command.initiator), command.initiator, ToString(command.destination), command.destination, ToString(command.opcode), command.opcode);
52 m_busDevice->AddLog(CEC_LOG_NOTICE, strLog);
53
54 if (m_busDevice->MyLogicalAddressContains(command.destination))
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 /* pass to listeners */
67 m_busDevice->GetProcessor()->AddCommand(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_MENU_REQUEST:
88 HandleMenuRequest(command);
89 break;
90 case CEC_OPCODE_GIVE_DEVICE_POWER_STATUS:
91 HandleGiveDevicePowerStatus(command);
92 break;
93 case CEC_OPCODE_GET_CEC_VERSION:
94 HandleGetCecVersion(command);
95 break;
96 case CEC_OPCODE_USER_CONTROL_PRESSED:
97 HandleUserControlPressed(command);
98 break;
99 case CEC_OPCODE_USER_CONTROL_RELEASE:
100 HandleUserControlRelease(command);
101 break;
102 case CEC_OPCODE_GIVE_AUDIO_STATUS:
103 HandleGiveAudioStatus(command);
104 break;
105 case CEC_OPCODE_GIVE_SYSTEM_AUDIO_MODE_STATUS:
106 HandleGiveSystemAudioModeStatus(command);
107 break;
108 case CEC_OPCODE_SYSTEM_AUDIO_MODE_REQUEST:
109 HandleSetSystemAudioModeRequest(command);
110 break;
111 default:
112 UnhandledCommand(command);
113 /* pass to listeners */
114 m_busDevice->GetProcessor()->AddCommand(command);
115 bHandled = false;
116 break;
117 }
118 }
119 else if (command.destination == CECDEVICE_BROADCAST)
120 {
121 CStdString strLog;
122 switch (command.opcode)
123 {
124 case CEC_OPCODE_SET_MENU_LANGUAGE:
125 HandleSetMenuLanguage(command);
126 /* pass to listeners */
127 m_busDevice->GetProcessor()->AddCommand(command);
128 break;
129 case CEC_OPCODE_REQUEST_ACTIVE_SOURCE:
130 HandleRequestActiveSource(command);
131 break;
132 case CEC_OPCODE_SET_STREAM_PATH:
133 HandleSetStreamPath(command);
134 break;
135 case CEC_OPCODE_ROUTING_CHANGE:
136 HandleRoutingChange(command);
137 break;
138 case CEC_OPCODE_DEVICE_VENDOR_ID:
139 HandleDeviceVendorId(command);
140 break;
141 case CEC_OPCODE_VENDOR_COMMAND_WITH_ID:
142 HandleDeviceVendorCommandWithId(command);
143 break;
144 case CEC_OPCODE_STANDBY:
145 HandleStandby(command);
146 /* pass to listeners */
147 m_busDevice->GetProcessor()->AddCommand(command);
148 break;
149 default:
150 UnhandledCommand(command);
151 /* pass to listeners */
152 m_busDevice->GetProcessor()->AddCommand(command);
153 bHandled = false;
154 break;
155 }
156 }
157 else
158 {
159 CStdString strLog;
160 strLog.Format("ignoring frame: we're not at destination %x", command.destination);
161 m_busDevice->AddLog(CEC_LOG_DEBUG, strLog.c_str());
162 bHandled = false;
163 }
164
165 return bHandled;
166 }
167
168 bool CCECCommandHandler::HandleDeviceCecVersion(const cec_command &command)
169 {
170 if (command.parameters.size == 1)
171 {
172 CCECBusDevice *device = GetDevice(command.initiator);
173 if (device)
174 device->SetCecVersion((cec_version) command.parameters[0]);
175 }
176
177 return true;
178 }
179
180 bool CCECCommandHandler::HandleDeviceVendorCommandWithId(const cec_command &command)
181 {
182 SetVendorId(command);
183 return true;
184 }
185
186 bool CCECCommandHandler::HandleDeviceVendorId(const cec_command &command)
187 {
188 SetVendorId(command);
189 return true;
190 }
191
192 bool CCECCommandHandler::HandleGetCecVersion(const cec_command &command)
193 {
194 CCECBusDevice *device = GetDevice(command.destination);
195 if (device)
196 return device->TransmitCECVersion(command.initiator);
197
198 return false;
199 }
200
201 bool CCECCommandHandler::HandleGiveAudioStatus(const cec_command &command)
202 {
203 CCECBusDevice *device = GetDevice(command.destination);
204 if (device && device->GetType() == CEC_DEVICE_TYPE_AUDIO_SYSTEM)
205 return ((CCECAudioSystem *) device)->TransmitAudioStatus(command.initiator);
206
207 return false;
208 }
209
210 bool CCECCommandHandler::HandleGiveDeckStatus(const cec_command &command)
211 {
212 CCECBusDevice *device = GetDevice(command.destination);
213 if (device)
214 return device->TransmitDeckStatus(command.initiator);
215
216 return false;
217 }
218
219 bool CCECCommandHandler::HandleGiveDevicePowerStatus(const cec_command &command)
220 {
221 CCECBusDevice *device = GetDevice(command.destination);
222 if (device)
223 return device->TransmitPowerState(command.initiator);
224
225 return false;
226 }
227
228 bool CCECCommandHandler::HandleGiveDeviceVendorId(const cec_command &command)
229 {
230 CCECBusDevice *device = GetDevice(command.destination);
231 if (device)
232 return device->TransmitVendorID(command.initiator);
233
234 return false;
235 }
236
237 bool CCECCommandHandler::HandleGiveOSDName(const cec_command &command)
238 {
239 CCECBusDevice *device = GetDevice(command.destination);
240 if (device)
241 return device->TransmitOSDName(command.initiator);
242
243 return false;
244 }
245
246 bool CCECCommandHandler::HandleGivePhysicalAddress(const cec_command &command)
247 {
248 CCECBusDevice *device = GetDevice(command.destination);
249 if (device)
250 return device->TransmitPhysicalAddress();
251
252 return false;
253 }
254
255 bool CCECCommandHandler::HandleMenuRequest(const cec_command &command)
256 {
257 if (command.parameters[0] == CEC_MENU_REQUEST_TYPE_QUERY)
258 {
259 CCECBusDevice *device = GetDevice(command.destination);
260 if (device)
261 return device->TransmitMenuState(command.initiator);
262 }
263 return false;
264 }
265
266 bool CCECCommandHandler::HandleReportPowerStatus(const cec_command &command)
267 {
268 if (command.parameters.size == 1)
269 {
270 CCECBusDevice *device = GetDevice(command.initiator);
271 if (device)
272 device->SetPowerStatus((cec_power_status) command.parameters[0]);
273 }
274 return true;
275 }
276
277 bool CCECCommandHandler::HandleRequestActiveSource(const cec_command &command)
278 {
279 CStdString strLog;
280 strLog.Format(">> %i requests active source", (uint8_t) command.initiator);
281 m_busDevice->AddLog(CEC_LOG_DEBUG, strLog.c_str());
282
283 vector<CCECBusDevice *> devices;
284 for (int iDevicePtr = (int)GetMyDevices(devices)-1; iDevicePtr >=0; iDevicePtr--)
285 devices[iDevicePtr]->TransmitActiveSource();
286 return true;
287 }
288
289 bool CCECCommandHandler::HandleRoutingChange(const cec_command &command)
290 {
291 if (command.parameters.size == 4)
292 {
293 uint16_t iOldAddress = ((uint16_t)command.parameters[0] << 8) | ((uint16_t)command.parameters[1]);
294 uint16_t iNewAddress = ((uint16_t)command.parameters[2] << 8) | ((uint16_t)command.parameters[3]);
295
296 CCECBusDevice *device = GetDevice(command.initiator);
297 if (device)
298 device->SetStreamPath(iNewAddress, iOldAddress);
299 }
300 return true;
301 }
302
303 bool CCECCommandHandler::HandleSetMenuLanguage(const cec_command &command)
304 {
305 if (command.parameters.size == 3)
306 {
307 CCECBusDevice *device = GetDevice(command.initiator);
308 if (device)
309 {
310 cec_menu_language language;
311 language.device = command.initiator;
312 for (uint8_t iPtr = 0; iPtr < 4; iPtr++)
313 language.language[iPtr] = command.parameters[iPtr];
314 language.language[3] = 0;
315 device->SetMenuLanguage(language);
316 }
317 }
318 return true;
319 }
320
321 bool CCECCommandHandler::HandleSetStreamPath(const cec_command &command)
322 {
323 if (command.parameters.size >= 2)
324 {
325 int streamaddr = ((uint16_t)command.parameters[0] << 8) | ((uint16_t)command.parameters[1]);
326 CStdString strLog;
327 strLog.Format(">> %i requests stream path from physical address %04x", command.initiator, streamaddr);
328 m_busDevice->AddLog(CEC_LOG_DEBUG, strLog.c_str());
329 CCECBusDevice *device = GetDeviceByPhysicalAddress(streamaddr);
330 if (device)
331 return device->TransmitActiveSource();
332 }
333 return true;
334 }
335
336 bool CCECCommandHandler::HandleSetSystemAudioModeRequest(const cec_command &command)
337 {
338 if (command.parameters.size >= 1)
339 {
340 CCECBusDevice *device = GetDevice(command.destination);
341 if (device&& device->GetType() == CEC_DEVICE_TYPE_AUDIO_SYSTEM)
342 return ((CCECAudioSystem *) device)->SetSystemAudioMode(command);
343 }
344 return true;
345 }
346
347 bool CCECCommandHandler::HandleStandby(const cec_command &command)
348 {
349 CCECBusDevice *device = GetDevice(command.initiator);
350 if (device)
351 device->SetPowerStatus(CEC_POWER_STATUS_STANDBY);
352 return true;
353 }
354
355 bool CCECCommandHandler::HandleGiveSystemAudioModeStatus(const cec_command &command)
356 {
357 CCECBusDevice *device = GetDevice(command.destination);
358 if (device && device->GetType() == CEC_DEVICE_TYPE_AUDIO_SYSTEM)
359 return ((CCECAudioSystem *) device)->TransmitSystemAudioModeStatus(command.initiator);
360
361 return false;
362 }
363
364 bool CCECCommandHandler::HandleUserControlPressed(const cec_command &command)
365 {
366 if (command.parameters.size > 0)
367 {
368 m_busDevice->GetProcessor()->AddKey();
369
370 if (command.parameters[0] <= CEC_USER_CONTROL_CODE_MAX)
371 {
372 CStdString strLog;
373 strLog.Format("key pressed: %1x", command.parameters[0]);
374 m_busDevice->AddLog(CEC_LOG_DEBUG, strLog.c_str());
375
376 m_busDevice->GetProcessor()->SetCurrentButton((cec_user_control_code) command.parameters[0]);
377 }
378 }
379 return true;
380 }
381
382 bool CCECCommandHandler::HandleUserControlRelease(const cec_command &command)
383 {
384 m_busDevice->GetProcessor()->AddKey();
385 return true;
386 }
387
388 void CCECCommandHandler::UnhandledCommand(const cec_command &command)
389 {
390 CStdString strLog;
391 strLog.Format("unhandled command with opcode %02x from address %d", command.opcode, command.initiator);
392 m_busDevice->AddLog(CEC_LOG_DEBUG, strLog);
393 }
394
395 unsigned int CCECCommandHandler::GetMyDevices(vector<CCECBusDevice *> &devices) const
396 {
397 unsigned int iReturn(0);
398
399 cec_logical_addresses addresses = m_busDevice->GetProcessor()->GetLogicalAddresses();
400 for (unsigned int iPtr = 0; iPtr < 16; iPtr++)
401 {
402 if (addresses[iPtr])
403 {
404 devices.push_back(GetDevice((cec_logical_address) iPtr));
405 ++iReturn;
406 }
407 }
408
409 return iReturn;
410 }
411
412 CCECBusDevice *CCECCommandHandler::GetDevice(cec_logical_address iLogicalAddress) const
413 {
414 CCECBusDevice *device = NULL;
415
416 if (iLogicalAddress >= CECDEVICE_TV && iLogicalAddress <= CECDEVICE_BROADCAST)
417 device = m_busDevice->GetProcessor()->m_busDevices[iLogicalAddress];
418
419 return device;
420 }
421
422 CCECBusDevice *CCECCommandHandler::GetDeviceByPhysicalAddress(uint16_t iPhysicalAddress) const
423 {
424 CCECBusDevice *device = NULL;
425
426 for (unsigned int iPtr = 0; iPtr < 16; iPtr++)
427 {
428 if (m_busDevice->GetProcessor()->m_busDevices[iPtr]->GetPhysicalAddress() == iPhysicalAddress)
429 {
430 device = m_busDevice->GetProcessor()->m_busDevices[iPtr];
431 break;
432 }
433 }
434
435 return device;
436 }
437
438 void CCECCommandHandler::SetVendorId(const cec_command &command)
439 {
440 if (command.parameters.size < 3)
441 {
442 m_busDevice->AddLog(CEC_LOG_WARNING, "invalid vendor ID received");
443 return;
444 }
445
446 uint64_t iVendorId = ((uint64_t)command.parameters[0] << 16) +
447 ((uint64_t)command.parameters[1] << 8) +
448 (uint64_t)command.parameters[2];
449
450 CCECBusDevice *device = GetDevice((cec_logical_address) command.initiator);
451 if (device)
452 device->SetVendorId(iVendorId, command.parameters.size > 3 ? command.parameters[3] : 0);
453 }
454
455 const char *CCECCommandHandler::ToString(const cec_logical_address address)
456 {
457 switch(address)
458 {
459 case CECDEVICE_AUDIOSYSTEM:
460 return "Audio";
461 case CECDEVICE_BROADCAST:
462 return "Broadcast";
463 case CECDEVICE_FREEUSE:
464 return "Free use";
465 case CECDEVICE_PLAYBACKDEVICE1:
466 return "Playback 1";
467 case CECDEVICE_PLAYBACKDEVICE2:
468 return "Playback 2";
469 case CECDEVICE_PLAYBACKDEVICE3:
470 return "Playback 3";
471 case CECDEVICE_RECORDINGDEVICE1:
472 return "Recorder 1";
473 case CECDEVICE_RECORDINGDEVICE2:
474 return "Recorder 2";
475 case CECDEVICE_RECORDINGDEVICE3:
476 return "Recorder 3";
477 case CECDEVICE_RESERVED1:
478 return "Reserved 1";
479 case CECDEVICE_RESERVED2:
480 return "Reserved 2";
481 case CECDEVICE_TUNER1:
482 return "Tuner 1";
483 case CECDEVICE_TUNER2:
484 return "Tuner 2";
485 case CECDEVICE_TUNER3:
486 return "Tuner 3";
487 case CECDEVICE_TUNER4:
488 return "Tuner 4";
489 case CECDEVICE_TV:
490 return "TV";
491 default:
492 return "unknown";
493 }
494 }
495
496 const char *CCECCommandHandler::ToString(const cec_opcode opcode)
497 {
498 switch (opcode)
499 {
500 case CEC_OPCODE_ACTIVE_SOURCE:
501 return "active source";
502 case CEC_OPCODE_IMAGE_VIEW_ON:
503 return "image view on";
504 case CEC_OPCODE_TEXT_VIEW_ON:
505 return "text view on";
506 case CEC_OPCODE_INACTIVE_SOURCE:
507 return "inactive source";
508 case CEC_OPCODE_REQUEST_ACTIVE_SOURCE:
509 return "request active source";
510 case CEC_OPCODE_ROUTING_CHANGE:
511 return "routing change";
512 case CEC_OPCODE_ROUTING_INFORMATION:
513 return "routing information";
514 case CEC_OPCODE_SET_STREAM_PATH:
515 return "set stream path";
516 case CEC_OPCODE_STANDBY:
517 return "standby";
518 case CEC_OPCODE_RECORD_OFF:
519 return "record off";
520 case CEC_OPCODE_RECORD_ON:
521 return "record on";
522 case CEC_OPCODE_RECORD_STATUS:
523 return "record status";
524 case CEC_OPCODE_RECORD_TV_SCREEN:
525 return "record tv screen";
526 case CEC_OPCODE_CLEAR_ANALOGUE_TIMER:
527 return "clear analogue timer";
528 case CEC_OPCODE_CLEAR_DIGITAL_TIMER:
529 return "clear digital timer";
530 case CEC_OPCODE_CLEAR_EXTERNAL_TIMER:
531 return "clear external timer";
532 case CEC_OPCODE_SET_ANALOGUE_TIMER:
533 return "set analogue timer";
534 case CEC_OPCODE_SET_DIGITAL_TIMER:
535 return "set digital timer";
536 case CEC_OPCODE_SET_EXTERNAL_TIMER:
537 return "set external timer";
538 case CEC_OPCODE_SET_TIMER_PROGRAM_TITLE:
539 return "set timer program title";
540 case CEC_OPCODE_TIMER_CLEARED_STATUS:
541 return "timer cleared status";
542 case CEC_OPCODE_TIMER_STATUS:
543 return "timer status";
544 case CEC_OPCODE_CEC_VERSION:
545 return "cec version";
546 case CEC_OPCODE_GET_CEC_VERSION:
547 return "get cec version";
548 case CEC_OPCODE_GIVE_PHYSICAL_ADDRESS:
549 return "give physical address";
550 case CEC_OPCODE_GET_MENU_LANGUAGE:
551 return "get menu language";
552 case CEC_OPCODE_REPORT_PHYSICAL_ADDRESS:
553 return "report physical address";
554 case CEC_OPCODE_SET_MENU_LANGUAGE:
555 return "set menu language";
556 case CEC_OPCODE_DECK_CONTROL:
557 return "deck control";
558 case CEC_OPCODE_DECK_STATUS:
559 return "deck status";
560 case CEC_OPCODE_GIVE_DECK_STATUS:
561 return "give deck status";
562 case CEC_OPCODE_PLAY:
563 return "play";
564 case CEC_OPCODE_GIVE_TUNER_DEVICE_STATUS:
565 return "give tuner status";
566 case CEC_OPCODE_SELECT_ANALOGUE_SERVICE:
567 return "select analogue service";
568 case CEC_OPCODE_SELECT_DIGITAL_SERVICE:
569 return "set digital service";
570 case CEC_OPCODE_TUNER_DEVICE_STATUS:
571 return "tuner device status";
572 case CEC_OPCODE_TUNER_STEP_DECREMENT:
573 return "tuner step decrement";
574 case CEC_OPCODE_TUNER_STEP_INCREMENT:
575 return "tuner step increment";
576 case CEC_OPCODE_DEVICE_VENDOR_ID:
577 return "device vendor id";
578 case CEC_OPCODE_GIVE_DEVICE_VENDOR_ID:
579 return "give device vendor id";
580 case CEC_OPCODE_VENDOR_COMMAND:
581 return "vendor command";
582 case CEC_OPCODE_VENDOR_COMMAND_WITH_ID:
583 return "vendor command with id";
584 case CEC_OPCODE_VENDOR_REMOTE_BUTTON_DOWN:
585 return "vendor remote button down";
586 case CEC_OPCODE_VENDOR_REMOTE_BUTTON_UP:
587 return "vendor remote button up";
588 case CEC_OPCODE_SET_OSD_STRING:
589 return "set osd string";
590 case CEC_OPCODE_GIVE_OSD_NAME:
591 return "give osd name";
592 case CEC_OPCODE_SET_OSD_NAME:
593 return "set osd name";
594 case CEC_OPCODE_MENU_REQUEST:
595 return "menu request";
596 case CEC_OPCODE_MENU_STATUS:
597 return "menu status";
598 case CEC_OPCODE_USER_CONTROL_PRESSED:
599 return "user control pressed";
600 case CEC_OPCODE_USER_CONTROL_RELEASE:
601 return "user control release";
602 case CEC_OPCODE_GIVE_DEVICE_POWER_STATUS:
603 return "give device power status";
604 case CEC_OPCODE_REPORT_POWER_STATUS:
605 return "report power status";
606 case CEC_OPCODE_FEATURE_ABORT:
607 return "feature abort";
608 case CEC_OPCODE_ABORT:
609 return "abort";
610 case CEC_OPCODE_GIVE_AUDIO_STATUS:
611 return "give audio status";
612 case CEC_OPCODE_GIVE_SYSTEM_AUDIO_MODE_STATUS:
613 return "give audio mode status";
614 case CEC_OPCODE_REPORT_AUDIO_STATUS:
615 return "report audio status";
616 case CEC_OPCODE_SET_SYSTEM_AUDIO_MODE:
617 return "set system audio mode";
618 case CEC_OPCODE_SYSTEM_AUDIO_MODE_REQUEST:
619 return "system audio mode request";
620 case CEC_OPCODE_SYSTEM_AUDIO_MODE_STATUS:
621 return "system audio mode status";
622 case CEC_OPCODE_SET_AUDIO_RATE:
623 return "set audio rate";
624 default:
625 return "UNKNOWN";
626 }
627 }