2 * This file is part of the libCEC(R) library.
4 * libCEC(R) is Copyright (C) 2011 Pulse-Eight Limited. All rights reserved.
5 * libCEC(R) is an original work, containing original code.
7 * libCEC(R) is a trademark of Pulse-Eight Limited.
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.
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.
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.
24 * Alternatively, you can license this library under a commercial license,
25 * please contact Pulse-Eight Licensing for more information.
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/
33 #include "CECProcessor.h"
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"
43 #include "util/StdString.h"
44 #include "platform/timeutils.h"
49 CCECProcessor::CCECProcessor(CLibCEC
*controller
, CAdapterCommunication
*serComm
, const char *strDeviceName
, cec_logical_address iLogicalAddress
/* = CECDEVICE_PLAYBACKDEVICE1 */, uint16_t iPhysicalAddress
/* = CEC_DEFAULT_PHYSICAL_ADDRESS*/) :
51 m_strDeviceName(strDeviceName
),
52 m_communication(serComm
),
53 m_controller(controller
),
56 m_logicalAddresses
.clear();
57 m_logicalAddresses
.set(iLogicalAddress
);
59 for (int iPtr
= 0; iPtr
< 16; iPtr
++)
60 m_busDevices
[iPtr
] = new CCECBusDevice(this, (cec_logical_address
) iPtr
, iPtr
== iLogicalAddress
? iPhysicalAddress
: 0);
63 CCECProcessor::CCECProcessor(CLibCEC
*controller
, CAdapterCommunication
*serComm
, const char *strDeviceName
, const cec_device_type_list
&types
) :
65 m_strDeviceName(strDeviceName
),
67 m_communication(serComm
),
68 m_controller(controller
),
71 m_logicalAddresses
.clear();
72 for (int iPtr
= 0; iPtr
< 16; iPtr
++)
76 case CECDEVICE_AUDIOSYSTEM
:
77 m_busDevices
[iPtr
] = new CCECAudioSystem(this, (cec_logical_address
) iPtr
, 0xFFFF);
79 case CECDEVICE_PLAYBACKDEVICE1
:
80 case CECDEVICE_PLAYBACKDEVICE2
:
81 case CECDEVICE_PLAYBACKDEVICE3
:
82 m_busDevices
[iPtr
] = new CCECPlaybackDevice(this, (cec_logical_address
) iPtr
, 0xFFFF);
84 case CECDEVICE_RECORDINGDEVICE1
:
85 case CECDEVICE_RECORDINGDEVICE2
:
86 case CECDEVICE_RECORDINGDEVICE3
:
87 m_busDevices
[iPtr
] = new CCECRecordingDevice(this, (cec_logical_address
) iPtr
, 0xFFFF);
89 case CECDEVICE_TUNER1
:
90 case CECDEVICE_TUNER2
:
91 case CECDEVICE_TUNER3
:
92 case CECDEVICE_TUNER4
:
93 m_busDevices
[iPtr
] = new CCECTuner(this, (cec_logical_address
) iPtr
, 0xFFFF);
96 m_busDevices
[iPtr
] = new CCECTV(this, (cec_logical_address
) iPtr
, 0);
99 m_busDevices
[iPtr
] = new CCECBusDevice(this, (cec_logical_address
) iPtr
, 0xFFFF);
105 CCECProcessor::~CCECProcessor(void)
107 m_startCondition
.Broadcast();
109 m_communication
= NULL
;
111 for (unsigned int iPtr
= 0; iPtr
< 16; iPtr
++)
112 delete m_busDevices
[iPtr
];
115 bool CCECProcessor::Start(void)
117 CLockObject
lock(&m_mutex
);
118 if (!m_communication
|| !m_communication
->IsOpen())
120 m_controller
->AddLog(CEC_LOG_ERROR
, "connection is closed");
126 if (!m_startCondition
.Wait(&m_mutex
) || !m_bStarted
)
128 m_controller
->AddLog(CEC_LOG_ERROR
, "could not create a processor thread");
134 m_controller
->AddLog(CEC_LOG_ERROR
, "could not create a processor thread");
139 bool CCECProcessor::TryLogicalAddress(cec_logical_address address
, const char *strLabel
, unsigned int iIndex
)
142 strLog
.Format("trying logical address '%s'", strLabel
);
143 AddLog(CEC_LOG_DEBUG
, strLog
);
145 SetAckMask(0x1 << address
);
146 if (!m_busDevices
[address
]->TransmitPoll(address
))
148 strLog
.Format("using logical address '%s'", strLabel
);
149 AddLog(CEC_LOG_NOTICE
, strLog
);
151 /* only set our OSD name and active source for the primary device */
152 if (m_logicalAddresses
.empty())
154 m_busDevices
[address
]->m_strDeviceName
= m_strDeviceName
;
155 m_busDevices
[address
]->m_bActiveSource
= true;
157 m_busDevices
[address
]->m_powerStatus
= (m_types
[0] == m_busDevices
[address
]->m_type
) ? CEC_POWER_STATUS_ON
: CEC_POWER_STATUS_STANDBY
;
158 m_busDevices
[address
]->m_cecVersion
= CEC_VERSION_1_3A
;
159 m_logicalAddresses
.set(address
);
162 m_busDevices
[address
]->SetPhysicalAddress(CEC_DEFAULT_PHYSICAL_ADDRESS
+ (iIndex
* 0x100));
167 strLog
.Format("logical address '%s' already taken", strLabel
);
168 AddLog(CEC_LOG_DEBUG
, strLog
);
172 bool CCECProcessor::FindLogicalAddressRecordingDevice(unsigned int iIndex
)
174 AddLog(CEC_LOG_DEBUG
, "detecting logical address for type 'recording device'");
175 return TryLogicalAddress(CECDEVICE_RECORDINGDEVICE1
, "recording 1", iIndex
) ||
176 TryLogicalAddress(CECDEVICE_RECORDINGDEVICE2
, "recording 2", iIndex
) ||
177 TryLogicalAddress(CECDEVICE_RECORDINGDEVICE3
, "recording 3", iIndex
);
180 bool CCECProcessor::FindLogicalAddressTuner(unsigned int iIndex
)
182 AddLog(CEC_LOG_DEBUG
, "detecting logical address for type 'tuner'");
183 return TryLogicalAddress(CECDEVICE_TUNER1
, "tuner 1", iIndex
) ||
184 TryLogicalAddress(CECDEVICE_TUNER2
, "tuner 2", iIndex
) ||
185 TryLogicalAddress(CECDEVICE_TUNER3
, "tuner 3", iIndex
) ||
186 TryLogicalAddress(CECDEVICE_TUNER4
, "tuner 4", iIndex
);
189 bool CCECProcessor::FindLogicalAddressPlaybackDevice(unsigned int iIndex
)
191 AddLog(CEC_LOG_DEBUG
, "detecting logical address for type 'playback device'");
192 return TryLogicalAddress(CECDEVICE_PLAYBACKDEVICE1
, "playback 1", iIndex
) ||
193 TryLogicalAddress(CECDEVICE_PLAYBACKDEVICE2
, "playback 2", iIndex
) ||
194 TryLogicalAddress(CECDEVICE_PLAYBACKDEVICE3
, "playback 3", iIndex
);
197 bool CCECProcessor::FindLogicalAddressAudioSystem(unsigned int iIndex
)
199 AddLog(CEC_LOG_DEBUG
, "detecting logical address for type 'audio'");
200 return TryLogicalAddress(CECDEVICE_AUDIOSYSTEM
, "audio", iIndex
);
203 bool CCECProcessor::FindLogicalAddresses(void)
206 m_logicalAddresses
.clear();
209 for (unsigned int iPtr
= 0; iPtr
< 5; iPtr
++)
211 if (m_types
.types
[iPtr
] == CEC_DEVICE_TYPE_RESERVED
)
214 strLog
.Format("%s - device %d: type %d", __FUNCTION__
, iPtr
, m_types
.types
[iPtr
]);
215 AddLog(CEC_LOG_DEBUG
, strLog
);
217 if (m_types
.types
[iPtr
] == CEC_DEVICE_TYPE_RECORDING_DEVICE
)
218 bReturn
&= FindLogicalAddressRecordingDevice(iPtr
);
219 if (m_types
.types
[iPtr
] == CEC_DEVICE_TYPE_TUNER
)
220 bReturn
&= FindLogicalAddressTuner(iPtr
);
221 if (m_types
.types
[iPtr
] == CEC_DEVICE_TYPE_PLAYBACK_DEVICE
)
222 bReturn
&= FindLogicalAddressPlaybackDevice(iPtr
);
223 if (m_types
.types
[iPtr
] == CEC_DEVICE_TYPE_AUDIO_SYSTEM
)
224 bReturn
&= FindLogicalAddressAudioSystem(iPtr
);
230 void *CCECProcessor::Process(void)
232 bool bParseFrame(false);
234 CCECAdapterMessage msg
;
237 if (m_logicalAddresses
.empty() && !FindLogicalAddresses())
239 CLockObject
lock(&m_mutex
);
240 m_controller
->AddLog(CEC_LOG_ERROR
, "could not detect our logical addressed");
241 m_startCondition
.Signal();
245 SetAckMask(m_logicalAddresses
.ackmask());
248 CLockObject
lock(&m_mutex
);
249 m_controller
->AddLog(CEC_LOG_DEBUG
, "processor thread started");
251 m_startCondition
.Signal();
261 CLockObject
lock(&m_mutex
);
262 if (m_commandBuffer
.Pop(command
))
266 else if (m_communication
->IsOpen() && m_communication
->Read(msg
, 50))
268 m_controller
->AddLog(msg
.is_error() ? CEC_LOG_WARNING
: CEC_LOG_DEBUG
, msg
.ToString());
269 if ((bParseFrame
= (ParseMessage(msg
) && !IsStopped())))
270 command
= m_currentframe
;
275 ParseCommand(command
);
280 m_controller
->CheckKeypressTimeout();
282 for (unsigned int iDevicePtr
= 0; iDevicePtr
< 16; iDevicePtr
++)
283 m_busDevices
[iDevicePtr
]->PollVendorId();
291 bool CCECProcessor::SetActiveView(void)
296 if (!m_logicalAddresses
.empty() && m_busDevices
[m_logicalAddresses
.primary
])
297 return m_busDevices
[m_logicalAddresses
.primary
]->TransmitActiveView();
301 bool CCECProcessor::SetInactiveView(void)
306 if (!m_logicalAddresses
.empty() && m_busDevices
[m_logicalAddresses
.primary
])
307 return m_busDevices
[m_logicalAddresses
.primary
]->TransmitInactiveView();
311 void CCECProcessor::LogOutput(const cec_command
&data
)
314 strTx
.Format("<< %02x", ((uint8_t)data
.initiator
<< 4) + (uint8_t)data
.destination
);
316 strTx
.AppendFormat(":%02x", (uint8_t)data
.opcode
);
318 for (uint8_t iPtr
= 0; iPtr
< data
.parameters
.size
; iPtr
++)
319 strTx
.AppendFormat(":%02x", data
.parameters
[iPtr
]);
320 m_controller
->AddLog(CEC_LOG_TRAFFIC
, strTx
.c_str());
323 bool CCECProcessor::SetLogicalAddress(cec_logical_address iLogicalAddress
)
325 if (m_logicalAddresses
.primary
!= iLogicalAddress
)
328 strLog
.Format("<< setting primary logical address to %1x", iLogicalAddress
);
329 m_controller
->AddLog(CEC_LOG_NOTICE
, strLog
.c_str());
330 m_logicalAddresses
.primary
= iLogicalAddress
;
331 m_logicalAddresses
.set(iLogicalAddress
);
332 return SetAckMask(m_logicalAddresses
.ackmask());
338 bool CCECProcessor::SetPhysicalAddress(uint16_t iPhysicalAddress
)
340 if (!m_logicalAddresses
.empty() && m_busDevices
[m_logicalAddresses
.primary
])
342 m_busDevices
[m_logicalAddresses
.primary
]->SetPhysicalAddress(iPhysicalAddress
);
343 return m_busDevices
[m_logicalAddresses
.primary
]->TransmitActiveView();
348 bool CCECProcessor::SwitchMonitoring(bool bEnable
)
351 strLog
.Format("== %s monitoring mode ==", bEnable
? "enabling" : "disabling");
352 m_controller
->AddLog(CEC_LOG_NOTICE
, strLog
.c_str());
354 m_bMonitor
= bEnable
;
356 return SetAckMask(0);
358 return SetAckMask(m_logicalAddresses
.ackmask());
361 bool CCECProcessor::PollDevice(cec_logical_address iAddress
)
363 if (iAddress
!= CECDEVICE_UNKNOWN
&& m_busDevices
[iAddress
])
364 return m_busDevices
[m_logicalAddresses
.primary
]->TransmitPoll(iAddress
);
368 cec_version
CCECProcessor::GetDeviceCecVersion(cec_logical_address iAddress
)
370 return m_busDevices
[iAddress
]->GetCecVersion();
373 bool CCECProcessor::GetDeviceMenuLanguage(cec_logical_address iAddress
, cec_menu_language
*language
)
375 if (m_busDevices
[iAddress
])
377 *language
= m_busDevices
[iAddress
]->GetMenuLanguage();
378 return (strcmp(language
->language
, "???") != 0);
383 uint64_t CCECProcessor::GetDeviceVendorId(cec_logical_address iAddress
)
385 if (m_busDevices
[iAddress
])
386 return m_busDevices
[iAddress
]->GetVendorId();
390 cec_power_status
CCECProcessor::GetDevicePowerStatus(cec_logical_address iAddress
)
392 if (m_busDevices
[iAddress
])
393 return m_busDevices
[iAddress
]->GetPowerStatus();
394 return CEC_POWER_STATUS_UNKNOWN
;
397 bool CCECProcessor::Transmit(const cec_command
&data
)
402 CCECAdapterMessage
*output
= new CCECAdapterMessage(data
);
403 bReturn
= Transmit(output
);
409 bool CCECProcessor::Transmit(CCECAdapterMessage
*output
)
412 CLockObject
lock(&m_mutex
);
414 CLockObject
msgLock(&output
->mutex
);
415 if (!m_communication
|| !m_communication
->Write(output
))
419 output
->condition
.Wait(&output
->mutex
);
420 if (output
->state
!= ADAPTER_MESSAGE_STATE_SENT
)
422 m_controller
->AddLog(CEC_LOG_ERROR
, "command was not sent");
427 if (output
->transmit_timeout
> 0)
429 if ((bReturn
= WaitForTransmitSucceeded(output
->size(), output
->transmit_timeout
)) == false)
430 m_controller
->AddLog(CEC_LOG_DEBUG
, "did not receive ack");
439 void CCECProcessor::TransmitAbort(cec_logical_address address
, cec_opcode opcode
, ECecAbortReason reason
/* = CEC_ABORT_REASON_UNRECOGNIZED_OPCODE */)
441 m_controller
->AddLog(CEC_LOG_DEBUG
, "<< transmitting abort message");
445 cec_command::format(command
, m_logicalAddresses
.primary
, address
, CEC_OPCODE_FEATURE_ABORT
);
446 command
.parameters
.push_back((uint8_t)opcode
);
447 command
.parameters
.push_back((uint8_t)reason
);
452 bool CCECProcessor::WaitForTransmitSucceeded(uint8_t iLength
, uint32_t iTimeout
/* = 1000 */)
455 bool bTransmitSucceeded(false);
456 uint8_t iPacketsLeft(iLength
/ 4);
458 int64_t iNow
= GetTimeMs();
459 int64_t iTargetTime
= iNow
+ (uint64_t) iTimeout
;
461 while (!bTransmitSucceeded
&& !bError
&& (iTimeout
== 0 || iNow
< iTargetTime
))
463 CCECAdapterMessage msg
;
465 if (!m_communication
->Read(msg
, iTimeout
> 0 ? (int32_t)(iTargetTime
- iNow
) : 1000))
471 if ((bError
= msg
.is_error()) == false)
473 m_controller
->AddLog(bError
? CEC_LOG_WARNING
: CEC_LOG_DEBUG
, msg
.ToString());
475 switch(msg
.message())
477 case MSGCODE_COMMAND_ACCEPTED
:
478 if (iPacketsLeft
> 0)
481 case MSGCODE_TRANSMIT_SUCCEEDED
:
482 bTransmitSucceeded
= (iPacketsLeft
== 0);
483 bError
= !bTransmitSucceeded
;
486 if (ParseMessage(msg
))
487 m_commandBuffer
.Push(m_currentframe
);
494 return bTransmitSucceeded
&& !bError
;
497 bool CCECProcessor::ParseMessage(const CCECAdapterMessage
&msg
)
504 switch(msg
.message())
506 case MSGCODE_FRAME_START
:
508 m_currentframe
.clear();
511 m_currentframe
.initiator
= msg
.initiator();
512 m_currentframe
.destination
= msg
.destination();
513 m_currentframe
.ack
= msg
.ack();
514 m_currentframe
.eom
= msg
.eom();
518 case MSGCODE_FRAME_DATA
:
522 m_currentframe
.push_back(msg
[1]);
523 m_currentframe
.eom
= msg
.eom();
535 void CCECProcessor::ParseCommand(cec_command
&command
)
538 dataStr
.Format(">> %1x%1x:%02x", command
.initiator
, command
.destination
, command
.opcode
);
539 for (uint8_t iPtr
= 0; iPtr
< command
.parameters
.size
; iPtr
++)
540 dataStr
.AppendFormat(":%02x", (unsigned int)command
.parameters
[iPtr
]);
541 m_controller
->AddLog(CEC_LOG_TRAFFIC
, dataStr
.c_str());
544 m_busDevices
[(uint8_t)command
.initiator
]->HandleCommand(command
);
547 uint16_t CCECProcessor::GetPhysicalAddress(void) const
549 if (!m_logicalAddresses
.empty() && m_busDevices
[m_logicalAddresses
.primary
])
550 return m_busDevices
[m_logicalAddresses
.primary
]->GetPhysicalAddress();
554 void CCECProcessor::SetCurrentButton(cec_user_control_code iButtonCode
)
556 m_controller
->SetCurrentButton(iButtonCode
);
559 void CCECProcessor::AddCommand(const cec_command
&command
)
561 m_controller
->AddCommand(command
);
564 void CCECProcessor::AddKey(cec_keypress
&key
)
566 m_controller
->AddKey(key
);
569 void CCECProcessor::AddKey(void)
571 m_controller
->AddKey();
574 void CCECProcessor::AddLog(cec_log_level level
, const CStdString
&strMessage
)
576 m_controller
->AddLog(level
, strMessage
);
579 bool CCECProcessor::SetAckMask(uint16_t iMask
)
583 strLog
.Format("setting ackmask to %2x", iMask
);
584 m_controller
->AddLog(CEC_LOG_DEBUG
, strLog
.c_str());
586 CCECAdapterMessage
*output
= new CCECAdapterMessage
;
588 output
->push_back(MSGSTART
);
589 output
->push_escaped(MSGCODE_SET_ACK_MASK
);
590 output
->push_escaped(iMask
>> 8);
591 output
->push_escaped((uint8_t)iMask
);
592 output
->push_back(MSGEND
);
594 if ((bReturn
= Transmit(output
)) == false)
595 m_controller
->AddLog(CEC_LOG_ERROR
, "could not set the ackmask");