From 48b10035c50ff1161fbd4a3a0c0ee04d1ee9198f Mon Sep 17 00:00:00 2001 From: Andrei Emeltchenko Date: Tue, 9 Feb 2016 12:22:21 +0200 Subject: [PATCH] drivers/nble: Add basic connection handling support Add connection handling skeleton keeping interfaces similar to net/bluetooth and putting internals to private conn_internal.h header. ... bt: rpc_deserialize (0xa8008924): on_ble_gap_connect_evt bt: on_ble_gap_connect_evt (0xa8008924): handle 3 ... bt: rpc_deserialize (0xa8008924): on_ble_gap_conn_update_evt bt: bt_conn_ref (0xa8008924): handle 3 ref 2 bt: bt_conn_unref (0xa8008924): handle 3 ref 1 ... bt: rpc_deserialize (0xa8008924): on_ble_gap_disconnect_evt bt: bt_conn_ref (0xa8008924): handle 3 ref 2 bt: on_ble_gap_disconnect_evt (0xa8008924): conn 0xa80080c4 handle 3 bt: bt_conn_unref (0xa8008924): handle 3 ref 1 bt: bt_conn_unref (0xa8008924): handle 3 ref 0 ... Change-Id: I1885ecad5c57323b7d5a68be0c63d98dc65350b3 Signed-off-by: Andrei Emeltchenko --- drivers/nble/conn.c | 117 ++++++++++++++++++++++++++++++++++- drivers/nble/conn_internal.h | 20 ++++++ drivers/nble/gap.c | 16 ----- 3 files changed, 136 insertions(+), 17 deletions(-) create mode 100644 drivers/nble/conn_internal.h diff --git a/drivers/nble/conn.c b/drivers/nble/conn.c index ac16548f2c7..fabbd9e520a 100644 --- a/drivers/nble/conn.c +++ b/drivers/nble/conn.c @@ -16,16 +16,83 @@ #include +#include +#include + #include #include +#include + +#include "gap_internal.h" +#include "conn_internal.h" + +static struct bt_conn conns[CONFIG_BLUETOOTH_MAX_CONN]; + +static struct bt_conn *conn_new(void) +{ + struct bt_conn *conn = NULL; + int i; + + for (i = 0; i < ARRAY_SIZE(conns); i++) { + if (!atomic_get(&conns[i].ref)) { + conn = &conns[i]; + break; + } + } + + if (!conn) { + return NULL; + } + + memset(conn, 0, sizeof(*conn)); + + atomic_set(&conn->ref, 1); + + return conn; +} + +static struct bt_conn *bt_conn_add_le(const bt_addr_le_t *peer) +{ + struct bt_conn *conn = conn_new(); + + if (!conn) { + return NULL; + } + + return conn; +} struct bt_conn *bt_conn_ref(struct bt_conn *conn) { - return NULL; + atomic_inc(&conn->ref); + + BT_DBG("handle %u ref %u", conn->handle, atomic_get(&conn->ref)); + + return conn; } void bt_conn_unref(struct bt_conn *conn) { + atomic_dec(&conn->ref); + + BT_DBG("handle %u ref %u", conn->handle, atomic_get(&conn->ref)); +} + +struct bt_conn *bt_conn_lookup_handle(uint16_t handle) +{ + int i; + + for (i = 0; i < ARRAY_SIZE(conns); i++) { + if (!atomic_get(&conns[i].ref)) { + continue; + } + + if (conns[i].handle == handle) { + return bt_conn_ref(&conns[i]); + } + } + + return NULL; } struct bt_conn *bt_conn_lookup_addr_le(const bt_addr_le_t *peer) @@ -105,3 +172,51 @@ int bt_conn_auth_passkey_confirm(struct bt_conn *conn, bool match) { return -ENOSYS; } + +/* Connection related events */ + +void on_ble_gap_connect_evt(const struct ble_gap_connect_evt *ev) +{ + struct bt_conn *conn; + + BT_DBG("handle %u", ev->conn_handle); + + conn = bt_conn_add_le(&ev->peer_bda); + if (!conn) { + BT_ERR("Unable to create conn"); + return; + } + + conn->handle = ev->conn_handle; +} + +void on_ble_gap_disconnect_evt(const struct ble_gap_disconnect_evt *ev) +{ + struct bt_conn *conn; + + conn = bt_conn_lookup_handle(ev->conn_handle); + if (!conn) { + BT_ERR("Unable to find conn for handle %u", ev->conn_handle); + return; + } + + BT_DBG("conn %p handle %u", conn, ev->conn_handle); + + bt_conn_unref(conn); + bt_conn_unref(conn); +} + +void on_ble_gap_conn_update_evt(const struct ble_gap_conn_update_evt *ev) +{ + struct bt_conn *conn; + + conn = bt_conn_lookup_handle(ev->conn_handle); + if (!conn) { + BT_ERR("Unable to find conn for handle %u", ev->conn_handle); + return; + } + + BT_DBG("conn %p handle %u", conn, ev->conn_handle); + + bt_conn_unref(conn); +} diff --git a/drivers/nble/conn_internal.h b/drivers/nble/conn_internal.h new file mode 100644 index 00000000000..f2bfd0532f3 --- /dev/null +++ b/drivers/nble/conn_internal.h @@ -0,0 +1,20 @@ +/* + * Copyright (c) 2016 Intel Corporation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +struct bt_conn { + uint16_t handle; + atomic_t ref; +}; diff --git a/drivers/nble/gap.c b/drivers/nble/gap.c index 5cdb406268a..4f5cde1b111 100644 --- a/drivers/nble/gap.c +++ b/drivers/nble/gap.c @@ -256,22 +256,6 @@ void ble_log(const struct ble_log_s *param, char *format, uint8_t len) #endif } -void on_ble_gap_connect_evt(const struct ble_gap_connect_evt *ev) -{ - BT_DBG("handle %u", ev->conn_handle); -} - -void on_ble_gap_disconnect_evt(const struct ble_gap_disconnect_evt *ev) -{ - BT_DBG("handle %u", ev->conn_handle); -} - -void on_ble_gap_conn_update_evt(const struct ble_gap_conn_update_evt *ev) -{ - BT_DBG("handle %u interval %u", ev->conn_handle, - ev->conn_values.interval); -} - void on_ble_gap_sm_status_evt(const struct ble_gap_sm_status_evt *ev) { BT_DBG("");