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