• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /******************************************************************************
2  *
3  *  Copyright (C) 2009-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 the main NFC Upper Layer internal definitions and
22  *  functions.
23  *
24  ******************************************************************************/
25 
26 #ifndef NFC_INT_H_
27 #define NFC_INT_H_
28 
29 #include "gki.h"
30 #include "nci_defs.h"
31 #include "nfc_api.h"
32 #include "nfc_target.h"
33 
34 #ifdef __cplusplus
35 extern "C" {
36 #endif
37 
38 /****************************************************************************
39 ** Internal NFC constants and definitions
40 ****************************************************************************/
41 
42 /****************************************************************************
43 ** NFC_TASK definitions
44 ****************************************************************************/
45 
46 /* NFC_TASK event masks */
47 #define NFC_TASK_EVT_TRANSPORT_READY EVENT_MASK(APPL_EVT_0)
48 
49 /* NFC Timer events */
50 #define NFC_TTYPE_NCI_WAIT_RSP 0
51 #define NFC_TTYPE_WAIT_2_DEACTIVATE 1
52 #define NFC_WAIT_RSP_RAW_VS 0x02
53 
54 #define NFC_TTYPE_LLCP_LINK_MANAGER 100
55 #define NFC_TTYPE_LLCP_LINK_INACT 101
56 #define NFC_TTYPE_LLCP_DATA_LINK 102
57 #define NFC_TTYPE_LLCP_DELAY_FIRST_PDU 103
58 #define NFC_TTYPE_RW_T1T_RESPONSE 104
59 #define NFC_TTYPE_RW_T2T_RESPONSE 105
60 #define NFC_TTYPE_RW_T3T_RESPONSE 106
61 #define NFC_TTYPE_RW_T4T_RESPONSE 107
62 #define NFC_TTYPE_RW_I93_RESPONSE 108
63 #define NFC_TTYPE_CE_T4T_UPDATE 109
64 /* added for p2p prio logic timer */
65 #define NFC_TTYPE_P2P_PRIO_RESPONSE 110
66 /* added for p2p prio logic clenaup */
67 #define NFC_TTYPE_P2P_PRIO_LOGIC_CLEANUP 111
68 #define NFC_TTYPE_VS_BASE 200
69 
70 /* NFC Task event messages */
71 
72 enum {
73   NFC_STATE_NONE,                /* not start up yet                         */
74   NFC_STATE_W4_HAL_OPEN,         /* waiting for HAL_NFC_OPEN_CPLT_EVT        */
75   NFC_STATE_CORE_INIT,           /* sending CORE_RESET and CORE_INIT         */
76   NFC_STATE_W4_POST_INIT_CPLT,   /* waiting for HAL_NFC_POST_INIT_CPLT_EVT   */
77   NFC_STATE_IDLE,                /* normal operation (discovery state)       */
78   NFC_STATE_OPEN,                /* NFC link is activated                    */
79   NFC_STATE_CLOSING,             /* de-activating                            */
80   NFC_STATE_W4_HAL_CLOSE,        /* waiting for HAL_NFC_POST_INIT_CPLT_EVT   */
81   NFC_STATE_NFCC_POWER_OFF_SLEEP /* NFCC is power-off sleep mode             */
82 };
83 typedef uint8_t tNFC_STATE;
84 
85 /* DM P2P Priority event type */
86 enum {
87   NFA_DM_P2P_PRIO_RSP = 0x01, /* P2P priority event from RSP   */
88   NFA_DM_P2P_PRIO_NTF         /* P2P priority event from NTF   */
89 };
90 
91 /* NFC control block flags */
92 /* NFC_Deactivate () is called and the NCI cmd is not sent   */
93 #define NFC_FL_DEACTIVATING 0x0001
94 /* restarting NFCC after PowerOffSleep          */
95 #define NFC_FL_RESTARTING 0x0002
96 /* enterning power off sleep mode               */
97 #define NFC_FL_POWER_OFF_SLEEP 0x0004
98 /* Power cycle NFCC                             */
99 #define NFC_FL_POWER_CYCLE_NFCC 0x0008
100 /* HAL requested control on NCI command window  */
101 #define NFC_FL_CONTROL_REQUESTED 0x0010
102 /* NCI command window is on the HAL side        */
103 #define NFC_FL_CONTROL_GRANTED 0x0020
104 /* NCI command window is on the HAL side        */
105 #define NFC_FL_DISCOVER_PENDING 0x0040
106 /* NFC_FL_CONTROL_REQUESTED on HAL request      */
107 #define NFC_FL_HAL_REQUESTED 0x0080
108 
109 #define NFC_PEND_CONN_ID 0xFE
110 #define NFC_CONN_ID_INT_MASK 0xF0
111 #define NFC_CONN_ID_ID_MASK NCI_CID_MASK
112 /* set num_buff to this for no flow control */
113 #define NFC_CONN_NO_FC 0xFF
114 #define NFC_NCI_CONN_NO_FC 0xFF
115 
116 #if (NFC_RW_ONLY == FALSE)
117 /* only allow the entries that the NFCC can support */
118 #define NFC_CHECK_MAX_CONN()                          \
119   {                                                   \
120     if (max > nfc_cb.max_conn) max = nfc_cb.max_conn; \
121   }
122 #else
123 #define NFC_CHECK_MAX_CONN()
124 #endif
125 
126 typedef struct {
127   tNFC_CONN_CBACK* p_cback; /* the callback function to receive the data */
128   BUFFER_Q tx_q;        /* transmit queue                                   */
129   BUFFER_Q rx_q;        /* receive queue                                    */
130   uint8_t id;           /* NFCEE ID or RF Discovery ID or NFC_TEST_ID       */
131   uint8_t act_protocol; /* the active protocol on this logical connection   */
132   uint8_t act_interface; /* the active interface on this logical connection   */
133   uint8_t conn_id;      /* the connection id assigned by NFCC for this conn */
134   uint8_t buff_size;    /* the max buffer size for this connection.     .   */
135   uint8_t num_buff;     /* num of buffers left to send on this connection   */
136   uint8_t init_credits; /* initial num of buffer credits                    */
137 } tNFC_CONN_CB;
138 
139 /* This data type is for NFC task to send a NCI VS command to NCIT task */
140 typedef struct {
141   NFC_HDR bt_hdr;         /* the NCI command          */
142   tNFC_VS_CBACK* p_cback; /* the callback function to receive RSP   */
143 } tNFC_NCI_VS_MSG;
144 
145 /* This data type is for HAL event */
146 typedef struct {
147   NFC_HDR hdr;
148   uint8_t hal_evt; /* HAL event code  */
149   uint8_t status;  /* tHAL_NFC_STATUS */
150 } tNFC_HAL_EVT_MSG;
151 
152 /* callback function pointer(8; use 8 to be safe + NFC_SAVED_CMD_SIZE(2) */
153 #define NFC_RECEIVE_MSGS_OFFSET (10)
154 
155 /* NFCC power state change pending callback */
156 typedef void(tNFC_PWR_ST_CBACK)(void);
157 #define NFC_SAVED_HDR_SIZE (2)
158 /* data Reassembly error (in NFC_HDR.layer_specific) */
159 #define NFC_RAS_TOO_BIG 0x08
160 #define NFC_RAS_FRAGMENTED 0x01
161 
162 /* NCI command buffer contains a VSC (in NFC_HDR.layer_specific) */
163 #define NFC_WAIT_RSP_VSC 0x01
164 
165 /* NFC control blocks */
166 typedef struct {
167   uint16_t flags; /* NFC control block flags - NFC_FL_* */
168   tNFC_CONN_CB conn_cb[NCI_MAX_CONN_CBS];
169   uint8_t conn_id[NFC_MAX_CONN_ID + 1]; /* index: conn_id; conn_id[]: index(1
170                                            based) to conn_cb[] */
171   tNFC_DISCOVER_CBACK* p_discv_cback;
172   tNFC_RESPONSE_CBACK* p_resp_cback;
173   tNFC_TEST_CBACK* p_test_cback;
174   tNFC_VS_CBACK*
175       p_vs_cb[NFC_NUM_VS_CBACKS]; /* Register for vendor specific events  */
176 
177 #if (NFC_RW_ONLY == FALSE)
178   /* NFCC information at init rsp */
179   uint32_t nci_features; /* the NCI features supported by NFCC */
180   uint16_t max_ce_table; /* the max routing table size       */
181   uint8_t max_conn;      /* the num of connections supported by NFCC */
182 #endif
183   uint8_t nci_ctrl_size; /* Max Control Packet Payload Size */
184 
185   const tNCI_DISCOVER_MAPS*
186       p_disc_maps; /* NCI RF Discovery interface mapping */
187   uint8_t vs_interface
188       [NFC_NFCC_MAX_NUM_VS_INTERFACE]; /* the NCI VS interfaces of NFCC    */
189   uint16_t nci_interfaces;             /* the NCI interfaces of NFCC       */
190   uint8_t nci_intf_extensions;
191   uint8_t nci_intf_extension_map[NCI_INTERFACE_EXTENSION_MAX];
192   uint8_t num_disc_maps; /* number of RF Discovery interface mappings */
193   void* p_disc_pending;  /* the parameters associated with pending
194                             NFC_DiscoveryStart */
195 
196   /* NFC_TASK timer management */
197   TIMER_LIST_Q timer_queue; /* 1-sec timer event queue */
198   TIMER_LIST_Q quick_timer_queue;
199 
200   TIMER_LIST_ENT deactivate_timer; /* Timer to wait for deactivation */
201 
202   tNFC_STATE nfc_state;
203   bool reassembly; /* Reassemble fragmented data pkt */
204   uint8_t trace_level;
205   uint8_t last_hdr[NFC_SAVED_HDR_SIZE]; /* part of last NCI command header */
206   uint8_t last_cmd[NFC_SAVED_CMD_SIZE]; /* part of last NCI command payload */
207   void* p_vsc_cback;       /* the callback function for last VSC command */
208   BUFFER_Q nci_cmd_xmit_q; /* NCI command queue */
209   TIMER_LIST_ENT
210   nci_wait_rsp_timer;         /* Timer for waiting for nci command response */
211   uint16_t nci_wait_rsp_tout; /* NCI command timeout (in ms) */
212   uint8_t nci_wait_rsp;       /* layer_specific for last NCI message */
213 
214   uint8_t nci_cmd_window; /* Number of commands the controller can accecpt
215                              without waiting for response */
216 
217   NFC_HDR* p_nci_init_rsp; /* holding INIT_RSP until receiving
218                               HAL_NFC_POST_INIT_CPLT_EVT */
219   tHAL_NFC_ENTRY* p_hal;
220 
221   uint8_t nci_version; /* NCI version used for NCI communication*/
222 
223   uint8_t hci_packet_size; /* maximum hci payload size*/
224 
225   uint8_t hci_conn_credits; /* maximum conn credits for static HCI*/
226 
227   uint16_t nci_max_v_size; /*maximum NFC V rf frame size*/
228 
229   uint8_t rawVsCbflag;
230   uint8_t deact_reason;
231 
232 } tNFC_CB;
233 
234 /*****************************************************************************
235 **  EXTERNAL FUNCTION DECLARATIONS
236 *****************************************************************************/
237 
238 /* Global NFC data */
239 extern tNFC_CB nfc_cb;
240 
241 /****************************************************************************
242 ** Internal nfc functions
243 ****************************************************************************/
244 
245 #define NCI_CALCULATE_ACK(a, v) \
246   { a &= ((1 << v) - 1); }
247 #define MAX_NUM_VALID_BITS_FOR_ACK 0x07
248 
249 extern void nfc_init(void);
250 
251 /* from nfc_utils.c */
252 extern tNFC_CONN_CB* nfc_alloc_conn_cb(tNFC_CONN_CBACK* p_cback);
253 extern tNFC_CONN_CB* nfc_find_conn_cb_by_conn_id(uint8_t conn_id);
254 extern tNFC_CONN_CB* nfc_find_conn_cb_by_handle(uint8_t target_handle);
255 extern void nfc_set_conn_id(tNFC_CONN_CB* p_cb, uint8_t conn_id);
256 extern void nfc_free_conn_cb(tNFC_CONN_CB* p_cb);
257 extern void nfc_reset_all_conn_cbs(void);
258 extern void nfc_data_event(tNFC_CONN_CB* p_cb);
259 
260 void nfc_ncif_send(NFC_HDR* p_buf, bool is_cmd);
261 extern uint8_t nfc_ncif_send_data(tNFC_CONN_CB* p_cb, NFC_HDR* p_data);
262 extern void nfc_ncif_cmd_timeout(void);
263 extern void nfc_wait_2_deactivate_timeout(void);
264 
265 extern bool nfc_ncif_process_event(NFC_HDR* p_msg);
266 extern void nfc_ncif_check_cmd_queue(NFC_HDR* p_buf);
267 extern void nfc_ncif_send_cmd(NFC_HDR* p_buf);
268 extern void nfc_ncif_proc_discover_ntf(uint8_t* p, uint16_t plen);
269 extern void nfc_ncif_rf_management_status(tNFC_DISCOVER_EVT event,
270                                           uint8_t status);
271 extern void nfc_ncif_set_config_status(uint8_t* p, uint8_t len);
272 extern void nfc_ncif_event_status(tNFC_RESPONSE_EVT event, uint8_t status);
273 extern void nfc_ncif_error_status(uint8_t conn_id, uint8_t status);
274 extern void nfc_ncif_proc_credits(uint8_t* p, uint16_t plen);
275 extern void nfc_ncif_proc_activate(uint8_t* p, uint8_t len);
276 extern void nfc_ncif_proc_deactivate(uint8_t status, uint8_t deact_type,
277                                      bool is_ntf);
278 #if (NFC_NFCEE_INCLUDED == TRUE && NFC_RW_ONLY == FALSE)
279 extern void nfc_ncif_proc_ee_action(uint8_t* p, uint16_t plen);
280 extern void nfc_ncif_proc_ee_discover_req(uint8_t* p, uint16_t plen);
281 extern void nfc_ncif_proc_get_routing(uint8_t* p, uint8_t len);
282 #endif
283 extern void nfc_ncif_proc_conn_create_rsp(uint8_t* p, uint16_t plen,
284                                           uint8_t dest_type);
285 extern void nfc_ncif_report_conn_close_evt(uint8_t conn_id, tNFC_STATUS status);
286 extern void nfc_ncif_proc_t3t_polling_ntf(uint8_t* p, uint16_t plen);
287 extern void nfc_ncif_proc_reset_rsp(uint8_t* p, bool is_ntf);
288 extern void nfc_ncif_proc_init_rsp(NFC_HDR* p_msg);
289 extern void nfc_ncif_proc_get_config_rsp(NFC_HDR* p_msg);
290 extern void nfc_ncif_proc_data(NFC_HDR* p_msg);
291 extern bool nfa_dm_p2p_prio_logic(uint8_t event, uint8_t* p, uint8_t ntf_rsp);
292 extern void nfa_dm_p2p_timer_event();
293 extern bool nfc_ncif_proc_proprietary_rsp(uint8_t mt, uint8_t gid, uint8_t oid);
294 extern void nfa_dm_p2p_prio_logic_cleanup();
295 extern void nfc_ncif_proc_isodep_nak_presence_check_status(uint8_t status,
296                                                            bool is_ntf);
297 extern void nfc_ncif_update_window(void);
298 #if (NFC_RW_ONLY == FALSE)
299 extern void nfc_ncif_proc_rf_field_ntf(uint8_t rf_status);
300 #else
301 #define nfc_ncif_proc_rf_field_ntf(rf_status)
302 #endif
303 
304 /* From nfc_task.c */
305 extern uint32_t nfc_task(uint32_t param);
306 void nfc_task_shutdown_nfcc(void);
307 
308 /* From nfc_main.c */
309 void nfc_enabled(tNFC_STATUS nfc_status, NFC_HDR* p_init_rsp_msg);
310 void nfc_set_state(tNFC_STATE nfc_state);
311 void nfc_main_flush_cmd_queue(void);
312 void nfc_gen_cleanup(void);
313 void nfc_main_handle_hal_evt(tNFC_HAL_EVT_MSG* p_msg);
314 
315 /* Timer functions */
316 void nfc_start_timer(TIMER_LIST_ENT* p_tle, uint16_t type, uint32_t timeout);
317 uint32_t nfc_remaining_time(TIMER_LIST_ENT* p_tle);
318 void nfc_stop_timer(TIMER_LIST_ENT* p_tle);
319 
320 void nfc_start_quick_timer(TIMER_LIST_ENT* p_tle, uint16_t type,
321                            uint32_t timeout);
322 void nfc_stop_quick_timer(TIMER_LIST_ENT* p_tle);
323 void nfc_process_quick_timer_evt(void);
324 
325 #ifdef __cplusplus
326 }
327 #endif
328 
329 #endif /* NFC_INT_H_ */
330