cec: fix some threading related bugs, like trying to join with a detached thread...
[deb_libcec.git] / src / lib / LibCEC.cpp
1 /*
2 * This file is part of the libCEC(R) library.
3 *
4 * libCEC(R) is Copyright (C) 2011 Pulse-Eight Limited. All rights reserved.
5 * libCEC(R) is an original work, containing original code.
6 *
7 * libCEC(R) is a trademark of Pulse-Eight Limited.
8 *
9 * This program is dual-licensed; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22 *
23 *
24 * Alternatively, you can license this library under a commercial license,
25 * please contact Pulse-Eight Licensing for more information.
26 *
27 * For more information contact:
28 * Pulse-Eight Licensing <license@pulse-eight.com>
29 * http://www.pulse-eight.com/
30 * http://www.pulse-eight.net/
31 */
32
33 #include "LibCEC.h"
34
35 #include "AdapterCommunication.h"
36 #include "AdapterDetection.h"
37 #include "CECProcessor.h"
38 #include "util/StdString.h"
39 #include "platform/timeutils.h"
40
41 using namespace std;
42 using namespace CEC;
43
44 CLibCEC::CLibCEC(const char *strDeviceName, cec_logical_address iLogicalAddress /* = CECDEVICE_PLAYBACKDEVICE1 */, uint16_t iPhysicalAddress /* = CEC_DEFAULT_PHYSICAL_ADDRESS */) :
45 m_iCurrentButton(CEC_USER_CONTROL_CODE_UNKNOWN),
46 m_buttontime(0)
47 {
48 m_comm = new CAdapterCommunication(this);
49 m_cec = new CCECProcessor(this, m_comm, strDeviceName, iLogicalAddress, iPhysicalAddress);
50 }
51
52 CLibCEC::~CLibCEC(void)
53 {
54 delete m_cec;
55 m_cec = NULL;
56
57 delete m_comm;
58 m_comm = NULL;
59 }
60
61 bool CLibCEC::Open(const char *strPort, uint64_t iTimeoutMs /* = 10000 */)
62 {
63 if (!m_comm)
64 return false;
65
66 if (m_comm->IsOpen())
67 {
68 AddLog(CEC_LOG_ERROR, "connection already open");
69 return false;
70 }
71
72 if (!m_comm->Open(strPort, 38400, iTimeoutMs))
73 {
74 AddLog(CEC_LOG_ERROR, "could not open a connection");
75 return false;
76 }
77
78 if (!m_cec->Start())
79 {
80 AddLog(CEC_LOG_ERROR, "could not start CEC communications");
81 return false;
82 }
83
84 return true;
85 }
86
87 void CLibCEC::Close(void)
88 {
89 if (m_cec)
90 {
91 m_cec->StopThread();
92 delete m_cec;
93 m_cec = NULL;
94 }
95 if (m_comm)
96 {
97 m_comm->Close();
98 delete m_comm;
99 m_comm = NULL;
100 }
101 }
102
103 int CLibCEC::FindAdapters(std::vector<cec_adapter> &deviceList, const char *strDevicePath /* = NULL */)
104 {
105 CStdString strDebug;
106 if (strDevicePath)
107 strDebug.Format("trying to autodetect the com port for device path '%s'", strDevicePath);
108 else
109 strDebug.Format("trying to autodetect all CEC adapters");
110 AddLog(CEC_LOG_DEBUG, strDebug);
111
112 return CAdapterDetection::FindAdapters(deviceList, strDevicePath);
113 }
114
115 bool CLibCEC::PingAdapter(void)
116 {
117 return m_comm ? m_comm->PingAdapter() : false;
118 }
119
120 bool CLibCEC::StartBootloader(void)
121 {
122 return m_comm ? m_comm->StartBootloader() : false;
123 }
124
125 int CLibCEC::GetMinVersion(void)
126 {
127 return CEC_MIN_VERSION;
128 }
129
130 int CLibCEC::GetLibVersion(void)
131 {
132 return CEC_LIB_VERSION;
133 }
134
135 bool CLibCEC::GetNextLogMessage(cec_log_message *message)
136 {
137 return m_logBuffer.Pop(*message);
138 }
139
140 bool CLibCEC::GetNextKeypress(cec_keypress *key)
141 {
142 return m_keyBuffer.Pop(*key);
143 }
144
145 bool CLibCEC::GetNextCommand(cec_command *command)
146 {
147 return m_commandBuffer.Pop(*command);
148 }
149
150 bool CLibCEC::Transmit(const cec_frame &data, bool bWaitForAck /* = true */)
151 {
152 return m_cec ? m_cec->Transmit(data, bWaitForAck) : false;
153 }
154
155 bool CLibCEC::SetLogicalAddress(cec_logical_address iLogicalAddress)
156 {
157 return m_cec ? m_cec->SetLogicalAddress(iLogicalAddress) : false;
158 }
159
160 bool CLibCEC::PowerOnDevices(cec_logical_address address /* = CECDEVICE_TV */)
161 {
162 return m_cec ? m_cec->PowerOnDevices(address) : false;
163 }
164
165 bool CLibCEC::StandbyDevices(cec_logical_address address /* = CECDEVICE_BROADCAST */)
166 {
167 return m_cec ? m_cec->StandbyDevices(address) : false;
168 }
169
170 bool CLibCEC::SetActiveView(void)
171 {
172 return m_cec ? m_cec->SetActiveView() : false;
173 }
174
175 bool CLibCEC::SetInactiveView(void)
176 {
177 return m_cec ? m_cec->SetInactiveView() : false;
178 }
179
180 void CLibCEC::AddLog(cec_log_level level, const string &strMessage)
181 {
182 cec_log_message message;
183 message.level = level;
184 message.message.assign(strMessage.c_str());
185 m_logBuffer.Push(message);
186 }
187
188 void CLibCEC::AddKey(void)
189 {
190 if (m_iCurrentButton != CEC_USER_CONTROL_CODE_UNKNOWN)
191 {
192 cec_keypress key;
193 key.duration = (unsigned int) (GetTimeMs() - m_buttontime);
194 key.keycode = m_iCurrentButton;
195 m_keyBuffer.Push(key);
196 m_iCurrentButton = CEC_USER_CONTROL_CODE_UNKNOWN;
197 m_buttontime = 0;
198 }
199 }
200
201 void CLibCEC::AddCommand(cec_logical_address source, cec_logical_address destination, cec_opcode opcode, cec_frame *parameters)
202 {
203 cec_command command;
204 command.source = source;
205 command.destination = destination;
206 command.opcode = opcode;
207 if (parameters)
208 command.parameters = *parameters;
209 if (m_commandBuffer.Push(command))
210 {
211 CStdString strDebug;
212 strDebug.Format("stored command '%d' in the command buffer. buffer size = %d", opcode, m_commandBuffer.Size());
213 AddLog(CEC_LOG_DEBUG, strDebug);
214 }
215 else
216 {
217 AddLog(CEC_LOG_WARNING, "command buffer is full");
218 }
219 }
220
221 void CLibCEC::CheckKeypressTimeout(void)
222 {
223 if (m_iCurrentButton != CEC_USER_CONTROL_CODE_UNKNOWN && GetTimeMs() - m_buttontime > CEC_BUTTON_TIMEOUT)
224 {
225 AddKey();
226 m_iCurrentButton = CEC_USER_CONTROL_CODE_UNKNOWN;
227 }
228 }
229
230 void CLibCEC::SetCurrentButton(cec_user_control_code iButtonCode)
231 {
232 m_iCurrentButton = iButtonCode;
233 m_buttontime = GetTimeMs();
234 }
235
236 DECLSPEC void * CECCreate(const char *strDeviceName, CEC::cec_logical_address iLogicalAddress /*= CEC::CECDEVICE_PLAYBACKDEVICE1 */, uint16_t iPhysicalAddress /* = CEC_DEFAULT_PHYSICAL_ADDRESS */)
237 {
238 return static_cast< void* > (new CLibCEC(strDeviceName, iLogicalAddress, iPhysicalAddress));
239 }