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 file contains functions that interface with the NFC NCI transport.
23  *  On the receive side, it routes events to the appropriate handler
24  *  (callback). On the transmit side, it manages the command transmission.
25  *
26  ******************************************************************************/
27 #include <string.h>
28 #include "gki.h"
29 #include "nfc_target.h"
30 #include "bt_types.h"
31 #include "hcidefs.h"
32 
33 #if (NFC_INCLUDED == TRUE)
34 #include "nfc_hal_api.h"
35 #include "nfc_api.h"
36 #include "nfc_int.h"
37 #include "nci_hmsgs.h"
38 #include "rw_int.h"
39 #include "ce_int.h"
40 
41 
42 #if (NFC_RW_ONLY == FALSE)
43 #include "ce_api.h"
44 #include "ce_int.h"
45 #include "llcp_int.h"
46 
47 /* NFC mandates support for at least one logical connection;
48  * Update max_conn to the NFCC capability on InitRsp */
49 #define NFC_SET_MAX_CONN_DEFAULT()    {nfc_cb.max_conn = 1;}
50 
51 #else /* NFC_RW_ONLY */
52 #define ce_init()
53 #define llcp_init()
54 
55 #define NFC_SET_MAX_CONN_DEFAULT()
56 
57 #endif /* NFC_RW_ONLY */
58 /****************************************************************************
59 ** Declarations
60 ****************************************************************************/
61 #if NFC_DYNAMIC_MEMORY == FALSE
62 tNFC_CB nfc_cb;
63 #endif
64 
65 #if (NFC_RW_ONLY == FALSE)
66 #define NFC_NUM_INTERFACE_MAP   2
67 #else
68 #define NFC_NUM_INTERFACE_MAP   1
69 #endif
70 
71 static const tNCI_DISCOVER_MAPS nfc_interface_mapping[NFC_NUM_INTERFACE_MAP] =
72 {
73     /* Protocols that use Frame Interface do not need to be included in the interface mapping */
74     {
75         NCI_PROTOCOL_ISO_DEP,
76         NCI_INTERFACE_MODE_POLL_N_LISTEN,
77         NCI_INTERFACE_ISO_DEP
78     }
79 #if (NFC_RW_ONLY == FALSE)
80     ,
81     /* this can not be set here due to 2079xB0 NFCC issues */
82     {
83         NCI_PROTOCOL_NFC_DEP,
84         NCI_INTERFACE_MODE_POLL_N_LISTEN,
85         NCI_INTERFACE_NFC_DEP
86     }
87 #endif
88 };
89 
90 
91 #if (BT_TRACE_VERBOSE == TRUE)
92 /*******************************************************************************
93 **
94 ** Function         nfc_state_name
95 **
96 ** Description      This function returns the state name.
97 **
98 ** NOTE             conditionally compiled to save memory.
99 **
100 ** Returns          pointer to the name
101 **
102 *******************************************************************************/
nfc_state_name(UINT8 state)103 static char *nfc_state_name (UINT8 state)
104 {
105     switch (state)
106     {
107     case NFC_STATE_NONE:
108         return ("NONE");
109     case NFC_STATE_W4_HAL_OPEN:
110         return ("W4_HAL_OPEN");
111     case NFC_STATE_CORE_INIT:
112         return ("CORE_INIT");
113     case NFC_STATE_W4_POST_INIT_CPLT:
114         return ("W4_POST_INIT_CPLT");
115     case NFC_STATE_IDLE:
116         return ("IDLE");
117     case NFC_STATE_OPEN:
118         return ("OPEN");
119     case NFC_STATE_CLOSING:
120         return ("CLOSING");
121     case NFC_STATE_W4_HAL_CLOSE:
122         return ("W4_HAL_CLOSE");
123     case NFC_STATE_NFCC_POWER_OFF_SLEEP:
124         return ("NFCC_POWER_OFF_SLEEP");
125     default:
126         return ("???? UNKNOWN STATE");
127     }
128 }
129 
130 /*******************************************************************************
131 **
132 ** Function         nfc_hal_event_name
133 **
134 ** Description      This function returns the HAL event name.
135 **
136 ** NOTE             conditionally compiled to save memory.
137 **
138 ** Returns          pointer to the name
139 **
140 *******************************************************************************/
nfc_hal_event_name(UINT8 event)141 static char *nfc_hal_event_name (UINT8 event)
142 {
143     switch (event)
144     {
145     case HAL_NFC_OPEN_CPLT_EVT:
146         return ("HAL_NFC_OPEN_CPLT_EVT");
147 
148     case HAL_NFC_CLOSE_CPLT_EVT:
149         return ("HAL_NFC_CLOSE_CPLT_EVT");
150 
151     case HAL_NFC_POST_INIT_CPLT_EVT:
152         return ("HAL_NFC_POST_INIT_CPLT_EVT");
153 
154     case HAL_NFC_PRE_DISCOVER_CPLT_EVT:
155         return ("HAL_NFC_PRE_DISCOVER_CPLT_EVT");
156 
157     case HAL_NFC_REQUEST_CONTROL_EVT:
158         return ("HAL_NFC_REQUEST_CONTROL_EVT");
159 
160     case HAL_NFC_RELEASE_CONTROL_EVT:
161         return ("HAL_NFC_RELEASE_CONTROL_EVT");
162 
163     case HAL_NFC_ERROR_EVT:
164         return ("HAL_NFC_ERROR_EVT");
165 
166     default:
167         return ("???? UNKNOWN EVENT");
168     }
169 }
170 #endif /* BT_TRACE_VERBOSE == TRUE */
171 
172 /*******************************************************************************
173 **
174 ** Function         nfc_main_notify_enable_status
175 **
176 ** Description      Notify status of Enable/PowerOffSleep/PowerCycle
177 **
178 *******************************************************************************/
nfc_main_notify_enable_status(tNFC_STATUS nfc_status)179 static void nfc_main_notify_enable_status (tNFC_STATUS nfc_status)
180 {
181     tNFC_RESPONSE   evt_data;
182 
183     evt_data.status = nfc_status;
184 
185     if (nfc_cb.p_resp_cback)
186     {
187         /* if getting out of PowerOffSleep mode or restarting NFCC */
188         if (nfc_cb.flags & (NFC_FL_RESTARTING|NFC_FL_POWER_CYCLE_NFCC))
189         {
190             nfc_cb.flags &= ~(NFC_FL_RESTARTING|NFC_FL_POWER_CYCLE_NFCC);
191             if (nfc_status != NFC_STATUS_OK)
192             {
193                 nfc_cb.flags |= NFC_FL_POWER_OFF_SLEEP;
194             }
195             (*nfc_cb.p_resp_cback) (NFC_NFCC_RESTART_REVT, &evt_data);
196         }
197         else
198         {
199             (*nfc_cb.p_resp_cback) (NFC_ENABLE_REVT, &evt_data);
200         }
201     }
202 }
203 
204 
205 
206 /*******************************************************************************
207 **
208 ** Function         nfc_enabled
209 **
210 ** Description      NFCC enabled, proceed with stack start up.
211 **
212 ** Returns          void
213 **
214 *******************************************************************************/
nfc_enabled(tNFC_STATUS nfc_status,BT_HDR * p_init_rsp_msg)215 void nfc_enabled (tNFC_STATUS nfc_status, BT_HDR *p_init_rsp_msg)
216 {
217     tNFC_RESPONSE evt_data;
218     tNFC_CONN_CB  *p_cb = &nfc_cb.conn_cb[NFC_RF_CONN_ID];
219     UINT8   *p;
220     UINT8   num_interfaces = 0, xx;
221     int     yy = 0;
222 
223     memset (&evt_data, 0, sizeof (tNFC_RESPONSE));
224 
225     if (nfc_status == NCI_STATUS_OK)
226     {
227         nfc_set_state (NFC_STATE_IDLE);
228 
229         p = (UINT8 *) (p_init_rsp_msg + 1) + p_init_rsp_msg->offset + NCI_MSG_HDR_SIZE + 1;
230         /* we currently only support NCI of the same version.
231         * We may need to change this, when we support multiple version of NFCC */
232         evt_data.enable.nci_version = NCI_VERSION;
233         STREAM_TO_UINT32 (evt_data.enable.nci_features, p);
234         STREAM_TO_UINT8 (num_interfaces, p);
235 
236         evt_data.enable.nci_interfaces = 0;
237         for (xx = 0; xx < num_interfaces; xx++)
238         {
239             if ((*p) <= NCI_INTERFACE_MAX)
240                 evt_data.enable.nci_interfaces |= (1 << (*p));
241             else if (((*p) > NCI_INTERFACE_FIRST_VS) && (yy < NFC_NFCC_MAX_NUM_VS_INTERFACE))
242             {
243                 /* save the VS RF interface in control block, if there's still room */
244                 nfc_cb.vs_interface[yy++] = *p;
245             }
246             p++;
247         }
248         nfc_cb.nci_interfaces    = evt_data.enable.nci_interfaces;
249         memcpy (evt_data.enable.vs_interface, nfc_cb.vs_interface, NFC_NFCC_MAX_NUM_VS_INTERFACE);
250         evt_data.enable.max_conn = *p++;
251         STREAM_TO_UINT16 (evt_data.enable.max_ce_table, p);
252 #if (NFC_RW_ONLY == FALSE)
253         nfc_cb.max_ce_table          = evt_data.enable.max_ce_table;
254         nfc_cb.nci_features          = evt_data.enable.nci_features;
255         nfc_cb.max_conn              = evt_data.enable.max_conn;
256 #endif
257         nfc_cb.nci_ctrl_size         = *p++; /* Max Control Packet Payload Length */
258         p_cb->init_credits           = p_cb->num_buff = 0;
259         STREAM_TO_UINT16 (evt_data.enable.max_param_size, p);
260         nfc_set_conn_id (p_cb, NFC_RF_CONN_ID);
261         evt_data.enable.manufacture_id   = *p++;
262         STREAM_TO_ARRAY (evt_data.enable.nfcc_info, p, NFC_NFCC_INFO_LEN);
263         NFC_DiscoveryMap (nfc_cb.num_disc_maps, (tNCI_DISCOVER_MAPS *) nfc_cb.p_disc_maps, NULL);
264     }
265     /* else not successful. the buffers will be freed in nfc_free_conn_cb () */
266     else
267     {
268         if (nfc_cb.flags & NFC_FL_RESTARTING)
269         {
270             nfc_set_state (NFC_STATE_NFCC_POWER_OFF_SLEEP);
271         }
272         else
273         {
274             nfc_free_conn_cb (p_cb);
275 
276             /* if NFCC didn't respond to CORE_RESET or CORE_INIT */
277             if (nfc_cb.nfc_state == NFC_STATE_CORE_INIT)
278             {
279                 /* report status after closing HAL */
280                 nfc_cb.p_hal->close ();
281                 return;
282             }
283             else
284                 nfc_set_state (NFC_STATE_NONE);
285         }
286     }
287 
288     nfc_main_notify_enable_status (nfc_status);
289 }
290 
291 /*******************************************************************************
292 **
293 ** Function         nfc_set_state
294 **
295 ** Description      Set the state of NFC stack
296 **
297 ** Returns          void
298 **
299 *******************************************************************************/
nfc_set_state(tNFC_STATE nfc_state)300 void nfc_set_state (tNFC_STATE nfc_state)
301 {
302 #if (BT_TRACE_VERBOSE == TRUE)
303     NFC_TRACE_DEBUG4 ("nfc_set_state %d (%s)->%d (%s)", nfc_cb.nfc_state, nfc_state_name (nfc_cb.nfc_state), nfc_state, nfc_state_name (nfc_state));
304 #else
305     NFC_TRACE_DEBUG2 ("nfc_set_state %d->%d", nfc_cb.nfc_state, nfc_state);
306 #endif
307     nfc_cb.nfc_state = nfc_state;
308 }
309 
310 /*******************************************************************************
311 **
312 ** Function         nfc_gen_cleanup
313 **
314 ** Description      Clean up for both going into low power mode and disabling NFC
315 **
316 *******************************************************************************/
nfc_gen_cleanup(void)317 void nfc_gen_cleanup (void)
318 {
319     nfc_cb.flags  &= ~NFC_FL_DEACTIVATING;
320 
321     /* the HAL pre-discover is still active - clear the pending flag/free the buffer */
322     if (nfc_cb.flags & NFC_FL_DISCOVER_PENDING)
323     {
324         nfc_cb.flags &= ~NFC_FL_DISCOVER_PENDING;
325         GKI_freebuf (nfc_cb.p_disc_pending);
326         nfc_cb.p_disc_pending = NULL;
327     }
328 
329     nfc_cb.flags &= ~(NFC_FL_CONTROL_REQUESTED | NFC_FL_CONTROL_GRANTED | NFC_FL_HAL_REQUESTED);
330 
331     nfc_stop_timer (&nfc_cb.deactivate_timer);
332 
333     /* Reset the connection control blocks */
334     nfc_reset_all_conn_cbs ();
335 
336     if (nfc_cb.p_nci_init_rsp)
337     {
338         GKI_freebuf (nfc_cb.p_nci_init_rsp);
339         nfc_cb.p_nci_init_rsp = NULL;
340     }
341 
342     /* clear any pending CMD/RSP */
343     nfc_main_flush_cmd_queue ();
344 }
345 
346 /*******************************************************************************
347 **
348 ** Function         nfc_main_handle_hal_evt
349 **
350 ** Description      Handle BT_EVT_TO_NFC_MSGS
351 **
352 *******************************************************************************/
nfc_main_handle_hal_evt(tNFC_HAL_EVT_MSG * p_msg)353 void nfc_main_handle_hal_evt (tNFC_HAL_EVT_MSG *p_msg)
354 {
355     UINT8  *ps;
356 
357     NFC_TRACE_DEBUG1 ("nfc_main_handle_hal_evt(): HAL event=0x%x", p_msg->hal_evt);
358 
359     switch (p_msg->hal_evt)
360     {
361     case HAL_NFC_OPEN_CPLT_EVT: /* only for failure case */
362         nfc_enabled (NFC_STATUS_FAILED, NULL);
363         break;
364 
365     case HAL_NFC_CLOSE_CPLT_EVT:
366         if (nfc_cb.p_resp_cback)
367         {
368             if (nfc_cb.nfc_state == NFC_STATE_W4_HAL_CLOSE)
369             {
370                 if (nfc_cb.flags & NFC_FL_POWER_OFF_SLEEP)
371                 {
372                     nfc_cb.flags &= ~NFC_FL_POWER_OFF_SLEEP;
373                     nfc_set_state (NFC_STATE_NFCC_POWER_OFF_SLEEP);
374                     (*nfc_cb.p_resp_cback) (NFC_NFCC_POWER_OFF_REVT, NULL);
375                 }
376                 else
377                 {
378                     nfc_set_state (NFC_STATE_NONE);
379                     (*nfc_cb.p_resp_cback) (NFC_DISABLE_REVT, NULL);
380                     nfc_cb.p_resp_cback = NULL;
381                 }
382             }
383             else
384             {
385                 /* found error during initialization */
386                 nfc_set_state (NFC_STATE_NONE);
387                 nfc_main_notify_enable_status (NFC_STATUS_FAILED);
388             }
389         }
390         break;
391 
392     case HAL_NFC_POST_INIT_CPLT_EVT:
393         if (nfc_cb.p_nci_init_rsp)
394         {
395             /*
396             ** if NFC_Disable() is called before receiving HAL_NFC_POST_INIT_CPLT_EVT,
397             ** then wait for HAL_NFC_CLOSE_CPLT_EVT.
398             */
399             if (nfc_cb.nfc_state == NFC_STATE_W4_POST_INIT_CPLT)
400             {
401                 if (p_msg->status == HAL_NFC_STATUS_OK)
402                 {
403                     nfc_enabled (NCI_STATUS_OK, nfc_cb.p_nci_init_rsp);
404                 }
405                 else /* if post initailization failed */
406                 {
407                     nfc_enabled (NCI_STATUS_FAILED, NULL);
408                 }
409             }
410 
411             GKI_freebuf (nfc_cb.p_nci_init_rsp);
412             nfc_cb.p_nci_init_rsp = NULL;
413         }
414         break;
415 
416     case HAL_NFC_PRE_DISCOVER_CPLT_EVT:
417         /* restore the command window, no matter if the discover command is still pending */
418         nfc_cb.nci_cmd_window = NCI_MAX_CMD_WINDOW;
419         nfc_cb.flags         &= ~NFC_FL_CONTROL_GRANTED;
420         if (nfc_cb.flags & NFC_FL_DISCOVER_PENDING)
421         {
422             /* issue the discovery command now, if it is still pending */
423             nfc_cb.flags &= ~NFC_FL_DISCOVER_PENDING;
424             ps            = (UINT8 *)nfc_cb.p_disc_pending;
425             nci_snd_discover_cmd (*ps, (tNFC_DISCOVER_PARAMS *)(ps + 1));
426             GKI_freebuf (nfc_cb.p_disc_pending);
427             nfc_cb.p_disc_pending = NULL;
428         }
429         else
430         {
431             /* check if there's other pending commands */
432             nfc_ncif_check_cmd_queue (NULL);
433         }
434 
435         if (p_msg->status == HAL_NFC_STATUS_ERR_CMD_TIMEOUT)
436             nfc_ncif_event_status (NFC_NFCC_TIMEOUT_REVT, NFC_STATUS_HW_TIMEOUT);
437         break;
438 
439     case HAL_NFC_REQUEST_CONTROL_EVT:
440         nfc_cb.flags    |= NFC_FL_CONTROL_REQUESTED;
441         nfc_cb.flags    |= NFC_FL_HAL_REQUESTED;
442         nfc_ncif_check_cmd_queue (NULL);
443         break;
444 
445     case HAL_NFC_RELEASE_CONTROL_EVT:
446         if (nfc_cb.flags & NFC_FL_CONTROL_GRANTED)
447         {
448             nfc_cb.flags &= ~NFC_FL_CONTROL_GRANTED;
449             nfc_cb.nci_cmd_window = NCI_MAX_CMD_WINDOW;
450             nfc_ncif_check_cmd_queue (NULL);
451 
452             if (p_msg->status == HAL_NFC_STATUS_ERR_CMD_TIMEOUT)
453                 nfc_ncif_event_status (NFC_NFCC_TIMEOUT_REVT, NFC_STATUS_HW_TIMEOUT);
454         }
455         break;
456 
457     case HAL_NFC_ERROR_EVT:
458         switch (p_msg->status)
459         {
460         case HAL_NFC_STATUS_ERR_TRANSPORT:
461             /* Notify app of transport error */
462             if (nfc_cb.p_resp_cback)
463             {
464                 (*nfc_cb.p_resp_cback) (NFC_NFCC_TRANSPORT_ERR_REVT, NULL);
465 
466                 /* if enabling NFC, notify upper layer of failure after closing HAL */
467                 if (nfc_cb.nfc_state < NFC_STATE_IDLE)
468                 {
469                     nfc_enabled (NFC_STATUS_FAILED, NULL);
470                 }
471             }
472             break;
473 
474         case HAL_NFC_STATUS_ERR_CMD_TIMEOUT:
475             nfc_ncif_event_status (NFC_NFCC_TIMEOUT_REVT, NFC_STATUS_HW_TIMEOUT);
476 
477             /* if enabling NFC, notify upper layer of failure after closing HAL */
478             if (nfc_cb.nfc_state < NFC_STATE_IDLE)
479             {
480                 nfc_enabled (NFC_STATUS_FAILED, NULL);
481                 return;
482             }
483             break;
484 
485         default:
486             break;
487         }
488         break;
489 
490     default:
491         NFC_TRACE_ERROR1 ("nfc_main_handle_hal_evt (): unhandled event (0x%x).", p_msg->hal_evt);
492         break;
493     }
494 }
495 
496 
497 /*******************************************************************************
498 **
499 ** Function         nfc_main_flush_cmd_queue
500 **
501 ** Description      This function is called when setting power off sleep state.
502 **
503 ** Returns          void
504 **
505 *******************************************************************************/
nfc_main_flush_cmd_queue(void)506 void nfc_main_flush_cmd_queue (void)
507 {
508     BT_HDR *p_msg;
509 
510     NFC_TRACE_DEBUG0 ("nfc_main_flush_cmd_queue ()");
511 
512     /* initialize command window */
513     nfc_cb.nci_cmd_window = NCI_MAX_CMD_WINDOW;
514 
515     /* Stop command-pending timer */
516     nfc_stop_timer(&nfc_cb.nci_wait_rsp_timer);
517 
518     /* dequeue and free buffer */
519     while ((p_msg = (BT_HDR *)GKI_dequeue (&nfc_cb.nci_cmd_xmit_q)) != NULL)
520     {
521         GKI_freebuf (p_msg);
522     }
523 }
524 
525 /*******************************************************************************
526 **
527 ** Function         nfc_main_post_hal_evt
528 **
529 ** Description      This function posts HAL event to NFC_TASK
530 **
531 ** Returns          void
532 **
533 *******************************************************************************/
nfc_main_post_hal_evt(UINT8 hal_evt,tHAL_NFC_STATUS status)534 void nfc_main_post_hal_evt (UINT8 hal_evt, tHAL_NFC_STATUS status)
535 {
536     tNFC_HAL_EVT_MSG *p_msg;
537 
538     if ((p_msg = (tNFC_HAL_EVT_MSG *) GKI_getbuf (sizeof(tNFC_HAL_EVT_MSG))) != NULL)
539     {
540         /* Initialize BT_HDR */
541         p_msg->hdr.len    = 0;
542         p_msg->hdr.event  = BT_EVT_TO_NFC_MSGS;
543         p_msg->hdr.offset = 0;
544         p_msg->hdr.layer_specific = 0;
545         p_msg->hal_evt = hal_evt;
546         p_msg->status  = status;
547         GKI_send_msg (NFC_TASK, NFC_MBOX_ID, p_msg);
548     }
549     else
550     {
551         NFC_TRACE_ERROR0 ("nfc_main_post_hal_evt (): No buffer");
552     }
553 }
554 
555 
556 /*******************************************************************************
557 **
558 ** Function         nfc_main_hal_cback
559 **
560 ** Description      HAL event handler
561 **
562 ** Returns          void
563 **
564 *******************************************************************************/
nfc_main_hal_cback(UINT8 event,tHAL_NFC_STATUS status)565 static void nfc_main_hal_cback(UINT8 event, tHAL_NFC_STATUS status)
566 {
567 #if (BT_TRACE_VERBOSE == TRUE)
568     NFC_TRACE_DEBUG3 ("nfc_main_hal_cback event: %s(0x%x), status=%d",
569                        nfc_hal_event_name (event), event, status);
570 #else
571     NFC_TRACE_DEBUG2 ("nfc_main_hal_cback event: 0x%x, status=%d", event, status);
572 #endif
573 
574     switch (event)
575     {
576     case HAL_NFC_OPEN_CPLT_EVT:
577         /*
578         ** if NFC_Disable() is called before receiving HAL_NFC_OPEN_CPLT_EVT,
579         ** then wait for HAL_NFC_CLOSE_CPLT_EVT.
580         */
581         if (nfc_cb.nfc_state == NFC_STATE_W4_HAL_OPEN)
582         {
583             if (status == HAL_NFC_STATUS_OK)
584             {
585                 /* Notify NFC_TASK that NCI tranport is initialized */
586                 GKI_send_event (NFC_TASK, NFC_TASK_EVT_TRANSPORT_READY);
587             }
588             else
589             {
590                 nfc_main_post_hal_evt (event, status);
591             }
592         }
593         break;
594 
595     case HAL_NFC_CLOSE_CPLT_EVT:
596     case HAL_NFC_POST_INIT_CPLT_EVT:
597     case HAL_NFC_PRE_DISCOVER_CPLT_EVT:
598     case HAL_NFC_REQUEST_CONTROL_EVT:
599     case HAL_NFC_RELEASE_CONTROL_EVT:
600     case HAL_NFC_ERROR_EVT:
601         nfc_main_post_hal_evt (event, status);
602         break;
603 
604     default:
605         NFC_TRACE_DEBUG1 ("nfc_main_hal_cback unhandled event %x", event);
606         break;
607     }
608 }
609 
610 /*******************************************************************************
611 **
612 ** Function         nfc_main_hal_data_cback
613 **
614 ** Description      HAL data event handler
615 **
616 ** Returns          void
617 **
618 *******************************************************************************/
nfc_main_hal_data_cback(UINT16 data_len,UINT8 * p_data)619 static void nfc_main_hal_data_cback(UINT16 data_len, UINT8   *p_data)
620 {
621     BT_HDR *p_msg;
622 
623     /* ignore all data while shutting down NFCC */
624     if (nfc_cb.nfc_state == NFC_STATE_W4_HAL_CLOSE)
625     {
626         return;
627     }
628 
629     if (p_data)
630     {
631         if ((p_msg = (BT_HDR *) GKI_getpoolbuf (NFC_NCI_POOL_ID)) != NULL)
632         {
633             /* Initialize BT_HDR */
634             p_msg->len    = data_len;
635             p_msg->event  = BT_EVT_TO_NFC_NCI;
636             p_msg->offset = NFC_RECEIVE_MSGS_OFFSET;
637 
638             /* no need to check length, it always less than pool size */
639             memcpy ((UINT8 *)(p_msg + 1) + p_msg->offset, p_data, p_msg->len);
640 
641             GKI_send_msg (NFC_TASK, NFC_MBOX_ID, p_msg);
642         }
643         else
644         {
645             NFC_TRACE_ERROR0 ("nfc_main_hal_data_cback (): No buffer");
646         }
647     }
648 }
649 
650 /*******************************************************************************
651 **
652 ** Function         NFC_Enable
653 **
654 ** Description      This function enables NFC. Prior to calling NFC_Enable:
655 **                  - the NFCC must be powered up, and ready to receive commands.
656 **                  - GKI must be enabled
657 **                  - NFC_TASK must be started
658 **                  - NCIT_TASK must be started (if using dedicated NCI transport)
659 **
660 **                  This function opens the NCI transport (if applicable),
661 **                  resets the NFC controller, and initializes the NFC subsystems.
662 **
663 **                  When the NFC startup procedure is completed, an
664 **                  NFC_ENABLE_REVT is returned to the application using the
665 **                  tNFC_RESPONSE_CBACK.
666 **
667 ** Returns          tNFC_STATUS
668 **
669 *******************************************************************************/
NFC_Enable(tNFC_RESPONSE_CBACK * p_cback)670 tNFC_STATUS NFC_Enable (tNFC_RESPONSE_CBACK *p_cback)
671 {
672     NFC_TRACE_API0 ("NFC_Enable ()");
673 
674     /* Validate callback */
675     if (!p_cback)
676     {
677         return (NFC_STATUS_INVALID_PARAM);
678     }
679     nfc_cb.p_resp_cback = p_cback;
680 
681     /* Open HAL transport. */
682     nfc_set_state (NFC_STATE_W4_HAL_OPEN);
683     nfc_cb.p_hal->open (nfc_main_hal_cback, nfc_main_hal_data_cback);
684 
685     return (NFC_STATUS_OK);
686 }
687 
688 /*******************************************************************************
689 **
690 ** Function         NFC_Disable
691 **
692 ** Description      This function performs clean up routines for shutting down
693 **                  NFC and closes the NCI transport (if using dedicated NCI
694 **                  transport).
695 **
696 **                  When the NFC shutdown procedure is completed, an
697 **                  NFC_DISABLED_REVT is returned to the application using the
698 **                  tNFC_RESPONSE_CBACK.
699 **
700 ** Returns          nothing
701 **
702 *******************************************************************************/
NFC_Disable(void)703 void NFC_Disable (void)
704 {
705     NFC_TRACE_API1 ("NFC_Disable (): nfc_state = %d", nfc_cb.nfc_state);
706 
707     if ((nfc_cb.nfc_state == NFC_STATE_NONE) || (nfc_cb.nfc_state == NFC_STATE_NFCC_POWER_OFF_SLEEP))
708     {
709         nfc_set_state (NFC_STATE_NONE);
710         if (nfc_cb.p_resp_cback)
711         {
712             (*nfc_cb.p_resp_cback) (NFC_DISABLE_REVT, NULL);
713             nfc_cb.p_resp_cback = NULL;
714         }
715         return;
716     }
717 
718     /* Close transport and clean up */
719     nfc_task_shutdown_nfcc ();
720 }
721 
722 /*******************************************************************************
723 **
724 ** Function         NFC_Init
725 **
726 ** Description      This function initializes control block for NFC
727 **
728 ** Returns          nothing
729 **
730 *******************************************************************************/
NFC_Init(tHAL_NFC_ENTRY * p_hal_entry_tbl)731 void NFC_Init (tHAL_NFC_ENTRY *p_hal_entry_tbl)
732 {
733     int xx;
734 
735     /* Clear nfc control block */
736     memset (&nfc_cb, 0, sizeof (tNFC_CB));
737 
738     /* Reset the nfc control block */
739     for (xx = 0; xx < NCI_MAX_CONN_CBS; xx++)
740     {
741         nfc_cb.conn_cb[xx].conn_id = NFC_ILLEGAL_CONN_ID;
742     }
743 
744     /* NCI init */
745     nfc_cb.p_hal            = p_hal_entry_tbl;
746     nfc_cb.nfc_state        = NFC_STATE_NONE;
747     nfc_cb.nci_cmd_window   = NCI_MAX_CMD_WINDOW;
748     nfc_cb.nci_wait_rsp_tout= NFC_CMD_CMPL_TIMEOUT;
749     nfc_cb.p_disc_maps      = nfc_interface_mapping;
750     nfc_cb.num_disc_maps    = NFC_NUM_INTERFACE_MAP;
751     nfc_cb.trace_level      = NFC_INITIAL_TRACE_LEVEL;
752     nfc_cb.nci_ctrl_size    = NCI_CTRL_INIT_SIZE;
753     nfc_cb.reassembly       = TRUE;
754 
755     rw_init ();
756     ce_init ();
757     llcp_init ();
758     NFC_SET_MAX_CONN_DEFAULT ();
759 }
760 
761 /*******************************************************************************
762 **
763 ** Function         NFC_GetLmrtSize
764 **
765 ** Description      Called by application wto query the Listen Mode Routing
766 **                  Table size supported by NFCC
767 **
768 ** Returns          Listen Mode Routing Table size
769 **
770 *******************************************************************************/
NFC_GetLmrtSize(void)771 UINT16 NFC_GetLmrtSize (void)
772 {
773     UINT16 size = 0;
774 #if (NFC_RW_ONLY == FALSE)
775     size = nfc_cb.max_ce_table;
776 #endif
777     return size;
778 }
779 
780 
781 /*******************************************************************************
782 **
783 ** Function         NFC_SetConfig
784 **
785 ** Description      This function is called to send the configuration parameter
786 **                  TLV to NFCC. The response from NFCC is reported by
787 **                  tNFC_RESPONSE_CBACK as NFC_SET_CONFIG_REVT.
788 **
789 ** Parameters       tlv_size - the length of p_param_tlvs.
790 **                  p_param_tlvs - the parameter ID/Len/Value list
791 **
792 ** Returns          tNFC_STATUS
793 **
794 *******************************************************************************/
NFC_SetConfig(UINT8 tlv_size,UINT8 * p_param_tlvs)795 tNFC_STATUS NFC_SetConfig (UINT8     tlv_size,
796                            UINT8    *p_param_tlvs)
797 {
798     return nci_snd_core_set_config (p_param_tlvs, tlv_size);
799 }
800 
801 /*******************************************************************************
802 **
803 ** Function         NFC_GetConfig
804 **
805 ** Description      This function is called to retrieve the parameter TLV from NFCC.
806 **                  The response from NFCC is reported by tNFC_RESPONSE_CBACK
807 **                  as NFC_GET_CONFIG_REVT.
808 **
809 ** Parameters       num_ids - the number of parameter IDs
810 **                  p_param_ids - the parameter ID list.
811 **
812 ** Returns          tNFC_STATUS
813 **
814 *******************************************************************************/
NFC_GetConfig(UINT8 num_ids,UINT8 * p_param_ids)815 tNFC_STATUS NFC_GetConfig (UINT8     num_ids,
816                            UINT8    *p_param_ids)
817 {
818     return nci_snd_core_get_config (p_param_ids, num_ids);
819 }
820 
821 /*******************************************************************************
822 **
823 ** Function         NFC_DiscoveryMap
824 **
825 ** Description      This function is called to set the discovery interface mapping.
826 **                  The response from NFCC is reported by tNFC_DISCOVER_CBACK as.
827 **                  NFC_MAP_DEVT.
828 **
829 ** Parameters       num - the number of items in p_params.
830 **                  p_maps - the discovery interface mappings
831 **                  p_cback - the discovery callback function
832 **
833 ** Returns          tNFC_STATUS
834 **
835 *******************************************************************************/
NFC_DiscoveryMap(UINT8 num,tNFC_DISCOVER_MAPS * p_maps,tNFC_DISCOVER_CBACK * p_cback)836 tNFC_STATUS NFC_DiscoveryMap (UINT8 num, tNFC_DISCOVER_MAPS *p_maps,
837                                         tNFC_DISCOVER_CBACK *p_cback)
838 {
839     UINT8   num_disc_maps = num;
840     UINT8   xx, yy, num_intf, intf_mask;
841     tNFC_DISCOVER_MAPS  max_maps[NFC_NFCC_MAX_NUM_VS_INTERFACE + NCI_INTERFACE_MAX];
842     BOOLEAN is_supported;
843 
844     nfc_cb.p_discv_cback = p_cback;
845     num_intf             = 0;
846     NFC_TRACE_DEBUG1 ("nci_interfaces supported by NFCC: 0x%x", nfc_cb.nci_interfaces);
847     for (xx = 0; xx < num_disc_maps; xx++)
848     {
849         is_supported = FALSE;
850         if (p_maps[xx].intf_type > NCI_INTERFACE_MAX)
851         {
852             for (yy = 0; yy < NFC_NFCC_MAX_NUM_VS_INTERFACE; yy++)
853             {
854                 if (nfc_cb.vs_interface[yy] == p_maps[xx].intf_type)
855                     is_supported    = TRUE;
856             }
857             NFC_TRACE_DEBUG3 ("[%d]: vs intf_type:0x%x is_supported:%d", xx, p_maps[xx].intf_type, is_supported);
858         }
859         else
860         {
861             intf_mask = (1 << (p_maps[xx].intf_type));
862             if (intf_mask & nfc_cb.nci_interfaces)
863             {
864                 is_supported    = TRUE;
865             }
866             NFC_TRACE_DEBUG4 ("[%d]: intf_type:%d intf_mask: 0x%x is_supported:%d", xx, p_maps[xx].intf_type, intf_mask, is_supported);
867         }
868         if (is_supported)
869             memcpy (&max_maps[num_intf++], &p_maps[xx], sizeof (tNFC_DISCOVER_MAPS));
870         else
871         {
872             NFC_TRACE_WARNING1 ("NFC_DiscoveryMap interface=0x%x is not supported by NFCC", p_maps[xx].intf_type);
873         }
874     }
875 
876     return nci_snd_discover_map_cmd (num_intf, (tNCI_DISCOVER_MAPS *) max_maps);
877 }
878 
879 /*******************************************************************************
880 **
881 ** Function         NFC_DiscoveryStart
882 **
883 ** Description      This function is called to start Polling and/or Listening.
884 **                  The response from NFCC is reported by tNFC_DISCOVER_CBACK as.
885 **                  NFC_START_DEVT. The notification from NFCC is reported by
886 **                  tNFC_DISCOVER_CBACK as NFC_RESULT_DEVT.
887 **
888 ** Parameters       num_params - the number of items in p_params.
889 **                  p_params - the discovery parameters
890 **                  p_cback - the discovery callback function
891 **
892 ** Returns          tNFC_STATUS
893 **
894 *******************************************************************************/
NFC_DiscoveryStart(UINT8 num_params,tNFC_DISCOVER_PARAMS * p_params,tNFC_DISCOVER_CBACK * p_cback)895 tNFC_STATUS NFC_DiscoveryStart (UINT8                 num_params,
896                                 tNFC_DISCOVER_PARAMS *p_params,
897                                 tNFC_DISCOVER_CBACK  *p_cback)
898 {
899     UINT8   *p;
900     int     params_size;
901     tNFC_STATUS status = NFC_STATUS_NO_BUFFERS;
902 
903     NFC_TRACE_API0 ("NFC_DiscoveryStart");
904     if (nfc_cb.p_disc_pending)
905     {
906         NFC_TRACE_ERROR0 ("There's pending NFC_DiscoveryStart");
907         status = NFC_STATUS_BUSY;
908     }
909     else
910     {
911         nfc_cb.p_discv_cback = p_cback;
912         nfc_cb.flags        |= NFC_FL_DISCOVER_PENDING;
913         nfc_cb.flags        |= NFC_FL_CONTROL_REQUESTED;
914         params_size          = sizeof (tNFC_DISCOVER_PARAMS) * num_params;
915         nfc_cb.p_disc_pending = GKI_getbuf ((UINT16)(BT_HDR_SIZE + 1 + params_size));
916         if (nfc_cb.p_disc_pending)
917         {
918             p       = (UINT8 *)nfc_cb.p_disc_pending;
919             *p++    = num_params;
920             memcpy (p, p_params, params_size);
921             status = NFC_STATUS_CMD_STARTED;
922             nfc_ncif_check_cmd_queue (NULL);
923         }
924     }
925 
926     NFC_TRACE_API1 ("NFC_DiscoveryStart status: 0x%x", status);
927     return status;
928 }
929 
930 /*******************************************************************************
931 **
932 ** Function         NFC_DiscoverySelect
933 **
934 ** Description      If tNFC_DISCOVER_CBACK reports status=NFC_MULTIPLE_PROT,
935 **                  the application needs to use this function to select the
936 **                  the logical endpoint to continue. The response from NFCC is
937 **                  reported by tNFC_DISCOVER_CBACK as NFC_SELECT_DEVT.
938 **
939 ** Parameters       rf_disc_id - The ID identifies the remote device.
940 **                  protocol - the logical endpoint on the remote devide
941 **                  rf_interface - the RF interface to communicate with NFCC
942 **
943 ** Returns          tNFC_STATUS
944 **
945 *******************************************************************************/
NFC_DiscoverySelect(UINT8 rf_disc_id,UINT8 protocol,UINT8 rf_interface)946 tNFC_STATUS NFC_DiscoverySelect (UINT8    rf_disc_id,
947                                  UINT8    protocol,
948                                  UINT8    rf_interface)
949 {
950     return nci_snd_discover_select_cmd (rf_disc_id, protocol, rf_interface);
951 }
952 
953 /*******************************************************************************
954 **
955 ** Function         NFC_ConnCreate
956 **
957 ** Description      This function is called to create a logical connection with
958 **                  NFCC for data exchange.
959 **
960 ** Parameters       dest_type - the destination type
961 **                  id   - the NFCEE ID or RF Discovery ID .
962 **                  protocol   - the protocol.
963 **                  p_cback - the connection callback function
964 **
965 ** Returns          tNFC_STATUS
966 **
967 *******************************************************************************/
NFC_ConnCreate(UINT8 dest_type,UINT8 id,UINT8 protocol,tNFC_CONN_CBACK * p_cback)968 tNFC_STATUS NFC_ConnCreate (UINT8            dest_type,
969                             UINT8            id,
970                             UINT8            protocol,
971                             tNFC_CONN_CBACK *p_cback)
972 {
973     tNFC_STATUS     status = NFC_STATUS_FAILED;
974     tNFC_CONN_CB    *p_cb;
975     UINT8           num_tlv=0, tlv_size=0;
976     UINT8           param_tlvs[4], *pp;
977 
978     p_cb = nfc_alloc_conn_cb (p_cback);
979     if (p_cb)
980     {
981         p_cb->id = id;
982         pp = param_tlvs;
983         if (dest_type == NCI_DEST_TYPE_NFCEE)
984         {
985             num_tlv = 1;
986             UINT8_TO_STREAM (pp, NCI_CON_CREATE_TAG_NFCEE_VAL);
987             UINT8_TO_STREAM (pp, 2);
988             UINT8_TO_STREAM (pp, id);
989             UINT8_TO_STREAM (pp, protocol);
990             tlv_size = 4;
991         }
992         else if (dest_type == NCI_DEST_TYPE_REMOTE)
993         {
994             num_tlv = 1;
995             UINT8_TO_STREAM (pp, NCI_CON_CREATE_TAG_RF_DISC_ID);
996             UINT8_TO_STREAM (pp, 1);
997             UINT8_TO_STREAM (pp, id);
998             tlv_size = 3;
999         }
1000         else if (dest_type == NCI_DEST_TYPE_NFCC)
1001         {
1002             p_cb->id = NFC_TEST_ID;
1003         }
1004         /* Add handling of NCI_DEST_TYPE_REMOTE when more RF interface definitions are added */
1005         p_cb->act_protocol = protocol;
1006         p_cb->p_cback = p_cback;
1007         status = nci_snd_core_conn_create (dest_type, num_tlv, tlv_size, param_tlvs);
1008         if (status == NFC_STATUS_FAILED)
1009             nfc_free_conn_cb (p_cb);
1010     }
1011     return status;
1012 }
1013 
1014 /*******************************************************************************
1015 **
1016 ** Function         NFC_ConnClose
1017 **
1018 ** Description      This function is called to close a logical connection with
1019 **                  NFCC.
1020 **
1021 ** Parameters       conn_id - the connection id.
1022 **
1023 ** Returns          tNFC_STATUS
1024 **
1025 *******************************************************************************/
NFC_ConnClose(UINT8 conn_id)1026 tNFC_STATUS NFC_ConnClose (UINT8 conn_id)
1027 {
1028     tNFC_CONN_CB *p_cb = nfc_find_conn_cb_by_conn_id (conn_id);
1029     tNFC_STATUS     status = NFC_STATUS_FAILED;
1030 
1031     if (p_cb)
1032     {
1033         status = nci_snd_core_conn_close (conn_id);
1034     }
1035     return status;
1036 }
1037 
1038 /*******************************************************************************
1039 **
1040 ** Function         NFC_SetStaticRfCback
1041 **
1042 ** Description      This function is called to update the data callback function
1043 **                  to receive the data for the given connection id.
1044 **
1045 ** Parameters       p_cback - the connection callback function
1046 **
1047 ** Returns          Nothing
1048 **
1049 *******************************************************************************/
NFC_SetStaticRfCback(tNFC_CONN_CBACK * p_cback)1050 void NFC_SetStaticRfCback (tNFC_CONN_CBACK    *p_cback)
1051 {
1052     tNFC_CONN_CB * p_cb = &nfc_cb.conn_cb[NFC_RF_CONN_ID];
1053 
1054     p_cb->p_cback = p_cback;
1055     /* just in case DH has received NCI data before the data callback is set
1056      * check if there's any data event to report on this connection id */
1057     nfc_data_event (p_cb);
1058 }
1059 
1060 /*******************************************************************************
1061 **
1062 ** Function         NFC_SetReassemblyFlag
1063 **
1064 ** Description      This function is called to set if nfc will reassemble
1065 **                  nci packet as much as its buffer can hold or it should not
1066 **                  reassemble but forward the fragmented nci packet to layer above.
1067 **                  If nci data pkt is fragmented, nfc may send multiple
1068 **                  NFC_DATA_CEVT with status NFC_STATUS_CONTINUE before sending
1069 **                  NFC_DATA_CEVT with status NFC_STATUS_OK based on reassembly
1070 **                  configuration and reassembly buffer size
1071 **
1072 ** Parameters       reassembly - flag to indicate if nfc may reassemble or not
1073 **
1074 ** Returns          Nothing
1075 **
1076 *******************************************************************************/
NFC_SetReassemblyFlag(BOOLEAN reassembly)1077 void NFC_SetReassemblyFlag (BOOLEAN    reassembly)
1078 {
1079     nfc_cb.reassembly = reassembly;
1080 }
1081 
1082 /*******************************************************************************
1083 **
1084 ** Function         NFC_SendData
1085 **
1086 ** Description      This function is called to send the given data packet
1087 **                  to the connection identified by the given connection id.
1088 **
1089 ** Parameters       conn_id - the connection id.
1090 **                  p_data - the data packet.
1091 **                  p_data->offset must be >= NCI_MSG_OFFSET_SIZE + NCI_DATA_HDR_SIZE
1092 **                  The data payload starts at ((UINT8 *) (p_data + 1) + p_data->offset)
1093 **
1094 ** Returns          tNFC_STATUS
1095 **
1096 *******************************************************************************/
NFC_SendData(UINT8 conn_id,BT_HDR * p_data)1097 tNFC_STATUS NFC_SendData (UINT8       conn_id,
1098                           BT_HDR     *p_data)
1099 {
1100     tNFC_STATUS     status = NFC_STATUS_FAILED;
1101     tNFC_CONN_CB *p_cb = nfc_find_conn_cb_by_conn_id (conn_id);
1102 
1103     if (p_cb && p_data && p_data->offset >= NCI_MSG_OFFSET_SIZE + NCI_DATA_HDR_SIZE)
1104     {
1105         status = nfc_ncif_send_data (p_cb, p_data);
1106     }
1107 
1108     if (status != NFC_STATUS_OK)
1109         GKI_freebuf (p_data);
1110 
1111     return status;
1112 }
1113 
1114 /*******************************************************************************
1115 **
1116 ** Function         NFC_FlushData
1117 **
1118 ** Description      This function is called to discard the tx data queue of
1119 **                  the given connection id.
1120 **
1121 ** Parameters       conn_id - the connection id.
1122 **
1123 ** Returns          tNFC_STATUS
1124 **
1125 *******************************************************************************/
NFC_FlushData(UINT8 conn_id)1126 tNFC_STATUS NFC_FlushData (UINT8       conn_id)
1127 {
1128     tNFC_STATUS     status = NFC_STATUS_FAILED;
1129     tNFC_CONN_CB    *p_cb = nfc_find_conn_cb_by_conn_id (conn_id);
1130     void            *p_buf;
1131 
1132     if (p_cb)
1133     {
1134         status  = NFC_STATUS_OK;
1135         while ((p_buf = GKI_dequeue (&p_cb->tx_q)) != NULL)
1136             GKI_freebuf (p_buf);
1137     }
1138 
1139     return status;
1140 }
1141 
1142 /*******************************************************************************
1143 **
1144 ** Function         NFC_Deactivate
1145 **
1146 ** Description      This function is called to stop the discovery process or
1147 **                  put the listen device in sleep mode or terminate the NFC link.
1148 **
1149 **                  The response from NFCC is reported by tNFC_DISCOVER_CBACK
1150 **                  as NFC_DEACTIVATE_DEVT.
1151 **
1152 ** Parameters       deactivate_type - NFC_DEACTIVATE_TYPE_IDLE, to IDLE mode.
1153 **                                    NFC_DEACTIVATE_TYPE_SLEEP to SLEEP mode.
1154 **                                    NFC_DEACTIVATE_TYPE_SLEEP_AF to SLEEP_AF mode.
1155 **
1156 ** Returns          tNFC_STATUS
1157 **
1158 *******************************************************************************/
NFC_Deactivate(tNFC_DEACT_TYPE deactivate_type)1159 tNFC_STATUS NFC_Deactivate (tNFC_DEACT_TYPE deactivate_type)
1160 {
1161     tNFC_CONN_CB * p_cb = &nfc_cb.conn_cb[NFC_RF_CONN_ID];
1162     tNFC_STATUS  status = NFC_STATUS_OK;
1163 
1164 #if (BT_TRACE_VERBOSE == TRUE)
1165     NFC_TRACE_API3 ("NFC_Deactivate %d (%s) deactivate_type:%d", nfc_cb.nfc_state, nfc_state_name (nfc_cb.nfc_state), deactivate_type);
1166 #else
1167     NFC_TRACE_API2 ("NFC_Deactivate %d deactivate_type:%d", nfc_cb.nfc_state, deactivate_type);
1168 #endif
1169 
1170     if (nfc_cb.flags & NFC_FL_DISCOVER_PENDING)
1171     {
1172         /* the HAL pre-discover is still active - clear the pending flag */
1173         nfc_cb.flags &= ~NFC_FL_DISCOVER_PENDING;
1174         if (!(nfc_cb.flags & NFC_FL_HAL_REQUESTED))
1175         {
1176             /* if HAL did not request for control, clear this bit now */
1177             nfc_cb.flags &= ~NFC_FL_CONTROL_REQUESTED;
1178         }
1179         GKI_freebuf (nfc_cb.p_disc_pending);
1180         nfc_cb.p_disc_pending = NULL;
1181         return NFC_STATUS_OK;
1182     }
1183 
1184     if (nfc_cb.nfc_state == NFC_STATE_OPEN)
1185     {
1186         nfc_set_state (NFC_STATE_CLOSING);
1187         NFC_TRACE_DEBUG3 ( "act_protocol %d credits:%d/%d", p_cb->act_protocol, p_cb->init_credits, p_cb->num_buff);
1188         if ((p_cb->act_protocol == NCI_PROTOCOL_NFC_DEP) &&
1189             (p_cb->init_credits != p_cb->num_buff))
1190         {
1191             nfc_cb.flags           |= NFC_FL_DEACTIVATING;
1192             nfc_cb.deactivate_timer.param = (TIMER_PARAM_TYPE) deactivate_type;
1193             nfc_start_timer (&nfc_cb.deactivate_timer , (UINT16) (NFC_TTYPE_WAIT_2_DEACTIVATE), NFC_DEACTIVATE_TIMEOUT);
1194             return status;
1195         }
1196     }
1197 
1198     status = nci_snd_deactivate_cmd (deactivate_type);
1199     return status;
1200 }
1201 
1202 /*******************************************************************************
1203 **
1204 ** Function         NFC_UpdateRFCommParams
1205 **
1206 ** Description      This function is called to update RF Communication parameters
1207 **                  once the Frame RF Interface has been activated.
1208 **
1209 **                  The response from NFCC is reported by tNFC_RESPONSE_CBACK
1210 **                  as NFC_RF_COMM_PARAMS_UPDATE_REVT.
1211 **
1212 ** Returns          tNFC_STATUS
1213 **
1214 *******************************************************************************/
NFC_UpdateRFCommParams(tNFC_RF_COMM_PARAMS * p_params)1215 tNFC_STATUS NFC_UpdateRFCommParams (tNFC_RF_COMM_PARAMS *p_params)
1216 {
1217     UINT8 tlvs[12];
1218     UINT8 *p = tlvs;
1219     UINT8 data_exch_config;
1220 
1221     /* RF Technology and Mode */
1222     if (p_params->include_rf_tech_mode)
1223     {
1224         UINT8_TO_STREAM (p, NCI_RF_PARAM_ID_TECH_N_MODE);
1225         UINT8_TO_STREAM (p, 1);
1226         UINT8_TO_STREAM (p, p_params->rf_tech_n_mode);
1227     }
1228 
1229     /* Transmit Bit Rate */
1230     if (p_params->include_tx_bit_rate)
1231     {
1232         UINT8_TO_STREAM (p, NCI_RF_PARAM_ID_TX_BIT_RATE);
1233         UINT8_TO_STREAM (p, 1);
1234         UINT8_TO_STREAM (p, p_params->tx_bit_rate);
1235     }
1236 
1237     /* Receive Bit Rate */
1238     if (p_params->include_tx_bit_rate)
1239     {
1240         UINT8_TO_STREAM (p, NCI_RF_PARAM_ID_RX_BIT_RATE);
1241         UINT8_TO_STREAM (p, 1);
1242         UINT8_TO_STREAM (p, p_params->rx_bit_rate);
1243     }
1244 
1245     /* NFC-B Data Exchange Configuration */
1246     if (p_params->include_nfc_b_config)
1247     {
1248         UINT8_TO_STREAM (p, NCI_RF_PARAM_ID_B_DATA_EX_PARAM);
1249         UINT8_TO_STREAM (p, 1);
1250 
1251         data_exch_config =  (p_params->min_tr0 & 0x03) << 6;          /* b7b6 : Mininum TR0 */
1252         data_exch_config |= (p_params->min_tr1 & 0x03) << 4;          /* b5b4 : Mininum TR1 */
1253         data_exch_config |= (p_params->suppression_eos & 0x01) << 3;  /* b3 :   Suppression of EoS */
1254         data_exch_config |= (p_params->suppression_sos & 0x01) << 2;  /* b2 :   Suppression of SoS */
1255         data_exch_config |= (p_params->min_tr2 & 0x03);               /* b1b0 : Mininum TR2 */
1256 
1257         UINT8_TO_STREAM (p, data_exch_config);
1258     }
1259 
1260     return nci_snd_parameter_update_cmd (tlvs, (UINT8) (p - tlvs));
1261 }
1262 
1263 /*******************************************************************************
1264 **
1265 ** Function         NFC_SetPowerOffSleep
1266 **
1267 ** Description      This function closes/opens transport and turns off/on NFCC.
1268 **
1269 ** Returns          tNFC_STATUS
1270 **
1271 *******************************************************************************/
NFC_SetPowerOffSleep(BOOLEAN enable)1272 tNFC_STATUS NFC_SetPowerOffSleep (BOOLEAN enable)
1273 {
1274     NFC_TRACE_API1 ("NFC_SetPowerOffSleep () enable = %d", enable);
1275 
1276     if (  (enable == FALSE)
1277         &&(nfc_cb.nfc_state == NFC_STATE_NFCC_POWER_OFF_SLEEP)  )
1278     {
1279         nfc_cb.flags |= NFC_FL_RESTARTING;
1280 
1281         /* open transport */
1282         nfc_set_state (NFC_STATE_W4_HAL_OPEN);
1283         nfc_cb.p_hal->open (nfc_main_hal_cback, nfc_main_hal_data_cback);
1284 
1285         return NFC_STATUS_OK;
1286     }
1287     else if (  (enable == TRUE)
1288              &&(nfc_cb.nfc_state == NFC_STATE_IDLE)  )
1289     {
1290         /* close transport to turn off NFCC and clean up */
1291         nfc_cb.flags |= NFC_FL_POWER_OFF_SLEEP;
1292         nfc_task_shutdown_nfcc ();
1293 
1294         return NFC_STATUS_OK;
1295     }
1296 
1297     NFC_TRACE_ERROR1 ("NFC_SetPowerOffSleep () invalid state = %d", nfc_cb.nfc_state);
1298     return NFC_STATUS_FAILED;
1299 }
1300 
1301 /*******************************************************************************
1302 **
1303 ** Function         NFC_PowerCycleNFCC
1304 **
1305 ** Description      This function turns off and then on NFCC.
1306 **
1307 ** Returns          tNFC_STATUS
1308 **
1309 *******************************************************************************/
NFC_PowerCycleNFCC(void)1310 tNFC_STATUS NFC_PowerCycleNFCC (void)
1311 {
1312     NFC_TRACE_API0 ("NFC_PowerCycleNFCC ()");
1313 
1314     if (nfc_cb.nfc_state == NFC_STATE_IDLE)
1315     {
1316         /* power cycle NFCC */
1317         nfc_cb.flags |= NFC_FL_POWER_CYCLE_NFCC;
1318         nfc_task_shutdown_nfcc ();
1319 
1320         return NFC_STATUS_OK;
1321     }
1322 
1323     NFC_TRACE_ERROR1 ("NFC_PowerCycleNFCC () invalid state = %d", nfc_cb.nfc_state);
1324     return NFC_STATUS_FAILED;
1325 }
1326 
1327 
1328 /*******************************************************************************
1329 **
1330 ** Function         NFC_SetTraceLevel
1331 **
1332 ** Description      This function sets the trace level for NFC.  If called with
1333 **                  a value of 0xFF, it simply returns the current trace level.
1334 **
1335 ** Returns          The new or current trace level
1336 **
1337 *******************************************************************************/
NFC_SetTraceLevel(UINT8 new_level)1338 UINT8 NFC_SetTraceLevel (UINT8 new_level)
1339 {
1340     NFC_TRACE_API1 ("NFC_SetTraceLevel () new_level = %d", new_level);
1341 
1342     if (new_level != 0xFF)
1343         nfc_cb.trace_level = new_level;
1344 
1345     return (nfc_cb.trace_level);
1346 }
1347 
1348 #if (BT_TRACE_VERBOSE == TRUE)
1349 /*******************************************************************************
1350 **
1351 ** Function         NFC_GetStatusName
1352 **
1353 ** Description      This function returns the status name.
1354 **
1355 ** NOTE             conditionally compiled to save memory.
1356 **
1357 ** Returns          pointer to the name
1358 **
1359 *******************************************************************************/
NFC_GetStatusName(tNFC_STATUS status)1360 char *NFC_GetStatusName (tNFC_STATUS status)
1361 {
1362     switch (status)
1363     {
1364     case NFC_STATUS_OK:
1365         return "OK";
1366     case NFC_STATUS_REJECTED:
1367         return "REJECTED";
1368     case NFC_STATUS_MSG_CORRUPTED:
1369         return "CORRUPTED";
1370     case NFC_STATUS_BUFFER_FULL:
1371         return "BUFFER_FULL";
1372     case NFC_STATUS_FAILED:
1373         return "FAILED";
1374     case NFC_STATUS_NOT_INITIALIZED:
1375         return "NOT_INITIALIZED";
1376     case NFC_STATUS_SYNTAX_ERROR:
1377         return "SYNTAX_ERROR";
1378     case NFC_STATUS_SEMANTIC_ERROR:
1379         return "SEMANTIC_ERROR";
1380     case NFC_STATUS_UNKNOWN_GID:
1381         return "UNKNOWN_GID";
1382     case NFC_STATUS_UNKNOWN_OID:
1383         return "UNKNOWN_OID";
1384     case NFC_STATUS_INVALID_PARAM:
1385         return "INVALID_PARAM";
1386     case NFC_STATUS_MSG_SIZE_TOO_BIG:
1387         return "MSG_SIZE_TOO_BIG";
1388     case NFC_STATUS_ALREADY_STARTED:
1389         return "ALREADY_STARTED";
1390     case NFC_STATUS_ACTIVATION_FAILED:
1391         return "ACTIVATION_FAILED";
1392     case NFC_STATUS_TEAR_DOWN:
1393         return "TEAR_DOWN";
1394     case NFC_STATUS_RF_TRANSMISSION_ERR:
1395         return "RF_TRANSMISSION_ERR";
1396     case NFC_STATUS_RF_PROTOCOL_ERR:
1397         return "RF_PROTOCOL_ERR";
1398     case NFC_STATUS_TIMEOUT:
1399         return "TIMEOUT";
1400     case NFC_STATUS_EE_INTF_ACTIVE_FAIL:
1401         return "EE_INTF_ACTIVE_FAIL";
1402     case NFC_STATUS_EE_TRANSMISSION_ERR:
1403         return "EE_TRANSMISSION_ERR";
1404     case NFC_STATUS_EE_PROTOCOL_ERR:
1405         return "EE_PROTOCOL_ERR";
1406     case NFC_STATUS_EE_TIMEOUT:
1407         return "EE_TIMEOUT";
1408     case NFC_STATUS_CMD_STARTED:
1409         return "CMD_STARTED";
1410     case NFC_STATUS_HW_TIMEOUT:
1411         return "HW_TIMEOUT";
1412     case NFC_STATUS_CONTINUE:
1413         return "CONTINUE";
1414     case NFC_STATUS_REFUSED:
1415         return "REFUSED";
1416     case NFC_STATUS_BAD_RESP:
1417         return "BAD_RESP";
1418     case NFC_STATUS_CMD_NOT_CMPLTD:
1419         return "CMD_NOT_CMPLTD";
1420     case NFC_STATUS_NO_BUFFERS:
1421         return "NO_BUFFERS";
1422     case NFC_STATUS_WRONG_PROTOCOL:
1423         return "WRONG_PROTOCOL";
1424     case NFC_STATUS_BUSY:
1425         return "BUSY";
1426     case NFC_STATUS_LINK_LOSS:
1427         return "LINK_LOSS";
1428     case NFC_STATUS_BAD_LENGTH:
1429         return "BAD_LENGTH";
1430     case NFC_STATUS_BAD_HANDLE:
1431         return "BAD_HANDLE";
1432     case NFC_STATUS_CONGESTED:
1433         return "CONGESTED";
1434     default:
1435         return"UNKNOWN";
1436     }
1437 }
1438 #endif
1439 
1440 #endif /* NFC_INCLUDED == TRUE */
1441