bt_att initial implementation
Subsystems (2)
v5 cover bt_att initial implementation
v5:
- Removed the PDU signing related functions for now. We"ll revisit them once we start implementing/testing the signed write command.
- Minor cosmetic changes.
Arman Uguray (4):
src/shared/att: Introduce struct bt_att. src/shared/att: Implement basic boilerplate. src/shared/att: Implement write handler and bt_att_send. src/shared/att: Handle incoming response PDUs
Makefile.am | 3 +- src/shared/att-types.h | 222 +++++++++++++ src/shared/att.c | 840 +++++++++++++++++++++++++++++++++++++++++++++++++ src/shared/att.h | 69 ++++ 4 files changed, 1133 insertions(+), 1 deletion(-) create mode 100644 src/shared/att-types.h create mode 100644 src/shared/att.c create mode 100644 src/shared/att.h
Signature · 1 line
1.8.3.2
PATCH v5 1/4 src/shared/att: Introduce struct bt_att.
This patch introduces struct bt_att, which handles the transport and encoding/decoding for the ATT protocol. The structure of the code follows that of src/shared/mgmt and lib/mgmt.h, where individual parameter structures are defined for all ATT protocol requests, responses, commands, indications, and notifications. The serialization and endianness conversion for all parameters are handled by bt_att.
struct bt_att is based around struct io and operates on a raw file descriptor.
--- Makefile.am | 3 +- src/shared/att-types.h | 222 +++++++++++++++++++++++++++++++++++++++++++++++++ src/shared/att.c | 170 +++++++++++++++++++++++++++++++++++++ src/shared/att.h | 69 +++++++++++++++ 4 files changed, 463 insertions(+), 1 deletion(-) create mode 100644 src/shared/att-types.h create mode 100644 src/shared/att.c create mode 100644 src/shared/att.h
Makefile.am +2 -1
@@ -156,7 +156,8 @@ src_bluetoothd_SOURCES = $(builtin_sources) \ src/shared/timeout.h src/shared/timeout-glib.c \ src/shared/queue.h src/shared/queue.c \ src/shared/util.h src/shared/util.c \ - src/shared/mgmt.h src/shared/mgmt.c + src/shared/mgmt.h src/shared/mgmt.c \ + src/shared/att-types.h src/shared/att.h src/shared/att.c src_bluetoothd_LDADD = lib/libbluetooth-internal.la gdbus/libgdbus-internal.la \ @GLIB_LIBS@ @DBUS_LIBS@ -ldl -lrt src_bluetoothd_LDFLAGS = $(AM_LDFLAGS) -Wl,--export-dynamic \
src/shared/att-types.h new file +222 -0
222 lines not on this page · raw message
src/shared/att.c new file +170 -0
170 lines not on this page · raw message
src/shared/att.h new file +69 -0
69 lines not on this page · raw message
Signature · 1 line
1.8.3.2
PATCH v5 2/4 src/shared/att: Implement basic boilerplate.
In reply to Arman UgurayThis patch implements the getters, setters, creation, ref, and unref functions for struct bt_att. Also added is a simple table for determining the ATT op type given an opcode and the io read handler that currently does nothing.
--- src/shared/att.c | 306 +++++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 286 insertions(+), 20 deletions(-)
src/shared/att.c +286 -20
@@ -25,15 +25,19 @@#include <config.h> #endif +#include <stdlib.h> #include <unistd.h> #include "src/shared/io.h" #include "src/shared/queue.h" +#include "src/shared/util.h" #include "lib/uuid.h" #include "src/shared/att.h" #define ATT_DEFAULT_LE_MTU 23#define ATT_MIN_PDU_LEN 1/* At least 1 byte for the opcode. */ +#define ATT_OP_CMD_MASK 0x40 +#define ATT_OP_SIGNED_MASK 0x80struct att_send_op ;
@@ -51,8 +55,6 @@ struct bt_att {struct queue *write_queue;/* Queue of PDUs ready to send */ bool writer_active; -/* TODO Add notify queue */ -uint8_t *buf;uint16_t mtu;
@@ -68,8 +70,66 @@ struct bt_att {void *debug_data; }; +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 , +}; + +static const struct { +uint8_t opcode; +enum att_op_type type; +} att_opcode_type_table[] = { + {BT_ATT_OP_ERROR_RSP ,ATT_OP_TYPE_RSP }, + {BT_ATT_OP_MTU_REQ ,ATT_OP_TYPE_REQ }, + {BT_ATT_OP_MTU_RSP ,ATT_OP_TYPE_RSP }, + {BT_ATT_OP_FIND_INFO_REQ ,ATT_OP_TYPE_REQ }, + {BT_ATT_OP_FIND_INFO_RSP ,ATT_OP_TYPE_RSP }, + {BT_ATT_OP_FIND_BY_TYPE_VAL_REQ ,ATT_OP_TYPE_REQ }, + {BT_ATT_OP_FIND_BY_TYPE_VAL_RSP ,ATT_OP_TYPE_RSP }, + {BT_ATT_OP_READ_BY_TYPE_REQ ,ATT_OP_TYPE_REQ }, + {BT_ATT_OP_READ_BY_TYPE_RSP ,ATT_OP_TYPE_RSP }, + {BT_ATT_OP_READ_REQ ,ATT_OP_TYPE_REQ }, + {BT_ATT_OP_READ_RSP ,ATT_OP_TYPE_RSP }, + {BT_ATT_OP_READ_BLOB_REQ ,ATT_OP_TYPE_REQ }, + {BT_ATT_OP_READ_BLOB_RSP ,ATT_OP_TYPE_RSP }, + {BT_ATT_OP_READ_MULT_REQ ,ATT_OP_TYPE_REQ }, + {BT_ATT_OP_READ_MULT_RSP ,ATT_OP_TYPE_RSP }, + {BT_ATT_OP_READ_BY_GRP_TYPE_REQ ,ATT_OP_TYPE_REQ }, + {BT_ATT_OP_READ_BY_GRP_TYPE_RSP ,ATT_OP_TYPE_RSP }, + {BT_ATT_OP_WRITE_REQ ,ATT_OP_TYPE_REQ }, + {BT_ATT_OP_WRITE_RSP ,ATT_OP_TYPE_RSP }, + {BT_ATT_OP_WRITE_CMD ,ATT_OP_TYPE_CMD }, + {BT_ATT_OP_SIGNED_WRITE_CMD ,ATT_OP_TYPE_CMD }, + {BT_ATT_OP_PREP_WRITE_REQ ,ATT_OP_TYPE_REQ }, + {BT_ATT_OP_PREP_WRITE_RSP ,ATT_OP_TYPE_RSP }, + {BT_ATT_OP_EXEC_WRITE_REQ ,ATT_OP_TYPE_REQ }, + {BT_ATT_OP_EXEC_WRITE_RSP ,ATT_OP_TYPE_RSP }, + {BT_ATT_OP_HANDLE_VAL_NOT ,ATT_OP_TYPE_NOT }, + {BT_ATT_OP_HANDLE_VAL_IND ,ATT_OP_TYPE_IND }, + {BT_ATT_OP_HANDLE_VAL_CONF ,ATT_OP_TYPE_CONF }, + { } +}; + +static enum att_op_type get_op_type (uint8_t opcode) +{ +int i; + +for (i =0 ; att_opcode_type_table[i].opcode; i++) { +if (att_opcode_type_table[i].opcode == opcode) +return att_opcode_type_table[i].type; + } + +return ATT_OP_TYPE_UNKNOWN ; +} +struct att_send_op {unsigned int id; +enum att_op_type type;uint16_t opcode;void *pdu;uint16_t len;
@@ -78,54 +138,206 @@ struct att_send_op {void *user_data; }; +static void destroy_att_send_op (void *data) +{ +struct att_send_op *op = data; + +if (op->destroy) + op->destroy (op->user_data); + +free (op->pdu); +free (op); +} + +static bool can_read_data (struct io *io,void *user_data) +{ +struct bt_att *att = user_data; +uint8_t *pdu; +ssize_t bytes_read; + + bytes_read =read (att->fd, att->buf, att->mtu); +if (bytes_read <0 ) +return false; + +util_hexdump ('>' , att->buf, bytes_read, + att->debug_callback, att->debug_data); + +if (bytes_read <ATT_MIN_PDU_LEN ) +return true; + +/* TODO: Handle different types of PDUs here */ +return true; +} +struct bt_att *bt_att_new (int fd) { -/* TODO */ +struct bt_att *att; + +if (fd <0 ) +return NULL ; + + att = new0(struct bt_att, 1); +if (!att) +return NULL ; + + att->fd = fd; + + att->mtu =ATT_DEFAULT_LE_MTU ; + att->buf =malloc (att->mtu); +if (!att->buf) + goto fail; + + att->io =io_new (fd); +if (!att->io) + goto fail; + + att->req_queue =queue_new (); +if (!att->req_queue) + goto fail; + + att->ind_queue =queue_new (); +if (!att->ind_queue) + goto fail; + + att->write_queue =queue_new (); +if (!att->write_queue) + goto fail; + +if (!io_set_read_handler (att->io, can_read_data, att,NULL )) + goto fail; + +return bt_att_ref (att); + +fail: +queue_destroy (att->req_queue,NULL ); +queue_destroy (att->ind_queue,NULL ); +queue_destroy (att->write_queue,NULL ); +io_destroy (att->io); +free (att->buf); +free (att); +return NULL ; }struct bt_att *bt_att_ref (struct bt_att *att) { -/* TODO */ -return NULL ; +if (!att) +return NULL ; + +__sync_fetch_and_add (&att->ref_count,1 ); + +return att; }void bt_att_unref (struct bt_att *att) { -/* TODO */ +if (!att) +return ; + +if (__sync_sub_and_fetch (&att->ref_count,1 )) +return ; + +bt_att_cancel_all (att); + +io_set_write_handler (att->io,NULL ,NULL ,NULL ); +io_set_read_handler (att->io,NULL ,NULL ,NULL ); + +queue_destroy (att->req_queue,NULL ); +queue_destroy (att->ind_queue,NULL ); +queue_destroy (att->write_queue,NULL ); + att->req_queue =NULL ; + att->ind_queue =NULL ; + att->write_queue =NULL ; + +io_destroy (att->io); + att->io =NULL ; + +if (att->close_on_unref) +close (att->fd); + +if (att->timeout_destroy) + att->timeout_destroy (att->timeout_data); + +if (att->debug_destroy) + att->debug_destroy (att->debug_data); + +free (att->buf); + att->buf =NULL ; + +free (att); }bool bt_att_set_close_on_unref (struct bt_att *att,bool do_close) { -/* TODO */ -return false; +if (!att) +return false; + + att->close_on_unref = do_close; + +return true; }bool bt_att_set_debug (struct bt_att *att,bt_att_debug_func_t callback,void *user_data,bt_att_destroy_func_t destroy) { -/* TODO */ -return false; +if (!att) +return false; + +if (att->debug_destroy) + att->debug_destroy (att->debug_data); + + att->debug_callback = callback; + att->debug_destroy = destroy; + att->debug_data = user_data; + +return true; }uint16_t bt_att_get_mtu (struct bt_att *att) { -/* TODO */ -return 0 ; +if (!att) +return 0 ; + +return att->mtu; }bool bt_att_set_mtu (struct bt_att *att,uint16_t mtu) { -/* TODO */ -return false; +char *buf; + +if (!att) +return false; + +if (mtu <ATT_DEFAULT_LE_MTU ) +return false; + + buf =malloc (mtu); +if (!buf) +return false; + +free (att->buf); + + att->mtu = mtu; + att->buf = buf; + +return true; }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) { -/* TODO */ -return false; +if (!att) +return false; + +if (att->timeout_destroy) + att->timeout_destroy (att->timeout_data); + + att->timeout_callback = callback; + att->timeout_destroy = destroy; + att->timeout_data = user_data; + +return true; } unsigned int bt_att_send(struct bt_att *att, uint8_t opcode,
@@ -137,16 +349,70 @@ unsigned int bt_att_send(struct bt_att *att, uint8_t opcode,return 0 ; } +static bool match_op_id (const void *a,const void *b) +{ +const struct att_send_op *op = a; +unsigned int id =PTR_TO_UINT (b); + +return op->id == id; +} +bool bt_att_cancel (struct bt_att *att,unsigned int id) { -/* TODO */ -return false; +struct att_send_op *op; + +if (!att || !id) +return false; + +if (att->pending_req && att->pending_req->id == id) { + op = att->pending_req; + goto done; + } + +if (att->pending_ind && att->pending_ind->id == id) { + op = att->pending_ind; + goto done; + } + + op =queue_remove_if (att->req_queue, match_op_id,UINT_TO_PTR (id)); +if (op) + goto done; + + op =queue_remove_if (att->ind_queue, match_op_id,UINT_TO_PTR (id)); +if (op) + goto done; + + op =queue_remove_if (att->write_queue, match_op_id,UINT_TO_PTR (id)); +if (op) + goto done; + +if (!op) +return false; + +done: +destroy_att_send_op (op); + +/* TODO: Set the write handler here */ + +return true; }bool bt_att_cancel_all (struct bt_att *att) { -/* TODO */ -return false; +if (!att) +return false; + +queue_remove_all (att->req_queue,NULL ,NULL , destroy_att_send_op); +queue_remove_all (att->ind_queue,NULL ,NULL , destroy_att_send_op); +queue_remove_all (att->write_queue,NULL ,NULL , destroy_att_send_op); + +if (att->pending_req) +destroy_att_send_op (att->pending_req); + +if (att->pending_ind) +destroy_att_send_op (att->pending_ind); + +return true; } unsigned int bt_att_register(struct bt_att *att, uint8_t opcode,
Signature · 1 line
1.8.3.2
PATCH v5 3/4 src/shared/att: Implement write handler and bt_att_send.
In reply to Arman UgurayThis patch implements the write handler logic, including the way send operations are process from the various internal queues. Added PDU encoding for the Exchange MTU request.
--- src/shared/att.c | 307 ++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 304 insertions(+), 3 deletions(-)
src/shared/att.c +304 -3
@@ -27,10 +27,12 @@#include <stdlib.h> #include <unistd.h> +#include <errno.h> #include "src/shared/io.h" #include "src/shared/queue.h" #include "src/shared/util.h" +#include "src/shared/timeout.h" #include "lib/uuid.h" #include "src/shared/att.h"
@@ -38,6 +40,7 @@#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 */ struct att_send_op ;
@@ -129,6 +132,7 @@ static enum att_op_type get_op_type(uint8_t opcode)struct att_send_op {unsigned int id; +unsigned int timeout_id;enum att_op_type type;uint16_t opcode;void *pdu;
@@ -138,10 +142,146 @@ struct att_send_op {void *user_data; }; +static bool encode_mtu_req (struct att_send_op *op,const void *param, +uint16_t length,uint16_t mtu) +{ +const struct bt_att_mtu_req_param *p = param; +const uint16_t len =3 ; + +if (length !=sizeof (*p)) +return false; + +if (len > mtu) +return false; + + op->pdu =malloc (len); +if (!op->pdu) +return false; + + ((uint8_t *) op->pdu)[0 ] = op->opcode; +put_le16 (p->client_rx_mtu, ((uint8_t *) op->pdu) +1 ); + op->len = len; + +return true; +} + +static bool encode_pdu (struct att_send_op *op,const void *param, +uint16_t length,uint16_t mtu) +{ +/* If no parameters are given, simply set the PDU to consist of the +* opcode (e.g. BT_ATT_OP_WRITE_RSP), +*/ +if (!length || !param) { + op->len =1 ; + op->pdu =malloc (1 ); +if (!op->pdu) +return false; + + ((uint8_t *) op->pdu)[0 ] = op->opcode; +return true; + } + +/* TODO: If the opcode has the "signed" bit set, make sure that the +* resulting PDU contains the authentication signature. Return an error, +* if the provided parameters structure is such that it leaves no room +* for an authentication signature in the PDU, or if no signing data +* has been set to generate the authentication signature. +*/ + +switch (op->opcode) { +case BT_ATT_OP_MTU_REQ : +return encode_mtu_req (op, param, length, mtu); + dafault: +break ; + } + +return false; +} + +static struct att_send_op *create_att_send_op (uint8_t opcode,const void *param, +uint16_t length,uint16_t mtu, +bt_att_request_func_t callback, +void *user_data, +bt_att_destroy_func_t destroy) +{ +struct att_send_op *op; +enum att_op_type op_type; + +if (!length && !param) +return NULL ; + + op_type =get_op_type (opcode); +if (op_type ==ATT_OP_TYPE_UNKNOWN ) +return NULL ; + +/* If the opcode corresponds to an operation type that does not elicit a +* response from the remote end, then no callback should have been +* provided, since it will never be called. +*/ +if (callback && op_type !=ATT_OP_TYPE_REQ && op_type !=ATT_OP_TYPE_IND ) +return NULL ; + +/* Similarly, if the operation does elicit a response then a callback +* must be provided. +*/ +if (!callback && (op_type ==ATT_OP_TYPE_REQ || op_type ==ATT_OP_TYPE_IND )) +return NULL ; + + op = new0(struct att_send_op, 1); +if (!op) +return NULL ; + + op->type = op_type; + op->opcode = opcode; + op->callback = callback; + op->destroy = destroy; + op->user_data = user_data; + +if (!encode_pdu (op, param, length, mtu)) { +free (op); +return NULL ; + } + +return op; +} + +static struct att_send_op *pick_next_send_op (struct bt_att *att) +{ +struct att_send_op *op; + +/* See if any operations are already in the write queue */ + op =queue_pop_head (att->write_queue); +if (op) +return op; + +/* If there is no pending request, pick an operation from the +* request queue. +*/ +if (!att->pending_req) { + op =queue_pop_head (att->req_queue); +if (op) +return op; + } + +/* There is either a request pending or no requests queued. If there is +* no pending indication, pick an operation from the indication queue. +*/ +if (!att->pending_ind) { + op =queue_pop_head (att->ind_queue); +if (op) +return op; + } + +return NULL ; +} +static void destroy_att_send_op (void *data) {struct att_send_op *op = data; +if (op->timeout_id) +timeout_remove (op->timeout_id); +if (op->destroy) op->destroy (op->user_data);
@@ -149,6 +289,128 @@ static void destroy_att_send_op(void *data)free (op); } +struct timeout_data { +struct bt_att *att; +unsigned int id; +}; + +static bool timeout_cb (void *user_data) +{ +struct timeout_data *timeout = user_data; +struct bt_att *att = timeout->att; +struct att_send_op *op =NULL ; + +if (att->pending_req && att->pending_req->id == timeout->id) { + op = att->pending_req; + att->pending_req =NULL ; + }else if (att->pending_ind && att->pending_ind->id == timeout->id) { + op = att->pending_ind; + att->pending_ind =NULL ; + } + +if (!op) +return false; + + att->invalid = true; + +util_debug (att->debug_callback, att->debug_data, +"Operation timed out: 0x%02x" , op->opcode); + +if (att->timeout_callback) + att->timeout_callback (op->id, op->opcode, att->timeout_data); + + op->timeout_id =0 ; +destroy_att_send_op (op); + +return false; +} + +static void write_watch_destroy (void *user_data) +{ +struct bt_att *att = user_data; + + att->writer_active = false; +} + +static bool can_write_data (struct io *io,void *user_data) +{ +struct bt_att *att = user_data; +struct att_send_op *op; +struct timeout_data *timeout; +ssize_t bytes_written; + + op =pick_next_send_op (att); +if (!op) +return false; + + bytes_written =write (att->fd, op->pdu, op->len); +if (bytes_written <0 ) { +util_debug (att->debug_callback, att->debug_data, +"write failed: %s" ,strerror (errno)); +if (op->callback) + op->callback (BT_ATT_OP_ERROR_RSP ,NULL ,0 , + op->user_data); + +destroy_att_send_op (op); +return true; + } + +util_debug (att->debug_callback, att->debug_data, +"ATT op 0x%02x" , op->opcode); + +util_hexdump ('<' , op->pdu, bytes_written, + att->debug_callback, att->debug_data); + +/* Based on the operation type, set either the pending request or the +* pending indication. If it came from the write queue, then there is +* no need to keep it around. +*/ +switch (op->type) { +case ATT_OP_TYPE_REQ : + att->pending_req = op; +break ; +case ATT_OP_TYPE_IND : + att->pending_ind = op; +break ; +default : +destroy_att_send_op (op); +return true; + } + + timeout = new0(struct timeout_data, 1); +if (!timeout) +return true; + + timeout->att = att; + timeout->id = op->id; + op->timeout_id =timeout_add (ATT_TIMEOUT_INTERVAL , timeout_cb, + timeout, free); + +/* Return true as there may be more operations ready to write. */ +return true; +} + +static void wakeup_writer (struct bt_att *att) +{ +if (att->writer_active) +return ; + +/* Set the write handler only if there is anything that can be sent +* at all. +*/ +if (queue_isempty (att->write_queue)) { +if ((att->pending_req ||queue_isempty (att->req_queue)) && + (att->pending_ind ||queue_isempty (att->ind_queue))) +return ; + } + +if (!io_set_write_handler (att->io, can_write_data, att, + write_watch_destroy)) +return ; + + att->writer_active = true; +} +static bool can_read_data (struct io *io,void *user_data) {struct bt_att *att = user_data;
@@ -345,8 +607,47 @@ unsigned int bt_att_send(struct bt_att *att, uint8_t opcode, bt_att_request_func_t callback, void *user_data, bt_att_destroy_func_t destroy) { - /* TODO */ - return 0; + struct att_send_op *op; +bool result; + +if (!att) +return 0 ; + +if (att->invalid) +return 0 ; + + op =create_att_send_op (opcode, param, length, att->mtu, callback, + user_data, destroy); +if (!op) +return 0 ; + +if (att->next_send_id <1 ) + att->next_send_id =1 ; + + op->id = att->next_send_id++; + +/* Add the op to the correct queue based on its type */ +switch (op->type) { +case ATT_OP_TYPE_REQ : + result =queue_push_tail (att->req_queue, op); +break ; +case ATT_OP_TYPE_IND : + result =queue_push_tail (att->ind_queue, op); +break ; +default : + result =queue_push_tail (att->write_queue, op); +break ; + } + +if (!result) { +free (op->pdu); +free (op); +return 0 ; + } + +wakeup_writer (att); + +return op->id; }static bool match_op_id (const void *a,const void *b)
@@ -392,7 +693,7 @@ bool bt_att_cancel(struct bt_att *att, unsigned int id) done:destroy_att_send_op (op); -/* TODO: Set the write handler here */ +wakeup_writer (att);return true; }
Signature · 1 line
1.8.3.2
PATCH v5 4/4 src/shared/att: Handle incoming response PDUs
In reply to Arman UgurayThis patch adds handling for incoming ATT protocol response PDUs. Basic incoming PDU handling logic added to the io read handler.
--- src/shared/att.c | 108 ++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 107 insertions(+), 1 deletion(-)
src/shared/att.c +107 -1
@@ -411,9 +411,102 @@ static void wakeup_writer(struct bt_att *att) att->writer_active = true; } +static bool request_complete (struct bt_att *att,uint8_t req_opcode, +uint8_t rsp_opcode,const void *param, +uint16_t len) +{ +struct att_send_op *op = att->pending_req; + +if (!op) { +/* There is no pending request so the response is unexpected. */ +wakeup_writer (att); +return false; + } + +if (op->opcode != req_opcode) { +/* The request opcode corresponding to the received response +* opcode does not match the currently pending request. +*/ +return false; + } + +if (op->callback) + op->callback (rsp_opcode, param, len, op->user_data); + +destroy_att_send_op (op); + att->pending_req =NULL ; + +wakeup_writer (att); +return true; +} + +static bool handle_error_rsp (struct bt_att *att,uint8_t opcode,uint8_t *pdu, +ssize_t pdu_len) +{ +struct bt_att_error_rsp_param param; +bool result; + +if (pdu_len !=5 ) +return false; + +memset (¶m,0 ,sizeof (param)); + param.request_opcode = pdu[1 ]; + param.handle =get_le16 (pdu +2 ); + param.error_code = pdu[4 ]; + +return request_complete (att, pdu[1 ], opcode, ¶m,sizeof (param)); +} + +static bool handle_mtu_rsp (struct bt_att *att,uint8_t opcode,uint8_t *pdu, +ssize_t pdu_len) +{ +struct bt_att_mtu_rsp_param param; +bool result; + +if (pdu_len !=3 ) +return false; + +memset (¶m,0 ,sizeof (param)); + param.server_rx_mtu =get_le16 (pdu +1 ); + +return request_complete (att,BT_ATT_OP_MTU_REQ , opcode, + ¶m,sizeof (param)); +} + +static void handle_rsp (struct bt_att *att,uint8_t opcode,uint8_t *pdu, +ssize_t pdu_len) +{ +bool success; + +switch (opcode) { +case BT_ATT_OP_ERROR_RSP : + success =handle_error_rsp (att, opcode, pdu, pdu_len); +break ; +case BT_ATT_OP_MTU_RSP : + success =handle_mtu_rsp (att, opcode, pdu, pdu_len); +break ; +default : + success = false; +util_debug (att->debug_callback, att->debug_data, +"Unknown response opcode: 0x%02x" , opcode); +break ; + } + +if (success) +return ; + +util_debug (att->debug_callback, att->debug_data, +"Failed to handle respone PDU; opcode: 0x%02x" , opcode); + +if (att->pending_req) +request_complete (att, att->pending_req->opcode, +BT_ATT_OP_ERROR_RSP ,NULL ,0 ); +} +static bool can_read_data (struct io *io,void *user_data) {struct bt_att *att = user_data; +uint8_t opcode;uint8_t *pdu;ssize_t bytes_read;
@@ -427,7 +520,20 @@ static bool can_read_data(struct io *io, void *user_data)if (bytes_read <ATT_MIN_PDU_LEN )return true; -/* TODO: Handle different types of PDUs here */ + pdu = att->buf; + opcode = pdu[0 ]; + +/* Act on the received PDU based on the opcode type */ +switch (get_op_type (opcode)) { +case ATT_OP_TYPE_RSP : +handle_rsp (att, opcode, pdu, bytes_read); +break ; +default : +util_debug (att->debug_callback, att->debug_data, +"ATT opcode cannot be handled: 0x%02x" , opcode); +break ; + } +return true; }
Signature · 1 line
1.8.3.2
Marcel Holtmann
maintainerHi Arman,
Quoted 4 lines
v5:
- Removed the PDU signing related functions for now. We"ll revisit them once we start implementing/testing the signed write command.
- Minor cosmetic changes.
forget my previous comment, I mixed up v4 with v5 :(
Quoted 5 lines
Arman Uguray (4):
src/shared/att: Introduce structbt_att. src/shared/att: Implement basic boilerplate. src/shared/att: Implement write handler andbt_att_send. src/shared/att: Handle incoming response PDUs
A prefix of shared: or shared/att: is enough. The src/ part is too much.
Quoted 9 lines
Makefile.am | 3 +- src/shared/att-types.h | 222 +++++++++++++ src/shared/att.c | 840 +++++++++++++++++++++++++++++++++++++++++++++++++ src/shared/att.h | 69 ++++ 4 files changed, 1133 insertions(+), 1 deletion(-)create mode 100644
src/shared/att-types.hcreate mode 100644src/shared/att.c
create mode 100644src/shared/att.h
However, you really need to use a proper compiler that gives you a decent amount of warnings.
CC src/shared/bluetoothd-att.osrc/shared/att.c: In function ‘encode_pdu’:src/shared/att.c:194:2: error: label ‘dafault’ defined but not used [-Werror=unused-label]
dafault:
^src/shared/att.c: In function ‘handle_error_rsp’:src/shared/att.c:447:8: error: unused variable ‘result’ [-Werror=unused-variable]
bool result;
^
src/shared/att.c: In function ‘handle_mtu_rsp’:
src/shared/att.c:464:8: error: unused variable ‘result’ [-Werror=unused-variable]
bool result;
^
src/shared/att.c: In function ‘bt_att_set_mtu’:
src/shared/att.c:689:11: error: pointer targets in assignment differ in signedness [-Werror=pointer-sign]
att->buf = buf;
^I fixed them all up for you since they were dead simple fixes, but please be a bit more careful for the next set of patches.
Regards
Marcel
Hi Marcel,
Quoted 1 line
However, you really need to use a proper compiler that gives you a decent=
amount of warnings.
Quoted 4 lines
CC
src/shared/bluetoothd-att.osrc/shared/att.c: In function =E2=80=98encode_pdu=E2=80=99:src/shared/att.c:194:2: error: label =E2=80=98dafault=E2=80=99 defined bu=
t not used [-Werror=3Dunused-label]
Quoted 4 lines
dafault:
^src/shared/att.c: In function =E2=80=98handle_error_rsp=E2=80=99:src/shared/att.c:447:8: error: unused variable =E2=80=98result=E2=80=99 [=
-Werror=3Dunused-variable]
Quoted 4 lines
bool result; ^
src/shared/att.c: In function =E2=80=98handle_mtu_rsp=E2=80=99:src/shared/att.c:464:8: error: unused variable =E2=80=98result=E2=80=99 [=
-Werror=3Dunused-variable]
Quoted 4 lines
bool result; ^
src/shared/att.c: In function =E2=80=98bt_att_set_mtu=E2=80=99:src/shared/att.c:689:11: error: pointer targets in assignment differ in s=
ignedness [-Werror=3Dpointer-sign]
Quoted 4 lines
att->buf =3D buf; ^I fixed them all up for you since they were dead simple fixes, but please=
be a bit more careful for the next set of patches.
Quoted 1 line
Thanks for doing that. I've been compiling for Chrome OS using the portage based toolchain, which really just runs the included configure scripts and compiles with gcc. I'll just explicitly stick in -Werror somewhere next time.
Thanks!
Arman