silence 'unused' warnings
[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-2012 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 "../devices/CECPlaybackDevice.h"
37 #include "../CECClient.h"
38 #include "../CECProcessor.h"
39 #include "../LibCEC.h"
40 #include "../CECTypeUtils.h"
41 #include "../platform/util/util.h"
42
43 using namespace CEC;
44 using namespace std;
45 using namespace PLATFORM;
46
47 #define LIB_CEC m_busDevice->GetProcessor()->GetLib()
48 #define ToString(p) CCECTypeUtils::ToString(p)
49
50 CCECCommandHandler::CCECCommandHandler(CCECBusDevice *busDevice) :
51 m_busDevice(busDevice),
52 m_processor(m_busDevice->GetProcessor()),
53 m_iTransmitTimeout(CEC_DEFAULT_TRANSMIT_TIMEOUT),
54 m_iTransmitWait(CEC_DEFAULT_TRANSMIT_WAIT),
55 m_iTransmitRetries(CEC_DEFAULT_TRANSMIT_RETRIES),
56 m_bHandlerInited(false),
57 m_bOPTSendDeckStatusUpdateOnActiveSource(false),
58 m_vendorId(CEC_VENDOR_UNKNOWN),
59 m_waitForResponse(new CWaitForResponse),
60 m_bActiveSourcePending(false)
61 {
62 }
63
64 CCECCommandHandler::~CCECCommandHandler(void)
65 {
66 DELETE_AND_NULL(m_waitForResponse);
67 }
68
69 bool CCECCommandHandler::HandleCommand(const cec_command &command)
70 {
71 if (command.opcode_set == 0)
72 return HandlePoll(command);
73
74 int iHandled(CEC_ABORT_REASON_UNRECOGNIZED_OPCODE);
75
76 CCECClient *client = m_busDevice->GetClient();
77 if (client)
78 client->AddCommand(command);
79
80 switch(command.opcode)
81 {
82 case CEC_OPCODE_REPORT_POWER_STATUS:
83 iHandled = HandleReportPowerStatus(command);
84 break;
85 case CEC_OPCODE_CEC_VERSION:
86 iHandled = HandleDeviceCecVersion(command);
87 break;
88 case CEC_OPCODE_SET_MENU_LANGUAGE:
89 iHandled = HandleSetMenuLanguage(command);
90 break;
91 case CEC_OPCODE_GIVE_PHYSICAL_ADDRESS:
92 iHandled = HandleGivePhysicalAddress(command);
93 break;
94 case CEC_OPCODE_GET_MENU_LANGUAGE:
95 iHandled = HandleGiveMenuLanguage(command);
96 break;
97 case CEC_OPCODE_GIVE_OSD_NAME:
98 iHandled = HandleGiveOSDName(command);
99 break;
100 case CEC_OPCODE_GIVE_DEVICE_VENDOR_ID:
101 iHandled = HandleGiveDeviceVendorId(command);
102 break;
103 case CEC_OPCODE_DEVICE_VENDOR_ID:
104 iHandled = HandleDeviceVendorId(command);
105 break;
106 case CEC_OPCODE_VENDOR_COMMAND_WITH_ID:
107 iHandled = HandleDeviceVendorCommandWithId(command);
108 break;
109 case CEC_OPCODE_GIVE_DECK_STATUS:
110 iHandled = HandleGiveDeckStatus(command);
111 break;
112 case CEC_OPCODE_DECK_CONTROL:
113 iHandled = HandleDeckControl(command);
114 break;
115 case CEC_OPCODE_MENU_REQUEST:
116 iHandled = HandleMenuRequest(command);
117 break;
118 case CEC_OPCODE_GIVE_DEVICE_POWER_STATUS:
119 iHandled = HandleGiveDevicePowerStatus(command);
120 break;
121 case CEC_OPCODE_GET_CEC_VERSION:
122 iHandled = HandleGetCecVersion(command);
123 break;
124 case CEC_OPCODE_USER_CONTROL_PRESSED:
125 iHandled = HandleUserControlPressed(command);
126 break;
127 case CEC_OPCODE_USER_CONTROL_RELEASE:
128 iHandled = HandleUserControlRelease(command);
129 break;
130 case CEC_OPCODE_GIVE_AUDIO_STATUS:
131 iHandled = HandleGiveAudioStatus(command);
132 break;
133 case CEC_OPCODE_GIVE_SYSTEM_AUDIO_MODE_STATUS:
134 iHandled = HandleGiveSystemAudioModeStatus(command);
135 break;
136 case CEC_OPCODE_SYSTEM_AUDIO_MODE_REQUEST:
137 iHandled = HandleSystemAudioModeRequest(command);
138 break;
139 case CEC_OPCODE_REPORT_AUDIO_STATUS:
140 iHandled = HandleReportAudioStatus(command);
141 break;
142 case CEC_OPCODE_SYSTEM_AUDIO_MODE_STATUS:
143 iHandled = HandleSystemAudioModeStatus(command);
144 break;
145 case CEC_OPCODE_SET_SYSTEM_AUDIO_MODE:
146 iHandled = HandleSetSystemAudioMode(command);
147 break;
148 case CEC_OPCODE_REQUEST_ACTIVE_SOURCE:
149 iHandled = HandleRequestActiveSource(command);
150 break;
151 case CEC_OPCODE_SET_STREAM_PATH:
152 iHandled = HandleSetStreamPath(command);
153 break;
154 case CEC_OPCODE_ROUTING_CHANGE:
155 iHandled = HandleRoutingChange(command);
156 break;
157 case CEC_OPCODE_ROUTING_INFORMATION:
158 iHandled = HandleRoutingInformation(command);
159 break;
160 case CEC_OPCODE_STANDBY:
161 iHandled = HandleStandby(command);
162 break;
163 case CEC_OPCODE_ACTIVE_SOURCE:
164 iHandled = HandleActiveSource(command);
165 break;
166 case CEC_OPCODE_REPORT_PHYSICAL_ADDRESS:
167 iHandled = HandleReportPhysicalAddress(command);
168 break;
169 case CEC_OPCODE_SET_OSD_NAME:
170 iHandled = HandleSetOSDName(command);
171 break;
172 case CEC_OPCODE_IMAGE_VIEW_ON:
173 iHandled = HandleImageViewOn(command);
174 break;
175 case CEC_OPCODE_TEXT_VIEW_ON:
176 iHandled = HandleTextViewOn(command);
177 break;
178 case CEC_OPCODE_FEATURE_ABORT:
179 iHandled = HandleFeatureAbort(command);
180 break;
181 case CEC_OPCODE_VENDOR_COMMAND:
182 iHandled = HandleVendorCommand(command);
183 break;
184 case CEC_OPCODE_VENDOR_REMOTE_BUTTON_DOWN:
185 iHandled = HandleVendorRemoteButtonDown(command);
186 break;
187 case CEC_OPCODE_VENDOR_REMOTE_BUTTON_UP:
188 iHandled = HandleVendorRemoteButtonUp(command);
189 break;
190 case CEC_OPCODE_PLAY:
191 // libCEC (currently) doesn't need to do anything with this, since player applications handle it
192 // but it should not respond with a feature abort
193 iHandled = COMMAND_HANDLED;
194 break;
195 default:
196 break;
197 }
198
199 if (iHandled == COMMAND_HANDLED)
200 m_waitForResponse->Received((command.opcode == CEC_OPCODE_FEATURE_ABORT && command.parameters.size > 0) ? (cec_opcode)command.parameters[0] : command.opcode);
201 else
202 UnhandledCommand(command, (cec_abort_reason)iHandled);
203
204 return iHandled == COMMAND_HANDLED;
205 }
206
207 int CCECCommandHandler::HandleActiveSource(const cec_command &command)
208 {
209 if (command.parameters.size == 2)
210 {
211 uint16_t iAddress = ((uint16_t)command.parameters[0] << 8) | ((uint16_t)command.parameters[1]);
212 CCECBusDevice *device = m_processor->GetDeviceByPhysicalAddress(iAddress);
213 if (device)
214 {
215 device->MarkAsActiveSource();
216 return COMMAND_HANDLED;
217 }
218 }
219
220 return CEC_ABORT_REASON_INVALID_OPERAND;
221 }
222
223 int CCECCommandHandler::HandleDeckControl(const cec_command &command)
224 {
225 CCECPlaybackDevice *device = CCECBusDevice::AsPlaybackDevice(GetDevice(command.destination));
226 if (device && command.parameters.size > 0)
227 {
228 device->SetDeckControlMode((cec_deck_control_mode) command.parameters[0]);
229 return COMMAND_HANDLED;
230 }
231
232 return CEC_ABORT_REASON_INVALID_OPERAND;
233 }
234
235 int CCECCommandHandler::HandleDeviceCecVersion(const cec_command &command)
236 {
237 if (command.parameters.size == 1)
238 {
239 CCECBusDevice *device = GetDevice(command.initiator);
240 if (device)
241 device->SetCecVersion((cec_version) command.parameters[0]);
242
243 return COMMAND_HANDLED;
244 }
245
246 return CEC_ABORT_REASON_INVALID_OPERAND;
247 }
248
249 int CCECCommandHandler::HandleDeviceVendorCommandWithId(const cec_command & UNUSED(command))
250 {
251 return CEC_ABORT_REASON_REFUSED;
252 }
253
254 int CCECCommandHandler::HandleDeviceVendorId(const cec_command &command)
255 {
256 SetVendorId(command);
257 return COMMAND_HANDLED;
258 }
259
260 int CCECCommandHandler::HandleFeatureAbort(const cec_command &command)
261 {
262 if (command.parameters.size == 2 &&
263 (command.parameters[1] == CEC_ABORT_REASON_UNRECOGNIZED_OPCODE ||
264 command.parameters[1] == CEC_ABORT_REASON_REFUSED))
265 m_processor->GetDevice(command.initiator)->SetUnsupportedFeature((cec_opcode)command.parameters[0]);
266 return COMMAND_HANDLED;
267 }
268
269 int CCECCommandHandler::HandleGetCecVersion(const cec_command &command)
270 {
271 if (m_processor->CECInitialised() && m_processor->IsHandledByLibCEC(command.destination))
272 {
273 CCECBusDevice *device = GetDevice(command.destination);
274 if (device && device->TransmitCECVersion(command.initiator))
275 return COMMAND_HANDLED;
276 return CEC_ABORT_REASON_INVALID_OPERAND;
277 }
278
279 return CEC_ABORT_REASON_NOT_IN_CORRECT_MODE_TO_RESPOND;
280 }
281
282 int CCECCommandHandler::HandleGiveAudioStatus(const cec_command &command)
283 {
284 if (m_processor->CECInitialised() && m_processor->IsHandledByLibCEC(command.destination))
285 {
286 CCECAudioSystem *device = CCECBusDevice::AsAudioSystem(GetDevice(command.destination));
287 if (device && device->TransmitAudioStatus(command.initiator))
288 return COMMAND_HANDLED;
289 return CEC_ABORT_REASON_INVALID_OPERAND;
290 }
291
292 return CEC_ABORT_REASON_NOT_IN_CORRECT_MODE_TO_RESPOND;
293 }
294
295 int CCECCommandHandler::HandleGiveDeckStatus(const cec_command &command)
296 {
297 if (m_processor->CECInitialised() && m_processor->IsHandledByLibCEC(command.destination))
298 {
299 CCECPlaybackDevice *device = CCECBusDevice::AsPlaybackDevice(GetDevice(command.destination));
300 if (device && device->TransmitDeckStatus(command.initiator))
301 return COMMAND_HANDLED;
302 return CEC_ABORT_REASON_INVALID_OPERAND;
303 }
304
305 return CEC_ABORT_REASON_NOT_IN_CORRECT_MODE_TO_RESPOND;
306 }
307
308 int CCECCommandHandler::HandleGiveDevicePowerStatus(const cec_command &command)
309 {
310 if (m_processor->CECInitialised() && m_processor->IsHandledByLibCEC(command.destination))
311 {
312 CCECBusDevice *device = GetDevice(command.destination);
313 if (device && device->TransmitPowerState(command.initiator))
314 return COMMAND_HANDLED;
315 return CEC_ABORT_REASON_INVALID_OPERAND;
316 }
317
318 return CEC_ABORT_REASON_INVALID_OPERAND;
319 }
320
321 int CCECCommandHandler::HandleGiveDeviceVendorId(const cec_command &command)
322 {
323 if (m_processor->CECInitialised() && m_processor->IsHandledByLibCEC(command.destination))
324 {
325 CCECBusDevice *device = GetDevice(command.destination);
326 if (device && device->TransmitVendorID(command.initiator))
327 return COMMAND_HANDLED;
328 }
329
330 return CEC_ABORT_REASON_INVALID_OPERAND;
331 }
332
333 int CCECCommandHandler::HandleGiveOSDName(const cec_command &command)
334 {
335 if (m_processor->CECInitialised() && m_processor->IsHandledByLibCEC(command.destination))
336 {
337 CCECBusDevice *device = GetDevice(command.destination);
338 if (device && device->TransmitOSDName(command.initiator))
339 return COMMAND_HANDLED;
340 }
341
342 return CEC_ABORT_REASON_INVALID_OPERAND;
343 }
344
345 int CCECCommandHandler::HandleGivePhysicalAddress(const cec_command &command)
346 {
347 if (m_processor->CECInitialised() && m_processor->IsHandledByLibCEC(command.destination))
348 {
349 CCECBusDevice *device = GetDevice(command.destination);
350 if (device && device->TransmitPhysicalAddress())
351 return COMMAND_HANDLED;
352 return CEC_ABORT_REASON_INVALID_OPERAND;
353 }
354
355 return CEC_ABORT_REASON_NOT_IN_CORRECT_MODE_TO_RESPOND;
356 }
357
358 int CCECCommandHandler::HandleGiveMenuLanguage(const cec_command &command)
359 {
360 if (m_processor->CECInitialised() && m_processor->IsHandledByLibCEC(command.destination))
361 {
362 CCECBusDevice *device = GetDevice(command.destination);
363 if (device && device->TransmitSetMenuLanguage(command.initiator))
364 return COMMAND_HANDLED;
365 return CEC_ABORT_REASON_INVALID_OPERAND;
366 }
367
368 return CEC_ABORT_REASON_NOT_IN_CORRECT_MODE_TO_RESPOND;
369 }
370
371 int CCECCommandHandler::HandleGiveSystemAudioModeStatus(const cec_command &command)
372 {
373 if (m_processor->CECInitialised() && m_processor->IsHandledByLibCEC(command.destination))
374 {
375 CCECAudioSystem *device = CCECBusDevice::AsAudioSystem(GetDevice(command.destination));
376 if (device && device->TransmitSystemAudioModeStatus(command.initiator))
377 return COMMAND_HANDLED;
378 return CEC_ABORT_REASON_INVALID_OPERAND;
379 }
380
381 return CEC_ABORT_REASON_NOT_IN_CORRECT_MODE_TO_RESPOND;
382 }
383
384 int CCECCommandHandler::HandleImageViewOn(const cec_command &command)
385 {
386 m_processor->GetDevice(command.initiator)->MarkAsActiveSource();
387 return COMMAND_HANDLED;
388 }
389
390 int CCECCommandHandler::HandleMenuRequest(const cec_command &command)
391 {
392 if (m_processor->CECInitialised() && m_processor->IsHandledByLibCEC(command.destination))
393 {
394 CCECBusDevice *device = GetDevice(command.destination);
395 if (device)
396 {
397 CCECClient *client = device->GetClient();
398 if (client)
399 {
400 if (command.parameters[0] == CEC_MENU_REQUEST_TYPE_ACTIVATE)
401 {
402 if (client->MenuStateChanged(CEC_MENU_STATE_ACTIVATED) == 1)
403 device->SetMenuState(CEC_MENU_STATE_ACTIVATED);
404 }
405 else if (command.parameters[0] == CEC_MENU_REQUEST_TYPE_DEACTIVATE)
406 {
407 if (client->MenuStateChanged(CEC_MENU_STATE_DEACTIVATED) == 1)
408 device->SetMenuState(CEC_MENU_STATE_DEACTIVATED);
409 }
410 }
411 if (device->TransmitMenuState(command.initiator))
412 return COMMAND_HANDLED;
413 }
414 return CEC_ABORT_REASON_INVALID_OPERAND;
415 }
416
417 return CEC_ABORT_REASON_NOT_IN_CORRECT_MODE_TO_RESPOND;
418 }
419
420 bool CCECCommandHandler::HandlePoll(const cec_command &command)
421 {
422 m_busDevice->HandlePoll(command.destination);
423 return true;
424 }
425
426 int CCECCommandHandler::HandleReportAudioStatus(const cec_command &command)
427 {
428 if (command.parameters.size == 1)
429 {
430 CCECAudioSystem *device = CCECBusDevice::AsAudioSystem(GetDevice(command.initiator));
431 if (device)
432 {
433 device->SetAudioStatus(command.parameters[0]);
434 return COMMAND_HANDLED;
435 }
436 }
437 return CEC_ABORT_REASON_INVALID_OPERAND;
438 }
439
440 int CCECCommandHandler::HandleReportPhysicalAddress(const cec_command &command)
441 {
442 if (command.parameters.size == 3)
443 {
444 uint16_t iNewAddress = ((uint16_t)command.parameters[0] << 8) | ((uint16_t)command.parameters[1]);
445 SetPhysicalAddress(command.initiator, iNewAddress);
446 return COMMAND_HANDLED;
447 }
448 return CEC_ABORT_REASON_INVALID_OPERAND;
449 }
450
451 int CCECCommandHandler::HandleReportPowerStatus(const cec_command &command)
452 {
453 if (command.parameters.size == 1)
454 {
455 CCECBusDevice *device = GetDevice(command.initiator);
456 if (device)
457 {
458 device->SetPowerStatus((cec_power_status) command.parameters[0]);
459 return COMMAND_HANDLED;
460 }
461 }
462 return CEC_ABORT_REASON_INVALID_OPERAND;
463 }
464
465 int CCECCommandHandler::HandleRequestActiveSource(const cec_command &command)
466 {
467 if (m_processor->CECInitialised())
468 {
469 LIB_CEC->AddLog(CEC_LOG_DEBUG, ">> %i requests active source", (uint8_t) command.initiator);
470 m_processor->GetDevice(command.initiator)->SetPowerStatus(CEC_POWER_STATUS_ON);
471
472 vector<CCECBusDevice *> devices;
473 for (size_t iDevicePtr = 0; iDevicePtr < GetMyDevices(devices); iDevicePtr++)
474 devices[iDevicePtr]->TransmitActiveSource();
475 }
476
477 return COMMAND_HANDLED;
478 }
479
480 int CCECCommandHandler::HandleRoutingChange(const cec_command &command)
481 {
482 if (command.parameters.size == 4)
483 {
484 uint16_t iOldAddress = ((uint16_t)command.parameters[0] << 8) | ((uint16_t)command.parameters[1]);
485 uint16_t iNewAddress = ((uint16_t)command.parameters[2] << 8) | ((uint16_t)command.parameters[3]);
486
487 CCECBusDevice *device = GetDevice(command.initiator);
488 if (device)
489 {
490 device->SetStreamPath(iNewAddress, iOldAddress);
491 return COMMAND_HANDLED;
492 }
493 }
494
495 return CEC_ABORT_REASON_INVALID_OPERAND;
496 }
497
498 int CCECCommandHandler::HandleRoutingInformation(const cec_command &command)
499 {
500 if (command.parameters.size == 2)
501 {
502 uint16_t iNewAddress = ((uint16_t)command.parameters[0] << 8) | ((uint16_t)command.parameters[1]);
503 CCECBusDevice *device = m_processor->GetDeviceByPhysicalAddress(iNewAddress);
504 if (device)
505 {
506 device->MarkAsActiveSource();
507 return COMMAND_HANDLED;
508 }
509 }
510
511 return CEC_ABORT_REASON_INVALID_OPERAND;
512 }
513
514 int CCECCommandHandler::HandleSetMenuLanguage(const cec_command &command)
515 {
516 if (command.parameters.size == 3)
517 {
518 CCECBusDevice *device = GetDevice(command.initiator);
519 if (device)
520 {
521 cec_menu_language language;
522 language.device = command.initiator;
523 for (uint8_t iPtr = 0; iPtr < 4; iPtr++)
524 language.language[iPtr] = command.parameters[iPtr];
525 language.language[3] = 0;
526 device->SetMenuLanguage(language);
527 return COMMAND_HANDLED;
528 }
529 }
530
531 return CEC_ABORT_REASON_INVALID_OPERAND;
532 }
533
534 int CCECCommandHandler::HandleSetOSDName(const cec_command &command)
535 {
536 if (command.parameters.size > 0)
537 {
538 CCECBusDevice *device = GetDevice(command.initiator);
539 if (device)
540 {
541 char buf[1024];
542 for (uint8_t iPtr = 0; iPtr < command.parameters.size; iPtr++)
543 buf[iPtr] = (char)command.parameters[iPtr];
544 buf[command.parameters.size] = 0;
545
546 CStdString strName(buf);
547 device->SetOSDName(strName);
548
549 return COMMAND_HANDLED;
550 }
551 }
552
553 return CEC_ABORT_REASON_INVALID_OPERAND;
554 }
555
556 int CCECCommandHandler::HandleSetStreamPath(const cec_command &command)
557 {
558 if (!m_processor->CECInitialised())
559 return CEC_ABORT_REASON_NOT_IN_CORRECT_MODE_TO_RESPOND;
560
561 if (command.parameters.size >= 2)
562 {
563 uint16_t iStreamAddress = ((uint16_t)command.parameters[0] << 8) | ((uint16_t)command.parameters[1]);
564 LIB_CEC->AddLog(CEC_LOG_DEBUG, ">> %i sets stream path to physical address %04x", command.initiator, iStreamAddress);
565
566 /* one of the device handled by libCEC has been made active */
567 CCECBusDevice *device = GetDeviceByPhysicalAddress(iStreamAddress);
568 if (device && device->IsHandledByLibCEC())
569 {
570 device->ActivateSource();
571 return COMMAND_HANDLED;
572 }
573 }
574
575 return CEC_ABORT_REASON_INVALID_OPERAND;
576 }
577
578 int CCECCommandHandler::HandleSystemAudioModeRequest(const cec_command &command)
579 {
580 if (m_processor->CECInitialised() && m_processor->IsHandledByLibCEC(command.destination))
581 {
582 CCECAudioSystem *device = CCECBusDevice::AsAudioSystem(GetDevice(command.destination));
583 if (device)
584 {
585 if (command.parameters.size >= 2)
586 {
587 device->SetPowerStatus(CEC_POWER_STATUS_ON);
588 device->SetSystemAudioModeStatus(CEC_SYSTEM_AUDIO_STATUS_ON);
589 uint16_t iNewAddress = ((uint16_t)command.parameters[0] << 8) | ((uint16_t)command.parameters[1]);
590 CCECBusDevice *newActiveDevice = GetDeviceByPhysicalAddress(iNewAddress);
591 if (newActiveDevice)
592 newActiveDevice->MarkAsActiveSource();
593 if (device->TransmitSetSystemAudioMode(command.initiator))
594 return COMMAND_HANDLED;
595 }
596 else
597 {
598 device->SetSystemAudioModeStatus(CEC_SYSTEM_AUDIO_STATUS_OFF);
599 if (device->TransmitSetSystemAudioMode(command.initiator))
600 return COMMAND_HANDLED;
601 }
602 }
603 }
604
605 return CEC_ABORT_REASON_NOT_IN_CORRECT_MODE_TO_RESPOND;
606 }
607
608 int CCECCommandHandler::HandleStandby(const cec_command &command)
609 {
610 CCECBusDevice *device = GetDevice(command.initiator);
611 if (device)
612 device->SetPowerStatus(CEC_POWER_STATUS_STANDBY);
613
614 return COMMAND_HANDLED;
615 }
616
617 int CCECCommandHandler::HandleSystemAudioModeStatus(const cec_command &command)
618 {
619 if (command.parameters.size == 1)
620 {
621 CCECAudioSystem *device = CCECBusDevice::AsAudioSystem(GetDevice(command.initiator));
622 if (device)
623 {
624 device->SetSystemAudioModeStatus((cec_system_audio_status)command.parameters[0]);
625 return COMMAND_HANDLED;
626 }
627 }
628
629 return CEC_ABORT_REASON_INVALID_OPERAND;
630 }
631
632 int CCECCommandHandler::HandleSetSystemAudioMode(const cec_command &command)
633 {
634 if (command.parameters.size == 1)
635 {
636 CCECAudioSystem *device = CCECBusDevice::AsAudioSystem(GetDevice(command.initiator));
637 if (device)
638 {
639 device->SetSystemAudioModeStatus((cec_system_audio_status)command.parameters[0]);
640 return COMMAND_HANDLED;
641 }
642 }
643
644 return CEC_ABORT_REASON_INVALID_OPERAND;
645 }
646
647 int CCECCommandHandler::HandleTextViewOn(const cec_command &command)
648 {
649 m_processor->GetDevice(command.initiator)->MarkAsActiveSource();
650 return COMMAND_HANDLED;
651 }
652
653 int CCECCommandHandler::HandleUserControlPressed(const cec_command &command)
654 {
655 if (!m_processor->CECInitialised() ||
656 !m_processor->IsHandledByLibCEC(command.destination))
657 return CEC_ABORT_REASON_NOT_IN_CORRECT_MODE_TO_RESPOND;
658
659 if (command.parameters.size == 0)
660 return CEC_ABORT_REASON_INVALID_OPERAND;
661
662 CCECBusDevice *device = GetDevice(command.destination);
663 if (!device)
664 return CEC_ABORT_REASON_INVALID_OPERAND;
665
666 CCECClient *client = device->GetClient();
667 if (client)
668 client->AddKey();
669
670 if (command.parameters[0] <= CEC_USER_CONTROL_CODE_MAX)
671 client->SetCurrentButton((cec_user_control_code) command.parameters[0]);
672
673 if (command.parameters[0] == CEC_USER_CONTROL_CODE_POWER ||
674 command.parameters[0] == CEC_USER_CONTROL_CODE_POWER_ON_FUNCTION)
675 {
676 bool bPowerOn(true);
677
678 // CEC_USER_CONTROL_CODE_POWER operates as a toggle
679 // assume CEC_USER_CONTROL_CODE_POWER_ON_FUNCTION does not
680 if (command.parameters[0] == CEC_USER_CONTROL_CODE_POWER)
681 {
682 cec_power_status status = device->GetCurrentPowerStatus();
683 bPowerOn = !(status == CEC_POWER_STATUS_ON || status == CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON);
684 }
685
686 if (bPowerOn)
687 {
688 device->ActivateSource();
689 }
690 else
691 {
692 device->MarkAsInactiveSource();
693 device->TransmitInactiveSource();
694 device->SetMenuState(CEC_MENU_STATE_DEACTIVATED);
695 }
696 }
697
698 return COMMAND_HANDLED;
699 }
700
701 int CCECCommandHandler::HandleUserControlRelease(const cec_command &command)
702 {
703 if (!m_processor->CECInitialised() ||
704 !m_processor->IsHandledByLibCEC(command.destination))
705 return CEC_ABORT_REASON_NOT_IN_CORRECT_MODE_TO_RESPOND;
706
707 CCECClient *client = m_processor->GetClient(command.destination);
708 if (client)
709 client->AddKey();
710
711 return COMMAND_HANDLED;
712 }
713
714 int CCECCommandHandler::HandleVendorCommand(const cec_command & UNUSED(command))
715 {
716 return CEC_ABORT_REASON_REFUSED;
717 }
718
719 void CCECCommandHandler::UnhandledCommand(const cec_command &command, const cec_abort_reason reason)
720 {
721 if (m_processor->IsHandledByLibCEC(command.destination))
722 {
723 LIB_CEC->AddLog(CEC_LOG_DEBUG, "sending abort with opcode %02x and reason '%s' to %s", command.opcode, ToString(reason), ToString(command.initiator));
724 m_processor->TransmitAbort(command.destination, command.initiator, command.opcode, reason);
725 }
726 }
727
728 size_t CCECCommandHandler::GetMyDevices(vector<CCECBusDevice *> &devices) const
729 {
730 size_t iReturn(0);
731
732 cec_logical_addresses addresses = m_processor->GetLogicalAddresses();
733 for (uint8_t iPtr = CECDEVICE_TV; iPtr < CECDEVICE_BROADCAST; iPtr++)
734 {
735 if (addresses[iPtr])
736 {
737 devices.push_back(GetDevice((cec_logical_address) iPtr));
738 ++iReturn;
739 }
740 }
741
742 return iReturn;
743 }
744
745 CCECBusDevice *CCECCommandHandler::GetDevice(cec_logical_address iLogicalAddress) const
746 {
747 return m_processor->GetDevice(iLogicalAddress);
748 }
749
750 CCECBusDevice *CCECCommandHandler::GetDeviceByPhysicalAddress(uint16_t iPhysicalAddress) const
751 {
752 return m_processor->GetDeviceByPhysicalAddress(iPhysicalAddress);
753 }
754
755 bool CCECCommandHandler::SetVendorId(const cec_command &command)
756 {
757 bool bChanged(false);
758 if (command.parameters.size < 3)
759 {
760 LIB_CEC->AddLog(CEC_LOG_WARNING, "invalid vendor ID received");
761 return bChanged;
762 }
763
764 uint64_t iVendorId = ((uint64_t)command.parameters[0] << 16) +
765 ((uint64_t)command.parameters[1] << 8) +
766 (uint64_t)command.parameters[2];
767
768 CCECBusDevice *device = GetDevice((cec_logical_address) command.initiator);
769 if (device)
770 bChanged = device->SetVendorId(iVendorId);
771 return bChanged;
772 }
773
774 void CCECCommandHandler::SetPhysicalAddress(cec_logical_address iAddress, uint16_t iNewAddress)
775 {
776 if (!m_processor->IsHandledByLibCEC(iAddress))
777 {
778 CCECBusDevice *otherDevice = m_processor->GetDeviceByPhysicalAddress(iNewAddress);
779 CCECClient *client = otherDevice ? otherDevice->GetClient() : NULL;
780
781 CCECBusDevice *device = m_processor->GetDevice(iAddress);
782 if (device)
783 device->SetPhysicalAddress(iNewAddress);
784 else
785 {
786 LIB_CEC->AddLog(CEC_LOG_DEBUG, "device with logical address %X not found", iAddress);
787 }
788
789 /* another device reported the same physical address as ours */
790 if (client)
791 client->ResetPhysicalAddress();
792 }
793 else
794 {
795 LIB_CEC->AddLog(CEC_LOG_DEBUG, "ignore physical address report for device %s (%X) because it's marked as handled by libCEC", ToString(iAddress), iAddress);
796 }
797 }
798
799 bool CCECCommandHandler::PowerOn(const cec_logical_address iInitiator, const cec_logical_address iDestination)
800 {
801 if (iDestination == CECDEVICE_TV)
802 return TransmitImageViewOn(iInitiator, iDestination);
803
804 return TransmitKeypress(iInitiator, iDestination, CEC_USER_CONTROL_CODE_POWER) &&
805 TransmitKeyRelease(iInitiator, iDestination);
806 }
807
808 bool CCECCommandHandler::TransmitImageViewOn(const cec_logical_address iInitiator, const cec_logical_address iDestination)
809 {
810 cec_command command;
811 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_IMAGE_VIEW_ON);
812
813 return Transmit(command);
814 }
815
816 bool CCECCommandHandler::TransmitStandby(const cec_logical_address iInitiator, const cec_logical_address iDestination)
817 {
818 cec_command command;
819 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_STANDBY);
820
821 return Transmit(command);
822 }
823
824 bool CCECCommandHandler::TransmitRequestActiveSource(const cec_logical_address iInitiator, bool bWaitForResponse /* = true */)
825 {
826 cec_command command;
827 cec_command::Format(command, iInitiator, CECDEVICE_BROADCAST, CEC_OPCODE_REQUEST_ACTIVE_SOURCE);
828
829 return Transmit(command, !bWaitForResponse);
830 }
831
832 bool CCECCommandHandler::TransmitRequestCecVersion(const cec_logical_address iInitiator, const cec_logical_address iDestination, bool bWaitForResponse /* = true */)
833 {
834 cec_command command;
835 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_GET_CEC_VERSION);
836
837 return Transmit(command, !bWaitForResponse);
838 }
839
840 bool CCECCommandHandler::TransmitRequestMenuLanguage(const cec_logical_address iInitiator, const cec_logical_address iDestination, bool bWaitForResponse /* = true */)
841 {
842 cec_command command;
843 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_GET_MENU_LANGUAGE);
844
845 return Transmit(command, !bWaitForResponse);
846 }
847
848 bool CCECCommandHandler::TransmitRequestOSDName(const cec_logical_address iInitiator, const cec_logical_address iDestination, bool bWaitForResponse /* = true */)
849 {
850 cec_command command;
851 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_GIVE_OSD_NAME);
852
853 return Transmit(command, !bWaitForResponse);
854 }
855
856 bool CCECCommandHandler::TransmitRequestPhysicalAddress(const cec_logical_address iInitiator, const cec_logical_address iDestination, bool bWaitForResponse /* = true */)
857 {
858 cec_command command;
859 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_GIVE_PHYSICAL_ADDRESS);
860
861 return Transmit(command, !bWaitForResponse);
862 }
863
864 bool CCECCommandHandler::TransmitRequestPowerStatus(const cec_logical_address iInitiator, const cec_logical_address iDestination, bool bWaitForResponse /* = true */)
865 {
866 cec_command command;
867 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_GIVE_DEVICE_POWER_STATUS);
868
869 return Transmit(command, !bWaitForResponse);
870 }
871
872 bool CCECCommandHandler::TransmitRequestVendorId(const cec_logical_address iInitiator, const cec_logical_address iDestination, bool bWaitForResponse /* = true */)
873 {
874 cec_command command;
875 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_GIVE_DEVICE_VENDOR_ID);
876
877 return Transmit(command, !bWaitForResponse);
878 }
879
880 bool CCECCommandHandler::TransmitActiveSource(const cec_logical_address iInitiator, uint16_t iPhysicalAddress)
881 {
882 cec_command command;
883 cec_command::Format(command, iInitiator, CECDEVICE_BROADCAST, CEC_OPCODE_ACTIVE_SOURCE);
884 command.parameters.PushBack((uint8_t) ((iPhysicalAddress >> 8) & 0xFF));
885 command.parameters.PushBack((uint8_t) (iPhysicalAddress & 0xFF));
886
887 return Transmit(command);
888 }
889
890 bool CCECCommandHandler::TransmitCECVersion(const cec_logical_address iInitiator, const cec_logical_address iDestination, cec_version cecVersion)
891 {
892 cec_command command;
893 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_CEC_VERSION);
894 command.parameters.PushBack((uint8_t)cecVersion);
895
896 return Transmit(command);
897 }
898
899 bool CCECCommandHandler::TransmitInactiveSource(const cec_logical_address iInitiator, uint16_t iPhysicalAddress)
900 {
901 cec_command command;
902 cec_command::Format(command, iInitiator, CECDEVICE_TV, CEC_OPCODE_INACTIVE_SOURCE);
903 command.parameters.PushBack((iPhysicalAddress >> 8) & 0xFF);
904 command.parameters.PushBack(iPhysicalAddress & 0xFF);
905
906 return Transmit(command);
907 }
908
909 bool CCECCommandHandler::TransmitMenuState(const cec_logical_address iInitiator, const cec_logical_address iDestination, cec_menu_state menuState)
910 {
911 cec_command command;
912 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_MENU_STATUS);
913 command.parameters.PushBack((uint8_t)menuState);
914
915 return Transmit(command);
916 }
917
918 bool CCECCommandHandler::TransmitOSDName(const cec_logical_address iInitiator, const cec_logical_address iDestination, CStdString strDeviceName)
919 {
920 cec_command command;
921 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_SET_OSD_NAME);
922 for (size_t iPtr = 0; iPtr < strDeviceName.length(); iPtr++)
923 command.parameters.PushBack(strDeviceName.at(iPtr));
924
925 return Transmit(command);
926 }
927
928 bool CCECCommandHandler::TransmitOSDString(const cec_logical_address iInitiator, const cec_logical_address iDestination, cec_display_control duration, const char *strMessage)
929 {
930 cec_command command;
931 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_SET_OSD_STRING);
932 command.parameters.PushBack((uint8_t)duration);
933
934 size_t iLen = strlen(strMessage);
935 if (iLen > 13) iLen = 13;
936
937 for (size_t iPtr = 0; iPtr < iLen; iPtr++)
938 command.parameters.PushBack(strMessage[iPtr]);
939
940 return Transmit(command);
941 }
942
943 bool CCECCommandHandler::TransmitPhysicalAddress(const cec_logical_address iInitiator, uint16_t iPhysicalAddress, cec_device_type type)
944 {
945 cec_command command;
946 cec_command::Format(command, iInitiator, CECDEVICE_BROADCAST, CEC_OPCODE_REPORT_PHYSICAL_ADDRESS);
947 command.parameters.PushBack((uint8_t) ((iPhysicalAddress >> 8) & 0xFF));
948 command.parameters.PushBack((uint8_t) (iPhysicalAddress & 0xFF));
949 command.parameters.PushBack((uint8_t) (type));
950
951 return Transmit(command);
952 }
953
954 bool CCECCommandHandler::TransmitSetMenuLanguage(const cec_logical_address iInitiator, const char lang[3])
955 {
956 cec_command command;
957 command.Format(command, iInitiator, CECDEVICE_BROADCAST, CEC_OPCODE_SET_MENU_LANGUAGE);
958 command.parameters.PushBack((uint8_t) lang[0]);
959 command.parameters.PushBack((uint8_t) lang[1]);
960 command.parameters.PushBack((uint8_t) lang[2]);
961
962 return Transmit(command);
963 }
964
965 bool CCECCommandHandler::TransmitPoll(const cec_logical_address iInitiator, const cec_logical_address iDestination)
966 {
967 cec_command command;
968 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_NONE);
969
970 return Transmit(command);
971 }
972
973 bool CCECCommandHandler::TransmitPowerState(const cec_logical_address iInitiator, const cec_logical_address iDestination, cec_power_status state)
974 {
975 cec_command command;
976 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_REPORT_POWER_STATUS);
977 command.parameters.PushBack((uint8_t) state);
978
979 return Transmit(command);
980 }
981
982 bool CCECCommandHandler::TransmitVendorID(const cec_logical_address iInitiator, uint64_t iVendorId)
983 {
984 cec_command command;
985 cec_command::Format(command, iInitiator, CECDEVICE_BROADCAST, CEC_OPCODE_DEVICE_VENDOR_ID);
986
987 command.parameters.PushBack((uint8_t) (((uint64_t)iVendorId >> 16) & 0xFF));
988 command.parameters.PushBack((uint8_t) (((uint64_t)iVendorId >> 8) & 0xFF));
989 command.parameters.PushBack((uint8_t) ((uint64_t)iVendorId & 0xFF));
990
991 return Transmit(command);
992 }
993
994 bool CCECCommandHandler::TransmitAudioStatus(const cec_logical_address iInitiator, const cec_logical_address iDestination, uint8_t state)
995 {
996 cec_command command;
997 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_REPORT_AUDIO_STATUS);
998 command.parameters.PushBack(state);
999
1000 return Transmit(command);
1001 }
1002
1003 bool CCECCommandHandler::TransmitSetSystemAudioMode(const cec_logical_address iInitiator, const cec_logical_address iDestination, cec_system_audio_status state)
1004 {
1005 cec_command command;
1006 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_SET_SYSTEM_AUDIO_MODE);
1007 command.parameters.PushBack((uint8_t)state);
1008
1009 return Transmit(command);
1010 }
1011
1012 bool CCECCommandHandler::TransmitSetStreamPath(uint16_t iStreamPath)
1013 {
1014 cec_command command;
1015 cec_command::Format(command, m_busDevice->GetLogicalAddress(), CECDEVICE_BROADCAST, CEC_OPCODE_SET_STREAM_PATH);
1016 command.parameters.PushBack((uint8_t) ((iStreamPath >> 8) & 0xFF));
1017 command.parameters.PushBack((uint8_t) (iStreamPath & 0xFF));
1018
1019 return Transmit(command);
1020 }
1021
1022 bool CCECCommandHandler::TransmitSystemAudioModeStatus(const cec_logical_address iInitiator, const cec_logical_address iDestination, cec_system_audio_status state)
1023 {
1024 cec_command command;
1025 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_SYSTEM_AUDIO_MODE_STATUS);
1026 command.parameters.PushBack((uint8_t)state);
1027
1028 return Transmit(command);
1029 }
1030
1031 bool CCECCommandHandler::TransmitDeckStatus(const cec_logical_address iInitiator, const cec_logical_address iDestination, cec_deck_info state)
1032 {
1033 cec_command command;
1034 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_DECK_STATUS);
1035 command.PushBack((uint8_t)state);
1036
1037 return Transmit(command);
1038 }
1039
1040 bool CCECCommandHandler::TransmitKeypress(const cec_logical_address iInitiator, const cec_logical_address iDestination, cec_user_control_code key, bool bWait /* = true */)
1041 {
1042 cec_command command;
1043 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_USER_CONTROL_PRESSED);
1044 command.parameters.PushBack((uint8_t)key);
1045
1046 return Transmit(command, !bWait);
1047 }
1048
1049 bool CCECCommandHandler::TransmitKeyRelease(const cec_logical_address iInitiator, const cec_logical_address iDestination, bool bWait /* = true */)
1050 {
1051 cec_command command;
1052 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_USER_CONTROL_RELEASE);
1053
1054 return Transmit(command, !bWait);
1055 }
1056
1057 bool CCECCommandHandler::Transmit(cec_command &command, bool bSuppressWait /* = false */)
1058 {
1059 bool bReturn(false);
1060 cec_opcode expectedResponse(cec_command::GetResponseOpcode(command.opcode));
1061 bool bExpectResponse(expectedResponse != CEC_OPCODE_NONE && !bSuppressWait);
1062 command.transmit_timeout = m_iTransmitTimeout;
1063
1064 if (command.initiator == CECDEVICE_UNKNOWN)
1065 {
1066 LIB_CEC->AddLog(CEC_LOG_ERROR, "not transmitting a command without a valid initiator");
1067 return bReturn;
1068 }
1069
1070 {
1071 uint8_t iTries(0), iMaxTries(!command.opcode_set ? 1 : m_iTransmitRetries + 1);
1072 while (!bReturn && ++iTries <= iMaxTries && !m_busDevice->IsUnsupportedFeature(command.opcode))
1073 {
1074 if ((bReturn = m_processor->Transmit(command)) == true)
1075 {
1076 LIB_CEC->AddLog(CEC_LOG_DEBUG, "command transmitted");
1077 if (bExpectResponse)
1078 {
1079 bReturn = m_waitForResponse->Wait(expectedResponse);
1080 LIB_CEC->AddLog(CEC_LOG_DEBUG, bReturn ? "expected response received (%X: %s)" : "expected response not received (%X: %s)", (int)expectedResponse, ToString(expectedResponse));
1081 }
1082 }
1083 }
1084 }
1085
1086 return bReturn;
1087 }
1088
1089 bool CCECCommandHandler::ActivateSource(void)
1090 {
1091 if (m_busDevice->IsActiveSource() &&
1092 m_busDevice->IsHandledByLibCEC())
1093 {
1094 {
1095 CLockObject lock(m_mutex);
1096 m_bActiveSourcePending = false;
1097 }
1098
1099 m_busDevice->SetPowerStatus(CEC_POWER_STATUS_ON);
1100 m_busDevice->SetMenuState(CEC_MENU_STATE_ACTIVATED);
1101
1102 bool bActiveSourceFailed = !m_busDevice->TransmitImageViewOn() ||
1103 !m_busDevice->TransmitActiveSource() ||
1104 !m_busDevice->TransmitMenuState(CECDEVICE_TV);
1105
1106 if (!bActiveSourceFailed)
1107 {
1108 CCECPlaybackDevice *playbackDevice = m_busDevice->AsPlaybackDevice();
1109 if (playbackDevice && SendDeckStatusUpdateOnActiveSource())
1110 bActiveSourceFailed = !playbackDevice->TransmitDeckStatus(CECDEVICE_TV);
1111 }
1112
1113 if (bActiveSourceFailed)
1114 {
1115 LIB_CEC->AddLog(CEC_LOG_DEBUG, "failed to make '%s' the active source. will retry later", m_busDevice->GetLogicalAddressName());
1116 CLockObject lock(m_mutex);
1117 m_bActiveSourcePending = true;
1118 return false;
1119 }
1120
1121 m_bHandlerInited = true;
1122 }
1123 return true;
1124 }
1125
1126 void CCECCommandHandler::SignalOpcode(cec_opcode opcode)
1127 {
1128 m_waitForResponse->Received(opcode);
1129 }
1130
1131 bool CCECCommandHandler::ActiveSourcePending(void)
1132 {
1133 CLockObject lock(m_mutex);
1134 return m_bActiveSourcePending;
1135 }