Skip to content

Commit 4e7a184

Browse files
committed
feat: add reset every conns
1 parent 5dd3581 commit 4e7a184

File tree

6 files changed

+44
-7
lines changed

6 files changed

+44
-7
lines changed

pgpemu-esp32/main/pgp_gap.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ extern uint8_t adv_config_done;
1212
// start BT advertising if we have fewer connections than configured
1313
void advertise_if_needed();
1414

15+
// close every connections
16+
void pgp_disconnect();
17+
1518
// explicitly start BT advertising
1619
void pgp_advertise();
1720

pgpemu-esp32/main/pgp_gatts.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -712,6 +712,7 @@ void gatts_profile_event_handler(esp_gatts_cb_event_t event,
712712
// start sent the update connection parameters to the peer device.
713713
esp_ble_gap_update_conn_params(&conn_params);
714714

715+
set_remote_bda(param->connect.conn_id, conn_params.bda);
715716
esp_ble_set_encryption(param->connect.remote_bda, ESP_BLE_SEC_ENCRYPT_MITM);
716717
break;
717718
case ESP_GATTS_DISCONNECT_EVT:

pgpemu-esp32/main/pgp_handshake_multi.c

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#include "pgp_handshake_multi.h"
22

3+
#include "esp_bt_defs.h"
4+
#include "esp_gap_ble_api.h"
35
#include "esp_log.h"
46
#include "log_tags.h"
57

@@ -79,6 +81,15 @@ int get_cert_state(uint16_t conn_id) {
7981
return entry->cert_state;
8082
}
8183

84+
void set_remote_bda(uint16_t conn_id, esp_bd_addr_t remote_bda) {
85+
client_state_t* entry = get_or_create_client_state_entry(conn_id);
86+
if (!entry) {
87+
ESP_LOGE(HANDSHAKE_TAG, "set_remote_bda: conn_id %d unknown", conn_id);
88+
return;
89+
}
90+
memcpy(entry->remote_bda, remote_bda, sizeof(esp_bd_addr_t));
91+
}
92+
8293
void connection_start(uint16_t conn_id) {
8394
active_connections++;
8495

@@ -170,3 +181,16 @@ void dump_client_states() {
170181
dump_client_state(i, &client_states[i]);
171182
}
172183
}
184+
185+
186+
void reset_client_states() {
187+
ESP_LOGI(HANDSHAKE_TAG, "active_connections: %d", active_connections);
188+
for (int i = 0; i < MAX_CONNECTIONS; i++) {
189+
// make sure it's not an empty slot
190+
if (conn_id_map[i] != 0xffff) {
191+
ESP_LOGI(HANDSHAKE_TAG, "disconnecting %d", i);
192+
esp_ble_gap_disconnect(client_states[i].remote_bda);
193+
connection_stop(client_states[i].conn_id);
194+
}
195+
}
196+
}

pgpemu-esp32/main/pgp_handshake_multi.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#ifndef PGP_HANDSHAKE_MULTI_H
22
#define PGP_HANDSHAKE_MULTI_H
33

4+
#include "esp_bt_defs.h"
45
#include "freertos/FreeRTOS.h"
56
#include "freertos/FreeRTOSConfig.h"
67
#include "freertos/task.h"
@@ -14,6 +15,7 @@ static const size_t CERT_BUFFER_LEN = 378;
1415
typedef struct {
1516
// esp bt connection id
1617
uint16_t conn_id;
18+
esp_bd_addr_t remote_bda;
1719
int cert_state;
1820
// TODO: we probably need to save the remote mac address so that we associate a reconnecting
1921
// client with its previous client state
@@ -44,8 +46,11 @@ client_state_t* get_client_state_entry(uint16_t conn_id);
4446
client_state_t* get_or_create_client_state_entry(uint16_t conn_id);
4547

4648
int get_cert_state(uint16_t conn_id);
49+
void set_remote_bda(uint16_t conn_id, esp_bd_addr_t remote_bda);
4750

4851
void dump_client_states();
52+
void reset_client_states();
53+
4954

5055
void connection_start(uint16_t conn_id);
5156
void connection_update(uint16_t conn_id);

pgpemu-esp32/main/stats.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,12 @@ void increment_spin(uint16_t conn_id) {
6565
}
6666

6767
void stats_get_runtime() {
68+
if (stats_len == 0) {
69+
ESP_LOGI(STATS_TAG, "no stats found");
70+
71+
return;
72+
}
73+
6874
for (size_t i = 0; i < stats_len; i++) {
6975
ESP_LOGI(STATS_TAG,
7076
"---STATS %d---\n"

pgpemu-esp32/main/uart.c

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -79,12 +79,11 @@ void process_char(uint8_t c) {
7979
"Hardware:\n"
8080
"- B - toggle input button\n"
8181
"Secrets:\n"
82-
"- x? - help\n"
8382
"- xq - leave secrets mode\n"
83+
"- xl - list slots\n"
84+
"- xc - clear slot\n"
8485
"- x... - select secrets slot\n"
8586
"- ! - activate selected slot and restart\n"
86-
"- l - list slots\n"
87-
"- C - clear slot\n"
8887
"Bluetooth:\n"
8988
"- bA - start advertising\n"
9089
"- ba - stop advertising\n"
@@ -285,13 +284,13 @@ static void uart_secrets_handler() {
285284
state = buf[0];
286285
ESP_LOGW(UART_TAG, "set=%c", state);
287286
break;
288-
case 'C':
287+
case 'c':
289288
// clear secret
290289
if (slot_chosen) {
291290
ESP_LOGW(UART_TAG, "clear=%d", delete_secrets_id(chosen_slot));
292291
}
293292
break;
294-
case 'W':
293+
case 'w':
295294
// write secret
296295
if (slot_chosen) {
297296
ESP_LOGW(UART_TAG,
@@ -431,17 +430,16 @@ static void uart_bluetooth_handler() {
431430

432431
switch (buf) {
433432
case 'A':
434-
ESP_LOGI(UART_TAG, "starting advertising");
435433
pgp_advertise();
436434
break;
437435
case 'a':
438-
ESP_LOGI(UART_TAG, "stopping advertising");
439436
pgp_advertise_stop();
440437
break;
441438
case 's':
442439
dump_client_states();
443440
break;
444441
case 'r':
442+
reset_client_states();
445443
break;
446444
case '1':
447445
case '2':

0 commit comments

Comments
 (0)