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