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 | ||
ad24cbaf LOK |
43 | CCECAdapterMessage::CCECAdapterMessage(const cec_command &command) |
44 | { | |
45 | clear(); | |
46 | ||
47 | //set ack polarity to high when transmitting to the broadcast address | |
48 | //set ack polarity low when transmitting to any other address | |
49 | push_back(MSGSTART); | |
50 | push_escaped(MSGCODE_TRANSMIT_ACK_POLARITY); | |
51 | if (command.destination == CECDEVICE_BROADCAST) | |
52 | push_escaped(CEC_TRUE); | |
53 | else | |
54 | push_escaped(CEC_FALSE); | |
55 | push_back(MSGEND); | |
56 | ||
57 | // add source and destination | |
58 | push_back(MSGSTART); | |
59 | push_escaped(MSGCODE_TRANSMIT); | |
60 | push_back(((uint8_t)command.initiator << 4) + (uint8_t)command.destination); | |
61 | push_back(MSGEND); | |
62 | ||
63 | // add opcode | |
64 | push_back(MSGSTART); | |
65 | push_escaped(command.parameters.empty() ? (uint8_t)MSGCODE_TRANSMIT_EOM : (uint8_t)MSGCODE_TRANSMIT); | |
66 | push_back((uint8_t) command.opcode); | |
67 | push_back(MSGEND); | |
68 | ||
69 | // add parameters | |
70 | for (int8_t iPtr = 0; iPtr < command.parameters.size; iPtr++) | |
71 | { | |
72 | push_back(MSGSTART); | |
73 | ||
74 | if (iPtr == command.parameters.size - 1) | |
75 | push_escaped( MSGCODE_TRANSMIT_EOM); | |
76 | else | |
77 | push_escaped(MSGCODE_TRANSMIT); | |
78 | ||
79 | push_escaped(command.parameters[iPtr]); | |
80 | ||
81 | push_back(MSGEND); | |
82 | } | |
83 | } | |
84 | ||
85 | CCECAdapterMessage &CCECAdapterMessage::operator =(const CCECAdapterMessage &msg) | |
86 | { | |
87 | packet = msg.packet; | |
88 | return *this; | |
89 | } | |
90 | ||
91 | void CCECAdapterMessage::push_escaped(int16_t byte) | |
92 | { | |
93 | if (byte >= MSGESC && byte != MSGSTART) | |
94 | { | |
95 | push_back(MSGESC); | |
96 | push_back(byte - ESCOFFSET); | |
97 | } | |
98 | else | |
99 | push_back(byte); | |
100 | } | |
101 | ||
2abe74eb | 102 | CAdapterCommunication::CAdapterCommunication(CLibCEC *controller) : |
12027dbe | 103 | m_port(NULL), |
50aa01e6 | 104 | m_controller(controller) |
a8f0bd18 LOK |
105 | { |
106 | m_port = new CSerialPort; | |
107 | } | |
108 | ||
828682d3 | 109 | CAdapterCommunication::~CAdapterCommunication(void) |
a8f0bd18 | 110 | { |
12027dbe LOK |
111 | Close(); |
112 | ||
113 | if (m_port) | |
114 | { | |
115 | delete m_port; | |
116 | m_port = NULL; | |
117 | } | |
a8f0bd18 LOK |
118 | } |
119 | ||
25701fa6 | 120 | bool CAdapterCommunication::Open(const char *strPort, uint16_t iBaudRate /* = 38400 */, uint32_t iTimeoutMs /* = 10000 */) |
a8f0bd18 | 121 | { |
f077e8ed | 122 | CLockObject lock(&m_mutex); |
13fd6a66 LOK |
123 | if (!m_port) |
124 | { | |
125 | m_controller->AddLog(CEC_LOG_ERROR, "port is NULL"); | |
a8f0bd18 | 126 | return false; |
13fd6a66 LOK |
127 | } |
128 | ||
129 | if (IsOpen()) | |
130 | { | |
131 | m_controller->AddLog(CEC_LOG_ERROR, "port is already open"); | |
132 | } | |
a8f0bd18 LOK |
133 | |
134 | if (!m_port->Open(strPort, iBaudRate)) | |
135 | { | |
136 | CStdString strError; | |
137 | strError.Format("error opening serial port '%s': %s", strPort, m_port->GetError().c_str()); | |
2abe74eb | 138 | m_controller->AddLog(CEC_LOG_ERROR, strError); |
a8f0bd18 LOK |
139 | return false; |
140 | } | |
141 | ||
2abe74eb | 142 | m_controller->AddLog(CEC_LOG_DEBUG, "connection opened"); |
a8f0bd18 LOK |
143 | |
144 | //clear any input bytes | |
145 | uint8_t buff[1024]; | |
59c9774c | 146 | m_port->Read(buff, sizeof(buff), 500); |
a8f0bd18 | 147 | |
828682d3 | 148 | if (CreateThread()) |
a8f0bd18 | 149 | { |
13fd6a66 | 150 | m_controller->AddLog(CEC_LOG_DEBUG, "communication thread created"); |
a8f0bd18 LOK |
151 | return true; |
152 | } | |
153 | else | |
154 | { | |
13fd6a66 | 155 | m_controller->AddLog(CEC_LOG_DEBUG, "could not create a communication thread"); |
a8f0bd18 LOK |
156 | } |
157 | ||
158 | return false; | |
159 | } | |
160 | ||
828682d3 | 161 | void CAdapterCommunication::Close(void) |
a8f0bd18 | 162 | { |
f077e8ed | 163 | CLockObject lock(&m_mutex); |
828682d3 | 164 | StopThread(); |
25701fa6 | 165 | |
d5bffd3c | 166 | m_rcvCondition.Broadcast(); |
a8f0bd18 LOK |
167 | } |
168 | ||
828682d3 | 169 | void *CAdapterCommunication::Process(void) |
a8f0bd18 | 170 | { |
12027dbe LOK |
171 | m_controller->AddLog(CEC_LOG_DEBUG, "communication thread started"); |
172 | ||
13fd6a66 | 173 | while (!IsStopped()) |
a8f0bd18 | 174 | { |
b9abf920 | 175 | ReadFromDevice(500); |
3c53ac93 LOK |
176 | WriteNextCommand(); |
177 | Sleep(5); | |
a8f0bd18 LOK |
178 | } |
179 | ||
a8f0bd18 LOK |
180 | return NULL; |
181 | } | |
182 | ||
25701fa6 | 183 | bool CAdapterCommunication::ReadFromDevice(uint32_t iTimeout) |
a8f0bd18 | 184 | { |
25701fa6 | 185 | int32_t iBytesRead; |
13fd6a66 LOK |
186 | uint8_t buff[1024]; |
187 | if (!m_port) | |
188 | return false; | |
b6c82769 | 189 | |
13fd6a66 LOK |
190 | iBytesRead = m_port->Read(buff, sizeof(buff), iTimeout); |
191 | if (iBytesRead < 0 || iBytesRead > 256) | |
a8f0bd18 | 192 | { |
13fd6a66 LOK |
193 | CStdString strError; |
194 | strError.Format("error reading from serial port: %s", m_port->GetError().c_str()); | |
195 | m_controller->AddLog(CEC_LOG_ERROR, strError); | |
13fd6a66 | 196 | return false; |
a8f0bd18 | 197 | } |
13fd6a66 LOK |
198 | else if (iBytesRead > 0) |
199 | AddData(buff, (uint8_t) iBytesRead); | |
25701fa6 | 200 | |
13fd6a66 | 201 | return iBytesRead > 0; |
a8f0bd18 LOK |
202 | } |
203 | ||
8bca69de | 204 | void CAdapterCommunication::AddData(uint8_t *data, uint8_t iLen) |
a8f0bd18 | 205 | { |
f077e8ed | 206 | CLockObject lock(&m_mutex); |
50aa01e6 LOK |
207 | for (unsigned int iPtr = 0; iPtr < iLen; iPtr++) |
208 | m_inBuffer.Push(data[iPtr]); | |
d5bffd3c LOK |
209 | |
210 | m_rcvCondition.Signal(); | |
a8f0bd18 LOK |
211 | } |
212 | ||
3c53ac93 | 213 | void CAdapterCommunication::WriteNextCommand(void) |
a8f0bd18 | 214 | { |
5a1e87c8 | 215 | CCECAdapterMessagePtr msg; |
3c53ac93 | 216 | if (m_outBuffer.Pop(msg)) |
a8f0bd18 | 217 | { |
5a1e87c8 | 218 | if (m_port->Write(msg) != (int32_t) msg.get()->size()) |
3c53ac93 LOK |
219 | { |
220 | CStdString strError; | |
221 | strError.Format("error writing to serial port: %s", m_port->GetError().c_str()); | |
222 | m_controller->AddLog(CEC_LOG_ERROR, strError); | |
223 | } | |
224 | else | |
225 | { | |
226 | m_controller->AddLog(CEC_LOG_DEBUG, "command sent"); | |
5a1e87c8 | 227 | CCondition::Sleep((uint32_t) msg.get()->size() * (uint32_t)24 /*data*/ + (uint32_t)5 /*start bit (4.5 ms)*/); |
3c53ac93 | 228 | } |
25701fa6 | 229 | } |
3c53ac93 | 230 | } |
2abe74eb | 231 | |
5a1e87c8 | 232 | bool CAdapterCommunication::Write(CCECAdapterMessagePtr data) |
3c53ac93 LOK |
233 | { |
234 | m_outBuffer.Push(data); | |
a8f0bd18 LOK |
235 | return true; |
236 | } | |
237 | ||
220537f2 | 238 | bool CAdapterCommunication::Read(CCECAdapterMessage &msg, uint32_t iTimeout) |
a8f0bd18 | 239 | { |
f077e8ed | 240 | CLockObject lock(&m_mutex); |
a8f0bd18 | 241 | |
50aa01e6 LOK |
242 | msg.clear(); |
243 | uint64_t iNow = GetTimeMs(); | |
244 | uint64_t iTarget = iNow + iTimeout; | |
245 | bool bGotFullMessage(false); | |
246 | bool bNextIsEscaped(false); | |
247 | bool bGotStart(false); | |
a8f0bd18 | 248 | |
50aa01e6 | 249 | while(!bGotFullMessage && iNow < iTarget) |
a8f0bd18 | 250 | { |
50aa01e6 LOK |
251 | uint8_t buf = 0; |
252 | if (!m_inBuffer.Pop(buf)) | |
a8f0bd18 | 253 | { |
f077e8ed | 254 | if (!m_rcvCondition.Wait(&m_mutex, iTarget - iNow)) |
50aa01e6 | 255 | return false; |
a8f0bd18 | 256 | } |
a8f0bd18 | 257 | |
50aa01e6 | 258 | if (!bGotStart) |
a8f0bd18 | 259 | { |
50aa01e6 LOK |
260 | if (buf == MSGSTART) |
261 | bGotStart = true; | |
262 | continue; | |
a8f0bd18 | 263 | } |
50aa01e6 | 264 | else if (buf == MSGSTART) //we found a msgstart before msgend, this is not right, remove |
a8f0bd18 | 265 | { |
50aa01e6 LOK |
266 | m_controller->AddLog(CEC_LOG_ERROR, "received MSGSTART before MSGEND"); |
267 | msg.clear(); | |
268 | bGotStart = true; | |
a8f0bd18 | 269 | } |
a8f0bd18 | 270 | |
50aa01e6 | 271 | if (buf == MSGEND) |
a8f0bd18 | 272 | { |
50aa01e6 | 273 | bGotFullMessage = true; |
a8f0bd18 | 274 | } |
50aa01e6 LOK |
275 | else if (bNextIsEscaped) |
276 | { | |
277 | msg.push_back(buf + (uint8_t)ESCOFFSET); | |
278 | bNextIsEscaped = false; | |
279 | } | |
280 | else if (buf == MSGESC) | |
281 | bNextIsEscaped = true; | |
282 | else | |
283 | msg.push_back(buf); | |
a8f0bd18 LOK |
284 | } |
285 | ||
50aa01e6 | 286 | return bGotFullMessage; |
a8f0bd18 LOK |
287 | } |
288 | ||
828682d3 | 289 | std::string CAdapterCommunication::GetError(void) const |
a8f0bd18 LOK |
290 | { |
291 | return m_port->GetError(); | |
292 | } | |
2abe74eb LOK |
293 | |
294 | bool CAdapterCommunication::StartBootloader(void) | |
295 | { | |
296 | if (!IsRunning()) | |
297 | return false; | |
298 | ||
299 | m_controller->AddLog(CEC_LOG_DEBUG, "starting the bootloader"); | |
5a1e87c8 | 300 | CCECAdapterMessagePtr output(new CCECAdapterMessage); |
25701fa6 | 301 | |
5a1e87c8 LOK |
302 | output->push_back(MSGSTART); |
303 | output->push_escaped(MSGCODE_START_BOOTLOADER); | |
304 | output->push_back(MSGEND); | |
2abe74eb LOK |
305 | |
306 | if (!Write(output)) | |
307 | { | |
308 | m_controller->AddLog(CEC_LOG_ERROR, "could not start the bootloader"); | |
309 | return false; | |
310 | } | |
311 | m_controller->AddLog(CEC_LOG_DEBUG, "bootloader start command transmitted"); | |
312 | return true; | |
313 | } | |
314 | ||
2abe74eb LOK |
315 | bool CAdapterCommunication::SetAckMask(uint16_t iMask) |
316 | { | |
317 | if (!IsRunning()) | |
318 | return false; | |
319 | ||
320 | CStdString strLog; | |
321 | strLog.Format("setting ackmask to %2x", iMask); | |
322 | m_controller->AddLog(CEC_LOG_DEBUG, strLog.c_str()); | |
323 | ||
5a1e87c8 | 324 | CCECAdapterMessagePtr output(new CCECAdapterMessage); |
2abe74eb | 325 | |
5a1e87c8 LOK |
326 | output->push_back(MSGSTART); |
327 | output->push_escaped(MSGCODE_SET_ACK_MASK); | |
328 | output->push_escaped(iMask >> 8); | |
329 | output->push_escaped((uint8_t)iMask); | |
330 | output->push_back(MSGEND); | |
2abe74eb LOK |
331 | |
332 | if (!Write(output)) | |
333 | { | |
334 | m_controller->AddLog(CEC_LOG_ERROR, "could not set the ackmask"); | |
335 | return false; | |
336 | } | |
337 | ||
338 | return true; | |
339 | } | |
340 | ||
341 | bool CAdapterCommunication::PingAdapter(void) | |
342 | { | |
343 | if (!IsRunning()) | |
344 | return false; | |
345 | ||
346 | m_controller->AddLog(CEC_LOG_DEBUG, "sending ping"); | |
5a1e87c8 | 347 | CCECAdapterMessagePtr output(new CCECAdapterMessage); |
25701fa6 | 348 | |
5a1e87c8 LOK |
349 | output->push_back(MSGSTART); |
350 | output->push_escaped(MSGCODE_PING); | |
351 | output->push_back(MSGEND); | |
2abe74eb LOK |
352 | |
353 | if (!Write(output)) | |
354 | { | |
355 | m_controller->AddLog(CEC_LOG_ERROR, "could not send ping command"); | |
356 | return false; | |
357 | } | |
358 | ||
359 | m_controller->AddLog(CEC_LOG_DEBUG, "ping tranmitted"); | |
360 | ||
361 | // TODO check for pong | |
362 | return true; | |
363 | } | |
13fd6a66 LOK |
364 | |
365 | bool CAdapterCommunication::IsOpen(void) const | |
366 | { | |
367 | return !IsStopped() && m_port->IsOpen(); | |
368 | } |