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