Commit | Line | Data |
---|---|---|
e9de9629 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. |
e9de9629 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 | ||
4d738fe3 | 34 | #include <set> |
060a7b5e | 35 | #include <map> |
2b44051c LOK |
36 | #include "lib/platform/threads/mutex.h" |
37 | #include "lib/platform/util/StdString.h" | |
e9de9629 LOK |
38 | |
39 | namespace CEC | |
40 | { | |
004b8382 | 41 | class CCECClient; |
e9de9629 | 42 | class CCECProcessor; |
1b5cc4a2 | 43 | class CCECCommandHandler; |
004b8382 LOK |
44 | class CCECAudioSystem; |
45 | class CCECPlaybackDevice; | |
46 | class CCECRecordingDevice; | |
47 | class CCECTuner; | |
48 | class CCECTV; | |
e9de9629 | 49 | |
060a7b5e LOK |
50 | class CResponse |
51 | { | |
52 | public: | |
53 | CResponse(cec_opcode opcode) : | |
54 | m_opcode(opcode){} | |
55 | ~CResponse(void) | |
56 | { | |
57 | Broadcast(); | |
58 | } | |
59 | ||
60 | bool Wait(uint32_t iTimeout) | |
61 | { | |
62 | return m_event.Wait(iTimeout); | |
63 | } | |
64 | ||
65 | void Broadcast(void) | |
66 | { | |
67 | m_event.Broadcast(); | |
68 | } | |
69 | ||
70 | private: | |
71 | cec_opcode m_opcode; | |
72 | PLATFORM::CEvent m_event; | |
73 | }; | |
74 | ||
75 | class CWaitForResponse | |
76 | { | |
77 | public: | |
78 | CWaitForResponse(void) {} | |
79 | ~CWaitForResponse(void) | |
80 | { | |
81 | Clear(); | |
82 | } | |
83 | ||
84 | void Clear() | |
85 | { | |
86 | PLATFORM::CLockObject lock(m_mutex); | |
87 | for (std::map<cec_opcode, CResponse*>::iterator it = m_waitingFor.begin(); it != m_waitingFor.end(); it++) | |
88 | it->second->Broadcast(); | |
89 | m_waitingFor.clear(); | |
90 | } | |
91 | ||
92 | bool Wait(cec_opcode opcode, uint32_t iTimeout = CEC_DEFAULT_TRANSMIT_WAIT) | |
93 | { | |
94 | CResponse *response = GetEvent(opcode); | |
95 | return response ? response->Wait(iTimeout) : false; | |
96 | } | |
97 | ||
98 | void Received(cec_opcode opcode) | |
99 | { | |
100 | CResponse *response = GetEvent(opcode); | |
101 | if (response) | |
102 | response->Broadcast(); | |
103 | } | |
104 | ||
105 | private: | |
106 | CResponse *GetEvent(cec_opcode opcode) | |
107 | { | |
108 | CResponse *retVal(NULL); | |
109 | { | |
110 | PLATFORM::CLockObject lock(m_mutex); | |
111 | std::map<cec_opcode, CResponse*>::iterator it = m_waitingFor.find(opcode); | |
112 | if (it != m_waitingFor.end()) | |
113 | { | |
114 | retVal = it->second; | |
115 | } | |
116 | else | |
117 | { | |
118 | retVal = new CResponse(opcode); | |
119 | m_waitingFor[opcode] = retVal; | |
120 | } | |
121 | return retVal; | |
122 | } | |
123 | } | |
124 | ||
125 | PLATFORM::CMutex m_mutex; | |
126 | std::map<cec_opcode, CResponse*> m_waitingFor; | |
127 | }; | |
128 | ||
e9de9629 LOK |
129 | class CCECBusDevice |
130 | { | |
787a3cb8 LOK |
131 | friend class CCECProcessor; |
132 | ||
e9de9629 | 133 | public: |
d2d1660c | 134 | CCECBusDevice(CCECProcessor *processor, cec_logical_address address, uint16_t iPhysicalAddress = CEC_INVALID_PHYSICAL_ADDRESS); |
e9de9629 LOK |
135 | virtual ~CCECBusDevice(void); |
136 | ||
004b8382 | 137 | virtual bool ReplaceHandler(bool bActivateSource = true); |
91ef4e2d LOK |
138 | |
139 | // TODO use something smarter than this | |
140 | /*! | |
141 | * @brief Get the command handler for this device. Call MarkHandlerReady() when done with it. | |
142 | * @return The current handler. | |
143 | */ | |
144 | virtual CCECCommandHandler * GetHandler(void); | |
145 | ||
146 | /*! | |
147 | * @brief To be called after GetHandler(), when no longer using it. | |
148 | */ | |
149 | virtual void MarkHandlerReady(void) { MarkReady(); } | |
150 | ||
004b8382 LOK |
151 | virtual CCECProcessor * GetProcessor(void) const { return m_processor; } |
152 | virtual uint64_t GetLastActive(void) const { return m_iLastActive; } | |
153 | virtual cec_device_type GetType(void) const { return m_type; } | |
f8ae3295 LOK |
154 | virtual cec_logical_address GetLogicalAddress(void) const { return m_iLogicalAddress; } |
155 | virtual const char* GetLogicalAddressName(void) const; | |
004b8382 LOK |
156 | virtual bool IsPresent(void); |
157 | virtual bool IsHandledByLibCEC(void); | |
158 | ||
159 | virtual bool HandleCommand(const cec_command &command); | |
33dd87a9 | 160 | virtual bool IsUnsupportedFeature(cec_opcode opcode); |
4d738fe3 | 161 | virtual void SetUnsupportedFeature(cec_opcode opcode); |
004b8382 LOK |
162 | |
163 | virtual bool TransmitKeypress(const cec_logical_address initiator, cec_user_control_code key, bool bWait = true); | |
164 | virtual bool TransmitKeyRelease(const cec_logical_address initiator, bool bWait = true); | |
165 | ||
166 | virtual cec_version GetCecVersion(const cec_logical_address initiator, bool bUpdate = false); | |
167 | virtual void SetCecVersion(const cec_version newVersion); | |
168 | virtual bool RequestCecVersion(const cec_logical_address initiator, bool bWaitForResponse = true); | |
2b44051c | 169 | virtual bool TransmitCECVersion(const cec_logical_address destination, bool bIsReply); |
004b8382 LOK |
170 | |
171 | virtual cec_menu_language & GetMenuLanguage(const cec_logical_address initiator, bool bUpdate = false); | |
172 | virtual void SetMenuLanguage(const char *strLanguage); | |
173 | virtual void SetMenuLanguage(const cec_menu_language &menuLanguage); | |
174 | virtual bool RequestMenuLanguage(const cec_logical_address initiator, bool bWaitForResponse = true); | |
2b44051c | 175 | virtual bool TransmitSetMenuLanguage(const cec_logical_address destination, bool bIsReply); |
004b8382 | 176 | |
2b44051c | 177 | virtual bool TransmitOSDString(const cec_logical_address destination, cec_display_control duration, const char *strMessage, bool bIsReply); |
004b8382 | 178 | |
c0152c09 | 179 | virtual CStdString GetCurrentOSDName(void); |
004b8382 LOK |
180 | virtual CStdString GetOSDName(const cec_logical_address initiator, bool bUpdate = false); |
181 | virtual void SetOSDName(CStdString strName); | |
182 | virtual bool RequestOSDName(const cec_logical_address source, bool bWaitForResponse = true); | |
2b44051c | 183 | virtual bool TransmitOSDName(const cec_logical_address destination, bool bIsReply); |
004b8382 LOK |
184 | |
185 | virtual uint16_t GetCurrentPhysicalAddress(void); | |
186 | virtual bool HasValidPhysicalAddress(void); | |
187 | virtual uint16_t GetPhysicalAddress(const cec_logical_address initiator, bool bSuppressUpdate = false); | |
188 | virtual bool SetPhysicalAddress(uint16_t iNewAddress); | |
189 | virtual bool RequestPhysicalAddress(const cec_logical_address initiator, bool bWaitForResponse = true); | |
2b44051c | 190 | virtual bool TransmitPhysicalAddress(bool bIsReply); |
004b8382 LOK |
191 | |
192 | virtual cec_power_status GetCurrentPowerStatus(void); | |
193 | virtual cec_power_status GetPowerStatus(const cec_logical_address initiator, bool bUpdate = false); | |
194 | virtual void SetPowerStatus(const cec_power_status powerStatus); | |
195 | virtual bool RequestPowerStatus(const cec_logical_address initiator, bool bWaitForResponse = true); | |
2b44051c | 196 | virtual bool TransmitPowerState(const cec_logical_address destination, bool bIsReply); |
004b8382 LOK |
197 | |
198 | virtual cec_vendor_id GetCurrentVendorId(void); | |
199 | virtual cec_vendor_id GetVendorId(const cec_logical_address initiator, bool bUpdate = false); | |
200 | virtual const char * GetVendorName(const cec_logical_address initiator, bool bUpdate = false); | |
201 | virtual bool SetVendorId(uint64_t iVendorId); | |
202 | virtual bool RequestVendorId(const cec_logical_address initiator, bool bWaitForResponse = true); | |
2b44051c | 203 | virtual bool TransmitVendorID(const cec_logical_address destination, bool bSendAbort, bool bIsReply); |
004b8382 LOK |
204 | |
205 | virtual cec_bus_device_status GetCurrentStatus(void) { return GetStatus(false, true); } | |
206 | virtual cec_bus_device_status GetStatus(bool bForcePoll = false, bool bSuppressPoll = false); | |
2b44051c | 207 | virtual void SetDeviceStatus(const cec_bus_device_status newStatus, cec_version libCECSpecVersion = CEC_VERSION_1_4); |
004b8382 | 208 | virtual void ResetDeviceStatus(void); |
be3b6983 | 209 | virtual bool TransmitPoll(const cec_logical_address destination, bool bUpdateDeviceStatus); |
004b8382 LOK |
210 | virtual void HandlePoll(const cec_logical_address destination); |
211 | virtual void HandlePollFrom(const cec_logical_address initiator); | |
0cfdeb5a | 212 | virtual bool HandleReceiveFailed(void); |
e9de9629 | 213 | |
004b8382 LOK |
214 | virtual cec_menu_state GetMenuState(const cec_logical_address initiator); |
215 | virtual void SetMenuState(const cec_menu_state state); | |
2b44051c | 216 | virtual bool TransmitMenuState(const cec_logical_address destination, bool bIsReply); |
004b8382 | 217 | |
060a7b5e | 218 | virtual bool ActivateSource(uint64_t iDelay = 0); |
004b8382 LOK |
219 | virtual bool IsActiveSource(void) const { return m_bActiveSource; } |
220 | virtual bool RequestActiveSource(bool bWaitForResponse = true); | |
221 | virtual void MarkAsActiveSource(void); | |
222 | virtual void MarkAsInactiveSource(void); | |
2b44051c | 223 | virtual bool TransmitActiveSource(bool bIsReply); |
004b8382 LOK |
224 | virtual bool TransmitImageViewOn(void); |
225 | virtual bool TransmitInactiveSource(void); | |
226 | virtual bool TransmitPendingActiveSourceCommands(void); | |
227 | virtual void SetStreamPath(uint16_t iNewAddress, uint16_t iOldAddress = CEC_INVALID_PHYSICAL_ADDRESS); | |
228 | ||
229 | virtual bool PowerOn(const cec_logical_address initiator); | |
230 | virtual bool Standby(const cec_logical_address initiator); | |
231 | ||
2b44051c | 232 | virtual bool TryLogicalAddress(cec_version libCECSpecVersion = CEC_VERSION_1_4); |
004b8382 LOK |
233 | |
234 | CCECClient * GetClient(void); | |
060a7b5e LOK |
235 | void SignalOpcode(cec_opcode opcode); |
236 | bool WaitForOpcode(cec_opcode opcode); | |
004b8382 LOK |
237 | |
238 | CCECAudioSystem * AsAudioSystem(void); | |
239 | static CCECAudioSystem * AsAudioSystem(CCECBusDevice *device); | |
240 | CCECPlaybackDevice * AsPlaybackDevice(void); | |
241 | static CCECPlaybackDevice * AsPlaybackDevice(CCECBusDevice *device); | |
242 | CCECRecordingDevice * AsRecordingDevice(void); | |
243 | static CCECRecordingDevice * AsRecordingDevice(CCECBusDevice *device); | |
244 | CCECTuner * AsTuner(void); | |
245 | static CCECTuner * AsTuner(CCECBusDevice *device); | |
246 | CCECTV * AsTV(void); | |
247 | static CCECTV * AsTV(CCECBusDevice *device); | |
5734016c | 248 | |
e9de9629 | 249 | protected: |
004b8382 | 250 | void CheckVendorIdRequested(const cec_logical_address source); |
1344fd1a LOK |
251 | void MarkBusy(void); |
252 | void MarkReady(void); | |
b64db02e | 253 | |
9fd73dd4 LOK |
254 | bool NeedsPoll(void); |
255 | ||
f8ae3295 LOK |
256 | cec_device_type m_type; |
257 | CStdString m_strDeviceName; | |
258 | uint16_t m_iPhysicalAddress; | |
259 | uint16_t m_iStreamPath; | |
260 | cec_logical_address m_iLogicalAddress; | |
261 | cec_power_status m_powerStatus; | |
262 | cec_menu_language m_menuLanguage; | |
263 | CCECProcessor * m_processor; | |
264 | CCECCommandHandler * m_handler; | |
265 | cec_vendor_id m_vendor; | |
b64db02e | 266 | bool m_bReplaceHandler; |
f8ae3295 LOK |
267 | cec_menu_state m_menuState; |
268 | bool m_bActiveSource; | |
045b2e29 LOK |
269 | int64_t m_iLastActive; |
270 | int64_t m_iLastPowerStateUpdate; | |
f8ae3295 LOK |
271 | cec_version m_cecVersion; |
272 | cec_bus_device_status m_deviceStatus; | |
4d738fe3 | 273 | std::set<cec_opcode> m_unsupportedFeatures; |
f00ff009 LOK |
274 | PLATFORM::CMutex m_mutex; |
275 | PLATFORM::CMutex m_handlerMutex; | |
67b9d205 | 276 | PLATFORM::CEvent m_replacing; |
1344fd1a | 277 | unsigned m_iHandlerUseCount; |
ef583662 | 278 | bool m_bAwaitingReceiveFailed; |
ebb6ddb3 | 279 | bool m_bVendorIdRequested; |
060a7b5e | 280 | CWaitForResponse *m_waitForResponse; |
e9de9629 LOK |
281 | }; |
282 | }; |