libcec-sys 9.0.3

FFI bindings to libcec
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
/*
 * This file is part of the libCEC(R) library.
 *
 * libCEC(R) is Copyright (C) 2011-2015 Pulse-Eight Limited.  All rights reserved.
 * libCEC(R) is an original work, containing original code.
 *
 * libCEC(R) is a trademark of Pulse-Eight Limited.
 *
 * This program is dual-licensed; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 * 02110-1301  USA
 *
 *
 * Alternatively, you can license this library under a commercial license,
 * please contact Pulse-Eight Licensing for more information.
 *
 * For more information contact:
 * Pulse-Eight Licensing       <license@pulse-eight.com>
 *     http://www.pulse-eight.com/
 *     http://www.pulse-eight.net/
 */

#include "env.h"
#include "USBCECAdapterCommands.h"

#include "USBCECAdapterMessage.h"
#include "USBCECAdapterCommunication.h"
#include "LibCEC.h"
#include "CECProcessor.h"
#include "CECTypeUtils.h"
#include "p8-platform/util/util.h"
#include <stdio.h>

using namespace CEC;
using namespace P8PLATFORM;

#define LIB_CEC     m_comm->m_callback->GetLib()
#define ToString(p) CCECTypeUtils::ToString(p)

CUSBCECAdapterCommands::CUSBCECAdapterCommands(CUSBCECAdapterCommunication *comm) :
    m_comm(comm),
    m_bSettingsRetrieved(false),
    m_bSettingAutoEnabled(false),
    m_iSettingLAMask(0),
    m_bNeedsWrite(false),
    m_bControlledMode(false),
    m_adapterType(P8_ADAPTERTYPE_UNKNOWN)
{
  m_savedConfiguration.Clear();
}

cec_datapacket CUSBCECAdapterCommands::RequestSetting(cec_adapter_messagecode msgCode)
{
  cec_datapacket retVal;
  retVal.Clear();

  CCECAdapterMessage params;
  CCECAdapterMessage *message = m_comm->SendCommand(msgCode, params);
  if (!!message &&
      (message->state == ADAPTER_MESSAGE_STATE_SENT_ACKED) &&
      (message->m_rx_len >= 3))
  {
    // shift out start, msgcode and end
    memcpy(retVal.data, &message->m_rx_data[2], message->m_rx_len - 3);
    retVal.size = message->m_rx_len - 3;
  }

  SAFE_DELETE(message);
  return retVal;
}

uint16_t CUSBCECAdapterCommands::RequestFirmwareVersion(void)
{
  m_savedConfiguration.iFirmwareVersion = CEC_FW_VERSION_UNKNOWN;
  unsigned int iFwVersionTry(0);

  while (m_savedConfiguration.iFirmwareVersion == CEC_FW_VERSION_UNKNOWN && iFwVersionTry++ < 3)
  {
#ifdef CEC_DEBUGGING
    LIB_CEC->AddLog(CEC_LOG_DEBUG, "usbcec: requesting the firmware version");
#endif
    cec_datapacket response = RequestSetting(MSGCODE_FIRMWARE_VERSION);
    if (response.size == 2)
      m_savedConfiguration.iFirmwareVersion = (response[0] << 8 | response[1]);
    else
    {
      LIB_CEC->AddLog(CEC_LOG_WARNING, "the adapter did not respond with a correct firmware version (try %d, size = %d)", iFwVersionTry, response.size);
      CEvent::Sleep(500);
    }
  }

  if (m_savedConfiguration.iFirmwareVersion == CEC_FW_VERSION_UNKNOWN)
  {
    LIB_CEC->AddLog(CEC_LOG_DEBUG, "usbcec: defaulting to firmware version 1");
    m_savedConfiguration.iFirmwareVersion = 1;
  }

  // firmware versions < 2 don't have an autonomous mode
  if (m_savedConfiguration.iFirmwareVersion < 2)
    m_bControlledMode = true;

  return m_savedConfiguration.iFirmwareVersion;
}

bool CUSBCECAdapterCommands::RequestSettingAutoEnabled(void)
{
#ifdef CEC_DEBUGGING
  LIB_CEC->AddLog(CEC_LOG_DEBUG, "usbcec: requesting autonomous mode setting");
#endif

  cec_datapacket response = RequestSetting(MSGCODE_GET_AUTO_ENABLED);
  if (response.size == 1)
  {
    m_bSettingAutoEnabled = response[0] == 1;
    LIB_CEC->AddLog(CEC_LOG_DEBUG, "usbcec: autonomous mode = %s", m_bSettingAutoEnabled ? "enabled" : "disabled");
    return true;
  }
  return false;
}

bool CUSBCECAdapterCommands::RequestSettingAutoPowerOn(void)
{
#if CEC_LIB_VERSION_MAJOR >= 5
#ifdef CEC_DEBUGGING
  LIB_CEC->AddLog(CEC_LOG_DEBUG, "usbcec: requesting auto power on setting");
#endif

  cec_datapacket response = RequestSetting(MSGCODE_GET_AUTO_POWER_ON);
  if (response.size == 1)
  {
    m_savedConfiguration.bAutoPowerOn = response[0];
    LIB_CEC->AddLog(CEC_LOG_DEBUG, "usbcec: auto power on = %s", m_savedConfiguration.bAutoPowerOn ? "enabled" : "disabled");
    return true;
  }
#endif
  return false;
}

bool CUSBCECAdapterCommands::RequestSettingCECVersion(void)
{
#ifdef CEC_DEBUGGING
  LIB_CEC->AddLog(CEC_LOG_DEBUG, "usbcec: requesting CEC version setting");
#endif

  cec_datapacket response = RequestSetting(MSGCODE_GET_HDMI_VERSION);
  if (response.size == 1)
  {
    m_savedConfiguration.cecVersion = (cec_version)response[0];
    LIB_CEC->AddLog(CEC_LOG_DEBUG, "usbcec: CEC version = %s", ToString(m_savedConfiguration.cecVersion));
    return true;
  }
  return false;
}

p8_cec_adapter_type CUSBCECAdapterCommands::RequestAdapterType(void)
{
  if (m_adapterType == P8_ADAPTERTYPE_UNKNOWN)
  {
#ifdef CEC_DEBUGGING
    LIB_CEC->AddLog(CEC_LOG_DEBUG, "usbcec: requesting adapter type");
#endif

    cec_datapacket response = RequestSetting(MSGCODE_GET_ADAPTER_TYPE);
    if (response.size == 1)
      m_adapterType = (p8_cec_adapter_type)response[0];
  }
  return m_adapterType;
}

uint32_t CUSBCECAdapterCommands::RequestBuildDate(void)
{
  if (m_savedConfiguration.iFirmwareBuildDate == CEC_FW_BUILD_UNKNOWN)
  {
#ifdef CEC_DEBUGGING
    LIB_CEC->AddLog(CEC_LOG_DEBUG, "usbcec: requesting firmware build date");
#endif

    cec_datapacket response = RequestSetting(MSGCODE_GET_BUILDDATE);
    if (response.size == 4)
      m_savedConfiguration.iFirmwareBuildDate = (uint32_t)response[0] << 24 | (uint32_t)response[1] << 16 | (uint32_t)response[2] << 8 | (uint32_t)response[3];
  }
  return m_savedConfiguration.iFirmwareBuildDate;
}

bool CUSBCECAdapterCommands::RequestSettingDefaultLogicalAddress(void)
{
#ifdef CEC_DEBUGGING
  LIB_CEC->AddLog(CEC_LOG_DEBUG, "usbcec: requesting default logical address setting");
#endif

  cec_datapacket response = RequestSetting(MSGCODE_GET_DEFAULT_LOGICAL_ADDRESS);
  if (response.size == 1)
  {
    m_savedConfiguration.logicalAddresses.primary = (cec_logical_address)response[0];
    LIB_CEC->AddLog(CEC_LOG_DEBUG, "usbcec: logical address = %s", ToString(m_savedConfiguration.logicalAddresses.primary));
    return true;
  }
  return false;
}

bool CUSBCECAdapterCommands::RequestSettingDeviceType(void)
{
#ifdef CEC_DEBUGGING
  LIB_CEC->AddLog(CEC_LOG_DEBUG, "usbcec: requesting device type setting");
#endif
  m_savedConfiguration.deviceTypes.Clear();

  cec_datapacket response = RequestSetting(MSGCODE_GET_DEVICE_TYPE);
  if (response.size == 1)
  {
    m_savedConfiguration.deviceTypes.Add((cec_device_type)response[0]);
    LIB_CEC->AddLog(CEC_LOG_DEBUG, "usbcec: device type = %s", ToString((cec_device_type)response[0]));
    return true;
  }
  LIB_CEC->AddLog(CEC_LOG_DEBUG, "usbcec: device type = (not set)");
  return false;
}

bool CUSBCECAdapterCommands::RequestSettingLogicalAddressMask(void)
{
#ifdef CEC_DEBUGGING
  LIB_CEC->AddLog(CEC_LOG_DEBUG, "usbcec: requesting logical address mask setting");
#endif

  cec_datapacket response = RequestSetting(MSGCODE_GET_LOGICAL_ADDRESS_MASK);
  if (response.size == 2)
  {
    m_iSettingLAMask = ((uint16_t)response[0] << 8) | ((uint16_t)response[1]);
    LIB_CEC->AddLog(CEC_LOG_DEBUG, "usbcec: logical address mask = %x", m_iSettingLAMask);
    return true;
  }
  return false;
}

bool CUSBCECAdapterCommands::RequestSettingOSDName(void)
{
#ifdef CEC_DEBUGGING
  LIB_CEC->AddLog(CEC_LOG_DEBUG, "usbcec: requesting OSD name setting");
#endif

  cec_datapacket response = RequestSetting(MSGCODE_GET_OSD_NAME);
  if (response.size == 0)
  {
    LIB_CEC->AddLog(CEC_LOG_DEBUG, "usbcec: device name = (not set)");
    m_savedConfiguration.strDeviceName[0] = (char)0;
    return false;
  }

  memcpy(m_savedConfiguration.strDeviceName, response.data, response.size <= LIBCEC_OSD_NAME_SIZE ? response.size : LIBCEC_OSD_NAME_SIZE);
  if (response.size < LIBCEC_OSD_NAME_SIZE) {
    m_savedConfiguration.strDeviceName[response.size] = (char)0;
  }
  return true;
}

bool CUSBCECAdapterCommands::RequestSettingPhysicalAddress(void)
{
#ifdef CEC_DEBUGGING
  LIB_CEC->AddLog(CEC_LOG_DEBUG, "usbcec: requesting physical address setting");
#endif

  cec_datapacket response = RequestSetting(MSGCODE_GET_PHYSICAL_ADDRESS);
  if (response.size == 2)
  {
    m_savedConfiguration.iPhysicalAddress = ((uint16_t)response[0] << 8) | ((uint16_t)response[1]);
    LIB_CEC->AddLog(CEC_LOG_DEBUG, "usbcec: physical address = %04x", m_savedConfiguration.iPhysicalAddress);
    return true;
  }
  LIB_CEC->AddLog(CEC_LOG_DEBUG, "usbcec: physical address = (not set)");
  return false;
}

bool CUSBCECAdapterCommands::SetSettingAutoEnabled(bool enabled)
{
  bool bReturn(false);

  /* check whether this value was changed */
  {
    CLockObject lock(m_mutex);
    if (m_bSettingAutoEnabled == enabled)
      return bReturn;
    m_bNeedsWrite = true;
  }

  CCECAdapterMessage params;
  params.PushEscaped(enabled ? 1 : 0);
  CCECAdapterMessage *message = m_comm->SendCommand(MSGCODE_SET_AUTO_ENABLED, params);
  bReturn = message && message->state == ADAPTER_MESSAGE_STATE_SENT_ACKED;
  SAFE_DELETE(message);

  if (bReturn)
  {
    LIB_CEC->AddLog(CEC_LOG_DEBUG, "usbcec: updating autonomous mode: %s", enabled ? "enabled" : "disabled");
    CLockObject lock(m_mutex);
    m_bSettingAutoEnabled = enabled;
  }
  else
  {
    LIB_CEC->AddLog(CEC_LOG_WARNING, "usbcec: failed to %s autonomous mode", enabled ? "enable" : "disable");
  }

  return bReturn;
}

bool CUSBCECAdapterCommands::SetSettingDeviceType(cec_device_type type)
{
  bool bReturn(false);

  /* check whether this value was changed */
  {
    CLockObject lock(m_mutex);
    if (m_savedConfiguration.deviceTypes.types[0] == type)
      return bReturn;
    m_bNeedsWrite = true;
  }

  CCECAdapterMessage params;
  params.PushEscaped((uint8_t)type);
  CCECAdapterMessage *message = m_comm->SendCommand(MSGCODE_SET_DEVICE_TYPE, params);
  bReturn = message && message->state == ADAPTER_MESSAGE_STATE_SENT_ACKED;
  SAFE_DELETE(message);

  if (bReturn)
  {
    LIB_CEC->AddLog(CEC_LOG_DEBUG, "usbcec: updated device type: %s -> %s",
        ToString(m_savedConfiguration.deviceTypes.types[0]),
        ToString(type));
    CLockObject lock(m_mutex);
    m_savedConfiguration.deviceTypes.types[0] = type;
  }
  else
  {
    LIB_CEC->AddLog(CEC_LOG_WARNING, "usbcec: failed to update device type to %s", ToString(type));
  }

  return bReturn;
}

bool CUSBCECAdapterCommands::SetSettingDefaultLogicalAddress(cec_logical_address address)
{
  bool bReturn(false);

  /* check whether this value was changed */
  {
    CLockObject lock(m_mutex);
    if (m_savedConfiguration.logicalAddresses.primary == address)
      return bReturn;
    m_bNeedsWrite = true;
  }

  CCECAdapterMessage params;
  params.PushEscaped((uint8_t)address);
  CCECAdapterMessage *message = m_comm->SendCommand(MSGCODE_SET_DEFAULT_LOGICAL_ADDRESS, params);
  bReturn = message && message->state == ADAPTER_MESSAGE_STATE_SENT_ACKED;
  SAFE_DELETE(message);

  if (bReturn)
  {
    LIB_CEC->AddLog(CEC_LOG_DEBUG, "usbcec: updated default logical address: %s -> %s",
        ToString(m_savedConfiguration.logicalAddresses.primary),
        ToString(address));
    CLockObject lock(m_mutex);
    m_savedConfiguration.logicalAddresses.primary = address;
  }
  else
  {
    LIB_CEC->AddLog(CEC_LOG_WARNING, "usbcec: failed to update default logical address to %s", ToString(address));
  }

  return bReturn;
}

bool CUSBCECAdapterCommands::SetSettingLogicalAddressMask(uint16_t iMask)
{
  bool bReturn(false);

  /* check whether this value was changed */
  {
    CLockObject lock(m_mutex);
    if (m_iSettingLAMask == iMask)
      return bReturn;
    m_bNeedsWrite = true;
  }

  CCECAdapterMessage params;
  params.PushEscaped(iMask >> 8);
  params.PushEscaped((uint8_t)iMask);
  CCECAdapterMessage *message = m_comm->SendCommand(MSGCODE_SET_LOGICAL_ADDRESS_MASK, params);
  bReturn = message && message->state == ADAPTER_MESSAGE_STATE_SENT_ACKED;
  SAFE_DELETE(message);

  if (bReturn)
  {
    LIB_CEC->AddLog(CEC_LOG_DEBUG, "usbcec: updated logical address mask: %02X -> %02X",
        m_iSettingLAMask,
        iMask);
    CLockObject lock(m_mutex);
    m_iSettingLAMask = iMask;
  }
  else
  {
    LIB_CEC->AddLog(CEC_LOG_WARNING, "usbcec: failed to update logical address mask to %02X", iMask);
  }

  return bReturn;
}

bool CUSBCECAdapterCommands::SetSettingPhysicalAddress(uint16_t iPhysicalAddress)
{
  bool bReturn(false);

  /* check whether this value was changed */
  {
    CLockObject lock(m_mutex);
    if (m_savedConfiguration.iPhysicalAddress == iPhysicalAddress)
      return bReturn;
    m_bNeedsWrite = true;
  }

  CCECAdapterMessage params;
  params.PushEscaped(iPhysicalAddress >> 8);
  params.PushEscaped((uint8_t)iPhysicalAddress);
  CCECAdapterMessage *message = m_comm->SendCommand(MSGCODE_SET_PHYSICAL_ADDRESS, params);
  bReturn = message && message->state == ADAPTER_MESSAGE_STATE_SENT_ACKED;
  SAFE_DELETE(message);

  if (bReturn)
  {
    LIB_CEC->AddLog(CEC_LOG_DEBUG, "usbcec: updated physical address: %04X -> %04X",
        m_savedConfiguration.iPhysicalAddress,
        iPhysicalAddress);
    CLockObject lock(m_mutex);
    m_savedConfiguration.iPhysicalAddress = iPhysicalAddress;
  }
  else
  {
    LIB_CEC->AddLog(CEC_LOG_WARNING, "usbcec: failed to update physical address to %04X", iPhysicalAddress);
  }

  return bReturn;
}

bool CUSBCECAdapterCommands::SetSettingAutoPowerOn(bool autoOn)
{
  bool bReturn(false);
#if CEC_LIB_VERSION_MAJOR >= 5
  if (m_savedConfiguration.iFirmwareVersion < 10)
    // only supported by v10+
    return bReturn;

  // check whether this value changed
  {
    CLockObject lock(m_mutex);
    if ((m_savedConfiguration.bAutoPowerOn == 1) == autoOn)
      return bReturn;
    m_bNeedsWrite = true;
  }

  CCECAdapterMessage params;
  params.PushEscaped(autoOn ? 1 : 0);
  CCECAdapterMessage *message = m_comm->SendCommand(MSGCODE_SET_AUTO_POWER_ON, params);
  bReturn = message && message->state == ADAPTER_MESSAGE_STATE_SENT_ACKED;
  SAFE_DELETE(message);

  if (bReturn)
  {
    CLockObject lock(m_mutex);
    m_savedConfiguration.bAutoPowerOn = (autoOn ? 1 : 0);
    LIB_CEC->AddLog(CEC_LOG_NOTICE, "usbcec: auto power on %s", autoOn ? "enabled" : "disabled");
  }
  else
  {
    LIB_CEC->AddLog(CEC_LOG_WARNING, "usbcec: failed to %s auto power on", autoOn ? "enable" : "disable");
  }
#endif

  return bReturn;
}

bool CUSBCECAdapterCommands::SetSettingCECVersion(cec_version version)
{
  bool bReturn(false);

  /* check whether this value was changed */
  {
    CLockObject lock(m_mutex);
    if (m_savedConfiguration.cecVersion == version)
      return bReturn;
    m_bNeedsWrite = true;
  }

  CCECAdapterMessage params;
  params.PushEscaped((uint8_t)version);
  CCECAdapterMessage *message = m_comm->SendCommand(MSGCODE_SET_HDMI_VERSION, params);
  bReturn = message && message->state == ADAPTER_MESSAGE_STATE_SENT_ACKED;
  SAFE_DELETE(message);

  if (bReturn)
  {
    LIB_CEC->AddLog(CEC_LOG_DEBUG, "usbcec: updated CEC version: %s -> %s",
        ToString(m_savedConfiguration.cecVersion),
        ToString(version));
    CLockObject lock(m_mutex);
    m_savedConfiguration.cecVersion = version;
  }
  else
  {
    LIB_CEC->AddLog(CEC_LOG_WARNING, "usbcec: failed to update CEC version to %s", ToString(version));
  }

  return bReturn;
}

bool CUSBCECAdapterCommands::SetSettingOSDName(const char *strOSDName)
{
  bool bReturn(false);

  /* check whether this value was changed */
  if (!strcmp(m_savedConfiguration.strDeviceName, strOSDName))
    return bReturn;

  CCECAdapterMessage params;
  for (size_t iPtr = 0; iPtr < strlen(strOSDName); iPtr++)
    params.PushEscaped(strOSDName[iPtr]);
  CCECAdapterMessage *message = m_comm->SendCommand(MSGCODE_SET_OSD_NAME, params);
  bReturn = message && message->state == ADAPTER_MESSAGE_STATE_SENT_ACKED;
  SAFE_DELETE(message);

  if (bReturn)
  {
    LIB_CEC->AddLog(CEC_LOG_DEBUG, "usbcec: updated OSD name: %s -> %s",
        m_savedConfiguration.strDeviceName,
        strOSDName);
    CLockObject lock(m_mutex);
    snprintf(m_savedConfiguration.strDeviceName, LIBCEC_OSD_NAME_SIZE, "%s", strOSDName);
  }
  else
  {
    LIB_CEC->AddLog(CEC_LOG_WARNING, "usbcec: failed to update OSD name to %s", strOSDName);
  }

  return bReturn;
}

bool CUSBCECAdapterCommands::WriteEEPROM(void)
{
  {
    CLockObject lock(m_mutex);
    if (!m_bNeedsWrite)
      return true;
  }

  CCECAdapterMessage params;
  CCECAdapterMessage *message = m_comm->SendCommand(MSGCODE_WRITE_EEPROM, params);
  bool bReturn = message && message->state == ADAPTER_MESSAGE_STATE_SENT_ACKED;
  SAFE_DELETE(message);

  if (bReturn)
  {
    LIB_CEC->AddLog(CEC_LOG_DEBUG, "usbcec: eeprom updated");
    CLockObject lock(m_mutex);
    m_bNeedsWrite = false;
  }
  else
  {
    LIB_CEC->AddLog(CEC_LOG_DEBUG, "usbcec: failed to update eeprom");
  }

  return bReturn;
}

bool CUSBCECAdapterCommands::SaveConfiguration(const libcec_configuration &configuration)
{
  bool bReturn(false);
  if (m_savedConfiguration.iFirmwareVersion < 2)
    return bReturn;

  if (!RequestSettings())
    return bReturn;

  if (CLibCEC::GetType(configuration.logicalAddresses.primary) != CEC_DEVICE_TYPE_RESERVED)
  {
    bReturn |= SetSettingDeviceType(CLibCEC::GetType(configuration.logicalAddresses.primary));
    bReturn |= SetSettingDefaultLogicalAddress(configuration.logicalAddresses.primary);
    bReturn |= SetSettingLogicalAddressMask(CLibCEC::GetMaskForType(configuration.logicalAddresses.primary));
  }
  else
  {
    bReturn |= SetSettingDeviceType(configuration.deviceTypes[0]);
  }
  bReturn |= SetSettingPhysicalAddress(configuration.iPhysicalAddress);
  bReturn |= SetSettingOSDName(configuration.strDeviceName);
  if (m_savedConfiguration.iFirmwareVersion >= 10)
  {
#if CEC_LIB_VERSION_MAJOR >= 5
    if ((configuration.bAutoPowerOn == 0) || (configuration.bAutoPowerOn == 1))
      bReturn |= SetSettingAutoPowerOn(configuration.bAutoPowerOn == 1);
#else
    bReturn |= SetSettingAutoPowerOn(false);
#endif
  }
  else
  {
    bReturn |= SetSettingCECVersion(configuration.cecVersion);
  }

  return bReturn;
}

bool CUSBCECAdapterCommands::RequestSettings(void)
{
  if (m_savedConfiguration.iFirmwareVersion < 2)
  {
    LIB_CEC->AddLog(CEC_LOG_DEBUG, "%s - firmware version %d does not have any eeprom settings", __FUNCTION__, m_savedConfiguration.iFirmwareVersion);
    // settings can only be saved when using firmware v2+
    return false;
  }

  if (m_bSettingsRetrieved)
    return true;

  bool bReturn(true);
  bReturn |= RequestSettingAutoEnabled();
  bReturn |= RequestSettingDefaultLogicalAddress();
  bReturn |= RequestSettingDeviceType();
  bReturn |= RequestSettingLogicalAddressMask();
  bReturn |= RequestSettingOSDName();
  bReturn |= RequestSettingPhysicalAddress();
  if (m_savedConfiguration.iFirmwareVersion >= 10)
    bReturn |= RequestSettingAutoPowerOn();
  else
    bReturn |= RequestSettingCECVersion();

  // don't read the following settings:
  // - auto enabled (always enabled)
  // - default logical address (autodetected)
  // - logical address mask (autodetected)
  // - CEC version (1.3a)

  m_bSettingsRetrieved = true;

  return bReturn;
}

bool CUSBCECAdapterCommands::GetConfiguration(libcec_configuration &configuration)
{
  // get the settings from the eeprom if needed
  if (!RequestSettings())
    return false;

  // copy the settings
  configuration.iFirmwareVersion   = m_savedConfiguration.iFirmwareVersion;
  configuration.iFirmwareBuildDate = m_savedConfiguration.iFirmwareBuildDate;
  configuration.deviceTypes        = m_savedConfiguration.deviceTypes;
  configuration.iPhysicalAddress   = m_savedConfiguration.iPhysicalAddress;
  configuration.cecVersion         = m_savedConfiguration.cecVersion;
#if CEC_LIB_VERSION_MAJOR >= 5
  configuration.bAutoPowerOn       = m_savedConfiguration.bAutoPowerOn;
#endif
  memcpy(configuration.strDeviceName, m_savedConfiguration.strDeviceName, LIBCEC_OSD_NAME_SIZE);

  return true;
}

bool CUSBCECAdapterCommands::PingAdapter(void)
{
#ifdef CEC_DEBUGGING
  LIB_CEC->AddLog(CEC_LOG_DEBUG, "usbcec: sending ping");
#endif

  CCECAdapterMessage params;
  CCECAdapterMessage *message = m_comm->SendCommand(MSGCODE_PING, params);
  bool bReturn = message && message->state == ADAPTER_MESSAGE_STATE_SENT_ACKED;
  SAFE_DELETE(message);
  return bReturn;
}

bool CUSBCECAdapterCommands::SetAckMask(uint16_t iMask)
{
  LIB_CEC->AddLog(CEC_LOG_DEBUG, "usbcec: updating ackmask: %04X", iMask);

  CCECAdapterMessage params;
  params.PushEscaped(iMask >> 8);
  params.PushEscaped((uint8_t)iMask);
  CCECAdapterMessage *message  = m_comm->SendCommand(MSGCODE_SET_ACK_MASK, params);
  bool bReturn = message && message->state == ADAPTER_MESSAGE_STATE_SENT_ACKED;
  SAFE_DELETE(message);
  return bReturn;
}

void CUSBCECAdapterCommands::SetActiveSource(bool bSetTo, bool bClientUnregistered)
{
  if (bClientUnregistered) return;
  if (m_savedConfiguration.iFirmwareVersion >= 3)
  {
    LIB_CEC->AddLog(CEC_LOG_DEBUG, "usbcec: updating active source status: %s", bSetTo ? "active" : "inactive");
    CCECAdapterMessage params;
    params.PushEscaped(bSetTo ? 1 : 0);
    CCECAdapterMessage *message = m_comm->SendCommand(MSGCODE_SET_ACTIVE_SOURCE, params);
    SAFE_DELETE(message);
  }
}

bool CUSBCECAdapterCommands::StartBootloader(void)
{
  LIB_CEC->AddLog(CEC_LOG_DEBUG, "usbcec: starting the bootloader");

  CCECAdapterMessage params;
  CCECAdapterMessage *message = m_comm->SendCommand(MSGCODE_START_BOOTLOADER, params);
  bool bReturn = message && message->state == ADAPTER_MESSAGE_STATE_SENT_ACKED;
  SAFE_DELETE(message);
  return bReturn;
}

bool CUSBCECAdapterCommands::SetLineTimeout(uint8_t iTimeout)
{
  LIB_CEC->AddLog(CEC_LOG_DEBUG, "usbcec: updating line timeout: %u", iTimeout);
  CCECAdapterMessage params;
  params.PushEscaped(iTimeout);
  CCECAdapterMessage *message = m_comm->SendCommand(MSGCODE_TRANSMIT_IDLETIME, params);
  bool bReturn = message && message->state == ADAPTER_MESSAGE_STATE_SENT_ACKED;
  SAFE_DELETE(message);
  return bReturn;
}

bool CUSBCECAdapterCommands::SetControlledMode(bool controlled)
{
  {
    CLockObject lock(m_mutex);
    if (m_bControlledMode == controlled)
      return true;
  }

  LIB_CEC->AddLog(CEC_LOG_DEBUG, "usbcec: %s controlled mode", controlled ? "enabling" : "disabling");

  CCECAdapterMessage params;
  params.PushEscaped(controlled ? 1 : 0);
  CCECAdapterMessage *message = m_comm->SendCommand(MSGCODE_SET_CONTROLLED, params);
  bool bReturn = message && message->state == ADAPTER_MESSAGE_STATE_SENT_ACKED;
  SAFE_DELETE(message);

  if (bReturn)
  {
    CLockObject lock(m_mutex);
    m_bControlledMode = controlled;
  }

  return bReturn;
}