cec: dropped second mutex in CAdapterCommunication. it's all called from 1 thread now
[deb_libcec.git] / src / lib / AdapterCommunication.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 "AdapterCommunication.h"
34
35 #include "LibCEC.h"
36 #include "platform/serialport.h"
37 #include "util/StdString.h"
38 #include "platform/timeutils.h"
39
40 using namespace std;
41 using namespace CEC;
42
43 CAdapterCommunication::CAdapterCommunication(CLibCEC *controller) :
44 m_port(NULL),
45 m_controller(controller)
46 {
47 m_port = new CSerialPort;
48 }
49
50 CAdapterCommunication::~CAdapterCommunication(void)
51 {
52 Close();
53
54 if (m_port)
55 {
56 delete m_port;
57 m_port = NULL;
58 }
59 }
60
61 bool CAdapterCommunication::Open(const char *strPort, uint16_t iBaudRate /* = 38400 */, uint32_t iTimeoutMs /* = 10000 */)
62 {
63 if (!m_port)
64 {
65 m_controller->AddLog(CEC_LOG_ERROR, "port is NULL");
66 return false;
67 }
68
69 if (IsOpen())
70 {
71 m_controller->AddLog(CEC_LOG_ERROR, "port is already open");
72 }
73
74 if (!m_port->Open(strPort, iBaudRate))
75 {
76 CStdString strError;
77 strError.Format("error opening serial port '%s': %s", strPort, m_port->GetError().c_str());
78 m_controller->AddLog(CEC_LOG_ERROR, strError);
79 return false;
80 }
81
82 m_controller->AddLog(CEC_LOG_DEBUG, "connection opened");
83
84 //clear any input bytes
85 uint8_t buff[1024];
86 m_port->Read(buff, sizeof(buff), 500);
87
88 if (CreateThread())
89 {
90 m_controller->AddLog(CEC_LOG_DEBUG, "communication thread created");
91 return true;
92 }
93 else
94 {
95 m_controller->AddLog(CEC_LOG_DEBUG, "could not create a communication thread");
96 }
97
98 return false;
99 }
100
101 void CAdapterCommunication::Close(void)
102 {
103 StopThread();
104
105 m_rcvCondition.Broadcast();
106 }
107
108 void *CAdapterCommunication::Process(void)
109 {
110 m_controller->AddLog(CEC_LOG_DEBUG, "communication thread started");
111
112 while (!IsStopped())
113 {
114 ReadFromDevice(500);
115 WriteNextCommand();
116 Sleep(5);
117 }
118
119 return NULL;
120 }
121
122 bool CAdapterCommunication::ReadFromDevice(uint32_t iTimeout)
123 {
124 int32_t iBytesRead;
125 uint8_t buff[1024];
126 if (!m_port)
127 return false;
128
129 iBytesRead = m_port->Read(buff, sizeof(buff), iTimeout);
130 if (iBytesRead < 0 || iBytesRead > 256)
131 {
132 CStdString strError;
133 strError.Format("error reading from serial port: %s", m_port->GetError().c_str());
134 m_controller->AddLog(CEC_LOG_ERROR, strError);
135 return false;
136 }
137 else if (iBytesRead > 0)
138 AddData(buff, (uint8_t) iBytesRead);
139
140 return iBytesRead > 0;
141 }
142
143 void CAdapterCommunication::AddData(uint8_t *data, uint8_t iLen)
144 {
145 CLockObject lock(&m_bufferMutex);
146 for (unsigned int iPtr = 0; iPtr < iLen; iPtr++)
147 m_inBuffer.Push(data[iPtr]);
148
149 m_rcvCondition.Signal();
150 }
151
152 void CAdapterCommunication::WriteNextCommand(void)
153 {
154 CCECAdapterMessage msg;
155 if (m_outBuffer.Pop(msg))
156 {
157 if (m_port->Write(msg) != (int32_t) msg.size())
158 {
159 CStdString strError;
160 strError.Format("error writing to serial port: %s", m_port->GetError().c_str());
161 m_controller->AddLog(CEC_LOG_ERROR, strError);
162 }
163 else
164 {
165 m_controller->AddLog(CEC_LOG_DEBUG, "command sent");
166 CCondition::Sleep((uint32_t) msg.size() * (uint32_t)24 /*data*/ + (uint32_t)5 /*start bit (4.5 ms)*/);
167 }
168 }
169 }
170
171 bool CAdapterCommunication::Write(const CCECAdapterMessage &data)
172 {
173 m_outBuffer.Push(data);
174 return true;
175 }
176
177 bool CAdapterCommunication::Read(CCECAdapterMessage &msg, uint32_t iTimeout)
178 {
179 CLockObject lock(&m_bufferMutex);
180
181 msg.clear();
182 uint64_t iNow = GetTimeMs();
183 uint64_t iTarget = iNow + iTimeout;
184 bool bGotFullMessage(false);
185 bool bNextIsEscaped(false);
186 bool bGotStart(false);
187
188 while(!bGotFullMessage && iNow < iTarget)
189 {
190 uint8_t buf = 0;
191 if (!m_inBuffer.Pop(buf))
192 {
193 if (!m_rcvCondition.Wait(&m_bufferMutex, iTarget - iNow))
194 return false;
195 }
196
197 if (!bGotStart)
198 {
199 if (buf == MSGSTART)
200 bGotStart = true;
201 continue;
202 }
203 else if (buf == MSGSTART) //we found a msgstart before msgend, this is not right, remove
204 {
205 m_controller->AddLog(CEC_LOG_ERROR, "received MSGSTART before MSGEND");
206 msg.clear();
207 bGotStart = true;
208 }
209
210 if (buf == MSGEND)
211 {
212 bGotFullMessage = true;
213 }
214 else if (bNextIsEscaped)
215 {
216 msg.push_back(buf + (uint8_t)ESCOFFSET);
217 bNextIsEscaped = false;
218 }
219 else if (buf == MSGESC)
220 bNextIsEscaped = true;
221 else
222 msg.push_back(buf);
223 }
224
225 return bGotFullMessage;
226 }
227
228 std::string CAdapterCommunication::GetError(void) const
229 {
230 return m_port->GetError();
231 }
232
233 bool CAdapterCommunication::StartBootloader(void)
234 {
235 if (!IsRunning())
236 return false;
237
238 m_controller->AddLog(CEC_LOG_DEBUG, "starting the bootloader");
239 CCECAdapterMessage output;
240
241 output.push_back(MSGSTART);
242 output.push_escaped(MSGCODE_START_BOOTLOADER);
243 output.push_back(MSGEND);
244
245 if (!Write(output))
246 {
247 m_controller->AddLog(CEC_LOG_ERROR, "could not start the bootloader");
248 return false;
249 }
250 m_controller->AddLog(CEC_LOG_DEBUG, "bootloader start command transmitted");
251 return true;
252 }
253
254 bool CAdapterCommunication::SetAckMask(uint16_t iMask)
255 {
256 if (!IsRunning())
257 return false;
258
259 CStdString strLog;
260 strLog.Format("setting ackmask to %2x", iMask);
261 m_controller->AddLog(CEC_LOG_DEBUG, strLog.c_str());
262
263 CCECAdapterMessage output;
264
265 output.push_back(MSGSTART);
266 output.push_escaped(MSGCODE_SET_ACK_MASK);
267 output.push_escaped(iMask >> 8);
268 output.push_escaped((uint8_t)iMask);
269 output.push_back(MSGEND);
270
271 if (!Write(output))
272 {
273 m_controller->AddLog(CEC_LOG_ERROR, "could not set the ackmask");
274 return false;
275 }
276
277 return true;
278 }
279
280 bool CAdapterCommunication::PingAdapter(void)
281 {
282 if (!IsRunning())
283 return false;
284
285 m_controller->AddLog(CEC_LOG_DEBUG, "sending ping");
286 CCECAdapterMessage output;
287
288 output.push_back(MSGSTART);
289 output.push_escaped(MSGCODE_PING);
290 output.push_back(MSGEND);
291
292 if (!Write(output))
293 {
294 m_controller->AddLog(CEC_LOG_ERROR, "could not send ping command");
295 return false;
296 }
297
298 m_controller->AddLog(CEC_LOG_DEBUG, "ping tranmitted");
299
300 // TODO check for pong
301 return true;
302 }
303
304 bool CAdapterCommunication::IsOpen(void) const
305 {
306 return !IsStopped() && m_port->IsOpen();
307 }