cec: handle image view on and text view on
[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{
ab1469a0
LOK
57 m_logicalAddresses.Clear();
58 m_logicalAddresses.Set(iLogicalAddress);
20b8870a 59 m_types.clear();
7871d66e 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{
ab1469a0 72 m_logicalAddresses.Clear();
f8513317 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
b58d9277 140bool CCECProcessor::TryLogicalAddress(cec_logical_address address)
f8513317 141{
93fff5c1 142 if (m_busDevices[address]->TryLogicalAddress())
f8513317 143 {
8747dd4f 144 /* only set our OSD name and active source for the primary device */
ab1469a0 145 if (m_logicalAddresses.IsEmpty())
8747dd4f 146 {
787a3cb8 147 m_busDevices[address]->m_strDeviceName = m_strDeviceName;
8747dd4f
LOK
148 m_busDevices[address]->m_bActiveSource = true;
149 }
ab1469a0 150 m_logicalAddresses.Set(address);
f8513317
LOK
151
152 // TODO
b58d9277 153 m_busDevices[address]->SetPhysicalAddress((uint16_t)CEC_DEFAULT_PHYSICAL_ADDRESS);
f8513317
LOK
154
155 return true;
156 }
157
f8513317
LOK
158 return false;
159}
160
b58d9277 161bool CCECProcessor::FindLogicalAddressRecordingDevice(void)
f8513317
LOK
162{
163 AddLog(CEC_LOG_DEBUG, "detecting logical address for type 'recording device'");
b58d9277
LOK
164 return TryLogicalAddress(CECDEVICE_RECORDINGDEVICE1) ||
165 TryLogicalAddress(CECDEVICE_RECORDINGDEVICE2) ||
166 TryLogicalAddress(CECDEVICE_RECORDINGDEVICE3);
f8513317
LOK
167}
168
b58d9277 169bool CCECProcessor::FindLogicalAddressTuner(void)
f8513317
LOK
170{
171 AddLog(CEC_LOG_DEBUG, "detecting logical address for type 'tuner'");
b58d9277
LOK
172 return TryLogicalAddress(CECDEVICE_TUNER1) ||
173 TryLogicalAddress(CECDEVICE_TUNER2) ||
174 TryLogicalAddress(CECDEVICE_TUNER3) ||
175 TryLogicalAddress(CECDEVICE_TUNER4);
f8513317
LOK
176}
177
b58d9277 178bool CCECProcessor::FindLogicalAddressPlaybackDevice(void)
f8513317
LOK
179{
180 AddLog(CEC_LOG_DEBUG, "detecting logical address for type 'playback device'");
b58d9277
LOK
181 return TryLogicalAddress(CECDEVICE_PLAYBACKDEVICE1) ||
182 TryLogicalAddress(CECDEVICE_PLAYBACKDEVICE2) ||
183 TryLogicalAddress(CECDEVICE_PLAYBACKDEVICE3);
f8513317
LOK
184}
185
b58d9277 186bool CCECProcessor::FindLogicalAddressAudioSystem(void)
f8513317
LOK
187{
188 AddLog(CEC_LOG_DEBUG, "detecting logical address for type 'audio'");
b58d9277 189 return TryLogicalAddress(CECDEVICE_AUDIOSYSTEM);
f8513317
LOK
190}
191
192bool CCECProcessor::FindLogicalAddresses(void)
193{
194 bool bReturn(true);
ab1469a0 195 m_logicalAddresses.Clear();
f8513317
LOK
196 CStdString strLog;
197
198 for (unsigned int iPtr = 0; iPtr < 5; iPtr++)
199 {
200 if (m_types.types[iPtr] == CEC_DEVICE_TYPE_RESERVED)
201 continue;
202
203 strLog.Format("%s - device %d: type %d", __FUNCTION__, iPtr, m_types.types[iPtr]);
204 AddLog(CEC_LOG_DEBUG, strLog);
205
206 if (m_types.types[iPtr] == CEC_DEVICE_TYPE_RECORDING_DEVICE)
b58d9277 207 bReturn &= FindLogicalAddressRecordingDevice();
f8513317 208 if (m_types.types[iPtr] == CEC_DEVICE_TYPE_TUNER)
b58d9277 209 bReturn &= FindLogicalAddressTuner();
f8513317 210 if (m_types.types[iPtr] == CEC_DEVICE_TYPE_PLAYBACK_DEVICE)
b58d9277 211 bReturn &= FindLogicalAddressPlaybackDevice();
f8513317 212 if (m_types.types[iPtr] == CEC_DEVICE_TYPE_AUDIO_SYSTEM)
b58d9277 213 bReturn &= FindLogicalAddressAudioSystem();
f8513317
LOK
214 }
215
216 return bReturn;
217}
218
2abe74eb 219void *CCECProcessor::Process(void)
f99bc831 220{
8747dd4f 221 bool bParseFrame(false);
e0407d3d
LOK
222 cec_command command;
223 CCECAdapterMessage msg;
224
8b0ee2cc 225 {
ab1469a0 226 if (m_logicalAddresses.IsEmpty() && !FindLogicalAddresses())
f8513317
LOK
227 {
228 CLockObject lock(&m_mutex);
229 m_controller->AddLog(CEC_LOG_ERROR, "could not detect our logical addressed");
230 m_startCondition.Signal();
231 return NULL;
232 }
233
ab1469a0 234 SetAckMask(m_logicalAddresses.AckMask());
f8513317
LOK
235
236 {
237 CLockObject lock(&m_mutex);
238 m_controller->AddLog(CEC_LOG_DEBUG, "processor thread started");
239 m_bStarted = true;
240 m_startCondition.Signal();
241 }
8b0ee2cc 242 }
abbca718 243
13fd6a66 244 while (!IsStopped())
abbca718 245 {
ab1469a0 246 command.Clear();
76321de4
LOK
247 msg.clear();
248
60fa4578
LOK
249 {
250 CLockObject lock(&m_mutex);
8747dd4f
LOK
251 if (m_commandBuffer.Pop(command))
252 {
253 bParseFrame = true;
254 }
255 else if (m_communication->IsOpen() && m_communication->Read(msg, 50))
b5f34cf9
LOK
256 {
257 m_controller->AddLog(msg.is_error() ? CEC_LOG_WARNING : CEC_LOG_DEBUG, msg.ToString());
f2198ab5 258 if ((bParseFrame = (ParseMessage(msg) && !IsStopped())) == true)
8747dd4f 259 command = m_currentframe;
b5f34cf9 260 }
60fa4578
LOK
261 }
262
13fd6a66 263 if (bParseFrame)
9dee1670 264 ParseCommand(command);
8747dd4f 265 bParseFrame = false;
abbca718 266
b5f34cf9
LOK
267 Sleep(5);
268
13fd6a66
LOK
269 m_controller->CheckKeypressTimeout();
270
0ab58650 271 for (unsigned int iDevicePtr = 0; iDevicePtr < 16; iDevicePtr++)
678e227c
LOK
272 {
273 if (!m_logicalAddresses[iDevicePtr])
274 m_busDevices[iDevicePtr]->PollVendorId();
275 }
0ab58650 276
b5f34cf9 277 Sleep(5);
abbca718
LOK
278 }
279
60fa4578 280 return NULL;
abbca718
LOK
281}
282
18203d17 283bool CCECProcessor::SetActiveSource(cec_device_type type /* = CEC_DEVICE_TYPE_RESERVED */)
abbca718 284{
8ac9c610
LOK
285 bool bReturn(false);
286
60fa4578 287 if (!IsRunning())
8ac9c610 288 return bReturn;
f99bc831 289
18203d17 290 cec_logical_address addr = m_logicalAddresses.primary;
04437dcf 291
18203d17
LOK
292 if (type != CEC_DEVICE_TYPE_RESERVED)
293 {
f2198ab5 294 for (uint8_t iPtr = 0; iPtr < 16; iPtr++)
18203d17
LOK
295 {
296 if (m_logicalAddresses[iPtr] && m_busDevices[iPtr]->m_type == type)
297 {
298 addr = (cec_logical_address) iPtr;
299 break;
300 }
301 }
302 }
303
4a870dc8
LOK
304 return SetStreamPath(m_busDevices[addr]->GetPhysicalAddress()) &&
305 m_busDevices[addr]->TransmitActiveSource();
18203d17
LOK
306}
307
1a6669b8
LOK
308bool CCECProcessor::SetActiveSource(cec_logical_address iAddress)
309{
310 return SetStreamPath(m_busDevices[iAddress]->GetPhysicalAddress());
311}
312
18203d17
LOK
313bool CCECProcessor::SetActiveView(void)
314{
4a870dc8 315 return SetActiveSource(m_types.IsEmpty() ? CEC_DEVICE_TYPE_RESERVED : m_types[0]);
8ac9c610
LOK
316}
317
28fa6c97 318bool CCECProcessor::SetDeckControlMode(cec_deck_control_mode mode, bool bSendUpdate /* = true */)
a9232a79
LOK
319{
320 bool bReturn(false);
321
322 CCECBusDevice *device = GetDeviceByType(CEC_DEVICE_TYPE_PLAYBACK_DEVICE);
323 if (device)
324 {
325 ((CCECPlaybackDevice *) device)->SetDeckControlMode(mode);
28fa6c97
LOK
326 if (bSendUpdate)
327 ((CCECPlaybackDevice *) device)->TransmitDeckStatus(CECDEVICE_TV);
a9232a79
LOK
328 bReturn = true;
329 }
330
331 return bReturn;
332}
333
28fa6c97 334bool CCECProcessor::SetDeckInfo(cec_deck_info info, bool bSendUpdate /* = true */)
a9232a79
LOK
335{
336 bool bReturn(false);
337
338 CCECBusDevice *device = GetDeviceByType(CEC_DEVICE_TYPE_PLAYBACK_DEVICE);
339 if (device)
340 {
341 ((CCECPlaybackDevice *) device)->SetDeckStatus(info);
28fa6c97
LOK
342 if (bSendUpdate)
343 ((CCECPlaybackDevice *) device)->TransmitDeckStatus(CECDEVICE_TV);
a9232a79
LOK
344 bReturn = true;
345 }
346
347 return bReturn;
348}
349
8ac9c610
LOK
350bool CCECProcessor::SetStreamPath(uint16_t iStreamPath)
351{
04437dcf
LOK
352 bool bReturn(false);
353
8ac9c610
LOK
354 CCECBusDevice *device = GetDeviceByPhysicalAddress(iStreamPath);
355 if (device)
356 {
7856411b 357 device->SetActiveDevice();
91bea174 358 bReturn = true;
8ac9c610
LOK
359 }
360
04437dcf 361 return bReturn;
abbca718
LOK
362}
363
2abe74eb 364bool CCECProcessor::SetInactiveView(void)
abbca718 365{
60fa4578 366 if (!IsRunning())
f99bc831
LOK
367 return false;
368
ab1469a0 369 if (!m_logicalAddresses.IsEmpty() && m_busDevices[m_logicalAddresses.primary])
29912296 370 return m_busDevices[m_logicalAddresses.primary]->TransmitInactiveView();
cc60ab1c 371 return false;
abbca718
LOK
372}
373
9dee1670 374void CCECProcessor::LogOutput(const cec_command &data)
abbca718 375{
1d3ca3de 376 CStdString strTx;
57f45e6c
LOK
377 strTx.Format("<< %02x", ((uint8_t)data.initiator << 4) + (uint8_t)data.destination);
378 if (data.opcode_set)
379 strTx.AppendFormat(":%02x", (uint8_t)data.opcode);
9dee1670 380
06a1f7ce 381 for (uint8_t iPtr = 0; iPtr < data.parameters.size; iPtr++)
1d3ca3de
LOK
382 strTx.AppendFormat(":%02x", data.parameters[iPtr]);
383 m_controller->AddLog(CEC_LOG_TRAFFIC, strTx.c_str());
9dee1670 384}
2abe74eb 385
06bfd4d7 386bool CCECProcessor::SetLogicalAddress(cec_logical_address iLogicalAddress)
abbca718 387{
f8513317 388 if (m_logicalAddresses.primary != iLogicalAddress)
06775107
LOK
389 {
390 CStdString strLog;
f8513317 391 strLog.Format("<< setting primary logical address to %1x", iLogicalAddress);
06775107 392 m_controller->AddLog(CEC_LOG_NOTICE, strLog.c_str());
f8513317 393 m_logicalAddresses.primary = iLogicalAddress;
ab1469a0
LOK
394 m_logicalAddresses.Set(iLogicalAddress);
395 return SetAckMask(m_logicalAddresses.AckMask());
06775107 396 }
2abe74eb 397
06bfd4d7 398 return true;
abbca718 399}
825ddb96 400
28fa6c97
LOK
401bool CCECProcessor::SetMenuState(cec_menu_state state, bool bSendUpdate /* = true */)
402{
403 for (uint8_t iPtr = 0; iPtr < 16; iPtr++)
404 {
405 if (m_logicalAddresses[iPtr])
406 m_busDevices[iPtr]->SetMenuState(state);
407 }
408
409 if (bSendUpdate)
410 m_busDevices[m_logicalAddresses.primary]->TransmitMenuState(CECDEVICE_TV);
411
412 return true;
413}
414
2492216a
LOK
415bool CCECProcessor::SetPhysicalAddress(uint16_t iPhysicalAddress)
416{
ab1469a0 417 if (!m_logicalAddresses.IsEmpty() && m_busDevices[m_logicalAddresses.primary])
cc60ab1c 418 {
f8513317 419 m_busDevices[m_logicalAddresses.primary]->SetPhysicalAddress(iPhysicalAddress);
8ac9c610 420 return SetActiveView();
cc60ab1c
LOK
421 }
422 return false;
1969b140
LOK
423}
424
8b7e5ff6
LOK
425bool CCECProcessor::SwitchMonitoring(bool bEnable)
426{
427 CStdString strLog;
428 strLog.Format("== %s monitoring mode ==", bEnable ? "enabling" : "disabling");
429 m_controller->AddLog(CEC_LOG_NOTICE, strLog.c_str());
430
431 m_bMonitor = bEnable;
432 if (bEnable)
06bfd4d7 433 return SetAckMask(0);
8b7e5ff6 434 else
ab1469a0 435 return SetAckMask(m_logicalAddresses.AckMask());
8b7e5ff6
LOK
436}
437
57f45e6c
LOK
438bool CCECProcessor::PollDevice(cec_logical_address iAddress)
439{
440 if (iAddress != CECDEVICE_UNKNOWN && m_busDevices[iAddress])
93729720 441 return m_busDevices[m_logicalAddresses.primary]->TransmitPoll(iAddress);
57f45e6c
LOK
442 return false;
443}
444
8ac9c610
LOK
445CCECBusDevice *CCECProcessor::GetDeviceByPhysicalAddress(uint16_t iPhysicalAddress) const
446{
b58d9277
LOK
447 if (m_busDevices[m_logicalAddresses.primary]->GetPhysicalAddress() == iPhysicalAddress)
448 return m_busDevices[m_logicalAddresses.primary];
8ac9c610 449
b58d9277 450 CCECBusDevice *device = NULL;
8ac9c610
LOK
451 for (unsigned int iPtr = 0; iPtr < 16; iPtr++)
452 {
453 if (m_busDevices[iPtr]->GetPhysicalAddress() == iPhysicalAddress)
454 {
455 device = m_busDevices[iPtr];
456 break;
457 }
458 }
459
460 return device;
461}
462
a9232a79
LOK
463CCECBusDevice *CCECProcessor::GetDeviceByType(cec_device_type type) const
464{
465 CCECBusDevice *device = NULL;
466
467 for (unsigned int iPtr = 0; iPtr < 16; iPtr++)
468 {
469 if (m_busDevices[iPtr]->m_type == type)
470 {
471 device = m_busDevices[iPtr];
472 break;
473 }
474 }
475
476 return device;
477}
478
6a1c0009
LOK
479cec_version CCECProcessor::GetDeviceCecVersion(cec_logical_address iAddress)
480{
481 return m_busDevices[iAddress]->GetCecVersion();
482}
483
a3269a0a
LOK
484bool CCECProcessor::GetDeviceMenuLanguage(cec_logical_address iAddress, cec_menu_language *language)
485{
cc60ab1c
LOK
486 if (m_busDevices[iAddress])
487 {
488 *language = m_busDevices[iAddress]->GetMenuLanguage();
5744fbba 489 return (strcmp(language->language, "???") != 0);
cc60ab1c
LOK
490 }
491 return false;
a3269a0a
LOK
492}
493
44c74256
LOK
494uint64_t CCECProcessor::GetDeviceVendorId(cec_logical_address iAddress)
495{
cc60ab1c
LOK
496 if (m_busDevices[iAddress])
497 return m_busDevices[iAddress]->GetVendorId();
498 return false;
44c74256
LOK
499}
500
e55f3f70
LOK
501cec_power_status CCECProcessor::GetDevicePowerStatus(cec_logical_address iAddress)
502{
cc60ab1c
LOK
503 if (m_busDevices[iAddress])
504 return m_busDevices[iAddress]->GetPowerStatus();
505 return CEC_POWER_STATUS_UNKNOWN;
e55f3f70
LOK
506}
507
8d84e2c0 508bool CCECProcessor::Transmit(const cec_command &data)
825ddb96 509{
28352a04 510 bool bReturn(false);
13929cff
LOK
511 LogOutput(data);
512
28352a04
LOK
513 CCECAdapterMessage *output = new CCECAdapterMessage(data);
514 bReturn = Transmit(output);
515 delete output;
516
517 return bReturn;
06bfd4d7 518}
13929cff 519
28352a04 520bool CCECProcessor::Transmit(CCECAdapterMessage *output)
06bfd4d7
LOK
521{
522 bool bReturn(false);
2abe74eb 523 CLockObject lock(&m_mutex);
eb35e4ca
LOK
524 {
525 CLockObject msgLock(&output->mutex);
526 if (!m_communication || !m_communication->Write(output))
527 return bReturn;
528 else
0e31a62c 529 {
3a0ca6e1 530 output->condition.Wait(&output->mutex);
0e31a62c
LOK
531 if (output->state != ADAPTER_MESSAGE_STATE_SENT)
532 {
533 m_controller->AddLog(CEC_LOG_ERROR, "command was not sent");
534 return bReturn;
535 }
536 }
2abe74eb 537
06bfd4d7 538 if (output->transmit_timeout > 0)
cd505625 539 {
06bfd4d7 540 if ((bReturn = WaitForTransmitSucceeded(output->size(), output->transmit_timeout)) == false)
0d8f286e 541 m_controller->AddLog(CEC_LOG_DEBUG, "did not receive ack");
cd505625
LOK
542 }
543 else
cd505625 544 bReturn = true;
2abe74eb
LOK
545 }
546
7ea0d558 547 return bReturn;
825ddb96 548}
abbca718 549
22b4e74a 550void CCECProcessor::TransmitAbort(cec_logical_address address, cec_opcode opcode, cec_abort_reason reason /* = CEC_ABORT_REASON_UNRECOGNIZED_OPCODE */)
abbca718 551{
9dee1670
LOK
552 m_controller->AddLog(CEC_LOG_DEBUG, "<< transmitting abort message");
553
06a1f7ce 554 cec_command command;
f8513317 555 // TODO
ab1469a0
LOK
556 cec_command::Format(command, m_logicalAddresses.primary, address, CEC_OPCODE_FEATURE_ABORT);
557 command.parameters.PushBack((uint8_t)opcode);
558 command.parameters.PushBack((uint8_t)reason);
9dee1670
LOK
559
560 Transmit(command);
abbca718
LOK
561}
562
1f0e9e0f 563bool CCECProcessor::WaitForTransmitSucceeded(uint8_t iLength, uint32_t iTimeout /* = 1000 */)
abbca718 564{
1f0e9e0f
LOK
565 bool bError(false);
566 bool bTransmitSucceeded(false);
7ea0d558 567 uint8_t iPacketsLeft(iLength / 4);
abbca718
LOK
568
569 int64_t iNow = GetTimeMs();
25701fa6 570 int64_t iTargetTime = iNow + (uint64_t) iTimeout;
abbca718 571
1f0e9e0f 572 while (!bTransmitSucceeded && !bError && (iTimeout == 0 || iNow < iTargetTime))
abbca718 573 {
220537f2 574 CCECAdapterMessage msg;
25701fa6 575
06a1f7ce 576 if (!m_communication->Read(msg, iTimeout > 0 ? (int32_t)(iTargetTime - iNow) : 1000))
abbca718 577 {
abbca718 578 iNow = GetTimeMs();
5d38b0b6
LOK
579 continue;
580 }
581
edab6137 582 if ((bError = msg.is_error()) == false)
7ea0d558 583 {
0544ff56
LOK
584 m_controller->AddLog(bError ? CEC_LOG_WARNING : CEC_LOG_DEBUG, msg.ToString());
585
edab6137
LOK
586 switch(msg.message())
587 {
588 case MSGCODE_COMMAND_ACCEPTED:
589 if (iPacketsLeft > 0)
590 iPacketsLeft--;
591 break;
592 case MSGCODE_TRANSMIT_SUCCEEDED:
593 bTransmitSucceeded = (iPacketsLeft == 0);
594 bError = !bTransmitSucceeded;
595 break;
596 default:
8747dd4f
LOK
597 if (ParseMessage(msg))
598 m_commandBuffer.Push(m_currentframe);
edab6137 599 }
7ea0d558 600
edab6137
LOK
601 iNow = GetTimeMs();
602 }
abbca718
LOK
603 }
604
1f0e9e0f 605 return bTransmitSucceeded && !bError;
abbca718
LOK
606}
607
0e7b6c54 608bool CCECProcessor::ParseMessage(const CCECAdapterMessage &msg)
abbca718 609{
7ea0d558 610 bool bEom = false;
60fa4578 611
0e7b6c54 612 if (msg.empty())
7ea0d558 613 return bEom;
abbca718 614
0e7b6c54 615 switch(msg.message())
abbca718 616 {
abbca718
LOK
617 case MSGCODE_FRAME_START:
618 {
ab1469a0 619 m_currentframe.Clear();
0e7b6c54 620 if (msg.size() >= 2)
abbca718 621 {
0e7b6c54
LOK
622 m_currentframe.initiator = msg.initiator();
623 m_currentframe.destination = msg.destination();
624 m_currentframe.ack = msg.ack();
625 m_currentframe.eom = msg.eom();
abbca718 626 }
abbca718
LOK
627 }
628 break;
629 case MSGCODE_FRAME_DATA:
630 {
0e7b6c54 631 if (msg.size() >= 2)
78e8010a 632 {
ab1469a0 633 m_currentframe.PushBack(msg[1]);
0e7b6c54 634 m_currentframe.eom = msg.eom();
abbca718 635 }
0e7b6c54 636 bEom = msg.eom();
abbca718 637 }
78e8010a 638 break;
abbca718
LOK
639 default:
640 break;
641 }
7ea0d558
LOK
642
643 return bEom;
abbca718
LOK
644}
645
e9de9629 646void CCECProcessor::ParseCommand(cec_command &command)
abbca718 647{
e9de9629 648 CStdString dataStr;
1d3ca3de 649 dataStr.Format(">> %1x%1x:%02x", command.initiator, command.destination, command.opcode);
e9de9629
LOK
650 for (uint8_t iPtr = 0; iPtr < command.parameters.size; iPtr++)
651 dataStr.AppendFormat(":%02x", (unsigned int)command.parameters[iPtr]);
1d3ca3de 652 m_controller->AddLog(CEC_LOG_TRAFFIC, dataStr.c_str());
dc113aca 653
7871d66e 654 if (!m_bMonitor && command.initiator >= CECDEVICE_TV && command.initiator <= CECDEVICE_BROADCAST)
e9de9629 655 m_busDevices[(uint8_t)command.initiator]->HandleCommand(command);
dc113aca
LOK
656}
657
6d858ba4
LOK
658cec_logical_addresses CCECProcessor::GetActiveDevices(void)
659{
660 cec_logical_addresses addresses;
661 for (unsigned int iPtr = 0; iPtr < 15; iPtr++)
662 {
663 if (m_busDevices[iPtr]->GetStatus() == CEC_DEVICE_STATUS_PRESENT)
664 addresses.Set((cec_logical_address) iPtr);
665 }
666 return addresses;
667}
668
669bool CCECProcessor::IsActiveDevice(cec_logical_address address)
670{
671 return m_busDevices[address]->GetStatus() == CEC_DEVICE_STATUS_PRESENT;
672}
673
674bool CCECProcessor::IsActiveDeviceType(cec_device_type type)
675{
6d858ba4 676 for (unsigned int iPtr = 0; iPtr < 15; iPtr++)
2813bd07
LOK
677 {
678 if (m_busDevices[iPtr]->GetType() == type && m_busDevices[iPtr]->GetStatus() == CEC_DEVICE_STATUS_PRESENT)
6d858ba4 679 return true;
2813bd07
LOK
680 }
681
6d858ba4
LOK
682 return false;
683}
684
0f23c85c
LOK
685uint16_t CCECProcessor::GetPhysicalAddress(void) const
686{
ab1469a0 687 if (!m_logicalAddresses.IsEmpty() && m_busDevices[m_logicalAddresses.primary])
f8513317 688 return m_busDevices[m_logicalAddresses.primary]->GetPhysicalAddress();
cc60ab1c 689 return false;
0f23c85c
LOK
690}
691
e9de9629 692void CCECProcessor::SetCurrentButton(cec_user_control_code iButtonCode)
dc113aca 693{
e9de9629 694 m_controller->SetCurrentButton(iButtonCode);
dc113aca
LOK
695}
696
e9de9629 697void CCECProcessor::AddCommand(const cec_command &command)
dc113aca 698{
0544ff56 699 m_controller->AddCommand(command);
dc113aca
LOK
700}
701
95ba7a09
LOK
702void CCECProcessor::AddKey(cec_keypress &key)
703{
704 m_controller->AddKey(key);
705}
706
e9de9629 707void CCECProcessor::AddKey(void)
dc113aca 708{
e9de9629 709 m_controller->AddKey();
abbca718 710}
acec5f48 711
e9de9629 712void CCECProcessor::AddLog(cec_log_level level, const CStdString &strMessage)
acec5f48 713{
e9de9629 714 m_controller->AddLog(level, strMessage);
acec5f48 715}
06bfd4d7
LOK
716
717bool CCECProcessor::SetAckMask(uint16_t iMask)
718{
28352a04 719 bool bReturn(false);
06bfd4d7
LOK
720 CStdString strLog;
721 strLog.Format("setting ackmask to %2x", iMask);
722 m_controller->AddLog(CEC_LOG_DEBUG, strLog.c_str());
723
28352a04 724 CCECAdapterMessage *output = new CCECAdapterMessage;
06bfd4d7
LOK
725
726 output->push_back(MSGSTART);
727 output->push_escaped(MSGCODE_SET_ACK_MASK);
728 output->push_escaped(iMask >> 8);
729 output->push_escaped((uint8_t)iMask);
730 output->push_back(MSGEND);
731
28352a04 732 if ((bReturn = Transmit(output)) == false)
06bfd4d7 733 m_controller->AddLog(CEC_LOG_ERROR, "could not set the ackmask");
06bfd4d7 734
28352a04
LOK
735 delete output;
736
737 return bReturn;
06bfd4d7 738}