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