diff --git a/android/gatt.c b/android/gatt.c
index 11612e6..6d29ccb 100644
--- a/android/gatt.c
+++ b/android/gatt.c
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);
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;
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;
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)
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)
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;
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;
}
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;
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);
* 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;
}
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;
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;
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;
DBG("");
- if (!find_client_by_id(cmd->client_if)) {
+ if (!find_app_by_id(cmd->client_if)) {
status = HAL_STATUS_FAILED;
goto failed;
}
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;
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;
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:
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;
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;
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;
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;
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;
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;
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;
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;
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;