diff --git a/attrib/example.c b/attrib/example.c index 2af862405..1ada76df4 100644 --- a/attrib/example.c +++ b/attrib/example.c @@ -26,19 +26,111 @@ #include #endif +#include +#include + +#include "sdpd.h" +#include "log.h" + #include "example.h" +#define ATT_PSM 27 + +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; +} + 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 0; } void server_example_exit(void) { + if (handle) + remove_record_from_server(handle); }