2015-07-02 04:47:13 +08:00
|
|
|
/** @file
|
2015-06-15 16:15:01 +08:00
|
|
|
* @brief Bluetooth connection handling
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright (c) 2015 Intel Corporation
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions are met:
|
|
|
|
*
|
|
|
|
* 1) Redistributions of source code must retain the above copyright notice,
|
|
|
|
* this list of conditions and the following disclaimer.
|
|
|
|
*
|
|
|
|
* 2) Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
* this list of conditions and the following disclaimer in the documentation
|
|
|
|
* and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* 3) Neither the name of Intel Corporation nor the names of its contributors
|
|
|
|
* may be used to endorse or promote products derived from this software without
|
|
|
|
* specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
|
|
|
|
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
#ifndef __BT_CONN_H
|
|
|
|
#define __BT_CONN_H
|
|
|
|
|
2015-09-14 23:10:38 +08:00
|
|
|
#if defined(CONFIG_BLUETOOTH_CENTRAL) || defined(CONFIG_BLUETOOTH_PERIPHERAL)
|
2015-07-13 18:43:02 +08:00
|
|
|
#include <stdbool.h>
|
|
|
|
|
2015-06-17 17:47:06 +08:00
|
|
|
#include <bluetooth/hci.h>
|
|
|
|
|
2015-07-02 04:47:13 +08:00
|
|
|
/** Opaque type representing a connection to a remote device */
|
2015-06-15 16:15:01 +08:00
|
|
|
struct bt_conn;
|
|
|
|
|
2015-07-02 04:47:13 +08:00
|
|
|
/** @brief Increment a connection's reference count.
|
2015-06-18 15:51:01 +08:00
|
|
|
*
|
|
|
|
* Increment the reference count of a connection object.
|
|
|
|
*
|
|
|
|
* @param conn Connection object.
|
|
|
|
*
|
|
|
|
* @return Connection object with incremented reference count.
|
|
|
|
*/
|
2015-06-15 16:15:01 +08:00
|
|
|
struct bt_conn *bt_conn_get(struct bt_conn *conn);
|
|
|
|
|
2015-07-02 04:47:13 +08:00
|
|
|
/** @brief Decrement a connection's reference count.
|
2015-06-18 15:51:01 +08:00
|
|
|
*
|
|
|
|
* Decrement the reference count of a connection object.
|
|
|
|
*
|
|
|
|
* @param conn Connection object.
|
2015-07-02 05:22:39 +08:00
|
|
|
*/
|
2015-06-15 16:15:01 +08:00
|
|
|
void bt_conn_put(struct bt_conn *conn);
|
|
|
|
|
2015-07-02 04:47:13 +08:00
|
|
|
/** @brief Look up an existing connection by address.
|
2015-06-18 15:51:01 +08:00
|
|
|
*
|
|
|
|
* Look up an existing connection based on the remote address.
|
|
|
|
*
|
|
|
|
* @param peer Remote address.
|
|
|
|
*
|
|
|
|
* @return Connection object or NULL if not found. The caller gets a
|
|
|
|
* new reference to the connection object which must be released with
|
|
|
|
* bt_conn_put() once done using the object.
|
|
|
|
*/
|
2015-06-17 17:47:06 +08:00
|
|
|
struct bt_conn *bt_conn_lookup_addr_le(const bt_addr_le_t *peer);
|
|
|
|
|
2015-07-02 04:47:13 +08:00
|
|
|
/** @brief Get destination (peer) address of a connection.
|
2015-06-18 15:36:59 +08:00
|
|
|
*
|
|
|
|
* @param conn Connection object.
|
|
|
|
*
|
|
|
|
* @return Destination address.
|
|
|
|
*/
|
|
|
|
const bt_addr_le_t *bt_conn_get_dst(const struct bt_conn *conn);
|
|
|
|
|
2015-07-15 17:31:08 +08:00
|
|
|
/** @brief Disconnect from a remote device or cancel pending connection.
|
|
|
|
*
|
|
|
|
* Disconnect an active connection with the specified reason code or cancel
|
|
|
|
* pending outgoing connection.
|
|
|
|
*
|
|
|
|
* @param conn Connection to disconnect.
|
|
|
|
* @param reason Reason code for the disconnection.
|
|
|
|
*
|
|
|
|
* @return Zero on success or (negative) error code on failure.
|
|
|
|
*/
|
|
|
|
int bt_conn_disconnect(struct bt_conn *conn, uint8_t reason);
|
|
|
|
|
2015-09-14 23:10:38 +08:00
|
|
|
#if defined(CONFIG_BLUETOOTH_CENTRAL)
|
2015-07-15 18:55:32 +08:00
|
|
|
/** @brief Initiate an LE connection to a remote device.
|
|
|
|
*
|
|
|
|
* Allows initiate new LE link to remote peer using its address.
|
|
|
|
* Returns a new reference that the the caller is responsible for managing.
|
|
|
|
*
|
|
|
|
* @param peer Remote address.
|
|
|
|
*
|
|
|
|
* @return Valid connection object on success or NULL otherwise.
|
|
|
|
*/
|
|
|
|
struct bt_conn *bt_conn_create_le(const bt_addr_le_t *peer);
|
2015-09-14 23:10:38 +08:00
|
|
|
#endif
|
2015-07-15 18:55:32 +08:00
|
|
|
|
2015-08-05 22:17:29 +08:00
|
|
|
/** Security level. */
|
|
|
|
typedef enum {
|
|
|
|
BT_SECURITY_LOW, /** No encryption and no authentication. */
|
|
|
|
BT_SECURITY_MEDIUM, /** encryption and no authentication (no MITM). */
|
|
|
|
BT_SECURITY_HIGH, /** encryption and authentication (MITM). */
|
|
|
|
BT_SECURITY_FIPS, /** Authenticated LE Secure Connections and
|
|
|
|
* encryption.
|
|
|
|
*/
|
|
|
|
} bt_security_t;
|
|
|
|
|
2015-09-14 23:10:38 +08:00
|
|
|
#if defined(CONFIG_BLUETOOTH_SMP)
|
2015-07-15 19:21:37 +08:00
|
|
|
/** @brief Set security level for a connection.
|
|
|
|
*
|
|
|
|
* This function enable security (encryption) for a connection. If device is
|
|
|
|
* already paired with sufficiently strong key encryption will be enabled. If
|
|
|
|
* link is already encrypted with sufficiently strong key this function does
|
|
|
|
* nothing.
|
|
|
|
*
|
|
|
|
* If device is not paired pairing will be initiated. If device is paired and
|
|
|
|
* keys are too weak but input output capabilities allow for strong enough keys
|
|
|
|
* pairing will be initiated.
|
|
|
|
*
|
|
|
|
* This function may return error if required level of security is not possible
|
|
|
|
* to achieve due to local or remote device limitation (eg input output
|
|
|
|
* capabilities).
|
|
|
|
*
|
|
|
|
* @param conn Connection object.
|
|
|
|
* @param sec Requested security level.
|
|
|
|
*
|
|
|
|
* @return 0 on success or negative error
|
|
|
|
*/
|
|
|
|
int bt_conn_security(struct bt_conn *conn, bt_security_t sec);
|
2015-09-14 23:10:38 +08:00
|
|
|
#endif
|
2015-07-15 19:21:37 +08:00
|
|
|
|
2015-07-02 04:47:13 +08:00
|
|
|
/** Connection callback structure */
|
2015-06-15 16:15:01 +08:00
|
|
|
struct bt_conn_cb {
|
2015-06-15 16:23:04 +08:00
|
|
|
void (*connected)(struct bt_conn *conn);
|
|
|
|
void (*disconnected)(struct bt_conn *conn);
|
2015-09-14 23:10:38 +08:00
|
|
|
#if defined(CONFIG_BLUETOOTH_SMP)
|
2015-09-02 00:06:35 +08:00
|
|
|
void (*identity_resolved)(struct bt_conn *conn,
|
|
|
|
const bt_addr_le_t *rpa,
|
|
|
|
const bt_addr_le_t *identity);
|
2015-09-02 00:16:54 +08:00
|
|
|
void (*security_changed)(struct bt_conn *conn, bt_security_t level);
|
2015-09-14 23:10:38 +08:00
|
|
|
#endif
|
2015-06-15 16:15:01 +08:00
|
|
|
struct bt_conn_cb *_next;
|
|
|
|
};
|
|
|
|
|
2015-07-02 04:47:13 +08:00
|
|
|
/** @brief Register connection callbacks.
|
2015-06-18 15:51:01 +08:00
|
|
|
*
|
|
|
|
* Register callbacks to monitor the state of connections.
|
|
|
|
*
|
|
|
|
* @param cb Callback struct.
|
|
|
|
*/
|
2015-06-15 16:15:01 +08:00
|
|
|
void bt_conn_cb_register(struct bt_conn_cb *cb);
|
|
|
|
|
2015-09-14 23:10:38 +08:00
|
|
|
#if defined(CONFIG_BLUETOOTH_CENTRAL)
|
2015-07-13 18:43:02 +08:00
|
|
|
/** @brief Automatically connect to remote device if it's in range.
|
|
|
|
*
|
|
|
|
* This function enables/disables automatic connection initiation.
|
|
|
|
* Everytime the device looses the connection with peer, this connection
|
|
|
|
* will be re-established if connectable advertisement from peer is received.
|
|
|
|
*
|
|
|
|
* @param conn Existing connection object.
|
|
|
|
* @param auto_conn boolean value. If true, auto connect is enabled,
|
|
|
|
* if false, auto connect is disabled.
|
|
|
|
*
|
|
|
|
* @return none
|
|
|
|
*/
|
|
|
|
void bt_conn_set_auto_conn(struct bt_conn *conn, bool auto_conn);
|
|
|
|
|
2015-09-14 23:10:38 +08:00
|
|
|
#endif /* CONFIG_BLUETOOTH_CENTRAL */
|
|
|
|
#endif /* CONFIG_BLUETOOTH_CENTRAL || CONFIG_BLUETOOTH_PERIPHERAL */
|
2015-06-15 16:15:01 +08:00
|
|
|
#endif /* __BT_CONN_H */
|