Commit | Line | Data |
---|---|---|
a75e3a5a LOK |
1 | /* |
2 | * This file is part of the libCEC(R) library. | |
3 | * | |
4 | * libCEC(R) is Copyright (C) 2011-2012 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 | ||
2b44051c | 33 | #include "env.h" |
a75e3a5a | 34 | #include "USBCECAdapterCommands.h" |
2b44051c LOK |
35 | |
36 | #include "USBCECAdapterMessage.h" | |
37 | #include "USBCECAdapterCommunication.h" | |
38 | #include "lib/LibCEC.h" | |
39 | #include "lib/CECProcessor.h" | |
40 | #include "lib/CECTypeUtils.h" | |
41 | #include <stdio.h> | |
a75e3a5a LOK |
42 | |
43 | using namespace CEC; | |
44 | using namespace PLATFORM; | |
45 | ||
004b8382 | 46 | #define LIB_CEC m_comm->m_callback->GetLib() |
0d800fe5 | 47 | #define ToString(p) CCECTypeUtils::ToString(p) |
004b8382 | 48 | |
7c5c5bf4 LOK |
49 | CUSBCECAdapterCommands::CUSBCECAdapterCommands(CUSBCECAdapterCommunication *comm) : |
50 | m_comm(comm), | |
51 | m_bSettingsRetrieved(false), | |
52 | m_bSettingAutoEnabled(false), | |
53 | m_settingCecVersion(CEC_VERSION_UNKNOWN), | |
54 | m_iSettingLAMask(0), | |
b2f56d35 | 55 | m_bNeedsWrite(false), |
a38292a3 | 56 | m_iBuildDate(CEC_FW_BUILD_UNKNOWN), |
2d418322 LOK |
57 | m_bControlledMode(false), |
58 | m_adapterType(P8_ADAPTERTYPE_UNKNOWN) | |
7c5c5bf4 LOK |
59 | { |
60 | m_persistedConfiguration.Clear(); | |
61 | } | |
62 | ||
a75e3a5a LOK |
63 | cec_datapacket CUSBCECAdapterCommands::RequestSetting(cec_adapter_messagecode msgCode) |
64 | { | |
65 | cec_datapacket retVal; | |
66 | retVal.Clear(); | |
67 | ||
68 | CCECAdapterMessage params; | |
69 | CCECAdapterMessage *message = m_comm->SendCommand(msgCode, params); | |
70 | if (message->state == ADAPTER_MESSAGE_STATE_SENT_ACKED) | |
71 | { | |
72 | retVal = message->response; | |
73 | retVal.Shift(2); // shift out start and msgcode | |
74 | retVal.size -= 1; // remove end | |
75 | } | |
c9d15485 | 76 | |
a75e3a5a LOK |
77 | delete message; |
78 | return retVal; | |
79 | } | |
80 | ||
81 | uint16_t CUSBCECAdapterCommands::RequestFirmwareVersion(void) | |
82 | { | |
7c5c5bf4 | 83 | m_persistedConfiguration.iFirmwareVersion = CEC_FW_VERSION_UNKNOWN; |
a75e3a5a LOK |
84 | unsigned int iFwVersionTry(0); |
85 | ||
7c5c5bf4 | 86 | while (m_persistedConfiguration.iFirmwareVersion == CEC_FW_VERSION_UNKNOWN && iFwVersionTry++ < 3) |
a75e3a5a | 87 | { |
b509ba1a | 88 | #ifdef CEC_DEBUGGING |
004b8382 | 89 | LIB_CEC->AddLog(CEC_LOG_DEBUG, "requesting the firmware version"); |
b509ba1a | 90 | #endif |
a75e3a5a LOK |
91 | cec_datapacket response = RequestSetting(MSGCODE_FIRMWARE_VERSION); |
92 | if (response.size == 2) | |
7c5c5bf4 | 93 | m_persistedConfiguration.iFirmwareVersion = (response[0] << 8 | response[1]); |
a75e3a5a LOK |
94 | else |
95 | { | |
daec0320 | 96 | LIB_CEC->AddLog(CEC_LOG_WARNING, "the adapter did not respond with a correct firmware version (try %d, size = %d)", iFwVersionTry, response.size); |
a75e3a5a LOK |
97 | CEvent::Sleep(500); |
98 | } | |
99 | } | |
100 | ||
7c5c5bf4 | 101 | if (m_persistedConfiguration.iFirmwareVersion == CEC_FW_VERSION_UNKNOWN) |
a75e3a5a | 102 | { |
004b8382 | 103 | LIB_CEC->AddLog(CEC_LOG_DEBUG, "defaulting to firmware version 1"); |
7c5c5bf4 | 104 | m_persistedConfiguration.iFirmwareVersion = 1; |
a75e3a5a LOK |
105 | } |
106 | ||
7c5c5bf4 | 107 | return m_persistedConfiguration.iFirmwareVersion; |
a75e3a5a LOK |
108 | } |
109 | ||
7c5c5bf4 | 110 | bool CUSBCECAdapterCommands::RequestSettingAutoEnabled(void) |
a75e3a5a | 111 | { |
b509ba1a | 112 | #ifdef CEC_DEBUGGING |
004b8382 | 113 | LIB_CEC->AddLog(CEC_LOG_DEBUG, "requesting autonomous mode setting"); |
b509ba1a | 114 | #endif |
a75e3a5a LOK |
115 | |
116 | cec_datapacket response = RequestSetting(MSGCODE_GET_AUTO_ENABLED); | |
117 | if (response.size == 1) | |
118 | { | |
7c5c5bf4 | 119 | m_bSettingAutoEnabled = response[0] == 1; |
004b8382 | 120 | LIB_CEC->AddLog(CEC_LOG_DEBUG, "using persisted autonomous mode setting: '%s'", m_bSettingAutoEnabled ? "enabled" : "disabled"); |
a75e3a5a LOK |
121 | return true; |
122 | } | |
123 | return false; | |
124 | } | |
125 | ||
7c5c5bf4 | 126 | bool CUSBCECAdapterCommands::RequestSettingCECVersion(void) |
a75e3a5a | 127 | { |
b509ba1a | 128 | #ifdef CEC_DEBUGGING |
004b8382 | 129 | LIB_CEC->AddLog(CEC_LOG_DEBUG, "requesting CEC version setting"); |
b509ba1a | 130 | #endif |
a75e3a5a LOK |
131 | |
132 | cec_datapacket response = RequestSetting(MSGCODE_GET_HDMI_VERSION); | |
133 | if (response.size == 1) | |
134 | { | |
7c5c5bf4 | 135 | m_settingCecVersion = (cec_version)response[0]; |
004b8382 | 136 | LIB_CEC->AddLog(CEC_LOG_DEBUG, "using persisted CEC version setting: '%s'", ToString(m_settingCecVersion)); |
a75e3a5a LOK |
137 | return true; |
138 | } | |
139 | return false; | |
140 | } | |
141 | ||
2d418322 LOK |
142 | p8_cec_adapter_type CUSBCECAdapterCommands::RequestAdapterType(void) |
143 | { | |
144 | if (m_adapterType == P8_ADAPTERTYPE_UNKNOWN) | |
145 | { | |
b509ba1a | 146 | #ifdef CEC_DEBUGGING |
2d418322 | 147 | LIB_CEC->AddLog(CEC_LOG_DEBUG, "requesting adapter type"); |
b509ba1a | 148 | #endif |
2d418322 LOK |
149 | |
150 | cec_datapacket response = RequestSetting(MSGCODE_GET_ADAPTER_TYPE); | |
151 | if (response.size == 1) | |
152 | m_adapterType = (p8_cec_adapter_type)response[0]; | |
153 | } | |
154 | return m_adapterType; | |
155 | } | |
156 | ||
b2f56d35 LOK |
157 | uint32_t CUSBCECAdapterCommands::RequestBuildDate(void) |
158 | { | |
d2d1660c | 159 | if (m_iBuildDate == CEC_FW_BUILD_UNKNOWN) |
c37545cf | 160 | { |
b509ba1a | 161 | #ifdef CEC_DEBUGGING |
004b8382 | 162 | LIB_CEC->AddLog(CEC_LOG_DEBUG, "requesting firmware build date"); |
b509ba1a | 163 | #endif |
b2f56d35 | 164 | |
c37545cf LOK |
165 | cec_datapacket response = RequestSetting(MSGCODE_GET_BUILDDATE); |
166 | if (response.size == 4) | |
167 | m_iBuildDate = (uint32_t)response[0] << 24 | (uint32_t)response[1] << 16 | (uint32_t)response[2] << 8 | (uint32_t)response[3]; | |
168 | } | |
169 | return m_iBuildDate; | |
b2f56d35 LOK |
170 | } |
171 | ||
7c5c5bf4 | 172 | bool CUSBCECAdapterCommands::RequestSettingDefaultLogicalAddress(void) |
a75e3a5a | 173 | { |
b509ba1a | 174 | #ifdef CEC_DEBUGGING |
004b8382 | 175 | LIB_CEC->AddLog(CEC_LOG_DEBUG, "requesting default logical address setting"); |
b509ba1a | 176 | #endif |
a75e3a5a LOK |
177 | |
178 | cec_datapacket response = RequestSetting(MSGCODE_GET_DEFAULT_LOGICAL_ADDRESS); | |
179 | if (response.size == 1) | |
180 | { | |
7c5c5bf4 | 181 | m_persistedConfiguration.logicalAddresses.primary = (cec_logical_address)response[0]; |
004b8382 | 182 | LIB_CEC->AddLog(CEC_LOG_DEBUG, "using persisted logical address setting: '%s'", ToString(m_persistedConfiguration.logicalAddresses.primary)); |
a75e3a5a LOK |
183 | return true; |
184 | } | |
185 | return false; | |
186 | } | |
187 | ||
7c5c5bf4 | 188 | bool CUSBCECAdapterCommands::RequestSettingDeviceType(void) |
a75e3a5a | 189 | { |
b509ba1a | 190 | #ifdef CEC_DEBUGGING |
004b8382 | 191 | LIB_CEC->AddLog(CEC_LOG_DEBUG, "requesting device type setting"); |
b509ba1a | 192 | #endif |
7c5c5bf4 | 193 | m_persistedConfiguration.deviceTypes.Clear(); |
a75e3a5a LOK |
194 | |
195 | cec_datapacket response = RequestSetting(MSGCODE_GET_DEVICE_TYPE); | |
196 | if (response.size == 1) | |
197 | { | |
7c5c5bf4 | 198 | m_persistedConfiguration.deviceTypes.Add((cec_device_type)response[0]); |
004b8382 | 199 | LIB_CEC->AddLog(CEC_LOG_DEBUG, "using persisted device type setting: '%s'", ToString((cec_device_type)response[0])); |
a75e3a5a LOK |
200 | return true; |
201 | } | |
004b8382 | 202 | LIB_CEC->AddLog(CEC_LOG_DEBUG, "no persisted device type setting"); |
a75e3a5a LOK |
203 | return false; |
204 | } | |
205 | ||
7c5c5bf4 | 206 | bool CUSBCECAdapterCommands::RequestSettingLogicalAddressMask(void) |
a75e3a5a | 207 | { |
b509ba1a | 208 | #ifdef CEC_DEBUGGING |
004b8382 | 209 | LIB_CEC->AddLog(CEC_LOG_DEBUG, "requesting logical address mask setting"); |
b509ba1a | 210 | #endif |
a75e3a5a LOK |
211 | |
212 | cec_datapacket response = RequestSetting(MSGCODE_GET_LOGICAL_ADDRESS_MASK); | |
213 | if (response.size == 2) | |
214 | { | |
7c5c5bf4 | 215 | m_iSettingLAMask = ((uint16_t)response[0] << 8) | ((uint16_t)response[1]); |
004b8382 | 216 | LIB_CEC->AddLog(CEC_LOG_DEBUG, "using persisted logical address mask setting: '%x'", m_iSettingLAMask); |
a75e3a5a LOK |
217 | return true; |
218 | } | |
219 | return false; | |
220 | } | |
221 | ||
7c5c5bf4 | 222 | bool CUSBCECAdapterCommands::RequestSettingOSDName(void) |
a75e3a5a | 223 | { |
b509ba1a | 224 | #ifdef CEC_DEBUGGING |
004b8382 | 225 | LIB_CEC->AddLog(CEC_LOG_DEBUG, "requesting OSD name setting"); |
b509ba1a | 226 | #endif |
a75e3a5a | 227 | |
7c5c5bf4 | 228 | memset(m_persistedConfiguration.strDeviceName, 0, 13); |
a75e3a5a LOK |
229 | cec_datapacket response = RequestSetting(MSGCODE_GET_OSD_NAME); |
230 | if (response.size == 0) | |
7c5c5bf4 | 231 | { |
004b8382 | 232 | LIB_CEC->AddLog(CEC_LOG_DEBUG, "no persisted device name setting"); |
a75e3a5a | 233 | return false; |
7c5c5bf4 | 234 | } |
a75e3a5a LOK |
235 | |
236 | char buf[14]; | |
237 | for (uint8_t iPtr = 0; iPtr < response.size && iPtr < 13; iPtr++) | |
238 | buf[iPtr] = (char)response[iPtr]; | |
239 | buf[response.size] = 0; | |
240 | ||
7c5c5bf4 | 241 | snprintf(m_persistedConfiguration.strDeviceName, 13, "%s", buf); |
004b8382 | 242 | LIB_CEC->AddLog(CEC_LOG_DEBUG, "using persisted device name setting: '%s'", buf); |
a75e3a5a LOK |
243 | return true; |
244 | } | |
245 | ||
7c5c5bf4 | 246 | bool CUSBCECAdapterCommands::RequestSettingPhysicalAddress(void) |
a75e3a5a | 247 | { |
b509ba1a | 248 | #ifdef CEC_DEBUGGING |
004b8382 | 249 | LIB_CEC->AddLog(CEC_LOG_DEBUG, "requesting physical address setting"); |
b509ba1a | 250 | #endif |
a75e3a5a LOK |
251 | |
252 | cec_datapacket response = RequestSetting(MSGCODE_GET_PHYSICAL_ADDRESS); | |
253 | if (response.size == 2) | |
254 | { | |
7c5c5bf4 | 255 | m_persistedConfiguration.iPhysicalAddress = ((uint16_t)response[0] << 8) | ((uint16_t)response[1]); |
004b8382 | 256 | LIB_CEC->AddLog(CEC_LOG_DEBUG, "using persisted physical address setting: '%4x'", m_persistedConfiguration.iPhysicalAddress); |
a75e3a5a LOK |
257 | return true; |
258 | } | |
004b8382 | 259 | LIB_CEC->AddLog(CEC_LOG_DEBUG, "no persisted physical address setting"); |
a75e3a5a LOK |
260 | return false; |
261 | } | |
262 | ||
263 | bool CUSBCECAdapterCommands::SetSettingAutoEnabled(bool enabled) | |
264 | { | |
2b44051c | 265 | bool bReturn(false); |
7c5c5bf4 LOK |
266 | |
267 | /* check whether this value was changed */ | |
c8b0c13a LOK |
268 | { |
269 | CLockObject lock(m_mutex); | |
270 | if (m_bSettingAutoEnabled == enabled) | |
271 | return bReturn; | |
272 | m_bNeedsWrite = true; | |
273 | } | |
7c5c5bf4 | 274 | |
004b8382 | 275 | LIB_CEC->AddLog(CEC_LOG_DEBUG, "turning autonomous mode %s", enabled ? "on" : "off"); |
a75e3a5a LOK |
276 | |
277 | CCECAdapterMessage params; | |
278 | params.PushEscaped(enabled ? 1 : 0); | |
279 | CCECAdapterMessage *message = m_comm->SendCommand(MSGCODE_SET_AUTO_ENABLED, params); | |
7c5c5bf4 | 280 | bReturn = message->state == ADAPTER_MESSAGE_STATE_SENT_ACKED; |
a75e3a5a | 281 | delete message; |
7c5c5bf4 LOK |
282 | |
283 | if (bReturn) | |
c8b0c13a LOK |
284 | { |
285 | CLockObject lock(m_mutex); | |
7c5c5bf4 | 286 | m_bSettingAutoEnabled = enabled; |
c8b0c13a | 287 | } |
7c5c5bf4 | 288 | |
a75e3a5a LOK |
289 | return bReturn; |
290 | } | |
291 | ||
292 | bool CUSBCECAdapterCommands::SetSettingDeviceType(cec_device_type type) | |
293 | { | |
2b44051c | 294 | bool bReturn(false); |
7c5c5bf4 LOK |
295 | |
296 | /* check whether this value was changed */ | |
c8b0c13a LOK |
297 | { |
298 | CLockObject lock(m_mutex); | |
299 | if (m_persistedConfiguration.deviceTypes.types[0] == type) | |
300 | return bReturn; | |
301 | m_bNeedsWrite = true; | |
302 | } | |
7c5c5bf4 | 303 | |
004b8382 | 304 | LIB_CEC->AddLog(CEC_LOG_DEBUG, "setting the device type to %X (previous: %X)", (uint8_t)type, (uint8_t)m_persistedConfiguration.deviceTypes.types[0]); |
a75e3a5a LOK |
305 | |
306 | CCECAdapterMessage params; | |
307 | params.PushEscaped((uint8_t)type); | |
308 | CCECAdapterMessage *message = m_comm->SendCommand(MSGCODE_SET_DEVICE_TYPE, params); | |
7c5c5bf4 | 309 | bReturn = message->state == ADAPTER_MESSAGE_STATE_SENT_ACKED; |
a75e3a5a | 310 | delete message; |
7c5c5bf4 | 311 | |
c8b0c13a LOK |
312 | if (bReturn) |
313 | { | |
314 | CLockObject lock(m_mutex); | |
315 | m_persistedConfiguration.deviceTypes.types[0] = type; | |
316 | } | |
317 | ||
a75e3a5a LOK |
318 | return bReturn; |
319 | } | |
320 | ||
321 | bool CUSBCECAdapterCommands::SetSettingDefaultLogicalAddress(cec_logical_address address) | |
322 | { | |
2b44051c | 323 | bool bReturn(false); |
7c5c5bf4 LOK |
324 | |
325 | /* check whether this value was changed */ | |
c8b0c13a LOK |
326 | { |
327 | CLockObject lock(m_mutex); | |
328 | if (m_persistedConfiguration.logicalAddresses.primary == address) | |
329 | return bReturn; | |
330 | m_bNeedsWrite = true; | |
331 | } | |
7c5c5bf4 | 332 | |
004b8382 | 333 | LIB_CEC->AddLog(CEC_LOG_DEBUG, "setting the default logical address to %X (previous: %X)", (uint8_t)address, (uint8_t)m_persistedConfiguration.logicalAddresses.primary); |
a75e3a5a LOK |
334 | |
335 | CCECAdapterMessage params; | |
336 | params.PushEscaped((uint8_t)address); | |
337 | CCECAdapterMessage *message = m_comm->SendCommand(MSGCODE_SET_DEFAULT_LOGICAL_ADDRESS, params); | |
7c5c5bf4 | 338 | bReturn = message->state == ADAPTER_MESSAGE_STATE_SENT_ACKED; |
a75e3a5a | 339 | delete message; |
7c5c5bf4 LOK |
340 | |
341 | if (bReturn) | |
c8b0c13a LOK |
342 | { |
343 | CLockObject lock(m_mutex); | |
7c5c5bf4 | 344 | m_persistedConfiguration.logicalAddresses.primary = address; |
c8b0c13a | 345 | } |
7c5c5bf4 | 346 | |
a75e3a5a LOK |
347 | return bReturn; |
348 | } | |
349 | ||
350 | bool CUSBCECAdapterCommands::SetSettingLogicalAddressMask(uint16_t iMask) | |
351 | { | |
2b44051c | 352 | bool bReturn(false); |
7c5c5bf4 LOK |
353 | |
354 | /* check whether this value was changed */ | |
c8b0c13a LOK |
355 | { |
356 | CLockObject lock(m_mutex); | |
357 | if (m_iSettingLAMask == iMask) | |
358 | return bReturn; | |
359 | m_bNeedsWrite = true; | |
360 | } | |
7c5c5bf4 | 361 | |
004b8382 | 362 | LIB_CEC->AddLog(CEC_LOG_DEBUG, "setting the logical address mask to %2X (previous: %2X)", iMask, m_iSettingLAMask); |
a75e3a5a LOK |
363 | |
364 | CCECAdapterMessage params; | |
365 | params.PushEscaped(iMask >> 8); | |
366 | params.PushEscaped((uint8_t)iMask); | |
367 | CCECAdapterMessage *message = m_comm->SendCommand(MSGCODE_SET_LOGICAL_ADDRESS_MASK, params); | |
7c5c5bf4 | 368 | bReturn = message->state == ADAPTER_MESSAGE_STATE_SENT_ACKED; |
a75e3a5a | 369 | delete message; |
7c5c5bf4 LOK |
370 | |
371 | if (bReturn) | |
c8b0c13a LOK |
372 | { |
373 | CLockObject lock(m_mutex); | |
7c5c5bf4 | 374 | m_iSettingLAMask = iMask; |
c8b0c13a | 375 | } |
7c5c5bf4 | 376 | |
a75e3a5a LOK |
377 | return bReturn; |
378 | } | |
379 | ||
380 | bool CUSBCECAdapterCommands::SetSettingPhysicalAddress(uint16_t iPhysicalAddress) | |
381 | { | |
2b44051c | 382 | bool bReturn(false); |
7c5c5bf4 LOK |
383 | |
384 | /* check whether this value was changed */ | |
c8b0c13a LOK |
385 | { |
386 | CLockObject lock(m_mutex); | |
387 | if (m_persistedConfiguration.iPhysicalAddress == iPhysicalAddress) | |
388 | return bReturn; | |
389 | m_bNeedsWrite = true; | |
390 | } | |
7c5c5bf4 | 391 | |
004b8382 | 392 | LIB_CEC->AddLog(CEC_LOG_DEBUG, "setting the physical address to %04X (previous: %04X)", iPhysicalAddress, m_persistedConfiguration.iPhysicalAddress); |
a75e3a5a LOK |
393 | |
394 | CCECAdapterMessage params; | |
395 | params.PushEscaped(iPhysicalAddress >> 8); | |
396 | params.PushEscaped((uint8_t)iPhysicalAddress); | |
397 | CCECAdapterMessage *message = m_comm->SendCommand(MSGCODE_SET_PHYSICAL_ADDRESS, params); | |
7c5c5bf4 | 398 | bReturn = message->state == ADAPTER_MESSAGE_STATE_SENT_ACKED; |
a75e3a5a | 399 | delete message; |
7c5c5bf4 LOK |
400 | |
401 | if (bReturn) | |
c8b0c13a LOK |
402 | { |
403 | CLockObject lock(m_mutex); | |
7c5c5bf4 | 404 | m_persistedConfiguration.iPhysicalAddress = iPhysicalAddress; |
c8b0c13a | 405 | } |
7c5c5bf4 | 406 | |
a75e3a5a LOK |
407 | return bReturn; |
408 | } | |
409 | ||
410 | bool CUSBCECAdapterCommands::SetSettingCECVersion(cec_version version) | |
411 | { | |
2b44051c | 412 | bool bReturn(false); |
7c5c5bf4 LOK |
413 | |
414 | /* check whether this value was changed */ | |
c8b0c13a LOK |
415 | { |
416 | CLockObject lock(m_mutex); | |
417 | if (m_settingCecVersion == version) | |
418 | return bReturn; | |
419 | m_bNeedsWrite = true; | |
420 | } | |
7c5c5bf4 | 421 | |
004b8382 | 422 | LIB_CEC->AddLog(CEC_LOG_DEBUG, "setting the CEC version to %s (previous: %s)", ToString(version), ToString(m_settingCecVersion)); |
a75e3a5a LOK |
423 | |
424 | CCECAdapterMessage params; | |
425 | params.PushEscaped((uint8_t)version); | |
426 | CCECAdapterMessage *message = m_comm->SendCommand(MSGCODE_SET_HDMI_VERSION, params); | |
7c5c5bf4 | 427 | bReturn = message->state == ADAPTER_MESSAGE_STATE_SENT_ACKED; |
a75e3a5a | 428 | delete message; |
7c5c5bf4 LOK |
429 | |
430 | if (bReturn) | |
c8b0c13a LOK |
431 | { |
432 | CLockObject lock(m_mutex); | |
7c5c5bf4 | 433 | m_settingCecVersion = version; |
c8b0c13a | 434 | } |
7c5c5bf4 | 435 | |
a75e3a5a LOK |
436 | return bReturn; |
437 | } | |
438 | ||
439 | bool CUSBCECAdapterCommands::SetSettingOSDName(const char *strOSDName) | |
440 | { | |
2b44051c | 441 | bool bReturn(false); |
7c5c5bf4 LOK |
442 | |
443 | /* check whether this value was changed */ | |
444 | if (!strcmp(m_persistedConfiguration.strDeviceName, strOSDName)) | |
7c5c5bf4 | 445 | return bReturn; |
7c5c5bf4 | 446 | |
004b8382 | 447 | LIB_CEC->AddLog(CEC_LOG_DEBUG, "setting the OSD name to %s (previous: %s)", strOSDName, m_persistedConfiguration.strDeviceName); |
a75e3a5a LOK |
448 | |
449 | CCECAdapterMessage params; | |
450 | for (size_t iPtr = 0; iPtr < strlen(strOSDName); iPtr++) | |
451 | params.PushEscaped(strOSDName[iPtr]); | |
452 | CCECAdapterMessage *message = m_comm->SendCommand(MSGCODE_SET_OSD_NAME, params); | |
7c5c5bf4 | 453 | bReturn = message->state == ADAPTER_MESSAGE_STATE_SENT_ACKED; |
a75e3a5a | 454 | delete message; |
7c5c5bf4 LOK |
455 | |
456 | if (bReturn) | |
457 | snprintf(m_persistedConfiguration.strDeviceName, 13, "%s", strOSDName); | |
458 | ||
a75e3a5a LOK |
459 | return bReturn; |
460 | } | |
461 | ||
462 | bool CUSBCECAdapterCommands::WriteEEPROM(void) | |
463 | { | |
c8b0c13a LOK |
464 | { |
465 | CLockObject lock(m_mutex); | |
466 | if (!m_bNeedsWrite) | |
467 | return true; | |
468 | } | |
7c5c5bf4 | 469 | |
004b8382 | 470 | LIB_CEC->AddLog(CEC_LOG_DEBUG, "writing settings in the EEPROM"); |
a75e3a5a LOK |
471 | |
472 | CCECAdapterMessage params; | |
473 | CCECAdapterMessage *message = m_comm->SendCommand(MSGCODE_WRITE_EEPROM, params); | |
c8b0c13a | 474 | bool bReturn = message->state == ADAPTER_MESSAGE_STATE_SENT_ACKED; |
a75e3a5a | 475 | delete message; |
c8b0c13a LOK |
476 | |
477 | if (bReturn) | |
478 | { | |
479 | CLockObject lock(m_mutex); | |
480 | m_bNeedsWrite = false; | |
481 | } | |
482 | ||
483 | return bReturn; | |
a75e3a5a LOK |
484 | } |
485 | ||
c0152c09 | 486 | bool CUSBCECAdapterCommands::PersistConfiguration(const libcec_configuration &configuration) |
a75e3a5a | 487 | { |
2b44051c | 488 | bool bReturn(false); |
7c5c5bf4 | 489 | if (m_persistedConfiguration.iFirmwareVersion < 2) |
2b44051c | 490 | return bReturn; |
7c5c5bf4 LOK |
491 | |
492 | if (!RequestSettings()) | |
2b44051c LOK |
493 | return bReturn; |
494 | ||
495 | bReturn |= SetSettingAutoEnabled(true); | |
496 | bReturn |= SetSettingDeviceType(CLibCEC::GetType(configuration.logicalAddresses.primary)); | |
497 | bReturn |= SetSettingDefaultLogicalAddress(configuration.logicalAddresses.primary); | |
498 | bReturn |= SetSettingLogicalAddressMask(CLibCEC::GetMaskForType(configuration.logicalAddresses.primary)); | |
499 | bReturn |= SetSettingPhysicalAddress(configuration.iPhysicalAddress); | |
500 | bReturn |= SetSettingCECVersion(configuration.clientVersion >= CEC_CLIENT_VERSION_1_8_0 ? configuration.cecVersion : CEC_VERSION_1_4); | |
501 | bReturn |= SetSettingOSDName(configuration.strDeviceName); | |
a75e3a5a | 502 | |
a75e3a5a LOK |
503 | return bReturn; |
504 | } | |
505 | ||
7c5c5bf4 | 506 | bool CUSBCECAdapterCommands::RequestSettings(void) |
a75e3a5a | 507 | { |
7c5c5bf4 | 508 | if (m_persistedConfiguration.iFirmwareVersion < 2) |
a75e3a5a | 509 | { |
004b8382 | 510 | LIB_CEC->AddLog(CEC_LOG_DEBUG, "%s - firmware version %d does not have any eeprom settings", __FUNCTION__, m_persistedConfiguration.iFirmwareVersion); |
7c5c5bf4 LOK |
511 | // settings can only be persisted with firmware v2+ |
512 | return false; | |
a75e3a5a LOK |
513 | } |
514 | ||
4c70b3b6 | 515 | if (m_bSettingsRetrieved) |
7c5c5bf4 | 516 | return true; |
a75e3a5a | 517 | |
7c5c5bf4 LOK |
518 | bool bReturn(true); |
519 | bReturn &= RequestSettingAutoEnabled(); | |
520 | bReturn &= RequestSettingCECVersion(); | |
521 | bReturn &= RequestSettingDefaultLogicalAddress(); | |
522 | bReturn &= RequestSettingDeviceType(); | |
523 | bReturn &= RequestSettingLogicalAddressMask(); | |
524 | bReturn &= RequestSettingOSDName(); | |
525 | bReturn &= RequestSettingPhysicalAddress(); | |
a75e3a5a LOK |
526 | |
527 | // don't read the following settings: | |
528 | // - auto enabled (always enabled) | |
529 | // - default logical address (autodetected) | |
530 | // - logical address mask (autodetected) | |
531 | // - CEC version (1.3a) | |
532 | ||
533 | // TODO to be added to the firmware: | |
534 | // - base device (4 bits) | |
535 | // - HDMI port number (4 bits) | |
536 | // - TV vendor id (12 bits) | |
537 | // - wake devices (8 bits) | |
538 | // - standby devices (8 bits) | |
539 | // - use TV menu language (1 bit) | |
540 | // - activate source (1 bit) | |
541 | // - power off screensaver (1 bit) | |
542 | // - power off on standby (1 bit) | |
543 | // - send inactive source (1 bit) | |
7c5c5bf4 LOK |
544 | |
545 | m_bSettingsRetrieved = true; | |
546 | ||
a75e3a5a LOK |
547 | return bReturn; |
548 | } | |
549 | ||
c0152c09 | 550 | bool CUSBCECAdapterCommands::GetConfiguration(libcec_configuration &configuration) |
7c5c5bf4 LOK |
551 | { |
552 | // get the settings from the eeprom if needed | |
553 | if (!RequestSettings()) | |
554 | return false; | |
555 | ||
556 | // copy the settings | |
c0152c09 LOK |
557 | configuration.iFirmwareVersion = m_persistedConfiguration.iFirmwareVersion; |
558 | configuration.deviceTypes = m_persistedConfiguration.deviceTypes; | |
559 | configuration.iPhysicalAddress = m_persistedConfiguration.iPhysicalAddress; | |
560 | snprintf(configuration.strDeviceName, 13, "%s", m_persistedConfiguration.strDeviceName); | |
7c5c5bf4 LOK |
561 | |
562 | return true; | |
563 | } | |
564 | ||
a75e3a5a LOK |
565 | bool CUSBCECAdapterCommands::PingAdapter(void) |
566 | { | |
b509ba1a | 567 | #ifdef CEC_DEBUGGING |
004b8382 | 568 | LIB_CEC->AddLog(CEC_LOG_DEBUG, "sending ping"); |
b509ba1a | 569 | #endif |
a75e3a5a LOK |
570 | |
571 | CCECAdapterMessage params; | |
572 | CCECAdapterMessage *message = m_comm->SendCommand(MSGCODE_PING, params); | |
573 | bool bReturn = message->state == ADAPTER_MESSAGE_STATE_SENT_ACKED; | |
574 | delete message; | |
575 | return bReturn; | |
576 | } | |
577 | ||
578 | bool CUSBCECAdapterCommands::SetAckMask(uint16_t iMask) | |
579 | { | |
004b8382 | 580 | LIB_CEC->AddLog(CEC_LOG_DEBUG, "setting ackmask to %2x", iMask); |
a75e3a5a LOK |
581 | |
582 | CCECAdapterMessage params; | |
583 | params.PushEscaped(iMask >> 8); | |
584 | params.PushEscaped((uint8_t)iMask); | |
585 | CCECAdapterMessage *message = m_comm->SendCommand(MSGCODE_SET_ACK_MASK, params); | |
586 | bool bReturn = message->state == ADAPTER_MESSAGE_STATE_SENT_ACKED; | |
587 | delete message; | |
588 | return bReturn; | |
589 | } | |
590 | ||
591 | bool CUSBCECAdapterCommands::StartBootloader(void) | |
592 | { | |
004b8382 | 593 | LIB_CEC->AddLog(CEC_LOG_DEBUG, "starting the bootloader"); |
a75e3a5a LOK |
594 | |
595 | CCECAdapterMessage params; | |
596 | CCECAdapterMessage *message = m_comm->SendCommand(MSGCODE_START_BOOTLOADER, params); | |
597 | bool bReturn = message->state == ADAPTER_MESSAGE_STATE_SENT_ACKED; | |
598 | delete message; | |
599 | return bReturn; | |
600 | } | |
601 | ||
602 | bool CUSBCECAdapterCommands::SetLineTimeout(uint8_t iTimeout) | |
603 | { | |
004b8382 | 604 | LIB_CEC->AddLog(CEC_LOG_DEBUG, "setting the line timeout to %d", iTimeout); |
a75e3a5a LOK |
605 | CCECAdapterMessage params; |
606 | params.PushEscaped(iTimeout); | |
607 | CCECAdapterMessage *message = m_comm->SendCommand(MSGCODE_TRANSMIT_IDLETIME, params); | |
608 | bool bReturn = message->state == ADAPTER_MESSAGE_STATE_SENT_ACKED; | |
609 | delete message; | |
610 | return bReturn; | |
611 | } | |
612 | ||
613 | bool CUSBCECAdapterCommands::SetControlledMode(bool controlled) | |
614 | { | |
a38292a3 LOK |
615 | { |
616 | CLockObject lock(m_mutex); | |
617 | if (m_bControlledMode == controlled) | |
618 | return true; | |
619 | } | |
620 | ||
004b8382 | 621 | LIB_CEC->AddLog(CEC_LOG_DEBUG, "turning controlled mode %s", controlled ? "on" : "off"); |
a75e3a5a LOK |
622 | |
623 | CCECAdapterMessage params; | |
624 | params.PushEscaped(controlled ? 1 : 0); | |
625 | CCECAdapterMessage *message = m_comm->SendCommand(MSGCODE_SET_CONTROLLED, params); | |
626 | bool bReturn = message->state == ADAPTER_MESSAGE_STATE_SENT_ACKED; | |
627 | delete message; | |
a38292a3 LOK |
628 | |
629 | if (bReturn) | |
630 | { | |
631 | CLockObject lock(m_mutex); | |
632 | m_bControlledMode = controlled; | |
633 | } | |
634 | ||
a75e3a5a LOK |
635 | return bReturn; |
636 | } |