-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathctrlm_ble_rcu_interface.cpp
More file actions
1692 lines (1365 loc) · 61.9 KB
/
ctrlm_ble_rcu_interface.cpp
File metadata and controls
1692 lines (1365 loc) · 61.9 KB
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
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* If not stated otherwise in this file or this component's license file the
* following copyright and licenses apply:
*
* Copyright 2014 RDK Management
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "ctrlm_ble_rcu_interface.h"
#include "ctrlm_ble_utils.h"
#include "ctrlm_voice_obj.h"
#define CTRLM_BLE_KEY_MSG_QUEUE_MSG_MAX (10)
#define CTRLM_BLE_KEY_MSG_QUEUE_MSG_SIZE_MAX (sizeof(ctrlm_ble_key_queue_device_changed_msg_t))
#define KEY_INPUT_DEVICE_BASE_DIR "/dev/input/"
#define KEY_INPUT_DEVICE_BASE_FILE "event"
using namespace std;
void *KeyMonitorThread(void *data);
static int HandleKeypress(ctrlm_ble_rcu_interface_t *metadata, struct input_event *event, const BleAddress &address);
static int OpenKeyInputDevice(uint64_t ieee_address);
static void FindRcuInputDevices(ctrlm_ble_rcu_interface_t *metadata,
std::map <BleAddress, int> &rcuKeypressFds,
fd_set &rfds,
int &nfds);
ctrlm_ble_rcu_interface_t::ctrlm_ble_rcu_interface_t(json_t *jsonConfig)
: m_isAlive(make_shared<bool>(true))
, m_GMainLoop(NULL)
, m_dbusConnection(NULL)
{
m_keyMonitorThread.name = "";
m_keyMonitorThread.id = 0;
m_keyMonitorThread.running = false;
m_keyThreadMsgQ = XR_MQ_INVALID;
GError *error = NULL;
m_dbusConnection = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &error);
if (m_dbusConnection == NULL) {
XLOGD_ERROR("Failed to create dbus connection, error = <%s>", (error == NULL) ? "" : error->message);
g_clear_error (&error);
return;
}
if(jsonConfig != NULL) {
XLOGD_INFO("using config from json");
m_config = ConfigSettings::fromJsonObj(jsonConfig);
} else {
XLOGD_INFO("using default config");
m_config = ConfigSettings::defaults();
}
// the factory for creating the BleRcu services for each device
m_servicesFactory = std::make_shared<BleRcuServicesFactory>(m_config);
if (!m_servicesFactory) {
XLOGD_ERROR("failed to setup the BLE services factory");
}
}
ctrlm_ble_rcu_interface_t::~ctrlm_ble_rcu_interface_t()
{
*m_isAlive = false;
shutdown();
XLOGD_INFO ("waiting for BLE key monitor thread to exit...");
if (m_keyMonitorThread.running) {
ctrlm_ble_key_queue_msg_header_t msg;
msg.type = CTRLM_BLE_KEY_QUEUE_MSG_TYPE_TERMINATE;
ctrlm_utils_queue_msg_push(m_keyThreadMsgQ, (const char *)&msg, sizeof(msg));
ctrlm_utils_thread_join(&m_keyMonitorThread, 2);
}
ctrlm_utils_message_queue_close(&m_keyThreadMsgQ);
if (m_dbusConnection) { g_object_unref(m_dbusConnection); }
}
void ctrlm_ble_rcu_interface_t::shutdown()
{
if (m_controller && m_adapter) {
XLOGD_INFO("Cancelling pairing state machine...");
m_controller->cancelPairing();
XLOGD_INFO("Checking if bluez adapter is trying to discover...");
if (m_adapter->isDiscovering()) {
XLOGD_WARN("Stopping discovery session");
m_adapter->stopDiscovery();
}
XLOGD_INFO("Checking if bluez adapter is pairable...");
if (m_adapter->isPairable()) {
XLOGD_WARN("Disabling bluez adapter pairability");
m_adapter->disablePairable();
}
XLOGD_INFO("Shutdown all services to exit gracefully...");
m_controller->shutdown();
}
// delete the adapter and controller when going into deepsleep so that we don't
// get notified of the remote disconnection that occurs when going to sleep.
// Upon waking, these will be re-initialized.
m_controller.reset();
m_adapter.reset();
}
void ctrlm_ble_rcu_interface_t::initialize()
{
if (m_adapter == nullptr) {
m_adapter = std::make_shared<BleRcuAdapterBluez> (m_config, m_servicesFactory, m_dbusConnection, m_GMainLoop);
}
if (m_controller == nullptr) {
// create the m_controller that manages the adapter and paired devices
m_controller = make_shared<BleRcuControllerImpl>(m_config, m_adapter);
}
if (m_controller) {
auto stateChangedSlot = [this](BleRcuController::State state)
{
XLOGD_DEBUG("BLE RCU state = <%s>", ctrlm_rf_pair_state_str((ctrlm_rf_pair_state_t)state));
ctrlm_hal_ble_RcuStatusData_t params;
params.property_updated = CTRLM_HAL_BLE_PROPERTY_STATE;
params.state = (ctrlm_rf_pair_state_t)state;
m_rcuStatusChangedSlots.invoke(¶ms);
};
m_controller->addStateChangedSlot(Slot<BleRcuController::State>(m_isAlive, stateChangedSlot));
auto deviceAddedSlot = [this](const BleAddress &address)
{
XLOGD_DEBUG("deviceAddedSlot %s", address.toString().c_str());
if (true == handleAddedDevice(address)) {
ctrlm_hal_ble_rcu_data_t rcuData = getAllDeviceProperties(address.toUInt64());
rcuData.ieee_address = address.toUInt64();
ctrlm_hal_ble_IndPaired_params_t params;
params.rcu_data = rcuData;
// Indicate up to the ControlMgr BLE network
m_rcuPairedSlots.invoke(¶ms);
addNewDeviceKeyMonitorThread(address);
}
};
m_controller->addManagedDeviceAddedSlot(Slot<const BleAddress &>(m_isAlive, deviceAddedSlot));
auto deviceRemovedSlot = [this](const BleAddress &address)
{
XLOGD_DEBUG("deviceRemovedSlot %s", address.toString().c_str());
ctrlm_ble_key_queue_device_changed_msg_t msg;
msg.header.type = CTRLM_BLE_KEY_QUEUE_MSG_TYPE_DEVICE_REMOVED;
msg.address = address;
ctrlm_utils_queue_msg_push(m_keyThreadMsgQ, (const char *)&msg, sizeof(msg));
ctrlm_hal_ble_IndUnPaired_params_t params;
params.ieee_address = address.toUInt64();
// Indicate up to the ControlMgr BLE network
m_rcuUnpairedSlots.invoke(¶ms);
};
m_controller->addManagedDeviceRemovedSlot(Slot<const BleAddress &>(m_isAlive, deviceRemovedSlot));
}
voice_params_par_t params;
ctrlm_get_voice_obj()->voice_params_par_get(¶ms);
m_parVoiceEosTimeout = params.par_voice_eos_timeout;
}
void ctrlm_ble_rcu_interface_t::setGMainLoop(GMainLoop* main_loop)
{
m_GMainLoop = main_loop;
}
std::vector<uint64_t> ctrlm_ble_rcu_interface_t::registerDevices()
{
XLOGD_INFO("Get list of currently connected devices, and register RCU Device interface DBus signal handlers...");
vector<uint64_t> ret;
if (!m_controller) {
XLOGD_ERROR("m_controller is NULL!!!");
return ret;
}
auto devices = m_controller->managedDevices();
for (auto const &device : devices) {
XLOGD_INFO("Setting up BLE interface for %s", device.toString().c_str());
if (true == handleAddedDevice(device)) {
addNewDeviceKeyMonitorThread(device);
}
ret.push_back(device.toUInt64());
}
return ret;
}
void ctrlm_ble_rcu_interface_t::startKeyMonitorThread()
{
// Create an asynchronous queue to receive incoming messages
if (false == ctrlm_utils_message_queue_open(&m_keyThreadMsgQ, CTRLM_BLE_KEY_MSG_QUEUE_MSG_MAX, CTRLM_BLE_KEY_MSG_QUEUE_MSG_SIZE_MAX)) {
XLOGD_ERROR("failed to create message queue to key monitor thread");
return;
}
// Start key monitor thread. This has to occur after the network as already added all the controllers
// because this thread will indicate up the device node ID when it finds it.
m_keyMonitorThread.name = "ctrlm_ble_key_mon";
sem_init(&m_keyThreadSem, 0, 0);
if (false == ctrlm_utils_thread_create(&m_keyMonitorThread, KeyMonitorThread, this)) {
sem_destroy(&m_keyThreadSem);
} else {
// Block until initialization is complete or a timeout occurs
XLOGD_INFO("Waiting for %s thread initialization...", m_keyMonitorThread.name);
sem_wait(&m_keyThreadSem);
sem_destroy(&m_keyThreadSem);
}
}
void ctrlm_ble_rcu_interface_t::tickleKeyMonitorThread(ctrlm_hal_network_property_thread_monitor_t *value)
{
if (value == 0) {
XLOGD_ERROR("Param is NULL");
return;
}
XLOGD_DEBUG("ctrlm checks whether we are still alive");
ctrlm_ble_key_thread_monitor_msg_t msg;
msg.header.type = CTRLM_BLE_KEY_QUEUE_MSG_TYPE_TICKLE;
msg.response = value->response;
ctrlm_utils_queue_msg_push(m_keyThreadMsgQ, (const char *)&msg, sizeof(msg));
}
bool ctrlm_ble_rcu_interface_t::handleAddedDevice(const BleAddress &address)
{
XLOGD_INFO("device <%s>", address.toString().c_str());
if (!m_controller) {
XLOGD_ERROR("m_controller is NULL!!!");
return false;
}
shared_ptr<BleRcuDevice> device = m_controller->managedDevice(address);
if (!device || !device->isValid()) {
XLOGD_ERROR("BleRcuDevice is invalid");
return false;
}
//add slots to BleRcuDevice
auto connectedChangedSlot = [this, address](bool connected)
{
XLOGD_INFO("BLE RCU %s connected changed to <%s>", address.toString().c_str(), connected ? "TRUE" : "FALSE");
if (connected) {
if (m_keyMonitorThread.running) {
addNewDeviceKeyMonitorThread(address);
}
}
ctrlm_hal_ble_RcuStatusData_t params;
params.property_updated = CTRLM_HAL_BLE_PROPERTY_CONNECTED;
params.rcu_data.ieee_address = address.toUInt64();
params.rcu_data.connected = connected;
m_rcuStatusChangedSlots.invoke(¶ms);
};
// We use READY signal from the device as a proxy for connected
device->addReadyChangedSlot(Slot<bool>(m_isAlive, connectedChangedSlot));
auto nameChangedSlot = [this, address](const string &name)
{
XLOGD_DEBUG("BLE RCU name changed to <%s>", name.c_str());
ctrlm_hal_ble_RcuStatusData_t params;
params.property_updated = CTRLM_HAL_BLE_PROPERTY_NAME;
params.rcu_data.ieee_address = address.toUInt64();
errno_t safec_rc = strcpy_s(params.rcu_data.name, sizeof(params.rcu_data.name), name.c_str());
ERR_CHK(safec_rc);
m_rcuStatusChangedSlots.invoke(¶ms);
};
device->addNameChangedSlot(Slot<const string&>(m_isAlive, nameChangedSlot));
auto manufacturerChangedSlot = [this, address](const string &name)
{
XLOGD_DEBUG("BLE RCU manufacturer name changed to <%s>", name.c_str());
ctrlm_hal_ble_RcuStatusData_t params;
params.property_updated = CTRLM_HAL_BLE_PROPERTY_MANUFACTURER;
params.rcu_data.ieee_address = address.toUInt64();
errno_t safec_rc = strcpy_s(params.rcu_data.manufacturer, sizeof(params.rcu_data.manufacturer), name.c_str());
ERR_CHK(safec_rc);
m_rcuStatusChangedSlots.invoke(¶ms);
};
device->addManufacturerNameChangedSlot(Slot<const string&>(m_isAlive, manufacturerChangedSlot));
auto modelChangedSlot = [this, address](const string &name)
{
XLOGD_DEBUG("BLE RCU model number changed to <%s>", name.c_str());
ctrlm_hal_ble_RcuStatusData_t params;
params.property_updated = CTRLM_HAL_BLE_PROPERTY_MODEL;
params.rcu_data.ieee_address = address.toUInt64();
errno_t safec_rc = strcpy_s(params.rcu_data.model, sizeof(params.rcu_data.model), name.c_str());
ERR_CHK(safec_rc);
m_rcuStatusChangedSlots.invoke(¶ms);
};
device->addModelNumberChangedSlot(Slot<const string&>(m_isAlive, modelChangedSlot));
auto serialChangedSlot = [this, address](const string &name)
{
XLOGD_DEBUG("BLE RCU serial number changed to <%s>", name.c_str());
ctrlm_hal_ble_RcuStatusData_t params;
params.property_updated = CTRLM_HAL_BLE_PROPERTY_SERIAL_NUMBER;
params.rcu_data.ieee_address = address.toUInt64();
errno_t safec_rc = strcpy_s(params.rcu_data.serial_number, sizeof(params.rcu_data.serial_number), name.c_str());
ERR_CHK(safec_rc);
m_rcuStatusChangedSlots.invoke(¶ms);
};
device->addSerialNumberChangedSlot(Slot<const string&>(m_isAlive, serialChangedSlot));
auto hardwareChangedSlot = [this, address](const string &name)
{
XLOGD_DEBUG("BLE RCU hardware revision changed to <%s>", name.c_str());
ctrlm_hal_ble_RcuStatusData_t params;
params.property_updated = CTRLM_HAL_BLE_PROPERTY_HW_REVISION;
params.rcu_data.ieee_address = address.toUInt64();
errno_t safec_rc = strcpy_s(params.rcu_data.hw_revision, sizeof(params.rcu_data.hw_revision), name.c_str());
ERR_CHK(safec_rc);
m_rcuStatusChangedSlots.invoke(¶ms);
};
device->addHardwareRevisionChangedSlot(Slot<const string&>(m_isAlive, hardwareChangedSlot));
auto firmwareChangedSlot = [this, address](const string &name)
{
XLOGD_DEBUG("BLE RCU firmware version changed to <%s>", name.c_str());
ctrlm_hal_ble_RcuStatusData_t params;
params.property_updated = CTRLM_HAL_BLE_PROPERTY_FW_REVISION;
params.rcu_data.ieee_address = address.toUInt64();
errno_t safec_rc = strcpy_s(params.rcu_data.fw_revision, sizeof(params.rcu_data.fw_revision), name.c_str());
ERR_CHK(safec_rc);
m_rcuStatusChangedSlots.invoke(¶ms);
};
device->addFirmwareVersionChangedSlot(Slot<const string&>(m_isAlive, firmwareChangedSlot));
auto softwareChangedSlot = [this, address](const string &name)
{
XLOGD_INFO("BLE RCU %s software version changed to <%s>", address.toString().c_str(), name.c_str());
ctrlm_hal_ble_RcuStatusData_t params;
params.property_updated = CTRLM_HAL_BLE_PROPERTY_SW_REVISION;
params.rcu_data.ieee_address = address.toUInt64();
errno_t safec_rc = strcpy_s(params.rcu_data.sw_revision, sizeof(params.rcu_data.sw_revision), name.c_str());
ERR_CHK(safec_rc);
m_rcuStatusChangedSlots.invoke(¶ms);
};
device->addSoftwareVersionChangedSlot(Slot<const string&>(m_isAlive, softwareChangedSlot));
auto batteryChangedSlot = [this, address](int level)
{
XLOGD_DEBUG("BLE RCU battery level changed to <%d%%>", level);
ctrlm_hal_ble_RcuStatusData_t params;
params.property_updated = CTRLM_HAL_BLE_PROPERTY_BATTERY_LEVEL;
params.rcu_data.ieee_address = address.toUInt64();
params.rcu_data.battery_level = (unsigned int)level;
m_rcuStatusChangedSlots.invoke(¶ms);
};
device->addBatteryLevelChangedSlot(Slot<int>(m_isAlive, batteryChangedSlot));
auto unpairReasonChangedSlot = [this, address](uint8_t reason)
{
XLOGD_DEBUG("BLE RCU unpair reason changed to <%u>", reason);
ctrlm_hal_ble_RcuStatusData_t params;
params.property_updated = CTRLM_HAL_BLE_PROPERTY_UNPAIR_REASON;
params.rcu_data.ieee_address = address.toUInt64();
params.rcu_data.unpair_reason = (ctrlm_ble_RcuUnpairReason_t)reason;
m_rcuStatusChangedSlots.invoke(¶ms);
};
device->addUnpairReasonChangedSlot(Slot<uint8_t>(m_isAlive, unpairReasonChangedSlot));
auto rebootReasonChangedSlot = [this, address](uint8_t reason, std::string assertReport)
{
XLOGD_DEBUG("BLE RCU reboot reason changed to <%u>", reason);
ctrlm_hal_ble_RcuStatusData_t params;
params.property_updated = CTRLM_HAL_BLE_PROPERTY_REBOOT_REASON;
params.rcu_data.ieee_address = address.toUInt64();
params.rcu_data.reboot_reason = (ctrlm_ble_RcuRebootReason_t)reason;
errno_t safec_rc = strcpy_s(params.rcu_data.assert_report, sizeof(params.rcu_data.assert_report), assertReport.c_str());
ERR_CHK(safec_rc);
m_rcuStatusChangedSlots.invoke(¶ms);
};
device->addRebootReasonChangedSlot(Slot<uint8_t, std::string>(m_isAlive, rebootReasonChangedSlot));
auto lastKeypressChangedSlot = [this, address](uint8_t key)
{
XLOGD_DEBUG("BLE RCU last key press changed to <%u>", key);
ctrlm_hal_ble_RcuStatusData_t params;
params.property_updated = CTRLM_HAL_BLE_PROPERTY_LAST_WAKEUP_KEY;
params.rcu_data.ieee_address = address.toUInt64();
params.rcu_data.last_wakeup_key = ctrlm_ble_utils_ConvertUsbKbdCodeToLinux(key);
m_rcuStatusChangedSlots.invoke(¶ms);
};
device->addLastKeypressChangedSlot(Slot<uint8_t>(m_isAlive, lastKeypressChangedSlot));
auto advConfigChangedSlot = [this, address](uint8_t config)
{
XLOGD_DEBUG("BLE RCU advertising config changed to <%u>", config);
ctrlm_hal_ble_RcuStatusData_t params;
params.property_updated = CTRLM_HAL_BLE_PROPERTY_WAKEUP_CONFIG;
params.rcu_data.ieee_address = address.toUInt64();
params.rcu_data.wakeup_config = config;
m_rcuStatusChangedSlots.invoke(¶ms);
};
device->addAdvConfigChangedSlot(Slot<uint8_t>(m_isAlive, advConfigChangedSlot));
auto advConfigCustomListChangedSlot = [this, address](const std::vector<uint8_t> &list)
{
XLOGD_DEBUG("BLE RCU advertising config custom list changed.");
ctrlm_hal_ble_RcuStatusData_t params;
params.property_updated = CTRLM_HAL_BLE_PROPERTY_WAKEUP_CUSTOM_LIST;
params.rcu_data.ieee_address = address.toUInt64();
if (list.size() <= (sizeof(params.rcu_data.wakeup_custom_list) / sizeof(params.rcu_data.wakeup_custom_list[0]))) {
int idx = 0;
for (unsigned int i = 0; i < list.size(); i += 2) {
if (list[i] != 0) {
// Format of this list is the keycode followed by config byte. We only care about the keycodes (even bytes)
params.rcu_data.wakeup_custom_list[idx] = ctrlm_ble_utils_ConvertUsbKbdCodeToLinux(list[i]);
idx++;
}
}
params.rcu_data.wakeup_custom_list_size = idx;
}
m_rcuStatusChangedSlots.invoke(¶ms);
};
device->addAdvConfigCustomListChangedSlot(Slot<const std::vector<uint8_t> &>(m_isAlive, advConfigCustomListChangedSlot));
auto audioStreamingChangedSlot = [this, address](bool streaming)
{
XLOGD_DEBUG("BLE RCU audio streaming changed to %s", streaming ? "TRUE" : "FALSE");
ctrlm_hal_ble_RcuStatusData_t params;
params.property_updated = CTRLM_HAL_BLE_PROPERTY_AUDIO_STREAMING;
params.rcu_data.ieee_address = address.toUInt64();
params.rcu_data.audio_streaming = streaming;
m_rcuStatusChangedSlots.invoke(¶ms);
};
device->addStreamingChangedSlot(Slot<bool>(m_isAlive, audioStreamingChangedSlot));
auto audioGainChangedSlot = [this, address](uint8_t gain)
{
XLOGD_DEBUG("BLE RCU gain level changed to %u", gain);
ctrlm_hal_ble_RcuStatusData_t params;
params.property_updated = CTRLM_HAL_BLE_PROPERTY_AUDIO_GAIN_LEVEL;
params.rcu_data.ieee_address = address.toUInt64();
params.rcu_data.audio_gain_level = gain;
m_rcuStatusChangedSlots.invoke(¶ms);
};
device->addGainLevelChangedSlot(Slot<uint8_t>(m_isAlive, audioGainChangedSlot));
auto audioCodecsChangedSlot = [this, address](uint32_t codecs)
{
XLOGD_DEBUG("BLE RCU audio codecs changed to 0x%X", codecs);
ctrlm_hal_ble_RcuStatusData_t params;
params.property_updated = CTRLM_HAL_BLE_PROPERTY_AUDIO_CODECS;
params.rcu_data.ieee_address = address.toUInt64();
params.rcu_data.audio_codecs = codecs;
m_rcuStatusChangedSlots.invoke(¶ms);
};
device->addAudioCodecsChangedSlot(Slot<uint32_t>(m_isAlive, audioCodecsChangedSlot));
auto codeIdChangedSlot = [this, address](int32_t codeId)
{
XLOGD_DEBUG("BLE RCU IR code ID changed to %d", codeId);
ctrlm_hal_ble_RcuStatusData_t params;
params.property_updated = CTRLM_HAL_BLE_PROPERTY_IR_CODE;
params.rcu_data.ieee_address = address.toUInt64();
params.rcu_data.ir_code = codeId;
m_rcuStatusChangedSlots.invoke(¶ms);
};
device->addCodeIdChangedSlot(Slot<int32_t>(m_isAlive, codeIdChangedSlot));
auto irSupportChangedSlot = [this, address](uint8_t irSupport)
{
XLOGD_INFO("BLE RCU IR Support bitmap changed to 0x%X", irSupport);
ctrlm_hal_ble_RcuStatusData_t params;
params.rcu_data.irdbs_supported = irSupport;
params.property_updated = CTRLM_HAL_BLE_PROPERTY_IRDBS_SUPPORTED;
params.rcu_data.ieee_address = address.toUInt64();
m_rcuStatusChangedSlots.invoke(¶ms);
};
device->addIrSupportChangedSlot(Slot<uint8_t>(m_isAlive, irSupportChangedSlot));
auto upgradingChangedSlot = [this, address](bool upgrading)
{
ctrlm_hal_ble_RcuStatusData_t params;
params.property_updated = CTRLM_HAL_BLE_PROPERTY_IS_UPGRADING;
params.rcu_data.ieee_address = address.toUInt64();
params.rcu_data.is_upgrading = upgrading;
m_rcuStatusChangedSlots.invoke(¶ms);
};
device->addUpgradingChangedSlot(Slot<bool>(m_isAlive, upgradingChangedSlot));
auto upgradeProgressChangedSlot = [this, address](int progress)
{
ctrlm_hal_ble_RcuStatusData_t params;
params.property_updated = CTRLM_HAL_BLE_PROPERTY_UPGRADE_PROGRESS;
params.rcu_data.ieee_address = address.toUInt64();
params.rcu_data.upgrade_progress = progress;
m_rcuStatusChangedSlots.invoke(¶ms);
};
device->addUpgradeProgressChangedSlot(Slot<int>(m_isAlive, upgradeProgressChangedSlot));
auto upgradeErrorSlot = [this, address](string errorMessage)
{
ctrlm_hal_ble_RcuStatusData_t params;
params.property_updated = CTRLM_HAL_BLE_PROPERTY_UPGRADE_ERROR;
params.rcu_data.ieee_address = address.toUInt64();
errno_t safec_rc = strcpy_s(params.rcu_data.upgrade_error, sizeof(params.rcu_data.upgrade_error), errorMessage.c_str());
ERR_CHK(safec_rc);
m_rcuStatusChangedSlots.invoke(¶ms);
};
device->addUpgradeErrorSlot(Slot<string>(m_isAlive, upgradeErrorSlot));
return true;
}
void ctrlm_ble_rcu_interface_t::handleDeepsleep(bool wakingUp)
{
XLOGD_WARN("Deepsleep transitioning, waking up = %s", wakingUp ? "TRUE" : "FALSE");
if (wakingUp) {
initialize();
if (m_adapter) {
m_adapter->reconnectAllDevices();
}
ctrlm_ble_key_queue_msg_header_t msg;
msg.type = CTRLM_BLE_KEY_QUEUE_MSG_TYPE_DEEPSLEEP_WAKEUP;
ctrlm_utils_queue_msg_push(m_keyThreadMsgQ, (const char *)&msg, sizeof(msg));
} else {
// We are going into deepsleep, clean up
shutdown();
}
}
ctrlm_hal_ble_rcu_data_t ctrlm_ble_rcu_interface_t::getAllDeviceProperties(uint64_t ieee_address)
{
ctrlm_hal_ble_rcu_data_t ret;
errno_t safec_rc = memset_s(&ret, sizeof(ret), 0, sizeof(ret)); ERR_CHK(safec_rc);
ret.ieee_address = ieee_address;
if (!m_controller) {
XLOGD_ERROR("m_controller is NULL!!!");
return ret;
}
shared_ptr<BleRcuDevice> device = m_controller->managedDevice(ieee_address);
safec_rc = strcpy_s(ret.name, sizeof(ret.name), device->name().c_str()); ERR_CHK(safec_rc);
ret.connected = device->isReady();
//Battery Service
ret.battery_level = device->batteryLevel();
//Device Info Service
safec_rc = strcpy_s(ret.manufacturer, sizeof(ret.manufacturer), device->manufacturer().c_str()); ERR_CHK(safec_rc);
safec_rc = strcpy_s(ret.model, sizeof(ret.model), device->model().c_str()); ERR_CHK(safec_rc);
safec_rc = strcpy_s(ret.fw_revision, sizeof(ret.fw_revision), device->firmwareRevision().c_str()); ERR_CHK(safec_rc);
safec_rc = strcpy_s(ret.hw_revision, sizeof(ret.hw_revision), device->hardwareRevision().c_str()); ERR_CHK(safec_rc);
safec_rc = strcpy_s(ret.sw_revision, sizeof(ret.sw_revision), device->softwareRevision().c_str()); ERR_CHK(safec_rc);
safec_rc = strcpy_s(ret.serial_number, sizeof(ret.serial_number), device->serialNumber().c_str()); ERR_CHK(safec_rc);
//Remote Control Service
ret.unpair_reason = (ctrlm_ble_RcuUnpairReason_t)device->unpairReason();
ret.reboot_reason = (ctrlm_ble_RcuRebootReason_t)device->rebootReason();
ret.last_wakeup_key = ctrlm_ble_utils_ConvertUsbKbdCodeToLinux(device->lastKeypress());
ret.wakeup_config = device->advConfig();
vector<uint8_t> list = device->advConfigCustomList();
if (list.size() <= (sizeof(ret.wakeup_custom_list) / sizeof(ret.wakeup_custom_list[0]))) {
// Format of this list is the keycode followed by config byte. We only care about the keycodes (even bytes)
int idx = 0;
for (unsigned int i = 0; i < list.size(); i += 2) {
if (list[i] != 0) {
ret.wakeup_custom_list[idx] = ctrlm_ble_utils_ConvertUsbKbdCodeToLinux(list[i]);
idx++;
}
}
ret.wakeup_custom_list_size = idx;
}
// IR Service
ret.irdbs_supported = device->irSupport();
// Voice Service
ret.audio_codecs = device->audioCodecs();
ret.audio_gain_level = device->audioGainLevel();
return ret;
}
bool ctrlm_ble_rcu_interface_t::pairWithCode(unsigned int code)
{
if (!m_controller) {
XLOGD_ERROR("m_controller is NULL!!!");
return false;
}
if (!m_controller->startPairingWithCode((uint8_t) code)) {
BleRcuError error = m_controller->lastError();
// Remote will continually send out IR pairing signals until the BLE pair request
// has been received. This means that the "Already in pairing state" error is normal.
// Let's omit this error print because it only serves to confuse those analyzing the logs.
if (error.message() != "Already in pairing state") {
XLOGD_ERROR("controller failed to start pairing, %s: %s", error.name().c_str(), error.message().c_str());
}
return false;
}
return true;
}
bool ctrlm_ble_rcu_interface_t::pairWithMacAddrs(const std::vector<uint64_t> &macAddrList)
{
if (m_controller) {
std::vector<BleAddress> addresses;
for (const auto &mac : macAddrList) {
addresses.push_back(BleAddress(mac));
}
if (!m_controller->startPairingWithList(addresses)) {
BleRcuError error = m_controller->lastError();
XLOGD_ERROR("controller failed to start pairing, %s: %s", error.name().c_str(), error.message().c_str());
return false;
}
return true;
}
return false;
}
bool ctrlm_ble_rcu_interface_t::pairAutoWithTimeout(int timeoutMs)
{
if (!m_controller) {
XLOGD_ERROR("m_controller is NULL!!!");
return false;
}
XLOGD_INFO("Starting BLE auto pairing procedure with timeout %dms. Will attempt to pair with the first discovered supported device.", timeoutMs);
if (!m_controller->startPairingAutoWithTimeout(timeoutMs)) {
BleRcuError error = m_controller->lastError();
XLOGD_ERROR("controller failed to start scan, %s: %s", error.name().c_str(), error.message().c_str());
return false;
}
return true;
}
bool ctrlm_ble_rcu_interface_t::pairCancel()
{
if (!m_controller) {
XLOGD_ERROR("m_controller is NULL!!!");
return false;
}
XLOGD_INFO("Canceling BLE pairing operations.");
if (!m_controller->cancelPairing()) {
BleRcuError error = m_controller->lastError();
XLOGD_ERROR("controller failed to cancel pairing, %s: %s", error.name().c_str(), error.message().c_str());
return false;
}
return true;
}
bool ctrlm_ble_rcu_interface_t::unpairDevice(uint64_t ieee_address)
{
BleAddress address(ieee_address);
XLOGD_INFO("unpairing device %s", address.toString().c_str());
if (m_controller) {
// This method currently doesn't wait for a success or failure response from bluez so
// error message isn't captured in lastError()
return m_controller->unpairDevice(address);
}
return false;
}
bool ctrlm_ble_rcu_interface_t::findMe(uint64_t ieee_address, ctrlm_fmr_alarm_level_t level)
{
// This method will wait for the operation to complete, so init a semaphore
sem_t semaphore;
bool success = false;
// lambda invoked when the request returns
auto replyHandler = [this, &semaphore, &success](PendingReply<> *reply) mutable
{
// check for errors (only for logging)
if (reply->isError()) {
XLOGD_ERROR("findMe failed due to <%s>", reply->errorMessage().c_str());
success = false;
} else {
XLOGD_DEBUG("findMe succeeded");
success = true;
}
sem_post(&semaphore);
};
BleAddress address(ieee_address);
XLOGD_INFO("triggering \"find me\" operation on remote %s", address.toString().c_str());
if (m_controller) {
const auto device = m_controller->managedDevice(address);
if (device) {
sem_init(&semaphore, 0, 0);
device->findMe((uint8_t)level, PendingReply<>(m_isAlive, replyHandler));
} else {
return false;
}
} else {
return false;
}
// Wait for the result semaphore to be signaled
sem_wait(&semaphore);
sem_destroy(&semaphore);
return success;
}
bool ctrlm_ble_rcu_interface_t::sendRcuAction(uint64_t ieee_address, ctrlm_ble_RcuAction_t action, bool waitForReply)
{
// This method will wait for the operation to complete, so init a semaphore
sem_t semaphore;
bool success = false;
BleAddress address(ieee_address);
// lambda invoked when the request returns
auto replyHandler = [this, &semaphore, &success, waitForReply, action, address](PendingReply<> *reply) mutable
{
// check for errors (only for logging)
if (reply->isError()) {
XLOGD_ERROR("sendRcuAction failed due to <%s>", reply->errorMessage().c_str());
success = false;
} else {
XLOGD_INFO("RCU action %s successfully sent to remote %s",
ctrlm_ble_rcu_action_str(action), address.toString().c_str());
success = true;
}
if (waitForReply) { sem_post(&semaphore); }
};
XLOGD_INFO("triggering RCU action %s on remote %s",
ctrlm_ble_rcu_action_str(action), address.toString().c_str());
if (m_controller) {
const auto device = m_controller->managedDevice(address);
if (device) {
if (waitForReply) { sem_init(&semaphore, 0, 0); }
success = true;
device->sendRcuAction((uint8_t)action, PendingReply<>(m_isAlive, replyHandler));
} else {
return false;
}
} else {
return false;
}
// Wait for the result semaphore to be signaled
if (waitForReply) {
sem_wait(&semaphore);
sem_destroy(&semaphore);
}
return success;
}
bool ctrlm_ble_rcu_interface_t::writeAdvertisingConfig(uint64_t ieee_address,
ctrlm_rcu_wakeup_config_t config,
int *customList,
int customListSize)
{
// This method will wait for the operation to complete, so init a semaphore
sem_t semaphore;
bool success = false;
BleAddress address(ieee_address);
// lambda invoked when the request returns
auto replyHandler = [this, &semaphore, &success, address](PendingReply<> *reply) mutable
{
// check for errors (only for logging)
if (reply->isError()) {
XLOGD_ERROR("%s: writeAdvertisingConfig failed due to <%s>",
address.toString().c_str(), reply->errorMessage().c_str());
success = false;
} else {
XLOGD_INFO("successfully wrote RCU advertising config on remote %s", address.toString().c_str());
success = true;
}
sem_post(&semaphore);
};
XLOGD_INFO("writing RCU advertising config on remote %s", address.toString().c_str());
if (m_controller) {
const auto device = m_controller->managedDevice(address);
if (device) {
sem_init(&semaphore, 0, 0);
vector<uint8_t> listConverted;
if (config == CTRLM_RCU_WAKEUP_CONFIG_CUSTOM && customList != NULL) {
for (int i = 0; i < customListSize; i++) {
listConverted.push_back(ctrlm_ble_utils_ConvertLinuxCodeToUsbKdb(customList[i]));
listConverted.push_back(0); // 0 to send directed advertisment for the key
}
}
device->writeAdvertisingConfig((uint8_t)config, listConverted, PendingReply<>(m_isAlive, replyHandler));
// Wait for the result semaphore to be signaled
sem_wait(&semaphore);
sem_destroy(&semaphore);
}
}
return success;
}
bool ctrlm_ble_rcu_interface_t::getUnpairReason(uint64_t ieee_address, ctrlm_ble_RcuUnpairReason_t &reason)
{
BleAddress address(ieee_address);
if (m_controller) {
const auto device = m_controller->managedDevice(address);
if (device) {
reason = (ctrlm_ble_RcuUnpairReason_t)device->unpairReason();
} else {
return false;
}
} else {
return false;
}
return true;
}
bool ctrlm_ble_rcu_interface_t::getRebootReason(uint64_t ieee_address, ctrlm_ble_RcuRebootReason_t &reason)
{
BleAddress address(ieee_address);
if (m_controller) {
const auto device = m_controller->managedDevice(address);
if (device) {
reason = (ctrlm_ble_RcuRebootReason_t)device->rebootReason();
} else {
return false;
}
} else {
return false;
}
return true;
}
bool ctrlm_ble_rcu_interface_t::getAudioFormat(uint64_t ieee_address, ctrlm_hal_ble_VoiceEncoding_t encoding, AudioFormat &format) {
BleAddress address(ieee_address);
XLOGD_INFO("getting RCU audio format on remote %s", address.toString().c_str());
BleRcuDevice::Encoding device_encoding = BleRcuDevice::Encoding::InvalidEncoding;
if(encoding == CTRLM_HAL_BLE_ENCODING_ADPCM) {
device_encoding = BleRcuDevice::Encoding::ADPCM_FRAME;
} else if(encoding == CTRLM_HAL_BLE_ENCODING_PCM) {
device_encoding = BleRcuDevice::Encoding::PCM16;
} else {
XLOGD_ERROR("getAudioFormat failed due to invalid encoding");
return(false);
}
if(m_controller) {
const auto device = m_controller->managedDevice(address);
if(device) {
return(device->getAudioFormat(device_encoding, format));
}
}
return(false);
}
bool ctrlm_ble_rcu_interface_t::startAudioStreaming(uint64_t ieee_address, ctrlm_hal_ble_VoiceEncoding_t encoding, ctrlm_hal_ble_VoiceStreamEnd_t streamEnd, int &fd)
{
// This method will wait for the operation to complete, so init a semaphore
sem_t semaphore;
bool success = false;
// lambda invoked when the request returns
auto replyHandler = [this, &semaphore, &success, &fd](PendingReply<int> *reply) mutable
{
// check for errors
if (reply->isError()) {
XLOGD_ERROR("startAudioStreaming failed due to <%s>", reply->errorMessage().c_str());
success = false;
} else {
success = true;
fd = reply->result();
XLOGD_DEBUG("startAudioStreaming succeeded, fd = %d", fd);
}
sem_post(&semaphore);
};
BleAddress address(ieee_address);
XLOGD_INFO("starting RCU audio streaming on remote %s", address.toString().c_str());