win32: fix accidental rename and compiler warnings
[deb_libcec.git] / src / lib / CECProcessor.cpp
CommitLineData
abbca718
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
2abe74eb 33#include "CECProcessor.h"
abbca718 34
2abe74eb 35#include "AdapterCommunication.h"
eafa9d46 36#include "devices/CECBusDevice.h"
51b2a094
LOK
37#include "devices/CECAudioSystem.h"
38#include "devices/CECPlaybackDevice.h"
39#include "devices/CECRecordingDevice.h"
40#include "devices/CECTuner.h"
41#include "devices/CECTV.h"
62f5527d 42#include "implementations/CECCommandHandler.h"
2abe74eb 43#include "LibCEC.h"
abbca718 44#include "util/StdString.h"
b9187cc6 45#include "platform/timeutils.h"
abbca718
LOK
46
47using namespace CEC;
48using namespace std;
49
1113cb7d 50CCECProcessor::CCECProcessor(CLibCEC *controller, const char *strDeviceName, cec_logical_address iLogicalAddress /* = CECDEVICE_PLAYBACKDEVICE1 */, uint16_t iPhysicalAddress /* = CEC_DEFAULT_PHYSICAL_ADDRESS*/) :
f8513317 51 m_bStarted(false),
16b1e052 52 m_iHDMIPort(CEC_DEFAULT_HDMI_PORT),
d2f1c157 53 m_iBaseDevice((cec_logical_address)CEC_DEFAULT_BASE_DEVICE),
855a3a98 54 m_lastInitiator(CECDEVICE_UNKNOWN),
2abe74eb 55 m_strDeviceName(strDeviceName),
8b7e5ff6 56 m_controller(controller),
7c63a480 57 m_bMonitor(false),
dcd240b2
LOK
58 m_busScan(NULL),
59 m_iStandardLineTimeout(3),
60 m_iRetryLineTimeout(3)
abbca718 61{
1113cb7d 62 m_communication = new CAdapterCommunication(this);
ab1469a0
LOK
63 m_logicalAddresses.Clear();
64 m_logicalAddresses.Set(iLogicalAddress);
20b8870a 65 m_types.clear();
7871d66e 66 for (int iPtr = 0; iPtr <= 16; iPtr++)
09c10b66 67 m_busDevices[iPtr] = new CCECBusDevice(this, (cec_logical_address) iPtr, iPtr == iLogicalAddress ? iPhysicalAddress : 0);
abbca718
LOK
68}
69
1113cb7d 70CCECProcessor::CCECProcessor(CLibCEC *controller, const char *strDeviceName, const cec_device_type_list &types) :
f8513317 71 m_bStarted(false),
16b1e052 72 m_iHDMIPort(CEC_DEFAULT_HDMI_PORT),
d2f1c157 73 m_iBaseDevice((cec_logical_address)CEC_DEFAULT_BASE_DEVICE),
f8513317
LOK
74 m_strDeviceName(strDeviceName),
75 m_types(types),
f8513317 76 m_controller(controller),
dcd240b2
LOK
77 m_bMonitor(false),
78 m_iStandardLineTimeout(3),
79 m_iRetryLineTimeout(3)
f8513317 80{
1113cb7d 81 m_communication = new CAdapterCommunication(this);
ab1469a0 82 m_logicalAddresses.Clear();
f8513317 83 for (int iPtr = 0; iPtr < 16; iPtr++)
51b2a094
LOK
84 {
85 switch(iPtr)
86 {
87 case CECDEVICE_AUDIOSYSTEM:
5d5a2dc2 88 m_busDevices[iPtr] = new CCECAudioSystem(this, (cec_logical_address) iPtr, 0xFFFF);
51b2a094
LOK
89 break;
90 case CECDEVICE_PLAYBACKDEVICE1:
91 case CECDEVICE_PLAYBACKDEVICE2:
92 case CECDEVICE_PLAYBACKDEVICE3:
5d5a2dc2 93 m_busDevices[iPtr] = new CCECPlaybackDevice(this, (cec_logical_address) iPtr, 0xFFFF);
51b2a094
LOK
94 break;
95 case CECDEVICE_RECORDINGDEVICE1:
96 case CECDEVICE_RECORDINGDEVICE2:
97 case CECDEVICE_RECORDINGDEVICE3:
5d5a2dc2 98 m_busDevices[iPtr] = new CCECRecordingDevice(this, (cec_logical_address) iPtr, 0xFFFF);
51b2a094
LOK
99 break;
100 case CECDEVICE_TUNER1:
101 case CECDEVICE_TUNER2:
102 case CECDEVICE_TUNER3:
103 case CECDEVICE_TUNER4:
5d5a2dc2 104 m_busDevices[iPtr] = new CCECTuner(this, (cec_logical_address) iPtr, 0xFFFF);
51b2a094
LOK
105 break;
106 case CECDEVICE_TV:
107 m_busDevices[iPtr] = new CCECTV(this, (cec_logical_address) iPtr, 0);
108 break;
109 default:
5d5a2dc2 110 m_busDevices[iPtr] = new CCECBusDevice(this, (cec_logical_address) iPtr, 0xFFFF);
51b2a094
LOK
111 break;
112 }
113 }
f8513317
LOK
114}
115
2abe74eb 116CCECProcessor::~CCECProcessor(void)
abbca718 117{
7c63a480
LOK
118 if (m_busScan)
119 {
120 m_busScan->StopThread();
121 delete m_busScan;
122 }
123
8b0ee2cc 124 m_startCondition.Broadcast();
2abe74eb 125 StopThread();
7c63a480 126
1113cb7d 127 delete m_communication;
2abe74eb
LOK
128 m_communication = NULL;
129 m_controller = NULL;
d1998c7b
LOK
130 for (unsigned int iPtr = 0; iPtr < 16; iPtr++)
131 delete m_busDevices[iPtr];
abbca718
LOK
132}
133
1113cb7d 134bool CCECProcessor::Start(const char *strPort, uint16_t iBaudRate /* = 38400 */, uint32_t iTimeoutMs /* = 10000 */)
abbca718 135{
8b0ee2cc 136 CLockObject lock(&m_mutex);
1113cb7d 137 if (!m_communication || m_communication->IsOpen())
13fd6a66 138 {
1113cb7d
LOK
139 m_controller->AddLog(CEC_LOG_ERROR, "connection already opened");
140 return false;
141 }
142
143 if (!m_communication->Open(strPort, iBaudRate, iTimeoutMs))
144 {
145 m_controller->AddLog(CEC_LOG_ERROR, "could not open a connection");
a8f0bd18 146 return false;
13fd6a66 147 }
abbca718 148
60fa4578 149 if (CreateThread())
8b0ee2cc 150 {
f8513317 151 if (!m_startCondition.Wait(&m_mutex) || !m_bStarted)
8b0ee2cc
LOK
152 {
153 m_controller->AddLog(CEC_LOG_ERROR, "could not create a processor thread");
154 return false;
155 }
9fd73dd4
LOK
156
157 lock.Leave();
158 if (SetAckMask(m_logicalAddresses.AckMask()) &&
d2f1c157 159 SetHDMIPort(m_iBaseDevice, m_iHDMIPort, true))
9fd73dd4 160 {
b750a5c3 161 m_controller->AddLog(CEC_LOG_DEBUG, "processor thread started");
7c63a480
LOK
162 m_busScan = new CCECBusScan(this);
163 m_busScan->CreateThread(true);
9fd73dd4
LOK
164 return true;
165 }
45b97de7
LOK
166 else
167 {
168 m_controller->AddLog(CEC_LOG_ERROR, "failed to initialise the processor");
169 }
8b0ee2cc 170 }
abbca718 171
45b97de7 172 m_controller->AddLog(CEC_LOG_ERROR, "could not create a processor thread");
a8f0bd18 173 return false;
abbca718
LOK
174}
175
b58d9277 176bool CCECProcessor::TryLogicalAddress(cec_logical_address address)
f8513317 177{
93fff5c1 178 if (m_busDevices[address]->TryLogicalAddress())
f8513317 179 {
8747dd4f 180 /* only set our OSD name and active source for the primary device */
ab1469a0 181 if (m_logicalAddresses.IsEmpty())
8747dd4f 182 {
787a3cb8 183 m_busDevices[address]->m_strDeviceName = m_strDeviceName;
8747dd4f
LOK
184 m_busDevices[address]->m_bActiveSource = true;
185 }
ab1469a0 186 m_logicalAddresses.Set(address);
f8513317
LOK
187 return true;
188 }
189
f8513317
LOK
190 return false;
191}
192
b58d9277 193bool CCECProcessor::FindLogicalAddressRecordingDevice(void)
f8513317
LOK
194{
195 AddLog(CEC_LOG_DEBUG, "detecting logical address for type 'recording device'");
b58d9277
LOK
196 return TryLogicalAddress(CECDEVICE_RECORDINGDEVICE1) ||
197 TryLogicalAddress(CECDEVICE_RECORDINGDEVICE2) ||
198 TryLogicalAddress(CECDEVICE_RECORDINGDEVICE3);
f8513317
LOK
199}
200
b58d9277 201bool CCECProcessor::FindLogicalAddressTuner(void)
f8513317
LOK
202{
203 AddLog(CEC_LOG_DEBUG, "detecting logical address for type 'tuner'");
b58d9277
LOK
204 return TryLogicalAddress(CECDEVICE_TUNER1) ||
205 TryLogicalAddress(CECDEVICE_TUNER2) ||
206 TryLogicalAddress(CECDEVICE_TUNER3) ||
207 TryLogicalAddress(CECDEVICE_TUNER4);
f8513317
LOK
208}
209
b58d9277 210bool CCECProcessor::FindLogicalAddressPlaybackDevice(void)
f8513317
LOK
211{
212 AddLog(CEC_LOG_DEBUG, "detecting logical address for type 'playback device'");
b58d9277
LOK
213 return TryLogicalAddress(CECDEVICE_PLAYBACKDEVICE1) ||
214 TryLogicalAddress(CECDEVICE_PLAYBACKDEVICE2) ||
215 TryLogicalAddress(CECDEVICE_PLAYBACKDEVICE3);
f8513317
LOK
216}
217
b58d9277 218bool CCECProcessor::FindLogicalAddressAudioSystem(void)
f8513317
LOK
219{
220 AddLog(CEC_LOG_DEBUG, "detecting logical address for type 'audio'");
b58d9277 221 return TryLogicalAddress(CECDEVICE_AUDIOSYSTEM);
f8513317
LOK
222}
223
224bool CCECProcessor::FindLogicalAddresses(void)
225{
226 bool bReturn(true);
ab1469a0 227 m_logicalAddresses.Clear();
f8513317
LOK
228 CStdString strLog;
229
230 for (unsigned int iPtr = 0; iPtr < 5; iPtr++)
231 {
232 if (m_types.types[iPtr] == CEC_DEVICE_TYPE_RESERVED)
233 continue;
234
235 strLog.Format("%s - device %d: type %d", __FUNCTION__, iPtr, m_types.types[iPtr]);
236 AddLog(CEC_LOG_DEBUG, strLog);
237
238 if (m_types.types[iPtr] == CEC_DEVICE_TYPE_RECORDING_DEVICE)
b58d9277 239 bReturn &= FindLogicalAddressRecordingDevice();
f8513317 240 if (m_types.types[iPtr] == CEC_DEVICE_TYPE_TUNER)
b58d9277 241 bReturn &= FindLogicalAddressTuner();
f8513317 242 if (m_types.types[iPtr] == CEC_DEVICE_TYPE_PLAYBACK_DEVICE)
b58d9277 243 bReturn &= FindLogicalAddressPlaybackDevice();
f8513317 244 if (m_types.types[iPtr] == CEC_DEVICE_TYPE_AUDIO_SYSTEM)
b58d9277 245 bReturn &= FindLogicalAddressAudioSystem();
f8513317
LOK
246 }
247
248 return bReturn;
249}
250
2abe74eb 251void *CCECProcessor::Process(void)
f99bc831 252{
8747dd4f 253 bool bParseFrame(false);
e0407d3d
LOK
254 cec_command command;
255 CCECAdapterMessage msg;
256
9fd73dd4 257 if (m_logicalAddresses.IsEmpty() && !FindLogicalAddresses())
8b0ee2cc 258 {
9fd73dd4
LOK
259 CLockObject lock(&m_mutex);
260 m_controller->AddLog(CEC_LOG_ERROR, "could not detect our logical addresses");
261 m_startCondition.Signal();
262 return NULL;
263 }
264 else
265 {
2afdf4c2 266 m_busDevices[m_logicalAddresses.primary]->TransmitPhysicalAddress();
9fd73dd4
LOK
267 CLockObject lock(&m_mutex);
268 m_bStarted = true;
269 m_controller->AddLog(CEC_LOG_DEBUG, "processor thread started");
270 m_startCondition.Signal();
8b0ee2cc 271 }
abbca718 272
13fd6a66 273 while (!IsStopped())
abbca718 274 {
ab1469a0 275 command.Clear();
76321de4
LOK
276 msg.clear();
277
60fa4578
LOK
278 {
279 CLockObject lock(&m_mutex);
8747dd4f
LOK
280 if (m_commandBuffer.Pop(command))
281 {
282 bParseFrame = true;
283 }
284 else if (m_communication->IsOpen() && m_communication->Read(msg, 50))
b5f34cf9 285 {
f2198ab5 286 if ((bParseFrame = (ParseMessage(msg) && !IsStopped())) == true)
8747dd4f 287 command = m_currentframe;
b5f34cf9 288 }
60fa4578
LOK
289 }
290
13fd6a66 291 if (bParseFrame)
9dee1670 292 ParseCommand(command);
8747dd4f 293 bParseFrame = false;
abbca718 294
b5f34cf9
LOK
295 Sleep(5);
296
13fd6a66 297 m_controller->CheckKeypressTimeout();
abbca718
LOK
298 }
299
1113cb7d
LOK
300 if (m_communication)
301 m_communication->Close();
302
60fa4578 303 return NULL;
abbca718
LOK
304}
305
18203d17 306bool CCECProcessor::SetActiveSource(cec_device_type type /* = CEC_DEVICE_TYPE_RESERVED */)
abbca718 307{
8ac9c610
LOK
308 bool bReturn(false);
309
60fa4578 310 if (!IsRunning())
8ac9c610 311 return bReturn;
f99bc831 312
18203d17 313 cec_logical_address addr = m_logicalAddresses.primary;
04437dcf 314
18203d17
LOK
315 if (type != CEC_DEVICE_TYPE_RESERVED)
316 {
f2198ab5 317 for (uint8_t iPtr = 0; iPtr < 16; iPtr++)
18203d17
LOK
318 {
319 if (m_logicalAddresses[iPtr] && m_busDevices[iPtr]->m_type == type)
320 {
321 addr = (cec_logical_address) iPtr;
322 break;
323 }
324 }
325 }
326
8d915412
LOK
327 bReturn = m_busDevices[CECDEVICE_TV]->PowerOn() &&
328 m_busDevices[addr]->TransmitActiveSource() &&
329 SetStreamPath(m_busDevices[addr]->GetPhysicalAddress(false));
330
331 if (bReturn && (m_busDevices[addr]->GetType() == CEC_DEVICE_TYPE_PLAYBACK_DEVICE ||
332 m_busDevices[addr]->GetType() == CEC_DEVICE_TYPE_RECORDING_DEVICE))
333 {
334 bReturn = ((CCECPlaybackDevice *)m_busDevices[addr])->TransmitDeckStatus(CECDEVICE_TV);
335 }
336
337 return bReturn;
18203d17
LOK
338}
339
dcd240b2
LOK
340void CCECProcessor::SetStandardLineTimeout(uint8_t iTimeout)
341{
342 CLockObject lock(&m_mutex);
343 m_iStandardLineTimeout = iTimeout;
344}
345
346void CCECProcessor::SetRetryLineTimeout(uint8_t iTimeout)
347{
348 CLockObject lock(&m_mutex);
349 m_iRetryLineTimeout = iTimeout;
350}
351
1a6669b8
LOK
352bool CCECProcessor::SetActiveSource(cec_logical_address iAddress)
353{
16b1e052 354 return SetStreamPath(m_busDevices[iAddress]->GetPhysicalAddress(false));
1a6669b8
LOK
355}
356
18203d17
LOK
357bool CCECProcessor::SetActiveView(void)
358{
4a870dc8 359 return SetActiveSource(m_types.IsEmpty() ? CEC_DEVICE_TYPE_RESERVED : m_types[0]);
8ac9c610
LOK
360}
361
28fa6c97 362bool CCECProcessor::SetDeckControlMode(cec_deck_control_mode mode, bool bSendUpdate /* = true */)
a9232a79
LOK
363{
364 bool bReturn(false);
365
366 CCECBusDevice *device = GetDeviceByType(CEC_DEVICE_TYPE_PLAYBACK_DEVICE);
367 if (device)
368 {
369 ((CCECPlaybackDevice *) device)->SetDeckControlMode(mode);
28fa6c97
LOK
370 if (bSendUpdate)
371 ((CCECPlaybackDevice *) device)->TransmitDeckStatus(CECDEVICE_TV);
a9232a79
LOK
372 bReturn = true;
373 }
374
375 return bReturn;
376}
377
28fa6c97 378bool CCECProcessor::SetDeckInfo(cec_deck_info info, bool bSendUpdate /* = true */)
a9232a79
LOK
379{
380 bool bReturn(false);
381
382 CCECBusDevice *device = GetDeviceByType(CEC_DEVICE_TYPE_PLAYBACK_DEVICE);
383 if (device)
384 {
385 ((CCECPlaybackDevice *) device)->SetDeckStatus(info);
28fa6c97
LOK
386 if (bSendUpdate)
387 ((CCECPlaybackDevice *) device)->TransmitDeckStatus(CECDEVICE_TV);
a9232a79
LOK
388 bReturn = true;
389 }
390
391 return bReturn;
392}
393
d2f1c157 394bool CCECProcessor::SetHDMIPort(cec_logical_address iBaseDevice, uint8_t iPort, bool bForce /* = false */)
16b1e052
LOK
395{
396 bool bReturn(false);
397
398 CStdString strLog;
d2f1c157 399 strLog.Format("setting HDMI port to %d on device %s (%d)", iPort, ToString(iBaseDevice), (int)iBaseDevice);
16b1e052
LOK
400 AddLog(CEC_LOG_DEBUG, strLog);
401
d2f1c157 402 m_iBaseDevice = iBaseDevice;
16b1e052 403 m_iHDMIPort = iPort;
9fd73dd4 404 if (!m_bStarted && !bForce)
16b1e052
LOK
405 return true;
406
407 uint16_t iPhysicalAddress(0);
d2f1c157
LOK
408 iPhysicalAddress = m_busDevices[iBaseDevice]->GetPhysicalAddress();
409 uint16_t iPos = 0;
410 if (iPhysicalAddress == 0)
411 iPos = 0x1000;
412 else if (iPhysicalAddress % 0x1000 == 0)
413 iPos = 0x100;
414 else if (iPhysicalAddress % 0x100 == 0)
415 iPos = 0x10;
416 else if (iPhysicalAddress % 0x10 == 0)
417 iPos = 0x1;
418
419 while(!bReturn && iPos > 0)
16b1e052 420 {
d2f1c157 421 iPhysicalAddress += (uint16_t)(iPort * iPos);
16b1e052
LOK
422 strLog.Format("checking physical address %4x", iPhysicalAddress);
423 AddLog(CEC_LOG_DEBUG, strLog);
45b97de7 424 if (PhysicalAddressInUse(iPhysicalAddress))
16b1e052
LOK
425 {
426 strLog.Format("physical address %4x is in use", iPhysicalAddress);
427 AddLog(CEC_LOG_DEBUG, strLog);
d2f1c157 428 iPos = (iPos == 1) ? 0 : iPos / 0x10;
16b1e052
LOK
429 }
430 else
431 {
45b97de7
LOK
432 strLog.Format("physical address %4x is free", iPhysicalAddress);
433 AddLog(CEC_LOG_DEBUG, strLog);
16b1e052
LOK
434 SetPhysicalAddress(iPhysicalAddress);
435 bReturn = true;
436 }
437 }
438
439 return bReturn;
440}
441
45b97de7 442bool CCECProcessor::PhysicalAddressInUse(uint16_t iPhysicalAddress)
16b1e052 443{
9fd73dd4 444 for (unsigned int iPtr = 0; iPtr < 15; iPtr++)
16b1e052
LOK
445 {
446 if (m_busDevices[iPtr]->GetPhysicalAddress(false) == iPhysicalAddress)
447 return true;
448 }
449 return false;
450}
451
8ac9c610
LOK
452bool CCECProcessor::SetStreamPath(uint16_t iStreamPath)
453{
04437dcf
LOK
454 bool bReturn(false);
455
8ac9c610
LOK
456 CCECBusDevice *device = GetDeviceByPhysicalAddress(iStreamPath);
457 if (device)
458 {
7856411b 459 device->SetActiveDevice();
91bea174 460 bReturn = true;
8ac9c610
LOK
461 }
462
04437dcf 463 return bReturn;
abbca718
LOK
464}
465
ab27363d 466bool CCECProcessor::TransmitInactiveSource(void)
abbca718 467{
60fa4578 468 if (!IsRunning())
f99bc831
LOK
469 return false;
470
ab1469a0 471 if (!m_logicalAddresses.IsEmpty() && m_busDevices[m_logicalAddresses.primary])
ab27363d 472 return m_busDevices[m_logicalAddresses.primary]->TransmitInactiveSource();
cc60ab1c 473 return false;
abbca718
LOK
474}
475
9dee1670 476void CCECProcessor::LogOutput(const cec_command &data)
abbca718 477{
1d3ca3de 478 CStdString strTx;
57f45e6c
LOK
479 strTx.Format("<< %02x", ((uint8_t)data.initiator << 4) + (uint8_t)data.destination);
480 if (data.opcode_set)
481 strTx.AppendFormat(":%02x", (uint8_t)data.opcode);
9dee1670 482
06a1f7ce 483 for (uint8_t iPtr = 0; iPtr < data.parameters.size; iPtr++)
1d3ca3de
LOK
484 strTx.AppendFormat(":%02x", data.parameters[iPtr]);
485 m_controller->AddLog(CEC_LOG_TRAFFIC, strTx.c_str());
9dee1670 486}
2abe74eb 487
06bfd4d7 488bool CCECProcessor::SetLogicalAddress(cec_logical_address iLogicalAddress)
abbca718 489{
f8513317 490 if (m_logicalAddresses.primary != iLogicalAddress)
06775107
LOK
491 {
492 CStdString strLog;
f8513317 493 strLog.Format("<< setting primary logical address to %1x", iLogicalAddress);
06775107 494 m_controller->AddLog(CEC_LOG_NOTICE, strLog.c_str());
f8513317 495 m_logicalAddresses.primary = iLogicalAddress;
ab1469a0
LOK
496 m_logicalAddresses.Set(iLogicalAddress);
497 return SetAckMask(m_logicalAddresses.AckMask());
06775107 498 }
2abe74eb 499
06bfd4d7 500 return true;
abbca718 501}
825ddb96 502
28fa6c97
LOK
503bool CCECProcessor::SetMenuState(cec_menu_state state, bool bSendUpdate /* = true */)
504{
505 for (uint8_t iPtr = 0; iPtr < 16; iPtr++)
506 {
507 if (m_logicalAddresses[iPtr])
508 m_busDevices[iPtr]->SetMenuState(state);
509 }
510
511 if (bSendUpdate)
512 m_busDevices[m_logicalAddresses.primary]->TransmitMenuState(CECDEVICE_TV);
513
514 return true;
515}
516
2492216a
LOK
517bool CCECProcessor::SetPhysicalAddress(uint16_t iPhysicalAddress)
518{
16b1e052 519 if (!m_logicalAddresses.IsEmpty())
cc60ab1c 520 {
16b1e052
LOK
521 for (uint8_t iPtr = 0; iPtr < 15; iPtr++)
522 if (m_logicalAddresses[iPtr])
523 m_busDevices[iPtr]->SetPhysicalAddress(iPhysicalAddress);
8ac9c610 524 return SetActiveView();
cc60ab1c
LOK
525 }
526 return false;
1969b140
LOK
527}
528
8b7e5ff6
LOK
529bool CCECProcessor::SwitchMonitoring(bool bEnable)
530{
531 CStdString strLog;
532 strLog.Format("== %s monitoring mode ==", bEnable ? "enabling" : "disabling");
533 m_controller->AddLog(CEC_LOG_NOTICE, strLog.c_str());
534
855a3a98
LOK
535 {
536 CLockObject lock(&m_mutex);
537 m_bMonitor = bEnable;
538
539 if (bEnable)
540 {
541 if (!m_busScan)
542 {
543 m_busScan = new CCECBusScan(this);
544 m_busScan->CreateThread(true);
545 }
546 }
547 else
548 {
549 if (m_busScan)
550 {
551 m_busScan->StopThread();
552 delete m_busScan;
553 m_busScan = NULL;
554 }
555 }
556 }
557
8b7e5ff6 558 if (bEnable)
06bfd4d7 559 return SetAckMask(0);
8b7e5ff6 560 else
ab1469a0 561 return SetAckMask(m_logicalAddresses.AckMask());
8b7e5ff6
LOK
562}
563
57f45e6c
LOK
564bool CCECProcessor::PollDevice(cec_logical_address iAddress)
565{
566 if (iAddress != CECDEVICE_UNKNOWN && m_busDevices[iAddress])
95a73fa7
LOK
567 {
568 return m_logicalAddresses.primary == CECDEVICE_UNKNOWN ?
569 m_busDevices[iAddress]->TransmitPoll(iAddress) :
570 m_busDevices[m_logicalAddresses.primary]->TransmitPoll(iAddress);
571 }
57f45e6c
LOK
572 return false;
573}
574
8fa35473 575uint8_t CCECProcessor::VolumeUp(void)
04e637f9
LOK
576{
577 uint8_t status = 0;
578 if (IsActiveDevice(CECDEVICE_AUDIOSYSTEM))
8fa35473 579 status = ((CCECAudioSystem *)m_busDevices[CECDEVICE_AUDIOSYSTEM])->VolumeUp();
04e637f9
LOK
580
581 return status;
582}
583
8fa35473 584uint8_t CCECProcessor::VolumeDown(void)
04e637f9
LOK
585{
586 uint8_t status = 0;
587 if (IsActiveDevice(CECDEVICE_AUDIOSYSTEM))
8fa35473 588 status = ((CCECAudioSystem *)m_busDevices[CECDEVICE_AUDIOSYSTEM])->VolumeDown();
04e637f9
LOK
589
590 return status;
591}
592
8fa35473 593uint8_t CCECProcessor::MuteAudio(void)
04e637f9
LOK
594{
595 uint8_t status = 0;
596 if (IsActiveDevice(CECDEVICE_AUDIOSYSTEM))
8fa35473 597 status = ((CCECAudioSystem *)m_busDevices[CECDEVICE_AUDIOSYSTEM])->MuteAudio();
04e637f9
LOK
598
599 return status;
600}
601
16b1e052 602CCECBusDevice *CCECProcessor::GetDeviceByPhysicalAddress(uint16_t iPhysicalAddress, bool bRefresh /* = false */) const
8ac9c610 603{
16b1e052 604 if (m_busDevices[m_logicalAddresses.primary]->GetPhysicalAddress(false) == iPhysicalAddress)
b58d9277 605 return m_busDevices[m_logicalAddresses.primary];
8ac9c610 606
b58d9277 607 CCECBusDevice *device = NULL;
8ac9c610
LOK
608 for (unsigned int iPtr = 0; iPtr < 16; iPtr++)
609 {
16b1e052 610 if (m_busDevices[iPtr]->GetPhysicalAddress(bRefresh) == iPhysicalAddress)
8ac9c610
LOK
611 {
612 device = m_busDevices[iPtr];
613 break;
614 }
615 }
616
617 return device;
618}
619
a9232a79
LOK
620CCECBusDevice *CCECProcessor::GetDeviceByType(cec_device_type type) const
621{
622 CCECBusDevice *device = NULL;
623
624 for (unsigned int iPtr = 0; iPtr < 16; iPtr++)
625 {
626 if (m_busDevices[iPtr]->m_type == type)
627 {
628 device = m_busDevices[iPtr];
629 break;
630 }
631 }
632
633 return device;
634}
635
6a1c0009
LOK
636cec_version CCECProcessor::GetDeviceCecVersion(cec_logical_address iAddress)
637{
638 return m_busDevices[iAddress]->GetCecVersion();
639}
640
ed21be2a
LOK
641cec_osd_name CCECProcessor::GetDeviceOSDName(cec_logical_address iAddress)
642{
643 CStdString strOSDName = m_busDevices[iAddress]->GetOSDName();
644 cec_osd_name retVal;
645
646 snprintf(retVal.name, sizeof(retVal.name), "%s", strOSDName.c_str());
647 retVal.device = iAddress;
648
649 return retVal;
650}
651
a3269a0a
LOK
652bool CCECProcessor::GetDeviceMenuLanguage(cec_logical_address iAddress, cec_menu_language *language)
653{
cc60ab1c
LOK
654 if (m_busDevices[iAddress])
655 {
656 *language = m_busDevices[iAddress]->GetMenuLanguage();
5744fbba 657 return (strcmp(language->language, "???") != 0);
cc60ab1c
LOK
658 }
659 return false;
a3269a0a
LOK
660}
661
44c74256
LOK
662uint64_t CCECProcessor::GetDeviceVendorId(cec_logical_address iAddress)
663{
cc60ab1c
LOK
664 if (m_busDevices[iAddress])
665 return m_busDevices[iAddress]->GetVendorId();
666 return false;
44c74256
LOK
667}
668
e55f3f70
LOK
669cec_power_status CCECProcessor::GetDevicePowerStatus(cec_logical_address iAddress)
670{
cc60ab1c
LOK
671 if (m_busDevices[iAddress])
672 return m_busDevices[iAddress]->GetPowerStatus();
673 return CEC_POWER_STATUS_UNKNOWN;
e55f3f70
LOK
674}
675
8d84e2c0 676bool CCECProcessor::Transmit(const cec_command &data)
825ddb96 677{
28352a04 678 bool bReturn(false);
13929cff
LOK
679 LogOutput(data);
680
28352a04
LOK
681 CCECAdapterMessage *output = new CCECAdapterMessage(data);
682 bReturn = Transmit(output);
683 delete output;
684
685 return bReturn;
06bfd4d7 686}
13929cff 687
28352a04 688bool CCECProcessor::Transmit(CCECAdapterMessage *output)
06bfd4d7
LOK
689{
690 bool bReturn(false);
2abe74eb 691 CLockObject lock(&m_mutex);
eb35e4ca 692 {
dcd240b2 693 m_communication->SetLineTimeout(m_iStandardLineTimeout);
ae693aaa 694 output->tries = 1;
9902f4e8 695
fd5e333a 696 do
0e31a62c 697 {
9902f4e8 698 if (output->tries > 0)
dcd240b2 699 m_communication->SetLineTimeout(m_iRetryLineTimeout);
9902f4e8 700
82d1a9a1
LOK
701 CLockObject msgLock(&output->mutex);
702 if (!m_communication || !m_communication->Write(output))
0e31a62c 703 return bReturn;
82d1a9a1
LOK
704 else
705 {
706 output->condition.Wait(&output->mutex);
707 if (output->state != ADAPTER_MESSAGE_STATE_SENT)
708 {
709 m_controller->AddLog(CEC_LOG_ERROR, "command was not sent");
710 return bReturn;
711 }
0e31a62c 712 }
2abe74eb 713
82d1a9a1
LOK
714 if (output->transmit_timeout > 0)
715 {
716 if ((bReturn = WaitForTransmitSucceeded(output)) == false)
717 m_controller->AddLog(CEC_LOG_DEBUG, "did not receive ack");
718 }
719 else
720 bReturn = true;
ae693aaa 721 }while (output->transmit_timeout > 0 && output->needs_retry() && ++output->tries < output->maxTries);
2abe74eb
LOK
722 }
723
dcd240b2 724 m_communication->SetLineTimeout(m_iStandardLineTimeout);
9902f4e8 725
7ea0d558 726 return bReturn;
825ddb96 727}
abbca718 728
22b4e74a 729void CCECProcessor::TransmitAbort(cec_logical_address address, cec_opcode opcode, cec_abort_reason reason /* = CEC_ABORT_REASON_UNRECOGNIZED_OPCODE */)
abbca718 730{
9dee1670
LOK
731 m_controller->AddLog(CEC_LOG_DEBUG, "<< transmitting abort message");
732
06a1f7ce 733 cec_command command;
f8513317 734 // TODO
ab1469a0
LOK
735 cec_command::Format(command, m_logicalAddresses.primary, address, CEC_OPCODE_FEATURE_ABORT);
736 command.parameters.PushBack((uint8_t)opcode);
737 command.parameters.PushBack((uint8_t)reason);
9dee1670
LOK
738
739 Transmit(command);
abbca718
LOK
740}
741
82d1a9a1 742bool CCECProcessor::WaitForTransmitSucceeded(CCECAdapterMessage *message)
abbca718 743{
1f0e9e0f
LOK
744 bool bError(false);
745 bool bTransmitSucceeded(false);
82d1a9a1 746 uint8_t iPacketsLeft(message->size() / 4);
abbca718
LOK
747
748 int64_t iNow = GetTimeMs();
82d1a9a1 749 int64_t iTargetTime = iNow + message->transmit_timeout;
abbca718 750
82d1a9a1 751 while (!bTransmitSucceeded && !bError && (message->transmit_timeout == 0 || iNow < iTargetTime))
abbca718 752 {
220537f2 753 CCECAdapterMessage msg;
25701fa6 754
82d1a9a1 755 if (!m_communication->Read(msg, message->transmit_timeout > 0 ? (int32_t)(iTargetTime - iNow) : 1000))
abbca718 756 {
abbca718 757 iNow = GetTimeMs();
5d38b0b6
LOK
758 continue;
759 }
760
855a3a98
LOK
761 if (msg.message() == MSGCODE_FRAME_START && msg.ack())
762 {
763 m_busDevices[msg.initiator()]->GetHandler()->HandlePoll(msg.initiator(), msg.destination());
764 m_lastInitiator = msg.initiator();
765 iNow = GetTimeMs();
766 continue;
767 }
768
769 bError = msg.is_error();
770 if (msg.message() == MSGCODE_RECEIVE_FAILED &&
771 m_lastInitiator != CECDEVICE_UNKNOWN &&
772 !m_busDevices[m_lastInitiator]->GetHandler()->HandleReceiveFailed())
773 {
774 iNow = GetTimeMs();
775 continue;
776 }
777
82d1a9a1 778 if (bError)
fd5e333a 779 {
82d1a9a1 780 message->reply = msg.message();
fd5e333a
LOK
781 m_controller->AddLog(CEC_LOG_DEBUG, msg.ToString());
782 }
82d1a9a1 783 else
7ea0d558 784 {
edab6137
LOK
785 switch(msg.message())
786 {
787 case MSGCODE_COMMAND_ACCEPTED:
fd5e333a 788 m_controller->AddLog(CEC_LOG_DEBUG, msg.ToString());
edab6137
LOK
789 if (iPacketsLeft > 0)
790 iPacketsLeft--;
791 break;
792 case MSGCODE_TRANSMIT_SUCCEEDED:
fd5e333a 793 m_controller->AddLog(CEC_LOG_DEBUG, msg.ToString());
edab6137
LOK
794 bTransmitSucceeded = (iPacketsLeft == 0);
795 bError = !bTransmitSucceeded;
82d1a9a1 796 message->reply = MSGCODE_TRANSMIT_SUCCEEDED;
edab6137
LOK
797 break;
798 default:
26128836
LOK
799 // ignore other data while waiting
800 break;
edab6137 801 }
7ea0d558 802
edab6137
LOK
803 iNow = GetTimeMs();
804 }
abbca718
LOK
805 }
806
1f0e9e0f 807 return bTransmitSucceeded && !bError;
abbca718
LOK
808}
809
0e7b6c54 810bool CCECProcessor::ParseMessage(const CCECAdapterMessage &msg)
abbca718 811{
855a3a98
LOK
812 bool bEom(false);
813 bool bIsError(msg.is_error());
60fa4578 814
0e7b6c54 815 if (msg.empty())
7ea0d558 816 return bEom;
abbca718 817
0e7b6c54 818 switch(msg.message())
abbca718 819 {
abbca718
LOK
820 case MSGCODE_FRAME_START:
821 {
ab1469a0 822 m_currentframe.Clear();
0e7b6c54 823 if (msg.size() >= 2)
abbca718 824 {
0e7b6c54
LOK
825 m_currentframe.initiator = msg.initiator();
826 m_currentframe.destination = msg.destination();
827 m_currentframe.ack = msg.ack();
828 m_currentframe.eom = msg.eom();
abbca718 829 }
8b86fb7a 830 if (m_currentframe.ack == 0x1)
855a3a98
LOK
831 {
832 m_lastInitiator = m_currentframe.initiator;
833 m_busDevices[m_lastInitiator]->GetHandler()->HandlePoll(m_currentframe.initiator, m_currentframe.destination);
834 }
835 }
836 break;
837 case MSGCODE_RECEIVE_FAILED:
838 {
839 if (m_lastInitiator != CECDEVICE_UNKNOWN)
840 bIsError = m_busDevices[m_lastInitiator]->GetHandler()->HandleReceiveFailed();
abbca718
LOK
841 }
842 break;
843 case MSGCODE_FRAME_DATA:
844 {
0e7b6c54 845 if (msg.size() >= 2)
78e8010a 846 {
ab1469a0 847 m_currentframe.PushBack(msg[1]);
0e7b6c54 848 m_currentframe.eom = msg.eom();
abbca718 849 }
0e7b6c54 850 bEom = msg.eom();
abbca718 851 }
78e8010a 852 break;
abbca718
LOK
853 default:
854 break;
855 }
7ea0d558 856
855a3a98 857 m_controller->AddLog(bIsError ? CEC_LOG_WARNING : CEC_LOG_DEBUG, msg.ToString());
7ea0d558 858 return bEom;
abbca718
LOK
859}
860
e9de9629 861void CCECProcessor::ParseCommand(cec_command &command)
abbca718 862{
e9de9629 863 CStdString dataStr;
1d3ca3de 864 dataStr.Format(">> %1x%1x:%02x", command.initiator, command.destination, command.opcode);
e9de9629
LOK
865 for (uint8_t iPtr = 0; iPtr < command.parameters.size; iPtr++)
866 dataStr.AppendFormat(":%02x", (unsigned int)command.parameters[iPtr]);
1d3ca3de 867 m_controller->AddLog(CEC_LOG_TRAFFIC, dataStr.c_str());
dc113aca 868
7871d66e 869 if (!m_bMonitor && command.initiator >= CECDEVICE_TV && command.initiator <= CECDEVICE_BROADCAST)
e9de9629 870 m_busDevices[(uint8_t)command.initiator]->HandleCommand(command);
dc113aca
LOK
871}
872
6d858ba4
LOK
873cec_logical_addresses CCECProcessor::GetActiveDevices(void)
874{
875 cec_logical_addresses addresses;
988de7b9 876 addresses.Clear();
6d858ba4
LOK
877 for (unsigned int iPtr = 0; iPtr < 15; iPtr++)
878 {
879 if (m_busDevices[iPtr]->GetStatus() == CEC_DEVICE_STATUS_PRESENT)
880 addresses.Set((cec_logical_address) iPtr);
881 }
882 return addresses;
883}
884
885bool CCECProcessor::IsActiveDevice(cec_logical_address address)
886{
887 return m_busDevices[address]->GetStatus() == CEC_DEVICE_STATUS_PRESENT;
888}
889
890bool CCECProcessor::IsActiveDeviceType(cec_device_type type)
891{
6d858ba4 892 for (unsigned int iPtr = 0; iPtr < 15; iPtr++)
2813bd07
LOK
893 {
894 if (m_busDevices[iPtr]->GetType() == type && m_busDevices[iPtr]->GetStatus() == CEC_DEVICE_STATUS_PRESENT)
6d858ba4 895 return true;
2813bd07
LOK
896 }
897
6d858ba4
LOK
898 return false;
899}
900
0f23c85c
LOK
901uint16_t CCECProcessor::GetPhysicalAddress(void) const
902{
ab1469a0 903 if (!m_logicalAddresses.IsEmpty() && m_busDevices[m_logicalAddresses.primary])
16b1e052 904 return m_busDevices[m_logicalAddresses.primary]->GetPhysicalAddress(false);
cc60ab1c 905 return false;
0f23c85c
LOK
906}
907
e9de9629 908void CCECProcessor::SetCurrentButton(cec_user_control_code iButtonCode)
dc113aca 909{
e9de9629 910 m_controller->SetCurrentButton(iButtonCode);
dc113aca
LOK
911}
912
e9de9629 913void CCECProcessor::AddCommand(const cec_command &command)
dc113aca 914{
0544ff56 915 m_controller->AddCommand(command);
dc113aca
LOK
916}
917
95ba7a09
LOK
918void CCECProcessor::AddKey(cec_keypress &key)
919{
920 m_controller->AddKey(key);
921}
922
e9de9629 923void CCECProcessor::AddKey(void)
dc113aca 924{
e9de9629 925 m_controller->AddKey();
abbca718 926}
acec5f48 927
e9de9629 928void CCECProcessor::AddLog(cec_log_level level, const CStdString &strMessage)
acec5f48 929{
e9de9629 930 m_controller->AddLog(level, strMessage);
acec5f48 931}
06bfd4d7
LOK
932
933bool CCECProcessor::SetAckMask(uint16_t iMask)
934{
28352a04 935 bool bReturn(false);
06bfd4d7
LOK
936 CStdString strLog;
937 strLog.Format("setting ackmask to %2x", iMask);
938 m_controller->AddLog(CEC_LOG_DEBUG, strLog.c_str());
939
28352a04 940 CCECAdapterMessage *output = new CCECAdapterMessage;
06bfd4d7
LOK
941
942 output->push_back(MSGSTART);
943 output->push_escaped(MSGCODE_SET_ACK_MASK);
944 output->push_escaped(iMask >> 8);
945 output->push_escaped((uint8_t)iMask);
946 output->push_back(MSGEND);
947
28352a04 948 if ((bReturn = Transmit(output)) == false)
06bfd4d7 949 m_controller->AddLog(CEC_LOG_ERROR, "could not set the ackmask");
06bfd4d7 950
28352a04
LOK
951 delete output;
952
953 return bReturn;
06bfd4d7 954}
a33794d8 955
7f919115 956bool CCECProcessor::TransmitKeypress(cec_logical_address iDestination, cec_user_control_code key)
a33794d8 957{
7f919115 958 return m_busDevices[iDestination]->TransmitKeypress(key);
a33794d8
LOK
959}
960
7f919115 961bool CCECProcessor::TransmitKeyRelease(cec_logical_address iDestination)
a33794d8 962{
7f919115 963 return m_busDevices[iDestination]->TransmitKeyRelease();
a33794d8 964}
7c63a480 965
03ae897d
LOK
966const char *CCECProcessor::ToString(const cec_menu_state state)
967{
968 switch (state)
969 {
970 case CEC_MENU_STATE_ACTIVATED:
971 return "activated";
972 case CEC_MENU_STATE_DEACTIVATED:
973 return "deactivated";
974 default:
975 return "unknown";
976 }
977}
978
979const char *CCECProcessor::ToString(const cec_version version)
980{
981 switch (version)
982 {
983 case CEC_VERSION_1_2:
984 return "1.2";
985 case CEC_VERSION_1_2A:
986 return "1.2a";
987 case CEC_VERSION_1_3:
988 return "1.3";
989 case CEC_VERSION_1_3A:
990 return "1.3a";
991 case CEC_VERSION_1_4:
992 return "1.4";
993 default:
994 return "unknown";
995 }
996}
997
998const char *CCECProcessor::ToString(const cec_power_status status)
999{
1000 switch (status)
1001 {
1002 case CEC_POWER_STATUS_ON:
1003 return "on";
1004 case CEC_POWER_STATUS_STANDBY:
1005 return "standby";
1006 case CEC_POWER_STATUS_IN_TRANSITION_ON_TO_STANDBY:
1007 return "in transition from on to standby";
1008 case CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON:
1009 return "in transition from standby to on";
1010 default:
1011 return "unknown";
1012 }
1013}
1014
1015const char *CCECProcessor::ToString(const cec_logical_address address)
1016{
1017 switch(address)
1018 {
1019 case CECDEVICE_AUDIOSYSTEM:
1020 return "Audio";
1021 case CECDEVICE_BROADCAST:
1022 return "Broadcast";
1023 case CECDEVICE_FREEUSE:
1024 return "Free use";
1025 case CECDEVICE_PLAYBACKDEVICE1:
1026 return "Playback 1";
1027 case CECDEVICE_PLAYBACKDEVICE2:
1028 return "Playback 2";
1029 case CECDEVICE_PLAYBACKDEVICE3:
1030 return "Playback 3";
1031 case CECDEVICE_RECORDINGDEVICE1:
1032 return "Recorder 1";
1033 case CECDEVICE_RECORDINGDEVICE2:
1034 return "Recorder 2";
1035 case CECDEVICE_RECORDINGDEVICE3:
1036 return "Recorder 3";
1037 case CECDEVICE_RESERVED1:
1038 return "Reserved 1";
1039 case CECDEVICE_RESERVED2:
1040 return "Reserved 2";
1041 case CECDEVICE_TUNER1:
1042 return "Tuner 1";
1043 case CECDEVICE_TUNER2:
1044 return "Tuner 2";
1045 case CECDEVICE_TUNER3:
1046 return "Tuner 3";
1047 case CECDEVICE_TUNER4:
1048 return "Tuner 4";
1049 case CECDEVICE_TV:
1050 return "TV";
1051 default:
1052 return "unknown";
1053 }
1054}
1055
1056const char *CCECProcessor::ToString(const cec_deck_control_mode mode)
1057{
1058 switch (mode)
1059 {
1060 case CEC_DECK_CONTROL_MODE_SKIP_FORWARD_WIND:
1061 return "skip forward wind";
1062 case CEC_DECK_CONTROL_MODE_EJECT:
1063 return "eject";
1064 case CEC_DECK_CONTROL_MODE_SKIP_REVERSE_REWIND:
1065 return "reverse rewind";
1066 case CEC_DECK_CONTROL_MODE_STOP:
1067 return "stop";
1068 default:
1069 return "unknown";
1070 }
1071}
1072
1073const char *CCECProcessor::ToString(const cec_deck_info status)
1074{
1075 switch (status)
1076 {
1077 case CEC_DECK_INFO_PLAY:
1078 return "play";
1079 case CEC_DECK_INFO_RECORD:
1080 return "record";
1081 case CEC_DECK_INFO_PLAY_REVERSE:
1082 return "play reverse";
1083 case CEC_DECK_INFO_STILL:
1084 return "still";
1085 case CEC_DECK_INFO_SLOW:
1086 return "slow";
1087 case CEC_DECK_INFO_SLOW_REVERSE:
1088 return "slow reverse";
1089 case CEC_DECK_INFO_FAST_FORWARD:
1090 return "fast forward";
1091 case CEC_DECK_INFO_FAST_REVERSE:
1092 return "fast reverse";
1093 case CEC_DECK_INFO_NO_MEDIA:
1094 return "no media";
1095 case CEC_DECK_INFO_STOP:
1096 return "stop";
1097 case CEC_DECK_INFO_SKIP_FORWARD_WIND:
1098 return "info skip forward wind";
1099 case CEC_DECK_INFO_SKIP_REVERSE_REWIND:
1100 return "info skip reverse rewind";
1101 case CEC_DECK_INFO_INDEX_SEARCH_FORWARD:
1102 return "info index search forward";
1103 case CEC_DECK_INFO_INDEX_SEARCH_REVERSE:
1104 return "info index search reverse";
1105 case CEC_DECK_INFO_OTHER_STATUS:
1106 return "other";
1107 default:
1108 return "unknown";
1109 }
1110}
1111
1112const char *CCECProcessor::ToString(const cec_opcode opcode)
1113{
1114 switch (opcode)
1115 {
1116 case CEC_OPCODE_ACTIVE_SOURCE:
1117 return "active source";
1118 case CEC_OPCODE_IMAGE_VIEW_ON:
1119 return "image view on";
1120 case CEC_OPCODE_TEXT_VIEW_ON:
1121 return "text view on";
1122 case CEC_OPCODE_INACTIVE_SOURCE:
1123 return "inactive source";
1124 case CEC_OPCODE_REQUEST_ACTIVE_SOURCE:
1125 return "request active source";
1126 case CEC_OPCODE_ROUTING_CHANGE:
1127 return "routing change";
1128 case CEC_OPCODE_ROUTING_INFORMATION:
1129 return "routing information";
1130 case CEC_OPCODE_SET_STREAM_PATH:
1131 return "set stream path";
1132 case CEC_OPCODE_STANDBY:
1133 return "standby";
1134 case CEC_OPCODE_RECORD_OFF:
1135 return "record off";
1136 case CEC_OPCODE_RECORD_ON:
1137 return "record on";
1138 case CEC_OPCODE_RECORD_STATUS:
1139 return "record status";
1140 case CEC_OPCODE_RECORD_TV_SCREEN:
1141 return "record tv screen";
1142 case CEC_OPCODE_CLEAR_ANALOGUE_TIMER:
1143 return "clear analogue timer";
1144 case CEC_OPCODE_CLEAR_DIGITAL_TIMER:
1145 return "clear digital timer";
1146 case CEC_OPCODE_CLEAR_EXTERNAL_TIMER:
1147 return "clear external timer";
1148 case CEC_OPCODE_SET_ANALOGUE_TIMER:
1149 return "set analogue timer";
1150 case CEC_OPCODE_SET_DIGITAL_TIMER:
1151 return "set digital timer";
1152 case CEC_OPCODE_SET_EXTERNAL_TIMER:
1153 return "set external timer";
1154 case CEC_OPCODE_SET_TIMER_PROGRAM_TITLE:
1155 return "set timer program title";
1156 case CEC_OPCODE_TIMER_CLEARED_STATUS:
1157 return "timer cleared status";
1158 case CEC_OPCODE_TIMER_STATUS:
1159 return "timer status";
1160 case CEC_OPCODE_CEC_VERSION:
1161 return "cec version";
1162 case CEC_OPCODE_GET_CEC_VERSION:
1163 return "get cec version";
1164 case CEC_OPCODE_GIVE_PHYSICAL_ADDRESS:
1165 return "give physical address";
1166 case CEC_OPCODE_GET_MENU_LANGUAGE:
1167 return "get menu language";
1168 case CEC_OPCODE_REPORT_PHYSICAL_ADDRESS:
1169 return "report physical address";
1170 case CEC_OPCODE_SET_MENU_LANGUAGE:
1171 return "set menu language";
1172 case CEC_OPCODE_DECK_CONTROL:
1173 return "deck control";
1174 case CEC_OPCODE_DECK_STATUS:
1175 return "deck status";
1176 case CEC_OPCODE_GIVE_DECK_STATUS:
1177 return "give deck status";
1178 case CEC_OPCODE_PLAY:
1179 return "play";
1180 case CEC_OPCODE_GIVE_TUNER_DEVICE_STATUS:
1181 return "give tuner status";
1182 case CEC_OPCODE_SELECT_ANALOGUE_SERVICE:
1183 return "select analogue service";
1184 case CEC_OPCODE_SELECT_DIGITAL_SERVICE:
1185 return "set digital service";
1186 case CEC_OPCODE_TUNER_DEVICE_STATUS:
1187 return "tuner device status";
1188 case CEC_OPCODE_TUNER_STEP_DECREMENT:
1189 return "tuner step decrement";
1190 case CEC_OPCODE_TUNER_STEP_INCREMENT:
1191 return "tuner step increment";
1192 case CEC_OPCODE_DEVICE_VENDOR_ID:
1193 return "device vendor id";
1194 case CEC_OPCODE_GIVE_DEVICE_VENDOR_ID:
1195 return "give device vendor id";
1196 case CEC_OPCODE_VENDOR_COMMAND:
1197 return "vendor command";
1198 case CEC_OPCODE_VENDOR_COMMAND_WITH_ID:
1199 return "vendor command with id";
1200 case CEC_OPCODE_VENDOR_REMOTE_BUTTON_DOWN:
1201 return "vendor remote button down";
1202 case CEC_OPCODE_VENDOR_REMOTE_BUTTON_UP:
1203 return "vendor remote button up";
1204 case CEC_OPCODE_SET_OSD_STRING:
1205 return "set osd string";
1206 case CEC_OPCODE_GIVE_OSD_NAME:
1207 return "give osd name";
1208 case CEC_OPCODE_SET_OSD_NAME:
1209 return "set osd name";
1210 case CEC_OPCODE_MENU_REQUEST:
1211 return "menu request";
1212 case CEC_OPCODE_MENU_STATUS:
1213 return "menu status";
1214 case CEC_OPCODE_USER_CONTROL_PRESSED:
1215 return "user control pressed";
1216 case CEC_OPCODE_USER_CONTROL_RELEASE:
1217 return "user control release";
1218 case CEC_OPCODE_GIVE_DEVICE_POWER_STATUS:
1219 return "give device power status";
1220 case CEC_OPCODE_REPORT_POWER_STATUS:
1221 return "report power status";
1222 case CEC_OPCODE_FEATURE_ABORT:
1223 return "feature abort";
1224 case CEC_OPCODE_ABORT:
1225 return "abort";
1226 case CEC_OPCODE_GIVE_AUDIO_STATUS:
1227 return "give audio status";
1228 case CEC_OPCODE_GIVE_SYSTEM_AUDIO_MODE_STATUS:
1229 return "give audio mode status";
1230 case CEC_OPCODE_REPORT_AUDIO_STATUS:
1231 return "report audio status";
1232 case CEC_OPCODE_SET_SYSTEM_AUDIO_MODE:
1233 return "set system audio mode";
1234 case CEC_OPCODE_SYSTEM_AUDIO_MODE_REQUEST:
1235 return "system audio mode request";
1236 case CEC_OPCODE_SYSTEM_AUDIO_MODE_STATUS:
1237 return "system audio mode status";
1238 case CEC_OPCODE_SET_AUDIO_RATE:
1239 return "set audio rate";
1240 default:
1241 return "UNKNOWN";
1242 }
1243}
1244
1245const char *CCECProcessor::ToString(const cec_system_audio_status mode)
1246{
1247 switch(mode)
1248 {
1249 case CEC_SYSTEM_AUDIO_STATUS_ON:
1250 return "on";
1251 case CEC_SYSTEM_AUDIO_STATUS_OFF:
1252 return "off";
1253 default:
1254 return "unknown";
1255 }
1256}
1257
1258const char *CCECProcessor::ToString(const cec_audio_status status)
1259{
1260 // TODO this is a mask
1261 return "TODO";
1262}
1263
1264const char *CCECProcessor::ToString(const cec_vendor_id vendor)
1265{
1266 switch (vendor)
1267 {
1268 case CEC_VENDOR_SAMSUNG:
1269 return "Samsung";
1270 case CEC_VENDOR_LG:
1271 return "LG";
1272 case CEC_VENDOR_PANASONIC:
1273 return "Panasonic";
1274 case CEC_VENDOR_PIONEER:
1275 return "Pioneer";
1276 case CEC_VENDOR_ONKYO:
1277 return "Onkyo";
1278 case CEC_VENDOR_YAMAHA:
1279 return "Yamaha";
ec0deac1
LOK
1280 case CEC_VENDOR_PHILIPS:
1281 return "Philips";
03ae897d
LOK
1282 default:
1283 return "Unknown";
1284 }
1285}
1286
7c63a480
LOK
1287void *CCECBusScan::Process(void)
1288{
1289 CCECBusDevice *device(NULL);
b750a5c3 1290 int iCount(50);
5e5637c6 1291 while (!IsStopped())
7c63a480 1292 {
ef79edcc 1293 if (iCount == 0)
7c63a480 1294 {
b750a5c3 1295 for (unsigned int iPtr = 0; iPtr <= 11 && !IsStopped(); iPtr++)
5e5637c6 1296 {
ef79edcc
LOK
1297 device = m_processor->m_busDevices[iPtr];
1298 if (device && device->GetStatus(true) == CEC_DEVICE_STATUS_PRESENT)
1299 {
1300 if (!IsStopped())
7f919115 1301 {
ef79edcc 1302 device->GetVendorId();
7f919115
LOK
1303 Sleep(5);
1304 }
1305 if (!IsStopped())
1306 {
1307 device->GetPowerStatus(true);
1308 Sleep(5);
1309 }
ef79edcc 1310 }
5e5637c6 1311 }
7c63a480 1312 }
ef79edcc
LOK
1313
1314 if (++iCount > 60)
1315 iCount = 0;
1316 Sleep(1000);
7c63a480
LOK
1317 }
1318 return NULL;
1319}
1113cb7d
LOK
1320
1321bool CCECProcessor::StartBootloader(void)
1322{
1323 return m_communication->StartBootloader();
1324}
1325
1326bool CCECProcessor::PingAdapter(void)
1327{
1328 return m_communication->PingAdapter();
1329}