-
Notifications
You must be signed in to change notification settings - Fork 20
/
logic_bluetooth.c
2090 lines (1909 loc) · 114 KB
/
logic_bluetooth.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
/*
* logic_bluetooth.c
*
* Created: 02/06/2019 18:08:22
* Author: limpkin
*/
#include "platform_defines.h"
#include "logic_bluetooth.h"
#include "conf_serialdrv.h"
#include "driver_timer.h"
#include "device_info.h"
#include "ble_manager.h"
#include "platform_io.h"
#include "logic_sleep.h"
#include "at_ble_api.h"
#include "logic_rng.h"
#include "ble_utils.h"
#include "platform.h"
#include "battery.h"
#include "defines.h"
#include "logic.h"
// Appearance: 0x03C1 (keyboard)
// Dev name perm: disable writing device name
// Device name: Mooltipass Mini BLE
// Minimum connection interval N (Value Time = N *1.25 ms): 9 = 11.25ms
// Maximum connection interval N (Value Time = N *1.25 ms): 24 = 30ms
// Slave preferred Connection latency (number of events): 30 (taken from ios guidelines)
// Slave preferred Link supervision time-out (Value Time = N * 10 ms): 300 (taken from ios guidelines)
at_ble_gap_deviceinfo_t logic_bluetooth_advanced_info = {.appearance=0x03C1, .dev_name_perm=AT_BLE_WRITE_DISABLE, .name.length=20, .name.value="Mooltipass Mini BLE", .slv_params.con_intv_min=9, .slv_params.con_intv_max=24, .slv_params.con_latency=30, .slv_params.superv_to=300};
/* Control point notification structure for HID services*/
hid_control_mode_ntf_t logic_bluetooth_hid_control_point_value[HID_MAX_SERV_INST];
/* Characteristic numbers for the report CCD notifications */
uint8_t logic_bluetooth_report_notif_characteristic_numbers_init_values[BLE_TOTAL_NUMBER_OF_REPORTS];
uint16_t* logic_bluetooth_report_notif_characteristic_numbers[BLE_TOTAL_NUMBER_OF_REPORTS];
uint16_t logic_bluetooth_report_notif_characteristic_numbers_counter = 0;
/* Report notification structure for HID services */
hid_report_ntf_t logic_bluetooth_report_ntf_info[BLE_TOTAL_NUMBER_OF_REPORTS];
/* HID GATT services instances */
hid_gatt_serv_handler_t logic_bluetooth_hid_gatt_instances[HID_MAX_SERV_INST];
/* Notification we're sending */
notif_sending_te logic_bluetooth_notif_being_sent = NONE_NOTIF_SENDING;
/* HID service instances */
hid_serv_t logic_bluetooth_hid_serv_instances[HID_MAX_SERV_INST];
/* Boot notification structure for keyboard service in boot protocol */
hid_boot_ntf_t logic_bluetooth_boot_keyboard_report_ntf_info;
/* Protocol mode notification structure for keyboard service */
hid_proto_mode_ntf_t logic_bluetooth_protocol_mode_value;
/* Battery service handler */
bat_gatt_service_handler_t logic_bluetooth_bas_service_handler;
/* Notification sent flag for battery char changed */
BOOL logic_bluetooth_battery_notification_flag = TRUE;
/* Bluetooth connection handle */
at_ble_handle_t logic_bluetooth_ble_connection_handle;
/* Battery level advertised */
uint8_t logic_bluetooth_pending_battery_level = UINT8_MAX;
uint8_t logic_bluetooth_ble_battery_notif_char = 1;
uint8_t logic_bluetooth_ble_battery_level = 33;
/* Array of pointers to keyboard & raw hid profiles */
hid_prf_info_t* hid_prf_dataref[HID_MAX_SERV_INST];
/* HID profile structure for raw hid profile */
hid_prf_info_t logic_bluetooth_raw_hid_prf_data;
/* HID profile structure for keyboard profile */
hid_prf_info_t logic_bluetooth_hid_prf_data;
/* HID reports for RAW HID interface */
uint8_t logic_bluetooth_raw_hid_data_out_buf[64];
uint8_t logic_bluetooth_raw_hid_data_in_buf[64];
/* Control points and reports for HID keyboard */
uint8_t logic_bluetooth_boot_mouse_in_report[1];
uint8_t logic_bluetooth_boot_keyb_out_report[1];
uint8_t logic_bluetooth_boot_keyb_in_report[1];
uint8_t logic_bluetooth_keyboard_in_report[8];
uint8_t logic_bluetooth_mouse_in_report[4];
uint8_t logic_bluetooth_ctrl_point[1];
BOOL logic_bluetooth_typed_report_sent = FALSE;
/* Bluetooth connection bools */
BOOL logic_bluetooth_can_communicate_with_host_prev = FALSE;
BOOL logic_bluetooth_last_packet_received_over_hid = FALSE;
BOOL logic_bluetooth_notifications_just_enabled = FALSE;
BOOL logic_bluetooth_can_communicate_with_host = FALSE;
BOOL logic_bluetooth_open_to_pairing = FALSE;
BOOL logic_bluetooth_advertising = FALSE;
BOOL logic_bluetooth_just_connected = FALSE;
BOOL logic_bluetooth_just_paired = FALSE;
BOOL logic_bluetooth_connected = FALSE;
BOOL logic_bluetooth_paired = FALSE;
BOOL logic_bluetooth_disable_flag = FALSE;
/* Bool to specify if we setup the callbacks */
BOOL logic_bluetooth_callbacks_set = FALSE;
/* Banning timeouts */
uint16_t logic_bluetooth_last_temp_banned_mac_entered_index;
uint8_t logic_bluetooth_temp_banned_mac[3][6];
uint16_t logic_bluetooth_banning_timeouts[3];
/* Logic to know if some device is trying to connect to us too often */
BOOL logic_bluetooth_too_many_invalid_connect_notif_sent = FALSE;
uint8_t logic_bluetooth_invalid_connect_counter = 0;
/* Custom communication service */
at_ble_handle_t logic_bluetooth_comms_custom_service_handler;
at_ble_characteristic_t logic_bluetooth_comms_service_characs[2];
static at_ble_status_t hid_custom_event(void *param)
{
at_ble_status_t status = AT_BLE_SUCCESS;
return status;
}
/*! \fn logic_bluetooth_ms_tick(void)
* \brief Function called every ms
*/
void logic_bluetooth_ms_tick(void)
{
/* Decrease banning timeouts */
for (uint16_t i = 0; i < ARRAY_SIZE(logic_bluetooth_banning_timeouts); i++)
{
if (logic_bluetooth_banning_timeouts[i] != 0)
{
logic_bluetooth_banning_timeouts[i]--;
}
}
}
/*! \fn logic_bluetooth_denied_connection_trigger(void)
* \brief Called whenever a device tried to connect to the mini BLE and we didn't have the keys
*/
void logic_bluetooth_denied_connection_trigger(void)
{
if (++logic_bluetooth_invalid_connect_counter == 0)
{
/* Use wrapover to our advantage */
logic_bluetooth_too_many_invalid_connect_notif_sent = FALSE;
}
}
/*! \fn logic_bluetooth_set_disable_flag(void)
* \brief Set flag to disable bluetooth
*/
void logic_bluetooth_set_disable_flag(void)
{
logic_bluetooth_disable_flag = TRUE;
}
/*! \fn logic_bluetooth_store_temp_ban_connected_address(uint8_t* address)
* \brief Store provided address for potential upcoming ban
*/
void logic_bluetooth_store_temp_ban_connected_address(uint8_t* address)
{
cpu_irq_enter_critical();
/* Find an empty slot to store the mac address */
for (uint16_t i = 0; i < ARRAY_SIZE(logic_bluetooth_banning_timeouts); i++)
{
if (logic_bluetooth_banning_timeouts[i] == 0)
{
/* Store last temp banned index */
logic_bluetooth_last_temp_banned_mac_entered_index = i;
/* Copy MAC into temp ban buffer */
memcpy(logic_bluetooth_temp_banned_mac[i], address, sizeof(logic_bluetooth_temp_banned_mac[0]));
}
}
cpu_irq_leave_critical();
}
/*! \fn logic_bluetooth_temporarily_ban_connected_device(void)
* \brief Temporarily ban currently connected device
* \return RETURN_OK if we were actually connected to a device and if we setup the temp ban
*/
RET_TYPE logic_bluetooth_temporarily_ban_connected_device(void)
{
/* Mac is filled when device is connected */
if ((logic_is_ble_enabled() != FALSE) && (logic_bluetooth_can_communicate_with_host != FALSE) && (logic_bluetooth_last_temp_banned_mac_entered_index < ARRAY_SIZE(logic_bluetooth_banning_timeouts)))
{
/* Temp ban for 34 seconds */
logic_bluetooth_banning_timeouts[logic_bluetooth_last_temp_banned_mac_entered_index] = 45678;
ble_disconnect_all_devices();
return RETURN_OK;
}
else
{
return RETURN_NOK;
}
}
/*! \fn logic_bluetooth_is_device_temp_banned(uint8_t* mac)
* \brief Know if a given mac is temporarily banned
* \param mac MAC address for checks
* \return TRUE or false
*/
BOOL logic_bluetooth_is_device_temp_banned(uint8_t* mac)
{
BOOL return_val = FALSE;
/* Check all slots */
cpu_irq_enter_critical();
for (uint16_t i = 0; i < ARRAY_SIZE(logic_bluetooth_banning_timeouts); i++)
{
if ((logic_bluetooth_banning_timeouts[i] != 0) && (memcmp(logic_bluetooth_temp_banned_mac[i], mac, sizeof(logic_bluetooth_temp_banned_mac[0])) == 0))
{
return_val = TRUE;
}
}
cpu_irq_leave_critical();
return return_val;
}
/*! \fn logic_bluetooth_get_open_to_pairing(void)
* \brief Know if we are open for pairing
* \return The boolean
*/
BOOL logic_bluetooth_get_open_to_pairing(void)
{
return logic_bluetooth_open_to_pairing;
}
/*! \fn logic_bluetooth_set_open_to_pairing_bool(BOOL pairing_bool)
* \brief Specify if we are open for pairing
* \param pairing_bool The boolean
*/
void logic_bluetooth_set_open_to_pairing_bool(BOOL pairing_bool)
{
logic_bluetooth_open_to_pairing = pairing_bool;
}
/*! \fn logic_bluetooth_hid_connected_callback(void* params)
* \brief Called during device connection
*/
static at_ble_status_t logic_bluetooth_hid_connected_callback(void* params)
{
DBG_LOG("Connected to device");
/* Set booleans */
logic_bluetooth_just_connected = TRUE;
logic_bluetooth_advertising = FALSE;
logic_bluetooth_connected = TRUE;
/* Store connection handle */
at_ble_connected_t* connected = (at_ble_connected_t*)params;
logic_bluetooth_ble_connection_handle = connected->handle;
return AT_BLE_SUCCESS;
}
/*! \fn logic_bluetooth_check_and_wait_for_notif_sent(void)
* \brief Check if a notification if being sent and wait for end
*/
void logic_bluetooth_check_and_wait_for_notif_sent(void)
{
while (logic_bluetooth_notif_being_sent != NONE_NOTIF_SENDING)
{
ble_event_task();
}
}
/*! \fn logic_bluetooth_hid_disconnected_callback(void* params)
* \brief Called during device disconnection
*/
static at_ble_status_t logic_bluetooth_hid_disconnected_callback(void *params)
{
DBG_LOG("Disconnected from device");
/* Callback in case we're waiting for a packet send */
comms_raw_hid_send_callback(BLE_INTERFACE);
/* From battery service */
logic_bluetooth_battery_notification_flag = TRUE;
/* Inform main MCU */
if (logic_bluetooth_can_communicate_with_host != FALSE)
{
comms_main_mcu_send_simple_event(AUX_MCU_EVENT_BLE_DISCONNECTED);
}
/* Reset booleans */
logic_bluetooth_notif_being_sent = NONE_NOTIF_SENDING;
logic_bluetooth_can_communicate_with_host = FALSE;
logic_bluetooth_just_connected = FALSE;
logic_bluetooth_just_paired = FALSE;
logic_bluetooth_connected = FALSE;
logic_bluetooth_paired = FALSE;
/* Start advertising again */
logic_bluetooth_start_advertising();
return AT_BLE_SUCCESS;
}
/*! \fn logic_bluetooth_clear_bonding_information(void)
* \brief Called to clear all bonding information and disconnect any potential devices
*/
void logic_bluetooth_clear_bonding_information(void)
{
if (logic_is_ble_enabled() == FALSE)
{
ble_clear_bond_info();
}
else
{
ble_disconnect_all_devices();
ble_clear_bond_info();
}
}
/*! \fn logic_bluetooth_successfull_pairing_call(ble_connected_dev_info_t* dev_info, at_ble_connected_t* connected_info)
* \brief Called during device pairing
* \param dev_info Device information pointer
* \param connected_info Connection information pointer
*/
void logic_bluetooth_successfull_pairing_call(ble_connected_dev_info_t* dev_info, at_ble_connected_t* connected_info)
{
aux_mcu_message_t* temp_tx_message_pt;
DBG_LOG("Paired to device");
/* Set booleans */
logic_bluetooth_too_many_invalid_connect_notif_sent = FALSE;
logic_bluetooth_notif_being_sent = NONE_NOTIF_SENDING;
logic_bluetooth_can_communicate_with_host = TRUE;
logic_bluetooth_invalid_connect_counter = 0;
logic_bluetooth_just_paired = TRUE;
logic_bluetooth_paired = TRUE;
/* Store ban info if IRK address */
if (connected_info->peer_addr.type != AT_BLE_ADDRESS_PUBLIC)
{
logic_bluetooth_store_temp_ban_connected_address(dev_info->bond_info.peer_irk.addr.addr);
}
/* Inform main MCU */
comms_main_mcu_get_empty_packet_ready_to_be_sent(&temp_tx_message_pt, AUX_MCU_MSG_TYPE_BLE_CMD);
/* Set payload size */
temp_tx_message_pt->payload_length1 = sizeof(temp_tx_message_pt->ble_message.message_id) + sizeof(temp_tx_message_pt->ble_message.uint32_t_padding) + sizeof(temp_tx_message_pt->ble_message.bonding_information_to_store_message);
/* Message ID */
temp_tx_message_pt->ble_message.message_id = BLE_MESSAGE_STORE_BOND_INFO;
/***********************/
/* Bonding information */
/***********************/
/* General stuff */
temp_tx_message_pt->ble_message.bonding_information_to_store_message.zero_to_be_valid = 0;
temp_tx_message_pt->ble_message.bonding_information_to_store_message.address_resolv_type = dev_info->conn_info.peer_addr.type;
memcpy(temp_tx_message_pt->ble_message.bonding_information_to_store_message.mac_address, dev_info->conn_info.peer_addr.addr, sizeof(dev_info->conn_info.peer_addr.addr));
temp_tx_message_pt->ble_message.bonding_information_to_store_message.auth_type = dev_info->bond_info.auth;
/* Peer LTK */
memcpy(temp_tx_message_pt->ble_message.bonding_information_to_store_message.peer_ltk_key, dev_info->bond_info.peer_ltk.key, sizeof(dev_info->bond_info.peer_ltk.key));
temp_tx_message_pt->ble_message.bonding_information_to_store_message.peer_ltk_ediv = dev_info->bond_info.peer_ltk.ediv;
memcpy(temp_tx_message_pt->ble_message.bonding_information_to_store_message.peer_ltk_random_nb, dev_info->bond_info.peer_ltk.nb, sizeof(dev_info->bond_info.peer_ltk.nb));
temp_tx_message_pt->ble_message.bonding_information_to_store_message.peer_ltk_key_size = dev_info->bond_info.peer_ltk.key_size;
/* CSRK */
memcpy(temp_tx_message_pt->ble_message.bonding_information_to_store_message.peer_csrk_key, dev_info->bond_info.peer_csrk.key, sizeof(dev_info->bond_info.peer_csrk.key));
/* IRK */
memcpy(temp_tx_message_pt->ble_message.bonding_information_to_store_message.peer_irk_key, dev_info->bond_info.peer_irk.key, sizeof(dev_info->bond_info.peer_irk.key));
temp_tx_message_pt->ble_message.bonding_information_to_store_message.peer_irk_resolv_type = dev_info->bond_info.peer_irk.addr.type;
memcpy(temp_tx_message_pt->ble_message.bonding_information_to_store_message.peer_irk_address, dev_info->bond_info.peer_irk.addr.addr, sizeof(dev_info->bond_info.peer_irk.addr.addr));
/* Host LTK & CSRK */
memcpy(temp_tx_message_pt->ble_message.bonding_information_to_store_message.host_ltk_key, dev_info->host_ltk.key, sizeof(dev_info->host_ltk.key));
temp_tx_message_pt->ble_message.bonding_information_to_store_message.host_ltk_ediv = dev_info->host_ltk.ediv;
memcpy(temp_tx_message_pt->ble_message.bonding_information_to_store_message.host_ltk_random_nb, dev_info->host_ltk.nb, sizeof(dev_info->host_ltk.nb));
temp_tx_message_pt->ble_message.bonding_information_to_store_message.host_ltk_key_size = dev_info->host_ltk.key_size;
memcpy(temp_tx_message_pt->ble_message.bonding_information_to_store_message.host_csrk_key, dev_info->host_csrk.key, sizeof(dev_info->host_csrk.key));
/* Zero stuff */
memset(temp_tx_message_pt->ble_message.bonding_information_to_store_message.reserved, 0, sizeof(temp_tx_message_pt->ble_message.bonding_information_to_store_message.reserved));
/* Send packet */
comms_main_mcu_send_message((void*)temp_tx_message_pt, (uint16_t)sizeof(aux_mcu_message_t));
switch (dev_info->bond_info.auth)
{
case AT_BLE_AUTH_NO_MITM_NO_BOND: DBG_LOG("No Man In The Middle protection(MITM) , No Bonding");break;
case AT_BLE_AUTH_NO_MITM_BOND: DBG_LOG("No MITM, Bonding");break;
case AT_BLE_AUTH_MITM_NO_BOND: DBG_LOG("MITM, No Bonding");break;
case AT_BLE_AUTH_MITM_BOND: DBG_LOG("MITM and Bonding");break;
default: break;
}
#ifndef DEBUG_LOG_DISABLED
DBG_LOG("Host LTK Key (%dB): %02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x", dev_info->host_ltk.key_size,dev_info->host_ltk.key[0],dev_info->host_ltk.key[1],dev_info->host_ltk.key[2],dev_info->host_ltk.key[3],dev_info->host_ltk.key[4],dev_info->host_ltk.key[5],dev_info->host_ltk.key[6],dev_info->host_ltk.key[7],dev_info->host_ltk.key[8],dev_info->host_ltk.key[9],dev_info->host_ltk.key[10],dev_info->host_ltk.key[11],dev_info->host_ltk.key[12],dev_info->host_ltk.key[13],dev_info->host_ltk.key[14],dev_info->host_ltk.key[15]);
DBG_LOG("LTK ediv: %04x, RNG: %02x%02x%02x%02x%02x%02x%02x%02x", dev_info->host_ltk.ediv, dev_info->host_ltk.nb[0],dev_info->host_ltk.nb[1],dev_info->host_ltk.nb[2],dev_info->host_ltk.nb[3],dev_info->host_ltk.nb[4],dev_info->host_ltk.nb[5],dev_info->host_ltk.nb[6],dev_info->host_ltk.nb[7]);
DBG_LOG("Peer LTK Key (%dB): %02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x", dev_info->bond_info.peer_ltk.key_size,dev_info->bond_info.peer_ltk.key[0],dev_info->bond_info.peer_ltk.key[1],dev_info->bond_info.peer_ltk.key[2],dev_info->bond_info.peer_ltk.key[3],dev_info->bond_info.peer_ltk.key[4],dev_info->bond_info.peer_ltk.key[5],dev_info->bond_info.peer_ltk.key[6],dev_info->bond_info.peer_ltk.key[7],dev_info->bond_info.peer_ltk.key[8],dev_info->bond_info.peer_ltk.key[9],dev_info->bond_info.peer_ltk.key[10],dev_info->bond_info.peer_ltk.key[11],dev_info->bond_info.peer_ltk.key[12],dev_info->bond_info.peer_ltk.key[13],dev_info->bond_info.peer_ltk.key[14],dev_info->bond_info.peer_ltk.key[15]);
DBG_LOG("LTK ediv: %04x, RNG: %02x%02x%02x%02x%02x%02x%02x%02x", dev_info->bond_info.peer_ltk.ediv, dev_info->bond_info.peer_ltk.nb[0],dev_info->bond_info.peer_ltk.nb[1],dev_info->bond_info.peer_ltk.nb[2],dev_info->bond_info.peer_ltk.nb[3],dev_info->bond_info.peer_ltk.nb[4],dev_info->bond_info.peer_ltk.nb[5],dev_info->bond_info.peer_ltk.nb[6],dev_info->bond_info.peer_ltk.nb[7]);
DBG_LOG("CSRK: %02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",dev_info->bond_info.peer_csrk.key[0],dev_info->bond_info.peer_csrk.key[1],dev_info->bond_info.peer_csrk.key[2],dev_info->bond_info.peer_csrk.key[3],dev_info->bond_info.peer_csrk.key[4],dev_info->bond_info.peer_csrk.key[5],dev_info->bond_info.peer_csrk.key[6],dev_info->bond_info.peer_csrk.key[7],dev_info->bond_info.peer_csrk.key[8],dev_info->bond_info.peer_csrk.key[9],dev_info->bond_info.peer_csrk.key[10],dev_info->bond_info.peer_csrk.key[11],dev_info->bond_info.peer_csrk.key[12],dev_info->bond_info.peer_csrk.key[13],dev_info->bond_info.peer_csrk.key[14],dev_info->bond_info.peer_csrk.key[15]);
DBG_LOG("IRK: %02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",dev_info->bond_info.peer_irk.key[0],dev_info->bond_info.peer_irk.key[1],dev_info->bond_info.peer_irk.key[2],dev_info->bond_info.peer_irk.key[3],dev_info->bond_info.peer_irk.key[4],dev_info->bond_info.peer_irk.key[5],dev_info->bond_info.peer_irk.key[6],dev_info->bond_info.peer_irk.key[7],dev_info->bond_info.peer_irk.key[8],dev_info->bond_info.peer_irk.key[9],dev_info->bond_info.peer_irk.key[10],dev_info->bond_info.peer_irk.key[11],dev_info->bond_info.peer_irk.key[12],dev_info->bond_info.peer_irk.key[13],dev_info->bond_info.peer_irk.key[14],dev_info->bond_info.peer_irk.key[15]);
#endif
switch(dev_info->bond_info.peer_irk.addr.type)
{
case(AT_BLE_ADDRESS_PUBLIC): DBG_LOG("Public IRK addr");break;
case(AT_BLE_ADDRESS_RANDOM_STATIC): DBG_LOG("Random static IRK addr");break;
case(AT_BLE_ADDRESS_RANDOM_PRIVATE_RESOLVABLE): DBG_LOG("Random private resolvable IRK addr");break;
case(AT_BLE_ADDRESS_RANDOM_PRIVATE_NON_RESOLVABLE): DBG_LOG("Random private non resolvable IRK addr");break;
default: break;
}
DBG_LOG("IRK addr: %02x%02x%02x%02x%02x%02x",dev_info->bond_info.peer_irk.addr.addr[0],dev_info->bond_info.peer_irk.addr.addr[1],dev_info->bond_info.peer_irk.addr.addr[2],dev_info->bond_info.peer_irk.addr.addr[3],dev_info->bond_info.peer_irk.addr.addr[4],dev_info->bond_info.peer_irk.addr.addr[5]);
}
/*! \fn logic_bluetooth_encryption_changed_success(uint8_t* mac)
* \brief Called after encryption changed success
* \param mac Host MAC address (public or IRK)
*/
void logic_bluetooth_encryption_changed_success(uint8_t* mac)
{
/* Set boolean */
logic_bluetooth_too_many_invalid_connect_notif_sent = FALSE;
logic_bluetooth_notif_being_sent = NONE_NOTIF_SENDING;
logic_bluetooth_can_communicate_with_host = TRUE;
logic_bluetooth_invalid_connect_counter = 0;
}
/* Callbacks for GAP */
static const ble_gap_event_cb_t hid_app_gap_handle =
{
.connected = logic_bluetooth_hid_connected_callback,
.disconnected = logic_bluetooth_hid_disconnected_callback,
//.pair_done = logic_bluetooth_hid_paired_callback,
//.encryption_status_changed = logic_bluetooth_encryption_changed_callback
};
/*! \fn logic_bluetooth_notification_confirmed_callback(void* params)
* \brief Called when the notification has been confirmed by client
*/
static at_ble_status_t logic_bluetooth_notification_confirmed_callback(void* params)
{
at_ble_cmd_complete_event_t* notification_status;
notification_status = (at_ble_cmd_complete_event_t*) params;
/* Debug */
if(notification_status->status == AT_BLE_SUCCESS)
{
DBG_LOG("sending notification to the peer success");
}
else
{
DBG_LOG("ERROR: failed sending notification to peer");
}
if ((logic_bluetooth_notif_being_sent == RAW_HID_NOTIF_SENDING) || (logic_bluetooth_notif_being_sent == CUSTOM_COMMS_NOTIF_SENDING))
{
comms_raw_hid_send_callback(BLE_INTERFACE);
}
else if (logic_bluetooth_notif_being_sent == KEYBOARD_NOTIF_SENDING)
{
logic_bluetooth_typed_report_sent = TRUE;
}
else if (logic_bluetooth_notif_being_sent == BATTERY_NOTIF_SENDING)
{
/* From battery service */
if(notification_status->status == AT_BLE_SUCCESS)
{
logic_bluetooth_battery_notification_flag = TRUE;
}
}
/* Reset flag */
logic_bluetooth_notif_being_sent = NONE_NOTIF_SENDING;
return AT_BLE_SUCCESS;
}
/*! \fn logic_bluetooth_characteristic_changed_handler(void* params)
* \brief Service characteristic change handler function
*/
at_ble_status_t logic_bluetooth_characteristic_changed_handler(void* params)
{
at_ble_characteristic_changed_t change_params;
/* Copy locally the change params */
memcpy((uint8_t*)&change_params, params, sizeof(at_ble_characteristic_changed_t));
DBG_LOG("Characteristic %d changed", change_params.char_handle);
/* Check for battery service */
if(logic_bluetooth_bas_service_handler.serv_chars.client_config_handle == change_params.char_handle)
{
DBG_LOG("Characteristic belongs to battery service");
at_ble_status_t status = bat_char_changed_event(&logic_bluetooth_bas_service_handler, (at_ble_characteristic_changed_t*)params);
if(status == AT_BLE_SUCCESS)
{
logic_bluetooth_battery_notification_flag = TRUE;
DBG_LOG("Battery service: notifications enabled");
}
else if (status == AT_BLE_PRF_NTF_DISABLED)
{
logic_bluetooth_battery_notification_flag = FALSE;
DBG_LOG("Battery service: notifications disabled");
}
return status;
}
/* Check for custom comms notification receive */
if (logic_bluetooth_comms_service_characs[0].client_config_handle == change_params.char_handle)
{
if (change_params.char_new_value[0] != 0)
{
DBG_LOG("Computer subscribed to custom comms notifications");
}
else
{
DBG_LOG("Computer unsubscribed to custom comms notifications");
}
return AT_BLE_SUCCESS;
}
/* Check for custom comms service data receive */
if (logic_bluetooth_comms_service_characs[1].char_val_handle == change_params.char_handle)
{
DBG_LOG("Characteristic belongs to custom comms service");
DBG_LOG("BLE receive: %02x %02x %02x%02x %02x%02x, %d bytes", change_params.char_new_value[0], change_params.char_new_value[1], change_params.char_new_value[2], change_params.char_new_value[3], change_params.char_new_value[4], change_params.char_new_value[5], change_params.char_len);
uint8_t* recv_buf = comms_raw_hid_get_recv_buffer(BLE_INTERFACE);
memcpy(recv_buf, change_params.char_new_value, change_params.char_len);
comms_raw_hid_recv_callback(BLE_INTERFACE, change_params.char_len);
logic_bluetooth_last_packet_received_over_hid = FALSE;
// Loopback
// logic_bluetooth_custom_comms_send_data(change_params.conn_handle, recv_buf, change_params.char_len);
return AT_BLE_SUCCESS;
}
/* See if this is for the hid service, and if so get the service instance */
uint8_t serv_inst = logic_bluetooth_get_hid_serv_instance(change_params.char_handle);
/* If above function returns 0xFF... it means we don't know which service this characteristics belongs to */
if (serv_inst == 0xFF)
{
DBG_LOG("ERROR: couldn't find service for this characteristic!!!!");
}
else
{
DBG_LOG("Characteristic belongs to HID service instance %d", serv_inst);
}
/* Get the kind of notification based on the characteristic handle */
uint8_t ntf_op = logic_bluetooth_get_notif_instance(serv_inst, change_params.char_handle);
switch(ntf_op)
{
case PROTOCOL_MODE:
{
logic_bluetooth_protocol_mode_value.serv_inst = serv_inst;
logic_bluetooth_protocol_mode_value.mode = change_params.char_new_value[0];
logic_bluetooth_protocol_mode_value.conn_handle = change_params.conn_handle;
hid_prf_dataref[serv_inst]->protocol_mode = change_params.char_new_value[0];
DBG_LOG("Protocol Mode Notification Callback : Service Instance %d New Protocol Mode %d Connection Handle %d", serv_inst, logic_bluetooth_protocol_mode_value.mode, change_params.conn_handle);
}
break;
case CHAR_REPORT:
{
DBG_LOG("Report Callback : Service Instance %d Bytes Received %d Connection Handle %d", serv_inst, change_params.char_len, change_params.conn_handle);
DBG_LOG("BLE receive: %02x %02x %02x%02x %02x%02x", change_params.char_new_value[0], change_params.char_new_value[1], change_params.char_new_value[2], change_params.char_new_value[3], change_params.char_new_value[4], change_params.char_new_value[5]);
if (serv_inst == BLE_RAW_HID_SERVICE_INSTANCE)
{
uint8_t* recv_buf = comms_raw_hid_get_recv_buffer(BLE_INTERFACE);
memcpy(recv_buf, change_params.char_new_value, change_params.char_len);
comms_raw_hid_recv_callback(BLE_INTERFACE, change_params.char_len);
logic_bluetooth_last_packet_received_over_hid = TRUE;
}
}
break;
case CHAR_REPORT_CCD:
{
uint8_t report_id = logic_bluetooth_get_reportid(serv_inst, change_params.char_handle);
if ((report_id-1) < BLE_TOTAL_NUMBER_OF_REPORTS)
{
logic_bluetooth_report_ntf_info[report_id-1].serv_inst = serv_inst;
logic_bluetooth_report_ntf_info[report_id-1].report_ID = report_id;
logic_bluetooth_report_ntf_info[report_id-1].conn_handle = change_params.conn_handle;
logic_bluetooth_report_ntf_info[report_id-1].ntf_conf = (change_params.char_new_value[1]<<8 | change_params.char_new_value[0]);
if (logic_bluetooth_report_ntf_info[report_id-1].ntf_conf != 0)
{
logic_bluetooth_notifications_just_enabled = TRUE;
}
DBG_LOG("Report Notification Callback Service Instance %d Report ID %d Notification(Enable/Disable) %d Connection Handle %d", serv_inst, report_id, logic_bluetooth_report_ntf_info[report_id-1].ntf_conf, change_params.conn_handle);
}
}
break;
case (BOOT_KEY_INPUT_REPORT):
{
DBG_LOG("BOOT_KEY_INPUT_REPORT callback... why is this here?");
}
break;
case (BOOT_KEY_INPUT_CCD):
{
logic_bluetooth_boot_keyboard_report_ntf_info.serv_inst = serv_inst;
logic_bluetooth_boot_keyboard_report_ntf_info.boot_value = ntf_op;
logic_bluetooth_boot_keyboard_report_ntf_info.conn_handle = change_params.conn_handle;
logic_bluetooth_boot_keyboard_report_ntf_info.ntf_conf = (change_params.char_new_value[1]<<8 | change_params.char_new_value[0]);
DBG_LOG("Boot Notification Callback :: Service Instance %d Boot Value %d Notification(Enable/Disable) %d", serv_inst, ntf_op, logic_bluetooth_boot_keyboard_report_ntf_info.ntf_conf);
}
break;
case CONTROL_POINT:
{
logic_bluetooth_hid_control_point_value[serv_inst].serv_inst = serv_inst;
logic_bluetooth_hid_control_point_value[serv_inst].control_value = (change_params.char_new_value[1]<<8 | change_params.char_new_value[0]);
DBG_LOG("Control Point Notification Callback :: Service Instance %d Control Value %d", logic_bluetooth_hid_control_point_value[serv_inst].serv_inst, logic_bluetooth_hid_control_point_value[serv_inst].control_value);
}
break;
default:
{
DBG_LOG("Unhandled Notification");
}
break;
}
return AT_BLE_SUCCESS;
}
/* GATT callbacks */
static const ble_gatt_server_event_cb_t hid_app_gatt_server_handle =
{
.notification_confirmed = logic_bluetooth_notification_confirmed_callback,
.characteristic_changed = logic_bluetooth_characteristic_changed_handler
};
/* All BLE Manager Custom Event callback */
static const ble_custom_event_cb_t hid_custom_event_cb =
{
.custom_event = hid_custom_event
};
/* keyboard report */
static uint8_t logic_bluetooth_keyb_report_map[] =
{
/* Want to have your keyboard working on iOS? you better include that mouse in it... because that makes sense... right? */
0x05, 0x01, // Usage Page (Generic Desktop)
0x09, 0x02, // Usage (Mouse)
0xA1, 0x01, // Collection (Application)
0x85, 0x01, // Report Id (1)
0x09, 0x01, // Usage (Pointer)
0xA1, 0x00, // Collection (Physical)
0x05, 0x09, // Usage Page (Buttons)
0x19, 0x01, // Usage Minimum (01) - Button 1
0x29, 0x03, // Usage Maximum (03) - Button 3
0x15, 0x00, // Logical Minimum (0)
0x25, 0x01, // Logical Maximum (1)
0x75, 0x01, // Report Size (1)
0x95, 0x03, // Report Count (3)
0x81, 0x02, // Input (Data, Variable, Absolute) - Button states
0x75, 0x05, // Report Size (5)
0x95, 0x01, // Report Count (1)
0x81, 0x01, // Input (Constant) - Padding or Reserved bits
0x05, 0x01, // Usage Page (Generic Desktop)
0x09, 0x30, // Usage (X)
0x09, 0x31, // Usage (Y)
0x09, 0x38, // Usage (Wheel)
0x15, 0x81, // Logical Minimum (-127)
0x25, 0x7F, // Logical Maximum (127)
0x75, 0x08, // Report Size (8)
0x95, 0x03, // Report Count (3)
0x81, 0x06, // Input (Data, Variable, Relative) - X & Y coordinate
0xC0, // End Collection
0xC0, // End Collection
0x05, 0x01, // Usage Page (Generic Desktop),
0x09, 0x06, // Usage (Keyboard),
0xA1, 0x01, // Collection (Application),
0x85, 0x02, // REPORT ID (2) - MANDATORY
0x05, 0x07, // Usage Page (Key Codes),
0x19, 0xE0, // Usage Minimum (224, keyboard left control),
0x29, 0xE7, // Usage Maximum (231, keyboard right gui),
0x15, 0x00, // Logical Minimum (0),
0x25, 0x01, // Logical Maximum (1),
0x75, 0x01, // Report Size (1),
0x95, 0x08, // Report Count (8),
0x81, 0x02, // Input (Data, Variable, Absolute), ;Modifier byte
0x95, 0x01, // Report Count (1)
0x75, 0x08, // Report Size (8)
0x81, 0x01, // Input: (constant) ; Reserved byte
0x95, 0x05, // Report Count (5),
0x75, 0x01, // Report Size (1),
0x05, 0x08, // Usage Page (LEDs),
0x19, 0x01, // Usage Minimum (1),
0x29, 0x05, // Usage Maximum (5),
0x91, 0x02, // Output (Data, Variable, Absolute), ;LED report
0x95, 0x01, // Report Count (1),
0x75, 0x03, // Report Size (3),
0x91, 0x01, // Output (Constant), ;LED report padding
0x95, 0x06, // Report Count (6),
0x75, 0x08, // Report Size (8),
0x15, 0x00, // Logical Minimum (0),
0x25, 0xff, // Logical Maximum(255), was 0x68 (104) before
0x05, 0x07, // Usage Page (Key Codes),
0x19, 0x00, // Usage Minimum (0),
0x29, 0xe7, // Usage Maximum (231), was 0x68 (104) before
0x81, 0x00, // Input (Data, Array),
0xc0 // End Collection
};
/* raw keyboard report */
static uint8_t logic_bluetooth_raw_report_map[] =
{
0x06, USB_RAWHID_USAGE_PAGE & 0xFF, (USB_RAWHID_USAGE_PAGE >> 8) & 0xFF,
0x0A, USB_RAWHID_USAGE & 0xFF, (USB_RAWHID_USAGE >> 8) & 0xFF,
0xA1, 0x01, // Collection (application)
0xA1, 0x02, // Collection (logical)
0x85, 0x03, // Report ID 3
0x75, 0x08, // report size = 8 bits
0x15, 0x00, // logical minimum = 0
0x26, 0xFF, 0x00, // logical maximum = 255
0x95, USB_RAWHID_TX_SIZE, // report count
0x09, 0x01, // usage
0x81, 0x02, // Input (array)
0xC0, // end collection (logical)
0xA1, 0x02, // Collection (logical)
0x85, 0x04, // Report ID 4
0x75, 0x08, // report size = 8 bits
0x15, 0x00, // logical minimum = 0
0x26, 0xFF, 0x00, // logical maximum = 255
0x95, USB_RAWHID_RX_SIZE, // report count
0x09, 0x02, // usage
0x91, 0x02, // Output (array)
0xC0, // end collection (logical)
0xC0 // end collection (application)
};
/*! \fn logic_bluetooth_get_hid_serv_instance(uint16_t handle)
* \brief Function to get service instance
* \param Connection handle
*/
uint8_t logic_bluetooth_get_hid_serv_instance(uint16_t handle)
{
DBG_LOG_LOGIC_BT_AD("logic_bluetooth_get_hid_serv_instance :: Get service Instance Handle %d", handle);
for(uint8_t id = 0; id<HID_MAX_SERV_INST; id++)
{
DBG_LOG_LOGIC_BT_AD("logic_bluetooth_get_hid_serv_instance :: Service Handle %d Characteristic Handle %d", *(logic_bluetooth_hid_serv_instances[id].hid_dev_serv_handle), logic_bluetooth_hid_serv_instances[id].hid_control_point->char_val.handle);
if((handle > *(logic_bluetooth_hid_serv_instances[id].hid_dev_serv_handle)) && (handle <= logic_bluetooth_hid_serv_instances[id].hid_control_point->char_val.handle))
{
DBG_LOG_LOGIC_BT_AD("logic_bluetooth_get_hid_serv_instance :: Service Instance %d", id);
return id;
}
}
return 0xff;
}
/*! \fn logic_bluetooth_get_notif_instance(uint8_t serv_num, uint16_t char_handle)
* \brief Function to get the notification instance.
* \param serv_num Service Instance
* \param char_handle Character Handle
* \return see enum
*/
uint8_t logic_bluetooth_get_notif_instance(uint8_t serv_num, uint16_t char_handle)
{
DBG_LOG_LOGIC_BT_AD("logic_bluetooth_get_notif_instance :: Search Handle %d", char_handle);
DBG_LOG_LOGIC_BT_AD("logic_bluetooth_get_notif_instance :: Protocol Mode Handle %d", logic_bluetooth_hid_serv_instances[serv_num].hid_dev_proto_mode_char->char_val.handle);
if(logic_bluetooth_hid_serv_instances[serv_num].hid_dev_proto_mode_char->char_val.handle == char_handle)
{
return PROTOCOL_MODE;
}
for(uint8_t id = 0; id < hid_prf_dataref[serv_num]->num_of_report; id++)
{
DBG_LOG_LOGIC_BT_AD("logic_bluetooth_get_notif_instance :: Report Handle %d", logic_bluetooth_hid_serv_instances[serv_num].hid_dev_report_val_char[id]->char_val.handle);
if(char_handle == logic_bluetooth_hid_serv_instances[serv_num].hid_dev_report_val_char[id]->char_val.handle)
{
return CHAR_REPORT;
}
DBG_LOG_LOGIC_BT_AD("logic_bluetooth_get_notif_instance :: Report CCD Handle %d",logic_bluetooth_hid_serv_instances[serv_num].hid_dev_report_val_char[id]->client_config_desc.handle);
if(char_handle == logic_bluetooth_hid_serv_instances[serv_num].hid_dev_report_val_char[id]->client_config_desc.handle)
{
return CHAR_REPORT_CCD;
}
}
DBG_LOG_LOGIC_BT_AD("logic_bluetooth_get_notif_instance :: Boot Keyboard Handle %d", logic_bluetooth_hid_serv_instances[serv_num].hid_dev_boot_keyboard_in_report->char_val.handle);
if(logic_bluetooth_hid_serv_instances[serv_num].hid_dev_boot_keyboard_in_report->char_val.handle == char_handle)
{
return BOOT_KEY_INPUT_REPORT;
}
DBG_LOG_LOGIC_BT_AD("logic_bluetooth_get_notif_instance :: Boot Keyboard CCD Handle %d", logic_bluetooth_hid_serv_instances[serv_num].hid_dev_boot_keyboard_in_report->client_config_desc.handle);
if(logic_bluetooth_hid_serv_instances[serv_num].hid_dev_boot_keyboard_in_report->client_config_desc.handle == char_handle)
{
return BOOT_KEY_INPUT_CCD;
}
DBG_LOG_LOGIC_BT_AD("logic_bluetooth_get_notif_instance :: Control Point Handle %d", logic_bluetooth_hid_serv_instances[serv_num].hid_control_point->char_val.handle);
if(logic_bluetooth_hid_serv_instances[serv_num].hid_control_point->char_val.handle == char_handle)
{
return CONTROL_POINT;
}
return 0xFF;
}
/*! \fn logic_bluetooth_boot_key_report_update(at_ble_handle_t conn_handle, uint8_t serv_inst, uint8_t* bootreport, uint16_t len)
* \brief Function to update the boot keyboard report
* \param conn_handle Connection handle
* \param serv_inst Service Instance
* \param bootreport Report to be send
* \param len Length of report
*/
void logic_bluetooth_boot_key_report_update(at_ble_handle_t conn_handle, uint8_t serv_inst, uint8_t* bootreport, uint16_t len)
{
at_ble_status_t status;
uint16_t length = 2;
uint8_t value = 0;
status = at_ble_characteristic_value_get(logic_bluetooth_hid_serv_instances[serv_inst].hid_dev_boot_keyboard_in_report->client_config_desc.handle, &value, &length);
if (status != AT_BLE_SUCCESS)
{
DBG_LOG("logic_bluetooth_boot_key_report_update :: Characteristic value get fail Reason %d", status);
}
//If Notification Enabled
if(value == 1)
{
status = at_ble_characteristic_value_set(logic_bluetooth_hid_serv_instances[serv_inst].hid_dev_boot_keyboard_in_report->char_val.handle, bootreport, len);
if (status != AT_BLE_SUCCESS)
{
DBG_LOG("logic_bluetooth_boot_key_report_update :: Characteristic value set fail Reason %d", status);
}
//Need to check for connection handle
status = at_ble_notification_send(conn_handle, logic_bluetooth_hid_serv_instances[serv_inst].hid_dev_boot_keyboard_in_report->char_val.handle);
if (status != AT_BLE_SUCCESS)
{
DBG_LOG("logic_bluetooth_boot_key_report_update :: Notification send failed %d", status);
}
}
}
/*! \fn logic_bluetooth_get_reportid(uint8_t serv, uint16_t handle)
* \brief Function to get the report ID
* \param serv HID service instance
* \param handle Connection handle
*/
uint8_t logic_bluetooth_get_reportid(uint8_t serv, uint16_t handle)
{
uint8_t descval[2] = {0, 0};
uint16_t len = 2;
uint8_t id = 0;
uint8_t status;
DBG_LOG_LOGIC_BT_AD("logic_bluetooth_get_reportid :: Report Number %d", hid_prf_dataref[serv]->num_of_report);
for(id = 0; id < hid_prf_dataref[serv]->num_of_report; id++)
{
DBG_LOG_LOGIC_BT_AD("logic_bluetooth_get_reportid :: id %d Search Handle %d, CCD Handle %d", id, handle, logic_bluetooth_hid_serv_instances[serv].hid_dev_report_val_char[id]->client_config_desc.handle);
if(handle == logic_bluetooth_hid_serv_instances[serv].hid_dev_report_val_char[id]->client_config_desc.handle)
{
DBG_LOG_LOGIC_BT_AD("logic_bluetooth_get_reportid :: Report ID Descriptor Handle %d :: id %d :: serv %d", logic_bluetooth_hid_serv_instances[serv].hid_dev_report_val_char[id]->additional_desc_list->handle, id, serv);
status = at_ble_descriptor_value_get(logic_bluetooth_hid_serv_instances[serv].hid_dev_report_val_char[id]->additional_desc_list->handle, &descval[0], &len);
if (status != AT_BLE_SUCCESS)
{
DBG_LOG_LOGIC_BT_AD("logic_bluetooth_get_reportid :: Decriptor value get failed");
}
else
{
DBG_LOG_LOGIC_BT_AD("logic_bluetooth_get_reportid :: Descriptor value Report ID %d Type %d", descval[0], descval[1]);
}
return descval[0];
}
}
return HID_INVALID_INST;
}
/*! \fn logic_bluetooth_get_report_characteristic(uint16_t handle, uint8_t serv, uint8_t reportid)
* \brief Function to get report characteristic id.
* \param handle Connection handle
* \param serv Service Instance
* \param reportid Report id
*/
uint8_t logic_bluetooth_get_report_characteristic(uint16_t handle, uint8_t serv, uint8_t reportid)
{
uint8_t descval[2] = {0, 0};
at_ble_status_t status;
uint16_t len = 2;
uint8_t id = 0;
DBG_LOG_LOGIC_BT_AD("logic_bluetooth_get_report_characteristic :: Handle %d Service Instance %d Report ID %d", handle, serv, reportid);
for(id = 0; id < hid_prf_dataref[serv]->num_of_report; id++)
{
DBG_LOG_LOGIC_BT_AD("logic_bluetooth_get_report_characteristic :: Report ID Descriptor Handle %d :: id %d :: serv %d", logic_bluetooth_hid_serv_instances[serv].hid_dev_report_val_char[id]->additional_desc_list->handle, id, serv);
status = at_ble_descriptor_value_get(logic_bluetooth_hid_serv_instances[serv].hid_dev_report_val_char[id]->additional_desc_list->handle, &descval[0], &len);
if (status != AT_BLE_SUCCESS)
{
DBG_LOG_LOGIC_BT_AD("logic_bluetooth_get_report_characteristic :: Decriptor value get failed Reason %d", status);
}
else
{
DBG_LOG_LOGIC_BT_AD("logic_bluetooth_get_report_characteristic :: Report Value ID %d Type %d", descval[0], descval[1]);
}
if(descval[0] == reportid)
{
DBG_LOG_LOGIC_BT_AD("logic_bluetooth_get_report_characteristic :: Report Characteristic ID %d", id);
return id;
}
}
DBG_LOG_LOGIC_BT_AD("Couldn't find report char!");
return HID_INVALID_INST;
}
/*! \fn logic_bluetooth_update_report(uint16_t conn_handle, uint8_t serv_inst, uint8_t reportid, uint8_t *report, uint16_t len, BOOL use_report_charac)
* \brief Function to update the report
* \param conn_handle Connection handle
* \param serv_inst Service Instance
* \param report_id Report ID
* \param report Report to be send
* \param len Length of report
* \param use_report_charac Bool to indicate if we should use the report characteristic instead of the boot keyboard
*/
void logic_bluetooth_update_report(uint16_t conn_handle, uint8_t serv_inst, uint8_t reportid, uint8_t* report, uint16_t len, BOOL use_report_charac)
{
// TODO: should we check for notification subscription?
uint16_t status = 0;
uint8_t id;
/* Standard report? */
if (use_report_charac != FALSE)
{
/* Get report characteristic id */
id = logic_bluetooth_get_report_characteristic(conn_handle, serv_inst, reportid);
if(id != HID_INVALID_INST)
{
if((status = at_ble_characteristic_value_set(logic_bluetooth_hid_serv_instances[serv_inst].hid_dev_report_val_char[id]->char_val.handle, report, len)) == AT_BLE_SUCCESS)
{
DBG_LOG("Updated characteristic %d for hid instance %d and report id %d: sending %d bytes", id, serv_inst, reportid, len);
status = at_ble_notification_send(conn_handle, logic_bluetooth_hid_serv_instances[serv_inst].hid_dev_report_val_char[id]->char_val.handle);
if (status != AT_BLE_SUCCESS)
{
DBG_LOG("ERROR: Couldn't send notification, reason %d", status);
}
}
else
{
DBG_LOG("ERROR: couldn't update characteristic %d for hid instance %d and report id %d: sending %d bytes", id, serv_inst, reportid, len);
}
}
}
else
{
/* Boot keyboard: big hack */
if((status = at_ble_characteristic_value_set(logic_bluetooth_hid_serv_instances[BLE_KEYBOARD_HID_SERVICE_INSTANCE].hid_dev_boot_keyboard_in_report->char_val.handle, report, len)) == AT_BLE_SUCCESS)
{
DBG_LOG("Updated boot keyboard characteristic");
status = at_ble_notification_send(conn_handle, logic_bluetooth_hid_serv_instances[BLE_KEYBOARD_HID_SERVICE_INSTANCE].hid_dev_boot_keyboard_in_report->char_val.handle);
if (status != AT_BLE_SUCCESS)
{
DBG_LOG("ERROR: Couldn't send notification, reason %d", status);
}
}
else
{
DBG_LOG("ERROR: couldn't update boot keyboard characteristic");
}
}
}
/*! \fn logic_bluetooth_hid_profile_init(uint8_t servinst, uint8_t device, uint8_t *mode, uint8_t report_num, uint8_t *report_type, uint8_t **report_val, uint8_t *report_len, hid_info_t *info)
* \brief Initialize the hid profile based on user input
* \param servinst HID service instance
* \param device HID device
* \param mode HID mode
* \param report_num Number of report
* \param report_type Report Type
* \param report_val Report Value
* \param report_len Report Length
* \param info HID device info
*/
void logic_bluetooth_hid_profile_init(uint8_t servinst, uint8_t device, uint8_t* mode, uint8_t report_num, uint8_t* report_type, uint8_t** report_val, uint8_t* report_len, hid_info_t* info)
{
uint16_t cur_characteristic_index = 0;
/* Configure the HID service type */
logic_bluetooth_hid_gatt_instances[servinst].serv.type = PRIMARY_SERVICE;
/* Configure the HID service permission */
if(BLE_PAIR_ENABLE)
{
if(BLE_MITM_REQ)
{
logic_bluetooth_hid_gatt_instances[servinst].serv.perm = (AT_BLE_ATTR_READABLE_REQ_AUTHN_NO_AUTHR | AT_BLE_ATTR_WRITABLE_REQ_AUTHN_NO_AUTHR);
}
else
{
logic_bluetooth_hid_gatt_instances[servinst].serv.perm = (AT_BLE_ATTR_READABLE_REQ_ENC_NO_AUTHN_NO_AUTHR | AT_BLE_ATTR_WRITABLE_REQ_ENC_NO_AUTHN_NO_AUTHR);
}
}
else
{
logic_bluetooth_hid_gatt_instances[servinst].serv.perm = (AT_BLE_ATTR_READABLE_NO_AUTHN_NO_AUTHR | AT_BLE_ATTR_WRITABLE_NO_AUTHN_NO_AUTHR);
}
/* Configure the HID service handle */
logic_bluetooth_hid_gatt_instances[servinst].serv.handle = 0;
/* Configure HID Service */
logic_bluetooth_hid_gatt_instances[servinst].serv.uuid.type = AT_BLE_UUID_16;
logic_bluetooth_hid_gatt_instances[servinst].serv.uuid.uuid[0] = (uint8_t) HID_SERV_UUID;
logic_bluetooth_hid_gatt_instances[servinst].serv.uuid.uuid[1] = (uint8_t) (HID_SERV_UUID>>8);
/* Configure HID include service list */
logic_bluetooth_hid_gatt_instances[servinst].serv.inc_list = NULL;
/* Configure the HID include service count */
logic_bluetooth_hid_gatt_instances[servinst].serv.included_count = 0;