1 /******************************************************************************
2  *
3  *  Copyright (C) 2010-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  *
22  *  This is the private interface file for the NFA Connection Handover.
23  *
24  ******************************************************************************/
25 #ifndef NFA_CHO_INT_H
26 #define NFA_CHO_INT_H
27 
28 #if (defined (NFA_CHO_INCLUDED) && (NFA_CHO_INCLUDED==TRUE))
29 #include "llcp_api.h"
30 #include "llcp_defs.h"
31 #include "nfa_cho_api.h"
32 
33 /*****************************************************************************
34 **  Constants and data types
35 *****************************************************************************/
36 
37 /* NFA Connection Handover state */
38 enum
39 {
40     NFA_CHO_ST_DISABLED,        /* Application has not registered      */
41     NFA_CHO_ST_IDLE,            /* No data link connection             */
42     NFA_CHO_ST_W4_CC,           /* Waiting for connection confirm      */
43     NFA_CHO_ST_CONNECTED,       /* Data link connected                 */
44 
45     NFA_CHO_ST_MAX
46 };
47 
48 typedef UINT8 tNFA_CHO_STATE;
49 
50 /* NFA Connection Handover substate in NFA_CHO_ST_CONNECTED */
51 enum
52 {
53     NFA_CHO_SUBSTATE_W4_LOCAL_HR,   /* Waiting for Hs record from local     */
54     NFA_CHO_SUBSTATE_W4_LOCAL_HS,   /* Waiting for Hs record from local     */
55     NFA_CHO_SUBSTATE_W4_REMOTE_HR,  /* Waiting for Hr record from remote    */
56     NFA_CHO_SUBSTATE_W4_REMOTE_HS,  /* Waiting for Hs record from remote    */
57 
58     NFA_CHO_SUBSTATE_MAX
59 };
60 
61 typedef UINT8 tNFA_CHO_SUBSTATE;
62 
63 /* Handover Message receiving status for SAR */
64 #define NFA_CHO_RX_NDEF_COMPLETE    0   /* received complete NDEF message */
65 #define NFA_CHO_RX_NDEF_TEMP_MEM    1   /* Cannot process due to temporary memory constraint */
66 #define NFA_CHO_RX_NDEF_PERM_MEM    2   /* Cannot process due to permanent memory constraint */
67 #define NFA_CHO_RX_NDEF_INCOMPLTE   3   /* Need more date       */
68 #define NFA_CHO_RX_NDEF_INVALID     4   /* Invalid NDEF message */
69 
70 typedef UINT8 tNFA_CHO_RX_NDEF_STATUS;
71 
72 /* Handover Message Type */
73 #define NFA_CHO_MSG_UNKNOWN         0   /* Unknown Message           */
74 #define NFA_CHO_MSG_HR              1   /* Handover Request Message  */
75 #define NFA_CHO_MSG_HS              2   /* Handover Select Message   */
76 #define NFA_CHO_MSG_BT_OOB          3   /* Simplified BT OOB message */
77 #define NFA_CHO_MSG_WIFI            4   /* Simplified WIFI message   */
78 
79 typedef UINT8 tNFA_CHO_MSG_TYPE;
80 
81 /* Timeout */
82 #define NFA_CHO_TIMEOUT_FOR_HS          1000    /* ms, waiting for Hs record */
83 #define NFA_CHO_TIMEOUT_FOR_RETRY       1000    /* ms, retry because of temp memory constrain */
84 #define NFA_CHO_TIMEOUT_SEGMENTED_HR    500     /* ms, waiting for next segmented Hr */
85 
86 #define NFA_CHO_EXCLUDING_PAYLOAD_ID    0xFF    /* don't include payload ID string */
87 
88 /* NFA Connection Handover internal events */
89 enum
90 {
91     NFA_CHO_API_REG_EVT     = NFA_SYS_EVT_START (NFA_ID_CHO), /* NFA_ChoRegister () */
92     NFA_CHO_API_DEREG_EVT,            /* NFA_ChoDeregister ()       */
93     NFA_CHO_API_CONNECT_EVT,          /* NFA_ChoConnect ()          */
94     NFA_CHO_API_DISCONNECT_EVT,       /* NFA_ChoDisconnect ()       */
95     NFA_CHO_API_SEND_HR_EVT,          /* NFA_ChoSendHr ()           */
96     NFA_CHO_API_SEND_HS_EVT,          /* NFA_ChoSendHs ()           */
97     NFA_CHO_API_SEL_ERR_EVT,          /* NFA_ChoSendSelectError ()  */
98 
99     NFA_CHO_RX_HANDOVER_MSG_EVT,      /* Received Handover Message  */
100 
101     NFA_CHO_LLCP_CONNECT_IND_EVT,     /* LLCP_SAP_EVT_CONNECT_IND       */
102     NFA_CHO_LLCP_CONNECT_RESP_EVT,    /* LLCP_SAP_EVT_CONNECT_RESP      */
103     NFA_CHO_LLCP_DISCONNECT_IND_EVT,  /* LLCP_SAP_EVT_DISCONNECT_IND    */
104     NFA_CHO_LLCP_DISCONNECT_RESP_EVT, /* LLCP_SAP_EVT_DISCONNECT_RESP   */
105     NFA_CHO_LLCP_CONGEST_EVT,         /* LLCP_SAP_EVT_CONGEST           */
106     NFA_CHO_LLCP_LINK_STATUS_EVT,     /* LLCP_SAP_EVT_LINK_STATUS       */
107 
108     NFA_CHO_NDEF_TYPE_HANDLER_EVT,    /* Callback event from NDEF Type handler */
109     NFA_CHO_TIMEOUT_EVT,              /* Timeout event              */
110 
111     NFA_CHO_LAST_EVT
112 };
113 
114 typedef UINT16 tNFA_CHO_INT_EVT;
115 
116 /* data type for NFA_CHO_API_REG_EVT */
117 typedef struct
118 {
119     BT_HDR              hdr;
120     BOOLEAN             enable_server;
121     tNFA_CHO_CBACK     *p_cback;
122 } tNFA_CHO_API_REG;
123 
124 /* data type for NFA_CHO_API_DEREG_EVT */
125 typedef BT_HDR tNFA_CHO_API_DEREG;
126 
127 /* data type for NFA_CHO_API_CONNECT_EVT */
128 typedef BT_HDR tNFA_CHO_API_CONNECT;
129 
130 /* data type for NFA_CHO_API_DISCONNECT_EVT */
131 typedef BT_HDR tNFA_CHO_API_DISCONNECT;
132 
133 /* data type for NFA_CHO_API_SEND_HR_EVT */
134 typedef struct
135 {
136     BT_HDR              hdr;
137     UINT8               num_ac_info;
138     tNFA_CHO_AC_INFO   *p_ac_info;
139     UINT8              *p_ndef;
140     UINT32              max_ndef_size;
141     UINT32              cur_ndef_size;
142 } tNFA_CHO_API_SEND_HR;
143 
144 /* data type for NFA_CHO_API_SEND_HS_EVT */
145 typedef struct
146 {
147     BT_HDR              hdr;
148     UINT8               num_ac_info;
149     tNFA_CHO_AC_INFO   *p_ac_info;
150     UINT8              *p_ndef;
151     UINT32              max_ndef_size;
152     UINT32              cur_ndef_size;
153 } tNFA_CHO_API_SEND_HS;
154 
155 /* data type for NFA_CHO_API_STOP_EVT */
156 typedef BT_HDR tNFA_CHO_API_STOP;
157 
158 /* data type for NFA_CHO_API_SEL_ERR_EVT */
159 typedef struct
160 {
161     BT_HDR              hdr;
162     UINT8               error_reason;
163     UINT32              error_data;
164 } tNFA_CHO_API_SEL_ERR;
165 
166 /* data type for NFA_CHO_NDEF_TYPE_HANDLER_EVT */
167 typedef struct
168 {
169     BT_HDR              hdr;
170     tNFA_NDEF_EVT       event;
171     tNFA_NDEF_EVT_DATA  data;
172 } tNFA_CHO_NDEF_TYPE_HDLR_EVT;
173 
174 /* union of all event data types */
175 typedef union
176 {
177     BT_HDR                      hdr;                /* NFA_CHO_TIMEOUT_EVT        */
178     tNFA_CHO_API_REG            api_reg;            /* NFA_CHO_API_REG_EVT        */
179     tNFA_CHO_API_DEREG          api_dereg;          /* NFA_CHO_API_DEREG_EVT      */
180     tNFA_CHO_API_CONNECT        api_connect;        /* NFA_CHO_API_CONNECT_EVT    */
181     tNFA_CHO_API_DISCONNECT     api_disconnect;     /* NFA_CHO_API_DISCONNECT_EVT */
182     tNFA_CHO_API_SEND_HR        api_send_hr;        /* NFA_CHO_API_SEND_HR_EVT    */
183     tNFA_CHO_API_SEND_HS        api_send_hs;        /* NFA_CHO_API_SEND_HS_EVT    */
184     tNFA_CHO_API_SEL_ERR        api_sel_err;        /* NFA_CHO_API_SEL_ERR_EVT    */
185     tNFA_CHO_NDEF_TYPE_HDLR_EVT ndef_type_hdlr;     /* NFA_CHO_NDEF_TYPE_HANDLER_EVT */
186     tLLCP_SAP_CBACK_DATA        llcp_cback_data;    /* LLCP callback data         */
187 } tNFA_CHO_INT_EVENT_DATA;
188 
189 /*****************************************************************************
190 **  control block
191 *****************************************************************************/
192 
193 #define NFA_CHO_FLAGS_LLCP_ACTIVATED    0x01
194 #define NFA_CHO_FLAGS_CLIENT_ONLY       0x02    /* Handover server is not enabled       */
195 #define NFA_CHO_FLAGS_CONN_COLLISION    0x04    /* collision when creating data link    */
196 
197 /* NFA Connection Handover control block */
198 typedef struct
199 {
200     tNFA_CHO_STATE      state;                  /* main state                           */
201     tNFA_CHO_SUBSTATE   substate;               /* substate in connected state          */
202     TIMER_LIST_ENT      timer;                  /* timer for rx handover message        */
203 
204     UINT8               server_sap;             /* SAP for local handover server        */
205     UINT8               client_sap;             /* SAP for connection to remote handover server */
206     UINT8               local_sap;              /* SSAP for connection, either server_sap or client_sap */
207     UINT8               remote_sap;             /* DSAP for connection                  */
208 
209     UINT8               flags;                  /* internal flags                       */
210     tNFA_CHO_DISC_REASON disc_reason;           /* disconnection reason                 */
211 
212     tNFA_HANDLE         hs_ndef_type_handle;    /* handle for HS NDEF Type handler      */
213     tNFA_HANDLE         bt_ndef_type_handle;    /* handle for BT OOB NDEF Type handler  */
214     tNFA_HANDLE         wifi_ndef_type_handle;  /* handle for WiFi NDEF Type handler    */
215 
216     UINT16              local_link_miu;         /* MIU of local LLCP                    */
217     UINT16              remote_miu;             /* peer's MIU of data link connection   */
218     BOOLEAN             congested;              /* TRUE if data link is congested       */
219 
220     UINT8               collision_local_sap;    /* SSAP for collision connection        */
221     UINT8               collision_remote_sap;   /* DSAP for collision connection        */
222     UINT16              collision_remote_miu;   /* peer's MIU of collision  connection  */
223     BOOLEAN             collision_congested;    /* TRUE if collision connection is congested */
224 
225     UINT16              tx_random_number;       /* it has been sent in Hr for collision */
226 
227     UINT8              *p_tx_ndef_msg;          /* allocate buffer for tx NDEF msg      */
228     UINT32              tx_ndef_cur_size;       /* current size of NDEF message         */
229     UINT32              tx_ndef_sent_size;      /* transmitted size of NDEF message     */
230 
231     UINT8              *p_rx_ndef_msg;          /* allocate buffer for rx NDEF msg      */
232     UINT32              rx_ndef_buf_size;       /* allocate buffer size for rx NDEF msg */
233     UINT32              rx_ndef_cur_size;       /* current rx size of NDEF message      */
234 
235     tNFA_CHO_CBACK     *p_cback;                /* callback registered by application   */
236 
237     UINT8               trace_level;
238 
239 #if (defined (NFA_CHO_TEST_INCLUDED) && (NFA_CHO_TEST_INCLUDED == TRUE))
240     UINT8               test_enabled;
241     UINT8               test_version;
242     UINT16              test_random_number;
243 #endif
244 } tNFA_CHO_CB;
245 
246 /*****************************************************************************
247 **  External variables
248 *****************************************************************************/
249 
250 /* NFA Connection Handover control block */
251 #if NFA_DYNAMIC_MEMORY == FALSE
252 extern tNFA_CHO_CB nfa_cho_cb;
253 #else
254 extern tNFA_CHO_CB *nfa_cho_cb_ptr;
255 #define nfa_cho_cb (*nfa_cho_cb_ptr)
256 #endif
257 
258 /*****************************************************************************
259 **  External functions
260 *****************************************************************************/
261 /* nfa_cho_main.c */
262 void nfa_cho_init (void);
263 
264 /* nfa_cho_sm.c */
265 void nfa_cho_sm_llcp_cback (tLLCP_SAP_CBACK_DATA *p_data);
266 void nfa_cho_sm_execute (tNFA_CHO_INT_EVT event, tNFA_CHO_INT_EVENT_DATA *p_evt_data);
267 
268 /* nfa_cho_util.c */
269 void nfa_cho_proc_ndef_type_handler_evt (tNFA_CHO_INT_EVENT_DATA *p_evt_data);
270 tNFA_STATUS nfa_cho_proc_api_reg (tNFA_CHO_INT_EVENT_DATA *p_evt_data);
271 void        nfa_cho_proc_api_dereg (void);
272 tNFA_STATUS nfa_cho_create_connection (void);
273 void nfa_cho_process_disconnection (tNFA_CHO_DISC_REASON disc_reason);
274 void nfa_cho_notify_tx_fail_evt (tNFA_STATUS status);
275 
276 tNFA_STATUS nfa_cho_send_handover_msg (void);
277 tNFA_CHO_RX_NDEF_STATUS nfa_cho_read_ndef_msg (UINT8 local_sap, UINT8 remote_sap);
278 tNFA_CHO_RX_NDEF_STATUS nfa_cho_reassemble_ho_msg (UINT8 local_sap, UINT8 remote_sap);
279 
280 tNFA_STATUS nfa_cho_send_hr (tNFA_CHO_API_SEND_HR *p_api_send_hr);
281 tNFA_STATUS nfa_cho_send_hs (tNFA_CHO_API_SEND_HS *p_api_select);
282 tNFA_STATUS nfa_cho_send_hs_error (UINT8 error_reason, UINT32 error_data);
283 
284 void nfa_cho_proc_hr (UINT32 length, UINT8 *p_ndef_msg);
285 void nfa_cho_proc_hs (UINT32 length, UINT8 *p_ndef_msg);
286 void nfa_cho_proc_simplified_format (UINT32 length, UINT8 *p_ndef_msg);
287 
288 tNFA_CHO_MSG_TYPE  nfa_cho_get_msg_type (UINT32 length, UINT8 *p_ndef_msg);
289 tNFA_CHO_ROLE_TYPE nfa_cho_get_local_device_role (UINT32 length, UINT8 *p_ndef_msg);
290 tNFA_STATUS nfa_cho_update_random_number (UINT8 *p_ndef_msg);
291 #endif /* (defined (NFA_CHO_INCLUDED) && (NFA_CHO_INCLUDED==TRUE)) */
292 #endif /* NFA_CHO_INT_H */
293