avct_ccb.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /******************************************************************************
  2. *
  3. * Copyright (C) 2003-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. /******************************************************************************
  19. *
  20. * This module contains functions which operate on the AVCTP connection
  21. * control block.
  22. *
  23. ******************************************************************************/
  24. #include <string.h>
  25. #include "stack/bt_types.h"
  26. #include "common/bt_target.h"
  27. #include "stack/avct_api.h"
  28. #include "avct_int.h"
  29. #if (defined(AVCT_INCLUDED) && AVCT_INCLUDED == TRUE)
  30. /*******************************************************************************
  31. **
  32. ** Function avct_ccb_alloc
  33. **
  34. ** Description Allocate a connection control block; copy parameters to ccb.
  35. **
  36. **
  37. ** Returns pointer to the ccb, or NULL if none could be allocated.
  38. **
  39. *******************************************************************************/
  40. tAVCT_CCB *avct_ccb_alloc(tAVCT_CC *p_cc)
  41. {
  42. tAVCT_CCB *p_ccb = &avct_cb.ccb[0];
  43. int i;
  44. for (i = 0; i < AVCT_NUM_CONN; i++, p_ccb++) {
  45. if (!p_ccb->allocated) {
  46. p_ccb->allocated = AVCT_ALOC_LCB;
  47. memcpy(&p_ccb->cc, p_cc, sizeof(tAVCT_CC));
  48. AVCT_TRACE_DEBUG("avct_ccb_alloc %d", i);
  49. break;
  50. }
  51. }
  52. if (i == AVCT_NUM_CONN) {
  53. /* out of ccbs */
  54. p_ccb = NULL;
  55. AVCT_TRACE_WARNING("Out of ccbs");
  56. }
  57. return p_ccb;
  58. }
  59. /*******************************************************************************
  60. **
  61. ** Function avct_ccb_dealloc
  62. **
  63. ** Description Deallocate a connection control block and call application
  64. ** callback.
  65. **
  66. **
  67. ** Returns void.
  68. **
  69. *******************************************************************************/
  70. void avct_ccb_dealloc(tAVCT_CCB *p_ccb, UINT8 event, UINT16 result, BD_ADDR bd_addr)
  71. {
  72. tAVCT_CTRL_CBACK *p_cback = p_ccb->cc.p_ctrl_cback;
  73. AVCT_TRACE_DEBUG("avct_ccb_dealloc %d", avct_ccb_to_idx(p_ccb));
  74. #if (AVCT_BROWSE_INCLUDED == TRUE)
  75. if (p_ccb->p_bcb == NULL) {
  76. memset(p_ccb, 0, sizeof(tAVCT_CCB));
  77. } else {
  78. /* control channel is down, but the browsing channel is still connected 0 disconnect it now */
  79. avct_bcb_event(p_ccb->p_bcb, AVCT_LCB_UL_UNBIND_EVT, (tAVCT_LCB_EVT *) &p_ccb);
  80. p_ccb->p_lcb = NULL;
  81. }
  82. #else
  83. memset(p_ccb, 0, sizeof(tAVCT_CCB));
  84. #endif
  85. if (event != AVCT_NO_EVT) {
  86. (*p_cback)(avct_ccb_to_idx(p_ccb), event, result, bd_addr);
  87. }
  88. }
  89. /*******************************************************************************
  90. **
  91. ** Function avct_ccb_to_idx
  92. **
  93. ** Description Given a pointer to an ccb, return its index.
  94. **
  95. **
  96. ** Returns Index of ccb.
  97. **
  98. *******************************************************************************/
  99. UINT8 avct_ccb_to_idx(tAVCT_CCB *p_ccb)
  100. {
  101. /* use array arithmetic to determine index */
  102. return (UINT8) (p_ccb - avct_cb.ccb);
  103. }
  104. /*******************************************************************************
  105. **
  106. ** Function avct_ccb_by_idx
  107. **
  108. ** Description Return ccb pointer based on ccb index (or handle).
  109. **
  110. **
  111. ** Returns pointer to the ccb, or NULL if none found.
  112. **
  113. *******************************************************************************/
  114. tAVCT_CCB *avct_ccb_by_idx(UINT8 idx)
  115. {
  116. tAVCT_CCB *p_ccb;
  117. /* verify index */
  118. if (idx < AVCT_NUM_CONN) {
  119. p_ccb = &avct_cb.ccb[idx];
  120. /* verify ccb is allocated */
  121. if (!p_ccb->allocated) {
  122. p_ccb = NULL;
  123. AVCT_TRACE_WARNING("ccb %d not allocated", idx);
  124. }
  125. } else {
  126. p_ccb = NULL;
  127. AVCT_TRACE_WARNING("No ccb for idx %d", idx);
  128. }
  129. return p_ccb;
  130. }
  131. #endif /* #if (defined(AVCT_INCLUDED) && AVCT_INCLUDED == TRUE) */