2 * This file is part of the libCEC(R) library.
4 * libCEC(R) is Copyright (C) 2011-2012 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 "adapter/AdapterMessage.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 "platform/util/timeutils.h"
48 using namespace PLATFORM
;
50 CCECProcessor::CCECProcessor(CLibCEC
*controller
, const char *strDeviceName
, cec_logical_address iLogicalAddress
/* = CECDEVICE_PLAYBACKDEVICE1 */, uint16_t iPhysicalAddress
/* = CEC_DEFAULT_PHYSICAL_ADDRESS*/) :
52 m_bInitialised(false),
53 m_iHDMIPort(CEC_DEFAULT_HDMI_PORT
),
54 m_iBaseDevice((cec_logical_address
)CEC_DEFAULT_BASE_DEVICE
),
55 m_lastInitiator(CECDEVICE_UNKNOWN
),
56 m_strDeviceName(strDeviceName
),
57 m_communication(NULL
),
58 m_controller(controller
),
60 m_iStandardLineTimeout(3),
61 m_iRetryLineTimeout(3),
62 m_iLastTransmission(0)
64 m_logicalAddresses
.Clear();
65 m_logicalAddresses
.Set(iLogicalAddress
);
67 for (int iPtr
= 0; iPtr
<= 16; iPtr
++)
68 m_busDevices
[iPtr
] = new CCECBusDevice(this, (cec_logical_address
) iPtr
, iPtr
== iLogicalAddress
? iPhysicalAddress
: 0);
71 CCECProcessor::CCECProcessor(CLibCEC
*controller
, const char *strDeviceName
, const cec_device_type_list
&types
) :
73 m_bInitialised(false),
74 m_iHDMIPort(CEC_DEFAULT_HDMI_PORT
),
75 m_iBaseDevice((cec_logical_address
)CEC_DEFAULT_BASE_DEVICE
),
76 m_strDeviceName(strDeviceName
),
78 m_communication(NULL
),
79 m_controller(controller
),
81 m_iStandardLineTimeout(3),
82 m_iRetryLineTimeout(3),
83 m_iLastTransmission(0)
85 m_logicalAddresses
.Clear();
86 for (int iPtr
= 0; iPtr
< 16; iPtr
++)
90 case CECDEVICE_AUDIOSYSTEM
:
91 m_busDevices
[iPtr
] = new CCECAudioSystem(this, (cec_logical_address
) iPtr
, 0xFFFF);
93 case CECDEVICE_PLAYBACKDEVICE1
:
94 case CECDEVICE_PLAYBACKDEVICE2
:
95 case CECDEVICE_PLAYBACKDEVICE3
:
96 m_busDevices
[iPtr
] = new CCECPlaybackDevice(this, (cec_logical_address
) iPtr
, 0xFFFF);
98 case CECDEVICE_RECORDINGDEVICE1
:
99 case CECDEVICE_RECORDINGDEVICE2
:
100 case CECDEVICE_RECORDINGDEVICE3
:
101 m_busDevices
[iPtr
] = new CCECRecordingDevice(this, (cec_logical_address
) iPtr
, 0xFFFF);
103 case CECDEVICE_TUNER1
:
104 case CECDEVICE_TUNER2
:
105 case CECDEVICE_TUNER3
:
106 case CECDEVICE_TUNER4
:
107 m_busDevices
[iPtr
] = new CCECTuner(this, (cec_logical_address
) iPtr
, 0xFFFF);
110 m_busDevices
[iPtr
] = new CCECTV(this, (cec_logical_address
) iPtr
, 0);
113 m_busDevices
[iPtr
] = new CCECBusDevice(this, (cec_logical_address
) iPtr
, 0xFFFF);
119 CCECProcessor::~CCECProcessor(void)
122 m_startCondition
.Broadcast();
125 delete m_communication
;
126 m_communication
= NULL
;
128 for (unsigned int iPtr
= 0; iPtr
< 16; iPtr
++)
129 delete m_busDevices
[iPtr
];
132 bool CCECProcessor::OpenConnection(const char *strPort
, uint16_t iBaudRate
, uint32_t iTimeoutMs
)
135 CLockObject
lock(m_mutex
);
138 CLibCEC::AddLog(CEC_LOG_ERROR
, "existing connection handler found");
142 m_communication
= new CAdapterCommunication(this, strPort
, iBaudRate
);
144 /* check for an already opened connection */
145 if (m_communication
->IsOpen())
147 CLibCEC::AddLog(CEC_LOG_ERROR
, "connection already opened");
151 uint64_t iNow
= GetTimeMs();
152 uint64_t iTarget
= iTimeoutMs
> 0 ? iNow
+ iTimeoutMs
: iNow
+ CEC_DEFAULT_TRANSMIT_WAIT
;
153 unsigned iConnectTry(0), iPingTry(0), iFwVersionTry(0);
154 bool bConnected(false), bPinged(false);
156 /* open a new connection */
157 while (iNow
< iTarget
&& (bConnected
= m_communication
->Open(iTimeoutMs
)) == false)
159 CLibCEC::AddLog(CEC_LOG_ERROR
, "could not open a connection (try %d)", ++iConnectTry
);
164 /* try to ping the adapter */
165 while (bConnected
&& iNow
< iTarget
&& (bPinged
= m_communication
->PingAdapter()) == false)
167 CLibCEC::AddLog(CEC_LOG_ERROR
, "the adapter did not respond correctly to a ping (try %d)", ++iPingTry
);
172 /* try to read the firmware version */
173 uint16_t iFirmwareVersion(CEC_FW_VERSION_UNKNOWN
);
174 while (bPinged
&& iNow
< iTarget
&& (iFirmwareVersion
= m_communication
->GetFirmwareVersion()) == CEC_FW_VERSION_UNKNOWN
)
176 CLibCEC::AddLog(CEC_LOG_ERROR
, "the adapter did not respond with a correct firmware version (try %d)", ++iFwVersionTry
);
181 if ((bReturn
= iFirmwareVersion
!= CEC_FW_VERSION_UNKNOWN
) == true)
182 CLibCEC::AddLog(CEC_LOG_NOTICE
, "connected to the CEC adapter. firmware version = %d", iFirmwareVersion
);
187 void CCECProcessor::SetInitialised(bool bSetTo
/* = true */)
189 CLockObject
lock(m_mutex
);
190 m_bInitialised
= bSetTo
;
193 bool CCECProcessor::Initialise(void)
197 CLockObject
lock(m_mutex
);
198 if (!m_logicalAddresses
.IsEmpty())
199 m_logicalAddresses
.Clear();
201 if (!FindLogicalAddresses())
203 CLibCEC::AddLog(CEC_LOG_ERROR
, "could not detect our logical addresses");
207 /* only set our OSD name for the primary device */
208 m_busDevices
[m_logicalAddresses
.primary
]->m_strDeviceName
= m_strDeviceName
;
211 /* get the vendor id from the TV, so we are using the correct handler */
212 m_busDevices
[CECDEVICE_TV
]->RequestVendorId();
215 if ((bReturn
= SetHDMIPort(m_iBaseDevice
, m_iHDMIPort
, true)) == false)
216 CLibCEC::AddLog(CEC_LOG_ERROR
, "unable to set HDMI port %d on %s (%x)", m_iHDMIPort
, ToString(m_iBaseDevice
), (uint8_t)m_iBaseDevice
);
218 SetInitialised(bReturn
);
223 bool CCECProcessor::Start(const char *strPort
, uint16_t iBaudRate
/* = 38400 */, uint32_t iTimeoutMs
/* = 10000 */)
228 CLockObject
lock(m_mutex
);
229 if (!OpenConnection(strPort
, iBaudRate
, iTimeoutMs
))
232 /* create the processor thread */
233 if (!CreateThread() || !m_startCondition
.Wait(m_mutex
) || !m_bStarted
)
235 CLibCEC::AddLog(CEC_LOG_ERROR
, "could not create a processor thread");
240 if ((bReturn
= Initialise()) == false)
242 CLibCEC::AddLog(CEC_LOG_ERROR
, "could not create a processor thread");
247 CLibCEC::AddLog(CEC_LOG_DEBUG
, "processor thread started");
253 bool CCECProcessor::TryLogicalAddress(cec_logical_address address
)
255 if (m_busDevices
[address
]->TryLogicalAddress())
257 m_logicalAddresses
.Set(address
);
264 bool CCECProcessor::FindLogicalAddressRecordingDevice(void)
266 CLibCEC::AddLog(CEC_LOG_DEBUG
, "detecting logical address for type 'recording device'");
267 return TryLogicalAddress(CECDEVICE_RECORDINGDEVICE1
) ||
268 TryLogicalAddress(CECDEVICE_RECORDINGDEVICE2
) ||
269 TryLogicalAddress(CECDEVICE_RECORDINGDEVICE3
);
272 bool CCECProcessor::FindLogicalAddressTuner(void)
274 CLibCEC::AddLog(CEC_LOG_DEBUG
, "detecting logical address for type 'tuner'");
275 return TryLogicalAddress(CECDEVICE_TUNER1
) ||
276 TryLogicalAddress(CECDEVICE_TUNER2
) ||
277 TryLogicalAddress(CECDEVICE_TUNER3
) ||
278 TryLogicalAddress(CECDEVICE_TUNER4
);
281 bool CCECProcessor::FindLogicalAddressPlaybackDevice(void)
283 CLibCEC::AddLog(CEC_LOG_DEBUG
, "detecting logical address for type 'playback device'");
284 return TryLogicalAddress(CECDEVICE_PLAYBACKDEVICE1
) ||
285 TryLogicalAddress(CECDEVICE_PLAYBACKDEVICE2
) ||
286 TryLogicalAddress(CECDEVICE_PLAYBACKDEVICE3
);
289 bool CCECProcessor::FindLogicalAddressAudioSystem(void)
291 CLibCEC::AddLog(CEC_LOG_DEBUG
, "detecting logical address for type 'audio'");
292 return TryLogicalAddress(CECDEVICE_AUDIOSYSTEM
);
295 bool CCECProcessor::ChangeDeviceType(cec_device_type from
, cec_device_type to
)
297 bool bChanged(false);
299 CLibCEC::AddLog(CEC_LOG_NOTICE
, "changing device type '%s' into '%s'", ToString(from
), ToString(to
));
301 CLockObject
lock(m_mutex
);
302 CCECBusDevice
*previousDevice
= GetDeviceByType(from
);
303 m_logicalAddresses
.primary
= CECDEVICE_UNKNOWN
;
305 for (unsigned int iPtr
= 0; iPtr
< 5; iPtr
++)
307 if (m_types
.types
[iPtr
] == CEC_DEVICE_TYPE_RESERVED
)
310 if (m_types
.types
[iPtr
] == from
)
313 m_types
.types
[iPtr
] = to
;
315 else if (m_types
.types
[iPtr
] == to
&& bChanged
)
317 m_types
.types
[iPtr
] = CEC_DEVICE_TYPE_RESERVED
;
323 FindLogicalAddresses();
325 CCECBusDevice
*newDevice
= GetDeviceByType(to
);
326 if (previousDevice
&& newDevice
)
328 newDevice
->SetDeviceStatus(CEC_DEVICE_STATUS_HANDLED_BY_LIBCEC
);
329 previousDevice
->SetDeviceStatus(CEC_DEVICE_STATUS_NOT_PRESENT
);
331 newDevice
->SetCecVersion(previousDevice
->GetCecVersion(false));
332 previousDevice
->SetCecVersion(CEC_VERSION_UNKNOWN
);
334 newDevice
->SetMenuLanguage(previousDevice
->GetMenuLanguage(false));
335 cec_menu_language lang
;
336 lang
.device
= previousDevice
->GetLogicalAddress();
337 for (unsigned int iPtr
= 0; iPtr
< 4; iPtr
++)
338 lang
.language
[iPtr
] = '?';
339 lang
.language
[3] = 0;
340 previousDevice
->SetMenuLanguage(lang
);
342 newDevice
->SetMenuState(previousDevice
->GetMenuState());
343 previousDevice
->SetMenuState(CEC_MENU_STATE_DEACTIVATED
);
345 newDevice
->SetOSDName(previousDevice
->GetOSDName(false));
346 previousDevice
->SetOSDName(ToString(previousDevice
->GetLogicalAddress()));
348 newDevice
->SetPhysicalAddress(previousDevice
->GetPhysicalAddress(false));
349 previousDevice
->SetPhysicalAddress(0xFFFF);
351 newDevice
->SetPowerStatus(previousDevice
->GetPowerStatus(false));
352 previousDevice
->SetPowerStatus(CEC_POWER_STATUS_UNKNOWN
);
354 newDevice
->SetVendorId(previousDevice
->GetVendorId(false));
355 previousDevice
->SetVendorId(CEC_VENDOR_UNKNOWN
);
357 if ((from
== CEC_DEVICE_TYPE_PLAYBACK_DEVICE
|| from
== CEC_DEVICE_TYPE_RECORDING_DEVICE
) &&
358 (to
== CEC_DEVICE_TYPE_PLAYBACK_DEVICE
|| to
== CEC_DEVICE_TYPE_RECORDING_DEVICE
))
360 ((CCECPlaybackDevice
*) newDevice
)->SetDeckControlMode(((CCECPlaybackDevice
*) previousDevice
)->GetDeckControlMode());
361 ((CCECPlaybackDevice
*) previousDevice
)->SetDeckControlMode(CEC_DECK_CONTROL_MODE_STOP
);
363 ((CCECPlaybackDevice
*) newDevice
)->SetDeckStatus(((CCECPlaybackDevice
*) previousDevice
)->GetDeckStatus());
364 ((CCECPlaybackDevice
*) previousDevice
)->SetDeckStatus(CEC_DECK_INFO_STOP
);
372 bool CCECProcessor::FindLogicalAddresses(void)
375 m_logicalAddresses
.Clear();
377 for (unsigned int iPtr
= 0; iPtr
< 5; iPtr
++)
379 if (m_types
.types
[iPtr
] == CEC_DEVICE_TYPE_RESERVED
)
382 CLibCEC::AddLog(CEC_LOG_DEBUG
, "%s - device %d: type %d", __FUNCTION__
, iPtr
, m_types
.types
[iPtr
]);
384 if (m_types
.types
[iPtr
] == CEC_DEVICE_TYPE_RECORDING_DEVICE
)
385 bReturn
&= FindLogicalAddressRecordingDevice();
386 if (m_types
.types
[iPtr
] == CEC_DEVICE_TYPE_TUNER
)
387 bReturn
&= FindLogicalAddressTuner();
388 if (m_types
.types
[iPtr
] == CEC_DEVICE_TYPE_PLAYBACK_DEVICE
)
389 bReturn
&= FindLogicalAddressPlaybackDevice();
390 if (m_types
.types
[iPtr
] == CEC_DEVICE_TYPE_AUDIO_SYSTEM
)
391 bReturn
&= FindLogicalAddressAudioSystem();
395 SetAckMask(m_logicalAddresses
.AckMask());
400 void CCECProcessor::ReplaceHandlers(void)
402 for (uint8_t iPtr
= 0; iPtr
<= CECDEVICE_PLAYBACKDEVICE3
; iPtr
++)
403 m_busDevices
[iPtr
]->ReplaceHandler(m_bInitialised
);
406 void *CCECProcessor::Process(void)
408 bool bParseFrame(false);
410 CCECAdapterMessage msg
;
413 CLockObject
lock(m_mutex
);
415 CLibCEC::AddLog(CEC_LOG_DEBUG
, "processor thread started");
416 m_startCondition
.Signal();
426 CLockObject
lock(m_mutex
);
427 if (m_commandBuffer
.Pop(command
))
429 else if (m_communication
->IsOpen() && m_communication
->Read(msg
, 50))
431 if ((bParseFrame
= (ParseMessage(msg
) && !IsStopped())) == true)
432 command
= m_currentframe
;
437 ParseCommand(command
);
442 m_controller
->CheckKeypressTimeout();
446 m_communication
->Close();
451 bool CCECProcessor::SetActiveSource(cec_device_type type
/* = CEC_DEVICE_TYPE_RESERVED */)
458 cec_logical_address addr
= m_logicalAddresses
.primary
;
460 if (type
!= CEC_DEVICE_TYPE_RESERVED
)
462 for (uint8_t iPtr
= 0; iPtr
<= 11; iPtr
++)
464 if (m_logicalAddresses
[iPtr
] && m_busDevices
[iPtr
]->m_type
== type
)
466 addr
= (cec_logical_address
) iPtr
;
472 m_busDevices
[addr
]->SetActiveSource();
473 if (m_busDevices
[addr
]->GetPhysicalAddress(false) != 0xFFFF)
475 bReturn
= m_busDevices
[addr
]->TransmitActiveSource();
479 m_busDevices
[addr
]->SetMenuState(CEC_MENU_STATE_ACTIVATED
);
480 m_busDevices
[addr
]->TransmitMenuState(CECDEVICE_TV
);
483 if (bReturn
&& (m_busDevices
[addr
]->GetType() == CEC_DEVICE_TYPE_PLAYBACK_DEVICE
||
484 m_busDevices
[addr
]->GetType() == CEC_DEVICE_TYPE_RECORDING_DEVICE
) &&
485 m_busDevices
[addr
]->GetHandler()->SendDeckStatusUpdateOnActiveSource())
487 bReturn
= ((CCECPlaybackDevice
*)m_busDevices
[addr
])->TransmitDeckStatus(CECDEVICE_TV
);
494 bool CCECProcessor::SetActiveSource(uint16_t iStreamPath
)
498 CCECBusDevice
*device
= GetDeviceByPhysicalAddress(iStreamPath
);
501 device
->SetActiveSource();
508 void CCECProcessor::SetStandardLineTimeout(uint8_t iTimeout
)
510 CLockObject
lock(m_mutex
);
511 m_iStandardLineTimeout
= iTimeout
;
514 void CCECProcessor::SetRetryLineTimeout(uint8_t iTimeout
)
516 CLockObject
lock(m_mutex
);
517 m_iRetryLineTimeout
= iTimeout
;
520 bool CCECProcessor::SetActiveView(void)
522 return SetActiveSource(m_types
.IsEmpty() ? CEC_DEVICE_TYPE_RESERVED
: m_types
[0]);
525 bool CCECProcessor::SetDeckControlMode(cec_deck_control_mode mode
, bool bSendUpdate
/* = true */)
529 CCECBusDevice
*device
= GetDeviceByType(CEC_DEVICE_TYPE_PLAYBACK_DEVICE
);
532 ((CCECPlaybackDevice
*) device
)->SetDeckControlMode(mode
);
534 ((CCECPlaybackDevice
*) device
)->TransmitDeckStatus(CECDEVICE_TV
);
541 bool CCECProcessor::SetDeckInfo(cec_deck_info info
, bool bSendUpdate
/* = true */)
545 CCECBusDevice
*device
= GetDeviceByType(CEC_DEVICE_TYPE_PLAYBACK_DEVICE
);
548 ((CCECPlaybackDevice
*) device
)->SetDeckStatus(info
);
550 ((CCECPlaybackDevice
*) device
)->TransmitDeckStatus(CECDEVICE_TV
);
557 bool CCECProcessor::SetHDMIPort(cec_logical_address iBaseDevice
, uint8_t iPort
, bool bForce
/* = false */)
560 CLockObject
lock(m_mutex
);
562 m_iBaseDevice
= iBaseDevice
;
564 if (!m_bStarted
&& !bForce
)
567 CLibCEC::AddLog(CEC_LOG_DEBUG
, "setting HDMI port to %d on device %s (%d)", iPort
, ToString(iBaseDevice
), (int)iBaseDevice
);
569 uint16_t iPhysicalAddress(0);
570 if (iBaseDevice
> CECDEVICE_TV
)
573 iPhysicalAddress
= m_busDevices
[iBaseDevice
]->GetPhysicalAddress();
577 if (iPhysicalAddress
< 0xffff)
579 if (iPhysicalAddress
== 0)
580 iPhysicalAddress
+= 0x1000 * iPort
;
581 else if (iPhysicalAddress
% 0x1000 == 0)
582 iPhysicalAddress
+= 0x100 * iPort
;
583 else if (iPhysicalAddress
% 0x100 == 0)
584 iPhysicalAddress
+= 0x10 * iPort
;
585 else if (iPhysicalAddress
% 0x10 == 0)
586 iPhysicalAddress
+= iPort
;
592 CLibCEC::AddLog(CEC_LOG_ERROR
, "failed to set the physical address");
596 SetPhysicalAddress(iPhysicalAddress
);
602 bool CCECProcessor::PhysicalAddressInUse(uint16_t iPhysicalAddress
)
604 for (unsigned int iPtr
= 0; iPtr
< 15; iPtr
++)
606 if (m_busDevices
[iPtr
]->GetPhysicalAddress(false) == iPhysicalAddress
)
612 bool CCECProcessor::TransmitInactiveSource(void)
617 if (!m_logicalAddresses
.IsEmpty() && m_busDevices
[m_logicalAddresses
.primary
])
618 return m_busDevices
[m_logicalAddresses
.primary
]->TransmitInactiveSource();
622 void CCECProcessor::LogOutput(const cec_command
&data
)
625 strTx
.Format("<< %02x", ((uint8_t)data
.initiator
<< 4) + (uint8_t)data
.destination
);
627 strTx
.AppendFormat(":%02x", (uint8_t)data
.opcode
);
629 for (uint8_t iPtr
= 0; iPtr
< data
.parameters
.size
; iPtr
++)
630 strTx
.AppendFormat(":%02x", data
.parameters
[iPtr
]);
631 CLibCEC::AddLog(CEC_LOG_TRAFFIC
, strTx
.c_str());
634 bool CCECProcessor::SetLogicalAddress(cec_logical_address iLogicalAddress
)
636 CLockObject
lock(m_mutex
);
637 if (m_logicalAddresses
.primary
!= iLogicalAddress
)
639 CLibCEC::AddLog(CEC_LOG_NOTICE
, "<< setting primary logical address to %1x", iLogicalAddress
);
640 m_logicalAddresses
.primary
= iLogicalAddress
;
641 m_logicalAddresses
.Set(iLogicalAddress
);
642 return SetAckMask(m_logicalAddresses
.AckMask());
648 bool CCECProcessor::SetMenuState(cec_menu_state state
, bool bSendUpdate
/* = true */)
650 for (uint8_t iPtr
= 0; iPtr
< 16; iPtr
++)
652 if (m_logicalAddresses
[iPtr
])
653 m_busDevices
[iPtr
]->SetMenuState(state
);
657 m_busDevices
[m_logicalAddresses
.primary
]->TransmitMenuState(CECDEVICE_TV
);
662 bool CCECProcessor::SetPhysicalAddress(uint16_t iPhysicalAddress
, bool bSendUpdate
/* = true */)
664 bool bSendActiveView(false);
666 cec_logical_addresses sendUpdatesTo
;
669 CLockObject
lock(m_mutex
);
670 if (!m_logicalAddresses
.IsEmpty())
672 bool bWasActiveSource(false);
673 for (uint8_t iPtr
= 0; iPtr
< 15; iPtr
++)
674 if (m_logicalAddresses
[iPtr
])
676 bWasActiveSource
|= m_busDevices
[iPtr
]->IsActiveSource();
677 m_busDevices
[iPtr
]->SetInactiveSource();
678 m_busDevices
[iPtr
]->SetPhysicalAddress(iPhysicalAddress
);
680 sendUpdatesTo
.Set((cec_logical_address
)iPtr
);
683 bSendActiveView
= bWasActiveSource
&& bSendUpdate
;
688 for (uint8_t iPtr
= 0; iPtr
< 15; iPtr
++)
689 if (sendUpdatesTo
[iPtr
])
690 m_busDevices
[iPtr
]->TransmitPhysicalAddress();
698 bool CCECProcessor::SwitchMonitoring(bool bEnable
)
700 CLibCEC::AddLog(CEC_LOG_NOTICE
, "== %s monitoring mode ==", bEnable
? "enabling" : "disabling");
703 CLockObject
lock(m_mutex
);
704 m_bMonitor
= bEnable
;
708 return SetAckMask(0);
710 return SetAckMask(m_logicalAddresses
.AckMask());
713 bool CCECProcessor::PollDevice(cec_logical_address iAddress
)
715 if (iAddress
!= CECDEVICE_UNKNOWN
&& m_busDevices
[iAddress
])
717 return m_logicalAddresses
.primary
== CECDEVICE_UNKNOWN
?
718 m_busDevices
[iAddress
]->TransmitPoll(iAddress
) :
719 m_busDevices
[m_logicalAddresses
.primary
]->TransmitPoll(iAddress
);
724 uint8_t CCECProcessor::VolumeUp(bool bSendRelease
/* = true */)
727 if (IsPresentDevice(CECDEVICE_AUDIOSYSTEM
))
728 status
= ((CCECAudioSystem
*)m_busDevices
[CECDEVICE_AUDIOSYSTEM
])->VolumeUp(bSendRelease
);
733 uint8_t CCECProcessor::VolumeDown(bool bSendRelease
/* = true */)
736 if (IsPresentDevice(CECDEVICE_AUDIOSYSTEM
))
737 status
= ((CCECAudioSystem
*)m_busDevices
[CECDEVICE_AUDIOSYSTEM
])->VolumeDown(bSendRelease
);
742 uint8_t CCECProcessor::MuteAudio(bool bSendRelease
/* = true */)
745 if (IsPresentDevice(CECDEVICE_AUDIOSYSTEM
))
746 status
= ((CCECAudioSystem
*)m_busDevices
[CECDEVICE_AUDIOSYSTEM
])->MuteAudio(bSendRelease
);
751 CCECBusDevice
*CCECProcessor::GetDeviceByPhysicalAddress(uint16_t iPhysicalAddress
, bool bRefresh
/* = false */) const
753 if (m_busDevices
[m_logicalAddresses
.primary
]->GetPhysicalAddress(false) == iPhysicalAddress
)
754 return m_busDevices
[m_logicalAddresses
.primary
];
756 CCECBusDevice
*device
= NULL
;
757 for (unsigned int iPtr
= 0; iPtr
< 16; iPtr
++)
759 if (m_busDevices
[iPtr
]->GetPhysicalAddress(bRefresh
) == iPhysicalAddress
)
761 device
= m_busDevices
[iPtr
];
769 CCECBusDevice
*CCECProcessor::GetDeviceByType(cec_device_type type
) const
771 CCECBusDevice
*device
= NULL
;
773 for (uint8_t iPtr
= 0; iPtr
< 16; iPtr
++)
775 if (m_busDevices
[iPtr
]->m_type
== type
&& m_logicalAddresses
[iPtr
])
777 device
= m_busDevices
[iPtr
];
785 CCECBusDevice
*CCECProcessor::GetPrimaryDevice(void) const
787 CCECBusDevice
*device(NULL
);
788 cec_logical_address primary
= m_logicalAddresses
.primary
;
789 if (primary
!= CECDEVICE_UNKNOWN
)
790 device
= m_busDevices
[primary
];
794 cec_version
CCECProcessor::GetDeviceCecVersion(cec_logical_address iAddress
)
796 return m_busDevices
[iAddress
]->GetCecVersion();
799 cec_osd_name
CCECProcessor::GetDeviceOSDName(cec_logical_address iAddress
)
801 CStdString strOSDName
= m_busDevices
[iAddress
]->GetOSDName();
804 snprintf(retVal
.name
, sizeof(retVal
.name
), "%s", strOSDName
.c_str());
805 retVal
.device
= iAddress
;
810 bool CCECProcessor::GetDeviceMenuLanguage(cec_logical_address iAddress
, cec_menu_language
*language
)
812 if (m_busDevices
[iAddress
])
814 *language
= m_busDevices
[iAddress
]->GetMenuLanguage();
815 return (strcmp(language
->language
, "???") != 0);
820 uint64_t CCECProcessor::GetDeviceVendorId(cec_logical_address iAddress
)
822 if (m_busDevices
[iAddress
])
823 return m_busDevices
[iAddress
]->GetVendorId();
827 uint16_t CCECProcessor::GetDevicePhysicalAddress(cec_logical_address iAddress
)
829 if (m_busDevices
[iAddress
])
830 return m_busDevices
[iAddress
]->GetPhysicalAddress(false);
834 cec_power_status
CCECProcessor::GetDevicePowerStatus(cec_logical_address iAddress
)
836 if (m_busDevices
[iAddress
])
837 return m_busDevices
[iAddress
]->GetPowerStatus();
838 return CEC_POWER_STATUS_UNKNOWN
;
841 cec_logical_address
CCECProcessor::GetActiveSource(void)
843 for (uint8_t iPtr
= 0; iPtr
<= 11; iPtr
++)
845 if (m_busDevices
[iPtr
]->IsActiveSource())
846 return (cec_logical_address
)iPtr
;
849 return CECDEVICE_UNKNOWN
;
852 bool CCECProcessor::IsActiveSource(cec_logical_address iAddress
)
854 return m_busDevices
[iAddress
]->IsActiveSource();
857 bool CCECProcessor::Transmit(const cec_command
&data
)
862 CCECAdapterMessage
*output
= new CCECAdapterMessage(data
);
864 /* set the number of retries */
865 if (data
.opcode
== CEC_OPCODE_NONE
)
866 output
->maxTries
= 1;
867 else if (data
.initiator
!= CECDEVICE_BROADCAST
)
868 output
->maxTries
= m_busDevices
[data
.initiator
]->GetHandler()->GetTransmitRetries() + 1;
870 bReturn
= Transmit(output
);
872 /* set to "not present" on failed ack */
873 if (output
->state
== ADAPTER_MESSAGE_STATE_SENT_NOT_ACKED
&&
874 output
->Destination() != CECDEVICE_BROADCAST
)
875 m_busDevices
[output
->Destination()]->SetDeviceStatus(CEC_DEVICE_STATUS_NOT_PRESENT
);
881 bool CCECProcessor::Transmit(CCECAdapterMessage
*output
)
884 CLockObject
lock(m_mutex
);
886 if (!m_communication
)
889 m_iLastTransmission
= GetTimeMs();
890 m_communication
->SetLineTimeout(m_iStandardLineTimeout
);
895 if (output
->tries
> 0)
896 m_communication
->SetLineTimeout(m_iRetryLineTimeout
);
897 bReturn
= m_communication
->Write(output
);
899 Sleep(CEC_DEFAULT_TRANSMIT_RETRY_WAIT
);
900 }while (!bReturn
&& output
->transmit_timeout
> 0 && output
->NeedsRetry() && ++output
->tries
< output
->maxTries
);
903 m_communication
->SetLineTimeout(m_iStandardLineTimeout
);
908 void CCECProcessor::TransmitAbort(cec_logical_address address
, cec_opcode opcode
, cec_abort_reason reason
/* = CEC_ABORT_REASON_UNRECOGNIZED_OPCODE */)
910 CLibCEC::AddLog(CEC_LOG_DEBUG
, "<< transmitting abort message");
914 cec_command::Format(command
, m_logicalAddresses
.primary
, address
, CEC_OPCODE_FEATURE_ABORT
);
915 command
.parameters
.PushBack((uint8_t)opcode
);
916 command
.parameters
.PushBack((uint8_t)reason
);
921 bool CCECProcessor::ParseMessage(const CCECAdapterMessage
&msg
)
924 bool bIsError(msg
.IsError());
929 switch(msg
.Message())
931 case MSGCODE_FRAME_START
:
933 m_currentframe
.Clear();
936 m_currentframe
.initiator
= msg
.Initiator();
937 m_currentframe
.destination
= msg
.Destination();
938 m_currentframe
.ack
= msg
.IsACK();
939 m_currentframe
.eom
= msg
.IsEOM();
941 if (m_currentframe
.ack
== 0x1)
943 m_lastInitiator
= m_currentframe
.initiator
;
944 m_busDevices
[m_lastInitiator
]->GetHandler()->HandlePoll(m_currentframe
.initiator
, m_currentframe
.destination
);
948 case MSGCODE_RECEIVE_FAILED
:
950 if (m_lastInitiator
!= CECDEVICE_UNKNOWN
)
951 bIsError
= m_busDevices
[m_lastInitiator
]->GetHandler()->HandleReceiveFailed();
954 case MSGCODE_FRAME_DATA
:
958 m_currentframe
.PushBack(msg
[1]);
959 m_currentframe
.eom
= msg
.IsEOM();
968 CLibCEC::AddLog(bIsError
? CEC_LOG_WARNING
: CEC_LOG_DEBUG
, msg
.ToString());
972 void CCECProcessor::ParseCommand(cec_command
&command
)
975 dataStr
.Format(">> %1x%1x:%02x", command
.initiator
, command
.destination
, command
.opcode
);
976 for (uint8_t iPtr
= 0; iPtr
< command
.parameters
.size
; iPtr
++)
977 dataStr
.AppendFormat(":%02x", (unsigned int)command
.parameters
[iPtr
]);
978 CLibCEC::AddLog(CEC_LOG_TRAFFIC
, dataStr
.c_str());
980 if (!m_bMonitor
&& command
.initiator
>= CECDEVICE_TV
&& command
.initiator
<= CECDEVICE_BROADCAST
)
981 m_busDevices
[(uint8_t)command
.initiator
]->HandleCommand(command
);
984 cec_logical_addresses
CCECProcessor::GetActiveDevices(void)
986 cec_logical_addresses addresses
;
988 for (unsigned int iPtr
= 0; iPtr
< 15; iPtr
++)
990 if (m_busDevices
[iPtr
]->GetStatus() == CEC_DEVICE_STATUS_PRESENT
)
991 addresses
.Set((cec_logical_address
) iPtr
);
996 bool CCECProcessor::IsPresentDevice(cec_logical_address address
)
998 return m_busDevices
[address
]->GetStatus() == CEC_DEVICE_STATUS_PRESENT
;
1001 bool CCECProcessor::IsPresentDeviceType(cec_device_type type
)
1003 for (unsigned int iPtr
= 0; iPtr
< 15; iPtr
++)
1005 if (m_busDevices
[iPtr
]->GetType() == type
&& m_busDevices
[iPtr
]->GetStatus() == CEC_DEVICE_STATUS_PRESENT
)
1012 uint16_t CCECProcessor::GetPhysicalAddress(void) const
1014 if (!m_logicalAddresses
.IsEmpty() && m_busDevices
[m_logicalAddresses
.primary
])
1015 return m_busDevices
[m_logicalAddresses
.primary
]->GetPhysicalAddress(false);
1019 bool CCECProcessor::SetAckMask(uint16_t iMask
)
1021 return m_communication
->SetAckMask(iMask
);
1024 bool CCECProcessor::TransmitKeypress(cec_logical_address iDestination
, cec_user_control_code key
, bool bWait
/* = true */)
1026 return m_busDevices
[iDestination
]->TransmitKeypress(key
, bWait
);
1029 bool CCECProcessor::TransmitKeyRelease(cec_logical_address iDestination
, bool bWait
/* = true */)
1031 return m_busDevices
[iDestination
]->TransmitKeyRelease(bWait
);
1034 const char *CCECProcessor::ToString(const cec_device_type type
)
1038 case CEC_DEVICE_TYPE_AUDIO_SYSTEM
:
1039 return "audio system";
1040 case CEC_DEVICE_TYPE_PLAYBACK_DEVICE
:
1041 return "playback device";
1042 case CEC_DEVICE_TYPE_RECORDING_DEVICE
:
1043 return "recording device";
1044 case CEC_DEVICE_TYPE_RESERVED
:
1046 case CEC_DEVICE_TYPE_TUNER
:
1048 case CEC_DEVICE_TYPE_TV
:
1055 const char *CCECProcessor::ToString(const cec_menu_state state
)
1059 case CEC_MENU_STATE_ACTIVATED
:
1061 case CEC_MENU_STATE_DEACTIVATED
:
1062 return "deactivated";
1068 const char *CCECProcessor::ToString(const cec_version version
)
1072 case CEC_VERSION_1_2
:
1074 case CEC_VERSION_1_2A
:
1076 case CEC_VERSION_1_3
:
1078 case CEC_VERSION_1_3A
:
1080 case CEC_VERSION_1_4
:
1087 const char *CCECProcessor::ToString(const cec_power_status status
)
1091 case CEC_POWER_STATUS_ON
:
1093 case CEC_POWER_STATUS_STANDBY
:
1095 case CEC_POWER_STATUS_IN_TRANSITION_ON_TO_STANDBY
:
1096 return "in transition from on to standby";
1097 case CEC_POWER_STATUS_IN_TRANSITION_STANDBY_TO_ON
:
1098 return "in transition from standby to on";
1104 const char *CCECProcessor::ToString(const cec_logical_address address
)
1108 case CECDEVICE_AUDIOSYSTEM
:
1110 case CECDEVICE_BROADCAST
:
1112 case CECDEVICE_FREEUSE
:
1114 case CECDEVICE_PLAYBACKDEVICE1
:
1115 return "Playback 1";
1116 case CECDEVICE_PLAYBACKDEVICE2
:
1117 return "Playback 2";
1118 case CECDEVICE_PLAYBACKDEVICE3
:
1119 return "Playback 3";
1120 case CECDEVICE_RECORDINGDEVICE1
:
1121 return "Recorder 1";
1122 case CECDEVICE_RECORDINGDEVICE2
:
1123 return "Recorder 2";
1124 case CECDEVICE_RECORDINGDEVICE3
:
1125 return "Recorder 3";
1126 case CECDEVICE_RESERVED1
:
1127 return "Reserved 1";
1128 case CECDEVICE_RESERVED2
:
1129 return "Reserved 2";
1130 case CECDEVICE_TUNER1
:
1132 case CECDEVICE_TUNER2
:
1134 case CECDEVICE_TUNER3
:
1136 case CECDEVICE_TUNER4
:
1145 const char *CCECProcessor::ToString(const cec_deck_control_mode mode
)
1149 case CEC_DECK_CONTROL_MODE_SKIP_FORWARD_WIND
:
1150 return "skip forward wind";
1151 case CEC_DECK_CONTROL_MODE_EJECT
:
1153 case CEC_DECK_CONTROL_MODE_SKIP_REVERSE_REWIND
:
1154 return "reverse rewind";
1155 case CEC_DECK_CONTROL_MODE_STOP
:
1162 const char *CCECProcessor::ToString(const cec_deck_info status
)
1166 case CEC_DECK_INFO_PLAY
:
1168 case CEC_DECK_INFO_RECORD
:
1170 case CEC_DECK_INFO_PLAY_REVERSE
:
1171 return "play reverse";
1172 case CEC_DECK_INFO_STILL
:
1174 case CEC_DECK_INFO_SLOW
:
1176 case CEC_DECK_INFO_SLOW_REVERSE
:
1177 return "slow reverse";
1178 case CEC_DECK_INFO_FAST_FORWARD
:
1179 return "fast forward";
1180 case CEC_DECK_INFO_FAST_REVERSE
:
1181 return "fast reverse";
1182 case CEC_DECK_INFO_NO_MEDIA
:
1184 case CEC_DECK_INFO_STOP
:
1186 case CEC_DECK_INFO_SKIP_FORWARD_WIND
:
1187 return "info skip forward wind";
1188 case CEC_DECK_INFO_SKIP_REVERSE_REWIND
:
1189 return "info skip reverse rewind";
1190 case CEC_DECK_INFO_INDEX_SEARCH_FORWARD
:
1191 return "info index search forward";
1192 case CEC_DECK_INFO_INDEX_SEARCH_REVERSE
:
1193 return "info index search reverse";
1194 case CEC_DECK_INFO_OTHER_STATUS
:
1201 const char *CCECProcessor::ToString(const cec_opcode opcode
)
1205 case CEC_OPCODE_ACTIVE_SOURCE
:
1206 return "active source";
1207 case CEC_OPCODE_IMAGE_VIEW_ON
:
1208 return "image view on";
1209 case CEC_OPCODE_TEXT_VIEW_ON
:
1210 return "text view on";
1211 case CEC_OPCODE_INACTIVE_SOURCE
:
1212 return "inactive source";
1213 case CEC_OPCODE_REQUEST_ACTIVE_SOURCE
:
1214 return "request active source";
1215 case CEC_OPCODE_ROUTING_CHANGE
:
1216 return "routing change";
1217 case CEC_OPCODE_ROUTING_INFORMATION
:
1218 return "routing information";
1219 case CEC_OPCODE_SET_STREAM_PATH
:
1220 return "set stream path";
1221 case CEC_OPCODE_STANDBY
:
1223 case CEC_OPCODE_RECORD_OFF
:
1224 return "record off";
1225 case CEC_OPCODE_RECORD_ON
:
1227 case CEC_OPCODE_RECORD_STATUS
:
1228 return "record status";
1229 case CEC_OPCODE_RECORD_TV_SCREEN
:
1230 return "record tv screen";
1231 case CEC_OPCODE_CLEAR_ANALOGUE_TIMER
:
1232 return "clear analogue timer";
1233 case CEC_OPCODE_CLEAR_DIGITAL_TIMER
:
1234 return "clear digital timer";
1235 case CEC_OPCODE_CLEAR_EXTERNAL_TIMER
:
1236 return "clear external timer";
1237 case CEC_OPCODE_SET_ANALOGUE_TIMER
:
1238 return "set analogue timer";
1239 case CEC_OPCODE_SET_DIGITAL_TIMER
:
1240 return "set digital timer";
1241 case CEC_OPCODE_SET_EXTERNAL_TIMER
:
1242 return "set external timer";
1243 case CEC_OPCODE_SET_TIMER_PROGRAM_TITLE
:
1244 return "set timer program title";
1245 case CEC_OPCODE_TIMER_CLEARED_STATUS
:
1246 return "timer cleared status";
1247 case CEC_OPCODE_TIMER_STATUS
:
1248 return "timer status";
1249 case CEC_OPCODE_CEC_VERSION
:
1250 return "cec version";
1251 case CEC_OPCODE_GET_CEC_VERSION
:
1252 return "get cec version";
1253 case CEC_OPCODE_GIVE_PHYSICAL_ADDRESS
:
1254 return "give physical address";
1255 case CEC_OPCODE_GET_MENU_LANGUAGE
:
1256 return "get menu language";
1257 case CEC_OPCODE_REPORT_PHYSICAL_ADDRESS
:
1258 return "report physical address";
1259 case CEC_OPCODE_SET_MENU_LANGUAGE
:
1260 return "set menu language";
1261 case CEC_OPCODE_DECK_CONTROL
:
1262 return "deck control";
1263 case CEC_OPCODE_DECK_STATUS
:
1264 return "deck status";
1265 case CEC_OPCODE_GIVE_DECK_STATUS
:
1266 return "give deck status";
1267 case CEC_OPCODE_PLAY
:
1269 case CEC_OPCODE_GIVE_TUNER_DEVICE_STATUS
:
1270 return "give tuner status";
1271 case CEC_OPCODE_SELECT_ANALOGUE_SERVICE
:
1272 return "select analogue service";
1273 case CEC_OPCODE_SELECT_DIGITAL_SERVICE
:
1274 return "set digital service";
1275 case CEC_OPCODE_TUNER_DEVICE_STATUS
:
1276 return "tuner device status";
1277 case CEC_OPCODE_TUNER_STEP_DECREMENT
:
1278 return "tuner step decrement";
1279 case CEC_OPCODE_TUNER_STEP_INCREMENT
:
1280 return "tuner step increment";
1281 case CEC_OPCODE_DEVICE_VENDOR_ID
:
1282 return "device vendor id";
1283 case CEC_OPCODE_GIVE_DEVICE_VENDOR_ID
:
1284 return "give device vendor id";
1285 case CEC_OPCODE_VENDOR_COMMAND
:
1286 return "vendor command";
1287 case CEC_OPCODE_VENDOR_COMMAND_WITH_ID
:
1288 return "vendor command with id";
1289 case CEC_OPCODE_VENDOR_REMOTE_BUTTON_DOWN
:
1290 return "vendor remote button down";
1291 case CEC_OPCODE_VENDOR_REMOTE_BUTTON_UP
:
1292 return "vendor remote button up";
1293 case CEC_OPCODE_SET_OSD_STRING
:
1294 return "set osd string";
1295 case CEC_OPCODE_GIVE_OSD_NAME
:
1296 return "give osd name";
1297 case CEC_OPCODE_SET_OSD_NAME
:
1298 return "set osd name";
1299 case CEC_OPCODE_MENU_REQUEST
:
1300 return "menu request";
1301 case CEC_OPCODE_MENU_STATUS
:
1302 return "menu status";
1303 case CEC_OPCODE_USER_CONTROL_PRESSED
:
1304 return "user control pressed";
1305 case CEC_OPCODE_USER_CONTROL_RELEASE
:
1306 return "user control release";
1307 case CEC_OPCODE_GIVE_DEVICE_POWER_STATUS
:
1308 return "give device power status";
1309 case CEC_OPCODE_REPORT_POWER_STATUS
:
1310 return "report power status";
1311 case CEC_OPCODE_FEATURE_ABORT
:
1312 return "feature abort";
1313 case CEC_OPCODE_ABORT
:
1315 case CEC_OPCODE_GIVE_AUDIO_STATUS
:
1316 return "give audio status";
1317 case CEC_OPCODE_GIVE_SYSTEM_AUDIO_MODE_STATUS
:
1318 return "give audio mode status";
1319 case CEC_OPCODE_REPORT_AUDIO_STATUS
:
1320 return "report audio status";
1321 case CEC_OPCODE_SET_SYSTEM_AUDIO_MODE
:
1322 return "set system audio mode";
1323 case CEC_OPCODE_SYSTEM_AUDIO_MODE_REQUEST
:
1324 return "system audio mode request";
1325 case CEC_OPCODE_SYSTEM_AUDIO_MODE_STATUS
:
1326 return "system audio mode status";
1327 case CEC_OPCODE_SET_AUDIO_RATE
:
1328 return "set audio rate";
1334 const char *CCECProcessor::ToString(const cec_system_audio_status mode
)
1338 case CEC_SYSTEM_AUDIO_STATUS_ON
:
1340 case CEC_SYSTEM_AUDIO_STATUS_OFF
:
1347 const char *CCECProcessor::ToString(const cec_audio_status
UNUSED(status
))
1349 // TODO this is a mask
1353 const char *CCECProcessor::ToString(const cec_vendor_id vendor
)
1357 case CEC_VENDOR_SAMSUNG
:
1361 case CEC_VENDOR_PANASONIC
:
1363 case CEC_VENDOR_PIONEER
:
1365 case CEC_VENDOR_ONKYO
:
1367 case CEC_VENDOR_YAMAHA
:
1369 case CEC_VENDOR_PHILIPS
:
1371 case CEC_VENDOR_SONY
:
1378 void *CCECBusScan::Process(void)
1380 CCECBusDevice
*device(NULL
);
1381 uint8_t iCounter(0);
1383 while (!IsStopped())
1385 if (++iCounter
< 10)
1390 for (unsigned int iPtr
= 0; iPtr
<= 11 && !IsStopped(); iPtr
++)
1392 device
= m_processor
->m_busDevices
[iPtr
];
1394 if (device
&& device
->GetStatus(true) == CEC_DEVICE_STATUS_PRESENT
)
1398 device
->GetVendorId();
1402 device
->GetPowerStatus(true);
1410 void CCECBusScan::WaitUntilIdle(void)
1415 int32_t iWaitTime
= 3000 - (int32_t)(GetTimeMs() - m_processor
->GetLastTransmission());
1416 while (iWaitTime
> 0)
1419 iWaitTime
= 3000 - (int32_t)(GetTimeMs() - m_processor
->GetLastTransmission());
1423 bool CCECProcessor::StartBootloader(void)
1425 return m_communication
->StartBootloader();
1428 bool CCECProcessor::PingAdapter(void)
1430 return m_communication
->PingAdapter();
1433 void CCECProcessor::HandlePoll(cec_logical_address initiator
, cec_logical_address destination
)
1435 m_busDevices
[initiator
]->GetHandler()->HandlePoll(initiator
, destination
);
1436 m_lastInitiator
= initiator
;
1439 bool CCECProcessor::HandleReceiveFailed(void)
1441 return m_lastInitiator
!= CECDEVICE_UNKNOWN
&&
1442 !m_busDevices
[m_lastInitiator
]->GetHandler()->HandleReceiveFailed();