more work on connection establishment
This commit is contained in:
parent
7fb2040173
commit
da0252fb53
4 changed files with 60 additions and 0 deletions
|
|
@ -29,6 +29,7 @@ uint8_t baros_init(baros_cfg_t *cfg);
|
|||
▐░█▄▄▄▄▄▄▄█░▌▐░▌ ▐░▌▐░▌ ▐░▌ ▐░█▄▄▄▄▄▄▄█░▌▐░▌ ▐░▌▐░█▄▄▄▄▄▄▄▄▄ ▐░▌ ▐░▌\n\
|
||||
▐░░░░░░░░░░▌ ▐░▌ ▐░▌▐░▌ ▐░▌▐░░░░░░░░░░▌ ▐░▌ ▐░▌▐░░░░░░░░░░░▌▐░▌ ▐░▌\n\
|
||||
▀▀▀▀▀▀▀▀▀▀ ▀ ▀ ▀ ▀ ▀▀▀▀▀▀▀▀▀▀ ▀ ▀ ▀▀▀▀▀▀▀▀▀▀▀ ▀ ▀\n\
|
||||
\n\n\
|
||||
"
|
||||
|
||||
#define BARHEAD_BANNER "\n\
|
||||
|
|
@ -39,6 +40,7 @@ uint8_t baros_init(baros_cfg_t *cfg);
|
|||
░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░ ░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░░▒▓█▓▒░ \n\
|
||||
░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░ ░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░░▒▓█▓▒░ \n\
|
||||
░▒▓███████▓▒░░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░░▒▓█▓▒░▒▓████████▓▒░▒▓█▓▒░░▒▓█▓▒░▒▓███████▓▒░ \n\
|
||||
\n\n\
|
||||
"
|
||||
|
||||
#endif
|
||||
|
|
|
|||
|
|
@ -34,6 +34,7 @@ ESP_EVENT_DECLARE_BASE(EVENT_BAROS_BLE);
|
|||
typedef enum {
|
||||
BAROS_BLE_DISC,
|
||||
BAROS_BLE_CONNECT,
|
||||
BAROS_BLE_CONNECT_BUDDY,
|
||||
} baros_ble_event_t;
|
||||
|
||||
typedef struct {
|
||||
|
|
|
|||
|
|
@ -99,6 +99,8 @@ void *baros_list_find_node(baros_list_node_t *list, baros_list_find_element_call
|
|||
n = n->next;
|
||||
}
|
||||
|
||||
ESP_LOGW(TAG, "Failed to find node");
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
|
@ -109,5 +111,7 @@ void *baros_list_find_element(baros_list_node_t *list, baros_list_find_element_c
|
|||
return n->element;
|
||||
}
|
||||
|
||||
ESP_LOGW(TAG, "Failed to find element");
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -41,7 +41,9 @@ static int svc_access_system(uint16_t conn_handle, uint16_t attr_handle, struct
|
|||
|
||||
static int barback_ble_char_access(uint16_t conn_handle, uint16_t attr_handle, struct ble_gatt_access_ctxt *ctxt, void *arg);
|
||||
|
||||
// Track discovevies
|
||||
baros_list_node_t *peer_list = NULL;
|
||||
// Connection config tracked from _connect(..) to EVENT_ESTAB
|
||||
baros_ble_connect_cfg_t *connect_attempt = NULL;
|
||||
|
||||
static const struct ble_gatt_svc_def service_defs[] = {
|
||||
|
|
@ -233,6 +235,14 @@ static int svc_access_name(uint16_t conn_handle, uint16_t attr_handle, struct bl
|
|||
return 0;
|
||||
}
|
||||
|
||||
|
||||
bool find_peer_by_addr(void *element, void *arg) {
|
||||
ble_addr_t *addr = arg;
|
||||
baros_ble_discovery_t *disco = element;
|
||||
|
||||
return memcmp(disco->addr.val, addr->val, 6) == 0;
|
||||
}
|
||||
|
||||
static int ble_gap_event(struct ble_gap_event *event, void *arg) {
|
||||
switch (event->type) {
|
||||
case BLE_GAP_EVENT_DISC: {
|
||||
|
|
@ -281,6 +291,7 @@ static int ble_gap_event(struct ble_gap_event *event, void *arg) {
|
|||
/* A new connection was established or a connection attempt failed */
|
||||
conn_handle = event->connect.conn_handle;
|
||||
|
||||
/*
|
||||
ESP_LOGI(TAG, "connection %s; conn_handle: %d status=%d\n",
|
||||
event->connect.status == 0 ? "established" : "failed",
|
||||
conn_handle,
|
||||
|
|
@ -294,6 +305,7 @@ static int ble_gap_event(struct ble_gap_event *event, void *arg) {
|
|||
ESP_LOGI(TAG, "addr: %p arg: %p", ((baros_ble_discovery_t*)connect_attempt->arg)->addr, connect_attempt->arg);
|
||||
connect_attempt->cb(((baros_ble_discovery_t*)connect_attempt->arg)->addr, connect_attempt->arg);
|
||||
}
|
||||
*/
|
||||
|
||||
break;
|
||||
|
||||
|
|
@ -306,6 +318,47 @@ static int ble_gap_event(struct ble_gap_event *event, void *arg) {
|
|||
|
||||
break;
|
||||
|
||||
case BLE_GAP_EVENT_LINK_ESTAB:
|
||||
conn_handle = event->connect.conn_handle;
|
||||
|
||||
ESP_LOGI(TAG, "connection %s; conn_handle: %d status=%d\n",
|
||||
event->connect.status == 0 ? "established" : "failed",
|
||||
conn_handle,
|
||||
event->connect.status
|
||||
);
|
||||
|
||||
// always keep advertising, maybe?
|
||||
// ble_advertise();
|
||||
|
||||
if(!connect_attempt) {
|
||||
struct ble_gap_conn_desc *conn_desc = malloc(sizeof(struct ble_gap_conn_desc));
|
||||
ble_gap_conn_find(conn_handle, conn_desc);
|
||||
|
||||
baros_ble_discovery_t *disco = baros_list_find_element(peer_list, find_peer_by_addr, &conn_desc->peer_id_addr);
|
||||
|
||||
if(disco == NULL) {
|
||||
ESP_LOGW(TAG, "Incoming connection from undiscovered peer");
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
esp_event_post(EVENT_BAROS_BLE, BAROS_BLE_CONNECT_BUDDY, disco, sizeof(baros_ble_discovery_t), portMAX_DELAY);
|
||||
|
||||
ESP_LOGI(TAG, "Connect attempt from: " MACSTR, MAC2STR(conn_desc->peer_id_addr.val));
|
||||
ESP_LOGI(TAG, "Name: %s", disco->name);
|
||||
|
||||
} else if(connect_attempt->cb != NULL) {
|
||||
ESP_LOGI(TAG, "addr: %p arg: %p", ((baros_ble_discovery_t*)connect_attempt->arg)->addr, connect_attempt->arg);
|
||||
connect_attempt->cb(((baros_ble_discovery_t*)connect_attempt->arg)->addr, connect_attempt->arg);
|
||||
}
|
||||
|
||||
// TODO
|
||||
// subscribe to mutual services,
|
||||
// emit blobal connection event for
|
||||
// addition actions
|
||||
|
||||
break;
|
||||
|
||||
case BLE_GAP_EVENT_ADV_COMPLETE:
|
||||
ESP_LOGI(TAG, "adv complete\n");
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue