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 LOK |
34 | |
35 | #include "LibCEC.h" | |
b9187cc6 | 36 | #include "platform/serialport.h" |
a8f0bd18 | 37 | #include "util/StdString.h" |
50aa01e6 | 38 | #include "platform/timeutils.h" |
a8f0bd18 LOK |
39 | |
40 | using namespace std; | |
41 | using namespace CEC; | |
42 | ||
2abe74eb | 43 | CAdapterCommunication::CAdapterCommunication(CLibCEC *controller) : |
12027dbe | 44 | m_port(NULL), |
50aa01e6 | 45 | m_controller(controller) |
a8f0bd18 LOK |
46 | { |
47 | m_port = new CSerialPort; | |
48 | } | |
49 | ||
828682d3 | 50 | CAdapterCommunication::~CAdapterCommunication(void) |
a8f0bd18 | 51 | { |
12027dbe LOK |
52 | Close(); |
53 | ||
54 | if (m_port) | |
55 | { | |
56 | delete m_port; | |
57 | m_port = NULL; | |
58 | } | |
a8f0bd18 LOK |
59 | } |
60 | ||
25701fa6 | 61 | bool CAdapterCommunication::Open(const char *strPort, uint16_t iBaudRate /* = 38400 */, uint32_t iTimeoutMs /* = 10000 */) |
a8f0bd18 | 62 | { |
13fd6a66 LOK |
63 | if (!m_port) |
64 | { | |
65 | m_controller->AddLog(CEC_LOG_ERROR, "port is NULL"); | |
a8f0bd18 | 66 | return false; |
13fd6a66 LOK |
67 | } |
68 | ||
69 | if (IsOpen()) | |
70 | { | |
71 | m_controller->AddLog(CEC_LOG_ERROR, "port is already open"); | |
72 | } | |
a8f0bd18 LOK |
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()); | |
2abe74eb | 78 | m_controller->AddLog(CEC_LOG_ERROR, strError); |
a8f0bd18 LOK |
79 | return false; |
80 | } | |
81 | ||
2abe74eb | 82 | m_controller->AddLog(CEC_LOG_DEBUG, "connection opened"); |
a8f0bd18 LOK |
83 | |
84 | //clear any input bytes | |
85 | uint8_t buff[1024]; | |
59c9774c | 86 | m_port->Read(buff, sizeof(buff), 500); |
a8f0bd18 | 87 | |
828682d3 | 88 | if (CreateThread()) |
a8f0bd18 | 89 | { |
13fd6a66 | 90 | m_controller->AddLog(CEC_LOG_DEBUG, "communication thread created"); |
a8f0bd18 LOK |
91 | return true; |
92 | } | |
93 | else | |
94 | { | |
13fd6a66 | 95 | m_controller->AddLog(CEC_LOG_DEBUG, "could not create a communication thread"); |
a8f0bd18 LOK |
96 | } |
97 | ||
98 | return false; | |
99 | } | |
100 | ||
828682d3 | 101 | void CAdapterCommunication::Close(void) |
a8f0bd18 | 102 | { |
828682d3 | 103 | StopThread(); |
25701fa6 | 104 | |
d5bffd3c | 105 | m_rcvCondition.Broadcast(); |
a8f0bd18 LOK |
106 | } |
107 | ||
828682d3 | 108 | void *CAdapterCommunication::Process(void) |
a8f0bd18 | 109 | { |
12027dbe LOK |
110 | m_controller->AddLog(CEC_LOG_DEBUG, "communication thread started"); |
111 | ||
13fd6a66 | 112 | while (!IsStopped()) |
a8f0bd18 | 113 | { |
b9abf920 | 114 | ReadFromDevice(500); |
3c53ac93 LOK |
115 | WriteNextCommand(); |
116 | Sleep(5); | |
a8f0bd18 LOK |
117 | } |
118 | ||
a8f0bd18 LOK |
119 | return NULL; |
120 | } | |
121 | ||
25701fa6 | 122 | bool CAdapterCommunication::ReadFromDevice(uint32_t iTimeout) |
a8f0bd18 | 123 | { |
25701fa6 | 124 | int32_t iBytesRead; |
13fd6a66 LOK |
125 | uint8_t buff[1024]; |
126 | if (!m_port) | |
127 | return false; | |
b6c82769 | 128 | |
13fd6a66 LOK |
129 | iBytesRead = m_port->Read(buff, sizeof(buff), iTimeout); |
130 | if (iBytesRead < 0 || iBytesRead > 256) | |
a8f0bd18 | 131 | { |
13fd6a66 LOK |
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); | |
13fd6a66 | 135 | return false; |
a8f0bd18 | 136 | } |
13fd6a66 LOK |
137 | else if (iBytesRead > 0) |
138 | AddData(buff, (uint8_t) iBytesRead); | |
25701fa6 | 139 | |
13fd6a66 | 140 | return iBytesRead > 0; |
a8f0bd18 LOK |
141 | } |
142 | ||
8bca69de | 143 | void CAdapterCommunication::AddData(uint8_t *data, uint8_t iLen) |
a8f0bd18 | 144 | { |
d5bffd3c | 145 | CLockObject lock(&m_bufferMutex); |
50aa01e6 LOK |
146 | for (unsigned int iPtr = 0; iPtr < iLen; iPtr++) |
147 | m_inBuffer.Push(data[iPtr]); | |
d5bffd3c LOK |
148 | |
149 | m_rcvCondition.Signal(); | |
a8f0bd18 LOK |
150 | } |
151 | ||
3c53ac93 | 152 | void CAdapterCommunication::WriteNextCommand(void) |
a8f0bd18 | 153 | { |
220537f2 | 154 | CCECAdapterMessage msg; |
3c53ac93 | 155 | if (m_outBuffer.Pop(msg)) |
a8f0bd18 | 156 | { |
3c53ac93 LOK |
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 | } | |
25701fa6 | 168 | } |
3c53ac93 | 169 | } |
2abe74eb | 170 | |
220537f2 | 171 | bool CAdapterCommunication::Write(const CCECAdapterMessage &data) |
3c53ac93 LOK |
172 | { |
173 | m_outBuffer.Push(data); | |
a8f0bd18 LOK |
174 | return true; |
175 | } | |
176 | ||
220537f2 | 177 | bool CAdapterCommunication::Read(CCECAdapterMessage &msg, uint32_t iTimeout) |
a8f0bd18 | 178 | { |
d5bffd3c | 179 | CLockObject lock(&m_bufferMutex); |
a8f0bd18 | 180 | |
50aa01e6 LOK |
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); | |
a8f0bd18 | 187 | |
50aa01e6 | 188 | while(!bGotFullMessage && iNow < iTarget) |
a8f0bd18 | 189 | { |
50aa01e6 LOK |
190 | uint8_t buf = 0; |
191 | if (!m_inBuffer.Pop(buf)) | |
a8f0bd18 | 192 | { |
50aa01e6 LOK |
193 | if (!m_rcvCondition.Wait(&m_bufferMutex, iTarget - iNow)) |
194 | return false; | |
a8f0bd18 | 195 | } |
a8f0bd18 | 196 | |
50aa01e6 | 197 | if (!bGotStart) |
a8f0bd18 | 198 | { |
50aa01e6 LOK |
199 | if (buf == MSGSTART) |
200 | bGotStart = true; | |
201 | continue; | |
a8f0bd18 | 202 | } |
50aa01e6 | 203 | else if (buf == MSGSTART) //we found a msgstart before msgend, this is not right, remove |
a8f0bd18 | 204 | { |
50aa01e6 LOK |
205 | m_controller->AddLog(CEC_LOG_ERROR, "received MSGSTART before MSGEND"); |
206 | msg.clear(); | |
207 | bGotStart = true; | |
a8f0bd18 | 208 | } |
a8f0bd18 | 209 | |
50aa01e6 | 210 | if (buf == MSGEND) |
a8f0bd18 | 211 | { |
50aa01e6 | 212 | bGotFullMessage = true; |
a8f0bd18 | 213 | } |
50aa01e6 LOK |
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); | |
a8f0bd18 LOK |
223 | } |
224 | ||
50aa01e6 | 225 | return bGotFullMessage; |
a8f0bd18 LOK |
226 | } |
227 | ||
828682d3 | 228 | std::string CAdapterCommunication::GetError(void) const |
a8f0bd18 LOK |
229 | { |
230 | return m_port->GetError(); | |
231 | } | |
2abe74eb LOK |
232 | |
233 | bool CAdapterCommunication::StartBootloader(void) | |
234 | { | |
235 | if (!IsRunning()) | |
236 | return false; | |
237 | ||
238 | m_controller->AddLog(CEC_LOG_DEBUG, "starting the bootloader"); | |
220537f2 | 239 | CCECAdapterMessage output; |
25701fa6 | 240 | |
2abe74eb | 241 | output.push_back(MSGSTART); |
220537f2 | 242 | output.push_escaped(MSGCODE_START_BOOTLOADER); |
2abe74eb LOK |
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 | ||
2abe74eb LOK |
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 | ||
220537f2 | 263 | CCECAdapterMessage output; |
2abe74eb LOK |
264 | |
265 | output.push_back(MSGSTART); | |
220537f2 LOK |
266 | output.push_escaped(MSGCODE_SET_ACK_MASK); |
267 | output.push_escaped(iMask >> 8); | |
268 | output.push_escaped((uint8_t)iMask); | |
2abe74eb LOK |
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"); | |
220537f2 | 286 | CCECAdapterMessage output; |
25701fa6 | 287 | |
2abe74eb | 288 | output.push_back(MSGSTART); |
220537f2 | 289 | output.push_escaped(MSGCODE_PING); |
2abe74eb LOK |
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 | } | |
13fd6a66 LOK |
303 | |
304 | bool CAdapterCommunication::IsOpen(void) const | |
305 | { | |
306 | return !IsStopped() && m_port->IsOpen(); | |
307 | } |