llcp_main.c revision e9df6ba5a8fcccf306a80b1670b423be8fe7746a
1/******************************************************************************
2 *
3 *  Copyright (C) 2010-2012 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 LLCP entry points
22 *
23 ******************************************************************************/
24
25#include <string.h>
26#include "gki.h"
27#include "nfc_target.h"
28#include "bt_types.h"
29#include "llcp_api.h"
30#include "llcp_int.h"
31#include "llcp_defs.h"
32#include "nfc_int.h"
33
34#if (LLCP_DYNAMIC_MEMORY == FALSE)
35tLLCP_CB llcp_cb;
36#endif
37
38/*******************************************************************************
39**
40** Function         llcp_init
41**
42** Description      This function is called once at startup to initialize
43**                  all the LLCP structures
44**
45** Returns          void
46**
47*******************************************************************************/
48void llcp_init (void)
49{
50    UINT32 pool_count;
51
52    memset (&llcp_cb, 0, sizeof (tLLCP_CB));
53
54    llcp_cb.trace_level = LLCP_INITIAL_TRACE_LEVEL;
55
56    LLCP_TRACE_DEBUG0 ("LLCP - llcp_init ()");
57
58    llcp_cb.lcb.local_link_miu = (LLCP_MIU <= LLCP_MAX_MIU ? LLCP_MIU : LLCP_MAX_MIU);
59    llcp_cb.lcb.local_opt      = LLCP_OPT_VALUE;
60    llcp_cb.lcb.local_wt       = LLCP_WAITING_TIME;
61    llcp_cb.lcb.local_lto      = LLCP_LTO_VALUE;
62
63    llcp_cb.lcb.inact_timeout_init   = LLCP_INIT_INACTIVITY_TIMEOUT;
64    llcp_cb.lcb.inact_timeout_target = LLCP_TARGET_INACTIVITY_TIMEOUT;
65    llcp_cb.lcb.symm_delay           = LLCP_DELAY_RESP_TIME;
66    llcp_cb.lcb.data_link_timeout    = LLCP_DATA_LINK_CONNECTION_TOUT;
67    llcp_cb.lcb.delay_first_pdu_timeout = LLCP_DELAY_TIME_TO_SEND_FIRST_PDU;
68
69    llcp_cb.lcb.wks  = LLCP_WKS_MASK_LM;
70
71    /* total number of buffers for LLCP */
72    pool_count = GKI_poolcount (LLCP_POOL_ID);
73
74    /* number of buffers for receiving data */
75    llcp_cb.num_rx_buff = (pool_count * LLCP_RX_BUFF_RATIO) / 100;
76
77    /* rx congestion start/end threshold */
78    llcp_cb.overall_rx_congest_start = (UINT8) ((llcp_cb.num_rx_buff * LLCP_RX_CONGEST_START) / 100);
79    llcp_cb.overall_rx_congest_end   = (UINT8) ((llcp_cb.num_rx_buff * LLCP_RX_CONGEST_END) / 100);
80
81    /* max number of buffers for receiving data on logical data link */
82    llcp_cb.max_num_ll_rx_buff = (UINT8) ((llcp_cb.num_rx_buff * LLCP_LL_RX_BUFF_LIMIT) / 100);
83
84    LLCP_TRACE_DEBUG4 ("num_rx_buff = %d, rx_congest_start = %d, rx_congest_end = %d, max_num_ll_rx_buff = %d",
85                        llcp_cb.num_rx_buff, llcp_cb.overall_rx_congest_start,
86                        llcp_cb.overall_rx_congest_end, llcp_cb.max_num_ll_rx_buff);
87
88    /* max number of buffers for transmitting data */
89    llcp_cb.max_num_tx_buff    = (UINT8) (pool_count - llcp_cb.num_rx_buff);
90
91    /* max number of buffers for transmitting data on logical data link */
92    llcp_cb.max_num_ll_tx_buff = (UINT8) ((llcp_cb.max_num_tx_buff * LLCP_LL_TX_BUFF_LIMIT) / 100);
93
94    LLCP_TRACE_DEBUG2 ("max_num_tx_buff = %d, max_num_ll_tx_buff = %d",
95                        llcp_cb.max_num_tx_buff, llcp_cb.max_num_ll_tx_buff);
96
97    llcp_cb.ll_tx_uncongest_ntf_start_sap = LLCP_SAP_SDP + 1;
98
99    LLCP_RegisterServer (LLCP_SAP_SDP, LLCP_LINK_TYPE_DATA_LINK_CONNECTION, "urn:nfc:sn:sdp", llcp_sdp_proc_data);
100}
101
102/*******************************************************************************
103**
104** Function         llcp_cleanup
105**
106** Description      This function is called once at closing to clean up
107**
108** Returns          void
109**
110*******************************************************************************/
111void llcp_cleanup (void)
112{
113    UINT8 sap;
114    tLLCP_APP_CB *p_app_cb;
115
116    LLCP_TRACE_DEBUG0 ("LLCP - llcp_cleanup ()");
117
118    for (sap = LLCP_SAP_SDP; sap < LLCP_NUM_SAPS; sap++)
119    {
120        p_app_cb = llcp_util_get_app_cb (sap);
121
122        if (  (p_app_cb)
123            &&(p_app_cb->p_app_cback)  )
124        {
125            LLCP_Deregister (sap);
126        }
127    }
128
129    nfc_stop_quick_timer (&llcp_cb.lcb.inact_timer);
130    nfc_stop_quick_timer (&llcp_cb.lcb.timer);
131}
132
133/*******************************************************************************
134**
135** Function         llcp_process_timeout
136**
137** Description      This function is called when an LLCP-related timeout occurs
138**
139** Returns          void
140**
141*******************************************************************************/
142void llcp_process_timeout (TIMER_LIST_ENT *p_tle)
143{
144    UINT8 reason;
145
146    LLCP_TRACE_DEBUG1 ("llcp_process_timeout: event=%d", p_tle->event);
147
148    switch (p_tle->event)
149    {
150    case NFC_TTYPE_LLCP_LINK_MANAGER:
151        /* Link timeout or Symm timeout */
152        llcp_link_process_link_timeout ();
153        break;
154
155    case NFC_TTYPE_LLCP_LINK_INACT:
156        /* inactivity timeout */
157        llcp_link_deactivate (LLCP_LINK_LOCAL_INITIATED);
158        break;
159
160    case NFC_TTYPE_LLCP_DATA_LINK:
161        reason = LLCP_SAP_DISCONNECT_REASON_TIMEOUT;
162        llcp_dlsm_execute ((tLLCP_DLCB *) (p_tle->param), LLCP_DLC_EVENT_TIMEOUT, &reason);
163        break;
164
165    case NFC_TTYPE_LLCP_DELAY_FIRST_PDU:
166        llcp_link_check_send_data ();
167        break;
168
169    default:
170        break;
171    }
172}
173
174/*******************************************************************************
175**
176** Function         LLCP_SetTraceLevel
177**
178** Description      This function sets the trace level for LLCP.  If called with
179**                  a value of 0xFF, it simply returns the current trace level.
180**
181** Returns          The new or current trace level
182**
183*******************************************************************************/
184UINT8 LLCP_SetTraceLevel (UINT8 new_level)
185{
186    if (new_level != 0xFF)
187        llcp_cb.trace_level = new_level;
188
189    return (llcp_cb.trace_level);
190}
191