Skip to content

Commit dcefa81

Browse files
author
Victor Tchistiak
committed
20190916 - rework set/reset/default pin logic
1 parent b346290 commit dcefa81

File tree

1 file changed

+29
-24
lines changed

1 file changed

+29
-24
lines changed

libraries/BluetoothSerial/src/BluetoothSerial.cpp

Lines changed: 29 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -115,9 +115,16 @@ static bool get_name_from_eir(uint8_t *eir, char *bdname, uint8_t *bdname_len)
115115
}
116116

117117
static bool btSetPin() {
118+
esp_bt_pin_type_t pin_type;
118119
if (_isPinSet) {
119-
log_i("pin set");
120-
esp_bt_pin_type_t pin_type = ESP_BT_PIN_TYPE_FIXED;
120+
if (_pin_len) {
121+
log_i("pin set");
122+
pin_type = ESP_BT_PIN_TYPE_FIXED;
123+
} else {
124+
_isPinSet = false;
125+
log_i("pin reset");
126+
pin_type = ESP_BT_PIN_TYPE_VARIABLE; // pin_code would be ignored (default)
127+
}
121128
return (esp_bt_gap_set_pin(pin_type, _pin_len, _pin_code) == ESP_OK);
122129
}
123130
return false;
@@ -294,13 +301,13 @@ static void esp_spp_cb(esp_spp_cb_event_t event, esp_spp_cb_param_t *param)
294301
case ESP_SPP_OPEN_EVT://Client connection open
295302
if (!_spp_client){
296303
_spp_client = param->open.handle;
297-
} else {
298-
secondConnectionAttempt = true;
299-
esp_spp_disconnect(param->open.handle);
300-
}
301-
xEventGroupSetBits(_spp_event_group, SPP_CONNECTED);
302-
log_i("ESP_SPP_OPEN_EVT");
303-
break;
304+
} else {
305+
secondConnectionAttempt = true;
306+
esp_spp_disconnect(param->open.handle);
307+
}
308+
xEventGroupSetBits(_spp_event_group, SPP_CONNECTED);
309+
log_i("ESP_SPP_OPEN_EVT");
310+
break;
304311
case ESP_SPP_START_EVT://server started
305312
log_i("ESP_SPP_START_EVT");
306313
break;
@@ -332,8 +339,9 @@ static void esp_bt_gap_cb(esp_bt_gap_cb_event_t event, esp_bt_gap_cb_param_t *pa
332339
esp_bt_gap_cancel_discovery();
333340
}
334341
} else if (param->disc_res.prop[i].type == ESP_BT_GAP_DEV_PROP_BDNAME) {
335-
strcpy(peer_bdname, (char *)param->disc_res.prop[i].val);
336-
peer_bdname_len = strlen(peer_bdname);
342+
peer_bdname_len = param->disc_res.prop[i].len;
343+
memcpy(peer_bdname, param->disc_res.prop[i].val, peer_bdname_len);
344+
peer_bdname[peer_bdname_len] = '\0';
337345
log_v("ESP_BT_GAP_DISC_RES_EVT : BDNAME : %s", peer_bdname);
338346
if (strlen(_remote_name) == peer_bdname_len
339347
&& strncmp(peer_bdname, _remote_name, peer_bdname_len) == 0) {
@@ -369,7 +377,8 @@ static void esp_bt_gap_cb(esp_bt_gap_cb_event_t event, esp_bt_gap_cb_param_t *pa
369377
log_i("ESP_BT_GAP_PIN_REQ_EVT min_16_digit:%d", param->pin_req.min_16_digit);
370378
if (param->pin_req.min_16_digit) {
371379
log_i("Input pin code: 0000 0000 0000 0000");
372-
esp_bt_pin_code_t pin_code = {0};
380+
esp_bt_pin_code_t pin_code;
381+
memset(pin_code, '0', ESP_BT_PIN_CODE_LEN);
373382
esp_bt_gap_pin_reply(param->pin_req.bda, true, 16, pin_code);
374383
} else {
375384
log_i("Input pin code: 1234");
@@ -637,19 +646,15 @@ void BluetoothSerial::enableSSP() {
637646
* Set default parameters for Legacy Pairing
638647
* Use fixed pin code
639648
*/
640-
bool BluetoothSerial::setPin(const char * pin) {
641-
if (pin && *pin) {
642-
int i = 0;
643-
while(*(pin + i) && i < ESP_BT_PIN_CODE_LEN) {
644-
_pin_code[i] = *(pin+i);
645-
i++;
646-
}
647-
_pin_len = i;
648-
} else if (pin){
649-
_pin_len = 0; // resetting pin to none
649+
bool BluetoothSerial::setPin(const char *pin) {
650+
bool isEmpty = !(pin && *pin);
651+
if (isEmpty && !_isPinSet) {
652+
return true; // nothing to do
653+
} else if (!isEmpty){
654+
_pin_len = strlen(pin);
655+
memcpy(_pin_code, pin, _pin_len);
650656
} else {
651-
log_e("No pin is provided");
652-
return false;
657+
_pin_len = 0; // resetting pin to none (default)
653658
}
654659
_isPinSet = true;
655660
if (isReady(false)) {

0 commit comments

Comments
 (0)