1 /* buf_simple.c - Simple network buffer management */
2 
3 /*
4  * Copyright (c) 2015-2019 Intel Corporation
5  *
6  * SPDX-License-Identifier: Apache-2.0
7  */
8 
9 #include <zephyr/logging/log.h>
10 LOG_MODULE_REGISTER(net_buf_simple, CONFIG_NET_BUF_LOG_LEVEL);
11 
12 #include <stdio.h>
13 #include <stddef.h>
14 #include <string.h>
15 #include <zephyr/sys/byteorder.h>
16 
17 #include <zephyr/net/buf.h>
18 
19 #if defined(CONFIG_NET_BUF_SIMPLE_LOG)
20 #define NET_BUF_SIMPLE_DBG(fmt, ...) LOG_DBG("(%p) " fmt, k_current_get(), \
21 				      ##__VA_ARGS__)
22 #define NET_BUF_SIMPLE_ERR(fmt, ...) LOG_ERR(fmt, ##__VA_ARGS__)
23 #define NET_BUF_SIMPLE_WARN(fmt, ...) LOG_WRN(fmt, ##__VA_ARGS__)
24 #define NET_BUF_SIMPLE_INFO(fmt, ...) LOG_INF(fmt, ##__VA_ARGS__)
25 #else
26 #define NET_BUF_SIMPLE_DBG(fmt, ...)
27 #define NET_BUF_SIMPLE_ERR(fmt, ...)
28 #define NET_BUF_SIMPLE_WARN(fmt, ...)
29 #define NET_BUF_SIMPLE_INFO(fmt, ...)
30 #endif /* CONFIG_NET_BUF_SIMPLE_LOG */
31 
net_buf_simple_init_with_data(struct net_buf_simple * buf,void * data,size_t size)32 void net_buf_simple_init_with_data(struct net_buf_simple *buf,
33 				   void *data, size_t size)
34 {
35 	buf->__buf = data;
36 	buf->data  = data;
37 	buf->size  = size;
38 	buf->len   = size;
39 }
40 
net_buf_simple_reserve(struct net_buf_simple * buf,size_t reserve)41 void net_buf_simple_reserve(struct net_buf_simple *buf, size_t reserve)
42 {
43 	__ASSERT_NO_MSG(buf);
44 	__ASSERT_NO_MSG(buf->len == 0U);
45 	NET_BUF_SIMPLE_DBG("buf %p reserve %zu", buf, reserve);
46 
47 	buf->data = buf->__buf + reserve;
48 }
49 
net_buf_simple_clone(const struct net_buf_simple * original,struct net_buf_simple * clone)50 void net_buf_simple_clone(const struct net_buf_simple *original,
51 			  struct net_buf_simple *clone)
52 {
53 	memcpy(clone, original, sizeof(struct net_buf_simple));
54 }
55 
net_buf_simple_add(struct net_buf_simple * buf,size_t len)56 void *net_buf_simple_add(struct net_buf_simple *buf, size_t len)
57 {
58 	uint8_t *tail = net_buf_simple_tail(buf);
59 
60 	NET_BUF_SIMPLE_DBG("buf %p len %zu", buf, len);
61 
62 	__ASSERT_NO_MSG(net_buf_simple_tailroom(buf) >= len);
63 
64 	buf->len += len;
65 	return tail;
66 }
67 
net_buf_simple_add_mem(struct net_buf_simple * buf,const void * mem,size_t len)68 void *net_buf_simple_add_mem(struct net_buf_simple *buf, const void *mem,
69 			     size_t len)
70 {
71 	NET_BUF_SIMPLE_DBG("buf %p len %zu", buf, len);
72 
73 	return memcpy(net_buf_simple_add(buf, len), mem, len);
74 }
75 
net_buf_simple_add_u8(struct net_buf_simple * buf,uint8_t val)76 uint8_t *net_buf_simple_add_u8(struct net_buf_simple *buf, uint8_t val)
77 {
78 	uint8_t *u8;
79 
80 	NET_BUF_SIMPLE_DBG("buf %p val 0x%02x", buf, val);
81 
82 	u8 = net_buf_simple_add(buf, 1);
83 	*u8 = val;
84 
85 	return u8;
86 }
87 
net_buf_simple_add_le16(struct net_buf_simple * buf,uint16_t val)88 void net_buf_simple_add_le16(struct net_buf_simple *buf, uint16_t val)
89 {
90 	NET_BUF_SIMPLE_DBG("buf %p val %u", buf, val);
91 
92 	sys_put_le16(val, net_buf_simple_add(buf, sizeof(val)));
93 }
94 
net_buf_simple_add_be16(struct net_buf_simple * buf,uint16_t val)95 void net_buf_simple_add_be16(struct net_buf_simple *buf, uint16_t val)
96 {
97 	NET_BUF_SIMPLE_DBG("buf %p val %u", buf, val);
98 
99 	sys_put_be16(val, net_buf_simple_add(buf, sizeof(val)));
100 }
101 
net_buf_simple_add_le24(struct net_buf_simple * buf,uint32_t val)102 void net_buf_simple_add_le24(struct net_buf_simple *buf, uint32_t val)
103 {
104 	NET_BUF_SIMPLE_DBG("buf %p val %u", buf, val);
105 
106 	sys_put_le24(val, net_buf_simple_add(buf, 3));
107 }
108 
net_buf_simple_add_be24(struct net_buf_simple * buf,uint32_t val)109 void net_buf_simple_add_be24(struct net_buf_simple *buf, uint32_t val)
110 {
111 	NET_BUF_SIMPLE_DBG("buf %p val %u", buf, val);
112 
113 	sys_put_be24(val, net_buf_simple_add(buf, 3));
114 }
115 
net_buf_simple_add_le32(struct net_buf_simple * buf,uint32_t val)116 void net_buf_simple_add_le32(struct net_buf_simple *buf, uint32_t val)
117 {
118 	NET_BUF_SIMPLE_DBG("buf %p val %u", buf, val);
119 
120 	sys_put_le32(val, net_buf_simple_add(buf, sizeof(val)));
121 }
122 
net_buf_simple_add_be32(struct net_buf_simple * buf,uint32_t val)123 void net_buf_simple_add_be32(struct net_buf_simple *buf, uint32_t val)
124 {
125 	NET_BUF_SIMPLE_DBG("buf %p val %u", buf, val);
126 
127 	sys_put_be32(val, net_buf_simple_add(buf, sizeof(val)));
128 }
129 
net_buf_simple_add_le40(struct net_buf_simple * buf,uint64_t val)130 void net_buf_simple_add_le40(struct net_buf_simple *buf, uint64_t val)
131 {
132 	NET_BUF_SIMPLE_DBG("buf %p val %" PRIu64, buf, val);
133 
134 	sys_put_le40(val, net_buf_simple_add(buf, 5));
135 }
136 
net_buf_simple_add_be40(struct net_buf_simple * buf,uint64_t val)137 void net_buf_simple_add_be40(struct net_buf_simple *buf, uint64_t val)
138 {
139 	NET_BUF_SIMPLE_DBG("buf %p val %" PRIu64, buf, val);
140 
141 	sys_put_be40(val, net_buf_simple_add(buf, 5));
142 }
143 
net_buf_simple_add_le48(struct net_buf_simple * buf,uint64_t val)144 void net_buf_simple_add_le48(struct net_buf_simple *buf, uint64_t val)
145 {
146 	NET_BUF_SIMPLE_DBG("buf %p val %" PRIu64, buf, val);
147 
148 	sys_put_le48(val, net_buf_simple_add(buf, 6));
149 }
150 
net_buf_simple_add_be48(struct net_buf_simple * buf,uint64_t val)151 void net_buf_simple_add_be48(struct net_buf_simple *buf, uint64_t val)
152 {
153 	NET_BUF_SIMPLE_DBG("buf %p val %" PRIu64, buf, val);
154 
155 	sys_put_be48(val, net_buf_simple_add(buf, 6));
156 }
157 
net_buf_simple_add_le64(struct net_buf_simple * buf,uint64_t val)158 void net_buf_simple_add_le64(struct net_buf_simple *buf, uint64_t val)
159 {
160 	NET_BUF_SIMPLE_DBG("buf %p val %" PRIu64, buf, val);
161 
162 	sys_put_le64(val, net_buf_simple_add(buf, sizeof(val)));
163 }
164 
net_buf_simple_add_be64(struct net_buf_simple * buf,uint64_t val)165 void net_buf_simple_add_be64(struct net_buf_simple *buf, uint64_t val)
166 {
167 	NET_BUF_SIMPLE_DBG("buf %p val %" PRIu64, buf, val);
168 
169 	sys_put_be64(val, net_buf_simple_add(buf, sizeof(val)));
170 }
171 
net_buf_simple_remove_mem(struct net_buf_simple * buf,size_t len)172 void *net_buf_simple_remove_mem(struct net_buf_simple *buf, size_t len)
173 {
174 	NET_BUF_SIMPLE_DBG("buf %p len %zu", buf, len);
175 
176 	__ASSERT_NO_MSG(buf->len >= len);
177 
178 	buf->len -= len;
179 	return buf->data + buf->len;
180 }
181 
net_buf_simple_remove_u8(struct net_buf_simple * buf)182 uint8_t net_buf_simple_remove_u8(struct net_buf_simple *buf)
183 {
184 	uint8_t val;
185 	void *ptr;
186 
187 	ptr = net_buf_simple_remove_mem(buf, sizeof(val));
188 	val = *(uint8_t *)ptr;
189 
190 	return val;
191 }
192 
net_buf_simple_remove_le16(struct net_buf_simple * buf)193 uint16_t net_buf_simple_remove_le16(struct net_buf_simple *buf)
194 {
195 	uint16_t val;
196 	void *ptr;
197 
198 	ptr = net_buf_simple_remove_mem(buf, sizeof(val));
199 	val = UNALIGNED_GET((uint16_t *)ptr);
200 
201 	return sys_le16_to_cpu(val);
202 }
203 
net_buf_simple_remove_be16(struct net_buf_simple * buf)204 uint16_t net_buf_simple_remove_be16(struct net_buf_simple *buf)
205 {
206 	uint16_t val;
207 	void *ptr;
208 
209 	ptr = net_buf_simple_remove_mem(buf, sizeof(val));
210 	val = UNALIGNED_GET((uint16_t *)ptr);
211 
212 	return sys_be16_to_cpu(val);
213 }
214 
net_buf_simple_remove_le24(struct net_buf_simple * buf)215 uint32_t net_buf_simple_remove_le24(struct net_buf_simple *buf)
216 {
217 	struct uint24 {
218 		uint32_t u24 : 24;
219 	} __packed val;
220 	void *ptr;
221 
222 	ptr = net_buf_simple_remove_mem(buf, sizeof(val));
223 	val = UNALIGNED_GET((struct uint24 *)ptr);
224 
225 	return sys_le24_to_cpu(val.u24);
226 }
227 
net_buf_simple_remove_be24(struct net_buf_simple * buf)228 uint32_t net_buf_simple_remove_be24(struct net_buf_simple *buf)
229 {
230 	struct uint24 {
231 		uint32_t u24 : 24;
232 	} __packed val;
233 	void *ptr;
234 
235 	ptr = net_buf_simple_remove_mem(buf, sizeof(val));
236 	val = UNALIGNED_GET((struct uint24 *)ptr);
237 
238 	return sys_be24_to_cpu(val.u24);
239 }
240 
net_buf_simple_remove_le32(struct net_buf_simple * buf)241 uint32_t net_buf_simple_remove_le32(struct net_buf_simple *buf)
242 {
243 	uint32_t val;
244 	void *ptr;
245 
246 	ptr = net_buf_simple_remove_mem(buf, sizeof(val));
247 	val = UNALIGNED_GET((uint32_t *)ptr);
248 
249 	return sys_le32_to_cpu(val);
250 }
251 
net_buf_simple_remove_be32(struct net_buf_simple * buf)252 uint32_t net_buf_simple_remove_be32(struct net_buf_simple *buf)
253 {
254 	uint32_t val;
255 	void *ptr;
256 
257 	ptr = net_buf_simple_remove_mem(buf, sizeof(val));
258 	val = UNALIGNED_GET((uint32_t *)ptr);
259 
260 	return sys_be32_to_cpu(val);
261 }
262 
net_buf_simple_remove_le40(struct net_buf_simple * buf)263 uint64_t net_buf_simple_remove_le40(struct net_buf_simple *buf)
264 {
265 	struct uint40 {
266 		uint64_t u40: 40;
267 	} __packed val;
268 	void *ptr;
269 
270 	ptr = net_buf_simple_remove_mem(buf, sizeof(val));
271 	val = UNALIGNED_GET((struct uint40 *)ptr);
272 
273 	return sys_le40_to_cpu(val.u40);
274 }
275 
net_buf_simple_remove_be40(struct net_buf_simple * buf)276 uint64_t net_buf_simple_remove_be40(struct net_buf_simple *buf)
277 {
278 	struct uint40 {
279 		uint64_t u40: 40;
280 	} __packed val;
281 	void *ptr;
282 
283 	ptr = net_buf_simple_remove_mem(buf, sizeof(val));
284 	val = UNALIGNED_GET((struct uint40 *)ptr);
285 
286 	return sys_be40_to_cpu(val.u40);
287 }
288 
net_buf_simple_remove_le48(struct net_buf_simple * buf)289 uint64_t net_buf_simple_remove_le48(struct net_buf_simple *buf)
290 {
291 	struct uint48 {
292 		uint64_t u48 : 48;
293 	} __packed val;
294 	void *ptr;
295 
296 	ptr = net_buf_simple_remove_mem(buf, sizeof(val));
297 	val = UNALIGNED_GET((struct uint48 *)ptr);
298 
299 	return sys_le48_to_cpu(val.u48);
300 }
301 
net_buf_simple_remove_be48(struct net_buf_simple * buf)302 uint64_t net_buf_simple_remove_be48(struct net_buf_simple *buf)
303 {
304 	struct uint48 {
305 		uint64_t u48 : 48;
306 	} __packed val;
307 	void *ptr;
308 
309 	ptr = net_buf_simple_remove_mem(buf, sizeof(val));
310 	val = UNALIGNED_GET((struct uint48 *)ptr);
311 
312 	return sys_be48_to_cpu(val.u48);
313 }
314 
net_buf_simple_remove_le64(struct net_buf_simple * buf)315 uint64_t net_buf_simple_remove_le64(struct net_buf_simple *buf)
316 {
317 	uint64_t val;
318 	void *ptr;
319 
320 	ptr = net_buf_simple_remove_mem(buf, sizeof(val));
321 	val = UNALIGNED_GET((uint64_t *)ptr);
322 
323 	return sys_le64_to_cpu(val);
324 }
325 
net_buf_simple_remove_be64(struct net_buf_simple * buf)326 uint64_t net_buf_simple_remove_be64(struct net_buf_simple *buf)
327 {
328 	uint64_t val;
329 	void *ptr;
330 
331 	ptr = net_buf_simple_remove_mem(buf, sizeof(val));
332 	val = UNALIGNED_GET((uint64_t *)ptr);
333 
334 	return sys_be64_to_cpu(val);
335 }
336 
net_buf_simple_push(struct net_buf_simple * buf,size_t len)337 void *net_buf_simple_push(struct net_buf_simple *buf, size_t len)
338 {
339 	NET_BUF_SIMPLE_DBG("buf %p len %zu", buf, len);
340 
341 	__ASSERT_NO_MSG(net_buf_simple_headroom(buf) >= len);
342 
343 	buf->data -= len;
344 	buf->len += len;
345 	return buf->data;
346 }
347 
net_buf_simple_push_mem(struct net_buf_simple * buf,const void * mem,size_t len)348 void *net_buf_simple_push_mem(struct net_buf_simple *buf, const void *mem,
349 			      size_t len)
350 {
351 	NET_BUF_SIMPLE_DBG("buf %p len %zu", buf, len);
352 
353 	return memcpy(net_buf_simple_push(buf, len), mem, len);
354 }
355 
net_buf_simple_push_le16(struct net_buf_simple * buf,uint16_t val)356 void net_buf_simple_push_le16(struct net_buf_simple *buf, uint16_t val)
357 {
358 	NET_BUF_SIMPLE_DBG("buf %p val %u", buf, val);
359 
360 	sys_put_le16(val, net_buf_simple_push(buf, sizeof(val)));
361 }
362 
net_buf_simple_push_be16(struct net_buf_simple * buf,uint16_t val)363 void net_buf_simple_push_be16(struct net_buf_simple *buf, uint16_t val)
364 {
365 	NET_BUF_SIMPLE_DBG("buf %p val %u", buf, val);
366 
367 	sys_put_be16(val, net_buf_simple_push(buf, sizeof(val)));
368 }
369 
net_buf_simple_push_u8(struct net_buf_simple * buf,uint8_t val)370 void net_buf_simple_push_u8(struct net_buf_simple *buf, uint8_t val)
371 {
372 	uint8_t *data = net_buf_simple_push(buf, 1);
373 
374 	*data = val;
375 }
376 
net_buf_simple_push_le24(struct net_buf_simple * buf,uint32_t val)377 void net_buf_simple_push_le24(struct net_buf_simple *buf, uint32_t val)
378 {
379 	NET_BUF_SIMPLE_DBG("buf %p val %u", buf, val);
380 
381 	sys_put_le24(val, net_buf_simple_push(buf, 3));
382 }
383 
net_buf_simple_push_be24(struct net_buf_simple * buf,uint32_t val)384 void net_buf_simple_push_be24(struct net_buf_simple *buf, uint32_t val)
385 {
386 	NET_BUF_SIMPLE_DBG("buf %p val %u", buf, val);
387 
388 	sys_put_be24(val, net_buf_simple_push(buf, 3));
389 }
390 
net_buf_simple_push_le32(struct net_buf_simple * buf,uint32_t val)391 void net_buf_simple_push_le32(struct net_buf_simple *buf, uint32_t val)
392 {
393 	NET_BUF_SIMPLE_DBG("buf %p val %u", buf, val);
394 
395 	sys_put_le32(val, net_buf_simple_push(buf, sizeof(val)));
396 }
397 
net_buf_simple_push_be32(struct net_buf_simple * buf,uint32_t val)398 void net_buf_simple_push_be32(struct net_buf_simple *buf, uint32_t val)
399 {
400 	NET_BUF_SIMPLE_DBG("buf %p val %u", buf, val);
401 
402 	sys_put_be32(val, net_buf_simple_push(buf, sizeof(val)));
403 }
404 
net_buf_simple_push_le40(struct net_buf_simple * buf,uint64_t val)405 void net_buf_simple_push_le40(struct net_buf_simple *buf, uint64_t val)
406 {
407 	NET_BUF_SIMPLE_DBG("buf %p val %" PRIu64, buf, val);
408 
409 	sys_put_le40(val, net_buf_simple_push(buf, 5));
410 }
411 
net_buf_simple_push_be40(struct net_buf_simple * buf,uint64_t val)412 void net_buf_simple_push_be40(struct net_buf_simple *buf, uint64_t val)
413 {
414 	NET_BUF_SIMPLE_DBG("buf %p val %" PRIu64, buf, val);
415 
416 	sys_put_be40(val, net_buf_simple_push(buf, 5));
417 }
418 
net_buf_simple_push_le48(struct net_buf_simple * buf,uint64_t val)419 void net_buf_simple_push_le48(struct net_buf_simple *buf, uint64_t val)
420 {
421 	NET_BUF_SIMPLE_DBG("buf %p val %" PRIu64, buf, val);
422 
423 	sys_put_le48(val, net_buf_simple_push(buf, 6));
424 }
425 
net_buf_simple_push_be48(struct net_buf_simple * buf,uint64_t val)426 void net_buf_simple_push_be48(struct net_buf_simple *buf, uint64_t val)
427 {
428 	NET_BUF_SIMPLE_DBG("buf %p val %" PRIu64, buf, val);
429 
430 	sys_put_be48(val, net_buf_simple_push(buf, 6));
431 }
432 
net_buf_simple_push_le64(struct net_buf_simple * buf,uint64_t val)433 void net_buf_simple_push_le64(struct net_buf_simple *buf, uint64_t val)
434 {
435 	NET_BUF_SIMPLE_DBG("buf %p val %" PRIu64, buf, val);
436 
437 	sys_put_le64(val, net_buf_simple_push(buf, sizeof(val)));
438 }
439 
net_buf_simple_push_be64(struct net_buf_simple * buf,uint64_t val)440 void net_buf_simple_push_be64(struct net_buf_simple *buf, uint64_t val)
441 {
442 	NET_BUF_SIMPLE_DBG("buf %p val %" PRIu64, buf, val);
443 
444 	sys_put_be64(val, net_buf_simple_push(buf, sizeof(val)));
445 }
446 
net_buf_simple_pull(struct net_buf_simple * buf,size_t len)447 void *net_buf_simple_pull(struct net_buf_simple *buf, size_t len)
448 {
449 	NET_BUF_SIMPLE_DBG("buf %p len %zu", buf, len);
450 
451 	__ASSERT_NO_MSG(buf->len >= len);
452 
453 	buf->len -= len;
454 	return buf->data += len;
455 }
456 
net_buf_simple_pull_mem(struct net_buf_simple * buf,size_t len)457 void *net_buf_simple_pull_mem(struct net_buf_simple *buf, size_t len)
458 {
459 	void *data = buf->data;
460 
461 	NET_BUF_SIMPLE_DBG("buf %p len %zu", buf, len);
462 
463 	__ASSERT_NO_MSG(buf->len >= len);
464 
465 	buf->len -= len;
466 	buf->data += len;
467 
468 	return data;
469 }
470 
net_buf_simple_pull_u8(struct net_buf_simple * buf)471 uint8_t net_buf_simple_pull_u8(struct net_buf_simple *buf)
472 {
473 	uint8_t val;
474 
475 	val = buf->data[0];
476 	net_buf_simple_pull(buf, 1);
477 
478 	return val;
479 }
480 
net_buf_simple_pull_le16(struct net_buf_simple * buf)481 uint16_t net_buf_simple_pull_le16(struct net_buf_simple *buf)
482 {
483 	uint16_t val;
484 
485 	val = UNALIGNED_GET((uint16_t *)buf->data);
486 	net_buf_simple_pull(buf, sizeof(val));
487 
488 	return sys_le16_to_cpu(val);
489 }
490 
net_buf_simple_pull_be16(struct net_buf_simple * buf)491 uint16_t net_buf_simple_pull_be16(struct net_buf_simple *buf)
492 {
493 	uint16_t val;
494 
495 	val = UNALIGNED_GET((uint16_t *)buf->data);
496 	net_buf_simple_pull(buf, sizeof(val));
497 
498 	return sys_be16_to_cpu(val);
499 }
500 
net_buf_simple_pull_le24(struct net_buf_simple * buf)501 uint32_t net_buf_simple_pull_le24(struct net_buf_simple *buf)
502 {
503 	struct uint24 {
504 		uint32_t u24:24;
505 	} __packed val;
506 
507 	val = UNALIGNED_GET((struct uint24 *)buf->data);
508 	net_buf_simple_pull(buf, sizeof(val));
509 
510 	return sys_le24_to_cpu(val.u24);
511 }
512 
net_buf_simple_pull_be24(struct net_buf_simple * buf)513 uint32_t net_buf_simple_pull_be24(struct net_buf_simple *buf)
514 {
515 	struct uint24 {
516 		uint32_t u24:24;
517 	} __packed val;
518 
519 	val = UNALIGNED_GET((struct uint24 *)buf->data);
520 	net_buf_simple_pull(buf, sizeof(val));
521 
522 	return sys_be24_to_cpu(val.u24);
523 }
524 
net_buf_simple_pull_le32(struct net_buf_simple * buf)525 uint32_t net_buf_simple_pull_le32(struct net_buf_simple *buf)
526 {
527 	uint32_t val;
528 
529 	val = UNALIGNED_GET((uint32_t *)buf->data);
530 	net_buf_simple_pull(buf, sizeof(val));
531 
532 	return sys_le32_to_cpu(val);
533 }
534 
net_buf_simple_pull_be32(struct net_buf_simple * buf)535 uint32_t net_buf_simple_pull_be32(struct net_buf_simple *buf)
536 {
537 	uint32_t val;
538 
539 	val = UNALIGNED_GET((uint32_t *)buf->data);
540 	net_buf_simple_pull(buf, sizeof(val));
541 
542 	return sys_be32_to_cpu(val);
543 }
544 
net_buf_simple_pull_le40(struct net_buf_simple * buf)545 uint64_t net_buf_simple_pull_le40(struct net_buf_simple *buf)
546 {
547 	struct uint40 {
548 		uint64_t u40: 40;
549 	} __packed val;
550 
551 	val = UNALIGNED_GET((struct uint40 *)buf->data);
552 	net_buf_simple_pull(buf, sizeof(val));
553 
554 	return sys_le40_to_cpu(val.u40);
555 }
556 
net_buf_simple_pull_be40(struct net_buf_simple * buf)557 uint64_t net_buf_simple_pull_be40(struct net_buf_simple *buf)
558 {
559 	struct uint40 {
560 		uint64_t u40: 40;
561 	} __packed val;
562 
563 	val = UNALIGNED_GET((struct uint40 *)buf->data);
564 	net_buf_simple_pull(buf, sizeof(val));
565 
566 	return sys_be40_to_cpu(val.u40);
567 }
568 
net_buf_simple_pull_le48(struct net_buf_simple * buf)569 uint64_t net_buf_simple_pull_le48(struct net_buf_simple *buf)
570 {
571 	struct uint48 {
572 		uint64_t u48:48;
573 	} __packed val;
574 
575 	val = UNALIGNED_GET((struct uint48 *)buf->data);
576 	net_buf_simple_pull(buf, sizeof(val));
577 
578 	return sys_le48_to_cpu(val.u48);
579 }
580 
net_buf_simple_pull_be48(struct net_buf_simple * buf)581 uint64_t net_buf_simple_pull_be48(struct net_buf_simple *buf)
582 {
583 	struct uint48 {
584 		uint64_t u48:48;
585 	} __packed val;
586 
587 	val = UNALIGNED_GET((struct uint48 *)buf->data);
588 	net_buf_simple_pull(buf, sizeof(val));
589 
590 	return sys_be48_to_cpu(val.u48);
591 }
592 
net_buf_simple_pull_le64(struct net_buf_simple * buf)593 uint64_t net_buf_simple_pull_le64(struct net_buf_simple *buf)
594 {
595 	uint64_t val;
596 
597 	val = UNALIGNED_GET((uint64_t *)buf->data);
598 	net_buf_simple_pull(buf, sizeof(val));
599 
600 	return sys_le64_to_cpu(val);
601 }
602 
net_buf_simple_pull_be64(struct net_buf_simple * buf)603 uint64_t net_buf_simple_pull_be64(struct net_buf_simple *buf)
604 {
605 	uint64_t val;
606 
607 	val = UNALIGNED_GET((uint64_t *)buf->data);
608 	net_buf_simple_pull(buf, sizeof(val));
609 
610 	return sys_be64_to_cpu(val);
611 }
612 
net_buf_simple_headroom(const struct net_buf_simple * buf)613 size_t net_buf_simple_headroom(const struct net_buf_simple *buf)
614 {
615 	return buf->data - buf->__buf;
616 }
617 
net_buf_simple_tailroom(const struct net_buf_simple * buf)618 size_t net_buf_simple_tailroom(const struct net_buf_simple *buf)
619 {
620 	return buf->size - net_buf_simple_headroom(buf) - buf->len;
621 }
622 
net_buf_simple_max_len(const struct net_buf_simple * buf)623 uint16_t net_buf_simple_max_len(const struct net_buf_simple *buf)
624 {
625 	return buf->size - net_buf_simple_headroom(buf);
626 }
627