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 | ||
2abe74eb | 33 | #include "CECProcessor.h" |
abbca718 | 34 | |
2abe74eb LOK |
35 | #include "AdapterCommunication.h" |
36 | #include "LibCEC.h" | |
abbca718 | 37 | #include "util/StdString.h" |
b9187cc6 | 38 | #include "platform/timeutils.h" |
abbca718 LOK |
39 | |
40 | using namespace CEC; | |
41 | using namespace std; | |
42 | ||
2b4d8297 | 43 | CCECProcessor::CCECProcessor(CLibCEC *controller, CAdapterCommunication *serComm, const char *strDeviceName, cec_logical_address iLogicalAddress /* = CECDEVICE_PLAYBACKDEVICE1 */, uint16_t iPhysicalAddress /* = CEC_DEFAULT_PHYSICAL_ADDRESS*/) : |
df7339c6 LOK |
44 | m_physicaladdress(iPhysicalAddress), |
45 | m_iLogicalAddress(iLogicalAddress), | |
2abe74eb LOK |
46 | m_strDeviceName(strDeviceName), |
47 | m_communication(serComm), | |
48 | m_controller(controller) | |
abbca718 | 49 | { |
abbca718 LOK |
50 | } |
51 | ||
2abe74eb | 52 | CCECProcessor::~CCECProcessor(void) |
abbca718 | 53 | { |
2abe74eb LOK |
54 | StopThread(); |
55 | m_communication = NULL; | |
56 | m_controller = NULL; | |
abbca718 LOK |
57 | } |
58 | ||
2abe74eb | 59 | bool CCECProcessor::Start(void) |
abbca718 | 60 | { |
2abe74eb | 61 | if (!m_communication || !m_communication->IsOpen()) |
13fd6a66 LOK |
62 | { |
63 | m_controller->AddLog(CEC_LOG_ERROR, "connection is closed"); | |
a8f0bd18 | 64 | return false; |
13fd6a66 | 65 | } |
abbca718 | 66 | |
a8f0bd18 | 67 | if (!SetLogicalAddress(m_iLogicalAddress)) |
abbca718 | 68 | { |
2abe74eb | 69 | m_controller->AddLog(CEC_LOG_ERROR, "could not set the logical address"); |
a8f0bd18 | 70 | return false; |
abbca718 LOK |
71 | } |
72 | ||
60fa4578 | 73 | if (CreateThread()) |
a8f0bd18 | 74 | return true; |
a8f0bd18 | 75 | else |
2abe74eb | 76 | m_controller->AddLog(CEC_LOG_ERROR, "could not create a processor thread"); |
abbca718 | 77 | |
a8f0bd18 | 78 | return false; |
abbca718 LOK |
79 | } |
80 | ||
2abe74eb | 81 | void *CCECProcessor::Process(void) |
f99bc831 | 82 | { |
2abe74eb | 83 | m_controller->AddLog(CEC_LOG_DEBUG, "processor thread started"); |
abbca718 | 84 | |
13fd6a66 | 85 | while (!IsStopped()) |
abbca718 | 86 | { |
60fa4578 | 87 | bool bParseFrame(false); |
76321de4 LOK |
88 | cec_frame msg; |
89 | msg.clear(); | |
90 | ||
60fa4578 LOK |
91 | { |
92 | CLockObject lock(&m_mutex); | |
76321de4 | 93 | if (m_communication->IsOpen() && m_communication->Read(msg, 50)) |
13fd6a66 | 94 | bParseFrame = ParseMessage(msg) && !IsStopped(); |
76321de4 LOK |
95 | |
96 | if (bParseFrame) | |
97 | { | |
98 | msg.clear(); | |
99 | msg = m_currentframe; | |
100 | } | |
60fa4578 LOK |
101 | } |
102 | ||
13fd6a66 | 103 | if (bParseFrame) |
76321de4 | 104 | ParseCurrentFrame(msg); |
abbca718 | 105 | |
13fd6a66 LOK |
106 | m_controller->CheckKeypressTimeout(); |
107 | ||
108 | if (!IsStopped()) | |
12027dbe | 109 | Sleep(50); |
abbca718 LOK |
110 | } |
111 | ||
60fa4578 | 112 | return NULL; |
abbca718 LOK |
113 | } |
114 | ||
2abe74eb | 115 | bool CCECProcessor::PowerOnDevices(cec_logical_address address /* = CECDEVICE_TV */) |
abbca718 | 116 | { |
60fa4578 | 117 | if (!IsRunning()) |
f99bc831 LOK |
118 | return false; |
119 | ||
abbca718 | 120 | CStdString strLog; |
88f45c9b | 121 | strLog.Format("<< powering on device with logical address %d", (int8_t)address); |
2abe74eb | 122 | m_controller->AddLog(CEC_LOG_DEBUG, strLog.c_str()); |
abbca718 | 123 | cec_frame frame; |
25701fa6 LOK |
124 | frame.clear(); |
125 | ||
abbca718 | 126 | frame.push_back(GetSourceDestination(address)); |
370757d1 | 127 | frame.push_back((uint8_t) CEC_OPCODE_IMAGE_VIEW_ON); |
abbca718 LOK |
128 | return Transmit(frame); |
129 | } | |
130 | ||
2abe74eb | 131 | bool CCECProcessor::StandbyDevices(cec_logical_address address /* = CECDEVICE_BROADCAST */) |
abbca718 | 132 | { |
60fa4578 | 133 | if (!IsRunning()) |
f99bc831 LOK |
134 | return false; |
135 | ||
abbca718 | 136 | CStdString strLog; |
88f45c9b | 137 | strLog.Format("<< putting device with logical address %d in standby mode", (int8_t)address); |
2abe74eb | 138 | m_controller->AddLog(CEC_LOG_DEBUG, strLog.c_str()); |
abbca718 | 139 | cec_frame frame; |
25701fa6 LOK |
140 | frame.clear(); |
141 | ||
abbca718 | 142 | frame.push_back(GetSourceDestination(address)); |
8bca69de | 143 | frame.push_back((uint8_t) CEC_OPCODE_STANDBY); |
abbca718 LOK |
144 | return Transmit(frame); |
145 | } | |
146 | ||
2abe74eb | 147 | bool CCECProcessor::SetActiveView(void) |
abbca718 | 148 | { |
60fa4578 | 149 | if (!IsRunning()) |
f99bc831 LOK |
150 | return false; |
151 | ||
88f45c9b | 152 | m_controller->AddLog(CEC_LOG_DEBUG, "<< setting active view"); |
abbca718 | 153 | cec_frame frame; |
25701fa6 LOK |
154 | frame.clear(); |
155 | ||
abbca718 | 156 | frame.push_back(GetSourceDestination(CECDEVICE_BROADCAST)); |
8bca69de | 157 | frame.push_back((uint8_t) CEC_OPCODE_ACTIVE_SOURCE); |
abbca718 LOK |
158 | frame.push_back((m_physicaladdress >> 8) & 0xFF); |
159 | frame.push_back(m_physicaladdress & 0xFF); | |
160 | return Transmit(frame); | |
161 | } | |
162 | ||
2abe74eb | 163 | bool CCECProcessor::SetInactiveView(void) |
abbca718 | 164 | { |
60fa4578 | 165 | if (!IsRunning()) |
f99bc831 LOK |
166 | return false; |
167 | ||
88f45c9b | 168 | m_controller->AddLog(CEC_LOG_DEBUG, "<< setting inactive view"); |
abbca718 | 169 | cec_frame frame; |
25701fa6 LOK |
170 | frame.clear(); |
171 | ||
abbca718 | 172 | frame.push_back(GetSourceDestination(CECDEVICE_BROADCAST)); |
8bca69de | 173 | frame.push_back((uint8_t) CEC_OPCODE_INACTIVE_SOURCE); |
abbca718 LOK |
174 | frame.push_back((m_physicaladdress >> 8) & 0xFF); |
175 | frame.push_back(m_physicaladdress & 0xFF); | |
176 | return Transmit(frame); | |
177 | } | |
178 | ||
2abe74eb | 179 | bool CCECProcessor::Transmit(const cec_frame &data, bool bWaitForAck /* = true */) |
abbca718 | 180 | { |
2abe74eb | 181 | CStdString txStr = "transmit "; |
25701fa6 LOK |
182 | for (unsigned int i = 0; i < data.size; i++) |
183 | txStr.AppendFormat(" %02x", data.data[i]); | |
2abe74eb LOK |
184 | m_controller->AddLog(CEC_LOG_DEBUG, txStr.c_str()); |
185 | ||
25701fa6 | 186 | if (data.size == 0) |
2abe74eb LOK |
187 | { |
188 | m_controller->AddLog(CEC_LOG_WARNING, "transmit buffer is empty"); | |
189 | return false; | |
190 | } | |
191 | ||
192 | cec_frame output; | |
25701fa6 | 193 | output.clear(); |
2abe74eb LOK |
194 | |
195 | //set ack polarity to high when transmitting to the broadcast address | |
196 | //set ack polarity low when transmitting to any other address | |
197 | output.push_back(MSGSTART); | |
198 | CAdapterCommunication::PushEscaped(output, MSGCODE_TRANSMIT_ACK_POLARITY); | |
199 | ||
25701fa6 | 200 | if ((data.data[0] & 0xF) == 0xF) |
2abe74eb LOK |
201 | CAdapterCommunication::PushEscaped(output, CEC_TRUE); |
202 | else | |
203 | CAdapterCommunication::PushEscaped(output, CEC_FALSE); | |
204 | ||
205 | output.push_back(MSGEND); | |
206 | ||
25701fa6 | 207 | for (int8_t i = 0; i < data.size; i++) |
2abe74eb LOK |
208 | { |
209 | output.push_back(MSGSTART); | |
210 | ||
25701fa6 | 211 | if (i == (int8_t)data.size - 1) |
2abe74eb LOK |
212 | CAdapterCommunication::PushEscaped(output, MSGCODE_TRANSMIT_EOM); |
213 | else | |
214 | CAdapterCommunication::PushEscaped(output, MSGCODE_TRANSMIT); | |
215 | ||
25701fa6 | 216 | CAdapterCommunication::PushEscaped(output, data.data[i]); |
2abe74eb LOK |
217 | |
218 | output.push_back(MSGEND); | |
219 | } | |
220 | ||
221 | return TransmitFormatted(output, bWaitForAck); | |
abbca718 LOK |
222 | } |
223 | ||
2abe74eb | 224 | bool CCECProcessor::SetLogicalAddress(cec_logical_address iLogicalAddress) |
abbca718 | 225 | { |
2abe74eb | 226 | CStdString strLog; |
88f45c9b | 227 | strLog.Format("<< setting logical address to %d", iLogicalAddress); |
2abe74eb LOK |
228 | m_controller->AddLog(CEC_LOG_NOTICE, strLog.c_str()); |
229 | ||
230 | m_iLogicalAddress = iLogicalAddress; | |
231 | return m_communication && m_communication->SetAckMask(0x1 << (uint8_t)m_iLogicalAddress); | |
abbca718 | 232 | } |
825ddb96 | 233 | |
2abe74eb | 234 | bool CCECProcessor::TransmitFormatted(const cec_frame &data, bool bWaitForAck /* = true */) |
825ddb96 | 235 | { |
2abe74eb LOK |
236 | CLockObject lock(&m_mutex); |
237 | if (!m_communication || !m_communication->Write(data)) | |
238 | return false; | |
239 | ||
050df0f9 | 240 | if (bWaitForAck) |
2abe74eb | 241 | { |
050df0f9 LOK |
242 | uint64_t now = GetTimeMs(); |
243 | uint64_t target = now + 1000; | |
244 | bool bError(false); | |
245 | bool bGotAck(false); | |
246 | ||
247 | while (!bGotAck && now < target) | |
248 | { | |
249 | bGotAck = WaitForAck(&bError, (uint64_t) (target - now)); | |
250 | now = GetTimeMs(); | |
251 | ||
252 | if (bError && now < target) | |
253 | { | |
254 | m_controller->AddLog(CEC_LOG_ERROR, "retransmitting previous frame"); | |
255 | if (!m_communication->Write(data)) | |
256 | return false; | |
257 | } | |
258 | } | |
2abe74eb LOK |
259 | } |
260 | ||
261 | return true; | |
825ddb96 | 262 | } |
abbca718 | 263 | |
2abe74eb | 264 | void CCECProcessor::TransmitAbort(cec_logical_address address, cec_opcode opcode, ECecAbortReason reason /* = CEC_ABORT_REASON_UNRECOGNIZED_OPCODE */) |
abbca718 | 265 | { |
2abe74eb | 266 | m_controller->AddLog(CEC_LOG_DEBUG, "transmitting abort message"); |
abbca718 | 267 | cec_frame frame; |
25701fa6 LOK |
268 | frame.clear(); |
269 | ||
abbca718 | 270 | frame.push_back(GetSourceDestination(address)); |
8bca69de LOK |
271 | frame.push_back((uint8_t) CEC_OPCODE_FEATURE_ABORT); |
272 | frame.push_back((uint8_t) opcode); | |
273 | frame.push_back((uint8_t) reason); | |
abbca718 LOK |
274 | Transmit(frame); |
275 | } | |
276 | ||
2abe74eb | 277 | void CCECProcessor::ReportCECVersion(cec_logical_address address /* = CECDEVICE_TV */) |
abbca718 LOK |
278 | { |
279 | cec_frame frame; | |
25701fa6 LOK |
280 | frame.clear(); |
281 | ||
88f45c9b | 282 | m_controller->AddLog(CEC_LOG_NOTICE, "<< reporting CEC version as 1.3a"); |
abbca718 | 283 | frame.push_back(GetSourceDestination(address)); |
8bca69de | 284 | frame.push_back((uint8_t) CEC_OPCODE_CEC_VERSION); |
25701fa6 | 285 | frame.push_back((uint8_t) CEC_VERSION_1_3A); |
abbca718 LOK |
286 | Transmit(frame); |
287 | } | |
288 | ||
2abe74eb | 289 | void CCECProcessor::ReportPowerState(cec_logical_address address /*= CECDEVICE_TV */, bool bOn /* = true */) |
abbca718 LOK |
290 | { |
291 | cec_frame frame; | |
25701fa6 LOK |
292 | frame.clear(); |
293 | ||
abbca718 | 294 | if (bOn) |
88f45c9b | 295 | m_controller->AddLog(CEC_LOG_NOTICE, "<< reporting \"On\" power status"); |
abbca718 | 296 | else |
88f45c9b | 297 | m_controller->AddLog(CEC_LOG_NOTICE, "<< reporting \"Off\" power status"); |
abbca718 LOK |
298 | |
299 | frame.push_back(GetSourceDestination(address)); | |
8bca69de LOK |
300 | frame.push_back((uint8_t) CEC_OPCODE_REPORT_POWER_STATUS); |
301 | frame.push_back(bOn ? (uint8_t) CEC_POWER_STATUS_ON : (uint8_t) CEC_POWER_STATUS_STANDBY); | |
abbca718 LOK |
302 | Transmit(frame); |
303 | } | |
304 | ||
2abe74eb | 305 | void CCECProcessor::ReportMenuState(cec_logical_address address /* = CECDEVICE_TV */, bool bActive /* = true */) |
abbca718 LOK |
306 | { |
307 | cec_frame frame; | |
25701fa6 LOK |
308 | frame.clear(); |
309 | ||
abbca718 | 310 | if (bActive) |
88f45c9b | 311 | m_controller->AddLog(CEC_LOG_NOTICE, "<< reporting menu state as active"); |
abbca718 | 312 | else |
88f45c9b | 313 | m_controller->AddLog(CEC_LOG_NOTICE, "<< reporting menu state as inactive"); |
abbca718 LOK |
314 | |
315 | frame.push_back(GetSourceDestination(address)); | |
8bca69de LOK |
316 | frame.push_back((uint8_t) CEC_OPCODE_MENU_STATUS); |
317 | frame.push_back(bActive ? (uint8_t) CEC_MENU_STATE_ACTIVATED : (uint8_t) CEC_MENU_STATE_DEACTIVATED); | |
abbca718 LOK |
318 | Transmit(frame); |
319 | } | |
320 | ||
2abe74eb | 321 | void CCECProcessor::ReportVendorID(cec_logical_address address /* = CECDEVICE_TV */) |
abbca718 | 322 | { |
88f45c9b | 323 | m_controller->AddLog(CEC_LOG_NOTICE, "<< vendor ID requested, feature abort"); |
abbca718 LOK |
324 | TransmitAbort(address, CEC_OPCODE_GIVE_DEVICE_VENDOR_ID); |
325 | } | |
326 | ||
2abe74eb | 327 | void CCECProcessor::ReportOSDName(cec_logical_address address /* = CECDEVICE_TV */) |
abbca718 LOK |
328 | { |
329 | cec_frame frame; | |
25701fa6 LOK |
330 | frame.clear(); |
331 | ||
abbca718 LOK |
332 | const char *osdname = m_strDeviceName.c_str(); |
333 | CStdString strLog; | |
88f45c9b | 334 | strLog.Format("<< reporting OSD name as %s", osdname); |
2abe74eb | 335 | m_controller->AddLog(CEC_LOG_NOTICE, strLog.c_str()); |
abbca718 | 336 | frame.push_back(GetSourceDestination(address)); |
8bca69de | 337 | frame.push_back((uint8_t) CEC_OPCODE_SET_OSD_NAME); |
abbca718 LOK |
338 | |
339 | for (unsigned int i = 0; i < strlen(osdname); i++) | |
340 | frame.push_back(osdname[i]); | |
341 | ||
342 | Transmit(frame); | |
343 | } | |
344 | ||
2abe74eb | 345 | void CCECProcessor::ReportPhysicalAddress(void) |
abbca718 LOK |
346 | { |
347 | cec_frame frame; | |
25701fa6 LOK |
348 | frame.clear(); |
349 | ||
abbca718 | 350 | CStdString strLog; |
88f45c9b | 351 | strLog.Format("<< reporting physical address as %04x", m_physicaladdress); |
2abe74eb | 352 | m_controller->AddLog(CEC_LOG_NOTICE, strLog.c_str()); |
abbca718 | 353 | frame.push_back(GetSourceDestination(CECDEVICE_BROADCAST)); |
8bca69de | 354 | frame.push_back((uint8_t) CEC_OPCODE_REPORT_PHYSICAL_ADDRESS); |
25701fa6 LOK |
355 | frame.push_back((uint8_t) ((m_physicaladdress >> 8) & 0xFF)); |
356 | frame.push_back((uint8_t) (m_physicaladdress & 0xFF)); | |
357 | frame.push_back((uint8_t) CEC_DEVICE_TYPE_PLAYBACK_DEVICE); | |
abbca718 LOK |
358 | Transmit(frame); |
359 | } | |
360 | ||
2abe74eb | 361 | void CCECProcessor::BroadcastActiveSource(void) |
abbca718 LOK |
362 | { |
363 | cec_frame frame; | |
25701fa6 LOK |
364 | frame.clear(); |
365 | ||
2abe74eb | 366 | m_controller->AddLog(CEC_LOG_NOTICE, "broadcasting active source"); |
abbca718 | 367 | frame.push_back(GetSourceDestination(CECDEVICE_BROADCAST)); |
8bca69de | 368 | frame.push_back((uint8_t) CEC_OPCODE_ACTIVE_SOURCE); |
25701fa6 LOK |
369 | frame.push_back((uint8_t) ((m_physicaladdress >> 8) & 0xFF)); |
370 | frame.push_back((uint8_t) (m_physicaladdress & 0xFF)); | |
abbca718 LOK |
371 | Transmit(frame); |
372 | } | |
373 | ||
2abe74eb | 374 | uint8_t CCECProcessor::GetSourceDestination(cec_logical_address destination /* = CECDEVICE_BROADCAST */) const |
abbca718 | 375 | { |
2abe74eb | 376 | return ((uint8_t)m_iLogicalAddress << 4) + (uint8_t)destination; |
abbca718 LOK |
377 | } |
378 | ||
050df0f9 | 379 | bool CCECProcessor::WaitForAck(bool *bError, uint32_t iTimeout /* = 1000 */) |
abbca718 LOK |
380 | { |
381 | bool bGotAck(false); | |
050df0f9 | 382 | *bError = false; |
abbca718 LOK |
383 | |
384 | int64_t iNow = GetTimeMs(); | |
25701fa6 | 385 | int64_t iTargetTime = iNow + (uint64_t) iTimeout; |
abbca718 | 386 | |
25701fa6 | 387 | while (!bGotAck && !bError && (iTimeout == 0 || iNow < iTargetTime)) |
abbca718 | 388 | { |
abbca718 | 389 | cec_frame msg; |
25701fa6 LOK |
390 | msg.clear(); |
391 | ||
a8f0bd18 | 392 | while (!bGotAck && !bError && m_communication->Read(msg, iTimeout)) |
abbca718 | 393 | { |
25701fa6 | 394 | uint8_t iCode = msg.data[0] & ~(MSGCODE_FRAME_EOM | MSGCODE_FRAME_ACK); |
abbca718 LOK |
395 | |
396 | switch (iCode) | |
397 | { | |
398 | case MSGCODE_COMMAND_ACCEPTED: | |
2abe74eb | 399 | m_controller->AddLog(CEC_LOG_DEBUG, "MSGCODE_COMMAND_ACCEPTED"); |
abbca718 LOK |
400 | break; |
401 | case MSGCODE_TRANSMIT_SUCCEEDED: | |
2abe74eb | 402 | m_controller->AddLog(CEC_LOG_DEBUG, "MSGCODE_TRANSMIT_SUCCEEDED"); |
abbca718 LOK |
403 | // TODO |
404 | bGotAck = true; | |
405 | break; | |
406 | case MSGCODE_RECEIVE_FAILED: | |
2abe74eb | 407 | m_controller->AddLog(CEC_LOG_WARNING, "MSGCODE_RECEIVE_FAILED"); |
050df0f9 | 408 | *bError = true; |
abbca718 LOK |
409 | break; |
410 | case MSGCODE_COMMAND_REJECTED: | |
2abe74eb | 411 | m_controller->AddLog(CEC_LOG_WARNING, "MSGCODE_COMMAND_REJECTED"); |
050df0f9 | 412 | *bError = true; |
abbca718 LOK |
413 | break; |
414 | case MSGCODE_TRANSMIT_FAILED_LINE: | |
2abe74eb | 415 | m_controller->AddLog(CEC_LOG_WARNING, "MSGCODE_TRANSMIT_FAILED_LINE"); |
050df0f9 | 416 | *bError = true; |
abbca718 LOK |
417 | break; |
418 | case MSGCODE_TRANSMIT_FAILED_ACK: | |
2abe74eb | 419 | m_controller->AddLog(CEC_LOG_WARNING, "MSGCODE_TRANSMIT_FAILED_ACK"); |
050df0f9 | 420 | *bError = true; |
abbca718 LOK |
421 | break; |
422 | case MSGCODE_TRANSMIT_FAILED_TIMEOUT_DATA: | |
2abe74eb | 423 | m_controller->AddLog(CEC_LOG_WARNING, "MSGCODE_TRANSMIT_FAILED_TIMEOUT_DATA"); |
050df0f9 | 424 | *bError = true; |
abbca718 LOK |
425 | break; |
426 | case MSGCODE_TRANSMIT_FAILED_TIMEOUT_LINE: | |
2abe74eb | 427 | m_controller->AddLog(CEC_LOG_WARNING, "MSGCODE_TRANSMIT_FAILED_TIMEOUT_LINE"); |
050df0f9 | 428 | *bError = true; |
abbca718 LOK |
429 | break; |
430 | default: | |
431 | m_frameBuffer.Push(msg); | |
abbca718 LOK |
432 | break; |
433 | } | |
434 | iNow = GetTimeMs(); | |
435 | } | |
436 | } | |
437 | ||
438 | return bGotAck && !bError; | |
439 | } | |
440 | ||
2abe74eb | 441 | bool CCECProcessor::ParseMessage(cec_frame &msg) |
abbca718 | 442 | { |
60fa4578 LOK |
443 | bool bReturn(false); |
444 | ||
25701fa6 | 445 | if (msg.size == 0) |
60fa4578 | 446 | return bReturn; |
abbca718 LOK |
447 | |
448 | CStdString logStr; | |
25701fa6 LOK |
449 | uint8_t iCode = msg.data[0] & ~(MSGCODE_FRAME_EOM | MSGCODE_FRAME_ACK); |
450 | bool bEom = (msg.data[0] & MSGCODE_FRAME_EOM) != 0; | |
451 | bool bAck = (msg.data[0] & MSGCODE_FRAME_ACK) != 0; | |
abbca718 LOK |
452 | |
453 | switch(iCode) | |
454 | { | |
455 | case MSGCODE_NOTHING: | |
2abe74eb | 456 | m_controller->AddLog(CEC_LOG_DEBUG, "MSGCODE_NOTHING"); |
abbca718 LOK |
457 | break; |
458 | case MSGCODE_TIMEOUT_ERROR: | |
459 | case MSGCODE_HIGH_ERROR: | |
460 | case MSGCODE_LOW_ERROR: | |
461 | { | |
462 | if (iCode == MSGCODE_TIMEOUT_ERROR) | |
463 | logStr = "MSGCODE_TIMEOUT"; | |
464 | else if (iCode == MSGCODE_HIGH_ERROR) | |
465 | logStr = "MSGCODE_HIGH_ERROR"; | |
466 | else | |
467 | logStr = "MSGCODE_LOW_ERROR"; | |
468 | ||
25701fa6 LOK |
469 | int iLine = (msg.size >= 3) ? (msg.data[1] << 8) | (msg.data[2]) : 0; |
470 | uint32_t iTime = (msg.size >= 7) ? (msg.data[3] << 24) | (msg.data[4] << 16) | (msg.data[5] << 8) | (msg.data[6]) : 0; | |
abbca718 LOK |
471 | logStr.AppendFormat(" line:%i", iLine); |
472 | logStr.AppendFormat(" time:%u", iTime); | |
2abe74eb | 473 | m_controller->AddLog(CEC_LOG_WARNING, logStr.c_str()); |
abbca718 LOK |
474 | } |
475 | break; | |
476 | case MSGCODE_FRAME_START: | |
477 | { | |
478 | logStr = "MSGCODE_FRAME_START"; | |
479 | m_currentframe.clear(); | |
25701fa6 | 480 | if (msg.size >= 2) |
abbca718 | 481 | { |
25701fa6 LOK |
482 | int iInitiator = msg.data[1] >> 4; |
483 | int iDestination = msg.data[1] & 0xF; | |
abbca718 LOK |
484 | logStr.AppendFormat(" initiator:%u destination:%u ack:%s %s", iInitiator, iDestination, bAck ? "high" : "low", bEom ? "eom" : ""); |
485 | ||
25701fa6 | 486 | m_currentframe.push_back(msg.data[1]); |
abbca718 | 487 | } |
2abe74eb | 488 | m_controller->AddLog(CEC_LOG_DEBUG, logStr.c_str()); |
abbca718 LOK |
489 | } |
490 | break; | |
491 | case MSGCODE_FRAME_DATA: | |
492 | { | |
493 | logStr = "MSGCODE_FRAME_DATA"; | |
25701fa6 | 494 | if (msg.size >= 2) |
abbca718 | 495 | { |
25701fa6 | 496 | uint8_t iData = msg.data[1]; |
abbca718 LOK |
497 | logStr.AppendFormat(" %02x", iData); |
498 | m_currentframe.push_back(iData); | |
499 | } | |
2abe74eb | 500 | m_controller->AddLog(CEC_LOG_DEBUG, logStr.c_str()); |
abbca718 LOK |
501 | } |
502 | if (bEom) | |
60fa4578 | 503 | bReturn = true; |
abbca718 LOK |
504 | break; |
505 | default: | |
506 | break; | |
507 | } | |
60fa4578 LOK |
508 | |
509 | return bReturn; | |
abbca718 LOK |
510 | } |
511 | ||
76321de4 | 512 | void CCECProcessor::ParseCurrentFrame(cec_frame &frame) |
abbca718 | 513 | { |
d54c6c91 LOK |
514 | uint8_t initiator = frame.data[0] >> 4; |
515 | uint8_t destination = frame.data[0] & 0xF; | |
abbca718 LOK |
516 | |
517 | CStdString dataStr; | |
88f45c9b | 518 | dataStr.Format(">> received frame: initiator: %u destination: %u", initiator, destination); |
abbca718 | 519 | |
d54c6c91 | 520 | if (frame.size > 1) |
abbca718 LOK |
521 | { |
522 | dataStr += " data:"; | |
d54c6c91 LOK |
523 | for (unsigned int i = 1; i < frame.size; i++) |
524 | dataStr.AppendFormat(" %02x", frame.data[i]); | |
abbca718 | 525 | } |
2abe74eb | 526 | m_controller->AddLog(CEC_LOG_DEBUG, dataStr.c_str()); |
abbca718 | 527 | |
d54c6c91 | 528 | if (frame.size <= 1) |
abbca718 LOK |
529 | return; |
530 | ||
d54c6c91 | 531 | cec_opcode opCode = (cec_opcode) frame.data[1]; |
abbca718 LOK |
532 | if (destination == (uint16_t) m_iLogicalAddress) |
533 | { | |
534 | switch(opCode) | |
535 | { | |
536 | case CEC_OPCODE_GIVE_PHYSICAL_ADDRESS: | |
537 | ReportPhysicalAddress(); | |
538 | SetActiveView(); | |
539 | break; | |
540 | case CEC_OPCODE_GIVE_OSD_NAME: | |
541 | ReportOSDName((cec_logical_address)initiator); | |
542 | break; | |
543 | case CEC_OPCODE_GIVE_DEVICE_VENDOR_ID: | |
544 | ReportVendorID((cec_logical_address)initiator); | |
545 | break; | |
546 | case CEC_OPCODE_MENU_REQUEST: | |
547 | ReportMenuState((cec_logical_address)initiator); | |
548 | break; | |
549 | case CEC_OPCODE_GIVE_DEVICE_POWER_STATUS: | |
550 | ReportPowerState((cec_logical_address)initiator); | |
551 | break; | |
552 | case CEC_OPCODE_GET_CEC_VERSION: | |
553 | ReportCECVersion((cec_logical_address)initiator); | |
554 | break; | |
555 | case CEC_OPCODE_USER_CONTROL_PRESSED: | |
d54c6c91 | 556 | if (frame.size > 2) |
abbca718 | 557 | { |
2abe74eb | 558 | m_controller->AddKey(); |
abbca718 | 559 | |
d54c6c91 LOK |
560 | if (frame.data[2] <= CEC_USER_CONTROL_CODE_MAX) |
561 | m_controller->SetCurrentButton((cec_user_control_code) frame.data[2]); | |
abbca718 LOK |
562 | } |
563 | break; | |
564 | case CEC_OPCODE_USER_CONTROL_RELEASE: | |
2abe74eb | 565 | m_controller->AddKey(); |
abbca718 LOK |
566 | break; |
567 | default: | |
d54c6c91 | 568 | cec_frame params = frame; |
25701fa6 | 569 | params.shift(2); |
2abe74eb | 570 | m_controller->AddCommand((cec_logical_address) initiator, (cec_logical_address) destination, opCode, ¶ms); |
abbca718 LOK |
571 | break; |
572 | } | |
573 | } | |
574 | else if (destination == (uint8_t) CECDEVICE_BROADCAST) | |
575 | { | |
370757d1 | 576 | CStdString strLog; |
abbca718 LOK |
577 | if (opCode == CEC_OPCODE_REQUEST_ACTIVE_SOURCE) |
578 | { | |
88f45c9b | 579 | strLog.Format(">> %i requests active source", initiator); |
370757d1 | 580 | m_controller->AddLog(CEC_LOG_DEBUG, strLog.c_str()); |
abbca718 LOK |
581 | BroadcastActiveSource(); |
582 | } | |
583 | else if (opCode == CEC_OPCODE_SET_STREAM_PATH) | |
584 | { | |
d54c6c91 | 585 | if (frame.size >= 4) |
abbca718 | 586 | { |
d54c6c91 | 587 | int streamaddr = ((int)frame.data[2] << 8) | ((int)frame.data[3]); |
88f45c9b | 588 | strLog.Format(">> %i requests stream path from physical address %04x", initiator, streamaddr); |
2abe74eb | 589 | m_controller->AddLog(CEC_LOG_DEBUG, strLog.c_str()); |
abbca718 LOK |
590 | if (streamaddr == m_physicaladdress) |
591 | BroadcastActiveSource(); | |
592 | } | |
593 | } | |
c49c485b LOK |
594 | else |
595 | { | |
d54c6c91 | 596 | cec_frame params = frame; |
25701fa6 | 597 | params.shift(2); |
2abe74eb | 598 | m_controller->AddCommand((cec_logical_address) initiator, (cec_logical_address) destination, opCode, ¶ms); |
c49c485b | 599 | } |
abbca718 LOK |
600 | } |
601 | else | |
602 | { | |
603 | CStdString strLog; | |
604 | strLog.Format("ignoring frame: destination: %u != %u", destination, (uint16_t)m_iLogicalAddress); | |
2abe74eb | 605 | m_controller->AddLog(CEC_LOG_DEBUG, strLog.c_str()); |
abbca718 LOK |
606 | } |
607 | } |