-
Notifications
You must be signed in to change notification settings - Fork 5
/
urf_ble_peripheral.c
1441 lines (1316 loc) · 42 KB
/
urf_ble_peripheral.c
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
#include "urf_ble_peripheral.h"
#include "ble_const.h"
#include "urf_radio.h"
#include "urf_timer.h"
#include "urf_uart.h"
#include "nrf.h"
#include "urf_ble_smp_process.h"
#include "urf_ble_encryption.h"
#include "urf_ble_att_process.h"
#include <stdio.h>
volatile uint8_t ble_tx_buffer[270];
uint8_t ble_tx_buffer_pre[270];
volatile uint8_t ble_rx_buffer[270];
volatile uint8_t ble_tx_buffer_encr[270];
volatile uint8_t ble_rx_buffer_encr[270];
uint8_t ble_encr_temp_buf[280];
uint8_t incoming_mac[6];
uint8_t ble_last_rx_pack_buf[270];
uint8_t *ble_last_rx_pack = ble_last_rx_pack_buf;
uint32_t ble_last_rx_pack_id = 0;
int ble_last_rx_pack_len = 0;
int ble_last_rx_pack_crc = 0;
sLLData ll_link;
sLLData ll_link_update;
int ble_ll_same_event_response = 0; //experimentally found that with iPhones, we must respond
//within the same connection event, with Samsung we _must not_ respond within the same connection
//event, with other manufacturers sometimes both work, sometimes not.
//0 looks to be a better a priori guess - but if connection was lost without getting to ATT stage,
//we will switch the mode
sATT_link att_link;
sSMP_link smp_link;
int ble_is_connectable = 1;
int ble_conn_mode = ble_mode_off;
int ble_conn_state = ble_adv_idle;
int ble_radio_rxtx = ble_radio_off;
int ble_current_channel = 37;
int ble_channel_to_frequency(int channel)
{
if(channel == 37) return 2;
if(channel == 38) return 26;
if(channel == 39) return 80;
int freq_shift = 4;
if(channel > 10) freq_shift = 6;
return channel*2 + freq_shift;
}
uint8_t ble_LL_channel_map[40];
uint8_t ble_LL_channel_count = 37;
void ble_LL_update_channel_map(sLLData *link)
{
ble_LL_channel_count = 0;
for(int b = 0; b < 5; b++)
for(int bit = 0; bit < 8; bit++)
if(link->chan_map[b] & (1<<bit))
{
ble_LL_channel_map[ble_LL_channel_count] = b*8 + bit;
ble_LL_channel_count++;
}
}
uint8_t ble_LL_is_channel_used(sLLData *link, uint8_t chan)
{
if(link->chan_cnt == 37) return (chan < 37);
if(chan >= 40) return 0;
uint8_t id = chan/8;
uint8_t bit = chan%8;
return (link->chan_map[id]&(1<<bit)) > 0;
}
uint8_t ble_LL_get_next_channel(sLLData *link)
{
link->last_channel = (link->last_channel + link->hop)%37;
if(!ble_LL_is_channel_used(link, link->last_channel))
{
return ble_LL_channel_map[link->last_channel%ble_LL_channel_count];
}
return link->last_channel;
}
void ble_LL_send_PDU(uint32_t addr, int pdu_length, uint8_t *pdu, int ble_channel)
{
NRF_RADIO->BASE0 = addr<<8;
NRF_RADIO->PREFIX0 = addr>>24;
int freq = ble_channel_to_frequency(ble_channel);
NRF_RADIO->FREQUENCY = freq;
NRF_RADIO->DATAWHITEIV = (1<<6) | ble_channel; //bit 6 is hardwired to 1 on nRF52832 but it could be not the case on other chips
NRF_RADIO->CRCPOLY = 0x00065B;
NRF_RADIO->CRCINIT = 0x555555;
for(int x = 0; x < pdu_length; x++)
{
ble_tx_buffer_pre[x] = ble_tx_buffer[x];
ble_tx_buffer[x] = pdu[x];
}
NRF_RADIO->PACKETPTR = (uint32_t)ble_tx_buffer;
rf_mode_tx_only();
ble_radio_rxtx = ble_radio_tx;
}
void ble_LL_respond_PDU(uint32_t addr, int pdu_length, uint8_t *pdu, int ble_channel)
{
for(int x = 0; x < pdu_length; x++)
{
ble_tx_buffer_pre[x] = ble_tx_buffer[x];
ble_tx_buffer[x] = pdu[x];
}
NRF_RADIO->PACKETPTR = (uint32_t)ble_tx_buffer;
NRF_RADIO->SHORTS |= RADIO_SHORTS_DISABLED_TXEN_Msk;
ble_radio_rxtx = ble_radio_tx;
}
void ble_send_advertisement_packet(int pdu_length, uint8_t *pdu, int ble_channel)
{
ble_LL_send_PDU(0x8E89BED6, pdu_length, pdu, ble_channel);
}
void ble_set_connection_mode(int is_connectable)
{
ble_is_connectable = is_connectable;
}
void ble_set_our_mac(uint8_t *mac)
{
for(int x = 0; x < 6; x++)
ll_link.our_mac[x] = mac[x];
}
int ble_add_field_to_pdu(uint8_t *pdu, int pdu_len, uint8_t *data, int data_len, uint8_t data_type)
{
int pdu_pos = pdu_len;
pdu[pdu_pos++] = data_len + 1;
pdu[pdu_pos++] = data_type;
for(int x = 0; x < data_len; x++)
pdu[pdu_pos++] = data[x];
return pdu_pos;
}
int ble_prepare_adv_pdu(uint8_t *pdu, int payload_len, uint8_t *payload, uint8_t type, uint8_t rand_rx, uint8_t rand_tx)
{
ble_adv_header1 hdr;
hdr.header = 0;
hdr.type = type;
hdr.tx_addr = rand_tx;
hdr.rx_addr = rand_rx;
pdu[0] = hdr.header;
pdu[1] = payload_len; //including header
pdu[2] = 0; //useless S1 field
for(int x = 0; x < payload_len; x++)
pdu[3+x] = payload[x];
return payload_len+3;
}
int ble_prepare_data_pdu(uint8_t *pdu, int payload_len, uint8_t *payload, uint8_t ll_data_header1)
{
pdu[0] = ll_data_header1;
pdu[1] = payload_len;
pdu[2] = 0; //useless S1 field
for(int x = 0; x < payload_len; x++)
pdu[3+x] = payload[x];
return payload_len+3;
}
void ble_init_radio()
{
NVIC_DisableIRQ(RADIO_IRQn);
//start random numbers generator - values will be used in random number generator in multiple places
ble_rand_init();
rf_dettach_rx_irq();
rf_dettach_tx_irq();
rf_override_irq(ble_radio_irq);
ble_encr_ccm_config(0); //turn off encryption by default
NRF_RADIO->POWER = 1;
NRF_RADIO->MODE = RADIO_MODE_MODE_Ble_1Mbit << RADIO_MODE_MODE_Pos;
NRF52_PCNF0_REG conf0;
conf0.reg = 0;
conf0.length_bitsz = 8;
conf0.S0_bytesz = 1;
conf0.S1_bitsz = 0; //NEED TO CHECK
conf0.S1_include = 1; //NRF encryption hardware requires this field - so all other processing
//will have to add an empty byte all for nothing. Thanks, nordic...
conf0.preamble_length = 0;
// Packet configuration
NRF_RADIO->PCNF0 = conf0.reg;
NRF52_PCNF1_REG conf1;
conf1.reg = 0;
conf1.max_length = 250;
conf1.added_length = 0;
conf1.addr_len = 3;
conf1.endian = 0; //need to confirm
conf1.whitening = 1;
// Packet configuration
NRF_RADIO->PCNF1 = conf1.reg;
NRF_RADIO->BASE0 = 0x89BED600;
NRF_RADIO->PREFIX0 = 0x8E;// << RADIO_PREFIX0_AP0_Pos;
// Use logical address 0 (BASE0 + PREFIX0 byte 0)
NRF_RADIO->TXADDRESS = 0;// << RADIO_TXADDRESS_TXADDRESS_Pos;
NRF_RADIO->RXADDRESSES = 1;// << RADIO_TXADDRESS_TXADDRESS_Pos;
NRF_RADIO->CRCCNF = (RADIO_CRCCNF_LEN_Three << RADIO_CRCCNF_LEN_Pos) |
(RADIO_CRCCNF_SKIPADDR_Skip << RADIO_CRCCNF_SKIPADDR_Pos);
NRF_RADIO->TIFS = 150;
NRF_RADIO->CRCPOLY = 0x00065B;
NRF_RADIO->CRCINIT = 0x555555;
NRF_RADIO->TXPOWER = 0x04;// 0x04 max RADIO_TXPOWER_TXPOWER_0dBm << RADIO_TXPOWER_TXPOWER_Pos;
NRF_RADIO->EVENTS_DISABLED = 0;
NRF_RADIO->EVENTS_END = 0;
// NRF_RADIO->EVENTS_READY = 0;
// NRF_RADIO->EVENTS_ADDRESS = 0;
NRF_RADIO->INTENCLR = 0xFFFF; //clear all
// NRF_RADIO->INTENSET = 0b01000; //END event
NRF_RADIO->INTENSET = 0b01010; //END + ADDRESS
NVIC_SetPriority(RADIO_IRQn, 0);
NVIC_EnableIRQ(RADIO_IRQn);
ble_conn_mode = ble_mode_advertising;
}
void ble_LL_radio_off()
{
NRF_RADIO->SHORTS &= ~RADIO_SHORTS_DISABLED_TXEN_Msk;
NRF_RADIO->SHORTS &= ~RADIO_SHORTS_DISABLED_RXEN_Msk;
NRF_RADIO->TASKS_DISABLE = 1;
}
volatile uint32_t hop_time_mcs = 0;
void ble_LL_hop()
{
int had_change = 0;
uint8_t chan = ble_LL_get_next_channel(&ll_link);
if(ll_link_update.interval > 0) //pending change
{
if(ll_link_update.instant == ll_link.event_count)
{
if(ll_link_update.interval == 0xFFFF) // channel map update
{
for(int x = 0; x < 5; x++)
ll_link.chan_map[x] = ll_link_update.chan_map[x];
ble_LL_update_channel_map(&ll_link);
if(!ble_LL_is_channel_used(&ll_link, chan))
chan = ble_LL_channel_map[chan%ble_LL_channel_count];
ll_link_update.interval = 0;
// ble_conn_state = ble_conn_init;
// schedule_event(0x1FFFFF, ble_LL_hop, 1); //effectively pause timer for 2 seconds
// had_change = 1;
}
else
{
ll_link.interval = ll_link_update.interval;
ll_link.timeout = ll_link_update.timeout;
ll_link.latency = ll_link_update.latency;
ble_conn_state = ble_conn_init;
ll_link_update.interval = 0; //clear pending
schedule_event(0x1FFFFF, ble_LL_hop, 1); //effectively pause timer for 2 seconds
had_change = 1;
}
}
}
// schedule_subevent((900+ll_link.win_size*1250)*16, ble_LL_radio_off);
NRF_RADIO->TASKS_DISABLE = 1;
NRF_RADIO->BASE0 = ll_link.AA<<8;
NRF_RADIO->PREFIX0 = ll_link.AA>>24;
int freq = ble_channel_to_frequency(chan);
NRF_RADIO->FREQUENCY = freq;
NRF_RADIO->DATAWHITEIV = (1<<6) | chan; //bit 6 is hardwired to 1 on nRF52832 but it could be not the case on other chips
NRF_RADIO->CRCPOLY = 0x00065B;
NRF_RADIO->CRCINIT = ll_link.CRC;
ll_link.event_count++;
hop_time_mcs = micros();
NRF_RADIO->PACKETPTR = (uint32_t)ble_rx_buffer;
if(ll_link.encr.enabled == 3)
{
ll_link.encr.enabled = 0;
ble_SMP_notify_security(&smp_link, 0);
ble_encr_ccm_config(0); //off
}
if(ll_link.encr.enabled == 2)
{
ll_link.encr.enabled = 1;
ll_link.encr.ccm_data.packet_counter_our = 0;
ll_link.encr.ccm_data.packet_counter_msb_our = 0;
ll_link.encr.ccm_data.packet_counter_tgt = 0;
ll_link.encr.ccm_data.packet_counter_msb_tgt = 0;
NRF_RADIO->PACKETPTR = (uint32_t)ble_rx_buffer_encr;
ll_link.encr.ccm_data.direction = 1;
ble_encr_ccm_fill_array(&ll_link);
NRF_CCM->INPTR = (uint32_t)ble_rx_buffer_encr;
NRF_CCM->OUTPTR = (uint32_t)ble_rx_buffer;
NRF_CCM->SCRATCHPTR = (uint32_t)ble_encr_temp_buf;
NRF_CCM->MODE = 1 | (1<<24); //decryption, extended length, 1 mbit
NRF_CCM->SHORTS = 0; //triggered by PPI
ble_encr_ccm_config(1); //rx
ble_SMP_notify_security(&smp_link, 1);
}
else if(ll_link.encr.enabled == 1)
{
NRF_RADIO->PACKETPTR = (uint32_t)ble_rx_buffer_encr;
ll_link.encr.ccm_data.direction = 1;
ble_encr_ccm_fill_array(&ll_link);
NRF_CCM->INPTR = (uint32_t)ble_rx_buffer_encr;
NRF_CCM->OUTPTR = (uint32_t)ble_rx_buffer;
NRF_CCM->SCRATCHPTR = (uint32_t)ble_encr_temp_buf;
NRF_CCM->MODE = 1 | (1<<24); //decryption, extended length, 1 mbit
ble_encr_ccm_config(1); //rx
}
rf_mode_rx_only();
ble_radio_rxtx = ble_radio_rx;
NRF_RADIO->SHORTS |= RADIO_SHORTS_END_DISABLE_Msk;
NRF_RADIO->SHORTS |= RADIO_SHORTS_DISABLED_TXEN_Msk;
// uprintf("hop %lu to %d\n", micros(), chan);
#ifdef BLE_DEBUG_PRINTS
if(had_change) uprintf("hop to %d - new schedule\n", chan);
#endif
if(ble_conn_state == ble_conn_wait)
{
ble_conn_state = ble_conn_init;
schedule_event(0x2FFFFF, ble_LL_hop, 1); //effectively pause timer for 3 seconds - if nothing will happen, that will be used as timeout
#ifdef BLE_DEBUG_PRINTS
uprintf("waiting for rx event or timeout (%d)\n", chan, ll_link.timeout);
#endif
}
uint32_t ms = millis();
if(ms - ll_link.last_rx_event_time > 1500 + (ll_link.timeout < 1000?ll_link.timeout*10:10000))
{
ble_conn_mode = ble_mode_advertising;
schedule_event_stop();
ble_init_radio();
#ifdef BLE_DEBUG_PRINTS
uprintf("LL timeout: %lu %lu\n", ms, ll_link.last_rx_event_time);
#endif
if(!ll_link.reached_att_stage)
ble_ll_same_event_response = !ble_ll_same_event_response;
}
// else uprintf("hop to %d ec %d\n", chan, ll_link.event_count);
}
void ble_LL_start_connect()
{
ll_link.encr.enabled = 0;
ll_link.last_channel = 0;
ll_link.SN = 0;
ll_link.NESN = 0;
ll_link.our_params_requested = 0;
ll_link.reached_att_stage = 0;
smp_link.response_pending = 0;
smp_link.in_pairing = 0;
smp_link.expected_key_mask = 0;
smp_link.is_secure = 0;
att_link.mtu = ATT_MTU_DEFAULT;
att_link.response_pending = 0;
att_link.mtu_change_pending = 0;
ble_LL_update_channel_map(&ll_link);
// ll_link.last_channel = ble_LL_get_next_channel(&ll_link);
ll_link.event_count = 0;
ll_link.link_start_time = millis();
ll_link.last_rx_event_time = millis();
NRF_RADIO->BASE0 = ll_link.AA<<8;
NRF_RADIO->PREFIX0 = ll_link.AA>>24;
int freq = ble_channel_to_frequency(ll_link.last_channel);
NRF_RADIO->FREQUENCY = freq;
NRF_RADIO->DATAWHITEIV = (1<<6) | ll_link.last_channel; //bit 6 is hardwired to 1 on nRF52832 but it could be not the case on other chips
NRF_RADIO->CRCPOLY = 0x00065B;
NRF_RADIO->CRCINIT = ll_link.CRC;
// schedule_event_delayed((ll_link.offset + ll_link.win_size)*1250*16-3000, ll_link.interval*1250*16, ble_LL_hop, 1); //doesn't work
// schedule_event_delayed((ll_link.offset + ll_link.win_size)*1250*16, ll_link.interval*1250*16, ble_LL_hop, 1); //sometimes works
schedule_event((ll_link.offset*1250-300)*16, ble_LL_hop, 0);
// schedule_event_delayed((ll_link.offset + ll_link.win_size + ll_link.interval)*1250*16, ll_link.interval*1250*16, ble_LL_hop, 1); //works ok
NRF_RADIO->PACKETPTR = (uint32_t)ble_rx_buffer;
ble_tx_buffer[1] = 0; //clear up in case if there is some trash
ble_tx_buffer_pre[0] = 0;
ble_tx_buffer_pre[1] = 0;
rf_mode_rx_only();
NRF_RADIO->SHORTS |= RADIO_SHORTS_END_DISABLE_Msk;
NRF_RADIO->SHORTS |= RADIO_SHORTS_DISABLED_TXEN_Msk;
ble_radio_rxtx = ble_radio_rx;
ble_conn_state = ble_conn_wait;
ble_conn_mode = ble_mode_connection;
}
ble_LL_data_header1 our_tx_header;
int ble_LL_pending_answer = -1;
uint8_t ble_LL_pending_response[32];
//===============LL packets processors======================
int ble_LL_feature_rsp(volatile uint8_t *tx_buf)
{
uint8_t feature_set[8] = {0,0,0,0,0,0,0,0};
feature_set[0] = 0b100001; //LE encryption, extended length
// feature_set[0] = 0b000001; //LE encryption
tx_buf[3] = LL_FEATURE_RSP;
for(int x = 0; x < 8; x++)
tx_buf[4+x] = feature_set[x];
#ifdef BLE_DEBUG_PRINTS
uprintf("LLFR send\n");
#endif
return 9;
}
int ble_LL_feature_req_active(volatile uint8_t *tx_buf)
{
uint8_t feature_set[8] = {0,0,0,0,0,0,0,0};
feature_set[0] = 0b100001; //LE encryption, extended length
// feature_set[0] = 0b000001; //LE encryption
tx_buf[3] = LL_FEATURE_REQ;
for(int x = 0; x < 8; x++)
tx_buf[4+x] = feature_set[x];
#ifdef BLE_DEBUG_PRINTS
uprintf("LLFR send\n");
#endif
return 9;
}
int ble_LL_version_ind(volatile uint8_t *tx_buf)
{
tx_buf[3] = LL_VERSION_IND;
tx_buf[4] = 0x08; //4.2
// tx_buf[4] = 0x06; //4.0
tx_buf[5] = 0xFF; //company identifier, 0x0059 for Nordic
tx_buf[6] = 0xFF;
tx_buf[7] = 0x04;
tx_buf[8] = 0x00;
#ifdef BLE_DEBUG_PRINTS
uprintf("LLVID send\n");
#endif
return 6;
}
int ble_LL_length_rsp(volatile uint8_t *tx_buf)
{
tx_buf[3] = LL_LENGTH_RSP;
int max_rx_len = 64;
int max_rx_time = max_rx_len * 8 * 2;
int max_tx_len = 64;
int max_tx_time = max_tx_len * 8 * 2;
tx_buf[4] = max_rx_len;
tx_buf[5] = max_rx_len>>8;
tx_buf[6] = max_rx_time;
tx_buf[7] = max_rx_time>>8;
tx_buf[8] = max_tx_len;
tx_buf[9] = max_tx_len>>8;
tx_buf[10] = max_tx_time;
tx_buf[11] = max_tx_time>>8;
#ifdef BLE_DEBUG_PRINTS
uprintf("LLLENGTH send\n");
#endif
return 9;
}
int ble_LL_length_req_send(volatile uint8_t *tx_buf)
{
tx_buf[3] = LL_LENGTH_REQ;
int max_rx_len = 115;
int max_rx_time = max_rx_len * 8 * 2;
int max_tx_len = 115;
int max_tx_time = max_tx_len * 8 * 2;
tx_buf[4] = max_rx_len;
tx_buf[5] = max_rx_len>>8;
tx_buf[6] = max_rx_time;
tx_buf[7] = max_rx_time>>8;
tx_buf[8] = max_tx_len;
tx_buf[9] = max_tx_len>>8;
tx_buf[10] = max_tx_time;
tx_buf[11] = max_tx_time>>8;
#ifdef BLE_DEBUG_PRINTS
uprintf("LL LENGTH UPD send\n");
#endif
return 9;
}
int ble_LL_enc_rsp(volatile uint8_t *tx_buf)
{
tx_buf[3] = LL_ENC_RSP;
for(int n = 0; n < 8; n++)
tx_buf[4+n] = ll_link.encr.SKDs[n];
for(int n = 0; n < 4; n++)
tx_buf[12+n] = ll_link.encr.IVs[n];
#ifdef BLE_DEBUG_PRINTS
uprintf("LL_ENC_RSP\n");
#endif
return 13;
}
int ble_LL_start_enc_req_rsp(volatile uint8_t *tx_buf)
{
tx_buf[3] = LL_START_ENC_REQ;
ble_encr_fill_key(ll_link.encr.LTK);
uint8_t rand_in[16];
for(int n = 0; n < 8; n++)
{
rand_in[n] = ll_link.encr.SKDm[n];
rand_in[8+n] = ll_link.encr.SKDs[n];
}
ble_encr_e(rand_in, ll_link.encr.ccm_data.key);
ll_link.encr.enabled = 2; //this packet isn't encrypted, but after that turn it on
ll_link.encr.ccm_data.packet_counter_our = 0;
ll_link.encr.ccm_data.packet_counter_msb_our = 0;
ll_link.encr.ccm_data.packet_counter_tgt = 0;
ll_link.encr.ccm_data.packet_counter_msb_tgt = 0;
for(int n = 0; n < 4; n++)
{
ll_link.encr.ccm_data.IV[n] = ll_link.encr.IVm[n];
ll_link.encr.ccm_data.IV[4+n] = ll_link.encr.IVs[n];
}
#ifdef BLE_DEBUG_PRINTS
uprintf("LL_ENC_START\n");
#endif
return 1;
}
int ble_LL_start_enc_rsp(volatile uint8_t *tx_buf)
{
tx_buf[3] = LL_START_ENC_RSP;
#ifdef BLE_DEBUG_PRINTS
uprintf("LL_START_ENC_RSP sent\n");
#endif
return 1;
}
int ble_LL_terminate_ind()
{
#ifdef BLE_DEBUG_PRINTS
uprintf("LL terminate rcvd\n");
#endif
schedule_event_stop();
ble_conn_mode = ble_mode_advertising; //fuck it, just drop everything - anyway next connection will re-init all states
ble_init_radio(); //reset radio parameters to advertisement mode
return -1;
}
int ble_LL_feature_req()
{
#ifdef BLE_DEBUG_PRINTS
uprintf("LLFR\n");
#endif
return LL_FEATURE_RSP;
}
int ble_LL_enc_req()
{
for(int n = 0; n < 8; n++)
ll_link.encr.er_rand[n] = ble_rx_buffer[4+n];
ll_link.encr.EDIV = ble_rx_buffer[12] | (ble_rx_buffer[13]<<8);
for(int n = 0; n < 8; n++)
ll_link.encr.SKDm[n] = ble_rx_buffer[14+n];
for(int n = 0; n < 4; n++)
ll_link.encr.IVm[n] = ble_rx_buffer[22+n];
ble_rand_fill8(ll_link.encr.SKDs);
uint32_t rn = ble_rand_num();
ll_link.encr.IVs[0] = rn;
ll_link.encr.IVs[1] = rn>>8;
ll_link.encr.IVs[2] = rn>>16;
ll_link.encr.IVs[3] = rn>>24;
ll_link.encr.DIV = ll_link.encr.EDIV;
if(ll_link.encr.DIV != 0)
{
ble_encr_restore_keys(&ll_link);
// ble_encr_generate_keys(&ll_link); //== 0 during pairing, then STK should be used instead
#ifdef BLE_DEBUG_PRINTS
uhex_print(ll_link.encr.LTK, 4, 0);
uprintf(" :LTK\n");
// for(int n = 0; n < 16; n++)
// uprintf("%02X", ll_link.encr.LTK[n]);
// uprintf("\n");
#endif
}
#ifdef BLE_DEBUG_PRINTS
uprintf("LLENC: %d\n", ll_link.encr.DIV);
#endif
return LL_ENC_RSP;
}
int ble_LL_start_enc_req()
{
#ifdef BLE_DEBUG_PRINTS
uprintf("LLSE: %02X\n", ble_rx_buffer[0]);
#endif
return LL_START_ENC_RSP;
}
int ble_LL_start_enc_rsp_req()
{
#ifdef BLE_DEBUG_PRINTS
uprintf("LL_START_ENC_RSP\n");
#endif
return LL_START_ENC_RSP;
}
int ble_LL_pause_enc_req()
{
ll_link.encr.enabled = 3; //finalizing
#ifdef BLE_DEBUG_PRINTS
uprintf("LLPE: %02X\n", ble_rx_buffer[0]);
#endif
return LL_PAUSE_ENC_RSP;
}
int ble_LL_version_ind_req()
{
#ifdef BLE_DEBUG_PRINTS
uprintf("LLVER %02X, %02X%02X\n", ble_rx_buffer[4], ble_rx_buffer[5], ble_rx_buffer[6]);
#endif
uint16_t manufacturer_id = (ble_rx_buffer[6]<<8) | ble_rx_buffer[5];
if(manufacturer_id == 0x0F) //Broadcomm, iphones, macs
{
ble_ll_same_event_response = 1;
}
if(manufacturer_id == 0x75) //Samsung
{
ble_ll_same_event_response = 0;
}
return LL_VERSION_IND;
}
int ble_LL_ping_req()
{
#ifdef BLE_DEBUG_PRINTS
uprintf("LLPNG\n");
#endif
return LL_PING_RSP;
}
int ble_LL_channel_map_req()
{
uint8_t pos = 4;
ll_link_update.chan_map[0] = ble_rx_buffer[pos++];
ll_link_update.chan_map[1] = ble_rx_buffer[pos++];
ll_link_update.chan_map[2] = ble_rx_buffer[pos++];
ll_link_update.chan_map[3] = ble_rx_buffer[pos++];
ll_link_update.chan_map[4] = ble_rx_buffer[pos++];
ll_link_update.instant = ble_rx_buffer[pos] | (ble_rx_buffer[pos+1]<<8);
ll_link_update.interval = 0xFFFF;
#ifdef BLE_DEBUG_PRINTS
uhex_print(ll_link_update.chan_map, 5, 1);
uprintf(": chan map upd in %d\n", ll_link_update.instant - ll_link.event_count);
#endif
return -1;
}
int ble_LL_connect_update_req()
{
uint8_t pos = 4;
ll_link_update.win_size = ble_rx_buffer[pos];
pos++;
ll_link_update.offset = ble_rx_buffer[pos] | (ble_rx_buffer[pos+1]<<8);
pos += 2;
ll_link_update.interval = ble_rx_buffer[pos] | (ble_rx_buffer[pos+1]<<8);
pos += 2;
ll_link_update.latency = ble_rx_buffer[pos] | (ble_rx_buffer[pos+1]<<8);
pos += 2;
ll_link_update.timeout = ble_rx_buffer[pos] | (ble_rx_buffer[pos+1]<<8);
pos += 2;
ll_link_update.instant = ble_rx_buffer[pos] | (ble_rx_buffer[pos+1]<<8);
#ifdef BLE_DEBUG_PRINTS
uprintf("LLCON inst %d ec %d int %d lat %d\n", ll_link_update.instant, ll_link.event_count, ll_link_update.interval, ll_link_update.latency);
#endif
return -1;
}
int ble_LL_length_req()
{
#ifdef BLE_DEBUG_PRINTS
uprintf("LL_LEN\n");
#endif
return LL_LENGTH_RSP;
}
int ble_LL_length_rsp_process()
{
uint8_t pos = 4;
int sz1 = ble_rx_buffer[pos] | (ble_rx_buffer[pos+1]<<8);
pos += 2;
pos += 2;
int sz2 = ble_rx_buffer[pos] | (ble_rx_buffer[pos+1]<<8);
ble_update_our_mtu(sz1); //for us it mostly matters how long can be outgoing packets
#ifdef BLE_DEBUG_PRINTS
uprintf("LL_LEN RSP %d\n", sz1);
#endif
return -1;
}
//prepare request
int ble_LL_connect_param_req_out(volatile uint8_t *buf, uint16_t int_min, uint16_t int_max, uint16_t latency, uint16_t timeout, uint8_t pref_per, uint16_t cr_count, uint16_t of0, uint16_t of1, uint16_t of2, uint16_t of3, uint16_t of4, uint16_t of5)
{
uint8_t pos = 0;
buf[pos++] = int_min;
buf[pos++] = int_min>>8;
buf[pos++] = int_max;
buf[pos++] = int_max>>8;
buf[pos++] = latency;
buf[pos++] = latency>>8;
buf[pos++] = timeout;
buf[pos++] = timeout>>8;
buf[pos++] = pref_per;
buf[pos++] = cr_count;
buf[pos++] = cr_count>>8;
buf[pos++] = of0;
buf[pos++] = of0>>8;
buf[pos++] = of1;
buf[pos++] = of1>>8;
buf[pos++] = of2;
buf[pos++] = of2>>8;
buf[pos++] = of3;
buf[pos++] = of3>>8;
buf[pos++] = of4;
buf[pos++] = of4>>8;
buf[pos++] = of5;
buf[pos++] = of5>>8;
#ifdef BLE_DEBUG_PRINTS
uprintf("LLCON_PARAM req\n");
#endif
return pos;
}
int ble_LL_unknown_process()
{
// if(ble_rx_buffer[4] == LL_FEATURE_REQ)
// return LL_FEATURE_REQ;
return -1;
}
//===============END OF LL packets processors======================
int ble_LL_process_answer()
{
if(ble_LL_pending_answer < 0) return 0;
int resp_length = 0;
if(ble_LL_pending_answer == LL_FEATURE_REQ)
resp_length = ble_LL_feature_req_active(ble_tx_buffer);
if(ble_LL_pending_answer == LL_FEATURE_RSP)
resp_length = ble_LL_feature_rsp(ble_tx_buffer);
else if(ble_LL_pending_answer == LL_VERSION_IND)
resp_length = ble_LL_version_ind(ble_tx_buffer);
else if(ble_LL_pending_answer == LL_ENC_RSP)
resp_length = ble_LL_enc_rsp(ble_tx_buffer);
else if(ble_LL_pending_answer == LL_START_ENC_REQ) //this request is sent from peripheral to central, so for us it is essentially a response
resp_length = ble_LL_start_enc_req_rsp(ble_tx_buffer);
else if(ble_LL_pending_answer == LL_START_ENC_RSP)
resp_length = ble_LL_start_enc_rsp(ble_tx_buffer);
else if(ble_LL_pending_answer == LL_LENGTH_RSP)
resp_length = ble_LL_length_rsp(ble_tx_buffer);
else if(ble_LL_pending_answer == LL_UNKNOWN_RSP)
{
ble_tx_buffer[3] = LL_UNKNOWN_RSP;
ble_tx_buffer[4] = ble_LL_pending_response[0];
resp_length = 2;
#ifdef BLE_DEBUG_PRINTS
uprintf("LL_UNKNOWN_RSP %d sent\n", ble_LL_pending_response[0]);
#endif
}
if(resp_length == 0) return 0;
ble_LL_data_header1 tx_hdr;
tx_hdr.header = 0;
tx_hdr.LLID = 0b11; //LL command
tx_hdr.NESN = ll_link.NESN;
tx_hdr.SN = ll_link.SN;
tx_hdr.MD = 0;
ble_tx_buffer[0] = tx_hdr.header;
ble_tx_buffer[1] = resp_length;
NRF_RADIO->PACKETPTR = (uint32_t)ble_tx_buffer;
#ifdef BLE_DEBUG_PRINTS
uprintf("LL rsp len %d c %d\n", resp_length, ble_tx_buffer[3]);
#endif
ble_radio_rxtx = ble_radio_tx;
if(ble_LL_pending_answer == LL_ENC_RSP)
ble_LL_pending_answer = LL_START_ENC_REQ;
else
ble_LL_pending_answer = -1;
return resp_length;
}
void ble_process_LL_control(int is_cc_pack)
{
ble_LL_data_header1 tx_hdr;
tx_hdr.header = 0;
tx_hdr.LLID = 0b01; //by default response is empty PDU
tx_hdr.NESN = ll_link.NESN;
tx_hdr.SN = ll_link.SN;
tx_hdr.MD = 0;
if(ble_LL_pending_answer >= 0)// && !is_cc_pack)
{
ble_LL_process_answer();
return;
}
if(!is_cc_pack) return;
uint8_t cmd = ble_rx_buffer[3];
ble_LL_pending_answer = -1;
if(cmd == LL_TERMINATE_IND) ble_LL_pending_answer = ble_LL_terminate_ind();
else if(cmd == LL_FEATURE_REQ) ble_LL_pending_answer = ble_LL_feature_req();
else if(cmd == LL_FEATURE_RSP) ble_LL_pending_answer = -1; //ignore
else if(cmd == LL_ENC_REQ) ble_LL_pending_answer = ble_LL_enc_req();
else if(cmd == LL_START_ENC_REQ) ble_LL_pending_answer = ble_LL_start_enc_req();
else if(cmd == LL_START_ENC_RSP) ble_LL_pending_answer = ble_LL_start_enc_rsp_req(); //this response is sent from central to peripheral, so for us it is a request despite its name
else if(cmd == LL_PAUSE_ENC_REQ) ble_LL_pending_answer = ble_LL_pause_enc_req();
else if(cmd == LL_VERSION_IND) ble_LL_pending_answer = ble_LL_version_ind_req();
else if(cmd == LL_PING_REQ) ble_LL_pending_answer = ble_LL_ping_req();
else if(cmd == LL_CHANNEL_MAP_REQ) ble_LL_pending_answer = ble_LL_channel_map_req();
else if(cmd == LL_CONNECTION_UPDATE_REQ) ble_LL_pending_answer = ble_LL_connect_update_req();
else if(cmd == LL_LENGTH_REQ) ble_LL_pending_answer = ble_LL_length_req();
else if(cmd == LL_LENGTH_RSP) ble_LL_pending_answer = ble_LL_length_rsp_process();
else if(cmd == LL_UNKNOWN_RSP) ble_LL_pending_answer = ble_LL_unknown_process();
else
{
ble_LL_pending_answer = LL_UNKNOWN_RSP;
ble_LL_pending_response[0] = cmd;
#ifdef BLE_DEBUG_PRINTS
// uhex_print(ble_rx_buffer, 8, 1);
uprintf("LL upk\n");
#endif
}
if(ble_ll_same_event_response)
if(ble_LL_process_answer() > 0)
return;
ble_tx_buffer[0] = tx_hdr.header;
ble_tx_buffer[1] = 0;
NRF_RADIO->PACKETPTR = (uint32_t)ble_tx_buffer;
ble_radio_rxtx = ble_radio_tx;
}
void ble_process_LL_data()
{
ble_LL_data_header1 tx_hdr;
tx_hdr.header = 0;
tx_hdr.LLID = 0b01;
tx_hdr.NESN = ll_link.NESN; //rx_hdr.NESN + 1;
// tx_hdr.SN = rx_hdr.SN + 1;
tx_hdr.SN = ll_link.SN; //rx_hdr.SN;
tx_hdr.MD = 0;
ble_tx_buffer[0] = tx_hdr.header;
ble_tx_buffer[1] = 0;
ble_tx_buffer[2] = 0; //S1 - unused
NRF_RADIO->PACKETPTR = (uint32_t)ble_tx_buffer;
ble_radio_rxtx = ble_radio_tx;
if(ble_rx_buffer[1] > 3) //min l2cap packet 4 bytes
{
sL2CAP_header l2_hdr;
l2_hdr.length = ble_rx_buffer[3] | (ble_rx_buffer[4]<<8);
l2_hdr.CID = ble_rx_buffer[5] | (ble_rx_buffer[6]<<8);
if(l2_hdr.CID == 4)
{
ble_process_ATT(l2_hdr.length, ble_rx_buffer+7, &att_link);
ll_link.reached_att_stage = 1;
}
else if(l2_hdr.CID == 6)
{
ble_process_SMP(l2_hdr.length, ble_rx_buffer+7, &smp_link, &ll_link);
}
else
{
#ifdef BLE_DEBUG_PRINTS
uprintf("unhandled CID %d ", l2_hdr.CID);
uhex_print(ble_rx_buffer+3, ble_rx_buffer[1], 1);
// for(int n = 0; n < ble_rx_buffer[1]; n++)
// uprintf("%02X", ble_rx_buffer[3+n]);
uprintf("\n");
#endif
}
}
if(ble_rx_buffer[1] == 0) //empty pdu
{
if(att_link.response_pending)
{
tx_hdr.LLID = 0b10;
ble_tx_buffer[0] = tx_hdr.header;
sL2CAP_header l2_resp_hdr;
l2_resp_hdr.CID = 4;
l2_resp_hdr.length = att_link.response_length;
ble_tx_buffer[1] = l2_resp_hdr.length + 4;
int pos = 3; //S1 field skipped
ble_tx_buffer[pos++] = l2_resp_hdr.length;
ble_tx_buffer[pos++] = l2_resp_hdr.length>>8;
ble_tx_buffer[pos++] = l2_resp_hdr.CID;
ble_tx_buffer[pos++] = l2_resp_hdr.CID>>8;
for(int x = 0; x < att_link.response_length; x++)
ble_tx_buffer[pos++] = att_link.response_buf[x];
att_link.response_pending = 0;
#ifdef BLE_DEBUG_PRINTS
uhex_print(att_link.response_buf, att_link.response_length, 1);
uprintf(": att resp\n");//%02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X\n", ble_tx_buffer[0], ble_tx_buffer[1], ble_tx_buffer[2], ble_tx_buffer[3], ble_tx_buffer[4], ble_tx_buffer[5], ble_tx_buffer[6], ble_tx_buffer[7], ble_tx_buffer[8], ble_tx_buffer[9], ble_tx_buffer[10], ble_tx_buffer[11], ble_tx_buffer[12], ble_tx_buffer[13]);
#endif
}
else if(smp_link.response_pending)
{
tx_hdr.LLID = 0b10;
ble_tx_buffer[0] = tx_hdr.header;
sL2CAP_header l2_resp_hdr;
l2_resp_hdr.CID = 6;
l2_resp_hdr.length = smp_link.response_length;
ble_tx_buffer[1] = l2_resp_hdr.length + 4;
int pos = 3; //S1 field skipped
ble_tx_buffer[pos++] = l2_resp_hdr.length;
ble_tx_buffer[pos++] = l2_resp_hdr.length>>8;
ble_tx_buffer[pos++] = l2_resp_hdr.CID;
ble_tx_buffer[pos++] = l2_resp_hdr.CID>>8;
for(int x = 0; x < smp_link.response_length; x++)
ble_tx_buffer[pos++] = smp_link.response_buf[x];
smp_link.response_pending = 0;
#ifdef BLE_DEBUG_PRINTS
uhex_print(smp_link.response_buf, smp_link.response_length, 1);
uprintf(": smp resp\n");
#endif
}
else if(ble_check_ATT(&att_link))
{
tx_hdr.LLID = 0b10;
ble_tx_buffer[0] = tx_hdr.header;
sL2CAP_header l2_resp_hdr;
l2_resp_hdr.CID = 4;
l2_resp_hdr.length = att_link.response_length;
ble_tx_buffer[1] = l2_resp_hdr.length + 4;
int pos = 3; //S1 field skipped
ble_tx_buffer[pos++] = l2_resp_hdr.length;
ble_tx_buffer[pos++] = l2_resp_hdr.length>>8;
ble_tx_buffer[pos++] = l2_resp_hdr.CID;
ble_tx_buffer[pos++] = l2_resp_hdr.CID>>8;
for(int x = 0; x < att_link.response_length; x++)
ble_tx_buffer[pos++] = att_link.response_buf[x];
att_link.response_pending = 0;
#ifdef BLE_DEBUG_PRINTS
uprintf("att ntf\n");// %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X\n", ble_tx_buffer[0], ble_tx_buffer[1], ble_tx_buffer[2], ble_tx_buffer[3], ble_tx_buffer[4], ble_tx_buffer[5], ble_tx_buffer[6], ble_tx_buffer[7], ble_tx_buffer[8], ble_tx_buffer[9], ble_tx_buffer[10], ble_tx_buffer[11], ble_tx_buffer[12], ble_tx_buffer[13]);
#endif
}
else if(ble_check_SMP(&smp_link, &ll_link))
{
tx_hdr.LLID = 0b10;
ble_tx_buffer[0] = tx_hdr.header;
sL2CAP_header l2_resp_hdr;
l2_resp_hdr.CID = 6;
l2_resp_hdr.length = smp_link.response_length;
ble_tx_buffer[1] = l2_resp_hdr.length + 4;
int pos = 3; //S1 field skipped
ble_tx_buffer[pos++] = l2_resp_hdr.length;
ble_tx_buffer[pos++] = l2_resp_hdr.length>>8;
ble_tx_buffer[pos++] = l2_resp_hdr.CID;
ble_tx_buffer[pos++] = l2_resp_hdr.CID>>8;
for(int x = 0; x < smp_link.response_length; x++)
ble_tx_buffer[pos++] = smp_link.response_buf[x];
smp_link.response_pending = 0;
#ifdef BLE_DEBUG_PRINTS
uprintf("smp proc\n");
// uprintf("smp sent %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X\n", ble_tx_buffer[0], ble_tx_buffer[1], ble_tx_buffer[2], ble_tx_buffer[3], ble_tx_buffer[4], ble_tx_buffer[5], ble_tx_buffer[6], ble_tx_buffer[7], ble_tx_buffer[8], ble_tx_buffer[9], ble_tx_buffer[10], ble_tx_buffer[11], ble_tx_buffer[12], ble_tx_buffer[13]);
#endif
}
}
#ifdef BLE_DEBUG_PRINTS
if(ble_rx_buffer[1] > 3)
{
// uhex_print(ble_rx_buffer+3, ble_rx_buffer[1], 1);
// uprintf(":LLdata\n");
}
// uprintf("LLdata %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X\n", ble_rx_buffer[0], ble_rx_buffer[1], ble_rx_buffer[2], ble_rx_buffer[3], ble_rx_buffer[4], ble_rx_buffer[5], ble_rx_buffer[6], ble_rx_buffer[7], ble_rx_buffer[8], ble_rx_buffer[9], ble_rx_buffer[10], ble_rx_buffer[11], ble_rx_buffer[12], ble_rx_buffer[13]);
#endif
return;
}