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"
38 #include "util/StdString.h"
39 #include "platform/timeutils.h"
44 CCECProcessor::CCECProcessor(CLibCEC
*controller
, CAdapterCommunication
*serComm
, const char *strDeviceName
, cec_logical_address iLogicalAddress
/* = CECDEVICE_PLAYBACKDEVICE1 */, uint16_t iPhysicalAddress
/* = CEC_DEFAULT_PHYSICAL_ADDRESS*/) :
46 m_strDeviceName(strDeviceName
),
47 m_communication(serComm
),
48 m_controller(controller
),
51 m_logicalAddresses
.clear();
52 m_logicalAddresses
.set(iLogicalAddress
);
54 for (int iPtr
= 0; iPtr
< 16; iPtr
++)
55 m_busDevices
[iPtr
] = new CCECBusDevice(this, (cec_logical_address
) iPtr
, iPtr
== iLogicalAddress
? iPhysicalAddress
: 0);
58 CCECProcessor::CCECProcessor(CLibCEC
*controller
, CAdapterCommunication
*serComm
, const char *strDeviceName
, const cec_device_type_list
&types
) :
60 m_strDeviceName(strDeviceName
),
62 m_communication(serComm
),
63 m_controller(controller
),
66 m_logicalAddresses
.clear();
67 for (int iPtr
= 0; iPtr
< 16; iPtr
++)
68 m_busDevices
[iPtr
] = new CCECBusDevice(this, (cec_logical_address
) iPtr
, 0);
71 CCECProcessor::~CCECProcessor(void)
73 m_startCondition
.Broadcast();
75 m_communication
= NULL
;
77 for (unsigned int iPtr
= 0; iPtr
< 16; iPtr
++)
78 delete m_busDevices
[iPtr
];
81 bool CCECProcessor::Start(void)
83 CLockObject
lock(&m_mutex
);
84 if (!m_communication
|| !m_communication
->IsOpen())
86 m_controller
->AddLog(CEC_LOG_ERROR
, "connection is closed");
92 if (!m_startCondition
.Wait(&m_mutex
) || !m_bStarted
)
94 m_controller
->AddLog(CEC_LOG_ERROR
, "could not create a processor thread");
100 m_controller
->AddLog(CEC_LOG_ERROR
, "could not create a processor thread");
105 bool CCECProcessor::TryLogicalAddress(cec_logical_address address
, const char *strLabel
)
108 strLog
.Format("trying logical address '%s'", strLabel
);
109 AddLog(CEC_LOG_DEBUG
, strLog
);
111 SetAckMask(0x1 << address
);
112 if (!m_busDevices
[address
]->PollDevice(address
))
114 strLog
.Format("using logical address '%s'", strLabel
);
115 AddLog(CEC_LOG_NOTICE
, strLog
);
117 /* only set our OSD name for the primary device */
118 if (m_logicalAddresses
.empty())
119 m_busDevices
[address
]->m_strDeviceName
= m_strDeviceName
;
120 m_logicalAddresses
.set(address
);
123 m_busDevices
[address
]->SetPhysicalAddress(CEC_DEFAULT_PHYSICAL_ADDRESS
);
128 strLog
.Format("logical address '%s' already taken", strLabel
);
129 AddLog(CEC_LOG_DEBUG
, strLog
);
133 bool CCECProcessor::FindLogicalAddressRecordingDevice(void)
135 AddLog(CEC_LOG_DEBUG
, "detecting logical address for type 'recording device'");
136 return TryLogicalAddress(CECDEVICE_RECORDINGDEVICE1
, "recording 1") ||
137 TryLogicalAddress(CECDEVICE_RECORDINGDEVICE2
, "recording 2") ||
138 TryLogicalAddress(CECDEVICE_RECORDINGDEVICE3
, "recording 3");
141 bool CCECProcessor::FindLogicalAddressTuner(void)
143 AddLog(CEC_LOG_DEBUG
, "detecting logical address for type 'tuner'");
144 return TryLogicalAddress(CECDEVICE_TUNER1
, "tuner 1") ||
145 TryLogicalAddress(CECDEVICE_TUNER2
, "tuner 2") ||
146 TryLogicalAddress(CECDEVICE_TUNER3
, "tuner 3") ||
147 TryLogicalAddress(CECDEVICE_TUNER4
, "tuner 4");
150 bool CCECProcessor::FindLogicalAddressPlaybackDevice(void)
152 AddLog(CEC_LOG_DEBUG
, "detecting logical address for type 'playback device'");
153 return TryLogicalAddress(CECDEVICE_PLAYBACKDEVICE1
, "playback 1") ||
154 TryLogicalAddress(CECDEVICE_PLAYBACKDEVICE2
, "playback 2") ||
155 TryLogicalAddress(CECDEVICE_PLAYBACKDEVICE3
, "playback 3");
158 bool CCECProcessor::FindLogicalAddressAudioSystem(void)
160 AddLog(CEC_LOG_DEBUG
, "detecting logical address for type 'audio'");
161 return TryLogicalAddress(CECDEVICE_AUDIOSYSTEM
, "audio");
164 bool CCECProcessor::FindLogicalAddresses(void)
167 m_logicalAddresses
.clear();
170 for (unsigned int iPtr
= 0; iPtr
< 5; iPtr
++)
172 if (m_types
.types
[iPtr
] == CEC_DEVICE_TYPE_RESERVED
)
175 strLog
.Format("%s - device %d: type %d", __FUNCTION__
, iPtr
, m_types
.types
[iPtr
]);
176 AddLog(CEC_LOG_DEBUG
, strLog
);
178 if (m_types
.types
[iPtr
] == CEC_DEVICE_TYPE_RECORDING_DEVICE
)
179 bReturn
&= FindLogicalAddressRecordingDevice();
180 if (m_types
.types
[iPtr
] == CEC_DEVICE_TYPE_TUNER
)
181 bReturn
&= FindLogicalAddressTuner();
182 if (m_types
.types
[iPtr
] == CEC_DEVICE_TYPE_PLAYBACK_DEVICE
)
183 bReturn
&= FindLogicalAddressPlaybackDevice();
184 if (m_types
.types
[iPtr
] == CEC_DEVICE_TYPE_AUDIO_SYSTEM
)
185 bReturn
&= FindLogicalAddressAudioSystem();
191 void *CCECProcessor::Process(void)
194 CCECAdapterMessage msg
;
197 if (m_logicalAddresses
.empty() && !FindLogicalAddresses())
199 CLockObject
lock(&m_mutex
);
200 m_controller
->AddLog(CEC_LOG_ERROR
, "could not detect our logical addressed");
201 m_startCondition
.Signal();
205 SetAckMask(m_logicalAddresses
.ackmask());
208 CLockObject
lock(&m_mutex
);
209 m_controller
->AddLog(CEC_LOG_DEBUG
, "processor thread started");
211 m_startCondition
.Signal();
217 bool bParseFrame(false);
222 CLockObject
lock(&m_mutex
);
223 if (m_communication
->IsOpen() && m_communication
->Read(msg
, 50))
225 m_controller
->AddLog(msg
.is_error() ? CEC_LOG_WARNING
: CEC_LOG_DEBUG
, msg
.ToString());
226 bParseFrame
= ParseMessage(msg
) && !IsStopped();
230 command
= m_currentframe
;
234 ParseCommand(command
);
238 m_controller
->CheckKeypressTimeout();
240 for (unsigned int iDevicePtr
= 0; iDevicePtr
< 16; iDevicePtr
++)
241 m_busDevices
[iDevicePtr
]->PollVendorId();
249 bool CCECProcessor::SetActiveView(void)
254 if (!m_logicalAddresses
.empty() && m_busDevices
[m_logicalAddresses
.primary
])
255 return m_busDevices
[m_logicalAddresses
.primary
]->BroadcastActiveView();
259 bool CCECProcessor::SetInactiveView(void)
264 if (!m_logicalAddresses
.empty() && m_busDevices
[m_logicalAddresses
.primary
])
265 return m_busDevices
[m_logicalAddresses
.primary
]->BroadcastInactiveView();
269 void CCECProcessor::LogOutput(const cec_command
&data
)
272 strTx
.Format("<< %02x", ((uint8_t)data
.initiator
<< 4) + (uint8_t)data
.destination
);
274 strTx
.AppendFormat(":%02x", (uint8_t)data
.opcode
);
276 for (uint8_t iPtr
= 0; iPtr
< data
.parameters
.size
; iPtr
++)
277 strTx
.AppendFormat(":%02x", data
.parameters
[iPtr
]);
278 m_controller
->AddLog(CEC_LOG_TRAFFIC
, strTx
.c_str());
281 bool CCECProcessor::SetLogicalAddress(cec_logical_address iLogicalAddress
)
283 if (m_logicalAddresses
.primary
!= iLogicalAddress
)
286 strLog
.Format("<< setting primary logical address to %1x", iLogicalAddress
);
287 m_controller
->AddLog(CEC_LOG_NOTICE
, strLog
.c_str());
288 m_logicalAddresses
.primary
= iLogicalAddress
;
289 m_logicalAddresses
.set(iLogicalAddress
);
290 return SetAckMask(m_logicalAddresses
.ackmask());
296 bool CCECProcessor::SetPhysicalAddress(uint16_t iPhysicalAddress
)
298 if (!m_logicalAddresses
.empty() && m_busDevices
[m_logicalAddresses
.primary
])
300 m_busDevices
[m_logicalAddresses
.primary
]->SetPhysicalAddress(iPhysicalAddress
);
301 return m_busDevices
[m_logicalAddresses
.primary
]->BroadcastActiveView();
306 bool CCECProcessor::SwitchMonitoring(bool bEnable
)
309 strLog
.Format("== %s monitoring mode ==", bEnable
? "enabling" : "disabling");
310 m_controller
->AddLog(CEC_LOG_NOTICE
, strLog
.c_str());
312 m_bMonitor
= bEnable
;
314 return SetAckMask(0);
316 return SetAckMask(m_logicalAddresses
.ackmask());
319 bool CCECProcessor::PollDevice(cec_logical_address iAddress
)
321 if (iAddress
!= CECDEVICE_UNKNOWN
&& m_busDevices
[iAddress
])
322 return m_busDevices
[iAddress
]->PollDevice();
326 cec_version
CCECProcessor::GetDeviceCecVersion(cec_logical_address iAddress
)
328 return m_busDevices
[iAddress
]->GetCecVersion();
331 bool CCECProcessor::GetDeviceMenuLanguage(cec_logical_address iAddress
, cec_menu_language
*language
)
333 if (m_busDevices
[iAddress
])
335 *language
= m_busDevices
[iAddress
]->GetMenuLanguage();
336 return (strcmp(language
->language
, "???") != 0);
341 uint64_t CCECProcessor::GetDeviceVendorId(cec_logical_address iAddress
)
343 if (m_busDevices
[iAddress
])
344 return m_busDevices
[iAddress
]->GetVendorId();
348 cec_power_status
CCECProcessor::GetDevicePowerStatus(cec_logical_address iAddress
)
350 if (m_busDevices
[iAddress
])
351 return m_busDevices
[iAddress
]->GetPowerStatus();
352 return CEC_POWER_STATUS_UNKNOWN
;
355 bool CCECProcessor::Transmit(const cec_command
&data
)
360 CCECAdapterMessage
*output
= new CCECAdapterMessage(data
);
361 bReturn
= Transmit(output
);
367 bool CCECProcessor::Transmit(CCECAdapterMessage
*output
)
370 CLockObject
lock(&m_mutex
);
372 CLockObject
msgLock(&output
->mutex
);
373 if (!m_communication
|| !m_communication
->Write(output
))
377 output
->condition
.Wait(&output
->mutex
);
378 if (output
->state
!= ADAPTER_MESSAGE_STATE_SENT
)
380 m_controller
->AddLog(CEC_LOG_ERROR
, "command was not sent");
385 if (output
->transmit_timeout
> 0)
387 if ((bReturn
= WaitForTransmitSucceeded(output
->size(), output
->transmit_timeout
)) == false)
388 m_controller
->AddLog(CEC_LOG_ERROR
, "did not receive ack");
397 void CCECProcessor::TransmitAbort(cec_logical_address address
, cec_opcode opcode
, ECecAbortReason reason
/* = CEC_ABORT_REASON_UNRECOGNIZED_OPCODE */)
399 m_controller
->AddLog(CEC_LOG_DEBUG
, "<< transmitting abort message");
403 cec_command::format(command
, m_logicalAddresses
.primary
, address
, CEC_OPCODE_FEATURE_ABORT
);
404 command
.parameters
.push_back((uint8_t)opcode
);
405 command
.parameters
.push_back((uint8_t)reason
);
410 bool CCECProcessor::WaitForTransmitSucceeded(uint8_t iLength
, uint32_t iTimeout
/* = 1000 */)
413 bool bTransmitSucceeded(false);
414 uint8_t iPacketsLeft(iLength
/ 4);
416 int64_t iNow
= GetTimeMs();
417 int64_t iTargetTime
= iNow
+ (uint64_t) iTimeout
;
419 while (!bTransmitSucceeded
&& !bError
&& (iTimeout
== 0 || iNow
< iTargetTime
))
421 CCECAdapterMessage msg
;
423 if (!m_communication
->Read(msg
, iTimeout
> 0 ? (int32_t)(iTargetTime
- iNow
) : 1000))
429 if ((bError
= msg
.is_error()) == false)
431 m_controller
->AddLog(bError
? CEC_LOG_WARNING
: CEC_LOG_DEBUG
, msg
.ToString());
433 switch(msg
.message())
435 case MSGCODE_COMMAND_ACCEPTED
:
436 if (iPacketsLeft
> 0)
439 case MSGCODE_TRANSMIT_SUCCEEDED
:
440 bTransmitSucceeded
= (iPacketsLeft
== 0);
441 bError
= !bTransmitSucceeded
;
451 return bTransmitSucceeded
&& !bError
;
454 bool CCECProcessor::ParseMessage(const CCECAdapterMessage
&msg
)
461 switch(msg
.message())
463 case MSGCODE_FRAME_START
:
465 m_currentframe
.clear();
468 m_currentframe
.initiator
= msg
.initiator();
469 m_currentframe
.destination
= msg
.destination();
470 m_currentframe
.ack
= msg
.ack();
471 m_currentframe
.eom
= msg
.eom();
475 case MSGCODE_FRAME_DATA
:
479 m_currentframe
.push_back(msg
[1]);
480 m_currentframe
.eom
= msg
.eom();
492 void CCECProcessor::ParseCommand(cec_command
&command
)
495 dataStr
.Format(">> %1x%1x:%02x", command
.initiator
, command
.destination
, command
.opcode
);
496 for (uint8_t iPtr
= 0; iPtr
< command
.parameters
.size
; iPtr
++)
497 dataStr
.AppendFormat(":%02x", (unsigned int)command
.parameters
[iPtr
]);
498 m_controller
->AddLog(CEC_LOG_TRAFFIC
, dataStr
.c_str());
501 m_busDevices
[(uint8_t)command
.initiator
]->HandleCommand(command
);
504 uint16_t CCECProcessor::GetPhysicalAddress(void) const
506 if (!m_logicalAddresses
.empty() && m_busDevices
[m_logicalAddresses
.primary
])
507 return m_busDevices
[m_logicalAddresses
.primary
]->GetPhysicalAddress();
511 void CCECProcessor::SetCurrentButton(cec_user_control_code iButtonCode
)
513 m_controller
->SetCurrentButton(iButtonCode
);
516 void CCECProcessor::AddCommand(const cec_command
&command
)
518 m_controller
->AddCommand(command
);
521 void CCECProcessor::AddKey(cec_keypress
&key
)
523 m_controller
->AddKey(key
);
526 void CCECProcessor::AddKey(void)
528 m_controller
->AddKey();
531 void CCECProcessor::AddLog(cec_log_level level
, const CStdString
&strMessage
)
533 m_controller
->AddLog(level
, strMessage
);
536 bool CCECProcessor::SetAckMask(uint16_t iMask
)
540 strLog
.Format("setting ackmask to %2x", iMask
);
541 m_controller
->AddLog(CEC_LOG_DEBUG
, strLog
.c_str());
543 CCECAdapterMessage
*output
= new CCECAdapterMessage
;
545 output
->push_back(MSGSTART
);
546 output
->push_escaped(MSGCODE_SET_ACK_MASK
);
547 output
->push_escaped(iMask
>> 8);
548 output
->push_escaped((uint8_t)iMask
);
549 output
->push_back(MSGEND
);
551 if ((bReturn
= Transmit(output
)) == false)
552 m_controller
->AddLog(CEC_LOG_ERROR
, "could not set the ackmask");