first pass at scanning and connecting, has some issues but so far can identify the other barback

This commit is contained in:
Morgan 'ARR\!' Allen 2021-09-10 13:48:36 -07:00
parent ad9ffa46c1
commit 0720a31280

View file

@ -327,6 +327,27 @@ static void ble_advertise(void) {
static int ble_gap_event(struct ble_gap_event *event, void *arg) { static int ble_gap_event(struct ble_gap_event *event, void *arg) {
switch (event->type) { switch (event->type) {
case BLE_GAP_EVENT_DISC: {
struct ble_gap_conn_desc desc;
struct ble_hs_adv_fields fields;
ESP_ERROR_CHECK(ble_hs_adv_parse_fields(&fields, event->disc.data, event->disc.length_data));
if(fields.name != NULL) {
char *name = (char*)malloc(fields.name_len);
memcpy(name, (uint8_t*)fields.name, fields.name_len);
name[fields.name_len] = '\0';
ESP_LOGI(TAG, "DISCOVERY: %s", name);
if(strstr(name, "BARBACK") != NULL) {
ESP_LOGI(TAG, "Let's Party!");
uint8_t addr_type = *(uint8_t*)arg;
(ble_gap_connect(addr_type, &(event->disc).addr, 30000, NULL, ble_gap_event, NULL));
}
}
return 0;
}
case BLE_GAP_EVENT_CONNECT: case BLE_GAP_EVENT_CONNECT:
/* A new connection was established or a connection attempt failed */ /* A new connection was established or a connection attempt failed */
conn_handle = event->connect.conn_handle; conn_handle = event->connect.conn_handle;
@ -402,6 +423,21 @@ static int ble_gap_event(struct ble_gap_event *event, void *arg) {
return 0; return 0;
} }
void ble_init_buddy_client() {
uint8_t addr_type;
struct ble_gap_disc_params disc_params = {
.filter_duplicates = 1,
.passive = 1,
.itvl = 0,
.window = 0,
.filter_policy = 0,
.limited = 0,
};
ESP_ERROR_CHECK(ble_hs_id_infer_auto(0, &addr_type));
ESP_ERROR_CHECK(ble_gap_disc(addr_type, BLE_HS_FOREVER, &disc_params, ble_gap_event, &addr_type));
}
static void on_sync() { static void on_sync() {
ESP_LOGI(TAG, "on_sync"); ESP_LOGI(TAG, "on_sync");
@ -415,6 +451,7 @@ static void on_sync() {
ESP_ERROR_CHECK(err); ESP_ERROR_CHECK(err);
ble_advertise(); ble_advertise();
ble_init_buddy_client();
} }
static void on_reset(int reason) { static void on_reset(int reason) {
@ -432,6 +469,7 @@ int8_t ble_send_notification(void *buf, uint8_t size) {
return ble_gattc_notify_custom(conn_handle, svc_handle_button, om); return ble_gattc_notify_custom(conn_handle, svc_handle_button, om);
} }
void ble_init(char *name) { void ble_init(char *name) {
esp_err_t err; esp_err_t err;