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