3 * BlueZ - Bluetooth protocol stack for Linux
5 * Copyright (C) 2015 Intel Corporation. All rights reserved.
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.
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.
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
32 #include <sys/epoll.h>
34 #include "lib/bluetooth.h"
35 #include "lib/l2cap.h"
37 #include "src/shared/mainloop.h"
38 #include "src/shared/util.h"
39 #include "src/shared/queue.h"
40 #include "src/shared/att.h"
41 #include "src/shared/gatt-db.h"
42 #include "src/shared/gatt-server.h"
43 #include "src/shared/gatt-client.h"
44 #include "peripheral/gatt.h"
48 #define UUID_GAP 0x1800
52 struct bt_gatt_server *gatt;
53 struct bt_gatt_client *client;
56 static int att_fd = -1;
57 static struct queue *conn_list = NULL;
58 static struct gatt_db *gatt_db = NULL;
59 static struct gatt_db *gatt_cache = NULL;
61 static uint8_t static_addr[6] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
62 static uint8_t dev_name[20];
63 static uint8_t dev_name_len = 0;
65 void gatt_set_static_address(uint8_t addr[6])
67 memcpy(static_addr, addr, sizeof(static_addr));
70 void gatt_set_device_name(uint8_t name[20], uint8_t len)
72 memcpy(dev_name, name, sizeof(dev_name));
76 static void gatt_conn_destroy(void *data)
78 struct gatt_conn *conn = data;
80 bt_gatt_client_unref(conn->client);
81 bt_gatt_server_unref(conn->gatt);
82 bt_att_unref(conn->att);
87 static void gatt_conn_disconnect(int err, void *user_data)
89 struct gatt_conn *conn = user_data;
91 printf("Device disconnected: %s\n", strerror(err));
93 queue_remove(conn_list, conn);
94 gatt_conn_destroy(conn);
97 static void client_ready_callback(bool success, uint8_t att_ecode,
100 printf("GATT client discovery complete\n");
103 static void client_service_changed_callback(uint16_t start_handle,
107 printf("GATT client service changed notification\n");
110 static struct gatt_conn *gatt_conn_new(int fd)
112 struct gatt_conn *conn;
115 conn = new0(struct gatt_conn, 1);
119 conn->att = bt_att_new(fd, false);
121 fprintf(stderr, "Failed to initialze ATT transport layer\n");
126 bt_att_set_close_on_unref(conn->att, true);
127 bt_att_register_disconnect(conn->att, gatt_conn_disconnect, conn, NULL);
129 bt_att_set_security(conn->att, BT_SECURITY_MEDIUM);
131 conn->gatt = bt_gatt_server_new(gatt_db, conn->att, mtu);
133 fprintf(stderr, "Failed to create GATT server\n");
134 bt_att_unref(conn->att);
139 conn->client = bt_gatt_client_new(gatt_cache, conn->att, mtu);
141 fprintf(stderr, "Failed to create GATT client\n");
142 bt_gatt_server_unref(conn->gatt);
143 bt_att_unref(conn->att);
148 bt_gatt_client_set_ready_handler(conn->client,
149 client_ready_callback, conn, NULL);
150 bt_gatt_client_set_service_changed(conn->client,
151 client_service_changed_callback, conn, NULL);
156 static void att_conn_callback(int fd, uint32_t events, void *user_data)
158 struct gatt_conn *conn;
159 struct sockaddr_l2 addr;
163 if (events & (EPOLLERR | EPOLLHUP)) {
164 mainloop_remove_fd(fd);
168 memset(&addr, 0, sizeof(addr));
169 addrlen = sizeof(addr);
171 new_fd = accept(att_fd, (struct sockaddr *) &addr, &addrlen);
173 fprintf(stderr, "Failed to accept new ATT connection: %m\n");
177 conn = gatt_conn_new(new_fd);
179 fprintf(stderr, "Failed to create GATT connection\n");
184 if (!queue_push_tail(conn_list, conn)) {
185 fprintf(stderr, "Failed to add GATT connection\n");
186 gatt_conn_destroy(conn);
190 printf("New device connected\n");
193 static void gap_device_name_read(struct gatt_db_attribute *attrib,
194 unsigned int id, uint16_t offset,
195 uint8_t opcode, struct bt_att *att,
199 const uint8_t *value;
202 if (offset > dev_name_len) {
203 error = BT_ATT_ERROR_INVALID_OFFSET;
208 len = dev_name_len - offset;
209 value = len ? &dev_name[offset] : NULL;
212 gatt_db_attribute_read_result(attrib, id, error, value, len);
215 static void populate_gap_service(struct gatt_db *db)
217 struct gatt_db_attribute *service;
220 bt_uuid16_create(&uuid, UUID_GAP);
221 service = gatt_db_add_service(db, &uuid, true, 6);
223 bt_uuid16_create(&uuid, GATT_CHARAC_DEVICE_NAME);
224 gatt_db_service_add_characteristic(service, &uuid,
226 BT_GATT_CHRC_PROP_READ,
227 gap_device_name_read, NULL, NULL);
229 gatt_db_service_set_active(service, true);
232 static void populate_devinfo_service(struct gatt_db *db)
234 struct gatt_db_attribute *service;
237 bt_uuid16_create(&uuid, 0x180a);
238 service = gatt_db_add_service(db, &uuid, true, 17);
240 gatt_db_service_set_active(service, true);
243 void gatt_server_start(void)
245 struct sockaddr_l2 addr;
250 att_fd = socket(PF_BLUETOOTH, SOCK_SEQPACKET | SOCK_CLOEXEC,
253 fprintf(stderr, "Failed to create ATT server socket: %m\n");
257 memset(&addr, 0, sizeof(addr));
258 addr.l2_family = AF_BLUETOOTH;
259 addr.l2_cid = htobs(ATT_CID);
260 memcpy(&addr.l2_bdaddr, static_addr, 6);
261 addr.l2_bdaddr_type = BDADDR_LE_RANDOM;
263 if (bind(att_fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
264 fprintf(stderr, "Failed to bind ATT server socket: %m\n");
270 if (listen(att_fd, 1) < 0) {
271 fprintf(stderr, "Failed to listen on ATT server socket: %m\n");
277 gatt_db = gatt_db_new();
284 populate_gap_service(gatt_db);
285 populate_devinfo_service(gatt_db);
287 gatt_cache = gatt_db_new();
289 conn_list = queue_new();
291 gatt_db_unref(gatt_db);
298 mainloop_add_fd(att_fd, EPOLLIN, att_conn_callback, NULL, NULL);
301 void gatt_server_stop(void)
306 mainloop_remove_fd(att_fd);
308 queue_destroy(conn_list, gatt_conn_destroy);
310 gatt_db_unref(gatt_cache);
313 gatt_db_unref(gatt_db);