• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /******************************************************************************
2  *
3  *  Copyright (C) 1999-2014 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  *
21  *  This file contains functions that interface with the NFC NCI transport.
22  *  On the receive side, it routes events to the appropriate handler
23  *  (callback). On the transmit side, it manages the command transmission.
24  *
25  ******************************************************************************/
26 #include <android-base/logging.h>
27 #include <android-base/stringprintf.h>
28 
29 #include "bt_types.h"
30 #include "nfc_api.h"
31 #include "nfc_int.h"
32 
33 using android::base::StringPrintf;
34 
35 /*******************************************************************************
36 **
37 ** Function         nfc_alloc_conn_cb
38 **
39 ** Description      This function is called to allocation a control block for
40 **                  NCI logical connection
41 **
42 ** Returns          The allocated control block or NULL
43 **
44 *******************************************************************************/
nfc_alloc_conn_cb(tNFC_CONN_CBACK * p_cback)45 tNFC_CONN_CB* nfc_alloc_conn_cb(tNFC_CONN_CBACK* p_cback) {
46   int xx, max = NCI_MAX_CONN_CBS;
47   tNFC_CONN_CB* p_conn_cb = nullptr;
48 
49   NFC_CHECK_MAX_CONN();
50   for (xx = 0; xx < max; xx++) {
51     if (nfc_cb.conn_cb[xx].conn_id == NFC_ILLEGAL_CONN_ID) {
52       nfc_cb.conn_cb[xx].conn_id =
53           NFC_PEND_CONN_ID; /* to indicate this cb is used */
54       p_conn_cb = &nfc_cb.conn_cb[xx];
55       p_conn_cb->p_cback = p_cback;
56       break;
57     }
58   }
59   return p_conn_cb;
60 }
61 
62 /*******************************************************************************
63 **
64 ** Function         nfc_set_conn_id
65 **
66 ** Description      This function is called to set the connection id to the
67 **                  connection control block and the id mapping table
68 **
69 ** Returns          void
70 **
71 *******************************************************************************/
nfc_set_conn_id(tNFC_CONN_CB * p_cb,uint8_t conn_id)72 void nfc_set_conn_id(tNFC_CONN_CB* p_cb, uint8_t conn_id) {
73   uint8_t handle;
74 
75   if (p_cb == nullptr) return;
76 
77   p_cb->conn_id = conn_id;
78   handle = (uint8_t)(p_cb - nfc_cb.conn_cb + 1);
79   nfc_cb.conn_id[conn_id] = handle;
80   LOG(VERBOSE) << StringPrintf("nfc_set_conn_id conn_id:%d, handle:%d", conn_id,
81                              handle);
82 }
83 
84 /*******************************************************************************
85 **
86 ** Function         nfc_find_conn_cb_by_handle
87 **
88 ** Description      This function is called to locate the control block for
89 **                  loopback test.
90 **
91 ** Returns          The loopback test control block or NULL
92 **
93 *******************************************************************************/
nfc_find_conn_cb_by_handle(uint8_t id)94 tNFC_CONN_CB* nfc_find_conn_cb_by_handle(uint8_t id) {
95   int xx;
96   tNFC_CONN_CB* p_conn_cb = nullptr;
97 
98   for (xx = 0; xx < NCI_MAX_CONN_CBS; xx++) {
99     if (nfc_cb.conn_cb[xx].id == id) {
100       p_conn_cb = &nfc_cb.conn_cb[xx];
101       break;
102     }
103   }
104   return p_conn_cb;
105 }
106 
107 /*******************************************************************************
108 **
109 ** Function         nfc_find_conn_cb_by_conn_id
110 **
111 ** Description      This function is called to locate the control block for
112 **                  the given connection id
113 **
114 ** Returns          The control block or NULL
115 **
116 *******************************************************************************/
nfc_find_conn_cb_by_conn_id(uint8_t conn_id)117 tNFC_CONN_CB* nfc_find_conn_cb_by_conn_id(uint8_t conn_id) {
118   tNFC_CONN_CB* p_conn_cb = nullptr;
119   uint8_t handle;
120   uint8_t id;
121   int xx;
122 
123   if (conn_id == NFC_PEND_CONN_ID) {
124     for (xx = 0; xx < NCI_MAX_CONN_CBS; xx++) {
125       if (nfc_cb.conn_cb[xx].conn_id == NFC_PEND_CONN_ID) {
126         p_conn_cb = &nfc_cb.conn_cb[xx];
127         break;
128       }
129     }
130   } else {
131     id = conn_id & NFC_CONN_ID_ID_MASK;
132     if (id < NFC_MAX_CONN_ID) {
133       handle = nfc_cb.conn_id[id];
134       if (handle > 0) p_conn_cb = &nfc_cb.conn_cb[handle - 1];
135     }
136   }
137 
138   return p_conn_cb;
139 }
140 
141 /*******************************************************************************
142 **
143 ** Function         nfc_free_conn_cb
144 **
145 ** Description      This function is called to free the control block and
146 **                  resources and id mapping table
147 **
148 ** Returns          void
149 **
150 *******************************************************************************/
nfc_free_conn_cb(tNFC_CONN_CB * p_cb)151 void nfc_free_conn_cb(tNFC_CONN_CB* p_cb) {
152   void* p_buf;
153 
154   if (p_cb == nullptr) return;
155 
156   while ((p_buf = GKI_dequeue(&p_cb->rx_q)) != nullptr) GKI_freebuf(p_buf);
157 
158   while ((p_buf = GKI_dequeue(&p_cb->tx_q)) != nullptr) GKI_freebuf(p_buf);
159 
160   if (p_cb->conn_id <= NFC_MAX_CONN_ID) {
161     nfc_cb.conn_id[p_cb->conn_id] = 0;
162   } else {
163     LOG(ERROR) << StringPrintf("invalid conn_id.");
164   }
165   p_cb->p_cback = nullptr;
166   p_cb->conn_id = NFC_ILLEGAL_CONN_ID;
167 }
168 
169 /*******************************************************************************
170 **
171 ** Function         nfc_reset_all_conn_cbs
172 **
173 ** Description      This function is called to free all the control blocks and
174 **                  resources and id mapping table
175 **
176 ** Returns          void
177 **
178 *******************************************************************************/
nfc_reset_all_conn_cbs(void)179 extern void nfc_reset_all_conn_cbs(void) {
180   int xx;
181   tNFC_CONN_CB* p_conn_cb = &nfc_cb.conn_cb[0];
182   tNFC_DEACTIVATE_DEVT deact = tNFC_DEACTIVATE_DEVT();
183 
184   deact.status = NFC_STATUS_NOT_INITIALIZED;
185   deact.type = NFC_DEACTIVATE_TYPE_IDLE;
186   deact.is_ntf = TRUE;
187   for (xx = 0; xx < NCI_MAX_CONN_CBS; xx++, p_conn_cb++) {
188     if (p_conn_cb->conn_id != NFC_ILLEGAL_CONN_ID) {
189       if (p_conn_cb->p_cback) {
190         tNFC_CONN nfc_conn;
191         nfc_conn.deactivate = deact;
192         (*p_conn_cb->p_cback)(p_conn_cb->conn_id, NFC_DEACTIVATE_CEVT,
193                               &nfc_conn);
194       }
195       nfc_free_conn_cb(p_conn_cb);
196     }
197   }
198 }
199