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