Commit | Line | Data |
---|---|---|
a8f0bd18 LOK |
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 | ||
828682d3 | 33 | #include "AdapterCommunication.h" |
2abe74eb | 34 | |
ef7696f5 | 35 | #include "AdapterMessage.h" |
1113cb7d | 36 | #include "CECProcessor.h" |
f00ff009 | 37 | #include "platform/serialport/serialport.h" |
a8f0bd18 LOK |
38 | |
39 | using namespace std; | |
40 | using namespace CEC; | |
f00ff009 | 41 | using namespace PLATFORM; |
a8f0bd18 | 42 | |
1113cb7d | 43 | CAdapterCommunication::CAdapterCommunication(CCECProcessor *processor) : |
12027dbe | 44 | m_port(NULL), |
a171d2fd LOK |
45 | m_processor(processor), |
46 | m_iLineTimeout(0) | |
a8f0bd18 | 47 | { |
f00ff009 | 48 | m_port = new PLATFORM::CSerialPort; |
a8f0bd18 LOK |
49 | } |
50 | ||
828682d3 | 51 | CAdapterCommunication::~CAdapterCommunication(void) |
a8f0bd18 | 52 | { |
12027dbe LOK |
53 | Close(); |
54 | ||
55 | if (m_port) | |
56 | { | |
57 | delete m_port; | |
58 | m_port = NULL; | |
59 | } | |
a8f0bd18 LOK |
60 | } |
61 | ||
25701fa6 | 62 | bool CAdapterCommunication::Open(const char *strPort, uint16_t iBaudRate /* = 38400 */, uint32_t iTimeoutMs /* = 10000 */) |
a8f0bd18 | 63 | { |
7b494bea LOK |
64 | uint64_t iNow = GetTimeMs(); |
65 | uint64_t iTimeout = iNow + iTimeoutMs; | |
66 | ||
f00ff009 | 67 | CLockObject lock(m_mutex); |
7b494bea | 68 | |
13fd6a66 LOK |
69 | if (!m_port) |
70 | { | |
1113cb7d | 71 | m_processor->AddLog(CEC_LOG_ERROR, "port is NULL"); |
a8f0bd18 | 72 | return false; |
13fd6a66 LOK |
73 | } |
74 | ||
75 | if (IsOpen()) | |
76 | { | |
1113cb7d | 77 | m_processor->AddLog(CEC_LOG_ERROR, "port is already open"); |
7b494bea | 78 | return true; |
13fd6a66 | 79 | } |
a8f0bd18 | 80 | |
7b494bea LOK |
81 | CStdString strError; |
82 | bool bConnected(false); | |
83 | while (!bConnected && iNow < iTimeout) | |
84 | { | |
d956c02e | 85 | if ((bConnected = m_port->Open(strPort, iBaudRate)) == false) |
7b494bea LOK |
86 | { |
87 | strError.Format("error opening serial port '%s': %s", strPort, m_port->GetError().c_str()); | |
88 | Sleep(250); | |
89 | iNow = GetTimeMs(); | |
90 | } | |
91 | } | |
92 | ||
93 | if (!bConnected) | |
a8f0bd18 | 94 | { |
1113cb7d | 95 | m_processor->AddLog(CEC_LOG_ERROR, strError); |
a8f0bd18 LOK |
96 | return false; |
97 | } | |
98 | ||
1113cb7d | 99 | m_processor->AddLog(CEC_LOG_DEBUG, "connection opened"); |
a8f0bd18 LOK |
100 | |
101 | //clear any input bytes | |
aee82ba0 LOK |
102 | uint8_t buff[1]; |
103 | while (m_port->Read(buff, 1, 5) == 1) {} | |
a8f0bd18 | 104 | |
828682d3 | 105 | if (CreateThread()) |
a8f0bd18 | 106 | { |
1113cb7d | 107 | m_processor->AddLog(CEC_LOG_DEBUG, "communication thread started"); |
a8f0bd18 LOK |
108 | return true; |
109 | } | |
110 | else | |
111 | { | |
ef7696f5 | 112 | m_processor->AddLog(CEC_LOG_ERROR, "could not create a communication thread"); |
a8f0bd18 LOK |
113 | } |
114 | ||
115 | return false; | |
116 | } | |
117 | ||
828682d3 | 118 | void CAdapterCommunication::Close(void) |
a8f0bd18 | 119 | { |
f00ff009 | 120 | CLockObject lock(m_mutex); |
d5bffd3c | 121 | m_rcvCondition.Broadcast(); |
b9eea66d | 122 | StopThread(); |
a8f0bd18 LOK |
123 | } |
124 | ||
828682d3 | 125 | void *CAdapterCommunication::Process(void) |
a8f0bd18 | 126 | { |
13fd6a66 | 127 | while (!IsStopped()) |
a8f0bd18 | 128 | { |
9e1cfa5c | 129 | ReadFromDevice(50); |
c02980af | 130 | Sleep(5); |
3c53ac93 | 131 | WriteNextCommand(); |
a8f0bd18 LOK |
132 | } |
133 | ||
ef7696f5 | 134 | CCECAdapterMessage *msg(NULL); |
a0878ee3 LOK |
135 | if (m_outBuffer.Pop(msg)) |
136 | msg->condition.Broadcast(); | |
137 | ||
a8f0bd18 LOK |
138 | return NULL; |
139 | } | |
140 | ||
28352a04 | 141 | bool CAdapterCommunication::Write(CCECAdapterMessage *data) |
3c53ac93 | 142 | { |
0e31a62c | 143 | data->state = ADAPTER_MESSAGE_STATE_WAITING; |
3c53ac93 | 144 | m_outBuffer.Push(data); |
a8f0bd18 LOK |
145 | return true; |
146 | } | |
147 | ||
220537f2 | 148 | bool CAdapterCommunication::Read(CCECAdapterMessage &msg, uint32_t iTimeout) |
a8f0bd18 | 149 | { |
f00ff009 | 150 | CLockObject lock(m_mutex); |
a8f0bd18 | 151 | |
ef7696f5 | 152 | msg.Clear(); |
50aa01e6 LOK |
153 | uint64_t iNow = GetTimeMs(); |
154 | uint64_t iTarget = iNow + iTimeout; | |
155 | bool bGotFullMessage(false); | |
156 | bool bNextIsEscaped(false); | |
157 | bool bGotStart(false); | |
a8f0bd18 | 158 | |
50aa01e6 | 159 | while(!bGotFullMessage && iNow < iTarget) |
a8f0bd18 | 160 | { |
50aa01e6 LOK |
161 | uint8_t buf = 0; |
162 | if (!m_inBuffer.Pop(buf)) | |
a8f0bd18 | 163 | { |
f00ff009 | 164 | if (!m_rcvCondition.Wait(m_mutex, (uint32_t) (iTarget - iNow))) |
50aa01e6 | 165 | return false; |
a8f0bd18 | 166 | } |
a8f0bd18 | 167 | |
50aa01e6 | 168 | if (!bGotStart) |
a8f0bd18 | 169 | { |
50aa01e6 LOK |
170 | if (buf == MSGSTART) |
171 | bGotStart = true; | |
172 | continue; | |
a8f0bd18 | 173 | } |
50aa01e6 | 174 | else if (buf == MSGSTART) //we found a msgstart before msgend, this is not right, remove |
a8f0bd18 | 175 | { |
ef7696f5 | 176 | if (msg.Size() > 0) |
1113cb7d | 177 | m_processor->AddLog(CEC_LOG_WARNING, "received MSGSTART before MSGEND, removing previous buffer contents"); |
ef7696f5 | 178 | msg.Clear(); |
50aa01e6 | 179 | bGotStart = true; |
a8f0bd18 | 180 | } |
a8f0bd18 | 181 | |
50aa01e6 | 182 | if (buf == MSGEND) |
a8f0bd18 | 183 | { |
50aa01e6 | 184 | bGotFullMessage = true; |
a8f0bd18 | 185 | } |
50aa01e6 LOK |
186 | else if (bNextIsEscaped) |
187 | { | |
ef7696f5 | 188 | msg.PushBack(buf + (uint8_t)ESCOFFSET); |
50aa01e6 LOK |
189 | bNextIsEscaped = false; |
190 | } | |
191 | else if (buf == MSGESC) | |
192 | bNextIsEscaped = true; | |
193 | else | |
ef7696f5 | 194 | msg.PushBack(buf); |
a8f0bd18 LOK |
195 | } |
196 | ||
0e31a62c LOK |
197 | if (bGotFullMessage) |
198 | msg.state = ADAPTER_MESSAGE_STATE_RECEIVED; | |
199 | ||
50aa01e6 | 200 | return bGotFullMessage; |
a8f0bd18 LOK |
201 | } |
202 | ||
828682d3 | 203 | std::string CAdapterCommunication::GetError(void) const |
a8f0bd18 LOK |
204 | { |
205 | return m_port->GetError(); | |
206 | } | |
2abe74eb LOK |
207 | |
208 | bool CAdapterCommunication::StartBootloader(void) | |
209 | { | |
28352a04 | 210 | bool bReturn(false); |
2abe74eb | 211 | if (!IsRunning()) |
28352a04 | 212 | return bReturn; |
2abe74eb | 213 | |
1113cb7d | 214 | m_processor->AddLog(CEC_LOG_DEBUG, "starting the bootloader"); |
28352a04 | 215 | CCECAdapterMessage *output = new CCECAdapterMessage; |
25701fa6 | 216 | |
ef7696f5 LOK |
217 | output->PushBack(MSGSTART); |
218 | output->PushEscaped(MSGCODE_START_BOOTLOADER); | |
219 | output->PushBack(MSGEND); | |
2abe74eb | 220 | |
f00ff009 | 221 | CLockObject lock(output->mutex); |
802b7a0f | 222 | if (Write(output)) |
f00ff009 | 223 | output->condition.Wait(output->mutex); |
a96eb42e | 224 | bReturn = output->state == ADAPTER_MESSAGE_STATE_SENT; |
28352a04 LOK |
225 | delete output; |
226 | ||
227 | return bReturn; | |
2abe74eb LOK |
228 | } |
229 | ||
2abe74eb LOK |
230 | bool CAdapterCommunication::PingAdapter(void) |
231 | { | |
28352a04 | 232 | bool bReturn(false); |
2abe74eb | 233 | if (!IsRunning()) |
28352a04 | 234 | return bReturn; |
2abe74eb | 235 | |
1113cb7d | 236 | m_processor->AddLog(CEC_LOG_DEBUG, "sending ping"); |
28352a04 | 237 | CCECAdapterMessage *output = new CCECAdapterMessage; |
25701fa6 | 238 | |
ef7696f5 LOK |
239 | output->PushBack(MSGSTART); |
240 | output->PushEscaped(MSGCODE_PING); | |
241 | output->PushBack(MSGEND); | |
2abe74eb | 242 | |
f00ff009 | 243 | CLockObject lock(output->mutex); |
802b7a0f | 244 | if (Write(output)) |
f00ff009 | 245 | output->condition.Wait(output->mutex); |
a96eb42e | 246 | bReturn = output->state == ADAPTER_MESSAGE_STATE_SENT; |
28352a04 LOK |
247 | delete output; |
248 | ||
249 | return bReturn; | |
2abe74eb | 250 | } |
13fd6a66 | 251 | |
a171d2fd LOK |
252 | bool CAdapterCommunication::SetLineTimeout(uint8_t iTimeout) |
253 | { | |
254 | bool bReturn(m_iLineTimeout != iTimeout); | |
255 | ||
256 | if (!bReturn) | |
257 | { | |
258 | CCECAdapterMessage *output = new CCECAdapterMessage; | |
259 | ||
ef7696f5 LOK |
260 | output->PushBack(MSGSTART); |
261 | output->PushEscaped(MSGCODE_TRANSMIT_IDLETIME); | |
262 | output->PushEscaped(iTimeout); | |
263 | output->PushBack(MSGEND); | |
a171d2fd LOK |
264 | |
265 | if ((bReturn = Write(output)) == false) | |
266 | m_processor->AddLog(CEC_LOG_ERROR, "could not set the idletime"); | |
267 | delete output; | |
268 | } | |
269 | ||
270 | return bReturn; | |
271 | } | |
272 | ||
f00ff009 | 273 | bool CAdapterCommunication::IsOpen(void) |
13fd6a66 | 274 | { |
b9eea66d | 275 | return !IsStopped() && m_port->IsOpen() && IsRunning(); |
13fd6a66 | 276 | } |
ef7696f5 LOK |
277 | |
278 | void CAdapterCommunication::AddData(uint8_t *data, uint8_t iLen) | |
279 | { | |
280 | CLockObject lock(m_mutex); | |
281 | for (uint8_t iPtr = 0; iPtr < iLen; iPtr++) | |
282 | m_inBuffer.Push(data[iPtr]); | |
283 | ||
284 | m_rcvCondition.Signal(); | |
285 | } | |
286 | ||
287 | bool CAdapterCommunication::ReadFromDevice(uint32_t iTimeout) | |
288 | { | |
289 | int32_t iBytesRead; | |
290 | uint8_t buff[1024]; | |
291 | if (!m_port) | |
292 | return false; | |
293 | ||
294 | iBytesRead = m_port->Read(buff, sizeof(buff), iTimeout); | |
295 | if (iBytesRead < 0 || iBytesRead > 256) | |
296 | { | |
297 | CStdString strError; | |
298 | strError.Format("error reading from serial port: %s", m_port->GetError().c_str()); | |
299 | m_processor->AddLog(CEC_LOG_ERROR, strError); | |
300 | return false; | |
301 | } | |
302 | else if (iBytesRead > 0) | |
303 | AddData(buff, (uint8_t) iBytesRead); | |
304 | ||
305 | return iBytesRead > 0; | |
306 | } | |
307 | ||
308 | void CAdapterCommunication::SendMessageToAdapter(CCECAdapterMessage *msg) | |
309 | { | |
310 | CLockObject lock(msg->mutex); | |
311 | if (m_port->Write(msg->packet.data, msg->Size()) != (int32_t) msg->Size()) | |
312 | { | |
313 | CStdString strError; | |
314 | strError.Format("error writing to serial port: %s", m_port->GetError().c_str()); | |
315 | m_processor->AddLog(CEC_LOG_ERROR, strError); | |
316 | msg->state = ADAPTER_MESSAGE_STATE_ERROR; | |
317 | } | |
318 | else | |
319 | { | |
320 | m_processor->AddLog(CEC_LOG_DEBUG, "command sent"); | |
321 | msg->state = ADAPTER_MESSAGE_STATE_SENT; | |
322 | } | |
323 | msg->condition.Signal(); | |
324 | } | |
325 | ||
326 | void CAdapterCommunication::WriteNextCommand(void) | |
327 | { | |
328 | CCECAdapterMessage *msg(NULL); | |
329 | if (m_outBuffer.Pop(msg)) | |
330 | SendMessageToAdapter(msg); | |
331 | } |