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