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