1 /*
2  * Copyright 2020 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #define LOG_TAG "btm"
18 
19 #include <cstdint>
20 #include "stack/btm/btm_int_types.h"  // tBTM_CB
21 #include "stack/include/rfcdefs.h"    // PORT_MAX_RFC_PORTS
22 
23 extern tBTM_CB btm_cb;
24 
25 /*******************************************************************************
26  *
27  * Function         BTM_AllocateSCN
28  *
29  * Description      Look through the Server Channel Numbers for a free one.
30  *
31  * Returns          Allocated SCN number or 0 if none.
32  *
33  ******************************************************************************/
BTM_AllocateSCN(void)34 uint8_t BTM_AllocateSCN(void) {
35   uint8_t x;
36   BTM_TRACE_DEBUG("BTM_AllocateSCN");
37 
38   // stack reserves scn 1 for HFP, HSP we still do the correct way
39   for (x = 1; x < PORT_MAX_RFC_PORTS; x++) {
40     if (!btm_cb.btm_scn[x]) {
41       btm_cb.btm_scn[x] = true;
42       return (x + 1);
43     }
44   }
45 
46   return (0); /* No free ports */
47 }
48 
49 /*******************************************************************************
50  *
51  * Function         BTM_TryAllocateSCN
52  *
53  * Description      Try to allocate a fixed server channel
54  *
55  * Returns          Returns true if server channel was available
56  *
57  ******************************************************************************/
58 
BTM_TryAllocateSCN(uint8_t scn)59 bool BTM_TryAllocateSCN(uint8_t scn) {
60   /* Make sure we don't exceed max port range.
61    * Stack reserves scn 1 for HFP, HSP we still do the correct way.
62    */
63   if ((scn >= PORT_MAX_RFC_PORTS) || (scn == 1) || (scn == 0)) return false;
64 
65   /* check if this port is available */
66   if (!btm_cb.btm_scn[scn - 1]) {
67     btm_cb.btm_scn[scn - 1] = true;
68     return true;
69   }
70 
71   return (false); /* Port was busy */
72 }
73 
74 /*******************************************************************************
75  *
76  * Function         BTM_FreeSCN
77  *
78  * Description      Free the specified SCN.
79  *
80  * Returns          true or false
81  *
82  ******************************************************************************/
BTM_FreeSCN(uint8_t scn)83 bool BTM_FreeSCN(uint8_t scn) {
84   BTM_TRACE_DEBUG("BTM_FreeSCN ");
85   if (scn <= PORT_MAX_RFC_PORTS && scn > 0) {
86     btm_cb.btm_scn[scn - 1] = false;
87     return (true);
88   } else {
89     return (false); /* Illegal SCN passed in */
90   }
91 }
92