libcec v0.5 (WIP)
[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{
abbca718
LOK
50}
51
2abe74eb 52CCECProcessor::~CCECProcessor(void)
abbca718 53{
2abe74eb
LOK
54 StopThread();
55 m_communication = NULL;
56 m_controller = NULL;
abbca718
LOK
57}
58
2abe74eb 59bool CCECProcessor::Start(void)
abbca718 60{
2abe74eb 61 if (!m_communication || !m_communication->IsOpen())
a8f0bd18 62 return false;
abbca718 63
a8f0bd18 64 if (!SetLogicalAddress(m_iLogicalAddress))
abbca718 65 {
2abe74eb 66 m_controller->AddLog(CEC_LOG_ERROR, "could not set the logical address");
a8f0bd18 67 return false;
abbca718
LOK
68 }
69
60fa4578 70 if (CreateThread())
a8f0bd18 71 return true;
a8f0bd18 72 else
2abe74eb 73 m_controller->AddLog(CEC_LOG_ERROR, "could not create a processor thread");
abbca718 74
a8f0bd18 75 return false;
abbca718
LOK
76}
77
2abe74eb 78void *CCECProcessor::Process(void)
f99bc831 79{
2abe74eb 80 m_controller->AddLog(CEC_LOG_DEBUG, "processor thread started");
abbca718 81
60fa4578 82 while (!m_bStop)
abbca718 83 {
60fa4578
LOK
84 bool bParseFrame(false);
85 {
86 CLockObject lock(&m_mutex);
87 cec_frame msg;
25701fa6
LOK
88 msg.clear();
89
60fa4578
LOK
90 if (!m_bStop && m_communication->IsOpen() && m_communication->Read(msg, CEC_BUTTON_TIMEOUT))
91 bParseFrame = ParseMessage(msg);
92 }
93
f7e6ba70 94 if (!m_bStop && bParseFrame)
60fa4578 95 ParseCurrentFrame();
abbca718 96
f7e6ba70
LOK
97 if (!m_bStop)
98 {
99 m_controller->CheckKeypressTimeout();
12027dbe 100 Sleep(50);
f7e6ba70 101 }
abbca718
LOK
102 }
103
60fa4578 104 return NULL;
abbca718
LOK
105}
106
2abe74eb 107bool CCECProcessor::PowerOnDevices(cec_logical_address address /* = CECDEVICE_TV */)
abbca718 108{
60fa4578 109 if (!IsRunning())
f99bc831
LOK
110 return false;
111
abbca718
LOK
112 CStdString strLog;
113 strLog.Format("powering on devices with logical address %d", (int8_t)address);
2abe74eb 114 m_controller->AddLog(CEC_LOG_DEBUG, strLog.c_str());
abbca718 115 cec_frame frame;
25701fa6
LOK
116 frame.clear();
117
abbca718 118 frame.push_back(GetSourceDestination(address));
8bca69de 119 frame.push_back((uint8_t) CEC_OPCODE_TEXT_VIEW_ON);
abbca718
LOK
120 return Transmit(frame);
121}
122
2abe74eb 123bool CCECProcessor::StandbyDevices(cec_logical_address address /* = CECDEVICE_BROADCAST */)
abbca718 124{
60fa4578 125 if (!IsRunning())
f99bc831
LOK
126 return false;
127
abbca718
LOK
128 CStdString strLog;
129 strLog.Format("putting all devices with logical address %d in standby mode", (int8_t)address);
2abe74eb 130 m_controller->AddLog(CEC_LOG_DEBUG, strLog.c_str());
abbca718 131 cec_frame frame;
25701fa6
LOK
132 frame.clear();
133
abbca718 134 frame.push_back(GetSourceDestination(address));
8bca69de 135 frame.push_back((uint8_t) CEC_OPCODE_STANDBY);
abbca718
LOK
136 return Transmit(frame);
137}
138
2abe74eb 139bool CCECProcessor::SetActiveView(void)
abbca718 140{
60fa4578 141 if (!IsRunning())
f99bc831
LOK
142 return false;
143
2abe74eb 144 m_controller->AddLog(CEC_LOG_DEBUG, "setting active view");
abbca718 145 cec_frame frame;
25701fa6
LOK
146 frame.clear();
147
abbca718 148 frame.push_back(GetSourceDestination(CECDEVICE_BROADCAST));
8bca69de 149 frame.push_back((uint8_t) CEC_OPCODE_ACTIVE_SOURCE);
abbca718
LOK
150 frame.push_back((m_physicaladdress >> 8) & 0xFF);
151 frame.push_back(m_physicaladdress & 0xFF);
152 return Transmit(frame);
153}
154
2abe74eb 155bool CCECProcessor::SetInactiveView(void)
abbca718 156{
60fa4578 157 if (!IsRunning())
f99bc831
LOK
158 return false;
159
2abe74eb 160 m_controller->AddLog(CEC_LOG_DEBUG, "setting inactive view");
abbca718 161 cec_frame frame;
25701fa6
LOK
162 frame.clear();
163
abbca718 164 frame.push_back(GetSourceDestination(CECDEVICE_BROADCAST));
8bca69de 165 frame.push_back((uint8_t) CEC_OPCODE_INACTIVE_SOURCE);
abbca718
LOK
166 frame.push_back((m_physicaladdress >> 8) & 0xFF);
167 frame.push_back(m_physicaladdress & 0xFF);
168 return Transmit(frame);
169}
170
2abe74eb 171bool CCECProcessor::Transmit(const cec_frame &data, bool bWaitForAck /* = true */)
abbca718 172{
2abe74eb 173 CStdString txStr = "transmit ";
25701fa6
LOK
174 for (unsigned int i = 0; i < data.size; i++)
175 txStr.AppendFormat(" %02x", data.data[i]);
2abe74eb
LOK
176 m_controller->AddLog(CEC_LOG_DEBUG, txStr.c_str());
177
25701fa6 178 if (data.size == 0)
2abe74eb
LOK
179 {
180 m_controller->AddLog(CEC_LOG_WARNING, "transmit buffer is empty");
181 return false;
182 }
183
184 cec_frame output;
25701fa6 185 output.clear();
2abe74eb
LOK
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
25701fa6 192 if ((data.data[0] & 0xF) == 0xF)
2abe74eb
LOK
193 CAdapterCommunication::PushEscaped(output, CEC_TRUE);
194 else
195 CAdapterCommunication::PushEscaped(output, CEC_FALSE);
196
197 output.push_back(MSGEND);
198
25701fa6 199 for (int8_t i = 0; i < data.size; i++)
2abe74eb
LOK
200 {
201 output.push_back(MSGSTART);
202
25701fa6 203 if (i == (int8_t)data.size - 1)
2abe74eb
LOK
204 CAdapterCommunication::PushEscaped(output, MSGCODE_TRANSMIT_EOM);
205 else
206 CAdapterCommunication::PushEscaped(output, MSGCODE_TRANSMIT);
207
25701fa6 208 CAdapterCommunication::PushEscaped(output, data.data[i]);
2abe74eb
LOK
209
210 output.push_back(MSGEND);
211 }
212
213 return TransmitFormatted(output, bWaitForAck);
abbca718
LOK
214}
215
2abe74eb 216bool CCECProcessor::SetLogicalAddress(cec_logical_address iLogicalAddress)
abbca718 217{
2abe74eb
LOK
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);
abbca718 224}
825ddb96 225
2abe74eb 226bool CCECProcessor::TransmitFormatted(const cec_frame &data, bool bWaitForAck /* = true */)
825ddb96 227{
2abe74eb
LOK
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;
825ddb96 239}
abbca718 240
2abe74eb 241void CCECProcessor::TransmitAbort(cec_logical_address address, cec_opcode opcode, ECecAbortReason reason /* = CEC_ABORT_REASON_UNRECOGNIZED_OPCODE */)
abbca718 242{
2abe74eb 243 m_controller->AddLog(CEC_LOG_DEBUG, "transmitting abort message");
abbca718 244 cec_frame frame;
25701fa6
LOK
245 frame.clear();
246
abbca718 247 frame.push_back(GetSourceDestination(address));
8bca69de
LOK
248 frame.push_back((uint8_t) CEC_OPCODE_FEATURE_ABORT);
249 frame.push_back((uint8_t) opcode);
250 frame.push_back((uint8_t) reason);
abbca718
LOK
251 Transmit(frame);
252}
253
2abe74eb 254void CCECProcessor::ReportCECVersion(cec_logical_address address /* = CECDEVICE_TV */)
abbca718
LOK
255{
256 cec_frame frame;
25701fa6
LOK
257 frame.clear();
258
2abe74eb 259 m_controller->AddLog(CEC_LOG_NOTICE, "reporting CEC version as 1.3a");
abbca718 260 frame.push_back(GetSourceDestination(address));
8bca69de 261 frame.push_back((uint8_t) CEC_OPCODE_CEC_VERSION);
25701fa6 262 frame.push_back((uint8_t) CEC_VERSION_1_3A);
abbca718
LOK
263 Transmit(frame);
264}
265
2abe74eb 266void CCECProcessor::ReportPowerState(cec_logical_address address /*= CECDEVICE_TV */, bool bOn /* = true */)
abbca718
LOK
267{
268 cec_frame frame;
25701fa6
LOK
269 frame.clear();
270
abbca718 271 if (bOn)
2abe74eb 272 m_controller->AddLog(CEC_LOG_NOTICE, "reporting \"On\" power status");
abbca718 273 else
2abe74eb 274 m_controller->AddLog(CEC_LOG_NOTICE, "reporting \"Off\" power status");
abbca718
LOK
275
276 frame.push_back(GetSourceDestination(address));
8bca69de
LOK
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);
abbca718
LOK
279 Transmit(frame);
280}
281
2abe74eb 282void CCECProcessor::ReportMenuState(cec_logical_address address /* = CECDEVICE_TV */, bool bActive /* = true */)
abbca718
LOK
283{
284 cec_frame frame;
25701fa6
LOK
285 frame.clear();
286
abbca718 287 if (bActive)
2abe74eb 288 m_controller->AddLog(CEC_LOG_NOTICE, "reporting menu state as active");
abbca718 289 else
2abe74eb 290 m_controller->AddLog(CEC_LOG_NOTICE, "reporting menu state as inactive");
abbca718
LOK
291
292 frame.push_back(GetSourceDestination(address));
8bca69de
LOK
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);
abbca718
LOK
295 Transmit(frame);
296}
297
2abe74eb 298void CCECProcessor::ReportVendorID(cec_logical_address address /* = CECDEVICE_TV */)
abbca718 299{
2abe74eb 300 m_controller->AddLog(CEC_LOG_NOTICE, "vendor ID requested, feature abort");
abbca718
LOK
301 TransmitAbort(address, CEC_OPCODE_GIVE_DEVICE_VENDOR_ID);
302}
303
2abe74eb 304void CCECProcessor::ReportOSDName(cec_logical_address address /* = CECDEVICE_TV */)
abbca718
LOK
305{
306 cec_frame frame;
25701fa6
LOK
307 frame.clear();
308
abbca718
LOK
309 const char *osdname = m_strDeviceName.c_str();
310 CStdString strLog;
311 strLog.Format("reporting OSD name as %s", osdname);
2abe74eb 312 m_controller->AddLog(CEC_LOG_NOTICE, strLog.c_str());
abbca718 313 frame.push_back(GetSourceDestination(address));
8bca69de 314 frame.push_back((uint8_t) CEC_OPCODE_SET_OSD_NAME);
abbca718
LOK
315
316 for (unsigned int i = 0; i < strlen(osdname); i++)
317 frame.push_back(osdname[i]);
318
319 Transmit(frame);
320}
321
2abe74eb 322void CCECProcessor::ReportPhysicalAddress(void)
abbca718
LOK
323{
324 cec_frame frame;
25701fa6
LOK
325 frame.clear();
326
abbca718
LOK
327 CStdString strLog;
328 strLog.Format("reporting physical address as %04x", m_physicaladdress);
2abe74eb 329 m_controller->AddLog(CEC_LOG_NOTICE, strLog.c_str());
abbca718 330 frame.push_back(GetSourceDestination(CECDEVICE_BROADCAST));
8bca69de 331 frame.push_back((uint8_t) CEC_OPCODE_REPORT_PHYSICAL_ADDRESS);
25701fa6
LOK
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);
abbca718
LOK
335 Transmit(frame);
336}
337
2abe74eb 338void CCECProcessor::BroadcastActiveSource(void)
abbca718
LOK
339{
340 cec_frame frame;
25701fa6
LOK
341 frame.clear();
342
2abe74eb 343 m_controller->AddLog(CEC_LOG_NOTICE, "broadcasting active source");
abbca718 344 frame.push_back(GetSourceDestination(CECDEVICE_BROADCAST));
8bca69de 345 frame.push_back((uint8_t) CEC_OPCODE_ACTIVE_SOURCE);
25701fa6
LOK
346 frame.push_back((uint8_t) ((m_physicaladdress >> 8) & 0xFF));
347 frame.push_back((uint8_t) (m_physicaladdress & 0xFF));
abbca718
LOK
348 Transmit(frame);
349}
350
2abe74eb 351uint8_t CCECProcessor::GetSourceDestination(cec_logical_address destination /* = CECDEVICE_BROADCAST */) const
abbca718 352{
2abe74eb 353 return ((uint8_t)m_iLogicalAddress << 4) + (uint8_t)destination;
abbca718
LOK
354}
355
25701fa6 356bool CCECProcessor::WaitForAck(uint32_t iTimeout /* = 1000 */)
abbca718
LOK
357{
358 bool bGotAck(false);
359 bool bError(false);
360
361 int64_t iNow = GetTimeMs();
25701fa6 362 int64_t iTargetTime = iNow + (uint64_t) iTimeout;
abbca718 363
25701fa6 364 while (!bGotAck && !bError && (iTimeout == 0 || iNow < iTargetTime))
abbca718 365 {
abbca718 366 cec_frame msg;
25701fa6
LOK
367 msg.clear();
368
a8f0bd18 369 while (!bGotAck && !bError && m_communication->Read(msg, iTimeout))
abbca718 370 {
25701fa6 371 uint8_t iCode = msg.data[0] & ~(MSGCODE_FRAME_EOM | MSGCODE_FRAME_ACK);
abbca718
LOK
372
373 switch (iCode)
374 {
375 case MSGCODE_COMMAND_ACCEPTED:
2abe74eb 376 m_controller->AddLog(CEC_LOG_DEBUG, "MSGCODE_COMMAND_ACCEPTED");
abbca718
LOK
377 break;
378 case MSGCODE_TRANSMIT_SUCCEEDED:
2abe74eb 379 m_controller->AddLog(CEC_LOG_DEBUG, "MSGCODE_TRANSMIT_SUCCEEDED");
abbca718
LOK
380 // TODO
381 bGotAck = true;
382 break;
383 case MSGCODE_RECEIVE_FAILED:
2abe74eb 384 m_controller->AddLog(CEC_LOG_WARNING, "MSGCODE_RECEIVE_FAILED");
abbca718
LOK
385 bError = true;
386 break;
387 case MSGCODE_COMMAND_REJECTED:
2abe74eb 388 m_controller->AddLog(CEC_LOG_WARNING, "MSGCODE_COMMAND_REJECTED");
abbca718
LOK
389 bError = true;
390 break;
391 case MSGCODE_TRANSMIT_FAILED_LINE:
2abe74eb 392 m_controller->AddLog(CEC_LOG_WARNING, "MSGCODE_TRANSMIT_FAILED_LINE");
abbca718
LOK
393 bError = true;
394 break;
395 case MSGCODE_TRANSMIT_FAILED_ACK:
2abe74eb 396 m_controller->AddLog(CEC_LOG_WARNING, "MSGCODE_TRANSMIT_FAILED_ACK");
abbca718
LOK
397 bError = true;
398 break;
399 case MSGCODE_TRANSMIT_FAILED_TIMEOUT_DATA:
2abe74eb 400 m_controller->AddLog(CEC_LOG_WARNING, "MSGCODE_TRANSMIT_FAILED_TIMEOUT_DATA");
abbca718
LOK
401 bError = true;
402 break;
403 case MSGCODE_TRANSMIT_FAILED_TIMEOUT_LINE:
2abe74eb 404 m_controller->AddLog(CEC_LOG_WARNING, "MSGCODE_TRANSMIT_FAILED_TIMEOUT_LINE");
abbca718
LOK
405 bError = true;
406 break;
407 default:
408 m_frameBuffer.Push(msg);
25701fa6 409 bGotAck = (msg.data[0] & MSGCODE_FRAME_ACK) != 0;
abbca718
LOK
410 break;
411 }
412 iNow = GetTimeMs();
413 }
414 }
415
416 return bGotAck && !bError;
417}
418
2abe74eb 419bool CCECProcessor::ParseMessage(cec_frame &msg)
abbca718 420{
60fa4578
LOK
421 bool bReturn(false);
422
25701fa6 423 if (msg.size == 0)
60fa4578 424 return bReturn;
abbca718
LOK
425
426 CStdString logStr;
25701fa6
LOK
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;
abbca718
LOK
430
431 switch(iCode)
432 {
433 case MSGCODE_NOTHING:
2abe74eb 434 m_controller->AddLog(CEC_LOG_DEBUG, "MSGCODE_NOTHING");
abbca718
LOK
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
25701fa6
LOK
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;
abbca718
LOK
449 logStr.AppendFormat(" line:%i", iLine);
450 logStr.AppendFormat(" time:%u", iTime);
2abe74eb 451 m_controller->AddLog(CEC_LOG_WARNING, logStr.c_str());
abbca718
LOK
452 }
453 break;
454 case MSGCODE_FRAME_START:
455 {
456 logStr = "MSGCODE_FRAME_START";
457 m_currentframe.clear();
25701fa6 458 if (msg.size >= 2)
abbca718 459 {
25701fa6
LOK
460 int iInitiator = msg.data[1] >> 4;
461 int iDestination = msg.data[1] & 0xF;
abbca718
LOK
462 logStr.AppendFormat(" initiator:%u destination:%u ack:%s %s", iInitiator, iDestination, bAck ? "high" : "low", bEom ? "eom" : "");
463
25701fa6 464 m_currentframe.push_back(msg.data[1]);
abbca718 465 }
2abe74eb 466 m_controller->AddLog(CEC_LOG_DEBUG, logStr.c_str());
abbca718
LOK
467 }
468 break;
469 case MSGCODE_FRAME_DATA:
470 {
471 logStr = "MSGCODE_FRAME_DATA";
25701fa6 472 if (msg.size >= 2)
abbca718 473 {
25701fa6 474 uint8_t iData = msg.data[1];
abbca718
LOK
475 logStr.AppendFormat(" %02x", iData);
476 m_currentframe.push_back(iData);
477 }
2abe74eb 478 m_controller->AddLog(CEC_LOG_DEBUG, logStr.c_str());
abbca718
LOK
479 }
480 if (bEom)
60fa4578 481 bReturn = true;
abbca718
LOK
482 break;
483 default:
484 break;
485 }
60fa4578
LOK
486
487 return bReturn;
abbca718
LOK
488}
489
2abe74eb 490void CCECProcessor::ParseCurrentFrame(void)
abbca718 491{
25701fa6
LOK
492 uint8_t initiator = m_currentframe.data[0] >> 4;
493 uint8_t destination = m_currentframe.data[0] & 0xF;
abbca718
LOK
494
495 CStdString dataStr;
496 dataStr.Format("received frame: initiator: %u destination: %u", initiator, destination);
497
25701fa6 498 if (m_currentframe.size > 1)
abbca718
LOK
499 {
500 dataStr += " data:";
25701fa6
LOK
501 for (unsigned int i = 1; i < m_currentframe.size; i++)
502 dataStr.AppendFormat(" %02x", m_currentframe.data[i]);
abbca718 503 }
2abe74eb 504 m_controller->AddLog(CEC_LOG_DEBUG, dataStr.c_str());
abbca718 505
25701fa6 506 if (m_currentframe.size <= 1)
abbca718
LOK
507 return;
508
25701fa6 509 cec_opcode opCode = (cec_opcode) m_currentframe.data[1];
abbca718
LOK
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:
25701fa6 534 if (m_currentframe.size > 2)
abbca718 535 {
2abe74eb 536 m_controller->AddKey();
abbca718 537
25701fa6
LOK
538 if (m_currentframe.data[2] <= CEC_USER_CONTROL_CODE_MAX)
539 m_controller->SetCurrentButton((cec_user_control_code) m_currentframe.data[2]);
abbca718
LOK
540 }
541 break;
542 case CEC_OPCODE_USER_CONTROL_RELEASE:
2abe74eb 543 m_controller->AddKey();
abbca718
LOK
544 break;
545 default:
825ddb96 546 cec_frame params = m_currentframe;
25701fa6 547 params.shift(2);
2abe74eb 548 m_controller->AddCommand((cec_logical_address) initiator, (cec_logical_address) destination, opCode, &params);
abbca718
LOK
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 {
25701fa6 560 if (m_currentframe.size >= 4)
abbca718 561 {
25701fa6 562 int streamaddr = ((int)m_currentframe.data[2] << 8) | ((int)m_currentframe.data[3]);
abbca718
LOK
563 CStdString strLog;
564 strLog.Format("%i requests stream path from physical address %04x", initiator, streamaddr);
2abe74eb 565 m_controller->AddLog(CEC_LOG_DEBUG, strLog.c_str());
abbca718
LOK
566 if (streamaddr == m_physicaladdress)
567 BroadcastActiveSource();
568 }
569 }
c49c485b
LOK
570 else
571 {
572 cec_frame params = m_currentframe;
25701fa6 573 params.shift(2);
2abe74eb 574 m_controller->AddCommand((cec_logical_address) initiator, (cec_logical_address) destination, opCode, &params);
c49c485b 575 }
abbca718
LOK
576 }
577 else
578 {
579 CStdString strLog;
580 strLog.Format("ignoring frame: destination: %u != %u", destination, (uint16_t)m_iLogicalAddress);
2abe74eb 581 m_controller->AddLog(CEC_LOG_DEBUG, strLog.c_str());
abbca718
LOK
582 }
583}