cec: don't send out system audio mode statuses when we're not an audio device
[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 "../devices/CECPlaybackDevice.h"
37 #include "../CECProcessor.h"
38
39 using namespace CEC;
40 using namespace std;
41
42 CCECCommandHandler::CCECCommandHandler(CCECBusDevice *busDevice)
43 {
44 m_busDevice = busDevice;
45 }
46
47 bool CCECCommandHandler::HandleCommand(const cec_command &command)
48 {
49 bool bHandled(true);
50
51 CStdString strLog;
52 strLog.Format(">> %s (%X) -> %s (%X): %s (%2X)", m_busDevice->GetProcessor()->ToString(command.initiator), command.initiator, m_busDevice->GetProcessor()->ToString(command.destination), command.destination, m_busDevice->GetProcessor()->ToString(command.opcode), command.opcode);
53 m_busDevice->AddLog(CEC_LOG_NOTICE, strLog);
54
55 switch(command.opcode)
56 {
57 case CEC_OPCODE_REPORT_POWER_STATUS:
58 HandleReportPowerStatus(command);
59 break;
60 case CEC_OPCODE_CEC_VERSION:
61 HandleDeviceCecVersion(command);
62 break;
63 case CEC_OPCODE_SET_MENU_LANGUAGE:
64 HandleSetMenuLanguage(command);
65 break;
66 case CEC_OPCODE_GIVE_PHYSICAL_ADDRESS:
67 HandleGivePhysicalAddress(command);
68 break;
69 case CEC_OPCODE_GIVE_OSD_NAME:
70 HandleGiveOSDName(command);
71 break;
72 case CEC_OPCODE_GIVE_DEVICE_VENDOR_ID:
73 HandleGiveDeviceVendorId(command);
74 break;
75 case CEC_OPCODE_DEVICE_VENDOR_ID:
76 HandleDeviceVendorId(command);
77 break;
78 case CEC_OPCODE_VENDOR_COMMAND_WITH_ID:
79 HandleDeviceVendorCommandWithId(command);
80 break;
81 case CEC_OPCODE_GIVE_DECK_STATUS:
82 HandleGiveDeckStatus(command);
83 break;
84 case CEC_OPCODE_DECK_CONTROL:
85 HandleDeckControl(command);
86 break;
87 case CEC_OPCODE_MENU_REQUEST:
88 HandleMenuRequest(command);
89 break;
90 case CEC_OPCODE_GIVE_DEVICE_POWER_STATUS:
91 HandleGiveDevicePowerStatus(command);
92 break;
93 case CEC_OPCODE_GET_CEC_VERSION:
94 HandleGetCecVersion(command);
95 break;
96 case CEC_OPCODE_USER_CONTROL_PRESSED:
97 HandleUserControlPressed(command);
98 break;
99 case CEC_OPCODE_USER_CONTROL_RELEASE:
100 HandleUserControlRelease(command);
101 break;
102 case CEC_OPCODE_GIVE_AUDIO_STATUS:
103 HandleGiveAudioStatus(command);
104 break;
105 case CEC_OPCODE_GIVE_SYSTEM_AUDIO_MODE_STATUS:
106 HandleGiveSystemAudioModeStatus(command);
107 break;
108 case CEC_OPCODE_SYSTEM_AUDIO_MODE_REQUEST:
109 HandleSetSystemAudioModeRequest(command);
110 break;
111 case CEC_OPCODE_REQUEST_ACTIVE_SOURCE:
112 HandleRequestActiveSource(command);
113 break;
114 case CEC_OPCODE_SET_STREAM_PATH:
115 HandleSetStreamPath(command);
116 break;
117 case CEC_OPCODE_ROUTING_CHANGE:
118 HandleRoutingChange(command);
119 break;
120 case CEC_OPCODE_ROUTING_INFORMATION:
121 HandleRoutingInformation(command);
122 break;
123 case CEC_OPCODE_STANDBY:
124 HandleStandby(command);
125 break;
126 case CEC_OPCODE_ACTIVE_SOURCE:
127 HandleActiveSource(command);
128 break;
129 case CEC_OPCODE_REPORT_PHYSICAL_ADDRESS:
130 HandleReportPhysicalAddress(command);
131 break;
132 case CEC_OPCODE_REPORT_AUDIO_STATUS:
133 HandleReportAudioStatus(command);
134 break;
135 case CEC_OPCODE_SYSTEM_AUDIO_MODE_STATUS:
136 HandleSystemAudioStatus(command);
137 break;
138 case CEC_OPCODE_SET_OSD_NAME:
139 HandleSetOSDName(command);
140 break;
141 case CEC_OPCODE_IMAGE_VIEW_ON:
142 HandleImageViewOn(command);
143 break;
144 case CEC_OPCODE_TEXT_VIEW_ON:
145 HandleTextViewOn(command);
146 break;
147 default:
148 UnhandledCommand(command);
149 bHandled = false;
150 break;
151 }
152
153 m_busDevice->GetProcessor()->AddCommand(command);
154 return bHandled;
155 }
156
157 bool CCECCommandHandler::HandleActiveSource(const cec_command &command)
158 {
159 if (command.parameters.size == 2)
160 {
161 uint16_t iAddress = ((uint16_t)command.parameters[0] << 8) | ((uint16_t)command.parameters[1]);
162 return m_busDevice->GetProcessor()->SetStreamPath(iAddress);
163 }
164
165 return true;
166 }
167
168 bool CCECCommandHandler::HandleDeckControl(const cec_command &command)
169 {
170 CCECBusDevice *device = GetDevice(command.destination);
171 if (device && (device->GetType() == CEC_DEVICE_TYPE_PLAYBACK_DEVICE || device->GetType() == CEC_DEVICE_TYPE_RECORDING_DEVICE) && command.parameters.size > 0)
172 {
173 ((CCECPlaybackDevice *) device)->SetDeckControlMode((cec_deck_control_mode) command.parameters[0]);
174 return true;
175 }
176
177 return false;
178 }
179
180 bool CCECCommandHandler::HandleDeviceCecVersion(const cec_command &command)
181 {
182 if (command.parameters.size == 1)
183 {
184 CCECBusDevice *device = GetDevice(command.initiator);
185 if (device)
186 device->SetCecVersion((cec_version) command.parameters[0]);
187 }
188
189 return true;
190 }
191
192 bool CCECCommandHandler::HandleDeviceVendorCommandWithId(const cec_command &command)
193 {
194 if (m_busDevice->MyLogicalAddressContains(command.destination))
195 m_busDevice->GetProcessor()->TransmitAbort(command.initiator, command.opcode, CEC_ABORT_REASON_REFUSED);
196
197 return true;
198 }
199
200 bool CCECCommandHandler::HandleDeviceVendorId(const cec_command &command)
201 {
202 SetVendorId(command);
203 return true;
204 }
205
206 bool CCECCommandHandler::HandleGetCecVersion(const cec_command &command)
207 {
208 if (m_busDevice->MyLogicalAddressContains(command.destination))
209 {
210 CCECBusDevice *device = GetDevice(command.destination);
211 if (device)
212 return device->TransmitCECVersion(command.initiator);
213 }
214
215 return false;
216 }
217
218 bool CCECCommandHandler::HandleGiveAudioStatus(const cec_command &command)
219 {
220 if (m_busDevice->MyLogicalAddressContains(command.destination))
221 {
222 CCECBusDevice *device = GetDevice(command.destination);
223 if (device && device->GetType() == CEC_DEVICE_TYPE_AUDIO_SYSTEM)
224 return ((CCECAudioSystem *) device)->TransmitAudioStatus(command.initiator);
225 }
226
227 return false;
228 }
229
230 bool CCECCommandHandler::HandleGiveDeckStatus(const cec_command &command)
231 {
232 if (m_busDevice->MyLogicalAddressContains(command.destination))
233 {
234 CCECBusDevice *device = GetDevice(command.destination);
235 if (device && (device->GetType() == CEC_DEVICE_TYPE_PLAYBACK_DEVICE || device->GetType() == CEC_DEVICE_TYPE_RECORDING_DEVICE))
236 return ((CCECPlaybackDevice *) device)->TransmitDeckStatus(command.initiator);
237 }
238
239 return false;
240 }
241
242 bool CCECCommandHandler::HandleGiveDevicePowerStatus(const cec_command &command)
243 {
244 if (m_busDevice->MyLogicalAddressContains(command.destination))
245 {
246 CCECBusDevice *device = GetDevice(command.destination);
247 if (device)
248 return device->TransmitPowerState(command.initiator);
249 }
250
251 return false;
252 }
253
254 bool CCECCommandHandler::HandleGiveDeviceVendorId(const cec_command &command)
255 {
256 if (m_busDevice->MyLogicalAddressContains(command.destination))
257 {
258 CCECBusDevice *device = GetDevice(command.destination);
259 if (device)
260 return device->TransmitVendorID(command.initiator);
261 }
262
263 return false;
264 }
265
266 bool CCECCommandHandler::HandleGiveOSDName(const cec_command &command)
267 {
268 if (m_busDevice->MyLogicalAddressContains(command.destination))
269 {
270 CCECBusDevice *device = GetDevice(command.destination);
271 if (device)
272 return device->TransmitOSDName(command.initiator);
273 }
274
275 return false;
276 }
277
278 bool CCECCommandHandler::HandleGivePhysicalAddress(const cec_command &command)
279 {
280 if (m_busDevice->MyLogicalAddressContains(command.destination))
281 {
282 CCECBusDevice *device = GetDevice(command.destination);
283 if (device)
284 return device->TransmitPhysicalAddress();
285 }
286
287 return false;
288 }
289
290 bool CCECCommandHandler::HandleGiveSystemAudioModeStatus(const cec_command &command)
291 {
292 if (m_busDevice->MyLogicalAddressContains(command.destination))
293 {
294 CCECBusDevice *device = GetDevice(command.destination);
295 if (device && device->GetType() == CEC_DEVICE_TYPE_AUDIO_SYSTEM)
296 return ((CCECAudioSystem *) device)->TransmitSystemAudioModeStatus(command.initiator);
297 }
298
299 return false;
300 }
301
302 bool CCECCommandHandler::HandleImageViewOn(const cec_command &command)
303 {
304 m_busDevice->GetProcessor()->SetActiveSource(command.initiator);
305 return true;
306 }
307
308 bool CCECCommandHandler::HandleMenuRequest(const cec_command &command)
309 {
310 if (m_busDevice->MyLogicalAddressContains(command.destination))
311 {
312 if (command.parameters[0] == CEC_MENU_REQUEST_TYPE_QUERY)
313 {
314 CCECBusDevice *device = GetDevice(command.destination);
315 if (device)
316 return device->TransmitMenuState(command.initiator);
317 }
318 }
319
320 return false;
321 }
322
323 bool CCECCommandHandler::HandleReportAudioStatus(const cec_command &command)
324 {
325 if (command.parameters.size == 1)
326 {
327 CCECBusDevice *device = GetDevice(command.initiator);
328 if (device && device->GetType() == CEC_DEVICE_TYPE_AUDIO_SYSTEM)
329 {
330 ((CCECAudioSystem *)device)->SetAudioStatus(command.parameters[0]);
331 return true;
332 }
333 }
334 return false;
335 }
336
337 bool CCECCommandHandler::HandleReportPhysicalAddress(const cec_command &command)
338 {
339 if (command.parameters.size == 3)
340 {
341 uint16_t iNewAddress = ((uint16_t)command.parameters[0] << 8) | ((uint16_t)command.parameters[1]);
342 SetPhysicalAddress(command.initiator, iNewAddress);
343 }
344 return true;
345 }
346
347 bool CCECCommandHandler::HandleReportPowerStatus(const cec_command &command)
348 {
349 if (command.parameters.size == 1)
350 {
351 CCECBusDevice *device = GetDevice(command.initiator);
352 if (device)
353 device->SetPowerStatus((cec_power_status) command.parameters[0]);
354 }
355 return true;
356 }
357
358 bool CCECCommandHandler::HandleRequestActiveSource(const cec_command &command)
359 {
360 CStdString strLog;
361 strLog.Format(">> %i requests active source", (uint8_t) command.initiator);
362 m_busDevice->AddLog(CEC_LOG_DEBUG, strLog.c_str());
363
364 vector<CCECBusDevice *> devices;
365 for (int iDevicePtr = (int)GetMyDevices(devices)-1; iDevicePtr >=0; iDevicePtr--)
366 devices[iDevicePtr]->TransmitActiveSource();
367
368 return true;
369 }
370
371 bool CCECCommandHandler::HandleRoutingChange(const cec_command &command)
372 {
373 if (command.parameters.size == 4)
374 {
375 uint16_t iOldAddress = ((uint16_t)command.parameters[0] << 8) | ((uint16_t)command.parameters[1]);
376 uint16_t iNewAddress = ((uint16_t)command.parameters[2] << 8) | ((uint16_t)command.parameters[3]);
377
378 CCECBusDevice *device = GetDevice(command.initiator);
379 if (device)
380 device->SetStreamPath(iNewAddress, iOldAddress);
381 }
382 return true;
383 }
384
385 bool CCECCommandHandler::HandleRoutingInformation(const cec_command &command)
386 {
387 if (command.parameters.size == 2)
388 {
389 uint16_t iNewAddress = ((uint16_t)command.parameters[0] << 8) | ((uint16_t)command.parameters[1]);
390 m_busDevice->GetProcessor()->SetStreamPath(iNewAddress);
391 }
392
393 return false;
394 }
395
396 bool CCECCommandHandler::HandleSetMenuLanguage(const cec_command &command)
397 {
398 if (command.parameters.size == 3)
399 {
400 CCECBusDevice *device = GetDevice(command.initiator);
401 if (device)
402 {
403 cec_menu_language language;
404 language.device = command.initiator;
405 for (uint8_t iPtr = 0; iPtr < 4; iPtr++)
406 language.language[iPtr] = command.parameters[iPtr];
407 language.language[3] = 0;
408 device->SetMenuLanguage(language);
409 return true;
410 }
411 }
412 return false;
413 }
414
415 bool CCECCommandHandler::HandleSetOSDName(const cec_command &command)
416 {
417 if (command.parameters.size > 0)
418 {
419 CCECBusDevice *device = GetDevice(command.initiator);
420 if (device)
421 {
422 char buf[1024];
423 for (uint8_t iPtr = 0; iPtr < command.parameters.size; iPtr++)
424 buf[iPtr] = (char)command.parameters[iPtr];
425 buf[command.parameters.size] = 0;
426
427 CStdString strName(buf);
428 device->SetOSDName(strName);
429
430 return true;
431 }
432 }
433 return false;
434 }
435
436 bool CCECCommandHandler::HandleSetStreamPath(const cec_command &command)
437 {
438 if (command.parameters.size >= 2)
439 {
440 uint16_t iStreamAddress = ((uint16_t)command.parameters[0] << 8) | ((uint16_t)command.parameters[1]);
441 CStdString strLog;
442 strLog.Format(">> %i sets stream path to physical address %04x", command.initiator, iStreamAddress);
443 m_busDevice->AddLog(CEC_LOG_DEBUG, strLog.c_str());
444
445 if (m_busDevice->GetProcessor()->SetStreamPath(iStreamAddress))
446 {
447 CCECBusDevice *device = GetDeviceByPhysicalAddress(iStreamAddress);
448 if (device)
449 {
450 return device->TransmitActiveSource() &&
451 device->TransmitMenuState(command.initiator);
452 }
453 }
454 }
455 return false;
456 }
457
458 bool CCECCommandHandler::HandleSetSystemAudioModeRequest(const cec_command &command)
459 {
460 if (m_busDevice->MyLogicalAddressContains(command.destination) && command.parameters.size >= 1)
461 {
462 CCECBusDevice *device = GetDevice(command.destination);
463 if (device&& device->GetType() == CEC_DEVICE_TYPE_AUDIO_SYSTEM)
464 return ((CCECAudioSystem *) device)->SetSystemAudioMode(command);
465 }
466 return false;
467 }
468
469 bool CCECCommandHandler::HandleStandby(const cec_command &command)
470 {
471 CCECBusDevice *device = GetDevice(command.initiator);
472 if (device)
473 device->SetPowerStatus(CEC_POWER_STATUS_STANDBY);
474
475 return true;
476 }
477
478 bool CCECCommandHandler::HandleSystemAudioStatus(const cec_command &command)
479 {
480 CCECBusDevice *device = GetDevice(command.initiator);
481 if (device && device->GetType() == CEC_DEVICE_TYPE_AUDIO_SYSTEM)
482 {
483 ((CCECAudioSystem *)device)->SetSystemAudioMode(command);
484 return true;
485 }
486
487 return false;
488 }
489
490 bool CCECCommandHandler::HandleTextViewOn(const cec_command &command)
491 {
492 m_busDevice->GetProcessor()->SetActiveSource(command.initiator);
493 return true;
494 }
495
496 bool CCECCommandHandler::HandleUserControlPressed(const cec_command &command)
497 {
498 if (m_busDevice->MyLogicalAddressContains(command.destination) && command.parameters.size > 0)
499 {
500 m_busDevice->GetProcessor()->AddKey();
501
502 if (command.parameters[0] <= CEC_USER_CONTROL_CODE_MAX)
503 {
504 CStdString strLog;
505 strLog.Format("key pressed: %x", command.parameters[0]);
506 m_busDevice->AddLog(CEC_LOG_DEBUG, strLog.c_str());
507
508 if (command.parameters[0] == CEC_USER_CONTROL_CODE_POWER ||
509 command.parameters[0] == CEC_USER_CONTROL_CODE_POWER_ON_FUNCTION)
510 {
511 CCECBusDevice *device = GetDevice(command.destination);
512 if (device)
513 device->SetPowerStatus(CEC_POWER_STATUS_ON);
514 }
515
516 m_busDevice->GetProcessor()->SetCurrentButton((cec_user_control_code) command.parameters[0]);
517 return true;
518 }
519 }
520 return false;
521 }
522
523 bool CCECCommandHandler::HandleUserControlRelease(const cec_command &command)
524 {
525 if (m_busDevice->MyLogicalAddressContains(command.destination))
526 m_busDevice->GetProcessor()->AddKey();
527
528 return true;
529 }
530
531 void CCECCommandHandler::UnhandledCommand(const cec_command &command)
532 {
533 CStdString strLog;
534 strLog.Format("unhandled command with opcode %02x from address %d", command.opcode, command.initiator);
535 m_busDevice->AddLog(CEC_LOG_DEBUG, strLog);
536 }
537
538 unsigned int CCECCommandHandler::GetMyDevices(vector<CCECBusDevice *> &devices) const
539 {
540 unsigned int iReturn(0);
541
542 cec_logical_addresses addresses = m_busDevice->GetProcessor()->GetLogicalAddresses();
543 for (uint8_t iPtr = 0; iPtr < 16; iPtr++)
544 {
545 if (addresses[iPtr])
546 {
547 devices.push_back(GetDevice((cec_logical_address) iPtr));
548 ++iReturn;
549 }
550 }
551
552 return iReturn;
553 }
554
555 CCECBusDevice *CCECCommandHandler::GetDevice(cec_logical_address iLogicalAddress) const
556 {
557 CCECBusDevice *device = NULL;
558
559 if (iLogicalAddress >= CECDEVICE_TV && iLogicalAddress <= CECDEVICE_BROADCAST)
560 device = m_busDevice->GetProcessor()->m_busDevices[iLogicalAddress];
561
562 return device;
563 }
564
565 CCECBusDevice *CCECCommandHandler::GetDeviceByPhysicalAddress(uint16_t iPhysicalAddress) const
566 {
567 return m_busDevice->GetProcessor()->GetDeviceByPhysicalAddress(iPhysicalAddress);
568 }
569
570 CCECBusDevice *CCECCommandHandler::GetDeviceByType(cec_device_type type) const
571 {
572 return m_busDevice->GetProcessor()->GetDeviceByType(type);
573 }
574
575 void CCECCommandHandler::SetVendorId(const cec_command &command)
576 {
577 if (command.parameters.size < 3)
578 {
579 m_busDevice->AddLog(CEC_LOG_WARNING, "invalid vendor ID received");
580 return;
581 }
582
583 uint64_t iVendorId = ((uint64_t)command.parameters[0] << 16) +
584 ((uint64_t)command.parameters[1] << 8) +
585 (uint64_t)command.parameters[2];
586
587 CCECBusDevice *device = GetDevice((cec_logical_address) command.initiator);
588 if (device)
589 device->SetVendorId(iVendorId);
590 }
591
592 void CCECCommandHandler::SetPhysicalAddress(cec_logical_address iAddress, uint16_t iNewAddress)
593 {
594 if (!m_busDevice->MyLogicalAddressContains(iAddress))
595 {
596 bool bOurAddress(m_busDevice->GetProcessor()->GetPhysicalAddress() == iNewAddress);
597 GetDevice(iAddress)->SetPhysicalAddress(iNewAddress);
598 if (bOurAddress)
599 {
600 /* another device reported the same physical address as ours
601 * since we don't have physical address detection yet, we'll just use the
602 * given address, increased by 0x100 for now */
603 m_busDevice->GetProcessor()->SetPhysicalAddress(iNewAddress + 0x100);
604 }
605 }
606 }