cec: handle CEC_OPCODE_ACTIVE_SOURCE
[deb_libcec.git] / src / lib / CECProcessor.cpp
... / ...
CommitLineData
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
47using namespace CEC;
48using namespace std;
49
50CCECProcessor::CCECProcessor(CLibCEC *controller, CAdapterCommunication *serComm, const char *strDeviceName, cec_logical_address iLogicalAddress /* = CECDEVICE_PLAYBACKDEVICE1 */, uint16_t iPhysicalAddress /* = CEC_DEFAULT_PHYSICAL_ADDRESS*/) :
51 m_bStarted(false),
52 m_strDeviceName(strDeviceName),
53 m_communication(serComm),
54 m_controller(controller),
55 m_bMonitor(false)
56{
57 m_logicalAddresses.clear();
58 m_logicalAddresses.set(iLogicalAddress);
59 m_types.clear();
60 for (int iPtr = 0; iPtr < 16; iPtr++)
61 m_busDevices[iPtr] = new CCECBusDevice(this, (cec_logical_address) iPtr, iPtr == iLogicalAddress ? iPhysicalAddress : 0);
62}
63
64CCECProcessor::CCECProcessor(CLibCEC *controller, CAdapterCommunication *serComm, const char *strDeviceName, const cec_device_type_list &types) :
65 m_bStarted(false),
66 m_strDeviceName(strDeviceName),
67 m_types(types),
68 m_communication(serComm),
69 m_controller(controller),
70 m_bMonitor(false)
71{
72 m_logicalAddresses.clear();
73 for (int iPtr = 0; iPtr < 16; iPtr++)
74 {
75 switch(iPtr)
76 {
77 case CECDEVICE_AUDIOSYSTEM:
78 m_busDevices[iPtr] = new CCECAudioSystem(this, (cec_logical_address) iPtr, 0xFFFF);
79 break;
80 case CECDEVICE_PLAYBACKDEVICE1:
81 case CECDEVICE_PLAYBACKDEVICE2:
82 case CECDEVICE_PLAYBACKDEVICE3:
83 m_busDevices[iPtr] = new CCECPlaybackDevice(this, (cec_logical_address) iPtr, 0xFFFF);
84 break;
85 case CECDEVICE_RECORDINGDEVICE1:
86 case CECDEVICE_RECORDINGDEVICE2:
87 case CECDEVICE_RECORDINGDEVICE3:
88 m_busDevices[iPtr] = new CCECRecordingDevice(this, (cec_logical_address) iPtr, 0xFFFF);
89 break;
90 case CECDEVICE_TUNER1:
91 case CECDEVICE_TUNER2:
92 case CECDEVICE_TUNER3:
93 case CECDEVICE_TUNER4:
94 m_busDevices[iPtr] = new CCECTuner(this, (cec_logical_address) iPtr, 0xFFFF);
95 break;
96 case CECDEVICE_TV:
97 m_busDevices[iPtr] = new CCECTV(this, (cec_logical_address) iPtr, 0);
98 break;
99 default:
100 m_busDevices[iPtr] = new CCECBusDevice(this, (cec_logical_address) iPtr, 0xFFFF);
101 break;
102 }
103 }
104}
105
106CCECProcessor::~CCECProcessor(void)
107{
108 m_startCondition.Broadcast();
109 StopThread();
110 m_communication = NULL;
111 m_controller = NULL;
112 for (unsigned int iPtr = 0; iPtr < 16; iPtr++)
113 delete m_busDevices[iPtr];
114}
115
116bool CCECProcessor::Start(void)
117{
118 CLockObject lock(&m_mutex);
119 if (!m_communication || !m_communication->IsOpen())
120 {
121 m_controller->AddLog(CEC_LOG_ERROR, "connection is closed");
122 return false;
123 }
124
125 if (CreateThread())
126 {
127 if (!m_startCondition.Wait(&m_mutex) || !m_bStarted)
128 {
129 m_controller->AddLog(CEC_LOG_ERROR, "could not create a processor thread");
130 return false;
131 }
132 return true;
133 }
134 else
135 m_controller->AddLog(CEC_LOG_ERROR, "could not create a processor thread");
136
137 return false;
138}
139
140bool CCECProcessor::TryLogicalAddress(cec_logical_address address, unsigned int iIndex)
141{
142 const char *strLabel = CCECCommandHandler::ToString(address);
143 CStdString strLog;
144 strLog.Format("trying logical address '%s'", strLabel);
145 AddLog(CEC_LOG_DEBUG, strLog);
146
147 SetAckMask(0x1 << address);
148 if (!m_busDevices[address]->TransmitPoll(address))
149 {
150 strLog.Format("using logical address '%s'", strLabel);
151 AddLog(CEC_LOG_NOTICE, strLog);
152
153 /* only set our OSD name and active source for the primary device */
154 if (m_logicalAddresses.empty())
155 {
156 m_busDevices[address]->m_strDeviceName = m_strDeviceName;
157 m_busDevices[address]->m_bActiveSource = true;
158 }
159 m_busDevices[address]->m_powerStatus = (m_types[0] == m_busDevices[address]->m_type) ? CEC_POWER_STATUS_ON : CEC_POWER_STATUS_STANDBY;
160 m_busDevices[address]->m_cecVersion = CEC_VERSION_1_3A;
161 m_logicalAddresses.set(address);
162
163 // TODO
164 m_busDevices[address]->SetPhysicalAddress(CEC_DEFAULT_PHYSICAL_ADDRESS + (iIndex * 0x100));
165
166 return true;
167 }
168
169 strLog.Format("logical address '%s' already taken", strLabel);
170 AddLog(CEC_LOG_DEBUG, strLog);
171 return false;
172}
173
174bool CCECProcessor::FindLogicalAddressRecordingDevice(unsigned int iIndex)
175{
176 AddLog(CEC_LOG_DEBUG, "detecting logical address for type 'recording device'");
177 return TryLogicalAddress(CECDEVICE_RECORDINGDEVICE1, iIndex) ||
178 TryLogicalAddress(CECDEVICE_RECORDINGDEVICE2, iIndex) ||
179 TryLogicalAddress(CECDEVICE_RECORDINGDEVICE3, iIndex);
180}
181
182bool CCECProcessor::FindLogicalAddressTuner(unsigned int iIndex)
183{
184 AddLog(CEC_LOG_DEBUG, "detecting logical address for type 'tuner'");
185 return TryLogicalAddress(CECDEVICE_TUNER1, iIndex) ||
186 TryLogicalAddress(CECDEVICE_TUNER2, iIndex) ||
187 TryLogicalAddress(CECDEVICE_TUNER3, iIndex) ||
188 TryLogicalAddress(CECDEVICE_TUNER4, iIndex);
189}
190
191bool CCECProcessor::FindLogicalAddressPlaybackDevice(unsigned int iIndex)
192{
193 AddLog(CEC_LOG_DEBUG, "detecting logical address for type 'playback device'");
194 return TryLogicalAddress(CECDEVICE_PLAYBACKDEVICE1, iIndex) ||
195 TryLogicalAddress(CECDEVICE_PLAYBACKDEVICE2, iIndex) ||
196 TryLogicalAddress(CECDEVICE_PLAYBACKDEVICE3, iIndex);
197}
198
199bool CCECProcessor::FindLogicalAddressAudioSystem(unsigned int iIndex)
200{
201 AddLog(CEC_LOG_DEBUG, "detecting logical address for type 'audio'");
202 return TryLogicalAddress(CECDEVICE_AUDIOSYSTEM, iIndex);
203}
204
205bool CCECProcessor::FindLogicalAddresses(void)
206{
207 bool bReturn(true);
208 m_logicalAddresses.clear();
209 CStdString strLog;
210
211 for (unsigned int iPtr = 0; iPtr < 5; iPtr++)
212 {
213 if (m_types.types[iPtr] == CEC_DEVICE_TYPE_RESERVED)
214 continue;
215
216 strLog.Format("%s - device %d: type %d", __FUNCTION__, iPtr, m_types.types[iPtr]);
217 AddLog(CEC_LOG_DEBUG, strLog);
218
219 if (m_types.types[iPtr] == CEC_DEVICE_TYPE_RECORDING_DEVICE)
220 bReturn &= FindLogicalAddressRecordingDevice(iPtr);
221 if (m_types.types[iPtr] == CEC_DEVICE_TYPE_TUNER)
222 bReturn &= FindLogicalAddressTuner(iPtr);
223 if (m_types.types[iPtr] == CEC_DEVICE_TYPE_PLAYBACK_DEVICE)
224 bReturn &= FindLogicalAddressPlaybackDevice(iPtr);
225 if (m_types.types[iPtr] == CEC_DEVICE_TYPE_AUDIO_SYSTEM)
226 bReturn &= FindLogicalAddressAudioSystem(iPtr);
227 }
228
229 return bReturn;
230}
231
232void *CCECProcessor::Process(void)
233{
234 bool bParseFrame(false);
235 cec_command command;
236 CCECAdapterMessage msg;
237
238 {
239 if (m_logicalAddresses.empty() && !FindLogicalAddresses())
240 {
241 CLockObject lock(&m_mutex);
242 m_controller->AddLog(CEC_LOG_ERROR, "could not detect our logical addressed");
243 m_startCondition.Signal();
244 return NULL;
245 }
246
247 SetAckMask(m_logicalAddresses.ackmask());
248
249 {
250 CLockObject lock(&m_mutex);
251 m_controller->AddLog(CEC_LOG_DEBUG, "processor thread started");
252 m_bStarted = true;
253 m_startCondition.Signal();
254 }
255 }
256
257 while (!IsStopped())
258 {
259 command.clear();
260 msg.clear();
261
262 {
263 CLockObject lock(&m_mutex);
264 if (m_commandBuffer.Pop(command))
265 {
266 bParseFrame = true;
267 }
268 else if (m_communication->IsOpen() && m_communication->Read(msg, 50))
269 {
270 m_controller->AddLog(msg.is_error() ? CEC_LOG_WARNING : CEC_LOG_DEBUG, msg.ToString());
271 if ((bParseFrame = (ParseMessage(msg) && !IsStopped())))
272 command = m_currentframe;
273 }
274 }
275
276 if (bParseFrame)
277 ParseCommand(command);
278 bParseFrame = false;
279
280 Sleep(5);
281
282 m_controller->CheckKeypressTimeout();
283
284 for (unsigned int iDevicePtr = 0; iDevicePtr < 16; iDevicePtr++)
285 m_busDevices[iDevicePtr]->PollVendorId();
286
287 Sleep(5);
288 }
289
290 return NULL;
291}
292
293bool CCECProcessor::SetActiveView(void)
294{
295 bool bReturn(false);
296
297 if (!IsRunning())
298 return bReturn;
299
300 if (!m_logicalAddresses.empty() && m_busDevices[m_logicalAddresses.primary])
301 {
302 SetStreamPath(m_busDevices[m_logicalAddresses.primary]->GetPhysicalAddress());
303 bReturn = m_busDevices[m_logicalAddresses.primary]->TransmitActiveSource();
304 }
305 return false;
306}
307
308bool CCECProcessor::SetStreamPath(uint16_t iStreamPath)
309{
310 CCECBusDevice *device = GetDeviceByPhysicalAddress(iStreamPath);
311 if (device)
312 {
313 for (unsigned int iPtr = 0; iPtr < 16; iPtr++)
314 m_busDevices[iPtr]->m_bActiveSource = false;
315
316 device->m_bActiveSource = true;
317 return true;
318 }
319
320 return false;
321}
322
323bool CCECProcessor::SetInactiveView(void)
324{
325 if (!IsRunning())
326 return false;
327
328 if (!m_logicalAddresses.empty() && m_busDevices[m_logicalAddresses.primary])
329 return m_busDevices[m_logicalAddresses.primary]->TransmitInactiveView();
330 return false;
331}
332
333void CCECProcessor::LogOutput(const cec_command &data)
334{
335 CStdString strTx;
336 strTx.Format("<< %02x", ((uint8_t)data.initiator << 4) + (uint8_t)data.destination);
337 if (data.opcode_set)
338 strTx.AppendFormat(":%02x", (uint8_t)data.opcode);
339
340 for (uint8_t iPtr = 0; iPtr < data.parameters.size; iPtr++)
341 strTx.AppendFormat(":%02x", data.parameters[iPtr]);
342 m_controller->AddLog(CEC_LOG_TRAFFIC, strTx.c_str());
343}
344
345bool CCECProcessor::SetLogicalAddress(cec_logical_address iLogicalAddress)
346{
347 if (m_logicalAddresses.primary != iLogicalAddress)
348 {
349 CStdString strLog;
350 strLog.Format("<< setting primary logical address to %1x", iLogicalAddress);
351 m_controller->AddLog(CEC_LOG_NOTICE, strLog.c_str());
352 m_logicalAddresses.primary = iLogicalAddress;
353 m_logicalAddresses.set(iLogicalAddress);
354 return SetAckMask(m_logicalAddresses.ackmask());
355 }
356
357 return true;
358}
359
360bool CCECProcessor::SetPhysicalAddress(uint16_t iPhysicalAddress)
361{
362 if (!m_logicalAddresses.empty() && m_busDevices[m_logicalAddresses.primary])
363 {
364 m_busDevices[m_logicalAddresses.primary]->SetPhysicalAddress(iPhysicalAddress);
365 return SetActiveView();
366 }
367 return false;
368}
369
370bool CCECProcessor::SwitchMonitoring(bool bEnable)
371{
372 CStdString strLog;
373 strLog.Format("== %s monitoring mode ==", bEnable ? "enabling" : "disabling");
374 m_controller->AddLog(CEC_LOG_NOTICE, strLog.c_str());
375
376 m_bMonitor = bEnable;
377 if (bEnable)
378 return SetAckMask(0);
379 else
380 return SetAckMask(m_logicalAddresses.ackmask());
381}
382
383bool CCECProcessor::PollDevice(cec_logical_address iAddress)
384{
385 if (iAddress != CECDEVICE_UNKNOWN && m_busDevices[iAddress])
386 return m_busDevices[m_logicalAddresses.primary]->TransmitPoll(iAddress);
387 return false;
388}
389
390
391CCECBusDevice *CCECProcessor::GetDeviceByPhysicalAddress(uint16_t iPhysicalAddress) const
392{
393 CCECBusDevice *device = NULL;
394
395 for (unsigned int iPtr = 0; iPtr < 16; iPtr++)
396 {
397 if (m_busDevices[iPtr]->GetPhysicalAddress() == iPhysicalAddress)
398 {
399 device = m_busDevices[iPtr];
400 break;
401 }
402 }
403
404 return device;
405}
406
407cec_version CCECProcessor::GetDeviceCecVersion(cec_logical_address iAddress)
408{
409 return m_busDevices[iAddress]->GetCecVersion();
410}
411
412bool CCECProcessor::GetDeviceMenuLanguage(cec_logical_address iAddress, cec_menu_language *language)
413{
414 if (m_busDevices[iAddress])
415 {
416 *language = m_busDevices[iAddress]->GetMenuLanguage();
417 return (strcmp(language->language, "???") != 0);
418 }
419 return false;
420}
421
422uint64_t CCECProcessor::GetDeviceVendorId(cec_logical_address iAddress)
423{
424 if (m_busDevices[iAddress])
425 return m_busDevices[iAddress]->GetVendorId();
426 return false;
427}
428
429cec_power_status CCECProcessor::GetDevicePowerStatus(cec_logical_address iAddress)
430{
431 if (m_busDevices[iAddress])
432 return m_busDevices[iAddress]->GetPowerStatus();
433 return CEC_POWER_STATUS_UNKNOWN;
434}
435
436bool CCECProcessor::Transmit(const cec_command &data)
437{
438 bool bReturn(false);
439 LogOutput(data);
440
441 CCECAdapterMessage *output = new CCECAdapterMessage(data);
442 bReturn = Transmit(output);
443 delete output;
444
445 return bReturn;
446}
447
448bool CCECProcessor::Transmit(CCECAdapterMessage *output)
449{
450 bool bReturn(false);
451 CLockObject lock(&m_mutex);
452 {
453 CLockObject msgLock(&output->mutex);
454 if (!m_communication || !m_communication->Write(output))
455 return bReturn;
456 else
457 {
458 output->condition.Wait(&output->mutex);
459 if (output->state != ADAPTER_MESSAGE_STATE_SENT)
460 {
461 m_controller->AddLog(CEC_LOG_ERROR, "command was not sent");
462 return bReturn;
463 }
464 }
465
466 if (output->transmit_timeout > 0)
467 {
468 if ((bReturn = WaitForTransmitSucceeded(output->size(), output->transmit_timeout)) == false)
469 m_controller->AddLog(CEC_LOG_DEBUG, "did not receive ack");
470 }
471 else
472 bReturn = true;
473 }
474
475 return bReturn;
476}
477
478void CCECProcessor::TransmitAbort(cec_logical_address address, cec_opcode opcode, ECecAbortReason reason /* = CEC_ABORT_REASON_UNRECOGNIZED_OPCODE */)
479{
480 m_controller->AddLog(CEC_LOG_DEBUG, "<< transmitting abort message");
481
482 cec_command command;
483 // TODO
484 cec_command::format(command, m_logicalAddresses.primary, address, CEC_OPCODE_FEATURE_ABORT);
485 command.parameters.push_back((uint8_t)opcode);
486 command.parameters.push_back((uint8_t)reason);
487
488 Transmit(command);
489}
490
491bool CCECProcessor::WaitForTransmitSucceeded(uint8_t iLength, uint32_t iTimeout /* = 1000 */)
492{
493 bool bError(false);
494 bool bTransmitSucceeded(false);
495 uint8_t iPacketsLeft(iLength / 4);
496
497 int64_t iNow = GetTimeMs();
498 int64_t iTargetTime = iNow + (uint64_t) iTimeout;
499
500 while (!bTransmitSucceeded && !bError && (iTimeout == 0 || iNow < iTargetTime))
501 {
502 CCECAdapterMessage msg;
503
504 if (!m_communication->Read(msg, iTimeout > 0 ? (int32_t)(iTargetTime - iNow) : 1000))
505 {
506 iNow = GetTimeMs();
507 continue;
508 }
509
510 if ((bError = msg.is_error()) == false)
511 {
512 m_controller->AddLog(bError ? CEC_LOG_WARNING : CEC_LOG_DEBUG, msg.ToString());
513
514 switch(msg.message())
515 {
516 case MSGCODE_COMMAND_ACCEPTED:
517 if (iPacketsLeft > 0)
518 iPacketsLeft--;
519 break;
520 case MSGCODE_TRANSMIT_SUCCEEDED:
521 bTransmitSucceeded = (iPacketsLeft == 0);
522 bError = !bTransmitSucceeded;
523 break;
524 default:
525 if (ParseMessage(msg))
526 m_commandBuffer.Push(m_currentframe);
527 }
528
529 iNow = GetTimeMs();
530 }
531 }
532
533 return bTransmitSucceeded && !bError;
534}
535
536bool CCECProcessor::ParseMessage(const CCECAdapterMessage &msg)
537{
538 bool bEom = false;
539
540 if (msg.empty())
541 return bEom;
542
543 switch(msg.message())
544 {
545 case MSGCODE_FRAME_START:
546 {
547 m_currentframe.clear();
548 if (msg.size() >= 2)
549 {
550 m_currentframe.initiator = msg.initiator();
551 m_currentframe.destination = msg.destination();
552 m_currentframe.ack = msg.ack();
553 m_currentframe.eom = msg.eom();
554 }
555 }
556 break;
557 case MSGCODE_FRAME_DATA:
558 {
559 if (msg.size() >= 2)
560 {
561 m_currentframe.push_back(msg[1]);
562 m_currentframe.eom = msg.eom();
563 }
564 bEom = msg.eom();
565 }
566 break;
567 default:
568 break;
569 }
570
571 return bEom;
572}
573
574void CCECProcessor::ParseCommand(cec_command &command)
575{
576 CStdString dataStr;
577 dataStr.Format(">> %1x%1x:%02x", command.initiator, command.destination, command.opcode);
578 for (uint8_t iPtr = 0; iPtr < command.parameters.size; iPtr++)
579 dataStr.AppendFormat(":%02x", (unsigned int)command.parameters[iPtr]);
580 m_controller->AddLog(CEC_LOG_TRAFFIC, dataStr.c_str());
581
582 if (!m_bMonitor)
583 m_busDevices[(uint8_t)command.initiator]->HandleCommand(command);
584}
585
586uint16_t CCECProcessor::GetPhysicalAddress(void) const
587{
588 if (!m_logicalAddresses.empty() && m_busDevices[m_logicalAddresses.primary])
589 return m_busDevices[m_logicalAddresses.primary]->GetPhysicalAddress();
590 return false;
591}
592
593void CCECProcessor::SetCurrentButton(cec_user_control_code iButtonCode)
594{
595 m_controller->SetCurrentButton(iButtonCode);
596}
597
598void CCECProcessor::AddCommand(const cec_command &command)
599{
600 m_controller->AddCommand(command);
601}
602
603void CCECProcessor::AddKey(cec_keypress &key)
604{
605 m_controller->AddKey(key);
606}
607
608void CCECProcessor::AddKey(void)
609{
610 m_controller->AddKey();
611}
612
613void CCECProcessor::AddLog(cec_log_level level, const CStdString &strMessage)
614{
615 m_controller->AddLog(level, strMessage);
616}
617
618bool CCECProcessor::SetAckMask(uint16_t iMask)
619{
620 bool bReturn(false);
621 CStdString strLog;
622 strLog.Format("setting ackmask to %2x", iMask);
623 m_controller->AddLog(CEC_LOG_DEBUG, strLog.c_str());
624
625 CCECAdapterMessage *output = new CCECAdapterMessage;
626
627 output->push_back(MSGSTART);
628 output->push_escaped(MSGCODE_SET_ACK_MASK);
629 output->push_escaped(iMask >> 8);
630 output->push_escaped((uint8_t)iMask);
631 output->push_back(MSGEND);
632
633 if ((bReturn = Transmit(output)) == false)
634 m_controller->AddLog(CEC_LOG_ERROR, "could not set the ackmask");
635
636 delete output;
637
638 return bReturn;
639}