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