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