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