e9caaf0835ba0cec3a4e83e72fb410541f825f35
[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 "CECBusDevice.h"
37 #include "LibCEC.h"
38 #include "util/StdString.h"
39 #include "platform/timeutils.h"
40
41 using namespace CEC;
42 using namespace std;
43
44 CCECProcessor::CCECProcessor(CLibCEC *controller, CAdapterCommunication *serComm, const char *strDeviceName, cec_logical_address iLogicalAddress /* = CECDEVICE_PLAYBACKDEVICE1 */, uint16_t iPhysicalAddress /* = CEC_DEFAULT_PHYSICAL_ADDRESS*/) :
45 m_iPhysicalAddress(iPhysicalAddress),
46 m_iLogicalAddress(iLogicalAddress),
47 m_strDeviceName(strDeviceName),
48 m_communication(serComm),
49 m_controller(controller),
50 m_bMonitor(false)
51 {
52 for (unsigned int iPtr = 0; iPtr < 16; iPtr++)
53 m_busDevices[iPtr] = new CCECBusDevice(this, (cec_logical_address) iPtr, 0);
54 }
55
56 CCECProcessor::~CCECProcessor(void)
57 {
58 StopThread();
59 m_communication = NULL;
60 m_controller = NULL;
61 for (unsigned int iPtr = 0; iPtr < 16; iPtr++)
62 delete m_busDevices[iPtr];
63 }
64
65 bool CCECProcessor::Start(void)
66 {
67 if (!m_communication || !m_communication->IsOpen())
68 {
69 m_controller->AddLog(CEC_LOG_ERROR, "connection is closed");
70 return false;
71 }
72
73 if (!SetLogicalAddress(m_iLogicalAddress))
74 {
75 m_controller->AddLog(CEC_LOG_ERROR, "could not set the logical address");
76 return false;
77 }
78
79 if (CreateThread())
80 return true;
81 else
82 m_controller->AddLog(CEC_LOG_ERROR, "could not create a processor thread");
83
84 return false;
85 }
86
87 void *CCECProcessor::Process(void)
88 {
89 m_controller->AddLog(CEC_LOG_DEBUG, "processor thread started");
90
91 cec_command command;
92 cec_adapter_message msg;
93
94 while (!IsStopped())
95 {
96 bool bParseFrame(false);
97 bool bError(false);
98 bool bTransmitSucceeded(false);
99 command.clear();
100 msg.clear();
101
102 {
103 CLockObject lock(&m_mutex);
104 if (m_communication->IsOpen() && m_communication->Read(msg, 50))
105 ParseMessage(msg, &bError, &bTransmitSucceeded, &bParseFrame);
106
107 bParseFrame &= !IsStopped();
108 if (bParseFrame)
109 command = m_currentframe;
110 }
111
112 if (bParseFrame)
113 ParseCommand(command);
114
115 m_controller->CheckKeypressTimeout();
116
117 if (!IsStopped())
118 Sleep(5);
119 }
120
121 return NULL;
122 }
123
124 bool CCECProcessor::PowerOnDevices(cec_logical_address address /* = CECDEVICE_TV */)
125 {
126 if (!IsRunning())
127 return false;
128
129 CStdString strLog;
130 strLog.Format("<< powering on device with logical address %d", (int8_t)address);
131 m_controller->AddLog(CEC_LOG_DEBUG, strLog.c_str());
132
133 cec_command command;
134 cec_command::format(command, m_iLogicalAddress, address, CEC_OPCODE_IMAGE_VIEW_ON);
135
136 return Transmit(command);
137 }
138
139 bool CCECProcessor::StandbyDevices(cec_logical_address address /* = CECDEVICE_BROADCAST */)
140 {
141 if (!IsRunning())
142 return false;
143
144 CStdString strLog;
145 strLog.Format("<< putting device with logical address %d in standby mode", (int8_t)address);
146 m_controller->AddLog(CEC_LOG_DEBUG, strLog.c_str());
147
148 cec_command command;
149 cec_command::format(command, m_iLogicalAddress, address, CEC_OPCODE_STANDBY);
150
151 return Transmit(command);
152 }
153
154 bool CCECProcessor::SetActiveView(void)
155 {
156 if (!IsRunning())
157 return false;
158
159 m_controller->AddLog(CEC_LOG_DEBUG, "<< setting active view");
160
161 cec_command command;
162 cec_command::format(command, m_iLogicalAddress, CECDEVICE_BROADCAST, CEC_OPCODE_ACTIVE_SOURCE);
163 command.parameters.push_back((m_iPhysicalAddress >> 8) & 0xFF);
164 command.parameters.push_back(m_iPhysicalAddress & 0xFF);
165
166 return Transmit(command);
167 }
168
169 bool CCECProcessor::SetInactiveView(void)
170 {
171 if (!IsRunning())
172 return false;
173
174 m_controller->AddLog(CEC_LOG_DEBUG, "<< setting inactive view");
175
176 cec_command command;
177 cec_command::format(command, m_iLogicalAddress, CECDEVICE_BROADCAST, CEC_OPCODE_INACTIVE_SOURCE);
178 command.parameters.push_back((m_iPhysicalAddress >> 8) & 0xFF);
179 command.parameters.push_back(m_iPhysicalAddress & 0xFF);
180
181 return Transmit(command);
182 }
183
184 void CCECProcessor::LogOutput(const cec_command &data)
185 {
186 CStdString txStr = "transmit ";
187 txStr.AppendFormat(" %02x", ((uint8_t)data.initiator << 4) + (uint8_t)data.destination);
188 txStr.AppendFormat(":%02x", (uint8_t)data.opcode);
189
190 for (uint8_t iPtr = 0; iPtr < data.parameters.size; iPtr++)
191 txStr.AppendFormat(":%02x", data.parameters[iPtr]);
192 m_controller->AddLog(CEC_LOG_DEBUG, txStr.c_str());
193 }
194
195 bool CCECProcessor::Transmit(const cec_command &data, bool bWaitForAck /* = true */)
196 {
197 LogOutput(data);
198
199 cec_adapter_message output;
200 output.clear();
201 CAdapterCommunication::FormatAdapterMessage(data, output);
202
203 return TransmitFormatted(output, bWaitForAck);
204 }
205
206 bool CCECProcessor::SetLogicalAddress(cec_logical_address iLogicalAddress)
207 {
208 CStdString strLog;
209 strLog.Format("<< setting logical address to %1x", iLogicalAddress);
210 m_controller->AddLog(CEC_LOG_NOTICE, strLog.c_str());
211
212 m_iLogicalAddress = iLogicalAddress;
213 return m_communication && m_communication->SetAckMask(0x1 << (uint8_t)m_iLogicalAddress);
214 }
215
216 bool CCECProcessor::SetPhysicalAddress(uint16_t iPhysicalAddress)
217 {
218 CStdString strLog;
219 strLog.Format("<< setting physical address to %2x", iPhysicalAddress);
220 m_controller->AddLog(CEC_LOG_NOTICE, strLog.c_str());
221
222 m_iPhysicalAddress = iPhysicalAddress;
223 return SetActiveView();
224 }
225
226 bool CCECProcessor::SetOSDString(cec_logical_address iLogicalAddress, cec_display_control duration, const char *strMessage)
227 {
228 CStdString strLog;
229 strLog.Format("<< display message '%s'", strMessage);
230 m_controller->AddLog(CEC_LOG_NOTICE, strLog.c_str());
231
232 cec_command command;
233 cec_command::format(command, m_iLogicalAddress, iLogicalAddress, CEC_OPCODE_SET_OSD_STRING);
234 command.parameters.push_back((uint8_t)duration);
235
236 for (unsigned int iPtr = 0; iPtr < strlen(strMessage); iPtr++)
237 command.parameters.push_back(strMessage[iPtr]);
238
239 return Transmit(command);
240 }
241
242 bool CCECProcessor::SwitchMonitoring(bool bEnable)
243 {
244 CStdString strLog;
245 strLog.Format("== %s monitoring mode ==", bEnable ? "enabling" : "disabling");
246 m_controller->AddLog(CEC_LOG_NOTICE, strLog.c_str());
247
248 m_bMonitor = bEnable;
249 if (bEnable)
250 return m_communication && m_communication->SetAckMask(0);
251 else
252 return m_communication && m_communication->SetAckMask(0x1 << (uint8_t)m_iLogicalAddress);
253 }
254
255 bool CCECProcessor::TransmitFormatted(const cec_adapter_message &data, bool bWaitForAck /* = true */)
256 {
257 CLockObject lock(&m_mutex);
258 if (!m_communication || !m_communication->Write(data))
259 return false;
260
261 if (bWaitForAck)
262 {
263 uint64_t now = GetTimeMs();
264 uint64_t target = now + 1000;
265 bool bError(false);
266 bool bGotAck(false);
267
268 while (!bGotAck && now < target)
269 {
270 bGotAck = WaitForAck(&bError, (uint32_t) (target - now));
271 now = GetTimeMs();
272
273 if (bError && now < target)
274 {
275 m_controller->AddLog(CEC_LOG_ERROR, "retransmitting previous frame");
276 if (!m_communication->Write(data))
277 return false;
278 }
279 }
280 }
281
282 return true;
283 }
284
285 void CCECProcessor::TransmitAbort(cec_logical_address address, cec_opcode opcode, ECecAbortReason reason /* = CEC_ABORT_REASON_UNRECOGNIZED_OPCODE */)
286 {
287 m_controller->AddLog(CEC_LOG_DEBUG, "<< transmitting abort message");
288
289 cec_command command;
290 cec_command::format(command, m_iLogicalAddress, address, CEC_OPCODE_FEATURE_ABORT);
291 command.parameters.push_back((uint8_t)opcode);
292 command.parameters.push_back((uint8_t)reason);
293
294 Transmit(command);
295 }
296
297 void CCECProcessor::ReportCECVersion(cec_logical_address address /* = CECDEVICE_TV */)
298 {
299 m_controller->AddLog(CEC_LOG_NOTICE, "<< reporting CEC version as 1.3a");
300
301 cec_command command;
302 cec_command::format(command, m_iLogicalAddress, address, CEC_OPCODE_CEC_VERSION);
303 command.parameters.push_back(CEC_VERSION_1_3A);
304
305 Transmit(command);
306 }
307
308 void CCECProcessor::ReportPowerState(cec_logical_address address /*= CECDEVICE_TV */, bool bOn /* = true */)
309 {
310 if (bOn)
311 m_controller->AddLog(CEC_LOG_NOTICE, "<< reporting \"On\" power status");
312 else
313 m_controller->AddLog(CEC_LOG_NOTICE, "<< reporting \"Off\" power status");
314
315 cec_command command;
316 cec_command::format(command, m_iLogicalAddress, address, CEC_OPCODE_REPORT_POWER_STATUS);
317 command.parameters.push_back(bOn ? (uint8_t) CEC_POWER_STATUS_ON : (uint8_t) CEC_POWER_STATUS_STANDBY);
318
319 Transmit(command);
320 }
321
322 void CCECProcessor::ReportMenuState(cec_logical_address address /* = CECDEVICE_TV */, bool bActive /* = true */)
323 {
324 if (bActive)
325 m_controller->AddLog(CEC_LOG_NOTICE, "<< reporting menu state as active");
326 else
327 m_controller->AddLog(CEC_LOG_NOTICE, "<< reporting menu state as inactive");
328
329 cec_command command;
330 cec_command::format(command, m_iLogicalAddress, address, CEC_OPCODE_MENU_STATUS);
331 command.parameters.push_back(bActive ? (uint8_t) CEC_MENU_STATE_ACTIVATED : (uint8_t) CEC_MENU_STATE_DEACTIVATED);
332
333 Transmit(command);
334 }
335
336 void CCECProcessor::ReportVendorID(cec_logical_address address /* = CECDEVICE_TV */)
337 {
338 m_controller->AddLog(CEC_LOG_NOTICE, "<< vendor ID requested, feature abort");
339 TransmitAbort(address, CEC_OPCODE_GIVE_DEVICE_VENDOR_ID);
340 }
341
342 void CCECProcessor::ReportOSDName(cec_logical_address address /* = CECDEVICE_TV */)
343 {
344 const char *osdname = m_strDeviceName.c_str();
345 CStdString strLog;
346 strLog.Format("<< reporting OSD name as %s", osdname);
347 m_controller->AddLog(CEC_LOG_NOTICE, strLog.c_str());
348
349 cec_command command;
350 cec_command::format(command, m_iLogicalAddress, address, CEC_OPCODE_SET_OSD_NAME);
351 for (unsigned int iPtr = 0; iPtr < strlen(osdname); iPtr++)
352 command.parameters.push_back(osdname[iPtr]);
353
354 Transmit(command);
355 }
356
357 void CCECProcessor::ReportPhysicalAddress(void)
358 {
359 CStdString strLog;
360 strLog.Format("<< reporting physical address as %04x", m_iPhysicalAddress);
361 m_controller->AddLog(CEC_LOG_NOTICE, strLog.c_str());
362
363 cec_command command;
364 cec_command::format(command, m_iLogicalAddress, CECDEVICE_BROADCAST, CEC_OPCODE_REPORT_PHYSICAL_ADDRESS);
365 command.parameters.push_back((uint8_t) ((m_iPhysicalAddress >> 8) & 0xFF));
366 command.parameters.push_back((uint8_t) (m_iPhysicalAddress & 0xFF));
367 command.parameters.push_back((uint8_t) (CEC_DEVICE_TYPE_PLAYBACK_DEVICE));
368
369 Transmit(command);
370 }
371
372 void CCECProcessor::BroadcastActiveSource(void)
373 {
374 m_controller->AddLog(CEC_LOG_NOTICE, "<< broadcasting active source");
375
376 cec_command command;
377 cec_command::format(command, m_iLogicalAddress, CECDEVICE_BROADCAST, CEC_OPCODE_ACTIVE_SOURCE);
378 command.parameters.push_back((uint8_t) ((m_iPhysicalAddress >> 8) & 0xFF));
379 command.parameters.push_back((uint8_t) (m_iPhysicalAddress & 0xFF));
380
381 Transmit(command);
382 }
383
384 bool CCECProcessor::WaitForAck(bool *bError, uint32_t iTimeout /* = 1000 */)
385 {
386 bool bTransmitSucceeded = false, bEom = false;
387 *bError = false;
388
389 int64_t iNow = GetTimeMs();
390 int64_t iTargetTime = iNow + (uint64_t) iTimeout;
391
392 while (!bTransmitSucceeded && !*bError && (iTimeout == 0 || iNow < iTargetTime))
393 {
394 cec_adapter_message msg;
395 msg.clear();
396
397 if (!m_communication->Read(msg, iTimeout > 0 ? (int32_t)(iTargetTime - iNow) : 1000))
398 {
399 iNow = GetTimeMs();
400 continue;
401 }
402
403 ParseMessage(msg, bError, &bTransmitSucceeded, &bEom, false);
404 iNow = GetTimeMs();
405 }
406
407 return bTransmitSucceeded && !*bError;
408 }
409
410 void CCECProcessor::ParseMessage(cec_adapter_message &msg, bool *bError, bool *bTransmitSucceeded, bool *bEom, bool bProcessMessages /* = true */)
411 {
412 *bError = false;
413 *bTransmitSucceeded = false;
414 *bEom = false;
415
416 if (msg.empty())
417 return;
418
419 CStdString logStr;
420
421 switch(msg.message())
422 {
423 case MSGCODE_NOTHING:
424 m_controller->AddLog(CEC_LOG_DEBUG, "MSGCODE_NOTHING");
425 break;
426 case MSGCODE_TIMEOUT_ERROR:
427 case MSGCODE_HIGH_ERROR:
428 case MSGCODE_LOW_ERROR:
429 {
430 if (msg.message() == MSGCODE_TIMEOUT_ERROR)
431 logStr = "MSGCODE_TIMEOUT";
432 else if (msg.message() == MSGCODE_HIGH_ERROR)
433 logStr = "MSGCODE_HIGH_ERROR";
434 else
435 logStr = "MSGCODE_LOW_ERROR";
436
437 int iLine = (msg.size() >= 3) ? (msg[1] << 8) | (msg[2]) : 0;
438 uint32_t iTime = (msg.size() >= 7) ? (msg[3] << 24) | (msg[4] << 16) | (msg[5] << 8) | (msg[6]) : 0;
439 logStr.AppendFormat(" line:%i", iLine);
440 logStr.AppendFormat(" time:%u", iTime);
441 m_controller->AddLog(CEC_LOG_WARNING, logStr.c_str());
442 *bError = true;
443 }
444 break;
445 case MSGCODE_FRAME_START:
446 {
447 if (bProcessMessages)
448 {
449 logStr = "MSGCODE_FRAME_START";
450 m_currentframe.clear();
451 if (msg.size() >= 2)
452 {
453 logStr.AppendFormat(" initiator:%u destination:%u ack:%s %s", msg.initiator(), msg.destination(), msg.ack() ? "high" : "low", msg.eom() ? "eom" : "");
454 m_currentframe.initiator = msg.initiator();
455 m_currentframe.destination = msg.destination();
456 m_currentframe.ack = msg.ack();
457 m_currentframe.eom = msg.eom();
458 }
459 m_controller->AddLog(CEC_LOG_DEBUG, logStr.c_str());
460 }
461 else
462 {
463 m_frameBuffer.Push(msg);
464 }
465 }
466 break;
467 case MSGCODE_FRAME_DATA:
468 {
469 if (bProcessMessages)
470 {
471 logStr = "MSGCODE_FRAME_DATA";
472 if (msg.size() >= 2)
473 {
474 uint8_t iData = msg[1];
475 logStr.AppendFormat(" %02x", iData);
476 m_currentframe.push_back(iData);
477 m_currentframe.eom = msg.eom();
478 }
479 m_controller->AddLog(CEC_LOG_DEBUG, logStr.c_str());
480 }
481 else
482 {
483 m_frameBuffer.Push(msg);
484 }
485
486 *bEom = msg.eom();
487 }
488 break;
489 case MSGCODE_COMMAND_ACCEPTED:
490 m_controller->AddLog(CEC_LOG_DEBUG, "MSGCODE_COMMAND_ACCEPTED");
491 break;
492 case MSGCODE_TRANSMIT_SUCCEEDED:
493 m_controller->AddLog(CEC_LOG_DEBUG, "MSGCODE_TRANSMIT_SUCCEEDED");
494 *bTransmitSucceeded = true;
495 break;
496 case MSGCODE_RECEIVE_FAILED:
497 m_controller->AddLog(CEC_LOG_WARNING, "MSGCODE_RECEIVE_FAILED");
498 *bError = true;
499 break;
500 case MSGCODE_COMMAND_REJECTED:
501 m_controller->AddLog(CEC_LOG_WARNING, "MSGCODE_COMMAND_REJECTED");
502 *bError = true;
503 break;
504 case MSGCODE_TRANSMIT_FAILED_LINE:
505 m_controller->AddLog(CEC_LOG_WARNING, "MSGCODE_TRANSMIT_FAILED_LINE");
506 *bError = true;
507 break;
508 case MSGCODE_TRANSMIT_FAILED_ACK:
509 m_controller->AddLog(CEC_LOG_WARNING, "MSGCODE_TRANSMIT_FAILED_ACK");
510 *bError = true;
511 break;
512 case MSGCODE_TRANSMIT_FAILED_TIMEOUT_DATA:
513 m_controller->AddLog(CEC_LOG_WARNING, "MSGCODE_TRANSMIT_FAILED_TIMEOUT_DATA");
514 *bError = true;
515 break;
516 case MSGCODE_TRANSMIT_FAILED_TIMEOUT_LINE:
517 m_controller->AddLog(CEC_LOG_WARNING, "MSGCODE_TRANSMIT_FAILED_TIMEOUT_LINE");
518 *bError = true;
519 break;
520 default:
521 break;
522 }
523 }
524
525 void CCECProcessor::ParseVendorId(cec_logical_address device, const cec_datapacket &data)
526 {
527 if (data.size < 3)
528 {
529 m_controller->AddLog(CEC_LOG_WARNING, "invalid vendor ID received");
530 return;
531 }
532
533 uint64_t iVendorId = ((uint64_t)data[0] << 3) +
534 ((uint64_t)data[1] << 2) +
535 (uint64_t)data[2];
536
537 m_busDevices[(uint8_t)device]->SetVendorId(iVendorId, data.size >= 4 ? data[3] : 0);
538 }
539
540 void CCECProcessor::ParseCommand(cec_command &command)
541 {
542 CStdString dataStr;
543 dataStr.Format(">> received frame: %1x%1x:%02x", command.initiator, command.destination, command.opcode);
544 for (uint8_t iPtr = 0; iPtr < command.parameters.size; iPtr++)
545 dataStr.AppendFormat(":%02x", (unsigned int)command.parameters[iPtr]);
546 m_controller->AddLog(CEC_LOG_DEBUG, dataStr.c_str());
547
548 if (!m_bMonitor)
549 m_busDevices[(uint8_t)command.initiator]->HandleCommand(command);
550 }
551
552 void CCECProcessor::SetCurrentButton(cec_user_control_code iButtonCode)
553 {
554 m_controller->SetCurrentButton(iButtonCode);
555 }
556
557 void CCECProcessor::AddCommand(const cec_command &command)
558 {
559 m_controller->AddCommand(command);
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 }