3 * This file is part of the libCEC(R) library.
5 * libCEC(R) is Copyright (C) 2011-2012 Pulse-Eight Limited. All rights reserved.
6 * libCEC(R) is an original work, containing original code.
8 * libCEC(R) is a trademark of Pulse-Eight Limited.
10 * This program is dual-licensed; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
25 * Alternatively, you can license this library under a commercial license,
26 * please contact Pulse-Eight Licensing for more information.
28 * For more information contact:
29 * Pulse-Eight Licensing <license@pulse-eight.com>
30 * http://www.pulse-eight.com/
31 * http://www.pulse-eight.net/
34 #include "lib/platform/threads/threads.h"
35 #include "lib/adapter/AdapterCommunication.h"
45 class CAdapterPingThread
;
46 class CUSBCECAdapterCommands
;
47 class CCECAdapterMessageQueue
;
48 class CCECAdapterMessage
;
50 class CUSBCECAdapterCommunication
: public IAdapterCommunication
, public PLATFORM::CThread
52 friend class CUSBCECAdapterCommands
;
53 friend class CCECAdapterMessageQueue
;
57 * @brief Create a new USB-CEC communication handler.
58 * @param callback The callback to use for incoming CEC commands.
59 * @param strPort The name of the com port to use.
60 * @param iBaudRate The baudrate to use on the com port connection.
62 CUSBCECAdapterCommunication(IAdapterCommunicationCallback
*callback
, const char *strPort
, uint16_t iBaudRate
= CEC_SERIAL_DEFAULT_BAUDRATE
);
63 virtual ~CUSBCECAdapterCommunication(void);
65 /** @name IAdapterCommunication implementation */
67 bool Open(uint32_t iTimeoutMs
= CEC_DEFAULT_CONNECT_TIMEOUT
, bool bSkipChecks
= false, bool bStartListening
= true);
70 std::string
GetError(void) const;
71 cec_adapter_message_state
Write(const cec_command
&data
, bool &bRetry
, uint8_t iLineTimeout
, bool bIsReply
);
73 bool StartBootloader(void);
74 bool SetLogicalAddresses(const cec_logical_addresses
&addresses
);
75 cec_logical_addresses
GetLogicalAddresses(void);
76 bool PingAdapter(void);
77 uint16_t GetFirmwareVersion(void);
78 uint32_t GetFirmwareBuildDate(void);
79 bool IsRunningLatestFirmware(void);
80 bool PersistConfiguration(const libcec_configuration
&configuration
);
81 bool GetConfiguration(libcec_configuration
&configuration
);
82 std::string
GetPortName(void);
83 uint16_t GetPhysicalAddress(void);
84 bool SetControlledMode(bool controlled
);
85 cec_vendor_id
GetVendorId(void) { return CEC_VENDOR_UNKNOWN
; }
86 bool SupportsSourceLogicalAddress(const cec_logical_address
UNUSED(address
)) { return true; }
93 * @brief Clear all input bytes.
94 * @param iTimeout Timeout when anything was received.
96 void ClearInputBytes(uint32_t iTimeout
= CEC_CLEAR_INPUT_DEFAULT_WAIT
);
99 * @brief Change the current CEC line timeout.
100 * @param iTimeout The new timeout.
101 * @return True when acked by the controller, false otherwise.
103 bool SetLineTimeout(uint8_t iTimeout
);
106 * @brief Send a command to the controller and wait for an ack.
107 * @param msgCode The command to send.
108 * @param params The parameters to the command.
109 * @param bIsRetry True when this command is being retried, false otherwise.
110 * @return The message. Delete when done with it.
112 CCECAdapterMessage
*SendCommand(cec_adapter_messagecode msgCode
, CCECAdapterMessage
¶ms
, bool bIsRetry
= false);
115 * @brief Change the "initialised" status.
116 * @param bSetTo The new value.
118 void SetInitialised(bool bSetTo
= true);
121 * @return True when initialised, false otherwise.
123 bool IsInitialised(void);
126 * @brief Pings the adapter, checks the firmware version and sets controlled mode.
127 * @param iTimeoutMs The timeout after which this fails if no proper data was received.
128 * @return True when the checks passed, false otherwise.
130 bool CheckAdapter(uint32_t iTimeoutMs
= CEC_DEFAULT_CONNECT_TIMEOUT
);
133 * @brief Handle a poll message inside the adapter message (checks if one is present).
134 * @param msg The adapter message to parse.
135 * @return True when the message resulted in a CEC error, false otherwise.
137 bool HandlePoll(const CCECAdapterMessage
&msg
);
140 * @brief Read data from the device.
141 * @param iTimeout The read timeout to use.
142 * @param iSize The maximum read size.
143 * @return True when something was read, false otherwise.
145 bool ReadFromDevice(uint32_t iTimeout
, size_t iSize
= 256);
148 * @brief Writes a message to the serial port.
149 * @param message The message to write.
150 * @return True when written, false otherwise.
152 bool WriteToDevice(CCECAdapterMessage
*message
);
155 * @brief Called before sending a CEC command over the line, so we know we're expecting an ack.
156 * @param dest The destination of the CEC command.
158 void MarkAsWaiting(const cec_logical_address dest
);
161 * @brief Clear and reset the message queue.
163 void ResetMessageQueue(void);
165 PLATFORM::ISocket
* m_port
; /**< the com port connection */
166 PLATFORM::CMutex m_mutex
; /**< mutex for changes in this class */
167 uint8_t m_iLineTimeout
; /**< the current line timeout on the CEC line */
168 cec_logical_address m_lastPollDestination
; /**< the destination of the last poll message that was received */
169 bool m_bInitialised
; /**< true when the connection is initialised, false otherwise */
170 bool m_bWaitingForAck
[15]; /**< array in which we store from which devices we're expecting acks */
171 CAdapterPingThread
* m_pingThread
; /**< ping thread, that pings the adapter every 15 seconds */
172 CUSBCECAdapterCommands
* m_commands
; /**< commands that can be sent to the adapter */
173 CCECAdapterMessageQueue
* m_adapterMessageQueue
; /**< the incoming and outgoing message queue */
174 cec_logical_addresses m_logicalAddresses
; /**< the logical address list that this instance is using */
175 int64_t m_iLastEepromWrite
; /**< last time that this instance did an eeprom write */
176 int64_t m_iScheduleEepromWrite
; /**< in case there were more than 2 changes within 30 seconds, do another write at this time */
179 class CAdapterPingThread
: public PLATFORM::CThread
182 CAdapterPingThread(CUSBCECAdapterCommunication
*com
, uint32_t iTimeout
) :
184 m_timeout(iTimeout
){}
185 virtual ~CAdapterPingThread(void) {}
187 virtual void* Process(void);
189 CUSBCECAdapterCommunication
*m_com
;
190 PLATFORM::CTimeout m_timeout
;