struct gatt_device *dev;
dev = new0(struct gatt_device, 1);
- if (!dev)
- return NULL;
bacpy(&dev->bdaddr, addr);
dev->services = queue_new();
- if (!dev->services) {
- error("gatt: Failed to allocate memory for client");
- destroy_device(dev);
- return NULL;
- }
-
dev->autoconnect_apps = queue_new();
- if (!dev->autoconnect_apps) {
- error("gatt: Failed to allocate memory for client");
- destroy_device(dev);
- return NULL;
- }
-
dev->pending_requests = queue_new();
- if (!dev->pending_requests) {
- error("gatt: Failed to allocate memory for client");
- destroy_device(dev);
- return NULL;
- }
- if (!queue_push_head(gatt_devices, dev)) {
- error("gatt: Cannot push device to queue");
- destroy_device(dev);
- return NULL;
- }
+ queue_push_head(gatt_devices, dev);
return device_ref(dev);
}
if (conn->device != device)
return;
+ if (!conn->app) {
+ error("gatt: can't notify mtu - no app registered for conn");
+ return;
+ }
+
switch (conn->app->type) {
case GATT_CLIENT:
notify_client_mtu_change(conn, true);
/* Check if already connected */
new_conn = new0(struct app_connection, 1);
- if (!new_conn)
- return NULL;
/* Make connection id unique to connection record (app, device) pair */
new_conn->app = app;
new_conn->id = last_conn_id++;
-
new_conn->transactions = queue_new();
- if (!new_conn->transactions) {
- free(new_conn);
- return NULL;
- }
- if (!queue_push_head(app_connections, new_conn)) {
- error("gatt: Cannot push client on the client queue!?");
- queue_destroy(new_conn->transactions, free);
- free(new_conn);
- return NULL;
- }
+ queue_push_head(app_connections, new_conn);
new_conn->device = device_ref(device);
struct service *s;
s = new0(struct service, 1);
- if (!s) {
- error("gatt: Cannot allocate memory for gatt_primary");
- return NULL;
- }
-
- s->chars = queue_new();
- if (!s->chars) {
- error("gatt: Cannot allocate memory for char cache");
- free(s);
- return NULL;
- }
-
- s->included = queue_new();
- if (!s->included) {
- error("gatt: Cannot allocate memory for included queue");
- queue_destroy(s->chars, NULL);
- free(s);
- return NULL;
- }
if (bt_string_to_uuid(&s->id.uuid, uuid) < 0) {
error("gatt: Cannot convert string to uuid");
- queue_destroy(s->chars, NULL);
free(s);
return NULL;
}
+ s->chars = queue_new();
+ s->included = queue_new();
s->id.instance = id;
/* Put primary service to our local list */
goto reply;
}
- if (!queue_push_tail(dev->services, s)) {
- error("gatt: Cannot push primary service to the list");
- destroy_service(s);
- gatt_status = GATT_FAILURE;
- goto reply;
- }
+ queue_push_tail(dev->services, s);
send_client_primary_notify(s, INT_TO_PTR(cb_data->conn->id));
if (!p)
continue;
- if (!queue_push_tail(dev->services, p)) {
- error("gatt: Cannot push primary service to the list");
- free(p);
- continue;
- }
+ queue_push_tail(dev->services, p);
DBG("attr handle = 0x%04x, end grp handle = 0x%04x uuid: %s",
prim->range.start, prim->range.end, prim->uuid);
struct discover_srvc_data *cb_data;
cb_data = new0(struct discover_srvc_data, 1);
- if (!cb_data) {
- error("gatt: Cannot allocate cb data");
- return 0;
- }
-
cb_data->conn = conn;
if (uuid) {
if (cid == ATT_CID)
mtu = ATT_DEFAULT_LE_MTU;
- attrib = g_attrib_new(io, mtu);
+ attrib = g_attrib_new(io, mtu, true);
if (!attrib) {
error("gatt: unable to create new GAttrib instance");
device_set_state(dev, DEVICE_DISCONNECTED);
/*
* There is no ongoing bonding, lets search for primary
* services
- *
*/
search_dev_for_srvc(conn, NULL);
}
&data);
/* For BR/EDR notify about MTU since it is not negotiable*/
- if (cid != ATT_CID)
+ if (cid != ATT_CID && status == GATT_SUCCESS)
queue_foreach(app_connections, notify_mtu_change, dev);
device_unref(dev);
dev = create_device(addr);
}
- if (!dev || dev->state != DEVICE_CONNECT_INIT)
+ if (dev->state != DEVICE_CONNECT_INIT)
return;
device_set_state(dev, DEVICE_CONNECT_READY);
}
app = new0(struct gatt_app, 1);
- if (!app) {
- error("gatt: Cannot allocate memory for registering app");
- return NULL;
- }
app->type = type;
- if (app->type == GATT_CLIENT) {
+ if (app->type == GATT_CLIENT)
app->notifications = queue_new();
- if (!app->notifications) {
- error("gatt: couldn't allocate notifications queue");
- destroy_gatt_app(app);
- return NULL;
- }
- }
memcpy(app->uuid, uuid, sizeof(app->uuid));
app->id = application_id++;
- if (!queue_push_head(gatt_apps, app)) {
- error("gatt: Cannot push app on the list");
- destroy_gatt_app(app);
- return NULL;
- }
+ queue_push_head(gatt_apps, app);
- if ((app->type == GATT_SERVER) &&
- !queue_push_tail(listen_apps, INT_TO_PTR(app->id))) {
- error("gatt: Cannot push server on the list");
- destroy_gatt_app(app);
- return NULL;
- }
+ if (app->type == GATT_SERVER)
+ queue_push_tail(listen_apps, INT_TO_PTR(app->id));
return app;
}
if (direct)
return connect_le(conn->device) == 0;
+ bt_gatt_add_autoconnect(conn->app->id, &conn->device->bdaddr);
return auto_connect_le(conn->device);
case DEVICE_CONNECTED:
notify_app_connect_status(conn, GATT_SUCCESS);
struct listen_data *d;
d = new0(struct listen_data, 1);
- if (!d)
- return NULL;
-
d->client_id = client_id;
d->start = start;
if (!advertising_cnt) {
data = create_listen_data(cmd->client_if, false);
- if (!data) {
- error("gatt: Could not allocate listen data");
- status = HAL_STATUS_NOMEM;
- goto reply;
- }
if (!bt_le_set_advertising(data->start, set_advertising_cb,
data)) {
return HAL_STATUS_FAILED;
device = find_device_by_addr(addr);
- if (!device) {
+ if (!device)
device = create_device(addr);
- if (!device)
- return HAL_STATUS_FAILED;
- }
conn_match.device = device;
conn_match.app = app;
goto reply;
}
- if (!queue_push_tail(listen_apps,
- INT_TO_PTR(cmd->client_if))) {
- error("gatt: Could not put client on listen queue");
- status = HAL_STATUS_FAILED;
- goto reply;
- }
+ queue_push_tail(listen_apps, INT_TO_PTR(cmd->client_if));
/* If listen is already on just return success*/
if (advertising_cnt > 0) {
}
data = create_listen_data(cmd->client_if, cmd->start);
- if (!data) {
- error("gatt: Could not allocate listen data");
- status = HAL_STATUS_NOMEM;
- goto reply;
- }
if (!bt_le_set_advertising(cmd->start, set_advertising_cb, data)) {
error("gatt: Could not set advertising");
* 1. on services queue together with primary service
* 2. on special queue inside primary service
*/
- if (!queue_push_tail(service->included, incl) ||
- !queue_push_tail(conn->device->services, incl)) {
- error("gatt: Cannot push incl service to the list");
- destroy_service(incl);
- continue;
- }
+ queue_push_tail(service->included, incl);
+ queue_push_tail(conn->device->services, incl);
}
/*
send_client_incl_service_notify(&service->id, incl, conn->id);
}
-static bool search_included_services(struct app_connection *conn,
+static void search_included_services(struct app_connection *conn,
struct service *service)
{
struct get_included_data *data;
uint16_t start, end;
data = new0(struct get_included_data, 1);
- if (!data) {
- error("gatt: failed to allocate memory for included_data");
- return false;
- }
-
data->prim = service;
data->conn = conn;
gatt_find_included(conn->device->attrib, start, end, get_included_cb,
data);
-
- return true;
}
static bool find_service(int32_t conn_id, struct element_id *service_id,
}
if (!prim_service->incl_search_done) {
- if (search_included_services(conn, prim_service)) {
- status = HAL_STATUS_SUCCESS;
- goto reply;
- }
-
- status = HAL_STATUS_FAILED;
- goto notify;
+ search_included_services(conn, prim_service);
+ status = HAL_STATUS_SUCCESS;
+ goto reply;
}
/* Try to use cache here */
struct characteristic *ch;
ch = new0(struct characteristic, 1);
- if (!ch) {
- error("gatt: Error while caching characteristic");
- continue;
- }
-
ch->descriptors = queue_new();
- if (!ch->descriptors) {
- error("gatt: Error while caching characteristic");
- free(ch);
- continue;
- }
memcpy(&ch->ch, characteristics->data, sizeof(ch->ch));
DBG("attr handle = 0x%04x, end handle = 0x%04x uuid: %s",
ch->ch.handle, ch->end_handle, ch->ch.uuid);
- if (!queue_push_tail(srvc->chars, ch)) {
- error("gatt: Error while caching characteristic");
- destroy_characteristic(ch);
- }
+ queue_push_tail(srvc->chars, ch);
}
}
struct att_range range;
cb_data = new0(struct discover_char_data, 1);
- if (!cb_data) {
- error("gatt: Cannot allocate cb data");
- status = HAL_STATUS_FAILED;
- goto done;
- }
-
cb_data->service = srvc;
cb_data->conn_id = conn->id;
bt_uuid_t uuid;
descr = new0(struct descriptor, 1);
- if (!descr)
- continue;
bt_string_to_uuid(&uuid, desc->uuid);
bt_uuid_to_uuid128(&uuid, &descr->id.uuid);
DBG("attr handle = 0x%04x, uuid: %s", desc->handle, desc->uuid);
- if (!queue_push_tail(ch->descriptors, descr))
- free(descr);
+ queue_push_tail(ch->descriptors, descr);
}
reply:
return false;
cb_data = new0(struct discover_desc_data, 1);
- if (!cb_data)
- return false;
-
cb_data->conn = conn;
cb_data->srvc = srvc;
cb_data->ch = ch;
struct char_op_data *d;
d = new0(struct char_op_data, 1);
- if (!d)
- return NULL;
-
d->conn_id = conn_id;
d->srvc_id = s_id;
d->char_id = ch_id;
cb_data = create_char_op_data(cmd->conn_id, &srvc->id, &ch->id,
cmd->srvc_id.is_primary);
- if (!cb_data) {
- error("gatt: Cannot allocate cb data");
- status = HAL_STATUS_NOMEM;
- goto failed;
- }
if (!set_auth_type(conn->device, cmd->auth_req)) {
error("gatt: Failed to set security %d", cmd->auth_req);
cmd->write_type == GATT_WRITE_TYPE_DEFAULT) {
cb_data = create_char_op_data(cmd->conn_id, &srvc->id, &ch->id,
cmd->srvc_id.is_primary);
- if (!cb_data) {
- error("gatt: Cannot allocate call data");
- status = HAL_STATUS_NOMEM;
- goto failed;
- }
}
if (!set_auth_type(conn->device, cmd->auth_req)) {
struct desc_data *d;
d = new0(struct desc_data, 1);
- if (!d)
- return NULL;
-
d->conn_id = conn_id;
d->srvc_id = s_id;
d->char_id = ch_id;
cb_data = create_desc_data(conn_id, &srvc->id, &ch->id, &descr->id,
primary);
- if (!cb_data) {
- error("gatt: Read descr. could not allocate callback data");
-
- status = HAL_STATUS_NOMEM;
- goto failed;
- }
if (!set_auth_type(conn->device, cmd->auth_req)) {
error("gatt: Failed to set security %d", cmd->auth_req);
goto failed;
}
- if (cmd->write_type != GATT_WRITE_TYPE_NO_RESPONSE) {
+ if (cmd->write_type != GATT_WRITE_TYPE_NO_RESPONSE)
cb_data = create_desc_data(conn_id, &srvc->id, &ch->id,
&descr->id, primary);
- if (!cb_data) {
- error("gatt: Write descr. could not allocate cb_data");
-
- status = HAL_STATUS_NOMEM;
- goto failed;
- }
- }
if (!set_auth_type(conn->device, cmd->auth_req)) {
error("gatt: Failed to set security %d", cmd->auth_req);
}
notification = new0(struct notification_data, 1);
- if (!notification) {
- status = HAL_STATUS_NOMEM;
- goto failed;
- }
memcpy(¬ification->ch, &cmd->char_id, sizeof(notification->ch));
memcpy(¬ification->service, &cmd->srvc_id,
*/
notification->ref = 2;
- if (!queue_push_tail(conn->app->notifications, notification)) {
- unregister_notification(notification);
- status = HAL_STATUS_FAILED;
- goto failed;
- }
+ queue_push_tail(conn->app->notifications, notification);
status = HAL_STATUS_SUCCESS;
struct queue *temp;
temp = queue_new();
- if (!temp)
- goto done;
val = queue_pop_head(device->pending_requests);
if (!val) {
struct queue *temp;
temp = queue_new();
- if (!temp)
- goto done;
val = queue_pop_head(device->pending_requests);
if (!val) {
}
range = new0(struct att_range, 1);
- if (!range) {
- destroy_pending_request(val);
- error = ATT_ECODE_INSUFF_RESOURCES;
- break;
- }
-
range->start = gatt_db_attribute_get_handle(
val->attrib);
static int32_t trans_id = 1;
transaction = new0(struct pending_trans_data, 1);
- if (!transaction)
- return NULL;
-
- if (!queue_push_tail(conn->transactions, transaction)) {
- free(transaction);
- return NULL;
- }
-
transaction->id = trans_id++;
transaction->opcode = opcode;
transaction->attrib = attrib;
transaction->serial_id = serial_id;
+ queue_push_tail(conn->transactions, transaction);
+
return transaction;
}
/* Store the request data, complete callback and transaction id */
transaction = conn_add_transact(conn, opcode, attrib, id);
- if (!transaction)
- goto failed;
bdaddr2android(&bdaddr, ev.bdaddr);
ev.conn_id = conn->id;
/* Store the request data, complete callback and transaction id */
transaction = conn_add_transact(conn, opcode, attrib, id);
- if (!transaction)
- goto failed;
memset(ev, 0, sizeof(*ev));
return NULL;
s = new0(struct service_sdp, 1);
- if (!s)
- return NULL;
-
s->service_handle = service_handle;
s->sdp_handle = add_sdp_record(&uuid, service_handle, end_handle, NULL);
if (!s->sdp_handle) {
if (!s)
return false;
- if (!queue_push_tail(services_sdp, s)) {
- free_service_sdp_record(s);
- return false;
- }
+ queue_push_tail(services_sdp, s);
return true;
}
return ATT_ECODE_INVALID_HANDLE;
q = queue_new();
- if (!q)
- return ATT_ECODE_INSUFF_RESOURCES;
switch (cmd[0]) {
case ATT_OP_READ_BY_TYPE_REQ:
struct gatt_db_attribute *attrib = queue_pop_head(q);
data = new0(struct pending_request, 1);
- if (!data) {
- queue_destroy(q, NULL);
- return ATT_ECODE_INSUFF_RESOURCES;
- }
-
data->attrib = attrib;
- if (!queue_push_tail(device->pending_requests, data)) {
- free(data);
- queue_destroy(q, NULL);
- return ATT_ECODE_INSUFF_RESOURCES;
- }
+ queue_push_tail(device->pending_requests, data);
}
queue_destroy(q, NULL);
return ATT_ECODE_INVALID_HANDLE;
data = new0(struct pending_request, 1);
- if (!data)
- return ATT_ECODE_INSUFF_RESOURCES;
-
data->offset = offset;
data->attrib = attrib;
- if (!queue_push_tail(dev->pending_requests, data)) {
- free(data);
- return ATT_ECODE_INSUFF_RESOURCES;
- }
+ queue_push_tail(dev->pending_requests, data);
process_dev_pending_requests(dev, cmd[0]);
return ATT_ECODE_INVALID_HANDLE;
q = queue_new();
- if (!q)
- return ATT_ECODE_UNLIKELY;
gatt_db_find_information(gatt_db, start, end, q);
}
temp = queue_new();
- if (!temp) {
- queue_destroy(q, NULL);
- return ATT_ECODE_UNLIKELY;
- }
attrib = queue_peek_head(q);
/* UUIDS can be only 128 bit and 16 bit */
return;
request_data = new0(struct pending_request, 1);
- if (!request_data) {
- find_data->error = ATT_ECODE_INSUFF_RESOURCES;
- return;
- }
-
request_data->filter_value = malloc0(find_data->search_vlen);
if (!request_data->filter_value) {
destroy_pending_request(request_data);
memcpy(request_data->filter_value, find_data->search_value,
find_data->search_vlen);
- if (!queue_push_tail(find_data->device->pending_requests,
- request_data)) {
- destroy_pending_request(request_data);
- find_data->error = ATT_ECODE_INSUFF_RESOURCES;
- }
+ queue_push_tail(find_data->device->pending_requests, request_data);
}
static uint8_t find_by_type_request(const uint8_t *cmd, uint16_t cmd_len,
data->attrib = attrib;
data->error = error;
-
data->completed = true;
}
return error;
data = new0(struct pending_request, 1);
- if (!data)
- return ATT_ECODE_INSUFF_RESOURCES;
-
data->attrib = attrib;
- if (!queue_push_tail(dev->pending_requests, data)) {
- free(data);
- return ATT_ECODE_INSUFF_RESOURCES;
- }
+ queue_push_tail(dev->pending_requests, data);
if (!gatt_db_attribute_write(attrib, 0, value, vlen, cmd[0],
g_attrib_get_att(dev->attrib),
return error;
data = new0(struct pending_request, 1);
- if (!data)
- return ATT_ECODE_INSUFF_RESOURCES;
-
data->attrib = attrib;
data->offset = offset;
- if (!queue_push_tail(dev->pending_requests, data)) {
- free(data);
- return ATT_ECODE_INSUFF_RESOURCES;
- }
+ queue_push_tail(dev->pending_requests, data);
data->value = g_memdup(value, vlen);
data->length = vlen;
ev->conn_id = conn->id;
transaction = conn_add_transact(conn, ATT_OP_EXEC_WRITE_REQ, NULL, 0);
- if (!transaction) {
- conn->wait_execute_write = false;
- return;
- }
ev->trans_id = transaction->id;
ev.exec_write = value;
data = new0(struct pending_request, 1);
- if (!data)
- return ATT_ECODE_INSUFF_RESOURCES;
- if (!queue_push_tail(dev->pending_requests, data)) {
- free(data);
- return ATT_ECODE_INSUFF_RESOURCES;
- }
+ queue_push_tail(dev->pending_requests, data);
queue_foreach(app_connections, send_server_write_execute_notify, &ev);
send_dev_complete_response(dev, cmd[0]);
dev = find_device_by_addr(&dst);
if (!dev) {
dev = create_device(&dst);
- if (!dev) {
- error("gatt: Could not create device");
- goto drop;
- }
} else {
if ((dev->state != DEVICE_DISCONNECTED) &&
!(dev->state == DEVICE_CONNECT_INIT &&
services_sdp = queue_new();
gatt_db = gatt_db_new();
- if (!gatt_devices || !gatt_apps || !listen_apps || !app_connections ||
- !services_sdp || !gatt_db) {
- error("gatt: Failed to allocate memory for queues");
+ if (!gatt_db) {
+ error("gatt: Failed to allocate memory for database");
goto failed;
}