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; | |
88c43521 | 88 | state = msg.state; |
ad24cbaf LOK |
89 | return *this; |
90 | } | |
91 | ||
e41b06f9 LOK |
92 | CStdString CCECAdapterMessage::ToString(void) const |
93 | { | |
94 | CStdString strMsg; | |
95 | switch (message()) | |
96 | { | |
97 | case MSGCODE_NOTHING: | |
98 | strMsg = "NOTHING"; | |
99 | break; | |
100 | case MSGCODE_PING: | |
101 | strMsg = "PING"; | |
102 | break; | |
103 | case MSGCODE_TIMEOUT_ERROR: | |
104 | case MSGCODE_HIGH_ERROR: | |
105 | case MSGCODE_LOW_ERROR: | |
106 | { | |
107 | if (message() == MSGCODE_TIMEOUT_ERROR) | |
108 | strMsg = "TIMEOUT"; | |
109 | else if (message() == MSGCODE_HIGH_ERROR) | |
110 | strMsg = "HIGH_ERROR"; | |
111 | else | |
112 | strMsg = "LOW_ERROR"; | |
113 | ||
114 | int iLine = (size() >= 3) ? (at(1) << 8) | at(2) : 0; | |
115 | uint32_t iTime = (size() >= 7) ? (at(3) << 24) | (at(4) << 16) | (at(5) << 8) | at(6) : 0; | |
116 | strMsg.AppendFormat(" line:%i", iLine); | |
117 | strMsg.AppendFormat(" time:%u", iTime); | |
118 | } | |
119 | break; | |
120 | case MSGCODE_FRAME_START: | |
121 | strMsg = "FRAME_START"; | |
122 | if (size() >= 2) | |
123 | strMsg.AppendFormat(" initiator:%1x destination:%1x ack:%s %s", initiator(), destination(), ack() ? "high" : "low", eom() ? "eom" : ""); | |
124 | break; | |
125 | case MSGCODE_FRAME_DATA: | |
126 | strMsg = "FRAME_DATA"; | |
127 | if (size() >= 2) | |
128 | strMsg.AppendFormat(" %02x %s", at(1), eom() ? "eom" : ""); | |
129 | break; | |
130 | case MSGCODE_RECEIVE_FAILED: | |
131 | strMsg = "RECEIVE_FAILED"; | |
132 | break; | |
133 | case MSGCODE_COMMAND_ACCEPTED: | |
134 | strMsg = "COMMAND_ACCEPTED"; | |
135 | break; | |
136 | case MSGCODE_COMMAND_REJECTED: | |
137 | strMsg = "COMMAND_REJECTED"; | |
138 | break; | |
139 | case MSGCODE_SET_ACK_MASK: | |
140 | strMsg = "SET_ACK_MASK"; | |
141 | break; | |
142 | case MSGCODE_TRANSMIT: | |
143 | strMsg = "TRANSMIT"; | |
144 | break; | |
145 | case MSGCODE_TRANSMIT_EOM: | |
146 | strMsg = "TRANSMIT_EOM"; | |
147 | break; | |
148 | case MSGCODE_TRANSMIT_IDLETIME: | |
149 | strMsg = "TRANSMIT_IDLETIME"; | |
150 | break; | |
151 | case MSGCODE_TRANSMIT_ACK_POLARITY: | |
152 | strMsg = "TRANSMIT_ACK_POLARITY"; | |
153 | break; | |
154 | case MSGCODE_TRANSMIT_LINE_TIMEOUT: | |
155 | strMsg = "TRANSMIT_LINE_TIMEOUT"; | |
156 | break; | |
157 | case MSGCODE_TRANSMIT_SUCCEEDED: | |
158 | strMsg = "TRANSMIT_SUCCEEDED"; | |
159 | break; | |
160 | case MSGCODE_TRANSMIT_FAILED_LINE: | |
161 | strMsg = "TRANSMIT_FAILED_LINE"; | |
162 | break; | |
163 | case MSGCODE_TRANSMIT_FAILED_ACK: | |
164 | strMsg = "TRANSMIT_FAILED_ACK"; | |
165 | break; | |
166 | case MSGCODE_TRANSMIT_FAILED_TIMEOUT_DATA: | |
167 | strMsg = "TRANSMIT_FAILED_TIMEOUT_DATA"; | |
168 | break; | |
169 | case MSGCODE_TRANSMIT_FAILED_TIMEOUT_LINE: | |
170 | strMsg = "TRANSMIT_FAILED_TIMEOUT_LINE"; | |
171 | break; | |
172 | case MSGCODE_FIRMWARE_VERSION: | |
173 | strMsg = "FIRMWARE_VERSION"; | |
174 | break; | |
175 | case MSGCODE_START_BOOTLOADER: | |
176 | strMsg = "START_BOOTLOADER"; | |
177 | break; | |
178 | case MSGCODE_FRAME_EOM: | |
179 | strMsg = "FRAME_EOM"; | |
180 | break; | |
181 | case MSGCODE_FRAME_ACK: | |
182 | strMsg = "FRAME_ACK"; | |
183 | break; | |
184 | } | |
185 | ||
186 | return strMsg; | |
187 | } | |
188 | ||
189 | bool CCECAdapterMessage::is_error(void) const | |
190 | { | |
191 | cec_adapter_messagecode code = message(); | |
192 | return (code == MSGCODE_TIMEOUT_ERROR || | |
193 | code == MSGCODE_HIGH_ERROR || | |
194 | code == MSGCODE_LOW_ERROR || | |
195 | code == MSGCODE_RECEIVE_FAILED || | |
196 | code == MSGCODE_COMMAND_REJECTED || | |
197 | code == MSGCODE_TRANSMIT_LINE_TIMEOUT || | |
198 | code == MSGCODE_TRANSMIT_FAILED_LINE || | |
199 | code == MSGCODE_TRANSMIT_FAILED_ACK || | |
200 | code == MSGCODE_TRANSMIT_FAILED_TIMEOUT_DATA || | |
201 | code == MSGCODE_TRANSMIT_FAILED_TIMEOUT_LINE); | |
202 | } | |
203 | ||
ad24cbaf LOK |
204 | void CCECAdapterMessage::push_escaped(int16_t byte) |
205 | { | |
206 | if (byte >= MSGESC && byte != MSGSTART) | |
207 | { | |
208 | push_back(MSGESC); | |
56701628 | 209 | push_back((uint8_t) (byte - ESCOFFSET)); |
ad24cbaf LOK |
210 | } |
211 | else | |
56701628 | 212 | push_back((uint8_t) byte); |
ad24cbaf LOK |
213 | } |
214 | ||
2abe74eb | 215 | CAdapterCommunication::CAdapterCommunication(CLibCEC *controller) : |
12027dbe | 216 | m_port(NULL), |
50aa01e6 | 217 | m_controller(controller) |
a8f0bd18 LOK |
218 | { |
219 | m_port = new CSerialPort; | |
220 | } | |
221 | ||
828682d3 | 222 | CAdapterCommunication::~CAdapterCommunication(void) |
a8f0bd18 | 223 | { |
12027dbe LOK |
224 | Close(); |
225 | ||
226 | if (m_port) | |
227 | { | |
228 | delete m_port; | |
229 | m_port = NULL; | |
230 | } | |
a8f0bd18 LOK |
231 | } |
232 | ||
25701fa6 | 233 | bool CAdapterCommunication::Open(const char *strPort, uint16_t iBaudRate /* = 38400 */, uint32_t iTimeoutMs /* = 10000 */) |
a8f0bd18 | 234 | { |
f077e8ed | 235 | CLockObject lock(&m_mutex); |
13fd6a66 LOK |
236 | if (!m_port) |
237 | { | |
238 | m_controller->AddLog(CEC_LOG_ERROR, "port is NULL"); | |
a8f0bd18 | 239 | return false; |
13fd6a66 LOK |
240 | } |
241 | ||
242 | if (IsOpen()) | |
243 | { | |
244 | m_controller->AddLog(CEC_LOG_ERROR, "port is already open"); | |
245 | } | |
a8f0bd18 LOK |
246 | |
247 | if (!m_port->Open(strPort, iBaudRate)) | |
248 | { | |
249 | CStdString strError; | |
250 | strError.Format("error opening serial port '%s': %s", strPort, m_port->GetError().c_str()); | |
2abe74eb | 251 | m_controller->AddLog(CEC_LOG_ERROR, strError); |
a8f0bd18 LOK |
252 | return false; |
253 | } | |
254 | ||
2abe74eb | 255 | m_controller->AddLog(CEC_LOG_DEBUG, "connection opened"); |
a8f0bd18 LOK |
256 | |
257 | //clear any input bytes | |
258 | uint8_t buff[1024]; | |
59c9774c | 259 | m_port->Read(buff, sizeof(buff), 500); |
a8f0bd18 | 260 | |
828682d3 | 261 | if (CreateThread()) |
a8f0bd18 | 262 | { |
b9eea66d LOK |
263 | m_startCondition.Wait(&m_mutex); |
264 | m_controller->AddLog(CEC_LOG_DEBUG, "communication thread started"); | |
a8f0bd18 LOK |
265 | return true; |
266 | } | |
267 | else | |
268 | { | |
13fd6a66 | 269 | m_controller->AddLog(CEC_LOG_DEBUG, "could not create a communication thread"); |
a8f0bd18 LOK |
270 | } |
271 | ||
272 | return false; | |
273 | } | |
274 | ||
828682d3 | 275 | void CAdapterCommunication::Close(void) |
a8f0bd18 | 276 | { |
f077e8ed | 277 | CLockObject lock(&m_mutex); |
b9eea66d | 278 | m_startCondition.Broadcast(); |
d5bffd3c | 279 | m_rcvCondition.Broadcast(); |
b9eea66d | 280 | StopThread(); |
a8f0bd18 LOK |
281 | } |
282 | ||
828682d3 | 283 | void *CAdapterCommunication::Process(void) |
a8f0bd18 | 284 | { |
b9eea66d LOK |
285 | { |
286 | CLockObject lock(&m_mutex); | |
287 | m_startCondition.Signal(); | |
288 | } | |
12027dbe | 289 | |
13fd6a66 | 290 | while (!IsStopped()) |
a8f0bd18 | 291 | { |
b9abf920 | 292 | ReadFromDevice(500); |
3c53ac93 LOK |
293 | WriteNextCommand(); |
294 | Sleep(5); | |
a8f0bd18 LOK |
295 | } |
296 | ||
a8f0bd18 LOK |
297 | return NULL; |
298 | } | |
299 | ||
25701fa6 | 300 | bool CAdapterCommunication::ReadFromDevice(uint32_t iTimeout) |
a8f0bd18 | 301 | { |
25701fa6 | 302 | int32_t iBytesRead; |
13fd6a66 LOK |
303 | uint8_t buff[1024]; |
304 | if (!m_port) | |
305 | return false; | |
b6c82769 | 306 | |
13fd6a66 LOK |
307 | iBytesRead = m_port->Read(buff, sizeof(buff), iTimeout); |
308 | if (iBytesRead < 0 || iBytesRead > 256) | |
a8f0bd18 | 309 | { |
13fd6a66 LOK |
310 | CStdString strError; |
311 | strError.Format("error reading from serial port: %s", m_port->GetError().c_str()); | |
312 | m_controller->AddLog(CEC_LOG_ERROR, strError); | |
13fd6a66 | 313 | return false; |
a8f0bd18 | 314 | } |
13fd6a66 LOK |
315 | else if (iBytesRead > 0) |
316 | AddData(buff, (uint8_t) iBytesRead); | |
25701fa6 | 317 | |
13fd6a66 | 318 | return iBytesRead > 0; |
a8f0bd18 LOK |
319 | } |
320 | ||
8bca69de | 321 | void CAdapterCommunication::AddData(uint8_t *data, uint8_t iLen) |
a8f0bd18 | 322 | { |
f077e8ed | 323 | CLockObject lock(&m_mutex); |
50aa01e6 LOK |
324 | for (unsigned int iPtr = 0; iPtr < iLen; iPtr++) |
325 | m_inBuffer.Push(data[iPtr]); | |
d5bffd3c LOK |
326 | |
327 | m_rcvCondition.Signal(); | |
a8f0bd18 LOK |
328 | } |
329 | ||
3c53ac93 | 330 | void CAdapterCommunication::WriteNextCommand(void) |
a8f0bd18 | 331 | { |
5a1e87c8 | 332 | CCECAdapterMessagePtr msg; |
3c53ac93 | 333 | if (m_outBuffer.Pop(msg)) |
a8f0bd18 | 334 | { |
eb35e4ca | 335 | CLockObject lock(&msg->mutex); |
0e31a62c | 336 | if (m_port->Write(msg) != (int32_t) msg->size()) |
3c53ac93 LOK |
337 | { |
338 | CStdString strError; | |
339 | strError.Format("error writing to serial port: %s", m_port->GetError().c_str()); | |
340 | m_controller->AddLog(CEC_LOG_ERROR, strError); | |
0e31a62c | 341 | msg->state = ADAPTER_MESSAGE_STATE_ERROR; |
3c53ac93 LOK |
342 | } |
343 | else | |
344 | { | |
345 | m_controller->AddLog(CEC_LOG_DEBUG, "command sent"); | |
0e31a62c LOK |
346 | CCondition::Sleep((uint32_t) msg->size() * (uint32_t)24 /*data*/ + (uint32_t)5 /*start bit (4.5 ms)*/); |
347 | msg->state = ADAPTER_MESSAGE_STATE_SENT; | |
3c53ac93 | 348 | } |
eb35e4ca | 349 | msg->condition.Signal(); |
25701fa6 | 350 | } |
3c53ac93 | 351 | } |
2abe74eb | 352 | |
5a1e87c8 | 353 | bool CAdapterCommunication::Write(CCECAdapterMessagePtr data) |
3c53ac93 | 354 | { |
0e31a62c | 355 | data->state = ADAPTER_MESSAGE_STATE_WAITING; |
3c53ac93 | 356 | m_outBuffer.Push(data); |
a8f0bd18 LOK |
357 | return true; |
358 | } | |
359 | ||
220537f2 | 360 | bool CAdapterCommunication::Read(CCECAdapterMessage &msg, uint32_t iTimeout) |
a8f0bd18 | 361 | { |
f077e8ed | 362 | CLockObject lock(&m_mutex); |
a8f0bd18 | 363 | |
50aa01e6 LOK |
364 | msg.clear(); |
365 | uint64_t iNow = GetTimeMs(); | |
366 | uint64_t iTarget = iNow + iTimeout; | |
367 | bool bGotFullMessage(false); | |
368 | bool bNextIsEscaped(false); | |
369 | bool bGotStart(false); | |
a8f0bd18 | 370 | |
50aa01e6 | 371 | while(!bGotFullMessage && iNow < iTarget) |
a8f0bd18 | 372 | { |
50aa01e6 LOK |
373 | uint8_t buf = 0; |
374 | if (!m_inBuffer.Pop(buf)) | |
a8f0bd18 | 375 | { |
56701628 | 376 | if (!m_rcvCondition.Wait(&m_mutex, (uint32_t) (iTarget - iNow))) |
50aa01e6 | 377 | return false; |
a8f0bd18 | 378 | } |
a8f0bd18 | 379 | |
50aa01e6 | 380 | if (!bGotStart) |
a8f0bd18 | 381 | { |
50aa01e6 LOK |
382 | if (buf == MSGSTART) |
383 | bGotStart = true; | |
384 | continue; | |
a8f0bd18 | 385 | } |
50aa01e6 | 386 | else if (buf == MSGSTART) //we found a msgstart before msgend, this is not right, remove |
a8f0bd18 | 387 | { |
50aa01e6 LOK |
388 | m_controller->AddLog(CEC_LOG_ERROR, "received MSGSTART before MSGEND"); |
389 | msg.clear(); | |
390 | bGotStart = true; | |
a8f0bd18 | 391 | } |
a8f0bd18 | 392 | |
50aa01e6 | 393 | if (buf == MSGEND) |
a8f0bd18 | 394 | { |
50aa01e6 | 395 | bGotFullMessage = true; |
a8f0bd18 | 396 | } |
50aa01e6 LOK |
397 | else if (bNextIsEscaped) |
398 | { | |
399 | msg.push_back(buf + (uint8_t)ESCOFFSET); | |
400 | bNextIsEscaped = false; | |
401 | } | |
402 | else if (buf == MSGESC) | |
403 | bNextIsEscaped = true; | |
404 | else | |
405 | msg.push_back(buf); | |
a8f0bd18 LOK |
406 | } |
407 | ||
0e31a62c LOK |
408 | if (bGotFullMessage) |
409 | msg.state = ADAPTER_MESSAGE_STATE_RECEIVED; | |
410 | ||
50aa01e6 | 411 | return bGotFullMessage; |
a8f0bd18 LOK |
412 | } |
413 | ||
828682d3 | 414 | std::string CAdapterCommunication::GetError(void) const |
a8f0bd18 LOK |
415 | { |
416 | return m_port->GetError(); | |
417 | } | |
2abe74eb LOK |
418 | |
419 | bool CAdapterCommunication::StartBootloader(void) | |
420 | { | |
421 | if (!IsRunning()) | |
422 | return false; | |
423 | ||
424 | m_controller->AddLog(CEC_LOG_DEBUG, "starting the bootloader"); | |
5a1e87c8 | 425 | CCECAdapterMessagePtr output(new CCECAdapterMessage); |
25701fa6 | 426 | |
5a1e87c8 LOK |
427 | output->push_back(MSGSTART); |
428 | output->push_escaped(MSGCODE_START_BOOTLOADER); | |
429 | output->push_back(MSGEND); | |
2abe74eb LOK |
430 | |
431 | if (!Write(output)) | |
432 | { | |
433 | m_controller->AddLog(CEC_LOG_ERROR, "could not start the bootloader"); | |
434 | return false; | |
435 | } | |
436 | m_controller->AddLog(CEC_LOG_DEBUG, "bootloader start command transmitted"); | |
437 | return true; | |
438 | } | |
439 | ||
2abe74eb LOK |
440 | bool CAdapterCommunication::SetAckMask(uint16_t iMask) |
441 | { | |
442 | if (!IsRunning()) | |
443 | return false; | |
444 | ||
445 | CStdString strLog; | |
446 | strLog.Format("setting ackmask to %2x", iMask); | |
447 | m_controller->AddLog(CEC_LOG_DEBUG, strLog.c_str()); | |
448 | ||
5a1e87c8 | 449 | CCECAdapterMessagePtr output(new CCECAdapterMessage); |
2abe74eb | 450 | |
5a1e87c8 LOK |
451 | output->push_back(MSGSTART); |
452 | output->push_escaped(MSGCODE_SET_ACK_MASK); | |
453 | output->push_escaped(iMask >> 8); | |
454 | output->push_escaped((uint8_t)iMask); | |
455 | output->push_back(MSGEND); | |
2abe74eb LOK |
456 | |
457 | if (!Write(output)) | |
458 | { | |
459 | m_controller->AddLog(CEC_LOG_ERROR, "could not set the ackmask"); | |
460 | return false; | |
461 | } | |
462 | ||
463 | return true; | |
464 | } | |
465 | ||
466 | bool CAdapterCommunication::PingAdapter(void) | |
467 | { | |
468 | if (!IsRunning()) | |
469 | return false; | |
470 | ||
471 | m_controller->AddLog(CEC_LOG_DEBUG, "sending ping"); | |
5a1e87c8 | 472 | CCECAdapterMessagePtr output(new CCECAdapterMessage); |
25701fa6 | 473 | |
5a1e87c8 LOK |
474 | output->push_back(MSGSTART); |
475 | output->push_escaped(MSGCODE_PING); | |
476 | output->push_back(MSGEND); | |
2abe74eb LOK |
477 | |
478 | if (!Write(output)) | |
479 | { | |
480 | m_controller->AddLog(CEC_LOG_ERROR, "could not send ping command"); | |
481 | return false; | |
482 | } | |
483 | ||
484 | m_controller->AddLog(CEC_LOG_DEBUG, "ping tranmitted"); | |
485 | ||
486 | // TODO check for pong | |
487 | return true; | |
488 | } | |
13fd6a66 LOK |
489 | |
490 | bool CAdapterCommunication::IsOpen(void) const | |
491 | { | |
b9eea66d | 492 | return !IsStopped() && m_port->IsOpen() && IsRunning(); |
13fd6a66 | 493 | } |