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