Commit | Line | Data |
---|---|---|
abbca718 LOK |
1 | #pragma once |
2 | /* | |
3 | * This file is part of the libCEC(R) library. | |
4 | * | |
b492c10e | 5 | * libCEC(R) is Copyright (C) 2011-2012 Pulse-Eight Limited. All rights reserved. |
abbca718 LOK |
6 | * libCEC(R) is an original work, containing original code. |
7 | * | |
8 | * libCEC(R) is a trademark of Pulse-Eight Limited. | |
9 | * | |
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. | |
14 | * | |
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. | |
19 | * | |
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. | |
23 | * | |
24 | * | |
25 | * Alternatively, you can license this library under a commercial license, | |
26 | * please contact Pulse-Eight Licensing for more information. | |
27 | * | |
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/ | |
32 | */ | |
33 | ||
25701fa6 | 34 | #include <string> |
4c9b9776 | 35 | #include "../../include/cectypes.h" |
7bb4ed43 | 36 | #include "platform/threads/threads.h" |
ba65909d | 37 | #include "platform/util/buffer.h" |
b1f94db1 | 38 | #include "adapter/AdapterCommunication.h" |
abbca718 | 39 | |
abbca718 LOK |
40 | namespace CEC |
41 | { | |
2abe74eb | 42 | class CLibCEC; |
7bdb5ccb | 43 | class IAdapterCommunication; |
e9de9629 | 44 | class CCECBusDevice; |
a8f0bd18 | 45 | |
a4b9f561 LOK |
46 | // a buffer that priotises the input from the TV. |
47 | // if we need more than this, we'll have to change it into a priority_queue | |
48 | class CCECInputBuffer | |
49 | { | |
50 | public: | |
51 | CCECInputBuffer(void) : m_bHasData(false) {} | |
52 | virtual ~CCECInputBuffer(void) | |
53 | { | |
54 | m_condition.Broadcast(); | |
55 | } | |
56 | ||
57 | bool Push(const cec_command &command) | |
58 | { | |
59 | bool bReturn(false); | |
60 | PLATFORM::CLockObject lock(m_mutex); | |
61 | if (command.initiator == CECDEVICE_TV) | |
62 | bReturn = m_tvInBuffer.Push(command); | |
63 | else | |
64 | bReturn = m_inBuffer.Push(command); | |
65 | ||
66 | m_bHasData |= bReturn; | |
4548e663 | 67 | if (m_bHasData) |
a4b9f561 LOK |
68 | m_condition.Signal(); |
69 | ||
70 | return bReturn; | |
71 | } | |
72 | ||
73 | bool Pop(cec_command &command, uint16_t iTimeout = 10000) | |
74 | { | |
75 | bool bReturn(false); | |
76 | PLATFORM::CLockObject lock(m_mutex); | |
77 | if (m_tvInBuffer.IsEmpty() && m_inBuffer.IsEmpty() && | |
78 | !m_condition.Wait(m_mutex, m_bHasData, iTimeout)) | |
79 | return bReturn; | |
80 | ||
81 | if (m_tvInBuffer.Pop(command)) | |
82 | bReturn = true; | |
83 | else if (m_inBuffer.Pop(command)) | |
84 | bReturn = true; | |
85 | ||
86 | m_bHasData = !m_tvInBuffer.IsEmpty() || !m_inBuffer.IsEmpty(); | |
87 | return bReturn; | |
88 | } | |
89 | ||
90 | private: | |
91 | PLATFORM::CMutex m_mutex; | |
92 | PLATFORM::CCondition<volatile bool> m_condition; | |
93 | volatile bool m_bHasData; | |
94 | PLATFORM::SyncedBuffer<cec_command> m_tvInBuffer; | |
95 | PLATFORM::SyncedBuffer<cec_command> m_inBuffer; | |
96 | }; | |
97 | ||
b1f94db1 | 98 | class CCECProcessor : public PLATFORM::CThread, public IAdapterCommunicationCallback |
abbca718 LOK |
99 | { |
100 | public: | |
7bdfd76c | 101 | CCECProcessor(CLibCEC *controller, const char *strDeviceName, const cec_device_type_list &types, uint16_t iPhysicalAddress); |
3efda01a | 102 | CCECProcessor(CLibCEC *controller, libcec_configuration *configuration); |
2abe74eb LOK |
103 | virtual ~CCECProcessor(void); |
104 | ||
1113cb7d | 105 | virtual bool Start(const char *strPort, uint16_t iBaudRate = 38400, uint32_t iTimeoutMs = 10000); |
e41b06f9 | 106 | virtual void *Process(void); |
eca71746 | 107 | virtual void Close(void); |
e41b06f9 | 108 | |
b1f94db1 LOK |
109 | virtual bool OnCommandReceived(const cec_command &command); |
110 | ||
8747dd4f | 111 | virtual bool IsMonitoring(void) const { return m_bMonitor; } |
16b1e052 | 112 | virtual CCECBusDevice * GetDeviceByPhysicalAddress(uint16_t iPhysicalAddress, bool bRefresh = false) const; |
a9232a79 | 113 | virtual CCECBusDevice * GetDeviceByType(cec_device_type type) const; |
842262d8 | 114 | virtual CCECBusDevice * GetPrimaryDevice(void) const; |
8747dd4f LOK |
115 | virtual cec_version GetDeviceCecVersion(cec_logical_address iAddress); |
116 | virtual bool GetDeviceMenuLanguage(cec_logical_address iAddress, cec_menu_language *language); | |
30b4aac0 LOK |
117 | virtual CStdString GetDeviceName(void) const |
118 | { | |
119 | CStdString strName; | |
120 | strName = m_configuration.strDeviceName; | |
121 | return strName; | |
122 | } | |
ed21be2a | 123 | virtual cec_osd_name GetDeviceOSDName(cec_logical_address iAddress); |
8747dd4f LOK |
124 | virtual uint64_t GetDeviceVendorId(cec_logical_address iAddress); |
125 | virtual cec_power_status GetDevicePowerStatus(cec_logical_address iAddress); | |
dd18e809 LOK |
126 | virtual cec_logical_address GetLogicalAddress(void) const { return m_configuration.logicalAddresses.primary; } |
127 | virtual cec_logical_addresses GetLogicalAddresses(void) const { return m_configuration.logicalAddresses; } | |
6d858ba4 | 128 | virtual cec_logical_addresses GetActiveDevices(void); |
eab72c40 | 129 | virtual uint16_t GetDevicePhysicalAddress(cec_logical_address iAddress); |
dd18e809 | 130 | virtual bool HasLogicalAddress(cec_logical_address address) const { return m_configuration.logicalAddresses.IsSet(address); } |
37b0c572 LOK |
131 | virtual bool IsPresentDevice(cec_logical_address address); |
132 | virtual bool IsPresentDeviceType(cec_device_type type); | |
8747dd4f | 133 | virtual uint16_t GetPhysicalAddress(void) const; |
5f316715 | 134 | virtual uint64_t GetLastTransmission(void) const { return m_iLastTransmission; } |
b4b1b49b LOK |
135 | virtual cec_logical_address GetActiveSource(void); |
136 | virtual bool IsActiveSource(cec_logical_address iAddress); | |
0cfdeb5a | 137 | virtual bool IsInitialised(void); |
f42d3e0f | 138 | virtual bool SetStreamPath(uint16_t iPhysicalAddress); |
3efda01a | 139 | virtual cec_client_version GetClientVersion(void) const { return (cec_client_version)m_configuration.clientVersion; }; |
ca27e6cf LOK |
140 | virtual bool StandbyDevices(cec_logical_address address = CECDEVICE_BROADCAST); |
141 | virtual bool PowerOnDevices(cec_logical_address address = CECDEVICE_BROADCAST); | |
abbca718 | 142 | |
abbca718 | 143 | virtual bool SetActiveView(void); |
18203d17 | 144 | virtual bool SetActiveSource(cec_device_type type = CEC_DEVICE_TYPE_RESERVED); |
28fa6c97 LOK |
145 | virtual bool SetDeckControlMode(cec_deck_control_mode mode, bool bSendUpdate = true); |
146 | virtual bool SetDeckInfo(cec_deck_info info, bool bSendUpdate = true); | |
d2f1c157 | 147 | virtual bool SetHDMIPort(cec_logical_address iBaseDevice, uint8_t iPort, bool bForce = false); |
ab27363d | 148 | virtual bool TransmitInactiveSource(void); |
06bfd4d7 | 149 | virtual bool SetLogicalAddress(cec_logical_address iLogicalAddress); |
28fa6c97 | 150 | virtual bool SetMenuState(cec_menu_state state, bool bSendUpdate = true); |
bebb19dc | 151 | virtual bool SetPhysicalAddress(uint16_t iPhysicalAddress, bool bSendUpdate = true); |
37b0c572 | 152 | virtual bool SetActiveSource(uint16_t iStreamPath); |
8b7e5ff6 | 153 | virtual bool SwitchMonitoring(bool bEnable); |
57f45e6c | 154 | virtual bool PollDevice(cec_logical_address iAddress); |
5c73f7f7 LOK |
155 | virtual uint8_t VolumeUp(bool bSendRelease = true); |
156 | virtual uint8_t VolumeDown(bool bSendRelease = true); | |
157 | virtual uint8_t MuteAudio(bool bSendRelease = true); | |
4bec9d79 LOK |
158 | virtual bool TransmitKeypress(cec_logical_address iDestination, cec_user_control_code key, bool bWait = true); |
159 | virtual bool TransmitKeyRelease(cec_logical_address iDestination, bool bWait = true); | |
8670c970 | 160 | virtual bool EnablePhysicalAddressDetection(void); |
dcd240b2 LOK |
161 | void SetStandardLineTimeout(uint8_t iTimeout); |
162 | void SetRetryLineTimeout(uint8_t iTimeout); | |
d40928b5 | 163 | virtual bool GetCurrentConfiguration(libcec_configuration *configuration); |
30b4aac0 | 164 | virtual bool SetConfiguration(const libcec_configuration *configuration); |
224ea877 LOK |
165 | virtual bool CanPersistConfiguration(void); |
166 | virtual bool PersistConfiguration(libcec_configuration *configuration); | |
3efda01a | 167 | virtual void RescanActiveDevices(void); |
acec5f48 | 168 | |
855a3a98 LOK |
169 | bool SetLineTimeout(uint8_t iTimeout); |
170 | ||
3e61b350 | 171 | const char *ToString(const cec_device_type type); |
03ae897d LOK |
172 | const char *ToString(const cec_menu_state state); |
173 | const char *ToString(const cec_version version); | |
174 | const char *ToString(const cec_power_status status); | |
175 | const char *ToString(const cec_logical_address address); | |
176 | const char *ToString(const cec_deck_control_mode mode); | |
177 | const char *ToString(const cec_deck_info status); | |
178 | const char *ToString(const cec_opcode opcode); | |
179 | const char *ToString(const cec_system_audio_status mode); | |
180 | const char *ToString(const cec_audio_status status); | |
181 | const char *ToString(const cec_vendor_id vendor); | |
caca2d81 | 182 | const char *ToString(const cec_client_version version); |
3efda01a | 183 | const char *ToString(const cec_server_version version); |
03ae897d | 184 | |
06bfd4d7 | 185 | virtual bool Transmit(const cec_command &data); |
22b4e74a | 186 | virtual void TransmitAbort(cec_logical_address address, cec_opcode opcode, cec_abort_reason reason = CEC_ABORT_REASON_UNRECOGNIZED_OPCODE); |
e9de9629 | 187 | |
3e61b350 | 188 | virtual bool ChangeDeviceType(cec_device_type from, cec_device_type to); |
f8513317 | 189 | virtual bool FindLogicalAddresses(void); |
93fff5c1 | 190 | virtual bool SetAckMask(uint16_t iMask); |
f8513317 | 191 | |
a2198e5e | 192 | virtual bool StartBootloader(const char *strPort = NULL); |
1113cb7d | 193 | virtual bool PingAdapter(void); |
6729ac71 | 194 | virtual void HandlePoll(cec_logical_address initiator, cec_logical_address destination); |
7bb4ed43 | 195 | virtual bool HandleReceiveFailed(cec_logical_address initiator); |
1113cb7d | 196 | |
f80cd208 LOK |
197 | virtual bool GetDeviceInformation(const char *strPort, libcec_configuration *config, uint32_t iTimeoutMs = 10000); |
198 | ||
f00ff009 | 199 | CCECBusDevice * m_busDevices[16]; |
abbca718 | 200 | |
e41b06f9 | 201 | private: |
f80cd208 | 202 | bool OpenConnection(const char *strPort, uint16_t iBaudRate, uint32_t iTimeoutMs, bool bStartListening = true); |
578c3905 | 203 | bool Initialise(void); |
2db8981f | 204 | void SetInitialised(bool bSetTo = true); |
caca2d81 | 205 | void CreateBusDevices(void); |
578c3905 | 206 | |
a3ffc068 | 207 | void ReplaceHandlers(void); |
45b97de7 | 208 | bool PhysicalAddressInUse(uint16_t iPhysicalAddress); |
b58d9277 LOK |
209 | bool TryLogicalAddress(cec_logical_address address); |
210 | bool FindLogicalAddressRecordingDevice(void); | |
211 | bool FindLogicalAddressTuner(void); | |
212 | bool FindLogicalAddressPlaybackDevice(void); | |
213 | bool FindLogicalAddressAudioSystem(void); | |
f8513317 | 214 | |
9dee1670 | 215 | void LogOutput(const cec_command &data); |
b1f94db1 | 216 | void ParseCommand(const cec_command &command); |
abbca718 | 217 | |
80726797 | 218 | bool m_bConnectionOpened; |
f00ff009 | 219 | bool m_bInitialised; |
f00ff009 | 220 | PLATFORM::CMutex m_mutex; |
7bb4ed43 | 221 | IAdapterCommunication * m_communication; |
f00ff009 LOK |
222 | CLibCEC* m_controller; |
223 | bool m_bMonitor; | |
f00ff009 LOK |
224 | cec_keypress m_previousKey; |
225 | PLATFORM::CThread * m_busScan; | |
f00ff009 LOK |
226 | uint8_t m_iStandardLineTimeout; |
227 | uint8_t m_iRetryLineTimeout; | |
228 | uint64_t m_iLastTransmission; | |
a4b9f561 | 229 | CCECInputBuffer m_inBuffer; |
30b4aac0 | 230 | libcec_configuration m_configuration; |
7c63a480 LOK |
231 | }; |
232 | ||
f00ff009 | 233 | class CCECBusScan : public PLATFORM::CThread |
7c63a480 LOK |
234 | { |
235 | public: | |
236 | CCECBusScan(CCECProcessor *processor) { m_processor = processor; } | |
a0878ee3 | 237 | virtual ~CCECBusScan(void) { StopThread(true); } |
7c63a480 LOK |
238 | virtual void *Process(void); |
239 | ||
240 | private: | |
5f316715 LOK |
241 | void WaitUntilIdle(void); |
242 | ||
7c63a480 | 243 | CCECProcessor *m_processor; |
abbca718 LOK |
244 | }; |
245 | }; |