l2cap.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /* SPDX-License-Identifier: LGPL-2.1-or-later */
  2. /*
  3. *
  4. * BlueZ - Bluetooth protocol stack for Linux
  5. *
  6. * Copyright (C) 2011-2014 Intel Corporation
  7. * Copyright (C) 2002-2010 Marcel Holtmann <marcel@holtmann.org>
  8. *
  9. *
  10. */
  11. #include <stdint.h>
  12. #include <stdbool.h>
  13. struct l2cap_frame {
  14. uint16_t index;
  15. bool in;
  16. uint16_t handle;
  17. uint8_t ident;
  18. uint16_t cid;
  19. uint16_t psm;
  20. uint16_t chan;
  21. uint8_t mode;
  22. uint8_t seq_num;
  23. const void *data;
  24. uint16_t size;
  25. };
  26. static inline void l2cap_frame_pull(struct l2cap_frame *frame,
  27. const struct l2cap_frame *source, uint16_t len)
  28. {
  29. if (frame != source) {
  30. frame->index = source->index;
  31. frame->in = source->in;
  32. frame->handle = source->handle;
  33. frame->ident = source->ident;
  34. frame->cid = source->cid;
  35. frame->psm = source->psm;
  36. frame->chan = source->chan;
  37. frame->mode = source->mode;
  38. }
  39. frame->data = source->data + len;
  40. frame->size = source->size - len;
  41. }
  42. static inline bool l2cap_frame_get_u8(struct l2cap_frame *frame, uint8_t *value)
  43. {
  44. if (frame->size < sizeof(*value))
  45. return false;
  46. if (value)
  47. *value = *((uint8_t *) frame->data);
  48. l2cap_frame_pull(frame, frame, sizeof(*value));
  49. return true;
  50. }
  51. static inline bool l2cap_frame_get_be16(struct l2cap_frame *frame,
  52. uint16_t *value)
  53. {
  54. if (frame->size < sizeof(*value))
  55. return false;
  56. if (value)
  57. *value = get_be16(frame->data);
  58. l2cap_frame_pull(frame, frame, sizeof(*value));
  59. return true;
  60. }
  61. static inline bool l2cap_frame_get_le16(struct l2cap_frame *frame,
  62. uint16_t *value)
  63. {
  64. if (frame->size < sizeof(*value))
  65. return false;
  66. if (value)
  67. *value = get_le16(frame->data);
  68. l2cap_frame_pull(frame, frame, sizeof(*value));
  69. return true;
  70. }
  71. static inline bool l2cap_frame_get_be32(struct l2cap_frame *frame,
  72. uint32_t *value)
  73. {
  74. if (frame->size < sizeof(*value))
  75. return false;
  76. if (value)
  77. *value = get_be32(frame->data);
  78. l2cap_frame_pull(frame, frame, sizeof(*value));
  79. return true;
  80. }
  81. static inline bool l2cap_frame_get_le32(struct l2cap_frame *frame,
  82. uint32_t *value)
  83. {
  84. if (frame->size < sizeof(*value))
  85. return false;
  86. if (value)
  87. *value = get_le32(frame->data);
  88. l2cap_frame_pull(frame, frame, sizeof(*value));
  89. return true;
  90. }
  91. static inline bool l2cap_frame_get_be64(struct l2cap_frame *frame,
  92. uint64_t *value)
  93. {
  94. if (frame->size < sizeof(*value))
  95. return false;
  96. if (value)
  97. *value = get_be64(frame->data);
  98. l2cap_frame_pull(frame, frame, sizeof(*value));
  99. return true;
  100. }
  101. static inline bool l2cap_frame_get_le64(struct l2cap_frame *frame,
  102. uint64_t *value)
  103. {
  104. if (frame->size < sizeof(*value))
  105. return false;
  106. if (value)
  107. *value = get_le64(frame->data);
  108. l2cap_frame_pull(frame, frame, sizeof(*value));
  109. return true;
  110. }
  111. static inline bool l2cap_frame_get_be128(struct l2cap_frame *frame,
  112. uint64_t *lvalue, uint64_t *rvalue)
  113. {
  114. if (frame->size < (sizeof(*lvalue) + sizeof(*rvalue)))
  115. return false;
  116. if (lvalue && rvalue) {
  117. *lvalue = get_be64(frame->data);
  118. *rvalue = get_be64(frame->data);
  119. }
  120. l2cap_frame_pull(frame, frame, (sizeof(*lvalue) + sizeof(*rvalue)));
  121. return true;
  122. }
  123. void l2cap_frame(uint16_t index, bool in, uint16_t handle, uint16_t cid,
  124. uint16_t psm, const void *data, uint16_t size);
  125. void l2cap_packet(uint16_t index, bool in, uint16_t handle, uint8_t flags,
  126. const void *data, uint16_t size);
  127. void rfcomm_packet(const struct l2cap_frame *frame);