@@ -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 (¬ification[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
12831338void ATTClass::writeResp (uint16_t connectionHandle, uint8_t dlen, uint8_t data[])
0 commit comments