• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /******************************************************************************
2  *
3  *  Copyright (C) 1999-2012 Broadcom Corporation
4  *
5  *  Licensed under the Apache License, Version 2.0 (the "License");
6  *  you may not use this file except in compliance with the License.
7  *  You may obtain a copy of the License at:
8  *
9  *  http://www.apache.org/licenses/LICENSE-2.0
10  *
11  *  Unless required by applicable law or agreed to in writing, software
12  *  distributed under the License is distributed on an "AS IS" BASIS,
13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *  See the License for the specific language governing permissions and
15  *  limitations under the License.
16  *
17  ******************************************************************************/
18 #include <android-base/logging.h>
19 #include <android-base/stringprintf.h>
20 #include <log/log.h>
21 
22 #include "gki_int.h"
23 
24 #if (GKI_NUM_TOTAL_BUF_POOLS > 16)
25 #error Number of pools out of range (16 Max)!
26 #endif
27 
28 #if (BTU_STACK_LITE_ENABLED == FALSE)
29 static void gki_add_to_pool_list(uint8_t pool_id);
30 static void gki_remove_from_pool_list(uint8_t pool_id);
31 #endif /*  BTU_STACK_LITE_ENABLED == FALSE */
32 
33 using android::base::StringPrintf;
34 
35 /*******************************************************************************
36 **
37 ** Function         gki_init_free_queue
38 **
39 ** Description      Internal function called at startup to initialize a free
40 **                  queue. It is called once for each free queue.
41 **
42 ** Returns          void
43 **
44 *******************************************************************************/
gki_init_free_queue(uint8_t id,uint16_t size,uint16_t total,void * p_mem)45 static void gki_init_free_queue(uint8_t id, uint16_t size, uint16_t total,
46                                 void* p_mem) {
47   uint16_t i;
48   uint16_t act_size;
49   BUFFER_HDR_T* hdr;
50   BUFFER_HDR_T* hdr1 = nullptr;
51   uint32_t* magic;
52   int32_t tempsize = size;
53   tGKI_COM_CB* p_cb = &gki_cb.com;
54 
55   /* Ensure an even number of longwords */
56   tempsize = (int32_t)ALIGN_POOL(size);
57   act_size = (uint16_t)(tempsize + BUFFER_PADDING_SIZE);
58 
59   /* Remember pool start and end addresses */
60   if (p_mem) {
61     p_cb->pool_start[id] = (uint8_t*)p_mem;
62     p_cb->pool_end[id] = (uint8_t*)p_mem + (act_size * total);
63   }
64 
65   p_cb->pool_size[id] = act_size;
66 
67   p_cb->freeq[id].size = (uint16_t)tempsize;
68   p_cb->freeq[id].total = total;
69   p_cb->freeq[id].cur_cnt = 0;
70   p_cb->freeq[id].max_cnt = 0;
71 
72   /* Initialize  index table */
73   if (p_mem) {
74     hdr = (BUFFER_HDR_T*)p_mem;
75     p_cb->freeq[id].p_first = hdr;
76     for (i = 0; i < total; i++) {
77       hdr->task_id = GKI_INVALID_TASK;
78       hdr->q_id = id;
79       hdr->status = BUF_STATUS_FREE;
80       magic = (uint32_t*)((uint8_t*)hdr + BUFFER_HDR_SIZE + tempsize);
81       *magic = MAGIC_NO;
82       hdr1 = hdr;
83       hdr = (BUFFER_HDR_T*)((uint8_t*)hdr + act_size);
84       hdr1->p_next = hdr;
85     }
86     if (hdr1 != nullptr) hdr = hdr1;
87     hdr->p_next = nullptr;
88     p_cb->freeq[id].p_last = hdr;
89   }
90   return;
91 }
92 
gki_alloc_free_queue(uint8_t id)93 static bool gki_alloc_free_queue(uint8_t id) {
94   FREE_QUEUE_T* Q;
95   tGKI_COM_CB* p_cb = &gki_cb.com;
96 
97   Q = &p_cb->freeq[p_cb->pool_list[id]];
98 
99   if (Q->p_first == nullptr) {
100     void* p_mem = GKI_os_malloc((Q->size + BUFFER_PADDING_SIZE) * Q->total);
101     if (p_mem) {
102       // re-initialize the queue with allocated memory
103       gki_init_free_queue(id, Q->size, Q->total, p_mem);
104       return true;
105     }
106     GKI_exception(GKI_ERROR_BUF_SIZE_TOOBIG,
107                   "gki_alloc_free_queue: Not enough memory");
108   }
109   return false;
110 }
111 
112 /*******************************************************************************
113 **
114 ** Function         gki_buffer_init
115 **
116 ** Description      Called once internally by GKI at startup to initialize all
117 **                  buffers and free buffer pools.
118 **
119 ** Returns          void
120 **
121 *******************************************************************************/
gki_buffer_init(void)122 void gki_buffer_init(void) {
123 #if (GKI_NUM_FIXED_BUF_POOLS > 0)
124   uint8_t i;
125 #endif
126   uint8_t tt, mb;
127   tGKI_COM_CB* p_cb = &gki_cb.com;
128 
129   /* Initialize mailboxes */
130   for (tt = 0; tt < GKI_MAX_TASKS; tt++) {
131     for (mb = 0; mb < NUM_TASK_MBOX; mb++) {
132       p_cb->OSTaskQFirst[tt][mb] = nullptr;
133       p_cb->OSTaskQLast[tt][mb] = nullptr;
134     }
135   }
136 
137   for (tt = 0; tt < GKI_NUM_TOTAL_BUF_POOLS; tt++) {
138     p_cb->pool_start[tt] = nullptr;
139     p_cb->pool_end[tt] = nullptr;
140     p_cb->pool_size[tt] = 0;
141 
142     p_cb->freeq[tt].p_first = nullptr;
143     p_cb->freeq[tt].p_last = nullptr;
144     p_cb->freeq[tt].size = 0;
145     p_cb->freeq[tt].total = 0;
146     p_cb->freeq[tt].cur_cnt = 0;
147     p_cb->freeq[tt].max_cnt = 0;
148   }
149 
150   /* Use default from target.h */
151   p_cb->pool_access_mask = GKI_DEF_BUFPOOL_PERM_MASK;
152 
153 #if (GKI_NUM_FIXED_BUF_POOLS > 0)
154   gki_init_free_queue(0, GKI_BUF0_SIZE, GKI_BUF0_MAX, p_cb->bufpool0);
155 #endif
156 
157 #if (GKI_NUM_FIXED_BUF_POOLS > 1)
158   gki_init_free_queue(1, GKI_BUF1_SIZE, GKI_BUF1_MAX, p_cb->bufpool1);
159 #endif
160 
161 #if (GKI_NUM_FIXED_BUF_POOLS > 2)
162   gki_init_free_queue(2, GKI_BUF2_SIZE, GKI_BUF2_MAX, p_cb->bufpool2);
163 #endif
164 
165 #if (GKI_NUM_FIXED_BUF_POOLS > 3)
166   gki_init_free_queue(3, GKI_BUF3_SIZE, GKI_BUF3_MAX, p_cb->bufpool3);
167 #endif
168 
169 #if (GKI_NUM_FIXED_BUF_POOLS > 4)
170   gki_init_free_queue(4, GKI_BUF4_SIZE, GKI_BUF4_MAX, p_cb->bufpool4);
171 #endif
172 
173 #if (GKI_NUM_FIXED_BUF_POOLS > 5)
174   gki_init_free_queue(5, GKI_BUF5_SIZE, GKI_BUF5_MAX, p_cb->bufpool5);
175 #endif
176 
177 #if (GKI_NUM_FIXED_BUF_POOLS > 6)
178   gki_init_free_queue(6, GKI_BUF6_SIZE, GKI_BUF6_MAX, p_cb->bufpool6);
179 #endif
180 
181 #if (GKI_NUM_FIXED_BUF_POOLS > 7)
182   gki_init_free_queue(7, GKI_BUF7_SIZE, GKI_BUF7_MAX, p_cb->bufpool7);
183 #endif
184 
185 #if (GKI_NUM_FIXED_BUF_POOLS > 8)
186   gki_init_free_queue(8, GKI_BUF8_SIZE, GKI_BUF8_MAX, p_cb->bufpool8);
187 #endif
188 
189 #if (GKI_NUM_FIXED_BUF_POOLS > 9)
190   gki_init_free_queue(9, GKI_BUF9_SIZE, GKI_BUF9_MAX, p_cb->bufpool9);
191 #endif
192 
193 #if (GKI_NUM_FIXED_BUF_POOLS > 10)
194   gki_init_free_queue(10, GKI_BUF10_SIZE, GKI_BUF10_MAX, p_cb->bufpool10);
195 #endif
196 
197 #if (GKI_NUM_FIXED_BUF_POOLS > 11)
198   gki_init_free_queue(11, GKI_BUF11_SIZE, GKI_BUF11_MAX, p_cb->bufpool11);
199 #endif
200 
201 #if (GKI_NUM_FIXED_BUF_POOLS > 12)
202   gki_init_free_queue(12, GKI_BUF12_SIZE, GKI_BUF12_MAX, p_cb->bufpool12);
203 #endif
204 
205 #if (GKI_NUM_FIXED_BUF_POOLS > 13)
206   gki_init_free_queue(13, GKI_BUF13_SIZE, GKI_BUF13_MAX, p_cb->bufpool13);
207 #endif
208 
209 #if (GKI_NUM_FIXED_BUF_POOLS > 14)
210   gki_init_free_queue(14, GKI_BUF14_SIZE, GKI_BUF14_MAX, p_cb->bufpool14);
211 #endif
212 
213 #if (GKI_NUM_FIXED_BUF_POOLS > 15)
214   gki_init_free_queue(15, GKI_BUF15_SIZE, GKI_BUF15_MAX, p_cb->bufpool15);
215 #endif
216 
217 /* add pools to the pool_list which is arranged in the order of size */
218 #if (GKI_NUM_FIXED_BUF_POOLS > 0)
219   for (i = 0; i < GKI_NUM_FIXED_BUF_POOLS; i++) {
220     p_cb->pool_list[i] = i;
221   }
222 #endif
223 
224   p_cb->curr_total_no_of_pools = GKI_NUM_FIXED_BUF_POOLS;
225 
226   return;
227 }
228 
229 /*******************************************************************************
230 **
231 ** Function         GKI_init_q
232 **
233 ** Description      Called by an application to initialize a buffer queue.
234 **
235 ** Returns          void
236 **
237 *******************************************************************************/
GKI_init_q(BUFFER_Q * p_q)238 void GKI_init_q(BUFFER_Q* p_q) {
239   p_q->p_first = p_q->p_last = nullptr;
240   p_q->count = 0;
241 
242   return;
243 }
244 
245 /*******************************************************************************
246 **
247 ** Function         GKI_getbuf
248 **
249 ** Description      Called by an application to get a free buffer which
250 **                  is of size greater or equal to the requested size.
251 **
252 **                  Note: This routine only takes buffers from public pools.
253 **                        It will not use any buffers from pools
254 **                        marked GKI_RESTRICTED_POOL.
255 **
256 ** Parameters       size - (input) number of bytes needed.
257 **
258 ** Returns          A pointer to the buffer, or NULL if none available
259 **
260 *******************************************************************************/
GKI_getbuf(uint16_t size)261 void* GKI_getbuf(uint16_t size) {
262   BUFFER_HDR_T* p_hdr;
263   FREE_QUEUE_T* Q;
264 
265 #if defined(DYN_ALLOC) || defined(FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION)
266   if (size == 0 || size > (USHRT_MAX - 3)) {
267     LOG(ERROR) << StringPrintf("%s: Requested size(%d) is invalid", __func__,
268                                size);
269     android_errorWriteLog(0x534e4554, "205729183");
270 #ifndef DYN_ALLOC
271     abort();
272 #else
273     return (nullptr);
274 #endif
275   }
276 
277   size = ALIGN_POOL(size);
278   size_t total_sz = size + sizeof(BUFFER_HDR_T)
279 #if (GKI_ENABLE_BUF_CORRUPTION_CHECK == TRUE)
280                     + sizeof(uint32_t);
281 #else
282       ;
283 #endif
284   p_hdr = (BUFFER_HDR_T*)GKI_os_malloc(total_sz);
285   if (!p_hdr) {
286     LOG(ERROR) << StringPrintf("%s: unable to allocate buffer!!!!!", __func__);
287     LOG(ERROR) << StringPrintf("%s: total_sz=%zu size=%d", __func__, total_sz,
288                                size);
289     abort();
290   }
291 
292   memset(p_hdr, 0, total_sz);
293 
294 #if (GKI_ENABLE_BUF_CORRUPTION_CHECK == TRUE)
295   *(uint32_t*)((uint8_t*)p_hdr + BUFFER_HDR_SIZE + size) = MAGIC_NO;
296 #endif
297   p_hdr->task_id = GKI_get_taskid();
298   p_hdr->status = BUF_STATUS_UNLINKED;
299   p_hdr->p_next = nullptr;
300   p_hdr->Type = 0;
301 
302   p_hdr->q_id = 0;
303   p_hdr->size = size;
304 
305   GKI_disable();
306   Q = &gki_cb.com.freeq[p_hdr->q_id];
307   if (++Q->cur_cnt > Q->max_cnt) Q->max_cnt = Q->cur_cnt;
308   GKI_enable();
309 
310   LOG(VERBOSE) << StringPrintf("%s: %p %d:%d", __func__,
311                                ((uint8_t*)p_hdr + BUFFER_HDR_SIZE), Q->cur_cnt,
312                                Q->max_cnt);
313   UNUSED(gki_alloc_free_queue);
314   return (void*)((uint8_t*)p_hdr + BUFFER_HDR_SIZE);
315 #else
316   uint8_t i;
317   tGKI_COM_CB* p_cb = &gki_cb.com;
318 
319   if (size == 0) {
320     GKI_exception(GKI_ERROR_BUF_SIZE_ZERO, "getbuf: Size is zero");
321     return (nullptr);
322   }
323 
324   /* Find the first buffer pool that is public that can hold the desired size */
325   for (i = 0; i < p_cb->curr_total_no_of_pools; i++) {
326     if (size <= p_cb->freeq[p_cb->pool_list[i]].size) break;
327   }
328 
329   if (i == p_cb->curr_total_no_of_pools) {
330     GKI_exception(GKI_ERROR_BUF_SIZE_TOOBIG, "getbuf: Size is too big");
331     return (nullptr);
332   }
333 
334   /* Make sure the buffers aren't disturbed til finished with allocation */
335   GKI_disable();
336 
337   /* search the public buffer pools that are big enough to hold the size
338    * until a free buffer is found */
339   for (; i < p_cb->curr_total_no_of_pools; i++) {
340     /* Only look at PUBLIC buffer pools (bypass RESTRICTED pools) */
341     if (((uint16_t)1 << p_cb->pool_list[i]) & p_cb->pool_access_mask) continue;
342 
343     Q = &p_cb->freeq[p_cb->pool_list[i]];
344     if (Q->cur_cnt < Q->total) {
345       if (Q->p_first == nullptr && gki_alloc_free_queue(i) != true) {
346         LOG(ERROR) << StringPrintf("%s: out of buffer", __func__);
347         GKI_enable();
348         return nullptr;
349       }
350 
351       if (Q->p_first == nullptr) {
352         /* gki_alloc_free_queue() failed to alloc memory */
353         LOG(ERROR) << StringPrintf("%s: fail alloc free queue", __func__);
354         GKI_enable();
355         return nullptr;
356       }
357 
358       p_hdr = Q->p_first;
359       Q->p_first = p_hdr->p_next;
360 
361       if (!Q->p_first) Q->p_last = nullptr;
362 
363       if (++Q->cur_cnt > Q->max_cnt) Q->max_cnt = Q->cur_cnt;
364 
365       GKI_enable();
366 
367       p_hdr->task_id = GKI_get_taskid();
368 
369       p_hdr->status = BUF_STATUS_UNLINKED;
370       p_hdr->p_next = nullptr;
371       p_hdr->Type = 0;
372       return ((void*)((uint8_t*)p_hdr + BUFFER_HDR_SIZE));
373     }
374   }
375 
376   LOG(ERROR) << StringPrintf("%s: unable to allocate buffer!!!!!", __func__);
377 
378   GKI_enable();
379 
380   return (nullptr);
381 #endif
382 }
383 
384 /*******************************************************************************
385 **
386 ** Function         GKI_getpoolbuf
387 **
388 ** Description      Called by an application to get a free buffer from
389 **                  a specific buffer pool.
390 **
391 **                  Note: If there are no more buffers available from the pool,
392 **                        the public buffers are searched for an available
393 **                        buffer.
394 **
395 ** Parameters       pool_id - (input) pool ID to get a buffer out of.
396 **
397 ** Returns          A pointer to the buffer, or NULL if none available
398 **
399 *******************************************************************************/
GKI_getpoolbuf(uint8_t pool_id)400 void* GKI_getpoolbuf(uint8_t pool_id) {
401 #if defined(DYN_ALLOC) || defined(FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION)
402   uint16_t size = 0;
403   switch (pool_id) {
404     // NFC_NCI_POOL_ID, NFC_RW_POOL_ID and NFC_CE_POOL_ID are all redefined to
405     // GKI_POOL_ID_2.
406     case GKI_POOL_ID_2:
407       size = GKI_BUF2_SIZE;
408       break;
409 
410     // LLCP_POOL_ID, GKI_MAX_BUF_SIZE_POOL_ID are redefined to GKI_POOL_ID_3.
411     case GKI_POOL_ID_3:
412       size = GKI_BUF3_SIZE;
413       break;
414 
415     default:
416       LOG(ERROR) << StringPrintf("%s: Unknown pool ID=%d", __func__, pool_id);
417 #ifndef DYN_ALLOC
418       abort();
419 #else
420       return (nullptr);
421 #endif
422       break;
423   }
424 
425   return GKI_getbuf(size);
426 #else
427   FREE_QUEUE_T* Q;
428   BUFFER_HDR_T* p_hdr;
429   tGKI_COM_CB* p_cb = &gki_cb.com;
430 
431   if (pool_id >= GKI_NUM_TOTAL_BUF_POOLS) return (nullptr);
432 
433   /* Make sure the buffers aren't disturbed til finished with allocation */
434   GKI_disable();
435 
436   Q = &p_cb->freeq[pool_id];
437   if (Q->cur_cnt < Q->total) {
438     if (Q->p_first == nullptr && gki_alloc_free_queue(pool_id) != true)
439       return nullptr;
440 
441     if (Q->p_first == nullptr) {
442       /* gki_alloc_free_queue() failed to alloc memory */
443       LOG(ERROR) << StringPrintf("%s: fail alloc free queue", __func__);
444       return nullptr;
445     }
446 
447     p_hdr = Q->p_first;
448     Q->p_first = p_hdr->p_next;
449 
450     if (!Q->p_first) Q->p_last = nullptr;
451 
452     if (++Q->cur_cnt > Q->max_cnt) Q->max_cnt = Q->cur_cnt;
453 
454     GKI_enable();
455 
456     p_hdr->task_id = GKI_get_taskid();
457 
458     p_hdr->status = BUF_STATUS_UNLINKED;
459     p_hdr->p_next = nullptr;
460     p_hdr->Type = 0;
461 
462     return ((void*)((uint8_t*)p_hdr + BUFFER_HDR_SIZE));
463   }
464 
465   /* If here, no buffers in the specified pool */
466   GKI_enable();
467 
468   /* try for free buffers in public pools */
469   return (GKI_getbuf(p_cb->freeq[pool_id].size));
470 #endif
471 }
472 
473 /*******************************************************************************
474 **
475 ** Function         GKI_freebuf
476 **
477 ** Description      Called by an application to return a buffer to the free
478 **                  pool.
479 **
480 ** Parameters       p_buf - (input) address of the beginning of a buffer.
481 **
482 ** Returns          void
483 **
484 *******************************************************************************/
GKI_freebuf(void * p_buf)485 void GKI_freebuf(void* p_buf) {
486   BUFFER_HDR_T* p_hdr;
487   FREE_QUEUE_T* Q;
488 
489 #if (GKI_ENABLE_BUF_CORRUPTION_CHECK == TRUE)
490   if (!p_buf || gki_chk_buf_damage(p_buf)) {
491     GKI_exception(GKI_ERROR_BUF_CORRUPTED, "Free - Buf Corrupted");
492     return;
493   }
494 #endif
495 
496   p_hdr = (BUFFER_HDR_T*)((uint8_t*)p_buf - BUFFER_HDR_SIZE);
497 
498   if (p_hdr->status != BUF_STATUS_UNLINKED) {
499     GKI_exception(GKI_ERROR_FREEBUF_BUF_LINKED, "Freeing Linked Buf");
500     return;
501   }
502 
503   if (p_hdr->q_id >= GKI_NUM_TOTAL_BUF_POOLS) {
504     GKI_exception(GKI_ERROR_FREEBUF_BAD_QID, "Bad Buf QId");
505     return;
506   }
507 
508 #if defined(DYN_ALLOC) || defined(FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION)
509   GKI_disable();
510   Q = &gki_cb.com.freeq[p_hdr->q_id];
511   if (Q->cur_cnt > 0) Q->cur_cnt--;
512   GKI_enable();
513 
514   GKI_os_free(p_hdr);
515 #else
516   GKI_disable();
517 
518   /*
519   ** Release the buffer
520   */
521   Q = &gki_cb.com.freeq[p_hdr->q_id];
522   if (Q->p_last)
523     Q->p_last->p_next = p_hdr;
524   else
525     Q->p_first = p_hdr;
526 
527   Q->p_last = p_hdr;
528   p_hdr->p_next = nullptr;
529   p_hdr->status = BUF_STATUS_FREE;
530   p_hdr->task_id = GKI_INVALID_TASK;
531   if (Q->cur_cnt > 0) Q->cur_cnt--;
532 
533   GKI_enable();
534 #endif
535 }
536 
537 /*******************************************************************************
538 **
539 ** Function         GKI_get_buf_size
540 **
541 ** Description      Called by an application to get the size of a buffer.
542 **
543 ** Parameters       p_buf - (input) address of the beginning of a buffer.
544 **
545 ** Returns          the size of the buffer
546 **
547 *******************************************************************************/
GKI_get_buf_size(void * p_buf)548 uint16_t GKI_get_buf_size(void* p_buf) {
549   BUFFER_HDR_T* p_hdr;
550 
551   p_hdr = (BUFFER_HDR_T*)((uint8_t*)p_buf - BUFFER_HDR_SIZE);
552 
553 #if defined(DYN_ALLOC) || defined(FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION)
554   return p_hdr->size;
555 #else
556   if ((uintptr_t)p_hdr & 1) return (0);
557 
558   if (p_hdr->q_id < GKI_NUM_TOTAL_BUF_POOLS) {
559     return (gki_cb.com.freeq[p_hdr->q_id].size);
560   }
561 
562   return (0);
563 #endif
564 }
565 
566 /*******************************************************************************
567 **
568 ** Function         gki_chk_buf_damage
569 **
570 ** Description      Called internally by OSS to check for buffer corruption.
571 **
572 ** Returns          TRUE if there is a problem, else FALSE
573 **
574 *******************************************************************************/
gki_chk_buf_damage(void * p_buf)575 bool gki_chk_buf_damage(void* p_buf) {
576 #if (GKI_ENABLE_BUF_CORRUPTION_CHECK == TRUE)
577 
578   uint32_t* magic;
579   magic = (uint32_t*)((uint8_t*)p_buf + GKI_get_buf_size(p_buf));
580 
581   if ((uintptr_t)magic & 1) return true;
582 
583   if (*magic == MAGIC_NO) return false;
584 
585   LOG(ERROR) << StringPrintf("%s: 0x%x %p", __func__, *magic, p_buf);
586   return true;
587 
588 #else
589   UNUSED(p_buf);
590   return false;
591 
592 #endif
593 }
594 
595 /*******************************************************************************
596 **
597 ** Function         GKI_send_msg
598 **
599 ** Description      Called by applications to send a buffer to a task
600 **
601 ** Returns          Nothing
602 **
603 *******************************************************************************/
GKI_send_msg(uint8_t task_id,uint8_t mbox,void * msg)604 void GKI_send_msg(uint8_t task_id, uint8_t mbox, void* msg) {
605   BUFFER_HDR_T* p_hdr;
606   tGKI_COM_CB* p_cb = &gki_cb.com;
607 
608   /* If task non-existant or not started, drop buffer */
609   if ((task_id >= GKI_MAX_TASKS) || (mbox >= NUM_TASK_MBOX) ||
610       (p_cb->OSRdyTbl[task_id] == TASK_DEAD)) {
611     GKI_exception(GKI_ERROR_SEND_MSG_BAD_DEST, "Sending to unknown dest");
612     GKI_freebuf(msg);
613     return;
614   }
615 
616 #if (GKI_ENABLE_BUF_CORRUPTION_CHECK == TRUE)
617   if (gki_chk_buf_damage(msg)) {
618     GKI_exception(GKI_ERROR_BUF_CORRUPTED, "Send - Buffer corrupted");
619     return;
620   }
621 #endif
622 
623   p_hdr = (BUFFER_HDR_T*)((uint8_t*)msg - BUFFER_HDR_SIZE);
624 
625   if (p_hdr->status != BUF_STATUS_UNLINKED) {
626     GKI_exception(GKI_ERROR_SEND_MSG_BUF_LINKED, "Send - buffer linked");
627     return;
628   }
629 
630   GKI_disable();
631 
632   if (p_cb->OSTaskQFirst[task_id][mbox])
633     p_cb->OSTaskQLast[task_id][mbox]->p_next = p_hdr;
634   else
635     p_cb->OSTaskQFirst[task_id][mbox] = p_hdr;
636 
637   p_cb->OSTaskQLast[task_id][mbox] = p_hdr;
638 
639   p_hdr->p_next = nullptr;
640   p_hdr->status = BUF_STATUS_QUEUED;
641   p_hdr->task_id = task_id;
642 
643   GKI_enable();
644 
645   GKI_send_event(task_id, (uint16_t)EVENT_MASK(mbox));
646 
647   return;
648 }
649 
650 /*******************************************************************************
651 **
652 ** Function         GKI_read_mbox
653 **
654 ** Description      Called by applications to read a buffer from one of
655 **                  the task mailboxes.  A task can only read its own mailbox.
656 **
657 ** Parameters:      mbox  - (input) mailbox ID to read (0, 1, 2, or 3)
658 **
659 ** Returns          NULL if the mailbox was empty, else the address of a buffer
660 **
661 *******************************************************************************/
GKI_read_mbox(uint8_t mbox)662 void* GKI_read_mbox(uint8_t mbox) {
663   uint8_t task_id = GKI_get_taskid();
664   void* p_buf = nullptr;
665   BUFFER_HDR_T* p_hdr;
666 
667   if ((task_id >= GKI_MAX_TASKS) || (mbox >= NUM_TASK_MBOX)) return (nullptr);
668 
669   GKI_disable();
670 
671   if (gki_cb.com.OSTaskQFirst[task_id][mbox]) {
672     p_hdr = gki_cb.com.OSTaskQFirst[task_id][mbox];
673     gki_cb.com.OSTaskQFirst[task_id][mbox] = p_hdr->p_next;
674 
675     p_hdr->p_next = nullptr;
676     p_hdr->status = BUF_STATUS_UNLINKED;
677 
678     p_buf = (uint8_t*)p_hdr + BUFFER_HDR_SIZE;
679   }
680 
681   GKI_enable();
682 
683   return (p_buf);
684 }
685 
686 /*******************************************************************************
687 **
688 ** Function         GKI_enqueue
689 **
690 ** Description      Enqueue a buffer at the tail of the queue
691 **
692 ** Parameters:      p_q  -  (input) pointer to a queue.
693 **                  p_buf - (input) address of the buffer to enqueue
694 **
695 ** Returns          void
696 **
697 *******************************************************************************/
GKI_enqueue(BUFFER_Q * p_q,void * p_buf)698 void GKI_enqueue(BUFFER_Q* p_q, void* p_buf) {
699   BUFFER_HDR_T* p_hdr;
700 
701 #if (GKI_ENABLE_BUF_CORRUPTION_CHECK == TRUE)
702   if (gki_chk_buf_damage(p_buf)) {
703     GKI_exception(GKI_ERROR_BUF_CORRUPTED, "Enqueue - Buffer corrupted");
704     return;
705   }
706 #endif
707 
708   p_hdr = (BUFFER_HDR_T*)((uint8_t*)p_buf - BUFFER_HDR_SIZE);
709 
710   if (p_hdr->status != BUF_STATUS_UNLINKED) {
711     GKI_exception(GKI_ERROR_ENQUEUE_BUF_LINKED, "Eneueue - buf already linked");
712     return;
713   }
714 
715   GKI_disable();
716 
717   /* Since the queue is exposed (C vs C++), keep the pointers in exposed format
718    */
719   if (p_q->p_first) {
720     BUFFER_HDR_T* p_last_hdr =
721         (BUFFER_HDR_T*)((uint8_t*)p_q->p_last - BUFFER_HDR_SIZE);
722     p_last_hdr->p_next = p_hdr;
723   } else
724     p_q->p_first = p_buf;
725 
726   p_q->p_last = p_buf;
727   p_q->count++;
728 
729   p_hdr->p_next = nullptr;
730   p_hdr->status = BUF_STATUS_QUEUED;
731 
732   GKI_enable();
733 
734   return;
735 }
736 
737 /*******************************************************************************
738 **
739 ** Function         GKI_enqueue_head
740 **
741 ** Description      Enqueue a buffer at the head of the queue
742 **
743 ** Parameters:      p_q  -  (input) pointer to a queue.
744 **                  p_buf - (input) address of the buffer to enqueue
745 **
746 ** Returns          void
747 **
748 *******************************************************************************/
GKI_enqueue_head(BUFFER_Q * p_q,void * p_buf)749 void GKI_enqueue_head(BUFFER_Q* p_q, void* p_buf) {
750   BUFFER_HDR_T* p_hdr;
751 
752 #if (GKI_ENABLE_BUF_CORRUPTION_CHECK == TRUE)
753   if (gki_chk_buf_damage(p_buf)) {
754     GKI_exception(GKI_ERROR_BUF_CORRUPTED, "Enqueue - Buffer corrupted");
755     return;
756   }
757 #endif
758 
759   p_hdr = (BUFFER_HDR_T*)((uint8_t*)p_buf - BUFFER_HDR_SIZE);
760 
761   if (p_hdr->status != BUF_STATUS_UNLINKED) {
762     GKI_exception(GKI_ERROR_ENQUEUE_BUF_LINKED,
763                   "Eneueue head - buf already linked");
764     return;
765   }
766 
767   GKI_disable();
768 
769   if (p_q->p_first) {
770     p_hdr->p_next = (BUFFER_HDR_T*)((uint8_t*)p_q->p_first - BUFFER_HDR_SIZE);
771     p_q->p_first = p_buf;
772   } else {
773     p_q->p_first = p_buf;
774     p_q->p_last = p_buf;
775     p_hdr->p_next = nullptr;
776   }
777   p_q->count++;
778 
779   p_hdr->status = BUF_STATUS_QUEUED;
780 
781   GKI_enable();
782 
783   return;
784 }
785 
786 /*******************************************************************************
787 **
788 ** Function         GKI_dequeue
789 **
790 ** Description      Dequeues a buffer from the head of a queue
791 **
792 ** Parameters:      p_q  - (input) pointer to a queue.
793 **
794 ** Returns          NULL if queue is empty, else buffer
795 **
796 *******************************************************************************/
GKI_dequeue(BUFFER_Q * p_q)797 void* GKI_dequeue(BUFFER_Q* p_q) {
798   BUFFER_HDR_T* p_hdr;
799 
800   GKI_disable();
801 
802   if (!p_q || !p_q->count) {
803     GKI_enable();
804     return (nullptr);
805   }
806 
807   p_hdr = (BUFFER_HDR_T*)((uint8_t*)p_q->p_first - BUFFER_HDR_SIZE);
808 
809   /* Keep buffers such that GKI header is invisible */
810   if (p_hdr->p_next)
811     p_q->p_first = ((uint8_t*)p_hdr->p_next + BUFFER_HDR_SIZE);
812   else {
813     p_q->p_first = nullptr;
814     p_q->p_last = nullptr;
815   }
816 
817   p_q->count--;
818 
819   p_hdr->p_next = nullptr;
820   p_hdr->status = BUF_STATUS_UNLINKED;
821 
822   GKI_enable();
823 
824   return ((uint8_t*)p_hdr + BUFFER_HDR_SIZE);
825 }
826 
827 /*******************************************************************************
828 **
829 ** Function         GKI_remove_from_queue
830 **
831 ** Description      Dequeue a buffer from the middle of the queue
832 **
833 ** Parameters:      p_q  - (input) pointer to a queue.
834 **                  p_buf - (input) address of the buffer to enqueue
835 **
836 ** Returns          NULL if queue is empty, else buffer
837 **
838 *******************************************************************************/
GKI_remove_from_queue(BUFFER_Q * p_q,void * p_buf)839 void* GKI_remove_from_queue(BUFFER_Q* p_q, void* p_buf) {
840   BUFFER_HDR_T* p_prev;
841   BUFFER_HDR_T* p_buf_hdr;
842 
843   GKI_disable();
844 
845   if (p_buf == p_q->p_first) {
846     GKI_enable();
847     return (GKI_dequeue(p_q));
848   }
849 
850   p_buf_hdr = (BUFFER_HDR_T*)((uint8_t*)p_buf - BUFFER_HDR_SIZE);
851   p_prev = (BUFFER_HDR_T*)((uint8_t*)p_q->p_first - BUFFER_HDR_SIZE);
852 
853   for (; p_prev; p_prev = p_prev->p_next) {
854     /* If the previous points to this one, move the pointers around */
855     if (p_prev->p_next == p_buf_hdr) {
856       p_prev->p_next = p_buf_hdr->p_next;
857 
858       /* If we are removing the last guy in the queue, update p_last */
859       if (p_buf == p_q->p_last) p_q->p_last = p_prev + 1;
860 
861       /* One less in the queue */
862       p_q->count--;
863 
864       /* The buffer is now unlinked */
865       p_buf_hdr->p_next = nullptr;
866       p_buf_hdr->status = BUF_STATUS_UNLINKED;
867 
868       GKI_enable();
869       return (p_buf);
870     }
871   }
872 
873   GKI_enable();
874   return (nullptr);
875 }
876 
877 /*******************************************************************************
878 **
879 ** Function         GKI_getfirst
880 **
881 ** Description      Return a pointer to the first buffer in a queue
882 **
883 ** Parameters:      p_q  - (input) pointer to a queue.
884 **
885 ** Returns          NULL if queue is empty, else buffer address
886 **
887 *******************************************************************************/
GKI_getfirst(BUFFER_Q * p_q)888 void* GKI_getfirst(BUFFER_Q* p_q) { return (p_q->p_first); }
889 
890 /*******************************************************************************
891 **
892 ** Function         GKI_getlast
893 **
894 ** Description      Return a pointer to the last buffer in a queue
895 **
896 ** Parameters:      p_q  - (input) pointer to a queue.
897 **
898 ** Returns          NULL if queue is empty, else buffer address
899 **
900 *******************************************************************************/
GKI_getlast(BUFFER_Q * p_q)901 void* GKI_getlast(BUFFER_Q* p_q) { return (p_q->p_last); }
902 
903 /*******************************************************************************
904 **
905 ** Function         GKI_getnext
906 **
907 ** Description      Return a pointer to the next buffer in a queue
908 **
909 ** Parameters:      p_buf - (input) pointer to the buffer to find the next one
910 **                                  from.
911 **
912 ** Returns          NULL if no more buffers in the queue, else next buffer
913 **                  address
914 **
915 *******************************************************************************/
GKI_getnext(void * p_buf)916 void* GKI_getnext(void* p_buf) {
917   BUFFER_HDR_T* p_hdr;
918 
919   p_hdr = (BUFFER_HDR_T*)((uint8_t*)p_buf - BUFFER_HDR_SIZE);
920 
921   if (p_hdr->p_next)
922     return ((uint8_t*)p_hdr->p_next + BUFFER_HDR_SIZE);
923   else
924     return (nullptr);
925 }
926 
927 /*******************************************************************************
928 **
929 ** Function         GKI_queue_is_empty
930 **
931 ** Description      Check the status of a queue.
932 **
933 ** Parameters:      p_q  - (input) pointer to a queue.
934 **
935 ** Returns          TRUE if queue is empty, else FALSE
936 **
937 *******************************************************************************/
GKI_queue_is_empty(BUFFER_Q * p_q)938 bool GKI_queue_is_empty(BUFFER_Q* p_q) { return ((bool)(p_q->count == 0)); }
939 
940 /*******************************************************************************
941 **
942 ** Function         GKI_find_buf_start
943 **
944 ** Description      This function is called with an address inside a buffer,
945 **                  and returns the start address ofthe buffer.
946 **
947 **                  The buffer should be one allocated from one of GKI's pools.
948 **
949 ** Parameters:      p_user_area - (input) address of anywhere in a GKI buffer.
950 **
951 ** Returns          void * - Address of the beginning of the specified buffer if
952 **                           successful, otherwise NULL if unsuccessful
953 **
954 *******************************************************************************/
GKI_find_buf_start(void * p_user_area)955 void* GKI_find_buf_start(void* p_user_area) {
956   uint16_t xx, size;
957   uint32_t yy;
958   tGKI_COM_CB* p_cb = &gki_cb.com;
959   uint8_t* p_ua = (uint8_t*)p_user_area;
960 
961   for (xx = 0; xx < GKI_NUM_TOTAL_BUF_POOLS; xx++) {
962     if ((p_ua > p_cb->pool_start[xx]) && (p_ua < p_cb->pool_end[xx])) {
963       yy = (uint32_t)(p_ua - p_cb->pool_start[xx]);
964 
965       size = p_cb->pool_size[xx];
966 
967       yy = (yy / size) * size;
968 
969       return ((void*)(p_cb->pool_start[xx] + yy + sizeof(BUFFER_HDR_T)));
970     }
971   }
972 
973   /* If here, invalid address - not in one of our buffers */
974   GKI_exception(GKI_ERROR_BUF_SIZE_ZERO, "GKI_get_buf_start:: bad addr");
975 
976   return (nullptr);
977 }
978 
979 /********************************************************
980  * The following functions are not needed for light stack
981  *********************************************************/
982 #ifndef BTU_STACK_LITE_ENABLED
983 #define BTU_STACK_LITE_ENABLED FALSE
984 #endif
985 
986 #if (BTU_STACK_LITE_ENABLED == FALSE)
987 
988 /*******************************************************************************
989 **
990 ** Function         GKI_set_pool_permission
991 **
992 ** Description      This function is called to set or change the permissions for
993 **                  the specified pool ID.
994 **
995 ** Parameters       pool_id - (input) pool ID to be set or changed
996 **                  permission - (input) GKI_PUBLIC_POOL or GKI_RESTRICTED_POOL
997 **
998 ** Returns          GKI_SUCCESS if successful
999 **                  GKI_INVALID_POOL if unsuccessful
1000 **
1001 *******************************************************************************/
GKI_set_pool_permission(uint8_t pool_id,uint8_t permission)1002 uint8_t GKI_set_pool_permission(uint8_t pool_id, uint8_t permission) {
1003   tGKI_COM_CB* p_cb = &gki_cb.com;
1004 
1005   if (pool_id < GKI_NUM_TOTAL_BUF_POOLS) {
1006     if (permission == GKI_RESTRICTED_POOL)
1007       p_cb->pool_access_mask =
1008           (uint16_t)(p_cb->pool_access_mask | (1 << pool_id));
1009 
1010     else /* mark the pool as public */
1011       p_cb->pool_access_mask =
1012           (uint16_t)(p_cb->pool_access_mask & ~(1 << pool_id));
1013 
1014     return (GKI_SUCCESS);
1015   } else
1016     return (GKI_INVALID_POOL);
1017 }
1018 
1019 /*******************************************************************************
1020 **
1021 ** Function         gki_add_to_pool_list
1022 **
1023 ** Description      Adds pool to the pool list which is arranged in the
1024 **                  order of size
1025 **
1026 ** Returns          void
1027 **
1028 *******************************************************************************/
gki_add_to_pool_list(uint8_t pool_id)1029 static void gki_add_to_pool_list(uint8_t pool_id) {
1030   int32_t i, j;
1031   tGKI_COM_CB* p_cb = &gki_cb.com;
1032 
1033   /* Find the position where the specified pool should be inserted into the list
1034    */
1035   for (i = 0; i < p_cb->curr_total_no_of_pools; i++) {
1036     if (p_cb->freeq[pool_id].size <= p_cb->freeq[p_cb->pool_list[i]].size)
1037       break;
1038   }
1039 
1040   /* Insert the new buffer pool ID into the list of pools */
1041   for (j = p_cb->curr_total_no_of_pools; j > i; j--) {
1042     p_cb->pool_list[j] = p_cb->pool_list[j - 1];
1043   }
1044 
1045   p_cb->pool_list[i] = pool_id;
1046 
1047   return;
1048 }
1049 
1050 /*******************************************************************************
1051 **
1052 ** Function         gki_remove_from_pool_list
1053 **
1054 ** Description      Removes pool from the pool list. Called when a pool is
1055 **                  deleted
1056 **
1057 ** Returns          void
1058 **
1059 *******************************************************************************/
gki_remove_from_pool_list(uint8_t pool_id)1060 static void gki_remove_from_pool_list(uint8_t pool_id) {
1061   tGKI_COM_CB* p_cb = &gki_cb.com;
1062   uint8_t i;
1063 
1064   for (i = 0; i < p_cb->curr_total_no_of_pools; i++) {
1065     if (pool_id == p_cb->pool_list[i]) break;
1066   }
1067 
1068   while (i < (p_cb->curr_total_no_of_pools - 1)) {
1069     p_cb->pool_list[i] = p_cb->pool_list[i + 1];
1070     i++;
1071   }
1072 
1073   return;
1074 }
1075 
1076 /*******************************************************************************
1077 **
1078 ** Function         GKI_poolcount
1079 **
1080 ** Description      Called by an application to get the total number of buffers
1081 **                  in the specified buffer pool.
1082 **
1083 ** Parameters       pool_id - (input) pool ID to get the free count of.
1084 **
1085 ** Returns          the total number of buffers in the pool
1086 **
1087 *******************************************************************************/
GKI_poolcount(uint8_t pool_id)1088 uint16_t GKI_poolcount(uint8_t pool_id) {
1089   if (pool_id >= GKI_NUM_TOTAL_BUF_POOLS) return (0);
1090 
1091   return (gki_cb.com.freeq[pool_id].total);
1092 }
1093 
1094 /*******************************************************************************
1095 **
1096 ** Function         GKI_poolfreecount
1097 **
1098 ** Description      Called by an application to get the number of free buffers
1099 **                  in the specified buffer pool.
1100 **
1101 ** Parameters       pool_id - (input) pool ID to get the free count of.
1102 **
1103 ** Returns          the number of free buffers in the pool
1104 **
1105 *******************************************************************************/
GKI_poolfreecount(uint8_t pool_id)1106 uint16_t GKI_poolfreecount(uint8_t pool_id) {
1107   FREE_QUEUE_T* Q;
1108 
1109   if (pool_id >= GKI_NUM_TOTAL_BUF_POOLS) return (0);
1110 
1111   Q = &gki_cb.com.freeq[pool_id];
1112 
1113   return ((uint16_t)(Q->total - Q->cur_cnt));
1114 }
1115 
1116 /*******************************************************************************
1117 **
1118 ** Function         GKI_change_buf_owner
1119 **
1120 ** Description      Called to change the task ownership of a buffer.
1121 **
1122 ** Parameters:      p_buf   - (input) pointer to the buffer
1123 **                  task_id - (input) task id to change ownership to
1124 **
1125 ** Returns          void
1126 **
1127 *******************************************************************************/
GKI_change_buf_owner(void * p_buf,uint8_t task_id)1128 void GKI_change_buf_owner(void* p_buf, uint8_t task_id) {
1129   BUFFER_HDR_T* p_hdr = (BUFFER_HDR_T*)((uint8_t*)p_buf - BUFFER_HDR_SIZE);
1130 
1131   p_hdr->task_id = task_id;
1132 
1133   return;
1134 }
1135 
1136 #if (GKI_SEND_MSG_FROM_ISR == TRUE)
1137 /*******************************************************************************
1138 **
1139 ** Function         GKI_isend_msg
1140 **
1141 ** Description      Called from interrupt context to send a buffer to a task
1142 **
1143 ** Returns          Nothing
1144 **
1145 *******************************************************************************/
GKI_isend_msg(uint8_t task_id,uint8_t mbox,void * msg)1146 void GKI_isend_msg(uint8_t task_id, uint8_t mbox, void* msg) {
1147   BUFFER_HDR_T* p_hdr;
1148   tGKI_COM_CB* p_cb = &gki_cb.com;
1149 
1150   /* If task non-existant or not started, drop buffer */
1151   if ((task_id >= GKI_MAX_TASKS) || (mbox >= NUM_TASK_MBOX) ||
1152       (p_cb->OSRdyTbl[task_id] == TASK_DEAD)) {
1153     GKI_exception(GKI_ERROR_SEND_MSG_BAD_DEST, "Sending to unknown dest");
1154     GKI_freebuf(msg);
1155     return;
1156   }
1157 
1158 #if (GKI_ENABLE_BUF_CORRUPTION_CHECK == TRUE)
1159   if (gki_chk_buf_damage(msg)) {
1160     GKI_exception(GKI_ERROR_BUF_CORRUPTED, "Send - Buffer corrupted");
1161     return;
1162   }
1163 #endif
1164 
1165 #if (GKI_ENABLE_OWNER_CHECK == TRUE)
1166   if (gki_chk_buf_owner(msg)) {
1167     GKI_exception(GKI_ERROR_NOT_BUF_OWNER, "Send by non-owner");
1168     return;
1169   }
1170 #endif
1171 
1172   p_hdr = (BUFFER_HDR_T*)((uint8_t*)msg - BUFFER_HDR_SIZE);
1173 
1174   if (p_hdr->status != BUF_STATUS_UNLINKED) {
1175     GKI_exception(GKI_ERROR_SEND_MSG_BUF_LINKED, "Send - buffer linked");
1176     return;
1177   }
1178 
1179   if (p_cb->OSTaskQFirst[task_id][mbox])
1180     p_cb->OSTaskQLast[task_id][mbox]->p_next = p_hdr;
1181   else
1182     p_cb->OSTaskQFirst[task_id][mbox] = p_hdr;
1183 
1184   p_cb->OSTaskQLast[task_id][mbox] = p_hdr;
1185 
1186   p_hdr->p_next = NULL;
1187   p_hdr->status = BUF_STATUS_QUEUED;
1188   p_hdr->task_id = task_id;
1189 
1190   GKI_isend_event(task_id, (uint16_t)EVENT_MASK(mbox));
1191 
1192   return;
1193 }
1194 #endif
1195 
1196 /*******************************************************************************
1197 **
1198 ** Function         GKI_create_pool
1199 **
1200 ** Description      Called by applications to create a buffer pool.
1201 **
1202 ** Parameters:      size - (input) length (in bytes) of each buffer in the pool
1203 **                  count - (input) number of buffers to allocate for the pool
1204 **                  permission - (input) restricted or public access?
1205 **                                      (GKI_PUBLIC_POOL or GKI_RESTRICTED_POOL)
1206 **                  p_mem_pool - (input) pointer to an OS memory pool, NULL if
1207 **                                       not provided
1208 **
1209 ** Returns          the buffer pool ID, which should be used in calls to
1210 **                  GKI_getpoolbuf(). If a pool could not be created, this
1211 **                  function returns 0xff.
1212 **
1213 *******************************************************************************/
GKI_create_pool(uint16_t size,uint16_t count,uint8_t permission,void * p_mem_pool)1214 uint8_t GKI_create_pool(uint16_t size, uint16_t count, uint8_t permission,
1215                         void* p_mem_pool) {
1216   uint8_t xx;
1217   uint32_t mem_needed;
1218   int32_t tempsize = size;
1219   tGKI_COM_CB* p_cb = &gki_cb.com;
1220 
1221   /* First make sure the size of each pool has a valid size with room for the
1222    * header info */
1223   if (size > MAX_USER_BUF_SIZE) return (GKI_INVALID_POOL);
1224 
1225   /* First, look for an unused pool */
1226   for (xx = 0; xx < GKI_NUM_TOTAL_BUF_POOLS; xx++) {
1227     if (!p_cb->pool_start[xx]) break;
1228   }
1229 
1230   if (xx == GKI_NUM_TOTAL_BUF_POOLS) return (GKI_INVALID_POOL);
1231 
1232   /* Ensure an even number of longwords */
1233   tempsize = (int32_t)ALIGN_POOL(size);
1234 
1235   mem_needed = (tempsize + BUFFER_PADDING_SIZE) * count;
1236 
1237   if (!p_mem_pool) p_mem_pool = GKI_os_malloc(mem_needed);
1238 
1239   if (p_mem_pool) {
1240     /* Initialize the new pool */
1241     gki_init_free_queue(xx, size, count, p_mem_pool);
1242     gki_add_to_pool_list(xx);
1243     (void)GKI_set_pool_permission(xx, permission);
1244     p_cb->curr_total_no_of_pools++;
1245 
1246     return (xx);
1247   } else
1248     return (GKI_INVALID_POOL);
1249 }
1250 
1251 /*******************************************************************************
1252 **
1253 ** Function         GKI_delete_pool
1254 **
1255 ** Description      Called by applications to delete a buffer pool.  The
1256 **                  function calls the operating specific function to free the
1257 **                  actual memory. An exception is generated if an error is
1258 **                  detected.
1259 **
1260 ** Parameters:      pool_id - (input) Id of the poll being deleted.
1261 **
1262 ** Returns          void
1263 **
1264 *******************************************************************************/
GKI_delete_pool(uint8_t pool_id)1265 void GKI_delete_pool(uint8_t pool_id) {
1266   FREE_QUEUE_T* Q;
1267   tGKI_COM_CB* p_cb = &gki_cb.com;
1268 
1269   if ((pool_id >= GKI_NUM_TOTAL_BUF_POOLS) || (!p_cb->pool_start[pool_id]))
1270     return;
1271 
1272   GKI_disable();
1273   Q = &p_cb->freeq[pool_id];
1274 
1275   if (!Q->cur_cnt) {
1276     Q->size = 0;
1277     Q->total = 0;
1278     Q->cur_cnt = 0;
1279     Q->max_cnt = 0;
1280     Q->p_first = nullptr;
1281     Q->p_last = nullptr;
1282 
1283     GKI_os_free(p_cb->pool_start[pool_id]);
1284 
1285     p_cb->pool_start[pool_id] = nullptr;
1286     p_cb->pool_end[pool_id] = nullptr;
1287     p_cb->pool_size[pool_id] = 0;
1288 
1289     gki_remove_from_pool_list(pool_id);
1290     p_cb->curr_total_no_of_pools--;
1291   } else
1292     GKI_exception(GKI_ERROR_DELETE_POOL_BAD_QID, "Deleting bad pool");
1293 
1294   GKI_enable();
1295 
1296   return;
1297 }
1298 
1299 #endif /*  BTU_STACK_LITE_ENABLED == FALSE */
1300 
1301 /*******************************************************************************
1302 **
1303 ** Function         GKI_get_pool_bufsize
1304 **
1305 ** Description      Called by an application to get the size of buffers in a
1306 **                  pool
1307 **
1308 ** Parameters       Pool ID.
1309 **
1310 ** Returns          the size of buffers in the pool
1311 **
1312 *******************************************************************************/
GKI_get_pool_bufsize(uint8_t pool_id)1313 uint16_t GKI_get_pool_bufsize(uint8_t pool_id) {
1314 #if defined(DYN_ALLOC) || defined(FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION)
1315   uint16_t size = 0;
1316   switch (pool_id) {
1317     case GKI_POOL_ID_0:
1318       size = GKI_BUF0_SIZE;
1319       break;
1320     case GKI_POOL_ID_1:
1321       size = GKI_BUF1_SIZE;
1322       break;
1323     case GKI_POOL_ID_2:
1324       size = GKI_BUF2_SIZE;
1325       break;
1326     case GKI_POOL_ID_3:
1327       size = GKI_BUF3_SIZE;
1328       break;
1329       /* Here could be more pool ids, but they are not used in the current
1330        * implementation */
1331     default:
1332       LOG(ERROR) << StringPrintf("%s: Unknown pool ID=%d", __func__, pool_id);
1333       return (0);
1334       break;
1335   }
1336   return (size);
1337 #else
1338   if (pool_id < GKI_NUM_TOTAL_BUF_POOLS)
1339     return (gki_cb.com.freeq[pool_id].size);
1340 
1341   return (0);
1342 #endif
1343 }
1344 
1345 /*******************************************************************************
1346 **
1347 ** Function         GKI_poolutilization
1348 **
1349 ** Description      Called by an application to get the buffer utilization
1350 **                  in the specified buffer pool.
1351 **
1352 ** Parameters       pool_id - (input) pool ID to get the free count of.
1353 **
1354 ** Returns          % of buffers used from 0 to 100
1355 **
1356 *******************************************************************************/
GKI_poolutilization(uint8_t pool_id)1357 uint16_t GKI_poolutilization(uint8_t pool_id) {
1358   FREE_QUEUE_T* Q;
1359 
1360   if (pool_id >= GKI_NUM_TOTAL_BUF_POOLS) return (100);
1361 
1362   Q = &gki_cb.com.freeq[pool_id];
1363 
1364   if (Q->total == 0) return (100);
1365 
1366   return ((Q->cur_cnt * 100) / Q->total);
1367 }
1368