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