cec: buffer up commands that are received while waiting for an ack. only send 'active...
[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 35#include "AdapterCommunication.h"
eafa9d46 36#include "devices/CECBusDevice.h"
51b2a094
LOK
37#include "devices/CECAudioSystem.h"
38#include "devices/CECPlaybackDevice.h"
39#include "devices/CECRecordingDevice.h"
40#include "devices/CECTuner.h"
41#include "devices/CECTV.h"
2abe74eb 42#include "LibCEC.h"
abbca718 43#include "util/StdString.h"
b9187cc6 44#include "platform/timeutils.h"
abbca718
LOK
45
46using namespace CEC;
47using namespace std;
48
2b4d8297 49CCECProcessor::CCECProcessor(CLibCEC *controller, CAdapterCommunication *serComm, const char *strDeviceName, cec_logical_address iLogicalAddress /* = CECDEVICE_PLAYBACKDEVICE1 */, uint16_t iPhysicalAddress /* = CEC_DEFAULT_PHYSICAL_ADDRESS*/) :
f8513317 50 m_bStarted(false),
2abe74eb
LOK
51 m_strDeviceName(strDeviceName),
52 m_communication(serComm),
8b7e5ff6 53 m_controller(controller),
06bfd4d7 54 m_bMonitor(false)
abbca718 55{
f8513317
LOK
56 m_logicalAddresses.clear();
57 m_logicalAddresses.set(iLogicalAddress);
58 m_types.clear();
7ea0d558 59 for (int iPtr = 0; iPtr < 16; iPtr++)
09c10b66 60 m_busDevices[iPtr] = new CCECBusDevice(this, (cec_logical_address) iPtr, iPtr == iLogicalAddress ? iPhysicalAddress : 0);
abbca718
LOK
61}
62
f8513317
LOK
63CCECProcessor::CCECProcessor(CLibCEC *controller, CAdapterCommunication *serComm, const char *strDeviceName, const cec_device_type_list &types) :
64 m_bStarted(false),
65 m_strDeviceName(strDeviceName),
66 m_types(types),
67 m_communication(serComm),
68 m_controller(controller),
69 m_bMonitor(false)
70{
71 m_logicalAddresses.clear();
72 for (int iPtr = 0; iPtr < 16; iPtr++)
51b2a094
LOK
73 {
74 switch(iPtr)
75 {
76 case CECDEVICE_AUDIOSYSTEM:
5d5a2dc2 77 m_busDevices[iPtr] = new CCECAudioSystem(this, (cec_logical_address) iPtr, 0xFFFF);
51b2a094
LOK
78 break;
79 case CECDEVICE_PLAYBACKDEVICE1:
80 case CECDEVICE_PLAYBACKDEVICE2:
81 case CECDEVICE_PLAYBACKDEVICE3:
5d5a2dc2 82 m_busDevices[iPtr] = new CCECPlaybackDevice(this, (cec_logical_address) iPtr, 0xFFFF);
51b2a094
LOK
83 break;
84 case CECDEVICE_RECORDINGDEVICE1:
85 case CECDEVICE_RECORDINGDEVICE2:
86 case CECDEVICE_RECORDINGDEVICE3:
5d5a2dc2 87 m_busDevices[iPtr] = new CCECRecordingDevice(this, (cec_logical_address) iPtr, 0xFFFF);
51b2a094
LOK
88 break;
89 case CECDEVICE_TUNER1:
90 case CECDEVICE_TUNER2:
91 case CECDEVICE_TUNER3:
92 case CECDEVICE_TUNER4:
5d5a2dc2 93 m_busDevices[iPtr] = new CCECTuner(this, (cec_logical_address) iPtr, 0xFFFF);
51b2a094
LOK
94 break;
95 case CECDEVICE_TV:
96 m_busDevices[iPtr] = new CCECTV(this, (cec_logical_address) iPtr, 0);
97 break;
98 default:
5d5a2dc2 99 m_busDevices[iPtr] = new CCECBusDevice(this, (cec_logical_address) iPtr, 0xFFFF);
51b2a094
LOK
100 break;
101 }
102 }
f8513317
LOK
103}
104
2abe74eb 105CCECProcessor::~CCECProcessor(void)
abbca718 106{
8b0ee2cc 107 m_startCondition.Broadcast();
2abe74eb
LOK
108 StopThread();
109 m_communication = NULL;
110 m_controller = NULL;
d1998c7b
LOK
111 for (unsigned int iPtr = 0; iPtr < 16; iPtr++)
112 delete m_busDevices[iPtr];
abbca718
LOK
113}
114
2abe74eb 115bool CCECProcessor::Start(void)
abbca718 116{
8b0ee2cc 117 CLockObject lock(&m_mutex);
2abe74eb 118 if (!m_communication || !m_communication->IsOpen())
13fd6a66
LOK
119 {
120 m_controller->AddLog(CEC_LOG_ERROR, "connection is closed");
a8f0bd18 121 return false;
13fd6a66 122 }
abbca718 123
60fa4578 124 if (CreateThread())
8b0ee2cc 125 {
f8513317 126 if (!m_startCondition.Wait(&m_mutex) || !m_bStarted)
8b0ee2cc
LOK
127 {
128 m_controller->AddLog(CEC_LOG_ERROR, "could not create a processor thread");
129 return false;
130 }
a8f0bd18 131 return true;
8b0ee2cc 132 }
a8f0bd18 133 else
2abe74eb 134 m_controller->AddLog(CEC_LOG_ERROR, "could not create a processor thread");
abbca718 135
a8f0bd18 136 return false;
abbca718
LOK
137}
138
51b2a094 139bool CCECProcessor::TryLogicalAddress(cec_logical_address address, const char *strLabel, unsigned int iIndex)
f8513317
LOK
140{
141 CStdString strLog;
142 strLog.Format("trying logical address '%s'", strLabel);
143 AddLog(CEC_LOG_DEBUG, strLog);
144
145 SetAckMask(0x1 << address);
93729720 146 if (!m_busDevices[address]->TransmitPoll(address))
f8513317 147 {
f8513317
LOK
148 strLog.Format("using logical address '%s'", strLabel);
149 AddLog(CEC_LOG_NOTICE, strLog);
787a3cb8 150
8747dd4f 151 /* only set our OSD name and active source for the primary device */
787a3cb8 152 if (m_logicalAddresses.empty())
8747dd4f 153 {
787a3cb8 154 m_busDevices[address]->m_strDeviceName = m_strDeviceName;
8747dd4f
LOK
155 m_busDevices[address]->m_bActiveSource = true;
156 }
f2cb2c46 157 m_busDevices[address]->m_powerStatus = (m_types[0] == m_busDevices[address]->m_type) ? CEC_POWER_STATUS_ON : CEC_POWER_STATUS_STANDBY;
5e822b09 158 m_busDevices[address]->m_cecVersion = CEC_VERSION_1_3A;
f8513317
LOK
159 m_logicalAddresses.set(address);
160
161 // TODO
c0b1d868 162 m_busDevices[address]->SetPhysicalAddress(CEC_DEFAULT_PHYSICAL_ADDRESS + (iIndex * 0x100));
f8513317
LOK
163
164 return true;
165 }
166
167 strLog.Format("logical address '%s' already taken", strLabel);
168 AddLog(CEC_LOG_DEBUG, strLog);
169 return false;
170}
171
51b2a094 172bool CCECProcessor::FindLogicalAddressRecordingDevice(unsigned int iIndex)
f8513317
LOK
173{
174 AddLog(CEC_LOG_DEBUG, "detecting logical address for type 'recording device'");
51b2a094
LOK
175 return TryLogicalAddress(CECDEVICE_RECORDINGDEVICE1, "recording 1", iIndex) ||
176 TryLogicalAddress(CECDEVICE_RECORDINGDEVICE2, "recording 2", iIndex) ||
177 TryLogicalAddress(CECDEVICE_RECORDINGDEVICE3, "recording 3", iIndex);
f8513317
LOK
178}
179
51b2a094 180bool CCECProcessor::FindLogicalAddressTuner(unsigned int iIndex)
f8513317
LOK
181{
182 AddLog(CEC_LOG_DEBUG, "detecting logical address for type 'tuner'");
51b2a094
LOK
183 return TryLogicalAddress(CECDEVICE_TUNER1, "tuner 1", iIndex) ||
184 TryLogicalAddress(CECDEVICE_TUNER2, "tuner 2", iIndex) ||
185 TryLogicalAddress(CECDEVICE_TUNER3, "tuner 3", iIndex) ||
186 TryLogicalAddress(CECDEVICE_TUNER4, "tuner 4", iIndex);
f8513317
LOK
187}
188
51b2a094 189bool CCECProcessor::FindLogicalAddressPlaybackDevice(unsigned int iIndex)
f8513317
LOK
190{
191 AddLog(CEC_LOG_DEBUG, "detecting logical address for type 'playback device'");
51b2a094
LOK
192 return TryLogicalAddress(CECDEVICE_PLAYBACKDEVICE1, "playback 1", iIndex) ||
193 TryLogicalAddress(CECDEVICE_PLAYBACKDEVICE2, "playback 2", iIndex) ||
194 TryLogicalAddress(CECDEVICE_PLAYBACKDEVICE3, "playback 3", iIndex);
f8513317
LOK
195}
196
51b2a094 197bool CCECProcessor::FindLogicalAddressAudioSystem(unsigned int iIndex)
f8513317
LOK
198{
199 AddLog(CEC_LOG_DEBUG, "detecting logical address for type 'audio'");
51b2a094 200 return TryLogicalAddress(CECDEVICE_AUDIOSYSTEM, "audio", iIndex);
f8513317
LOK
201}
202
203bool CCECProcessor::FindLogicalAddresses(void)
204{
205 bool bReturn(true);
206 m_logicalAddresses.clear();
207 CStdString strLog;
208
209 for (unsigned int iPtr = 0; iPtr < 5; iPtr++)
210 {
211 if (m_types.types[iPtr] == CEC_DEVICE_TYPE_RESERVED)
212 continue;
213
214 strLog.Format("%s - device %d: type %d", __FUNCTION__, iPtr, m_types.types[iPtr]);
215 AddLog(CEC_LOG_DEBUG, strLog);
216
217 if (m_types.types[iPtr] == CEC_DEVICE_TYPE_RECORDING_DEVICE)
51b2a094 218 bReturn &= FindLogicalAddressRecordingDevice(iPtr);
f8513317 219 if (m_types.types[iPtr] == CEC_DEVICE_TYPE_TUNER)
51b2a094 220 bReturn &= FindLogicalAddressTuner(iPtr);
f8513317 221 if (m_types.types[iPtr] == CEC_DEVICE_TYPE_PLAYBACK_DEVICE)
51b2a094 222 bReturn &= FindLogicalAddressPlaybackDevice(iPtr);
f8513317 223 if (m_types.types[iPtr] == CEC_DEVICE_TYPE_AUDIO_SYSTEM)
51b2a094 224 bReturn &= FindLogicalAddressAudioSystem(iPtr);
f8513317
LOK
225 }
226
227 return bReturn;
228}
229
2abe74eb 230void *CCECProcessor::Process(void)
f99bc831 231{
8747dd4f 232 bool bParseFrame(false);
e0407d3d
LOK
233 cec_command command;
234 CCECAdapterMessage msg;
235
8b0ee2cc 236 {
f8513317
LOK
237 if (m_logicalAddresses.empty() && !FindLogicalAddresses())
238 {
239 CLockObject lock(&m_mutex);
240 m_controller->AddLog(CEC_LOG_ERROR, "could not detect our logical addressed");
241 m_startCondition.Signal();
242 return NULL;
243 }
244
245 SetAckMask(m_logicalAddresses.ackmask());
246
247 {
248 CLockObject lock(&m_mutex);
249 m_controller->AddLog(CEC_LOG_DEBUG, "processor thread started");
250 m_bStarted = true;
251 m_startCondition.Signal();
252 }
8b0ee2cc 253 }
abbca718 254
13fd6a66 255 while (!IsStopped())
abbca718 256 {
9dee1670 257 command.clear();
76321de4
LOK
258 msg.clear();
259
60fa4578
LOK
260 {
261 CLockObject lock(&m_mutex);
8747dd4f
LOK
262 if (m_commandBuffer.Pop(command))
263 {
264 bParseFrame = true;
265 }
266 else if (m_communication->IsOpen() && m_communication->Read(msg, 50))
b5f34cf9
LOK
267 {
268 m_controller->AddLog(msg.is_error() ? CEC_LOG_WARNING : CEC_LOG_DEBUG, msg.ToString());
8747dd4f
LOK
269 if ((bParseFrame = (ParseMessage(msg) && !IsStopped())))
270 command = m_currentframe;
b5f34cf9 271 }
60fa4578
LOK
272 }
273
13fd6a66 274 if (bParseFrame)
9dee1670 275 ParseCommand(command);
8747dd4f 276 bParseFrame = false;
abbca718 277
b5f34cf9
LOK
278 Sleep(5);
279
13fd6a66
LOK
280 m_controller->CheckKeypressTimeout();
281
0ab58650
LOK
282 for (unsigned int iDevicePtr = 0; iDevicePtr < 16; iDevicePtr++)
283 m_busDevices[iDevicePtr]->PollVendorId();
284
b5f34cf9 285 Sleep(5);
abbca718
LOK
286 }
287
60fa4578 288 return NULL;
abbca718
LOK
289}
290
2abe74eb 291bool CCECProcessor::SetActiveView(void)
abbca718 292{
60fa4578 293 if (!IsRunning())
f99bc831
LOK
294 return false;
295
f8513317 296 if (!m_logicalAddresses.empty() && m_busDevices[m_logicalAddresses.primary])
29912296 297 return m_busDevices[m_logicalAddresses.primary]->TransmitActiveView();
cc60ab1c 298 return false;
abbca718
LOK
299}
300
2abe74eb 301bool CCECProcessor::SetInactiveView(void)
abbca718 302{
60fa4578 303 if (!IsRunning())
f99bc831
LOK
304 return false;
305
f8513317 306 if (!m_logicalAddresses.empty() && m_busDevices[m_logicalAddresses.primary])
29912296 307 return m_busDevices[m_logicalAddresses.primary]->TransmitInactiveView();
cc60ab1c 308 return false;
abbca718
LOK
309}
310
9dee1670 311void CCECProcessor::LogOutput(const cec_command &data)
abbca718 312{
1d3ca3de 313 CStdString strTx;
57f45e6c
LOK
314 strTx.Format("<< %02x", ((uint8_t)data.initiator << 4) + (uint8_t)data.destination);
315 if (data.opcode_set)
316 strTx.AppendFormat(":%02x", (uint8_t)data.opcode);
9dee1670 317
06a1f7ce 318 for (uint8_t iPtr = 0; iPtr < data.parameters.size; iPtr++)
1d3ca3de
LOK
319 strTx.AppendFormat(":%02x", data.parameters[iPtr]);
320 m_controller->AddLog(CEC_LOG_TRAFFIC, strTx.c_str());
9dee1670 321}
2abe74eb 322
06bfd4d7 323bool CCECProcessor::SetLogicalAddress(cec_logical_address iLogicalAddress)
abbca718 324{
f8513317 325 if (m_logicalAddresses.primary != iLogicalAddress)
06775107
LOK
326 {
327 CStdString strLog;
f8513317 328 strLog.Format("<< setting primary logical address to %1x", iLogicalAddress);
06775107 329 m_controller->AddLog(CEC_LOG_NOTICE, strLog.c_str());
f8513317
LOK
330 m_logicalAddresses.primary = iLogicalAddress;
331 m_logicalAddresses.set(iLogicalAddress);
332 return SetAckMask(m_logicalAddresses.ackmask());
06775107 333 }
2abe74eb 334
06bfd4d7 335 return true;
abbca718 336}
825ddb96 337
2492216a
LOK
338bool CCECProcessor::SetPhysicalAddress(uint16_t iPhysicalAddress)
339{
f8513317 340 if (!m_logicalAddresses.empty() && m_busDevices[m_logicalAddresses.primary])
cc60ab1c 341 {
f8513317 342 m_busDevices[m_logicalAddresses.primary]->SetPhysicalAddress(iPhysicalAddress);
29912296 343 return m_busDevices[m_logicalAddresses.primary]->TransmitActiveView();
cc60ab1c
LOK
344 }
345 return false;
1969b140
LOK
346}
347
8b7e5ff6
LOK
348bool CCECProcessor::SwitchMonitoring(bool bEnable)
349{
350 CStdString strLog;
351 strLog.Format("== %s monitoring mode ==", bEnable ? "enabling" : "disabling");
352 m_controller->AddLog(CEC_LOG_NOTICE, strLog.c_str());
353
354 m_bMonitor = bEnable;
355 if (bEnable)
06bfd4d7 356 return SetAckMask(0);
8b7e5ff6 357 else
f8513317 358 return SetAckMask(m_logicalAddresses.ackmask());
8b7e5ff6
LOK
359}
360
57f45e6c
LOK
361bool CCECProcessor::PollDevice(cec_logical_address iAddress)
362{
363 if (iAddress != CECDEVICE_UNKNOWN && m_busDevices[iAddress])
93729720 364 return m_busDevices[m_logicalAddresses.primary]->TransmitPoll(iAddress);
57f45e6c
LOK
365 return false;
366}
367
6a1c0009
LOK
368cec_version CCECProcessor::GetDeviceCecVersion(cec_logical_address iAddress)
369{
370 return m_busDevices[iAddress]->GetCecVersion();
371}
372
a3269a0a
LOK
373bool CCECProcessor::GetDeviceMenuLanguage(cec_logical_address iAddress, cec_menu_language *language)
374{
cc60ab1c
LOK
375 if (m_busDevices[iAddress])
376 {
377 *language = m_busDevices[iAddress]->GetMenuLanguage();
5744fbba 378 return (strcmp(language->language, "???") != 0);
cc60ab1c
LOK
379 }
380 return false;
a3269a0a
LOK
381}
382
44c74256
LOK
383uint64_t CCECProcessor::GetDeviceVendorId(cec_logical_address iAddress)
384{
cc60ab1c
LOK
385 if (m_busDevices[iAddress])
386 return m_busDevices[iAddress]->GetVendorId();
387 return false;
44c74256
LOK
388}
389
e55f3f70
LOK
390cec_power_status CCECProcessor::GetDevicePowerStatus(cec_logical_address iAddress)
391{
cc60ab1c
LOK
392 if (m_busDevices[iAddress])
393 return m_busDevices[iAddress]->GetPowerStatus();
394 return CEC_POWER_STATUS_UNKNOWN;
e55f3f70
LOK
395}
396
8d84e2c0 397bool CCECProcessor::Transmit(const cec_command &data)
825ddb96 398{
28352a04 399 bool bReturn(false);
13929cff
LOK
400 LogOutput(data);
401
28352a04
LOK
402 CCECAdapterMessage *output = new CCECAdapterMessage(data);
403 bReturn = Transmit(output);
404 delete output;
405
406 return bReturn;
06bfd4d7 407}
13929cff 408
28352a04 409bool CCECProcessor::Transmit(CCECAdapterMessage *output)
06bfd4d7
LOK
410{
411 bool bReturn(false);
2abe74eb 412 CLockObject lock(&m_mutex);
eb35e4ca
LOK
413 {
414 CLockObject msgLock(&output->mutex);
415 if (!m_communication || !m_communication->Write(output))
416 return bReturn;
417 else
0e31a62c 418 {
3a0ca6e1 419 output->condition.Wait(&output->mutex);
0e31a62c
LOK
420 if (output->state != ADAPTER_MESSAGE_STATE_SENT)
421 {
422 m_controller->AddLog(CEC_LOG_ERROR, "command was not sent");
423 return bReturn;
424 }
425 }
2abe74eb 426
06bfd4d7 427 if (output->transmit_timeout > 0)
cd505625 428 {
06bfd4d7 429 if ((bReturn = WaitForTransmitSucceeded(output->size(), output->transmit_timeout)) == false)
0d8f286e 430 m_controller->AddLog(CEC_LOG_DEBUG, "did not receive ack");
cd505625
LOK
431 }
432 else
cd505625 433 bReturn = true;
2abe74eb
LOK
434 }
435
7ea0d558 436 return bReturn;
825ddb96 437}
abbca718 438
2abe74eb 439void CCECProcessor::TransmitAbort(cec_logical_address address, cec_opcode opcode, ECecAbortReason reason /* = CEC_ABORT_REASON_UNRECOGNIZED_OPCODE */)
abbca718 440{
9dee1670
LOK
441 m_controller->AddLog(CEC_LOG_DEBUG, "<< transmitting abort message");
442
06a1f7ce 443 cec_command command;
f8513317
LOK
444 // TODO
445 cec_command::format(command, m_logicalAddresses.primary, address, CEC_OPCODE_FEATURE_ABORT);
9dee1670
LOK
446 command.parameters.push_back((uint8_t)opcode);
447 command.parameters.push_back((uint8_t)reason);
448
449 Transmit(command);
abbca718
LOK
450}
451
1f0e9e0f 452bool CCECProcessor::WaitForTransmitSucceeded(uint8_t iLength, uint32_t iTimeout /* = 1000 */)
abbca718 453{
1f0e9e0f
LOK
454 bool bError(false);
455 bool bTransmitSucceeded(false);
7ea0d558 456 uint8_t iPacketsLeft(iLength / 4);
abbca718
LOK
457
458 int64_t iNow = GetTimeMs();
25701fa6 459 int64_t iTargetTime = iNow + (uint64_t) iTimeout;
abbca718 460
1f0e9e0f 461 while (!bTransmitSucceeded && !bError && (iTimeout == 0 || iNow < iTargetTime))
abbca718 462 {
220537f2 463 CCECAdapterMessage msg;
25701fa6 464
06a1f7ce 465 if (!m_communication->Read(msg, iTimeout > 0 ? (int32_t)(iTargetTime - iNow) : 1000))
abbca718 466 {
abbca718 467 iNow = GetTimeMs();
5d38b0b6
LOK
468 continue;
469 }
470
edab6137 471 if ((bError = msg.is_error()) == false)
7ea0d558 472 {
0544ff56
LOK
473 m_controller->AddLog(bError ? CEC_LOG_WARNING : CEC_LOG_DEBUG, msg.ToString());
474
edab6137
LOK
475 switch(msg.message())
476 {
477 case MSGCODE_COMMAND_ACCEPTED:
478 if (iPacketsLeft > 0)
479 iPacketsLeft--;
480 break;
481 case MSGCODE_TRANSMIT_SUCCEEDED:
482 bTransmitSucceeded = (iPacketsLeft == 0);
483 bError = !bTransmitSucceeded;
484 break;
485 default:
8747dd4f
LOK
486 if (ParseMessage(msg))
487 m_commandBuffer.Push(m_currentframe);
edab6137 488 }
7ea0d558 489
edab6137
LOK
490 iNow = GetTimeMs();
491 }
abbca718
LOK
492 }
493
1f0e9e0f 494 return bTransmitSucceeded && !bError;
abbca718
LOK
495}
496
0e7b6c54 497bool CCECProcessor::ParseMessage(const CCECAdapterMessage &msg)
abbca718 498{
7ea0d558 499 bool bEom = false;
60fa4578 500
0e7b6c54 501 if (msg.empty())
7ea0d558 502 return bEom;
abbca718 503
0e7b6c54 504 switch(msg.message())
abbca718 505 {
abbca718
LOK
506 case MSGCODE_FRAME_START:
507 {
7ea0d558 508 m_currentframe.clear();
0e7b6c54 509 if (msg.size() >= 2)
abbca718 510 {
0e7b6c54
LOK
511 m_currentframe.initiator = msg.initiator();
512 m_currentframe.destination = msg.destination();
513 m_currentframe.ack = msg.ack();
514 m_currentframe.eom = msg.eom();
abbca718 515 }
abbca718
LOK
516 }
517 break;
518 case MSGCODE_FRAME_DATA:
519 {
0e7b6c54 520 if (msg.size() >= 2)
78e8010a 521 {
e41b06f9 522 m_currentframe.push_back(msg[1]);
0e7b6c54 523 m_currentframe.eom = msg.eom();
abbca718 524 }
0e7b6c54 525 bEom = msg.eom();
abbca718 526 }
78e8010a 527 break;
abbca718
LOK
528 default:
529 break;
530 }
7ea0d558
LOK
531
532 return bEom;
abbca718
LOK
533}
534
e9de9629 535void CCECProcessor::ParseCommand(cec_command &command)
abbca718 536{
e9de9629 537 CStdString dataStr;
1d3ca3de 538 dataStr.Format(">> %1x%1x:%02x", command.initiator, command.destination, command.opcode);
e9de9629
LOK
539 for (uint8_t iPtr = 0; iPtr < command.parameters.size; iPtr++)
540 dataStr.AppendFormat(":%02x", (unsigned int)command.parameters[iPtr]);
1d3ca3de 541 m_controller->AddLog(CEC_LOG_TRAFFIC, dataStr.c_str());
dc113aca 542
e9de9629
LOK
543 if (!m_bMonitor)
544 m_busDevices[(uint8_t)command.initiator]->HandleCommand(command);
dc113aca
LOK
545}
546
0f23c85c
LOK
547uint16_t CCECProcessor::GetPhysicalAddress(void) const
548{
f8513317
LOK
549 if (!m_logicalAddresses.empty() && m_busDevices[m_logicalAddresses.primary])
550 return m_busDevices[m_logicalAddresses.primary]->GetPhysicalAddress();
cc60ab1c 551 return false;
0f23c85c
LOK
552}
553
e9de9629 554void CCECProcessor::SetCurrentButton(cec_user_control_code iButtonCode)
dc113aca 555{
e9de9629 556 m_controller->SetCurrentButton(iButtonCode);
dc113aca
LOK
557}
558
e9de9629 559void CCECProcessor::AddCommand(const cec_command &command)
dc113aca 560{
0544ff56 561 m_controller->AddCommand(command);
dc113aca
LOK
562}
563
95ba7a09
LOK
564void CCECProcessor::AddKey(cec_keypress &key)
565{
566 m_controller->AddKey(key);
567}
568
e9de9629 569void CCECProcessor::AddKey(void)
dc113aca 570{
e9de9629 571 m_controller->AddKey();
abbca718 572}
acec5f48 573
e9de9629 574void CCECProcessor::AddLog(cec_log_level level, const CStdString &strMessage)
acec5f48 575{
e9de9629 576 m_controller->AddLog(level, strMessage);
acec5f48 577}
06bfd4d7
LOK
578
579bool CCECProcessor::SetAckMask(uint16_t iMask)
580{
28352a04 581 bool bReturn(false);
06bfd4d7
LOK
582 CStdString strLog;
583 strLog.Format("setting ackmask to %2x", iMask);
584 m_controller->AddLog(CEC_LOG_DEBUG, strLog.c_str());
585
28352a04 586 CCECAdapterMessage *output = new CCECAdapterMessage;
06bfd4d7
LOK
587
588 output->push_back(MSGSTART);
589 output->push_escaped(MSGCODE_SET_ACK_MASK);
590 output->push_escaped(iMask >> 8);
591 output->push_escaped((uint8_t)iMask);
592 output->push_back(MSGEND);
593
28352a04 594 if ((bReturn = Transmit(output)) == false)
06bfd4d7 595 m_controller->AddLog(CEC_LOG_ERROR, "could not set the ackmask");
06bfd4d7 596
28352a04
LOK
597 delete output;
598
599 return bReturn;
06bfd4d7 600}