#include "../CECClient.h"
#include "../CECProcessor.h"
#include "../LibCEC.h"
+#include "../CECTypeUtils.h"
+#include "../platform/util/util.h"
using namespace CEC;
using namespace std;
using namespace PLATFORM;
#define LIB_CEC m_busDevice->GetProcessor()->GetLib()
-#define ToString(p) LIB_CEC->ToString(p)
+#define ToString(p) CCECTypeUtils::ToString(p)
CCECCommandHandler::CCECCommandHandler(CCECBusDevice *busDevice) :
m_busDevice(busDevice),
CCECCommandHandler::~CCECCommandHandler(void)
{
- delete m_waitForResponse;
+ DELETE_AND_NULL(m_waitForResponse);
}
bool CCECCommandHandler::HandleCommand(const cec_command &command)
bool CCECCommandHandler::HandleDeckControl(const cec_command &command)
{
- CCECBusDevice *device = GetDevice(command.destination);
- if (device && (device->GetType() == CEC_DEVICE_TYPE_PLAYBACK_DEVICE || device->GetType() == CEC_DEVICE_TYPE_RECORDING_DEVICE) && command.parameters.size > 0)
+ CCECPlaybackDevice *device = CCECBusDevice::AsPlaybackDevice(GetDevice(command.destination));
+ if (device && command.parameters.size > 0)
{
- ((CCECPlaybackDevice *) device)->SetDeckControlMode((cec_deck_control_mode) command.parameters[0]);
+ device->SetDeckControlMode((cec_deck_control_mode) command.parameters[0]);
return true;
}
bool CCECCommandHandler::HandleDeviceVendorCommandWithId(const cec_command &command)
{
- if (m_processor->IsRunning() && m_processor->IsHandledByLibCEC(command.destination))
- m_processor->TransmitAbort(m_busDevice->GetLogicalAddress(), command.initiator, command.opcode, CEC_ABORT_REASON_REFUSED);
+ if (m_processor->CECInitialised() && m_processor->IsHandledByLibCEC(command.destination))
+ m_processor->TransmitAbort(command.destination, command.initiator, command.opcode, CEC_ABORT_REASON_REFUSED);
return true;
}
bool CCECCommandHandler::HandleGetCecVersion(const cec_command &command)
{
- if (m_processor->IsRunning() && m_processor->IsHandledByLibCEC(command.destination))
+ if (m_processor->CECInitialised() && m_processor->IsHandledByLibCEC(command.destination))
{
CCECBusDevice *device = GetDevice(command.destination);
if (device)
bool CCECCommandHandler::HandleGiveAudioStatus(const cec_command &command)
{
- if (m_processor->IsRunning() && m_processor->IsHandledByLibCEC(command.destination))
+ if (m_processor->CECInitialised() && m_processor->IsHandledByLibCEC(command.destination))
{
- CCECBusDevice *device = GetDevice(command.destination);
- if (device && device->GetType() == CEC_DEVICE_TYPE_AUDIO_SYSTEM)
- return ((CCECAudioSystem *) device)->TransmitAudioStatus(command.initiator);
+ CCECAudioSystem *device = CCECBusDevice::AsAudioSystem(GetDevice(command.destination));
+ if (device)
+ return device->TransmitAudioStatus(command.initiator);
}
return false;
bool CCECCommandHandler::HandleGiveDeckStatus(const cec_command &command)
{
- if (m_processor->IsRunning() && m_processor->IsHandledByLibCEC(command.destination))
+ if (m_processor->CECInitialised() && m_processor->IsHandledByLibCEC(command.destination))
{
- CCECBusDevice *device = GetDevice(command.destination);
- if (device && (device->GetType() == CEC_DEVICE_TYPE_PLAYBACK_DEVICE || device->GetType() == CEC_DEVICE_TYPE_RECORDING_DEVICE))
- return ((CCECPlaybackDevice *) device)->TransmitDeckStatus(command.initiator);
+ CCECPlaybackDevice *device = CCECBusDevice::AsPlaybackDevice(GetDevice(command.destination));
+ if (device)
+ return device->TransmitDeckStatus(command.initiator);
}
return false;
bool CCECCommandHandler::HandleGiveDevicePowerStatus(const cec_command &command)
{
- if (m_processor->IsRunning() && m_processor->IsHandledByLibCEC(command.destination))
+ if (m_processor->CECInitialised() && m_processor->IsHandledByLibCEC(command.destination))
{
CCECBusDevice *device = GetDevice(command.destination);
if (device)
bool CCECCommandHandler::HandleGiveDeviceVendorId(const cec_command &command)
{
- if (m_processor->IsRunning() && m_processor->IsHandledByLibCEC(command.destination))
+ if (m_processor->CECInitialised() && m_processor->IsHandledByLibCEC(command.destination))
{
CCECBusDevice *device = GetDevice(command.destination);
if (device)
bool CCECCommandHandler::HandleGiveOSDName(const cec_command &command)
{
- if (m_processor->IsRunning() && m_processor->IsHandledByLibCEC(command.destination))
+ if (m_processor->CECInitialised() && m_processor->IsHandledByLibCEC(command.destination))
{
CCECBusDevice *device = GetDevice(command.destination);
if (device)
bool CCECCommandHandler::HandleGivePhysicalAddress(const cec_command &command)
{
- if (m_processor->IsRunning() && m_processor->IsHandledByLibCEC(command.destination))
+ if (m_processor->CECInitialised() && m_processor->IsHandledByLibCEC(command.destination))
{
CCECBusDevice *device = GetDevice(command.destination);
if (device)
bool CCECCommandHandler::HandleGiveMenuLanguage(const cec_command &command)
{
- if (m_processor->IsRunning() && m_processor->IsHandledByLibCEC(command.destination))
+ if (m_processor->CECInitialised() && m_processor->IsHandledByLibCEC(command.destination))
{
CCECBusDevice *device = GetDevice(command.destination);
if (device)
bool CCECCommandHandler::HandleGiveSystemAudioModeStatus(const cec_command &command)
{
- if (m_processor->IsRunning() && m_processor->IsHandledByLibCEC(command.destination))
+ if (m_processor->CECInitialised() && m_processor->IsHandledByLibCEC(command.destination))
{
- CCECBusDevice *device = GetDevice(command.destination);
- if (device && device->GetType() == CEC_DEVICE_TYPE_AUDIO_SYSTEM)
- return ((CCECAudioSystem *) device)->TransmitSystemAudioModeStatus(command.initiator);
+ CCECAudioSystem *device = CCECBusDevice::AsAudioSystem(GetDevice(command.destination));
+ if (device)
+ return device->TransmitSystemAudioModeStatus(command.initiator);
}
return false;
bool CCECCommandHandler::HandleMenuRequest(const cec_command &command)
{
- if (m_processor->IsRunning() && m_processor->IsHandledByLibCEC(command.destination))
+ if (m_processor->CECInitialised() && m_processor->IsHandledByLibCEC(command.destination))
{
CCECBusDevice *device = GetDevice(command.destination);
if (device)
{
if (command.parameters.size == 1)
{
- CCECBusDevice *device = GetDevice(command.initiator);
- if (device && device->GetType() == CEC_DEVICE_TYPE_AUDIO_SYSTEM)
+ CCECAudioSystem *device = CCECBusDevice::AsAudioSystem(GetDevice(command.initiator));
+ if (device)
{
- ((CCECAudioSystem *)device)->SetAudioStatus(command.parameters[0]);
+ device->SetAudioStatus(command.parameters[0]);
return true;
}
}
bool CCECCommandHandler::HandleRequestActiveSource(const cec_command &command)
{
- if (m_processor->IsRunning())
+ if (m_processor->CECInitialised())
{
LIB_CEC->AddLog(CEC_LOG_DEBUG, ">> %i requests active source", (uint8_t) command.initiator);
m_processor->GetDevice(command.initiator)->SetPowerStatus(CEC_POWER_STATUS_ON);
bool CCECCommandHandler::HandleSetStreamPath(const cec_command &command)
{
- if (m_processor->IsRunning() && command.parameters.size >= 2)
+ if (m_processor->CECInitialised() && command.parameters.size >= 2)
{
uint16_t iStreamAddress = ((uint16_t)command.parameters[0] << 8) | ((uint16_t)command.parameters[1]);
LIB_CEC->AddLog(CEC_LOG_DEBUG, ">> %i sets stream path to physical address %04x", command.initiator, iStreamAddress);
bool CCECCommandHandler::HandleSystemAudioModeRequest(const cec_command &command)
{
- if (m_processor->IsRunning() && m_processor->IsHandledByLibCEC(command.destination))
+ if (m_processor->CECInitialised() && m_processor->IsHandledByLibCEC(command.destination))
{
- CCECBusDevice *device = GetDevice(command.destination);
- if (device && device->GetType() == CEC_DEVICE_TYPE_AUDIO_SYSTEM)
+ CCECAudioSystem *device = CCECBusDevice::AsAudioSystem(GetDevice(command.destination));
+ if (device)
{
if (command.parameters.size >= 2)
{
device->SetPowerStatus(CEC_POWER_STATUS_ON);
- ((CCECAudioSystem *) device)->SetSystemAudioModeStatus(CEC_SYSTEM_AUDIO_STATUS_ON);
+ device->SetSystemAudioModeStatus(CEC_SYSTEM_AUDIO_STATUS_ON);
uint16_t iNewAddress = ((uint16_t)command.parameters[0] << 8) | ((uint16_t)command.parameters[1]);
CCECBusDevice *newActiveDevice = GetDeviceByPhysicalAddress(iNewAddress);
if (newActiveDevice)
newActiveDevice->MarkAsActiveSource();
- return ((CCECAudioSystem *) device)->TransmitSetSystemAudioMode(command.initiator);
+ return device->TransmitSetSystemAudioMode(command.initiator);
}
else
{
- ((CCECAudioSystem *) device)->SetSystemAudioModeStatus(CEC_SYSTEM_AUDIO_STATUS_OFF);
- return ((CCECAudioSystem *) device)->TransmitSetSystemAudioMode(command.initiator);
+ device->SetSystemAudioModeStatus(CEC_SYSTEM_AUDIO_STATUS_OFF);
+ return device->TransmitSetSystemAudioMode(command.initiator);
}
}
}
{
if (command.parameters.size == 1)
{
- CCECBusDevice *device = GetDevice(command.initiator);
- if (device && device->GetType() == CEC_DEVICE_TYPE_AUDIO_SYSTEM)
+ CCECAudioSystem *device = CCECBusDevice::AsAudioSystem(GetDevice(command.initiator));
+ if (device)
{
- ((CCECAudioSystem *)device)->SetSystemAudioModeStatus((cec_system_audio_status)command.parameters[0]);
+ device->SetSystemAudioModeStatus((cec_system_audio_status)command.parameters[0]);
return true;
}
}
{
if (command.parameters.size == 1)
{
- CCECBusDevice *device = GetDevice(command.initiator);
- if (device && device->GetType() == CEC_DEVICE_TYPE_AUDIO_SYSTEM)
+ CCECAudioSystem *device = CCECBusDevice::AsAudioSystem(GetDevice(command.initiator));
+ if (device)
{
- ((CCECAudioSystem *)device)->SetSystemAudioModeStatus((cec_system_audio_status)command.parameters[0]);
+ device->SetSystemAudioModeStatus((cec_system_audio_status)command.parameters[0]);
return true;
}
}
bool CCECCommandHandler::HandleUserControlPressed(const cec_command &command)
{
- if (m_processor->IsRunning() &&
+ if (m_processor->CECInitialised() &&
m_processor->IsHandledByLibCEC(command.destination) &&
command.parameters.size > 0)
{
{
LIB_CEC->AddLog(CEC_LOG_DEBUG, "unhandled command with opcode %02x from address %d", command.opcode, command.initiator);
- if (m_processor->IsRunning() && m_processor->IsHandledByLibCEC(command.destination))
+ if (m_processor->CECInitialised() && m_processor->IsHandledByLibCEC(command.destination))
m_processor->TransmitAbort(m_busDevice->GetLogicalAddress(), command.initiator, command.opcode, CEC_ABORT_REASON_UNRECOGNIZED_OPCODE);
}
{
if (!m_processor->IsHandledByLibCEC(iAddress))
{
- CCECBusDevice *device = m_processor->GetDeviceByPhysicalAddress(iNewAddress);
- bool bOurAddress(device && device->GetLogicalAddress() != m_busDevice->GetLogicalAddress() &&
- device->IsHandledByLibCEC());
-
- GetDevice(iAddress)->SetPhysicalAddress(iNewAddress);
+ CCECBusDevice *otherDevice = m_processor->GetDeviceByPhysicalAddress(iNewAddress);
+ CCECClient *client = otherDevice ? otherDevice->GetClient() : NULL;
- if (bOurAddress)
+ CCECBusDevice *device = m_processor->GetDevice(iAddress);
+ if (device)
+ device->SetPhysicalAddress(iNewAddress);
+ else
{
- /* another device reported the same physical address as ours
- * since we don't have physical address detection yet, we'll just use the
- * given address, increased by 0x100 for now */
- CCECClient *client = device->GetClient();
- if (client)
- client->SetPhysicalAddress(iNewAddress + 0x100);
+ LIB_CEC->AddLog(CEC_LOG_DEBUG, "device with logical address %X not found", iAddress);
}
+
+ /* another device reported the same physical address as ours */
+ if (client)
+ client->ResetPhysicalAddress();
+ }
+ else
+ {
+ LIB_CEC->AddLog(CEC_LOG_DEBUG, "ignore physical address report for device %s (%X) because it's marked as handled by libCEC", ToString(iAddress), iAddress);
}
}
m_busDevice->TransmitImageViewOn();
m_busDevice->TransmitActiveSource();
m_busDevice->TransmitMenuState(CECDEVICE_TV);
- if ((m_busDevice->GetType() == CEC_DEVICE_TYPE_PLAYBACK_DEVICE ||
- m_busDevice->GetType() == CEC_DEVICE_TYPE_RECORDING_DEVICE) &&
- SendDeckStatusUpdateOnActiveSource())
- ((CCECPlaybackDevice *)m_busDevice)->TransmitDeckStatus(CECDEVICE_TV);
+
+ CCECPlaybackDevice *playbackDevice = m_busDevice->AsPlaybackDevice();
+ if (playbackDevice && SendDeckStatusUpdateOnActiveSource())
+ playbackDevice->TransmitDeckStatus(CECDEVICE_TV);
m_bHandlerInited = true;
}
return true;