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