Skip to content

Commit 97571e3

Browse files
author
unknownconstant
committed
With write encryptionn requirement
1 parent 9cde1de commit 97571e3

File tree

3 files changed

+72
-6
lines changed

3 files changed

+72
-6
lines changed

src/utility/ATT.cpp

+57-2
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,8 @@ void ATTClass::handleData(uint16_t connectionHandle, uint8_t dlen, uint8_t data[
276276
case ATT_OP_ERROR:
277277
#ifdef _BLE_TRACE_
278278
Serial.println("[Info] data error");
279+
// Serial.print("Error: ");
280+
// btct.printBytes(data, dlen);
279281
#endif
280282
error(connectionHandle, dlen, data);
281283
break;
@@ -559,6 +561,7 @@ bool ATTClass::handleNotify(uint16_t handle, const uint8_t* value, int length)
559561
memcpy(&notification[notificationLength], value, length);
560562
notificationLength += length;
561563

564+
/// TODO: Set encyption requirement on notify.
562565
HCI.sendAclPkt(_peers[i].connectionHandle, ATT_CID, notificationLength, notification);
563566

564567
numNotifications++;
@@ -1214,6 +1217,7 @@ void ATTClass::writeReqOrCmd(uint16_t connectionHandle, uint16_t mtu, uint8_t op
12141217
uint8_t* value = &data[sizeof(handle)];
12151218

12161219
BLELocalAttribute* attribute = GATT.attribute(handle - 1);
1220+
bool holdResponse = false;
12171221

12181222
if (attribute->type() == BLETypeCharacteristic) {
12191223
BLELocalCharacteristic* characteristic = (BLELocalCharacteristic*)attribute;
@@ -1226,10 +1230,33 @@ void ATTClass::writeReqOrCmd(uint16_t connectionHandle, uint16_t mtu, uint8_t op
12261230
}
12271231
return;
12281232
}
1233+
// Check permssion
1234+
if((characteristic->properties() & BLEProperty::BLEAuth)> 0 && (getPeerEncryption(connectionHandle) & PEER_ENCRYPTION::ENCRYPTED_AES) == 0){
1235+
holdResponse = true;
1236+
sendError(connectionHandle, ATT_OP_WRITE_REQ, handle, ATT_ECODE_INSUFF_ENC);
1237+
}
12291238

12301239
for (int i = 0; i < ATT_MAX_PEERS; i++) {
12311240
if (_peers[i].connectionHandle == connectionHandle) {
1232-
characteristic->writeValue(BLEDevice(_peers[i].addressType, _peers[i].address), value, valueLength);
1241+
if(holdResponse){
1242+
1243+
writeBufferSize = 0;
1244+
memcpy(writeBuffer, &handle, 2);
1245+
writeBufferSize+=2;
1246+
1247+
writeBuffer[writeBufferSize++] = _peers[i].addressType;
1248+
1249+
memcpy(&writeBuffer[writeBufferSize], _peers[i].address, sizeof(_peers[i].address));
1250+
writeBufferSize += sizeof(_peers[i].address);
1251+
1252+
writeBuffer[writeBufferSize] = valueLength;
1253+
writeBufferSize += sizeof(valueLength);
1254+
1255+
memcpy(&writeBuffer[writeBufferSize], value, valueLength);
1256+
writeBufferSize += valueLength;
1257+
}else{
1258+
characteristic->writeValue(BLEDevice(_peers[i].addressType, _peers[i].address), value, valueLength);
1259+
}
12331260
break;
12341261
}
12351262
}
@@ -1276,8 +1303,36 @@ void ATTClass::writeReqOrCmd(uint16_t connectionHandle, uint16_t mtu, uint8_t op
12761303
response[0] = ATT_OP_WRITE_RESP;
12771304
responseLength = 1;
12781305

1279-
HCI.sendAclPkt(connectionHandle, ATT_CID, responseLength, response);
1306+
if(holdResponse){
1307+
memcpy(holdBuffer, response, responseLength);
1308+
holdBufferSize = responseLength;
1309+
}else{
1310+
HCI.sendAclPkt(connectionHandle, ATT_CID, responseLength, response);
1311+
}
1312+
}
1313+
}
1314+
int ATTClass::processWriteBuffer(){
1315+
if(writeBufferSize==0){
1316+
return 0;
12801317
}
1318+
1319+
struct __attribute__ ((packed)) WriteBuffer {
1320+
uint16_t handle;
1321+
uint8_t addressType;
1322+
uint8_t address[6];
1323+
uint8_t valueLength;
1324+
uint8_t value[];
1325+
} *writeBufferStruct = (WriteBuffer*)&ATT.writeBuffer;
1326+
// uint8_t value[writeBufferStruct->valueLength];
1327+
// memcpy(value, writeBufferStruct->value, writeBufferStruct->valueLength);
1328+
BLELocalAttribute* attribute = GATT.attribute(writeBufferStruct->handle-1);
1329+
BLELocalCharacteristic* characteristic = (BLELocalCharacteristic*)attribute;
1330+
#ifdef _BLE_TRACE_
1331+
Serial.println("Writing value");
1332+
#endif
1333+
characteristic->writeValue(BLEDevice(writeBufferStruct->addressType, writeBufferStruct->address), writeBufferStruct->value, writeBufferStruct->valueLength);
1334+
writeBufferSize = 0;
1335+
return 1;
12811336
}
12821337

12831338
void ATTClass::writeResp(uint16_t connectionHandle, uint8_t dlen, uint8_t data[])

src/utility/ATT.h

+3
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,10 @@ class ATTClass {
9595
virtual int setPeerIOCap(uint16_t connectionHandle, uint8_t IOCap[]);
9696
virtual int getPeerIOCap(uint16_t connectionHandle, uint8_t IOCap[]);
9797
uint8_t holdBuffer[64];
98+
uint8_t writeBuffer[64];
9899
uint8_t holdBufferSize;
100+
uint8_t writeBufferSize;
101+
virtual int processWriteBuffer();
99102
private:
100103
virtual void error(uint16_t connectionHandle, uint8_t dlen, uint8_t data[]);
101104
virtual void mtuReq(uint16_t connectionHandle, uint8_t dlen, uint8_t data[]);

src/utility/HCI.cpp

+12-4
Original file line numberDiff line numberDiff line change
@@ -666,13 +666,20 @@ void HCIClass::handleEventPkt(uint8_t /*plen*/, uint8_t pdata[])
666666
#endif
667667
if(encryptionChange->enabled>0){
668668
ATT.setPeerEncryption(encryptionChange->connectionHandle, PEER_ENCRYPTION::ENCRYPTED_AES);
669+
if(ATT.writeBufferSize > 0){
670+
ATT.processWriteBuffer();
671+
}
672+
if(ATT.holdBufferSize>0){
673+
#ifdef _BLE_TRACE_
674+
Serial.print("Sending queued response size: ");
675+
Serial.println(ATT.holdBufferSize);
676+
#endif
677+
HCI.sendAclPkt(encryptionChange->connectionHandle, ATT_CID, ATT.holdBufferSize, ATT.holdBuffer);
678+
ATT.holdBufferSize = 0;
679+
}
669680
}else{
670681
ATT.setPeerEncryption(encryptionChange->connectionHandle, PEER_ENCRYPTION::NO_ENCRYPTION);
671682
}
672-
if(ATT.holdBufferSize>0){
673-
HCI.sendAclPkt(encryptionChange->connectionHandle, ATT_CID, ATT.holdBufferSize, ATT.holdBuffer);
674-
ATT.holdBufferSize = 0;
675-
}
676683
}
677684
else if (eventHdr->evt == EVT_CMD_COMPLETE)
678685
{
@@ -940,6 +947,7 @@ void HCIClass::handleEventPkt(uint8_t /*plen*/, uint8_t pdata[])
940947

941948
uint8_t Z = 0;
942949
for(int i=0; i<16; i++){
950+
/// TODO: Implement secure random
943951
Nb[i] = rand(); //// Should use ESP or ECCx08
944952
}
945953
#ifdef _BLE_TRACE_

0 commit comments

Comments
 (0)