cec: fixed - log the parameter too when there's only 1
[deb_libcec.git] / src / lib / CECProcessor.cpp
CommitLineData
abbca718
LOK
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
2abe74eb 33#include "CECProcessor.h"
abbca718 34
2abe74eb
LOK
35#include "AdapterCommunication.h"
36#include "LibCEC.h"
abbca718 37#include "util/StdString.h"
b9187cc6 38#include "platform/timeutils.h"
abbca718
LOK
39
40using namespace CEC;
41using namespace std;
42
2b4d8297 43CCECProcessor::CCECProcessor(CLibCEC *controller, CAdapterCommunication *serComm, const char *strDeviceName, cec_logical_address iLogicalAddress /* = CECDEVICE_PLAYBACKDEVICE1 */, uint16_t iPhysicalAddress /* = CEC_DEFAULT_PHYSICAL_ADDRESS*/) :
2492216a 44 m_iPhysicalAddress(iPhysicalAddress),
df7339c6 45 m_iLogicalAddress(iLogicalAddress),
2abe74eb
LOK
46 m_strDeviceName(strDeviceName),
47 m_communication(serComm),
8b7e5ff6
LOK
48 m_controller(controller),
49 m_bMonitor(false)
abbca718 50{
5f150ee2
LOK
51 for (uint8_t iPtr = 0; iPtr < 16; iPtr++)
52 m_vendorIds[iPtr] = CEC_VENDOR_UNKNOWN;
53 for (uint8_t iPtr = 0; iPtr < 16; iPtr++)
54 m_vendorClasses[iPtr] = (uint8_t) 0;
abbca718
LOK
55}
56
2abe74eb 57CCECProcessor::~CCECProcessor(void)
abbca718 58{
2abe74eb
LOK
59 StopThread();
60 m_communication = NULL;
61 m_controller = NULL;
abbca718
LOK
62}
63
2abe74eb 64bool CCECProcessor::Start(void)
abbca718 65{
2abe74eb 66 if (!m_communication || !m_communication->IsOpen())
13fd6a66
LOK
67 {
68 m_controller->AddLog(CEC_LOG_ERROR, "connection is closed");
a8f0bd18 69 return false;
13fd6a66 70 }
abbca718 71
a8f0bd18 72 if (!SetLogicalAddress(m_iLogicalAddress))
abbca718 73 {
2abe74eb 74 m_controller->AddLog(CEC_LOG_ERROR, "could not set the logical address");
a8f0bd18 75 return false;
abbca718
LOK
76 }
77
60fa4578 78 if (CreateThread())
a8f0bd18 79 return true;
a8f0bd18 80 else
2abe74eb 81 m_controller->AddLog(CEC_LOG_ERROR, "could not create a processor thread");
abbca718 82
a8f0bd18 83 return false;
abbca718
LOK
84}
85
2abe74eb 86void *CCECProcessor::Process(void)
f99bc831 87{
2abe74eb 88 m_controller->AddLog(CEC_LOG_DEBUG, "processor thread started");
abbca718 89
9dee1670
LOK
90 cec_command command;
91 cec_adapter_message msg;
92
13fd6a66 93 while (!IsStopped())
abbca718 94 {
60fa4578 95 bool bParseFrame(false);
78e8010a
LOK
96 bool bError(false);
97 bool bTransmitSucceeded(false);
9dee1670 98 command.clear();
76321de4
LOK
99 msg.clear();
100
60fa4578
LOK
101 {
102 CLockObject lock(&m_mutex);
76321de4 103 if (m_communication->IsOpen() && m_communication->Read(msg, 50))
78e8010a 104 ParseMessage(msg, &bError, &bTransmitSucceeded, &bParseFrame);
76321de4 105
78e8010a 106 bParseFrame &= !IsStopped();
76321de4 107 if (bParseFrame)
9dee1670 108 command = m_currentframe;
60fa4578
LOK
109 }
110
13fd6a66 111 if (bParseFrame)
9dee1670 112 ParseCommand(command);
abbca718 113
13fd6a66
LOK
114 m_controller->CheckKeypressTimeout();
115
116 if (!IsStopped())
d5bffd3c 117 Sleep(5);
abbca718
LOK
118 }
119
60fa4578 120 return NULL;
abbca718
LOK
121}
122
2abe74eb 123bool CCECProcessor::PowerOnDevices(cec_logical_address address /* = CECDEVICE_TV */)
abbca718 124{
60fa4578 125 if (!IsRunning())
f99bc831
LOK
126 return false;
127
abbca718 128 CStdString strLog;
88f45c9b 129 strLog.Format("<< powering on device with logical address %d", (int8_t)address);
2abe74eb 130 m_controller->AddLog(CEC_LOG_DEBUG, strLog.c_str());
25701fa6 131
06a1f7ce
LOK
132 cec_command command;
133 cec_command::format(command, m_iLogicalAddress, address, CEC_OPCODE_IMAGE_VIEW_ON);
134
135 return Transmit(command);
abbca718
LOK
136}
137
2abe74eb 138bool CCECProcessor::StandbyDevices(cec_logical_address address /* = CECDEVICE_BROADCAST */)
abbca718 139{
60fa4578 140 if (!IsRunning())
f99bc831
LOK
141 return false;
142
abbca718 143 CStdString strLog;
88f45c9b 144 strLog.Format("<< putting device with logical address %d in standby mode", (int8_t)address);
2abe74eb 145 m_controller->AddLog(CEC_LOG_DEBUG, strLog.c_str());
25701fa6 146
06a1f7ce
LOK
147 cec_command command;
148 cec_command::format(command, m_iLogicalAddress, address, CEC_OPCODE_STANDBY);
149
150 return Transmit(command);
abbca718
LOK
151}
152
2abe74eb 153bool CCECProcessor::SetActiveView(void)
abbca718 154{
60fa4578 155 if (!IsRunning())
f99bc831
LOK
156 return false;
157
88f45c9b 158 m_controller->AddLog(CEC_LOG_DEBUG, "<< setting active view");
9dee1670 159
06a1f7ce
LOK
160 cec_command command;
161 cec_command::format(command, m_iLogicalAddress, CECDEVICE_BROADCAST, CEC_OPCODE_ACTIVE_SOURCE);
2492216a
LOK
162 command.parameters.push_back((m_iPhysicalAddress >> 8) & 0xFF);
163 command.parameters.push_back(m_iPhysicalAddress & 0xFF);
9dee1670
LOK
164
165 return Transmit(command);
abbca718
LOK
166}
167
2abe74eb 168bool CCECProcessor::SetInactiveView(void)
abbca718 169{
60fa4578 170 if (!IsRunning())
f99bc831
LOK
171 return false;
172
88f45c9b 173 m_controller->AddLog(CEC_LOG_DEBUG, "<< setting inactive view");
9dee1670 174
06a1f7ce
LOK
175 cec_command command;
176 cec_command::format(command, m_iLogicalAddress, CECDEVICE_BROADCAST, CEC_OPCODE_INACTIVE_SOURCE);
2492216a
LOK
177 command.parameters.push_back((m_iPhysicalAddress >> 8) & 0xFF);
178 command.parameters.push_back(m_iPhysicalAddress & 0xFF);
9dee1670
LOK
179
180 return Transmit(command);
abbca718
LOK
181}
182
9dee1670 183void CCECProcessor::LogOutput(const cec_command &data)
abbca718 184{
2abe74eb 185 CStdString txStr = "transmit ";
9dee1670 186 txStr.AppendFormat(" %02x", ((uint8_t)data.initiator << 4) + (uint8_t)data.destination);
2b885768 187 txStr.AppendFormat(":%02x", (uint8_t)data.opcode);
9dee1670 188
06a1f7ce 189 for (uint8_t iPtr = 0; iPtr < data.parameters.size; iPtr++)
2b885768 190 txStr.AppendFormat(":%02x", data.parameters[iPtr]);
2abe74eb 191 m_controller->AddLog(CEC_LOG_DEBUG, txStr.c_str());
9dee1670 192}
2abe74eb 193
9dee1670
LOK
194bool CCECProcessor::Transmit(const cec_command &data, bool bWaitForAck /* = true */)
195{
196 LogOutput(data);
2abe74eb 197
9dee1670 198 cec_adapter_message output;
25701fa6 199 output.clear();
9dee1670 200 CAdapterCommunication::FormatAdapterMessage(data, output);
2abe74eb
LOK
201
202 return TransmitFormatted(output, bWaitForAck);
abbca718
LOK
203}
204
2abe74eb 205bool CCECProcessor::SetLogicalAddress(cec_logical_address iLogicalAddress)
abbca718 206{
2abe74eb 207 CStdString strLog;
2492216a 208 strLog.Format("<< setting logical address to %1x", iLogicalAddress);
2abe74eb
LOK
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);
abbca718 213}
825ddb96 214
2492216a
LOK
215bool CCECProcessor::SetPhysicalAddress(uint16_t iPhysicalAddress)
216{
217 CStdString strLog;
218 strLog.Format("<< setting physical address to %2x", iPhysicalAddress);
219 m_controller->AddLog(CEC_LOG_NOTICE, strLog.c_str());
220
221 m_iPhysicalAddress = iPhysicalAddress;
222 return SetActiveView();
223}
224
1969b140
LOK
225bool CCECProcessor::SetOSDString(cec_logical_address iLogicalAddress, cec_display_control duration, const char *strMessage)
226{
227 CStdString strLog;
228 strLog.Format("<< display message '%s'", strMessage);
229 m_controller->AddLog(CEC_LOG_NOTICE, strLog.c_str());
230
231 cec_command command;
232 cec_command::format(command, m_iLogicalAddress, iLogicalAddress, CEC_OPCODE_SET_OSD_STRING);
233 command.parameters.push_back((uint8_t)duration);
234
235 for (unsigned int iPtr = 0; iPtr < strlen(strMessage); iPtr++)
236 command.parameters.push_back(strMessage[iPtr]);
237
238 return Transmit(command);
239}
240
8b7e5ff6
LOK
241bool CCECProcessor::SwitchMonitoring(bool bEnable)
242{
243 CStdString strLog;
244 strLog.Format("== %s monitoring mode ==", bEnable ? "enabling" : "disabling");
245 m_controller->AddLog(CEC_LOG_NOTICE, strLog.c_str());
246
247 m_bMonitor = bEnable;
248 if (bEnable)
249 return m_communication && m_communication->SetAckMask(0);
250 else
251 return m_communication && m_communication->SetAckMask(0x1 << (uint8_t)m_iLogicalAddress);
252}
253
9dee1670 254bool CCECProcessor::TransmitFormatted(const cec_adapter_message &data, bool bWaitForAck /* = true */)
825ddb96 255{
2abe74eb
LOK
256 CLockObject lock(&m_mutex);
257 if (!m_communication || !m_communication->Write(data))
258 return false;
259
050df0f9 260 if (bWaitForAck)
2abe74eb 261 {
050df0f9
LOK
262 uint64_t now = GetTimeMs();
263 uint64_t target = now + 1000;
264 bool bError(false);
265 bool bGotAck(false);
266
267 while (!bGotAck && now < target)
268 {
06a1f7ce 269 bGotAck = WaitForAck(&bError, (uint32_t) (target - now));
050df0f9
LOK
270 now = GetTimeMs();
271
272 if (bError && now < target)
273 {
274 m_controller->AddLog(CEC_LOG_ERROR, "retransmitting previous frame");
275 if (!m_communication->Write(data))
276 return false;
277 }
278 }
2abe74eb
LOK
279 }
280
281 return true;
825ddb96 282}
abbca718 283
2abe74eb 284void CCECProcessor::TransmitAbort(cec_logical_address address, cec_opcode opcode, ECecAbortReason reason /* = CEC_ABORT_REASON_UNRECOGNIZED_OPCODE */)
abbca718 285{
9dee1670
LOK
286 m_controller->AddLog(CEC_LOG_DEBUG, "<< transmitting abort message");
287
06a1f7ce
LOK
288 cec_command command;
289 cec_command::format(command, m_iLogicalAddress, address, CEC_OPCODE_FEATURE_ABORT);
9dee1670
LOK
290 command.parameters.push_back((uint8_t)opcode);
291 command.parameters.push_back((uint8_t)reason);
292
293 Transmit(command);
abbca718
LOK
294}
295
2abe74eb 296void CCECProcessor::ReportCECVersion(cec_logical_address address /* = CECDEVICE_TV */)
abbca718 297{
88f45c9b 298 m_controller->AddLog(CEC_LOG_NOTICE, "<< reporting CEC version as 1.3a");
9dee1670 299
06a1f7ce
LOK
300 cec_command command;
301 cec_command::format(command, m_iLogicalAddress, address, CEC_OPCODE_CEC_VERSION);
9dee1670
LOK
302 command.parameters.push_back(CEC_VERSION_1_3A);
303
304 Transmit(command);
abbca718
LOK
305}
306
2abe74eb 307void CCECProcessor::ReportPowerState(cec_logical_address address /*= CECDEVICE_TV */, bool bOn /* = true */)
abbca718 308{
abbca718 309 if (bOn)
88f45c9b 310 m_controller->AddLog(CEC_LOG_NOTICE, "<< reporting \"On\" power status");
abbca718 311 else
88f45c9b 312 m_controller->AddLog(CEC_LOG_NOTICE, "<< reporting \"Off\" power status");
abbca718 313
06a1f7ce
LOK
314 cec_command command;
315 cec_command::format(command, m_iLogicalAddress, address, CEC_OPCODE_REPORT_POWER_STATUS);
9dee1670
LOK
316 command.parameters.push_back(bOn ? (uint8_t) CEC_POWER_STATUS_ON : (uint8_t) CEC_POWER_STATUS_STANDBY);
317
318 Transmit(command);
abbca718
LOK
319}
320
2abe74eb 321void CCECProcessor::ReportMenuState(cec_logical_address address /* = CECDEVICE_TV */, bool bActive /* = true */)
abbca718 322{
abbca718 323 if (bActive)
88f45c9b 324 m_controller->AddLog(CEC_LOG_NOTICE, "<< reporting menu state as active");
abbca718 325 else
88f45c9b 326 m_controller->AddLog(CEC_LOG_NOTICE, "<< reporting menu state as inactive");
abbca718 327
06a1f7ce
LOK
328 cec_command command;
329 cec_command::format(command, m_iLogicalAddress, address, CEC_OPCODE_MENU_STATUS);
9dee1670
LOK
330 command.parameters.push_back(bActive ? (uint8_t) CEC_MENU_STATE_ACTIVATED : (uint8_t) CEC_MENU_STATE_DEACTIVATED);
331
332 Transmit(command);
abbca718
LOK
333}
334
2abe74eb 335void CCECProcessor::ReportVendorID(cec_logical_address address /* = CECDEVICE_TV */)
abbca718 336{
88f45c9b 337 m_controller->AddLog(CEC_LOG_NOTICE, "<< vendor ID requested, feature abort");
abbca718
LOK
338 TransmitAbort(address, CEC_OPCODE_GIVE_DEVICE_VENDOR_ID);
339}
340
2abe74eb 341void CCECProcessor::ReportOSDName(cec_logical_address address /* = CECDEVICE_TV */)
abbca718 342{
abbca718
LOK
343 const char *osdname = m_strDeviceName.c_str();
344 CStdString strLog;
88f45c9b 345 strLog.Format("<< reporting OSD name as %s", osdname);
2abe74eb 346 m_controller->AddLog(CEC_LOG_NOTICE, strLog.c_str());
abbca718 347
06a1f7ce
LOK
348 cec_command command;
349 cec_command::format(command, m_iLogicalAddress, address, CEC_OPCODE_SET_OSD_NAME);
9dee1670
LOK
350 for (unsigned int iPtr = 0; iPtr < strlen(osdname); iPtr++)
351 command.parameters.push_back(osdname[iPtr]);
abbca718 352
9dee1670 353 Transmit(command);
abbca718
LOK
354}
355
2abe74eb 356void CCECProcessor::ReportPhysicalAddress(void)
abbca718 357{
abbca718 358 CStdString strLog;
2492216a 359 strLog.Format("<< reporting physical address as %04x", m_iPhysicalAddress);
2abe74eb 360 m_controller->AddLog(CEC_LOG_NOTICE, strLog.c_str());
9dee1670 361
06a1f7ce
LOK
362 cec_command command;
363 cec_command::format(command, m_iLogicalAddress, CECDEVICE_BROADCAST, CEC_OPCODE_REPORT_PHYSICAL_ADDRESS);
2492216a
LOK
364 command.parameters.push_back((uint8_t) ((m_iPhysicalAddress >> 8) & 0xFF));
365 command.parameters.push_back((uint8_t) (m_iPhysicalAddress & 0xFF));
4a75b661 366 command.parameters.push_back((uint8_t) (CEC_DEVICE_TYPE_PLAYBACK_DEVICE));
9dee1670
LOK
367
368 Transmit(command);
abbca718
LOK
369}
370
2abe74eb 371void CCECProcessor::BroadcastActiveSource(void)
abbca718 372{
a6d4fc28 373 m_controller->AddLog(CEC_LOG_NOTICE, "<< broadcasting active source");
abbca718 374
06a1f7ce
LOK
375 cec_command command;
376 cec_command::format(command, m_iLogicalAddress, CECDEVICE_BROADCAST, CEC_OPCODE_ACTIVE_SOURCE);
2492216a
LOK
377 command.parameters.push_back((uint8_t) ((m_iPhysicalAddress >> 8) & 0xFF));
378 command.parameters.push_back((uint8_t) (m_iPhysicalAddress & 0xFF));
9dee1670
LOK
379
380 Transmit(command);
abbca718
LOK
381}
382
050df0f9 383bool CCECProcessor::WaitForAck(bool *bError, uint32_t iTimeout /* = 1000 */)
abbca718 384{
78e8010a 385 bool bTransmitSucceeded = false, bEom = false;
050df0f9 386 *bError = false;
abbca718
LOK
387
388 int64_t iNow = GetTimeMs();
25701fa6 389 int64_t iTargetTime = iNow + (uint64_t) iTimeout;
abbca718 390
78e8010a 391 while (!bTransmitSucceeded && !*bError && (iTimeout == 0 || iNow < iTargetTime))
abbca718 392 {
9dee1670 393 cec_adapter_message msg;
25701fa6
LOK
394 msg.clear();
395
06a1f7ce 396 if (!m_communication->Read(msg, iTimeout > 0 ? (int32_t)(iTargetTime - iNow) : 1000))
abbca718 397 {
abbca718 398 iNow = GetTimeMs();
5d38b0b6
LOK
399 continue;
400 }
401
78e8010a 402 ParseMessage(msg, bError, &bTransmitSucceeded, &bEom, false);
5d38b0b6 403 iNow = GetTimeMs();
abbca718
LOK
404 }
405
78e8010a 406 return bTransmitSucceeded && !*bError;
abbca718
LOK
407}
408
78e8010a 409void CCECProcessor::ParseMessage(cec_adapter_message &msg, bool *bError, bool *bTransmitSucceeded, bool *bEom, bool bProcessMessages /* = true */)
abbca718 410{
78e8010a
LOK
411 *bError = false;
412 *bTransmitSucceeded = false;
413 *bEom = false;
60fa4578 414
a6b6469c 415 if (msg.empty())
78e8010a 416 return;
abbca718
LOK
417
418 CStdString logStr;
abbca718 419
a6b6469c 420 switch(msg.message())
abbca718
LOK
421 {
422 case MSGCODE_NOTHING:
2abe74eb 423 m_controller->AddLog(CEC_LOG_DEBUG, "MSGCODE_NOTHING");
abbca718
LOK
424 break;
425 case MSGCODE_TIMEOUT_ERROR:
426 case MSGCODE_HIGH_ERROR:
427 case MSGCODE_LOW_ERROR:
428 {
a6b6469c 429 if (msg.message() == MSGCODE_TIMEOUT_ERROR)
abbca718 430 logStr = "MSGCODE_TIMEOUT";
a6b6469c 431 else if (msg.message() == MSGCODE_HIGH_ERROR)
abbca718
LOK
432 logStr = "MSGCODE_HIGH_ERROR";
433 else
434 logStr = "MSGCODE_LOW_ERROR";
435
a6b6469c
LOK
436 int iLine = (msg.size() >= 3) ? (msg[1] << 8) | (msg[2]) : 0;
437 uint32_t iTime = (msg.size() >= 7) ? (msg[3] << 24) | (msg[4] << 16) | (msg[5] << 8) | (msg[6]) : 0;
abbca718
LOK
438 logStr.AppendFormat(" line:%i", iLine);
439 logStr.AppendFormat(" time:%u", iTime);
2abe74eb 440 m_controller->AddLog(CEC_LOG_WARNING, logStr.c_str());
78e8010a 441 *bError = true;
abbca718
LOK
442 }
443 break;
444 case MSGCODE_FRAME_START:
445 {
78e8010a 446 if (bProcessMessages)
abbca718 447 {
78e8010a
LOK
448 logStr = "MSGCODE_FRAME_START";
449 m_currentframe.clear();
450 if (msg.size() >= 2)
451 {
452 logStr.AppendFormat(" initiator:%u destination:%u ack:%s %s", msg.initiator(), msg.destination(), msg.ack() ? "high" : "low", msg.eom() ? "eom" : "");
453 m_currentframe.initiator = msg.initiator();
454 m_currentframe.destination = msg.destination();
455 m_currentframe.ack = msg.ack();
456 m_currentframe.eom = msg.eom();
457 }
458 m_controller->AddLog(CEC_LOG_DEBUG, logStr.c_str());
459 }
460 else
461 {
462 m_frameBuffer.Push(msg);
abbca718 463 }
abbca718
LOK
464 }
465 break;
466 case MSGCODE_FRAME_DATA:
467 {
78e8010a
LOK
468 if (bProcessMessages)
469 {
470 logStr = "MSGCODE_FRAME_DATA";
471 if (msg.size() >= 2)
472 {
473 uint8_t iData = msg[1];
474 logStr.AppendFormat(" %02x", iData);
475 m_currentframe.push_back(iData);
476 m_currentframe.eom = msg.eom();
477 }
478 m_controller->AddLog(CEC_LOG_DEBUG, logStr.c_str());
479 }
480 else
abbca718 481 {
78e8010a 482 m_frameBuffer.Push(msg);
abbca718 483 }
78e8010a
LOK
484
485 *bEom = msg.eom();
abbca718 486 }
78e8010a
LOK
487 break;
488 case MSGCODE_COMMAND_ACCEPTED:
489 m_controller->AddLog(CEC_LOG_DEBUG, "MSGCODE_COMMAND_ACCEPTED");
490 break;
491 case MSGCODE_TRANSMIT_SUCCEEDED:
492 m_controller->AddLog(CEC_LOG_DEBUG, "MSGCODE_TRANSMIT_SUCCEEDED");
493 *bTransmitSucceeded = true;
494 break;
495 case MSGCODE_RECEIVE_FAILED:
496 m_controller->AddLog(CEC_LOG_WARNING, "MSGCODE_RECEIVE_FAILED");
497 *bError = true;
498 break;
499 case MSGCODE_COMMAND_REJECTED:
500 m_controller->AddLog(CEC_LOG_WARNING, "MSGCODE_COMMAND_REJECTED");
501 *bError = true;
502 break;
503 case MSGCODE_TRANSMIT_FAILED_LINE:
504 m_controller->AddLog(CEC_LOG_WARNING, "MSGCODE_TRANSMIT_FAILED_LINE");
505 *bError = true;
506 break;
507 case MSGCODE_TRANSMIT_FAILED_ACK:
508 m_controller->AddLog(CEC_LOG_WARNING, "MSGCODE_TRANSMIT_FAILED_ACK");
509 *bError = true;
510 break;
511 case MSGCODE_TRANSMIT_FAILED_TIMEOUT_DATA:
512 m_controller->AddLog(CEC_LOG_WARNING, "MSGCODE_TRANSMIT_FAILED_TIMEOUT_DATA");
513 *bError = true;
514 break;
515 case MSGCODE_TRANSMIT_FAILED_TIMEOUT_LINE:
516 m_controller->AddLog(CEC_LOG_WARNING, "MSGCODE_TRANSMIT_FAILED_TIMEOUT_LINE");
517 *bError = true;
abbca718
LOK
518 break;
519 default:
520 break;
521 }
522}
523
9dee1670 524void CCECProcessor::ParseVendorId(cec_logical_address device, const cec_datapacket &data)
5f150ee2 525{
9dee1670 526 if (data.size < 3)
5f150ee2
LOK
527 {
528 m_controller->AddLog(CEC_LOG_WARNING, "invalid vendor ID received");
529 return;
530 }
531
a6b6469c
LOK
532 uint64_t iVendorId = ((uint64_t)data[0] << 3) +
533 ((uint64_t)data[1] << 2) +
534 (uint64_t)data[2];
5f150ee2
LOK
535
536 m_vendorIds[(uint8_t)device] = iVendorId;
9dee1670 537 m_vendorClasses[(uint8_t)device] = data.size >= 4 ? data[3] : 0;
5f150ee2
LOK
538
539 CStdString strLog;
6a69ddf2 540 strLog.Format("device %d: vendor = %s (%04x) class = %2x", (uint8_t)device, CECVendorIdToString(m_vendorIds[(uint8_t)device]), iVendorId, m_vendorClasses[(uint8_t)device]);
5f150ee2
LOK
541 m_controller->AddLog(CEC_LOG_DEBUG, strLog.c_str());
542}
543
dc113aca 544bool CCECProcessor::HandleANCommand(cec_command &command)
abbca718 545{
dc113aca
LOK
546 bool bHandled(true);
547 if (command.destination == m_iLogicalAddress)
abbca718 548 {
dc113aca
LOK
549 switch(command.opcode)
550 {
551 // TODO
552 default:
553 bHandled = false;
554 break;
555 }
556 }
557 else if (command.destination == CECDEVICE_BROADCAST)
558 {
559 switch(command.opcode)
560 {
561 // TODO
562 default:
563 bHandled = false;
564 break;
565 }
abbca718 566 }
abbca718 567
dc113aca
LOK
568 if (!bHandled)
569 bHandled = HandleCecCommand(command);
570
571 return bHandled;
572}
573
574bool CCECProcessor::HandleSLCommand(cec_command &command)
575{
576 bool bHandled(true);
577 if (command.destination == m_iLogicalAddress)
578 {
579 switch(command.opcode)
580 {
581 // TODO
582 default:
583 bHandled = false;
584 break;
585 }
586 }
587 else if (command.destination == CECDEVICE_BROADCAST)
588 {
589 switch(command.opcode)
590 {
591 // TODO
592 default:
593 bHandled = false;
594 break;
595 }
596 }
597
598 if (!bHandled)
599 bHandled = HandleCecCommand(command);
600
601 return bHandled;
602}
603
604bool CCECProcessor::HandleCecCommand(cec_command &command)
605{
606 bool bHandled(true);
8b7e5ff6 607
9dee1670 608 if (command.destination == m_iLogicalAddress)
abbca718 609 {
9dee1670 610 switch(command.opcode)
abbca718
LOK
611 {
612 case CEC_OPCODE_GIVE_PHYSICAL_ADDRESS:
613 ReportPhysicalAddress();
abbca718
LOK
614 break;
615 case CEC_OPCODE_GIVE_OSD_NAME:
9dee1670 616 ReportOSDName(command.initiator);
abbca718
LOK
617 break;
618 case CEC_OPCODE_GIVE_DEVICE_VENDOR_ID:
9dee1670 619 ReportVendorID(command.initiator);
abbca718 620 break;
50c806cd
LOK
621 case CEC_OPCODE_DEVICE_VENDOR_ID:
622 ParseVendorId(command.initiator, command.parameters);
623 break;
5f150ee2 624 case CEC_OPCODE_VENDOR_COMMAND_WITH_ID:
9dee1670 625 ParseVendorId(command.initiator, command.parameters);
5f150ee2 626 break;
594eb5cf
LOK
627 case CEC_OPCODE_GIVE_DECK_STATUS:
628 // need to support opcodes play and deck control before doing anything with this
9dee1670 629 TransmitAbort(command.initiator, CEC_OPCODE_GIVE_DECK_STATUS);
594eb5cf 630 break;
abbca718 631 case CEC_OPCODE_MENU_REQUEST:
9dee1670
LOK
632 if (command.parameters[0] == CEC_MENU_REQUEST_TYPE_QUERY)
633 ReportMenuState(command.initiator);
abbca718
LOK
634 break;
635 case CEC_OPCODE_GIVE_DEVICE_POWER_STATUS:
9dee1670 636 ReportPowerState(command.initiator);
abbca718
LOK
637 break;
638 case CEC_OPCODE_GET_CEC_VERSION:
9dee1670 639 ReportCECVersion(command.initiator);
abbca718
LOK
640 break;
641 case CEC_OPCODE_USER_CONTROL_PRESSED:
9dee1670 642 if (command.parameters.size > 0)
abbca718 643 {
2abe74eb 644 m_controller->AddKey();
abbca718 645
9dee1670 646 if (command.parameters[0] <= CEC_USER_CONTROL_CODE_MAX)
3cf3a86e
LOK
647 {
648 CStdString strLog;
649 strLog.Format("key pressed: %1x", command.parameters[0]);
650 m_controller->AddLog(CEC_LOG_DEBUG, strLog.c_str());
651
9dee1670 652 m_controller->SetCurrentButton((cec_user_control_code) command.parameters[0]);
3cf3a86e 653 }
abbca718
LOK
654 }
655 break;
656 case CEC_OPCODE_USER_CONTROL_RELEASE:
2abe74eb 657 m_controller->AddKey();
abbca718
LOK
658 break;
659 default:
9dee1670 660 m_controller->AddCommand(command);
dc113aca 661 bHandled = false;
abbca718
LOK
662 break;
663 }
664 }
9dee1670 665 else if (command.destination == CECDEVICE_BROADCAST)
abbca718 666 {
370757d1 667 CStdString strLog;
56b36fa6 668 switch (command.opcode)
abbca718 669 {
56b36fa6 670 case CEC_OPCODE_REQUEST_ACTIVE_SOURCE:
9dee1670 671 strLog.Format(">> %i requests active source", (uint8_t) command.initiator);
370757d1 672 m_controller->AddLog(CEC_LOG_DEBUG, strLog.c_str());
abbca718 673 BroadcastActiveSource();
56b36fa6
LOK
674 break;
675 case CEC_OPCODE_SET_STREAM_PATH:
9dee1670 676 if (command.parameters.size >= 2)
abbca718 677 {
56b36fa6 678 int streamaddr = ((uint16_t)command.parameters[0] << 8) | ((uint16_t)command.parameters[1]);
9dee1670 679 strLog.Format(">> %i requests stream path from physical address %04x", command.initiator, streamaddr);
2abe74eb 680 m_controller->AddLog(CEC_LOG_DEBUG, strLog.c_str());
2492216a 681 if (streamaddr == m_iPhysicalAddress)
abbca718
LOK
682 BroadcastActiveSource();
683 }
56b36fa6
LOK
684 break;
685 case CEC_OPCODE_ROUTING_CHANGE:
686 if (command.parameters.size == 4)
687 {
688 uint16_t iOldAddress = ((uint16_t)command.parameters[0] << 8) | ((uint16_t)command.parameters[1]);
689 uint16_t iNewAddress = ((uint16_t)command.parameters[2] << 8) | ((uint16_t)command.parameters[3]);
690 strLog.Format(">> %i changed physical address from %04x to %04x", command.initiator, iOldAddress, iNewAddress);
691 m_controller->AddLog(CEC_LOG_DEBUG, strLog.c_str());
692
693 m_controller->AddCommand(command);
694 }
695 break;
696 default:
9dee1670 697 m_controller->AddCommand(command);
dc113aca 698 bHandled = false;
56b36fa6 699 break;
c49c485b 700 }
abbca718
LOK
701 }
702 else
703 {
704 CStdString strLog;
9dee1670 705 strLog.Format("ignoring frame: destination: %u != %u", command.destination, (uint8_t)m_iLogicalAddress);
2abe74eb 706 m_controller->AddLog(CEC_LOG_DEBUG, strLog.c_str());
dc113aca
LOK
707 bHandled = false;
708 }
709
710 return bHandled;
711}
712
713void CCECProcessor::ParseCommand(cec_command &command)
714{
715 CStdString dataStr;
716 dataStr.Format(">> received frame: %1x%1x:%02x", command.initiator, command.destination, command.opcode);
715e0c66
LOK
717 for (uint8_t iPtr = 0; iPtr < command.parameters.size; iPtr++)
718 dataStr.AppendFormat(":%02x", (unsigned int)command.parameters[iPtr]);
dc113aca
LOK
719 m_controller->AddLog(CEC_LOG_DEBUG, dataStr.c_str());
720
721 if (!m_bMonitor)
722 {
723 switch(m_vendorIds[command.initiator])
724 {
725 case CEC_VENDOR_LG:
726 HandleSLCommand(command);
9b21e2c3 727 break;
dc113aca
LOK
728 case CEC_VENDOR_SAMSUNG:
729 HandleANCommand(command);
9b21e2c3 730 break;
dc113aca
LOK
731 default:
732 HandleCecCommand(command);
9b21e2c3 733 break;
dc113aca 734 }
abbca718
LOK
735 }
736}
acec5f48
LOK
737
738const char *CCECProcessor::CECVendorIdToString(const uint64_t iVendorId)
739{
740 switch (iVendorId)
741 {
742 case CEC_VENDOR_SAMSUNG:
743 return "Samsung";
6a69ddf2
LOK
744 case CEC_VENDOR_LG:
745 return "LG";
acec5f48
LOK
746 default:
747 return "Unknown";
748 }
749}