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