cec: refactor CEC read/write. keep a single lock for all writes, not one per device
[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"
28089abc 36#include "../devices/CECPlaybackDevice.h"
387b6f6f 37#include "../CECProcessor.h"
e9de9629
LOK
38
39using namespace CEC;
8747dd4f 40using namespace std;
e9de9629 41
8fa35473
LOK
42CCECCommandHandler::CCECCommandHandler(CCECBusDevice *busDevice) :
43 m_busDevice(busDevice),
44 m_processor(m_busDevice->GetProcessor()),
45 m_iTransmitTimeout(1000)
e9de9629 46{
8fa35473
LOK
47}
48
49CCECCommandHandler::~CCECCommandHandler(void)
50{
51 m_condition.Broadcast();
e9de9629
LOK
52}
53
54bool CCECCommandHandler::HandleCommand(const cec_command &command)
55{
8fa35473 56 bool bHandled(true), bHandlerChanged(false);
e9de9629 57
5e822b09 58 CStdString strLog;
fcf10e27 59 strLog.Format(">> %s (%X) -> %s (%X): %s (%2X)", m_processor->ToString(command.initiator), command.initiator, m_processor->ToString(command.destination), command.destination, m_processor->ToString(command.opcode), command.opcode);
5e822b09
LOK
60 m_busDevice->AddLog(CEC_LOG_NOTICE, strLog);
61
855a3a98
LOK
62 m_processor->AddCommand(command);
63
6b72afcd 64 switch(command.opcode)
e9de9629 65 {
6b72afcd
LOK
66 case CEC_OPCODE_REPORT_POWER_STATUS:
67 HandleReportPowerStatus(command);
68 break;
69 case CEC_OPCODE_CEC_VERSION:
70 HandleDeviceCecVersion(command);
71 break;
72 case CEC_OPCODE_SET_MENU_LANGUAGE:
73 HandleSetMenuLanguage(command);
74 break;
75 case CEC_OPCODE_GIVE_PHYSICAL_ADDRESS:
76 HandleGivePhysicalAddress(command);
77 break;
78 case CEC_OPCODE_GIVE_OSD_NAME:
79 HandleGiveOSDName(command);
80 break;
81 case CEC_OPCODE_GIVE_DEVICE_VENDOR_ID:
82 HandleGiveDeviceVendorId(command);
83 break;
84 case CEC_OPCODE_DEVICE_VENDOR_ID:
8fa35473 85 bHandlerChanged = HandleDeviceVendorId(command);
6b72afcd
LOK
86 break;
87 case CEC_OPCODE_VENDOR_COMMAND_WITH_ID:
88 HandleDeviceVendorCommandWithId(command);
89 break;
90 case CEC_OPCODE_GIVE_DECK_STATUS:
91 HandleGiveDeckStatus(command);
92 break;
93 case CEC_OPCODE_DECK_CONTROL:
94 HandleDeckControl(command);
95 break;
96 case CEC_OPCODE_MENU_REQUEST:
97 HandleMenuRequest(command);
98 break;
99 case CEC_OPCODE_GIVE_DEVICE_POWER_STATUS:
100 HandleGiveDevicePowerStatus(command);
101 break;
102 case CEC_OPCODE_GET_CEC_VERSION:
103 HandleGetCecVersion(command);
104 break;
105 case CEC_OPCODE_USER_CONTROL_PRESSED:
106 HandleUserControlPressed(command);
107 break;
108 case CEC_OPCODE_USER_CONTROL_RELEASE:
109 HandleUserControlRelease(command);
110 break;
111 case CEC_OPCODE_GIVE_AUDIO_STATUS:
112 HandleGiveAudioStatus(command);
113 break;
114 case CEC_OPCODE_GIVE_SYSTEM_AUDIO_MODE_STATUS:
115 HandleGiveSystemAudioModeStatus(command);
116 break;
117 case CEC_OPCODE_SYSTEM_AUDIO_MODE_REQUEST:
aa517a0d
LOK
118 HandleSystemAudioModeRequest(command);
119 break;
120 case CEC_OPCODE_REPORT_AUDIO_STATUS:
855a3a98 121 HandleReportAudioStatus(command);
aa517a0d
LOK
122 break;
123 case CEC_OPCODE_SYSTEM_AUDIO_MODE_STATUS:
855a3a98 124 HandleSystemAudioModeStatus(command);
aa517a0d
LOK
125 break;
126 case CEC_OPCODE_SET_SYSTEM_AUDIO_MODE:
855a3a98 127 HandleSetSystemAudioMode(command);
6b72afcd
LOK
128 break;
129 case CEC_OPCODE_REQUEST_ACTIVE_SOURCE:
130 HandleRequestActiveSource(command);
131 break;
132 case CEC_OPCODE_SET_STREAM_PATH:
133 HandleSetStreamPath(command);
134 break;
135 case CEC_OPCODE_ROUTING_CHANGE:
136 HandleRoutingChange(command);
137 break;
907bd60f
LOK
138 case CEC_OPCODE_ROUTING_INFORMATION:
139 HandleRoutingInformation(command);
140 break;
6b72afcd
LOK
141 case CEC_OPCODE_STANDBY:
142 HandleStandby(command);
143 break;
144 case CEC_OPCODE_ACTIVE_SOURCE:
145 HandleActiveSource(command);
146 break;
907bd60f
LOK
147 case CEC_OPCODE_REPORT_PHYSICAL_ADDRESS:
148 HandleReportPhysicalAddress(command);
149 break;
15d1a84c
LOK
150 case CEC_OPCODE_SET_OSD_NAME:
151 HandleSetOSDName(command);
152 break;
1a6669b8
LOK
153 case CEC_OPCODE_IMAGE_VIEW_ON:
154 HandleImageViewOn(command);
155 break;
156 case CEC_OPCODE_TEXT_VIEW_ON:
157 HandleTextViewOn(command);
158 break;
6b72afcd
LOK
159 default:
160 UnhandledCommand(command);
e9de9629 161 bHandled = false;
6b72afcd 162 break;
e9de9629
LOK
163 }
164
8fa35473
LOK
165 if (bHandled && !bHandlerChanged)
166 {
167 CLockObject lock(&m_processor->m_transmitMutex);
168 m_condition.Signal();
169 }
170
e9de9629
LOK
171 return bHandled;
172}
173
be5b0e24
LOK
174bool CCECCommandHandler::HandleActiveSource(const cec_command &command)
175{
176 if (command.parameters.size == 2)
177 {
178 uint16_t iAddress = ((uint16_t)command.parameters[0] << 8) | ((uint16_t)command.parameters[1]);
fcf10e27 179 return m_processor->SetStreamPath(iAddress);
be5b0e24
LOK
180 }
181
182 return true;
183}
184
a9232a79
LOK
185bool CCECCommandHandler::HandleDeckControl(const cec_command &command)
186{
187 CCECBusDevice *device = GetDevice(command.destination);
88e5de6f 188 if (device && (device->GetType() == CEC_DEVICE_TYPE_PLAYBACK_DEVICE || device->GetType() == CEC_DEVICE_TYPE_RECORDING_DEVICE) && command.parameters.size > 0)
a9232a79
LOK
189 {
190 ((CCECPlaybackDevice *) device)->SetDeckControlMode((cec_deck_control_mode) command.parameters[0]);
191 return true;
192 }
193
194 return false;
195}
196
6a1c0009
LOK
197bool CCECCommandHandler::HandleDeviceCecVersion(const cec_command &command)
198{
199 if (command.parameters.size == 1)
200 {
201 CCECBusDevice *device = GetDevice(command.initiator);
202 if (device)
203 device->SetCecVersion((cec_version) command.parameters[0]);
204 }
205
206 return true;
207}
208
e9de9629
LOK
209bool CCECCommandHandler::HandleDeviceVendorCommandWithId(const cec_command &command)
210{
6b72afcd 211 if (m_busDevice->MyLogicalAddressContains(command.destination))
fcf10e27 212 m_processor->TransmitAbort(command.initiator, command.opcode, CEC_ABORT_REASON_REFUSED);
6b72afcd 213
6a1c0009 214 return true;
e9de9629
LOK
215}
216
217bool CCECCommandHandler::HandleDeviceVendorId(const cec_command &command)
218{
8fa35473 219 return SetVendorId(command);
e9de9629
LOK
220}
221
222bool CCECCommandHandler::HandleGetCecVersion(const cec_command &command)
223{
6b72afcd
LOK
224 if (m_busDevice->MyLogicalAddressContains(command.destination))
225 {
226 CCECBusDevice *device = GetDevice(command.destination);
227 if (device)
228 return device->TransmitCECVersion(command.initiator);
229 }
0f23c85c
LOK
230
231 return false;
e9de9629
LOK
232}
233
a1f8fb1b
LOK
234bool CCECCommandHandler::HandleGiveAudioStatus(const cec_command &command)
235{
6b72afcd
LOK
236 if (m_busDevice->MyLogicalAddressContains(command.destination))
237 {
238 CCECBusDevice *device = GetDevice(command.destination);
239 if (device && device->GetType() == CEC_DEVICE_TYPE_AUDIO_SYSTEM)
240 return ((CCECAudioSystem *) device)->TransmitAudioStatus(command.initiator);
241 }
a1f8fb1b
LOK
242
243 return false;
244}
245
e9de9629
LOK
246bool CCECCommandHandler::HandleGiveDeckStatus(const cec_command &command)
247{
6b72afcd
LOK
248 if (m_busDevice->MyLogicalAddressContains(command.destination))
249 {
250 CCECBusDevice *device = GetDevice(command.destination);
251 if (device && (device->GetType() == CEC_DEVICE_TYPE_PLAYBACK_DEVICE || device->GetType() == CEC_DEVICE_TYPE_RECORDING_DEVICE))
252 return ((CCECPlaybackDevice *) device)->TransmitDeckStatus(command.initiator);
253 }
0f23c85c
LOK
254
255 return false;
e9de9629
LOK
256}
257
258bool CCECCommandHandler::HandleGiveDevicePowerStatus(const cec_command &command)
259{
6b72afcd
LOK
260 if (m_busDevice->MyLogicalAddressContains(command.destination))
261 {
262 CCECBusDevice *device = GetDevice(command.destination);
263 if (device)
264 return device->TransmitPowerState(command.initiator);
265 }
0f23c85c
LOK
266
267 return false;
e9de9629
LOK
268}
269
270bool CCECCommandHandler::HandleGiveDeviceVendorId(const cec_command &command)
271{
6b72afcd
LOK
272 if (m_busDevice->MyLogicalAddressContains(command.destination))
273 {
274 CCECBusDevice *device = GetDevice(command.destination);
275 if (device)
276 return device->TransmitVendorID(command.initiator);
277 }
0f23c85c
LOK
278
279 return false;
e9de9629
LOK
280}
281
282bool CCECCommandHandler::HandleGiveOSDName(const cec_command &command)
283{
6b72afcd
LOK
284 if (m_busDevice->MyLogicalAddressContains(command.destination))
285 {
286 CCECBusDevice *device = GetDevice(command.destination);
287 if (device)
288 return device->TransmitOSDName(command.initiator);
289 }
0f23c85c
LOK
290
291 return false;
e9de9629
LOK
292}
293
294bool CCECCommandHandler::HandleGivePhysicalAddress(const cec_command &command)
295{
6b72afcd
LOK
296 if (m_busDevice->MyLogicalAddressContains(command.destination))
297 {
298 CCECBusDevice *device = GetDevice(command.destination);
299 if (device)
300 return device->TransmitPhysicalAddress();
301 }
09c10b66
LOK
302
303 return false;
e9de9629
LOK
304}
305
1a6669b8
LOK
306bool CCECCommandHandler::HandleGiveSystemAudioModeStatus(const cec_command &command)
307{
308 if (m_busDevice->MyLogicalAddressContains(command.destination))
309 {
310 CCECBusDevice *device = GetDevice(command.destination);
311 if (device && device->GetType() == CEC_DEVICE_TYPE_AUDIO_SYSTEM)
312 return ((CCECAudioSystem *) device)->TransmitSystemAudioModeStatus(command.initiator);
313 }
314
315 return false;
316}
317
318bool CCECCommandHandler::HandleImageViewOn(const cec_command &command)
319{
fcf10e27 320 m_processor->SetActiveSource(command.initiator);
1a6669b8
LOK
321 return true;
322}
323
e9de9629
LOK
324bool CCECCommandHandler::HandleMenuRequest(const cec_command &command)
325{
6b72afcd 326 if (m_busDevice->MyLogicalAddressContains(command.destination))
0f23c85c 327 {
6b72afcd
LOK
328 if (command.parameters[0] == CEC_MENU_REQUEST_TYPE_QUERY)
329 {
330 CCECBusDevice *device = GetDevice(command.destination);
331 if (device)
332 return device->TransmitMenuState(command.initiator);
333 }
15d1a84c
LOK
334 }
335
336 return false;
337}
338
339bool CCECCommandHandler::HandleReportAudioStatus(const cec_command &command)
340{
341 if (command.parameters.size == 1)
342 {
343 CCECBusDevice *device = GetDevice(command.initiator);
344 if (device && device->GetType() == CEC_DEVICE_TYPE_AUDIO_SYSTEM)
6b72afcd 345 {
15d1a84c
LOK
346 ((CCECAudioSystem *)device)->SetAudioStatus(command.parameters[0]);
347 return true;
6b72afcd 348 }
0f23c85c
LOK
349 }
350 return false;
e9de9629
LOK
351}
352
907bd60f
LOK
353bool CCECCommandHandler::HandleReportPhysicalAddress(const cec_command &command)
354{
0bfce006 355 if (command.parameters.size == 3)
907bd60f
LOK
356 {
357 uint16_t iNewAddress = ((uint16_t)command.parameters[0] << 8) | ((uint16_t)command.parameters[1]);
16b1e052 358 SetPhysicalAddress(command.initiator, iNewAddress);
907bd60f
LOK
359 }
360 return true;
361}
362
e55f3f70
LOK
363bool CCECCommandHandler::HandleReportPowerStatus(const cec_command &command)
364{
365 if (command.parameters.size == 1)
366 {
367 CCECBusDevice *device = GetDevice(command.initiator);
368 if (device)
369 device->SetPowerStatus((cec_power_status) command.parameters[0]);
370 }
371 return true;
372}
373
e9de9629
LOK
374bool CCECCommandHandler::HandleRequestActiveSource(const cec_command &command)
375{
376 CStdString strLog;
377 strLog.Format(">> %i requests active source", (uint8_t) command.initiator);
378 m_busDevice->AddLog(CEC_LOG_DEBUG, strLog.c_str());
8747dd4f
LOK
379
380 vector<CCECBusDevice *> devices;
381 for (int iDevicePtr = (int)GetMyDevices(devices)-1; iDevicePtr >=0; iDevicePtr--)
382 devices[iDevicePtr]->TransmitActiveSource();
c4098482 383
8747dd4f 384 return true;
e9de9629
LOK
385}
386
387bool CCECCommandHandler::HandleRoutingChange(const cec_command &command)
388{
389 if (command.parameters.size == 4)
390 {
391 uint16_t iOldAddress = ((uint16_t)command.parameters[0] << 8) | ((uint16_t)command.parameters[1]);
392 uint16_t iNewAddress = ((uint16_t)command.parameters[2] << 8) | ((uint16_t)command.parameters[3]);
e9de9629 393
0f23c85c
LOK
394 CCECBusDevice *device = GetDevice(command.initiator);
395 if (device)
9dc04b07 396 device->SetStreamPath(iNewAddress, iOldAddress);
e9de9629
LOK
397 }
398 return true;
399}
400
907bd60f
LOK
401bool CCECCommandHandler::HandleRoutingInformation(const cec_command &command)
402{
403 if (command.parameters.size == 2)
404 {
405 uint16_t iNewAddress = ((uint16_t)command.parameters[0] << 8) | ((uint16_t)command.parameters[1]);
fcf10e27 406 m_processor->SetStreamPath(iNewAddress);
907bd60f 407 }
15d1a84c
LOK
408
409 return false;
907bd60f
LOK
410}
411
a3269a0a
LOK
412bool CCECCommandHandler::HandleSetMenuLanguage(const cec_command &command)
413{
414 if (command.parameters.size == 3)
415 {
416 CCECBusDevice *device = GetDevice(command.initiator);
417 if (device)
418 {
419 cec_menu_language language;
420 language.device = command.initiator;
56701628 421 for (uint8_t iPtr = 0; iPtr < 4; iPtr++)
a3269a0a
LOK
422 language.language[iPtr] = command.parameters[iPtr];
423 language.language[3] = 0;
424 device->SetMenuLanguage(language);
15d1a84c
LOK
425 return true;
426 }
427 }
428 return false;
429}
430
431bool CCECCommandHandler::HandleSetOSDName(const cec_command &command)
432{
433 if (command.parameters.size > 0)
434 {
435 CCECBusDevice *device = GetDevice(command.initiator);
436 if (device)
437 {
438 char buf[1024];
439 for (uint8_t iPtr = 0; iPtr < command.parameters.size; iPtr++)
440 buf[iPtr] = (char)command.parameters[iPtr];
441 buf[command.parameters.size] = 0;
442
443 CStdString strName(buf);
444 device->SetOSDName(strName);
6b72afcd 445
15d1a84c 446 return true;
a3269a0a
LOK
447 }
448 }
15d1a84c 449 return false;
a3269a0a
LOK
450}
451
e9de9629
LOK
452bool CCECCommandHandler::HandleSetStreamPath(const cec_command &command)
453{
454 if (command.parameters.size >= 2)
455 {
b6c7bc94 456 uint16_t iStreamAddress = ((uint16_t)command.parameters[0] << 8) | ((uint16_t)command.parameters[1]);
e9de9629 457 CStdString strLog;
b6c7bc94 458 strLog.Format(">> %i sets stream path to physical address %04x", command.initiator, iStreamAddress);
e9de9629 459 m_busDevice->AddLog(CEC_LOG_DEBUG, strLog.c_str());
04437dcf 460
fcf10e27 461 if (m_processor->SetStreamPath(iStreamAddress))
b6c7bc94
LOK
462 {
463 CCECBusDevice *device = GetDeviceByPhysicalAddress(iStreamAddress);
464 if (device)
465 {
466 return device->TransmitActiveSource() &&
467 device->TransmitMenuState(command.initiator);
468 }
469 }
e9de9629 470 }
15d1a84c 471 return false;
e9de9629
LOK
472}
473
aa517a0d 474bool CCECCommandHandler::HandleSystemAudioModeRequest(const cec_command &command)
e5e86c76 475{
aa517a0d 476 if (m_busDevice->MyLogicalAddressContains(command.destination))
e5e86c76
LOK
477 {
478 CCECBusDevice *device = GetDevice(command.destination);
aa517a0d
LOK
479 if (device && device->GetType() == CEC_DEVICE_TYPE_AUDIO_SYSTEM)
480 {
481 if (command.parameters.size >= 2)
482 {
483 device->SetPowerStatus(CEC_POWER_STATUS_ON);
484 ((CCECAudioSystem *) device)->SetSystemAudioModeStatus(CEC_SYSTEM_AUDIO_STATUS_ON);
485 uint16_t iNewAddress = ((uint16_t)command.parameters[0] << 8) | ((uint16_t)command.parameters[1]);
486 CCECBusDevice *newActiveDevice = GetDeviceByPhysicalAddress(iNewAddress);
487 if (newActiveDevice)
fcf10e27 488 m_processor->SetActiveSource(newActiveDevice->GetLogicalAddress());
aa517a0d
LOK
489 return ((CCECAudioSystem *) device)->TransmitSetSystemAudioMode(command.initiator);
490 }
491 else
492 {
493 ((CCECAudioSystem *) device)->SetSystemAudioModeStatus(CEC_SYSTEM_AUDIO_STATUS_OFF);
494 return ((CCECAudioSystem *) device)->TransmitSetSystemAudioMode(command.initiator);
495 }
496 }
e5e86c76 497 }
6b72afcd 498 return false;
e5e86c76
LOK
499}
500
4d6b4433
LOK
501bool CCECCommandHandler::HandleStandby(const cec_command &command)
502{
503 CCECBusDevice *device = GetDevice(command.initiator);
504 if (device)
505 device->SetPowerStatus(CEC_POWER_STATUS_STANDBY);
6b72afcd 506
4d6b4433
LOK
507 return true;
508}
509
aa517a0d 510bool CCECCommandHandler::HandleSystemAudioModeStatus(const cec_command &command)
868dc71f 511{
aa517a0d 512 if (command.parameters.size == 1)
868dc71f 513 {
aa517a0d
LOK
514 CCECBusDevice *device = GetDevice(command.initiator);
515 if (device && device->GetType() == CEC_DEVICE_TYPE_AUDIO_SYSTEM)
516 {
517 ((CCECAudioSystem *)device)->SetSystemAudioModeStatus((cec_system_audio_status)command.parameters[0]);
518 return true;
519 }
520 }
521
522 return false;
523}
524
525bool CCECCommandHandler::HandleSetSystemAudioMode(const cec_command &command)
526{
527 if (command.parameters.size == 1)
528 {
529 CCECBusDevice *device = GetDevice(command.initiator);
530 if (device && device->GetType() == CEC_DEVICE_TYPE_AUDIO_SYSTEM)
531 {
532 ((CCECAudioSystem *)device)->SetSystemAudioModeStatus((cec_system_audio_status)command.parameters[0]);
533 return true;
534 }
868dc71f
LOK
535 }
536
537 return false;
538}
539
1a6669b8 540bool CCECCommandHandler::HandleTextViewOn(const cec_command &command)
cf0ecd85 541{
fcf10e27 542 m_processor->SetActiveSource(command.initiator);
1a6669b8 543 return true;
cf0ecd85
LOK
544}
545
e9de9629
LOK
546bool CCECCommandHandler::HandleUserControlPressed(const cec_command &command)
547{
6b72afcd 548 if (m_busDevice->MyLogicalAddressContains(command.destination) && command.parameters.size > 0)
e9de9629 549 {
fcf10e27 550 m_processor->AddKey();
e9de9629
LOK
551
552 if (command.parameters[0] <= CEC_USER_CONTROL_CODE_MAX)
553 {
554 CStdString strLog;
68391d4f 555 strLog.Format("key pressed: %x", command.parameters[0]);
e9de9629
LOK
556 m_busDevice->AddLog(CEC_LOG_DEBUG, strLog.c_str());
557
68391d4f
LOK
558 if (command.parameters[0] == CEC_USER_CONTROL_CODE_POWER ||
559 command.parameters[0] == CEC_USER_CONTROL_CODE_POWER_ON_FUNCTION)
560 {
561 CCECBusDevice *device = GetDevice(command.destination);
562 if (device)
563 device->SetPowerStatus(CEC_POWER_STATUS_ON);
564 }
565
fcf10e27 566 m_processor->SetCurrentButton((cec_user_control_code) command.parameters[0]);
15d1a84c 567 return true;
e9de9629
LOK
568 }
569 }
15d1a84c 570 return false;
e9de9629
LOK
571}
572
573bool CCECCommandHandler::HandleUserControlRelease(const cec_command &command)
574{
6b72afcd 575 if (m_busDevice->MyLogicalAddressContains(command.destination))
fcf10e27 576 m_processor->AddKey();
6b72afcd 577
e9de9629
LOK
578 return true;
579}
580
581void CCECCommandHandler::UnhandledCommand(const cec_command &command)
582{
b5b53c7d
LOK
583 CStdString strLog;
584 strLog.Format("unhandled command with opcode %02x from address %d", command.opcode, command.initiator);
585 m_busDevice->AddLog(CEC_LOG_DEBUG, strLog);
0f23c85c
LOK
586}
587
8747dd4f
LOK
588unsigned int CCECCommandHandler::GetMyDevices(vector<CCECBusDevice *> &devices) const
589{
590 unsigned int iReturn(0);
591
fcf10e27 592 cec_logical_addresses addresses = m_processor->GetLogicalAddresses();
f2198ab5 593 for (uint8_t iPtr = 0; iPtr < 16; iPtr++)
8747dd4f
LOK
594 {
595 if (addresses[iPtr])
596 {
597 devices.push_back(GetDevice((cec_logical_address) iPtr));
598 ++iReturn;
599 }
600 }
601
602 return iReturn;
603}
604
0f23c85c
LOK
605CCECBusDevice *CCECCommandHandler::GetDevice(cec_logical_address iLogicalAddress) const
606{
607 CCECBusDevice *device = NULL;
608
609 if (iLogicalAddress >= CECDEVICE_TV && iLogicalAddress <= CECDEVICE_BROADCAST)
fcf10e27 610 device = m_processor->m_busDevices[iLogicalAddress];
0f23c85c
LOK
611
612 return device;
e9de9629 613}
6685ae07
LOK
614
615CCECBusDevice *CCECCommandHandler::GetDeviceByPhysicalAddress(uint16_t iPhysicalAddress) const
616{
fcf10e27 617 return m_processor->GetDeviceByPhysicalAddress(iPhysicalAddress);
6685ae07 618}
181b3475 619
c4098482
LOK
620CCECBusDevice *CCECCommandHandler::GetDeviceByType(cec_device_type type) const
621{
fcf10e27 622 return m_processor->GetDeviceByType(type);
c4098482
LOK
623}
624
8fa35473 625bool CCECCommandHandler::SetVendorId(const cec_command &command)
181b3475 626{
8fa35473 627 bool bChanged(false);
181b3475
LOK
628 if (command.parameters.size < 3)
629 {
630 m_busDevice->AddLog(CEC_LOG_WARNING, "invalid vendor ID received");
8fa35473 631 return bChanged;
181b3475
LOK
632 }
633
bae71306
LOK
634 uint64_t iVendorId = ((uint64_t)command.parameters[0] << 16) +
635 ((uint64_t)command.parameters[1] << 8) +
181b3475
LOK
636 (uint64_t)command.parameters[2];
637
638 CCECBusDevice *device = GetDevice((cec_logical_address) command.initiator);
639 if (device)
8fa35473
LOK
640 bChanged = device->SetVendorId(iVendorId);
641 return bChanged;
181b3475 642}
5e822b09 643
16b1e052
LOK
644void CCECCommandHandler::SetPhysicalAddress(cec_logical_address iAddress, uint16_t iNewAddress)
645{
646 if (!m_busDevice->MyLogicalAddressContains(iAddress))
647 {
fcf10e27 648 bool bOurAddress(m_processor->GetPhysicalAddress() == iNewAddress);
16b1e052
LOK
649 GetDevice(iAddress)->SetPhysicalAddress(iNewAddress);
650 if (bOurAddress)
651 {
652 /* another device reported the same physical address as ours
653 * since we don't have physical address detection yet, we'll just use the
654 * given address, increased by 0x100 for now */
fcf10e27 655 m_processor->SetPhysicalAddress(iNewAddress + 0x100);
16b1e052
LOK
656 }
657 }
658}
855a3a98
LOK
659
660void CCECCommandHandler::HandlePoll(const cec_logical_address iInitiator, const cec_logical_address iDestination)
661{
662 CStdString strLog;
663 strLog.Format("<< POLL: %s (%x) -> %s (%x)", m_processor->ToString(iInitiator), iInitiator, m_processor->ToString(iDestination), iDestination);
664 m_processor->AddLog(CEC_LOG_DEBUG, strLog);
665}
666
667bool CCECCommandHandler::HandleReceiveFailed(void)
668{
669 /* default = error */
670 return true;
671}
8fa35473
LOK
672
673bool CCECCommandHandler::TransmitPowerOn(const cec_logical_address iInitiator, const cec_logical_address iDestination)
674{
675 cec_command command;
676 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_IMAGE_VIEW_ON, m_iTransmitTimeout);
677
678 return Transmit(command);
679}
680
681bool CCECCommandHandler::TransmitStandby(const cec_logical_address iInitiator, const cec_logical_address iDestination)
682{
683 cec_command command;
684 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_STANDBY, m_iTransmitTimeout);
685
686 return Transmit(command);
687}
688
689bool CCECCommandHandler::TransmitRequestCecVersion(const cec_logical_address iInitiator, const cec_logical_address iDestination)
690{
691 cec_command command;
692 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_GET_CEC_VERSION, m_iTransmitTimeout);
693
694 return Transmit(command);
695}
696
697bool CCECCommandHandler::TransmitRequestMenuLanguage(const cec_logical_address iInitiator, const cec_logical_address iDestination)
698{
699 cec_command command;
700 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_GET_MENU_LANGUAGE, m_iTransmitTimeout);
701
702 return Transmit(command);
703}
704
705bool CCECCommandHandler::TransmitRequestOSDName(const cec_logical_address iInitiator, const cec_logical_address iDestination)
706{
707 cec_command command;
708 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_GIVE_OSD_NAME, m_iTransmitTimeout);
709
710 return Transmit(command);
711}
712
713bool CCECCommandHandler::TransmitRequestPhysicalAddress(const cec_logical_address iInitiator, const cec_logical_address iDestination)
714{
715 cec_command command;
716 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_GIVE_PHYSICAL_ADDRESS, m_iTransmitTimeout);
717
718 return Transmit(command);
719}
720
721bool CCECCommandHandler::TransmitRequestPowerStatus(const cec_logical_address iInitiator, const cec_logical_address iDestination)
722{
723 cec_command command;
724 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_GIVE_DEVICE_POWER_STATUS, m_iTransmitTimeout);
725
726 return Transmit(command);
727}
728
729bool CCECCommandHandler::TransmitRequestVendorId(const cec_logical_address iInitiator, const cec_logical_address iDestination)
730{
731 cec_command command;
732 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_GIVE_DEVICE_VENDOR_ID, m_iTransmitTimeout);
733
734 return Transmit(command);
735}
736
737bool CCECCommandHandler::TransmitActiveSource(const cec_logical_address iInitiator, uint16_t iPhysicalAddress)
738{
739 cec_command command;
740 cec_command::Format(command, iInitiator, CECDEVICE_BROADCAST, CEC_OPCODE_ACTIVE_SOURCE, m_iTransmitTimeout);
741 command.parameters.PushBack((uint8_t) ((iPhysicalAddress >> 8) & 0xFF));
742 command.parameters.PushBack((uint8_t) (iPhysicalAddress & 0xFF));
743
744 return Transmit(command);
745}
746
747bool CCECCommandHandler::TransmitCECVersion(const cec_logical_address iInitiator, const cec_logical_address iDestination, cec_version cecVersion)
748{
749 cec_command command;
750 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_CEC_VERSION, m_iTransmitTimeout);
751 command.parameters.PushBack((uint8_t)cecVersion);
752
753 return Transmit(command);
754}
755
756bool CCECCommandHandler::TransmitInactiveSource(const cec_logical_address iInitiator, uint16_t iPhysicalAddress)
757{
758 cec_command command;
759 cec_command::Format(command, iInitiator, CECDEVICE_TV, CEC_OPCODE_INACTIVE_SOURCE, m_iTransmitTimeout);
760 command.parameters.PushBack((iPhysicalAddress >> 8) & 0xFF);
761 command.parameters.PushBack(iPhysicalAddress & 0xFF);
762
763 return Transmit(command);
764}
765
766bool CCECCommandHandler::TransmitMenuState(const cec_logical_address iInitiator, const cec_logical_address iDestination, cec_menu_state menuState)
767{
768 cec_command command;
769 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_MENU_STATUS, m_iTransmitTimeout);
770 command.parameters.PushBack((uint8_t)menuState);
771
772 return Transmit(command);
773}
774
775bool CCECCommandHandler::TransmitOSDName(const cec_logical_address iInitiator, const cec_logical_address iDestination, CStdString strDeviceName)
776{
777 cec_command command;
778 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_SET_OSD_NAME, m_iTransmitTimeout);
779 for (unsigned int iPtr = 0; iPtr < strDeviceName.length(); iPtr++)
780 command.parameters.PushBack(strDeviceName.at(iPtr));
781
782 return Transmit(command);
783}
784
785bool CCECCommandHandler::TransmitOSDString(const cec_logical_address iInitiator, const cec_logical_address iDestination, cec_display_control duration, const char *strMessage)
786{
787 cec_command command;
788 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_SET_OSD_STRING, m_iTransmitTimeout);
789 command.parameters.PushBack((uint8_t)duration);
790
791 unsigned int iLen = strlen(strMessage);
792 if (iLen > 13) iLen = 13;
793
794 for (unsigned int iPtr = 0; iPtr < iLen; iPtr++)
795 command.parameters.PushBack(strMessage[iPtr]);
796
797 return Transmit(command);
798}
799
800bool CCECCommandHandler::TransmitPhysicalAddress(const cec_logical_address iInitiator, uint16_t iPhysicalAddress, cec_device_type type)
801{
802 cec_command command;
803 cec_command::Format(command, iInitiator, CECDEVICE_BROADCAST, CEC_OPCODE_REPORT_PHYSICAL_ADDRESS, m_iTransmitTimeout);
804 command.parameters.PushBack((uint8_t) ((iPhysicalAddress >> 8) & 0xFF));
805 command.parameters.PushBack((uint8_t) (iPhysicalAddress & 0xFF));
806 command.parameters.PushBack((uint8_t) (type));
807
808 return Transmit(command);
809}
810
811bool CCECCommandHandler::TransmitPoll(const cec_logical_address iInitiator, const cec_logical_address iDestination)
812{
813 cec_command command;
814 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_NONE, m_iTransmitTimeout);
815 command.retries = 0;
816
817 return Transmit(command);
818}
819
820bool CCECCommandHandler::TransmitPowerState(const cec_logical_address iInitiator, const cec_logical_address iDestination, cec_power_status state)
821{
822 cec_command command;
823 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_REPORT_POWER_STATUS, m_iTransmitTimeout);
824 command.parameters.PushBack((uint8_t) state);
825
826 return Transmit(command);
827}
828
829bool CCECCommandHandler::TransmitVendorID(const cec_logical_address iInitiator, uint64_t iVendorId)
830{
831 cec_command command;
832 cec_command::Format(command, iInitiator, CECDEVICE_BROADCAST, CEC_OPCODE_DEVICE_VENDOR_ID, m_iTransmitTimeout);
833
834 command.parameters.PushBack((uint8_t) (((uint64_t)iVendorId >> 16) & 0xFF));
835 command.parameters.PushBack((uint8_t) (((uint64_t)iVendorId >> 8) & 0xFF));
836 command.parameters.PushBack((uint8_t) ((uint64_t)iVendorId & 0xFF));
837
838 return Transmit(command);
839}
840
841bool CCECCommandHandler::TransmitAudioStatus(const cec_logical_address iInitiator, const cec_logical_address iDestination, uint8_t state)
842{
843 cec_command command;
844 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_REPORT_AUDIO_STATUS, m_iTransmitTimeout);
845 command.parameters.PushBack(state);
846
847 return Transmit(command);
848}
849
850bool CCECCommandHandler::TransmitSetSystemAudioMode(const cec_logical_address iInitiator, const cec_logical_address iDestination, cec_system_audio_status state)
851{
852 cec_command command;
853 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_SET_SYSTEM_AUDIO_MODE, m_iTransmitTimeout);
854 command.parameters.PushBack((uint8_t)state);
855
856 return Transmit(command);
857}
858
859bool CCECCommandHandler::TransmitSystemAudioModeStatus(const cec_logical_address iInitiator, const cec_logical_address iDestination, cec_system_audio_status state)
860{
861 cec_command command;
862 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_SYSTEM_AUDIO_MODE_STATUS, m_iTransmitTimeout);
863 command.parameters.PushBack((uint8_t)state);
864
865 return Transmit(command);
866}
867
868bool CCECCommandHandler::TransmitDeckStatus(const cec_logical_address iInitiator, const cec_logical_address iDestination, cec_deck_info state)
869{
870 cec_command command;
871 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_DECK_STATUS, m_iTransmitTimeout);
872 command.PushBack((uint8_t)state);
873
874 return Transmit(command);
875}
876
877bool CCECCommandHandler::SendKeypress(const cec_logical_address iInitiator, const cec_logical_address iDestination, cec_user_control_code key)
878{
879 cec_command command;
880 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_USER_CONTROL_PRESSED, m_iTransmitTimeout);
881 command.parameters.PushBack((uint8_t)key);
882
883 return Transmit(command);
884}
885
886bool CCECCommandHandler::SendKeyRelease(const cec_logical_address iInitiator, const cec_logical_address iDestination)
887{
888 cec_command command;
889 cec_command::Format(command, iInitiator, iDestination, CEC_OPCODE_USER_CONTROL_RELEASE, m_iTransmitTimeout);
890
891 return Transmit(command);
892}
893
894bool CCECCommandHandler::Transmit(cec_command &command)
895{
896 CLockObject writeLock(&m_processor->m_transmitMutex);
897 if (m_processor->Transmit(command))
898 return m_condition.Wait(&m_processor->m_transmitMutex, m_iTransmitTimeout);
899
900 return false;
901}