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