Commit | Line | Data |
---|---|---|
abbca718 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 | ||
33 | #include "CECParser.h" | |
34 | ||
35 | #include <algorithm> | |
36 | #include <cstdio> | |
37 | #include <cstdlib> | |
38 | #include <cstring> | |
39 | #include <sys/stat.h> | |
40 | #include "util/StdString.h" | |
41 | #include "libPlatform/serialport.h" | |
42 | #include "util/threads.h" | |
43 | #include "util/timeutils.h" | |
44 | #include "CECDetect.h" | |
45 | ||
46 | using namespace CEC; | |
47 | using namespace std; | |
48 | ||
49 | #define CEC_MAX_RETRY 5 | |
50 | ||
51 | /*! | |
52 | * ICECDevice implementation | |
53 | */ | |
54 | //@{ | |
df7339c6 | 55 | CCECParser::CCECParser(const char *strDeviceName, cec_logical_address iLogicalAddress /* = CECDEVICE_PLAYBACKDEVICE1 */, int iPhysicalAddress /* = CEC_DEFAULT_PHYSICAL_ADDRESS*/) : |
abbca718 LOK |
56 | m_inbuf(NULL), |
57 | m_iInbufSize(0), | |
58 | m_iInbufUsed(0), | |
59 | m_iCurrentButton(CEC_USER_CONTROL_CODE_UNKNOWN), | |
df7339c6 LOK |
60 | m_physicaladdress(iPhysicalAddress), |
61 | m_iLogicalAddress(iLogicalAddress), | |
abbca718 LOK |
62 | m_strDeviceName(strDeviceName), |
63 | m_bRunning(false) | |
64 | { | |
65 | m_serialport = new CSerialPort; | |
66 | } | |
67 | ||
68 | CCECParser::~CCECParser(void) | |
69 | { | |
f99bc831 | 70 | Close(0); |
abbca718 LOK |
71 | m_serialport->Close(); |
72 | delete m_serialport; | |
73 | } | |
74 | ||
75 | bool CCECParser::Open(const char *strPort, int iTimeoutMs /* = 10000 */) | |
76 | { | |
77 | bool bReturn(false); | |
78 | ||
79 | if (!(bReturn = m_serialport->Open(strPort, 38400))) | |
80 | { | |
81 | CStdString strError; | |
82 | strError.Format("error opening serial port '%s': %s", strPort, m_serialport->GetError().c_str()); | |
83 | AddLog(CEC_LOG_ERROR, strError); | |
84 | return bReturn; | |
85 | } | |
86 | ||
87 | //clear any input bytes | |
88 | uint8_t buff[1024]; | |
89 | m_serialport->Read(buff, sizeof(buff), CEC_SETTLE_DOWN_TIME); | |
90 | ||
91 | if (bReturn) | |
6dfe9213 | 92 | bReturn = SetLogicalAddress(m_iLogicalAddress); |
abbca718 LOK |
93 | |
94 | if (!bReturn) | |
95 | { | |
96 | CStdString strError; | |
97 | strError.Format("error opening serial port '%s': %s", strPort, m_serialport->GetError().c_str()); | |
98 | AddLog(CEC_LOG_ERROR, strError); | |
99 | return bReturn; | |
100 | } | |
101 | ||
102 | if (bReturn) | |
103 | { | |
104 | m_bRunning = true; | |
105 | if (pthread_create(&m_thread, NULL, (void *(*) (void *))&CCECParser::ThreadHandler, (void *)this) == 0) | |
106 | pthread_detach(m_thread); | |
107 | else | |
108 | m_bRunning = false; | |
109 | } | |
110 | ||
111 | return bReturn; | |
112 | } | |
113 | ||
f99bc831 LOK |
114 | bool CCECParser::Close(int iTimeoutMs /* = 2000 */) |
115 | { | |
116 | m_bRunning = false; | |
117 | bool bExit(false); | |
118 | if (iTimeoutMs > 0) | |
119 | { | |
120 | bExit = m_exitCondition.Wait(&m_mutex, iTimeoutMs); | |
121 | m_mutex.Unlock(); | |
122 | } | |
123 | else | |
124 | { | |
125 | pthread_join(m_thread, NULL); | |
126 | bExit = true; | |
127 | } | |
128 | ||
129 | return bExit; | |
130 | } | |
131 | ||
abbca718 LOK |
132 | void *CCECParser::ThreadHandler(CCECParser *parser) |
133 | { | |
134 | if (parser) | |
135 | parser->Process(); | |
136 | return 0; | |
137 | } | |
138 | ||
139 | bool CCECParser::Process(void) | |
140 | { | |
141 | int64_t now = GetTimeMs(); | |
142 | while (m_bRunning) | |
143 | { | |
144 | { | |
145 | CLockObject lock(&m_mutex, 1000); | |
146 | if (lock.IsLocked()) | |
147 | { | |
148 | if (!ReadFromDevice(100)) | |
149 | { | |
150 | m_bRunning = false; | |
151 | return false; | |
152 | } | |
153 | } | |
154 | } | |
155 | ||
156 | //AddLog(CEC_LOG_DEBUG, "processing messages"); | |
157 | ProcessMessages(); | |
158 | now = GetTimeMs(); | |
159 | CheckKeypressTimeout(now); | |
160 | CCondition::Sleep(50); | |
161 | } | |
162 | ||
163 | AddLog(CEC_LOG_DEBUG, "reader thread terminated"); | |
164 | m_bRunning = false; | |
f99bc831 | 165 | m_exitCondition.Signal(); |
abbca718 LOK |
166 | return true; |
167 | } | |
168 | ||
169 | bool CCECParser::Ping(void) | |
170 | { | |
f99bc831 LOK |
171 | if (!m_bRunning) |
172 | return false; | |
173 | ||
abbca718 LOK |
174 | AddLog(CEC_LOG_DEBUG, "sending ping"); |
175 | cec_frame output; | |
176 | output.push_back(MSGSTART); | |
177 | PushEscaped(output, MSGCODE_PING); | |
178 | output.push_back(MSGEND); | |
179 | ||
180 | if (!TransmitFormatted(output, false, (int64_t) 5000)) | |
181 | { | |
182 | AddLog(CEC_LOG_ERROR, "could not send ping command"); | |
183 | return false; | |
184 | } | |
185 | ||
186 | AddLog(CEC_LOG_DEBUG, "ping tranmitted"); | |
187 | ||
188 | // TODO check for pong | |
189 | return true; | |
190 | } | |
191 | ||
192 | bool CCECParser::StartBootloader(void) | |
193 | { | |
f99bc831 LOK |
194 | if (!m_bRunning) |
195 | return false; | |
196 | ||
abbca718 LOK |
197 | AddLog(CEC_LOG_DEBUG, "starting the bootloader"); |
198 | cec_frame output; | |
199 | output.push_back(MSGSTART); | |
200 | PushEscaped(output, MSGCODE_START_BOOTLOADER); | |
201 | output.push_back(MSGEND); | |
202 | ||
203 | if (!TransmitFormatted(output, false, (int64_t) 5000)) | |
204 | { | |
205 | AddLog(CEC_LOG_ERROR, "could not start the bootloader"); | |
206 | return false; | |
207 | } | |
208 | ||
209 | AddLog(CEC_LOG_DEBUG, "bootloader start command transmitted"); | |
210 | return true; | |
211 | } | |
212 | ||
213 | uint8_t CCECParser::GetSourceDestination(cec_logical_address destination /* = CECDEVICE_BROADCAST */) | |
214 | { | |
215 | return ((uint8_t)m_iLogicalAddress << 4) + (uint8_t)destination; | |
216 | } | |
217 | ||
218 | bool CCECParser::PowerOffDevices(cec_logical_address address /* = CECDEVICE_BROADCAST */) | |
219 | { | |
f99bc831 LOK |
220 | if (!m_bRunning) |
221 | return false; | |
222 | ||
abbca718 LOK |
223 | CStdString strLog; |
224 | strLog.Format("powering off devices with logical address %d", (int8_t)address); | |
225 | AddLog(CEC_LOG_DEBUG, strLog.c_str()); | |
226 | cec_frame frame; | |
227 | frame.push_back(GetSourceDestination(address)); | |
228 | frame.push_back(CEC_OPCODE_STANDBY); | |
229 | return Transmit(frame); | |
230 | } | |
231 | ||
232 | bool CCECParser::PowerOnDevices(cec_logical_address address /* = CECDEVICE_BROADCAST */) | |
233 | { | |
f99bc831 LOK |
234 | if (!m_bRunning) |
235 | return false; | |
236 | ||
abbca718 LOK |
237 | CStdString strLog; |
238 | strLog.Format("powering on devices with logical address %d", (int8_t)address); | |
239 | AddLog(CEC_LOG_DEBUG, strLog.c_str()); | |
240 | cec_frame frame; | |
241 | frame.push_back(GetSourceDestination(address)); | |
242 | frame.push_back(CEC_OPCODE_TEXT_VIEW_ON); | |
243 | return Transmit(frame); | |
244 | } | |
245 | ||
246 | bool CCECParser::StandbyDevices(cec_logical_address address /* = CECDEVICE_BROADCAST */) | |
247 | { | |
f99bc831 LOK |
248 | if (!m_bRunning) |
249 | return false; | |
250 | ||
abbca718 LOK |
251 | CStdString strLog; |
252 | strLog.Format("putting all devices with logical address %d in standby mode", (int8_t)address); | |
253 | AddLog(CEC_LOG_DEBUG, strLog.c_str()); | |
254 | cec_frame frame; | |
255 | frame.push_back(GetSourceDestination(address)); | |
256 | frame.push_back(CEC_OPCODE_STANDBY); | |
257 | return Transmit(frame); | |
258 | } | |
259 | ||
260 | bool CCECParser::SetActiveView(void) | |
261 | { | |
f99bc831 LOK |
262 | if (!m_bRunning) |
263 | return false; | |
264 | ||
abbca718 LOK |
265 | AddLog(CEC_LOG_DEBUG, "setting active view"); |
266 | cec_frame frame; | |
267 | frame.push_back(GetSourceDestination(CECDEVICE_BROADCAST)); | |
268 | frame.push_back(CEC_OPCODE_ACTIVE_SOURCE); | |
269 | frame.push_back((m_physicaladdress >> 8) & 0xFF); | |
270 | frame.push_back(m_physicaladdress & 0xFF); | |
271 | return Transmit(frame); | |
272 | } | |
273 | ||
274 | bool CCECParser::SetInactiveView(void) | |
275 | { | |
f99bc831 LOK |
276 | if (!m_bRunning) |
277 | return false; | |
278 | ||
abbca718 LOK |
279 | AddLog(CEC_LOG_DEBUG, "setting inactive view"); |
280 | cec_frame frame; | |
281 | frame.push_back(GetSourceDestination(CECDEVICE_BROADCAST)); | |
282 | frame.push_back(CEC_OPCODE_INACTIVE_SOURCE); | |
283 | frame.push_back((m_physicaladdress >> 8) & 0xFF); | |
284 | frame.push_back(m_physicaladdress & 0xFF); | |
285 | return Transmit(frame); | |
286 | } | |
287 | ||
288 | bool CCECParser::GetNextLogMessage(cec_log_message *message) | |
289 | { | |
f99bc831 | 290 | return m_bRunning ? m_logBuffer.Pop(*message) : false; |
abbca718 LOK |
291 | } |
292 | ||
293 | bool CCECParser::GetNextKeypress(cec_keypress *key) | |
294 | { | |
f99bc831 | 295 | return m_bRunning ? m_keyBuffer.Pop(*key) : false; |
abbca718 | 296 | } |
825ddb96 LOK |
297 | |
298 | bool CCECParser::GetNextCommand(cec_command *command) | |
299 | { | |
f99bc831 | 300 | return m_bRunning ? m_commandBuffer.Pop(*command) : false; |
825ddb96 | 301 | } |
abbca718 LOK |
302 | //@} |
303 | ||
825ddb96 | 304 | void CCECParser::TransmitAbort(cec_logical_address address, cec_opcode opcode, ECecAbortReason reason /* = CEC_ABORT_REASON_UNRECOGNIZED_OPCODE */) |
abbca718 LOK |
305 | { |
306 | AddLog(CEC_LOG_DEBUG, "transmitting abort message"); | |
307 | cec_frame frame; | |
308 | frame.push_back(GetSourceDestination(address)); | |
309 | frame.push_back(CEC_OPCODE_FEATURE_ABORT); | |
310 | frame.push_back(opcode); | |
311 | frame.push_back(reason); | |
312 | Transmit(frame); | |
313 | } | |
314 | ||
315 | void CCECParser::ReportCECVersion(cec_logical_address address /* = CECDEVICE_TV */) | |
316 | { | |
317 | cec_frame frame; | |
318 | AddLog(CEC_LOG_NOTICE, "reporting CEC version as 1.3a"); | |
319 | frame.push_back(GetSourceDestination(address)); | |
320 | frame.push_back(CEC_OPCODE_CEC_VERSION); | |
321 | frame.push_back(CEC_VERSION_1_3A); | |
322 | Transmit(frame); | |
323 | } | |
324 | ||
325 | void CCECParser::ReportPowerState(cec_logical_address address /*= CECDEVICE_TV */, bool bOn /* = true */) | |
326 | { | |
327 | cec_frame frame; | |
328 | if (bOn) | |
329 | AddLog(CEC_LOG_NOTICE, "reporting \"On\" power status"); | |
330 | else | |
331 | AddLog(CEC_LOG_NOTICE, "reporting \"Off\" power status"); | |
332 | ||
333 | frame.push_back(GetSourceDestination(address)); | |
334 | frame.push_back(CEC_OPCODE_REPORT_POWER_STATUS); | |
335 | frame.push_back(bOn ? CEC_POWER_STATUS_ON : CEC_POWER_STATUS_STANDBY); | |
336 | Transmit(frame); | |
337 | } | |
338 | ||
339 | void CCECParser::ReportMenuState(cec_logical_address address /* = CECDEVICE_TV */, bool bActive /* = true */) | |
340 | { | |
341 | cec_frame frame; | |
342 | if (bActive) | |
343 | AddLog(CEC_LOG_NOTICE, "reporting menu state as active"); | |
344 | else | |
345 | AddLog(CEC_LOG_NOTICE, "reporting menu state as inactive"); | |
346 | ||
347 | frame.push_back(GetSourceDestination(address)); | |
348 | frame.push_back(CEC_OPCODE_MENU_STATUS); | |
349 | frame.push_back(bActive ? CEC_MENU_STATE_ACTIVATED : CEC_MENU_STATE_DEACTIVATED); | |
350 | Transmit(frame); | |
351 | } | |
352 | ||
353 | void CCECParser::ReportVendorID(cec_logical_address address /* = CECDEVICE_TV */) | |
354 | { | |
355 | AddLog(CEC_LOG_NOTICE, "vendor ID requested, feature abort"); | |
356 | TransmitAbort(address, CEC_OPCODE_GIVE_DEVICE_VENDOR_ID); | |
357 | } | |
358 | ||
359 | void CCECParser::ReportOSDName(cec_logical_address address /* = CECDEVICE_TV */) | |
360 | { | |
361 | cec_frame frame; | |
362 | const char *osdname = m_strDeviceName.c_str(); | |
363 | CStdString strLog; | |
364 | strLog.Format("reporting OSD name as %s", osdname); | |
365 | AddLog(CEC_LOG_NOTICE, strLog.c_str()); | |
366 | frame.push_back(GetSourceDestination(address)); | |
367 | frame.push_back(CEC_OPCODE_SET_OSD_NAME); | |
368 | ||
369 | for (unsigned int i = 0; i < strlen(osdname); i++) | |
370 | frame.push_back(osdname[i]); | |
371 | ||
372 | Transmit(frame); | |
373 | } | |
374 | ||
375 | void CCECParser::ReportPhysicalAddress(void) | |
376 | { | |
377 | cec_frame frame; | |
378 | CStdString strLog; | |
379 | strLog.Format("reporting physical address as %04x", m_physicaladdress); | |
380 | AddLog(CEC_LOG_NOTICE, strLog.c_str()); | |
381 | frame.push_back(GetSourceDestination(CECDEVICE_BROADCAST)); | |
382 | frame.push_back(CEC_OPCODE_REPORT_PHYSICAL_ADDRESS); | |
383 | frame.push_back((m_physicaladdress >> 8) & 0xFF); | |
384 | frame.push_back(m_physicaladdress & 0xFF); | |
385 | frame.push_back(CEC_DEVICE_TYPE_PLAYBACK_DEVICE); | |
386 | Transmit(frame); | |
387 | } | |
388 | ||
389 | void CCECParser::BroadcastActiveSource(void) | |
390 | { | |
391 | cec_frame frame; | |
392 | AddLog(CEC_LOG_NOTICE, "broadcasting active source"); | |
393 | frame.push_back(GetSourceDestination(CECDEVICE_BROADCAST)); | |
394 | frame.push_back(CEC_OPCODE_ACTIVE_SOURCE); | |
395 | frame.push_back((m_physicaladdress >> 8) & 0xFF); | |
396 | frame.push_back(m_physicaladdress & 0xFF); | |
397 | Transmit(frame); | |
398 | } | |
399 | ||
400 | bool CCECParser::TransmitFormatted(const cec_frame &data, bool bWaitForAck /* = true */, int64_t iTimeout /* = 2000 */) | |
401 | { | |
402 | CLockObject lock(&m_mutex, iTimeout); | |
403 | if (!lock.IsLocked()) | |
404 | { | |
405 | AddLog(CEC_LOG_ERROR, "could not get a write lock"); | |
406 | return false; | |
407 | } | |
408 | ||
409 | if (m_serialport->Write(data) != data.size()) | |
410 | { | |
411 | CStdString strError; | |
412 | strError.Format("error writing to serial port: %s", m_serialport->GetError().c_str()); | |
413 | AddLog(CEC_LOG_ERROR, strError); | |
414 | return false; | |
415 | } | |
416 | AddLog(CEC_LOG_DEBUG, "command sent"); | |
417 | ||
418 | CCondition::Sleep((int) data.size() * 24 /*data*/ + 5 /*start bit (4.5 ms)*/ + 50 /* to be on the safe side */); | |
419 | if (bWaitForAck && !WaitForAck()) | |
420 | { | |
421 | AddLog(CEC_LOG_DEBUG, "did not receive ACK"); | |
422 | return false; | |
423 | } | |
424 | ||
425 | return true; | |
426 | } | |
427 | ||
428 | bool CCECParser::Transmit(const cec_frame &data, bool bWaitForAck /* = true */, int64_t iTimeout /* = 5000 */) | |
429 | { | |
430 | CStdString txStr = "transmit "; | |
431 | for (unsigned int i = 0; i < data.size(); i++) | |
432 | txStr.AppendFormat(" %02x", data[i]); | |
433 | AddLog(CEC_LOG_DEBUG, txStr.c_str()); | |
434 | ||
435 | if (data.empty()) | |
436 | { | |
437 | AddLog(CEC_LOG_WARNING, "transmit buffer is empty"); | |
438 | return false; | |
439 | } | |
440 | ||
441 | cec_frame output; | |
442 | ||
443 | //set ack polarity to high when transmitting to the broadcast address | |
444 | //set ack polarity low when transmitting to any other address | |
445 | output.push_back(MSGSTART); | |
446 | PushEscaped(output, MSGCODE_TRANSMIT_ACK_POLARITY); | |
447 | ||
448 | if ((data[0] & 0xF) == 0xF) | |
449 | PushEscaped(output, CEC_TRUE); | |
450 | else | |
451 | PushEscaped(output, CEC_FALSE); | |
452 | ||
453 | output.push_back(MSGEND); | |
454 | ||
455 | for (unsigned int i = 0; i < data.size(); i++) | |
456 | { | |
457 | output.push_back(MSGSTART); | |
458 | ||
459 | if (i == data.size() - 1) | |
460 | PushEscaped(output, MSGCODE_TRANSMIT_EOM); | |
461 | else | |
462 | PushEscaped(output, MSGCODE_TRANSMIT); | |
463 | ||
464 | PushEscaped(output, data[i]); | |
465 | ||
466 | output.push_back(MSGEND); | |
467 | } | |
468 | ||
469 | return TransmitFormatted(output, bWaitForAck, iTimeout); | |
470 | } | |
471 | ||
472 | bool CCECParser::WaitForAck(int64_t iTimeout /* = 1000 */) | |
473 | { | |
474 | bool bGotAck(false); | |
475 | bool bError(false); | |
476 | ||
477 | int64_t iNow = GetTimeMs(); | |
478 | int64_t iTargetTime = iNow + iTimeout; | |
479 | ||
480 | while (!bGotAck && !bError && (iTimeout <= 0 || iNow < iTargetTime)) | |
481 | { | |
482 | if (!ReadFromDevice((int) iTimeout)) | |
483 | { | |
484 | AddLog(CEC_LOG_ERROR, "failed to read from device"); | |
485 | return false; | |
486 | } | |
487 | ||
488 | cec_frame msg; | |
489 | while (!bGotAck && !bError && GetMessage(msg, false)) | |
490 | { | |
491 | uint8_t iCode = msg[0] & ~(MSGCODE_FRAME_EOM | MSGCODE_FRAME_ACK); | |
492 | ||
493 | switch (iCode) | |
494 | { | |
495 | case MSGCODE_COMMAND_ACCEPTED: | |
496 | AddLog(CEC_LOG_DEBUG, "MSGCODE_COMMAND_ACCEPTED"); | |
497 | break; | |
498 | case MSGCODE_TRANSMIT_SUCCEEDED: | |
499 | AddLog(CEC_LOG_DEBUG, "MSGCODE_TRANSMIT_SUCCEEDED"); | |
500 | // TODO | |
501 | bGotAck = true; | |
502 | break; | |
503 | case MSGCODE_RECEIVE_FAILED: | |
504 | AddLog(CEC_LOG_WARNING, "MSGCODE_RECEIVE_FAILED"); | |
505 | bError = true; | |
506 | break; | |
507 | case MSGCODE_COMMAND_REJECTED: | |
508 | AddLog(CEC_LOG_WARNING, "MSGCODE_COMMAND_REJECTED"); | |
509 | bError = true; | |
510 | break; | |
511 | case MSGCODE_TRANSMIT_FAILED_LINE: | |
512 | AddLog(CEC_LOG_WARNING, "MSGCODE_TRANSMIT_FAILED_LINE"); | |
513 | bError = true; | |
514 | break; | |
515 | case MSGCODE_TRANSMIT_FAILED_ACK: | |
516 | AddLog(CEC_LOG_WARNING, "MSGCODE_TRANSMIT_FAILED_ACK"); | |
517 | bError = true; | |
518 | break; | |
519 | case MSGCODE_TRANSMIT_FAILED_TIMEOUT_DATA: | |
520 | AddLog(CEC_LOG_WARNING, "MSGCODE_TRANSMIT_FAILED_TIMEOUT_DATA"); | |
521 | bError = true; | |
522 | break; | |
523 | case MSGCODE_TRANSMIT_FAILED_TIMEOUT_LINE: | |
524 | AddLog(CEC_LOG_WARNING, "MSGCODE_TRANSMIT_FAILED_TIMEOUT_LINE"); | |
525 | bError = true; | |
526 | break; | |
527 | default: | |
528 | m_frameBuffer.Push(msg); | |
529 | bGotAck = (msg[0] & MSGCODE_FRAME_ACK) != 0; | |
530 | break; | |
531 | } | |
532 | iNow = GetTimeMs(); | |
533 | } | |
534 | } | |
535 | ||
536 | return bGotAck && !bError; | |
537 | } | |
538 | ||
539 | bool CCECParser::ReadFromDevice(int iTimeout) | |
540 | { | |
541 | uint8_t buff[1024]; | |
542 | int iBytesRead = m_serialport->Read(buff, sizeof(buff), iTimeout); | |
543 | if (iBytesRead < 0) | |
544 | { | |
545 | CStdString strError; | |
546 | strError.Format("error reading from serial port: %s", m_serialport->GetError().c_str()); | |
547 | AddLog(CEC_LOG_ERROR, strError); | |
548 | return false; | |
549 | } | |
550 | else if (iBytesRead > 0) | |
551 | AddData(buff, iBytesRead); | |
552 | ||
553 | return true; | |
554 | } | |
555 | ||
556 | void CCECParser::ProcessMessages(void) | |
557 | { | |
558 | cec_frame msg; | |
f99bc831 | 559 | while (m_bRunning && GetMessage(msg)) |
abbca718 LOK |
560 | ParseMessage(msg); |
561 | } | |
562 | ||
563 | bool CCECParser::GetMessage(cec_frame &msg, bool bFromBuffer /* = true */) | |
564 | { | |
565 | if (bFromBuffer && m_frameBuffer.Pop(msg)) | |
566 | return true; | |
567 | ||
568 | if (m_iInbufUsed < 1) | |
569 | return false; | |
570 | ||
571 | //search for first start of message | |
572 | int startpos = -1; | |
573 | for (int i = 0; i < m_iInbufUsed; i++) | |
574 | { | |
575 | if (m_inbuf[i] == MSGSTART) | |
576 | { | |
577 | startpos = i; | |
578 | break; | |
579 | } | |
580 | } | |
581 | ||
582 | if (startpos == -1) | |
583 | return false; | |
584 | ||
585 | //move anything from the first start of message to the beginning of the buffer | |
586 | if (startpos > 0) | |
587 | { | |
588 | memmove(m_inbuf, m_inbuf + startpos, m_iInbufUsed - startpos); | |
589 | m_iInbufUsed -= startpos; | |
590 | } | |
591 | ||
592 | if (m_iInbufUsed < 2) | |
593 | return false; | |
594 | ||
595 | //look for end of message | |
596 | startpos = -1; | |
597 | int endpos = -1; | |
598 | for (int i = 1; i < m_iInbufUsed; i++) | |
599 | { | |
600 | if (m_inbuf[i] == MSGEND) | |
601 | { | |
602 | endpos = i; | |
603 | break; | |
604 | } | |
605 | else if (m_inbuf[i] == MSGSTART) | |
606 | { | |
607 | startpos = i; | |
608 | break; | |
609 | } | |
610 | } | |
611 | ||
612 | if (startpos > 0) //we found a msgstart before msgend, this is not right, remove | |
613 | { | |
614 | AddLog(CEC_LOG_ERROR, "received MSGSTART before MSGEND"); | |
615 | memmove(m_inbuf, m_inbuf + startpos, m_iInbufUsed - startpos); | |
616 | m_iInbufUsed -= startpos; | |
617 | return false; | |
618 | } | |
619 | ||
620 | if (endpos > 0) //found a MSGEND | |
621 | { | |
622 | msg.clear(); | |
623 | bool isesc = false; | |
624 | for (int i = 1; i < endpos; i++) | |
625 | { | |
626 | if (isesc) | |
627 | { | |
628 | msg.push_back(m_inbuf[i] + (uint8_t)ESCOFFSET); | |
629 | isesc = false; | |
630 | } | |
631 | else if (m_inbuf[i] == MSGESC) | |
632 | { | |
633 | isesc = true; | |
634 | } | |
635 | else | |
636 | { | |
637 | msg.push_back(m_inbuf[i]); | |
638 | } | |
639 | } | |
640 | ||
641 | if (endpos + 1 < m_iInbufUsed) | |
642 | memmove(m_inbuf, m_inbuf + endpos + 1, m_iInbufUsed - endpos - 1); | |
643 | ||
644 | m_iInbufUsed -= endpos + 1; | |
645 | ||
646 | return true; | |
647 | } | |
648 | ||
649 | return false; | |
650 | } | |
651 | ||
652 | void CCECParser::ParseMessage(cec_frame &msg) | |
653 | { | |
654 | if (msg.empty()) | |
655 | return; | |
656 | ||
657 | CStdString logStr; | |
658 | uint8_t iCode = msg[0] & ~(MSGCODE_FRAME_EOM | MSGCODE_FRAME_ACK); | |
659 | bool bEom = (msg[0] & MSGCODE_FRAME_EOM) != 0; | |
660 | bool bAck = (msg[0] & MSGCODE_FRAME_ACK) != 0; | |
661 | ||
662 | switch(iCode) | |
663 | { | |
664 | case MSGCODE_NOTHING: | |
665 | AddLog(CEC_LOG_DEBUG, "MSGCODE_NOTHING"); | |
666 | break; | |
667 | case MSGCODE_TIMEOUT_ERROR: | |
668 | case MSGCODE_HIGH_ERROR: | |
669 | case MSGCODE_LOW_ERROR: | |
670 | { | |
671 | if (iCode == MSGCODE_TIMEOUT_ERROR) | |
672 | logStr = "MSGCODE_TIMEOUT"; | |
673 | else if (iCode == MSGCODE_HIGH_ERROR) | |
674 | logStr = "MSGCODE_HIGH_ERROR"; | |
675 | else | |
676 | logStr = "MSGCODE_LOW_ERROR"; | |
677 | ||
678 | int iLine = (msg.size() >= 3) ? (msg[1] << 8) | (msg[2]) : 0; | |
679 | uint32_t iTime = (msg.size() >= 7) ? (msg[3] << 24) | (msg[4] << 16) | (msg[5] << 8) | (msg[6]) : 0; | |
680 | logStr.AppendFormat(" line:%i", iLine); | |
681 | logStr.AppendFormat(" time:%u", iTime); | |
682 | AddLog(CEC_LOG_WARNING, logStr.c_str()); | |
683 | } | |
684 | break; | |
685 | case MSGCODE_FRAME_START: | |
686 | { | |
687 | logStr = "MSGCODE_FRAME_START"; | |
688 | m_currentframe.clear(); | |
689 | if (msg.size() >= 2) | |
690 | { | |
691 | int iInitiator = msg[1] >> 4; | |
692 | int iDestination = msg[1] & 0xF; | |
693 | logStr.AppendFormat(" initiator:%u destination:%u ack:%s %s", iInitiator, iDestination, bAck ? "high" : "low", bEom ? "eom" : ""); | |
694 | ||
695 | m_currentframe.push_back(msg[1]); | |
696 | } | |
697 | AddLog(CEC_LOG_DEBUG, logStr.c_str()); | |
698 | } | |
699 | break; | |
700 | case MSGCODE_FRAME_DATA: | |
701 | { | |
702 | logStr = "MSGCODE_FRAME_DATA"; | |
703 | if (msg.size() >= 2) | |
704 | { | |
705 | uint8_t iData = msg[1]; | |
706 | logStr.AppendFormat(" %02x", iData); | |
707 | m_currentframe.push_back(iData); | |
708 | } | |
709 | AddLog(CEC_LOG_DEBUG, logStr.c_str()); | |
710 | } | |
711 | if (bEom) | |
712 | ParseCurrentFrame(); | |
713 | break; | |
714 | default: | |
715 | break; | |
716 | } | |
717 | } | |
718 | ||
719 | void CCECParser::ParseCurrentFrame(void) | |
720 | { | |
721 | uint8_t initiator = m_currentframe[0] >> 4; | |
722 | uint8_t destination = m_currentframe[0] & 0xF; | |
723 | ||
724 | CStdString dataStr; | |
725 | dataStr.Format("received frame: initiator: %u destination: %u", initiator, destination); | |
726 | ||
727 | if (m_currentframe.size() > 1) | |
728 | { | |
729 | dataStr += " data:"; | |
730 | for (unsigned int i = 1; i < m_currentframe.size(); i++) | |
731 | dataStr.AppendFormat(" %02x", m_currentframe[i]); | |
732 | } | |
733 | AddLog(CEC_LOG_DEBUG, dataStr.c_str()); | |
734 | ||
735 | if (m_currentframe.size() <= 1) | |
736 | return; | |
737 | ||
738 | vector<uint8_t> tx; | |
825ddb96 | 739 | cec_opcode opCode = (cec_opcode) m_currentframe[1]; |
abbca718 LOK |
740 | if (destination == (uint16_t) m_iLogicalAddress) |
741 | { | |
742 | switch(opCode) | |
743 | { | |
744 | case CEC_OPCODE_GIVE_PHYSICAL_ADDRESS: | |
745 | ReportPhysicalAddress(); | |
746 | SetActiveView(); | |
747 | break; | |
748 | case CEC_OPCODE_GIVE_OSD_NAME: | |
749 | ReportOSDName((cec_logical_address)initiator); | |
750 | break; | |
751 | case CEC_OPCODE_GIVE_DEVICE_VENDOR_ID: | |
752 | ReportVendorID((cec_logical_address)initiator); | |
753 | break; | |
754 | case CEC_OPCODE_MENU_REQUEST: | |
755 | ReportMenuState((cec_logical_address)initiator); | |
756 | break; | |
757 | case CEC_OPCODE_GIVE_DEVICE_POWER_STATUS: | |
758 | ReportPowerState((cec_logical_address)initiator); | |
759 | break; | |
760 | case CEC_OPCODE_GET_CEC_VERSION: | |
761 | ReportCECVersion((cec_logical_address)initiator); | |
762 | break; | |
763 | case CEC_OPCODE_USER_CONTROL_PRESSED: | |
764 | if (m_currentframe.size() > 2) | |
765 | { | |
766 | AddKey(); | |
767 | ||
768 | if (m_currentframe[2] <= CEC_USER_CONTROL_CODE_MAX) | |
769 | { | |
770 | m_iCurrentButton = (cec_user_control_code) m_currentframe[2]; | |
771 | m_buttontime = GetTimeMs(); | |
772 | } | |
773 | } | |
774 | break; | |
775 | case CEC_OPCODE_USER_CONTROL_RELEASE: | |
776 | AddKey(); | |
777 | break; | |
778 | default: | |
825ddb96 LOK |
779 | cec_frame params = m_currentframe; |
780 | params.erase(params.begin(), params.begin() + 2); | |
781 | AddCommand((cec_logical_address) initiator, (cec_logical_address) destination, opCode, ¶ms); | |
abbca718 LOK |
782 | break; |
783 | } | |
784 | } | |
785 | else if (destination == (uint8_t) CECDEVICE_BROADCAST) | |
786 | { | |
787 | if (opCode == CEC_OPCODE_REQUEST_ACTIVE_SOURCE) | |
788 | { | |
789 | BroadcastActiveSource(); | |
790 | } | |
791 | else if (opCode == CEC_OPCODE_SET_STREAM_PATH) | |
792 | { | |
793 | if (m_currentframe.size() >= 4) | |
794 | { | |
795 | int streamaddr = ((int)m_currentframe[2] << 8) | ((int)m_currentframe[3]); | |
796 | CStdString strLog; | |
797 | strLog.Format("%i requests stream path from physical address %04x", initiator, streamaddr); | |
798 | AddLog(CEC_LOG_DEBUG, strLog.c_str()); | |
799 | if (streamaddr == m_physicaladdress) | |
800 | BroadcastActiveSource(); | |
801 | } | |
802 | } | |
c49c485b LOK |
803 | else |
804 | { | |
805 | cec_frame params = m_currentframe; | |
806 | params.erase(params.begin(), params.begin() + 2); | |
807 | AddCommand((cec_logical_address) initiator, (cec_logical_address) destination, opCode, ¶ms); | |
808 | } | |
abbca718 LOK |
809 | } |
810 | else | |
811 | { | |
812 | CStdString strLog; | |
813 | strLog.Format("ignoring frame: destination: %u != %u", destination, (uint16_t)m_iLogicalAddress); | |
814 | AddLog(CEC_LOG_DEBUG, strLog.c_str()); | |
815 | } | |
816 | } | |
817 | ||
818 | void CCECParser::AddData(uint8_t *data, int iLen) | |
819 | { | |
820 | if (iLen + m_iInbufUsed > m_iInbufSize) | |
821 | { | |
822 | m_iInbufSize = iLen + m_iInbufUsed; | |
823 | m_inbuf = (uint8_t*)realloc(m_inbuf, m_iInbufSize); | |
824 | } | |
825 | ||
826 | memcpy(m_inbuf + m_iInbufUsed, data, iLen); | |
827 | m_iInbufUsed += iLen; | |
828 | } | |
829 | ||
830 | void CCECParser::PushEscaped(cec_frame &vec, uint8_t byte) | |
831 | { | |
832 | if (byte >= MSGESC && byte != MSGSTART) | |
833 | { | |
834 | vec.push_back(MSGESC); | |
835 | vec.push_back(byte - ESCOFFSET); | |
836 | } | |
837 | else | |
838 | { | |
839 | vec.push_back(byte); | |
840 | } | |
841 | } | |
842 | ||
843 | void CCECParser::CheckKeypressTimeout(int64_t now) | |
844 | { | |
845 | if (m_iCurrentButton != CEC_USER_CONTROL_CODE_UNKNOWN && now - m_buttontime > 500) | |
846 | { | |
847 | AddKey(); | |
848 | m_iCurrentButton = CEC_USER_CONTROL_CODE_UNKNOWN; | |
849 | } | |
850 | } | |
851 | ||
6dfe9213 | 852 | bool CCECParser::SetLogicalAddress(cec_logical_address iLogicalAddress) |
abbca718 LOK |
853 | { |
854 | CStdString strLog; | |
6dfe9213 | 855 | strLog.Format("setting logical address to %d", iLogicalAddress); |
abbca718 LOK |
856 | AddLog(CEC_LOG_NOTICE, strLog.c_str()); |
857 | ||
6dfe9213 LOK |
858 | m_iLogicalAddress = iLogicalAddress; |
859 | return SetAckMask(0x1 << (uint8_t)m_iLogicalAddress); | |
860 | } | |
861 | ||
862 | bool CCECParser::SetAckMask(uint16_t iMask) | |
863 | { | |
864 | CStdString strLog; | |
865 | strLog.Format("setting ackmask to %2x", iMask); | |
866 | AddLog(CEC_LOG_DEBUG, strLog.c_str()); | |
867 | ||
abbca718 | 868 | cec_frame output; |
abbca718 | 869 | |
6dfe9213 | 870 | output.push_back(MSGSTART); |
abbca718 | 871 | PushEscaped(output, MSGCODE_SET_ACK_MASK); |
6dfe9213 | 872 | PushEscaped(output, iMask >> 8); |
bcd03b37 | 873 | PushEscaped(output, (uint8_t)iMask); |
abbca718 LOK |
874 | output.push_back(MSGEND); |
875 | ||
876 | if (m_serialport->Write(output) == -1) | |
877 | { | |
6dfe9213 LOK |
878 | strLog.Format("error writing to serial port: %s", m_serialport->GetError().c_str()); |
879 | AddLog(CEC_LOG_ERROR, strLog); | |
abbca718 LOK |
880 | return false; |
881 | } | |
882 | ||
883 | return true; | |
884 | } | |
885 | ||
886 | void CCECParser::AddLog(cec_log_level level, const string &strMessage) | |
887 | { | |
888 | cec_log_message message; | |
889 | message.level = level; | |
890 | message.message.assign(strMessage.c_str()); | |
891 | m_logBuffer.Push(message); | |
892 | } | |
893 | ||
894 | void CCECParser::AddKey(void) | |
895 | { | |
896 | if (m_iCurrentButton != CEC_USER_CONTROL_CODE_UNKNOWN) | |
897 | { | |
898 | cec_keypress key; | |
899 | key.duration = (unsigned int) (GetTimeMs() - m_buttontime); | |
900 | key.keycode = m_iCurrentButton; | |
901 | m_keyBuffer.Push(key); | |
902 | m_iCurrentButton = CEC_USER_CONTROL_CODE_UNKNOWN; | |
903 | m_buttontime = 0; | |
904 | } | |
905 | } | |
906 | ||
825ddb96 LOK |
907 | void CCECParser::AddCommand(cec_logical_address source, cec_logical_address destination, cec_opcode opcode, cec_frame *parameters) |
908 | { | |
909 | cec_command command; | |
910 | command.source = source; | |
911 | command.destination = destination; | |
912 | command.opcode = opcode; | |
913 | if (parameters) | |
914 | command.parameters = *parameters; | |
c49c485b LOK |
915 | if (m_commandBuffer.Push(command)) |
916 | { | |
917 | CStdString strDebug; | |
918 | strDebug.Format("stored command '%d' in the command buffer. buffer size = %d", opcode, m_commandBuffer.Size()); | |
919 | AddLog(CEC_LOG_DEBUG, strDebug); | |
920 | } | |
921 | else | |
922 | { | |
923 | AddLog(CEC_LOG_WARNING, "command buffer is full"); | |
924 | } | |
825ddb96 LOK |
925 | } |
926 | ||
abbca718 LOK |
927 | int CCECParser::GetMinVersion(void) |
928 | { | |
929 | return CEC_MIN_VERSION; | |
930 | } | |
931 | ||
932 | int CCECParser::GetLibVersion(void) | |
933 | { | |
934 | return CEC_LIB_VERSION; | |
935 | } | |
936 | ||
937 | int CCECParser::FindDevices(std::vector<cec_device> &deviceList, const char *strDevicePath /* = NULL */) | |
938 | { | |
939 | CStdString strDebug; | |
940 | if (strDevicePath) | |
941 | strDebug.Format("trying to autodetect the com port for device path '%s'", strDevicePath); | |
942 | else | |
943 | strDebug.Format("trying to autodetect all CEC adapters"); | |
944 | AddLog(CEC_LOG_DEBUG, strDebug); | |
945 | ||
946 | return CCECDetect::FindDevices(deviceList, strDevicePath); | |
947 | } | |
948 | ||
bcd03b37 | 949 | DECLSPEC void * CECCreate(const char *strDeviceName, CEC::cec_logical_address iLogicalAddress /*= CEC::CECDEVICE_PLAYBACKDEVICE1 */, int iPhysicalAddress /* = CEC_DEFAULT_PHYSICAL_ADDRESS */) |
abbca718 | 950 | { |
df7339c6 | 951 | return static_cast< void* > (new CCECParser(strDeviceName, iLogicalAddress, iPhysicalAddress)); |
abbca718 | 952 | } |