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