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