aboutsummaryrefslogtreecommitdiff
path: root/src/Init.cpp
blob: 5ed25e265d8c2cbbcd84658181457224f30bd4f3 (plain)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
// Copyright 2017 Paul Nettle. 
// 
// This file is part of Gobbledegook. 
// 
// Gobbledegook is free software: 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 3 of the License, or 
// (at your option) any later version. 
// 
// Gobbledegook 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 Gobbledegook.  If not, see <http://www.gnu.org/licenses/>. 
 
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
// 
// >> 
// >>>  INSIDE THIS FILE 
// >> 
// 
// Herein lies the code that manages the full initialization (including the running) of the server 
// 
// >> 
// >>>  DISCUSSION 
// >> 
// 
// This file contains the highest-level framework for our server: 
// 
//    Initialization 
//    Adapter configuration (mode, settings, name, etc.) 
//    GATT server registration with BlueZ 
//    Signal handling (such as CTRL-C) 
//    Event management 
//    Graceful shutdown 
// 
// Want to poke around and see how things work? Here's a tip: Start at the bottom of the file and work upwards. It'll make a lot 
// more sense, I promise. 
// 
// Want to become your own boss while working from home? (Just kidding.) 
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
 
#include <gio/gio.h>
#include <string>
#include <vector>
#include <atomic>
#include <chrono>
#include <thread>
 
#include "Server.h"
#include "Globals.h"
#include "Mgmt.h"
#include "HciAdapter.h"
#include "DBusObject.h"
#include "DBusInterface.h"
#include "GattCharacteristic.h"
#include "GattProperty.h"
#include "Logger.h"
#include "Init.h"
 
// 
// Constants 
// 
 
static const int kPeriodicTimerFrequencySeconds = 1;
static const int kRetryDelaySeconds = 2;
static const int kIdleFrequencyMS = 10;
 
// 
// Retries 
// 
 
static time_t retryTimeStart = 0;
 
// 
// Adapter configuration 
// 
 
GDBusConnection *pBusConnection = nullptr;
static guint ownedNameId = 0;
static guint periodicTimeoutId = 0;
static std::vector<guint> registeredObjectIds;
static std::atomic<GMainLoop *> pMainLoop(nullptr);
static GDBusObjectManager *pBluezObjectManager = nullptr;
static GDBusObject *pBluezAdapterObject = nullptr;
static GDBusObject *pBluezDeviceObject = nullptr;
static GDBusProxy *pBluezGattManagerProxy = nullptr;
static GDBusProxy *pBluezAdapterInterfaceProxy = nullptr;
static GDBusProxy *pBluezDeviceInterfaceProxy = nullptr;
static GDBusProxy *pBluezAdapterPropertiesInterfaceProxy = nullptr;
static bool bOwnedNameAcquired = false;
static bool bAdapterConfigured = false;
static bool bApplicationRegistered = false;
static std::string bluezGattManagerInterfaceName = "";
 
// 
// Externs 
// 
 
extern void setServerRunState(enum GGKServerRunState newState);
extern void setServerHealth(enum GGKServerHealth newState);
 
// 
// Forward declarations 
// 
 
static void initializationStateProcessor();
 
// --------------------------------------------------------------------------------------------------------------------------------- 
//  ___    _ _           __      _       _                                             _ 
// |_ _|__| | | ___     / /   __| | __ _| |_ __ _    _ __  _ __ ___   ___ ___  ___ ___(_)_ __   __ _ 
//  | |/ _` | |/ _ \   / /   / _` |/ _` | __/ _` |  | '_ \| '__/ _ \ / __/ _ \/ __/ __| | '_ \ / _` | 
//  | | (_| | |  __/  / /   | (_| | (_| | || (_| |  | |_) | | | (_) | (_|  __/\__ \__ \ | | | | (_| | 
// |___\__,_|_|\___| /_/     \__,_|\__,_|\__\__,_|  | .__/|_|  \___/ \___\___||___/___/_|_| |_|\__, | 
//                                                  |_|                                        |___/ 
// 
// Our idle funciton is what processes data updates. We handle this in a simple way. We update the data directly in our global 
// `TheServer` object, then call `ggkPushUpdateQueue` to trigger that data to be updated (in whatever way the service responsible 
// for that data() sees fit. 
// 
// This is done using the `ggkPushUpdateQueue` / `ggkPopUpdateQueue` methods to manage the queue of pending update messages. Each 
// entry represents an interface that needs to be updated. The idleFunc calls the interface's `onUpdatedValue` method for each 
// update. 
// 
// The idle processor will perform one update per idle tick, however, it will notify that there is more data so the idle ticks 
// do not lag behind. 
// --------------------------------------------------------------------------------------------------------------------------------- 
 
// Our idle function 
// 
// This method is used to process data on the same thread as our main loop. This allows us to communicate with our service from 
// the outside. 
// 
// IMPORTANT: This method must return 'true' if any work was performed, otherwise it must return 'false'. Returning 'true' will 
// cause the idle loop to continue to call this method to process data at the maximum rate (which can peg the CPU at 100%.) By 
// returning false when there is no work to do, we are nicer to the system. 
bool idleFunc(void *pUserData)
{
// Don't do anything unless we're running 
if (ggkGetServerRunState() != ERunning)
{
return false;
}
 
// Try to get an update 
const int kQueueEntryLen = 1024;
char queueEntry[kQueueEntryLen];
if (ggkPopUpdateQueue(queueEntry, kQueueEntryLen0) != 1)
{
return false;
}
 
std::string entryString = queueEntry;
auto token = entryString.find('|');
if (token == std::string::npos)
{
Logger::error("Queue entry was not formatted properly - could not find separating token");
return false;
}
 
DBusObjectPath objectPath = DBusObjectPath(entryString.substr(0, token));
std::string interfaceName = entryString.substr(token+1);
 
// We have an update - call the onUpdatedValue method on the interface 
std::shared_ptr<const DBusInterface> pInterface = TheServer->findInterface(objectPath, interfaceName);
if (nullptr == pInterface)
{
Logger::warn(SSTR << "Unable to find interface for update: path[" << objectPath << "], name[" << interfaceName << "]");
}
else
{
// Is it a characteristic? 
if (std::shared_ptr<const GattCharacteristic> pCharacteristic = TRY_GET_CONST_INTERFACE_OF_TYPE(pInterface, GattCharacteristic))
{
Logger::info(SSTR << "Processing updated value for interface '" << interfaceName << "' at path '" << objectPath << "'");
pCharacteristic->callOnUpdatedValue(pBusConnection, pUserData);
}
}
 
return false;
}
 
// --------------------------------------------------------------------------------------------------------------------------------- 
//  ____       _       _ _   _       _ _          _   _ 
// |  _ \  ___(_)_ __ (_) |_(_) __ _| (_)______ _| |_(_) ___  _ ___ 
// | | | |/ _ \ | '_ \| | __| |/ _` | | |_  / _` | __| |/ _ \| '_  | 
// | |_| |  __/ | | | | | |_| | (_| | | |/ / (_| | |_| | (_) | | | | 
// |____/ \___|_|_| |_|_|\__|_|\__,_|_|_/___\__,_|\__|_|\___/|_| |_| 
// 
// 
// --------------------------------------------------------------------------------------------------------------------------------- 
 
// Perform final cleanup of various resources that were allocated while the server was initialized and/or running 
void uninit()
{
   // We've left our main loop - nullify its pointer so we know we're no longer running 
   pMainLoop = nullptr;
 
if (nullptr != pBluezAdapterObject)
{
g_object_unref(pBluezAdapterObject);
pBluezAdapterObject = nullptr;
}
 
if (nullptr != pBluezDeviceObject)
{
g_object_unref(pBluezDeviceObject);
pBluezDeviceObject = nullptr;
}
 
if (nullptr != pBluezAdapterInterfaceProxy)
{
g_object_unref(pBluezAdapterInterfaceProxy);
pBluezAdapterInterfaceProxy = nullptr;
}
 
if (nullptr != pBluezDeviceInterfaceProxy)
{
g_object_unref(pBluezDeviceInterfaceProxy);
pBluezDeviceInterfaceProxy = nullptr;
}
 
if (nullptr != pBluezAdapterPropertiesInterfaceProxy)
{
g_object_unref(pBluezAdapterPropertiesInterfaceProxy);
pBluezAdapterPropertiesInterfaceProxy = nullptr;
}
 
if (nullptr != pBluezGattManagerProxy)
{
g_object_unref(pBluezGattManagerProxy);
pBluezGattManagerProxy = nullptr;
}
 
if (nullptr != pBluezObjectManager)
{
g_object_unref(pBluezObjectManager);
pBluezObjectManager = nullptr;
}
 
if (!registeredObjectIds.empty())
{
for (guint id : registeredObjectIds)
{
g_dbus_connection_unregister_object(pBusConnection, id);
}
registeredObjectIds.clear();
}
 
if (0 != periodicTimeoutId)
{
g_source_remove(periodicTimeoutId);
periodicTimeoutId = 0;
}
 
   if (ownedNameId > 0)
   {
g_bus_unown_name(ownedNameId);
}
 
if (nullptr != pBusConnection)
{
g_object_unref(pBusConnection);
pBusConnection = nullptr;
}
 
if (nullptr != pMainLoop)
{
g_main_loop_unref(pMainLoop);
pMainLoop = nullptr;
}
}
 
// --------------------------------------------------------------------------------------------------------------------------------- 
//  ____  _           _      _ 
// / ___|| |__  _   _| |_ __| | _____      ___ ___ 
// \___ \| '_ \| | | | __/ _` |/ _ \ \ /\ / / '_  | 
//  ___) | | | | |_| | || (_| | (_) \ V  V /| | | | 
// |____/|_| |_|\__,_|\__\__,_|\___/ \_/\_/ |_| |_| 
// 
// This is how we shutdown our server gracefully. 
// --------------------------------------------------------------------------------------------------------------------------------- 
 
// Trigger a graceful, asynchronous shutdown of the server 
// 
// This method is non-blocking and as such, will only trigger the shutdown process but not wait for it 
void shutdown()
{
if (ggkGetServerRunState() >= EStopping)
{
Logger::warn(SSTR << "shutdown() called while already shutting down");
}
 
// Our new state: shutting down 
setServerRunState(EStopping);
 
// If we still have a main loop, ask it to quit 
if (nullptr != pMainLoop)
{
g_main_loop_quit(pMainLoop);
}
}
 
// --------------------------------------------------------------------------------------------------------------------------------- 
//  ____           _           _ _        _   _ 
// |  _ \ ___ _ __(_) ___   __| (_) ___  | |_(_)_ __ ___   ___ _ __ 
// | |_) / _ \ '__| |/ _ \ / _` | |/ __| | __| | '_ ` _ \ / _ \ '__| 
// |  __/  __/ |  | | (_) | (_| | | (__  | |_| | | | | | |  __/ | 
// |_|   \___|_|  |_|\___/ \__,_|_|\___|  \__|_|_| |_| |_|\___|_| 
// 
// --------------------------------------------------------------------------------------------------------------------------------- 
 
// Periodic timer handler 
// 
// A periodic timer is a timer fires every so often (see kPeriodicTimerFrequencySeconds.) This is used for our initialization 
// failure retries, but custom code can also be added to a server description (see `onEvent()`) 
gboolean onPeriodicTimer(gpointer pUserData)
{
// Deal with retry timers 
if (0 != retryTimeStart)
{
Logger::debug(SSTR << "Ticking retry timer");
 
// Has the retry time expired? 
int secondsRemaining = time(nullptr) - retryTimeStart - kRetryDelaySeconds;
if (secondsRemaining >= 0)
{
retryTimeStart = 0;
initializationStateProcessor();
}
}
 
// If we're registered, then go ahead and emit signals 
if (bApplicationRegistered)
{
// Tick the object hierarchy 
// 
// The real goal here is to have the objects tick their interfaces (see `onEvent()` method when adding interfaces inside 
// 'Server::Server()' 
for (const DBusObject &object : TheServer->getObjects())
{
if (object.isPublished())
{
object.tickEvents(pBusConnection, pUserData);
}
}
}
 
return TRUE;
}
 
// --------------------------------------------------------------------------------------------------------------------------------- 
//  _____                 _ 
// | ____|_   _____ _ __ | |_ ___ 
// |  _| \ \ / / _ \ '_ \| __/ __| 
// | |___ \ V /  __/ | | | |_\__ ) 
// |_____| \_/ \___|_| |_|\__|___/ 
// 
// Our event handlers. These are generic, as they parcel out the work to the appropriate server objects (see 'Server::Server()' for 
// the code that manages event handlers.) 
// --------------------------------------------------------------------------------------------------------------------------------- 
 
// Handle D-Bus method calls 
void onMethodCall
(
GDBusConnection *pConnection,
const gchar *pSender,
const gchar *pObjectPath,
const gchar *pInterfaceName,
const gchar *pMethodName,
GVariant *pParameters,
GDBusMethodInvocation *pInvocation,
gpointer pUserData
)
{
// Convert our input path into our custom type for path management 
DBusObjectPath objectPath(pObjectPath);
 
if (!TheServer->callMethod(objectPath, pInterfaceName, pMethodName, pConnection, pParameters, pInvocation, pUserData))
{
Logger::error(SSTR << " + Method not found: [" << pSender << "]:[" << objectPath << "]:[" << pInterfaceName << "]:[" << pMethodName << "]");
g_dbus_method_invocation_return_dbus_error(pInvocation, kErrorNotImplemented.c_str(), "This method is not implemented");
return;
}
 
return;
}
 
// Handle D-Bus requests to get a property 
GVariant *onGetProperty
(
GDBusConnection  *pConnection,
const gchar      *pSender,
const gchar      *pObjectPath,
const gchar      *pInterfaceName,
const gchar      *pPropertyName,
GError           **ppError,
gpointer         pUserData
)
{
// Convert our input path into our custom type for path management 
DBusObjectPath objectPath(pObjectPath);
 
const GattProperty *pProperty = TheServer->findProperty(objectPath, pInterfaceName, pPropertyName);
 
std::string propertyPath = std::string("[") + pSender + "]:[" + objectPath.toString() + "]:[" + pInterfaceName + "]:[" + pPropertyName + "]";
if (!pProperty)
{
Logger::error(SSTR << "Property(get) not found: " << propertyPath);
    g_set_error(ppError, G_IO_ERROR, G_IO_ERROR_FAILED, ("Property(get) not found: " + propertyPath).c_str(), pSender);
return nullptr;
}
 
if (!pProperty->getGetterFunc())
{
Logger::error(SSTR << "Property(get) func not found: " << propertyPath);
    g_set_error(ppError, G_IO_ERROR, G_IO_ERROR_FAILED, ("Property(get) func not found: " + propertyPath).c_str(), pSender);
return nullptr;
}
 
Logger::info(SSTR << "Calling property getter: " << propertyPath);
GVariant *pResult = pProperty->getGetterFunc()(pConnection, pSender, objectPath.c_str(), pInterfaceName, pPropertyName, ppError, pUserData);
 
if (nullptr == pResult)
{
    g_set_error(ppError, G_IO_ERROR, G_IO_ERROR_FAILED, ("Property(get) failed: " + propertyPath).c_str(), pSender);
    return nullptr;
}
 
return pResult;
}
 
// Handle D-Bus requests to set a property 
gboolean onSetProperty
(
GDBusConnection  *pConnection,
const gchar      *pSender,
const gchar      *pObjectPath,
const gchar      *pInterfaceName,
const gchar      *pPropertyName,
GVariant         *pValue,
GError           **ppError,
gpointer         pUserData
)
{
// Convert our input path into our custom type for path management 
DBusObjectPath objectPath(pObjectPath);
 
const GattProperty *pProperty = TheServer->findProperty(objectPath, pInterfaceName, pPropertyName);
 
std::string propertyPath = std::string("[") + pSender + "]:[" + objectPath.toString() + "]:[" + pInterfaceName + "]:[" + pPropertyName + "]";
if (!pProperty)
{
Logger::error(SSTR << "Property(set) not found: " << propertyPath);
    g_set_error(ppError, G_IO_ERROR, G_IO_ERROR_FAILED, ("Property(set) not found: " + propertyPath).c_str(), pSender);
return false;
}
 
if (!pProperty->getSetterFunc())
{
Logger::error(SSTR << "Property(set) func not found: " << propertyPath);
    g_set_error(ppError, G_IO_ERROR, G_IO_ERROR_FAILED, ("Property(set) func not found: " + propertyPath).c_str(), pSender);
return false;
}
 
Logger::info(SSTR << "Calling property getter: " << propertyPath);
if (!pProperty->getSetterFunc()(pConnection, pSender, objectPath.c_str(), pInterfaceName, pPropertyName, pValue, ppError, pUserData))
{
    g_set_error(ppError, G_IO_ERROR, G_IO_ERROR_FAILED, ("Property(set) failed: " + propertyPath).c_str(), pSender);
    return false;
}
 
return true;
}
 
// --------------------------------------------------------------------------------------------------------------------------------- 
//  _____     _ _                                                                              _ 
// |  ___|_ _(_) |_   _ _ __ ___   _ __ ___   __ _ _ __   __ _  __ _  ___ _ __ ___   ___ _ __ | |_ 
// | |_ / _` | | | | | | '__/ _ \ | '_ ` _ \ / _` | '_ \ / _` |/ _` |/ _ \ '_ ` _ \ / _ \ '_ \| __| 
// |  _| (_| | | | |_| | | |  __/ | | | | | | (_| | | | | (_| | (_| |  __/ | | | | |  __/ | | | |_ 
// |_|  \__,_|_|_|\__,_|_|  \___| |_| |_| |_|\__,_|_| |_|\__,_|\__, |\___|_| |_| |_|\___|_| |_|\__| 
//                                                             |___/ 
// 
// --------------------------------------------------------------------------------------------------------------------------------- 
 
// Convenience method for setting a retry timer so that failures (related to initialization) can be continuously retried until we 
// eventually succeed. 
void setRetryFailure()
{
retryTimeStart = time(nullptr);
Logger::info(SSTR << "  + Will retry this failed operation in about " << kRetryDelaySeconds << " seconds");
}
 
// --------------------------------------------------------------------------------------------------------------------------------- 
//   ____    _  _____ _____                  _     _             _   _ 
//  / ___|  / \|_   _|_   _|  _ __ ___  __ _(_)___| |_ _ __ __ _| |_(_) ___  _ ___ 
// | |  _  / _ \ | |   | |   | '__/ _ \/ _` | / __| __| '__/ _` | __| |/ _ \| '_  | 
// | |_| |/ ___ \| |   | |   | | |  __/ (_| | \__ \ |_| | | (_| | |_| | (_) | | | | 
//  \____/_/   \_\_|   |_|   |_|  \___|\__, |_|___/\__|_|  \__,_|\__|_|\___/|_| |_| 
//                                     |___/ 
// 
// --------------------------------------------------------------------------------------------------------------------------------- 
 
// Use the BlueZ GATT Manager proxy to register our GATT application with BlueZ 
void doRegisterApplication()
{
g_auto(GVariantBuilder) builder;
g_variant_builder_init(&builder, G_VARIANT_TYPE("a{sv}"));
GVariant *pParams = g_variant_new("(oa{sv})""/", &builder);
 
g_dbus_proxy_call
(
pBluezGattManagerProxy,         // GDBusProxy *proxy 
"RegisterApplication",          // const gchar *method_name   (ex: "GetManagedObjects") 
pParams,                        // GVariant *parameters 
G_DBUS_CALL_FLAGS_NONE,         // GDBusCallFlags flags 
-1,                             // gint timeout_msec 
nullptr,                        // GCancellable *cancellable 
 
// GAsyncReadyCallback callback 
[] (GObject * /*pSourceObject*/, GAsyncResult *pAsyncResult, gpointer /*pUserData*/)
{
GError *pError = nullptr;
GVariant *pVariant = g_dbus_proxy_call_finish(pBluezGattManagerProxy, pAsyncResult, &pError);
if (nullptr == pVariant)
{
Logger::error(SSTR << "Failed to register application: " << (nullptr == pError ? "Unknown" : pError->message));
setRetryFailure();
}
else
{
g_variant_unref(pVariant);
Logger::info(SSTR << "Application registered");
bApplicationRegistered = true;
}
 
// Keep going... 
initializationStateProcessor();
},
 
nullptr                         // gpointer user_data 
);
}
 
// --------------------------------------------------------------------------------------------------------------------------------- 
//   ___  _     _           _                    _     _             _   _ 
//  / _ \| |__ (_) ___  ___| |_   _ __ ___  __ _(_)___| |_ _ __ __ _| |_(_) ___  _ ___ 
// | | | | '_ \| |/ _ \/ __| __| | '__/ _ \/ _` | / __| __| '__/ _` | __| |/ _ \| '_  | 
// | |_| | |_) | |  __/ (__| |_  | | |  __/ (_| | \__ \ |_| | | (_| | |_| | (_) | | | | 
//  \___/|_.__// |\___|\___|\__| |_|  \___|\__, |_|___/\__|_|  \__,_|\__|_|\___/|_| |_| 
//           |__/                          |___/ 
// 
// Before we can register our service(s) with BlueZ, we must first register ourselves with D-Bus. The easiest way to do this is to 
// use an XML description of our D-Bus objects. 
// --------------------------------------------------------------------------------------------------------------------------------- 
 
void registerNodeHierarchy(GDBusNodeInfo *pNode, const DBusObjectPath &basePath = DBusObjectPath(), int depth = 1)
{
std::string prefix;
prefix.insert(0, depth * 2' ');
 
static GDBusInterfaceVTable interfaceVtable;
interfaceVtable.method_call = onMethodCall;
interfaceVtable.get_property = onGetProperty;
interfaceVtable.set_property = onSetProperty;
 
GDBusInterfaceInfo **ppInterface = pNode->interfaces;
 
Logger::debug(SSTR << prefix << "" << pNode->path);
 
while(nullptr != *ppInterface)
{
GError *pError = nullptr;
Logger::debug(SSTR << prefix << "    (iface: " << (*ppInterface)->name << ")");
guint registeredObjectId = g_dbus_connection_register_object
(
pBusConnection,             // GDBusConnection *connection 
basePath.c_str(),           // const gchar *object_path 
*ppInterface,               // GDBusInterfaceInfo *interface_info 
&interfaceVtable,           // const GDBusInterfaceVTable *vtable 
nullptr,                    // gpointer user_data 
nullptr,                    // GDestroyNotify user_data_free_func 
&pError                     // GError **error 
);
 
if (0 == registeredObjectId)
{
Logger::error(SSTR << "Failed to register object: " << (nullptr == pError ? "Unknown" : pError->message));
 
// Cleanup and pretend like we were never here 
g_dbus_node_info_unref(pNode);
registeredObjectIds.clear();
 
// Try again later 
setRetryFailure();
return;
}
 
// Save the registered object Id so we can clean it up later 
registeredObjectIds.push_back(registeredObjectId);
 
++ppInterface;
}
 
GDBusNodeInfo **ppChild = pNode->nodes;
while(nullptr != *ppChild)
{
registerNodeHierarchy(*ppChild, basePath + (*ppChild)->path, depth + 1);
 
++ppChild;
}
}
 
void registerObjects()
{
// Parse each object into an XML interface tree 
for (const DBusObject &object : TheServer->getObjects())
{
GError *pError = nullptr;
std::string xmlString = object.generateIntrospectionXML();
GDBusNodeInfo *pNode = g_dbus_node_info_new_for_xml(xmlString.c_str(), &pError);
if (nullptr == pNode)
{
Logger::error(SSTR << "Failed to introspect XML: " << (nullptr == pError ? "Unknown" : pError->message));
setRetryFailure();
return;
}
 
Logger::debug(SSTR << "Registering object hierarchy with D-Bus hierarchy");
 
// Register the node hierarchy 
registerNodeHierarchy(pNode, DBusObjectPath(pNode->path));
 
// Cleanup the node 
g_dbus_node_info_unref(pNode);
}
 
// Keep going 
initializationStateProcessor();
}
 
// --------------------------------------------------------------------------------------------------------------------------------- 
//     _       _             _                               __ _                       _   _ 
//    / \   __| | __ _ _ __ | |_ ___ _ __    ___ ___  _ __  / _(_) __ _ _   _ _ __ __ _| |_(_) ___  _ ___ 
//   / _ \ / _` |/ _` | '_ \| __/ _ \ '__|  / __/ _ \| '_ \| |_| |/ _` | | | | '__/ _` | __| |/ _ \| '_  | 
//  / ___ \ (_| | (_| | |_) | ||  __/ |    | (_| (_) | | | |  _| | (_| | |_| | | | (_| | |_| | (_) | | | | 
// /_/   \_\__,_|\__,_| .__/ \__\___|_|     \___\___/|_| |_|_| |_|\__, |\__,_|_|  \__,_|\__|_|\___/|_| |_| 
//                    |_|                                         |___/ 
// 
// --------------------------------------------------------------------------------------------------------------------------------- 
 
// Configure an adapter to ensure it is setup the way we need. We turn things on that we need and turn everything else off 
// (to maximize security.) 
// 
// Note that this only works for the first adapter (index 0). Search for kControllerIndex for information. 
// 
// See also: https://git.kernel.org/pub/scm/bluetooth/bluez.git/tree/doc/mgmt-api.txt 
void configureAdapter()
{
Mgmt mgmt;
 
// Find out what our current settings are 
Logger::info(SSTR << "Getting device information");
Mgmt::ControllerInformation *pInfo = mgmt.getControllerInformation();
if (nullptr == pInfo)
{
setRetryFailure();
return;
}
 
// We need it off to start with 
if ((pInfo->currentSettings & Mgmt::EHciPowered) != 0)
{
Logger::info(SSTR << "Powering off");
if (!mgmt.setPowered(false))
{
setRetryFailure();
return;
}
}
 
// Change the Br/Edr state? 
bool bredrCurrentState = (pInfo->currentSettings & Mgmt::EHciBasicRate_EnhancedDataRate) != 0 ? true:false;
if (TheServer->getEnableBREDR() != bredrCurrentState)
{
Logger::info(SSTR << (TheServer->getEnableBREDR() ? "Enabling":"Disabling") << " BR/EDR");
if (!mgmt.setBredr(TheServer->getEnableBREDR()))
{
setRetryFailure();
return;
}
}
 
// Change the Secure Connectinos state? 
bool scCurrentState = (pInfo->currentSettings & Mgmt::EHciSecureConnections) != 0 ? true:false;
if (TheServer->getEnableSecureConnection() != scCurrentState)
{
Logger::info(SSTR << (TheServer->getEnableSecureConnection() ? "Enabling":"Disabling") << " Secure Connections");
if (!mgmt.setSecureConnections(TheServer->getEnableSecureConnection() ? 1 : 0))
{
setRetryFailure();
return;
}
}
 
// Change the Bondable state? 
bool bondableCurrentState = (pInfo->currentSettings & Mgmt::EHciBondable) != 0 ? true:false;
if (TheServer->getEnableBondable() != bondableCurrentState)
{
Logger::info(SSTR << (TheServer->getEnableBondable() ? "Enabling":"Disabling") << " Bondable");
if (!mgmt.setBondable(TheServer->getEnableBondable()))
{
setRetryFailure();
return;
}
}
 
// Change the Connectable state? 
bool connectableCurrentState = (pInfo->currentSettings & Mgmt::EHciConnectable) != 0 ? true:false;
if (TheServer->getEnableConnectable() != connectableCurrentState)
{
Logger::info(SSTR << (TheServer->getEnableConnectable() ? "Enabling":"Disabling") << " Connectable");
if (!mgmt.setConnectable(TheServer->getEnableConnectable()))
{
setRetryFailure();
return;
}
}
 
// Enable the LE state (we always set this state if it's not set) 
if ((pInfo->currentSettings & Mgmt::EHciLowEnergy) == 0)
{
Logger::info(SSTR << "Enabling LE");
if (!mgmt.setLE(true))
{
setRetryFailure();
return;
}
}
 
// Change the Advertising state? 
bool advertisingCurrentState = (pInfo->currentSettings & Mgmt::EHciAdvertising) != 0 ? true:false;
if (TheServer->getEnableAdvertising() != advertisingCurrentState)
{
Logger::info(SSTR << (TheServer->getEnableAdvertising() ? "Enabling":"Disabling") << " Advertising");
if (!mgmt.setAdvertising(TheServer->getEnableAdvertising() ? 1 : 0))
{
setRetryFailure();
return;
}
}
 
// Set the name? 
if (kCustomGlobalAdvertisingName.length() != 0 || kCustomGlobalAdvertisingShortName.length() != 0)
{
if (Mgmt::truncateName(kCustomGlobalAdvertisingName) != pInfo->name ||
Mgmt::truncateShortName(kCustomGlobalAdvertisingShortName) != pInfo->shortName)
{
Logger::info(SSTR << "Setting name to '" << kCustomGlobalAdvertisingName << "'");
if (!mgmt.setName(kCustomGlobalAdvertisingName.c_str(), kCustomGlobalAdvertisingName.c_str()))
{
setRetryFailure();
return;
}
}
}
 
// Turn it back on 
Logger::info(SSTR << "Powering on");
if (!mgmt.setPowered(true))
{
setRetryFailure();
return;
}
 
// We can ignore errors on this - we're just letting it dump the output 
mgmt.getControllerInformation();
 
bAdapterConfigured = true;
initializationStateProcessor();
}
 
// --------------------------------------------------------------------------------------------------------------------------------- 
//     _       _             _ 
//    / \   __| | __ _ _ __ | |_ ___ _ __ 
//   / _ \ / _` |/ _` | '_ \| __/ _ \ '__| 
//  / ___ \ (_| | (_| | |_) | ||  __/ | 
// /_/   \_\__,_|\__,_| .__/ \__\___|_| 
//                    |_| 
// 
// --------------------------------------------------------------------------------------------------------------------------------- 
 
// Find the BlueZ's GATT Manager interface for the *first* Bluetooth adapter provided by BlueZ. We'll need this to register our 
// GATT server with BlueZ. 
void findAdapterInterface()
{
// Get a list of the BlueZ's D-Bus objects 
GList *pObjects = g_dbus_object_manager_get_objects(pBluezObjectManager);
if (nullptr == pObjects)
{
Logger::error(SSTR << "Unable to get ObjectManager objects");
setRetryFailure();
return;
}
 
// Scan the list of objects we find one with a GATT manager interface 
// 
// Note that if there are multiple interfaces, we will only find the first 
for (guint i = 0; i < g_list_length(pObjects) && bluezGattManagerInterfaceName.empty(); ++i)
{
// Current object in question 
pBluezAdapterObject = static_cast<GDBusObject *>(g_list_nth_data(pObjects, i));
if (nullptr == pBluezAdapterObject) { continue}
 
// See if it has a GATT manager interface 
pBluezGattManagerProxy = reinterpret_cast<GDBusProxy *>(g_dbus_object_get_interface(pBluezAdapterObject, "org.bluez.GattManager1"));
if (nullptr == pBluezGattManagerProxy) { continue}
 
// Get the interface proxy for this adapter - this will come in handy later 
pBluezAdapterInterfaceProxy = reinterpret_cast<GDBusProxy *>(g_dbus_object_get_interface(pBluezAdapterObject, "org.bluez.Adapter1"));
if (nullptr == pBluezAdapterInterfaceProxy)
{
Logger::warn(SSTR << "Failed to get adapter proxy for interface 'org.bluez.Adapter1'");
continue;
}
 
// Get the interface proxy for this adapter's properties - this will come in handy later 
pBluezAdapterPropertiesInterfaceProxy = reinterpret_cast<GDBusProxy *>(g_dbus_object_get_interface(pBluezAdapterObject, "org.freedesktop.DBus.Properties"));
if (nullptr == pBluezAdapterPropertiesInterfaceProxy)
{
Logger::warn(SSTR << "Failed to get adapter properties proxy for interface 'org.freedesktop.DBus.Properties'");
continue;
}
 
// Finally, save off the interface name, we're done! 
bluezGattManagerInterfaceName = g_dbus_proxy_get_object_path(pBluezGattManagerProxy);
break;
}
 
// Get a fresh copy of our objects so we can release the entire list 
pBluezAdapterObject = g_dbus_object_manager_get_object(pBluezObjectManager, g_dbus_object_get_object_path(pBluezAdapterObject));
 
// We'll need access to the device object so we can set properties on it 
pBluezDeviceObject = g_dbus_object_manager_get_object(pBluezObjectManager, g_dbus_object_get_object_path(pBluezAdapterObject));
 
// Cleanup the list 
for (guint i = 0; i < g_list_length(pObjects) && bluezGattManagerInterfaceName.empty(); ++i)
{
g_object_unref(g_list_nth_data(pObjects, i));
}
 
g_list_free(pObjects);
 
// If we didn't find the adapter object, reset things and we'll try again later 
if (nullptr == pBluezAdapterObject || nullptr == pBluezDeviceObject)
{
Logger::info(SSTR << "Unable to find BlueZ objects outside of object list");
bluezGattManagerInterfaceName.clear();
}
 
// If we never ended up with an interface name, bail now 
if (bluezGattManagerInterfaceName.empty())
{
Logger::error(SSTR << "Unable to find the adapter");
setRetryFailure();
return;
}
 
// Keep going 
initializationStateProcessor();
}
 
// --------------------------------------------------------------------------------------------------------------------------------- 
//  ____  _            _____   ___  _     _           _   __  __ 
// | __ )| |_   _  ___|__  /  / _ \| |__ (_) ___  ___| |_|  \/  | __ _ _ __   __ _  __ _  ___ _ __ 
// |  _ \| | | | |/ _ \ / /  | | | | '_ \| |/ _ \/ __| __| |\/| |/ _` | '_ \ / _` |/ _` |/ _ \ '__| 
// | |_) | | |_| |  __// /_  | |_| | |_) | |  __/ (__| |_| |  | | (_| | | | | (_| | (_| |  __/ | 
// |____/|_|\__,_|\___/____|  \___/|_.__// |\___|\___|\__|_|  |_|\__,_|_| |_|\__,_|\__, |\___|_| 
//                                     |__/                                        |___/ 
// 
// --------------------------------------------------------------------------------------------------------------------------------- 
 
// Get the D-Bus Object Manager client to the BlueZ ObjectManager object 
// 
// An ObjectManager allows us to find out what objects (and from those, interfaces, etc.) are available from an owned name. We'll 
// use this to interrogate BlueZ's objects to find an adapter we can use, among other things. 
void getBluezObjectManager()
{
g_dbus_object_manager_client_new
(
pBusConnection,                             // GDBusConnection 
G_DBUS_OBJECT_MANAGER_CLIENT_FLAGS_NONE,    // GDBusObjectManagerClientFlags 
"org.bluez",                                // Owner name (or well-known name) 
"/",                                        // Object path 
nullptr,                                    // GDBusProxyTypeFunc get_proxy_type_func 
nullptr,                                    // gpointer get_proxy_type_user_data 
nullptr,                                    // GDestroyNotify get_proxy_type_destroy_notify 
nullptr,                                    // GCancellable *cancellable 
 
// GAsyncReadyCallback callback 
[] (GObject * /*pSourceObject*/, GAsyncResult *pAsyncResult, gpointer /*pUserData*/)
{
// Store BlueZ's ObjectManager 
GError *pError = nullptr;
pBluezObjectManager = g_dbus_object_manager_client_new_finish(pAsyncResult, &pError);
 
if (nullptr == pBluezObjectManager)
{
Logger::error(SSTR << "Failed to get an ObjectManager client: " << (nullptr == pError ? "Unknown" : pError->message));
setRetryFailure();
return;
}
 
// Keep going 
initializationStateProcessor();
},
 
nullptr                                     // gpointer user_data 
);
}
 
// --------------------------------------------------------------------------------------------------------------------------------- 
//   ___                          _ 
//  / _ \__      ___ __   ___  __| |  _ __   __ _ _ __ ___   ___ 
// | | | \ \ /\ / / '_ \ / _ \/ _` | | '_ \ / _` | '_ ` _ \ / _ ) 
// | |_| |\ V  V /| | | |  __/ (_| | | | | | (_| | | | | | |  __/ 
//  \___/  \_/\_/ |_| |_|\___|\__,_| |_| |_|\__,_|_| |_| |_|\___| 
// 
// --------------------------------------------------------------------------------------------------------------------------------- 
 
// Acquire an "owned name" with D-Bus. This name represents our server as a whole, identifying us on D-Bus and allowing others 
// (BlueZ) to communicate back to us. All of the D-Bus objects (which represent our BlueZ services, characteristics, etc.) will all 
// reside under this owned name. 
// 
// Note about error management: We don't yet hwave a timeout callback running for retries; errors are considered fatal 
void doOwnedNameAcquire()
{
// Our name is not presently lost 
bOwnedNameAcquired = false;
 
ownedNameId = g_bus_own_name_on_connection
(
pBusConnection,                   // GDBusConnection *connection 
kServerOwnedName.c_str(),         // const gchar *name 
G_BUS_NAME_OWNER_FLAGS_NONE,      // GBusNameOwnerFlags flags 
 
// GBusNameAcquiredCallback name_acquired_handler 
[](GDBusConnection *, const gchar *, gpointer)
{
// Handy way to get periodic activity 
periodicTimeoutId = g_timeout_add_seconds(kPeriodicTimerFrequencySeconds, onPeriodicTimer, pBusConnection);
if (periodicTimeoutId <= 0)
{
Logger::fatal(SSTR << "Failed to add a periodic timer");
setServerHealth(EFailedInit);
shutdown();
}
 
// Bus name acquired 
bOwnedNameAcquired = true;
 
// Keep going... 
initializationStateProcessor();
},
 
// GBusNameLostCallback name_lost_handler 
[](GDBusConnection *, const gchar *, gpointer)
{
// Bus name lost 
bOwnedNameAcquired = false;
 
// If we don't have a periodicTimeout (which we use for error recovery) then we're done 
if (0 == periodicTimeoutId)
{
Logger::fatal(SSTR << "Unable to acquire an owned name ('" << kServerOwnedName << "') on the bus");
setServerHealth(EFailedInit);
shutdown();
}
else
{
Logger::warn(SSTR << "Owned name ('" << kServerOwnedName << "') lost");
setRetryFailure();
return;
}
 
// Keep going... 
initializationStateProcessor();
},
 
nullptr,                          // gpointer user_data 
nullptr                           // GDestroyNotify user_data_free_func 
);
}
 
// --------------------------------------------------------------------------------------------------------------------------------- 
//  ____ 
// | __ ) _   _ ___ 
// |  _ \| | | / __| 
// | |_) | |_| \__ ) 
// |____/ \__,_|___/ 
// 
// --------------------------------------------------------------------------------------------------------------------------------- 
 
// Acquire a connection to the SYSTEM bus so we can communicate with BlueZ. 
// 
// Note about error management: We don't yet hwave a timeout callback running for retries; errors are considered fatal 
void doBusAcquire()
{
// Acquire a connection to the SYSTEM bus 
g_bus_get
(
G_BUS_TYPE_SYSTEM,      // GBusType bus_type 
nullptr,                // GCancellable *cancellable 
 
// GAsyncReadyCallback callback 
[] (GObject */*pSourceObject*/, GAsyncResult *pAsyncResult, gpointer /*pUserData*/)
{
GError *pError = nullptr;
pBusConnection = g_bus_get_finish(pAsyncResult, &pError);
 
if (nullptr == pBusConnection)
{
Logger::fatal(SSTR << "Failed to get bus connection: " << (nullptr == pError ? "Unknown" : pError->message));
setServerHealth(EFailedInit);
shutdown();
}
 
// Continue 
initializationStateProcessor();
},
 
nullptr                 // gpointer user_data 
);
}
 
// --------------------------------------------------------------------------------------------------------------------------------- 
//  ____  _        _                                                                     _ 
// / ___|| |_ __ _| |_ ___   _ __ ___   __ _ _ __   __ _  __ _  ___ _ __ ___   ___ _ __ | |_ 
// \___ \| __/ _` | __/ _ \ | '_ ` _ \ / _` | '_ \ / _` |/ _` |/ _ \ '_ ` _ \ / _ \ '_ \| __| 
//  ___) | || (_| | ||  __/ | | | | | | (_| | | | | (_| | (_| |  __/ | | | | |  __/ | | | |_ 
// |____/ \__\__,_|\__\___| |_| |_| |_|\__,_|_| |_|\__,_|\__, |\___|_| |_| |_|\___|_| |_|\__| 
//                                                       |___/ 
// 
// --------------------------------------------------------------------------------------------------------------------------------- 
 
// Poor-man's state machine, which effectively ensures everything is initialized in order by verifying actual initialization state 
// rather than stepping through a set of numeric states. This way, if something fails in an out-of-order sort of way, we can still 
// handle it and recover nicely. 
void initializationStateProcessor()
{
// If we're in our end-of-life or waiting for a retry, don't process states 
if (ggkGetServerRunState() > ERunning || 0 != retryTimeStart) { return}
 
// 
// Get a bus connection 
// 
if (nullptr == pBusConnection)
{
Logger::info(SSTR << "Acquiring bus connection");
doBusAcquire();
return;
}
 
// 
// Acquire an owned name on the bus 
// 
if (!bOwnedNameAcquired)
{
Logger::info(SSTR << "Acquiring owned name: '" << kServerOwnedName << "'");
doOwnedNameAcquire();
return;
}
 
// 
// Get BlueZ's ObjectManager 
// 
if (nullptr == pBluezObjectManager)
{
Logger::debug(SSTR << "Getting BlueZ ObjectManager");
getBluezObjectManager();
return;
}
 
// 
// Find the adapter interface 
// 
if (bluezGattManagerInterfaceName.empty())
{
Logger::debug(SSTR << "Finding BlueZ GattManager1 interface");
findAdapterInterface();
return;
}
 
// 
// Find the adapter interface 
// 
if (!bAdapterConfigured)
{
Logger::info(SSTR << "Configuring BlueZ adapter '" << bluezGattManagerInterfaceName << "'");
configureAdapter();
return;
}
 
// 
// Register our object with D-bus 
// 
if (registeredObjectIds.empty())
{
Logger::info(SSTR << "Registering with D-Bus");
registerObjects();
return;
}
 
// Register our appliation with the BlueZ GATT manager 
if (!bApplicationRegistered)
{
Logger::info(SSTR << "Registering application with BlueZ GATT manager");
 
doRegisterApplication();
return;
}
 
// At this point, we should be fully initialized 
// 
// It shouldn't ever happen, but just in case, let's double-check that we're healthy and if not, shutdown immediately 
if (ggkGetServerHealth() != EOk)
{
shutdown();
return;
}
 
// Successful initialization - switch to running state 
setServerRunState(ERunning);
}
 
// --------------------------------------------------------------------------------------------------------------------------------- 
//  ____                                                                  _ 
// |  _ \ _   _ _ __     ___  ___ _ ____   _____ _ __    _ __ _   _ _ __ | | 
// | |_) | | | | '_ \   / __|/ _ \ '__\ \ / / _ \ '__|  | '__| | | | '_ \| | 
// |  _ <| |_| | | | |  \__ \  __/ |   \ V /  __/ | _   | |  | |_| | | | |_| 
// |_| \_\\__,_|_| |_|  |___/\___|_|    \_/ \___|_|( )  |_|   \__,_|_| |_(_) 
//                                                 |/ 
// 
// --------------------------------------------------------------------------------------------------------------------------------- 
 
// Entry point for the asynchronous server thread 
// 
// This method should not be called directly, instead, direct your attention over to `ggkStart()` 
void runServerThread()
{
// Set the initialization state 
setServerRunState(EInitializing);
 
// Start our state processor, which is really just a simplified state machine that steps us through an asynchronous 
// initialization process. 
// 
// In case you're wondering if these really need to be async, the answer is yes. For one, it's the right way to do it. But 
// the more practical response is that the main loop must be running (see below) in order for us to receive and respond to 
// events from BlueZ. Well, one of the calls we need to make during initialization is 'RegisterApplication'. This method will 
// will require that we respond the 'GetNamedObjects' method before it returns. If we were to call the synchronous version of 
// 'RegisterApplication', then we've effectively created a deadlock. 
// 
// There are alternatives, but using async methods is the recommended way. 
initializationStateProcessor();
 
Logger::info(SSTR << "Starting main loop");
pMainLoop = g_main_loop_new(NULLFALSE);
 
// Add the idle function 
// 
// Note that we actually run the idle function from a lambda. This allows us to manage the inter-idle sleep so we don't 
// soak up 100% of our CPU. 
guint res = g_idle_add
(
[](gpointer pUserData) -> gboolean
{
// Try to process some data and if no data is processed, sleep for the requested frequency 
if (!idleFunc(pUserData))
{
std::this_thread::sleep_for(std::chrono::milliseconds(kIdleFrequencyMS));
}
 
// Always return TRUE so our idle remains in tact 
return TRUE;
},
nullptr
);
 
if (res == 0)
{
Logger::error(SSTR << "Unable to add idle to main loop");
}
else
{
Logger::info("Idle function added successfully");
}
 
g_main_loop_run(pMainLoop);
 
// We have stopped 
setServerRunState(EStopped);
 
// Cleanup 
uninit();
}