break break break
[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 "LibCEC.h"
38#include "util/StdString.h"
39#include "platform/timeutils.h"
40
41using namespace CEC;
42using namespace std;
43
44CCECProcessor::CCECProcessor(CLibCEC *controller, CAdapterCommunication *serComm, const char *strDeviceName, cec_logical_address iLogicalAddress /* = CECDEVICE_PLAYBACKDEVICE1 */, uint16_t iPhysicalAddress /* = CEC_DEFAULT_PHYSICAL_ADDRESS*/) :
45 m_bStarted(false),
46 m_strDeviceName(strDeviceName),
47 m_communication(serComm),
48 m_controller(controller),
49 m_bMonitor(false)
50{
51 m_logicalAddresses.clear();
52 m_logicalAddresses.set(iLogicalAddress);
53 m_types.clear();
54 for (int iPtr = 0; iPtr < 16; iPtr++)
55 m_busDevices[iPtr] = new CCECBusDevice(this, (cec_logical_address) iPtr, iPtr == iLogicalAddress ? iPhysicalAddress : 0);
56}
57
58CCECProcessor::CCECProcessor(CLibCEC *controller, CAdapterCommunication *serComm, const char *strDeviceName, const cec_device_type_list &types) :
59 m_bStarted(false),
60 m_strDeviceName(strDeviceName),
61 m_types(types),
62 m_communication(serComm),
63 m_controller(controller),
64 m_bMonitor(false)
65{
66 m_logicalAddresses.clear();
67 for (int iPtr = 0; iPtr < 16; iPtr++)
68 m_busDevices[iPtr] = new CCECBusDevice(this, (cec_logical_address) iPtr, 0);
69}
70
71CCECProcessor::~CCECProcessor(void)
72{
73 m_startCondition.Broadcast();
74 StopThread();
75 m_communication = NULL;
76 m_controller = NULL;
77 for (unsigned int iPtr = 0; iPtr < 16; iPtr++)
78 delete m_busDevices[iPtr];
79}
80
81bool CCECProcessor::Start(void)
82{
83 CLockObject lock(&m_mutex);
84 if (!m_communication || !m_communication->IsOpen())
85 {
86 m_controller->AddLog(CEC_LOG_ERROR, "connection is closed");
87 return false;
88 }
89
90 if (CreateThread())
91 {
92 if (!m_startCondition.Wait(&m_mutex) || !m_bStarted)
93 {
94 m_controller->AddLog(CEC_LOG_ERROR, "could not create a processor thread");
95 return false;
96 }
97 return true;
98 }
99 else
100 m_controller->AddLog(CEC_LOG_ERROR, "could not create a processor thread");
101
102 return false;
103}
104
105bool CCECProcessor::TryLogicalAddress(cec_logical_address address, const char *strLabel)
106{
107 CStdString strLog;
108 strLog.Format("trying logical address '%s'", strLabel);
109 AddLog(CEC_LOG_DEBUG, strLog);
110
111 SetAckMask(0x1 << address);
112 if (!m_busDevices[address]->PollDevice(address))
113 {
114 strLog.Format("using logical address '%s'", strLabel);
115 AddLog(CEC_LOG_NOTICE, strLog);
116
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);
121
122 // TODO
123 m_busDevices[address]->SetPhysicalAddress(CEC_DEFAULT_PHYSICAL_ADDRESS);
124
125 return true;
126 }
127
128 strLog.Format("logical address '%s' already taken", strLabel);
129 AddLog(CEC_LOG_DEBUG, strLog);
130 return false;
131}
132
133bool CCECProcessor::FindLogicalAddressRecordingDevice(void)
134{
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");
139}
140
141bool CCECProcessor::FindLogicalAddressTuner(void)
142{
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");
148}
149
150bool CCECProcessor::FindLogicalAddressPlaybackDevice(void)
151{
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");
156}
157
158bool CCECProcessor::FindLogicalAddressAudioSystem(void)
159{
160 AddLog(CEC_LOG_DEBUG, "detecting logical address for type 'audio'");
161 return TryLogicalAddress(CECDEVICE_AUDIOSYSTEM, "audio");
162}
163
164bool CCECProcessor::FindLogicalAddresses(void)
165{
166 bool bReturn(true);
167 m_logicalAddresses.clear();
168 CStdString strLog;
169
170 for (unsigned int iPtr = 0; iPtr < 5; iPtr++)
171 {
172 if (m_types.types[iPtr] == CEC_DEVICE_TYPE_RESERVED)
173 continue;
174
175 strLog.Format("%s - device %d: type %d", __FUNCTION__, iPtr, m_types.types[iPtr]);
176 AddLog(CEC_LOG_DEBUG, strLog);
177
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();
186 }
187
188 return bReturn;
189}
190
191void *CCECProcessor::Process(void)
192{
193 cec_command command;
194 CCECAdapterMessage msg;
195
196 {
197 if (m_logicalAddresses.empty() && !FindLogicalAddresses())
198 {
199 CLockObject lock(&m_mutex);
200 m_controller->AddLog(CEC_LOG_ERROR, "could not detect our logical addressed");
201 m_startCondition.Signal();
202 return NULL;
203 }
204
205 SetAckMask(m_logicalAddresses.ackmask());
206
207 {
208 CLockObject lock(&m_mutex);
209 m_controller->AddLog(CEC_LOG_DEBUG, "processor thread started");
210 m_bStarted = true;
211 m_startCondition.Signal();
212 }
213 }
214
215 while (!IsStopped())
216 {
217 bool bParseFrame(false);
218 command.clear();
219 msg.clear();
220
221 {
222 CLockObject lock(&m_mutex);
223 if (m_communication->IsOpen() && m_communication->Read(msg, 50))
224 {
225 m_controller->AddLog(msg.is_error() ? CEC_LOG_WARNING : CEC_LOG_DEBUG, msg.ToString());
226 bParseFrame = ParseMessage(msg) && !IsStopped();
227 }
228
229 if (bParseFrame)
230 command = m_currentframe;
231 }
232
233 if (bParseFrame)
234 ParseCommand(command);
235
236 Sleep(5);
237
238 m_controller->CheckKeypressTimeout();
239
240 for (unsigned int iDevicePtr = 0; iDevicePtr < 16; iDevicePtr++)
241 m_busDevices[iDevicePtr]->PollVendorId();
242
243 Sleep(5);
244 }
245
246 return NULL;
247}
248
249bool CCECProcessor::SetActiveView(void)
250{
251 if (!IsRunning())
252 return false;
253
254 if (!m_logicalAddresses.empty() && m_busDevices[m_logicalAddresses.primary])
255 return m_busDevices[m_logicalAddresses.primary]->BroadcastActiveView();
256 return false;
257}
258
259bool CCECProcessor::SetInactiveView(void)
260{
261 if (!IsRunning())
262 return false;
263
264 if (!m_logicalAddresses.empty() && m_busDevices[m_logicalAddresses.primary])
265 return m_busDevices[m_logicalAddresses.primary]->BroadcastInactiveView();
266 return false;
267}
268
269void CCECProcessor::LogOutput(const cec_command &data)
270{
271 CStdString strTx;
272 strTx.Format("<< %02x", ((uint8_t)data.initiator << 4) + (uint8_t)data.destination);
273 if (data.opcode_set)
274 strTx.AppendFormat(":%02x", (uint8_t)data.opcode);
275
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());
279}
280
281bool CCECProcessor::SetLogicalAddress(cec_logical_address iLogicalAddress)
282{
283 if (m_logicalAddresses.primary != iLogicalAddress)
284 {
285 CStdString strLog;
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());
291 }
292
293 return true;
294}
295
296bool CCECProcessor::SetPhysicalAddress(uint16_t iPhysicalAddress)
297{
298 if (!m_logicalAddresses.empty() && m_busDevices[m_logicalAddresses.primary])
299 {
300 m_busDevices[m_logicalAddresses.primary]->SetPhysicalAddress(iPhysicalAddress);
301 return m_busDevices[m_logicalAddresses.primary]->BroadcastActiveView();
302 }
303 return false;
304}
305
306bool CCECProcessor::SwitchMonitoring(bool bEnable)
307{
308 CStdString strLog;
309 strLog.Format("== %s monitoring mode ==", bEnable ? "enabling" : "disabling");
310 m_controller->AddLog(CEC_LOG_NOTICE, strLog.c_str());
311
312 m_bMonitor = bEnable;
313 if (bEnable)
314 return SetAckMask(0);
315 else
316 return SetAckMask(m_logicalAddresses.ackmask());
317}
318
319bool CCECProcessor::PollDevice(cec_logical_address iAddress)
320{
321 if (iAddress != CECDEVICE_UNKNOWN && m_busDevices[iAddress])
322 return m_busDevices[iAddress]->PollDevice();
323 return false;
324}
325
326cec_version CCECProcessor::GetDeviceCecVersion(cec_logical_address iAddress)
327{
328 return m_busDevices[iAddress]->GetCecVersion();
329}
330
331bool CCECProcessor::GetDeviceMenuLanguage(cec_logical_address iAddress, cec_menu_language *language)
332{
333 if (m_busDevices[iAddress])
334 {
335 *language = m_busDevices[iAddress]->GetMenuLanguage();
336 return (strcmp(language->language, "???") != 0);
337 }
338 return false;
339}
340
341uint64_t CCECProcessor::GetDeviceVendorId(cec_logical_address iAddress)
342{
343 if (m_busDevices[iAddress])
344 return m_busDevices[iAddress]->GetVendorId();
345 return false;
346}
347
348cec_power_status CCECProcessor::GetDevicePowerStatus(cec_logical_address iAddress)
349{
350 if (m_busDevices[iAddress])
351 return m_busDevices[iAddress]->GetPowerStatus();
352 return CEC_POWER_STATUS_UNKNOWN;
353}
354
355bool CCECProcessor::Transmit(const cec_command &data)
356{
357 bool bReturn(false);
358 LogOutput(data);
359
360 CCECAdapterMessage *output = new CCECAdapterMessage(data);
361 bReturn = Transmit(output);
362 delete output;
363
364 return bReturn;
365}
366
367bool CCECProcessor::Transmit(CCECAdapterMessage *output)
368{
369 bool bReturn(false);
370 CLockObject lock(&m_mutex);
371 {
372 CLockObject msgLock(&output->mutex);
373 if (!m_communication || !m_communication->Write(output))
374 return bReturn;
375 else
376 {
377 output->condition.Wait(&output->mutex);
378 if (output->state != ADAPTER_MESSAGE_STATE_SENT)
379 {
380 m_controller->AddLog(CEC_LOG_ERROR, "command was not sent");
381 return bReturn;
382 }
383 }
384
385 if (output->transmit_timeout > 0)
386 {
387 if ((bReturn = WaitForTransmitSucceeded(output->size(), output->transmit_timeout)) == false)
388 m_controller->AddLog(CEC_LOG_ERROR, "did not receive ack");
389 }
390 else
391 bReturn = true;
392 }
393
394 return bReturn;
395}
396
397void CCECProcessor::TransmitAbort(cec_logical_address address, cec_opcode opcode, ECecAbortReason reason /* = CEC_ABORT_REASON_UNRECOGNIZED_OPCODE */)
398{
399 m_controller->AddLog(CEC_LOG_DEBUG, "<< transmitting abort message");
400
401 cec_command command;
402 // TODO
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);
406
407 Transmit(command);
408}
409
410bool CCECProcessor::WaitForTransmitSucceeded(uint8_t iLength, uint32_t iTimeout /* = 1000 */)
411{
412 bool bError(false);
413 bool bTransmitSucceeded(false);
414 uint8_t iPacketsLeft(iLength / 4);
415
416 int64_t iNow = GetTimeMs();
417 int64_t iTargetTime = iNow + (uint64_t) iTimeout;
418
419 while (!bTransmitSucceeded && !bError && (iTimeout == 0 || iNow < iTargetTime))
420 {
421 CCECAdapterMessage msg;
422
423 if (!m_communication->Read(msg, iTimeout > 0 ? (int32_t)(iTargetTime - iNow) : 1000))
424 {
425 iNow = GetTimeMs();
426 continue;
427 }
428
429 if ((bError = msg.is_error()) == false)
430 {
431 m_controller->AddLog(bError ? CEC_LOG_WARNING : CEC_LOG_DEBUG, msg.ToString());
432
433 switch(msg.message())
434 {
435 case MSGCODE_COMMAND_ACCEPTED:
436 if (iPacketsLeft > 0)
437 iPacketsLeft--;
438 break;
439 case MSGCODE_TRANSMIT_SUCCEEDED:
440 bTransmitSucceeded = (iPacketsLeft == 0);
441 bError = !bTransmitSucceeded;
442 break;
443 default:
444 ParseMessage(msg);
445 }
446
447 iNow = GetTimeMs();
448 }
449 }
450
451 return bTransmitSucceeded && !bError;
452}
453
454bool CCECProcessor::ParseMessage(const CCECAdapterMessage &msg)
455{
456 bool bEom = false;
457
458 if (msg.empty())
459 return bEom;
460
461 switch(msg.message())
462 {
463 case MSGCODE_FRAME_START:
464 {
465 m_currentframe.clear();
466 if (msg.size() >= 2)
467 {
468 m_currentframe.initiator = msg.initiator();
469 m_currentframe.destination = msg.destination();
470 m_currentframe.ack = msg.ack();
471 m_currentframe.eom = msg.eom();
472 }
473 }
474 break;
475 case MSGCODE_FRAME_DATA:
476 {
477 if (msg.size() >= 2)
478 {
479 m_currentframe.push_back(msg[1]);
480 m_currentframe.eom = msg.eom();
481 }
482 bEom = msg.eom();
483 }
484 break;
485 default:
486 break;
487 }
488
489 return bEom;
490}
491
492void CCECProcessor::ParseCommand(cec_command &command)
493{
494 CStdString dataStr;
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());
499
500 if (!m_bMonitor)
501 m_busDevices[(uint8_t)command.initiator]->HandleCommand(command);
502}
503
504uint16_t CCECProcessor::GetPhysicalAddress(void) const
505{
506 if (!m_logicalAddresses.empty() && m_busDevices[m_logicalAddresses.primary])
507 return m_busDevices[m_logicalAddresses.primary]->GetPhysicalAddress();
508 return false;
509}
510
511void CCECProcessor::SetCurrentButton(cec_user_control_code iButtonCode)
512{
513 m_controller->SetCurrentButton(iButtonCode);
514}
515
516void CCECProcessor::AddCommand(const cec_command &command)
517{
518 m_controller->AddCommand(command);
519}
520
521void CCECProcessor::AddKey(cec_keypress &key)
522{
523 m_controller->AddKey(key);
524}
525
526void CCECProcessor::AddKey(void)
527{
528 m_controller->AddKey();
529}
530
531void CCECProcessor::AddLog(cec_log_level level, const CStdString &strMessage)
532{
533 m_controller->AddLog(level, strMessage);
534}
535
536bool CCECProcessor::SetAckMask(uint16_t iMask)
537{
538 bool bReturn(false);
539 CStdString strLog;
540 strLog.Format("setting ackmask to %2x", iMask);
541 m_controller->AddLog(CEC_LOG_DEBUG, strLog.c_str());
542
543 CCECAdapterMessage *output = new CCECAdapterMessage;
544
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);
550
551 if ((bReturn = Transmit(output)) == false)
552 m_controller->AddLog(CEC_LOG_ERROR, "could not set the ackmask");
553
554 delete output;
555
556 return bReturn;
557}