blob: 93fa7c6053bf0061365665712129ed649295ae6f [file] [log] [blame]
nxpandroidc7611652015-09-23 16:42:05 +05301/******************************************************************************
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
nxpandroidc7611652015-09-23 16:42:05 +053019/******************************************************************************
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 ******************************************************************************/
nxf24591c1cbeab2018-02-21 17:32:26 +053026#include <android-base/stringprintf.h>
27#include <base/logging.h>
28
nxpandroidc7611652015-09-23 16:42:05 +053029#include "bt_types.h"
30#include "nfc_api.h"
nxpandroidc7611652015-09-23 16:42:05 +053031#include "nfc_int.h"
32
nxf24591c1cbeab2018-02-21 17:32:26 +053033using android::base::StringPrintf;
34
35extern bool nfc_debug_enabled;
36
nxpandroidc7611652015-09-23 16:42:05 +053037/*******************************************************************************
38**
39** Function nfc_alloc_conn_cb
40**
41** Description This function is called to allocation a control block for
42** NCI logical connection
43**
44** Returns The allocated control block or NULL
45**
46*******************************************************************************/
nxpandroid8f6d0532017-07-12 18:25:30 +053047tNFC_CONN_CB* nfc_alloc_conn_cb(tNFC_CONN_CBACK* p_cback) {
48 int xx, max = NCI_MAX_CONN_CBS;
49 tNFC_CONN_CB* p_conn_cb = NULL;
nxf24591c1cbeab2018-02-21 17:32:26 +053050
nxpandroid8f6d0532017-07-12 18:25:30 +053051 NFC_CHECK_MAX_CONN();
nxpandroid8f6d0532017-07-12 18:25:30 +053052 for (xx = 0; xx < max; xx++) {
53 if (nfc_cb.conn_cb[xx].conn_id == NFC_ILLEGAL_CONN_ID) {
54 nfc_cb.conn_cb[xx].conn_id =
55 NFC_PEND_CONN_ID; /* to indicate this cb is used */
56 p_conn_cb = &nfc_cb.conn_cb[xx];
57 p_conn_cb->p_cback = p_cback;
58 break;
nxpandroidc7611652015-09-23 16:42:05 +053059 }
nxpandroid8f6d0532017-07-12 18:25:30 +053060 }
61 return p_conn_cb;
nxpandroidc7611652015-09-23 16:42:05 +053062}
63
64/*******************************************************************************
65**
66** Function nfc_set_conn_id
67**
68** Description This function is called to set the connection id to the
69** connection control block and the id mapping table
70**
71** Returns void
72**
73*******************************************************************************/
nxpandroid8f6d0532017-07-12 18:25:30 +053074void nfc_set_conn_id(tNFC_CONN_CB* p_cb, uint8_t conn_id) {
75 uint8_t handle;
nxpandroidc7611652015-09-23 16:42:05 +053076
nxpandroid8f6d0532017-07-12 18:25:30 +053077 if (p_cb == NULL) return;
nxpandroidc7611652015-09-23 16:42:05 +053078
nxpandroid8f6d0532017-07-12 18:25:30 +053079 p_cb->conn_id = conn_id;
80 handle = (uint8_t)(p_cb - nfc_cb.conn_cb + 1);
81 nfc_cb.conn_id[conn_id] = handle;
nxf24591c1cbeab2018-02-21 17:32:26 +053082 DLOG_IF(INFO, nfc_debug_enabled)
83 << StringPrintf("nfc_set_conn_id conn_id:%d, handle:%d", conn_id, handle);
nxpandroidc7611652015-09-23 16:42:05 +053084}
85
86/*******************************************************************************
87**
88** Function nfc_find_conn_cb_by_handle
89**
90** Description This function is called to locate the control block for
91** loopback test.
92**
93** Returns The loopback test control block or NULL
94**
95*******************************************************************************/
nxpandroid8f6d0532017-07-12 18:25:30 +053096tNFC_CONN_CB* nfc_find_conn_cb_by_handle(uint8_t id) {
97 int xx;
98 tNFC_CONN_CB* p_conn_cb = NULL;
nxpandroidc7611652015-09-23 16:42:05 +053099
nxpandroid8f6d0532017-07-12 18:25:30 +0530100 for (xx = 0; xx < NCI_MAX_CONN_CBS; xx++) {
101 if (nfc_cb.conn_cb[xx].id == id) {
102 p_conn_cb = &nfc_cb.conn_cb[xx];
103 break;
nxpandroidc7611652015-09-23 16:42:05 +0530104 }
nxpandroid8f6d0532017-07-12 18:25:30 +0530105 }
106 return p_conn_cb;
nxpandroidc7611652015-09-23 16:42:05 +0530107}
108
109/*******************************************************************************
110**
111** Function nfc_find_conn_cb_by_conn_id
112**
113** Description This function is called to locate the control block for
114** the given connection id
115**
116** Returns The control block or NULL
117**
118*******************************************************************************/
nxpandroid8f6d0532017-07-12 18:25:30 +0530119tNFC_CONN_CB* nfc_find_conn_cb_by_conn_id(uint8_t conn_id) {
120 tNFC_CONN_CB* p_conn_cb = NULL;
121 uint8_t handle;
122 uint8_t id;
123 int xx;
nxpandroidc7611652015-09-23 16:42:05 +0530124
nxpandroid8f6d0532017-07-12 18:25:30 +0530125 if (conn_id == NFC_PEND_CONN_ID) {
126 for (xx = 0; xx < NCI_MAX_CONN_CBS; xx++) {
127 if (nfc_cb.conn_cb[xx].conn_id == NFC_PEND_CONN_ID) {
128 p_conn_cb = &nfc_cb.conn_cb[xx];
129 break;
130 }
nxpandroidc7611652015-09-23 16:42:05 +0530131 }
nxpandroid8f6d0532017-07-12 18:25:30 +0530132 } else {
133 id = conn_id & NFC_CONN_ID_ID_MASK;
134 if (id < NFC_MAX_CONN_ID) {
135 handle = nfc_cb.conn_id[id];
136 if (handle > 0) p_conn_cb = &nfc_cb.conn_cb[handle - 1];
nxpandroidc7611652015-09-23 16:42:05 +0530137 }
nxpandroid8f6d0532017-07-12 18:25:30 +0530138 }
nxpandroidc7611652015-09-23 16:42:05 +0530139
nxpandroid8f6d0532017-07-12 18:25:30 +0530140 return p_conn_cb;
nxpandroidc7611652015-09-23 16:42:05 +0530141}
142
143/*******************************************************************************
144**
145** Function nfc_free_conn_cb
146**
147** Description This function is called to free the control block and
148** resources and id mapping table
149**
150** Returns void
151**
152*******************************************************************************/
nxpandroid8f6d0532017-07-12 18:25:30 +0530153void nfc_free_conn_cb(tNFC_CONN_CB* p_cb) {
154 void* p_buf;
nxpandroidc7611652015-09-23 16:42:05 +0530155
nxpandroid8f6d0532017-07-12 18:25:30 +0530156 if (p_cb == NULL) return;
nxpandroidc7611652015-09-23 16:42:05 +0530157
nxpandroid8f6d0532017-07-12 18:25:30 +0530158 while ((p_buf = GKI_dequeue(&p_cb->rx_q)) != NULL) GKI_freebuf(p_buf);
nxpandroidc7611652015-09-23 16:42:05 +0530159
nxpandroid8f6d0532017-07-12 18:25:30 +0530160 while ((p_buf = GKI_dequeue(&p_cb->tx_q)) != NULL) GKI_freebuf(p_buf);
nxpandroidc7611652015-09-23 16:42:05 +0530161
nxpandroid8f6d0532017-07-12 18:25:30 +0530162 nfc_cb.conn_id[p_cb->conn_id] = 0;
163 p_cb->p_cback = NULL;
164 p_cb->conn_id = NFC_ILLEGAL_CONN_ID;
nxpandroidc7611652015-09-23 16:42:05 +0530165}
166
167/*******************************************************************************
168**
169** Function nfc_reset_all_conn_cbs
170**
171** Description This function is called to free all the control blocks and
172** resources and id mapping table
173**
174** Returns void
175**
176*******************************************************************************/
nxpandroid8f6d0532017-07-12 18:25:30 +0530177extern void nfc_reset_all_conn_cbs(void) {
178 int xx;
179 tNFC_CONN_CB* p_conn_cb = &nfc_cb.conn_cb[0];
180 tNFC_DEACTIVATE_DEVT deact;
nxpandroidc7611652015-09-23 16:42:05 +0530181
nxpandroid8f6d0532017-07-12 18:25:30 +0530182 deact.status = NFC_STATUS_NOT_INITIALIZED;
183 deact.type = NFC_DEACTIVATE_TYPE_IDLE;
nxf24591c1cbeab2018-02-21 17:32:26 +0530184 deact.is_ntf = TRUE;
nxpandroid8f6d0532017-07-12 18:25:30 +0530185 for (xx = 0; xx < NCI_MAX_CONN_CBS; xx++, p_conn_cb++) {
186 if (p_conn_cb->conn_id != NFC_ILLEGAL_CONN_ID) {
nxf24591c1cbeab2018-02-21 17:32:26 +0530187 if (p_conn_cb->p_cback) {
188 tNFC_CONN nfc_conn;
189 nfc_conn.deactivate = deact;
nxpandroid8f6d0532017-07-12 18:25:30 +0530190 (*p_conn_cb->p_cback)(p_conn_cb->conn_id, NFC_DEACTIVATE_CEVT,
nxf24591c1cbeab2018-02-21 17:32:26 +0530191 &nfc_conn);
192 }
nxpandroid8f6d0532017-07-12 18:25:30 +0530193 nfc_free_conn_cb(p_conn_cb);
nxpandroidc7611652015-09-23 16:42:05 +0530194 }
nxpandroid8f6d0532017-07-12 18:25:30 +0530195 }
nxpandroidc7611652015-09-23 16:42:05 +0530196}