Skip to content

Commit 6b56e82

Browse files
author
unknownconstant
authored
Merge pull request #4 from unknownconstant/pairing
Fixed packet fragmentation
2 parents 065fc1d + 1431f3d commit 6b56e82

File tree

5 files changed

+100
-7
lines changed

5 files changed

+100
-7
lines changed

src/utility/HCI.cpp

+17-4
Original file line numberDiff line numberDiff line change
@@ -707,9 +707,7 @@ void HCIClass::handleAclDataPkt(uint8_t /*plen*/, uint8_t pdata[])
707707
uint16_t cid;
708708
} *aclHdr = (HCIACLHdr*)pdata;
709709

710-
#ifdef _BLE_TRACE_
711-
Serial.println("Received data");
712-
#endif
710+
713711
uint16_t aclFlags = (aclHdr->handle & 0xf000) >> 12;
714712

715713
if ((aclHdr->dlen - 4) != aclHdr->len) {
@@ -729,6 +727,17 @@ void HCIClass::handleAclDataPkt(uint8_t /*plen*/, uint8_t pdata[])
729727
}
730728

731729
if ((aclHdr->dlen - 4) != aclHdr->len) {
730+
#ifdef _BLE_TRACE_
731+
Serial.println("Don't have full packet yet");
732+
Serial.print("Handle: ");
733+
btct.printBytes((uint8_t*)&aclHdr->handle,2);
734+
Serial.print("dlen: ");
735+
btct.printBytes((uint8_t*)&aclHdr->dlen,2);
736+
Serial.print("len: ");
737+
btct.printBytes((uint8_t*)&aclHdr->len,2);
738+
Serial.print("cid: ");
739+
btct.printBytes((uint8_t*)&aclHdr->cid,2);
740+
#endif
732741
// don't have the full packet yet
733742
return;
734743
}
@@ -751,7 +760,11 @@ void HCIClass::handleAclDataPkt(uint8_t /*plen*/, uint8_t pdata[])
751760
#ifdef _BLE_TRACE_
752761
Serial.println("Security data");
753762
#endif
754-
L2CAPSignaling.handleSecurityData(aclHdr->handle & 0x0fff, aclHdr->len, &_recvBuffer[1 + sizeof(HCIACLHdr)]);
763+
if (aclFlags == 0x1){
764+
L2CAPSignaling.handleSecurityData(aclHdr->handle & 0x0fff, aclHdr->len, &_aclPktBuffer[sizeof(HCIACLHdr)]);
765+
}else{
766+
L2CAPSignaling.handleSecurityData(aclHdr->handle & 0x0fff, aclHdr->len, &_recvBuffer[1 + sizeof(HCIACLHdr)]);
767+
}
755768

756769
}else {
757770
struct __attribute__ ((packed)) {

src/utility/L2CAPSignaling.cpp

+11-2
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
#include "btct.h"
2323
#include "L2CAPSignaling.h"
2424
#include "keyDistribution.h"
25-
25+
#include "bitDescriptions.h"
2626
#define CONNECTION_PARAMETER_UPDATE_REQUEST 0x12
2727
#define CONNECTION_PARAMETER_UPDATE_RESPONSE 0x13
2828

@@ -143,6 +143,15 @@ void L2CAPSignalingClass::handleSecurityData(uint16_t connectionHandle, uint8_t
143143
ATT.remoteKeyDistribution = KeyDistribution(pairingRequest->initiatorKeyDistribution);
144144
ATT.localKeyDistribution = KeyDistribution(pairingRequest->responderKeyDistribution);
145145
KeyDistribution rkd(pairingRequest->responderKeyDistribution);
146+
AuthReq req(pairingRequest->authReq);
147+
#ifdef _BLE_TRACE_
148+
Serial.print("Req has properties: ");
149+
Serial.print(req.Bonding()?"bonding, ":"no bonding, ");
150+
Serial.print(req.CT2()?"CT2, ":"no CT2, ");
151+
Serial.print(req.KeyPress()?"KeyPress, ":"no KeyPress, ");
152+
Serial.print(req.MITM()?"MITM, ":"no MITM, ");
153+
Serial.print(req.SC()?"SC, ":"no SC, ");
154+
#endif
146155

147156
uint8_t peerIOCap[3];
148157
peerIOCap[0] = pairingRequest->authReq;
@@ -152,7 +161,7 @@ void L2CAPSignalingClass::handleSecurityData(uint16_t connectionHandle, uint8_t
152161
ATT.setPeerEncryption(connectionHandle, ATT.getPeerEncryption(connectionHandle) | PEER_ENCRYPTION::PAIRING_REQUEST);
153162
#ifdef _BLE_TRACE_
154163
Serial.print("Peer encryption : 0b");
155-
Serial.print(ATT.getPeerEncryption(connectionHandle), BIN);
164+
Serial.println(ATT.getPeerEncryption(connectionHandle), BIN);
156165
#endif
157166
struct __attribute__ ((packed)) PairingResponse {
158167
uint8_t code;

src/utility/bitDescriptions.cpp

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#include "bitDescriptions.h"
2+
3+
4+
#define BONDING_BIT 0b00000001
5+
#define MITM_BIT 0b00000100
6+
#define SC_BIT 0b00001000
7+
#define KEYPRESS_BIT 0b00010000
8+
#define CT2_BIT 0b00100000
9+
10+
11+
AuthReq::AuthReq(){}
12+
AuthReq::AuthReq(uint8_t octet):_octet(octet){}
13+
bool AuthReq::Bonding(){ return (_octet & BONDING_BIT)>0;}
14+
bool AuthReq::MITM(){ return (_octet & MITM_BIT)>0;}
15+
bool AuthReq::SC(){ return (_octet & SC_BIT)>0;}
16+
bool AuthReq::KeyPress(){ return (_octet & KEYPRESS_BIT)>0;}
17+
bool AuthReq::CT2(){ return (_octet & CT2_BIT)>0;}
18+
19+
20+
void AuthReq::setBonding(bool state) { _octet= state? _octet|BONDING_BIT : _octet&~BONDING_BIT;}
21+
void AuthReq::setMITM(bool state) { _octet= state? _octet|MITM_BIT : _octet&~MITM_BIT;}
22+
void AuthReq::setSC(bool state){ _octet= state? _octet|SC_BIT : _octet&~SC_BIT;}
23+
void AuthReq::setKeyPress(bool state){ _octet= state? _octet|KEYPRESS_BIT : _octet&~KEYPRESS_BIT;}
24+
void AuthReq::setCT2(bool state){ _octet= state? _octet|CT2_BIT : _octet&~CT2_BIT;}
25+
26+
uint8_t _octet;
27+
28+
29+
void AuthReq::setOctet( uint8_t octet){_octet = octet;}
30+
uint8_t AuthReq::getOctet() {return _octet;}

src/utility/bitDescriptions.h

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#ifndef _BIT_DESCRIPTIONS_H_
2+
#define _BIT_DESCRIPTIONS_H_
3+
#include <Arduino.h>
4+
5+
class AuthReq{
6+
public:
7+
AuthReq();
8+
AuthReq(uint8_t octet);
9+
void setOctet( uint8_t octet);
10+
uint8_t getOctet();
11+
12+
13+
// The Bonding_Flags field is a 2-bit field that indicates the type of bonding being requested by the initiating device
14+
bool Bonding();
15+
// The MITM field is a 1-bit flag that is set to one if the device is requesting MITM protection
16+
bool MITM();
17+
// The SC field is a 1 bit flag. If LE Secure Connections pairing is supported by the device, then the SC field shall be set to 1, otherwise it shall be set to 0.
18+
bool SC();
19+
// The keypress field is a 1-bit flag that is used only in the Passkey Entry protocol and shall be ignored in other protocols.
20+
bool KeyPress();
21+
// The CT2 field is a 1-bit flag that shall be set to 1 upon transmission to indicate support for the h7 function.
22+
bool CT2();
23+
24+
void setBonding(bool state);
25+
void setMITM(bool state);
26+
void setSC(bool state);
27+
void setKeyPress(bool state);
28+
void setCT2(bool state);
29+
private:
30+
uint8_t _octet;
31+
};
32+
33+
enum IOCap {
34+
DisplayOnly,
35+
DisplayYesNo,
36+
KeyboardOnly,
37+
NoInputNoOutput,
38+
KeyboardDisplay
39+
};
40+
41+
#endif

src/utility/keyDistribution.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#include "keyDistribution.h"
22

3-
KeyDistribution::KeyDistribution(){}
3+
KeyDistribution::KeyDistribution():_octet(0){}
44
KeyDistribution::KeyDistribution(uint8_t octet):_octet(octet){}
55

66
#define ENCKEY 0b00000001

0 commit comments

Comments
 (0)