Linux bluetooth development
 help / color / mirror / Atom feed
* [PATCH 1/5] Add read/write callbacks to attribute server
@ 2011-02-21 21:30 Anderson Lizardo
  2011-02-21 21:30 ` [PATCH 2/5] Initial Client Characteristic Configuration implementation Anderson Lizardo
                   ` (8 more replies)
  0 siblings, 9 replies; 21+ messages in thread
From: Anderson Lizardo @ 2011-02-21 21:30 UTC (permalink / raw)
  To: linux-bluetooth; +Cc: Anderson Lizardo

These callbacks will allow profiles to act before an attribute is read
and after it is written, to e.g. update the attribute value to/from an
external source.

Note that by the time the callback is called, the necessary security
checks (attribute permissions, authentication and encryption) were
already performed by the core attribute server.

The callback can optionally return an ATT status code, which will be
sent to the client using an Error Response PDU.
---
 attrib/att.h        |    7 +++++++
 src/attrib-server.c |   29 ++++++++++++++++++++++++++---
 src/attrib-server.h |    4 ++--
 3 files changed, 35 insertions(+), 5 deletions(-)

diff --git a/attrib/att.h b/attrib/att.h
index 7d9afeb..05ae606 100644
--- a/attrib/att.h
+++ b/attrib/att.h
@@ -118,11 +118,18 @@ enum {
 	ATT_NOT_PERMITTED,	/* Operation not permitted */
 };
 
+struct attribute;
+
+typedef uint8_t (*att_cb_t)(struct attribute *, gpointer);
+
 struct attribute {
 	uint16_t handle;
 	uuid_t uuid;
 	int read_reqs;
 	int write_reqs;
+	att_cb_t read_cb;
+	att_cb_t write_cb;
+	gpointer cb_user_data;
 	int len;
 	uint8_t data[0];
 };
diff --git a/src/attrib-server.c b/src/attrib-server.c
index 7ec0f56..28649f1 100644
--- a/src/attrib-server.c
+++ b/src/attrib-server.c
@@ -229,6 +229,10 @@ static uint16_t read_by_group(struct gatt_channel *channel, uint16_t start,
 
 		status = att_check_reqs(channel, ATT_OP_READ_BY_GROUP_REQ,
 								a->read_reqs);
+
+		if (status == 0x00 && a->read_cb)
+			status = a->read_cb(a, a->cb_user_data);
+
 		if (status) {
 			g_slist_foreach(groups, (GFunc) g_free, NULL);
 			g_slist_free(groups);
@@ -317,6 +321,10 @@ static uint16_t read_by_type(struct gatt_channel *channel, uint16_t start,
 
 		status = att_check_reqs(channel, ATT_OP_READ_BY_TYPE_REQ,
 								a->read_reqs);
+
+		if (status == 0x00 && a->read_cb)
+			status = a->read_cb(a, a->cb_user_data);
+
 		if (status) {
 			g_slist_free(types);
 			return enc_error_resp(ATT_OP_READ_BY_TYPE_REQ,
@@ -564,6 +572,10 @@ static uint16_t read_value(struct gatt_channel *channel, uint16_t handle,
 	a = l->data;
 
 	status = att_check_reqs(channel, ATT_OP_READ_REQ, a->read_reqs);
+
+	if (status == 0x00 && a->read_cb)
+		status = a->read_cb(a, a->cb_user_data);
+
 	if (status)
 		return enc_error_resp(ATT_OP_READ_REQ, handle, status, pdu,
 									len);
@@ -591,6 +603,10 @@ static uint16_t read_blob(struct gatt_channel *channel, uint16_t handle,
 					ATT_ECODE_INVALID_OFFSET, pdu, len);
 
 	status = att_check_reqs(channel, ATT_OP_READ_BLOB_REQ, a->read_reqs);
+
+	if (status == 0x00 && a->read_cb)
+		status = a->read_cb(a, a->cb_user_data);
+
 	if (status)
 		return enc_error_resp(ATT_OP_READ_BLOB_REQ, handle, status,
 								pdu, len);
@@ -623,6 +639,13 @@ static uint16_t write_value(struct gatt_channel *channel, uint16_t handle,
 	memcpy(&uuid, &a->uuid, sizeof(uuid_t));
 	attrib_db_update(handle, &uuid, value, vlen);
 
+	if (a->write_cb) {
+		status = a->write_cb(a, a->cb_user_data);
+		if (status)
+			return enc_error_resp(ATT_OP_WRITE_REQ, handle, status,
+								pdu, len);
+	}
+
 	return enc_write_resp(pdu, len);
 }
 
@@ -1013,8 +1036,8 @@ void attrib_free_sdp(uint32_t sdp_handle)
 	remove_record_from_server(sdp_handle);
 }
 
-int attrib_db_add(uint16_t handle, uuid_t *uuid, int read_reqs, int write_reqs,
-						const uint8_t *value, int len)
+struct attribute *attrib_db_add(uint16_t handle, uuid_t *uuid, int read_reqs,
+				int write_reqs, const uint8_t *value, int len)
 {
 	struct attribute *a;
 
@@ -1030,7 +1053,7 @@ int attrib_db_add(uint16_t handle, uuid_t *uuid, int read_reqs, int write_reqs,
 
 	database = g_slist_insert_sorted(database, a, attribute_cmp);
 
-	return 0;
+	return a;
 }
 
 int attrib_db_update(uint16_t handle, uuid_t *uuid, const uint8_t *value,
diff --git a/src/attrib-server.h b/src/attrib-server.h
index ecd695c..85f3bdb 100644
--- a/src/attrib-server.h
+++ b/src/attrib-server.h
@@ -25,8 +25,8 @@
 int attrib_server_init(void);
 void attrib_server_exit(void);
 
-int attrib_db_add(uint16_t handle, uuid_t *uuid, int read_reqs, int write_reqs,
-						const uint8_t *value, int len);
+struct attribute *attrib_db_add(uint16_t handle, uuid_t *uuid, int read_reqs,
+				int write_reqs, const uint8_t *value, int len);
 int attrib_db_update(uint16_t handle, uuid_t *uuid, const uint8_t *value,
 								int len);
 int attrib_db_del(uint16_t handle);
-- 
1.7.0.4


^ permalink raw reply related	[flat|nested] 21+ messages in thread

end of thread, other threads:[~2011-03-07 20:52 UTC | newest]

Thread overview: 21+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2011-02-21 21:30 [PATCH 1/5] Add read/write callbacks to attribute server Anderson Lizardo
2011-02-21 21:30 ` [PATCH 2/5] Initial Client Characteristic Configuration implementation Anderson Lizardo
2011-02-21 21:30 ` [PATCH 3/5] Check permissions before setting client configs Anderson Lizardo
2011-02-21 21:30 ` [PATCH 4/5] Implement server-side ATT handle notification Anderson Lizardo
2011-02-21 21:30 ` [PATCH 5/5] Implement ATT handle indication Anderson Lizardo
2011-02-22 21:01 ` [PATCHv2 1/5] Add read/write callbacks to attribute server Anderson Lizardo
2011-02-23  3:21   ` Johan Hedberg
2011-02-23 14:28     ` Anderson Lizardo
2011-02-23 15:14   ` [PATCH v3 " Anderson Lizardo
2011-02-23 15:14   ` [PATCH v3 2/5] Initial Client Characteristic Configuration implementation Anderson Lizardo
2011-02-23 15:14   ` [PATCH v3 3/5] Check properties before setting client configs Anderson Lizardo
2011-02-23 15:14   ` [PATCH v3 4/5] Implement server-side ATT handle notification Anderson Lizardo
2011-02-23 15:14   ` [PATCH v3 5/5] Implement ATT handle indication Anderson Lizardo
2011-02-24 19:10     ` Johan Hedberg
2011-02-22 21:01 ` [PATCHv2 2/5] Initial Client Characteristic Configuration implementation Anderson Lizardo
2011-02-23  3:26   ` Johan Hedberg
2011-02-23 14:29     ` Anderson Lizardo
2011-03-07 20:52     ` Peter Dons Tychsen
2011-02-22 21:01 ` [PATCHv2 3/5] Check permissions before setting client configs Anderson Lizardo
2011-02-22 21:01 ` [PATCHv2 4/5] Implement server-side ATT handle notification Anderson Lizardo
2011-02-22 21:01 ` [PATCHv2 5/5] Implement ATT handle indication Anderson Lizardo

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox