~ubuntu-branches/ubuntu/wily/bluez/wily

« back to all changes in this revision

Viewing changes to attrib/gatt.c

ImportĀ upstreamĀ versionĀ 4.81

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 *
 
3
 *  BlueZ - Bluetooth protocol stack for Linux
 
4
 *
 
5
 *  Copyright (C) 2010  Nokia Corporation
 
6
 *  Copyright (C) 2010  Marcel Holtmann <marcel@holtmann.org>
 
7
 *
 
8
 *
 
9
 *  This program is free software; you can redistribute it and/or modify
 
10
 *  it under the terms of the GNU General Public License as published by
 
11
 *  the Free Software Foundation; either version 2 of the License, or
 
12
 *  (at your option) any later version.
 
13
 *
 
14
 *  This program is distributed in the hope that it will be useful,
 
15
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
16
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
17
 *  GNU General Public License for more details.
 
18
 *
 
19
 *  You should have received a copy of the GNU General Public License
 
20
 *  along with this program; if not, write to the Free Software
 
21
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 
22
 *
 
23
 */
 
24
 
 
25
#include <stdint.h>
 
26
#include <glib.h>
 
27
#include <bluetooth/sdp.h>
 
28
#include <bluetooth/sdp_lib.h>
 
29
 
 
30
#include "att.h"
 
31
#include "gattrib.h"
 
32
#include "gatt.h"
 
33
 
 
34
guint gatt_discover_primary(GAttrib *attrib, uint16_t start, uint16_t end,
 
35
                uuid_t *uuid, GAttribResultFunc func, gpointer user_data)
 
36
{
 
37
        uint8_t pdu[ATT_DEFAULT_MTU];
 
38
        uuid_t prim;
 
39
        guint16 plen;
 
40
        uint8_t op;
 
41
 
 
42
        sdp_uuid16_create(&prim, GATT_PRIM_SVC_UUID);
 
43
 
 
44
        if (uuid == NULL) {
 
45
 
 
46
                /* Discover all primary services */
 
47
                op = ATT_OP_READ_BY_GROUP_REQ;
 
48
                plen = enc_read_by_grp_req(start, end, &prim, pdu, sizeof(pdu));
 
49
        } else {
 
50
                const void *value;
 
51
                int vlen;
 
52
 
 
53
                /* Discover primary service by service UUID */
 
54
                op = ATT_OP_FIND_BY_TYPE_REQ;
 
55
 
 
56
                if (uuid->type == SDP_UUID16) {
 
57
                        value = &uuid->value.uuid16;
 
58
                        vlen = sizeof(uuid->value.uuid16);
 
59
                } else {
 
60
                        value = &uuid->value.uuid128;
 
61
                        vlen = sizeof(uuid->value.uuid128);
 
62
                }
 
63
 
 
64
                plen = enc_find_by_type_req(start, end, &prim, value, vlen,
 
65
                                                        pdu, sizeof(pdu));
 
66
        }
 
67
 
 
68
        if (plen == 0)
 
69
                return 0;
 
70
 
 
71
        return g_attrib_send(attrib, op, pdu, plen, func, user_data, NULL);
 
72
}
 
73
 
 
74
guint gatt_discover_char(GAttrib *attrib, uint16_t start, uint16_t end,
 
75
                                GAttribResultFunc func, gpointer user_data)
 
76
{
 
77
        uuid_t uuid;
 
78
 
 
79
        sdp_uuid16_create(&uuid, GATT_CHARAC_UUID);
 
80
 
 
81
        return gatt_read_char_by_uuid(attrib, start, end, &uuid, func,
 
82
                                                        user_data);
 
83
}
 
84
 
 
85
guint gatt_read_char_by_uuid(GAttrib *attrib, uint16_t start, uint16_t end,
 
86
                                        uuid_t *uuid, GAttribResultFunc func,
 
87
                                        gpointer user_data)
 
88
{
 
89
        uint8_t pdu[ATT_DEFAULT_MTU];
 
90
        guint16 plen;
 
91
 
 
92
        plen = enc_read_by_type_req(start, end, uuid, pdu, sizeof(pdu));
 
93
        if (plen == 0)
 
94
                return 0;
 
95
 
 
96
        return g_attrib_send(attrib, ATT_OP_READ_BY_TYPE_REQ,
 
97
                                        pdu, plen, func, user_data, NULL);
 
98
}
 
99
 
 
100
guint gatt_read_char(GAttrib *attrib, uint16_t handle, GAttribResultFunc func,
 
101
                                                        gpointer user_data)
 
102
{
 
103
        uint8_t pdu[ATT_DEFAULT_MTU];
 
104
        guint16 plen;
 
105
 
 
106
        plen = enc_read_req(handle, pdu, sizeof(pdu));
 
107
        return g_attrib_send(attrib, ATT_OP_READ_REQ, pdu, plen, func,
 
108
                                                        user_data, NULL);
 
109
}
 
110
 
 
111
guint gatt_write_char(GAttrib *attrib, uint16_t handle, uint8_t *value,
 
112
                        int vlen, GAttribResultFunc func, gpointer user_data)
 
113
{
 
114
        uint8_t pdu[ATT_DEFAULT_MTU];
 
115
        guint16 plen;
 
116
 
 
117
        plen = enc_write_req(handle, value, vlen, pdu, sizeof(pdu));
 
118
        return g_attrib_send(attrib, ATT_OP_WRITE_REQ, pdu, plen, func,
 
119
                                                        user_data, NULL);
 
120
}
 
121
 
 
122
guint gatt_find_info(GAttrib *attrib, uint16_t start, uint16_t end,
 
123
                                GAttribResultFunc func, gpointer user_data)
 
124
{
 
125
        uint8_t pdu[ATT_DEFAULT_MTU];
 
126
        guint16 plen;
 
127
 
 
128
        plen = enc_find_info_req(start, end, pdu, sizeof(pdu));
 
129
        if (plen == 0)
 
130
                return 0;
 
131
 
 
132
        return g_attrib_send(attrib, ATT_OP_FIND_INFO_REQ, pdu, plen, func,
 
133
                                                        user_data, NULL);
 
134
}
 
135
 
 
136
guint gatt_write_cmd(GAttrib *attrib, uint16_t handle, uint8_t *value, int vlen,
 
137
                                GDestroyNotify notify, gpointer user_data)
 
138
{
 
139
        uint8_t pdu[ATT_DEFAULT_MTU];
 
140
        guint16 plen;
 
141
 
 
142
        plen = enc_write_cmd(handle, value, vlen, pdu, sizeof(pdu));
 
143
        return g_attrib_send(attrib, ATT_OP_WRITE_CMD, pdu, plen, NULL,
 
144
                                                        user_data, notify);
 
145
}