All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v4 1/2] android/gatt: Handle read characteristic client command
@ 2014-03-31 16:12 Grzegorz Kolodziejczyk
  2014-03-31 16:12 ` [PATCH v4 2/2] android/gatt: Handle write " Grzegorz Kolodziejczyk
  2014-03-31 17:16 ` [PATCH v4 1/2] android/gatt: Handle read " Szymon Janc
  0 siblings, 2 replies; 3+ messages in thread
From: Grzegorz Kolodziejczyk @ 2014-03-31 16:12 UTC (permalink / raw)
  To: linux-bluetooth

This adds read characteristic client command handling.
---
 android/gatt.c | 139 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 137 insertions(+), 2 deletions(-)

diff --git a/android/gatt.c b/android/gatt.c
index 8dacd86..674a682 100644
--- a/android/gatt.c
+++ b/android/gatt.c
@@ -243,6 +243,26 @@ static bool match_notification(const void *a, const void *b)
 	return true;
 }
 
+static bool match_char_by_element_id(const void *data, const void *user_data)
+{
+	const struct element_id *exp_id = user_data;
+	const struct characteristic *chars = data;
+	bt_uuid_t uuid;
+
+	bt_string_to_uuid(&uuid, chars->ch.uuid);
+	if (exp_id->instance == chars->id.instance)
+		return !bt_uuid_cmp(&uuid, &exp_id->uuid);
+
+	return false;
+}
+
+static void hal_gatt_id_to_element_id(const struct hal_gatt_gatt_id *from,
+							struct element_id *to)
+{
+	to->instance = from->inst_id;
+	android2uuid(from->uuid, &to->uuid);
+}
+
 static void destroy_notification(void *data)
 {
 	struct notification_data *notification = data;
@@ -1359,13 +1379,128 @@ static void handle_client_get_descriptor(const void *buf, uint16_t len)
 			HAL_OP_GATT_CLIENT_GET_DESCRIPTOR, HAL_STATUS_FAILED);
 }
 
+struct read_char_data {
+	int32_t conn_id;
+	struct element_id srvc_id;
+	struct element_id char_id;
+	uint8_t primary;
+};
+
+static void send_client_read_char_notify(int32_t status, const uint8_t *pdu,
+						uint16_t len, int32_t conn_id,
+						struct element_id *srvc_id,
+						struct element_id *char_id,
+						uint8_t primary)
+{
+	uint8_t buf[IPC_MTU];
+	struct hal_ev_gatt_client_read_characteristic *ev = (void *) buf;
+	ssize_t vlen;
+
+	memset(buf, 0, sizeof(buf));
+
+	ev->conn_id = conn_id;
+	ev->status = status;
+
+	ev->data.srvc_id.inst_id = srvc_id->instance;
+	uuid2android(&srvc_id->uuid, ev->data.srvc_id.uuid);
+	ev->data.srvc_id.is_primary = primary;
+
+	ev->data.char_id.inst_id = char_id->instance;
+	uuid2android(&char_id->uuid, ev->data.char_id.uuid);
+
+	if (pdu) {
+		vlen = dec_read_resp(pdu, len, ev->data.value, sizeof(buf));
+		if (vlen < 0) {
+			error("gatt: Protocol error");
+			ev->status = GATT_FAILURE;
+		} else {
+			ev->data.len = vlen;
+		}
+	}
+
+	ipc_send_notif(hal_ipc, HAL_SERVICE_ID_GATT,
+					HAL_EV_GATT_CLIENT_READ_CHARACTERISTIC,
+					sizeof(*ev) + ev->data.len, ev);
+}
+
+static void read_char_cb(guint8 status, const guint8 *pdu, guint16 len,
+							gpointer user_data)
+{
+	struct read_char_data *data = user_data;
+
+	send_client_read_char_notify(status, pdu, len, data->conn_id,
+						&data->srvc_id, &data->char_id,
+						data->primary);
+
+	free(data);
+}
+
 static void handle_client_read_characteristic(const void *buf, uint16_t len)
 {
+	const struct hal_cmd_gatt_client_read_characteristic *cmd = buf;
+	struct read_char_data *cb_data;
+	struct characteristic *ch;
+	struct gatt_device *dev;
+	struct service *srvc;
+	struct element_id srvc_id;
+	struct element_id char_id;
+	uint8_t status;
+
 	DBG("");
 
+	/* TODO authorization needs to be handled */
+
+	hal_srvc_id_to_element_id(&cmd->srvc_id, &srvc_id);
+	hal_gatt_id_to_element_id(&cmd->gatt_id, &char_id);
+
+	if (!find_service(cmd->conn_id, &srvc_id, &dev, &srvc)) {
+		status = HAL_STATUS_FAILED;
+		goto failed;
+	}
+
+	/* search characteristics by element id */
+	ch = queue_find(srvc->chars, match_char_by_element_id, &char_id);
+	if (!ch) {
+		error("gatt: Characteristic with inst_id: %d not found",
+							cmd->gatt_id.inst_id);
+		status = HAL_STATUS_FAILED;
+		goto failed;
+	}
+
+	cb_data = new0(struct read_char_data, 1);
+	if (!cb_data) {
+		error("gatt: Cannot allocate cb data");
+		status = HAL_STATUS_NOMEM;
+		goto failed;
+	}
+
+	cb_data->conn_id = cmd->conn_id;
+	cb_data->primary = cmd->srvc_id.is_primary;
+	cb_data->srvc_id = srvc_id;
+	cb_data->char_id = char_id;
+
+	if (!gatt_read_char(dev->attrib, ch->ch.value_handle,
+						read_char_cb, cb_data)) {
+		error("gatt: Cannot read characteristic with inst_id: %d",
+							cmd->gatt_id.inst_id);
+		status = HAL_STATUS_FAILED;
+		free(cb_data);
+		goto failed;
+	}
+
+	status = HAL_STATUS_SUCCESS;
+
+failed:
 	ipc_send_rsp(hal_ipc, HAL_SERVICE_ID_GATT,
-					HAL_OP_GATT_CLIENT_READ_CHARACTERISTIC,
-					HAL_STATUS_FAILED);
+				HAL_OP_GATT_CLIENT_READ_CHARACTERISTIC, status);
+
+	/* We should send notification with service, characteristic id in case
+	 * of errors.
+	 */
+	if (status != HAL_STATUS_SUCCESS)
+		send_client_read_char_notify(GATT_FAILURE, NULL, 0,
+					cmd->conn_id, &srvc_id, &char_id,
+					cmd->srvc_id.is_primary);
 }
 
 static void handle_client_write_characteristic(const void *buf, uint16_t len)
-- 
1.8.5.2


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

* [PATCH v4 2/2] android/gatt: Handle write characteristic client command
  2014-03-31 16:12 [PATCH v4 1/2] android/gatt: Handle read characteristic client command Grzegorz Kolodziejczyk
@ 2014-03-31 16:12 ` Grzegorz Kolodziejczyk
  2014-03-31 17:16 ` [PATCH v4 1/2] android/gatt: Handle read " Szymon Janc
  1 sibling, 0 replies; 3+ messages in thread
From: Grzegorz Kolodziejczyk @ 2014-03-31 16:12 UTC (permalink / raw)
  To: linux-bluetooth

This adds write characteristic client command handling.
---
 android/gatt.c | 109 +++++++++++++++++++++++++++++++++++++++++++++++++++++++--
 1 file changed, 107 insertions(+), 2 deletions(-)

diff --git a/android/gatt.c b/android/gatt.c
index 674a682..4d6220f 100644
--- a/android/gatt.c
+++ b/android/gatt.c
@@ -1503,13 +1503,118 @@ failed:
 					cmd->srvc_id.is_primary);
 }
 
+struct write_char_data {
+	int32_t conn_id;
+	struct element_id srvc_id;
+	struct element_id char_id;
+	uint8_t primary;
+};
+
+static void send_client_write_char_notify(int32_t status, int32_t conn_id,
+					struct element_id *srvc_id,
+					struct element_id *char_id,
+					uint8_t primary)
+{
+	struct hal_ev_gatt_client_write_characteristic ev;
+
+	memset(&ev, 0, sizeof(ev));
+
+	ev.conn_id = conn_id;
+	ev.status = status;
+
+	ev.data.srvc_id.inst_id = srvc_id->instance;
+	uuid2android(&srvc_id->uuid, ev.data.srvc_id.uuid);
+	ev.data.srvc_id.is_primary = primary;
+
+	ev.data.char_id.inst_id = char_id->instance;
+	uuid2android(&char_id->uuid, ev.data.srvc_id.uuid);
+
+	ipc_send_notif(hal_ipc, HAL_SERVICE_ID_GATT,
+					HAL_EV_GATT_CLIENT_WRITE_CHARACTERISTIC,
+					sizeof(ev), &ev);
+}
+
+static void write_char_cb(guint8 status, const guint8 *pdu, guint16 len,
+							gpointer user_data)
+{
+	struct write_char_data *data = user_data;
+
+	send_client_write_char_notify(status, data->conn_id, &data->srvc_id,
+					&data->char_id, data->primary);
+
+	free(data);
+}
+
 static void handle_client_write_characteristic(const void *buf, uint16_t len)
 {
+	const struct hal_cmd_gatt_client_write_characteristic *cmd = buf;
+	struct write_char_data *cb_data;
+	struct characteristic *ch;
+	struct gatt_device *dev;
+	struct service *srvc;
+	struct element_id srvc_id;
+	struct element_id char_id;
+	uint8_t status;
+
 	DBG("");
 
+	if (len != sizeof(*cmd) + cmd->len) {
+		error("Invalid write char size (%u bytes), terminating", len);
+		raise(SIGTERM);
+		return;
+	}
+
+	hal_srvc_id_to_element_id(&cmd->srvc_id, &srvc_id);
+	hal_gatt_id_to_element_id(&cmd->gatt_id, &char_id);
+
+	if (!find_service(cmd->conn_id, &srvc_id, &dev, &srvc)) {
+		status = HAL_STATUS_FAILED;
+		goto failed;
+	}
+
+	/* search characteristics by instance id */
+	ch = queue_find(srvc->chars, match_char_by_element_id, &char_id);
+	if (!ch) {
+		error("gatt: Characteristic with inst_id: %d not found",
+							cmd->gatt_id.inst_id);
+		status = HAL_STATUS_FAILED;
+		goto failed;
+	}
+
+	cb_data = new0(struct write_char_data, 1);
+	if (!cb_data) {
+		error("gatt: Cannot allocate call data");
+		status = HAL_STATUS_NOMEM;
+		goto failed;
+	}
+
+	cb_data->conn_id = cmd->conn_id;
+	cb_data->primary = cmd->srvc_id.is_primary;
+	cb_data->srvc_id = srvc_id;
+	cb_data->char_id = char_id;
+
+	if (!gatt_write_char(dev->attrib, ch->ch.value_handle, cmd->value,
+					cmd->len, write_char_cb, cb_data)) {
+		error("gatt: Cannot read characteristic with inst_id: %d",
+							cmd->gatt_id.inst_id);
+		status = HAL_STATUS_FAILED;
+		free(cb_data);
+		goto failed;
+	}
+
+	status = HAL_STATUS_SUCCESS;
+
+failed:
 	ipc_send_rsp(hal_ipc, HAL_SERVICE_ID_GATT,
-					HAL_OP_GATT_CLIENT_WRITE_CHARACTERISTIC,
-					HAL_STATUS_FAILED);
+			HAL_OP_GATT_CLIENT_WRITE_CHARACTERISTIC, status);
+
+	/* We should send notification with service, characteristic id in case
+	 * of errors.
+	 */
+	if (status != HAL_STATUS_SUCCESS)
+		send_client_write_char_notify(GATT_FAILURE, cmd->conn_id,
+						&srvc_id, &char_id,
+						cmd->srvc_id.is_primary);
 }
 
 static void handle_client_read_descriptor(const void *buf, uint16_t len)
-- 
1.8.5.2


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

* Re: [PATCH v4 1/2] android/gatt: Handle read characteristic client command
  2014-03-31 16:12 [PATCH v4 1/2] android/gatt: Handle read characteristic client command Grzegorz Kolodziejczyk
  2014-03-31 16:12 ` [PATCH v4 2/2] android/gatt: Handle write " Grzegorz Kolodziejczyk
@ 2014-03-31 17:16 ` Szymon Janc
  1 sibling, 0 replies; 3+ messages in thread
From: Szymon Janc @ 2014-03-31 17:16 UTC (permalink / raw)
  To: Grzegorz Kolodziejczyk; +Cc: linux-bluetooth

Hi Grzegorz,

On Monday 31 of March 2014 18:12:50 Grzegorz Kolodziejczyk wrote:
> This adds read characteristic client command handling.
> ---
>  android/gatt.c | 139
> ++++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed,
> 137 insertions(+), 2 deletions(-)

Both patches applied, thanks.

-- 
BR
Szymon Janc

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

end of thread, other threads:[~2014-03-31 17:16 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2014-03-31 16:12 [PATCH v4 1/2] android/gatt: Handle read characteristic client command Grzegorz Kolodziejczyk
2014-03-31 16:12 ` [PATCH v4 2/2] android/gatt: Handle write " Grzegorz Kolodziejczyk
2014-03-31 17:16 ` [PATCH v4 1/2] android/gatt: Handle read " Szymon Janc

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.