cec: buffer up commands that are received while waiting for an ack. only send 'active...
[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(">> %x -> %x: %s (%2x)", command.initiator, 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_opcode opcode)
441 {
442 switch (opcode)
443 {
444 case CEC_OPCODE_ACTIVE_SOURCE:
445 return "active source";
446 case CEC_OPCODE_IMAGE_VIEW_ON:
447 return "image view on";
448 case CEC_OPCODE_TEXT_VIEW_ON:
449 return "text view on";
450 case CEC_OPCODE_INACTIVE_SOURCE:
451 return "inactive source";
452 case CEC_OPCODE_REQUEST_ACTIVE_SOURCE:
453 return "request active source";
454 case CEC_OPCODE_ROUTING_CHANGE:
455 return "routing change";
456 case CEC_OPCODE_ROUTING_INFORMATION:
457 return "routing information";
458 case CEC_OPCODE_SET_STREAM_PATH:
459 return "set stream path";
460 case CEC_OPCODE_STANDBY:
461 return "standby";
462 case CEC_OPCODE_RECORD_OFF:
463 return "record off";
464 case CEC_OPCODE_RECORD_ON:
465 return "record on";
466 case CEC_OPCODE_RECORD_STATUS:
467 return "record status";
468 case CEC_OPCODE_RECORD_TV_SCREEN:
469 return "record tv screen";
470 case CEC_OPCODE_CLEAR_ANALOGUE_TIMER:
471 return "clear analogue timer";
472 case CEC_OPCODE_CLEAR_DIGITAL_TIMER:
473 return "clear digital timer";
474 case CEC_OPCODE_CLEAR_EXTERNAL_TIMER:
475 return "clear external timer";
476 case CEC_OPCODE_SET_ANALOGUE_TIMER:
477 return "set analogue timer";
478 case CEC_OPCODE_SET_DIGITAL_TIMER:
479 return "set digital timer";
480 case CEC_OPCODE_SET_EXTERNAL_TIMER:
481 return "set external timer";
482 case CEC_OPCODE_SET_TIMER_PROGRAM_TITLE:
483 return "set timer program title";
484 case CEC_OPCODE_TIMER_CLEARED_STATUS:
485 return "timer cleared status";
486 case CEC_OPCODE_TIMER_STATUS:
487 return "timer status";
488 case CEC_OPCODE_CEC_VERSION:
489 return "cec version";
490 case CEC_OPCODE_GET_CEC_VERSION:
491 return "get cec version";
492 case CEC_OPCODE_GIVE_PHYSICAL_ADDRESS:
493 return "give physical address";
494 case CEC_OPCODE_GET_MENU_LANGUAGE:
495 return "get menu language";
496 case CEC_OPCODE_REPORT_PHYSICAL_ADDRESS:
497 return "report physical address";
498 case CEC_OPCODE_SET_MENU_LANGUAGE:
499 return "set menu language";
500 case CEC_OPCODE_DECK_CONTROL:
501 return "deck control";
502 case CEC_OPCODE_DECK_STATUS:
503 return "deck status";
504 case CEC_OPCODE_GIVE_DECK_STATUS:
505 return "give deck status";
506 case CEC_OPCODE_PLAY:
507 return "play";
508 case CEC_OPCODE_GIVE_TUNER_DEVICE_STATUS:
509 return "give tuner status";
510 case CEC_OPCODE_SELECT_ANALOGUE_SERVICE:
511 return "select analogue service";
512 case CEC_OPCODE_SELECT_DIGITAL_SERVICE:
513 return "set digital service";
514 case CEC_OPCODE_TUNER_DEVICE_STATUS:
515 return "tuner device status";
516 case CEC_OPCODE_TUNER_STEP_DECREMENT:
517 return "tuner step decrement";
518 case CEC_OPCODE_TUNER_STEP_INCREMENT:
519 return "tuner step increment";
520 case CEC_OPCODE_DEVICE_VENDOR_ID:
521 return "device vendor id";
522 case CEC_OPCODE_GIVE_DEVICE_VENDOR_ID:
523 return "give device vendor id";
524 case CEC_OPCODE_VENDOR_COMMAND:
525 return "vendor command";
526 case CEC_OPCODE_VENDOR_COMMAND_WITH_ID:
527 return "vendor command with id";
528 case CEC_OPCODE_VENDOR_REMOTE_BUTTON_DOWN:
529 return "vendor remote button down";
530 case CEC_OPCODE_VENDOR_REMOTE_BUTTON_UP:
531 return "vendor remote button up";
532 case CEC_OPCODE_SET_OSD_STRING:
533 return "set osd string";
534 case CEC_OPCODE_GIVE_OSD_NAME:
535 return "give osd name";
536 case CEC_OPCODE_SET_OSD_NAME:
537 return "set osd name";
538 case CEC_OPCODE_MENU_REQUEST:
539 return "menu request";
540 case CEC_OPCODE_MENU_STATUS:
541 return "menu status";
542 case CEC_OPCODE_USER_CONTROL_PRESSED:
543 return "user control pressed";
544 case CEC_OPCODE_USER_CONTROL_RELEASE:
545 return "user control release";
546 case CEC_OPCODE_GIVE_DEVICE_POWER_STATUS:
547 return "give device power status";
548 case CEC_OPCODE_REPORT_POWER_STATUS:
549 return "report power status";
550 case CEC_OPCODE_FEATURE_ABORT:
551 return "feature abort";
552 case CEC_OPCODE_ABORT:
553 return "abort";
554 case CEC_OPCODE_GIVE_AUDIO_STATUS:
555 return "give audio status";
556 case CEC_OPCODE_GIVE_SYSTEM_AUDIO_MODE_STATUS:
557 return "give audio mode status";
558 case CEC_OPCODE_REPORT_AUDIO_STATUS:
559 return "report audio status";
560 case CEC_OPCODE_SET_SYSTEM_AUDIO_MODE:
561 return "set system audio mode";
562 case CEC_OPCODE_SYSTEM_AUDIO_MODE_REQUEST:
563 return "system audio mode request";
564 case CEC_OPCODE_SYSTEM_AUDIO_MODE_STATUS:
565 return "system audio mode status";
566 case CEC_OPCODE_SET_AUDIO_RATE:
567 return "set audio rate";
568 default:
569 return "UNKNOWN";
570 }
571 }