1 /******************************************************************************
2 *
3 * Copyright (C) 1999-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 functions for the SMP L2Cap interface
22 *
23 ******************************************************************************/
24
25 #include "bt_target.h"
26
27 #if SMP_INCLUDED == TRUE
28
29 #include <string.h>
30 #include "btm_ble_api.h"
31 #include "l2c_api.h"
32
33 #include "smp_int.h"
34
35
36
37 static void smp_connect_cback (BD_ADDR bd_addr, BOOLEAN connected, UINT16 reason, tBT_TRANSPORT transport);
38 static void smp_data_ind (BD_ADDR bd_addr, BT_HDR *p_buf);
39
40 /*******************************************************************************
41 **
42 ** Function smp_l2cap_if_init
43 **
44 ** Description This function is called during the SMP task startup
45 ** to register interface functions with L2CAP.
46 **
47 *******************************************************************************/
smp_l2cap_if_init(void)48 void smp_l2cap_if_init (void)
49 {
50 tL2CAP_FIXED_CHNL_REG fixed_reg;
51 SMP_TRACE_EVENT ("SMDBG l2c smp_l2cap_if_init");
52 fixed_reg.fixed_chnl_opts.mode = L2CAP_FCR_BASIC_MODE;
53 fixed_reg.fixed_chnl_opts.max_transmit = 0;
54 fixed_reg.fixed_chnl_opts.rtrans_tout = 0;
55 fixed_reg.fixed_chnl_opts.mon_tout = 0;
56 fixed_reg.fixed_chnl_opts.mps = 0;
57 fixed_reg.fixed_chnl_opts.tx_win_sz = 0;
58
59 fixed_reg.pL2CA_FixedConn_Cb = smp_connect_cback;
60 fixed_reg.pL2CA_FixedData_Cb = smp_data_ind;
61 fixed_reg.pL2CA_FixedCong_Cb = NULL; /* do not handle congestion on this channel */
62 fixed_reg.default_idle_tout = 60; /* set 60 seconds timeout, 0xffff default idle timeout */
63
64 /* Now, register with L2CAP */
65 L2CA_RegisterFixedChannel (L2CAP_SMP_CID, &fixed_reg);
66 }
67
68 /*******************************************************************************
69 **
70 ** Function smp_connect_cback
71 **
72 ** Description This callback function is called by L2CAP to indicate that
73 ** SMP channel is
74 ** connected (conn = TRUE)/disconnected (conn = FALSE).
75 **
76 *******************************************************************************/
smp_connect_cback(BD_ADDR bd_addr,BOOLEAN connected,UINT16 reason,tBT_TRANSPORT transport)77 static void smp_connect_cback (BD_ADDR bd_addr, BOOLEAN connected, UINT16 reason,
78 tBT_TRANSPORT transport)
79 {
80 tSMP_CB *p_cb = &smp_cb;
81 tSMP_INT_DATA int_data;
82
83 SMP_TRACE_EVENT ("SMDBG l2c smp_connect_cback ");
84
85 if (transport == BT_TRANSPORT_BR_EDR)
86 {
87 SMP_TRACE_ERROR ("smp_connect_cback : Wrong transport");
88 return;
89 }
90
91 if (memcmp(bd_addr, p_cb->pairing_bda, BD_ADDR_LEN) == 0)
92 {
93 SMP_TRACE_EVENT ("smp_connect_cback() for pairing BDA: %08x%04x Event: %s",
94 (bd_addr[0]<<24)+(bd_addr[1]<<16)+(bd_addr[2]<<8) + bd_addr[3],
95 (bd_addr[4]<<8)+bd_addr[5], (connected) ? "connected" : "disconnected");
96
97 if (connected)
98 {
99 if(!p_cb->connect_initialized)
100 {
101 p_cb->connect_initialized = TRUE;
102 /* initiating connection established */
103 p_cb->role = L2CA_GetBleConnRole(bd_addr);
104
105 /* initialize local i/r key to be default keys */
106 p_cb->loc_r_key = p_cb->loc_i_key = SMP_SEC_DEFAULT_KEY;
107 p_cb->loc_auth_req = p_cb->peer_auth_req = SMP_DEFAULT_AUTH_REQ;
108 p_cb->cb_evt = SMP_IO_CAP_REQ_EVT;
109 smp_sm_event(p_cb, SMP_L2CAP_CONN_EVT, NULL);
110 }
111 }
112 else
113 {
114 int_data.reason = reason;
115 /* Disconnected while doing security */
116 smp_sm_event(p_cb, SMP_L2CAP_DISCONN_EVT, &int_data);
117 }
118 }
119 }
120
121 /*******************************************************************************
122 **
123 ** Function smp_data_ind
124 **
125 ** Description This function is called when data is received from L2CAP on
126 ** SMP channel.
127 **
128 **
129 ** Returns void
130 **
131 *******************************************************************************/
smp_data_ind(BD_ADDR bd_addr,BT_HDR * p_buf)132 static void smp_data_ind (BD_ADDR bd_addr, BT_HDR *p_buf)
133 {
134 tSMP_CB *p_cb = &smp_cb;
135 UINT8 *p = (UINT8 *)(p_buf + 1) + p_buf->offset;
136 UINT8 cmd ;
137 SMP_TRACE_EVENT ("SMDBG l2c smp_data_ind");
138
139 SMP_TRACE_EVENT ("Got smp_data_ind");
140
141 STREAM_TO_UINT8(cmd, p);
142
143 /* sanity check */
144 if ((SMP_OPCODE_MAX <= cmd) || (cmd == 0))
145 {
146 SMP_TRACE_WARNING( "Ignore received command with RESERVED code 0x%02x", cmd);
147 GKI_freebuf (p_buf);
148 return;
149 }
150
151 /* reject the pairing request if there is an on-going SMP pairing */
152 if (SMP_OPCODE_PAIRING_REQ == cmd || SMP_OPCODE_SEC_REQ == cmd)
153 {
154 if (p_cb->state == SMP_ST_IDLE)
155 {
156 p_cb->role = L2CA_GetBleConnRole(bd_addr);
157 memcpy(&p_cb->pairing_bda[0], bd_addr, BD_ADDR_LEN);
158 }
159 else if (memcmp(&bd_addr[0], p_cb->pairing_bda, BD_ADDR_LEN))
160 {
161 GKI_freebuf (p_buf);
162 smp_reject_unexp_pair_req(bd_addr);
163 return;
164 }
165 /* else, out of state pairing request/security request received, passed into SM */
166 }
167
168 if (memcmp(&bd_addr[0], p_cb->pairing_bda, BD_ADDR_LEN) == 0)
169 {
170 if (p_cb->state != SMP_ST_RELEASE_DELAY)
171 {
172 btu_stop_timer (&p_cb->rsp_timer_ent);
173 btu_start_timer (&p_cb->rsp_timer_ent, BTU_TTYPE_SMP_PAIRING_CMD,
174 SMP_WAIT_FOR_RSP_TOUT);
175 }
176 p_cb->rcvd_cmd_code = cmd;
177 p_cb->rcvd_cmd_len = (UINT8) p_buf->len;
178 smp_sm_event(p_cb, cmd, p);
179 }
180
181 GKI_freebuf (p_buf);
182 }
183 #endif
184