@@ -276,6 +276,8 @@ void ATTClass::handleData(uint16_t connectionHandle, uint8_t dlen, uint8_t data[
276
276
case ATT_OP_ERROR:
277
277
#ifdef _BLE_TRACE_
278
278
Serial.println (" [Info] data error" );
279
+ // Serial.print("Error: ");
280
+ // btct.printBytes(data, dlen);
279
281
#endif
280
282
error (connectionHandle, dlen, data);
281
283
break ;
@@ -559,6 +561,7 @@ bool ATTClass::handleNotify(uint16_t handle, const uint8_t* value, int length)
559
561
memcpy (¬ification[notificationLength], value, length);
560
562
notificationLength += length;
561
563
564
+ // / TODO: Set encyption requirement on notify.
562
565
HCI.sendAclPkt (_peers[i].connectionHandle , ATT_CID, notificationLength, notification);
563
566
564
567
numNotifications++;
@@ -1214,6 +1217,7 @@ void ATTClass::writeReqOrCmd(uint16_t connectionHandle, uint16_t mtu, uint8_t op
1214
1217
uint8_t * value = &data[sizeof (handle)];
1215
1218
1216
1219
BLELocalAttribute* attribute = GATT.attribute (handle - 1 );
1220
+ bool holdResponse = false ;
1217
1221
1218
1222
if (attribute->type () == BLETypeCharacteristic) {
1219
1223
BLELocalCharacteristic* characteristic = (BLELocalCharacteristic*)attribute;
@@ -1226,10 +1230,33 @@ void ATTClass::writeReqOrCmd(uint16_t connectionHandle, uint16_t mtu, uint8_t op
1226
1230
}
1227
1231
return ;
1228
1232
}
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
+ }
1229
1238
1230
1239
for (int i = 0 ; i < ATT_MAX_PEERS; i++) {
1231
1240
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
+ }
1233
1260
break ;
1234
1261
}
1235
1262
}
@@ -1276,8 +1303,36 @@ void ATTClass::writeReqOrCmd(uint16_t connectionHandle, uint16_t mtu, uint8_t op
1276
1303
response[0 ] = ATT_OP_WRITE_RESP;
1277
1304
responseLength = 1 ;
1278
1305
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 ;
1280
1317
}
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 ;
1281
1336
}
1282
1337
1283
1338
void ATTClass::writeResp (uint16_t connectionHandle, uint8_t dlen, uint8_t data[])
0 commit comments