cec: buffer up commands that are received while waiting for an ack. only send 'active...
[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;
8747dd4f 39using namespace std;
e9de9629
LOK
40
41CCECCommandHandler::CCECCommandHandler(CCECBusDevice *busDevice)
42{
43 m_busDevice = busDevice;
44}
45
46bool CCECCommandHandler::HandleCommand(const cec_command &command)
47{
48 bool bHandled(true);
49
5e822b09
LOK
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
f8513317 54 if (m_busDevice->MyLogicalAddressContains(command.destination))
e9de9629
LOK
55 {
56 switch(command.opcode)
57 {
e55f3f70
LOK
58 case CEC_OPCODE_REPORT_POWER_STATUS:
59 HandleReportPowerStatus(command);
60 break;
6a1c0009
LOK
61 case CEC_OPCODE_CEC_VERSION:
62 HandleDeviceCecVersion(command);
63 break;
a3269a0a
LOK
64 case CEC_OPCODE_SET_MENU_LANGUAGE:
65 HandleSetMenuLanguage(command);
a9e03a86 66 m_busDevice->GetProcessor()->AddCommand(command);
a3269a0a 67 break;
e9de9629
LOK
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;
a1f8fb1b
LOK
101 case CEC_OPCODE_GIVE_AUDIO_STATUS:
102 HandleGiveAudioStatus(command);
103 break;
cf0ecd85
LOK
104 case CEC_OPCODE_GIVE_SYSTEM_AUDIO_MODE_STATUS:
105 HandleGiveSystemAudioModeStatus(command);
106 break;
e5e86c76
LOK
107 case CEC_OPCODE_SYSTEM_AUDIO_MODE_REQUEST:
108 HandleSetSystemAudioModeRequest(command);
109 break;
e9de9629
LOK
110 default:
111 UnhandledCommand(command);
a9e03a86 112 m_busDevice->GetProcessor()->AddCommand(command);
e9de9629
LOK
113 bHandled = false;
114 break;
115 }
116 }
117 else if (command.destination == CECDEVICE_BROADCAST)
118 {
119 CStdString strLog;
120 switch (command.opcode)
121 {
a3269a0a
LOK
122 case CEC_OPCODE_SET_MENU_LANGUAGE:
123 HandleSetMenuLanguage(command);
a9e03a86 124 m_busDevice->GetProcessor()->AddCommand(command);
a3269a0a 125 break;
e9de9629
LOK
126 case CEC_OPCODE_REQUEST_ACTIVE_SOURCE:
127 HandleRequestActiveSource(command);
128 break;
129 case CEC_OPCODE_SET_STREAM_PATH:
130 HandleSetStreamPath(command);
a9e03a86 131 m_busDevice->GetProcessor()->AddCommand(command);
e9de9629
LOK
132 break;
133 case CEC_OPCODE_ROUTING_CHANGE:
134 HandleRoutingChange(command);
a9e03a86 135 m_busDevice->GetProcessor()->AddCommand(command);
e9de9629
LOK
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);
a9e03a86 145 m_busDevice->GetProcessor()->AddCommand(command);
e9de9629
LOK
146 bHandled = false;
147 break;
148 }
149 }
150 else
151 {
152 CStdString strLog;
f8513317 153 strLog.Format("ignoring frame: we're not at destination %x", command.destination);
e9de9629
LOK
154 m_busDevice->AddLog(CEC_LOG_DEBUG, strLog.c_str());
155 bHandled = false;
156 }
157
158 return bHandled;
159}
160
6a1c0009
LOK
161bool 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
e9de9629
LOK
173bool CCECCommandHandler::HandleDeviceVendorCommandWithId(const cec_command &command)
174{
181b3475 175 SetVendorId(command);
6a1c0009 176 return true;
e9de9629
LOK
177}
178
179bool CCECCommandHandler::HandleDeviceVendorId(const cec_command &command)
180{
181b3475 181 SetVendorId(command);
6a1c0009 182 return true;
e9de9629
LOK
183}
184
185bool CCECCommandHandler::HandleGetCecVersion(const cec_command &command)
186{
f8513317 187 CCECBusDevice *device = GetDevice(command.destination);
0f23c85c 188 if (device)
29912296 189 return device->TransmitCECVersion(command.initiator);
0f23c85c
LOK
190
191 return false;
e9de9629
LOK
192}
193
a1f8fb1b
LOK
194bool 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
e9de9629
LOK
203bool CCECCommandHandler::HandleGiveDeckStatus(const cec_command &command)
204{
f8513317 205 CCECBusDevice *device = GetDevice(command.destination);
0f23c85c 206 if (device)
29912296 207 return device->TransmitDeckStatus(command.initiator);
0f23c85c
LOK
208
209 return false;
e9de9629
LOK
210}
211
212bool CCECCommandHandler::HandleGiveDevicePowerStatus(const cec_command &command)
213{
f8513317 214 CCECBusDevice *device = GetDevice(command.destination);
0f23c85c 215 if (device)
29912296 216 return device->TransmitPowerState(command.initiator);
0f23c85c
LOK
217
218 return false;
e9de9629
LOK
219}
220
221bool CCECCommandHandler::HandleGiveDeviceVendorId(const cec_command &command)
222{
f8513317 223 CCECBusDevice *device = GetDevice(command.destination);
0f23c85c 224 if (device)
29912296 225 return device->TransmitVendorID(command.initiator);
0f23c85c
LOK
226
227 return false;
e9de9629
LOK
228}
229
230bool CCECCommandHandler::HandleGiveOSDName(const cec_command &command)
231{
f8513317 232 CCECBusDevice *device = GetDevice(command.destination);
0f23c85c 233 if (device)
29912296 234 return device->TransmitOSDName(command.initiator);
0f23c85c
LOK
235
236 return false;
e9de9629
LOK
237}
238
239bool CCECCommandHandler::HandleGivePhysicalAddress(const cec_command &command)
240{
f8513317 241 CCECBusDevice *device = GetDevice(command.destination);
09c10b66 242 if (device)
29912296 243 return device->TransmitPhysicalAddress();
09c10b66
LOK
244
245 return false;
e9de9629
LOK
246}
247
248bool CCECCommandHandler::HandleMenuRequest(const cec_command &command)
249{
250 if (command.parameters[0] == CEC_MENU_REQUEST_TYPE_QUERY)
0f23c85c 251 {
f8513317 252 CCECBusDevice *device = GetDevice(command.destination);
0f23c85c 253 if (device)
29912296 254 return device->TransmitMenuState(command.initiator);
0f23c85c
LOK
255 }
256 return false;
e9de9629
LOK
257}
258
e55f3f70
LOK
259bool 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
e9de9629
LOK
270bool 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());
8747dd4f
LOK
275
276 vector<CCECBusDevice *> devices;
277 for (int iDevicePtr = (int)GetMyDevices(devices)-1; iDevicePtr >=0; iDevicePtr--)
278 devices[iDevicePtr]->TransmitActiveSource();
279 return true;
e9de9629
LOK
280}
281
282bool 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]);
e9de9629 288
0f23c85c
LOK
289 CCECBusDevice *device = GetDevice(command.initiator);
290 if (device)
9dc04b07 291 device->SetStreamPath(iNewAddress, iOldAddress);
e9de9629
LOK
292 }
293 return true;
294}
295
a3269a0a
LOK
296bool 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;
56701628 305 for (uint8_t iPtr = 0; iPtr < 4; iPtr++)
a3269a0a
LOK
306 language.language[iPtr] = command.parameters[iPtr];
307 language.language[3] = 0;
308 device->SetMenuLanguage(language);
309 }
310 }
311 return true;
312}
313
e9de9629
LOK
314bool 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());
6685ae07
LOK
322 CCECBusDevice *device = GetDeviceByPhysicalAddress(streamaddr);
323 if (device)
324 return device->TransmitActiveSource();
e9de9629
LOK
325 }
326 return true;
327}
328
e5e86c76
LOK
329bool 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
cf0ecd85
LOK
340bool 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
e9de9629
LOK
349bool 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
367bool CCECCommandHandler::HandleUserControlRelease(const cec_command &command)
368{
369 m_busDevice->GetProcessor()->AddKey();
370 return true;
371}
372
373void CCECCommandHandler::UnhandledCommand(const cec_command &command)
374{
b5b53c7d
LOK
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);
0f23c85c
LOK
378}
379
8747dd4f
LOK
380unsigned 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
0f23c85c
LOK
397CCECBusDevice *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;
e9de9629 405}
6685ae07
LOK
406
407CCECBusDevice *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}
181b3475 422
181b3475
LOK
423void 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
bae71306
LOK
431 uint64_t iVendorId = ((uint64_t)command.parameters[0] << 16) +
432 ((uint64_t)command.parameters[1] << 8) +
181b3475
LOK
433 (uint64_t)command.parameters[2];
434
435 CCECBusDevice *device = GetDevice((cec_logical_address) command.initiator);
436 if (device)
a7bed660 437 device->SetVendorId(iVendorId, command.parameters.size > 3 ? command.parameters[3] : 0);
181b3475 438}
5e822b09
LOK
439
440const 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}