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