nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 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 | |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 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 | ******************************************************************************/ |
nxf24591 | c1cbeab | 2018-02-21 17:32:26 +0530 | [diff] [blame] | 26 | #include <android-base/stringprintf.h> |
| 27 | #include <base/logging.h> |
| 28 | |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 29 | #include "bt_types.h" |
| 30 | #include "nfc_api.h" |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 31 | #include "nfc_int.h" |
| 32 | |
nxf24591 | c1cbeab | 2018-02-21 17:32:26 +0530 | [diff] [blame] | 33 | using android::base::StringPrintf; |
| 34 | |
| 35 | extern bool nfc_debug_enabled; |
| 36 | |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 37 | /******************************************************************************* |
| 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 | *******************************************************************************/ |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 47 | tNFC_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; |
nxf24591 | c1cbeab | 2018-02-21 17:32:26 +0530 | [diff] [blame] | 50 | |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 51 | NFC_CHECK_MAX_CONN(); |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 52 | 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; |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 59 | } |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 60 | } |
| 61 | return p_conn_cb; |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 62 | } |
| 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 | *******************************************************************************/ |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 74 | void nfc_set_conn_id(tNFC_CONN_CB* p_cb, uint8_t conn_id) { |
| 75 | uint8_t handle; |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 76 | |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 77 | if (p_cb == NULL) return; |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 78 | |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 79 | 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; |
nxf24591 | c1cbeab | 2018-02-21 17:32:26 +0530 | [diff] [blame] | 82 | DLOG_IF(INFO, nfc_debug_enabled) |
| 83 | << StringPrintf("nfc_set_conn_id conn_id:%d, handle:%d", conn_id, handle); |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 84 | } |
| 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 | *******************************************************************************/ |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 96 | tNFC_CONN_CB* nfc_find_conn_cb_by_handle(uint8_t id) { |
| 97 | int xx; |
| 98 | tNFC_CONN_CB* p_conn_cb = NULL; |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 99 | |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 100 | 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; |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 104 | } |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 105 | } |
| 106 | return p_conn_cb; |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 107 | } |
| 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 | *******************************************************************************/ |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 119 | tNFC_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; |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 124 | |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 125 | 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 | } |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 131 | } |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 132 | } 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]; |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 137 | } |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 138 | } |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 139 | |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 140 | return p_conn_cb; |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 141 | } |
| 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 | *******************************************************************************/ |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 153 | void nfc_free_conn_cb(tNFC_CONN_CB* p_cb) { |
| 154 | void* p_buf; |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 155 | |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 156 | if (p_cb == NULL) return; |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 157 | |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 158 | while ((p_buf = GKI_dequeue(&p_cb->rx_q)) != NULL) GKI_freebuf(p_buf); |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 159 | |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 160 | while ((p_buf = GKI_dequeue(&p_cb->tx_q)) != NULL) GKI_freebuf(p_buf); |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 161 | |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 162 | nfc_cb.conn_id[p_cb->conn_id] = 0; |
| 163 | p_cb->p_cback = NULL; |
| 164 | p_cb->conn_id = NFC_ILLEGAL_CONN_ID; |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 165 | } |
| 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 | *******************************************************************************/ |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 177 | extern 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; |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 181 | |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 182 | deact.status = NFC_STATUS_NOT_INITIALIZED; |
| 183 | deact.type = NFC_DEACTIVATE_TYPE_IDLE; |
nxf24591 | c1cbeab | 2018-02-21 17:32:26 +0530 | [diff] [blame] | 184 | deact.is_ntf = TRUE; |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 185 | for (xx = 0; xx < NCI_MAX_CONN_CBS; xx++, p_conn_cb++) { |
| 186 | if (p_conn_cb->conn_id != NFC_ILLEGAL_CONN_ID) { |
nxf24591 | c1cbeab | 2018-02-21 17:32:26 +0530 | [diff] [blame] | 187 | if (p_conn_cb->p_cback) { |
| 188 | tNFC_CONN nfc_conn; |
| 189 | nfc_conn.deactivate = deact; |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 190 | (*p_conn_cb->p_cback)(p_conn_cb->conn_id, NFC_DEACTIVATE_CEVT, |
nxf24591 | c1cbeab | 2018-02-21 17:32:26 +0530 | [diff] [blame] | 191 | &nfc_conn); |
| 192 | } |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 193 | nfc_free_conn_cb(p_conn_cb); |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 194 | } |
nxpandroid | 8f6d053 | 2017-07-12 18:25:30 +0530 | [diff] [blame] | 195 | } |
nxpandroid | c761165 | 2015-09-23 16:42:05 +0530 | [diff] [blame] | 196 | } |