2014-04-14 08:58:37

by Lukasz Rymanowski

[permalink] [raw]
Subject: [PATCH 1/3] android/client: Add support for client->listen

---
android/client/if-gatt.c | 32 +++++++++++++++++++++++++++++++-
1 file changed, 31 insertions(+), 1 deletion(-)

diff --git a/android/client/if-gatt.c b/android/client/if-gatt.c
index 250472a..f421421 100644
--- a/android/client/if-gatt.c
+++ b/android/client/if-gatt.c
@@ -576,6 +576,13 @@ static void gattc_read_remote_rssi_cb(int client_if, bt_bdaddr_t *bda, int rssi,
client_if, bt_bdaddr_t2str(bda, buf), rssi, status);
}

+/* Callback invoked in response to listen */
+static void gattc_listen_cb(int status, int client_if)
+{
+ haltest_info("%s: client_if=%d status=%d\n", __func__, client_if,
+ status);
+}
+
static const btgatt_client_callbacks_t btgatt_client_callbacks = {
.register_client_cb = gattc_register_client_cb,
.scan_result_cb = gattc_scan_result_cb,
@@ -593,7 +600,8 @@ static const btgatt_client_callbacks_t btgatt_client_callbacks = {
.read_descriptor_cb = gattc_read_descriptor_cb,
.write_descriptor_cb = gattc_write_descriptor_cb,
.execute_write_cb = gattc_execute_write_cb,
- .read_remote_rssi_cb = gattc_read_remote_rssi_cb
+ .read_remote_rssi_cb = gattc_read_remote_rssi_cb,
+ .listen_cb = gattc_listen_cb,
};

/* BT-GATT Server callbacks */
@@ -922,6 +930,27 @@ static void disconnect_p(int argc, const char **argv)
EXEC(if_gatt->client->disconnect, client_if, &bd_addr, conn_id);
}

+/* listen */
+
+/* Same completion as unregister for now, start stop is not auto completed */
+#define listen_c unregister_client_c
+
+static void listen_p(int argc, const char **argv)
+{
+ int client_if;
+ int start = 1;
+
+ RETURN_IF_NULL(if_gatt);
+
+ VERIFY_CLIENT_IF(2, client_if);
+
+ /* start */
+ if (argc >= 4)
+ start = atoi(argv[3]);
+
+ EXEC(if_gatt->client->listen, client_if, start);
+}
+
/* refresh */

static void refresh_c(int argc, const char **argv, enum_func *enum_func,
@@ -1405,6 +1434,7 @@ static struct method client_methods[] = {
STD_METHODCH(get_device_type, "<addr>"),
STD_METHODCH(test_command,
"<cmd> <addr> <uuid> [u1] [u2] [u3] [u4] [u5]"),
+ STD_METHODCH(listen, "<client_if> [1|0]"),
END_METHOD
};

--
1.8.4



2014-04-14 09:58:23

by Szymon Janc

[permalink] [raw]
Subject: Re: [PATCH 1/3] android/client: Add support for client->listen

Hi Ɓukasz,

On Monday 14 of April 2014 10:58:37 Lukasz Rymanowski wrote:
> ---
> android/client/if-gatt.c | 32 +++++++++++++++++++++++++++++++-
> 1 file changed, 31 insertions(+), 1 deletion(-)
>
> diff --git a/android/client/if-gatt.c b/android/client/if-gatt.c
> index 250472a..f421421 100644
> --- a/android/client/if-gatt.c
> +++ b/android/client/if-gatt.c
> @@ -576,6 +576,13 @@ static void gattc_read_remote_rssi_cb(int client_if, bt_bdaddr_t *bda, int rssi,
> client_if, bt_bdaddr_t2str(bda, buf), rssi, status);
> }
>
> +/* Callback invoked in response to listen */
> +static void gattc_listen_cb(int status, int client_if)
> +{
> + haltest_info("%s: client_if=%d status=%d\n", __func__, client_if,
> + status);
> +}
> +
> static const btgatt_client_callbacks_t btgatt_client_callbacks = {
> .register_client_cb = gattc_register_client_cb,
> .scan_result_cb = gattc_scan_result_cb,
> @@ -593,7 +600,8 @@ static const btgatt_client_callbacks_t btgatt_client_callbacks = {
> .read_descriptor_cb = gattc_read_descriptor_cb,
> .write_descriptor_cb = gattc_write_descriptor_cb,
> .execute_write_cb = gattc_execute_write_cb,
> - .read_remote_rssi_cb = gattc_read_remote_rssi_cb
> + .read_remote_rssi_cb = gattc_read_remote_rssi_cb,
> + .listen_cb = gattc_listen_cb,
> };
>
> /* BT-GATT Server callbacks */
> @@ -922,6 +930,27 @@ static void disconnect_p(int argc, const char **argv)
> EXEC(if_gatt->client->disconnect, client_if, &bd_addr, conn_id);
> }
>
> +/* listen */
> +
> +/* Same completion as unregister for now, start stop is not auto completed */
> +#define listen_c unregister_client_c
> +
> +static void listen_p(int argc, const char **argv)
> +{
> + int client_if;
> + int start = 1;
> +
> + RETURN_IF_NULL(if_gatt);
> +
> + VERIFY_CLIENT_IF(2, client_if);
> +
> + /* start */
> + if (argc >= 4)
> + start = atoi(argv[3]);
> +
> + EXEC(if_gatt->client->listen, client_if, start);
> +}
> +
> /* refresh */
>
> static void refresh_c(int argc, const char **argv, enum_func *enum_func,
> @@ -1405,6 +1434,7 @@ static struct method client_methods[] = {
> STD_METHODCH(get_device_type, "<addr>"),
> STD_METHODCH(test_command,
> "<cmd> <addr> <uuid> [u1] [u2] [u3] [u4] [u5]"),
> + STD_METHODCH(listen, "<client_if> [1|0]"),
> END_METHOD
> };

This patch is now upstream, thanks.

--
Best regards,
Szymon Janc

2014-04-14 08:58:39

by Lukasz Rymanowski

[permalink] [raw]
Subject: [PATCH 3/3] android/gatt: Add support for client->listen

This patch adds support for gatt client listen which start/stop
advertising
---
android/gatt.c | 66 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++--
1 file changed, 64 insertions(+), 2 deletions(-)

diff --git a/android/gatt.c b/android/gatt.c
index 53131d4..e076a72 100644
--- a/android/gatt.c
+++ b/android/gatt.c
@@ -130,6 +130,8 @@ static struct queue *conn_list = NULL; /* Connected devices */
static struct queue *conn_wait_queue = NULL; /* Devs waiting to connect */
static struct queue *disc_dev_list = NULL; /* Disconnected devices */

+static struct queue *listen_clients = NULL;
+
static void bt_le_discovery_stop_cb(void);

static void android2uuid(const uint8_t *uuid, bt_uuid_t *dst)
@@ -1279,12 +1281,65 @@ reply:
put_device_on_disc_list(dev);
}

+static void send_client_listen_notify(int32_t id, int32_t status)
+{
+ struct hal_ev_gatt_client_listen ev;
+
+ /* Server if because of typo in android headers */
+ ev.server_if = id;
+ ev.status = status;
+
+ ipc_send_notif(hal_ipc, HAL_SERVICE_ID_GATT,
+ HAL_EV_GATT_CLIENT_LISTEN,
+ sizeof(ev), &ev);
+ if (!status)
+ queue_remove(listen_clients, INT_TO_PTR(id));
+}
+
+static void set_advertising_cb(uint8_t status, void *user_data)
+{
+ int32_t client_id = PTR_TO_INT(user_data);
+
+ send_client_listen_notify(client_id, status);
+}
+
static void handle_client_listen(const void *buf, uint16_t len)
{
+ const struct hal_cmd_gatt_client_listen *cmd = buf;
+ uint8_t status;
+
DBG("");

+ if (queue_find(listen_clients, match_by_value,
+ INT_TO_PTR(cmd->client_if))) {
+
+ status = HAL_STATUS_SUCCESS;
+ goto reply;
+ }
+
+ if (!queue_push_tail(listen_clients, INT_TO_PTR(cmd->client_if))) {
+ error("gatt: Could not put client on listen queue");
+ status = HAL_STATUS_FAILED;
+ goto reply;
+ }
+
+ if (!bt_le_set_advertising(cmd->start, set_advertising_cb,
+ INT_TO_PTR(cmd->client_if))) {
+ error("gatt: Could not set advertising");
+ status = HAL_STATUS_FAILED;
+ queue_remove(listen_clients, INT_TO_PTR(cmd->client_if));
+ goto reply;
+ }
+
+ status = HAL_STATUS_SUCCESS;
+
+reply:
ipc_send_rsp(hal_ipc, HAL_SERVICE_ID_GATT, HAL_OP_GATT_CLIENT_LISTEN,
- HAL_STATUS_FAILED);
+ status);
+
+ if (status != HAL_STATUS_SUCCESS)
+ send_client_listen_notify(cmd->client_if, GATT_FAILURE);
+
}

static void handle_client_refresh(const void *buf, uint16_t len)
@@ -3008,9 +3063,10 @@ bool bt_gatt_register(struct ipc *ipc, const bdaddr_t *addr)
gatt_clients = queue_new();
gatt_servers = queue_new();
disc_dev_list = queue_new();
+ listen_clients = queue_new();

if (!conn_list || !conn_wait_queue || !gatt_clients || !gatt_servers ||
- !disc_dev_list) {
+ !disc_dev_list || !listen_clients) {
error("gatt: Failed to allocate memory for queues");

queue_destroy(gatt_servers, NULL);
@@ -3028,6 +3084,9 @@ bool bt_gatt_register(struct ipc *ipc, const bdaddr_t *addr)
queue_destroy(disc_dev_list, NULL);
disc_dev_list = NULL;

+ queue_destroy(listen_clients, NULL);
+ listen_clients = NULL;
+
return false;
}

@@ -3062,4 +3121,7 @@ void bt_gatt_unregister(void)

queue_destroy(disc_dev_list, destroy_device);
disc_dev_list = NULL;
+
+ queue_destroy(listen_clients, NULL);
+ listen_clients = NULL;
}
--
1.8.4


2014-04-14 08:58:38

by Lukasz Rymanowski

[permalink] [raw]
Subject: [PATCH 2/3] android/bluetooth: Expose gap api to set advertising

With this patch it is possible to start/stop advertising by gap clients
---
android/bluetooth.c | 43 +++++++++++++++++++++++++++++++++++++++++++
android/bluetooth.h | 4 ++++
2 files changed, 47 insertions(+)

diff --git a/android/bluetooth.c b/android/bluetooth.c
index b3fad59..6624492 100644
--- a/android/bluetooth.c
+++ b/android/bluetooth.c
@@ -2882,6 +2882,49 @@ static bool stop_discovery(uint8_t type)
return false;
}

+struct adv_user_data {
+ bt_le_set_advertising_done cb;
+ void *user_data;
+};
+
+static void set_advertising_cb(uint8_t status, uint16_t length,
+ const void *param, void *user_data)
+{
+ struct adv_user_data *data = user_data;
+
+ DBG("");
+
+ if (status)
+ error("Failed to set adverising for index %u: %s (0x%02x))",
+ adapter.index, mgmt_errstr(status), status);
+
+ data->cb(status, data->user_data);
+ free(data);
+ return;
+}
+
+bool bt_le_set_advertising(bool advertising, bt_le_set_advertising_done cb,
+ void *user_data)
+{
+ struct adv_user_data *data;
+ uint8_t adv = advertising ? 0x01 : 0x00;
+
+ data = new0(struct adv_user_data, 1);
+ if (!data)
+ return false;
+
+ data->cb = cb;
+ data->user_data = user_data;
+
+ if (mgmt_send(mgmt_if, MGMT_OP_SET_ADVERTISING, adapter.index,
+ sizeof(adv), &adv, set_advertising_cb, data, NULL) > 0)
+ return true;
+
+ error("Failed to set advertising");
+ free(data);
+ return false;
+}
+
bool bt_le_discovery_stop(bt_le_discovery_stopped cb)
{
if (!adapter.cur_discovery_type) {
diff --git a/android/bluetooth.h b/android/bluetooth.h
index 807ebe7..3eddf16 100644
--- a/android/bluetooth.h
+++ b/android/bluetooth.h
@@ -42,3 +42,7 @@ bool bt_le_discovery_start(bt_le_device_found cb);

typedef void (*bt_le_discovery_stopped)(void);
bool bt_le_discovery_stop(bt_le_discovery_stopped cb);
+
+typedef void (*bt_le_set_advertising_done)(uint8_t status, void *user_data);
+bool bt_le_set_advertising(bool advertising, bt_le_set_advertising_done cb,
+ void *user_data);
--
1.8.4