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