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