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
37static void smp_connect_cback (BD_ADDR bd_addr, BOOLEAN connected, UINT16 reason);
38static 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*******************************************************************************/
48void smp_l2cap_if_init (void)
49{
50    tL2CAP_FIXED_CHNL_REG  fixed_reg;
51    SMP_TRACE_EVENT0 ("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.default_idle_tout  = 60;      /* set 60 seconds timeout, 0xffff default idle timeout */
62
63    /* Now, register with L2CAP */
64    L2CA_RegisterFixedChannel (L2CAP_SMP_CID, &fixed_reg);
65}
66
67/*******************************************************************************
68**
69** Function         smp_connect_cback
70**
71** Description      This callback function is called by L2CAP to indicate that
72**                  SMP channel is
73**                      connected (conn = TRUE)/disconnected (conn = FALSE).
74**
75*******************************************************************************/
76static void smp_connect_cback (BD_ADDR bd_addr, BOOLEAN connected, UINT16 reason)
77{
78    tSMP_CB   *p_cb = &smp_cb;
79    tSMP_INT_DATA   int_data;
80
81    SMP_TRACE_EVENT0 ("SMDBG l2c smp_connect_cback ");
82
83    if (memcmp(bd_addr, p_cb->pairing_bda, BD_ADDR_LEN) == 0)
84    {
85        SMP_TRACE_EVENT3 ("smp_connect_cback()  for pairing BDA: %08x%04x  Event: %s",
86                        (bd_addr[0]<<24)+(bd_addr[1]<<16)+(bd_addr[2]<<8) + bd_addr[3],
87                        (bd_addr[4]<<8)+bd_addr[5], (connected) ? "connected" : "disconnected");
88
89        if (connected)
90        {
91            if(!p_cb->connect_initialized)
92            {
93                p_cb->connect_initialized = TRUE;
94                /* initiating connection established */
95                p_cb->role = L2CA_GetBleConnRole(bd_addr);
96
97                /* initialize local i/r key to be default keys */
98                p_cb->loc_r_key = p_cb->loc_i_key =  SMP_SEC_DEFAULT_KEY;
99                p_cb->loc_auth_req = p_cb->peer_auth_req = SMP_DEFAULT_AUTH_REQ;
100                p_cb->cb_evt = SMP_IO_CAP_REQ_EVT;
101                smp_sm_event(p_cb, SMP_L2CAP_CONN_EVT, NULL);
102            }
103        }
104        else
105        {
106            int_data.reason = reason;
107            /* Disconnected while doing security */
108            smp_sm_event(p_cb, SMP_L2CAP_DISCONN_EVT, &int_data);
109        }
110    }
111}
112
113/*******************************************************************************
114**
115** Function         smp_data_ind
116**
117** Description      This function is called when data is received from L2CAP on
118**                  SMP channel.
119**
120**
121** Returns          void
122**
123*******************************************************************************/
124static void smp_data_ind (BD_ADDR bd_addr, BT_HDR *p_buf)
125{
126    tSMP_CB *p_cb = &smp_cb;
127    UINT8   *p = (UINT8 *)(p_buf + 1) + p_buf->offset;
128    UINT8   cmd ;
129    SMP_TRACE_EVENT0 ("SMDBG l2c smp_data_ind");
130
131    SMP_TRACE_EVENT0 ("Got smp_data_ind");
132
133    STREAM_TO_UINT8(cmd, p);
134
135
136    /* reject the pairing request if there is an on-going SMP pairing */
137    if (SMP_OPCODE_PAIRING_REQ == cmd || SMP_OPCODE_SEC_REQ == cmd)
138    {
139        if (p_cb->state == SMP_ST_IDLE)
140        {
141            p_cb->role = L2CA_GetBleConnRole(bd_addr);
142            memcpy(&p_cb->pairing_bda[0], bd_addr, BD_ADDR_LEN);
143        }
144        else if (memcmp(&bd_addr[0], p_cb->pairing_bda, BD_ADDR_LEN))
145        {
146            p_cb->failure = SMP_PAIR_NOT_SUPPORT;
147            smp_send_cmd(SMP_OPCODE_PAIRING_FAILED, p_cb);
148        }
149    }
150
151    if (memcmp(&bd_addr[0], p_cb->pairing_bda, BD_ADDR_LEN) == 0)
152    {
153        btu_stop_timer (&p_cb->rsp_timer_ent);
154        smp_sm_event(p_cb, cmd, p);
155    }
156
157    GKI_freebuf (p_buf);
158}
159#endif
160