13
13
Kristian Lauszus, TKJ Electronics
14
14
Web : http://www.tkjelectronics.com
15
15
e-mail : kristianl@tkjelectronics.com
16
- */
16
+
17
+ Modified 9 April 2013 by Shun Ito
18
+ Web : http://ll0s0ll.wordpress.com/
19
+ */
17
20
18
21
#include " BTD.h"
19
22
#define DEBUG // Uncomment to print data for debugging
@@ -477,15 +480,30 @@ void BTD::HCI_event_task() {
477
480
#endif
478
481
hci_link_key_request_negative_reply ();
479
482
break ;
480
-
483
+ // -- MODIFIED> -----------------------------------------------------------//
481
484
case EV_AUTHENTICATION_COMPLETE:
485
+ #ifdef DEBUG
486
+ Notify (PSTR (" \r\n [HCI_EV] AUTHENTICATION_COMPLETE..." ), 0x80 );
487
+ if (!hcibuf[2 ])
488
+ Notify (PSTR (" OK" ), 0x80 );
489
+ #endif
490
+ if (!hcibuf[2 ]) { // Status
491
+ #ifdef EXTRADEBUG
492
+ Notify (PSTR (" Connection_Handle:" ), 0x80 );
493
+ PrintHex<uint8_t >(hcibuf[3 ], 0x80 );
494
+ Notify (PSTR (" " ), 0x80 );
495
+ PrintHex<uint8_t >(hcibuf[4 ], 0x80 );
496
+ #endif
497
+ hci_Set_Connection_Encryption (hci_handle);
498
+ }
482
499
if (pairWithWii && !connectToWii) {
483
500
#ifdef DEBUG
484
501
Notify (PSTR (" \r\n Pairing successful" ), 0x80 );
485
502
#endif
486
503
connectToWii = true ; // Only send the ACL data to the Wii service
487
504
}
488
505
break ;
506
+ // -- <MODIFIED -----------------------------------------------------------//
489
507
/* We will just ignore the following events */
490
508
case EV_NUM_COMPLETE_PKT:
491
509
case EV_ROLE_CHANGED:
@@ -496,9 +514,81 @@ void BTD::HCI_event_task() {
496
514
case EV_MAX_SLOTS_CHANGE:
497
515
case EV_QOS_SETUP_COMPLETE:
498
516
case EV_LINK_KEY_NOTIFICATION:
499
- case EV_ENCRYPTION_CHANGE:
517
+ // -- MODIFIED> -----------------------------------------------------------//
518
+ // case EV_ENCRYPTION_CHANGE:
519
+ // -- <MODIFIED -----------------------------------------------------------//
500
520
case EV_READ_REMOTE_VERSION_INFORMATION_COMPLETE:
501
521
break ;
522
+ // -- MODIFIED> -----------------------------------------------------------//
523
+ case EV_IO_CAPABILITY_REQUEST:
524
+ #ifdef EXTRADEBUG
525
+ Notify (PSTR (" \r\n [HCI_EV] IO_CAPABILITY_REQUEST" ), 0x80 );
526
+ #endif
527
+ disc_bdaddr[0 ] = hcibuf[2 ];
528
+ disc_bdaddr[1 ] = hcibuf[3 ];
529
+ disc_bdaddr[2 ] = hcibuf[4 ];
530
+ disc_bdaddr[3 ] = hcibuf[5 ];
531
+ disc_bdaddr[4 ] = hcibuf[6 ];
532
+ disc_bdaddr[5 ] = hcibuf[7 ];
533
+
534
+ hci_IO_Capability_Request_Reply ();
535
+ break ;
536
+
537
+ case EV_IO_CAPABILITY_RESPONSE:
538
+ #ifdef EXTRADEBUG
539
+ Notify (PSTR (" \r\n [HCI_EV] IO_CAPABILITY_RESPONSE - BD_ADDR:" ), 0x80 );
540
+ PrintHex<uint8_t >(hcibuf[2 ], 0x80 );
541
+ Notify (PSTR (" " ), 0x80 );
542
+ PrintHex<uint8_t >(hcibuf[3 ], 0x80 );
543
+ Notify (PSTR (" " ), 0x80 );
544
+ PrintHex<uint8_t >(hcibuf[4 ], 0x80 );
545
+ Notify (PSTR (" " ), 0x80 );
546
+ PrintHex<uint8_t >(hcibuf[5 ], 0x80 );
547
+ Notify (PSTR (" " ), 0x80 );
548
+ PrintHex<uint8_t >(hcibuf[6 ], 0x80 );
549
+ Notify (PSTR (" " ), 0x80 );
550
+ PrintHex<uint8_t >(hcibuf[7 ], 0x80 );
551
+ Notify (PSTR (" IO_Capability:" ), 0x80 );
552
+ PrintHex<uint8_t >(hcibuf[8 ], 0x80 );
553
+ Notify (PSTR (" OOB_Data_Present:" ), 0x80 );
554
+ PrintHex<uint8_t >(hcibuf[9 ], 0x80 );
555
+ Notify (PSTR (" Authentication_Requirements:" ), 0x80 );
556
+ PrintHex<uint8_t >(hcibuf[10 ], 0x80 );
557
+ #endif
558
+ break ;
559
+
560
+ case EV_USER_CONFIRMATION_REQUEST:
561
+ #ifdef DEBUG
562
+ Notify (PSTR (" \r\n [HCI_EV] User_Confirmation_Request - Numeric_Value:0x" ), 0x80 );
563
+ PrintHex<uint8_t >(hcibuf[11 ], 0x80 );
564
+ PrintHex<uint8_t >(hcibuf[10 ], 0x80 );
565
+ PrintHex<uint8_t >(hcibuf[9 ], 0x80 );
566
+ PrintHex<uint8_t >(hcibuf[8 ], 0x80 );
567
+ #endif
568
+ hci_User_Confirmation_Request_Reply ();
569
+ break ;
570
+
571
+ case EV_SIMPLE_PAIRING_COMPLETE:
572
+ #ifdef DEBUG
573
+ Notify (PSTR (" \r\n [HCI_EV] SIMPLE_PAIRING_COMPLETE... " ), 0x80 );
574
+ if (!hcibuf[2 ])
575
+ Notify (PSTR (" OK" ), 0x80 );
576
+ #endif
577
+ break ;
578
+
579
+ case EV_ENCRYPTION_CHANGE:
580
+ #ifdef DEBUG
581
+ Notify (PSTR (" \r\n [HCI_EV] ENCRYPTION_CHANGE... " ), 0x80 );
582
+ #endif
583
+ if (!hcibuf[2 ]) { // Status
584
+ #ifdef DEBUG
585
+ Notify (PSTR (" OK" ), 0x80 );
586
+ #endif
587
+ m_simple_pairing_completed = true ;
588
+ }
589
+ break ;
590
+
591
+ // -- <MODIFIED -----------------------------------------------------------//
502
592
#ifdef EXTRADEBUG
503
593
default :
504
594
if (hcibuf[0 ] != 0x00 ) {
@@ -585,17 +675,47 @@ void BTD::HCI_task() {
585
675
hci_state = HCI_CHECK_WII_SERVICE;
586
676
}
587
677
break ;
588
-
678
+ // -- MODIFIED> -----------------------------------------------------------//
589
679
case HCI_SET_NAME_STATE:
590
680
if (hci_cmd_complete) {
591
681
#ifdef DEBUG
592
682
Notify (PSTR (" \r\n The name is set to: " ), 0x80 );
593
683
Serial.print (btdName);
594
684
#endif
595
- hci_state = HCI_CHECK_WII_SERVICE;
685
+ hci_write_class_of_device ();
686
+ hci_state = HCI_WRITE_CoD_STATE;
596
687
}
597
688
break ;
598
-
689
+
690
+ case HCI_WRITE_CoD_STATE:
691
+ if (hci_cmd_complete) {
692
+ #ifdef DEBUG
693
+ Notify (PSTR (" \r\n [HCI_EV] Write_CoD_CMD COMPLETE" ), 0x80 );
694
+ #endif
695
+ hci_Write_Simple_Pairing_Mode ();
696
+ hci_state = HCI_WRITE_SIMPLE_PAIRING_STATE;
697
+ }
698
+ break ;
699
+
700
+ case HCI_WRITE_SIMPLE_PAIRING_STATE:
701
+ if (hci_cmd_complete) {
702
+ #ifdef DEBUG
703
+ Notify (PSTR (" \r\n [HCI_EV] WRITE_SIMPLE_PAIRING COMPLETE" ), 0x80 );
704
+ #endif
705
+ hci_Set_Event_Mask_SPM ();
706
+ hci_state = HCI_SET_EVENT_MASK_STATE;
707
+ }
708
+ break ;
709
+
710
+ case HCI_SET_EVENT_MASK_STATE:
711
+ if (hci_cmd_complete) {
712
+ #ifdef DEBUG
713
+ Notify (PSTR (" \r\n [HCI_EV] SET_EVENT_MASK COMPLETE" ), 0x80 );
714
+ #endif
715
+ hci_state = HCI_CHECK_WII_SERVICE;
716
+ }
717
+ break ;
718
+ // -- <MODIFIED -----------------------------------------------------------//
599
719
case HCI_CHECK_WII_SERVICE:
600
720
if (pairWithWii) { // Check if it should try to connect to a wiimote
601
721
#ifdef DEBUG
@@ -1015,17 +1135,89 @@ void BTD::hci_disconnect(uint16_t handle) { // This is called by the different s
1015
1135
1016
1136
HCI_Command (hcibuf, 6 );
1017
1137
}
1018
-
1019
- void BTD::hci_write_class_of_device () { // See http://bluetooth-pentest.narod.ru/software/bluetooth_class_of_device-service_generator.html
1020
- hcibuf[0 ] = 0x24 ; // HCI OCF = 3
1021
- hcibuf[1 ] = 0x03 << 2 ; // HCI OGF = 3
1022
- hcibuf[2 ] = 0x03 ; // parameter length = 3
1023
- hcibuf[3 ] = 0x04 ; // Robot
1024
- hcibuf[4 ] = 0x08 ; // Toy
1025
- hcibuf[5 ] = 0x00 ;
1026
-
1027
- HCI_Command (hcibuf, 6 );
1138
+ // -- MODIFIED> -----------------------------------------------------------//
1139
+ void BTD::hci_write_class_of_device () {
1140
+ hcibuf[0 ] = 0x24 ; // HCI OCF = 24
1141
+ hcibuf[1 ] = 0x03 << 2 ; // HCI OGF = 3
1142
+ hcibuf[2 ] = 0x03 ; // Parameter Length = 3
1143
+ hcibuf[3 ] = 0x40 ; // Minor Device Class - 0x40=keyboard 0xC0=Combo(keyboard/pointing device)
1144
+ hcibuf[4 ] = 0x05 ; // Major Device Class - Peripheral
1145
+ hcibuf[5 ] = 0x00 ; //
1146
+
1147
+ HCI_Command (hcibuf, 6 );
1148
+ }
1149
+ void BTD::hci_Write_Simple_Pairing_Mode () {
1150
+ hcibuf[0 ] = 0x56 ; // HCI OCF = 56
1151
+ hcibuf[1 ] = 0x03 << 2 ; // HCI OGF = 3
1152
+ hcibuf[2 ] = 0x01 ; // Parameter Length = 3
1153
+ hcibuf[3 ] = 0x01 ; // Simple_Pairing_Mode - 0x01=enabled
1154
+
1155
+ HCI_Command (hcibuf, 4 );
1156
+ }
1157
+ void BTD::hci_Set_Event_Mask_SPM () {
1158
+ hcibuf[0 ] = 0x01 ; // HCI OCF = 1
1159
+ hcibuf[1 ] = 0x03 << 2 ; // HCI OGF = 3
1160
+ hcibuf[2 ] = 0x08 ; // Parameter Length = 8
1161
+ hcibuf[3 ] = 0xFF ; // Event_Mask:0x00 ff 1f ff ff ff ff ff
1162
+ hcibuf[4 ] = 0xFF ; //
1163
+ hcibuf[5 ] = 0xFF ; //
1164
+ hcibuf[6 ] = 0xFF ; //
1165
+ hcibuf[7 ] = 0xFF ; //
1166
+ hcibuf[8 ] = 0x1F ; //
1167
+ hcibuf[9 ] = 0xFF ; //
1168
+ hcibuf[10 ] = 0x00 ; //
1169
+
1170
+ HCI_Command (hcibuf, 11 );
1171
+ }
1172
+ void BTD::hci_Authentication_Requested (uint16_t handle) {
1173
+ hcibuf[0 ] = 0x11 ; // HCI OCF = 11
1174
+ hcibuf[1 ] = 0x01 << 2 ; // HCI OGF = 1
1175
+ hcibuf[2 ] = 0x02 ; // Parameter Length = 8
1176
+ hcibuf[3 ] = (uint8_t )(handle & 0xFF );// connection handle - low byte
1177
+ hcibuf[4 ] = (uint8_t )((handle >> 8 ) & 0x0F );// connection handle - high byte
1178
+
1179
+ HCI_Command (hcibuf, 5 );
1180
+ }
1181
+ void BTD::hci_IO_Capability_Request_Reply () {
1182
+ hcibuf[0 ] = 0x2B ; // HCI OCF = 2B
1183
+ hcibuf[1 ] = 0x01 << 2 ; // HCI OGF = 1
1184
+ hcibuf[2 ] = 0x09 ; // Parameter Length = 9
1185
+ hcibuf[3 ] = disc_bdaddr[0 ]; // 6 octet bdaddr
1186
+ hcibuf[4 ] = disc_bdaddr[1 ];
1187
+ hcibuf[5 ] = disc_bdaddr[2 ];
1188
+ hcibuf[6 ] = disc_bdaddr[3 ];
1189
+ hcibuf[7 ] = disc_bdaddr[4 ];
1190
+ hcibuf[8 ] = disc_bdaddr[5 ];
1191
+ hcibuf[9 ] = 0x03 ; // IO_Capability - 0x03 NoInputNoOutput
1192
+ hcibuf[10 ] = 0x00 ; // OOB_Data_Present - 0x00 not present
1193
+ hcibuf[11 ] = 0x05 ; // Authentication_Requirements - 0x00?
1194
+
1195
+ HCI_Command (hcibuf, 12 );
1196
+ }
1197
+ void BTD::hci_User_Confirmation_Request_Reply () {
1198
+ hcibuf[0 ] = 0x2C ; // HCI OCF = 2C
1199
+ hcibuf[1 ] = 0x01 << 2 ; // HCI OGF = 1
1200
+ hcibuf[2 ] = 0x06 ; // Parameter Length = 6
1201
+ hcibuf[3 ] = disc_bdaddr[0 ]; // 6 octet bdaddr
1202
+ hcibuf[4 ] = disc_bdaddr[1 ];
1203
+ hcibuf[5 ] = disc_bdaddr[2 ];
1204
+ hcibuf[6 ] = disc_bdaddr[3 ];
1205
+ hcibuf[7 ] = disc_bdaddr[4 ];
1206
+ hcibuf[8 ] = disc_bdaddr[5 ];
1207
+
1208
+ HCI_Command (hcibuf, 9 );
1209
+ }
1210
+ void BTD::hci_Set_Connection_Encryption (uint16_t handle) {
1211
+ hcibuf[0 ] = 0x13 ; // HCI OCF = 13
1212
+ hcibuf[1 ] = 0x01 << 2 ; // HCI OGF = 1
1213
+ hcibuf[2 ] = 0x03 ; // Parameter Length = 9
1214
+ hcibuf[3 ] = (uint8_t )(handle & 0xFF ); // Connection_Handle - low byte
1215
+ hcibuf[4 ] = (uint8_t )((handle >> 8 ) & 0x0F ); // Connection_Handle - high byte
1216
+ hcibuf[5 ] = 0x01 ; // Encryption_Enable - 0x01=ON
1217
+
1218
+ HCI_Command (hcibuf, 6 );
1028
1219
}
1220
+ // -- <MODIFIED -----------------------------------------------------------//
1029
1221
/* ******************************************************************
1030
1222
* *
1031
1223
* HCI ACL Data Packet *
0 commit comments