Skip to content

Commit 364e8e0

Browse files
committed
BluetoothSerial: add sec_mask, role in ::connect
1 parent 12d5e72 commit 364e8e0

File tree

2 files changed

+19
-10
lines changed

2 files changed

+19
-10
lines changed

libraries/BluetoothSerial/src/BluetoothSerial.cpp

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,8 @@ static esp_bt_pin_code_t _pin_code;
7272
static int _pin_len;
7373
static bool _isPinSet;
7474
static bool _enableSSP;
75+
static esp_spp_sec_t _sec_mask;
76+
static esp_spp_role_t _role;
7577
// start connect on ESP_SPP_DISCOVERY_COMP_EVT or save entry for getChannels
7678
static bool _doConnect;
7779
static std::map <int, std::string> sdpRecords;
@@ -359,8 +361,7 @@ static void esp_spp_cb(esp_spp_cb_event_t event, esp_spp_cb_param_t *param)
359361
param->disc_comp.scn[0]);
360362
#endif
361363
xEventGroupClearBits(_spp_event_group, SPP_CLOSED);
362-
// if(esp_spp_connect(ESP_SPP_SEC_AUTHENTICATE, ESP_SPP_ROLE_MASTER, param->disc_comp.scn[0], _peer_bd_addr) ) {
363-
if(esp_spp_connect(ESP_SPP_SEC_ENCRYPT|ESP_SPP_SEC_AUTHENTICATE, /* ESP_SPP_ROLE_SLAVE */ ESP_SPP_ROLE_MASTER, param->disc_comp.scn[0], _peer_bd_addr) != ESP_OK) {
364+
if(esp_spp_connect(_sec_mask, _role, param->disc_comp.scn[0], _peer_bd_addr) != ESP_OK) {
364365
log_e("ESP_SPP_DISCOVERY_COMP_EVT connect failed");
365366
xEventGroupSetBits(_spp_event_group, SPP_CLOSED);
366367
}
@@ -928,6 +929,8 @@ bool BluetoothSerial::connect(String remoteName)
928929
disconnect();
929930
_doConnect = true;
930931
_isRemoteAddressSet = false;
932+
_sec_mask = ESP_SPP_SEC_ENCRYPT|ESP_SPP_SEC_AUTHENTICATE;
933+
_role = ESP_SPP_ROLE_MASTER;
931934
strncpy(_remote_name, remoteName.c_str(), ESP_BT_GAP_MAX_BDNAME_LEN);
932935
_remote_name[ESP_BT_GAP_MAX_BDNAME_LEN] = 0;
933936
log_i("master : remoteName");
@@ -946,8 +949,14 @@ bool BluetoothSerial::connect(String remoteName)
946949

947950
/**
948951
* @Param channel: specify channel or 0 for auto-detect
952+
* @Param sec_mask:
953+
* ESP_SPP_SEC_ENCRYPT|ESP_SPP_SEC_AUTHENTICATE
954+
* ESP_SPP_SEC_NONE
955+
* @Param role:
956+
* ESP_SPP_ROLE_MASTER master can handle up to 7 connections to slaves
957+
* ESP_SPP_ROLE_SLAVE can only have one connection to a master
949958
*/
950-
bool BluetoothSerial::connect(uint8_t remoteAddress[], int channel)
959+
bool BluetoothSerial::connect(uint8_t remoteAddress[], int channel, esp_spp_sec_t sec_mask, esp_spp_role_t role)
951960
{
952961
if (!isReady(true, READY_TIMEOUT)) return false;
953962
if (!remoteAddress) {
@@ -958,6 +967,8 @@ bool BluetoothSerial::connect(uint8_t remoteAddress[], int channel)
958967
_doConnect = true;
959968
_remote_name[0] = 0;
960969
_isRemoteAddressSet = true;
970+
_sec_mask = sec_mask;
971+
_role = role;
961972
memcpy(_peer_bd_addr, remoteAddress, ESP_BD_ADDR_LEN);
962973
log_i("master : remoteAddress");
963974
xEventGroupClearBits(_spp_event_group, SPP_CLOSED);
@@ -966,18 +977,15 @@ bool BluetoothSerial::connect(uint8_t remoteAddress[], int channel)
966977
log_i("spp connect to remote %s channel %d",
967978
bda2str(_peer_bd_addr, bda_str, sizeof(bda_str)),
968979
channel);
969-
#warning todo: set master/slave
970-
// return esp_spp_connect(ESP_SPP_SEC_ENCRYPT|ESP_SPP_SEC_AUTHENTICATE, /* ESP_SPP_ROLE_SLAVE */ ESP_SPP_ROLE_MASTER, channel, _peer_bd_addr) == ESP_OK;
971-
// if(esp_spp_connect(ESP_SPP_SEC_NONE, /* ESP_SPP_ROLE_SLAVE */ ESP_SPP_ROLE_MASTER, channel, _peer_bd_addr) != ESP_OK ) {
972-
if(esp_spp_connect(ESP_SPP_SEC_ENCRYPT|ESP_SPP_SEC_AUTHENTICATE, /* ESP_SPP_ROLE_SLAVE */ ESP_SPP_ROLE_MASTER, channel, _peer_bd_addr) != ESP_OK ) {
980+
if(esp_spp_connect(sec_mask, role, channel, _peer_bd_addr) != ESP_OK ) {
973981
log_e("spp connect failed");
974982
return false;
975983
}
976984
bool rc=waitForConnect(READY_TIMEOUT);
977985
if(rc) {
978986
log_i("connected");
979987
} else {
980-
if(this.isClosed()) {
988+
if(this->isClosed()) {
981989
log_e("connect failed");
982990
} else {
983991
log_e("connect timed out after %dms", READY_TIMEOUT);

libraries/BluetoothSerial/src/BluetoothSerial.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,9 @@ class BluetoothSerial: public Stream
6262
void enableSSP();
6363
bool setPin(const char *pin);
6464
bool connect(String remoteName);
65-
bool connect(uint8_t remoteAddress[], int channel=0);
66-
bool connect(const BTAddress &remoteAddress, int channel=0) { return connect(*remoteAddress.getNative(), channel); };
65+
bool connect(uint8_t remoteAddress[], int channel=0, esp_spp_sec_t sec_mask=(ESP_SPP_SEC_ENCRYPT|ESP_SPP_SEC_AUTHENTICATE), esp_spp_role_t role=ESP_SPP_ROLE_MASTER);
66+
bool connect(const BTAddress &remoteAddress, int channel=0, esp_spp_sec_t sec_mask=(ESP_SPP_SEC_ENCRYPT|ESP_SPP_SEC_AUTHENTICATE), esp_spp_role_t role=ESP_SPP_ROLE_MASTER) {
67+
return connect(*remoteAddress.getNative(), channel, sec_mask); };
6768
bool connect();
6869
bool connected(int timeout=0);
6970
bool isClosed();

0 commit comments

Comments
 (0)