static bool find_service_with_uuid(const void *data, const void *user_data) { const struct gatt_db_service *service = data; const bt_uuid_t *uuid = user_data; bt_uuid_t svc_uuid; gatt_db_attribute_get_service_uuid(service->attributes[0], &svc_uuid); return bt_uuid_cmp(uuid, &svc_uuid) == 0; }
static void log_service_event(struct gatt_db_attribute *attr, const char *str) { char uuid_str[MAX_LEN_UUID_STR]; bt_uuid_t uuid; uint16_t start, end; gatt_db_attribute_get_service_uuid(attr, &uuid); bt_uuid_to_string(&uuid, uuid_str, sizeof(uuid_str)); gatt_db_attribute_get_service_handles(attr, &start, &end); PRLOG("%s - UUID: %s start: 0x%04x end: 0x%04x\n", str, uuid_str, start, end); }
static void foreach_service_in_range(void *data, void *user_data) { struct gatt_db_service *service = data; struct foreach_data *foreach_data = user_data; uint16_t svc_start; bt_uuid_t uuid; svc_start = get_handle_at_index(service, 0); if (svc_start > foreach_data->end || svc_start < foreach_data->start) return; if (foreach_data->uuid) { gatt_db_attribute_get_service_uuid(service->attributes[0], &uuid); if (bt_uuid_cmp(&uuid, foreach_data->uuid)) return; } foreach_data->func(service->attributes[0], foreach_data->user_data); }
static void print_incl(struct gatt_db_attribute *attr, void *user_data) { struct server *server = user_data; uint16_t handle, start, end; struct gatt_db_attribute *service; bt_uuid_t uuid; if (!gatt_db_attribute_get_incl_data(attr, &handle, &start, &end)) return; service = gatt_db_get_attribute(server->db, start); if (!service) return; gatt_db_attribute_get_service_uuid(service, &uuid); printf("\t " COLOR_GREEN "include" COLOR_OFF " - handle: " "0x%04x, - start: 0x%04x, end: 0x%04x," "uuid: ", handle, start, end); print_uuid(&uuid); }
static void service_added(struct gatt_db_attribute *attr, void *user_data) { struct gas *gas = user_data; bt_uuid_t uuid, gap_uuid; if (!bt_gatt_client_is_ready(gas->client)) return; gatt_db_attribute_get_service_uuid(attr, &uuid); bt_uuid16_create(&gap_uuid, GAP_UUID16); if (bt_uuid_cmp(&uuid, &gap_uuid)) return; if (gas->attr) { error("More than one GAP service added to device"); return; } DBG("GAP service added"); gas->attr = attr; handle_gap_service(gas); }
struct gatt_db_attribute *gatt_db_insert_service(struct gatt_db *db, uint16_t handle, const bt_uuid_t *uuid, bool primary, uint16_t num_handles) { struct gatt_db_service *service, *after; after = NULL; if (!db || handle < 1) return NULL; if (num_handles < 1 || (handle + num_handles - 1) > UINT16_MAX) return NULL; service = find_insert_loc(db, handle, handle + num_handles - 1, &after); if (service) { const bt_uuid_t *type; bt_uuid_t value; if (primary) type = &primary_service_uuid; else type = &secondary_service_uuid; gatt_db_attribute_get_service_uuid(service->attributes[0], &value); /* Check if service match */ if (!bt_uuid_cmp(&service->attributes[0]->uuid, type) && !bt_uuid_cmp(&value, uuid) && service->num_handles == num_handles && service->attributes[0]->handle == handle) return service->attributes[0]; return NULL; } service = gatt_db_service_create(uuid, handle, primary, num_handles); if (!service) return NULL; if (after) { if (!queue_push_after(db->services, after, service)) goto fail; } else if (!queue_push_head(db->services, service)) { goto fail; } service->db = db; service->attributes[0]->handle = handle; service->num_handles = num_handles; /* Fast-forward next_handle if the new service was added to the end */ db->next_handle = MAX(handle + num_handles, db->next_handle); return service->attributes[0]; fail: gatt_db_service_destroy(service); return NULL; }