buf.h 74 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516
  1. /** @file
  2. * @brief Buffer management.
  3. */
  4. /*
  5. * Copyright (c) 2015 Intel Corporation
  6. *
  7. * SPDX-License-Identifier: Apache-2.0
  8. */
  9. #ifndef ZEPHYR_INCLUDE_NET_BUF_H_
  10. #define ZEPHYR_INCLUDE_NET_BUF_H_
  11. #include <stddef.h>
  12. #include <zephyr/types.h>
  13. #include <sys/util.h>
  14. #include <zephyr.h>
  15. #ifndef CONFIG_NET_BUF_USER_DATA_SIZE
  16. #define CONFIG_NET_BUF_USER_DATA_SIZE 0
  17. #endif
  18. #ifdef __cplusplus
  19. extern "C" {
  20. #endif
  21. /* Actions add start */
  22. #define NET_BUF_TIMESTAMP_CHECK_TIME (10) /* 10ms */
  23. /* Actions add end */
  24. /**
  25. * @brief Network buffer library
  26. * @defgroup net_buf Network Buffer Library
  27. * @ingroup networking
  28. * @{
  29. */
  30. /* Alignment needed for various parts of the buffer definition */
  31. #define __net_buf_align __aligned(sizeof(void *))
  32. /**
  33. * @def NET_BUF_SIMPLE_DEFINE
  34. * @brief Define a net_buf_simple stack variable.
  35. *
  36. * This is a helper macro which is used to define a net_buf_simple object
  37. * on the stack.
  38. *
  39. * @param _name Name of the net_buf_simple object.
  40. * @param _size Maximum data storage for the buffer.
  41. */
  42. #define NET_BUF_SIMPLE_DEFINE(_name, _size) \
  43. uint8_t net_buf_data_##_name[_size]; \
  44. struct net_buf_simple _name = { \
  45. .data = net_buf_data_##_name, \
  46. .len = 0, \
  47. .size = _size, \
  48. .__buf = net_buf_data_##_name, \
  49. }
  50. /**
  51. * @def NET_BUF_SIMPLE_DEFINE_STATIC
  52. * @brief Define a static net_buf_simple variable.
  53. *
  54. * This is a helper macro which is used to define a static net_buf_simple
  55. * object.
  56. *
  57. * @param _name Name of the net_buf_simple object.
  58. * @param _size Maximum data storage for the buffer.
  59. */
  60. #define NET_BUF_SIMPLE_DEFINE_STATIC(_name, _size) \
  61. static __noinit uint8_t net_buf_data_##_name[_size]; \
  62. static struct net_buf_simple _name = { \
  63. .data = net_buf_data_##_name, \
  64. .len = 0, \
  65. .size = _size, \
  66. .__buf = net_buf_data_##_name, \
  67. }
  68. /**
  69. * @brief Simple network buffer representation.
  70. *
  71. * This is a simpler variant of the net_buf object (in fact net_buf uses
  72. * net_buf_simple internally). It doesn't provide any kind of reference
  73. * counting, user data, dynamic allocation, or in general the ability to
  74. * pass through kernel objects such as FIFOs.
  75. *
  76. * The main use of this is for scenarios where the meta-data of the normal
  77. * net_buf isn't needed and causes too much overhead. This could be e.g.
  78. * when the buffer only needs to be allocated on the stack or when the
  79. * access to and lifetime of the buffer is well controlled and constrained.
  80. */
  81. struct net_buf_simple {
  82. /** Pointer to the start of data in the buffer. */
  83. uint8_t *data;
  84. /**
  85. * Length of the data behind the data pointer.
  86. *
  87. * To determine the max length, use net_buf_simple_max_len(), not #size!
  88. */
  89. uint16_t len;
  90. /** Amount of data that net_buf_simple#__buf 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]; \
  117. }) { \
  118. .buf.size = _size, \
  119. }))
  120. /**
  121. * @brief Initialize a net_buf_simple object.
  122. *
  123. * This needs to be called after creating a net_buf_simple object using
  124. * the NET_BUF_SIMPLE macro.
  125. *
  126. * @param buf Buffer to initialize.
  127. * @param reserve_head Headroom to reserve.
  128. */
  129. static inline void net_buf_simple_init(struct net_buf_simple *buf,
  130. size_t reserve_head)
  131. {
  132. if (!buf->__buf) {
  133. buf->__buf = (uint8_t *)buf + sizeof(*buf);
  134. }
  135. buf->data = buf->__buf + reserve_head;
  136. buf->len = 0U;
  137. }
  138. /**
  139. * @brief Initialize a net_buf_simple object with data.
  140. *
  141. * Initialized buffer object with external data.
  142. *
  143. * @param buf Buffer to initialize.
  144. * @param data External data pointer
  145. * @param size Amount of data the pointed data buffer if able to fit.
  146. */
  147. void net_buf_simple_init_with_data(struct net_buf_simple *buf,
  148. void *data, size_t size);
  149. /**
  150. * @brief Reset buffer
  151. *
  152. * Reset buffer data so it can be reused for other purposes.
  153. *
  154. * @param buf Buffer to reset.
  155. */
  156. static inline void net_buf_simple_reset(struct net_buf_simple *buf)
  157. {
  158. buf->len = 0U;
  159. buf->data = buf->__buf;
  160. }
  161. /**
  162. * Clone buffer state, using the same data buffer.
  163. *
  164. * Initializes a buffer to point to the same data as an existing buffer.
  165. * Allows operations on the same data without altering the length and
  166. * offset of the original.
  167. *
  168. * @param original Buffer to clone.
  169. * @param clone The new clone.
  170. */
  171. void net_buf_simple_clone(const struct net_buf_simple *original,
  172. struct net_buf_simple *clone);
  173. /**
  174. * @brief Prepare data to be added at the end of the buffer
  175. *
  176. * Increments the data length of a buffer to account for more data
  177. * at the end.
  178. *
  179. * @param buf Buffer to update.
  180. * @param len Number of bytes to increment the length with.
  181. *
  182. * @return The original tail of the buffer.
  183. */
  184. void *net_buf_simple_add(struct net_buf_simple *buf, size_t len);
  185. /**
  186. * @brief Copy given number of bytes from memory to the end of the buffer
  187. *
  188. * Increments the data length of the buffer to account for more data at the
  189. * end.
  190. *
  191. * @param buf Buffer to update.
  192. * @param mem Location of data to be added.
  193. * @param len Length of data to be added
  194. *
  195. * @return The original tail of the buffer.
  196. */
  197. void *net_buf_simple_add_mem(struct net_buf_simple *buf, const void *mem,
  198. size_t len);
  199. /**
  200. * @brief Add (8-bit) byte at the end of the buffer
  201. *
  202. * Increments the data length of the buffer to account for more data at the
  203. * end.
  204. *
  205. * @param buf Buffer to update.
  206. * @param val byte value to be added.
  207. *
  208. * @return Pointer to the value added
  209. */
  210. uint8_t *net_buf_simple_add_u8(struct net_buf_simple *buf, uint8_t val);
  211. /**
  212. * @brief Add 16-bit value at the end of the buffer
  213. *
  214. * Adds 16-bit value in little endian format at the end of buffer.
  215. * Increments the data length of a buffer to account for more data
  216. * at the end.
  217. *
  218. * @param buf Buffer to update.
  219. * @param val 16-bit value to be added.
  220. */
  221. void net_buf_simple_add_le16(struct net_buf_simple *buf, uint16_t val);
  222. /**
  223. * @brief Add 16-bit value at the end of the buffer
  224. *
  225. * Adds 16-bit value in big endian format at the end of buffer.
  226. * Increments the data length of a buffer to account for more data
  227. * at the end.
  228. *
  229. * @param buf Buffer to update.
  230. * @param val 16-bit value to be added.
  231. */
  232. void net_buf_simple_add_be16(struct net_buf_simple *buf, uint16_t val);
  233. /**
  234. * @brief Add 24-bit value at the end of the buffer
  235. *
  236. * Adds 24-bit value in little endian format at the end of buffer.
  237. * Increments the data length of a buffer to account for more data
  238. * at the end.
  239. *
  240. * @param buf Buffer to update.
  241. * @param val 24-bit value to be added.
  242. */
  243. void net_buf_simple_add_le24(struct net_buf_simple *buf, uint32_t val);
  244. /**
  245. * @brief Add 24-bit value at the end of the buffer
  246. *
  247. * Adds 24-bit value in big endian format at the end of buffer.
  248. * Increments the data length of a buffer to account for more data
  249. * at the end.
  250. *
  251. * @param buf Buffer to update.
  252. * @param val 24-bit value to be added.
  253. */
  254. void net_buf_simple_add_be24(struct net_buf_simple *buf, uint32_t val);
  255. /**
  256. * @brief Add 32-bit value at the end of the buffer
  257. *
  258. * Adds 32-bit value in little endian format at the end of buffer.
  259. * Increments the data length of a buffer to account for more data
  260. * at the end.
  261. *
  262. * @param buf Buffer to update.
  263. * @param val 32-bit value to be added.
  264. */
  265. void net_buf_simple_add_le32(struct net_buf_simple *buf, uint32_t val);
  266. /**
  267. * @brief Add 32-bit value at the end of the buffer
  268. *
  269. * Adds 32-bit value in big endian format at the end of buffer.
  270. * Increments the data length of a buffer to account for more data
  271. * at the end.
  272. *
  273. * @param buf Buffer to update.
  274. * @param val 32-bit value to be added.
  275. */
  276. void net_buf_simple_add_be32(struct net_buf_simple *buf, uint32_t val);
  277. /**
  278. * @brief Add 48-bit value at the end of the buffer
  279. *
  280. * Adds 48-bit value in little endian format at the end of buffer.
  281. * Increments the data length of a buffer to account for more data
  282. * at the end.
  283. *
  284. * @param buf Buffer to update.
  285. * @param val 48-bit value to be added.
  286. */
  287. void net_buf_simple_add_le48(struct net_buf_simple *buf, uint64_t val);
  288. /**
  289. * @brief Add 48-bit value at the end of the buffer
  290. *
  291. * Adds 48-bit value in big endian format at the end of buffer.
  292. * Increments the data length of a buffer to account for more data
  293. * at the end.
  294. *
  295. * @param buf Buffer to update.
  296. * @param val 48-bit value to be added.
  297. */
  298. void net_buf_simple_add_be48(struct net_buf_simple *buf, uint64_t val);
  299. /**
  300. * @brief Add 64-bit value at the end of the buffer
  301. *
  302. * Adds 64-bit value in little endian format at the end of buffer.
  303. * Increments the data length of a buffer to account for more data
  304. * at the end.
  305. *
  306. * @param buf Buffer to update.
  307. * @param val 64-bit value to be added.
  308. */
  309. void net_buf_simple_add_le64(struct net_buf_simple *buf, uint64_t val);
  310. /**
  311. * @brief Add 64-bit value at the end of the buffer
  312. *
  313. * Adds 64-bit value in big endian format at the end of buffer.
  314. * Increments the data length of a buffer to account for more data
  315. * at the end.
  316. *
  317. * @param buf Buffer to update.
  318. * @param val 64-bit value to be added.
  319. */
  320. void net_buf_simple_add_be64(struct net_buf_simple *buf, uint64_t val);
  321. /**
  322. * @brief Remove data from the end of the buffer.
  323. *
  324. * Removes data from the end of the buffer by modifying the buffer length.
  325. *
  326. * @param buf Buffer to update.
  327. * @param len Number of bytes to remove.
  328. *
  329. * @return New end of the buffer data.
  330. */
  331. void *net_buf_simple_remove_mem(struct net_buf_simple *buf, size_t len);
  332. /**
  333. * @brief Remove a 8-bit value from the end of the buffer
  334. *
  335. * Same idea as with net_buf_simple_remove_mem(), but a helper for operating
  336. * on 8-bit values.
  337. *
  338. * @param buf A valid pointer on a buffer.
  339. *
  340. * @return The 8-bit removed value
  341. */
  342. uint8_t net_buf_simple_remove_u8(struct net_buf_simple *buf);
  343. /**
  344. * @brief Remove and convert 16 bits from the end of the buffer.
  345. *
  346. * Same idea as with net_buf_simple_remove_mem(), but a helper for operating
  347. * on 16-bit little endian data.
  348. *
  349. * @param buf A valid pointer on a buffer.
  350. *
  351. * @return 16-bit value converted from little endian to host endian.
  352. */
  353. uint16_t net_buf_simple_remove_le16(struct net_buf_simple *buf);
  354. /**
  355. * @brief Remove and convert 16 bits from the end of the buffer.
  356. *
  357. * Same idea as with net_buf_simple_remove_mem(), but a helper for operating
  358. * on 16-bit big endian data.
  359. *
  360. * @param buf A valid pointer on a buffer.
  361. *
  362. * @return 16-bit value converted from big endian to host endian.
  363. */
  364. uint16_t net_buf_simple_remove_be16(struct net_buf_simple *buf);
  365. /**
  366. * @brief Remove and convert 24 bits from the end of the buffer.
  367. *
  368. * Same idea as with net_buf_simple_remove_mem(), but a helper for operating
  369. * on 24-bit little endian data.
  370. *
  371. * @param buf A valid pointer on a buffer.
  372. *
  373. * @return 24-bit value converted from little endian to host endian.
  374. */
  375. uint32_t net_buf_simple_remove_le24(struct net_buf_simple *buf);
  376. /**
  377. * @brief Remove and convert 24 bits from the end of the buffer.
  378. *
  379. * Same idea as with net_buf_simple_remove_mem(), but a helper for operating
  380. * on 24-bit big endian data.
  381. *
  382. * @param buf A valid pointer on a buffer.
  383. *
  384. * @return 24-bit value converted from big endian to host endian.
  385. */
  386. uint32_t net_buf_simple_remove_be24(struct net_buf_simple *buf);
  387. /**
  388. * @brief Remove and convert 32 bits from the end of the buffer.
  389. *
  390. * Same idea as with net_buf_simple_remove_mem(), but a helper for operating
  391. * on 32-bit little endian data.
  392. *
  393. * @param buf A valid pointer on a buffer.
  394. *
  395. * @return 32-bit value converted from little endian to host endian.
  396. */
  397. uint32_t net_buf_simple_remove_le32(struct net_buf_simple *buf);
  398. /**
  399. * @brief Remove and convert 32 bits from the end of the buffer.
  400. *
  401. * Same idea as with net_buf_simple_remove_mem(), but a helper for operating
  402. * on 32-bit big endian data.
  403. *
  404. * @param buf A valid pointer on a buffer.
  405. *
  406. * @return 32-bit value converted from big endian to host endian.
  407. */
  408. uint32_t net_buf_simple_remove_be32(struct net_buf_simple *buf);
  409. /**
  410. * @brief Remove and convert 48 bits from the end of the buffer.
  411. *
  412. * Same idea as with net_buf_simple_remove_mem(), but a helper for operating
  413. * on 48-bit little endian data.
  414. *
  415. * @param buf A valid pointer on a buffer.
  416. *
  417. * @return 48-bit value converted from little endian to host endian.
  418. */
  419. uint64_t net_buf_simple_remove_le48(struct net_buf_simple *buf);
  420. /**
  421. * @brief Remove and convert 48 bits from the end of the buffer.
  422. *
  423. * Same idea as with net_buf_simple_remove_mem(), but a helper for operating
  424. * on 48-bit big endian data.
  425. *
  426. * @param buf A valid pointer on a buffer.
  427. *
  428. * @return 48-bit value converted from big endian to host endian.
  429. */
  430. uint64_t net_buf_simple_remove_be48(struct net_buf_simple *buf);
  431. /**
  432. * @brief Remove and convert 64 bits from the end of the buffer.
  433. *
  434. * Same idea as with net_buf_simple_remove_mem(), but a helper for operating
  435. * on 64-bit little endian data.
  436. *
  437. * @param buf A valid pointer on a buffer.
  438. *
  439. * @return 64-bit value converted from little endian to host endian.
  440. */
  441. uint64_t net_buf_simple_remove_le64(struct net_buf_simple *buf);
  442. /**
  443. * @brief Remove and convert 64 bits from the end of the buffer.
  444. *
  445. * Same idea as with net_buf_simple_remove_mem(), but a helper for operating
  446. * on 64-bit big endian data.
  447. *
  448. * @param buf A valid pointer on a buffer.
  449. *
  450. * @return 64-bit value converted from big endian to host endian.
  451. */
  452. uint64_t net_buf_simple_remove_be64(struct net_buf_simple *buf);
  453. /**
  454. * @brief Prepare data to be added to the start of the buffer
  455. *
  456. * Modifies the data pointer and buffer length to account for more data
  457. * in the beginning of the buffer.
  458. *
  459. * @param buf Buffer to update.
  460. * @param len Number of bytes to add to the beginning.
  461. *
  462. * @return The new beginning of the buffer data.
  463. */
  464. void *net_buf_simple_push(struct net_buf_simple *buf, size_t len);
  465. /**
  466. * @brief Copy given number of bytes from memory to the start of the buffer.
  467. *
  468. * Modifies the data pointer and buffer length to account for more data
  469. * in the beginning of the buffer.
  470. *
  471. * @param buf Buffer to update.
  472. * @param mem Location of data to be added.
  473. * @param len Length of data to be added.
  474. *
  475. * @return The new beginning of the buffer data.
  476. */
  477. void *net_buf_simple_push_mem(struct net_buf_simple *buf, const void *mem,
  478. size_t len);
  479. /**
  480. * @brief Push 16-bit value to the beginning of the buffer
  481. *
  482. * Adds 16-bit value in little endian format to the beginning of the
  483. * buffer.
  484. *
  485. * @param buf Buffer to update.
  486. * @param val 16-bit value to be pushed to the buffer.
  487. */
  488. void net_buf_simple_push_le16(struct net_buf_simple *buf, uint16_t val);
  489. /**
  490. * @brief Push 16-bit value to the beginning of the buffer
  491. *
  492. * Adds 16-bit value in big endian format to the beginning of the
  493. * buffer.
  494. *
  495. * @param buf Buffer to update.
  496. * @param val 16-bit value to be pushed to the buffer.
  497. */
  498. void net_buf_simple_push_be16(struct net_buf_simple *buf, uint16_t val);
  499. /**
  500. * @brief Push 8-bit value to the beginning of the buffer
  501. *
  502. * Adds 8-bit value the beginning of the buffer.
  503. *
  504. * @param buf Buffer to update.
  505. * @param val 8-bit value to be pushed to the buffer.
  506. */
  507. void net_buf_simple_push_u8(struct net_buf_simple *buf, uint8_t val);
  508. /**
  509. * @brief Push 24-bit value to the beginning of the buffer
  510. *
  511. * Adds 24-bit value in little endian format to the beginning of the
  512. * buffer.
  513. *
  514. * @param buf Buffer to update.
  515. * @param val 24-bit value to be pushed to the buffer.
  516. */
  517. void net_buf_simple_push_le24(struct net_buf_simple *buf, uint32_t val);
  518. /**
  519. * @brief Push 24-bit value to the beginning of the buffer
  520. *
  521. * Adds 24-bit value in big endian format to the beginning of the
  522. * buffer.
  523. *
  524. * @param buf Buffer to update.
  525. * @param val 24-bit value to be pushed to the buffer.
  526. */
  527. void net_buf_simple_push_be24(struct net_buf_simple *buf, uint32_t val);
  528. /**
  529. * @brief Push 32-bit value to the beginning of the buffer
  530. *
  531. * Adds 32-bit value in little endian format to the beginning of the
  532. * buffer.
  533. *
  534. * @param buf Buffer to update.
  535. * @param val 32-bit value to be pushed to the buffer.
  536. */
  537. void net_buf_simple_push_le32(struct net_buf_simple *buf, uint32_t val);
  538. /**
  539. * @brief Push 32-bit value to the beginning of the buffer
  540. *
  541. * Adds 32-bit value in big endian format to the beginning of the
  542. * buffer.
  543. *
  544. * @param buf Buffer to update.
  545. * @param val 32-bit value to be pushed to the buffer.
  546. */
  547. void net_buf_simple_push_be32(struct net_buf_simple *buf, uint32_t val);
  548. /**
  549. * @brief Push 48-bit value to the beginning of the buffer
  550. *
  551. * Adds 48-bit value in little endian format to the beginning of the
  552. * buffer.
  553. *
  554. * @param buf Buffer to update.
  555. * @param val 48-bit value to be pushed to the buffer.
  556. */
  557. void net_buf_simple_push_le48(struct net_buf_simple *buf, uint64_t val);
  558. /**
  559. * @brief Push 48-bit value to the beginning of the buffer
  560. *
  561. * Adds 48-bit value in big endian format to the beginning of the
  562. * buffer.
  563. *
  564. * @param buf Buffer to update.
  565. * @param val 48-bit value to be pushed to the buffer.
  566. */
  567. void net_buf_simple_push_be48(struct net_buf_simple *buf, uint64_t val);
  568. /**
  569. * @brief Push 64-bit value to the beginning of the buffer
  570. *
  571. * Adds 64-bit value in little endian format to the beginning of the
  572. * buffer.
  573. *
  574. * @param buf Buffer to update.
  575. * @param val 64-bit value to be pushed to the buffer.
  576. */
  577. void net_buf_simple_push_le64(struct net_buf_simple *buf, uint64_t val);
  578. /**
  579. * @brief Push 64-bit value to the beginning of the buffer
  580. *
  581. * Adds 64-bit value in big endian format to the beginning of the
  582. * buffer.
  583. *
  584. * @param buf Buffer to update.
  585. * @param val 64-bit value to be pushed to the buffer.
  586. */
  587. void net_buf_simple_push_be64(struct net_buf_simple *buf, uint64_t val);
  588. /**
  589. * @brief Remove data from the beginning of the buffer.
  590. *
  591. * Removes data from the beginning of the buffer by modifying the data
  592. * pointer and buffer length.
  593. *
  594. * @param buf Buffer to update.
  595. * @param len Number of bytes to remove.
  596. *
  597. * @return New beginning of the buffer data.
  598. */
  599. void *net_buf_simple_pull(struct net_buf_simple *buf, size_t len);
  600. /**
  601. * @brief Remove data from the beginning of the buffer.
  602. *
  603. * Removes data from the beginning of the buffer by modifying the data
  604. * pointer and buffer length.
  605. *
  606. * @param buf Buffer to update.
  607. * @param len Number of bytes to remove.
  608. *
  609. * @return Pointer to the old location of the buffer data.
  610. */
  611. void *net_buf_simple_pull_mem(struct net_buf_simple *buf, size_t len);
  612. /**
  613. * @brief Remove a 8-bit value from the beginning of the buffer
  614. *
  615. * Same idea as with net_buf_simple_pull(), but a helper for operating
  616. * on 8-bit values.
  617. *
  618. * @param buf A valid pointer on a buffer.
  619. *
  620. * @return The 8-bit removed value
  621. */
  622. uint8_t net_buf_simple_pull_u8(struct net_buf_simple *buf);
  623. /**
  624. * @brief Remove and convert 16 bits from the beginning of the buffer.
  625. *
  626. * Same idea as with net_buf_simple_pull(), but a helper for operating
  627. * on 16-bit little endian data.
  628. *
  629. * @param buf A valid pointer on a buffer.
  630. *
  631. * @return 16-bit value converted from little endian to host endian.
  632. */
  633. uint16_t net_buf_simple_pull_le16(struct net_buf_simple *buf);
  634. /**
  635. * @brief Remove and convert 16 bits from the beginning of the buffer.
  636. *
  637. * Same idea as with net_buf_simple_pull(), but a helper for operating
  638. * on 16-bit big endian data.
  639. *
  640. * @param buf A valid pointer on a buffer.
  641. *
  642. * @return 16-bit value converted from big endian to host endian.
  643. */
  644. uint16_t net_buf_simple_pull_be16(struct net_buf_simple *buf);
  645. /**
  646. * @brief Remove and convert 24 bits from the beginning of the buffer.
  647. *
  648. * Same idea as with net_buf_simple_pull(), but a helper for operating
  649. * on 24-bit little endian data.
  650. *
  651. * @param buf A valid pointer on a buffer.
  652. *
  653. * @return 24-bit value converted from little endian to host endian.
  654. */
  655. uint32_t net_buf_simple_pull_le24(struct net_buf_simple *buf);
  656. /**
  657. * @brief Remove and convert 24 bits from the beginning of the buffer.
  658. *
  659. * Same idea as with net_buf_simple_pull(), but a helper for operating
  660. * on 24-bit big endian data.
  661. *
  662. * @param buf A valid pointer on a buffer.
  663. *
  664. * @return 24-bit value converted from big endian to host endian.
  665. */
  666. uint32_t net_buf_simple_pull_be24(struct net_buf_simple *buf);
  667. /**
  668. * @brief Remove and convert 32 bits from the beginning of the buffer.
  669. *
  670. * Same idea as with net_buf_simple_pull(), but a helper for operating
  671. * on 32-bit little endian data.
  672. *
  673. * @param buf A valid pointer on a buffer.
  674. *
  675. * @return 32-bit value converted from little endian to host endian.
  676. */
  677. uint32_t net_buf_simple_pull_le32(struct net_buf_simple *buf);
  678. /**
  679. * @brief Remove and convert 32 bits from the beginning of the buffer.
  680. *
  681. * Same idea as with net_buf_simple_pull(), but a helper for operating
  682. * on 32-bit big endian data.
  683. *
  684. * @param buf A valid pointer on a buffer.
  685. *
  686. * @return 32-bit value converted from big endian to host endian.
  687. */
  688. uint32_t net_buf_simple_pull_be32(struct net_buf_simple *buf);
  689. /**
  690. * @brief Remove and convert 48 bits from the beginning of the buffer.
  691. *
  692. * Same idea as with net_buf_simple_pull(), but a helper for operating
  693. * on 48-bit little endian data.
  694. *
  695. * @param buf A valid pointer on a buffer.
  696. *
  697. * @return 48-bit value converted from little endian to host endian.
  698. */
  699. uint64_t net_buf_simple_pull_le48(struct net_buf_simple *buf);
  700. /**
  701. * @brief Remove and convert 48 bits from the beginning of the buffer.
  702. *
  703. * Same idea as with net_buf_simple_pull(), but a helper for operating
  704. * on 48-bit big endian data.
  705. *
  706. * @param buf A valid pointer on a buffer.
  707. *
  708. * @return 48-bit value converted from big endian to host endian.
  709. */
  710. uint64_t net_buf_simple_pull_be48(struct net_buf_simple *buf);
  711. /**
  712. * @brief Remove and convert 64 bits from the beginning of the buffer.
  713. *
  714. * Same idea as with net_buf_simple_pull(), but a helper for operating
  715. * on 64-bit little endian data.
  716. *
  717. * @param buf A valid pointer on a buffer.
  718. *
  719. * @return 64-bit value converted from little endian to host endian.
  720. */
  721. uint64_t net_buf_simple_pull_le64(struct net_buf_simple *buf);
  722. /**
  723. * @brief Remove and convert 64 bits from the beginning of the buffer.
  724. *
  725. * Same idea as with net_buf_simple_pull(), but a helper for operating
  726. * on 64-bit big endian data.
  727. *
  728. * @param buf A valid pointer on a buffer.
  729. *
  730. * @return 64-bit value converted from big endian to host endian.
  731. */
  732. uint64_t net_buf_simple_pull_be64(struct net_buf_simple *buf);
  733. /**
  734. * @brief Get the tail pointer for a buffer.
  735. *
  736. * Get a pointer to the end of the data in a buffer.
  737. *
  738. * @param buf Buffer.
  739. *
  740. * @return Tail pointer for the buffer.
  741. */
  742. static inline uint8_t *net_buf_simple_tail(struct net_buf_simple *buf)
  743. {
  744. return buf->data + buf->len;
  745. }
  746. /**
  747. * @brief Check buffer headroom.
  748. *
  749. * Check how much free space there is in the beginning of the buffer.
  750. *
  751. * buf A valid pointer on a buffer
  752. *
  753. * @return Number of bytes available in the beginning of the buffer.
  754. */
  755. size_t net_buf_simple_headroom(struct net_buf_simple *buf);
  756. /**
  757. * @brief Check buffer tailroom.
  758. *
  759. * Check how much free space there is at the end of the buffer.
  760. *
  761. * @param buf A valid pointer on a buffer
  762. *
  763. * @return Number of bytes available at the end of the buffer.
  764. */
  765. size_t net_buf_simple_tailroom(struct net_buf_simple *buf);
  766. /**
  767. * @brief Check maximum net_buf_simple::len value.
  768. *
  769. * This value is depending on the number of bytes being reserved as headroom.
  770. *
  771. * @param buf A valid pointer on a buffer
  772. *
  773. * @return Number of bytes usable behind the net_buf_simple::data pointer.
  774. */
  775. uint16_t net_buf_simple_max_len(struct net_buf_simple *buf);
  776. /**
  777. * @brief Parsing state of a buffer.
  778. *
  779. * This is used for temporarily storing the parsing state of a buffer
  780. * while giving control of the parsing to a routine which we don't
  781. * control.
  782. */
  783. struct net_buf_simple_state {
  784. /** Offset of the data pointer from the beginning of the storage */
  785. uint16_t offset;
  786. /** Length of data */
  787. uint16_t len;
  788. };
  789. /**
  790. * @brief Save the parsing state of a buffer.
  791. *
  792. * Saves the parsing state of a buffer so it can be restored later.
  793. *
  794. * @param buf Buffer from which the state should be saved.
  795. * @param state Storage for the state.
  796. */
  797. static inline void net_buf_simple_save(struct net_buf_simple *buf,
  798. struct net_buf_simple_state *state)
  799. {
  800. state->offset = net_buf_simple_headroom(buf);
  801. state->len = buf->len;
  802. }
  803. /**
  804. * @brief Restore the parsing state of a buffer.
  805. *
  806. * Restores the parsing state of a buffer from a state previously stored
  807. * by net_buf_simple_save().
  808. *
  809. * @param buf Buffer to which the state should be restored.
  810. * @param state Stored state.
  811. */
  812. static inline void net_buf_simple_restore(struct net_buf_simple *buf,
  813. struct net_buf_simple_state *state)
  814. {
  815. buf->data = buf->__buf + state->offset;
  816. buf->len = state->len;
  817. }
  818. /**
  819. * Flag indicating that the buffer has associated fragments. Only used
  820. * internally by the buffer handling code while the buffer is inside a
  821. * FIFO, meaning this never needs to be explicitly set or unset by the
  822. * net_buf API user. As long as the buffer is outside of a FIFO, i.e.
  823. * in practice always for the user for this API, the buf->frags pointer
  824. * should be used instead.
  825. */
  826. #define NET_BUF_FRAGS BIT(0)
  827. /**
  828. * Flag indicating that the buffer's associated data pointer, points to
  829. * externally allocated memory. Therefore once ref goes down to zero, the
  830. * pointed data will not need to be deallocated. This never needs to be
  831. * explicitly set or unet by the net_buf API user. Such net_buf is
  832. * exclusively instantiated via net_buf_alloc_with_data() function.
  833. * Reference count mechanism however will behave the same way, and ref
  834. * count going to 0 will free the net_buf but no the data pointer in it.
  835. */
  836. #define NET_BUF_EXTERNAL_DATA BIT(1)
  837. /**
  838. * @brief Network buffer representation.
  839. *
  840. * This struct is used to represent network buffers. Such buffers are
  841. * normally defined through the NET_BUF_POOL_*_DEFINE() APIs and allocated
  842. * using the net_buf_alloc() API.
  843. */
  844. struct net_buf {
  845. union {
  846. /** Allow placing the buffer into sys_slist_t */
  847. sys_snode_t node;
  848. /** Fragments associated with this buffer. */
  849. struct net_buf *frags;
  850. };
  851. /** Reference count. */
  852. uint8_t ref;
  853. /** Bit-field of buffer flags. */
  854. uint8_t flags;
  855. /** Where the buffer should go when freed up. */
  856. uint8_t pool_id;
  857. /* Union for convenience access to the net_buf_simple members, also
  858. * preserving the old API.
  859. */
  860. union {
  861. /* The ABI of this struct must match net_buf_simple */
  862. struct {
  863. /** Pointer to the start of data in the buffer. */
  864. uint8_t *data;
  865. /** Length of the data behind the data pointer. */
  866. uint16_t len;
  867. /** Amount of data that this buffer can store. */
  868. uint16_t size;
  869. /** Start of the data storage. Not to be accessed
  870. * directly (the data pointer should be used
  871. * instead).
  872. */
  873. uint8_t *__buf;
  874. };
  875. struct net_buf_simple b;
  876. };
  877. /* Actions add start */
  878. #if defined(CONFIG_NET_BUF_POOL_USAGE)
  879. uint32_t timestamp;
  880. #endif
  881. /* Actions add end */
  882. /** System metadata for this buffer. */
  883. uint8_t user_data[CONFIG_NET_BUF_USER_DATA_SIZE] __net_buf_align;
  884. };
  885. struct net_buf_data_cb {
  886. uint8_t * (*alloc)(struct net_buf *buf, size_t *size,
  887. k_timeout_t timeout);
  888. uint8_t * (*ref)(struct net_buf *buf, uint8_t *data);
  889. void (*unref)(struct net_buf *buf, uint8_t *data);
  890. };
  891. struct net_buf_data_alloc {
  892. const struct net_buf_data_cb *cb;
  893. void *alloc_data;
  894. /* Actions add start */
  895. uint16_t data_size;
  896. /* Actions add end */
  897. };
  898. /**
  899. * @brief Network buffer pool representation.
  900. *
  901. * This struct is used to represent a pool of network buffers.
  902. */
  903. struct net_buf_pool {
  904. /** LIFO to place the buffer into when free */
  905. struct k_lifo free;
  906. /** Number of buffers in pool */
  907. const uint16_t buf_count;
  908. /** Number of uninitialized buffers */
  909. uint16_t uninit_count;
  910. #if defined(CONFIG_NET_BUF_POOL_USAGE)
  911. /** Amount of available buffers in the pool. */
  912. atomic_t avail_count;
  913. /** Total size of the pool. */
  914. const uint16_t pool_size;
  915. /** Name of the pool. Used when printing pool information. */
  916. const char *name;
  917. /* Actions add start */
  918. /** Actions add, amount of maximize buffers in the pool. */
  919. int16_t max_used;
  920. /* Actions add end */
  921. #endif /* CONFIG_NET_BUF_POOL_USAGE */
  922. /** Optional destroy callback when buffer is freed. */
  923. void (*const destroy)(struct net_buf *buf);
  924. /** Data allocation handlers. */
  925. const struct net_buf_data_alloc *alloc;
  926. /** Start of buffer storage array */
  927. struct net_buf * const __bufs;
  928. };
  929. /** @cond INTERNAL_HIDDEN */
  930. #if defined(CONFIG_NET_BUF_POOL_USAGE)
  931. #define NET_BUF_POOL_INITIALIZER(_pool, _alloc, _bufs, _count, _destroy) \
  932. { \
  933. .free = Z_LIFO_INITIALIZER(_pool.free), \
  934. .buf_count = _count, \
  935. .uninit_count = _count, \
  936. .avail_count = ATOMIC_INIT(_count), \
  937. .max_used = 0, \
  938. .name = STRINGIFY(_pool), \
  939. .destroy = _destroy, \
  940. .alloc = _alloc, \
  941. .__bufs = _bufs, \
  942. }
  943. #else
  944. #define NET_BUF_POOL_INITIALIZER(_pool, _alloc, _bufs, _count, _destroy) \
  945. { \
  946. .free = Z_LIFO_INITIALIZER(_pool.free), \
  947. .buf_count = _count, \
  948. .uninit_count = _count, \
  949. .destroy = _destroy, \
  950. .alloc = _alloc, \
  951. .__bufs = _bufs, \
  952. }
  953. #endif /* CONFIG_NET_BUF_POOL_USAGE */
  954. extern const struct net_buf_data_alloc net_buf_heap_alloc;
  955. /** @endcond */
  956. /**
  957. * @def NET_BUF_POOL_HEAP_DEFINE
  958. * @brief Define a new pool for buffers using the heap for the data.
  959. *
  960. * Defines a net_buf_pool struct and the necessary memory storage (array of
  961. * structs) for the needed amount of buffers. After this, the buffers can be
  962. * accessed from the pool through net_buf_alloc. The pool is defined as a
  963. * static variable, so if it needs to be exported outside the current module
  964. * this needs to happen with the help of a separate pointer rather than an
  965. * extern declaration.
  966. *
  967. * The data payload of the buffers will be allocated from the heap using
  968. * k_malloc, so CONFIG_HEAP_MEM_POOL_SIZE must be set to a positive value.
  969. * This kind of pool does not support blocking on the data allocation, so
  970. * the timeout passed to net_buf_alloc will be always treated as K_NO_WAIT
  971. * when trying to allocate the data. This means that allocation failures,
  972. * i.e. NULL returns, must always be handled cleanly.
  973. *
  974. * If provided with a custom destroy callback, this callback is
  975. * responsible for eventually calling net_buf_destroy() to complete the
  976. * process of returning the buffer to the pool.
  977. *
  978. * @param _name Name of the pool variable.
  979. * @param _count Number of buffers in the pool.
  980. * @param _destroy Optional destroy callback when buffer is freed.
  981. */
  982. #define NET_BUF_POOL_HEAP_DEFINE(_name, _count, _destroy) \
  983. static struct net_buf net_buf_##_name[_count] __noinit; \
  984. static struct net_buf_pool _name __net_buf_align \
  985. __in_section(_net_buf_pool, static, _name) = \
  986. NET_BUF_POOL_INITIALIZER(_name, &net_buf_heap_alloc, \
  987. net_buf_##_name, _count, _destroy)
  988. struct net_buf_pool_fixed {
  989. size_t data_size;
  990. uint8_t *data_pool;
  991. };
  992. /** @cond INTERNAL_HIDDEN */
  993. extern const struct net_buf_data_cb net_buf_fixed_cb;
  994. /** @endcond */
  995. /**
  996. * @def NET_BUF_POOL_FIXED_DEFINE
  997. * @brief Define a new pool for buffers based on fixed-size data
  998. *
  999. * Defines a net_buf_pool struct and the necessary memory storage (array of
  1000. * structs) for the needed amount of buffers. After this, the buffers can be
  1001. * accessed from the pool through net_buf_alloc. The pool is defined as a
  1002. * static variable, so if it needs to be exported outside the current module
  1003. * this needs to happen with the help of a separate pointer rather than an
  1004. * extern declaration.
  1005. *
  1006. * The data payload of the buffers will be allocated from a byte array
  1007. * of fixed sized chunks. This kind of pool does not support blocking on
  1008. * the data allocation, so the timeout passed to net_buf_alloc will be
  1009. * always treated as K_NO_WAIT when trying to allocate the data. This means
  1010. * that allocation failures, i.e. NULL returns, must always be handled
  1011. * cleanly.
  1012. *
  1013. * If provided with a custom destroy callback, this callback is
  1014. * responsible for eventually calling net_buf_destroy() to complete the
  1015. * process of returning the buffer to the pool.
  1016. *
  1017. * @param _name Name of the pool variable.
  1018. * @param _count Number of buffers in the pool.
  1019. * @param _data_size Maximum data payload per buffer.
  1020. * @param _destroy Optional destroy callback when buffer is freed.
  1021. */
  1022. #define NET_BUF_POOL_FIXED_DEFINE(_name, _count, _data_size, _destroy) \
  1023. static struct net_buf net_buf_##_name[_count] __noinit; \
  1024. static uint8_t __noinit net_buf_data_##_name[_count][_data_size]; \
  1025. static const struct net_buf_pool_fixed net_buf_fixed_##_name = { \
  1026. .data_size = _data_size, \
  1027. .data_pool = (uint8_t *)net_buf_data_##_name, \
  1028. }; \
  1029. static const struct net_buf_data_alloc net_buf_fixed_alloc_##_name = {\
  1030. .cb = &net_buf_fixed_cb, \
  1031. .alloc_data = (void *)&net_buf_fixed_##_name, \
  1032. .data_size = _data_size, \
  1033. }; \
  1034. static struct net_buf_pool _name __net_buf_align \
  1035. __in_section(_net_buf_pool, static, _name) = \
  1036. NET_BUF_POOL_INITIALIZER(_name, &net_buf_fixed_alloc_##_name, \
  1037. net_buf_##_name, _count, _destroy)
  1038. /** @cond INTERNAL_HIDDEN */
  1039. extern const struct net_buf_data_cb net_buf_var_cb;
  1040. /** @endcond */
  1041. /**
  1042. * @def NET_BUF_POOL_VAR_DEFINE
  1043. * @brief Define a new pool for buffers with variable size payloads
  1044. *
  1045. * Defines a net_buf_pool struct and the necessary memory storage (array of
  1046. * structs) for the needed amount of buffers. After this, the buffers can be
  1047. * accessed from the pool through net_buf_alloc. The pool is defined as a
  1048. * static variable, so if it needs to be exported outside the current module
  1049. * this needs to happen with the help of a separate pointer rather than an
  1050. * extern declaration.
  1051. *
  1052. * The data payload of the buffers will be based on a memory pool from which
  1053. * variable size payloads may be allocated.
  1054. *
  1055. * If provided with a custom destroy callback, this callback is
  1056. * responsible for eventually calling net_buf_destroy() to complete the
  1057. * process of returning the buffer to the pool.
  1058. *
  1059. * @param _name Name of the pool variable.
  1060. * @param _count Number of buffers in the pool.
  1061. * @param _data_size Total amount of memory available for data payloads.
  1062. * @param _destroy Optional destroy callback when buffer is freed.
  1063. */
  1064. #define NET_BUF_POOL_VAR_DEFINE(_name, _count, _data_size, _destroy) \
  1065. static struct net_buf _net_buf_##_name[_count] __noinit; \
  1066. K_HEAP_DEFINE(net_buf_mem_pool_##_name, _data_size); \
  1067. static const struct net_buf_data_alloc net_buf_data_alloc_##_name = { \
  1068. .cb = &net_buf_var_cb, \
  1069. .alloc_data = &net_buf_mem_pool_##_name, \
  1070. }; \
  1071. static struct net_buf_pool _name __net_buf_align \
  1072. __in_section(_net_buf_pool, static, _name) = \
  1073. NET_BUF_POOL_INITIALIZER(_name, &net_buf_data_alloc_##_name, \
  1074. _net_buf_##_name, _count, _destroy)
  1075. /**
  1076. * @def NET_BUF_POOL_DEFINE
  1077. * @brief Define a new pool for buffers
  1078. *
  1079. * Defines a net_buf_pool struct and the necessary memory storage (array of
  1080. * structs) for the needed amount of buffers. After this,the buffers can be
  1081. * accessed from the pool through net_buf_alloc. The pool is defined as a
  1082. * static variable, so if it needs to be exported outside the current module
  1083. * this needs to happen with the help of a separate pointer rather than an
  1084. * extern declaration.
  1085. *
  1086. * If provided with a custom destroy callback this callback is
  1087. * responsible for eventually calling net_buf_destroy() to complete the
  1088. * process of returning the buffer to the pool.
  1089. *
  1090. * @param _name Name of the pool variable.
  1091. * @param _count Number of buffers in the pool.
  1092. * @param _size Maximum data size for each buffer.
  1093. * @param _ud_size Amount of user data space to reserve.
  1094. * @param _destroy Optional destroy callback when buffer is freed.
  1095. */
  1096. #define NET_BUF_POOL_DEFINE(_name, _count, _size, _ud_size, _destroy) \
  1097. BUILD_ASSERT(_ud_size <= CONFIG_NET_BUF_USER_DATA_SIZE); \
  1098. NET_BUF_POOL_FIXED_DEFINE(_name, _count, _size, _destroy)
  1099. /**
  1100. * @brief Looks up a pool based on its ID.
  1101. *
  1102. * @param id Pool ID (e.g. from buf->pool_id).
  1103. *
  1104. * @return Pointer to pool.
  1105. */
  1106. struct net_buf_pool *net_buf_pool_get(int id);
  1107. /**
  1108. * @brief Get a zero-based index for a buffer.
  1109. *
  1110. * This function will translate a buffer into a zero-based index,
  1111. * based on its placement in its buffer pool. This can be useful if you
  1112. * want to associate an external array of meta-data contexts with the
  1113. * buffers of a pool.
  1114. *
  1115. * @param buf Network buffer.
  1116. *
  1117. * @return Zero-based index for the buffer.
  1118. */
  1119. int net_buf_id(struct net_buf *buf);
  1120. /**
  1121. * @brief Allocate a new fixed buffer from a pool.
  1122. *
  1123. * @param pool Which pool to allocate the buffer from.
  1124. * @param timeout Affects the action taken should the pool be empty.
  1125. * If K_NO_WAIT, then return immediately. If K_FOREVER, then
  1126. * wait as long as necessary. Otherwise, wait until the specified
  1127. * timeout. Note that some types of data allocators do not support
  1128. * blocking (such as the HEAP type). In this case it's still possible
  1129. * for net_buf_alloc() to fail (return NULL) even if it was given
  1130. * K_FOREVER.
  1131. *
  1132. * @return New buffer or NULL if out of buffers.
  1133. */
  1134. #if defined(CONFIG_NET_BUF_LOG)
  1135. struct net_buf *net_buf_alloc_fixed_debug(struct net_buf_pool *pool,
  1136. k_timeout_t timeout, const char *func,
  1137. int line);
  1138. #define net_buf_alloc_fixed(_pool, _timeout) \
  1139. net_buf_alloc_fixed_debug(_pool, _timeout, __func__, __LINE__)
  1140. #else
  1141. struct net_buf *net_buf_alloc_fixed(struct net_buf_pool *pool,
  1142. k_timeout_t timeout);
  1143. #endif
  1144. /**
  1145. * @copydetails net_buf_alloc_fixed
  1146. */
  1147. static inline struct net_buf *net_buf_alloc(struct net_buf_pool *pool,
  1148. k_timeout_t timeout)
  1149. {
  1150. return net_buf_alloc_fixed(pool, timeout);
  1151. }
  1152. /**
  1153. * @brief Allocate a new variable length buffer from a pool.
  1154. *
  1155. * @param pool Which pool to allocate the buffer from.
  1156. * @param size Amount of data the buffer must be able to fit.
  1157. * @param timeout Affects the action taken should the pool be empty.
  1158. * If K_NO_WAIT, then return immediately. If K_FOREVER, then
  1159. * wait as long as necessary. Otherwise, wait until the specified
  1160. * timeout. Note that some types of data allocators do not support
  1161. * blocking (such as the HEAP type). In this case it's still possible
  1162. * for net_buf_alloc() to fail (return NULL) even if it was given
  1163. * K_FOREVER.
  1164. *
  1165. * @return New buffer or NULL if out of buffers.
  1166. */
  1167. #if defined(CONFIG_NET_BUF_LOG)
  1168. struct net_buf *net_buf_alloc_len_debug(struct net_buf_pool *pool, size_t size,
  1169. k_timeout_t timeout, const char *func,
  1170. int line);
  1171. #define net_buf_alloc_len(_pool, _size, _timeout) \
  1172. net_buf_alloc_len_debug(_pool, _size, _timeout, __func__, __LINE__)
  1173. #else
  1174. struct net_buf *net_buf_alloc_len(struct net_buf_pool *pool, size_t size,
  1175. k_timeout_t timeout);
  1176. #endif
  1177. /**
  1178. * @brief Allocate a new buffer from a pool but with external data pointer.
  1179. *
  1180. * Allocate a new buffer from a pool, where the data pointer comes from the
  1181. * user and not from the pool.
  1182. *
  1183. * @param pool Which pool to allocate the buffer from.
  1184. * @param data External data pointer
  1185. * @param size Amount of data the pointed data buffer if able to fit.
  1186. * @param timeout Affects the action taken should the pool be empty.
  1187. * If K_NO_WAIT, then return immediately. If K_FOREVER, then
  1188. * wait as long as necessary. Otherwise, wait until the specified
  1189. * timeout. Note that some types of data allocators do not support
  1190. * blocking (such as the HEAP type). In this case it's still possible
  1191. * for net_buf_alloc() to fail (return NULL) even if it was given
  1192. * K_FOREVER.
  1193. *
  1194. * @return New buffer or NULL if out of buffers.
  1195. */
  1196. #if defined(CONFIG_NET_BUF_LOG)
  1197. struct net_buf *net_buf_alloc_with_data_debug(struct net_buf_pool *pool,
  1198. void *data, size_t size,
  1199. k_timeout_t timeout,
  1200. const char *func, int line);
  1201. #define net_buf_alloc_with_data(_pool, _data_, _size, _timeout) \
  1202. net_buf_alloc_with_data_debug(_pool, _data_, _size, _timeout, \
  1203. __func__, __LINE__)
  1204. #else
  1205. struct net_buf *net_buf_alloc_with_data(struct net_buf_pool *pool,
  1206. void *data, size_t size,
  1207. k_timeout_t timeout);
  1208. #endif
  1209. /**
  1210. * @brief Get a buffer from a FIFO.
  1211. *
  1212. * This function is NOT thread-safe if the buffers in the FIFO contain
  1213. * fragments.
  1214. *
  1215. * @param fifo Which FIFO to take the buffer from.
  1216. * @param timeout Affects the action taken should the FIFO be empty.
  1217. * If K_NO_WAIT, then return immediately. If K_FOREVER, then wait as
  1218. * long as necessary. Otherwise, wait until the specified timeout.
  1219. *
  1220. * @return New buffer or NULL if the FIFO is empty.
  1221. */
  1222. #if defined(CONFIG_NET_BUF_LOG)
  1223. struct net_buf *net_buf_get_debug(struct k_fifo *fifo, k_timeout_t timeout,
  1224. const char *func, int line);
  1225. #define net_buf_get(_fifo, _timeout) \
  1226. net_buf_get_debug(_fifo, _timeout, __func__, __LINE__)
  1227. #else
  1228. struct net_buf *net_buf_get(struct k_fifo *fifo, k_timeout_t timeout);
  1229. #endif
  1230. /**
  1231. * @brief Destroy buffer from custom destroy callback
  1232. *
  1233. * This helper is only intended to be used from custom destroy callbacks.
  1234. * If no custom destroy callback is given to NET_BUF_POOL_*_DEFINE() then
  1235. * there is no need to use this API.
  1236. *
  1237. * @param buf Buffer to destroy.
  1238. */
  1239. static inline void net_buf_destroy(struct net_buf *buf)
  1240. {
  1241. struct net_buf_pool *pool = net_buf_pool_get(buf->pool_id);
  1242. k_lifo_put(&pool->free, buf);
  1243. }
  1244. /**
  1245. * @brief Reset buffer
  1246. *
  1247. * Reset buffer data and flags so it can be reused for other purposes.
  1248. *
  1249. * @param buf Buffer to reset.
  1250. */
  1251. void net_buf_reset(struct net_buf *buf);
  1252. /**
  1253. * @brief Initialize buffer with the given headroom.
  1254. *
  1255. * The buffer is not expected to contain any data when this API is called.
  1256. *
  1257. * @param buf Buffer to initialize.
  1258. * @param reserve How much headroom to reserve.
  1259. */
  1260. void net_buf_simple_reserve(struct net_buf_simple *buf, size_t reserve);
  1261. /**
  1262. * @brief Put a buffer into a list
  1263. *
  1264. * If the buffer contains follow-up fragments this function will take care of
  1265. * inserting them as well into the list.
  1266. *
  1267. * @param list Which list to append the buffer to.
  1268. * @param buf Buffer.
  1269. */
  1270. void net_buf_slist_put(sys_slist_t *list, struct net_buf *buf);
  1271. /**
  1272. * @brief Get a buffer from a list.
  1273. *
  1274. * If the buffer had any fragments, these will automatically be recovered from
  1275. * the list as well and be placed to the buffer's fragment list. This function
  1276. * is NOT thread-safe when recovering fragments.
  1277. *
  1278. * @param list Which list to take the buffer from.
  1279. *
  1280. * @return New buffer or NULL if the FIFO is empty.
  1281. */
  1282. struct net_buf *net_buf_slist_get(sys_slist_t *list);
  1283. /**
  1284. * @brief Put a buffer to the end of a FIFO.
  1285. *
  1286. * If the buffer contains follow-up fragments this function will take care of
  1287. * inserting them as well into the FIFO.
  1288. *
  1289. * @param fifo Which FIFO to put the buffer to.
  1290. * @param buf Buffer.
  1291. */
  1292. void net_buf_put(struct k_fifo *fifo, struct net_buf *buf);
  1293. /**
  1294. * @brief Decrements the reference count of a buffer.
  1295. *
  1296. * The buffer is put back into the pool if the reference count reaches zero.
  1297. *
  1298. * @param buf A valid pointer on a buffer
  1299. */
  1300. #if defined(CONFIG_NET_BUF_LOG)
  1301. void net_buf_unref_debug(struct net_buf *buf, const char *func, int line);
  1302. #define net_buf_unref(_buf) \
  1303. net_buf_unref_debug(_buf, __func__, __LINE__)
  1304. #else
  1305. void net_buf_unref(struct net_buf *buf);
  1306. #endif
  1307. /**
  1308. * @brief Increment the reference count of a buffer.
  1309. *
  1310. * @param buf A valid pointer on a buffer
  1311. *
  1312. * @return the buffer newly referenced
  1313. */
  1314. struct net_buf *net_buf_ref(struct net_buf *buf);
  1315. /**
  1316. * @brief Clone buffer
  1317. *
  1318. * Duplicate given buffer including any data and headers currently stored.
  1319. *
  1320. * @param buf A valid pointer on a buffer
  1321. * @param timeout Affects the action taken should the pool be empty.
  1322. * If K_NO_WAIT, then return immediately. If K_FOREVER, then
  1323. * wait as long as necessary. Otherwise, wait until the specified
  1324. * timeout.
  1325. *
  1326. * @return Cloned buffer or NULL if out of buffers.
  1327. */
  1328. struct net_buf *net_buf_clone(struct net_buf *buf, k_timeout_t timeout);
  1329. /**
  1330. * @brief Get a pointer to the user data of a buffer.
  1331. *
  1332. * @param buf A valid pointer on a buffer
  1333. *
  1334. * @return Pointer to the user data of the buffer.
  1335. */
  1336. static inline void *net_buf_user_data(const struct net_buf *buf)
  1337. {
  1338. return (void *)buf->user_data;
  1339. }
  1340. /**
  1341. * @brief Initialize buffer with the given headroom.
  1342. *
  1343. * The buffer is not expected to contain any data when this API is called.
  1344. *
  1345. * @param buf Buffer to initialize.
  1346. * @param reserve How much headroom to reserve.
  1347. */
  1348. static inline void net_buf_reserve(struct net_buf *buf, size_t reserve)
  1349. {
  1350. net_buf_simple_reserve(&buf->b, reserve);
  1351. }
  1352. /**
  1353. * @brief Prepare data to be added at the end of the buffer
  1354. *
  1355. * Increments the data length of a buffer to account for more data
  1356. * at the end.
  1357. *
  1358. * @param buf Buffer to update.
  1359. * @param len Number of bytes to increment the length with.
  1360. *
  1361. * @return The original tail of the buffer.
  1362. */
  1363. static inline void *net_buf_add(struct net_buf *buf, size_t len)
  1364. {
  1365. return net_buf_simple_add(&buf->b, len);
  1366. }
  1367. /**
  1368. * @brief Copies the given number of bytes to the end of the buffer
  1369. *
  1370. * Increments the data length of the buffer to account for more data at
  1371. * the end.
  1372. *
  1373. * @param buf Buffer to update.
  1374. * @param mem Location of data to be added.
  1375. * @param len Length of data to be added
  1376. *
  1377. * @return The original tail of the buffer.
  1378. */
  1379. static inline void *net_buf_add_mem(struct net_buf *buf, const void *mem,
  1380. size_t len)
  1381. {
  1382. return net_buf_simple_add_mem(&buf->b, mem, len);
  1383. }
  1384. /**
  1385. * @brief Add (8-bit) byte at the end of the buffer
  1386. *
  1387. * Increments the data length of the buffer to account for more data at
  1388. * the end.
  1389. *
  1390. * @param buf Buffer to update.
  1391. * @param val byte value to be added.
  1392. *
  1393. * @return Pointer to the value added
  1394. */
  1395. static inline uint8_t *net_buf_add_u8(struct net_buf *buf, uint8_t val)
  1396. {
  1397. return net_buf_simple_add_u8(&buf->b, val);
  1398. }
  1399. /**
  1400. * @brief Add 16-bit value at the end of the buffer
  1401. *
  1402. * Adds 16-bit value in little endian format at the end of buffer.
  1403. * Increments the data length of a buffer to account for more data
  1404. * at the end.
  1405. *
  1406. * @param buf Buffer to update.
  1407. * @param val 16-bit value to be added.
  1408. */
  1409. static inline void net_buf_add_le16(struct net_buf *buf, uint16_t val)
  1410. {
  1411. net_buf_simple_add_le16(&buf->b, val);
  1412. }
  1413. /**
  1414. * @brief Add 16-bit value at the end of the buffer
  1415. *
  1416. * Adds 16-bit value in big endian format at the end of buffer.
  1417. * Increments the data length of a buffer to account for more data
  1418. * at the end.
  1419. *
  1420. * @param buf Buffer to update.
  1421. * @param val 16-bit value to be added.
  1422. */
  1423. static inline void net_buf_add_be16(struct net_buf *buf, uint16_t val)
  1424. {
  1425. net_buf_simple_add_be16(&buf->b, val);
  1426. }
  1427. /**
  1428. * @brief Add 24-bit value at the end of the buffer
  1429. *
  1430. * Adds 24-bit value in little endian format at the end of buffer.
  1431. * Increments the data length of a buffer to account for more data
  1432. * at the end.
  1433. *
  1434. * @param buf Buffer to update.
  1435. * @param val 24-bit value to be added.
  1436. */
  1437. static inline void net_buf_add_le24(struct net_buf *buf, uint32_t val)
  1438. {
  1439. net_buf_simple_add_le24(&buf->b, val);
  1440. }
  1441. /**
  1442. * @brief Add 24-bit value at the end of the buffer
  1443. *
  1444. * Adds 24-bit value in big endian format at the end of buffer.
  1445. * Increments the data length of a buffer to account for more data
  1446. * at the end.
  1447. *
  1448. * @param buf Buffer to update.
  1449. * @param val 24-bit value to be added.
  1450. */
  1451. static inline void net_buf_add_be24(struct net_buf *buf, uint32_t val)
  1452. {
  1453. net_buf_simple_add_be24(&buf->b, val);
  1454. }
  1455. /**
  1456. * @brief Add 32-bit value at the end of the buffer
  1457. *
  1458. * Adds 32-bit value in little endian format at the end of buffer.
  1459. * Increments the data length of a buffer to account for more data
  1460. * at the end.
  1461. *
  1462. * @param buf Buffer to update.
  1463. * @param val 32-bit value to be added.
  1464. */
  1465. static inline void net_buf_add_le32(struct net_buf *buf, uint32_t val)
  1466. {
  1467. net_buf_simple_add_le32(&buf->b, val);
  1468. }
  1469. /**
  1470. * @brief Add 32-bit value at the end of the buffer
  1471. *
  1472. * Adds 32-bit value in big endian format at the end of buffer.
  1473. * Increments the data length of a buffer to account for more data
  1474. * at the end.
  1475. *
  1476. * @param buf Buffer to update.
  1477. * @param val 32-bit value to be added.
  1478. */
  1479. static inline void net_buf_add_be32(struct net_buf *buf, uint32_t val)
  1480. {
  1481. net_buf_simple_add_be32(&buf->b, val);
  1482. }
  1483. /**
  1484. * @brief Add 48-bit value at the end of the buffer
  1485. *
  1486. * Adds 48-bit value in little endian format at the end of buffer.
  1487. * Increments the data length of a buffer to account for more data
  1488. * at the end.
  1489. *
  1490. * @param buf Buffer to update.
  1491. * @param val 48-bit value to be added.
  1492. */
  1493. static inline void net_buf_add_le48(struct net_buf *buf, uint64_t val)
  1494. {
  1495. net_buf_simple_add_le48(&buf->b, val);
  1496. }
  1497. /**
  1498. * @brief Add 48-bit value at the end of the buffer
  1499. *
  1500. * Adds 48-bit value in big endian format at the end of buffer.
  1501. * Increments the data length of a buffer to account for more data
  1502. * at the end.
  1503. *
  1504. * @param buf Buffer to update.
  1505. * @param val 48-bit value to be added.
  1506. */
  1507. static inline void net_buf_add_be48(struct net_buf *buf, uint64_t val)
  1508. {
  1509. net_buf_simple_add_be48(&buf->b, val);
  1510. }
  1511. /**
  1512. * @brief Add 64-bit value at the end of the buffer
  1513. *
  1514. * Adds 64-bit value in little endian format at the end of buffer.
  1515. * Increments the data length of a buffer to account for more data
  1516. * at the end.
  1517. *
  1518. * @param buf Buffer to update.
  1519. * @param val 64-bit value to be added.
  1520. */
  1521. static inline void net_buf_add_le64(struct net_buf *buf, uint64_t val)
  1522. {
  1523. net_buf_simple_add_le64(&buf->b, val);
  1524. }
  1525. /**
  1526. * @brief Add 64-bit value at the end of the buffer
  1527. *
  1528. * Adds 64-bit value in big endian format at the end of buffer.
  1529. * Increments the data length of a buffer to account for more data
  1530. * at the end.
  1531. *
  1532. * @param buf Buffer to update.
  1533. * @param val 64-bit value to be added.
  1534. */
  1535. static inline void net_buf_add_be64(struct net_buf *buf, uint64_t val)
  1536. {
  1537. net_buf_simple_add_be64(&buf->b, val);
  1538. }
  1539. /**
  1540. * @brief Remove data from the end of the buffer.
  1541. *
  1542. * Removes data from the end of the buffer by modifying the buffer length.
  1543. *
  1544. * @param buf Buffer to update.
  1545. * @param len Number of bytes to remove.
  1546. *
  1547. * @return New end of the buffer data.
  1548. */
  1549. static inline void *net_buf_remove_mem(struct net_buf *buf, size_t len)
  1550. {
  1551. return net_buf_simple_remove_mem(&buf->b, len);
  1552. }
  1553. /**
  1554. * @brief Remove a 8-bit value from the end of the buffer
  1555. *
  1556. * Same idea as with net_buf_remove_mem(), but a helper for operating on
  1557. * 8-bit values.
  1558. *
  1559. * @param buf A valid pointer on a buffer.
  1560. *
  1561. * @return The 8-bit removed value
  1562. */
  1563. static inline uint8_t net_buf_remove_u8(struct net_buf *buf)
  1564. {
  1565. return net_buf_simple_remove_u8(&buf->b);
  1566. }
  1567. /**
  1568. * @brief Remove and convert 16 bits from the end of the buffer.
  1569. *
  1570. * Same idea as with net_buf_remove_mem(), but a helper for operating on
  1571. * 16-bit little endian data.
  1572. *
  1573. * @param buf A valid pointer on a buffer.
  1574. *
  1575. * @return 16-bit value converted from little endian to host endian.
  1576. */
  1577. static inline uint16_t net_buf_remove_le16(struct net_buf *buf)
  1578. {
  1579. return net_buf_simple_remove_le16(&buf->b);
  1580. }
  1581. /**
  1582. * @brief Remove and convert 16 bits from the end of the buffer.
  1583. *
  1584. * Same idea as with net_buf_remove_mem(), but a helper for operating on
  1585. * 16-bit big endian data.
  1586. *
  1587. * @param buf A valid pointer on a buffer.
  1588. *
  1589. * @return 16-bit value converted from big endian to host endian.
  1590. */
  1591. static inline uint16_t net_buf_remove_be16(struct net_buf *buf)
  1592. {
  1593. return net_buf_simple_remove_be16(&buf->b);
  1594. }
  1595. /**
  1596. * @brief Remove and convert 24 bits from the end of the buffer.
  1597. *
  1598. * Same idea as with net_buf_remove_mem(), but a helper for operating on
  1599. * 24-bit big endian data.
  1600. *
  1601. * @param buf A valid pointer on a buffer.
  1602. *
  1603. * @return 24-bit value converted from big endian to host endian.
  1604. */
  1605. static inline uint32_t net_buf_remove_be24(struct net_buf *buf)
  1606. {
  1607. return net_buf_simple_remove_be24(&buf->b);
  1608. }
  1609. /**
  1610. * @brief Remove and convert 24 bits from the end of the buffer.
  1611. *
  1612. * Same idea as with net_buf_remove_mem(), but a helper for operating on
  1613. * 24-bit little endian data.
  1614. *
  1615. * @param buf A valid pointer on a buffer.
  1616. *
  1617. * @return 24-bit value converted from little endian to host endian.
  1618. */
  1619. static inline uint32_t net_buf_remove_le24(struct net_buf *buf)
  1620. {
  1621. return net_buf_simple_remove_le24(&buf->b);
  1622. }
  1623. /**
  1624. * @brief Remove and convert 32 bits from the end of the buffer.
  1625. *
  1626. * Same idea as with net_buf_remove_mem(), but a helper for operating on
  1627. * 32-bit little endian data.
  1628. *
  1629. * @param buf A valid pointer on a buffer.
  1630. *
  1631. * @return 32-bit value converted from little endian to host endian.
  1632. */
  1633. static inline uint32_t net_buf_remove_le32(struct net_buf *buf)
  1634. {
  1635. return net_buf_simple_remove_le32(&buf->b);
  1636. }
  1637. /**
  1638. * @brief Remove and convert 32 bits from the end of the buffer.
  1639. *
  1640. * Same idea as with net_buf_remove_mem(), but a helper for operating on
  1641. * 32-bit big endian data.
  1642. *
  1643. * @param buf A valid pointer on a buffer
  1644. *
  1645. * @return 32-bit value converted from big endian to host endian.
  1646. */
  1647. static inline uint32_t net_buf_remove_be32(struct net_buf *buf)
  1648. {
  1649. return net_buf_simple_remove_be32(&buf->b);
  1650. }
  1651. /**
  1652. * @brief Remove and convert 48 bits from the end of the buffer.
  1653. *
  1654. * Same idea as with net_buf_remove_mem(), but a helper for operating on
  1655. * 48-bit little endian data.
  1656. *
  1657. * @param buf A valid pointer on a buffer.
  1658. *
  1659. * @return 48-bit value converted from little endian to host endian.
  1660. */
  1661. static inline uint64_t net_buf_remove_le48(struct net_buf *buf)
  1662. {
  1663. return net_buf_simple_remove_le48(&buf->b);
  1664. }
  1665. /**
  1666. * @brief Remove and convert 48 bits from the end of the buffer.
  1667. *
  1668. * Same idea as with net_buf_remove_mem(), but a helper for operating on
  1669. * 48-bit big endian data.
  1670. *
  1671. * @param buf A valid pointer on a buffer
  1672. *
  1673. * @return 48-bit value converted from big endian to host endian.
  1674. */
  1675. static inline uint64_t net_buf_remove_be48(struct net_buf *buf)
  1676. {
  1677. return net_buf_simple_remove_be48(&buf->b);
  1678. }
  1679. /**
  1680. * @brief Remove and convert 64 bits from the end of the buffer.
  1681. *
  1682. * Same idea as with net_buf_remove_mem(), but a helper for operating on
  1683. * 64-bit little endian data.
  1684. *
  1685. * @param buf A valid pointer on a buffer.
  1686. *
  1687. * @return 64-bit value converted from little endian to host endian.
  1688. */
  1689. static inline uint64_t net_buf_remove_le64(struct net_buf *buf)
  1690. {
  1691. return net_buf_simple_remove_le64(&buf->b);
  1692. }
  1693. /**
  1694. * @brief Remove and convert 64 bits from the end of the buffer.
  1695. *
  1696. * Same idea as with net_buf_remove_mem(), but a helper for operating on
  1697. * 64-bit big endian data.
  1698. *
  1699. * @param buf A valid pointer on a buffer
  1700. *
  1701. * @return 64-bit value converted from big endian to host endian.
  1702. */
  1703. static inline uint64_t net_buf_remove_be64(struct net_buf *buf)
  1704. {
  1705. return net_buf_simple_remove_be64(&buf->b);
  1706. }
  1707. /**
  1708. * @brief Prepare data to be added at the start of the buffer
  1709. *
  1710. * Modifies the data pointer and buffer length to account for more data
  1711. * in the beginning of the buffer.
  1712. *
  1713. * @param buf Buffer to update.
  1714. * @param len Number of bytes to add to the beginning.
  1715. *
  1716. * @return The new beginning of the buffer data.
  1717. */
  1718. static inline void *net_buf_push(struct net_buf *buf, size_t len)
  1719. {
  1720. return net_buf_simple_push(&buf->b, len);
  1721. }
  1722. /**
  1723. * @brief Copies the given number of bytes to the start of the buffer
  1724. *
  1725. * Modifies the data pointer and buffer length to account for more data
  1726. * in the beginning of the buffer.
  1727. *
  1728. * @param buf Buffer to update.
  1729. * @param mem Location of data to be added.
  1730. * @param len Length of data to be added.
  1731. *
  1732. * @return The new beginning of the buffer data.
  1733. */
  1734. static inline void *net_buf_push_mem(struct net_buf *buf, const void *mem,
  1735. size_t len)
  1736. {
  1737. return net_buf_simple_push_mem(&buf->b, mem, len);
  1738. }
  1739. /**
  1740. * @brief Push 8-bit value to the beginning of the buffer
  1741. *
  1742. * Adds 8-bit value the beginning of the buffer.
  1743. *
  1744. * @param buf Buffer to update.
  1745. * @param val 8-bit value to be pushed to the buffer.
  1746. */
  1747. static inline void net_buf_push_u8(struct net_buf *buf, uint8_t val)
  1748. {
  1749. net_buf_simple_push_u8(&buf->b, val);
  1750. }
  1751. /**
  1752. * @brief Push 16-bit value to the beginning of the buffer
  1753. *
  1754. * Adds 16-bit value in little endian format to the beginning of the
  1755. * buffer.
  1756. *
  1757. * @param buf Buffer to update.
  1758. * @param val 16-bit value to be pushed to the buffer.
  1759. */
  1760. static inline void net_buf_push_le16(struct net_buf *buf, uint16_t val)
  1761. {
  1762. net_buf_simple_push_le16(&buf->b, val);
  1763. }
  1764. /**
  1765. * @brief Push 16-bit value to the beginning of the buffer
  1766. *
  1767. * Adds 16-bit value in big endian format to the beginning of the
  1768. * buffer.
  1769. *
  1770. * @param buf Buffer to update.
  1771. * @param val 16-bit value to be pushed to the buffer.
  1772. */
  1773. static inline void net_buf_push_be16(struct net_buf *buf, uint16_t val)
  1774. {
  1775. net_buf_simple_push_be16(&buf->b, val);
  1776. }
  1777. /**
  1778. * @brief Push 24-bit value to the beginning of the buffer
  1779. *
  1780. * Adds 24-bit value in little endian format to the beginning of the
  1781. * buffer.
  1782. *
  1783. * @param buf Buffer to update.
  1784. * @param val 24-bit value to be pushed to the buffer.
  1785. */
  1786. static inline void net_buf_push_le24(struct net_buf *buf, uint32_t val)
  1787. {
  1788. net_buf_simple_push_le24(&buf->b, val);
  1789. }
  1790. /**
  1791. * @brief Push 24-bit value to the beginning of the buffer
  1792. *
  1793. * Adds 24-bit value in big endian format to the beginning of the
  1794. * buffer.
  1795. *
  1796. * @param buf Buffer to update.
  1797. * @param val 24-bit value to be pushed to the buffer.
  1798. */
  1799. static inline void net_buf_push_be24(struct net_buf *buf, uint32_t val)
  1800. {
  1801. net_buf_simple_push_be24(&buf->b, val);
  1802. }
  1803. /**
  1804. * @brief Push 32-bit value to the beginning of the buffer
  1805. *
  1806. * Adds 32-bit value in little endian format to the beginning of the
  1807. * buffer.
  1808. *
  1809. * @param buf Buffer to update.
  1810. * @param val 32-bit value to be pushed to the buffer.
  1811. */
  1812. static inline void net_buf_push_le32(struct net_buf *buf, uint32_t val)
  1813. {
  1814. net_buf_simple_push_le32(&buf->b, val);
  1815. }
  1816. /**
  1817. * @brief Push 32-bit value to the beginning of the buffer
  1818. *
  1819. * Adds 32-bit value in big endian format to the beginning of the
  1820. * buffer.
  1821. *
  1822. * @param buf Buffer to update.
  1823. * @param val 32-bit value to be pushed to the buffer.
  1824. */
  1825. static inline void net_buf_push_be32(struct net_buf *buf, uint32_t val)
  1826. {
  1827. net_buf_simple_push_be32(&buf->b, val);
  1828. }
  1829. /**
  1830. * @brief Push 48-bit value to the beginning of the buffer
  1831. *
  1832. * Adds 48-bit value in little endian format to the beginning of the
  1833. * buffer.
  1834. *
  1835. * @param buf Buffer to update.
  1836. * @param val 48-bit value to be pushed to the buffer.
  1837. */
  1838. static inline void net_buf_push_le48(struct net_buf *buf, uint64_t val)
  1839. {
  1840. net_buf_simple_push_le48(&buf->b, val);
  1841. }
  1842. /**
  1843. * @brief Push 48-bit value to the beginning of the buffer
  1844. *
  1845. * Adds 48-bit value in big endian format to the beginning of the
  1846. * buffer.
  1847. *
  1848. * @param buf Buffer to update.
  1849. * @param val 48-bit value to be pushed to the buffer.
  1850. */
  1851. static inline void net_buf_push_be48(struct net_buf *buf, uint64_t val)
  1852. {
  1853. net_buf_simple_push_be48(&buf->b, val);
  1854. }
  1855. /**
  1856. * @brief Push 64-bit value to the beginning of the buffer
  1857. *
  1858. * Adds 64-bit value in little endian format to the beginning of the
  1859. * buffer.
  1860. *
  1861. * @param buf Buffer to update.
  1862. * @param val 64-bit value to be pushed to the buffer.
  1863. */
  1864. static inline void net_buf_push_le64(struct net_buf *buf, uint64_t val)
  1865. {
  1866. net_buf_simple_push_le64(&buf->b, val);
  1867. }
  1868. /**
  1869. * @brief Push 64-bit value to the beginning of the buffer
  1870. *
  1871. * Adds 64-bit value in big endian format to the beginning of the
  1872. * buffer.
  1873. *
  1874. * @param buf Buffer to update.
  1875. * @param val 64-bit value to be pushed to the buffer.
  1876. */
  1877. static inline void net_buf_push_be64(struct net_buf *buf, uint64_t val)
  1878. {
  1879. net_buf_simple_push_be64(&buf->b, val);
  1880. }
  1881. /**
  1882. * @brief Remove data from the beginning of the buffer.
  1883. *
  1884. * Removes data from the beginning of the buffer by modifying the data
  1885. * pointer and buffer length.
  1886. *
  1887. * @param buf Buffer to update.
  1888. * @param len Number of bytes to remove.
  1889. *
  1890. * @return New beginning of the buffer data.
  1891. */
  1892. static inline void *net_buf_pull(struct net_buf *buf, size_t len)
  1893. {
  1894. return net_buf_simple_pull(&buf->b, len);
  1895. }
  1896. /**
  1897. * @brief Remove data from the beginning of the buffer.
  1898. *
  1899. * Removes data from the beginning of the buffer by modifying the data
  1900. * pointer and buffer length.
  1901. *
  1902. * @param buf Buffer to update.
  1903. * @param len Number of bytes to remove.
  1904. *
  1905. * @return Pointer to the old beginning of the buffer data.
  1906. */
  1907. static inline void *net_buf_pull_mem(struct net_buf *buf, size_t len)
  1908. {
  1909. return net_buf_simple_pull_mem(&buf->b, len);
  1910. }
  1911. /**
  1912. * @brief Remove a 8-bit value from the beginning of the buffer
  1913. *
  1914. * Same idea as with net_buf_pull(), but a helper for operating on
  1915. * 8-bit values.
  1916. *
  1917. * @param buf A valid pointer on a buffer.
  1918. *
  1919. * @return The 8-bit removed value
  1920. */
  1921. static inline uint8_t net_buf_pull_u8(struct net_buf *buf)
  1922. {
  1923. return net_buf_simple_pull_u8(&buf->b);
  1924. }
  1925. /**
  1926. * @brief Remove and convert 16 bits from the beginning of the buffer.
  1927. *
  1928. * Same idea as with net_buf_pull(), but a helper for operating on
  1929. * 16-bit little endian data.
  1930. *
  1931. * @param buf A valid pointer on a buffer.
  1932. *
  1933. * @return 16-bit value converted from little endian to host endian.
  1934. */
  1935. static inline uint16_t net_buf_pull_le16(struct net_buf *buf)
  1936. {
  1937. return net_buf_simple_pull_le16(&buf->b);
  1938. }
  1939. /**
  1940. * @brief Remove and convert 16 bits from the beginning of the buffer.
  1941. *
  1942. * Same idea as with net_buf_pull(), but a helper for operating on
  1943. * 16-bit big endian data.
  1944. *
  1945. * @param buf A valid pointer on a buffer.
  1946. *
  1947. * @return 16-bit value converted from big endian to host endian.
  1948. */
  1949. static inline uint16_t net_buf_pull_be16(struct net_buf *buf)
  1950. {
  1951. return net_buf_simple_pull_be16(&buf->b);
  1952. }
  1953. /**
  1954. * @brief Remove and convert 24 bits from the beginning of the buffer.
  1955. *
  1956. * Same idea as with net_buf_pull(), but a helper for operating on
  1957. * 24-bit little endian data.
  1958. *
  1959. * @param buf A valid pointer on a buffer.
  1960. *
  1961. * @return 24-bit value converted from little endian to host endian.
  1962. */
  1963. static inline uint32_t net_buf_pull_le24(struct net_buf *buf)
  1964. {
  1965. return net_buf_simple_pull_le24(&buf->b);
  1966. }
  1967. /**
  1968. * @brief Remove and convert 24 bits from the beginning of the buffer.
  1969. *
  1970. * Same idea as with net_buf_pull(), but a helper for operating on
  1971. * 24-bit big endian data.
  1972. *
  1973. * @param buf A valid pointer on a buffer.
  1974. *
  1975. * @return 24-bit value converted from big endian to host endian.
  1976. */
  1977. static inline uint32_t net_buf_pull_be24(struct net_buf *buf)
  1978. {
  1979. return net_buf_simple_pull_be24(&buf->b);
  1980. }
  1981. /**
  1982. * @brief Remove and convert 32 bits from the beginning of the buffer.
  1983. *
  1984. * Same idea as with net_buf_pull(), but a helper for operating on
  1985. * 32-bit little endian data.
  1986. *
  1987. * @param buf A valid pointer on a buffer.
  1988. *
  1989. * @return 32-bit value converted from little endian to host endian.
  1990. */
  1991. static inline uint32_t net_buf_pull_le32(struct net_buf *buf)
  1992. {
  1993. return net_buf_simple_pull_le32(&buf->b);
  1994. }
  1995. /**
  1996. * @brief Remove and convert 32 bits from the beginning of the buffer.
  1997. *
  1998. * Same idea as with net_buf_pull(), but a helper for operating on
  1999. * 32-bit big endian data.
  2000. *
  2001. * @param buf A valid pointer on a buffer
  2002. *
  2003. * @return 32-bit value converted from big endian to host endian.
  2004. */
  2005. static inline uint32_t net_buf_pull_be32(struct net_buf *buf)
  2006. {
  2007. return net_buf_simple_pull_be32(&buf->b);
  2008. }
  2009. /**
  2010. * @brief Remove and convert 48 bits from the beginning of the buffer.
  2011. *
  2012. * Same idea as with net_buf_pull(), but a helper for operating on
  2013. * 48-bit little endian data.
  2014. *
  2015. * @param buf A valid pointer on a buffer.
  2016. *
  2017. * @return 48-bit value converted from little endian to host endian.
  2018. */
  2019. static inline uint64_t net_buf_pull_le48(struct net_buf *buf)
  2020. {
  2021. return net_buf_simple_pull_le48(&buf->b);
  2022. }
  2023. /**
  2024. * @brief Remove and convert 48 bits from the beginning of the buffer.
  2025. *
  2026. * Same idea as with net_buf_pull(), but a helper for operating on
  2027. * 48-bit big endian data.
  2028. *
  2029. * @param buf A valid pointer on a buffer
  2030. *
  2031. * @return 48-bit value converted from big endian to host endian.
  2032. */
  2033. static inline uint64_t net_buf_pull_be48(struct net_buf *buf)
  2034. {
  2035. return net_buf_simple_pull_be48(&buf->b);
  2036. }
  2037. /**
  2038. * @brief Remove and convert 64 bits from the beginning of the buffer.
  2039. *
  2040. * Same idea as with net_buf_pull(), but a helper for operating on
  2041. * 64-bit little endian data.
  2042. *
  2043. * @param buf A valid pointer on a buffer.
  2044. *
  2045. * @return 64-bit value converted from little endian to host endian.
  2046. */
  2047. static inline uint64_t net_buf_pull_le64(struct net_buf *buf)
  2048. {
  2049. return net_buf_simple_pull_le64(&buf->b);
  2050. }
  2051. /**
  2052. * @brief Remove and convert 64 bits from the beginning of the buffer.
  2053. *
  2054. * Same idea as with net_buf_pull(), but a helper for operating on
  2055. * 64-bit big endian data.
  2056. *
  2057. * @param buf A valid pointer on a buffer
  2058. *
  2059. * @return 64-bit value converted from big endian to host endian.
  2060. */
  2061. static inline uint64_t net_buf_pull_be64(struct net_buf *buf)
  2062. {
  2063. return net_buf_simple_pull_be64(&buf->b);
  2064. }
  2065. /**
  2066. * @brief Check buffer tailroom.
  2067. *
  2068. * Check how much free space there is at the end of the buffer.
  2069. *
  2070. * @param buf A valid pointer on a buffer
  2071. *
  2072. * @return Number of bytes available at the end of the buffer.
  2073. */
  2074. static inline size_t net_buf_tailroom(struct net_buf *buf)
  2075. {
  2076. return net_buf_simple_tailroom(&buf->b);
  2077. }
  2078. /**
  2079. * @brief Check buffer headroom.
  2080. *
  2081. * Check how much free space there is in the beginning of the buffer.
  2082. *
  2083. * buf A valid pointer on a buffer
  2084. *
  2085. * @return Number of bytes available in the beginning of the buffer.
  2086. */
  2087. static inline size_t net_buf_headroom(struct net_buf *buf)
  2088. {
  2089. return net_buf_simple_headroom(&buf->b);
  2090. }
  2091. /**
  2092. * @brief Check maximum net_buf::len value.
  2093. *
  2094. * This value is depending on the number of bytes being reserved as headroom.
  2095. *
  2096. * @param buf A valid pointer on a buffer
  2097. *
  2098. * @return Number of bytes usable behind the net_buf::data pointer.
  2099. */
  2100. static inline uint16_t net_buf_max_len(struct net_buf *buf)
  2101. {
  2102. return net_buf_simple_max_len(&buf->b);
  2103. }
  2104. /**
  2105. * @brief Get the tail pointer for a buffer.
  2106. *
  2107. * Get a pointer to the end of the data in a buffer.
  2108. *
  2109. * @param buf Buffer.
  2110. *
  2111. * @return Tail pointer for the buffer.
  2112. */
  2113. static inline uint8_t *net_buf_tail(struct net_buf *buf)
  2114. {
  2115. return net_buf_simple_tail(&buf->b);
  2116. }
  2117. /**
  2118. * @brief Find the last fragment in the fragment list.
  2119. *
  2120. * @return Pointer to last fragment in the list.
  2121. */
  2122. struct net_buf *net_buf_frag_last(struct net_buf *frags);
  2123. /**
  2124. * @brief Insert a new fragment to a chain of bufs.
  2125. *
  2126. * Insert a new fragment into the buffer fragments list after the parent.
  2127. *
  2128. * Note: This function takes ownership of the fragment reference so the
  2129. * caller is not required to unref.
  2130. *
  2131. * @param parent Parent buffer/fragment.
  2132. * @param frag Fragment to insert.
  2133. */
  2134. void net_buf_frag_insert(struct net_buf *parent, struct net_buf *frag);
  2135. /**
  2136. * @brief Add a new fragment to the end of a chain of bufs.
  2137. *
  2138. * Append a new fragment into the buffer fragments list.
  2139. *
  2140. * Note: This function takes ownership of the fragment reference so the
  2141. * caller is not required to unref.
  2142. *
  2143. * @param head Head of the fragment chain.
  2144. * @param frag Fragment to add.
  2145. *
  2146. * @return New head of the fragment chain. Either head (if head
  2147. * was non-NULL) or frag (if head was NULL).
  2148. */
  2149. struct net_buf *net_buf_frag_add(struct net_buf *head, struct net_buf *frag);
  2150. /**
  2151. * @brief Delete existing fragment from a chain of bufs.
  2152. *
  2153. * @param parent Parent buffer/fragment, or NULL if there is no parent.
  2154. * @param frag Fragment to delete.
  2155. *
  2156. * @return Pointer to the buffer following the fragment, or NULL if it
  2157. * had no further fragments.
  2158. */
  2159. #if defined(CONFIG_NET_BUF_LOG)
  2160. struct net_buf *net_buf_frag_del_debug(struct net_buf *parent,
  2161. struct net_buf *frag,
  2162. const char *func, int line);
  2163. #define net_buf_frag_del(_parent, _frag) \
  2164. net_buf_frag_del_debug(_parent, _frag, __func__, __LINE__)
  2165. #else
  2166. struct net_buf *net_buf_frag_del(struct net_buf *parent, struct net_buf *frag);
  2167. #endif
  2168. /**
  2169. * @brief Copy bytes from net_buf chain starting at offset to linear buffer
  2170. *
  2171. * Copy (extract) @a len bytes from @a src net_buf chain, starting from @a
  2172. * offset in it, to a linear buffer @a dst. Return number of bytes actually
  2173. * copied, which may be less than requested, if net_buf chain doesn't have
  2174. * enough data, or destination buffer is too small.
  2175. *
  2176. * @param dst Destination buffer
  2177. * @param dst_len Destination buffer length
  2178. * @param src Source net_buf chain
  2179. * @param offset Starting offset to copy from
  2180. * @param len Number of bytes to copy
  2181. * @return number of bytes actually copied
  2182. */
  2183. size_t net_buf_linearize(void *dst, size_t dst_len,
  2184. struct net_buf *src, size_t offset, size_t len);
  2185. /**
  2186. * @typedef net_buf_allocator_cb
  2187. * @brief Network buffer allocator callback.
  2188. *
  2189. * @details The allocator callback is called when net_buf_append_bytes
  2190. * needs to allocate a new net_buf.
  2191. *
  2192. * @param timeout Affects the action taken should the net buf pool be empty.
  2193. * If K_NO_WAIT, then return immediately. If K_FOREVER, then
  2194. * wait as long as necessary. Otherwise, wait until the specified
  2195. * timeout.
  2196. * @param user_data The user data given in net_buf_append_bytes call.
  2197. * @return pointer to allocated net_buf or NULL on error.
  2198. */
  2199. typedef struct net_buf *(*net_buf_allocator_cb)(k_timeout_t timeout,
  2200. void *user_data);
  2201. /**
  2202. * @brief Append data to a list of net_buf
  2203. *
  2204. * @details Append data to a net_buf. If there is not enough space in the
  2205. * net_buf then more net_buf will be added, unless there are no free net_buf
  2206. * and timeout occurs. If not allocator is provided it attempts to allocate from
  2207. * the same pool as the original buffer.
  2208. *
  2209. * @param buf Network buffer.
  2210. * @param len Total length of input data
  2211. * @param value Data to be added
  2212. * @param timeout Timeout is passed to the net_buf allocator callback.
  2213. * @param allocate_cb When a new net_buf is required, use this callback.
  2214. * @param user_data A user data pointer to be supplied to the allocate_cb.
  2215. * This pointer is can be anything from a mem_pool or a net_pkt, the
  2216. * logic is left up to the allocate_cb function.
  2217. *
  2218. * @return Length of data actually added. This may be less than input
  2219. * length if other timeout than K_FOREVER was used, and there
  2220. * were no free fragments in a pool to accommodate all data.
  2221. */
  2222. size_t net_buf_append_bytes(struct net_buf *buf, size_t len,
  2223. const void *value, k_timeout_t timeout,
  2224. net_buf_allocator_cb allocate_cb, void *user_data);
  2225. /**
  2226. * @brief Skip N number of bytes in a net_buf
  2227. *
  2228. * @details Skip N number of bytes starting from fragment's offset. If the total
  2229. * length of data is placed in multiple fragments, this function will skip from
  2230. * all fragments until it reaches N number of bytes. Any fully skipped buffers
  2231. * are removed from the net_buf list.
  2232. *
  2233. * @param buf Network buffer.
  2234. * @param len Total length of data to be skipped.
  2235. *
  2236. * @return Pointer to the fragment or
  2237. * NULL and pos is 0 after successful skip,
  2238. * NULL and pos is 0xffff otherwise.
  2239. */
  2240. static inline struct net_buf *net_buf_skip(struct net_buf *buf, size_t len)
  2241. {
  2242. while (buf && len--) {
  2243. net_buf_pull_u8(buf);
  2244. if (!buf->len) {
  2245. buf = net_buf_frag_del(NULL, buf);
  2246. }
  2247. }
  2248. return buf;
  2249. }
  2250. /**
  2251. * @brief Calculate amount of bytes stored in fragments.
  2252. *
  2253. * Calculates the total amount of data stored in the given buffer and the
  2254. * fragments linked to it.
  2255. *
  2256. * @param buf Buffer to start off with.
  2257. *
  2258. * @return Number of bytes in the buffer and its fragments.
  2259. */
  2260. static inline size_t net_buf_frags_len(struct net_buf *buf)
  2261. {
  2262. size_t bytes = 0;
  2263. while (buf) {
  2264. bytes += buf->len;
  2265. buf = buf->frags;
  2266. }
  2267. return bytes;
  2268. }
  2269. /* Actions add start */
  2270. struct net_buf_pool_continue {
  2271. uint8_t init_flag;
  2272. uint16_t data_size;
  2273. uint8_t *data_pool;
  2274. uint8_t *alloc_pos;
  2275. uint16_t curr_used;
  2276. uint16_t max_used;
  2277. };
  2278. extern const struct net_buf_data_cb net_buf_continue_cb;
  2279. #define BT_BUF_POOL_ALLOC_DATA_DEFINE(_name, _size) \
  2280. static u8_t _net_data_##_name[ROUND_UP((_size), 4)] __net_buf_align __noinit; \
  2281. struct net_buf_pool_continue _name = { \
  2282. .init_flag = 0, \
  2283. .data_size = _size, \
  2284. .data_pool = _net_data_##_name, \
  2285. }
  2286. #define NET_BUF_POOL_CONTINUE_DEFINE(_name, _count, _size, _destroy, _alloc_data) \
  2287. static struct net_buf net_buf_##_name[_count] __noinit; \
  2288. static const struct net_buf_data_alloc net_buf_continue_alloc_##_name = {\
  2289. .cb = &net_buf_continue_cb, \
  2290. .alloc_data = _alloc_data, \
  2291. .data_size = _size, \
  2292. }; \
  2293. struct net_buf_pool _name __net_buf_align \
  2294. __in_section(_net_buf_pool, static, _name) = \
  2295. NET_BUF_POOL_INITIALIZER(_name, &net_buf_continue_alloc_##_name, \
  2296. net_buf_##_name, _count, _destroy)
  2297. #define BT_BUF_POOL_DEFINE(_name, _count, _size, _ud_size, _destroy, _alloc_data) \
  2298. BUILD_ASSERT(_ud_size <= CONFIG_NET_BUF_USER_DATA_SIZE); \
  2299. NET_BUF_POOL_CONTINUE_DEFINE(_name, _count, (_size + _ud_size), _destroy, _alloc_data)
  2300. /* Actions add end */
  2301. /**
  2302. * @}
  2303. */
  2304. #ifdef __cplusplus
  2305. }
  2306. #endif
  2307. #endif /* ZEPHYR_INCLUDE_NET_BUF_H_ */