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