2 * This file is part of the libCEC(R) library.
4 * libCEC(R) is Copyright (C) 2011 Pulse-Eight Limited. All rights reserved.
5 * libCEC(R) is an original work, containing original code.
7 * libCEC(R) is a trademark of Pulse-Eight Limited.
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.
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.
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.
24 * Alternatively, you can license this library under a commercial license,
25 * please contact Pulse-Eight Licensing for more information.
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/
33 #include "CECProcessor.h"
35 #include "AdapterCommunication.h"
36 #include "devices/CECBusDevice.h"
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"
42 #include "implementations/CECCommandHandler.h"
44 #include "util/StdString.h"
45 #include "platform/timeutils.h"
50 CCECProcessor::CCECProcessor(CLibCEC
*controller
, CAdapterCommunication
*serComm
, const char *strDeviceName
, cec_logical_address iLogicalAddress
/* = CECDEVICE_PLAYBACKDEVICE1 */, uint16_t iPhysicalAddress
/* = CEC_DEFAULT_PHYSICAL_ADDRESS*/) :
52 m_strDeviceName(strDeviceName
),
53 m_communication(serComm
),
54 m_controller(controller
),
57 m_logicalAddresses
.Clear();
58 m_logicalAddresses
.Set(iLogicalAddress
);
60 for (int iPtr
= 0; iPtr
<= 16; iPtr
++)
61 m_busDevices
[iPtr
] = new CCECBusDevice(this, (cec_logical_address
) iPtr
, iPtr
== iLogicalAddress
? iPhysicalAddress
: 0);
64 CCECProcessor::CCECProcessor(CLibCEC
*controller
, CAdapterCommunication
*serComm
, const char *strDeviceName
, const cec_device_type_list
&types
) :
66 m_strDeviceName(strDeviceName
),
68 m_communication(serComm
),
69 m_controller(controller
),
72 m_logicalAddresses
.Clear();
73 for (int iPtr
= 0; iPtr
< 16; iPtr
++)
77 case CECDEVICE_AUDIOSYSTEM
:
78 m_busDevices
[iPtr
] = new CCECAudioSystem(this, (cec_logical_address
) iPtr
, 0xFFFF);
80 case CECDEVICE_PLAYBACKDEVICE1
:
81 case CECDEVICE_PLAYBACKDEVICE2
:
82 case CECDEVICE_PLAYBACKDEVICE3
:
83 m_busDevices
[iPtr
] = new CCECPlaybackDevice(this, (cec_logical_address
) iPtr
, 0xFFFF);
85 case CECDEVICE_RECORDINGDEVICE1
:
86 case CECDEVICE_RECORDINGDEVICE2
:
87 case CECDEVICE_RECORDINGDEVICE3
:
88 m_busDevices
[iPtr
] = new CCECRecordingDevice(this, (cec_logical_address
) iPtr
, 0xFFFF);
90 case CECDEVICE_TUNER1
:
91 case CECDEVICE_TUNER2
:
92 case CECDEVICE_TUNER3
:
93 case CECDEVICE_TUNER4
:
94 m_busDevices
[iPtr
] = new CCECTuner(this, (cec_logical_address
) iPtr
, 0xFFFF);
97 m_busDevices
[iPtr
] = new CCECTV(this, (cec_logical_address
) iPtr
, 0);
100 m_busDevices
[iPtr
] = new CCECBusDevice(this, (cec_logical_address
) iPtr
, 0xFFFF);
106 CCECProcessor::~CCECProcessor(void)
108 m_startCondition
.Broadcast();
110 m_communication
= NULL
;
112 for (unsigned int iPtr
= 0; iPtr
< 16; iPtr
++)
113 delete m_busDevices
[iPtr
];
116 bool CCECProcessor::Start(void)
118 CLockObject
lock(&m_mutex
);
119 if (!m_communication
|| !m_communication
->IsOpen())
121 m_controller
->AddLog(CEC_LOG_ERROR
, "connection is closed");
127 if (!m_startCondition
.Wait(&m_mutex
) || !m_bStarted
)
129 m_controller
->AddLog(CEC_LOG_ERROR
, "could not create a processor thread");
135 m_controller
->AddLog(CEC_LOG_ERROR
, "could not create a processor thread");
140 bool CCECProcessor::TryLogicalAddress(cec_logical_address address
)
142 if (m_busDevices
[address
]->TryLogicalAddress())
144 /* only set our OSD name and active source for the primary device */
145 if (m_logicalAddresses
.IsEmpty())
147 m_busDevices
[address
]->m_strDeviceName
= m_strDeviceName
;
148 m_busDevices
[address
]->m_bActiveSource
= true;
150 m_logicalAddresses
.Set(address
);
153 m_busDevices
[address
]->SetPhysicalAddress((uint16_t)CEC_DEFAULT_PHYSICAL_ADDRESS
);
161 bool CCECProcessor::FindLogicalAddressRecordingDevice(void)
163 AddLog(CEC_LOG_DEBUG
, "detecting logical address for type 'recording device'");
164 return TryLogicalAddress(CECDEVICE_RECORDINGDEVICE1
) ||
165 TryLogicalAddress(CECDEVICE_RECORDINGDEVICE2
) ||
166 TryLogicalAddress(CECDEVICE_RECORDINGDEVICE3
);
169 bool CCECProcessor::FindLogicalAddressTuner(void)
171 AddLog(CEC_LOG_DEBUG
, "detecting logical address for type 'tuner'");
172 return TryLogicalAddress(CECDEVICE_TUNER1
) ||
173 TryLogicalAddress(CECDEVICE_TUNER2
) ||
174 TryLogicalAddress(CECDEVICE_TUNER3
) ||
175 TryLogicalAddress(CECDEVICE_TUNER4
);
178 bool CCECProcessor::FindLogicalAddressPlaybackDevice(void)
180 AddLog(CEC_LOG_DEBUG
, "detecting logical address for type 'playback device'");
181 return TryLogicalAddress(CECDEVICE_PLAYBACKDEVICE1
) ||
182 TryLogicalAddress(CECDEVICE_PLAYBACKDEVICE2
) ||
183 TryLogicalAddress(CECDEVICE_PLAYBACKDEVICE3
);
186 bool CCECProcessor::FindLogicalAddressAudioSystem(void)
188 AddLog(CEC_LOG_DEBUG
, "detecting logical address for type 'audio'");
189 return TryLogicalAddress(CECDEVICE_AUDIOSYSTEM
);
192 bool CCECProcessor::FindLogicalAddresses(void)
195 m_logicalAddresses
.Clear();
198 for (unsigned int iPtr
= 0; iPtr
< 5; iPtr
++)
200 if (m_types
.types
[iPtr
] == CEC_DEVICE_TYPE_RESERVED
)
203 strLog
.Format("%s - device %d: type %d", __FUNCTION__
, iPtr
, m_types
.types
[iPtr
]);
204 AddLog(CEC_LOG_DEBUG
, strLog
);
206 if (m_types
.types
[iPtr
] == CEC_DEVICE_TYPE_RECORDING_DEVICE
)
207 bReturn
&= FindLogicalAddressRecordingDevice();
208 if (m_types
.types
[iPtr
] == CEC_DEVICE_TYPE_TUNER
)
209 bReturn
&= FindLogicalAddressTuner();
210 if (m_types
.types
[iPtr
] == CEC_DEVICE_TYPE_PLAYBACK_DEVICE
)
211 bReturn
&= FindLogicalAddressPlaybackDevice();
212 if (m_types
.types
[iPtr
] == CEC_DEVICE_TYPE_AUDIO_SYSTEM
)
213 bReturn
&= FindLogicalAddressAudioSystem();
219 void *CCECProcessor::Process(void)
221 bool bParseFrame(false);
223 CCECAdapterMessage msg
;
226 if (m_logicalAddresses
.IsEmpty() && !FindLogicalAddresses())
228 CLockObject
lock(&m_mutex
);
229 m_controller
->AddLog(CEC_LOG_ERROR
, "could not detect our logical addressed");
230 m_startCondition
.Signal();
234 SetAckMask(m_logicalAddresses
.AckMask());
237 CLockObject
lock(&m_mutex
);
238 m_controller
->AddLog(CEC_LOG_DEBUG
, "processor thread started");
240 m_startCondition
.Signal();
250 CLockObject
lock(&m_mutex
);
251 if (m_commandBuffer
.Pop(command
))
255 else if (m_communication
->IsOpen() && m_communication
->Read(msg
, 50))
257 m_controller
->AddLog(msg
.is_error() ? CEC_LOG_WARNING
: CEC_LOG_DEBUG
, msg
.ToString());
258 if ((bParseFrame
= (ParseMessage(msg
) && !IsStopped())) == true)
259 command
= m_currentframe
;
264 ParseCommand(command
);
269 m_controller
->CheckKeypressTimeout();
271 for (unsigned int iDevicePtr
= 0; iDevicePtr
< 16; iDevicePtr
++)
272 m_busDevices
[iDevicePtr
]->PollVendorId();
280 bool CCECProcessor::SetActiveSource(cec_device_type type
/* = CEC_DEVICE_TYPE_RESERVED */)
287 cec_logical_address addr
= m_logicalAddresses
.primary
;
289 if (type
!= CEC_DEVICE_TYPE_RESERVED
)
291 for (uint8_t iPtr
= 0; iPtr
< 16; iPtr
++)
293 if (m_logicalAddresses
[iPtr
] && m_busDevices
[iPtr
]->m_type
== type
)
295 addr
= (cec_logical_address
) iPtr
;
301 return SetStreamPath(m_busDevices
[addr
]->GetPhysicalAddress()) &&
302 m_busDevices
[addr
]->TransmitActiveSource();
305 bool CCECProcessor::SetActiveView(void)
307 return SetActiveSource(m_types
.IsEmpty() ? CEC_DEVICE_TYPE_RESERVED
: m_types
[0]);
310 bool CCECProcessor::SetDeckControlMode(cec_deck_control_mode mode
, bool bSendUpdate
/* = true */)
314 CCECBusDevice
*device
= GetDeviceByType(CEC_DEVICE_TYPE_PLAYBACK_DEVICE
);
317 ((CCECPlaybackDevice
*) device
)->SetDeckControlMode(mode
);
319 ((CCECPlaybackDevice
*) device
)->TransmitDeckStatus(CECDEVICE_TV
);
326 bool CCECProcessor::SetDeckInfo(cec_deck_info info
, bool bSendUpdate
/* = true */)
330 CCECBusDevice
*device
= GetDeviceByType(CEC_DEVICE_TYPE_PLAYBACK_DEVICE
);
333 ((CCECPlaybackDevice
*) device
)->SetDeckStatus(info
);
335 ((CCECPlaybackDevice
*) device
)->TransmitDeckStatus(CECDEVICE_TV
);
342 bool CCECProcessor::SetStreamPath(uint16_t iStreamPath
)
346 CCECBusDevice
*device
= GetDeviceByPhysicalAddress(iStreamPath
);
349 device
->SetActiveDevice();
356 bool CCECProcessor::SetInactiveView(void)
361 if (!m_logicalAddresses
.IsEmpty() && m_busDevices
[m_logicalAddresses
.primary
])
362 return m_busDevices
[m_logicalAddresses
.primary
]->TransmitInactiveView();
366 void CCECProcessor::LogOutput(const cec_command
&data
)
369 strTx
.Format("<< %02x", ((uint8_t)data
.initiator
<< 4) + (uint8_t)data
.destination
);
371 strTx
.AppendFormat(":%02x", (uint8_t)data
.opcode
);
373 for (uint8_t iPtr
= 0; iPtr
< data
.parameters
.size
; iPtr
++)
374 strTx
.AppendFormat(":%02x", data
.parameters
[iPtr
]);
375 m_controller
->AddLog(CEC_LOG_TRAFFIC
, strTx
.c_str());
378 bool CCECProcessor::SetLogicalAddress(cec_logical_address iLogicalAddress
)
380 if (m_logicalAddresses
.primary
!= iLogicalAddress
)
383 strLog
.Format("<< setting primary logical address to %1x", iLogicalAddress
);
384 m_controller
->AddLog(CEC_LOG_NOTICE
, strLog
.c_str());
385 m_logicalAddresses
.primary
= iLogicalAddress
;
386 m_logicalAddresses
.Set(iLogicalAddress
);
387 return SetAckMask(m_logicalAddresses
.AckMask());
393 bool CCECProcessor::SetMenuState(cec_menu_state state
, bool bSendUpdate
/* = true */)
395 for (uint8_t iPtr
= 0; iPtr
< 16; iPtr
++)
397 if (m_logicalAddresses
[iPtr
])
398 m_busDevices
[iPtr
]->SetMenuState(state
);
402 m_busDevices
[m_logicalAddresses
.primary
]->TransmitMenuState(CECDEVICE_TV
);
407 bool CCECProcessor::SetPhysicalAddress(uint16_t iPhysicalAddress
)
409 if (!m_logicalAddresses
.IsEmpty() && m_busDevices
[m_logicalAddresses
.primary
])
411 m_busDevices
[m_logicalAddresses
.primary
]->SetPhysicalAddress(iPhysicalAddress
);
412 return SetActiveView();
417 bool CCECProcessor::SwitchMonitoring(bool bEnable
)
420 strLog
.Format("== %s monitoring mode ==", bEnable
? "enabling" : "disabling");
421 m_controller
->AddLog(CEC_LOG_NOTICE
, strLog
.c_str());
423 m_bMonitor
= bEnable
;
425 return SetAckMask(0);
427 return SetAckMask(m_logicalAddresses
.AckMask());
430 bool CCECProcessor::PollDevice(cec_logical_address iAddress
)
432 if (iAddress
!= CECDEVICE_UNKNOWN
&& m_busDevices
[iAddress
])
433 return m_busDevices
[m_logicalAddresses
.primary
]->TransmitPoll(iAddress
);
437 CCECBusDevice
*CCECProcessor::GetDeviceByPhysicalAddress(uint16_t iPhysicalAddress
) const
439 if (m_busDevices
[m_logicalAddresses
.primary
]->GetPhysicalAddress() == iPhysicalAddress
)
440 return m_busDevices
[m_logicalAddresses
.primary
];
442 CCECBusDevice
*device
= NULL
;
443 for (unsigned int iPtr
= 0; iPtr
< 16; iPtr
++)
445 if (m_busDevices
[iPtr
]->GetPhysicalAddress() == iPhysicalAddress
)
447 device
= m_busDevices
[iPtr
];
455 CCECBusDevice
*CCECProcessor::GetDeviceByType(cec_device_type type
) const
457 CCECBusDevice
*device
= NULL
;
459 for (unsigned int iPtr
= 0; iPtr
< 16; iPtr
++)
461 if (m_busDevices
[iPtr
]->m_type
== type
)
463 device
= m_busDevices
[iPtr
];
471 cec_version
CCECProcessor::GetDeviceCecVersion(cec_logical_address iAddress
)
473 return m_busDevices
[iAddress
]->GetCecVersion();
476 bool CCECProcessor::GetDeviceMenuLanguage(cec_logical_address iAddress
, cec_menu_language
*language
)
478 if (m_busDevices
[iAddress
])
480 *language
= m_busDevices
[iAddress
]->GetMenuLanguage();
481 return (strcmp(language
->language
, "???") != 0);
486 uint64_t CCECProcessor::GetDeviceVendorId(cec_logical_address iAddress
)
488 if (m_busDevices
[iAddress
])
489 return m_busDevices
[iAddress
]->GetVendorId();
493 cec_power_status
CCECProcessor::GetDevicePowerStatus(cec_logical_address iAddress
)
495 if (m_busDevices
[iAddress
])
496 return m_busDevices
[iAddress
]->GetPowerStatus();
497 return CEC_POWER_STATUS_UNKNOWN
;
500 bool CCECProcessor::Transmit(const cec_command
&data
)
505 CCECAdapterMessage
*output
= new CCECAdapterMessage(data
);
506 bReturn
= Transmit(output
);
512 bool CCECProcessor::Transmit(CCECAdapterMessage
*output
)
515 CLockObject
lock(&m_mutex
);
517 CLockObject
msgLock(&output
->mutex
);
518 if (!m_communication
|| !m_communication
->Write(output
))
522 output
->condition
.Wait(&output
->mutex
);
523 if (output
->state
!= ADAPTER_MESSAGE_STATE_SENT
)
525 m_controller
->AddLog(CEC_LOG_ERROR
, "command was not sent");
530 if (output
->transmit_timeout
> 0)
532 if ((bReturn
= WaitForTransmitSucceeded(output
->size(), output
->transmit_timeout
)) == false)
533 m_controller
->AddLog(CEC_LOG_DEBUG
, "did not receive ack");
542 void CCECProcessor::TransmitAbort(cec_logical_address address
, cec_opcode opcode
, cec_abort_reason reason
/* = CEC_ABORT_REASON_UNRECOGNIZED_OPCODE */)
544 m_controller
->AddLog(CEC_LOG_DEBUG
, "<< transmitting abort message");
548 cec_command::Format(command
, m_logicalAddresses
.primary
, address
, CEC_OPCODE_FEATURE_ABORT
);
549 command
.parameters
.PushBack((uint8_t)opcode
);
550 command
.parameters
.PushBack((uint8_t)reason
);
555 bool CCECProcessor::WaitForTransmitSucceeded(uint8_t iLength
, uint32_t iTimeout
/* = 1000 */)
558 bool bTransmitSucceeded(false);
559 uint8_t iPacketsLeft(iLength
/ 4);
561 int64_t iNow
= GetTimeMs();
562 int64_t iTargetTime
= iNow
+ (uint64_t) iTimeout
;
564 while (!bTransmitSucceeded
&& !bError
&& (iTimeout
== 0 || iNow
< iTargetTime
))
566 CCECAdapterMessage msg
;
568 if (!m_communication
->Read(msg
, iTimeout
> 0 ? (int32_t)(iTargetTime
- iNow
) : 1000))
574 if ((bError
= msg
.is_error()) == false)
576 m_controller
->AddLog(bError
? CEC_LOG_WARNING
: CEC_LOG_DEBUG
, msg
.ToString());
578 switch(msg
.message())
580 case MSGCODE_COMMAND_ACCEPTED
:
581 if (iPacketsLeft
> 0)
584 case MSGCODE_TRANSMIT_SUCCEEDED
:
585 bTransmitSucceeded
= (iPacketsLeft
== 0);
586 bError
= !bTransmitSucceeded
;
589 if (ParseMessage(msg
))
590 m_commandBuffer
.Push(m_currentframe
);
597 return bTransmitSucceeded
&& !bError
;
600 bool CCECProcessor::ParseMessage(const CCECAdapterMessage
&msg
)
607 switch(msg
.message())
609 case MSGCODE_FRAME_START
:
611 m_currentframe
.Clear();
614 m_currentframe
.initiator
= msg
.initiator();
615 m_currentframe
.destination
= msg
.destination();
616 m_currentframe
.ack
= msg
.ack();
617 m_currentframe
.eom
= msg
.eom();
621 case MSGCODE_FRAME_DATA
:
625 m_currentframe
.PushBack(msg
[1]);
626 m_currentframe
.eom
= msg
.eom();
638 void CCECProcessor::ParseCommand(cec_command
&command
)
641 dataStr
.Format(">> %1x%1x:%02x", command
.initiator
, command
.destination
, command
.opcode
);
642 for (uint8_t iPtr
= 0; iPtr
< command
.parameters
.size
; iPtr
++)
643 dataStr
.AppendFormat(":%02x", (unsigned int)command
.parameters
[iPtr
]);
644 m_controller
->AddLog(CEC_LOG_TRAFFIC
, dataStr
.c_str());
646 if (!m_bMonitor
&& command
.initiator
>= CECDEVICE_TV
&& command
.initiator
<= CECDEVICE_BROADCAST
)
647 m_busDevices
[(uint8_t)command
.initiator
]->HandleCommand(command
);
650 cec_logical_addresses
CCECProcessor::GetActiveDevices(void)
652 cec_logical_addresses addresses
;
653 for (unsigned int iPtr
= 0; iPtr
< 15; iPtr
++)
655 if (m_busDevices
[iPtr
]->GetStatus() == CEC_DEVICE_STATUS_PRESENT
)
656 addresses
.Set((cec_logical_address
) iPtr
);
661 bool CCECProcessor::IsActiveDevice(cec_logical_address address
)
663 return m_busDevices
[address
]->GetStatus() == CEC_DEVICE_STATUS_PRESENT
;
666 bool CCECProcessor::IsActiveDeviceType(cec_device_type type
)
668 cec_logical_addresses activeDevices
= GetActiveDevices();
669 for (unsigned int iPtr
= 0; iPtr
< 15; iPtr
++)
670 if (activeDevices
.IsSet((cec_logical_address
) iPtr
) && m_busDevices
[iPtr
]->GetType() == type
)
675 uint16_t CCECProcessor::GetPhysicalAddress(void) const
677 if (!m_logicalAddresses
.IsEmpty() && m_busDevices
[m_logicalAddresses
.primary
])
678 return m_busDevices
[m_logicalAddresses
.primary
]->GetPhysicalAddress();
682 void CCECProcessor::SetCurrentButton(cec_user_control_code iButtonCode
)
684 m_controller
->SetCurrentButton(iButtonCode
);
687 void CCECProcessor::AddCommand(const cec_command
&command
)
689 m_controller
->AddCommand(command
);
692 void CCECProcessor::AddKey(cec_keypress
&key
)
694 m_controller
->AddKey(key
);
697 void CCECProcessor::AddKey(void)
699 m_controller
->AddKey();
702 void CCECProcessor::AddLog(cec_log_level level
, const CStdString
&strMessage
)
704 m_controller
->AddLog(level
, strMessage
);
707 bool CCECProcessor::SetAckMask(uint16_t iMask
)
711 strLog
.Format("setting ackmask to %2x", iMask
);
712 m_controller
->AddLog(CEC_LOG_DEBUG
, strLog
.c_str());
714 CCECAdapterMessage
*output
= new CCECAdapterMessage
;
716 output
->push_back(MSGSTART
);
717 output
->push_escaped(MSGCODE_SET_ACK_MASK
);
718 output
->push_escaped(iMask
>> 8);
719 output
->push_escaped((uint8_t)iMask
);
720 output
->push_back(MSGEND
);
722 if ((bReturn
= Transmit(output
)) == false)
723 m_controller
->AddLog(CEC_LOG_ERROR
, "could not set the ackmask");