Commit | Line | Data |
---|---|---|
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" |
2abe74eb | 37 | #include "LibCEC.h" |
abbca718 | 38 | #include "util/StdString.h" |
b9187cc6 | 39 | #include "platform/timeutils.h" |
abbca718 LOK |
40 | |
41 | using namespace CEC; | |
42 | using namespace std; | |
43 | ||
2b4d8297 | 44 | CCECProcessor::CCECProcessor(CLibCEC *controller, CAdapterCommunication *serComm, const char *strDeviceName, cec_logical_address iLogicalAddress /* = CECDEVICE_PLAYBACKDEVICE1 */, uint16_t iPhysicalAddress /* = CEC_DEFAULT_PHYSICAL_ADDRESS*/) : |
179efe86 | 45 | m_iLogicalAddress(iLogicalAddress), |
2abe74eb LOK |
46 | m_strDeviceName(strDeviceName), |
47 | m_communication(serComm), | |
8b7e5ff6 | 48 | m_controller(controller), |
06bfd4d7 | 49 | m_bMonitor(false) |
abbca718 | 50 | { |
7ea0d558 | 51 | for (int iPtr = 0; iPtr < 16; iPtr++) |
09c10b66 | 52 | m_busDevices[iPtr] = new CCECBusDevice(this, (cec_logical_address) iPtr, iPtr == iLogicalAddress ? iPhysicalAddress : 0); |
abbca718 LOK |
53 | } |
54 | ||
2abe74eb | 55 | CCECProcessor::~CCECProcessor(void) |
abbca718 | 56 | { |
8b0ee2cc | 57 | m_startCondition.Broadcast(); |
2abe74eb LOK |
58 | StopThread(); |
59 | m_communication = NULL; | |
60 | m_controller = NULL; | |
d1998c7b LOK |
61 | for (unsigned int iPtr = 0; iPtr < 16; iPtr++) |
62 | delete m_busDevices[iPtr]; | |
abbca718 LOK |
63 | } |
64 | ||
2abe74eb | 65 | bool CCECProcessor::Start(void) |
abbca718 | 66 | { |
8b0ee2cc | 67 | CLockObject lock(&m_mutex); |
2abe74eb | 68 | if (!m_communication || !m_communication->IsOpen()) |
13fd6a66 LOK |
69 | { |
70 | m_controller->AddLog(CEC_LOG_ERROR, "connection is closed"); | |
a8f0bd18 | 71 | return false; |
13fd6a66 | 72 | } |
abbca718 | 73 | |
60fa4578 | 74 | if (CreateThread()) |
8b0ee2cc LOK |
75 | { |
76 | if (!m_startCondition.Wait(&m_mutex)) | |
77 | { | |
78 | m_controller->AddLog(CEC_LOG_ERROR, "could not create a processor thread"); | |
79 | return false; | |
80 | } | |
a8f0bd18 | 81 | return true; |
8b0ee2cc | 82 | } |
a8f0bd18 | 83 | else |
2abe74eb | 84 | m_controller->AddLog(CEC_LOG_ERROR, "could not create a processor thread"); |
abbca718 | 85 | |
a8f0bd18 | 86 | return false; |
abbca718 LOK |
87 | } |
88 | ||
2abe74eb | 89 | void *CCECProcessor::Process(void) |
f99bc831 | 90 | { |
e0407d3d LOK |
91 | cec_command command; |
92 | CCECAdapterMessage msg; | |
93 | ||
94 | SetAckMask(0x1 << (uint8_t)m_iLogicalAddress); | |
95 | ||
8b0ee2cc LOK |
96 | { |
97 | CLockObject lock(&m_mutex); | |
98 | m_controller->AddLog(CEC_LOG_DEBUG, "processor thread started"); | |
99 | m_startCondition.Signal(); | |
100 | } | |
abbca718 | 101 | |
13fd6a66 | 102 | while (!IsStopped()) |
abbca718 | 103 | { |
60fa4578 | 104 | bool bParseFrame(false); |
9dee1670 | 105 | command.clear(); |
76321de4 LOK |
106 | msg.clear(); |
107 | ||
60fa4578 LOK |
108 | { |
109 | CLockObject lock(&m_mutex); | |
88c43521 | 110 | if (m_communication->IsOpen() && m_communication->Read(msg, 50)) |
b5f34cf9 LOK |
111 | { |
112 | m_controller->AddLog(msg.is_error() ? CEC_LOG_WARNING : CEC_LOG_DEBUG, msg.ToString()); | |
88c43521 | 113 | bParseFrame = ParseMessage(msg) && !IsStopped(); |
b5f34cf9 | 114 | } |
76321de4 LOK |
115 | |
116 | if (bParseFrame) | |
9dee1670 | 117 | command = m_currentframe; |
60fa4578 LOK |
118 | } |
119 | ||
13fd6a66 | 120 | if (bParseFrame) |
9dee1670 | 121 | ParseCommand(command); |
abbca718 | 122 | |
b5f34cf9 LOK |
123 | Sleep(5); |
124 | ||
13fd6a66 LOK |
125 | m_controller->CheckKeypressTimeout(); |
126 | ||
0ab58650 LOK |
127 | for (unsigned int iDevicePtr = 0; iDevicePtr < 16; iDevicePtr++) |
128 | m_busDevices[iDevicePtr]->PollVendorId(); | |
129 | ||
b5f34cf9 | 130 | Sleep(5); |
abbca718 LOK |
131 | } |
132 | ||
60fa4578 | 133 | return NULL; |
abbca718 LOK |
134 | } |
135 | ||
2abe74eb | 136 | bool CCECProcessor::SetActiveView(void) |
abbca718 | 137 | { |
60fa4578 | 138 | if (!IsRunning()) |
f99bc831 LOK |
139 | return false; |
140 | ||
cc60ab1c LOK |
141 | if (m_iLogicalAddress != CECDEVICE_UNKNOWN && m_busDevices[m_iLogicalAddress]) |
142 | return m_busDevices[m_iLogicalAddress]->BroadcastActiveView(); | |
143 | return false; | |
abbca718 LOK |
144 | } |
145 | ||
2abe74eb | 146 | bool CCECProcessor::SetInactiveView(void) |
abbca718 | 147 | { |
60fa4578 | 148 | if (!IsRunning()) |
f99bc831 LOK |
149 | return false; |
150 | ||
cc60ab1c LOK |
151 | if (m_iLogicalAddress != CECDEVICE_UNKNOWN && m_busDevices[m_iLogicalAddress]) |
152 | return m_busDevices[m_iLogicalAddress]->BroadcastInactiveView(); | |
153 | return false; | |
abbca718 LOK |
154 | } |
155 | ||
9dee1670 | 156 | void CCECProcessor::LogOutput(const cec_command &data) |
abbca718 | 157 | { |
1d3ca3de LOK |
158 | CStdString strTx; |
159 | strTx.Format("<< %02x:%02x", ((uint8_t)data.initiator << 4) + (uint8_t)data.destination, (uint8_t)data.opcode); | |
9dee1670 | 160 | |
06a1f7ce | 161 | for (uint8_t iPtr = 0; iPtr < data.parameters.size; iPtr++) |
1d3ca3de LOK |
162 | strTx.AppendFormat(":%02x", data.parameters[iPtr]); |
163 | m_controller->AddLog(CEC_LOG_TRAFFIC, strTx.c_str()); | |
9dee1670 | 164 | } |
2abe74eb | 165 | |
06bfd4d7 | 166 | bool CCECProcessor::SetLogicalAddress(cec_logical_address iLogicalAddress) |
abbca718 | 167 | { |
06bfd4d7 | 168 | if (m_iLogicalAddress != iLogicalAddress) |
06775107 LOK |
169 | { |
170 | CStdString strLog; | |
171 | strLog.Format("<< setting logical address to %1x", iLogicalAddress); | |
172 | m_controller->AddLog(CEC_LOG_NOTICE, strLog.c_str()); | |
06775107 | 173 | m_iLogicalAddress = iLogicalAddress; |
06bfd4d7 | 174 | return SetAckMask(0x1 << (uint8_t)m_iLogicalAddress); |
06775107 | 175 | } |
2abe74eb | 176 | |
06bfd4d7 | 177 | return true; |
abbca718 | 178 | } |
825ddb96 | 179 | |
2492216a LOK |
180 | bool CCECProcessor::SetPhysicalAddress(uint16_t iPhysicalAddress) |
181 | { | |
cc60ab1c LOK |
182 | if (m_iLogicalAddress != CECDEVICE_UNKNOWN && m_busDevices[m_iLogicalAddress]) |
183 | { | |
184 | m_busDevices[m_iLogicalAddress]->SetPhysicalAddress(iPhysicalAddress); | |
185 | return m_busDevices[m_iLogicalAddress]->BroadcastActiveView(); | |
186 | } | |
187 | return false; | |
1969b140 LOK |
188 | } |
189 | ||
8b7e5ff6 LOK |
190 | bool CCECProcessor::SwitchMonitoring(bool bEnable) |
191 | { | |
192 | CStdString strLog; | |
193 | strLog.Format("== %s monitoring mode ==", bEnable ? "enabling" : "disabling"); | |
194 | m_controller->AddLog(CEC_LOG_NOTICE, strLog.c_str()); | |
195 | ||
196 | m_bMonitor = bEnable; | |
197 | if (bEnable) | |
06bfd4d7 | 198 | return SetAckMask(0); |
8b7e5ff6 | 199 | else |
06bfd4d7 | 200 | return SetAckMask(0x1 << (uint8_t)m_iLogicalAddress); |
8b7e5ff6 LOK |
201 | } |
202 | ||
6a1c0009 LOK |
203 | cec_version CCECProcessor::GetDeviceCecVersion(cec_logical_address iAddress) |
204 | { | |
205 | return m_busDevices[iAddress]->GetCecVersion(); | |
206 | } | |
207 | ||
a3269a0a LOK |
208 | bool CCECProcessor::GetDeviceMenuLanguage(cec_logical_address iAddress, cec_menu_language *language) |
209 | { | |
cc60ab1c LOK |
210 | if (m_busDevices[iAddress]) |
211 | { | |
212 | *language = m_busDevices[iAddress]->GetMenuLanguage(); | |
5744fbba | 213 | return (strcmp(language->language, "???") != 0); |
cc60ab1c LOK |
214 | } |
215 | return false; | |
a3269a0a LOK |
216 | } |
217 | ||
44c74256 LOK |
218 | uint64_t CCECProcessor::GetDeviceVendorId(cec_logical_address iAddress) |
219 | { | |
cc60ab1c LOK |
220 | if (m_busDevices[iAddress]) |
221 | return m_busDevices[iAddress]->GetVendorId(); | |
222 | return false; | |
44c74256 LOK |
223 | } |
224 | ||
e55f3f70 LOK |
225 | cec_power_status CCECProcessor::GetDevicePowerStatus(cec_logical_address iAddress) |
226 | { | |
cc60ab1c LOK |
227 | if (m_busDevices[iAddress]) |
228 | return m_busDevices[iAddress]->GetPowerStatus(); | |
229 | return CEC_POWER_STATUS_UNKNOWN; | |
e55f3f70 LOK |
230 | } |
231 | ||
8d84e2c0 | 232 | bool CCECProcessor::Transmit(const cec_command &data) |
825ddb96 | 233 | { |
28352a04 | 234 | bool bReturn(false); |
13929cff LOK |
235 | LogOutput(data); |
236 | ||
28352a04 LOK |
237 | CCECAdapterMessage *output = new CCECAdapterMessage(data); |
238 | bReturn = Transmit(output); | |
239 | delete output; | |
240 | ||
241 | return bReturn; | |
06bfd4d7 | 242 | } |
13929cff | 243 | |
28352a04 | 244 | bool CCECProcessor::Transmit(CCECAdapterMessage *output) |
06bfd4d7 LOK |
245 | { |
246 | bool bReturn(false); | |
2abe74eb | 247 | CLockObject lock(&m_mutex); |
eb35e4ca LOK |
248 | { |
249 | CLockObject msgLock(&output->mutex); | |
250 | if (!m_communication || !m_communication->Write(output)) | |
251 | return bReturn; | |
252 | else | |
0e31a62c | 253 | { |
3a0ca6e1 | 254 | output->condition.Wait(&output->mutex); |
0e31a62c LOK |
255 | if (output->state != ADAPTER_MESSAGE_STATE_SENT) |
256 | { | |
257 | m_controller->AddLog(CEC_LOG_ERROR, "command was not sent"); | |
258 | return bReturn; | |
259 | } | |
260 | } | |
2abe74eb | 261 | |
06bfd4d7 | 262 | if (output->transmit_timeout > 0) |
cd505625 | 263 | { |
06bfd4d7 | 264 | if ((bReturn = WaitForTransmitSucceeded(output->size(), output->transmit_timeout)) == false) |
cd505625 LOK |
265 | m_controller->AddLog(CEC_LOG_ERROR, "did not receive ack"); |
266 | } | |
267 | else | |
cd505625 | 268 | bReturn = true; |
2abe74eb LOK |
269 | } |
270 | ||
7ea0d558 | 271 | return bReturn; |
825ddb96 | 272 | } |
abbca718 | 273 | |
2abe74eb | 274 | void CCECProcessor::TransmitAbort(cec_logical_address address, cec_opcode opcode, ECecAbortReason reason /* = CEC_ABORT_REASON_UNRECOGNIZED_OPCODE */) |
abbca718 | 275 | { |
9dee1670 LOK |
276 | m_controller->AddLog(CEC_LOG_DEBUG, "<< transmitting abort message"); |
277 | ||
06a1f7ce LOK |
278 | cec_command command; |
279 | cec_command::format(command, m_iLogicalAddress, address, CEC_OPCODE_FEATURE_ABORT); | |
9dee1670 LOK |
280 | command.parameters.push_back((uint8_t)opcode); |
281 | command.parameters.push_back((uint8_t)reason); | |
282 | ||
283 | Transmit(command); | |
abbca718 LOK |
284 | } |
285 | ||
1f0e9e0f | 286 | bool CCECProcessor::WaitForTransmitSucceeded(uint8_t iLength, uint32_t iTimeout /* = 1000 */) |
abbca718 | 287 | { |
1f0e9e0f LOK |
288 | bool bError(false); |
289 | bool bTransmitSucceeded(false); | |
7ea0d558 | 290 | uint8_t iPacketsLeft(iLength / 4); |
abbca718 LOK |
291 | |
292 | int64_t iNow = GetTimeMs(); | |
25701fa6 | 293 | int64_t iTargetTime = iNow + (uint64_t) iTimeout; |
abbca718 | 294 | |
1f0e9e0f | 295 | while (!bTransmitSucceeded && !bError && (iTimeout == 0 || iNow < iTargetTime)) |
abbca718 | 296 | { |
220537f2 | 297 | CCECAdapterMessage msg; |
25701fa6 | 298 | |
06a1f7ce | 299 | if (!m_communication->Read(msg, iTimeout > 0 ? (int32_t)(iTargetTime - iNow) : 1000)) |
abbca718 | 300 | { |
abbca718 | 301 | iNow = GetTimeMs(); |
5d38b0b6 LOK |
302 | continue; |
303 | } | |
304 | ||
edab6137 | 305 | if ((bError = msg.is_error()) == false) |
7ea0d558 | 306 | { |
0544ff56 LOK |
307 | m_controller->AddLog(bError ? CEC_LOG_WARNING : CEC_LOG_DEBUG, msg.ToString()); |
308 | ||
edab6137 LOK |
309 | switch(msg.message()) |
310 | { | |
311 | case MSGCODE_COMMAND_ACCEPTED: | |
312 | if (iPacketsLeft > 0) | |
313 | iPacketsLeft--; | |
314 | break; | |
315 | case MSGCODE_TRANSMIT_SUCCEEDED: | |
316 | bTransmitSucceeded = (iPacketsLeft == 0); | |
317 | bError = !bTransmitSucceeded; | |
318 | break; | |
319 | default: | |
320 | ParseMessage(msg); | |
321 | } | |
7ea0d558 | 322 | |
edab6137 LOK |
323 | iNow = GetTimeMs(); |
324 | } | |
abbca718 LOK |
325 | } |
326 | ||
1f0e9e0f | 327 | return bTransmitSucceeded && !bError; |
abbca718 LOK |
328 | } |
329 | ||
0e7b6c54 | 330 | bool CCECProcessor::ParseMessage(const CCECAdapterMessage &msg) |
abbca718 | 331 | { |
7ea0d558 | 332 | bool bEom = false; |
60fa4578 | 333 | |
0e7b6c54 | 334 | if (msg.empty()) |
7ea0d558 | 335 | return bEom; |
abbca718 | 336 | |
0e7b6c54 | 337 | switch(msg.message()) |
abbca718 | 338 | { |
abbca718 LOK |
339 | case MSGCODE_FRAME_START: |
340 | { | |
7ea0d558 | 341 | m_currentframe.clear(); |
0e7b6c54 | 342 | if (msg.size() >= 2) |
abbca718 | 343 | { |
0e7b6c54 LOK |
344 | m_currentframe.initiator = msg.initiator(); |
345 | m_currentframe.destination = msg.destination(); | |
346 | m_currentframe.ack = msg.ack(); | |
347 | m_currentframe.eom = msg.eom(); | |
abbca718 | 348 | } |
abbca718 LOK |
349 | } |
350 | break; | |
351 | case MSGCODE_FRAME_DATA: | |
352 | { | |
0e7b6c54 | 353 | if (msg.size() >= 2) |
78e8010a | 354 | { |
e41b06f9 | 355 | m_currentframe.push_back(msg[1]); |
0e7b6c54 | 356 | m_currentframe.eom = msg.eom(); |
abbca718 | 357 | } |
0e7b6c54 | 358 | bEom = msg.eom(); |
abbca718 | 359 | } |
78e8010a | 360 | break; |
abbca718 LOK |
361 | default: |
362 | break; | |
363 | } | |
7ea0d558 LOK |
364 | |
365 | return bEom; | |
abbca718 LOK |
366 | } |
367 | ||
e9de9629 | 368 | void CCECProcessor::ParseCommand(cec_command &command) |
abbca718 | 369 | { |
e9de9629 | 370 | CStdString dataStr; |
1d3ca3de | 371 | dataStr.Format(">> %1x%1x:%02x", command.initiator, command.destination, command.opcode); |
e9de9629 LOK |
372 | for (uint8_t iPtr = 0; iPtr < command.parameters.size; iPtr++) |
373 | dataStr.AppendFormat(":%02x", (unsigned int)command.parameters[iPtr]); | |
1d3ca3de | 374 | m_controller->AddLog(CEC_LOG_TRAFFIC, dataStr.c_str()); |
dc113aca | 375 | |
e9de9629 LOK |
376 | if (!m_bMonitor) |
377 | m_busDevices[(uint8_t)command.initiator]->HandleCommand(command); | |
dc113aca LOK |
378 | } |
379 | ||
0f23c85c LOK |
380 | uint16_t CCECProcessor::GetPhysicalAddress(void) const |
381 | { | |
cc60ab1c LOK |
382 | if (m_iLogicalAddress != CECDEVICE_UNKNOWN && m_busDevices[m_iLogicalAddress]) |
383 | return m_busDevices[m_iLogicalAddress]->GetPhysicalAddress(); | |
384 | return false; | |
0f23c85c LOK |
385 | } |
386 | ||
e9de9629 | 387 | void CCECProcessor::SetCurrentButton(cec_user_control_code iButtonCode) |
dc113aca | 388 | { |
e9de9629 | 389 | m_controller->SetCurrentButton(iButtonCode); |
dc113aca LOK |
390 | } |
391 | ||
e9de9629 | 392 | void CCECProcessor::AddCommand(const cec_command &command) |
dc113aca | 393 | { |
0544ff56 | 394 | m_controller->AddCommand(command); |
dc113aca LOK |
395 | } |
396 | ||
95ba7a09 LOK |
397 | void CCECProcessor::AddKey(cec_keypress &key) |
398 | { | |
399 | m_controller->AddKey(key); | |
400 | } | |
401 | ||
e9de9629 | 402 | void CCECProcessor::AddKey(void) |
dc113aca | 403 | { |
e9de9629 | 404 | m_controller->AddKey(); |
abbca718 | 405 | } |
acec5f48 | 406 | |
e9de9629 | 407 | void CCECProcessor::AddLog(cec_log_level level, const CStdString &strMessage) |
acec5f48 | 408 | { |
e9de9629 | 409 | m_controller->AddLog(level, strMessage); |
acec5f48 | 410 | } |
06bfd4d7 LOK |
411 | |
412 | bool CCECProcessor::SetAckMask(uint16_t iMask) | |
413 | { | |
28352a04 | 414 | bool bReturn(false); |
06bfd4d7 LOK |
415 | CStdString strLog; |
416 | strLog.Format("setting ackmask to %2x", iMask); | |
417 | m_controller->AddLog(CEC_LOG_DEBUG, strLog.c_str()); | |
418 | ||
28352a04 | 419 | CCECAdapterMessage *output = new CCECAdapterMessage; |
06bfd4d7 LOK |
420 | |
421 | output->push_back(MSGSTART); | |
422 | output->push_escaped(MSGCODE_SET_ACK_MASK); | |
423 | output->push_escaped(iMask >> 8); | |
424 | output->push_escaped((uint8_t)iMask); | |
425 | output->push_back(MSGEND); | |
426 | ||
28352a04 | 427 | if ((bReturn = Transmit(output)) == false) |
06bfd4d7 | 428 | m_controller->AddLog(CEC_LOG_ERROR, "could not set the ackmask"); |
06bfd4d7 | 429 | |
28352a04 LOK |
430 | delete output; |
431 | ||
432 | return bReturn; | |
06bfd4d7 | 433 | } |