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