Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sync changes with ArduinoBLE #62

Open
wants to merge 122 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
Commits
Show all changes
122 commits
Select commit Hold shift + click to select a range
2986119
Add enhanced advertising examples
polldo Nov 2, 2020
c663798
Initial - encrypted read
Jan 1, 2021
9cde1de
Merged master
Jan 1, 2021
e5ccd3b
Merge pull request #1 from unknownconstant/pairing
Jan 1, 2021
97571e3
With write encryptionn requirement
Jan 1, 2021
f364628
Merge pull request #2 from unknownconstant/pairing
Jan 1, 2021
fa42bb2
Write encryption & visible LTK / IRK
Jan 2, 2021
065fc1d
Merge pull request #3 from unknownconstant/pairing
Jan 2, 2021
1431f3d
Fixed packet fragmentation
Jan 2, 2021
6b56e82
Merge pull request #4 from unknownconstant/pairing
Jan 2, 2021
6ef59a5
Android bugfix
Jan 3, 2021
7ecf299
Merge pull request #5 from unknownconstant/pairing
Jan 3, 2021
0aa2e87
Handle remote DHKey confirmation before own DHKey
eltos Jan 3, 2021
750b7f1
Actually check remote DHKey
eltos Jan 3, 2021
915e914
Move LTK signaling and confirm to separate method
eltos Jan 4, 2021
f54e7a2
Add method to control and query pairing
eltos Jan 4, 2021
283038f
Merge pull request #6 from eltos/master
Jan 4, 2021
d9fb4bc
Secure random & reject unknown LTK
Jan 4, 2021
fb2b817
Merge branch 'master' into secure-random
Jan 4, 2021
83d11ed
Merge pull request #16 from unknownconstant/secure-random
Jan 4, 2021
e2868a7
Pairing code & binary confirm callbacks
Jan 7, 2021
6b08d21
Updated example sketch
Jan 7, 2021
a429760
Merge pull request #17 from unknownconstant/pairing-code
Jan 7, 2021
3dd53d1
Spellcheck fixes
Jan 24, 2021
55d5bf2
add access to raw advertisement data
tlossen Dec 26, 2020
a9550cf
Saturate length of retrieved advertisement data
polldo Apr 20, 2021
bd492be
Add advertisement data auxiliary functions
polldo Apr 20, 2021
b5d1b73
Add Nano RP2040 Connect to "smoke test" examples compilation CI workflow
per1234 Apr 28, 2021
e080601
Add retrieval of manufacturer data
polldo Apr 20, 2021
3e0cae6
Merge pull request #177 from per1234/ci
per1234 Jun 20, 2021
e1b0d81
moved BLE library to markdown
LiamAljundi Aug 6, 2021
ceb3932
updated content
LiamAljundi Aug 23, 2021
21a332f
fixed suggestions from Karl's review
LiamAljundi Aug 24, 2021
7ed640c
Merge pull request #200 from LiamAljundi/liamAljundi/ble-library-migr…
karlsoderby Aug 24, 2021
236aeda
updated library properties and readme
akash73 Aug 30, 2021
b155246
fix of bad indexing in ATTClass::handleNotifyOrInd which affected BLE…
Lukas-kV Dec 8, 2021
f6b7952
Merge pull request #211 from Lukas-kV/master
facchinm Dec 9, 2021
c8de775
Replace boolean with standard bool (#42)
fpistm Dec 14, 2021
14596d4
Adding sync-labels workflow for sharing one set of labels across all …
aentinger Dec 14, 2021
af4d848
Use major version ref of `carlosperate/download-file-action` (#215)
per1234 Dec 20, 2021
95b2449
Configure Dependabot to check for outdated actions used in workflows …
per1234 Dec 20, 2021
8d4d1dc
fix potential arrary overflow problem of _recvBuffer (#214)
bigbearishappy Dec 22, 2021
ed61a17
Bluetooth trademark fix
karlsoderby Feb 23, 2022
38fe924
Merge pull request #221 from arduino-libraries/karlsoderby/bluetooth-…
karlsoderby Feb 23, 2022
57ce514
Bump actions/checkout from 2 to 3
dependabot[bot] Mar 2, 2022
ca92e1b
Merge pull request #224 from arduino-libraries/dependabot/github_acti…
per1234 Mar 2, 2022
c9bfd21
Update readme.md
bboyho Mar 19, 2022
c925d82
Add compatibility with Nicla Vision
giulcioffi Mar 24, 2022
c12774c
Release 1.2.2
giulcioffi Mar 24, 2022
bfc3202
Update api.md
jacobhylen Mar 31, 2022
b146def
Merge pull request #225 from bboyho/master
karlsoderby Apr 1, 2022
5d5aaaa
Merge pull request #228 from jacobhylen/jacobhylen/change-links
karlsoderby Apr 1, 2022
4cf376e
Unification of documentation
mklemarczyk Apr 7, 2022
da1da57
Merge pull request #231 from mklemarczyk/hotfix/docs-api-fix
facchinm Apr 11, 2022
6438f81
Bump actions/download-artifact from 2 to 3
dependabot[bot] Apr 11, 2022
785ef5c
Bump actions/upload-artifact from 2 to 3
dependabot[bot] Apr 11, 2022
130af4e
Merge pull request #234 from arduino-libraries/dependabot/github_acti…
per1234 Apr 11, 2022
7dc1f79
Merge pull request #233 from arduino-libraries/dependabot/github_acti…
per1234 Apr 11, 2022
b94ff5f
Update api.md
jacobhylen Apr 29, 2022
ad8b870
Merge pull request #235 from jacobhylen/jacobhylen/docs-maintenance
karlsoderby Apr 29, 2022
b39ac7e
Add support for NICLA VISION
giulcioffi Sep 21, 2021
8f9e785
Merge pull request #156 from unknownconstant/master
facchinm May 13, 2022
f107880
Merge pull request #183 from polldo/adv-data-retrieve
facchinm May 13, 2022
f2f73e8
Merge pull request #134 from polldo/example-enhanced-advertising
facchinm May 13, 2022
58136fc
Bugfix: memory leak caused by variables not being deleted in end(). #192
alranel May 12, 2022
3c6a53a
Respect refcounting before freeing dynamically allocated variables
alranel May 12, 2022
1223199
Call end() upon destruction
alranel May 12, 2022
f24ba26
Prevent double object deletion
alranel May 12, 2022
2b72c94
Don't double-free characteristics
facchinm May 23, 2022
afccc49
Don't terminate CordioHCIHook::getDriver() on end() for Murata chips
facchinm May 23, 2022
3003f12
Merge pull request #237 from alranel/bugfix/192-leak
facchinm May 30, 2022
f074724
Release 1.3.0
facchinm May 30, 2022
d5bc5b8
Prepend 'BLE_GATT_' to READ and WRITE enums to avoid clashes with Ard…
giulcioffi May 31, 2022
f24f335
Publish 1.3.1
facchinm May 31, 2022
9bf21b7
Fix Long Term Key Request Negative Reply Command
laviator98 Jun 9, 2022
fae1e36
Change non returning functions to void + spelling (#251)
dominsch Aug 23, 2022
991f076
Support esp32, esp32-C3, esp32-S3 (#252)
dominsch Aug 23, 2022
cd9cbad
Fix hard fault when str is NULL
grobwrk Aug 30, 2022
6e97bf2
Fix memory leak on end() -> begin() -> end()
facchinm Aug 31, 2022
2a1cf72
Merge pull request #259 from grobx/arduino/master
facchinm Aug 31, 2022
b5fc368
fix the potential overflow problem of leAdvertisingReport->eirData
bigbearishappy Aug 31, 2022
5954d96
Merge pull request #260 from facchinm/fix_end_hang
facchinm Sep 5, 2022
dd56eb7
Release 1.3.2
facchinm Sep 5, 2022
07d249a
Fix BLE scanning block after few minutes
sallasia Oct 3, 2022
c928892
Update GAP.cpp
sallasia Oct 3, 2022
ccdca60
Merge pull request #264 from sallasia/steve_allasia
facchinm Oct 10, 2022
1ec7d00
Bump geekyeggo/delete-artifact from 1 to 2
dependabot[bot] Oct 13, 2022
46985b0
Merge pull request #267 from arduino-libraries/dependabot/github_acti…
per1234 Oct 13, 2022
8ae816c
Bump carlosperate/download-file-action from 1 to 2
dependabot[bot] Oct 25, 2022
b40f618
Merge pull request #270 from arduino-libraries/dependabot/github_acti…
per1234 Oct 25, 2022
f852716
Add compatibility with Giga
facchinm Mar 10, 2023
289d3d6
Publish version 1.3.3
facchinm Mar 10, 2023
a263bc7
Restore logic for end() on STM32H7 boards (1DX module)
facchinm Mar 27, 2023
55e7bb6
Publish 1.3.4
facchinm Mar 27, 2023
9aa695e
Fix warnings:
roleroz May 29, 2023
1dfbf62
Remove another unused variable
roleroz May 29, 2023
0ad2d7d
Support C33
facchinm Apr 4, 2023
7e0c757
Merge pull request #303 from facchinm/c33
facchinm Jun 22, 2023
0753ec0
Fix broken image embed in documentation
per1234 Jul 2, 2023
a29f1aa
Fix broken link in documentation
per1234 Jul 2, 2023
726a6c8
Merge pull request #304 from per1234/fix-link
per1234 Jul 2, 2023
8ca5415
Update library.properties
facchinm Jul 11, 2023
675fc91
Merge pull request #302 from roleroz/master
facchinm Aug 4, 2023
c0cd781
Add support for OPTA
manchoz Aug 24, 2023
54d6317
Merge pull request #319 from manchoz/add_opta_support
facchinm Aug 25, 2023
2a35e72
wip: add support for UNO R4 WiFi
facchinm Jul 3, 2023
742ba78
unor4wifi: fix metadata and gh action
facchinm Jul 11, 2023
c05459e
Merge pull request #305 from facchinm/uno_r4_wifi
facchinm Aug 30, 2023
56dbfb3
Publish 1.3.6
facchinm Aug 30, 2023
3043861
Bump actions/checkout from 3 to 4
dependabot[bot] Sep 5, 2023
dff9e41
Merge pull request #323 from arduino-libraries/dependabot/github_acti…
per1234 Sep 5, 2023
d49c7f3
Squashed commit of the following:
AresMaster Oct 17, 2023
3f8c27a
Merge branch 'pr/1'
AresMaster Oct 17, 2023
63b1e88
Small fixes and changes
AresMaster Oct 17, 2023
fe46ea8
Example fixes
AresMaster Oct 17, 2023
12d92e3
Fixes, update masks, static mac address
AresMaster Oct 18, 2023
04e0204
Temporary fix for device pairing
AresMaster Oct 18, 2023
ea20081
Fixes for pairing with Static Random Address
AresMaster Oct 19, 2023
4fd8381
Merge branch 'main' of https://github.com/AresMaster/STM32duinoBLE
AresMaster Oct 19, 2023
2019736
Revert change in default address type
AresMaster Oct 19, 2023
81dd688
Deletion of unnecessary files
AresMaster Oct 31, 2023
578fdb6
Merge branch 'main' of https://github.com/AresMaster/STM32duinoBLE
AresMaster Oct 31, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Initial - encrypted read
  • Loading branch information
unknownconstant committed Jan 1, 2021
commit c66379882f727276b74c319c1ec9fa824fc96bf1
1 change: 1 addition & 0 deletions src/ArduinoBLE.h
Original file line number Diff line number Diff line change
@@ -24,5 +24,6 @@
#include "BLEProperty.h"
#include "BLEStringCharacteristic.h"
#include "BLETypedCharacteristics.h"
#include "utility/btct.h"

#endif
40 changes: 39 additions & 1 deletion src/BLEProperty.h
Original file line number Diff line number Diff line change
@@ -17,6 +17,8 @@
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/

// #include <stdint.h>

#ifndef _BLE_PROPERTY_H_
#define _BLE_PROPERTY_H_

@@ -26,7 +28,43 @@ enum BLEProperty {
BLEWriteWithoutResponse = 0x04,
BLEWrite = 0x08,
BLENotify = 0x10,
BLEIndicate = 0x20
BLEIndicate = 0x20,
BLEAuth = 1 << 6,
BLEExtProp = 1 << 7,
};

#define ESP_GATT_CHAR_PROP_BIT_BROADCAST (1 << 0) /* 0x01 */ /* relate to BTA_GATT_CHAR_PROP_BIT_BROADCAST in bta/bta_gatt_api.h */
#define ESP_GATT_CHAR_PROP_BIT_READ (1 << 1) /* 0x02 */ /* relate to BTA_GATT_CHAR_PROP_BIT_READ in bta/bta_gatt_api.h */
#define ESP_GATT_CHAR_PROP_BIT_WRITE_NR (1 << 2) /* 0x04 */ /* relate to BTA_GATT_CHAR_PROP_BIT_WRITE_NR in bta/bta_gatt_api.h */
#define ESP_GATT_CHAR_PROP_BIT_WRITE (1 << 3) /* 0x08 */ /* relate to BTA_GATT_CHAR_PROP_BIT_WRITE in bta/bta_gatt_api.h */
#define ESP_GATT_CHAR_PROP_BIT_NOTIFY (1 << 4) /* 0x10 */ /* relate to BTA_GATT_CHAR_PROP_BIT_NOTIFY in bta/bta_gatt_api.h */
#define ESP_GATT_CHAR_PROP_BIT_INDICATE (1 << 5) /* 0x20 */ /* relate to BTA_GATT_CHAR_PROP_BIT_INDICATE in bta/bta_gatt_api.h */
#define ESP_GATT_CHAR_PROP_BIT_AUTH (1 << 6) /* 0x40 */ /* relate to BTA_GATT_CHAR_PROP_BIT_AUTH in bta/bta_gatt_api.h */
#define ESP_GATT_CHAR_PROP_BIT_EXT_PROP (1 << 7) /* 0x80 */ /* relate to BTA_GATT_CHAR_PROP_BIT_EXT_PROP in bta/bta_gatt_api.h */

#define ESP_GATT_PERM_READ (1 << 0) /* bit 0 - 0x0001 */ /* relate to BTA_GATT_PERM_READ in bta/bta_gatt_api.h */
#define ESP_GATT_PERM_READ_ENCRYPTED (1 << 1) /* bit 1 - 0x0002 */ /* relate to BTA_GATT_PERM_READ_ENCRYPTED in bta/bta_gatt_api.h */
#define ESP_GATT_PERM_READ_ENC_MITM (1 << 2) /* bit 2 - 0x0004 */ /* relate to BTA_GATT_PERM_READ_ENC_MITM in bta/bta_gatt_api.h */
#define ESP_GATT_PERM_WRITE (1 << 4) /* bit 4 - 0x0010 */ /* relate to BTA_GATT_PERM_WRITE in bta/bta_gatt_api.h */
#define ESP_GATT_PERM_WRITE_ENCRYPTED (1 << 5) /* bit 5 - 0x0020 */ /* relate to BTA_GATT_PERM_WRITE_ENCRYPTED in bta/bta_gatt_api.h */
#define ESP_GATT_PERM_WRITE_ENC_MITM (1 << 6) /* bit 6 - 0x0040 */ /* relate to BTA_GATT_PERM_WRITE_ENC_MITM in bta/bta_gatt_api.h */
#define ESP_GATT_PERM_WRITE_SIGNED (1 << 7) /* bit 7 - 0x0080 */ /* relate to BTA_GATT_PERM_WRITE_SIGNED in bta/bta_gatt_api.h */
#define ESP_GATT_PERM_WRITE_SIGNED_MITM (1 << 8) /* bit 8 - 0x0100 */ /* relate to BTA_GATT_PERM_WRITE_SIGNED_MITM in bta/bta_gatt_api.h */
#define ESP_GATT_PERM_READ_AUTHORIZATION (1 << 9) /* bit 9 - 0x0200 */
#define ESP_GATT_PERM_WRITE_AUTHORIZATION (1 << 10) /* bit 10 - 0x0400 */

enum BLE_GATT_PERM_ {
READ = 1 << 0,
READ_ENCRYPTED = 1 << 1,
READ_ENC_MITM = 1 << 2,
WRITE = 1 << 4,
WRITE_ENCRYPTED = 1 << 5,
WRITE_ENC_MITM = 1 << 6,
WRITE_SIGNED = 1 << 7,
WRITE_SIGNED_MITM = 1 << 8,
READ_AUTHORIZATION = 1 << 9,
WRITE_AUTHORIZATION = 1 << 10,
};


#endif
4 changes: 4 additions & 0 deletions src/local/BLELocalDevice.cpp
Original file line number Diff line number Diff line change
@@ -107,6 +107,10 @@ int BLELocalDevice::begin()
end();
return 0;
}
if (HCI.setLeEventMask(0x00000000000001FF) != 0) {
end();
return 0;
}

uint16_t pktLen;
uint8_t maxPkt;
1 change: 1 addition & 0 deletions src/local/BLELocalDevice.h
Original file line number Diff line number Diff line change
@@ -74,6 +74,7 @@ class BLELocalDevice {

void debug(Stream& stream);
void noDebug();
uint8_t BDaddress[6];

private:
};
132 changes: 129 additions & 3 deletions src/utility/ATT.cpp
Original file line number Diff line number Diff line change
@@ -95,6 +95,7 @@ ATTClass::ATTClass() :
memset(_peers[i].address, 0x00, sizeof(_peers[i].address));
_peers[i].mtu = 23;
_peers[i].device = NULL;
_peers[i].encryption = 0x0;
}

memset(_eventHandlers, 0x00, sizeof(_eventHandlers));
@@ -267,12 +268,22 @@ void ATTClass::handleData(uint16_t connectionHandle, uint8_t dlen, uint8_t data[

uint16_t mtu = this->mtu(connectionHandle);

#ifdef _BLE_TRACE_
Serial.print("data opcode: 0x");
Serial.println(opcode, HEX);
#endif
switch (opcode) {
case ATT_OP_ERROR:
#ifdef _BLE_TRACE_
Serial.println("[Info] data error");
#endif
error(connectionHandle, dlen, data);
break;

case ATT_OP_MTU_REQ:
#ifdef _BLE_TRACE_
Serial.println("MTU");
#endif
mtuReq(connectionHandle, dlen, data);
break;

@@ -281,6 +292,9 @@ void ATTClass::handleData(uint16_t connectionHandle, uint8_t dlen, uint8_t data[
break;

case ATT_OP_FIND_INFO_REQ:
#ifdef _BLE_TRACE_
Serial.println("Find info");
#endif
findInfoReq(connectionHandle, mtu, dlen, data);
break;

@@ -293,6 +307,9 @@ void ATTClass::handleData(uint16_t connectionHandle, uint8_t dlen, uint8_t data[
break;

case ATT_OP_READ_BY_TYPE_REQ:
#ifdef _BLE_TRACE_
Serial.println("By type");
#endif
readByTypeReq(connectionHandle, mtu, dlen, data);
break;

@@ -319,6 +336,9 @@ void ATTClass::handleData(uint16_t connectionHandle, uint8_t dlen, uint8_t data[

case ATT_OP_WRITE_REQ:
case ATT_OP_WRITE_CMD:
#ifdef _BLE_TRACE_
Serial.println("Write req");
#endif
writeReqOrCmd(connectionHandle, mtu, opcode, dlen, data);
break;

@@ -346,6 +366,9 @@ void ATTClass::handleData(uint16_t connectionHandle, uint8_t dlen, uint8_t data[
case ATT_OP_READ_MULTI_REQ:
case ATT_OP_SIGNED_WRITE_CMD:
default:
#ifdef _BLE_TRACE_
Serial.println("[Info] Unhandled dara");
#endif
sendError(connectionHandle, opcode, 0x00, ATT_ECODE_REQ_NOT_SUPP);
break;
}
@@ -398,6 +421,10 @@ void ATTClass::removeConnection(uint16_t handle, uint8_t /*reason*/)
_peers[peerIndex].addressType = 0x00;
memset(_peers[peerIndex].address, 0x00, sizeof(_peers[peerIndex].address));
_peers[peerIndex].mtu = 23;
_peers[peerIndex].encryption = PEER_ENCRYPTION::NO_ENCRYPTION;
_peers[peerIndex].IOCap[0] = 0;
_peers[peerIndex].IOCap[1] = 0;
_peers[peerIndex].IOCap[2] = 0;

if (_peers[peerIndex].device) {
delete _peers[peerIndex].device;
@@ -807,6 +834,14 @@ void ATTClass::readByGroupReq(uint16_t connectionHandle, uint16_t mtu, uint8_t d
uint16_t endHandle;
uint16_t uuid;
} *readByGroupReq = (ReadByGroupReq*)data;
#ifdef _BLE_TRACE_
Serial.print("readByGroupReq: start: 0x");
Serial.println(readByGroupReq->startHandle,HEX);
Serial.print("readByGroupReq: end: 0x");
Serial.println(readByGroupReq->endHandle,HEX);
Serial.print("readByGroupReq: UUID: 0x");
Serial.println(readByGroupReq->uuid,HEX);
#endif

if (dlen != sizeof(ReadByGroupReq) || (readByGroupReq->uuid != BLETypeService && readByGroupReq->uuid != 0x2801)) {
sendError(connectionHandle, ATT_OP_READ_BY_GROUP_REQ, readByGroupReq->startHandle, ATT_ECODE_UNSUPP_GRP_TYPE);
@@ -819,7 +854,10 @@ void ATTClass::readByGroupReq(uint16_t connectionHandle, uint16_t mtu, uint8_t d
response[0] = ATT_OP_READ_BY_GROUP_RESP;
response[1] = 0x00;
responseLength = 2;

#ifdef _BLE_TRACE_
Serial.print("readByGroupReq: attrcount: ");
Serial.println(GATT.attributeCount());
#endif
for (uint16_t i = (readByGroupReq->startHandle - 1); i < GATT.attributeCount() && i <= (readByGroupReq->endHandle - 1); i++) {
BLELocalAttribute* attribute = GATT.attribute(i);

@@ -906,6 +944,8 @@ void ATTClass::readOrReadBlobReq(uint16_t connectionHandle, uint16_t mtu, uint8_
return;
}
}
/// if auth error, hold the response in a buffer.
bool holdResponse = false;

uint16_t handle = *(uint16_t*)data;
uint16_t offset = (opcode == ATT_OP_READ_REQ) ? 0 : *(uint16_t*)&data[sizeof(handle)];
@@ -962,6 +1002,11 @@ void ATTClass::readOrReadBlobReq(uint16_t connectionHandle, uint16_t mtu, uint8_
sendError(connectionHandle, opcode, handle, ATT_ECODE_READ_NOT_PERM);
return;
}
// If characteristic requires encryption send error & hold response until encrypted
if ((characteristic->properties() & BLEAuth) > 0 && (getPeerEncryption(connectionHandle) & PEER_ENCRYPTION::ENCRYPTED_AES)==0) {
holdResponse = true;
sendError(connectionHandle, opcode, handle, ATT_ECODE_INSUFF_ENC);
}

uint16_t valueLength = characteristic->valueLength();

@@ -994,8 +1039,12 @@ void ATTClass::readOrReadBlobReq(uint16_t connectionHandle, uint16_t mtu, uint8_
memcpy(&response[responseLength], descriptor->value() + offset, valueLength);
responseLength += valueLength;
}

HCI.sendAclPkt(connectionHandle, ATT_CID, responseLength, response);
if(holdResponse){
memcpy(holdBuffer, response, responseLength);
holdBufferSize = responseLength;
}else{
HCI.sendAclPkt(connectionHandle, ATT_CID, responseLength, response);
}
}

void ATTClass::readResp(uint16_t connectionHandle, uint8_t dlen, uint8_t data[])
@@ -1687,4 +1736,81 @@ void ATTClass::writeCmd(uint16_t connectionHandle, uint16_t handle, const uint8_
sendReq(connectionHandle, &writeReq, 3 + dataLen, NULL);
}

// Set encryption state for a peer
int ATTClass::setPeerEncryption(uint16_t connectionHandle, uint8_t encryption){
for(int i=0; i<ATT_MAX_PEERS; i++){
if(_peers[i].connectionHandle != connectionHandle){
continue;
}
_peers[i].encryption = encryption;
return 1;
}
return 0;
}
// Set the IO capabilities for a peer
int ATTClass::setPeerIOCap(uint16_t connectionHandle, uint8_t IOCap[3]){
for(int i=0; i<ATT_MAX_PEERS; i++){
if(_peers[i].connectionHandle != connectionHandle){
continue;
}
memcpy(_peers[i].IOCap, IOCap, 3);
return 1;
}
return 0;
}
// Return the connection handle for the first peer that is requesting encryption
uint16_t ATTClass::getPeerEncrptingConnectionHandle(){
for(int i=0; i<ATT_MAX_PEERS; i++){
if(_peers[i].encryption & PEER_ENCRYPTION::REQUESTED_ENCRYPTION > 0){
return _peers[i].connectionHandle;
}
}
return ATT_MAX_PEERS + 1;
}
// Get the encryption state for a particular peer / connection handle
uint8_t ATTClass::getPeerEncryption(uint16_t connectionHandle) {
for(int i=0; i<ATT_MAX_PEERS; i++){
if(_peers[i].connectionHandle != connectionHandle){continue;}
return _peers[i].encryption;
}
return 0;
}
// Get the IOCapabilities for a peer
int ATTClass::getPeerIOCap(uint16_t connectionHandle, uint8_t IOCap[3]) {
for(int i=0; i<ATT_MAX_PEERS; i++){
if(_peers[i].connectionHandle != connectionHandle){continue;}
// return _peers[i].encryption;
memcpy(IOCap, _peers[i].IOCap, 3);
}
return 0;
}
// Get the BD_ADDR for a peer
int ATTClass::getPeerAddr(uint16_t connectionHandle, uint8_t peerAddr[])
{
for(int i=0; i<ATT_MAX_PEERS; i++)
{
if(_peers[i].connectionHandle != connectionHandle){continue;}
memcpy(peerAddr, _peers[i].address,6);
return 1;
}
return 0;
}
// Get the BD_ADDR for a peer in the format needed by f6 for pairing.
int ATTClass::getPeerAddrWithType(uint16_t connectionHandle, uint8_t peerAddr[])
{
for(int i=0; i<ATT_MAX_PEERS; i++)
{
if(_peers[i].connectionHandle != connectionHandle){continue;}
for(int k=0; k<6; k++){
peerAddr[6-k] = _peers[i].address[k];
}
if(_peers[i].addressType){
peerAddr[0] = 0x01;
}else{
peerAddr[0] = 0x00;
}
return 1;
}
return 0;
}
ATTClass ATT;
23 changes: 22 additions & 1 deletion src/utility/ATT.h
Original file line number Diff line number Diff line change
@@ -25,6 +25,7 @@
#include "BLEDevice.h"

#define ATT_CID 0x0004
#define BLE_CTL 0x0008

#if defined(ARDUINO_PORTENTA_H7_M4) || defined(ARDUINO_PORTENTA_H7_M7)
#define ATT_MAX_PEERS 7
@@ -34,6 +35,16 @@
#define ATT_MAX_PEERS 3
#endif

enum PEER_ENCRYPTION {
NO_ENCRYPTION = 0,
REQUESTED_ENCRYPTION = 1 << 0,
SENT_PUBKEY = 1 << 1,
DH_KEY_CALULATED = 1 << 2,
RECEIVED_DH_CHECK = 1 << 3,
SENT_DH_CHECK = 1 << 4,
ENCRYPTED_AES = 1 << 7
};

class BLERemoteDevice;

class ATTClass {
@@ -76,7 +87,15 @@ class ATTClass {
int readReq(uint16_t connectionHandle, uint16_t handle, uint8_t responseBuffer[]);
int writeReq(uint16_t connectionHandle, uint16_t handle, const uint8_t* data, uint8_t dataLen, uint8_t responseBuffer[]);
void writeCmd(uint16_t connectionHandle, uint16_t handle, const uint8_t* data, uint8_t dataLen);

int setPeerEncryption(uint16_t connectionHandle, uint8_t encryption);
uint8_t getPeerEncryption(uint16_t connectionHandle);
uint16_t getPeerEncrptingConnectionHandle();
int getPeerAddr(uint16_t connectionHandle, uint8_t peerAddr[]);
int getPeerAddrWithType(uint16_t connectionHandle, uint8_t peerAddr[]);
int setPeerIOCap(uint16_t connectionHandle, uint8_t IOCap[]);
int getPeerIOCap(uint16_t connectionHandle, uint8_t IOCap[]);
uint8_t holdBuffer[64];
uint8_t holdBufferSize;
private:
void error(uint16_t connectionHandle, uint8_t dlen, uint8_t data[]);
void mtuReq(uint16_t connectionHandle, uint8_t dlen, uint8_t data[]);
@@ -119,6 +138,8 @@ class ATTClass {
uint8_t address[6];
uint16_t mtu;
BLERemoteDevice* device;
uint8_t encryption;
uint8_t IOCap[3];
} _peers[ATT_MAX_PEERS];

volatile bool _cnf;
Loading