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