gatt-service.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. *
  4. * BlueZ - Bluetooth protocol stack for Linux
  5. *
  6. * Copyright (C) 2011 Nokia Corporation
  7. * Copyright (C) 2011 Marcel Holtmann <marcel@holtmann.org>
  8. *
  9. *
  10. */
  11. #ifdef HAVE_CONFIG_H
  12. #include <config.h>
  13. #endif
  14. #include <glib.h>
  15. #include "lib/bluetooth.h"
  16. #include "lib/sdp.h"
  17. #include "lib/uuid.h"
  18. #include "src/adapter.h"
  19. #include "src/shared/util.h"
  20. #include "attrib/gattrib.h"
  21. #include "attrib/att.h"
  22. #include "attrib/gatt.h"
  23. #include "attrib/att-database.h"
  24. #include "src/attrib-server.h"
  25. #include "attrib/gatt-service.h"
  26. #include "src/log.h"
  27. struct gatt_info {
  28. bt_uuid_t uuid;
  29. uint8_t props;
  30. int authentication;
  31. int authorization;
  32. GSList *callbacks;
  33. unsigned int num_attrs;
  34. uint16_t *value_handle;
  35. uint16_t *ccc_handle;
  36. };
  37. struct attrib_cb {
  38. attrib_event_t event;
  39. void *fn;
  40. void *user_data;
  41. };
  42. static inline void put_uuid_le(const bt_uuid_t *src, void *dst)
  43. {
  44. if (src->type == BT_UUID16)
  45. put_le16(src->value.u16, dst);
  46. else
  47. /* Convert from 128-bit BE to LE */
  48. bswap_128(&src->value.u128, dst);
  49. }
  50. static GSList *parse_opts(gatt_option opt1, va_list args)
  51. {
  52. gatt_option opt = opt1;
  53. struct gatt_info *info;
  54. struct attrib_cb *cb;
  55. GSList *l = NULL;
  56. info = g_new0(struct gatt_info, 1);
  57. l = g_slist_append(l, info);
  58. while (opt != GATT_OPT_INVALID) {
  59. switch (opt) {
  60. case GATT_OPT_CHR_UUID16:
  61. bt_uuid16_create(&info->uuid, va_arg(args, int));
  62. /* characteristic declaration and value */
  63. info->num_attrs += 2;
  64. break;
  65. case GATT_OPT_CHR_UUID:
  66. memcpy(&info->uuid, va_arg(args, bt_uuid_t *),
  67. sizeof(bt_uuid_t));
  68. /* characteristic declaration and value */
  69. info->num_attrs += 2;
  70. break;
  71. case GATT_OPT_CHR_PROPS:
  72. info->props = va_arg(args, int);
  73. if (info->props & (GATT_CHR_PROP_NOTIFY |
  74. GATT_CHR_PROP_INDICATE))
  75. /* client characteristic configuration */
  76. info->num_attrs += 1;
  77. /* TODO: "Extended Properties" property requires a
  78. * descriptor, but it is not supported yet. */
  79. break;
  80. case GATT_OPT_CHR_VALUE_CB:
  81. cb = g_new0(struct attrib_cb, 1);
  82. cb->event = va_arg(args, attrib_event_t);
  83. cb->fn = va_arg(args, void *);
  84. cb->user_data = va_arg(args, void *);
  85. info->callbacks = g_slist_append(info->callbacks, cb);
  86. break;
  87. case GATT_OPT_CHR_VALUE_GET_HANDLE:
  88. info->value_handle = va_arg(args, void *);
  89. break;
  90. case GATT_OPT_CCC_GET_HANDLE:
  91. info->ccc_handle = va_arg(args, void *);
  92. break;
  93. case GATT_OPT_CHR_AUTHENTICATION:
  94. info->authentication = va_arg(args, gatt_option);
  95. break;
  96. case GATT_OPT_CHR_AUTHORIZATION:
  97. info->authorization = va_arg(args, gatt_option);
  98. break;
  99. case GATT_CHR_VALUE_READ:
  100. case GATT_CHR_VALUE_WRITE:
  101. case GATT_CHR_VALUE_BOTH:
  102. case GATT_OPT_INVALID:
  103. default:
  104. error("Invalid option: %d", opt);
  105. }
  106. opt = va_arg(args, gatt_option);
  107. if (opt == GATT_OPT_CHR_UUID16 || opt == GATT_OPT_CHR_UUID) {
  108. info = g_new0(struct gatt_info, 1);
  109. l = g_slist_append(l, info);
  110. }
  111. }
  112. return l;
  113. }
  114. static struct attribute *add_service_declaration(struct btd_adapter *adapter,
  115. uint16_t handle, uint16_t svc, bt_uuid_t *uuid)
  116. {
  117. bt_uuid_t bt_uuid;
  118. uint8_t atval[16];
  119. int len;
  120. put_uuid_le(uuid, &atval[0]);
  121. len = bt_uuid_len(uuid);
  122. bt_uuid16_create(&bt_uuid, svc);
  123. return attrib_db_add(adapter, handle, &bt_uuid, ATT_NONE,
  124. ATT_NOT_PERMITTED, atval, len);
  125. }
  126. static int att_read_req(int authorization, int authentication, uint8_t props)
  127. {
  128. if (authorization == GATT_CHR_VALUE_READ ||
  129. authorization == GATT_CHR_VALUE_BOTH)
  130. return ATT_AUTHORIZATION;
  131. else if (authentication == GATT_CHR_VALUE_READ ||
  132. authentication == GATT_CHR_VALUE_BOTH)
  133. return ATT_AUTHENTICATION;
  134. else if (!(props & GATT_CHR_PROP_READ))
  135. return ATT_NOT_PERMITTED;
  136. return ATT_NONE;
  137. }
  138. static int att_write_req(int authorization, int authentication, uint8_t props)
  139. {
  140. if (authorization == GATT_CHR_VALUE_WRITE ||
  141. authorization == GATT_CHR_VALUE_BOTH)
  142. return ATT_AUTHORIZATION;
  143. else if (authentication == GATT_CHR_VALUE_WRITE ||
  144. authentication == GATT_CHR_VALUE_BOTH)
  145. return ATT_AUTHENTICATION;
  146. else if (!(props & (GATT_CHR_PROP_WRITE |
  147. GATT_CHR_PROP_WRITE_WITHOUT_RESP)))
  148. return ATT_NOT_PERMITTED;
  149. return ATT_NONE;
  150. }
  151. static int find_callback(gconstpointer a, gconstpointer b)
  152. {
  153. const struct attrib_cb *cb = a;
  154. unsigned int event = GPOINTER_TO_UINT(b);
  155. return cb->event - event;
  156. }
  157. static gboolean add_characteristic(struct btd_adapter *adapter,
  158. uint16_t *handle, struct gatt_info *info)
  159. {
  160. int read_req, write_req;
  161. uint16_t h = *handle;
  162. struct attribute *a;
  163. bt_uuid_t bt_uuid;
  164. uint8_t atval[ATT_MAX_VALUE_LEN];
  165. GSList *l;
  166. if ((info->uuid.type != BT_UUID16 && info->uuid.type != BT_UUID128) ||
  167. !info->props) {
  168. error("Characteristic UUID or properties are missing");
  169. return FALSE;
  170. }
  171. read_req = att_read_req(info->authorization, info->authentication,
  172. info->props);
  173. write_req = att_write_req(info->authorization, info->authentication,
  174. info->props);
  175. /* TODO: static characteristic values are not supported, therefore a
  176. * callback must be always provided if a read/write property is set */
  177. if (read_req != ATT_NOT_PERMITTED) {
  178. gpointer reqs = GUINT_TO_POINTER(ATTRIB_READ);
  179. if (!g_slist_find_custom(info->callbacks, reqs,
  180. find_callback)) {
  181. error("Callback for read required");
  182. return FALSE;
  183. }
  184. }
  185. if (write_req != ATT_NOT_PERMITTED) {
  186. gpointer reqs = GUINT_TO_POINTER(ATTRIB_WRITE);
  187. if (!g_slist_find_custom(info->callbacks, reqs,
  188. find_callback)) {
  189. error("Callback for write required");
  190. return FALSE;
  191. }
  192. }
  193. /* characteristic declaration */
  194. bt_uuid16_create(&bt_uuid, GATT_CHARAC_UUID);
  195. atval[0] = info->props;
  196. put_le16(h + 1, &atval[1]);
  197. put_uuid_le(&info->uuid, &atval[3]);
  198. if (attrib_db_add(adapter, h++, &bt_uuid, ATT_NONE, ATT_NOT_PERMITTED,
  199. atval, 3 + info->uuid.type / 8) == NULL)
  200. return FALSE;
  201. /* characteristic value */
  202. a = attrib_db_add(adapter, h++, &info->uuid, read_req, write_req,
  203. NULL, 0);
  204. if (a == NULL)
  205. return FALSE;
  206. for (l = info->callbacks; l != NULL; l = l->next) {
  207. struct attrib_cb *cb = l->data;
  208. switch (cb->event) {
  209. case ATTRIB_READ:
  210. a->read_cb = cb->fn;
  211. break;
  212. case ATTRIB_WRITE:
  213. a->write_cb = cb->fn;
  214. break;
  215. }
  216. a->cb_user_data = cb->user_data;
  217. }
  218. if (info->value_handle != NULL)
  219. *info->value_handle = a->handle;
  220. /* client characteristic configuration descriptor */
  221. if (info->props & (GATT_CHR_PROP_NOTIFY | GATT_CHR_PROP_INDICATE)) {
  222. uint8_t cfg_val[2];
  223. bt_uuid16_create(&bt_uuid, GATT_CLIENT_CHARAC_CFG_UUID);
  224. cfg_val[0] = 0x00;
  225. cfg_val[1] = 0x00;
  226. a = attrib_db_add(adapter, h++, &bt_uuid, ATT_NONE,
  227. ATT_AUTHENTICATION, cfg_val, sizeof(cfg_val));
  228. if (a == NULL)
  229. return FALSE;
  230. if (info->ccc_handle != NULL)
  231. *info->ccc_handle = a->handle;
  232. }
  233. *handle = h;
  234. return TRUE;
  235. }
  236. static void free_gatt_info(void *data)
  237. {
  238. struct gatt_info *info = data;
  239. g_slist_free_full(info->callbacks, g_free);
  240. g_free(info);
  241. }
  242. static void service_attr_del(struct btd_adapter *adapter, uint16_t start_handle,
  243. uint16_t end_handle)
  244. {
  245. uint16_t handle;
  246. /* For a 128-bit category primary service below handle should be checked
  247. * for both non-zero as well as >= 0xffff. As on last iteration the
  248. * handle will turn to 0 from 0xffff and loop will be infinite.
  249. */
  250. for (handle = start_handle; (handle != 0 && handle <= end_handle);
  251. handle++) {
  252. if (attrib_db_del(adapter, handle) < 0)
  253. error("Can't delete handle 0x%04x", handle);
  254. }
  255. }
  256. gboolean gatt_service_add(struct btd_adapter *adapter, uint16_t uuid,
  257. bt_uuid_t *svc_uuid, gatt_option opt1, ...)
  258. {
  259. char uuidstr[MAX_LEN_UUID_STR];
  260. uint16_t start_handle, h;
  261. unsigned int size;
  262. va_list args;
  263. GSList *chrs, *l;
  264. bt_uuid_to_string(svc_uuid, uuidstr, MAX_LEN_UUID_STR);
  265. if (svc_uuid->type != BT_UUID16 && svc_uuid->type != BT_UUID128) {
  266. error("Invalid service uuid: %s", uuidstr);
  267. return FALSE;
  268. }
  269. va_start(args, opt1);
  270. chrs = parse_opts(opt1, args);
  271. va_end(args);
  272. /* calculate how many attributes are necessary for this service */
  273. for (l = chrs, size = 1; l != NULL; l = l->next) {
  274. struct gatt_info *info = l->data;
  275. size += info->num_attrs;
  276. }
  277. start_handle = attrib_db_find_avail(adapter, svc_uuid, size);
  278. if (start_handle == 0) {
  279. error("Not enough free handles to register service");
  280. goto fail;
  281. }
  282. DBG("New service: handle 0x%04x, UUID %s, %d attributes",
  283. start_handle, uuidstr, size);
  284. /* service declaration */
  285. h = start_handle;
  286. if (add_service_declaration(adapter, h++, uuid, svc_uuid) == NULL)
  287. goto fail;
  288. for (l = chrs; l != NULL; l = l->next) {
  289. struct gatt_info *info = l->data;
  290. DBG("New characteristic: handle 0x%04x", h);
  291. if (!add_characteristic(adapter, &h, info)) {
  292. service_attr_del(adapter, start_handle, h - 1);
  293. goto fail;
  294. }
  295. }
  296. g_assert(size < USHRT_MAX);
  297. g_assert(h == 0 || (h - start_handle == (uint16_t) size));
  298. g_slist_free_full(chrs, free_gatt_info);
  299. return TRUE;
  300. fail:
  301. g_slist_free_full(chrs, free_gatt_info);
  302. return FALSE;
  303. }