ble_gatt_client
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros
att.c
Go to the documentation of this file.
1 
11 /*
12  *
13  * BlueZ - Bluetooth protocol stack for Linux
14  *
15  * Copyright (C) 2014 Google Inc.
16  *
17  *
18  * This library is free software; you can redistribute it and/or
19  * modify it under the terms of the GNU Lesser General Public
20  * License as published by the Free Software Foundation; either
21  * version 2.1 of the License, or (at your option) any later version.
22  *
23  * This library is distributed in the hope that it will be useful,
24  * but WITHOUT ANY WARRANTY; without even the implied warranty of
25  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
26  * Lesser General Public License for more details.
27  *
28  * You should have received a copy of the GNU Lesser General Public
29  * License along with this library; if not, write to the Free Software
30  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
31  *
32  */
33 
34 #ifdef HAVE_CONFIG_H
35 #include "config.h"
36 #endif
37 
38 #include <stdlib.h>
39 #include <unistd.h>
40 #include <errno.h>
41 
42 #include "io.h"
43 #include "queue.h"
44 #include "util.h"
45 #include "timeout.h"
46 #include "bluetooth.h"
47 #include "uuid.h"
48 #include "att.h"
49 #include "crypto.h"
50 
51 #define ATT_MIN_PDU_LEN 1 /* At least 1 byte for the opcode. */
52 #define ATT_OP_CMD_MASK 0x40
53 #define ATT_OP_SIGNED_MASK 0x80
54 #define ATT_TIMEOUT_INTERVAL 30000 /* 30000 ms */
55 
56 /* Length of signature in write signed packet */
57 #define BT_ATT_SIGNATURE_LEN 12
58 
59 struct att_send_op;
60 
64 struct bt_att {
66  int ref_count;
68  int fd;
70  struct io *io;
76  struct queue *req_queue;
80  struct queue *ind_queue;
84  struct queue *write_queue;
88  struct queue *notify_list;
92  bool in_req;
94  uint8_t *buf;
96  uint16_t mtu;
98  unsigned int next_send_id;
100  unsigned int next_reg_id;
112  void *debug_data;
114  struct bt_crypto *crypto;
121 };
122 
123 struct sign_info {
124  uint8_t key[16];
126  void *user_data;
127 };
128 
137 };
138 
139 static const struct {
140  uint8_t opcode;
142 } att_opcode_type_table[] = {
171  { }
172 };
173 
174 static enum att_op_type get_op_type(uint8_t opcode)
175 {
176  int i;
177 
178  for (i = 0; att_opcode_type_table[i].opcode; i++) {
179  if (att_opcode_type_table[i].opcode == opcode)
180  return att_opcode_type_table[i].type;
181  }
182 
183  return ATT_OP_TYPE_UNKNOWN;
184 }
185 
186 static const struct {
187  uint8_t req_opcode;
188  uint8_t rsp_opcode;
201  { }
202 };
203 
204 static uint8_t get_req_opcode(uint8_t rsp_opcode)
205 {
206  int i;
207 
208  for (i = 0; att_req_rsp_mapping_table[i].rsp_opcode; i++) {
209  if (att_req_rsp_mapping_table[i].rsp_opcode == rsp_opcode)
210  return att_req_rsp_mapping_table[i].req_opcode;
211  }
212 
213  return 0;
214 }
215 
216 struct att_send_op {
217  unsigned int id;
218  unsigned int timeout_id;
220  uint16_t opcode;
221  void *pdu;
222  uint16_t len;
225  void *user_data;
226 };
227 
235 static void destroy_att_send_op(void *data)
236 {
237  struct att_send_op *op = data;
238 
239  if (op->timeout_id)
241 
242  if (op->destroy)
243  op->destroy(op->user_data);
244 
245  free(op->pdu);
246  free(op);
247 }
248 
249 static void cancel_att_send_op(struct att_send_op *op)
250 {
251  if (op->destroy)
252  op->destroy(op->user_data);
253 
254  op->user_data = NULL;
255  op->callback = NULL;
256  op->destroy = NULL;
257 }
258 
259 struct att_notify {
260  unsigned int id;
261  uint16_t opcode;
264  void *user_data;
265 };
266 
267 static void destroy_att_notify(void *data)
268 {
269  struct att_notify *notify = data;
270 
271  if (notify->destroy)
272  notify->destroy(notify->user_data);
273 
274  free(notify);
275 }
276 
277 static bool match_notify_id(const void *a, const void *b)
278 {
279  const struct att_notify *notify = a;
280  unsigned int id = PTR_TO_UINT(b);
281 
282  return notify->id == id;
283 }
284 
285 struct att_disconn {
286  unsigned int id;
287  bool removed;
290  void *user_data;
291 };
292 
293 static void destroy_att_disconn(void *data)
294 {
295  struct att_disconn *disconn = data;
296 
297  if (disconn->destroy)
298  disconn->destroy(disconn->user_data);
299 
300  free(disconn);
301 }
302 
303 static bool match_disconn_id(const void *a, const void *b)
304 {
305  const struct att_disconn *disconn = a;
306  unsigned int id = PTR_TO_UINT(b);
307 
308  return disconn->id == id;
309 }
310 
311 static bool encode_pdu(struct bt_att *att, struct att_send_op *op,
312  const void *pdu, uint16_t length)
313 {
314  uint16_t pdu_len = 1;
315  struct sign_info *sign = att->local_sign;
316  uint32_t sign_cnt;
317 
318  if (sign && (op->opcode & ATT_OP_SIGNED_MASK))
319  pdu_len += BT_ATT_SIGNATURE_LEN;
320 
321  if (length && pdu)
322  pdu_len += length;
323 
324  if (pdu_len > att->mtu)
325  return false;
326 
327  op->len = pdu_len;
328  op->pdu = malloc(op->len);
329  if (!op->pdu)
330  return false;
331 
332  ((uint8_t *) op->pdu)[0] = op->opcode;
333  if (pdu_len > 1)
334  memcpy(op->pdu + 1, pdu, length);
335 
336  if (!sign || !(op->opcode & ATT_OP_SIGNED_MASK))
337  return true;
338 
339  if (!sign->counter(&sign_cnt, sign->user_data))
340  goto fail;
341 
342  if ((bt_crypto_sign_att(att->crypto, sign->key, op->pdu, 1 + length,
343  sign_cnt, &((uint8_t *) op->pdu)[1 + length])))
344  return true;
345 
347  "ATT unable to generate signature");
348 
349 fail:
350  free(op->pdu);
351  return false;
352 }
353 
354 static struct att_send_op *create_att_send_op(struct bt_att *att,
355  uint8_t opcode,
356  const void *pdu,
357  uint16_t length,
359  void *user_data,
361 {
362  struct att_send_op *op;
363  enum att_op_type op_type;
364 
365  if (length && !pdu)
366  return NULL;
367 
368  op_type = get_op_type(opcode);
369  if (op_type == ATT_OP_TYPE_UNKNOWN)
370  return NULL;
371 
372  /* If the opcode corresponds to an operation type that does not elicit a
373  * response from the remote end, then no callback should have been
374  * provided, since it will never be called.
375  */
376  if (callback && op_type != ATT_OP_TYPE_REQ && op_type != ATT_OP_TYPE_IND)
377  return NULL;
378 
379  /* Similarly, if the operation does elicit a response then a callback
380  * must be provided.
381  */
382  if (!callback && (op_type == ATT_OP_TYPE_REQ || op_type == ATT_OP_TYPE_IND))
383  return NULL;
384 
385  op = new0(struct att_send_op, 1);
386  if (!op)
387  return NULL;
388 
389  op->type = op_type;
390  op->opcode = opcode;
391  op->callback = callback;
392  op->destroy = destroy;
393  op->user_data = user_data;
394 
395  if (!encode_pdu(att, op, pdu, length)) {
396  free(op);
397  return NULL;
398  }
399 
400  return op;
401 }
402 
403 static struct att_send_op *pick_next_send_op(struct bt_att *att)
404 {
405  struct att_send_op *op;
406 
407  /* See if any operations are already in the write queue */
408  op = queue_pop_head(att->write_queue);
409  if (op)
410  return op;
411 
412  /* If there is no pending request, pick an operation from the
413  * request queue.
414  */
415  if (!att->pending_req) {
416  op = queue_pop_head(att->req_queue);
417  if (op)
418  return op;
419  }
420 
421  /* There is either a request pending or no requests queued. If there is
422  * no pending indication, pick an operation from the indication queue.
423  */
424  if (!att->pending_ind) {
425  op = queue_pop_head(att->ind_queue);
426  if (op)
427  return op;
428  }
429 
430  return NULL;
431 }
432 
433 struct timeout_data {
434  struct bt_att *att;
435  unsigned int id;
436 };
437 
438 static bool timeout_cb(void *user_data)
439 {
440  struct timeout_data *timeout = user_data;
441  struct bt_att *att = timeout->att;
442  struct att_send_op *op = NULL;
443 
444  if (att->pending_req && att->pending_req->id == timeout->id) {
445  op = att->pending_req;
446  att->pending_req = NULL;
447  } else if (att->pending_ind && att->pending_ind->id == timeout->id) {
448  op = att->pending_ind;
449  att->pending_ind = NULL;
450  }
451 
452  if (!op)
453  return false;
454 
456  "Operation timed out: 0x%02x", op->opcode);
457 
458  if (att->timeout_callback)
459  att->timeout_callback(op->id, op->opcode, att->timeout_data);
460 
461  op->timeout_id = 0;
463 
464  /*
465  * Directly terminate the connection as required by the ATT protocol.
466  * This should trigger an io disconnect event which will clean up the
467  * io and notify the upper layer.
468  */
469  io_shutdown(att->io);
470 
471  return false;
472 }
473 
474 static void write_watch_destroy(void *user_data)
475 {
476  struct bt_att *att = user_data;
477 
478  att->writer_active = false;
479 }
480 
481 static bool can_write_data(struct io *io, void *user_data)
482 {
483  struct bt_att *att = user_data;
484  struct att_send_op *op;
485  struct timeout_data *timeout;
486  ssize_t ret;
487  struct iovec iov;
488 
489  op = pick_next_send_op(att);
490  if (!op)
491  return false;
492 
493  iov.iov_base = op->pdu;
494  iov.iov_len = op->len;
495 
496  ret = io_send(io, &iov, 1);
497  if (ret < 0) {
499  "write failed: %s", strerror(-ret));
500  if (op->callback)
501  op->callback(BT_ATT_OP_ERROR_RSP, NULL, 0,
502  op->user_data);
503 
505  return true;
506  }
507 
509  "ATT op 0x%02x", op->opcode);
510 
511  util_hexdump('<', op->pdu, ret, att->debug_callback, att->debug_data);
512 
513  /* Based on the operation type, set either the pending request or the
514  * pending indication. If it came from the write queue, then there is
515  * no need to keep it around.
516  */
517  switch (op->type) {
518  case ATT_OP_TYPE_REQ:
519  att->pending_req = op;
520  break;
521  case ATT_OP_TYPE_IND:
522  att->pending_ind = op;
523  break;
524  case ATT_OP_TYPE_RSP:
525  /* Set in_req to false to indicate that no request is pending */
526  att->in_req = false;
527  /* Fall through to the next case */
528  case ATT_OP_TYPE_CMD:
529  case ATT_OP_TYPE_NOT:
530  case ATT_OP_TYPE_CONF:
531  case ATT_OP_TYPE_UNKNOWN:
532  default:
534  return true;
535  }
536 
537  timeout = new0(struct timeout_data, 1);
538  if (!timeout)
539  return true;
540 
541  timeout->att = att;
542  timeout->id = op->id;
543  op->timeout_id = timeout_add(ATT_TIMEOUT_INTERVAL, timeout_cb,
544  timeout, free);
545 
546  /* Return true as there may be more operations ready to write. */
547  return true;
548 }
549 
550 static void wakeup_writer(struct bt_att *att)
551 {
552  if (att->writer_active)
553  return;
554 
555  /* Set the write handler only if there is anything that can be sent
556  * at all.
557  */
558  if (queue_isempty(att->write_queue)) {
559  if ((att->pending_req || queue_isempty(att->req_queue)) &&
560  (att->pending_ind || queue_isempty(att->ind_queue)))
561  return;
562  }
563 
564  if (!io_set_write_handler(att->io, can_write_data, att,
566  return;
567 
568  att->writer_active = true;
569 }
570 
571 static void disconn_handler(void *data, void *user_data)
572 {
573  struct att_disconn *disconn = data;
574  int err = PTR_TO_INT(user_data);
575 
576  if (disconn->removed)
577  return;
578 
579  if (disconn->callback)
580  disconn->callback(err, disconn->user_data);
581 }
582 
583 static bool disconnect_cb(struct io *io, void *user_data)
584 {
585  struct bt_att *att = user_data;
586  int err;
587  socklen_t len;
588 
589  len = sizeof(err);
590 
591  if (getsockopt(att->fd, SOL_SOCKET, SO_ERROR, &err, &len) < 0) {
593  "Failed to obtain disconnect error: %s",
594  strerror(errno));
595  err = 0;
596  }
597 
599  "Physical link disconnected: %s",
600  strerror(err));
601 
602  io_destroy(att->io);
603  att->io = NULL;
604 
605  bt_att_cancel_all(att);
606 
607  bt_att_ref(att);
608 
610 
612  bt_att_unref(att);
613 
614  return false;
615 }
616 
617 static bool change_security(struct bt_att *att, uint8_t ecode)
618 {
619  int security;
620 
621  security = bt_att_get_security(att);
622  if (security != BT_ATT_SECURITY_AUTO)
623  return false;
624 
626  security < BT_ATT_SECURITY_MEDIUM)
627  security = BT_ATT_SECURITY_MEDIUM;
628  else if (ecode == BT_ATT_ERROR_AUTHENTICATION &&
629  security < BT_ATT_SECURITY_HIGH)
630  security = BT_ATT_SECURITY_HIGH;
631  else
632  return false;
633 
634  return bt_att_set_security(att, security);
635 }
636 
637 static bool handle_error_rsp(struct bt_att *att, uint8_t *pdu,
638  ssize_t pdu_len, uint8_t *opcode)
639 {
640  const struct bt_att_pdu_error_rsp *rsp;
641  struct att_send_op *op = att->pending_req;
642 
643  if (pdu_len != sizeof(*rsp)) {
644  *opcode = 0;
645  return false;
646  }
647 
648  rsp = (void *) pdu;
649 
650  *opcode = rsp->opcode;
651 
652  /* Attempt to change security */
653  if (!change_security(att, rsp->ecode))
654  return false;
655 
657  "Retrying operation %p", op);
658 
659  att->pending_req = NULL;
660 
661  /* Push operation back to request queue */
662  return queue_push_head(att->req_queue, op);
663 }
664 
665 static void handle_rsp(struct bt_att *att, uint8_t opcode, uint8_t *pdu,
666  ssize_t pdu_len)
667 {
668  struct att_send_op *op = att->pending_req;
669  uint8_t req_opcode;
670  uint8_t rsp_opcode;
671  uint8_t *rsp_pdu = NULL;
672  uint16_t rsp_pdu_len = 0;
673 
674  /*
675  * If no request is pending, then the response is unexpected. Disconnect
676  * the bearer.
677  */
678  if (!op) {
680  "Received unexpected ATT response");
681  io_shutdown(att->io);
682  return;
683  }
684 
685  /*
686  * If the received response doesn't match the pending request, or if
687  * the request is malformed, end the current request with failure.
688  */
689  if (opcode == BT_ATT_OP_ERROR_RSP) {
690  /* Return if error response cause a retry */
691  if (handle_error_rsp(att, pdu, pdu_len, &req_opcode)) {
692  wakeup_writer(att);
693  return;
694  }
695  } else if (!(req_opcode = get_req_opcode(opcode)))
696  goto fail;
697 
698  if (req_opcode != op->opcode)
699  goto fail;
700 
701  rsp_opcode = opcode;
702 
703  if (pdu_len > 0) {
704  rsp_pdu = pdu;
705  rsp_pdu_len = pdu_len;
706  }
707 
708  goto done;
709 
710 fail:
712  "Failed to handle response PDU; opcode: 0x%02x", opcode);
713 
714  rsp_opcode = BT_ATT_OP_ERROR_RSP;
715 
716 done:
717  if (op->callback)
718  op->callback(rsp_opcode, rsp_pdu, rsp_pdu_len, op->user_data);
719 
721  att->pending_req = NULL;
722 
723  wakeup_writer(att);
724 }
725 
726 static void handle_conf(struct bt_att *att, uint8_t *pdu, ssize_t pdu_len)
727 {
728  struct att_send_op *op = att->pending_ind;
729 
730  /*
731  * Disconnect the bearer if the confirmation is unexpected or the PDU is
732  * invalid.
733  */
734  if (!op || pdu_len) {
736  "Received unexpected/invalid ATT confirmation");
737  io_shutdown(att->io);
738  return;
739  }
740 
741  if (op->callback)
742  op->callback(BT_ATT_OP_HANDLE_VAL_CONF, NULL, 0, op->user_data);
743 
745  att->pending_ind = NULL;
746 
747  wakeup_writer(att);
748 }
749 
750 struct notify_data {
751  uint8_t opcode;
752  uint8_t *pdu;
753  ssize_t pdu_len;
755 };
756 
757 static bool opcode_match(uint8_t opcode, uint8_t test_opcode)
758 {
759  enum att_op_type op_type = get_op_type(test_opcode);
760 
761  if (opcode == BT_ATT_ALL_REQUESTS && (op_type == ATT_OP_TYPE_REQ ||
762  op_type == ATT_OP_TYPE_CMD))
763  return true;
764 
765  return opcode == test_opcode;
766 }
767 
768 static void respond_not_supported(struct bt_att *att, uint8_t opcode)
769 {
770  struct bt_att_pdu_error_rsp pdu;
771 
772  pdu.opcode = opcode;
773  pdu.handle = 0x0000;
775 
776  bt_att_send(att, BT_ATT_OP_ERROR_RSP, &pdu, sizeof(pdu), NULL, NULL,
777  NULL);
778 }
779 
780 static bool handle_signed(struct bt_att *att, uint8_t opcode, uint8_t *pdu,
781  ssize_t pdu_len)
782 {
783  uint8_t *signature;
784  uint32_t sign_cnt;
785  struct sign_info *sign;
786 
787  /* Check if there is enough data for a signature */
788  if (pdu_len < 2 + BT_ATT_SIGNATURE_LEN)
789  goto fail;
790 
791  sign = att->remote_sign;
792  if (!sign)
793  goto fail;
794 
795  signature = pdu + (pdu_len - BT_ATT_SIGNATURE_LEN);
796  sign_cnt = get_le32(signature);
797 
798  /* Validate counter */
799  if (!sign->counter(&sign_cnt, sign->user_data))
800  goto fail;
801 
802  /* Generate signature and verify it */
803  if (!bt_crypto_sign_att(att->crypto, sign->key, pdu,
804  pdu_len - BT_ATT_SIGNATURE_LEN, sign_cnt,
805  signature))
806  goto fail;
807 
808  return true;
809 
810 fail:
812  "ATT failed to verify signature: 0x%02x", opcode);
813 
814  return false;
815 }
816 
817 static void handle_notify(struct bt_att *att, uint8_t opcode, uint8_t *pdu,
818  ssize_t pdu_len)
819 {
820  const struct queue_entry *entry;
821  bool found;
822 
823  if ((opcode & ATT_OP_SIGNED_MASK) && !att->ext_signed) {
824  if (!handle_signed(att, opcode, pdu, pdu_len))
825  return;
826  pdu_len -= BT_ATT_SIGNATURE_LEN;
827  }
828 
829  bt_att_ref(att);
830 
831  found = false;
832  entry = queue_get_entries(att->notify_list);
833 
834  while (entry) {
835  struct att_notify *notify = entry->data;
836 
837  entry = entry->next;
838 
839  if (!opcode_match(notify->opcode, opcode))
840  continue;
841 
842  found = true;
843 
844  if (notify->callback)
845  notify->callback(opcode, pdu, pdu_len,
846  notify->user_data);
847 
848  /* callback could remove all entries from notify list */
849  if (queue_isempty(att->notify_list))
850  break;
851  }
852 
853  /*
854  * If this was a request and no handler was registered for it, respond
855  * with "Not Supported"
856  */
857  if (!found && get_op_type(opcode) == ATT_OP_TYPE_REQ)
858  respond_not_supported(att, opcode);
859 
860  bt_att_unref(att);
861 }
862 
863 static bool can_read_data(struct io *io, void *user_data)
864 {
865  struct bt_att *att = user_data;
866  uint8_t opcode;
867  uint8_t *pdu;
868  ssize_t bytes_read;
869 
870  bytes_read = read(att->fd, att->buf, att->mtu);
871  if (bytes_read < 0)
872  return false;
873 
874  util_hexdump('>', att->buf, bytes_read,
875  att->debug_callback, att->debug_data);
876 
877  if (bytes_read < ATT_MIN_PDU_LEN)
878  return true;
879 
880  pdu = att->buf;
881  opcode = pdu[0];
882 
883  bt_att_ref(att);
884 
885  /* Act on the received PDU based on the opcode type */
886  switch (get_op_type(opcode)) {
887  case ATT_OP_TYPE_RSP:
889  "ATT response received: 0x%02x", opcode);
890  handle_rsp(att, opcode, pdu + 1, bytes_read - 1);
891  break;
892  case ATT_OP_TYPE_CONF:
894  "ATT confirmation received: 0x%02x", opcode);
895  handle_conf(att, pdu + 1, bytes_read - 1);
896  break;
897  case ATT_OP_TYPE_REQ:
898  /*
899  * If a request is currently pending, then the sequential
900  * protocol was violated. Disconnect the bearer, which will
901  * promptly notify the upper layer via disconnect handlers.
902  */
903  if (att->in_req) {
905  "Received request while another is "
906  "pending: 0x%02x", opcode);
907  io_shutdown(att->io);
908  bt_att_unref(att);
909 
910  return false;
911  }
912 
913  att->in_req = true;
914 
915  /* Fall through to the next case */
916  case ATT_OP_TYPE_CMD:
917  case ATT_OP_TYPE_NOT:
918  case ATT_OP_TYPE_UNKNOWN:
919  case ATT_OP_TYPE_IND:
920  default:
921  /* For all other opcodes notify the upper layer of the PDU and
922  * let them act on it.
923  */
925  "ATT PDU received: 0x%02x", opcode);
926  handle_notify(att, opcode, pdu + 1, bytes_read - 1);
927  break;
928  }
929 
930  bt_att_unref(att);
931 
932  return true;
933 }
934 
935 static bool is_io_l2cap_based(int fd)
936 {
937  int domain;
938  int proto;
939  int err;
940  socklen_t len;
941 
942  domain = 0;
943  len = sizeof(domain);
944  err = getsockopt(fd, SOL_SOCKET, SO_DOMAIN, &domain, &len);
945  if (err < 0)
946  return false;
947 
948  if (domain != AF_BLUETOOTH)
949  return false;
950 
951  proto = 0;
952  len = sizeof(proto);
953  err = getsockopt(fd, SOL_SOCKET, SO_PROTOCOL, &proto, &len);
954  if (err < 0)
955  return false;
956 
957  return proto == BTPROTO_L2CAP;
958 }
959 
960 static void bt_att_free(struct bt_att *att)
961 {
962  if (att->pending_req)
964 
965  if (att->pending_ind)
967 
968  io_destroy(att->io);
969  bt_crypto_unref(att->crypto);
970 
971  queue_destroy(att->req_queue, NULL);
972  queue_destroy(att->ind_queue, NULL);
973  queue_destroy(att->write_queue, NULL);
974  queue_destroy(att->notify_list, NULL);
975  queue_destroy(att->disconn_list, NULL);
976 
977  if (att->timeout_destroy)
978  att->timeout_destroy(att->timeout_data);
979 
980  if (att->debug_destroy)
981  att->debug_destroy(att->debug_data);
982 
983  free(att->local_sign);
984  free(att->remote_sign);
985 
986  free(att->buf);
987 
988  free(att);
989 }
990 
991 struct bt_att *bt_att_new(int fd, bool ext_signed)
992 {
993  struct bt_att *att;
994 
995  if (fd < 0)
996  return NULL;
997 
998  att = new0(struct bt_att, 1);
999  if (!att)
1000  return NULL;
1001 
1002  att->fd = fd;
1003  att->ext_signed = ext_signed;
1004  att->mtu = BT_ATT_DEFAULT_LE_MTU;
1005  att->buf = malloc(att->mtu);
1006  if (!att->buf)
1007  goto fail;
1008 
1009  att->io = io_new(fd);
1010  if (!att->io)
1011  goto fail;
1012 
1013  /* crypto is optional, if not available leave it NULL */
1014  if (!ext_signed)
1015  att->crypto = bt_crypto_new();
1016 
1017  att->req_queue = queue_new();
1018  if (!att->req_queue)
1019  goto fail;
1020 
1021  att->ind_queue = queue_new();
1022  if (!att->ind_queue)
1023  goto fail;
1024 
1025  att->write_queue = queue_new();
1026  if (!att->write_queue)
1027  goto fail;
1028 
1029  att->notify_list = queue_new();
1030  if (!att->notify_list)
1031  goto fail;
1032 
1033  att->disconn_list = queue_new();
1034  if (!att->disconn_list)
1035  goto fail;
1036 
1037  if (!io_set_read_handler(att->io, can_read_data, att, NULL))
1038  goto fail;
1039 
1040  if (!io_set_disconnect_handler(att->io, disconnect_cb, att, NULL))
1041  goto fail;
1042 
1043  att->io_on_l2cap = is_io_l2cap_based(att->fd);
1044  if (!att->io_on_l2cap)
1046 
1047  return bt_att_ref(att);
1048 
1049 fail:
1050  bt_att_free(att);
1051 
1052  return NULL;
1053 }
1054 
1055 struct bt_att *bt_att_ref(struct bt_att *att)
1056 {
1057  if (!att)
1058  return NULL;
1059 
1060  __sync_fetch_and_add(&att->ref_count, 1);
1061 
1062  return att;
1063 }
1064 
1065 void bt_att_unref(struct bt_att *att)
1066 {
1067  if (!att)
1068  return;
1069 
1070  if (__sync_sub_and_fetch(&att->ref_count, 1))
1071  return;
1072 
1073  bt_att_unregister_all(att);
1074  bt_att_cancel_all(att);
1075 
1076  bt_att_free(att);
1077 }
1078 
1079 bool bt_att_set_close_on_unref(struct bt_att *att, bool do_close)
1080 {
1081  if (!att || !att->io)
1082  return false;
1083 
1084  return io_set_close_on_destroy(att->io, do_close);
1085 }
1086 
1087 int bt_att_get_fd(struct bt_att *att)
1088 {
1089  if (!att)
1090  return -1;
1091 
1092  return att->fd;
1093 }
1094 
1095 bool bt_att_set_debug(struct bt_att *att, bt_att_debug_func_t callback,
1096  void *user_data, bt_att_destroy_func_t destroy)
1097 {
1098  if (!att)
1099  return false;
1100 
1101  if (att->debug_destroy)
1102  att->debug_destroy(att->debug_data);
1103 
1104  att->debug_callback = callback;
1105  att->debug_destroy = destroy;
1106  att->debug_data = user_data;
1107 
1108  return true;
1109 }
1110 
1111 uint16_t bt_att_get_mtu(struct bt_att *att)
1112 {
1113  if (!att)
1114  return 0;
1115 
1116  return att->mtu;
1117 }
1118 
1119 bool bt_att_set_mtu(struct bt_att *att, uint16_t mtu)
1120 {
1121  void *buf;
1122 
1123  if (!att)
1124  return false;
1125 
1126  if (mtu < BT_ATT_DEFAULT_LE_MTU)
1127  return false;
1128 
1129  buf = malloc(mtu);
1130  if (!buf)
1131  return false;
1132 
1133  free(att->buf);
1134 
1135  att->mtu = mtu;
1136  att->buf = buf;
1137 
1138  return true;
1139 }
1140 
1142  void *user_data,
1143  bt_att_destroy_func_t destroy)
1144 {
1145  if (!att)
1146  return false;
1147 
1148  if (att->timeout_destroy)
1149  att->timeout_destroy(att->timeout_data);
1150 
1151  att->timeout_callback = callback;
1152  att->timeout_destroy = destroy;
1153  att->timeout_data = user_data;
1154 
1155  return true;
1156 }
1157 
1158 unsigned int bt_att_register_disconnect(struct bt_att *att,
1159  bt_att_disconnect_func_t callback,
1160  void *user_data,
1161  bt_att_destroy_func_t destroy)
1162 {
1163  struct att_disconn *disconn;
1164 
1165  if (!att || !att->io)
1166  return 0;
1167 
1168  disconn = new0(struct att_disconn, 1);
1169  if (!disconn)
1170  return 0;
1171 
1172  disconn->callback = callback;
1173  disconn->destroy = destroy;
1174  disconn->user_data = user_data;
1175 
1176  if (att->next_reg_id < 1)
1177  att->next_reg_id = 1;
1178 
1179  disconn->id = att->next_reg_id++;
1180 
1181  if (!queue_push_tail(att->disconn_list, disconn)) {
1182  free(disconn);
1183  return 0;
1184  }
1185 
1186  return disconn->id;
1187 }
1188 
1189 bool bt_att_unregister_disconnect(struct bt_att *att, unsigned int id)
1190 {
1191  struct att_disconn *disconn;
1192 
1193  if (!att || !id)
1194  return false;
1195 
1197  UINT_TO_PTR(id));
1198  if (!disconn)
1199  return false;
1200 
1201  destroy_att_disconn(disconn);
1202  return true;
1203 }
1204 
1220 unsigned int bt_att_send(struct bt_att *att, uint8_t opcode,
1221  const void *pdu, uint16_t length,
1224 {
1225  struct att_send_op *op;
1226  bool result;
1227 
1228  if (!att || !att->io)
1229  return 0;
1230 
1231  op = create_att_send_op(att, opcode, pdu, length, callback, user_data,
1232  destroy);
1233  if (!op)
1234  return 0;
1235 
1236  if (att->next_send_id < 1)
1237  att->next_send_id = 1;
1238 
1239  op->id = att->next_send_id++;
1240 
1241  /* Add the op to the correct queue based on its type */
1242  switch (op->type) {
1243  case ATT_OP_TYPE_REQ:
1244  result = queue_push_tail(att->req_queue, op);
1245  break;
1246  case ATT_OP_TYPE_IND:
1247  result = queue_push_tail(att->ind_queue, op);
1248  break;
1249  case ATT_OP_TYPE_CMD:
1250  case ATT_OP_TYPE_NOT:
1251  case ATT_OP_TYPE_UNKNOWN:
1252  case ATT_OP_TYPE_RSP:
1253  case ATT_OP_TYPE_CONF:
1254  default:
1255  result = queue_push_tail(att->write_queue, op);
1256  break;
1257  }
1258 
1259  if (!result) {
1260  free(op->pdu);
1261  free(op);
1262  return 0;
1263  }
1264 
1265  wakeup_writer(att);
1266 
1267  return op->id;
1268 }
1269 
1270 static bool match_op_id(const void *a, const void *b)
1271 {
1272  const struct att_send_op *op = a;
1273  unsigned int id = PTR_TO_UINT(b);
1274 
1275  return op->id == id;
1276 }
1277 
1278 bool bt_att_cancel(struct bt_att *att, unsigned int id)
1279 {
1280  struct att_send_op *op;
1281 
1282  if (!att || !id)
1283  return false;
1284 
1285  if (att->pending_req && att->pending_req->id == id) {
1286  /* Don't cancel the pending request; remove it's handlers */
1288  return true;
1289  }
1290 
1291  if (att->pending_ind && att->pending_ind->id == id) {
1292  /* Don't cancel the pending indication; remove it's handlers */
1294  return true;
1295  }
1296 
1298  if (op)
1299  goto done;
1300 
1302  if (op)
1303  goto done;
1304 
1306  if (op)
1307  goto done;
1308 
1309  if (!op)
1310  return false;
1311 
1312 done:
1313  destroy_att_send_op(op);
1314 
1315  wakeup_writer(att);
1316 
1317  return true;
1318 }
1319 
1320 bool bt_att_cancel_all(struct bt_att *att)
1321 {
1322  if (!att)
1323  return false;
1324 
1325  queue_remove_all(att->req_queue, NULL, NULL, destroy_att_send_op);
1326  queue_remove_all(att->ind_queue, NULL, NULL, destroy_att_send_op);
1328 
1329  if (att->pending_req)
1330  /* Don't cancel the pending request; remove it's handlers */
1332 
1333  if (att->pending_ind)
1334  /* Don't cancel the pending request; remove it's handlers */
1336 
1337  return true;
1338 }
1339 
1340 static uint8_t att_ecode_from_error(int err)
1341 {
1342  /*
1343  * If the error fits in a single byte, treat it as an ATT protocol
1344  * error as is. Since "0" is not a valid ATT protocol error code, we map
1345  * that to UNLIKELY below.
1346  */
1347  if (err > 0 && err < UINT8_MAX)
1348  return err;
1349 
1350  /*
1351  * Since we allow UNIX errnos, map them to appropriate ATT protocol
1352  * and "Common Profile and Service" error codes.
1353  */
1354  switch (err) {
1355  case -ENOENT:
1357  case -ENOMEM:
1359  case -EALREADY:
1361  case -EOVERFLOW:
1362  return BT_ERROR_OUT_OF_RANGE;
1363  }
1364 
1365  return BT_ATT_ERROR_UNLIKELY;
1366 }
1367 
1368 unsigned int bt_att_send_error_rsp(struct bt_att *att, uint8_t opcode,
1369  uint16_t handle, int error)
1370 {
1371  struct bt_att_pdu_error_rsp pdu;
1372  uint8_t ecode;
1373 
1374  if (!att || !opcode)
1375  return 0;
1376 
1377  ecode = att_ecode_from_error(error);
1378 
1379  memset(&pdu, 0, sizeof(pdu));
1380 
1381  pdu.opcode = opcode;
1382  put_le16(handle, &pdu.handle);
1383  pdu.ecode = ecode;
1384 
1385  return bt_att_send(att, BT_ATT_OP_ERROR_RSP, &pdu, sizeof(pdu),
1386  NULL, NULL, NULL);
1387 }
1388 
1389 unsigned int bt_att_register(struct bt_att *att, uint8_t opcode,
1390  bt_att_notify_func_t callback,
1391  void *user_data,
1392  bt_att_destroy_func_t destroy)
1393 {
1394  struct att_notify *notify;
1395 
1396  if (!att || !callback || !att->io)
1397  return 0;
1398 
1399  notify = new0(struct att_notify, 1);
1400  if (!notify)
1401  return 0;
1402 
1403  notify->opcode = opcode;
1404  notify->callback = callback;
1405  notify->destroy = destroy;
1406  notify->user_data = user_data;
1407 
1408  if (att->next_reg_id < 1)
1409  att->next_reg_id = 1;
1410 
1411  notify->id = att->next_reg_id++;
1412 
1413  if (!queue_push_tail(att->notify_list, notify)) {
1414  free(notify);
1415  return 0;
1416  }
1417 
1418  return notify->id;
1419 }
1420 
1421 bool bt_att_unregister(struct bt_att *att, unsigned int id)
1422 {
1423  struct att_notify *notify;
1424 
1425  if (!att || !id)
1426  return false;
1427 
1429  UINT_TO_PTR(id));
1430  if (!notify)
1431  return false;
1432 
1433  destroy_att_notify(notify);
1434  return true;
1435 }
1436 
1438 {
1439  if (!att)
1440  return false;
1441 
1442  queue_remove_all(att->notify_list, NULL, NULL, destroy_att_notify);
1444 
1445  return true;
1446 }
1447 
1448 int bt_att_get_security(struct bt_att *att)
1449 {
1450  struct bt_security sec;
1451  socklen_t len;
1452 
1453  if (!att)
1454  return -EINVAL;
1455 
1456  if (!att->io_on_l2cap)
1457  return att->io_sec_level;
1458 
1459  memset(&sec, 0, sizeof(sec));
1460  len = sizeof(sec);
1461  if (getsockopt(att->fd, SOL_BLUETOOTH, BT_SECURITY, &sec, &len) < 0)
1462  return -EIO;
1463 
1464  return sec.level;
1465 }
1466 
1467 bool bt_att_set_security(struct bt_att *att, int level)
1468 {
1469  struct bt_security sec;
1470 
1471  if (!att || level < BT_ATT_SECURITY_AUTO ||
1472  level > BT_ATT_SECURITY_HIGH)
1473  return false;
1474 
1475  if (!att->io_on_l2cap) {
1476  att->io_sec_level = level;
1477  return true;
1478  }
1479 
1480  memset(&sec, 0, sizeof(sec));
1481  sec.level = level;
1482 
1483  if (setsockopt(att->fd, SOL_BLUETOOTH, BT_SECURITY, &sec,
1484  sizeof(sec)) < 0)
1485  return false;
1486 
1487  return true;
1488 }
1489 
1490 static bool sign_set_key(struct sign_info **sign, uint8_t key[16],
1491  bt_att_counter_func_t func, void *user_data)
1492 {
1493  if (!(*sign)) {
1494  *sign = new0(struct sign_info, 1);
1495  if (!(*sign))
1496  return false;
1497  }
1498 
1499  (*sign)->counter = func;
1500  (*sign)->user_data = user_data;
1501  memcpy((*sign)->key, key, 16);
1502 
1503  return true;
1504 }
1505 
1506 bool bt_att_set_local_key(struct bt_att *att, uint8_t sign_key[16],
1507  bt_att_counter_func_t func, void *user_data)
1508 {
1509  if (!att)
1510  return false;
1511 
1512  return sign_set_key(&att->local_sign, sign_key, func, user_data);
1513 }
1514 
1515 bool bt_att_set_remote_key(struct bt_att *att, uint8_t sign_key[16],
1516  bt_att_counter_func_t func, void *user_data)
1517 {
1518  if (!att)
1519  return false;
1520 
1521  return sign_set_key(&att->remote_sign, sign_key, func, user_data);
1522 }
1523 
1524 bool bt_att_has_crypto(struct bt_att *att)
1525 {
1526  if (!att)
1527  return false;
1528 
1529  return att->crypto ? true : false;
1530 }
#define BT_ERROR_OUT_OF_RANGE
Definition: att-types.h:107
void(* bt_att_destroy_func_t)(void *user_data)
Definition: att.h:44
void(* bt_att_disconnect_func_t)(int err, void *user_data)
Definition: att.h:48
int fd
socket
Definition: att.c:68
#define BT_ATT_OP_HANDLE_VAL_CONF
Definition: att-types.h:67
command_func_t func
uint8_t req_opcode
Definition: att.c:187
#define BT_SECURITY
Definition: bluetooth.h:63
#define BT_ATT_OP_READ_BY_TYPE_REQ
Definition: att-types.h:47
bool bt_att_set_remote_key(struct bt_att *att, uint8_t sign_key[16], bt_att_counter_func_t func, void *user_data)
Definition: att.c:1515
enum att_op_type type
Definition: att.c:141
#define PTR_TO_INT(p)
Definition: util.h:79
void queue_foreach(struct queue *queue, queue_foreach_func_t function, void *user_data)
Definition: queue.c:325
const struct queue_entry * queue_get_entries(struct queue *queue)
Definition: queue.c:540
bool io_set_write_handler(struct io *io, io_callback_func_t callback, void *user_data, io_destroy_func_t destroy)
Definition: io-mainloop.c:317
unsigned int next_reg_id
IDs for registered callbacks.
Definition: att.c:100
struct queue * disconn_list
List of disconnect handlers.
Definition: att.c:90
#define BT_ATT_OP_READ_RSP
Definition: att-types.h:50
#define BT_ATT_SECURITY_MEDIUM
Definition: att-types.h:32
#define BT_ATT_OP_READ_BY_TYPE_RSP
Definition: att-types.h:48
bool bt_att_cancel_all(struct bt_att *att)
Definition: att.c:1320
#define BT_ATT_SECURITY_AUTO
Definition: att-types.h:30
struct io * io_new(int fd)
Definition: io-mainloop.c:197
static bool is_io_l2cap_based(int fd)
Definition: att.c:935
#define BT_ATT_OP_SIGNED_WRITE_CMD
Definition: att-types.h:60
data structure to manage io
Definition: io-mainloop.c:49
unsigned int id
Definition: att.c:286
struct att_send_op * pending_ind
Pending indication state.
Definition: att.c:82
ssize_t io_send(struct io *io, const struct iovec *iov, int iovcnt)
Definition: io-mainloop.c:395
struct sign_info * remote_sign
remote key structure pointer
Definition: att.c:120
uint8_t opcode
Definition: att.c:751
void * data
Definition: queue.h:32
void(* bt_att_debug_func_t)(const char *str, void *user_data)
Definition: att.h:45
bool handler_found
Definition: att.c:754
bool bt_att_unregister_all(struct bt_att *att)
Definition: att.c:1437
bt_att_destroy_func_t destroy
Definition: att.c:289
bool bt_att_unregister_disconnect(struct bt_att *att, unsigned int id)
Definition: att.c:1189
bool io_set_close_on_destroy(struct io *io, bool do_close)
Definition: io-mainloop.c:260
void * user_data
Definition: att.c:290
#define AF_BLUETOOTH
Definition: bluetooth.h:41
bool bt_att_set_local_key(struct bt_att *att, uint8_t sign_key[16], bt_att_counter_func_t func, void *user_data)
Definition: att.c:1506
uint8_t key[16]
Definition: att.c:124
static uint8_t get_req_opcode(uint8_t rsp_opcode)
Definition: att.c:204
#define BT_ATT_OP_HANDLE_VAL_IND
Definition: att-types.h:66
#define BT_ERROR_ALREADY_IN_PROGRESS
Definition: att-types.h:106
bt_att_destroy_func_t destroy
Definition: att.c:224
void * queue_pop_head(struct queue *queue)
Definition: queue.c:268
Definition: gatt-db.c:70
struct sign_info * local_sign
local key structure pointer
Definition: att.c:118
static bool match_notify_id(const void *a, const void *b)
Definition: att.c:277
#define ATT_OP_SIGNED_MASK
Definition: att.c:53
uint8_t rsp_opcode
Definition: att.c:188
void * timeout_data
Definition: att.c:106
void util_hexdump(const char dir, const unsigned char *buf, size_t len, util_debug_func_t function, void *user_data)
Definition: util.c:81
#define BT_ATT_OP_READ_BLOB_REQ
Definition: att-types.h:51
static bool encode_pdu(struct bt_att *att, struct att_send_op *op, const void *pdu, uint16_t length)
Definition: att.c:311
#define BT_ATT_OP_PREP_WRITE_REQ
Definition: att-types.h:61
#define BT_ATT_OP_PREP_WRITE_RSP
Definition: att-types.h:62
static uint8_t att_ecode_from_error(int err)
Definition: att.c:1340
Definition: queue.h:30
enum att_op_type type
Definition: att.c:219
static void wakeup_writer(struct bt_att *att)
Definition: att.c:550
static void handle_conf(struct bt_att *att, uint8_t *pdu, ssize_t pdu_len)
Definition: att.c:726
#define BT_ATT_OP_MTU_REQ
Definition: att-types.h:41
bool bt_att_unregister(struct bt_att *att, unsigned int id)
Definition: att.c:1421
uint16_t mtu
actual number of bytes for pdu ATT exchange
Definition: att.c:96
#define BT_ATT_OP_WRITE_REQ
Definition: att-types.h:57
Definition: queue.c:40
unsigned int next_send_id
IDs for "send" ops.
Definition: att.c:98
int ref_count
reference counter incremented by bt_att_ref, decremented by bt_att_unref
Definition: att.c:66
static bool match_op_id(const void *a, const void *b)
Definition: att.c:1270
static enum att_op_type get_op_type(uint8_t opcode)
Definition: att.c:174
ssize_t pdu_len
Definition: att.c:753
#define INT_TO_PTR(u)
Definition: util.h:80
#define BT_SECURITY_LOW
Definition: bluetooth.h:69
static void put_le16(uint16_t val, void *dst)
Definition: util.h:130
#define BT_ATT_OP_FIND_INFO_RSP
Definition: att-types.h:44
static bool disconnect_cb(struct io *io, void *user_data)
Definition: att.c:583
bool queue_isempty(struct queue *queue)
Definition: queue.c:569
bool io_set_disconnect_handler(struct io *io, io_callback_func_t callback, void *user_data, io_destroy_func_t destroy)
Definition: io-mainloop.c:356
bool writer_active
true if already engaged in write operation
Definition: att.c:86
static struct att_send_op * create_att_send_op(struct bt_att *att, uint8_t opcode, const void *pdu, uint16_t length, bt_att_response_func_t callback, void *user_data, bt_att_destroy_func_t destroy)
Definition: att.c:354
#define BT_ATT_OP_READ_BLOB_RSP
Definition: att-types.h:52
void * user_data
Definition: mainloop.c:83
void io_destroy(struct io *io)
Definition: io-mainloop.c:226
int io_sec_level
i/o seurity level: Only used for non-L2CAP
Definition: att.c:74
bool bt_att_has_crypto(struct bt_att *att)
Definition: att.c:1524
struct bt_crypto * bt_crypto_new(void)
Definition: crypto.c:158
void * user_data
Definition: att.c:126
bool bt_att_set_mtu(struct bt_att *att, uint16_t mtu)
Definition: att.c:1119
bool in_req
There's a pending incoming request.
Definition: att.c:92
#define BT_ATT_SECURITY_HIGH
Definition: att-types.h:33
#define BT_ATT_OP_WRITE_RSP
Definition: att-types.h:58
struct io * io
io structure for low level i/o (read and write)
Definition: att.c:70
#define BT_ATT_OP_READ_MULT_RSP
Definition: att-types.h:54
#define BT_ATT_ERROR_UNLIKELY
Definition: att-types.h:94
bool bt_att_set_debug(struct bt_att *att, bt_att_debug_func_t callback, void *user_data, bt_att_destroy_func_t destroy)
Definition: att.c:1095
static struct att_send_op * pick_next_send_op(struct bt_att *att)
Definition: att.c:403
void queue_destroy(struct queue *queue, queue_destroy_func_t destroy)
Definition: queue.c:102
static void cancel_att_send_op(struct att_send_op *op)
Definition: att.c:249
bool bt_att_set_security(struct bt_att *att, int level)
Definition: att.c:1467
static const struct @0 att_opcode_type_table[]
static uint32_t get_le32(const void *ptr)
Definition: util.h:110
bool ext_signed
true, requires key signature
Definition: att.c:116
#define PTR_TO_UINT(p)
Definition: util.h:76
static bool timeout_cb(void *user_data)
Definition: att.c:438
bool bt_att_cancel(struct bt_att *att, unsigned int id)
Definition: att.c:1278
static void handle_rsp(struct bt_att *att, uint8_t opcode, uint8_t *pdu, ssize_t pdu_len)
Definition: att.c:665
static void handle_notify(struct bt_att *att, uint8_t opcode, uint8_t *pdu, ssize_t pdu_len)
Definition: att.c:817
uint16_t bt_att_get_mtu(struct bt_att *att)
Definition: att.c:1111
bool queue_push_tail(struct queue *queue, void *data)
Definition: queue.c:167
struct queue * queue_new(void)
Definition: queue.c:81
#define BT_ATT_OP_EXEC_WRITE_REQ
Definition: att-types.h:63
struct bt_att * bt_att_new(int fd, bool ext_signed)
Definition: att.c:991
#define BT_ATT_SIGNATURE_LEN
Definition: att.c:57
bool io_on_l2cap
true if an l2cap socket
Definition: att.c:72
bool bt_att_set_timeout_cb(struct bt_att *att, bt_att_timeout_func_t callback, void *user_data, bt_att_destroy_func_t destroy)
Definition: att.c:1141
#define BT_ATT_OP_MTU_RSP
Definition: att-types.h:42
bt_att_destroy_func_t debug_destroy
data management function for debug
Definition: att.c:110
void * user_data
Definition: att.c:264
#define BT_ATT_OP_READ_BY_GRP_TYPE_REQ
Definition: att-types.h:55
bool bt_att_set_close_on_unref(struct bt_att *att, bool do_close)
Definition: att.c:1079
unsigned int bt_att_send_error_rsp(struct bt_att *att, uint8_t opcode, uint16_t handle, int error)
Definition: att.c:1368
bt_att_debug_func_t debug_callback
debug callback
Definition: att.c:108
#define BT_ATT_OP_ERROR_RSP
Definition: att-types.h:40
unsigned int queue_remove_all(struct queue *queue, queue_match_func_t function, void *user_data, queue_destroy_func_t destroy)
Definition: queue.c:487
struct bt_att * att
Definition: att.c:434
#define BT_ATT_OP_WRITE_CMD
Definition: att-types.h:59
bool io_set_read_handler(struct io *io, io_callback_func_t callback, void *user_data, io_destroy_func_t destroy)
Definition: io-mainloop.c:278
bt_att_response_func_t callback
Definition: att.c:223
struct queue * notify_list
List of registered callbacks.
Definition: att.c:88
#define BT_ATT_OP_HANDLE_VAL_NOT
Definition: att-types.h:65
Definition: att.c:123
void util_debug(util_debug_func_t function, void *user_data, const char *format,...)
Definition: util.c:56
#define BT_ATT_OP_FIND_BY_TYPE_VAL_REQ
Definition: att-types.h:45
static void bt_att_free(struct bt_att *att)
Definition: att.c:960
unsigned int timeout_id
Definition: att.c:218
bool removed
Definition: att.c:287
bt_att_destroy_func_t timeout_destroy
timeout function to manage data context (house keeping)
Definition: att.c:104
bool bt_crypto_sign_att(struct bt_crypto *crypto, const uint8_t key[16], const uint8_t *m, uint16_t m_len, uint32_t sign_cnt, uint8_t signature[12])
Definition: crypto.c:286
#define ATT_TIMEOUT_INTERVAL
Definition: att.c:54
void(* bt_att_notify_func_t)(uint8_t opcode, const void *pdu, uint16_t length, void *user_data)
Definition: att.h:42
bool(* bt_att_counter_func_t)(uint32_t *sign_cnt, void *user_data)
Definition: att.h:49
#define UINT_TO_PTR(u)
Definition: util.h:77
static const struct @1 att_req_rsp_mapping_table[]
static bool change_security(struct bt_att *att, uint8_t ecode)
Definition: att.c:617
#define BT_ATT_OP_READ_MULT_REQ
Definition: att-types.h:53
static bool opcode_match(uint8_t opcode, uint8_t test_opcode)
Definition: att.c:757
static bool sign_set_key(struct sign_info **sign, uint8_t key[16], bt_att_counter_func_t func, void *user_data)
Definition: att.c:1490
#define BT_ATT_ERROR_AUTHENTICATION
Definition: att-types.h:85
static bool can_read_data(struct io *io, void *user_data)
Definition: att.c:863
void * queue_remove_if(struct queue *queue, queue_match_func_t function, void *user_data)
Definition: queue.c:440
uint16_t len
Definition: att.c:222
unsigned int id
Definition: att.c:217
void * pdu
Definition: att.c:221
#define ATT_MIN_PDU_LEN
Definition: att.c:51
struct att_send_op * pending_req
Pending request state.
Definition: att.c:78
#define BT_ATT_OP_READ_BY_GRP_TYPE_RSP
Definition: att-types.h:56
bt_att_timeout_func_t timeout_callback
timeout function for callback
Definition: att.c:102
#define BT_ATT_ALL_REQUESTS
Definition: att-types.h:78
uint8_t * pdu
Definition: att.c:752
#define BT_ATT_ERROR_INVALID_HANDLE
Definition: att-types.h:81
static void disconn_handler(void *data, void *user_data)
Definition: att.c:571
Definition: att.c:64
bt_att_destroy_func_t destroy
Definition: att.c:263
#define BT_ATT_DEFAULT_LE_MTU
Definition: att-types.h:35
int bt_att_get_security(struct bt_att *att)
Definition: att.c:1448
void(* bt_att_response_func_t)(uint8_t opcode, const void *pdu, uint16_t length, void *user_data)
Definition: att.h:40
#define BT_ATT_ERROR_INSUFFICIENT_RESOURCES
Definition: att-types.h:97
void bt_crypto_unref(struct bt_crypto *crypto)
Definition: crypto.c:200
#define BTPROTO_L2CAP
Definition: bluetooth.h:45
static void destroy_att_notify(void *data)
Definition: att.c:267
bt_att_counter_func_t counter
Definition: att.c:125
att_op_type
Definition: att.c:129
static void respond_not_supported(struct bt_att *att, uint8_t opcode)
Definition: att.c:768
#define BT_ATT_OP_FIND_BY_TYPE_VAL_RSP
Definition: att-types.h:46
bool io_shutdown(struct io *io)
Definition: io-mainloop.c:417
#define SOL_BLUETOOTH
Definition: bluetooth.h:60
uint16_t opcode
Definition: att.c:220
#define BT_ATT_OP_EXEC_WRITE_RSP
Definition: att-types.h:64
uint16_t opcode
Definition: att.c:261
struct bt_crypto * crypto
crypto structure
Definition: att.c:114
unsigned int timeout_add(unsigned int timeout, timeout_func_t func, void *user_data, timeout_destroy_func_t destroy)
Definition: timeout-glib.c:58
struct queue_entry * next
Definition: queue.h:33
static bool match_disconn_id(const void *a, const void *b)
Definition: att.c:303
bt_att_notify_func_t callback
Definition: att.c:262
static void destroy_att_disconn(void *data)
Definition: att.c:293
uint8_t level
Definition: bluetooth.h:65
static bool can_write_data(struct io *io, void *user_data)
Definition: att.c:481
unsigned int id
Definition: att.c:435
uint8_t * buf
buffer pointer
Definition: att.c:94
struct queue * write_queue
Queue of PDUs ready to send.
Definition: att.c:84
bt_att_disconnect_func_t callback
Definition: att.c:288
void(* bt_att_timeout_func_t)(unsigned int id, uint8_t opcode, void *user_data)
Definition: att.h:46
struct queue * ind_queue
Queued ATT protocol indications.
Definition: att.c:80
#define new0(t, n)
Definition: util.h:82
void timeout_remove(unsigned int id)
Definition: timeout-glib.c:80
#define BT_ATT_ERROR_REQUEST_NOT_SUPPORTED
Definition: att-types.h:86
#define BT_ATT_OP_READ_REQ
Definition: att-types.h:49
#define BT_ATT_OP_FIND_INFO_REQ
Definition: att-types.h:43
static void destroy_att_send_op(void *data)
destroy att send operation calls the destroy callback with user_data as an argument free pdu data ...
Definition: att.c:235
uint8_t opcode
Definition: att.c:140
static bool handle_error_rsp(struct bt_att *att, uint8_t *pdu, ssize_t pdu_len, uint8_t *opcode)
Definition: att.c:637
void * debug_data
user pointer for debug
Definition: att.c:112
unsigned int id
Definition: att.c:260
unsigned int bt_att_register(struct bt_att *att, uint8_t opcode, bt_att_notify_func_t callback, void *user_data, bt_att_destroy_func_t destroy)
Definition: att.c:1389
static void write_watch_destroy(void *user_data)
Definition: att.c:474
struct queue * req_queue
Queued ATT protocol requests.
Definition: att.c:76
unsigned int bt_att_register_disconnect(struct bt_att *att, bt_att_disconnect_func_t callback, void *user_data, bt_att_destroy_func_t destroy)
Definition: att.c:1158
unsigned int bt_att_send(struct bt_att *att, uint8_t opcode, const void *pdu, uint16_t length, bt_att_response_func_t callback, void *user_data, bt_att_destroy_func_t destroy)
Definition: att.c:1220
void * user_data
Definition: att.c:225
void bt_att_unref(struct bt_att *att)
Definition: att.c:1065
int bt_att_get_fd(struct bt_att *att)
Definition: att.c:1087
static bool handle_signed(struct bt_att *att, uint8_t opcode, uint8_t *pdu, ssize_t pdu_len)
Definition: att.c:780
struct bt_att * bt_att_ref(struct bt_att *att)
Definition: att.c:1055
#define BT_ATT_ERROR_INSUFFICIENT_ENCRYPTION
Definition: att-types.h:95
bool queue_push_head(struct queue *queue, void *data)
Definition: queue.c:198