fixed: somenow not initializing this pointer crashes us in libpthread
[deb_libcec.git] / src / lib / CECProcessor.h
... / ...
CommitLineData
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
40namespace 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)
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 bool Start(const char *strPort, uint16_t iBaudRate = CEC_SERIAL_DEFAULT_BAUDRATE, uint32_t iTimeoutMs = CEC_DEFAULT_CONNECT_TIMEOUT);
106 void *Process(void);
107 void Close(void);
108
109 bool OnCommandReceived(const cec_command &command);
110
111 bool IsMonitoring(void) const { return m_bMonitor; }
112 CCECBusDevice * GetDeviceByPhysicalAddress(uint16_t iPhysicalAddress, bool bSuppressUpdate = true);
113 CCECBusDevice * GetDeviceByType(cec_device_type type) const;
114 CCECBusDevice * GetPrimaryDevice(void) const;
115 cec_version GetDeviceCecVersion(cec_logical_address iAddress);
116 bool GetDeviceMenuLanguage(cec_logical_address iAddress, cec_menu_language *language);
117 CStdString GetDeviceName(void) const;
118 cec_osd_name GetDeviceOSDName(cec_logical_address iAddress);
119 uint64_t GetDeviceVendorId(cec_logical_address iAddress);
120 cec_power_status GetDevicePowerStatus(cec_logical_address iAddress);
121 cec_logical_address GetLogicalAddress(void) const { return m_configuration.logicalAddresses.primary; }
122 cec_logical_addresses GetLogicalAddresses(void) const { return m_configuration.logicalAddresses; }
123 cec_logical_addresses GetActiveDevices(void);
124 uint16_t GetDevicePhysicalAddress(cec_logical_address iAddress);
125 bool HasLogicalAddress(cec_logical_address address) const { return m_configuration.logicalAddresses.IsSet(address); }
126 bool IsPresentDevice(cec_logical_address address);
127 bool IsPresentDeviceType(cec_device_type type);
128 uint16_t GetPhysicalAddress(void) const;
129 uint64_t GetLastTransmission(void) const { return m_iLastTransmission; }
130 cec_logical_address GetActiveSource(bool bRequestActiveSource = true);
131 bool IsActiveSource(cec_logical_address iAddress);
132 bool IsInitialised(void);
133 bool SetStreamPath(uint16_t iPhysicalAddress);
134 cec_client_version GetClientVersion(void) const { return (cec_client_version)m_configuration.clientVersion; };
135 bool StandbyDevices(cec_logical_address address = CECDEVICE_BROADCAST);
136 bool PowerOnDevices(cec_logical_address address = CECDEVICE_BROADCAST);
137
138 bool SetActiveView(void);
139 bool SetActiveSource(cec_device_type type = CEC_DEVICE_TYPE_RESERVED);
140 bool SetDeckControlMode(cec_deck_control_mode mode, bool bSendUpdate = true);
141 bool SetDeckInfo(cec_deck_info info, bool bSendUpdate = true);
142 bool SetHDMIPort(cec_logical_address iBaseDevice, uint8_t iPort, bool bForce = false);
143 bool TransmitInactiveSource(void);
144 bool SetLogicalAddress(cec_logical_address iLogicalAddress);
145 bool SetMenuState(cec_menu_state state, bool bSendUpdate = true);
146 bool SetPhysicalAddress(uint16_t iPhysicalAddress, bool bSendUpdate = true);
147 bool SetActiveSource(uint16_t iStreamPath);
148 bool SwitchMonitoring(bool bEnable);
149 bool PollDevice(cec_logical_address iAddress);
150 uint8_t VolumeUp(bool bSendRelease = true);
151 uint8_t VolumeDown(bool bSendRelease = true);
152 uint8_t MuteAudio(bool bSendRelease = true);
153 bool TransmitKeypress(cec_logical_address iDestination, cec_user_control_code key, bool bWait = true);
154 bool TransmitKeyRelease(cec_logical_address iDestination, bool bWait = true);
155 bool EnablePhysicalAddressDetection(void);
156 void SetStandardLineTimeout(uint8_t iTimeout);
157 void SetRetryLineTimeout(uint8_t iTimeout);
158 bool GetCurrentConfiguration(libcec_configuration *configuration);
159 bool SetConfiguration(const libcec_configuration *configuration);
160 bool CanPersistConfiguration(void);
161 bool PersistConfiguration(libcec_configuration *configuration);
162 void RescanActiveDevices(void);
163
164 bool SetLineTimeout(uint8_t iTimeout);
165
166 const char *ToString(const cec_device_type type);
167 const char *ToString(const cec_menu_state state);
168 const char *ToString(const cec_version version);
169 const char *ToString(const cec_power_status status);
170 const char *ToString(const cec_logical_address address);
171 const char *ToString(const cec_deck_control_mode mode);
172 const char *ToString(const cec_deck_info status);
173 const char *ToString(const cec_opcode opcode);
174 const char *ToString(const cec_system_audio_status mode);
175 const char *ToString(const cec_audio_status status);
176 const char *ToString(const cec_vendor_id vendor);
177 const char *ToString(const cec_client_version version);
178 const char *ToString(const cec_server_version version);
179
180 static bool IsValidPhysicalAddress(uint16_t iPhysicalAddress);
181
182 bool Transmit(const cec_command &data);
183 void TransmitAbort(cec_logical_address address, cec_opcode opcode, cec_abort_reason reason = CEC_ABORT_REASON_UNRECOGNIZED_OPCODE);
184
185 bool ChangeDeviceType(cec_device_type from, cec_device_type to);
186 bool FindLogicalAddresses(void);
187 bool SetAckMask(uint16_t iMask);
188
189 bool StartBootloader(const char *strPort = NULL);
190 bool PingAdapter(void);
191 void HandlePoll(cec_logical_address initiator, cec_logical_address destination);
192 bool HandleReceiveFailed(cec_logical_address initiator);
193
194 bool GetDeviceInformation(const char *strPort, libcec_configuration *config, uint32_t iTimeoutMs = CEC_DEFAULT_CONNECT_TIMEOUT);
195
196 bool TransmitPendingActiveSourceCommands(void);
197
198 CCECBusDevice * m_busDevices[16];
199
200 private:
201 bool OpenConnection(const char *strPort, uint16_t iBaudRate, uint32_t iTimeoutMs, bool bStartListening = true);
202 bool Initialise(void);
203 void SetInitialised(bool bSetTo = true);
204 void CreateBusDevices(void);
205
206 void ReplaceHandlers(void);
207 bool PhysicalAddressInUse(uint16_t iPhysicalAddress);
208 bool TryLogicalAddress(cec_logical_address address);
209 bool FindLogicalAddressRecordingDevice(void);
210 bool FindLogicalAddressTuner(void);
211 bool FindLogicalAddressPlaybackDevice(void);
212 bool FindLogicalAddressAudioSystem(void);
213
214 void LogOutput(const cec_command &data);
215 void ParseCommand(const cec_command &command);
216
217 bool m_bConnectionOpened;
218 bool m_bInitialised;
219 PLATFORM::CMutex m_mutex;
220 IAdapterCommunication * m_communication;
221 CLibCEC* m_controller;
222 bool m_bMonitor;
223 cec_keypress m_previousKey;
224 PLATFORM::CThread * m_busScan;
225 uint8_t m_iStandardLineTimeout;
226 uint8_t m_iRetryLineTimeout;
227 uint64_t m_iLastTransmission;
228 CCECInputBuffer m_inBuffer;
229 libcec_configuration m_configuration;
230 };
231};