2011-02-15 14:16:48

by Bruna Moreira

[permalink] [raw]
Subject: [PATCH 1/2] Add encode/decode for write response

The encode/decode functions for Write Response operations were created
to keep consistency with the rest of GATT API.
---
attrib/att.c | 21 +++++++++++++++++++++
attrib/att.h | 2 ++
src/attrib-server.c | 4 +---
3 files changed, 24 insertions(+), 3 deletions(-)

diff --git a/attrib/att.c b/attrib/att.c
index dff8597..3259fca 100644
--- a/attrib/att.c
+++ b/attrib/att.c
@@ -526,6 +526,27 @@ uint16_t dec_write_req(const uint8_t *pdu, int len, uint16_t *handle,
return len;
}

+uint16_t enc_write_resp(uint8_t *pdu, int len)
+{
+ if (pdu == NULL)
+ return 0;
+
+ pdu[0] = ATT_OP_WRITE_RESP;
+
+ return sizeof(pdu[0]);
+}
+
+uint16_t dec_write_resp(const uint8_t *pdu, int len)
+{
+ if (pdu == NULL)
+ return 0;
+
+ if (pdu[0] != ATT_OP_WRITE_RESP)
+ return 0;
+
+ return len;
+}
+
uint16_t enc_read_req(uint16_t handle, uint8_t *pdu, int len)
{
const uint16_t min_len = sizeof(pdu[0]) + sizeof(handle);
diff --git a/attrib/att.h b/attrib/att.h
index 7e81dc4..7d9afeb 100644
--- a/attrib/att.h
+++ b/attrib/att.h
@@ -215,6 +215,8 @@ uint16_t enc_write_req(uint16_t handle, const uint8_t *value, int vlen,
uint8_t *pdu, int len);
uint16_t dec_write_req(const uint8_t *pdu, int len, uint16_t *handle,
uint8_t *value, int *vlen);
+uint16_t enc_write_resp(uint8_t *pdu, int len);
+uint16_t dec_write_resp(const uint8_t *pdu, int len);
uint16_t enc_read_req(uint16_t handle, uint8_t *pdu, int len);
uint16_t enc_read_blob_req(uint16_t handle, uint16_t offset, uint8_t *pdu,
int len);
diff --git a/src/attrib-server.c b/src/attrib-server.c
index 72f5b17..85b39a8 100644
--- a/src/attrib-server.c
+++ b/src/attrib-server.c
@@ -601,9 +601,7 @@ 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);

- pdu[0] = ATT_OP_WRITE_RESP;
-
- return sizeof(pdu[0]);
+ return enc_write_resp(pdu, len);
}

static uint16_t mtu_exchange(struct gatt_channel *channel, uint16_t mtu,
--
1.7.0.4



2011-02-15 14:48:45

by Johan Hedberg

[permalink] [raw]
Subject: Re: [PATCH 1/2] Add encode/decode for write response

Hi Bruna,

On Tue, Feb 15, 2011, Bruna Moreira wrote:
> The encode/decode functions for Write Response operations were created
> to keep consistency with the rest of GATT API.
> ---
> attrib/att.c | 21 +++++++++++++++++++++
> attrib/att.h | 2 ++
> src/attrib-server.c | 4 +---
> 3 files changed, 24 insertions(+), 3 deletions(-)

Both patches have been pushed upstream. Thanks.

Johan

2011-02-15 14:16:49

by Bruna Moreira

[permalink] [raw]
Subject: [PATCH 2/2] Add Write Request operation in gatttool

Add option and callbacks for Write Request operation in gatttool.
---
attrib/gatttool.c | 61 ++++++++++++++++++++++++++++++++++++++++++++++++++++-
1 files changed, 60 insertions(+), 1 deletions(-)

diff --git a/attrib/gatttool.c b/attrib/gatttool.c
index 8e8ed8e..b9f0087 100644
--- a/attrib/gatttool.c
+++ b/attrib/gatttool.c
@@ -63,6 +63,7 @@ static gboolean opt_listen = FALSE;
static gboolean opt_char_desc = FALSE;
static gboolean opt_le = FALSE;
static gboolean opt_char_write = FALSE;
+static gboolean opt_char_write_req = FALSE;
static GMainLoop *event_loop;
static gboolean got_error = FALSE;

@@ -436,6 +437,59 @@ error:
return FALSE;
}

+static void char_write_req_cb(guint8 status, const guint8 *pdu, guint16 plen,
+ gpointer user_data)
+{
+ if (status != 0) {
+ g_printerr("Characteristic Write Request failed: "
+ "%s\n", att_ecode2str(status));
+ goto done;
+ }
+
+ if (!dec_write_resp(pdu, plen)) {
+ g_printerr("Protocol error\n");
+ goto done;
+ }
+
+ g_print("Characteristic value was written sucessfully\n");
+
+done:
+ if (opt_listen == FALSE)
+ g_main_loop_quit(event_loop);
+}
+
+static gboolean characteristics_write_req(gpointer user_data)
+{
+ GAttrib *attrib = user_data;
+ uint8_t *value;
+ size_t len;
+
+ if (opt_handle <= 0) {
+ g_printerr("A valid handle is required\n");
+ goto error;
+ }
+
+ if (opt_value == NULL || opt_value[0] == '\0') {
+ g_printerr("A value is required\n");
+ goto error;
+ }
+
+ len = attr_data_from_string(opt_value, &value);
+ if (len == 0) {
+ g_printerr("Invalid value\n");
+ goto error;
+ }
+
+ gatt_write_char(attrib, opt_handle, value, len, char_write_req_cb,
+ NULL);
+
+ return FALSE;
+
+error:
+ g_main_loop_quit(event_loop);
+ return FALSE;
+}
+
static void char_desc_cb(guint8 status, const guint8 *pdu, guint16 plen,
gpointer user_data)
{
@@ -530,7 +584,10 @@ static GOptionEntry gatt_options[] = {
{ "char-read", 0, 0, G_OPTION_ARG_NONE, &opt_char_read,
"Characteristics Value/Descriptor Read", NULL },
{ "char-write", 0, 0, G_OPTION_ARG_NONE, &opt_char_write,
- "Characteristics Value Write", NULL },
+ "Characteristics Value Write Without Response (Write Command)",
+ NULL },
+ { "char-write-req", 0, 0, G_OPTION_ARG_NONE, &opt_char_write_req,
+ "Characteristics Value Write (Write Request)", NULL },
{ "char-desc", 0, 0, G_OPTION_ARG_NONE, &opt_char_desc,
"Characteristics Descriptor Discovery", NULL },
{ "listen", 0, 0, G_OPTION_ARG_NONE, &opt_listen,
@@ -602,6 +659,8 @@ int main(int argc, char *argv[])
callback = characteristics_read;
else if (opt_char_write)
callback = characteristics_write;
+ else if (opt_char_write_req)
+ callback = characteristics_write_req;
else if (opt_char_desc)
callback = characteristics_desc;
else {
--
1.7.0.4