2014-04-28 13:31:59

by Jakub Tyszkowski

[permalink] [raw]
Subject: [PATCH 0/7] android/gatt: Further connection handling refactor

This patch set unifies how client and server connections are handled.

Last patch adds the actual server connection handling, which mostly use common
code.

Jakub Tyszkowski (7):
android/gatt: Use common struct for client and server applications
android/gatt: Store client and server apps on a single list
android/gatt: Rename connection list
android/gatt: Rename connection struct
android/gatt: Fix some whitespace issues
ndroid/gatt: Extract common code from client_connect
android/gatt: Handle Server apps connections

android/gatt.c | 488 +++++++++++++++++++++++++++++----------------------------
1 file changed, 248 insertions(+), 240 deletions(-)

--
1.9.1



2014-04-28 16:01:55

by Szymon Janc

[permalink] [raw]
Subject: Re: [PATCH 0/7] android/gatt: Further connection handling refactor

Hi Jakub,

On Monday 28 of April 2014 15:31:59 Jakub Tyszkowski wrote:
> This patch set unifies how client and server connections are handled.
>
> Last patch adds the actual server connection handling, which mostly use common
> code.
>
> Jakub Tyszkowski (7):
> android/gatt: Use common struct for client and server applications
> android/gatt: Store client and server apps on a single list
> android/gatt: Rename connection list
> android/gatt: Rename connection struct
> android/gatt: Fix some whitespace issues
> ndroid/gatt: Extract common code from client_connect
> android/gatt: Handle Server apps connections
>
> android/gatt.c | 488 +++++++++++++++++++++++++++++----------------------------
> 1 file changed, 248 insertions(+), 240 deletions(-)

All patches applied. Thanks.
(3 and 5 where squashed, also some other minor changes we discussed are present)


--
Best regards,
Szymon Janc

2014-04-28 13:32:06

by Jakub Tyszkowski

[permalink] [raw]
Subject: [PATCH 7/7] android/gatt: Handle Server apps connections

This handles server connections.
---
android/gatt.c | 44 ++++++++++++++++++++++++++++++++++++++++----
1 file changed, 40 insertions(+), 4 deletions(-)

diff --git a/android/gatt.c b/android/gatt.c
index e775f19..e634aa6 100644
--- a/android/gatt.c
+++ b/android/gatt.c
@@ -628,12 +628,29 @@ static void send_client_connection_notify(struct app_connection *connection,
sizeof(ev), &ev);
}

+static void send_server_connection_notify(struct app_connection *connection,
+ bool connected)
+{
+ struct hal_ev_gatt_server_connection ev;
+
+ ev.server_if = connection->app->id;
+ ev.conn_id = connection->id;
+ ev.connected = connected;
+
+ bdaddr2android(&connection->device->bdaddr, &ev.bdaddr);
+
+ ipc_send_notif(hal_ipc, HAL_SERVICE_ID_GATT,
+ HAL_EV_GATT_SERVER_CONNECTION, sizeof(ev), &ev);
+}
+
static void send_app_disconnect_notify(struct app_connection *connection,
int32_t status)
{
if (connection->app->type == APP_CLIENT)
send_client_disconnection_notify(connection, status);
- /* TODO: handle APP_SERVER */
+ else
+ send_server_connection_notify(connection, status ? true :
+ false);
}

static void send_app_connect_notify(struct app_connection *connection,
@@ -641,7 +658,9 @@ static void send_app_connect_notify(struct app_connection *connection,
{
if (connection->app->type == APP_CLIENT)
send_client_connection_notify(connection, status);
- /* TODO: handle APP_SERVER */
+ else
+ send_server_connection_notify(connection, status ? false :
+ true);
}

static void disconnect_notify_by_device(void *data, void *user_data)
@@ -3125,18 +3144,35 @@ failed:

static void handle_server_connect(const void *buf, uint16_t len)
{
+ const struct hal_cmd_gatt_server_connect *cmd = buf;
+ uint8_t status;
+ bdaddr_t addr;
+
DBG("");

+ status = handle_connect(cmd->server_if, &addr, cmd->is_direct);
+
ipc_send_rsp(hal_ipc, HAL_SERVICE_ID_GATT, HAL_OP_GATT_SERVER_CONNECT,
- HAL_STATUS_FAILED);
+ status);
}

static void handle_server_disconnect(const void *buf, uint16_t len)
{
+ const struct hal_cmd_gatt_server_disconnect *cmd = buf;
+ struct app_connection *conn;
+ uint8_t status;
+
DBG("");

+ /* TODO: should we care to match also bdaddr when conn_id is unique? */
+ conn = find_connection_by_id(cmd->conn_id);
+ if (conn)
+ trigger_disconnection(conn);
+
+ status = HAL_STATUS_SUCCESS;
+
ipc_send_rsp(hal_ipc, HAL_SERVICE_ID_GATT,
- HAL_OP_GATT_SERVER_DISCONNECT, HAL_STATUS_FAILED);
+ HAL_OP_GATT_SERVER_DISCONNECT, status);
}

static void handle_server_add_service(const void *buf, uint16_t len)
--
1.9.1


2014-04-28 13:32:05

by Jakub Tyszkowski

[permalink] [raw]
Subject: [PATCH 6/7] ndroid/gatt: Extract common code from client_connect

Code extracted from client connect handler will be reused by server
connect handler
---
android/gatt.c | 60 +++++++++++++++++++++++++++++-----------------------------
1 file changed, 30 insertions(+), 30 deletions(-)

diff --git a/android/gatt.c b/android/gatt.c
index 549bb30..e775f19 100644
--- a/android/gatt.c
+++ b/android/gatt.c
@@ -1251,56 +1251,56 @@ static struct app_connection *find_conn(const bdaddr_t *addr, int32_t app_id,
&conn_match);
}

-static void handle_client_connect(const void *buf, uint16_t len)
+static uint8_t handle_connect(int32_t app_id, const bdaddr_t *addr,
+ bool is_direct)
{
- const struct hal_cmd_gatt_client_connect *cmd = buf;
struct app_connection conn_match;
struct app_connection *conn;
struct gatt_device *device;
- struct gatt_app *client;
- bdaddr_t addr;
- uint8_t status;
+ struct gatt_app *app;

DBG("");

- client = find_app_by_id(cmd->client_if);
- if (!client) {
- status = HAL_STATUS_FAILED;
- goto reply;
- }
-
- android2bdaddr(&cmd->bdaddr, &addr);
+ app = find_app_by_id(app_id);
+ if (!app)
+ return HAL_STATUS_FAILED;

- device = find_device_by_addr(&addr);
+ device = find_device_by_addr(addr);
if (!device) {
- device = create_device(&addr);
- if (!device) {
- status = HAL_STATUS_FAILED;
- goto reply;
- }
+ device = create_device(addr);
+ if (!device)
+ return HAL_STATUS_FAILED;
}

conn_match.device = device;
- conn_match.app = client;
+ conn_match.app = app;

conn = queue_find(app_connections, match_connection_by_device_and_app,
&conn_match);
if (!conn) {
- conn = create_connection(device, client);
- if (!conn) {
- status = HAL_STATUS_NOMEM;
- goto reply;
- }
+ conn = create_connection(device, app);
+ if (!conn)
+ return HAL_STATUS_NOMEM;
}

- if (!trigger_connection(conn)) {
- status = HAL_STATUS_FAILED;
- goto reply;
- }
+ if (!trigger_connection(conn))
+ return HAL_STATUS_FAILED;

- status = HAL_STATUS_SUCCESS;
+ return HAL_STATUS_SUCCESS;
+}
+
+static void handle_client_connect(const void *buf, uint16_t len)
+{
+ const struct hal_cmd_gatt_client_connect *cmd = buf;
+ uint8_t status;
+ bdaddr_t addr;
+
+ DBG("");
+
+ android2bdaddr(&cmd->bdaddr, &addr);
+
+ status = handle_connect(cmd->client_if, &addr, cmd->is_direct);

-reply:
ipc_send_rsp(hal_ipc, HAL_SERVICE_ID_GATT, HAL_OP_GATT_CLIENT_CONNECT,
status);
}
--
1.9.1


2014-04-28 13:32:04

by Jakub Tyszkowski

[permalink] [raw]
Subject: [PATCH 5/7] android/gatt: Fix some whitespace issues

---
android/gatt.c | 13 +++++--------
1 file changed, 5 insertions(+), 8 deletions(-)

diff --git a/android/gatt.c b/android/gatt.c
index 706d009..549bb30 100644
--- a/android/gatt.c
+++ b/android/gatt.c
@@ -972,8 +972,7 @@ static void connect_cb(GIOChannel *io, GError *gerr, gpointer user_data)
reply:
data.dev = dev;
data.status = status;
- queue_foreach(app_connections, send_app_connect_notifications,
- &data);
+ queue_foreach(app_connections, send_app_connect_notifications, &data);
device_unref(dev);

/* Check if we should restart scan */
@@ -1248,9 +1247,8 @@ static struct app_connection *find_conn(const bdaddr_t *addr, int32_t app_id,
conn_match.device = dev;
conn_match.app = app;

- return queue_find(app_connections,
- match_connection_by_device_and_app,
- &conn_match);
+ return queue_find(app_connections, match_connection_by_device_and_app,
+ &conn_match);
}

static void handle_client_connect(const void *buf, uint16_t len)
@@ -1285,9 +1283,8 @@ static void handle_client_connect(const void *buf, uint16_t len)
conn_match.device = device;
conn_match.app = client;

- conn = queue_find(app_connections,
- match_connection_by_device_and_app,
- &conn_match);
+ conn = queue_find(app_connections, match_connection_by_device_and_app,
+ &conn_match);
if (!conn) {
conn = create_connection(device, client);
if (!conn) {
--
1.9.1


2014-04-28 13:32:03

by Jakub Tyszkowski

[permalink] [raw]
Subject: [PATCH 4/7] android/gatt: Rename connection struct

This represents client or server application connection and not the io
connection thus should be named properly.
---
android/gatt.c | 94 +++++++++++++++++++++++++++++-----------------------------
1 file changed, 47 insertions(+), 47 deletions(-)

diff --git a/android/gatt.c b/android/gatt.c
index 2d860ff..706d009 100644
--- a/android/gatt.c
+++ b/android/gatt.c
@@ -118,7 +118,7 @@ struct service {
struct notification_data {
struct hal_gatt_srvc_id service;
struct hal_gatt_gatt_id ch;
- struct connection *conn;
+ struct app_connection *conn;
guint notif_id;
guint ind_id;
int ref;
@@ -140,7 +140,7 @@ struct gatt_device {
int conn_cnt;
};

-struct connection {
+struct app_connection {
struct gatt_device *device;
struct gatt_app *app;
int32_t id;
@@ -303,7 +303,7 @@ static bool match_pending_device(const void *data, const void *user_data)

static bool match_connection_by_id(const void *data, const void *user_data)
{
- const struct connection *conn = data;
+ const struct app_connection *conn = data;
const int32_t id = PTR_TO_INT(user_data);

return conn->id == id;
@@ -312,13 +312,13 @@ static bool match_connection_by_id(const void *data, const void *user_data)
static bool match_connection_by_device_and_app(const void *data,
const void *user_data)
{
- const struct connection *conn = data;
- const struct connection *match = user_data;
+ const struct app_connection *conn = data;
+ const struct app_connection *match = user_data;

return conn->device == match->device && conn->app == match->app;
}

-static struct connection *find_connection_by_id(int32_t conn_id)
+static struct app_connection *find_connection_by_id(int32_t conn_id)
{
return queue_find(app_connections, match_connection_by_id,
INT_TO_PTR(conn_id));
@@ -326,7 +326,7 @@ static struct connection *find_connection_by_id(int32_t conn_id)

static bool match_connection_by_device(const void *data, const void *user_data)
{
- const struct connection *conn = data;
+ const struct app_connection *conn = data;
const struct gatt_device *dev = user_data;

return conn->device == dev;
@@ -334,7 +334,7 @@ static bool match_connection_by_device(const void *data, const void *user_data)

static bool match_connection_by_app(const void *data, const void *user_data)
{
- const struct connection *conn = data;
+ const struct app_connection *conn = data;
const struct gatt_app *app = user_data;

return conn->app == app;
@@ -598,7 +598,7 @@ failed:
status);
}

-static void send_client_disconnection_notify(struct connection *connection,
+static void send_client_disconnection_notify(struct app_connection *connection,
int32_t status)
{
struct hal_ev_gatt_client_disconnect ev;
@@ -613,7 +613,7 @@ static void send_client_disconnection_notify(struct connection *connection,
HAL_EV_GATT_CLIENT_DISCONNECT, sizeof(ev), &ev);
}

-static void send_client_connection_notify(struct connection *connection,
+static void send_client_connection_notify(struct app_connection *connection,
int32_t status)
{
struct hal_ev_gatt_client_connect ev;
@@ -628,7 +628,7 @@ static void send_client_connection_notify(struct connection *connection,
sizeof(ev), &ev);
}

-static void send_app_disconnect_notify(struct connection *connection,
+static void send_app_disconnect_notify(struct app_connection *connection,
int32_t status)
{
if (connection->app->type == APP_CLIENT)
@@ -636,7 +636,7 @@ static void send_app_disconnect_notify(struct connection *connection,
/* TODO: handle APP_SERVER */
}

-static void send_app_connect_notify(struct connection *connection,
+static void send_app_connect_notify(struct app_connection *connection,
int32_t status)
{
if (connection->app->type == APP_CLIENT)
@@ -646,7 +646,7 @@ static void send_app_connect_notify(struct connection *connection,

static void disconnect_notify_by_device(void *data, void *user_data)
{
- struct connection *conn = data;
+ struct app_connection *conn = data;
struct gatt_device *dev = user_data;

if (dev != conn->device)
@@ -694,7 +694,7 @@ static void device_unref(struct gatt_device *device)

static void destroy_connection(void *data)
{
- struct connection *conn = data;
+ struct app_connection *conn = data;

if (!queue_find(gatt_devices, match_by_value, conn->device))
goto cleanup;
@@ -738,7 +738,7 @@ static void send_client_primary_notify(void *data, void *user_data)
sizeof(ev), &ev);
}

-static void send_client_all_primary(struct connection *connection,
+static void send_client_all_primary(struct app_connection *connection,
int32_t status)
{
struct hal_ev_gatt_client_search_complete ev;
@@ -804,7 +804,7 @@ static struct service *create_service(uint8_t id, bool primary, char *uuid,

static void primary_cb(uint8_t status, GSList *services, void *user_data)
{
- struct connection *conn = user_data;
+ struct app_connection *conn = user_data;
GSList *l;
int32_t gatt_status;
uint8_t instance_id;
@@ -923,7 +923,7 @@ struct connect_data {

static void send_app_connect_notifications(void *data, void *user_data)
{
- struct connection *conn = data;
+ struct app_connection *conn = data;
struct connect_data *con_data = user_data;

if (conn->device == con_data->dev)
@@ -1119,14 +1119,14 @@ static struct gatt_device *create_device(const bdaddr_t *addr)
return device_ref(dev);
}

-static struct connection *create_connection(struct gatt_device *device,
+static struct app_connection *create_connection(struct gatt_device *device,
struct gatt_app *app)
{
- struct connection *new_conn;
+ struct app_connection *new_conn;
static int32_t last_conn_id = 1;

/* Check if already connected */
- new_conn = new0(struct connection, 1);
+ new_conn = new0(struct app_connection, 1);
if (!new_conn)
return NULL;

@@ -1149,7 +1149,7 @@ static struct connection *create_connection(struct gatt_device *device,
return new_conn;
}

-static void trigger_disconnection(struct connection *connection)
+static void trigger_disconnection(struct app_connection *connection)
{
/* Notify client */
if (queue_remove(app_connections, connection))
@@ -1160,7 +1160,7 @@ static void trigger_disconnection(struct connection *connection)

static void app_disconnect_devices(struct gatt_app *client)
{
- struct connection *conn;
+ struct app_connection *conn;

/* find every connection for client record and trigger disconnect */
conn = queue_remove_if(app_connections, match_connection_by_app,
@@ -1173,7 +1173,7 @@ static void app_disconnect_devices(struct gatt_app *client)
}
}

-static bool trigger_connection(struct connection *connection)
+static bool trigger_connection(struct app_connection *connection)
{
switch (connection->device->state) {
case DEVICE_DISCONNECTED:
@@ -1224,10 +1224,10 @@ static void handle_client_unregister(const void *buf, uint16_t len)
HAL_OP_GATT_CLIENT_UNREGISTER, status);
}

-static struct connection *find_conn(const bdaddr_t *addr, int32_t app_id,
+static struct app_connection *find_conn(const bdaddr_t *addr, int32_t app_id,
int32_t app_type)
{
- struct connection conn_match;
+ struct app_connection conn_match;
struct gatt_device *dev = NULL;
struct gatt_app *app;

@@ -1256,8 +1256,8 @@ static struct connection *find_conn(const bdaddr_t *addr, int32_t app_id,
static void handle_client_connect(const void *buf, uint16_t len)
{
const struct hal_cmd_gatt_client_connect *cmd = buf;
- struct connection conn_match;
- struct connection *conn;
+ struct app_connection conn_match;
+ struct app_connection *conn;
struct gatt_device *device;
struct gatt_app *client;
bdaddr_t addr;
@@ -1311,7 +1311,7 @@ reply:
static void handle_client_disconnect(const void *buf, uint16_t len)
{
const struct hal_cmd_gatt_client_disconnect *cmd = buf;
- struct connection *conn;
+ struct app_connection *conn;
uint8_t status;

DBG("");
@@ -1490,7 +1490,7 @@ done:
static void handle_client_search_service(const void *buf, uint16_t len)
{
const struct hal_cmd_gatt_client_search_service *cmd = buf;
- struct connection *conn;
+ struct app_connection *conn;
uint8_t status;

DBG("");
@@ -1567,7 +1567,7 @@ static void send_client_incl_service_notify(const struct service *prim,

struct get_included_data {
struct service *prim;
- struct connection *conn;
+ struct app_connection *conn;
};

static int get_inst_id_of_prim_services(const struct gatt_device *dev)
@@ -1583,7 +1583,7 @@ static int get_inst_id_of_prim_services(const struct gatt_device *dev)
static void get_included_cb(uint8_t status, GSList *included, void *user_data)
{
struct get_included_data *data = user_data;
- struct connection *conn = data->conn;
+ struct app_connection *conn = data->conn;
struct service *service = data->prim;
struct service *incl;
int instance_id;
@@ -1646,7 +1646,7 @@ failed:
GATT_FAILURE);
}

-static bool search_included_services(struct connection *connection,
+static bool search_included_services(struct app_connection *connection,
struct service *service)
{
struct get_included_data *data;
@@ -1667,11 +1667,11 @@ static bool search_included_services(struct connection *connection,
}

static bool find_service(int32_t conn_id, struct element_id *service_id,
- struct connection **connection,
+ struct app_connection **connection,
struct service **service)
{
struct service *srvc;
- struct connection *conn;
+ struct app_connection *conn;

conn = find_connection_by_id(conn_id);
if (!conn) {
@@ -1696,7 +1696,7 @@ static bool find_service(int32_t conn_id, struct element_id *service_id,
static void handle_client_get_included_service(const void *buf, uint16_t len)
{
const struct hal_cmd_gatt_client_get_included_service *cmd = buf;
- struct connection *conn;
+ struct app_connection *conn;
struct service *prim_service;
struct service *incl_service;
struct element_id match_id;
@@ -1857,7 +1857,7 @@ static void handle_client_get_characteristic(const void *buf, uint16_t len)
const struct hal_cmd_gatt_client_get_characteristic *cmd = buf;
struct characteristic *ch;
struct element_id match_id;
- struct connection *conn;
+ struct app_connection *conn;
struct service *srvc;
uint8_t status;

@@ -1998,7 +1998,7 @@ static uint16_t parse_descrs(const uint8_t *pdu, guint16 len,
}

struct discover_desc_data {
- struct connection *conn;
+ struct app_connection *conn;
struct service *srvc;
struct characteristic *ch;

@@ -2009,7 +2009,7 @@ static void gatt_discover_desc_cb(guint8 status, const guint8 *pdu, guint16 len,
gpointer user_data)
{
struct discover_desc_data *data = user_data;
- struct connection *conn = data->conn;
+ struct app_connection *conn = data->conn;
struct service *srvc = data->srvc;
struct characteristic *ch = data->ch;
struct descriptor *descr;
@@ -2045,7 +2045,7 @@ reply:
free(data);
}

-static bool build_descr_cache(struct connection *connection,
+static bool build_descr_cache(struct app_connection *connection,
struct service *srvc,
struct characteristic *ch)
{
@@ -2092,7 +2092,7 @@ static void handle_client_get_descriptor(const void *buf, uint16_t len)
struct service *srvc;
struct element_id srvc_id;
struct element_id char_id;
- struct connection *conn;
+ struct app_connection *conn;
int32_t conn_id;
uint8_t primary;
uint8_t status;
@@ -2233,7 +2233,7 @@ static void handle_client_read_characteristic(const void *buf, uint16_t len)
const struct hal_cmd_gatt_client_read_characteristic *cmd = buf;
struct char_op_data *cb_data;
struct characteristic *ch;
- struct connection *conn;
+ struct app_connection *conn;
struct service *srvc;
struct element_id srvc_id;
struct element_id char_id;
@@ -2328,7 +2328,7 @@ static void handle_client_write_characteristic(const void *buf, uint16_t len)
const struct hal_cmd_gatt_client_write_characteristic *cmd = buf;
struct char_op_data *cb_data = NULL;
struct characteristic *ch;
- struct connection *conn;
+ struct app_connection *conn;
struct service *srvc;
struct element_id srvc_id;
struct element_id char_id;
@@ -2514,7 +2514,7 @@ static void handle_client_read_descriptor(const void *buf, uint16_t len)
struct element_id char_id;
struct element_id descr_id;
struct element_id srvc_id;
- struct connection *conn;
+ struct app_connection *conn;
int32_t conn_id = 0;
uint8_t primary;
uint8_t status;
@@ -2629,7 +2629,7 @@ static void handle_client_write_descriptor(const void *buf, uint16_t len)
struct element_id srvc_id;
struct element_id char_id;
struct element_id descr_id;
- struct connection *conn;
+ struct app_connection *conn;
int32_t conn_id;
uint8_t primary;
uint8_t status;
@@ -2752,7 +2752,7 @@ static void write_execute_cb(guint8 status, const guint8 *pdu, guint16 len,
static void handle_client_execute_write(const void *buf, uint16_t len)
{
const struct hal_cmd_gatt_client_execute_write *cmd = buf;
- struct connection *conn;
+ struct app_connection *conn;
uint8_t status;
uint8_t flags;

@@ -2847,7 +2847,7 @@ static void handle_client_register_for_notification(const void *buf,
struct notification_data *notification;
struct characteristic *c;
struct element_id match_id;
- struct connection *conn;
+ struct app_connection *conn;
int32_t conn_id = 0;
struct service *service;
uint8_t status;
@@ -2953,7 +2953,7 @@ static void handle_client_deregister_for_notification(const void *buf,
{
const struct hal_cmd_gatt_client_deregister_for_notification *cmd = buf;
struct notification_data *notification, notif;
- struct connection *conn;
+ struct app_connection *conn;
int32_t conn_id = 0;
uint8_t status;
int32_t gatt_status;
--
1.9.1


2014-04-28 13:32:02

by Jakub Tyszkowski

[permalink] [raw]
Subject: [PATCH 3/7] android/gatt: Rename connection list

This list will be used for both, client and server apps connections.
---
android/gatt.c | 34 +++++++++++++++++-----------------
1 file changed, 17 insertions(+), 17 deletions(-)

diff --git a/android/gatt.c b/android/gatt.c
index 4f50cc0..2d860ff 100644
--- a/android/gatt.c
+++ b/android/gatt.c
@@ -152,7 +152,7 @@ static bool scanning = false;

static struct queue *gatt_apps = NULL;
static struct queue *gatt_devices = NULL;
-static struct queue *client_connections = NULL;
+static struct queue *app_connections = NULL;

static int32_t app_id = 1;

@@ -320,7 +320,7 @@ static bool match_connection_by_device_and_app(const void *data,

static struct connection *find_connection_by_id(int32_t conn_id)
{
- return queue_find(client_connections, match_connection_by_id,
+ return queue_find(app_connections, match_connection_by_id,
INT_TO_PTR(conn_id));
}

@@ -711,10 +711,10 @@ cleanup:
static void device_disconnect_clients(struct gatt_device *dev)
{
/* Notify disconnection to all clients */
- queue_foreach(client_connections, disconnect_notify_by_device, dev);
+ queue_foreach(app_connections, disconnect_notify_by_device, dev);

/* Remove all clients by given device's */
- queue_remove_all(client_connections, match_connection_by_device, dev,
+ queue_remove_all(app_connections, match_connection_by_device, dev,
destroy_connection);
}

@@ -972,7 +972,7 @@ static void connect_cb(GIOChannel *io, GError *gerr, gpointer user_data)
reply:
data.dev = dev;
data.status = status;
- queue_foreach(client_connections, send_app_connect_notifications,
+ queue_foreach(app_connections, send_app_connect_notifications,
&data);
device_unref(dev);

@@ -1136,7 +1136,7 @@ static struct connection *create_connection(struct gatt_device *device,
new_conn->app = app;
new_conn->id = last_conn_id++;

- if (!queue_push_head(client_connections, new_conn)) {
+ if (!queue_push_head(app_connections, new_conn)) {
error("gatt: Cannot push client on the client queue!?");

free(new_conn);
@@ -1152,7 +1152,7 @@ static struct connection *create_connection(struct gatt_device *device,
static void trigger_disconnection(struct connection *connection)
{
/* Notify client */
- if (queue_remove(client_connections, connection))
+ if (queue_remove(app_connections, connection))
send_app_disconnect_notify(connection, GATT_SUCCESS);

destroy_connection(connection);
@@ -1163,12 +1163,12 @@ static void app_disconnect_devices(struct gatt_app *client)
struct connection *conn;

/* find every connection for client record and trigger disconnect */
- conn = queue_remove_if(client_connections, match_connection_by_app,
+ conn = queue_remove_if(app_connections, match_connection_by_app,
client);
while (conn) {
trigger_disconnection(conn);

- conn = queue_remove_if(client_connections,
+ conn = queue_remove_if(app_connections,
match_connection_by_app, client);
}
}
@@ -1248,7 +1248,7 @@ static struct connection *find_conn(const bdaddr_t *addr, int32_t app_id,
conn_match.device = dev;
conn_match.app = app;

- return queue_find(client_connections,
+ return queue_find(app_connections,
match_connection_by_device_and_app,
&conn_match);
}
@@ -1285,7 +1285,7 @@ static void handle_client_connect(const void *buf, uint16_t len)
conn_match.device = device;
conn_match.app = client;

- conn = queue_find(client_connections,
+ conn = queue_find(app_connections,
match_connection_by_device_and_app,
&conn_match);
if (!conn) {
@@ -3471,11 +3471,11 @@ bool bt_gatt_register(struct ipc *ipc, const bdaddr_t *addr)

gatt_devices = queue_new();
gatt_apps = queue_new();
- client_connections = queue_new();
+ app_connections = queue_new();
listen_clients = queue_new();

if (!gatt_devices || !gatt_apps || !listen_clients ||
- !client_connections) {
+ !app_connections) {
error("gatt: Failed to allocate memory for queues");

queue_destroy(gatt_apps, NULL);
@@ -3484,8 +3484,8 @@ bool bt_gatt_register(struct ipc *ipc, const bdaddr_t *addr)
queue_destroy(gatt_devices, NULL);
gatt_devices = NULL;

- queue_destroy(client_connections, NULL);
- client_connections = NULL;
+ queue_destroy(app_connections, NULL);
+ app_connections = NULL;

queue_destroy(listen_clients, NULL);
listen_clients = NULL;
@@ -3513,8 +3513,8 @@ void bt_gatt_unregister(void)
queue_destroy(gatt_apps, destroy_gatt_app);
gatt_apps = NULL;

- queue_destroy(client_connections, destroy_connection);
- client_connections = NULL;
+ queue_destroy(app_connections, destroy_connection);
+ app_connections = NULL;

queue_destroy(gatt_devices, destroy_device);
gatt_devices = NULL;
--
1.9.1


2014-04-28 13:32:01

by Jakub Tyszkowski

[permalink] [raw]
Subject: [PATCH 2/7] android/gatt: Store client and server apps on a single list

This will simplify and reduce code amount for handling client and server
app as it will use common code.
---
android/gatt.c | 134 +++++++++++++++++++++------------------------------------
1 file changed, 48 insertions(+), 86 deletions(-)

diff --git a/android/gatt.c b/android/gatt.c
index 2e4bb8c..4f50cc0 100644
--- a/android/gatt.c
+++ b/android/gatt.c
@@ -150,11 +150,12 @@ static struct ipc *hal_ipc = NULL;
static bdaddr_t adapter_addr;
static bool scanning = false;

-static struct queue *gatt_clients = NULL;
-static struct queue *gatt_servers = NULL;
+static struct queue *gatt_apps = NULL;
static struct queue *gatt_devices = NULL;
static struct queue *client_connections = NULL;

+static int32_t app_id = 1;
+
static struct queue *listen_clients = NULL;

static void bt_le_discovery_stop_cb(void);
@@ -245,7 +246,7 @@ static void destroy_service(void *data)
free(srvc);
}

-static bool match_client_by_uuid(const void *data, const void *user_data)
+static bool match_app_by_uuid(const void *data, const void *user_data)
{
const uint8_t *exp_uuid = user_data;
const struct gatt_app *client = data;
@@ -253,15 +254,7 @@ static bool match_client_by_uuid(const void *data, const void *user_data)
return !memcmp(exp_uuid, client->uuid, sizeof(client->uuid));
}

-static bool match_server_by_uuid(const void *data, const void *user_data)
-{
- const uint8_t *exp_uuid = user_data;
- const struct gatt_app *server = data;
-
- return !memcmp(exp_uuid, server->uuid, sizeof(server->uuid));
-}
-
-static bool match_client_by_id(const void *data, const void *user_data)
+static bool match_app_by_id(const void *data, const void *user_data)
{
int32_t exp_id = PTR_TO_INT(user_data);
const struct gatt_app *client = data;
@@ -269,22 +262,9 @@ static bool match_client_by_id(const void *data, const void *user_data)
return client->id == exp_id;
}

-static bool match_server_by_id(const void *data, const void *user_data)
-{
- int32_t exp_id = PTR_TO_INT(user_data);
- const struct gatt_app *server = data;
-
- return server->id == exp_id;
-}
-
-static struct gatt_app *find_client_by_id(int32_t id)
-{
- return queue_find(gatt_clients, match_client_by_id, INT_TO_PTR(id));
-}
-
-static struct gatt_app *find_server_by_id(int32_t id)
+static struct gatt_app *find_app_by_id(int32_t id)
{
- return queue_find(gatt_servers, match_server_by_id, INT_TO_PTR(id));
+ return queue_find(gatt_apps, match_app_by_id, INT_TO_PTR(id));
}

static bool match_by_value(const void *data, const void *user_data)
@@ -531,32 +511,27 @@ static void connection_cleanup(struct gatt_device *device)
device_set_state(device, DEVICE_DISCONNECTED);
}

-static void destroy_gatt_client(void *data)
+static void destroy_gatt_app(void *data)
{
- struct gatt_app *client = data;
+ struct gatt_app *app = data;

/* First we want to get all notifications and unregister them.
* We don't pass unregister_notification to queue_destroy,
* because destroy notification performs operations on queue
* too. So remove all elements and then destroy queue.
*/
- while (queue_peek_head(client->notifications)) {
- struct notification_data *notification;
-
- notification = queue_pop_head(client->notifications);
- unregister_notification(notification);
- }

- queue_destroy(client->notifications, free);
+ if (app->type == APP_CLIENT)
+ while (queue_peek_head(app->notifications)) {
+ struct notification_data *notification;

- free(client);
-}
+ notification = queue_pop_head(app->notifications);
+ unregister_notification(notification);
+ }

-static void destroy_gatt_server(void *data)
-{
- struct gatt_app *server = data;
+ queue_destroy(app->notifications, free);

- free(server);
+ free(app);
}

static void handle_client_register(const void *buf, uint16_t len)
@@ -564,14 +539,13 @@ static void handle_client_register(const void *buf, uint16_t len)
const struct hal_cmd_gatt_client_register *cmd = buf;
struct hal_ev_gatt_client_register_client ev;
struct gatt_app *client;
- static int32_t client_cnt = 1;
uint8_t status;

DBG("");

memset(&ev, 0, sizeof(ev));

- if (queue_find(gatt_clients, match_client_by_uuid, &cmd->uuid)) {
+ if (queue_find(gatt_apps, match_app_by_uuid, &cmd->uuid)) {
error("gatt: client uuid is already on list");
status = HAL_STATUS_FAILED;
goto failed;
@@ -588,18 +562,18 @@ static void handle_client_register(const void *buf, uint16_t len)
client->notifications = queue_new();
if (!client->notifications) {
error("gatt: couldn't allocate notifications queue");
- destroy_gatt_client(client);
+ destroy_gatt_app(client);
status = HAL_STATUS_NOMEM;
goto failed;
}

memcpy(client->uuid, cmd->uuid, sizeof(client->uuid));

- client->id = client_cnt++;
+ client->id = app_id++;

- if (!queue_push_head(gatt_clients, client)) {
+ if (!queue_push_head(gatt_apps, client)) {
error("gatt: Cannot push client on the list");
- destroy_gatt_client(client);
+ destroy_gatt_app(client);
status = HAL_STATUS_NOMEM;
goto failed;
}
@@ -1063,7 +1037,7 @@ static void handle_client_scan(const void *buf, uint16_t len)

DBG("new state %d", cmd->start);

- registered = find_client_by_id(cmd->client_if);
+ registered = find_app_by_id(cmd->client_if);
if (!registered) {
error("gatt: Client not registered");
status = HAL_STATUS_FAILED;
@@ -1232,7 +1206,7 @@ static void handle_client_unregister(const void *buf, uint16_t len)

DBG("");

- cl = queue_remove_if(gatt_clients, match_client_by_id,
+ cl = queue_remove_if(gatt_apps, match_app_by_id,
INT_TO_PTR(cmd->client_if));
if (!cl) {
error("gatt: client_if=%d not found", cmd->client_if);
@@ -1242,7 +1216,7 @@ static void handle_client_unregister(const void *buf, uint16_t len)
* for this client. If so, remove this client from those lists.
*/
app_disconnect_devices(cl);
- destroy_gatt_client(cl);
+ destroy_gatt_app(cl);
status = HAL_STATUS_SUCCESS;
}

@@ -1258,11 +1232,7 @@ static struct connection *find_conn(const bdaddr_t *addr, int32_t app_id,
struct gatt_app *app;

/* Check if app is registered */
- if (app_type == APP_CLIENT)
- app = find_client_by_id(app_id);
- else
- app = find_server_by_id(app_id);
-
+ app = find_app_by_id(app_id);
if (!app) {
error("gatt: Client id %d not found", app_id);
return NULL;
@@ -1295,7 +1265,7 @@ static void handle_client_connect(const void *buf, uint16_t len)

DBG("");

- client = find_client_by_id(cmd->client_if);
+ client = find_app_by_id(cmd->client_if);
if (!client) {
status = HAL_STATUS_FAILED;
goto reply;
@@ -1401,7 +1371,7 @@ static void handle_client_listen(const void *buf, uint16_t len)

DBG("");

- if (!find_client_by_id(cmd->client_if)) {
+ if (!find_app_by_id(cmd->client_if)) {
error("gatt: Client not registered");
status = HAL_STATUS_FAILED;
goto reply;
@@ -3034,7 +3004,7 @@ static void handle_client_read_remote_rssi(const void *buf, uint16_t len)

DBG("");

- if (!find_client_by_id(cmd->client_if)) {
+ if (!find_app_by_id(cmd->client_if)) {
status = HAL_STATUS_FAILED;
goto failed;
}
@@ -3084,14 +3054,13 @@ static void handle_server_register(const void *buf, uint16_t len)
const struct hal_cmd_gatt_server_register *cmd = buf;
struct hal_ev_gatt_server_register ev;
struct gatt_app *server;
- static int32_t server_cnt = 1;
uint32_t status;

DBG("");

memset(&ev, 0, sizeof(ev));

- if (queue_find(gatt_servers, match_server_by_uuid, &cmd->uuid)) {
+ if (queue_find(gatt_apps, match_app_by_uuid, &cmd->uuid)) {
error("gatt: Server uuid is already on list");
status = HAL_STATUS_FAILED;
goto failed;
@@ -3107,9 +3076,9 @@ static void handle_server_register(const void *buf, uint16_t len)

memcpy(server->uuid, cmd->uuid, sizeof(server->uuid));

- server->id = server_cnt++;
+ server->id = app_id++;

- if (!queue_push_head(gatt_servers, server)) {
+ if (!queue_push_head(gatt_apps, server)) {
error("gatt: Cannot push server on the list");
free(server);
status = HAL_STATUS_FAILED;
@@ -3142,14 +3111,14 @@ static void handle_server_unregister(const void *buf, uint16_t len)

DBG("");

- server = find_server_by_id(cmd->server_if);
+ server = find_app_by_id(cmd->server_if);
if (!server) {
error("gatt: server_if=%d not found", cmd->server_if);
status = HAL_STATUS_FAILED;
goto failed;
}

- destroy_gatt_server(server);
+ destroy_gatt_app(server);
status = HAL_STATUS_SUCCESS;

failed:
@@ -3183,7 +3152,7 @@ static void handle_server_add_service(const void *buf, uint16_t len)

DBG("");

- server = find_server_by_id(cmd->server_if);
+ server = find_app_by_id(cmd->server_if);
if (!server) {
error("gatt: server_if=%d not found", cmd->server_if);
status = HAL_STATUS_FAILED;
@@ -3212,7 +3181,7 @@ static void handle_server_add_included_service(const void *buf, uint16_t len)

DBG("");

- server = find_server_by_id(cmd->server_if);
+ server = find_app_by_id(cmd->server_if);
if (!server) {
error("gatt: server_if=%d not found", cmd->server_if);
status = HAL_STATUS_FAILED;
@@ -3240,7 +3209,7 @@ static void handle_server_add_characteristic(const void *buf, uint16_t len)

DBG("");

- server = find_server_by_id(cmd->server_if);
+ server = find_app_by_id(cmd->server_if);
if (!server) {
error("gatt: server_if=%d not found", cmd->server_if);
status = HAL_STATUS_FAILED;
@@ -3272,7 +3241,7 @@ static void handle_server_add_descriptor(const void *buf, uint16_t len)

DBG("");

- server = find_server_by_id(cmd->server_if);
+ server = find_app_by_id(cmd->server_if);
if (!server) {
error("gatt: server_if=%d not found", cmd->server_if);
status = HAL_STATUS_FAILED;
@@ -3301,7 +3270,7 @@ static void handle_server_start_service(const void *buf, uint16_t len)

DBG("");

- server = find_server_by_id(cmd->server_if);
+ server = find_app_by_id(cmd->server_if);
if (!server) {
error("gatt: server_if=%d not found", cmd->server_if);
status = HAL_STATUS_FAILED;
@@ -3328,7 +3297,7 @@ static void handle_server_stop_service(const void *buf, uint16_t len)

DBG("");

- server = find_server_by_id(cmd->server_if);
+ server = find_app_by_id(cmd->server_if);
if (!server) {
error("gatt: server_if=%d not found", cmd->server_if);
status = HAL_STATUS_FAILED;
@@ -3354,7 +3323,7 @@ static void handle_server_delete_service(const void *buf, uint16_t len)

DBG("");

- server = find_server_by_id(cmd->server_if);
+ server = find_app_by_id(cmd->server_if);
if (!server) {
error("gatt: server_if=%d not found", cmd->server_if);
status = HAL_STATUS_FAILED;
@@ -3501,20 +3470,16 @@ bool bt_gatt_register(struct ipc *ipc, const bdaddr_t *addr)
DBG("");

gatt_devices = queue_new();
- gatt_clients = queue_new();
- gatt_servers = queue_new();
+ gatt_apps = queue_new();
client_connections = queue_new();
listen_clients = queue_new();

- if (!gatt_devices || !gatt_clients || !gatt_servers ||
- !listen_clients || !client_connections) {
+ if (!gatt_devices || !gatt_apps || !listen_clients ||
+ !client_connections) {
error("gatt: Failed to allocate memory for queues");

- queue_destroy(gatt_servers, NULL);
- gatt_servers = NULL;
-
- queue_destroy(gatt_clients, NULL);
- gatt_clients = NULL;
+ queue_destroy(gatt_apps, NULL);
+ gatt_apps = NULL;

queue_destroy(gatt_devices, NULL);
gatt_devices = NULL;
@@ -3545,11 +3510,8 @@ void bt_gatt_unregister(void)
ipc_unregister(hal_ipc, HAL_SERVICE_ID_GATT);
hal_ipc = NULL;

- queue_destroy(gatt_servers, destroy_gatt_server);
- gatt_servers = NULL;
-
- queue_destroy(gatt_clients, destroy_gatt_client);
- gatt_clients = NULL;
+ queue_destroy(gatt_apps, destroy_gatt_app);
+ gatt_apps = NULL;

queue_destroy(client_connections, destroy_connection);
client_connections = NULL;
--
1.9.1


2014-04-28 13:32:00

by Jakub Tyszkowski

[permalink] [raw]
Subject: [PATCH 1/7] android/gatt: Use common struct for client and server applications

This will allow to handle client and server connection using the same
connection logic.
---
android/gatt.c | 165 +++++++++++++++++++++++++++++++--------------------------
1 file changed, 89 insertions(+), 76 deletions(-)

diff --git a/android/gatt.c b/android/gatt.c
index a7b8659..2e4bb8c 100644
--- a/android/gatt.c
+++ b/android/gatt.c
@@ -70,15 +70,19 @@ static const char const *device_state_str[] = {
"CONNECTED",
};

-struct gatt_client {
- int32_t id;
- uint8_t uuid[16];
- struct queue *notifications;
-};
+typedef enum {
+ APP_CLIENT,
+ APP_SERVER,
+} gatt_app_type_t;

-struct gatt_server {
+struct gatt_app {
int32_t id;
uint8_t uuid[16];
+
+ gatt_app_type_t type;
+
+ /* Valid for client applications */
+ struct queue *notifications;
};

struct element_id {
@@ -138,7 +142,7 @@ struct gatt_device {

struct connection {
struct gatt_device *device;
- struct gatt_client *client;
+ struct gatt_app *app;
int32_t id;
};

@@ -244,7 +248,7 @@ static void destroy_service(void *data)
static bool match_client_by_uuid(const void *data, const void *user_data)
{
const uint8_t *exp_uuid = user_data;
- const struct gatt_client *client = data;
+ const struct gatt_app *client = data;

return !memcmp(exp_uuid, client->uuid, sizeof(client->uuid));
}
@@ -252,7 +256,7 @@ static bool match_client_by_uuid(const void *data, const void *user_data)
static bool match_server_by_uuid(const void *data, const void *user_data)
{
const uint8_t *exp_uuid = user_data;
- const struct gatt_server *server = data;
+ const struct gatt_app *server = data;

return !memcmp(exp_uuid, server->uuid, sizeof(server->uuid));
}
@@ -260,7 +264,7 @@ static bool match_server_by_uuid(const void *data, const void *user_data)
static bool match_client_by_id(const void *data, const void *user_data)
{
int32_t exp_id = PTR_TO_INT(user_data);
- const struct gatt_client *client = data;
+ const struct gatt_app *client = data;

return client->id == exp_id;
}
@@ -268,17 +272,17 @@ static bool match_client_by_id(const void *data, const void *user_data)
static bool match_server_by_id(const void *data, const void *user_data)
{
int32_t exp_id = PTR_TO_INT(user_data);
- const struct gatt_server *server = data;
+ const struct gatt_app *server = data;

return server->id == exp_id;
}

-static struct gatt_client *find_client_by_id(int32_t id)
+static struct gatt_app *find_client_by_id(int32_t id)
{
return queue_find(gatt_clients, match_client_by_id, INT_TO_PTR(id));
}

-static struct gatt_server *find_server_by_id(int32_t id)
+static struct gatt_app *find_server_by_id(int32_t id)
{
return queue_find(gatt_servers, match_server_by_id, INT_TO_PTR(id));
}
@@ -325,13 +329,13 @@ static bool match_connection_by_id(const void *data, const void *user_data)
return conn->id == id;
}

-static bool match_connection_by_device_and_client(const void *data,
+static bool match_connection_by_device_and_app(const void *data,
const void *user_data)
{
const struct connection *conn = data;
const struct connection *match = user_data;

- return conn->device == match->device && conn->client == match->client;
+ return conn->device == match->device && conn->app == match->app;
}

static struct connection *find_connection_by_id(int32_t conn_id)
@@ -348,12 +352,12 @@ static bool match_connection_by_device(const void *data, const void *user_data)
return conn->device == dev;
}

-static bool match_connection_by_client(const void *data, const void *user_data)
+static bool match_connection_by_app(const void *data, const void *user_data)
{
const struct connection *conn = data;
- const struct gatt_client *client = user_data;
+ const struct gatt_app *app = user_data;

- return conn->client == client;
+ return conn->app == app;
}

static struct gatt_device *find_device_by_addr(const bdaddr_t *addr)
@@ -455,14 +459,13 @@ static bool match_char_by_element_id(const void *data, const void *user_data)
static void destroy_notification(void *data)
{
struct notification_data *notification = data;
- struct gatt_client *client;
+ struct gatt_app *app;

if (--notification->ref)
return;

- client = notification->conn->client;
- queue_remove_if(client->notifications, match_notification,
- notification);
+ app = notification->conn->app;
+ queue_remove_if(app->notifications, match_notification, notification);
free(notification);
}

@@ -530,7 +533,7 @@ static void connection_cleanup(struct gatt_device *device)

static void destroy_gatt_client(void *data)
{
- struct gatt_client *client = data;
+ struct gatt_app *client = data;

/* First we want to get all notifications and unregister them.
* We don't pass unregister_notification to queue_destroy,
@@ -551,7 +554,7 @@ static void destroy_gatt_client(void *data)

static void destroy_gatt_server(void *data)
{
- struct gatt_server *server = data;
+ struct gatt_app *server = data;

free(server);
}
@@ -560,7 +563,7 @@ static void handle_client_register(const void *buf, uint16_t len)
{
const struct hal_cmd_gatt_client_register *cmd = buf;
struct hal_ev_gatt_client_register_client ev;
- struct gatt_client *client;
+ struct gatt_app *client;
static int32_t client_cnt = 1;
uint8_t status;

@@ -574,7 +577,8 @@ static void handle_client_register(const void *buf, uint16_t len)
goto failed;
}

- client = new0(struct gatt_client, 1);
+ client = new0(struct gatt_app, 1);
+ client->type = APP_CLIENT;
if (!client) {
error("gatt: cannot allocate memory for registering client");
status = HAL_STATUS_NOMEM;
@@ -625,7 +629,7 @@ static void send_client_disconnection_notify(struct connection *connection,
{
struct hal_ev_gatt_client_disconnect ev;

- ev.client_if = connection->client->id;
+ ev.client_if = connection->app->id;
ev.conn_id = connection->id;
ev.status = status;

@@ -640,7 +644,7 @@ static void send_client_connection_notify(struct connection *connection,
{
struct hal_ev_gatt_client_connect ev;

- ev.client_if = connection->client->id;
+ ev.client_if = connection->app->id;
ev.conn_id = connection->id;
ev.status = status;

@@ -650,16 +654,20 @@ static void send_client_connection_notify(struct connection *connection,
sizeof(ev), &ev);
}

-static void send_client_disconnect_notify(struct connection *connection,
+static void send_app_disconnect_notify(struct connection *connection,
int32_t status)
{
- send_client_disconnection_notify(connection, status);
+ if (connection->app->type == APP_CLIENT)
+ send_client_disconnection_notify(connection, status);
+ /* TODO: handle APP_SERVER */
}

-static void send_client_connect_notify(struct connection *connection,
+static void send_app_connect_notify(struct connection *connection,
int32_t status)
{
- send_client_connection_notify(connection, status);
+ if (connection->app->type == APP_CLIENT)
+ send_client_connection_notify(connection, status);
+ /* TODO: handle APP_SERVER */
}

static void disconnect_notify_by_device(void *data, void *user_data)
@@ -671,10 +679,10 @@ static void disconnect_notify_by_device(void *data, void *user_data)
return;

if (dev->state == DEVICE_CONNECTED)
- send_client_disconnect_notify(conn, GATT_SUCCESS);
+ send_app_disconnect_notify(conn, GATT_SUCCESS);
else if (dev->state == DEVICE_CONNECT_INIT ||
dev->state == DEVICE_CONNECT_READY)
- send_client_connect_notify(conn, GATT_FAILURE);
+ send_app_connect_notify(conn, GATT_FAILURE);
}

static void destroy_device(void *data)
@@ -939,13 +947,13 @@ struct connect_data {
int32_t status;
};

-static void send_client_connect_notifications(void *data, void *user_data)
+static void send_app_connect_notifications(void *data, void *user_data)
{
struct connection *conn = data;
struct connect_data *con_data = user_data;

if (conn->device == con_data->dev)
- send_client_connect_notify(conn, con_data->status);
+ send_app_connect_notify(conn, con_data->status);
}

static void connect_cb(GIOChannel *io, GError *gerr, gpointer user_data)
@@ -990,7 +998,7 @@ static void connect_cb(GIOChannel *io, GError *gerr, gpointer user_data)
reply:
data.dev = dev;
data.status = status;
- queue_foreach(client_connections, send_client_connect_notifications,
+ queue_foreach(client_connections, send_app_connect_notifications,
&data);
device_unref(dev);

@@ -1138,7 +1146,7 @@ static struct gatt_device *create_device(const bdaddr_t *addr)
}

static struct connection *create_connection(struct gatt_device *device,
- struct gatt_client *client)
+ struct gatt_app *app)
{
struct connection *new_conn;
static int32_t last_conn_id = 1;
@@ -1149,9 +1157,9 @@ static struct connection *create_connection(struct gatt_device *device,
return NULL;

/* Make connection id unique to connection record
- * (client, device) pair.
+ * (app, device) pair.
*/
- new_conn->client = client;
+ new_conn->app = app;
new_conn->id = last_conn_id++;

if (!queue_push_head(client_connections, new_conn)) {
@@ -1171,23 +1179,23 @@ static void trigger_disconnection(struct connection *connection)
{
/* Notify client */
if (queue_remove(client_connections, connection))
- send_client_disconnect_notify(connection, GATT_SUCCESS);
+ send_app_disconnect_notify(connection, GATT_SUCCESS);

destroy_connection(connection);
}

-static void client_disconnect_devices(struct gatt_client *client)
+static void app_disconnect_devices(struct gatt_app *client)
{
struct connection *conn;

/* find every connection for client record and trigger disconnect */
- conn = queue_remove_if(client_connections, match_connection_by_client,
+ conn = queue_remove_if(client_connections, match_connection_by_app,
client);
while (conn) {
trigger_disconnection(conn);

conn = queue_remove_if(client_connections,
- match_connection_by_client, client);
+ match_connection_by_app, client);
}
}

@@ -1198,7 +1206,7 @@ static bool trigger_connection(struct connection *connection)
device_set_state(connection->device, DEVICE_CONNECT_INIT);
break;
case DEVICE_CONNECTED:
- send_client_connect_notify(connection, GATT_SUCCESS);
+ send_app_connect_notify(connection, GATT_SUCCESS);
break;
default:
break;
@@ -1220,7 +1228,7 @@ static void handle_client_unregister(const void *buf, uint16_t len)
{
const struct hal_cmd_gatt_client_unregister *cmd = buf;
uint8_t status;
- struct gatt_client *cl;
+ struct gatt_app *cl;

DBG("");

@@ -1233,7 +1241,7 @@ static void handle_client_unregister(const void *buf, uint16_t len)
/* Check if there is any connect request or connected device
* for this client. If so, remove this client from those lists.
*/
- client_disconnect_devices(cl);
+ app_disconnect_devices(cl);
destroy_gatt_client(cl);
status = HAL_STATUS_SUCCESS;
}
@@ -1242,33 +1250,37 @@ static void handle_client_unregister(const void *buf, uint16_t len)
HAL_OP_GATT_CLIENT_UNREGISTER, status);
}

-static struct connection *find_connection_by_addr_and_client_id(bdaddr_t *addr,
- int32_t client_id)
+static struct connection *find_conn(const bdaddr_t *addr, int32_t app_id,
+ int32_t app_type)
{
struct connection conn_match;
struct gatt_device *dev = NULL;
- struct gatt_client *client;
+ struct gatt_app *app;

- /* Check if client is registered */
- client = find_client_by_id(client_id);
- if (!client) {
- error("gatt: Client id %d not found", client_id);
+ /* Check if app is registered */
+ if (app_type == APP_CLIENT)
+ app = find_client_by_id(app_id);
+ else
+ app = find_server_by_id(app_id);
+
+ if (!app) {
+ error("gatt: Client id %d not found", app_id);
return NULL;
}

/* Check if device is known */
dev = find_device_by_addr(addr);
if (!dev) {
- error("gatt: Client id %d not found", client_id);
+ error("gatt: Client id %d not found", app_id);
return NULL;
}

conn_match.device = dev;
- conn_match.client = client;
+ conn_match.app = app;

return queue_find(client_connections,
- match_connection_by_device_and_client,
- &conn_match);
+ match_connection_by_device_and_app,
+ &conn_match);
}

static void handle_client_connect(const void *buf, uint16_t len)
@@ -1277,7 +1289,7 @@ static void handle_client_connect(const void *buf, uint16_t len)
struct connection conn_match;
struct connection *conn;
struct gatt_device *device;
- struct gatt_client *client;
+ struct gatt_app *client;
bdaddr_t addr;
uint8_t status;

@@ -1301,10 +1313,10 @@ static void handle_client_connect(const void *buf, uint16_t len)
}

conn_match.device = device;
- conn_match.client = client;
+ conn_match.app = client;

conn = queue_find(client_connections,
- match_connection_by_device_and_client,
+ match_connection_by_device_and_app,
&conn_match);
if (!conn) {
conn = create_connection(device, client);
@@ -2876,7 +2888,7 @@ static void handle_client_register_for_notification(const void *buf,

android2bdaddr(&cmd->bdaddr, &addr);

- conn = find_connection_by_addr_and_client_id(&addr, cmd->client_if);
+ conn = find_conn(&addr, cmd->client_if, APP_CLIENT);
if (!conn) {
status = HAL_STATUS_FAILED;
goto failed;
@@ -2910,7 +2922,7 @@ static void handle_client_register_for_notification(const void *buf,
sizeof(notification->service));
notification->conn = conn;

- if (queue_find(conn->client->notifications, match_notification,
+ if (queue_find(conn->app->notifications, match_notification,
notification)) {
free(notification);
status = HAL_STATUS_SUCCESS;
@@ -2949,7 +2961,7 @@ static void handle_client_register_for_notification(const void *buf,
*/
notification->ref = 2;

- if (!queue_push_tail(conn->client->notifications, notification)) {
+ if (!queue_push_tail(conn->app->notifications, notification)) {
unregister_notification(notification);
status = HAL_STATUS_FAILED;
goto failed;
@@ -2981,7 +2993,7 @@ static void handle_client_deregister_for_notification(const void *buf,

android2bdaddr(&cmd->bdaddr, &addr);

- conn = find_connection_by_addr_and_client_id(&addr, cmd->client_if);
+ conn = find_conn(&addr, cmd->client_if, APP_CLIENT);
if (!conn) {
status = HAL_STATUS_FAILED;
goto failed;
@@ -2993,7 +3005,7 @@ static void handle_client_deregister_for_notification(const void *buf,
memcpy(&notif.service, &cmd->srvc_id, sizeof(notif.service));
notif.conn = conn;

- notification = queue_find(conn->client->notifications,
+ notification = queue_find(conn->app->notifications,
match_notification, &notif);
if (!notification) {
status = HAL_STATUS_FAILED;
@@ -3071,7 +3083,7 @@ static void handle_server_register(const void *buf, uint16_t len)
{
const struct hal_cmd_gatt_server_register *cmd = buf;
struct hal_ev_gatt_server_register ev;
- struct gatt_server *server;
+ struct gatt_app *server;
static int32_t server_cnt = 1;
uint32_t status;

@@ -3085,7 +3097,8 @@ static void handle_server_register(const void *buf, uint16_t len)
goto failed;
}

- server = new0(struct gatt_server, 1);
+ server = new0(struct gatt_app, 1);
+ server->type = APP_SERVER;
if (!server) {
error("gatt: Cannot allocate memory for registering server");
status = HAL_STATUS_NOMEM;
@@ -3125,7 +3138,7 @@ static void handle_server_unregister(const void *buf, uint16_t len)
{
const struct hal_cmd_gatt_server_unregister *cmd = buf;
uint8_t status;
- struct gatt_server *server;
+ struct gatt_app *server;

DBG("");

@@ -3164,7 +3177,7 @@ static void handle_server_add_service(const void *buf, uint16_t len)
{
const struct hal_cmd_gatt_server_add_service *cmd = buf;
char uuidstr[MAX_LEN_UUID_STR];
- struct gatt_server *server;
+ struct gatt_app *server;
struct element_id srvc_id;
uint8_t status;

@@ -3194,7 +3207,7 @@ failed:
static void handle_server_add_included_service(const void *buf, uint16_t len)
{
const struct hal_cmd_gatt_server_add_inc_service *cmd = buf;
- struct gatt_server *server;
+ struct gatt_app *server;
uint8_t status;

DBG("");
@@ -3221,7 +3234,7 @@ static void handle_server_add_characteristic(const void *buf, uint16_t len)
{
const struct hal_cmd_gatt_server_add_characteristic *cmd = buf;
char uuidstr[MAX_LEN_UUID_STR];
- struct gatt_server *server;
+ struct gatt_app *server;
bt_uuid_t char_uuid;
uint8_t status;

@@ -3253,7 +3266,7 @@ static void handle_server_add_descriptor(const void *buf, uint16_t len)
{
const struct hal_cmd_gatt_server_add_descriptor *cmd = buf;
char uuidstr[MAX_LEN_UUID_STR];
- struct gatt_server *server;
+ struct gatt_app *server;
bt_uuid_t descr_uuid;
uint8_t status;

@@ -3283,7 +3296,7 @@ failed:
static void handle_server_start_service(const void *buf, uint16_t len)
{
const struct hal_cmd_gatt_server_start_service *cmd = buf;
- struct gatt_server *server;
+ struct gatt_app *server;
uint8_t status;

DBG("");
@@ -3310,7 +3323,7 @@ failed:
static void handle_server_stop_service(const void *buf, uint16_t len)
{
const struct hal_cmd_gatt_server_stop_service *cmd = buf;
- struct gatt_server *server;
+ struct gatt_app *server;
uint8_t status;

DBG("");
@@ -3336,7 +3349,7 @@ failed:
static void handle_server_delete_service(const void *buf, uint16_t len)
{
const struct hal_cmd_gatt_server_delete_service *cmd = buf;
- struct gatt_server *server;
+ struct gatt_app *server;
uint8_t status;

DBG("");
--
1.9.1