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