~timchen119/+junk/bluez5-trusty

« back to all changes in this revision

Viewing changes to src/shared/gap.c

  • Committer: tim
  • Date: 2015-09-04 08:03:03 UTC
  • Revision ID: tim@tim-inspiron-5442-20150904080303-75u7z8bdsl17xz8q
* init

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) 2012-2014  Intel Corporation. All rights reserved.
 
6
 *
 
7
 *
 
8
 *  This library is free software; you can redistribute it and/or
 
9
 *  modify it under the terms of the GNU Lesser General Public
 
10
 *  License as published by the Free Software Foundation; either
 
11
 *  version 2.1 of the License, or (at your option) any later version.
 
12
 *
 
13
 *  This library is distributed in the hope that it will be useful,
 
14
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
15
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
16
 *  Lesser General Public License for more details.
 
17
 *
 
18
 *  You should have received a copy of the GNU Lesser General Public
 
19
 *  License along with this library; if not, write to the Free Software
 
20
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 
21
 *
 
22
 */
 
23
 
 
24
#ifdef HAVE_CONFIG_H
 
25
#include <config.h>
 
26
#endif
 
27
 
 
28
#include <stdio.h>
 
29
#include <stdlib.h>
 
30
 
 
31
#include "lib/bluetooth.h"
 
32
#include "lib/mgmt.h"
 
33
 
 
34
#include "src/shared/util.h"
 
35
#include "src/shared/queue.h"
 
36
#include "src/shared/mgmt.h"
 
37
#include "src/shared/gap.h"
 
38
 
 
39
#define FLAG_MGMT_CONN_CONTROL  (0 << 1)
 
40
 
 
41
struct bt_gap {
 
42
        int ref_count;
 
43
        uint16_t index;
 
44
        struct mgmt *mgmt;
 
45
 
 
46
        uint8_t mgmt_version;
 
47
        uint16_t mgmt_revision;
 
48
        bool mgmt_ready;
 
49
 
 
50
        unsigned long flags;
 
51
 
 
52
        bt_gap_ready_func_t ready_handler;
 
53
        bt_gap_destroy_func_t ready_destroy;
 
54
        void *ready_data;
 
55
 
 
56
        uint8_t static_addr[6];
 
57
        uint8_t local_irk[16];
 
58
        struct queue *irk_list;
 
59
};
 
60
 
 
61
struct irk_entry {
 
62
        uint8_t addr_type;
 
63
        uint8_t addr[6];
 
64
        uint8_t key[16];
 
65
};
 
66
 
 
67
static void irk_entry_free(void *data)
 
68
{
 
69
        struct irk_entry *irk = data;
 
70
 
 
71
        free(irk);
 
72
}
 
73
 
 
74
static void ready_status(struct bt_gap *gap, bool status)
 
75
{
 
76
        gap->mgmt_ready = status;
 
77
 
 
78
        if (gap->ready_handler)
 
79
                gap->ready_handler(status, gap->ready_data);
 
80
}
 
81
 
 
82
static void read_commands_complete(uint8_t status, uint16_t length,
 
83
                                        const void *param, void *user_data)
 
84
{
 
85
        struct bt_gap *gap = user_data;
 
86
        const struct mgmt_rp_read_commands *rp = param;
 
87
        uint16_t num_commands, num_events;
 
88
        const uint16_t *opcode;
 
89
        size_t expected_len;
 
90
        int i;
 
91
 
 
92
        if (status != MGMT_STATUS_SUCCESS) {
 
93
                ready_status(gap, false);
 
94
                return;
 
95
        }
 
96
 
 
97
        if (length < sizeof(*rp)) {
 
98
                ready_status(gap, false);
 
99
                return;
 
100
        }
 
101
 
 
102
        num_commands = le16_to_cpu(rp->num_commands);
 
103
        num_events = le16_to_cpu(rp->num_events);
 
104
 
 
105
        expected_len = sizeof(*rp) + num_commands * sizeof(uint16_t) +
 
106
                                                num_events * sizeof(uint16_t);
 
107
 
 
108
        if (length < expected_len) {
 
109
                ready_status(gap, false);
 
110
                return;
 
111
        }
 
112
 
 
113
        opcode = rp->opcodes;
 
114
 
 
115
        for (i = 0; i < num_commands; i++) {
 
116
                uint16_t op = get_le16(opcode++);
 
117
 
 
118
                if (op == MGMT_OP_ADD_DEVICE)
 
119
                        gap->flags |= FLAG_MGMT_CONN_CONTROL;
 
120
        }
 
121
 
 
122
        ready_status(gap, true);
 
123
}
 
124
 
 
125
static void read_version_complete(uint8_t status, uint16_t length,
 
126
                                        const void *param, void *user_data)
 
127
{
 
128
        struct bt_gap *gap = user_data;
 
129
        const struct mgmt_rp_read_version *rp = param;
 
130
 
 
131
        if (status != MGMT_STATUS_SUCCESS) {
 
132
                ready_status(gap, false);
 
133
                return;
 
134
        }
 
135
 
 
136
        if (length < sizeof(*rp)) {
 
137
                ready_status(gap, false);
 
138
                return;
 
139
        }
 
140
 
 
141
        gap->mgmt_version = rp->version;
 
142
        gap->mgmt_revision = le16_to_cpu(rp->revision);
 
143
 
 
144
        if (!mgmt_send(gap->mgmt, MGMT_OP_READ_COMMANDS,
 
145
                                MGMT_INDEX_NONE, 0, NULL,
 
146
                                read_commands_complete, gap, NULL)) {
 
147
                ready_status(gap, false);
 
148
                return;
 
149
        }
 
150
}
 
151
 
 
152
struct bt_gap *bt_gap_new_default(void)
 
153
{
 
154
        return bt_gap_new_index(0x0000);
 
155
}
 
156
 
 
157
struct bt_gap *bt_gap_new_index(uint16_t index)
 
158
{
 
159
        struct bt_gap *gap;
 
160
 
 
161
        if (index == MGMT_INDEX_NONE)
 
162
                return NULL;
 
163
 
 
164
        gap = new0(struct bt_gap, 1);
 
165
        if (!gap)
 
166
                return NULL;
 
167
 
 
168
        gap->index = index;
 
169
 
 
170
        gap->mgmt = mgmt_new_default();
 
171
        if (!gap->mgmt) {
 
172
                free(gap);
 
173
                return NULL;
 
174
        }
 
175
 
 
176
        gap->irk_list = queue_new();
 
177
 
 
178
        gap->mgmt_ready = false;
 
179
 
 
180
        if (!mgmt_send(gap->mgmt, MGMT_OP_READ_VERSION,
 
181
                                        MGMT_INDEX_NONE, 0, NULL,
 
182
                                        read_version_complete, gap, NULL)) {
 
183
                mgmt_unref(gap->mgmt);
 
184
                return NULL;
 
185
        }
 
186
 
 
187
        return bt_gap_ref(gap);
 
188
}
 
189
 
 
190
struct bt_gap *bt_gap_ref(struct bt_gap *gap)
 
191
{
 
192
        if (!gap)
 
193
                return NULL;
 
194
 
 
195
        __sync_fetch_and_add(&gap->ref_count, 1);
 
196
 
 
197
        return gap;
 
198
}
 
199
 
 
200
void bt_gap_unref(struct bt_gap *gap)
 
201
{
 
202
        if (!gap)
 
203
                return;
 
204
 
 
205
        if (__sync_sub_and_fetch(&gap->ref_count, 1))
 
206
                return;
 
207
 
 
208
        gap->mgmt_ready = false;
 
209
 
 
210
        mgmt_cancel_all(gap->mgmt);
 
211
        mgmt_unregister_all(gap->mgmt);
 
212
 
 
213
        if (gap->ready_destroy)
 
214
                gap->ready_destroy(gap->ready_data);
 
215
 
 
216
        queue_destroy(gap->irk_list, irk_entry_free);
 
217
 
 
218
        mgmt_unref(gap->mgmt);
 
219
 
 
220
        free(gap);
 
221
}
 
222
 
 
223
bool bt_gap_set_ready_handler(struct bt_gap *gap,
 
224
                                bt_gap_ready_func_t handler, void *user_data,
 
225
                                bt_gap_destroy_func_t destroy)
 
226
{
 
227
        if (!gap)
 
228
                return false;
 
229
 
 
230
        if (gap->ready_destroy)
 
231
                gap->ready_destroy(gap->ready_data);
 
232
 
 
233
        gap->ready_handler = handler;
 
234
        gap->ready_destroy = destroy;
 
235
        gap->ready_data = user_data;
 
236
 
 
237
        return true;
 
238
}
 
239
 
 
240
bool bt_gap_set_static_addr(struct bt_gap *gap, uint8_t addr[6])
 
241
{
 
242
        if (!gap)
 
243
                return false;
 
244
 
 
245
        memcpy(gap->static_addr, addr, 6);
 
246
 
 
247
        return true;
 
248
}
 
249
 
 
250
bool bt_gap_set_local_irk(struct bt_gap *gap, uint8_t key[16])
 
251
{
 
252
        if (!gap)
 
253
                return false;
 
254
 
 
255
        memcpy(gap->local_irk, key, 16);
 
256
 
 
257
        return true;
 
258
}
 
259
 
 
260
bool bt_gap_add_peer_irk(struct bt_gap *gap, uint8_t addr_type,
 
261
                                        uint8_t addr[6], uint8_t key[16])
 
262
{
 
263
        struct irk_entry *irk;
 
264
 
 
265
        if (!gap)
 
266
                return false;
 
267
 
 
268
        if (addr_type > BT_GAP_ADDR_TYPE_LE_RANDOM)
 
269
                return false;
 
270
 
 
271
        irk = new0(struct irk_entry, 1);
 
272
        if (!irk)
 
273
                return false;
 
274
 
 
275
        irk->addr_type = addr_type;
 
276
        memcpy(irk->addr, addr, 6);
 
277
        memcpy(irk->key, key, 16);
 
278
 
 
279
        if (!queue_push_tail(gap->irk_list, irk)) {
 
280
                free(irk);
 
281
                return false;
 
282
        }
 
283
 
 
284
        return true;
 
285
}