cec: delay the 'active source' command for panasonic, until the vendor command that...
[deb_libcec.git] / src / lib / CECProcessor.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 <string>
35 #include "../../include/cectypes.h"
36 #include "platform/threads/threads.h"
37 #include "platform/util/buffer.h"
38 #include "adapter/AdapterCommunication.h"
39
40 namespace CEC
41 {
42 class CLibCEC;
43 class IAdapterCommunication;
44 class CCECBusDevice;
45
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;
67 if (m_bHasData)
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
98 class CCECProcessor : public PLATFORM::CThread, public IAdapterCommunicationCallback
99 {
100 public:
101 CCECProcessor(CLibCEC *controller, const char *strDeviceName, const cec_device_type_list &types, uint16_t iPhysicalAddress);
102 CCECProcessor(CLibCEC *controller, libcec_configuration *configuration);
103 virtual ~CCECProcessor(void);
104
105 virtual bool Start(const char *strPort, uint16_t iBaudRate = 38400, uint32_t iTimeoutMs = 10000);
106 virtual void *Process(void);
107 virtual void Close(void);
108
109 virtual bool OnCommandReceived(const cec_command &command);
110
111 virtual bool IsMonitoring(void) const { return m_bMonitor; }
112 virtual CCECBusDevice * GetDeviceByPhysicalAddress(uint16_t iPhysicalAddress, bool bRefresh = false, bool bSuppressPoll = false) const;
113 virtual CCECBusDevice * GetDeviceByType(cec_device_type type) const;
114 virtual CCECBusDevice * GetPrimaryDevice(void) const;
115 virtual cec_version GetDeviceCecVersion(cec_logical_address iAddress);
116 virtual bool GetDeviceMenuLanguage(cec_logical_address iAddress, cec_menu_language *language);
117 virtual CStdString GetDeviceName(void) const
118 {
119 CStdString strName;
120 strName = m_configuration.strDeviceName;
121 return strName;
122 }
123 virtual cec_osd_name GetDeviceOSDName(cec_logical_address iAddress);
124 virtual uint64_t GetDeviceVendorId(cec_logical_address iAddress);
125 virtual cec_power_status GetDevicePowerStatus(cec_logical_address iAddress);
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; }
128 virtual cec_logical_addresses GetActiveDevices(void);
129 virtual uint16_t GetDevicePhysicalAddress(cec_logical_address iAddress);
130 virtual bool HasLogicalAddress(cec_logical_address address) const { return m_configuration.logicalAddresses.IsSet(address); }
131 virtual bool IsPresentDevice(cec_logical_address address);
132 virtual bool IsPresentDeviceType(cec_device_type type);
133 virtual uint16_t GetPhysicalAddress(void) const;
134 virtual uint64_t GetLastTransmission(void) const { return m_iLastTransmission; }
135 virtual cec_logical_address GetActiveSource(void);
136 virtual bool IsActiveSource(cec_logical_address iAddress);
137 virtual bool IsInitialised(void);
138 virtual bool SetStreamPath(uint16_t iPhysicalAddress);
139 virtual cec_client_version GetClientVersion(void) const { return (cec_client_version)m_configuration.clientVersion; };
140 virtual bool StandbyDevices(cec_logical_address address = CECDEVICE_BROADCAST);
141 virtual bool PowerOnDevices(cec_logical_address address = CECDEVICE_BROADCAST);
142
143 virtual bool SetActiveView(void);
144 virtual bool SetActiveSource(cec_device_type type = CEC_DEVICE_TYPE_RESERVED);
145 virtual bool SetDeckControlMode(cec_deck_control_mode mode, bool bSendUpdate = true);
146 virtual bool SetDeckInfo(cec_deck_info info, bool bSendUpdate = true);
147 virtual bool SetHDMIPort(cec_logical_address iBaseDevice, uint8_t iPort, bool bForce = false);
148 virtual bool TransmitInactiveSource(void);
149 virtual bool SetLogicalAddress(cec_logical_address iLogicalAddress);
150 virtual bool SetMenuState(cec_menu_state state, bool bSendUpdate = true);
151 virtual bool SetPhysicalAddress(uint16_t iPhysicalAddress, bool bSendUpdate = true);
152 virtual bool SetActiveSource(uint16_t iStreamPath);
153 virtual bool SwitchMonitoring(bool bEnable);
154 virtual bool PollDevice(cec_logical_address iAddress);
155 virtual uint8_t VolumeUp(bool bSendRelease = true);
156 virtual uint8_t VolumeDown(bool bSendRelease = true);
157 virtual uint8_t MuteAudio(bool bSendRelease = true);
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);
160 virtual bool EnablePhysicalAddressDetection(void);
161 void SetStandardLineTimeout(uint8_t iTimeout);
162 void SetRetryLineTimeout(uint8_t iTimeout);
163 virtual bool GetCurrentConfiguration(libcec_configuration *configuration);
164 virtual bool SetConfiguration(const libcec_configuration *configuration);
165 virtual bool CanPersistConfiguration(void);
166 virtual bool PersistConfiguration(libcec_configuration *configuration);
167 virtual void RescanActiveDevices(void);
168
169 bool SetLineTimeout(uint8_t iTimeout);
170
171 const char *ToString(const cec_device_type type);
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);
182 const char *ToString(const cec_client_version version);
183 const char *ToString(const cec_server_version version);
184
185 virtual bool Transmit(const cec_command &data);
186 virtual void TransmitAbort(cec_logical_address address, cec_opcode opcode, cec_abort_reason reason = CEC_ABORT_REASON_UNRECOGNIZED_OPCODE);
187
188 virtual bool ChangeDeviceType(cec_device_type from, cec_device_type to);
189 virtual bool FindLogicalAddresses(void);
190 virtual bool SetAckMask(uint16_t iMask);
191
192 virtual bool StartBootloader(const char *strPort = NULL);
193 virtual bool PingAdapter(void);
194 virtual void HandlePoll(cec_logical_address initiator, cec_logical_address destination);
195 virtual bool HandleReceiveFailed(cec_logical_address initiator);
196
197 virtual bool GetDeviceInformation(const char *strPort, libcec_configuration *config, uint32_t iTimeoutMs = 10000);
198
199 bool TransmitPendingActiveSourceCommands(void);
200
201 CCECBusDevice * m_busDevices[16];
202
203 private:
204 bool OpenConnection(const char *strPort, uint16_t iBaudRate, uint32_t iTimeoutMs, bool bStartListening = true);
205 bool Initialise(void);
206 void SetInitialised(bool bSetTo = true);
207 void CreateBusDevices(void);
208
209 void ReplaceHandlers(void);
210 bool PhysicalAddressInUse(uint16_t iPhysicalAddress);
211 bool TryLogicalAddress(cec_logical_address address);
212 bool FindLogicalAddressRecordingDevice(void);
213 bool FindLogicalAddressTuner(void);
214 bool FindLogicalAddressPlaybackDevice(void);
215 bool FindLogicalAddressAudioSystem(void);
216
217 void LogOutput(const cec_command &data);
218 void ParseCommand(const cec_command &command);
219
220 bool m_bConnectionOpened;
221 bool m_bInitialised;
222 PLATFORM::CMutex m_mutex;
223 IAdapterCommunication * m_communication;
224 CLibCEC* m_controller;
225 bool m_bMonitor;
226 cec_keypress m_previousKey;
227 PLATFORM::CThread * m_busScan;
228 uint8_t m_iStandardLineTimeout;
229 uint8_t m_iRetryLineTimeout;
230 uint64_t m_iLastTransmission;
231 CCECInputBuffer m_inBuffer;
232 libcec_configuration m_configuration;
233 };
234
235 class CCECBusScan : public PLATFORM::CThread
236 {
237 public:
238 CCECBusScan(CCECProcessor *processor) { m_processor = processor; }
239 virtual ~CCECBusScan(void) { StopThread(true); }
240 virtual void *Process(void);
241
242 private:
243 void WaitUntilIdle(void);
244
245 CCECProcessor *m_processor;
246 };
247 };