ble_gatt_client
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros
att.c File Reference

att protocol implementation More...

#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include "io.h"
#include "queue.h"
#include "util.h"
#include "timeout.h"
#include "bluetooth.h"
#include "uuid.h"
#include "att.h"
#include "crypto.h"

Go to the source code of this file.

Data Structures

struct  bt_att
 
struct  sign_info
 
struct  att_send_op
 
struct  att_notify
 
struct  att_disconn
 
struct  timeout_data
 
struct  notify_data
 

Macros

#define ATT_MIN_PDU_LEN   1 /* At least 1 byte for the opcode. */
 
#define ATT_OP_CMD_MASK   0x40
 
#define ATT_OP_SIGNED_MASK   0x80
 
#define ATT_TIMEOUT_INTERVAL   30000 /* 30000 ms */
 
#define BT_ATT_SIGNATURE_LEN   12
 

Enumerations

enum  att_op_type {
  ATT_OP_TYPE_REQ, ATT_OP_TYPE_RSP, ATT_OP_TYPE_CMD, ATT_OP_TYPE_IND,
  ATT_OP_TYPE_NOT, ATT_OP_TYPE_CONF, ATT_OP_TYPE_UNKNOWN
}
 

Functions

static enum att_op_type get_op_type (uint8_t opcode)
 
static uint8_t get_req_opcode (uint8_t rsp_opcode)
 
static void destroy_att_send_op (void *data)
 destroy att send operation calls the destroy callback with user_data as an argument free pdu data More...
 
static void cancel_att_send_op (struct att_send_op *op)
 
static void destroy_att_notify (void *data)
 
static bool match_notify_id (const void *a, const void *b)
 
static void destroy_att_disconn (void *data)
 
static bool match_disconn_id (const void *a, const void *b)
 
static bool encode_pdu (struct bt_att *att, struct att_send_op *op, const void *pdu, uint16_t length)
 
static struct att_send_opcreate_att_send_op (struct bt_att *att, uint8_t opcode, const void *pdu, uint16_t length, bt_att_response_func_t callback, void *user_data, bt_att_destroy_func_t destroy)
 
static struct att_send_oppick_next_send_op (struct bt_att *att)
 
static bool timeout_cb (void *user_data)
 
static void write_watch_destroy (void *user_data)
 
static bool can_write_data (struct io *io, void *user_data)
 
static void wakeup_writer (struct bt_att *att)
 
static void disconn_handler (void *data, void *user_data)
 
static bool disconnect_cb (struct io *io, void *user_data)
 
static bool change_security (struct bt_att *att, uint8_t ecode)
 
static bool handle_error_rsp (struct bt_att *att, uint8_t *pdu, ssize_t pdu_len, uint8_t *opcode)
 
static void handle_rsp (struct bt_att *att, uint8_t opcode, uint8_t *pdu, ssize_t pdu_len)
 
static void handle_conf (struct bt_att *att, uint8_t *pdu, ssize_t pdu_len)
 
static bool opcode_match (uint8_t opcode, uint8_t test_opcode)
 
static void respond_not_supported (struct bt_att *att, uint8_t opcode)
 
static bool handle_signed (struct bt_att *att, uint8_t opcode, uint8_t *pdu, ssize_t pdu_len)
 
static void handle_notify (struct bt_att *att, uint8_t opcode, uint8_t *pdu, ssize_t pdu_len)
 
static bool can_read_data (struct io *io, void *user_data)
 
static bool is_io_l2cap_based (int fd)
 
static void bt_att_free (struct bt_att *att)
 
struct bt_attbt_att_new (int fd, bool ext_signed)
 
struct bt_attbt_att_ref (struct bt_att *att)
 
void bt_att_unref (struct bt_att *att)
 
bool bt_att_set_close_on_unref (struct bt_att *att, bool do_close)
 
int bt_att_get_fd (struct bt_att *att)
 
bool bt_att_set_debug (struct bt_att *att, bt_att_debug_func_t callback, void *user_data, bt_att_destroy_func_t destroy)
 
uint16_t bt_att_get_mtu (struct bt_att *att)
 
bool bt_att_set_mtu (struct bt_att *att, uint16_t mtu)
 
bool bt_att_set_timeout_cb (struct bt_att *att, bt_att_timeout_func_t callback, void *user_data, bt_att_destroy_func_t destroy)
 
unsigned int bt_att_register_disconnect (struct bt_att *att, bt_att_disconnect_func_t callback, void *user_data, bt_att_destroy_func_t destroy)
 
bool bt_att_unregister_disconnect (struct bt_att *att, unsigned int id)
 
unsigned int bt_att_send (struct bt_att *att, uint8_t opcode, const void *pdu, uint16_t length, bt_att_response_func_t callback, void *user_data, bt_att_destroy_func_t destroy)
 
static bool match_op_id (const void *a, const void *b)
 
bool bt_att_cancel (struct bt_att *att, unsigned int id)
 
bool bt_att_cancel_all (struct bt_att *att)
 
static uint8_t att_ecode_from_error (int err)
 
unsigned int bt_att_send_error_rsp (struct bt_att *att, uint8_t opcode, uint16_t handle, int error)
 
unsigned int bt_att_register (struct bt_att *att, uint8_t opcode, bt_att_notify_func_t callback, void *user_data, bt_att_destroy_func_t destroy)
 
bool bt_att_unregister (struct bt_att *att, unsigned int id)
 
bool bt_att_unregister_all (struct bt_att *att)
 
int bt_att_get_security (struct bt_att *att)
 
bool bt_att_set_security (struct bt_att *att, int level)
 
static bool sign_set_key (struct sign_info **sign, uint8_t key[16], bt_att_counter_func_t func, void *user_data)
 
bool bt_att_set_local_key (struct bt_att *att, uint8_t sign_key[16], bt_att_counter_func_t func, void *user_data)
 
bool bt_att_set_remote_key (struct bt_att *att, uint8_t sign_key[16], bt_att_counter_func_t func, void *user_data)
 
bool bt_att_has_crypto (struct bt_att *att)
 

Variables

struct {
   uint8_t   opcode
 
   enum att_op_type   type
 
att_opcode_type_table []
 
struct {
   uint8_t   req_opcode
 
   uint8_t   rsp_opcode
 
att_req_rsp_mapping_table []
 

Detailed Description

att protocol implementation

Author
Gilbert Brault
See also
gatt-helpers.c for pdu creation

Definition in file att.c.

Macro Definition Documentation

#define ATT_MIN_PDU_LEN   1 /* At least 1 byte for the opcode. */

Definition at line 51 of file att.c.

Referenced by can_read_data().

#define ATT_OP_CMD_MASK   0x40

Definition at line 52 of file att.c.

#define ATT_OP_SIGNED_MASK   0x80

Definition at line 53 of file att.c.

Referenced by encode_pdu(), and handle_notify().

#define ATT_TIMEOUT_INTERVAL   30000 /* 30000 ms */

Definition at line 54 of file att.c.

Referenced by can_write_data().

#define BT_ATT_SIGNATURE_LEN   12

Definition at line 57 of file att.c.

Referenced by encode_pdu(), handle_notify(), and handle_signed().

Enumeration Type Documentation

Enumerator
ATT_OP_TYPE_REQ 
ATT_OP_TYPE_RSP 
ATT_OP_TYPE_CMD 
ATT_OP_TYPE_IND 
ATT_OP_TYPE_NOT 
ATT_OP_TYPE_CONF 
ATT_OP_TYPE_UNKNOWN 

Definition at line 129 of file att.c.

Function Documentation

static uint8_t att_ecode_from_error ( int  err)
static
int bt_att_get_fd ( struct bt_att att)

Definition at line 1087 of file att.c.

References bt_att::fd.

uint16_t bt_att_get_mtu ( struct bt_att att)
bool bt_att_has_crypto ( struct bt_att att)

Definition at line 1524 of file att.c.

References bt_att::crypto.

struct bt_att* bt_att_ref ( struct bt_att att)

Definition at line 1055 of file att.c.

References bt_att::ref_count.

Referenced by bt_att_new(), bt_gatt_client_new(), can_read_data(), disconnect_cb(), and handle_notify().

unsigned int bt_att_register ( struct bt_att att,
uint8_t  opcode,
bt_att_notify_func_t  callback,
void *  user_data,
bt_att_destroy_func_t  destroy 
)
unsigned int bt_att_register_disconnect ( struct bt_att att,
bt_att_disconnect_func_t  callback,
void *  user_data,
bt_att_destroy_func_t  destroy 
)
unsigned int bt_att_send ( struct bt_att att,
uint8_t  opcode,
const void *  pdu,
uint16_t  length,
bt_att_response_func_t  callback,
void *  user_data,
bt_att_destroy_func_t  destroy 
)

encode & send an att message If (op-code type == ATT_OP_TYPE_REQ) process the response with callback function see att_opcode_type_table (att.c) for opcode type definition

Parameters
attstructure of the communication channel
opcodeatt message op-code
pduprotocol data unit buffer
lengthsize of pdu
callbackcallback function depending on opcode to process response
user_datarequest data when relevant
destroyfunction to manage user_data
Returns
att message sequence number or 0 if error

Definition at line 1220 of file att.c.

References ATT_OP_TYPE_CMD, ATT_OP_TYPE_CONF, ATT_OP_TYPE_IND, ATT_OP_TYPE_NOT, ATT_OP_TYPE_REQ, ATT_OP_TYPE_RSP, ATT_OP_TYPE_UNKNOWN, create_att_send_op(), att_send_op::id, bt_att::ind_queue, bt_att::io, bt_att::next_send_id, att_send_op::pdu, queue_push_tail(), bt_att::req_queue, att_send_op::type, wakeup_writer(), and bt_att::write_queue.

Referenced by bt_att_send_error_rsp(), bt_gatt_client_prepare_write(), bt_gatt_client_read_long_value(), bt_gatt_client_read_multiple(), bt_gatt_client_read_value(), bt_gatt_client_write_execute(), bt_gatt_client_write_long_value(), bt_gatt_client_write_value(), bt_gatt_client_write_without_response(), bt_gatt_discover_characteristics(), bt_gatt_discover_descriptors(), bt_gatt_discover_included_services(), bt_gatt_exchange_mtu(), bt_gatt_read_by_type(), cancel_long_write_req(), cancel_prep_write_session(), complete_write_long_op(), discover_chrcs_cb(), discover_descs_cb(), discover_included_cb(), discover_services(), find_by_type_val_cb(), handle_next_prep_write(), notify_cb(), notify_data_write_ccc(), read_by_grp_type_cb(), read_by_type_cb(), read_included(), read_included_cb(), read_long_cb(), and respond_not_supported().

unsigned int bt_att_send_error_rsp ( struct bt_att att,
uint8_t  opcode,
uint16_t  handle,
int  error 
)
bool bt_att_set_close_on_unref ( struct bt_att att,
bool  do_close 
)

Definition at line 1079 of file att.c.

References bt_att::io, and io_set_close_on_destroy().

Referenced by client_create().

bool bt_att_set_debug ( struct bt_att att,
bt_att_debug_func_t  callback,
void *  user_data,
bt_att_destroy_func_t  destroy 
)

Definition at line 1095 of file att.c.

References bt_att::debug_callback, bt_att::debug_data, and bt_att::debug_destroy.

Referenced by client_create().

bool bt_att_set_local_key ( struct bt_att att,
uint8_t  sign_key[16],
bt_att_counter_func_t  func,
void *  user_data 
)

Definition at line 1506 of file att.c.

References bt_att::local_sign, and sign_set_key().

Referenced by cmd_set_sign_key().

bool bt_att_set_mtu ( struct bt_att att,
uint16_t  mtu 
)

Definition at line 1119 of file att.c.

References BT_ATT_DEFAULT_LE_MTU, bt_att::buf, and bt_att::mtu.

Referenced by mtu_cb().

bool bt_att_set_remote_key ( struct bt_att att,
uint8_t  sign_key[16],
bt_att_counter_func_t  func,
void *  user_data 
)

Definition at line 1515 of file att.c.

References bt_att::remote_sign, and sign_set_key().

bool bt_att_set_security ( struct bt_att att,
int  level 
)
bool bt_att_set_timeout_cb ( struct bt_att att,
bt_att_timeout_func_t  callback,
void *  user_data,
bt_att_destroy_func_t  destroy 
)

Definition at line 1141 of file att.c.

References bt_att::timeout_callback, bt_att::timeout_data, and bt_att::timeout_destroy.

bool bt_att_unregister ( struct bt_att att,
unsigned int  id 
)
bool bt_att_unregister_all ( struct bt_att att)
bool bt_att_unregister_disconnect ( struct bt_att att,
unsigned int  id 
)
static void cancel_att_send_op ( struct att_send_op op)
static

Definition at line 249 of file att.c.

References att_send_op::callback, att_send_op::destroy, and att_send_op::user_data.

Referenced by bt_att_cancel(), and bt_att_cancel_all().

static bool change_security ( struct bt_att att,
uint8_t  ecode 
)
static
static struct att_send_op* create_att_send_op ( struct bt_att att,
uint8_t  opcode,
const void *  pdu,
uint16_t  length,
bt_att_response_func_t  callback,
void *  user_data,
bt_att_destroy_func_t  destroy 
)
static
static void destroy_att_disconn ( void *  data)
static

Definition at line 293 of file att.c.

References att_disconn::destroy, and att_disconn::user_data.

Referenced by bt_att_unregister_all(), and bt_att_unregister_disconnect().

static void destroy_att_notify ( void *  data)
static

Definition at line 267 of file att.c.

References att_notify::destroy, and att_notify::user_data.

Referenced by bt_att_unregister(), and bt_att_unregister_all().

static void destroy_att_send_op ( void *  data)
static

destroy att send operation calls the destroy callback with user_data as an argument free pdu data

Parameters
dataatt_send_op pointer

Definition at line 235 of file att.c.

References att_send_op::destroy, att_send_op::pdu, att_send_op::timeout_id, timeout_remove(), and att_send_op::user_data.

Referenced by bt_att_cancel(), bt_att_cancel_all(), bt_att_free(), can_write_data(), handle_conf(), handle_rsp(), and timeout_cb().

static void disconn_handler ( void *  data,
void *  user_data 
)
static

Definition at line 571 of file att.c.

References att_disconn::callback, PTR_TO_INT, att_disconn::removed, and att_disconn::user_data.

Referenced by disconnect_cb().

static bool disconnect_cb ( struct io io,
void *  user_data 
)
static
static enum att_op_type get_op_type ( uint8_t  opcode)
static

Definition at line 174 of file att.c.

References ATT_OP_TYPE_UNKNOWN, and att_opcode_type_table.

Referenced by can_read_data(), create_att_send_op(), handle_notify(), and opcode_match().

static uint8_t get_req_opcode ( uint8_t  rsp_opcode)
static

Definition at line 204 of file att.c.

References att_req_rsp_mapping_table.

Referenced by handle_rsp().

static void handle_conf ( struct bt_att att,
uint8_t *  pdu,
ssize_t  pdu_len 
)
static
static bool handle_error_rsp ( struct bt_att att,
uint8_t *  pdu,
ssize_t  pdu_len,
uint8_t *  opcode 
)
static
static bool handle_signed ( struct bt_att att,
uint8_t  opcode,
uint8_t *  pdu,
ssize_t  pdu_len 
)
static
static bool is_io_l2cap_based ( int  fd)
static

Definition at line 935 of file att.c.

References AF_BLUETOOTH, and BTPROTO_L2CAP.

Referenced by bt_att_new().

static bool match_disconn_id ( const void *  a,
const void *  b 
)
static

Definition at line 303 of file att.c.

References att_disconn::id, and PTR_TO_UINT.

Referenced by bt_att_unregister_disconnect().

static bool match_notify_id ( const void *  a,
const void *  b 
)
static

Definition at line 277 of file att.c.

References att_notify::id, and PTR_TO_UINT.

Referenced by bt_att_unregister().

static bool match_op_id ( const void *  a,
const void *  b 
)
static

Definition at line 1270 of file att.c.

References att_send_op::id, and PTR_TO_UINT.

Referenced by bt_att_cancel().

static bool opcode_match ( uint8_t  opcode,
uint8_t  test_opcode 
)
static

Definition at line 757 of file att.c.

References ATT_OP_TYPE_CMD, ATT_OP_TYPE_REQ, BT_ATT_ALL_REQUESTS, and get_op_type().

Referenced by handle_notify().

static struct att_send_op* pick_next_send_op ( struct bt_att att)
static
static void respond_not_supported ( struct bt_att att,
uint8_t  opcode 
)
static
static bool sign_set_key ( struct sign_info **  sign,
uint8_t  key[16],
bt_att_counter_func_t  func,
void *  user_data 
)
static

Definition at line 1490 of file att.c.

References func, and new0.

Referenced by bt_att_set_local_key(), and bt_att_set_remote_key().

static void write_watch_destroy ( void *  user_data)
static

Definition at line 474 of file att.c.

References bt_att::writer_active.

Referenced by wakeup_writer().

Variable Documentation

const { ... } att_opcode_type_table[]

Referenced by get_op_type().

const { ... } att_req_rsp_mapping_table[]
Initial value:
= {
{ }
}
#define BT_ATT_OP_READ_BY_TYPE_REQ
Definition: att-types.h:47
#define BT_ATT_OP_READ_RSP
Definition: att-types.h:50
#define BT_ATT_OP_READ_BY_TYPE_RSP
Definition: att-types.h:48
#define BT_ATT_OP_READ_BLOB_REQ
Definition: att-types.h:51
#define BT_ATT_OP_PREP_WRITE_REQ
Definition: att-types.h:61
#define BT_ATT_OP_PREP_WRITE_RSP
Definition: att-types.h:62
#define BT_ATT_OP_MTU_REQ
Definition: att-types.h:41
#define BT_ATT_OP_WRITE_REQ
Definition: att-types.h:57
#define BT_ATT_OP_FIND_INFO_RSP
Definition: att-types.h:44
#define BT_ATT_OP_READ_BLOB_RSP
Definition: att-types.h:52
#define BT_ATT_OP_WRITE_RSP
Definition: att-types.h:58
#define BT_ATT_OP_READ_MULT_RSP
Definition: att-types.h:54
#define BT_ATT_OP_EXEC_WRITE_REQ
Definition: att-types.h:63
#define BT_ATT_OP_MTU_RSP
Definition: att-types.h:42
#define BT_ATT_OP_READ_BY_GRP_TYPE_REQ
Definition: att-types.h:55
#define BT_ATT_OP_FIND_BY_TYPE_VAL_REQ
Definition: att-types.h:45
#define BT_ATT_OP_READ_MULT_REQ
Definition: att-types.h:53
#define BT_ATT_OP_READ_BY_GRP_TYPE_RSP
Definition: att-types.h:56
#define BT_ATT_OP_FIND_BY_TYPE_VAL_RSP
Definition: att-types.h:46
#define BT_ATT_OP_EXEC_WRITE_RSP
Definition: att-types.h:64
#define BT_ATT_OP_READ_REQ
Definition: att-types.h:49
#define BT_ATT_OP_FIND_INFO_REQ
Definition: att-types.h:43

Referenced by get_req_opcode().

uint8_t req_opcode

Definition at line 187 of file att.c.

Referenced by handle_rsp().

uint8_t rsp_opcode

Definition at line 188 of file att.c.

Referenced by handle_rsp().