Skip to contentSkip to conversation

monitor: Add helper functions to get data from L2CAP frames

PATCH In review Luiz Augusto von Dentz 1 participant 2 messages Aug 22, 2014 – Aug 25, 2014 Bluetooth drivers

lore
Share & tools
lore

Luiz Augusto von Dentz

author maintainer
#0 lore raw

PATCH 1/1 monitor: Add helper functions to get data from L2CAP frames

From: Luiz Augusto von Dentz <[email protected]>

---
 monitor/l2cap.h | 118 +++++++++++++++++++++++++++++++++++++++++++++++++++-----
 1 file changed, 109 insertions(+), 9 deletions(-)
monitor/l2cap.h +109 -9
index a0f844b..645e3ef 100644
@@ -40,15 +40,115 @@ struct l2cap_frame {
 static inline void l2cap_frame_pull(struct l2cap_frame *frame,
 				const struct l2cap_frame *source, uint16_t len)
 {
-	frame->index   = source->index;
-	frame->in      = source->in;
-	frame->handle  = source->handle;
-	frame->cid     = source->cid;
-	frame->psm     = source->psm;
-	frame->chan    = source->chan;
-	frame->mode    = source->mode;
-	frame->data    = source->data + len;
-	frame->size    = source->size - len;
+	if (frame != source) {
+		frame->index   = source->index;
+		frame->in      = source->in;
+		frame->handle  = source->handle;
+		frame->cid     = source->cid;
+		frame->psm     = source->psm;
+		frame->chan    = source->chan;
+		frame->mode    = source->mode;
+	}
+
+	frame->data = source->data + len;
+	frame->size = source->size - len;
+}
+
+static inline int l2cap_frame_get_u8(struct l2cap_frame *frame, uint8_t *value)
+{
+	if (frame->size < sizeof(*value))
+		return -1;
+
+	if (value)
+		*value = *((uint8_t *) frame->data);
+
+	l2cap_frame_pull(frame, frame, sizeof(*value));
+
+	return 0;
+}
+
+static inline int l2cap_frame_get_be16(struct l2cap_frame *frame,
+								uint16_t *value)
+{
+	if (frame->size < sizeof(*value))
+		return -1;
+
+	if (value)
+		*value = get_be16(frame->data);
+
+	l2cap_frame_pull(frame, frame, sizeof(*value));
+
+	return 0;
+}
+
+static inline int l2cap_frame_get_le16(struct l2cap_frame *frame,
+								uint16_t *value)
+{
+	if (frame->size < sizeof(*value))
+		return -1;
+
+	if (value)
+		*value = get_le16(frame->data);
+
+	l2cap_frame_pull(frame, frame, sizeof(*value));
+
+	return 0;
+}
+
+static inline int l2cap_frame_get_be32(struct l2cap_frame *frame,
+								uint32_t *value)
+{
+	if (frame->size < sizeof(*value))
+		return -1;
+
+	if (value)
+		*value = get_be32(frame->data);
+
+	l2cap_frame_pull(frame, frame, sizeof(*value));
+
+	return 0;
+}
+
+static inline int l2cap_frame_get_le32(struct l2cap_frame *frame,
+								uint32_t *value)
+{
+	if (frame->size < sizeof(*value))
+		return -1;
+
+	if (value)
+		*value = get_le32(frame->data);
+
+	l2cap_frame_pull(frame, frame, sizeof(*value));
+
+	return 0;
+}
+
+static inline int l2cap_frame_get_be64(struct l2cap_frame *frame,
+								uint64_t *value)
+{
+	if (frame->size < sizeof(*value))
+		return -1;
+
+	if (value)
+		*value = get_be64(frame->data);
+
+	l2cap_frame_pull(frame, frame, sizeof(*value));
+
+	return 0;
+}
+
+static inline int l2cap_frame_get_le64(struct l2cap_frame *frame,
+								uint64_t *value)
+{
+	if (frame->size < sizeof(*value))
+		return -1;
+
+	if (value)
+		*value = get_le64(frame->data);
+
+	l2cap_frame_pull(frame, frame, sizeof(*value));
+
+	return 0;
 }
 
 void l2cap_packet(uint16_t index, bool in, uint16_t handle, uint8_t flags,
Signature · 1 line
1.9.3
author maintainer
#1 lore raw

Hi,

Quoted 136 lines · Luiz Augusto von Dentz

From: Luiz Augusto von Dentz <[email protected]>

---
 monitor/l2cap.h | 118 +++++++++++++++++++++++++++++++++++++++++++++++++++-----
 1 file changed, 109 insertions(+), 9 deletions(-)
monitor/l2cap.h +109 -10
index a0f844b..645e3ef 100644
@@ -40,15 +40,115 @@ struct l2cap_frame {
 static inline void l2cap_frame_pull(struct l2cap_frame *frame,
                                const struct l2cap_frame *source, uint16_t len)
 {
-       frame->index   = source->index;
-       frame->in      = source->in;
-       frame->handle  = source->handle;
-       frame->cid     = source->cid;
-       frame->psm     = source->psm;
-       frame->chan    = source->chan;
-       frame->mode    = source->mode;
-       frame->data    = source->data + len;
-       frame->size    = source->size - len;
+       if (frame != source) {
+               frame->index   = source->index;
+               frame->in      = source->in;
+               frame->handle  = source->handle;
+               frame->cid     = source->cid;
+               frame->psm     = source->psm;
+               frame->chan    = source->chan;
+               frame->mode    = source->mode;
+       }
+
+       frame->data = source->data + len;
+       frame->size = source->size - len;
+}
+
+static inline int l2cap_frame_get_u8(struct l2cap_frame *frame, uint8_t *value)
+{
+       if (frame->size < sizeof(*value))
+               return -1;
+
+       if (value)
+               *value = *((uint8_t *) frame->data);
+
+       l2cap_frame_pull(frame, frame, sizeof(*value));
+
+       return 0;
+}
+
+static inline int l2cap_frame_get_be16(struct l2cap_frame *frame,
+                                                               uint16_t *value)
+{
+       if (frame->size < sizeof(*value))
+               return -1;
+
+       if (value)
+               *value = get_be16(frame->data);
+
+       l2cap_frame_pull(frame, frame, sizeof(*value));
+
+       return 0;
+}
+
+static inline int l2cap_frame_get_le16(struct l2cap_frame *frame,
+                                                               uint16_t *value)
+{
+       if (frame->size < sizeof(*value))
+               return -1;
+
+       if (value)
+               *value = get_le16(frame->data);
+
+       l2cap_frame_pull(frame, frame, sizeof(*value));
+
+       return 0;
+}
+
+static inline int l2cap_frame_get_be32(struct l2cap_frame *frame,
+                                                               uint32_t *value)
+{
+       if (frame->size < sizeof(*value))
+               return -1;
+
+       if (value)
+               *value = get_be32(frame->data);
+
+       l2cap_frame_pull(frame, frame, sizeof(*value));
+
+       return 0;
+}
+
+static inline int l2cap_frame_get_le32(struct l2cap_frame *frame,
+                                                               uint32_t *value)
+{
+       if (frame->size < sizeof(*value))
+               return -1;
+
+       if (value)
+               *value = get_le32(frame->data);
+
+       l2cap_frame_pull(frame, frame, sizeof(*value));
+
+       return 0;
+}
+
+static inline int l2cap_frame_get_be64(struct l2cap_frame *frame,
+                                                               uint64_t *value)
+{
+       if (frame->size < sizeof(*value))
+               return -1;
+
+       if (value)
+               *value = get_be64(frame->data);
+
+       l2cap_frame_pull(frame, frame, sizeof(*value));
+
+       return 0;
+}
+
+static inline int l2cap_frame_get_le64(struct l2cap_frame *frame,
+                                                               uint64_t *value)
+{
+       if (frame->size < sizeof(*value))
+               return -1;
+
+       if (value)
+               *value = get_le64(frame->data);
+
+       l2cap_frame_pull(frame, frame, sizeof(*value));
+
+       return 0;
 }
 
 void l2cap_packet(uint16_t index, bool in, uint16_t handle, uint8_t flags,
--
Quoted 1 line

1.9.3

Pushed.

Signature · 1 line
Luiz Augusto von Dentz