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