mesh_buf.h 53 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758
  1. /** @file
  2. * @brief Buffer management.
  3. */
  4. /*
  5. * SPDX-FileCopyrightText: 2015 Intel Corporation
  6. * SPDX-FileContributor: 2018-2021 Espressif Systems (Shanghai) CO LTD
  7. *
  8. * SPDX-License-Identifier: Apache-2.0
  9. */
  10. #ifndef _BLE_MESH_BUF_H_
  11. #define _BLE_MESH_BUF_H_
  12. #include "mesh_config.h"
  13. #include "mesh_types.h"
  14. #include "mesh_slist.h"
  15. #include "mesh_compiler.h"
  16. #ifdef __cplusplus
  17. extern "C" {
  18. #endif
  19. /* Unaligned access */
  20. #define UNALIGNED_GET(p) \
  21. __extension__ ({ \
  22. struct __attribute__((__packed__)) { \
  23. __typeof__(*(p)) __v; \
  24. } *__p = (__typeof__(__p)) (p); \
  25. __p->__v; \
  26. })
  27. #define BLE_MESH_NET_BUF_USER_DATA_SIZE 4
  28. /**
  29. * @brief Network buffer library
  30. * @defgroup net_buf Network Buffer Library
  31. * @ingroup networking
  32. * @{
  33. */
  34. /* Alignment needed for various parts of the buffer definition */
  35. #define __net_buf_align __aligned(sizeof(int))
  36. /**
  37. * @def NET_BUF_SIMPLE_DEFINE
  38. * @brief Define a net_buf_simple stack variable.
  39. *
  40. * This is a helper macro which is used to define a net_buf_simple object
  41. * on the stack.
  42. *
  43. * @param _name Name of the net_buf_simple object.
  44. * @param _size Maximum data storage for the buffer.
  45. */
  46. #define NET_BUF_SIMPLE_DEFINE(_name, _size) \
  47. uint8_t net_buf_data_##_name[_size]; \
  48. struct net_buf_simple _name = { \
  49. .data = net_buf_data_##_name, \
  50. .len = 0, \
  51. .size = _size, \
  52. .__buf = net_buf_data_##_name, \
  53. }
  54. /**
  55. * @def NET_BUF_SIMPLE_DEFINE_STATIC
  56. * @brief Define a static net_buf_simple variable.
  57. *
  58. * This is a helper macro which is used to define a static net_buf_simple
  59. * object.
  60. *
  61. * @param _name Name of the net_buf_simple object.
  62. * @param _size Maximum data storage for the buffer.
  63. */
  64. #define NET_BUF_SIMPLE_DEFINE_STATIC(_name, _size) \
  65. static uint8_t net_buf_data_##_name[_size]; \
  66. static struct net_buf_simple _name = { \
  67. .data = net_buf_data_##_name, \
  68. .len = 0, \
  69. .size = _size, \
  70. .__buf = net_buf_data_##_name, \
  71. }
  72. /**
  73. * @brief Simple network buffer representation.
  74. *
  75. * This is a simpler variant of the net_buf object (in fact net_buf uses
  76. * net_buf_simple internally). It doesn't provide any kind of reference
  77. * counting, user data, dynamic allocation, or in general the ability to
  78. * pass through kernel objects such as FIFOs.
  79. *
  80. * The main use of this is for scenarios where the meta-data of the normal
  81. * net_buf isn't needed and causes too much overhead. This could be e.g.
  82. * when the buffer only needs to be allocated on the stack or when the
  83. * access to and lifetime of the buffer is well controlled and constrained.
  84. */
  85. struct net_buf_simple {
  86. /** Pointer to the start of data in the buffer. */
  87. uint8_t *data;
  88. /** Length of the data behind the data pointer. */
  89. uint16_t len;
  90. /** Amount of data that this buffer can store. */
  91. uint16_t size;
  92. /** Start of the data storage. Not to be accessed directly
  93. * (the data pointer should be used instead).
  94. */
  95. uint8_t *__buf;
  96. };
  97. /**
  98. * @def NET_BUF_SIMPLE
  99. * @brief Define a net_buf_simple stack variable and get a pointer to it.
  100. *
  101. * This is a helper macro which is used to define a net_buf_simple object on
  102. * the stack and the get a pointer to it as follows:
  103. *
  104. * struct net_buf_simple *my_buf = NET_BUF_SIMPLE(10);
  105. *
  106. * After creating the object it needs to be initialized by calling
  107. * net_buf_simple_init().
  108. *
  109. * @param _size Maximum data storage for the buffer.
  110. *
  111. * @return Pointer to stack-allocated net_buf_simple object.
  112. */
  113. #define NET_BUF_SIMPLE(_size) \
  114. ((struct net_buf_simple *)(&(struct { \
  115. struct net_buf_simple buf; \
  116. uint8_t data[_size] __net_buf_align; \
  117. }) { \
  118. .buf.size = _size, \
  119. .buf.__buf = NULL, \
  120. }))
  121. /**
  122. * @brief Initialize a net_buf_simple object.
  123. *
  124. * This needs to be called after creating a net_buf_simple object using
  125. * the NET_BUF_SIMPLE macro.
  126. *
  127. * @param buf Buffer to initialize.
  128. * @param reserve_head Headroom to reserve.
  129. */
  130. static inline void net_buf_simple_init(struct net_buf_simple *buf,
  131. size_t reserve_head)
  132. {
  133. if (!buf->__buf) {
  134. buf->__buf = (uint8_t *)buf + sizeof(*buf);
  135. }
  136. buf->data = buf->__buf + reserve_head;
  137. buf->len = 0;
  138. }
  139. /**
  140. * @brief Initialize a net_buf_simple object with data.
  141. *
  142. * Initialized buffer object with external data.
  143. *
  144. * @param buf Buffer to initialize.
  145. * @param data External data pointer
  146. * @param size Amount of data the pointed data buffer if able to fit.
  147. */
  148. void net_buf_simple_init_with_data(struct net_buf_simple *buf,
  149. void *data, size_t size);
  150. /**
  151. * @brief Reset buffer
  152. *
  153. * Reset buffer data so it can be reused for other purposes.
  154. *
  155. * @param buf Buffer to reset.
  156. */
  157. static inline void net_buf_simple_reset(struct net_buf_simple *buf)
  158. {
  159. buf->len = 0;
  160. buf->data = buf->__buf;
  161. }
  162. /**
  163. * Clone buffer state, using the same data buffer.
  164. *
  165. * Initializes a buffer to point to the same data as an existing buffer.
  166. * Allows operations on the same data without altering the length and
  167. * offset of the original.
  168. *
  169. * @param original Buffer to clone.
  170. * @param clone The new clone.
  171. */
  172. void net_buf_simple_clone(const struct net_buf_simple *original,
  173. struct net_buf_simple *clone);
  174. /**
  175. * @brief Prepare data to be added at the end of the buffer
  176. *
  177. * Increments the data length of a buffer to account for more data
  178. * at the end.
  179. *
  180. * @param buf Buffer to update.
  181. * @param len Number of bytes to increment the length with.
  182. *
  183. * @return The original tail of the buffer.
  184. */
  185. void *net_buf_simple_add(struct net_buf_simple *buf, size_t len);
  186. /**
  187. * @brief Copy given number of bytes from memory to the end of the buffer
  188. *
  189. * Increments the data length of the buffer to account for more data at the
  190. * end.
  191. *
  192. * @param buf Buffer to update.
  193. * @param mem Location of data to be added.
  194. * @param len Length of data to be added
  195. *
  196. * @return The original tail of the buffer.
  197. */
  198. void *net_buf_simple_add_mem(struct net_buf_simple *buf, const void *mem,
  199. size_t len);
  200. /**
  201. * @brief Add (8-bit) byte at the end of the buffer
  202. *
  203. * Increments the data length of the buffer to account for more data at the
  204. * end.
  205. *
  206. * @param buf Buffer to update.
  207. * @param val byte value to be added.
  208. *
  209. * @return Pointer to the value added
  210. */
  211. uint8_t *net_buf_simple_add_u8(struct net_buf_simple *buf, uint8_t val);
  212. /**
  213. * @brief Add 16-bit value at the end of the buffer
  214. *
  215. * Adds 16-bit value in little endian format at the end of buffer.
  216. * Increments the data length of a buffer to account for more data
  217. * at the end.
  218. *
  219. * @param buf Buffer to update.
  220. * @param val 16-bit value to be added.
  221. */
  222. void net_buf_simple_add_le16(struct net_buf_simple *buf, uint16_t val);
  223. /**
  224. * @brief Add 16-bit value at the end of the buffer
  225. *
  226. * Adds 16-bit value in big endian format at the end of buffer.
  227. * Increments the data length of a buffer to account for more data
  228. * at the end.
  229. *
  230. * @param buf Buffer to update.
  231. * @param val 16-bit value to be added.
  232. */
  233. void net_buf_simple_add_be16(struct net_buf_simple *buf, uint16_t val);
  234. /**
  235. * @brief Add 24-bit value at the end of the buffer
  236. *
  237. * Adds 24-bit value in little endian format at the end of buffer.
  238. * Increments the data length of a buffer to account for more data
  239. * at the end.
  240. *
  241. * @param buf Buffer to update.
  242. * @param val 24-bit value to be added.
  243. */
  244. void net_buf_simple_add_le24(struct net_buf_simple *buf, uint32_t val);
  245. /**
  246. * @brief Add 24-bit value at the end of the buffer
  247. *
  248. * Adds 24-bit value in big endian format at the end of buffer.
  249. * Increments the data length of a buffer to account for more data
  250. * at the end.
  251. *
  252. * @param buf Buffer to update.
  253. * @param val 24-bit value to be added.
  254. */
  255. void net_buf_simple_add_be24(struct net_buf_simple *buf, uint32_t val);
  256. /**
  257. * @brief Add 32-bit value at the end of the buffer
  258. *
  259. * Adds 32-bit value in little endian format at the end of buffer.
  260. * Increments the data length of a buffer to account for more data
  261. * at the end.
  262. *
  263. * @param buf Buffer to update.
  264. * @param val 32-bit value to be added.
  265. */
  266. void net_buf_simple_add_le32(struct net_buf_simple *buf, uint32_t val);
  267. /**
  268. * @brief Add 32-bit value at the end of the buffer
  269. *
  270. * Adds 32-bit value in big endian format at the end of buffer.
  271. * Increments the data length of a buffer to account for more data
  272. * at the end.
  273. *
  274. * @param buf Buffer to update.
  275. * @param val 32-bit value to be added.
  276. */
  277. void net_buf_simple_add_be32(struct net_buf_simple *buf, uint32_t val);
  278. /**
  279. * @brief Add 48-bit value at the end of the buffer
  280. *
  281. * Adds 48-bit value in little endian format at the end of buffer.
  282. * Increments the data length of a buffer to account for more data
  283. * at the end.
  284. *
  285. * @param buf Buffer to update.
  286. * @param val 48-bit value to be added.
  287. */
  288. void net_buf_simple_add_le48(struct net_buf_simple *buf, uint64_t val);
  289. /**
  290. * @brief Add 48-bit value at the end of the buffer
  291. *
  292. * Adds 48-bit value in big endian format at the end of buffer.
  293. * Increments the data length of a buffer to account for more data
  294. * at the end.
  295. *
  296. * @param buf Buffer to update.
  297. * @param val 48-bit value to be added.
  298. */
  299. void net_buf_simple_add_be48(struct net_buf_simple *buf, uint64_t val);
  300. /**
  301. * @brief Add 64-bit value at the end of the buffer
  302. *
  303. * Adds 64-bit value in little endian format at the end of buffer.
  304. * Increments the data length of a buffer to account for more data
  305. * at the end.
  306. *
  307. * @param buf Buffer to update.
  308. * @param val 64-bit value to be added.
  309. */
  310. void net_buf_simple_add_le64(struct net_buf_simple *buf, uint64_t val);
  311. /**
  312. * @brief Add 64-bit value at the end of the buffer
  313. *
  314. * Adds 64-bit value in big endian format at the end of buffer.
  315. * Increments the data length of a buffer to account for more data
  316. * at the end.
  317. *
  318. * @param buf Buffer to update.
  319. * @param val 64-bit value to be added.
  320. */
  321. void net_buf_simple_add_be64(struct net_buf_simple *buf, uint64_t val);
  322. /**
  323. * @brief Push data to the beginning of the buffer.
  324. *
  325. * Modifies the data pointer and buffer length to account for more data
  326. * in the beginning of the buffer.
  327. *
  328. * @param buf Buffer to update.
  329. * @param len Number of bytes to add to the beginning.
  330. *
  331. * @return The new beginning of the buffer data.
  332. */
  333. void *net_buf_simple_push(struct net_buf_simple *buf, size_t len);
  334. /**
  335. * @brief Push 16-bit value to the beginning of the buffer
  336. *
  337. * Adds 16-bit value in little endian format to the beginning of the
  338. * buffer.
  339. *
  340. * @param buf Buffer to update.
  341. * @param val 16-bit value to be pushed to the buffer.
  342. */
  343. void net_buf_simple_push_le16(struct net_buf_simple *buf, uint16_t val);
  344. /**
  345. * @brief Push 16-bit value to the beginning of the buffer
  346. *
  347. * Adds 16-bit value in big endian format to the beginning of the
  348. * buffer.
  349. *
  350. * @param buf Buffer to update.
  351. * @param val 16-bit value to be pushed to the buffer.
  352. */
  353. void net_buf_simple_push_be16(struct net_buf_simple *buf, uint16_t val);
  354. /**
  355. * @brief Push 8-bit value to the beginning of the buffer
  356. *
  357. * Adds 8-bit value the beginning of the buffer.
  358. *
  359. * @param buf Buffer to update.
  360. * @param val 8-bit value to be pushed to the buffer.
  361. */
  362. void net_buf_simple_push_u8(struct net_buf_simple *buf, uint8_t val);
  363. /**
  364. * @brief Push 24-bit value to the beginning of the buffer
  365. *
  366. * Adds 24-bit value in little endian format to the beginning of the
  367. * buffer.
  368. *
  369. * @param buf Buffer to update.
  370. * @param val 24-bit value to be pushed to the buffer.
  371. */
  372. void net_buf_simple_push_le24(struct net_buf_simple *buf, uint32_t val);
  373. /**
  374. * @brief Push 24-bit value to the beginning of the buffer
  375. *
  376. * Adds 24-bit value in big endian format to the beginning of the
  377. * buffer.
  378. *
  379. * @param buf Buffer to update.
  380. * @param val 24-bit value to be pushed to the buffer.
  381. */
  382. void net_buf_simple_push_be24(struct net_buf_simple *buf, uint32_t val);
  383. /**
  384. * @brief Push 32-bit value to the beginning of the buffer
  385. *
  386. * Adds 32-bit value in little endian format to the beginning of the
  387. * buffer.
  388. *
  389. * @param buf Buffer to update.
  390. * @param val 32-bit value to be pushed to the buffer.
  391. */
  392. void net_buf_simple_push_le32(struct net_buf_simple *buf, uint32_t val);
  393. /**
  394. * @brief Push 32-bit value to the beginning of the buffer
  395. *
  396. * Adds 32-bit value in big endian format to the beginning of the
  397. * buffer.
  398. *
  399. * @param buf Buffer to update.
  400. * @param val 32-bit value to be pushed to the buffer.
  401. */
  402. void net_buf_simple_push_be32(struct net_buf_simple *buf, uint32_t val);
  403. /**
  404. * @brief Push 48-bit value to the beginning of the buffer
  405. *
  406. * Adds 48-bit value in little endian format to the beginning of the
  407. * buffer.
  408. *
  409. * @param buf Buffer to update.
  410. * @param val 48-bit value to be pushed to the buffer.
  411. */
  412. void net_buf_simple_push_le48(struct net_buf_simple *buf, uint64_t val);
  413. /**
  414. * @brief Push 48-bit value to the beginning of the buffer
  415. *
  416. * Adds 48-bit value in big endian format to the beginning of the
  417. * buffer.
  418. *
  419. * @param buf Buffer to update.
  420. * @param val 48-bit value to be pushed to the buffer.
  421. */
  422. void net_buf_simple_push_be48(struct net_buf_simple *buf, uint64_t val);
  423. /**
  424. * @brief Push 64-bit value to the beginning of the buffer
  425. *
  426. * Adds 64-bit value in little endian format to the beginning of the
  427. * buffer.
  428. *
  429. * @param buf Buffer to update.
  430. * @param val 64-bit value to be pushed to the buffer.
  431. */
  432. void net_buf_simple_push_le64(struct net_buf_simple *buf, uint64_t val);
  433. /**
  434. * @brief Push 64-bit value to the beginning of the buffer
  435. *
  436. * Adds 64-bit value in big endian format to the beginning of the
  437. * buffer.
  438. *
  439. * @param buf Buffer to update.
  440. * @param val 64-bit value to be pushed to the buffer.
  441. */
  442. void net_buf_simple_push_be64(struct net_buf_simple *buf, uint64_t val);
  443. /**
  444. * @brief Remove data from the beginning of the buffer.
  445. *
  446. * Removes data from the beginning of the buffer by modifying the data
  447. * pointer and buffer length.
  448. *
  449. * @param buf Buffer to update.
  450. * @param len Number of bytes to remove.
  451. *
  452. * @return New beginning of the buffer data.
  453. */
  454. void *net_buf_simple_pull(struct net_buf_simple *buf, size_t len);
  455. /**
  456. * @brief Remove data from the beginning of the buffer.
  457. *
  458. * Removes data from the beginning of the buffer by modifying the data
  459. * pointer and buffer length.
  460. *
  461. * @param buf Buffer to update.
  462. * @param len Number of bytes to remove.
  463. *
  464. * @return Pointer to the old location of the buffer data.
  465. */
  466. void *net_buf_simple_pull_mem(struct net_buf_simple *buf, size_t len);
  467. /**
  468. * @brief Remove a 8-bit value from the beginning of the buffer
  469. *
  470. * Same idea as with net_buf_simple_pull(), but a helper for operating
  471. * on 8-bit values.
  472. *
  473. * @param buf A valid pointer on a buffer.
  474. *
  475. * @return The 8-bit removed value
  476. */
  477. uint8_t net_buf_simple_pull_u8(struct net_buf_simple *buf);
  478. /**
  479. * @brief Remove and convert 16 bits from the beginning of the buffer.
  480. *
  481. * Same idea as with net_buf_simple_pull(), but a helper for operating
  482. * on 16-bit little endian data.
  483. *
  484. * @param buf A valid pointer on a buffer.
  485. *
  486. * @return 16-bit value converted from little endian to host endian.
  487. */
  488. uint16_t net_buf_simple_pull_le16(struct net_buf_simple *buf);
  489. /**
  490. * @brief Remove and convert 16 bits from the beginning of the buffer.
  491. *
  492. * Same idea as with net_buf_simple_pull(), but a helper for operating
  493. * on 16-bit big endian data.
  494. *
  495. * @param buf A valid pointer on a buffer.
  496. *
  497. * @return 16-bit value converted from big endian to host endian.
  498. */
  499. uint16_t net_buf_simple_pull_be16(struct net_buf_simple *buf);
  500. /**
  501. * @brief Remove and convert 24 bits from the beginning of the buffer.
  502. *
  503. * Same idea as with net_buf_simple_pull(), but a helper for operating
  504. * on 24-bit little endian data.
  505. *
  506. * @param buf A valid pointer on a buffer.
  507. *
  508. * @return 24-bit value converted from little endian to host endian.
  509. */
  510. uint32_t net_buf_simple_pull_le24(struct net_buf_simple *buf);
  511. /**
  512. * @brief Remove and convert 24 bits from the beginning of the buffer.
  513. *
  514. * Same idea as with net_buf_simple_pull(), but a helper for operating
  515. * on 24-bit big endian data.
  516. *
  517. * @param buf A valid pointer on a buffer.
  518. *
  519. * @return 24-bit value converted from big endian to host endian.
  520. */
  521. uint32_t net_buf_simple_pull_be24(struct net_buf_simple *buf);
  522. /**
  523. * @brief Remove and convert 32 bits from the beginning of the buffer.
  524. *
  525. * Same idea as with net_buf_simple_pull(), but a helper for operating
  526. * on 32-bit little endian data.
  527. *
  528. * @param buf A valid pointer on a buffer.
  529. *
  530. * @return 32-bit value converted from little endian to host endian.
  531. */
  532. uint32_t net_buf_simple_pull_le32(struct net_buf_simple *buf);
  533. /**
  534. * @brief Remove and convert 32 bits from the beginning of the buffer.
  535. *
  536. * Same idea as with net_buf_simple_pull(), but a helper for operating
  537. * on 32-bit big endian data.
  538. *
  539. * @param buf A valid pointer on a buffer.
  540. *
  541. * @return 32-bit value converted from big endian to host endian.
  542. */
  543. uint32_t net_buf_simple_pull_be32(struct net_buf_simple *buf);
  544. /**
  545. * @brief Remove and convert 48 bits from the beginning of the buffer.
  546. *
  547. * Same idea as with net_buf_simple_pull(), but a helper for operating
  548. * on 48-bit little endian data.
  549. *
  550. * @param buf A valid pointer on a buffer.
  551. *
  552. * @return 48-bit value converted from little endian to host endian.
  553. */
  554. uint64_t net_buf_simple_pull_le48(struct net_buf_simple *buf);
  555. /**
  556. * @brief Remove and convert 48 bits from the beginning of the buffer.
  557. *
  558. * Same idea as with net_buf_simple_pull(), but a helper for operating
  559. * on 48-bit big endian data.
  560. *
  561. * @param buf A valid pointer on a buffer.
  562. *
  563. * @return 48-bit value converted from big endian to host endian.
  564. */
  565. uint64_t net_buf_simple_pull_be48(struct net_buf_simple *buf);
  566. /**
  567. * @brief Remove and convert 64 bits from the beginning of the buffer.
  568. *
  569. * Same idea as with net_buf_simple_pull(), but a helper for operating
  570. * on 64-bit little endian data.
  571. *
  572. * @param buf A valid pointer on a buffer.
  573. *
  574. * @return 64-bit value converted from little endian to host endian.
  575. */
  576. uint64_t net_buf_simple_pull_le64(struct net_buf_simple *buf);
  577. /**
  578. * @brief Remove and convert 64 bits from the beginning of the buffer.
  579. *
  580. * Same idea as with net_buf_simple_pull(), but a helper for operating
  581. * on 64-bit big endian data.
  582. *
  583. * @param buf A valid pointer on a buffer.
  584. *
  585. * @return 64-bit value converted from big endian to host endian.
  586. */
  587. uint64_t net_buf_simple_pull_be64(struct net_buf_simple *buf);
  588. /**
  589. * @brief Get the tail pointer for a buffer.
  590. *
  591. * Get a pointer to the end of the data in a buffer.
  592. *
  593. * @param buf Buffer.
  594. *
  595. * @return Tail pointer for the buffer.
  596. */
  597. static inline uint8_t *net_buf_simple_tail(struct net_buf_simple *buf)
  598. {
  599. return buf->data + buf->len;
  600. }
  601. /**
  602. * @brief Check buffer headroom.
  603. *
  604. * Check how much free space there is in the beginning of the buffer.
  605. *
  606. * buf A valid pointer on a buffer
  607. *
  608. * @return Number of bytes available in the beginning of the buffer.
  609. */
  610. size_t net_buf_simple_headroom(struct net_buf_simple *buf);
  611. /**
  612. * @brief Check buffer tailroom.
  613. *
  614. * Check how much free space there is at the end of the buffer.
  615. *
  616. * @param buf A valid pointer on a buffer
  617. *
  618. * @return Number of bytes available at the end of the buffer.
  619. */
  620. size_t net_buf_simple_tailroom(struct net_buf_simple *buf);
  621. /**
  622. * @brief Parsing state of a buffer.
  623. *
  624. * This is used for temporarily storing the parsing state of a buffer
  625. * while giving control of the parsing to a routine which we don't
  626. * control.
  627. */
  628. struct net_buf_simple_state {
  629. /** Offset of the data pointer from the beginning of the storage */
  630. uint16_t offset;
  631. /** Length of data */
  632. uint16_t len;
  633. };
  634. /**
  635. * @brief Save the parsing state of a buffer.
  636. *
  637. * Saves the parsing state of a buffer so it can be restored later.
  638. *
  639. * @param buf Buffer from which the state should be saved.
  640. * @param state Storage for the state.
  641. */
  642. static inline void net_buf_simple_save(struct net_buf_simple *buf,
  643. struct net_buf_simple_state *state)
  644. {
  645. state->offset = net_buf_simple_headroom(buf);
  646. state->len = buf->len;
  647. }
  648. /**
  649. * @brief Restore the parsing state of a buffer.
  650. *
  651. * Restores the parsing state of a buffer from a state previously stored
  652. * by net_buf_simple_save().
  653. *
  654. * @param buf Buffer to which the state should be restored.
  655. * @param state Stored state.
  656. */
  657. static inline void net_buf_simple_restore(struct net_buf_simple *buf,
  658. struct net_buf_simple_state *state)
  659. {
  660. buf->data = buf->__buf + state->offset;
  661. buf->len = state->len;
  662. }
  663. /**
  664. * @brief Initialize buffer with the given headroom.
  665. *
  666. * The buffer is not expected to contain any data when this API is called.
  667. *
  668. * @param buf Buffer to initialize.
  669. * @param reserve How much headroom to reserve.
  670. */
  671. void net_buf_simple_reserve(struct net_buf_simple *buf, size_t reserve);
  672. /**
  673. * Flag indicating that the buffer has associated fragments. Only used
  674. * internally by the buffer handling code while the buffer is inside a
  675. * FIFO, meaning this never needs to be explicitly set or unset by the
  676. * net_buf API user. As long as the buffer is outside of a FIFO, i.e.
  677. * in practice always for the user for this API, the buf->frags pointer
  678. * should be used instead.
  679. */
  680. #define NET_BUF_FRAGS BIT(0)
  681. /**
  682. * @brief Network buffer representation.
  683. *
  684. * This struct is used to represent network buffers. Such buffers are
  685. * normally defined through the NET_BUF_POOL_*_DEFINE() APIs and allocated
  686. * using the net_buf_alloc() API.
  687. */
  688. struct net_buf {
  689. union {
  690. /** Allow placing the buffer into sys_slist_t */
  691. sys_snode_t node;
  692. /** Fragments associated with this buffer. */
  693. struct net_buf *frags;
  694. };
  695. /** Reference count. */
  696. uint8_t ref;
  697. /** Bit-field of buffer flags. */
  698. uint8_t flags;
  699. /** Where the buffer should go when freed up. */
  700. struct net_buf_pool *pool;
  701. /* Union for convenience access to the net_buf_simple members, also
  702. * preserving the old API.
  703. */
  704. union {
  705. /* The ABI of this struct must match net_buf_simple */
  706. struct {
  707. /** Pointer to the start of data in the buffer. */
  708. uint8_t *data;
  709. /** Length of the data behind the data pointer. */
  710. uint16_t len;
  711. /** Amount of data that this buffer can store. */
  712. uint16_t size;
  713. /** Start of the data storage. Not to be accessed
  714. * directly (the data pointer should be used
  715. * instead).
  716. */
  717. uint8_t *__buf;
  718. };
  719. struct net_buf_simple b;
  720. };
  721. /** System metadata for this buffer. */
  722. uint8_t user_data[BLE_MESH_NET_BUF_USER_DATA_SIZE] __net_buf_align;
  723. };
  724. struct net_buf_data_cb {
  725. uint8_t *(*alloc)(struct net_buf *buf, size_t *size, int32_t timeout);
  726. uint8_t *(*ref)(struct net_buf *buf, uint8_t *data);
  727. void (*unref)(struct net_buf *buf, uint8_t *data);
  728. };
  729. struct net_buf_data_alloc {
  730. const struct net_buf_data_cb *cb;
  731. void *alloc_data;
  732. };
  733. struct net_buf_pool {
  734. /** Number of buffers in pool */
  735. const uint16_t buf_count;
  736. /** Number of uninitialized buffers */
  737. uint16_t uninit_count;
  738. #if defined(CONFIG_BLE_MESH_NET_BUF_POOL_USAGE)
  739. /** Amount of available buffers in the pool. */
  740. int16_t avail_count;
  741. /** Total size of the pool. */
  742. const uint16_t pool_size;
  743. /** Name of the pool. Used when printing pool information. */
  744. const char *name;
  745. #endif /* CONFIG_BLE_MESH_NET_BUF_POOL_USAGE */
  746. /** Optional destroy callback when buffer is freed. */
  747. void (*const destroy)(struct net_buf *buf);
  748. /** Data allocation handlers. */
  749. const struct net_buf_data_alloc *alloc;
  750. /** Helper to access the start of storage (for net_buf_pool_init) */
  751. struct net_buf *const __bufs;
  752. };
  753. #if defined(CONFIG_BLE_MESH_NET_BUF_POOL_USAGE)
  754. #define NET_BUF_POOL_INITIALIZER(_pool, _alloc, _bufs, _count, _destroy) \
  755. { \
  756. .alloc = _alloc, \
  757. .__bufs = (struct net_buf *)_bufs, \
  758. .buf_count = _count, \
  759. .uninit_count = _count, \
  760. .avail_count = _count, \
  761. .destroy = _destroy, \
  762. .name = STRINGIFY(_pool), \
  763. }
  764. #else
  765. #define NET_BUF_POOL_INITIALIZER(_pool, _alloc, _bufs, _count, _destroy) \
  766. { \
  767. .alloc = _alloc, \
  768. .__bufs = (struct net_buf *)_bufs, \
  769. .buf_count = _count, \
  770. .uninit_count = _count, \
  771. .destroy = _destroy, \
  772. }
  773. #endif /* CONFIG_BLE_MESH_NET_BUF_POOL_USAGE */
  774. struct net_buf_pool_fixed {
  775. size_t data_size;
  776. uint8_t *data_pool;
  777. };
  778. /** @cond INTERNAL_HIDDEN */
  779. extern const struct net_buf_data_cb net_buf_fixed_cb;
  780. /**
  781. * @def NET_BUF_POOL_FIXED_DEFINE
  782. * @brief Define a new pool for buffers based on fixed-size data
  783. *
  784. * Defines a net_buf_pool struct and the necessary memory storage (array of
  785. * structs) for the needed amount of buffers. After this, the buffers can be
  786. * accessed from the pool through net_buf_alloc. The pool is defined as a
  787. * static variable, so if it needs to be exported outside the current module
  788. * this needs to happen with the help of a separate pointer rather than an
  789. * extern declaration.
  790. *
  791. * The data payload of the buffers will be allocated from a byte array
  792. * of fixed sized chunks. This kind of pool does not support blocking on
  793. * the data allocation, so the timeout passed to net_buf_alloc will be
  794. * always treated as K_NO_WAIT when trying to allocate the data. This means
  795. * that allocation failures, i.e. NULL returns, must always be handled
  796. * cleanly.
  797. *
  798. * If provided with a custom destroy callback, this callback is
  799. * responsible for eventually calling net_buf_destroy() to complete the
  800. * process of returning the buffer to the pool.
  801. *
  802. * @param _name Name of the pool variable.
  803. * @param _count Number of buffers in the pool.
  804. * @param _data_size Maximum data payload per buffer.
  805. * @param _destroy Optional destroy callback when buffer is freed.
  806. */
  807. #define NET_BUF_POOL_FIXED_DEFINE(_name, _count, _data_size, _destroy) \
  808. static struct net_buf net_buf_##_name[_count]; \
  809. static uint8_t net_buf_data_##_name[_count][_data_size]; \
  810. static const struct net_buf_pool_fixed net_buf_fixed_##_name = { \
  811. .data_size = _data_size, \
  812. .data_pool = (uint8_t *)net_buf_data_##_name, \
  813. }; \
  814. static const struct net_buf_data_alloc net_buf_fixed_alloc_##_name = { \
  815. .cb = &net_buf_fixed_cb, \
  816. .alloc_data = (void *)&net_buf_fixed_##_name, \
  817. }; \
  818. struct net_buf_pool _name __net_buf_align \
  819. __in_section(_net_buf_pool, static, _name) = \
  820. NET_BUF_POOL_INITIALIZER(_name, &net_buf_fixed_alloc_##_name, \
  821. net_buf_##_name, _count, _destroy)
  822. /**
  823. * @def NET_BUF_POOL_DEFINE
  824. * @brief Define a new pool for buffers
  825. *
  826. * Defines a net_buf_pool struct and the necessary memory storage (array of
  827. * structs) for the needed amount of buffers. After this,the buffers can be
  828. * accessed from the pool through net_buf_alloc. The pool is defined as a
  829. * static variable, so if it needs to be exported outside the current module
  830. * this needs to happen with the help of a separate pointer rather than an
  831. * extern declaration.
  832. *
  833. * If provided with a custom destroy callback this callback is
  834. * responsible for eventually calling net_buf_destroy() to complete the
  835. * process of returning the buffer to the pool.
  836. *
  837. * @param _name Name of the pool variable.
  838. * @param _count Number of buffers in the pool.
  839. * @param _size Maximum data size for each buffer.
  840. * @param _ud_size Amount of user data space to reserve.
  841. * @param _destroy Optional destroy callback when buffer is freed.
  842. */
  843. #define NET_BUF_POOL_DEFINE(_name, _count, _size, _ud_size, _destroy) \
  844. NET_BUF_POOL_FIXED_DEFINE(_name, _count, _size, _destroy)
  845. /**
  846. * @brief Get a zero-based index for a buffer.
  847. *
  848. * This function will translate a buffer into a zero-based index,
  849. * based on its placement in its buffer pool. This can be useful if you
  850. * want to associate an external array of meta-data contexts with the
  851. * buffers of a pool.
  852. *
  853. * @param buf Network buffer.
  854. *
  855. * @return Zero-based index for the buffer.
  856. */
  857. int net_buf_id(struct net_buf *buf);
  858. /**
  859. * @brief Allocate a new fixed buffer from a pool.
  860. *
  861. * @param pool Which pool to allocate the buffer from.
  862. * @param timeout Affects the action taken should the pool be empty.
  863. * If K_NO_WAIT, then return immediately. If K_FOREVER, then
  864. * wait as long as necessary. Otherwise, wait up to the specified
  865. * number of milliseconds before timing out. Note that some types
  866. * of data allocators do not support blocking (such as the HEAP
  867. * type). In this case it's still possible for net_buf_alloc() to
  868. * fail (return NULL) even if it was given K_FOREVER.
  869. *
  870. * @return New buffer or NULL if out of buffers.
  871. */
  872. #if defined(CONFIG_BLE_MESH_NET_BUF_LOG)
  873. struct net_buf *net_buf_alloc_fixed_debug(struct net_buf_pool *pool, int32_t timeout,
  874. const char *func, int line);
  875. #define net_buf_alloc_fixed(_pool, _timeout) \
  876. net_buf_alloc_fixed_debug(_pool, _timeout, __func__, __LINE__)
  877. #else
  878. struct net_buf *net_buf_alloc_fixed(struct net_buf_pool *pool, int32_t timeout);
  879. #endif
  880. /**
  881. * @def net_buf_alloc
  882. *
  883. * @copydetails net_buf_alloc_fixed
  884. */
  885. #define net_buf_alloc(pool, timeout) net_buf_alloc_fixed(pool, timeout)
  886. /**
  887. * @brief Reset buffer
  888. *
  889. * Reset buffer data and flags so it can be reused for other purposes.
  890. *
  891. * @param buf Buffer to reset.
  892. */
  893. void net_buf_reset(struct net_buf *buf);
  894. /**
  895. * @def net_buf_reserve
  896. * @brief Initialize buffer with the given headroom.
  897. *
  898. * The buffer is not expected to contain any data when this API is called.
  899. *
  900. * @param buf Buffer to initialize.
  901. * @param reserve How much headroom to reserve.
  902. */
  903. #define net_buf_reserve(buf, reserve) net_buf_simple_reserve(&(buf)->b, reserve)
  904. /**
  905. * @brief Put a buffer into a list
  906. *
  907. * Put a buffer to the end of a list. If the buffer contains follow-up
  908. * fragments this function will take care of inserting them as well
  909. * into the list.
  910. *
  911. * @param list Which list to append the buffer to.
  912. * @param buf Buffer.
  913. */
  914. void net_buf_slist_put(sys_slist_t *list, struct net_buf *buf);
  915. /**
  916. * @brief Get a buffer from a list.
  917. *
  918. * Get buffer from a list. If the buffer had any fragments, these will
  919. * automatically be recovered from the list as well and be placed to
  920. * the buffer's fragment list.
  921. *
  922. * @param list Which list to take the buffer from.
  923. *
  924. * @return New buffer or NULL if the FIFO is empty.
  925. */
  926. struct net_buf *net_buf_slist_get(sys_slist_t *list);
  927. /**
  928. * @brief Decrements the reference count of a buffer.
  929. *
  930. * Decrements the reference count of a buffer and puts it back into the
  931. * pool if the count reaches zero.
  932. *
  933. * @param buf A valid pointer on a buffer
  934. */
  935. #if defined(CONFIG_BLE_MESH_NET_BUF_LOG)
  936. void net_buf_unref_debug(struct net_buf *buf, const char *func, int line);
  937. #define net_buf_unref(_buf) \
  938. net_buf_unref_debug(_buf, __func__, __LINE__)
  939. #else
  940. void net_buf_unref(struct net_buf *buf);
  941. #endif
  942. /**
  943. * @brief Increment the reference count of a buffer.
  944. *
  945. * @param buf A valid pointer on a buffer
  946. *
  947. * @return the buffer newly referenced
  948. */
  949. struct net_buf *net_buf_ref(struct net_buf *buf);
  950. /**
  951. * @brief Get a pointer to the user data of a buffer.
  952. *
  953. * @param buf A valid pointer on a buffer
  954. *
  955. * @return Pointer to the user data of the buffer.
  956. */
  957. static inline void *net_buf_user_data(struct net_buf *buf)
  958. {
  959. return (void *)buf->user_data;
  960. }
  961. /**
  962. * @def net_buf_add
  963. * @brief Prepare data to be added at the end of the buffer
  964. *
  965. * Increments the data length of a buffer to account for more data
  966. * at the end.
  967. *
  968. * @param buf Buffer to update.
  969. * @param len Number of bytes to increment the length with.
  970. *
  971. * @return The original tail of the buffer.
  972. */
  973. #define net_buf_add(buf, len) net_buf_simple_add(&(buf)->b, len)
  974. /**
  975. * @def net_buf_add_mem
  976. * @brief Copy bytes from memory to the end of the buffer
  977. *
  978. * Copies the given number of bytes to the end of the buffer. Increments the
  979. * data length of the buffer to account for more data at the end.
  980. *
  981. * @param buf Buffer to update.
  982. * @param mem Location of data to be added.
  983. * @param len Length of data to be added
  984. *
  985. * @return The original tail of the buffer.
  986. */
  987. #define net_buf_add_mem(buf, mem, len) net_buf_simple_add_mem(&(buf)->b, mem, len)
  988. /**
  989. * @def net_buf_add_u8
  990. * @brief Add (8-bit) byte at the end of the buffer
  991. *
  992. * Adds a byte at the end of the buffer. Increments the data length of
  993. * the buffer to account for more data at the end.
  994. *
  995. * @param buf Buffer to update.
  996. * @param val byte value to be added.
  997. *
  998. * @return Pointer to the value added
  999. */
  1000. #define net_buf_add_u8(buf, val) net_buf_simple_add_u8(&(buf)->b, val)
  1001. /**
  1002. * @def net_buf_add_le16
  1003. * @brief Add 16-bit value at the end of the buffer
  1004. *
  1005. * Adds 16-bit value in little endian format at the end of buffer.
  1006. * Increments the data length of a buffer to account for more data
  1007. * at the end.
  1008. *
  1009. * @param buf Buffer to update.
  1010. * @param val 16-bit value to be added.
  1011. */
  1012. #define net_buf_add_le16(buf, val) net_buf_simple_add_le16(&(buf)->b, val)
  1013. /**
  1014. * @def net_buf_add_be16
  1015. * @brief Add 16-bit value at the end of the buffer
  1016. *
  1017. * Adds 16-bit value in big endian format at the end of buffer.
  1018. * Increments the data length of a buffer to account for more data
  1019. * at the end.
  1020. *
  1021. * @param buf Buffer to update.
  1022. * @param val 16-bit value to be added.
  1023. */
  1024. #define net_buf_add_be16(buf, val) net_buf_simple_add_be16(&(buf)->b, val)
  1025. /**
  1026. * @def net_buf_add_le24
  1027. * @brief Add 24-bit value at the end of the buffer
  1028. *
  1029. * Adds 24-bit value in little endian format at the end of buffer.
  1030. * Increments the data length of a buffer to account for more data
  1031. * at the end.
  1032. *
  1033. * @param buf Buffer to update.
  1034. * @param val 24-bit value to be added.
  1035. */
  1036. #define net_buf_add_le24(buf, val) net_buf_simple_add_le24(&(buf)->b, val)
  1037. /**
  1038. * @def net_buf_add_be24
  1039. * @brief Add 24-bit value at the end of the buffer
  1040. *
  1041. * Adds 24-bit value in big endian format at the end of buffer.
  1042. * Increments the data length of a buffer to account for more data
  1043. * at the end.
  1044. *
  1045. * @param buf Buffer to update.
  1046. * @param val 24-bit value to be added.
  1047. */
  1048. #define net_buf_add_be24(buf, val) net_buf_simple_add_be24(&(buf)->b, val)
  1049. /**
  1050. * @def net_buf_add_le32
  1051. * @brief Add 32-bit value at the end of the buffer
  1052. *
  1053. * Adds 32-bit value in little endian format at the end of buffer.
  1054. * Increments the data length of a buffer to account for more data
  1055. * at the end.
  1056. *
  1057. * @param buf Buffer to update.
  1058. * @param val 32-bit value to be added.
  1059. */
  1060. #define net_buf_add_le32(buf, val) net_buf_simple_add_le32(&(buf)->b, val)
  1061. /**
  1062. * @def net_buf_add_be32
  1063. * @brief Add 32-bit value at the end of the buffer
  1064. *
  1065. * Adds 32-bit value in big endian format at the end of buffer.
  1066. * Increments the data length of a buffer to account for more data
  1067. * at the end.
  1068. *
  1069. * @param buf Buffer to update.
  1070. * @param val 32-bit value to be added.
  1071. */
  1072. #define net_buf_add_be32(buf, val) net_buf_simple_add_be32(&(buf)->b, val)
  1073. /**
  1074. * @def net_buf_add_le48
  1075. * @brief Add 48-bit value at the end of the buffer
  1076. *
  1077. * Adds 48-bit value in little endian format at the end of buffer.
  1078. * Increments the data length of a buffer to account for more data
  1079. * at the end.
  1080. *
  1081. * @param buf Buffer to update.
  1082. * @param val 48-bit value to be added.
  1083. */
  1084. #define net_buf_add_le48(buf, val) net_buf_simple_add_le48(&(buf)->b, val)
  1085. /**
  1086. * @def net_buf_add_be48
  1087. * @brief Add 48-bit value at the end of the buffer
  1088. *
  1089. * Adds 48-bit value in big endian format at the end of buffer.
  1090. * Increments the data length of a buffer to account for more data
  1091. * at the end.
  1092. *
  1093. * @param buf Buffer to update.
  1094. * @param val 48-bit value to be added.
  1095. */
  1096. #define net_buf_add_be48(buf, val) net_buf_simple_add_be48(&(buf)->b, val)
  1097. /**
  1098. * @def net_buf_add_le64
  1099. * @brief Add 64-bit value at the end of the buffer
  1100. *
  1101. * Adds 64-bit value in little endian format at the end of buffer.
  1102. * Increments the data length of a buffer to account for more data
  1103. * at the end.
  1104. *
  1105. * @param buf Buffer to update.
  1106. * @param val 64-bit value to be added.
  1107. */
  1108. #define net_buf_add_le64(buf, val) net_buf_simple_add_le64(&(buf)->b, val)
  1109. /**
  1110. * @def net_buf_add_be64
  1111. * @brief Add 64-bit value at the end of the buffer
  1112. *
  1113. * Adds 64-bit value in big endian format at the end of buffer.
  1114. * Increments the data length of a buffer to account for more data
  1115. * at the end.
  1116. *
  1117. * @param buf Buffer to update.
  1118. * @param val 64-bit value to be added.
  1119. */
  1120. #define net_buf_add_be64(buf, val) net_buf_simple_add_be64(&(buf)->b, val)
  1121. /**
  1122. * @def net_buf_push
  1123. * @brief Push data to the beginning of the buffer.
  1124. *
  1125. * Modifies the data pointer and buffer length to account for more data
  1126. * in the beginning of the buffer.
  1127. *
  1128. * @param buf Buffer to update.
  1129. * @param len Number of bytes to add to the beginning.
  1130. *
  1131. * @return The new beginning of the buffer data.
  1132. */
  1133. #define net_buf_push(buf, len) net_buf_simple_push(&(buf)->b, len)
  1134. /**
  1135. * @def net_buf_push_le16
  1136. * @brief Push 16-bit value to the beginning of the buffer
  1137. *
  1138. * Adds 16-bit value in little endian format to the beginning of the
  1139. * buffer.
  1140. *
  1141. * @param buf Buffer to update.
  1142. * @param val 16-bit value to be pushed to the buffer.
  1143. */
  1144. #define net_buf_push_le16(buf, val) net_buf_simple_push_le16(&(buf)->b, val)
  1145. /**
  1146. * @def net_buf_push_be16
  1147. * @brief Push 16-bit value to the beginning of the buffer
  1148. *
  1149. * Adds 16-bit value in little endian format to the beginning of the
  1150. * buffer.
  1151. *
  1152. * @param buf Buffer to update.
  1153. * @param val 16-bit value to be pushed to the buffer.
  1154. */
  1155. #define net_buf_push_be16(buf, val) net_buf_simple_push_be16(&(buf)->b, val)
  1156. /**
  1157. * @def net_buf_push_u8
  1158. * @brief Push 8-bit value to the beginning of the buffer
  1159. *
  1160. * Adds 8-bit value the beginning of the buffer.
  1161. *
  1162. * @param buf Buffer to update.
  1163. * @param val 8-bit value to be pushed to the buffer.
  1164. */
  1165. #define net_buf_push_u8(buf, val) net_buf_simple_push_u8(&(buf)->b, val)
  1166. /**
  1167. * @def net_buf_push_le24
  1168. * @brief Push 24-bit value to the beginning of the buffer
  1169. *
  1170. * Adds 24-bit value in little endian format to the beginning of the
  1171. * buffer.
  1172. *
  1173. * @param buf Buffer to update.
  1174. * @param val 24-bit value to be pushed to the buffer.
  1175. */
  1176. #define net_buf_push_le24(buf, val) net_buf_simple_push_le24(&(buf)->b, val)
  1177. /**
  1178. * @def net_buf_push_be24
  1179. * @brief Push 24-bit value to the beginning of the buffer
  1180. *
  1181. * Adds 24-bit value in little endian format to the beginning of the
  1182. * buffer.
  1183. *
  1184. * @param buf Buffer to update.
  1185. * @param val 24-bit value to be pushed to the buffer.
  1186. */
  1187. #define net_buf_push_be24(buf, val) net_buf_simple_push_be24(&(buf)->b, val)
  1188. /**
  1189. * @def net_buf_push_le32
  1190. * @brief Push 32-bit value to the beginning of the buffer
  1191. *
  1192. * Adds 32-bit value in little endian format to the beginning of the
  1193. * buffer.
  1194. *
  1195. * @param buf Buffer to update.
  1196. * @param val 32-bit value to be pushed to the buffer.
  1197. */
  1198. #define net_buf_push_le32(buf, val) net_buf_simple_push_le32(&(buf)->b, val)
  1199. /**
  1200. * @def net_buf_push_be32
  1201. * @brief Push 32-bit value to the beginning of the buffer
  1202. *
  1203. * Adds 32-bit value in little endian format to the beginning of the
  1204. * buffer.
  1205. *
  1206. * @param buf Buffer to update.
  1207. * @param val 32-bit value to be pushed to the buffer.
  1208. */
  1209. #define net_buf_push_be32(buf, val) net_buf_simple_push_be32(&(buf)->b, val)
  1210. /**
  1211. * @def net_buf_push_le48
  1212. * @brief Push 48-bit value to the beginning of the buffer
  1213. *
  1214. * Adds 48-bit value in little endian format to the beginning of the
  1215. * buffer.
  1216. *
  1217. * @param buf Buffer to update.
  1218. * @param val 48-bit value to be pushed to the buffer.
  1219. */
  1220. #define net_buf_push_le48(buf, val) net_buf_simple_push_le48(&(buf)->b, val)
  1221. /**
  1222. * @def net_buf_push_be48
  1223. * @brief Push 48-bit value to the beginning of the buffer
  1224. *
  1225. * Adds 48-bit value in little endian format to the beginning of the
  1226. * buffer.
  1227. *
  1228. * @param buf Buffer to update.
  1229. * @param val 48-bit value to be pushed to the buffer.
  1230. */
  1231. #define net_buf_push_be48(buf, val) net_buf_simple_push_be48(&(buf)->b, val)
  1232. /**
  1233. * @def net_buf_push_le64
  1234. * @brief Push 64-bit value to the beginning of the buffer
  1235. *
  1236. * Adds 64-bit value in little endian format to the beginning of the
  1237. * buffer.
  1238. *
  1239. * @param buf Buffer to update.
  1240. * @param val 64-bit value to be pushed to the buffer.
  1241. */
  1242. #define net_buf_push_le64(buf, val) net_buf_simple_push_le64(&(buf)->b, val)
  1243. /**
  1244. * @def net_buf_push_be64
  1245. * @brief Push 64-bit value to the beginning of the buffer
  1246. *
  1247. * Adds 64-bit value in little endian format to the beginning of the
  1248. * buffer.
  1249. *
  1250. * @param buf Buffer to update.
  1251. * @param val 64-bit value to be pushed to the buffer.
  1252. */
  1253. #define net_buf_push_be64(buf, val) net_buf_simple_push_be64(&(buf)->b, val)
  1254. /**
  1255. * @def net_buf_pull
  1256. * @brief Remove data from the beginning of the buffer.
  1257. *
  1258. * Removes data from the beginning of the buffer by modifying the data
  1259. * pointer and buffer length.
  1260. *
  1261. * @param buf Buffer to update.
  1262. * @param len Number of bytes to remove.
  1263. *
  1264. * @return New beginning of the buffer data.
  1265. */
  1266. #define net_buf_pull(buf, len) net_buf_simple_pull(&(buf)->b, len)
  1267. /**
  1268. * @def net_buf_pull_mem
  1269. * @brief Remove data from the beginning of the buffer.
  1270. *
  1271. * Removes data from the beginning of the buffer by modifying the data
  1272. * pointer and buffer length.
  1273. *
  1274. * @param buf Buffer to update.
  1275. * @param len Number of bytes to remove.
  1276. *
  1277. * @return Pointer to the old beginning of the buffer data.
  1278. */
  1279. #define net_buf_pull_mem(buf, len) net_buf_simple_pull_mem(&(buf)->b, len)
  1280. /**
  1281. * @def net_buf_pull_u8
  1282. * @brief Remove a 8-bit value from the beginning of the buffer
  1283. *
  1284. * Same idea as with net_buf_pull(), but a helper for operating on
  1285. * 8-bit values.
  1286. *
  1287. * @param buf A valid pointer on a buffer.
  1288. *
  1289. * @return The 8-bit removed value
  1290. */
  1291. #define net_buf_pull_u8(buf) net_buf_simple_pull_u8(&(buf)->b)
  1292. /**
  1293. * @def net_buf_pull_le16
  1294. * @brief Remove and convert 16 bits from the beginning of the buffer.
  1295. *
  1296. * Same idea as with net_buf_pull(), but a helper for operating on
  1297. * 16-bit little endian data.
  1298. *
  1299. * @param buf A valid pointer on a buffer.
  1300. *
  1301. * @return 16-bit value converted from little endian to host endian.
  1302. */
  1303. #define net_buf_pull_le16(buf) net_buf_simple_pull_le16(&(buf)->b)
  1304. /**
  1305. * @def net_buf_pull_be16
  1306. * @brief Remove and convert 16 bits from the beginning of the buffer.
  1307. *
  1308. * Same idea as with net_buf_pull(), but a helper for operating on
  1309. * 16-bit big endian data.
  1310. *
  1311. * @param buf A valid pointer on a buffer.
  1312. *
  1313. * @return 16-bit value converted from big endian to host endian.
  1314. */
  1315. #define net_buf_pull_be16(buf) net_buf_simple_pull_be16(&(buf)->b)
  1316. /**
  1317. * @def net_buf_pull_le24
  1318. * @brief Remove and convert 24 bits from the beginning of the buffer.
  1319. *
  1320. * Same idea as with net_buf_pull(), but a helper for operating on
  1321. * 24-bit little endian data.
  1322. *
  1323. * @param buf A valid pointer on a buffer.
  1324. *
  1325. * @return 24-bit value converted from little endian to host endian.
  1326. */
  1327. #define net_buf_pull_le24(buf) net_buf_simple_pull_le24(&(buf)->b)
  1328. /**
  1329. * @def net_buf_pull_be24
  1330. * @brief Remove and convert 24 bits from the beginning of the buffer.
  1331. *
  1332. * Same idea as with net_buf_pull(), but a helper for operating on
  1333. * 24-bit big endian data.
  1334. *
  1335. * @param buf A valid pointer on a buffer.
  1336. *
  1337. * @return 24-bit value converted from big endian to host endian.
  1338. */
  1339. #define net_buf_pull_be24(buf) net_buf_simple_pull_be24(&(buf)->b)
  1340. /**
  1341. * @def net_buf_pull_le32
  1342. * @brief Remove and convert 32 bits from the beginning of the buffer.
  1343. *
  1344. * Same idea as with net_buf_pull(), but a helper for operating on
  1345. * 32-bit little endian data.
  1346. *
  1347. * @param buf A valid pointer on a buffer.
  1348. *
  1349. * @return 32-bit value converted from little endian to host endian.
  1350. */
  1351. #define net_buf_pull_le32(buf) net_buf_simple_pull_le32(&(buf)->b)
  1352. /**
  1353. * @def net_buf_pull_be32
  1354. * @brief Remove and convert 32 bits from the beginning of the buffer.
  1355. *
  1356. * Same idea as with net_buf_pull(), but a helper for operating on
  1357. * 32-bit big endian data.
  1358. *
  1359. * @param buf A valid pointer on a buffer
  1360. *
  1361. * @return 32-bit value converted from big endian to host endian.
  1362. */
  1363. #define net_buf_pull_be32(buf) net_buf_simple_pull_be32(&(buf)->b)
  1364. /**
  1365. * @def net_buf_pull_le48
  1366. * @brief Remove and convert 48 bits from the beginning of the buffer.
  1367. *
  1368. * Same idea as with net_buf_pull(), but a helper for operating on
  1369. * 48-bit little endian data.
  1370. *
  1371. * @param buf A valid pointer on a buffer.
  1372. *
  1373. * @return 48-bit value converted from little endian to host endian.
  1374. */
  1375. #define net_buf_pull_le48(buf) net_buf_simple_pull_le48(&(buf)->b)
  1376. /**
  1377. * @def net_buf_pull_be48
  1378. * @brief Remove and convert 48 bits from the beginning of the buffer.
  1379. *
  1380. * Same idea as with net_buf_pull(), but a helper for operating on
  1381. * 48-bit big endian data.
  1382. *
  1383. * @param buf A valid pointer on a buffer
  1384. *
  1385. * @return 48-bit value converted from big endian to host endian.
  1386. */
  1387. #define net_buf_pull_be48(buf) net_buf_simple_pull_be48(&(buf)->b)
  1388. /**
  1389. * @def net_buf_pull_le64
  1390. * @brief Remove and convert 64 bits from the beginning of the buffer.
  1391. *
  1392. * Same idea as with net_buf_pull(), but a helper for operating on
  1393. * 64-bit little endian data.
  1394. *
  1395. * @param buf A valid pointer on a buffer.
  1396. *
  1397. * @return 64-bit value converted from little endian to host endian.
  1398. */
  1399. #define net_buf_pull_le64(buf) net_buf_simple_pull_le64(&(buf)->b)
  1400. /**
  1401. * @def net_buf_pull_be64
  1402. * @brief Remove and convert 64 bits from the beginning of the buffer.
  1403. *
  1404. * Same idea as with net_buf_pull(), but a helper for operating on
  1405. * 64-bit big endian data.
  1406. *
  1407. * @param buf A valid pointer on a buffer
  1408. *
  1409. * @return 64-bit value converted from big endian to host endian.
  1410. */
  1411. #define net_buf_pull_be64(buf) net_buf_simple_pull_be64(&(buf)->b)
  1412. /**
  1413. * @def net_buf_tailroom
  1414. * @brief Check buffer tailroom.
  1415. *
  1416. * Check how much free space there is at the end of the buffer.
  1417. *
  1418. * @param buf A valid pointer on a buffer
  1419. *
  1420. * @return Number of bytes available at the end of the buffer.
  1421. */
  1422. #define net_buf_tailroom(buf) net_buf_simple_tailroom(&(buf)->b)
  1423. /**
  1424. * @def net_buf_headroom
  1425. * @brief Check buffer headroom.
  1426. *
  1427. * Check how much free space there is in the beginning of the buffer.
  1428. *
  1429. * buf A valid pointer on a buffer
  1430. *
  1431. * @return Number of bytes available in the beginning of the buffer.
  1432. */
  1433. #define net_buf_headroom(buf) net_buf_simple_headroom(&(buf)->b)
  1434. /**
  1435. * @def net_buf_tail
  1436. * @brief Get the tail pointer for a buffer.
  1437. *
  1438. * Get a pointer to the end of the data in a buffer.
  1439. *
  1440. * @param buf Buffer.
  1441. *
  1442. * @return Tail pointer for the buffer.
  1443. */
  1444. #define net_buf_tail(buf) net_buf_simple_tail(&(buf)->b)
  1445. /**
  1446. * @brief Find the last fragment in the fragment list.
  1447. *
  1448. * @return Pointer to last fragment in the list.
  1449. */
  1450. struct net_buf *net_buf_frag_last(struct net_buf *frags);
  1451. /**
  1452. * @brief Insert a new fragment to a chain of bufs.
  1453. *
  1454. * Insert a new fragment into the buffer fragments list after the parent.
  1455. *
  1456. * Note: This function takes ownership of the fragment reference so the
  1457. * caller is not required to unref.
  1458. *
  1459. * @param parent Parent buffer/fragment.
  1460. * @param frag Fragment to insert.
  1461. */
  1462. void net_buf_frag_insert(struct net_buf *parent, struct net_buf *frag);
  1463. /**
  1464. * @brief Add a new fragment to the end of a chain of bufs.
  1465. *
  1466. * Append a new fragment into the buffer fragments list.
  1467. *
  1468. * Note: This function takes ownership of the fragment reference so the
  1469. * caller is not required to unref.
  1470. *
  1471. * @param head Head of the fragment chain.
  1472. * @param frag Fragment to add.
  1473. *
  1474. * @return New head of the fragment chain. Either head (if head
  1475. * was non-NULL) or frag (if head was NULL).
  1476. */
  1477. struct net_buf *net_buf_frag_add(struct net_buf *head, struct net_buf *frag);
  1478. /**
  1479. * @brief Delete existing fragment from a chain of bufs.
  1480. *
  1481. * @param parent Parent buffer/fragment, or NULL if there is no parent.
  1482. * @param frag Fragment to delete.
  1483. *
  1484. * @return Pointer to the buffer following the fragment, or NULL if it
  1485. * had no further fragments.
  1486. */
  1487. #if defined(CONFIG_BLE_MESH_NET_BUF_LOG)
  1488. struct net_buf *net_buf_frag_del_debug(struct net_buf *parent,
  1489. struct net_buf *frag,
  1490. const char *func, int line);
  1491. #define net_buf_frag_del(_parent, _frag) \
  1492. net_buf_frag_del_debug(_parent, _frag, __func__, __LINE__)
  1493. #else
  1494. struct net_buf *net_buf_frag_del(struct net_buf *parent, struct net_buf *frag);
  1495. #endif
  1496. /**
  1497. * @brief Copy bytes from net_buf chain starting at offset to linear buffer
  1498. *
  1499. * Copy (extract) @a len bytes from @a src net_buf chain, starting from @a
  1500. * offset in it, to a linear buffer @a dst. Return number of bytes actually
  1501. * copied, which may be less than requested, if net_buf chain doesn't have
  1502. * enough data, or destination buffer is too small.
  1503. *
  1504. * @param dst Destination buffer
  1505. * @param dst_len Destination buffer length
  1506. * @param src Source net_buf chain
  1507. * @param offset Starting offset to copy from
  1508. * @param len Number of bytes to copy
  1509. * @return number of bytes actually copied
  1510. */
  1511. size_t net_buf_linearize(void *dst, size_t dst_len,
  1512. struct net_buf *src, size_t offset, size_t len);
  1513. /**
  1514. * @typedef net_buf_allocator_cb
  1515. * @brief Network buffer allocator callback.
  1516. *
  1517. * @details The allocator callback is called when net_buf_append_bytes
  1518. * needs to allocate a new net_buf.
  1519. *
  1520. * @param timeout Affects the action taken should the net buf pool be empty.
  1521. * If K_NO_WAIT, then return immediately. If K_FOREVER, then
  1522. * wait as long as necessary. Otherwise, wait up to the specified
  1523. * number of milliseconds before timing out.
  1524. * @param user_data The user data given in net_buf_append_bytes call.
  1525. * @return pointer to allocated net_buf or NULL on error.
  1526. */
  1527. typedef struct net_buf *(*net_buf_allocator_cb)(int32_t timeout, void *user_data);
  1528. /**
  1529. * @brief Append data to a list of net_buf
  1530. *
  1531. * @details Append data to a net_buf. If there is not enough space in the
  1532. * net_buf then more net_buf will be added, unless there are no free net_buf
  1533. * and timeout occurs.
  1534. *
  1535. * @param buf Network buffer.
  1536. * @param len Total length of input data
  1537. * @param value Data to be added
  1538. * @param timeout Timeout is passed to the net_buf allocator callback.
  1539. * @param allocate_cb When a new net_buf is required, use this callback.
  1540. * @param user_data A user data pointer to be supplied to the allocate_cb.
  1541. * This pointer is can be anything from a mem_pool or a net_pkt, the
  1542. * logic is left up to the allocate_cb function.
  1543. *
  1544. * @return Length of data actually added. This may be less than input
  1545. * length if other timeout than K_FOREVER was used, and there
  1546. * were no free fragments in a pool to accommodate all data.
  1547. */
  1548. size_t net_buf_append_bytes(struct net_buf *buf, size_t len,
  1549. const void *value, int32_t timeout,
  1550. net_buf_allocator_cb allocate_cb, void *user_data);
  1551. /**
  1552. * @brief Skip N number of bytes in a net_buf
  1553. *
  1554. * @details Skip N number of bytes starting from fragment's offset. If the total
  1555. * length of data is placed in multiple fragments, this function will skip from
  1556. * all fragments until it reaches N number of bytes. Any fully skipped buffers
  1557. * are removed from the net_buf list.
  1558. *
  1559. * @param buf Network buffer.
  1560. * @param len Total length of data to be skipped.
  1561. *
  1562. * @return Pointer to the fragment or
  1563. * NULL and pos is 0 after successful skip,
  1564. * NULL and pos is 0xffff otherwise.
  1565. */
  1566. static inline struct net_buf *net_buf_skip(struct net_buf *buf, size_t len)
  1567. {
  1568. while (buf && len--) {
  1569. net_buf_pull_u8(buf);
  1570. if (!buf->len) {
  1571. buf = net_buf_frag_del(NULL, buf);
  1572. }
  1573. }
  1574. return buf;
  1575. }
  1576. /**
  1577. * @brief Calculate amount of bytes stored in fragments.
  1578. *
  1579. * Calculates the total amount of data stored in the given buffer and the
  1580. * fragments linked to it.
  1581. *
  1582. * @param buf Buffer to start off with.
  1583. *
  1584. * @return Number of bytes in the buffer and its fragments.
  1585. */
  1586. static inline size_t net_buf_frags_len(struct net_buf *buf)
  1587. {
  1588. size_t bytes = 0;
  1589. while (buf) {
  1590. bytes += buf->len;
  1591. buf = buf->frags;
  1592. }
  1593. return bytes;
  1594. }
  1595. /**
  1596. * @}
  1597. */
  1598. #ifdef __cplusplus
  1599. }
  1600. #endif
  1601. #endif /* _BLE_MESH_BUF_H_ */