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