-
Notifications
You must be signed in to change notification settings - Fork 2
/
flipper-BLE.html
864 lines (731 loc) · 24.8 KB
/
flipper-BLE.html
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
<head>
<!-- CodeMirror CSS -->
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.58.3/codemirror.min.css">
<link href="https://fonts.googleapis.com/css2?family=Roboto:wght@400;500&display=swap" rel="stylesheet">
<style>
body {
font-family: Arial, sans-serif;
margin: 0;
padding: 0;
align-items: center;
font-family: 'Roboto', sans-serif;
}
.container {
/* Smooth, broad shadow for a more modern, 'floating' look */
box-shadow: 0 4px 20px rgba(0, 0, 0, 0.2);
/* Border radius for slightly rounded corners */
border-radius: 8px;
/* Transition for smooth effects on state changes, e.g., hover */
transition: box-shadow 0.3s ease;
/* Full width as specified */
width: 100%;
/* Optional: Consider adding some padding for content spacing */
padding: 20px;
}
select {
width: 100%;
padding: 10px;
margin-bottom: 20px;
border: 1px solid #ccc;
border-radius: 4px;
font-size: 16px;
}
.CodeMirror {
border: 1px solid #ccc;
border-radius: 4px;
height: 700;
}
button {
background-color: #007BFF; /* Blue color */
color: white; /* Text color */
padding: 10px 20px; /* Top/bottom and left/right padding */
border: none; /* Remove default border */
border-radius: 5px; /* Rounded corners */
cursor: pointer; /* Hand cursor on hover */
font-size: 16px; /* Font size */
transition: background-color 0.3s; /* Smooth transition for hover effect */
margin-bottom: 10px;
}
button:hover {
background-color: #0056b3; /* Darker blue on hover */
}
button:active {
background-color: #004085; /* Even darker blue when active (clicked) */
}
</style>
</head>
<body>
<div class="container">
<select id="colorSelector">
<option value="Device Type" disabled selected>Select Device Type</option>
<option value="0x07, 0x19, 0x05, 0x00, 0x55, 0x10, 0x00, 0x00, 0x01, 0x06, 0xf6, 0x85, 0xda, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00">Apple AirTag</option>
<option value="0x04, 0x04, 0x20, 0x00, 0x00, 0x00, 0x0f, 0x05, 0xc0, 0x13, 0x00, 0x4c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00">Apple Keyboard</option>
<option value="0x04, 0x04, 0x20, 0x00, 0x00, 0x00, 0x0f, 0x05, 0xc0, 0x02, 0x60, 0x4c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00">Apple Transfer Number Notification</option>
<option value="0x04, 0x04, 0x20, 0x00, 0x00, 0x00, 0x0f, 0x05, 0xc0, 0x09, 0x60, 0x4c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00">Apple Set Up New iPhone Notification</option>
<option value="0x04, 0x04, 0x20, 0x00, 0x00, 0x00, 0x0f, 0x05, 0xc0, 0x27, 0x60, 0x4c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00">Apple Join This Apple TV Notification</option>
</select>
<!-- Hidden code box -->
<div id="hiddenCode" style="display:none;">#include "gap.h"
#include <ble/ble.h>
#include <furi_hal.h>
#include <furi.h>
#define TAG "BtGap"
#define FAST_ADV_TIMEOUT 30000
#define INITIAL_ADV_TIMEOUT 60000
#define GAP_INTERVAL_TO_MS(x)(uint16_t)((x) *1.25)
// Created with https://techryptic.github.io/
// Kudos to the following contributors who should be highlighted: ChatGPT, MidJourney, Salvador M. (For parallel efforts in the gap.c edits. We independently crafted similar edits to gap.c. Even though I didn't upload mine to GitHub, I wanted to give you a shoutout for your contribution.), shoutout to STMicroelectronics for the GAP commands used in gap.c, willyjlz for the FAP App (They contacted me through Reddit seeking assistance with the conversion.), and several Continuity projects (Openhaystack, AirGuard, Find-You, FakeTag) on GitHubs.
// This code strictly adheres to ethical standards and does not contain any elements related to the DDoS attacks. If you're seeking such functionalities, please look elsewhere.
// If you want to learn more about the Continuity Protocol: https://cfp.recon.cx/2023/talk/VSYA9H/ and https://cfp.recon.cx/media/2023/submissions/VSYA9H/resources/HandoffReconSlides_Eywqahv.pdf
// Apple consistently introduces new features. If you're looking to capture new BLE notification types, I'd suggest using a Faraday box equipped with a screen. The notifications are standardized, with only a few bytes differentiating them.
typedef struct
{
uint16_t gap_svc_handle;
uint16_t dev_name_char_handle;
uint16_t appearance_char_handle;
uint16_t connection_handle;
uint8_t adv_svc_uuid_len;
uint8_t adv_svc_uuid[20];
char *adv_name;
}
GapSvc;
typedef struct
{
GapSvc service;
GapConfig * config;
GapConnectionParams connection_params;
GapState state;
FuriMutex * state_mutex;
GapEventCallback on_event_cb;
void *context;
FuriTimer * advertise_timer;
FuriThread * thread;
FuriMessageQueue * command_queue;
bool enable_adv;
}
Gap;
typedef enum
{
GapCommandAdvFast,
GapCommandAdvLowPower,
GapCommandAdvStop,
GapCommandKillThread,
}
GapCommand;
static
const uint8_t gap_irk[16] = { 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0
};
static
const uint8_t gap_erk[16] = { 0xfe, 0xdc, 0xba, 0x09, 0x87, 0x65, 0x43, 0x21, 0xfe, 0xdc, 0xba, 0x09, 0x87, 0x65, 0x43, 0x21
};
static Gap *gap = NULL;
static void gap_advertise_start(GapState new_state);
static int32_t gap_app(void *context);
static void gap_verify_connection_parameters(Gap *gap)
{
furi_assert(gap);
FURI_LOG_I( TAG,
"Connection parameters: Connection Interval: %d (%d ms), Slave Latency: %d, Supervision Timeout: %d",
gap->connection_params.conn_interval,
GAP_INTERVAL_TO_MS(gap->connection_params.conn_interval),
gap->connection_params.slave_latency,
gap->connection_params.supervisor_timeout);
// Send connection parameters request update if necessary
GapConnectionParamsRequest *params = &gap->config->conn_param;
if (params->conn_int_min > gap->connection_params.conn_interval ||
params->conn_int_max < gap->connection_params.conn_interval)
{
FURI_LOG_W(TAG, "Unsupported connection interval. Request connection parameters update");
if (aci_l2cap_connection_parameter_update_req( gap->service.connection_handle,
params->conn_int_min,
params->conn_int_max,
gap->connection_params.slave_latency,
gap->connection_params.supervisor_timeout))
{
FURI_LOG_E(TAG, "Failed to request connection parameters update");
}
}
}
SVCCTL_UserEvtFlowStatus_t SVCCTL_App_Notification(void *pckt)
{
hci_event_pckt * event_pckt;
evt_le_meta_event * meta_evt;
evt_blue_aci * blue_evt;
hci_le_phy_update_complete_event_rp0 * evt_le_phy_update_complete;
uint8_t tx_phy;
uint8_t rx_phy;
tBleStatus ret = BLE_STATUS_INVALID_PARAMS;
event_pckt = (hci_event_pckt*)((hci_uart_pckt*) pckt)->data;
if (gap)
{
furi_mutex_acquire(gap->state_mutex, FuriWaitForever);
}
switch (event_pckt->evt)
{
case EVT_DISCONN_COMPLETE:
{
hci_disconnection_complete_event_rp0 *disconnection_complete_event =
(hci_disconnection_complete_event_rp0*) event_pckt->data;
if (disconnection_complete_event->Connection_Handle == gap->service.connection_handle)
{
gap->service.connection_handle = 0;
gap->state = GapStateIdle;
FURI_LOG_I( TAG, "Disconnect from client. Reason: %02X", disconnection_complete_event->Reason);
}
if (gap->enable_adv)
{
// Restart advertising
gap_advertise_start(GapStateAdvFast);
}
GapEvent event = {.type = GapEventTypeDisconnected
};
gap->on_event_cb(event, gap->context);
}
break;
case EVT_LE_META_EVENT:
meta_evt = (evt_le_meta_event*) event_pckt->data;
switch (meta_evt->subevent)
{
case EVT_LE_CONN_UPDATE_COMPLETE:
{
hci_le_connection_update_complete_event_rp0 *event =
(hci_le_connection_update_complete_event_rp0*) meta_evt->data;
gap->connection_params.conn_interval = event->Conn_Interval;
gap->connection_params.slave_latency = event->Conn_Latency;
gap->connection_params.supervisor_timeout = event->Supervision_Timeout;
FURI_LOG_I(TAG, "Connection parameters event complete");
gap_verify_connection_parameters(gap);
break;
}
case EVT_LE_PHY_UPDATE_COMPLETE:
evt_le_phy_update_complete = (hci_le_phy_update_complete_event_rp0*) meta_evt->data;
if (evt_le_phy_update_complete->Status)
{
FURI_LOG_E( TAG, "Update PHY failed, status %d", evt_le_phy_update_complete->Status);
}
else
{
FURI_LOG_I(TAG, "Update PHY succeed");
}
ret = hci_le_read_phy(gap->service.connection_handle, &tx_phy, &rx_phy);
if (ret)
{
FURI_LOG_E(TAG, "Read PHY failed, status: %d", ret);
}
else
{
FURI_LOG_I(TAG, "PHY Params TX = %d, RX = %d ", tx_phy, rx_phy);
}
break;
case EVT_LE_CONN_COMPLETE:
{
hci_le_connection_complete_event_rp0 *event =
(hci_le_connection_complete_event_rp0*) meta_evt->data;
gap->connection_params.conn_interval = event->Conn_Interval;
gap->connection_params.slave_latency = event->Conn_Latency;
gap->connection_params.supervisor_timeout = event->Supervision_Timeout;
// Stop advertising as connection completed
furi_timer_stop(gap->advertise_timer);
// Update connection status and handle
gap->state = GapStateConnected;
gap->service.connection_handle = event->Connection_Handle;
gap_verify_connection_parameters(gap);
// Start pairing by sending security request
aci_gap_slave_security_req(event->Connection_Handle);
}
break;
default:
break;
}
break;
case EVT_VENDOR:
blue_evt = (evt_blue_aci*) event_pckt->data;
switch (blue_evt->ecode)
{
aci_gap_pairing_complete_event_rp0 * pairing_complete;
case EVT_BLUE_GAP_LIMITED_DISCOVERABLE:
FURI_LOG_I(TAG, "Limited discoverable event");
break;
case EVT_BLUE_GAP_PASS_KEY_REQUEST:
{
// Generate random PIN code
uint32_t pin = rand() % 999999; //-V1064
aci_gap_pass_key_resp(gap->service.connection_handle, pin);
if (furi_hal_rtc_is_flag_set(FuriHalRtcFlagLock))
{
FURI_LOG_I(TAG, "Pass key request event. Pin: ******");
}
else
{
FURI_LOG_I(TAG, "Pass key request event. Pin: %06ld", pin);
}
GapEvent event = {.type = GapEventTypePinCodeShow, .data.pin_code = pin
};
gap->on_event_cb(event, gap->context);
}
break;
case EVT_BLUE_ATT_EXCHANGE_MTU_RESP:
{
aci_att_exchange_mtu_resp_event_rp0 *pr = (void*) blue_evt->data;
FURI_LOG_I(TAG, "Rx MTU size: %d", pr->Server_RX_MTU);
// Set maximum packet size given header size is 3 bytes
GapEvent event = { .type = GapEventTypeUpdateMTU, .data.max_packet_size = pr->Server_RX_MTU - 3
};
gap->on_event_cb(event, gap->context);
}
break;
case EVT_BLUE_GAP_AUTHORIZATION_REQUEST:
FURI_LOG_D(TAG, "Authorization request event");
break;
case EVT_BLUE_GAP_SLAVE_SECURITY_INITIATED:
FURI_LOG_D(TAG, "Slave security initiated");
break;
case EVT_BLUE_GAP_BOND_LOST:
FURI_LOG_D(TAG, "Bond lost event. Start rebonding");
aci_gap_allow_rebond(gap->service.connection_handle);
break;
case EVT_BLUE_GAP_DEVICE_FOUND:
FURI_LOG_D(TAG, "Device found event");
break;
case EVT_BLUE_GAP_ADDR_NOT_RESOLVED:
FURI_LOG_D(TAG, "Address not resolved event");
break;
case EVT_BLUE_GAP_KEYPRESS_NOTIFICATION:
FURI_LOG_D(TAG, "Key press notification event");
break;
case EVT_BLUE_GAP_NUMERIC_COMPARISON_VALUE:
{
uint32_t pin =
((aci_gap_numeric_comparison_value_event_rp0*)(blue_evt->data))->Numeric_Value;
FURI_LOG_I(TAG, "Verify numeric comparison: %06ld", pin);
GapEvent event = {.type = GapEventTypePinCodeVerify, .data.pin_code = pin
};
bool result = gap->on_event_cb(event, gap->context);
aci_gap_numeric_comparison_value_confirm_yesno(gap->service.connection_handle, result);
break;
}
case EVT_BLUE_GAP_PAIRING_CMPLT:
pairing_complete = (aci_gap_pairing_complete_event_rp0*) blue_evt->data;
if (pairing_complete->Status)
{
FURI_LOG_E( TAG,
"Pairing failed with status: %d. Terminating connection",
pairing_complete->Status);
aci_gap_terminate(gap->service.connection_handle, 5);
}
else
{
FURI_LOG_I(TAG, "Pairing complete");
GapEvent event = {.type = GapEventTypeConnected
};
gap->on_event_cb(event, gap->context);
}
break;
case EVT_BLUE_GAP_PROCEDURE_COMPLETE:
FURI_LOG_D(TAG, "Procedure complete event");
break;
case EVT_BLUE_L2CAP_CONNECTION_UPDATE_RESP:
{
uint16_t result =
((aci_l2cap_connection_update_resp_event_rp0*)(blue_evt->data))->Result;
if (result == 0)
{
FURI_LOG_D(TAG, "Connection parameters accepted");
}
else if (result == 1)
{
FURI_LOG_D(TAG, "Connection parameters denied");
}
break;
}
}
default:
break;
}
if (gap)
{
furi_mutex_release(gap->state_mutex);
}
return SVCCTL_UserEvtFlowEnable;
}
static void set_advertisment_service_uid(uint8_t *uid, uint8_t uid_len)
{
if (uid_len == 2)
{
gap->service.adv_svc_uuid[0] = AD_TYPE_16_BIT_SERV_UUID;
}
else if (uid_len == 4)
{
gap->service.adv_svc_uuid[0] = AD_TYPE_32_BIT_SERV_UUID;
}
else if (uid_len == 16)
{
gap->service.adv_svc_uuid[0] = AD_TYPE_128_BIT_SERV_UUID_CMPLT_LIST;
}
memcpy(&gap->service.adv_svc_uuid[gap->service.adv_svc_uuid_len], uid, uid_len);
gap->service.adv_svc_uuid_len += uid_len;
}
static void gap_init_svc(Gap *gap)
{
tBleStatus status;
uint32_t srd_bd_addr[2];
// HCI Reset to synchronise BLE Stack
hci_reset();
// Configure mac address
aci_hal_write_config_data( CONFIG_DATA_PUBADDR_OFFSET, CONFIG_DATA_PUBADDR_LEN, gap->config->mac_address);
/*Static random Address
*The two upper bits shall be set to 1
*The lowest 32bits is read from the UDN to differentiate between devices
*The RNG may be used to provide a random number on each power on
*/
srd_bd_addr[1] = 0x0000ED6E;
srd_bd_addr[0] = LL_FLASH_GetUDN();
aci_hal_write_config_data( CONFIG_DATA_RANDOM_ADDRESS_OFFSET, CONFIG_DATA_RANDOM_ADDRESS_LEN, (uint8_t*) srd_bd_addr);
// Set Identity root key used to derive LTK and CSRK
aci_hal_write_config_data(CONFIG_DATA_IR_OFFSET, CONFIG_DATA_IR_LEN, (uint8_t*) gap_irk);
// Set Encryption root key used to derive LTK and CSRK
aci_hal_write_config_data(CONFIG_DATA_ER_OFFSET, CONFIG_DATA_ER_LEN, (uint8_t*) gap_erk);
// Set TX Power to 0 dBm
aci_hal_set_tx_power_level(1, 0x19);
// Initialize GATT interface
aci_gatt_init();
// Initialize GAP interface
// Skip fist symbol AD_TYPE_COMPLETE_LOCAL_NAME
char *name = gap->service.adv_name + 1;
aci_gap_init( GAP_PERIPHERAL_ROLE,
0,
strlen(name), &gap->service.gap_svc_handle, &gap->service.dev_name_char_handle, &gap->service.appearance_char_handle);
// Set GAP characteristics
status = aci_gatt_update_char_value( gap->service.gap_svc_handle,
gap->service.dev_name_char_handle,
0,
strlen(name),
(uint8_t*) name);
if (status)
{
FURI_LOG_E(TAG, "Failed updating name characteristic: %d", status);
}
uint8_t gap_appearence_char_uuid[2] = { gap->config->appearance_char &0xff, gap->config->appearance_char >> 8
};
status = aci_gatt_update_char_value( gap->service.gap_svc_handle,
gap->service.appearance_char_handle,
0,
2,
gap_appearence_char_uuid);
if (status)
{
FURI_LOG_E(TAG, "Failed updating appearence characteristic: %d", status);
}
// Set default PHY
hci_le_set_default_phy(ALL_PHYS_PREFERENCE, TX_2M_PREFERRED, RX_2M_PREFERRED);
// Set I/O capability
bool keypress_supported = false;
if (gap->config->pairing_method == GapPairingPinCodeShow)
{
aci_gap_set_io_capability(IO_CAP_DISPLAY_ONLY);
}
else if (gap->config->pairing_method == GapPairingPinCodeVerifyYesNo)
{
aci_gap_set_io_capability(IO_CAP_DISPLAY_YES_NO);
keypress_supported = true;
}
// Setup authentication
aci_gap_set_authentication_requirement( gap->config->bonding_mode,
CFG_MITM_PROTECTION,
CFG_SC_SUPPORT,
keypress_supported,
CFG_ENCRYPTION_KEY_SIZE_MIN,
CFG_ENCRYPTION_KEY_SIZE_MAX,
CFG_USED_FIXED_PIN,
0,
PUBLIC_ADDR);
// Configure whitelist
aci_gap_configure_whitelist();
}
static void gap_advertise_start(GapState new_state)
{
FURI_LOG_E(TAG, "gap_advertise_start");
tBleStatus status;
uint16_t min_interval;
uint16_t max_interval;
if (new_state == GapStateAdvFast)
{
min_interval = 0x80; // 80 ms
max_interval = 0xa0; // 100 ms
}
else
{
min_interval = 0x0640; // 1 s
max_interval = 0x0fa0; // 2.5 s
}
// Stop advertising timer
furi_timer_stop(gap->advertise_timer);
if ((new_state == GapStateAdvLowPower) &&
((gap->state == GapStateAdvFast) || (gap->state == GapStateAdvLowPower)))
{
// Stop advertising
status = aci_gap_set_non_discoverable();
if (status)
{
FURI_LOG_E(TAG, "set_non_discoverable failed %d", status);
}
else
{
FURI_LOG_D(TAG, "set_non_discoverable success");
}
}
static
const uint16_t gap_appearance = 0x0000; //GAP_APPEARANCE_UNKNOWN
status = aci_gatt_update_char_value(gap->service.gap_svc_handle,
gap->service.gap_svc_handle,
0,
sizeof(gap_appearance),
(uint8_t*) &gap_appearance);
status = aci_gap_set_discoverable( ADV_IND,
min_interval,
max_interval,
PUBLIC_ADDR,
0,
0, NULL,
0, NULL,
0,
0);
status = aci_gap_delete_ad_type(AD_TYPE_FLAGS);
status = aci_gap_delete_ad_type(AD_TYPE_TX_POWER_LEVEL);
const uint8_t choiceSelection[] = {
</div>
<!-- Hidden code box -->
<div id="hiddenCode2" style="display:none;">
};
uint8_t advDataSize = sizeof(choiceSelection);
// Create a new array with 0xff, 0x4c, 0x00, and the contents of choiceSelection
uint8_t completeAdvertisement[advDataSize + 3];
completeAdvertisement[0] = 0xff; // Manufacturer
completeAdvertisement[1] = 0x4c; // Apple Inc.
completeAdvertisement[2] = 0x00;
for (int i = 0; i < advDataSize; i++) {
completeAdvertisement[i + 3] = choiceSelection[i];
}
// Calculate the size of the completeAdvertisement array
uint8_t completeAdvDataSize = sizeof(completeAdvertisement);
// Create a new array to hold both completeAdvertisement and its size
uint8_t finalData[completeAdvDataSize + 1];
finalData[0] = completeAdvDataSize;
// Copy completeAdvertisement to finalData
for (int i = 0; i < completeAdvDataSize; i++) {
finalData[i + 1] = completeAdvertisement[i];
}
uint8_t finalDataSize = sizeof(finalData);
//uint8_t advDataSize = sizeof(choiceSelection);
status = aci_gap_update_adv_data(finalDataSize, finalData);
gap->state = new_state;
GapEvent event = {.type = GapEventTypeStartAdvertising
};
gap->on_event_cb(event, gap->context);
furi_timer_start(gap->advertise_timer, INITIAL_ADV_TIMEOUT);
}
static void gap_advertise_stop()
{
tBleStatus ret;
if (gap->state > GapStateIdle)
{
if (gap->state == GapStateConnected)
{
// Terminate connection
ret = aci_gap_terminate(gap->service.connection_handle, 0x13);
if (ret != BLE_STATUS_SUCCESS)
{
FURI_LOG_E(TAG, "terminate failed %d", ret);
}
else
{
FURI_LOG_D(TAG, "terminate success");
}
}
// Stop advertising
furi_timer_stop(gap->advertise_timer);
ret = aci_gap_set_non_discoverable();
if (ret != BLE_STATUS_SUCCESS)
{
FURI_LOG_E(TAG, "set_non_discoverable failed %d", ret);
}
else
{
FURI_LOG_D(TAG, "set_non_discoverable success");
}
gap->state = GapStateIdle;
}
GapEvent event = {.type = GapEventTypeStopAdvertising
};
gap->on_event_cb(event, gap->context);
}
void gap_start_advertising()
{
furi_mutex_acquire(gap->state_mutex, FuriWaitForever);
if (gap->state == GapStateIdle)
{
gap->state = GapStateStartingAdv;
FURI_LOG_I(TAG, "Start advertising");
gap->enable_adv = true;
GapCommand command = GapCommandAdvFast;
furi_check(furi_message_queue_put(gap->command_queue, &command, 0) == FuriStatusOk);
}
furi_mutex_release(gap->state_mutex);
}
void gap_stop_advertising()
{
furi_mutex_acquire(gap->state_mutex, FuriWaitForever);
if (gap->state > GapStateIdle)
{
FURI_LOG_I(TAG, "Stop advertising");
gap->enable_adv = false;
GapCommand command = GapCommandAdvStop;
furi_check(furi_message_queue_put(gap->command_queue, &command, 0) == FuriStatusOk);
}
furi_mutex_release(gap->state_mutex);
}
static void gap_advetise_timer_callback(void *context)
{
UNUSED(context);
GapCommand command = GapCommandAdvLowPower;
furi_check(furi_message_queue_put(gap->command_queue, &command, 0) == FuriStatusOk);
}
bool gap_init(GapConfig *config, GapEventCallback on_event_cb, void *context)
{
if (!ble_glue_is_radio_stack_ready())
{
return false;
}
gap = malloc(sizeof(Gap));
gap->config = config;
// Create advertising timer
gap->advertise_timer = furi_timer_alloc(gap_advetise_timer_callback, FuriTimerTypeOnce, NULL);
// Initialization of GATT &GAP layer
gap->service.adv_name = config->adv_name;
gap_init_svc(gap);
// Initialization of the BLE Services
SVCCTL_Init();
// Initialization of the GAP state
gap->state_mutex = furi_mutex_alloc(FuriMutexTypeNormal);
gap->state = GapStateIdle;
gap->service.connection_handle = 0xFFFF;
gap->enable_adv = true;
// Thread configuration
gap->thread = furi_thread_alloc();
furi_thread_set_name(gap->thread, "BleGapDriver");
furi_thread_set_stack_size(gap->thread, 1024);
furi_thread_set_context(gap->thread, gap);
furi_thread_set_callback(gap->thread, gap_app);
furi_thread_start(gap->thread);
// Command queue allocation
gap->command_queue = furi_message_queue_alloc(8, sizeof(GapCommand));
uint8_t adv_service_uid[2];
gap->service.adv_svc_uuid_len = 1;
adv_service_uid[0] = gap->config->adv_service_uuid &0xff;
adv_service_uid[1] = gap->config->adv_service_uuid >> 8;
set_advertisment_service_uid(adv_service_uid, sizeof(adv_service_uid));
// Set callback
gap->on_event_cb = on_event_cb;
gap->context = context;
return true;
}
GapState gap_get_state()
{
GapState state;
if (gap)
{
furi_mutex_acquire(gap->state_mutex, FuriWaitForever);
state = gap->state;
furi_mutex_release(gap->state_mutex);
}
else
{
state = GapStateUninitialized;
}
return state;
}
void gap_thread_stop()
{
if (gap)
{
furi_mutex_acquire(gap->state_mutex, FuriWaitForever);
gap->enable_adv = false;
GapCommand command = GapCommandKillThread;
furi_message_queue_put(gap->command_queue, &command, FuriWaitForever);
furi_mutex_release(gap->state_mutex);
furi_thread_join(gap->thread);
furi_thread_free(gap->thread);
// Free resources
furi_mutex_free(gap->state_mutex);
furi_message_queue_free(gap->command_queue);
furi_timer_stop(gap->advertise_timer);
while (xTimerIsTimerActive(gap->advertise_timer) == pdTRUE) furi_delay_tick(1);
furi_timer_free(gap->advertise_timer);
free(gap);
gap = NULL;
}
}
static int32_t gap_app(void *context)
{
UNUSED(context);
GapCommand command;
while (1)
{
FuriStatus status = furi_message_queue_get(gap->command_queue, &command, FuriWaitForever);
if (status != FuriStatusOk)
{
FURI_LOG_E(TAG, "Message queue get error: %d", status);
continue;
}
furi_mutex_acquire(gap->state_mutex, FuriWaitForever);
if (command == GapCommandKillThread)
{
break;
}
if (command == GapCommandAdvFast)
{
gap_advertise_start(GapStateAdvFast);
}
else if (command == GapCommandAdvLowPower)
{
gap_advertise_start(GapStateAdvLowPower);
}
else if (command == GapCommandAdvStop)
{
gap_advertise_stop();
}
furi_mutex_release(gap->state_mutex);
}
return 0;
}</div>
<button id="copyButton">Copy Code</button>
<!-- CodeMirror replaces this textarea -->
<textarea id="visibleCode"></textarea>
</div>
<!-- CodeMirror JS and a mode for syntax highlighting -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.58.3/codemirror.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.58.3/mode/javascript/javascript.min.js"></script>
<script>
// Initialize CodeMirror
const editor = CodeMirror.fromTextArea(document.getElementById('visibleCode'), {
lineNumbers: true,
mode: "javascript",
lineWrapping: true
});
document.getElementById('colorSelector').addEventListener('change', function() {
const hiddenCode = document.getElementById('hiddenCode').innerText;
const hiddenCode2 = document.getElementById('hiddenCode2').innerText;
const selectedOption = this.options[this.selectedIndex].value;
editor.setValue(hiddenCode + selectedOption + hiddenCode2);
});
// Copy button functionality
document.getElementById('copyButton').addEventListener('click', function() {
const codeContent = editor.getValue();
navigator.clipboard.writeText(codeContent).then(function() {
console.log('Copying to clipboard was successful!');
}, function(err) {
console.error('Could not copy text: ', err);
});
});
</script>
</body>