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