Move the GATT Service Record registration to the server

The GATT Server SDP record should be in the server, so it is not added
when the server is disabled.
This commit is contained in:
Vinicius Costa Gomes 2010-08-24 14:50:37 -03:00
parent b5b44ce5c8
commit 5a48a21cbd
4 changed files with 84 additions and 99 deletions

View File

@ -34,15 +34,12 @@
#include <glib.h>
#include "sdpd.h"
#include "log.h"
#include "attrib-server.h"
#include "att.h"
#include "example.h"
#define ATT_PSM 27
/* FIXME: Not defined by SIG? UUID128? */
#define OPCODES_SUPPORTED_UUID 0xA001
#define BATTERY_STATE_SVC_UUID 0xA002
@ -60,72 +57,6 @@
#define VENDOR_SPECIFIC_SVC_UUID 0xA00E
#define VENDOR_SPECIFIC_TYPE_UUID 0xA00F
static uint32_t handle = 0;
static sdp_record_t *server_record_new(void)
{
sdp_list_t *svclass_id, *apseq, *proto[2], *profiles, *root, *aproto;
uuid_t root_uuid, proto_uuid, gatt_uuid, l2cap;
sdp_profile_desc_t profile;
sdp_record_t *record;
sdp_data_t *psm, *sh, *eh;
uint16_t lp = ATT_PSM, start = 0x0001, end = 0x000f;
record = sdp_record_alloc();
if (record == NULL)
return NULL;
sdp_uuid16_create(&root_uuid, PUBLIC_BROWSE_GROUP);
root = sdp_list_append(NULL, &root_uuid);
sdp_set_browse_groups(record, root);
sdp_list_free(root, NULL);
sdp_uuid16_create(&gatt_uuid, GENERIC_ATTRIB_SVCLASS_ID);
svclass_id = sdp_list_append(NULL, &gatt_uuid);
sdp_set_service_classes(record, svclass_id);
sdp_list_free(svclass_id, NULL);
sdp_uuid16_create(&profile.uuid, GENERIC_ATTRIB_PROFILE_ID);
profile.version = 0x0100;
profiles = sdp_list_append(NULL, &profile);
sdp_set_profile_descs(record, profiles);
sdp_list_free(profiles, NULL);
sdp_uuid16_create(&l2cap, L2CAP_UUID);
proto[0] = sdp_list_append(NULL, &l2cap);
psm = sdp_data_alloc(SDP_UINT16, &lp);
proto[0] = sdp_list_append(proto[0], psm);
apseq = sdp_list_append(NULL, proto[0]);
sdp_uuid16_create(&proto_uuid, ATT_UUID);
proto[1] = sdp_list_append(NULL, &proto_uuid);
sh = sdp_data_alloc(SDP_UINT16, &start);
proto[1] = sdp_list_append(proto[1], sh);
eh = sdp_data_alloc(SDP_UINT16, &end);
proto[1] = sdp_list_append(proto[1], eh);
apseq = sdp_list_append(apseq, proto[1]);
aproto = sdp_list_append(NULL, apseq);
sdp_set_access_protos(record, aproto);
sdp_set_info_attr(record, "Generic Attribute Profile", "BlueZ", NULL);
sdp_set_url_attr(record, "http://www.bluez.org/",
"http://www.bluez.org/", "http://www.bluez.org/");
sdp_set_service_id(record, gatt_uuid);
sdp_data_free(psm);
sdp_data_free(sh);
sdp_data_free(eh);
sdp_list_free(proto[0], NULL);
sdp_list_free(proto[1], NULL);
sdp_list_free(apseq, NULL);
sdp_list_free(aproto, NULL);
return record;
}
static gboolean change_humidity(gpointer user_data)
{
static uint8_t humidity = 0x28;
@ -434,33 +365,5 @@ static int register_attributes(void)
int server_example_init(void)
{
sdp_record_t *record;
/*
* FIXME: Add BR/EDR service record and attributes into the GATT
* database. BlueZ gatt server will be automatically enabled if
* any plugin registers at least one primary service.
*/
record = server_record_new();
if (record == NULL) {
error("Unable to create GATT service record");
return -1;
}
if (add_record_to_server(BDADDR_ANY, record) < 0) {
error("Failed to register GATT service record");
sdp_record_free(record);
return -1;
}
handle = record->handle;
return register_attributes();
}
void server_example_exit(void)
{
if (handle)
remove_record_from_server(handle);
}

View File

@ -23,4 +23,3 @@
*/
int server_example_init(void);
void server_example_exit(void);

View File

@ -109,7 +109,6 @@ void attrib_manager_exit(void)
{
btd_unregister_device_driver(&client_driver);
server_example_exit();
attrib_client_exit();
dbus_connection_unref(connection);

View File

@ -39,6 +39,7 @@
#include "log.h"
#include "glib-helper.h"
#include "btio.h"
#include "sdpd.h"
#include "att.h"
#include "gattrib.h"
@ -57,10 +58,75 @@ struct gatt_channel {
static GIOChannel *l2cap_io = NULL;
static GSList *clients = NULL;
static uint32_t handle = 0;
static uuid_t prim_uuid = { .type = SDP_UUID16, .value.uuid16 = GATT_PRIM_SVC_UUID };
static uuid_t snd_uuid = { .type = SDP_UUID16, .value.uuid16 = GATT_SND_SVC_UUID };
static sdp_record_t *server_record_new(void)
{
sdp_list_t *svclass_id, *apseq, *proto[2], *profiles, *root, *aproto;
uuid_t root_uuid, proto_uuid, gatt_uuid, l2cap;
sdp_profile_desc_t profile;
sdp_record_t *record;
sdp_data_t *psm, *sh, *eh;
uint16_t lp = GATT_PSM, start = 0x0001, end = 0xffff;
record = sdp_record_alloc();
if (record == NULL)
return NULL;
sdp_uuid16_create(&root_uuid, PUBLIC_BROWSE_GROUP);
root = sdp_list_append(NULL, &root_uuid);
sdp_set_browse_groups(record, root);
sdp_list_free(root, NULL);
sdp_uuid16_create(&gatt_uuid, GENERIC_ATTRIB_SVCLASS_ID);
svclass_id = sdp_list_append(NULL, &gatt_uuid);
sdp_set_service_classes(record, svclass_id);
sdp_list_free(svclass_id, NULL);
sdp_uuid16_create(&profile.uuid, GENERIC_ATTRIB_PROFILE_ID);
profile.version = 0x0100;
profiles = sdp_list_append(NULL, &profile);
sdp_set_profile_descs(record, profiles);
sdp_list_free(profiles, NULL);
sdp_uuid16_create(&l2cap, L2CAP_UUID);
proto[0] = sdp_list_append(NULL, &l2cap);
psm = sdp_data_alloc(SDP_UINT16, &lp);
proto[0] = sdp_list_append(proto[0], psm);
apseq = sdp_list_append(NULL, proto[0]);
sdp_uuid16_create(&proto_uuid, ATT_UUID);
proto[1] = sdp_list_append(NULL, &proto_uuid);
sh = sdp_data_alloc(SDP_UINT16, &start);
proto[1] = sdp_list_append(proto[1], sh);
eh = sdp_data_alloc(SDP_UINT16, &end);
proto[1] = sdp_list_append(proto[1], eh);
apseq = sdp_list_append(apseq, proto[1]);
aproto = sdp_list_append(NULL, apseq);
sdp_set_access_protos(record, aproto);
sdp_set_info_attr(record, "Generic Attribute Profile", "BlueZ", NULL);
sdp_set_url_attr(record, "http://www.bluez.org/",
"http://www.bluez.org/", "http://www.bluez.org/");
sdp_set_service_id(record, gatt_uuid);
sdp_data_free(psm);
sdp_data_free(sh);
sdp_data_free(eh);
sdp_list_free(proto[0], NULL);
sdp_list_free(proto[1], NULL);
sdp_list_free(apseq, NULL);
sdp_list_free(aproto, NULL);
return record;
}
static uint16_t read_by_group(uint16_t start, uint16_t end, uuid_t *uuid,
uint8_t *pdu, int len)
{
@ -453,6 +519,7 @@ static gboolean send_notification(gpointer user_data)
int attrib_server_init(void)
{
GError *gerr = NULL;
sdp_record_t *record;
/* BR/EDR socket */
l2cap_io = bt_io_listen(BT_IO_L2CAP, NULL, confirm_event,
@ -468,6 +535,20 @@ int attrib_server_init(void)
return -1;
}
record = server_record_new();
if (record == NULL) {
error("Unable to create GATT service record");
return -1;
}
if (add_record_to_server(BDADDR_ANY, record) < 0) {
error("Failed to register GATT service record");
sdp_record_free(record);
return -1;
}
handle = record->handle;
return 0;
}
@ -487,6 +568,9 @@ void attrib_server_exit(void)
g_source_remove(channel->id);
}
if (handle)
remove_record_from_server(handle);
g_slist_free(clients);
}