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