summaryrefslogtreecommitdiffstats
path: root/stack/hid/hidd_mgmt.c
blob: 819dd11a8780d5f900bf0e951b5b3b49b524d289 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
/*****************************************************************************/
/*                                                                           */
/*  Name:          hidd_mgmt.c                                               */
/*                                                                           */
/*  Description:   this file contains the HID Device Management logic        */
/*                                                                           */
/*  NOTE:          In the interest of keeping code small, there is an        */
/*                 inherent assumption that L2CAP always runs a timer,       */
/*                 and so the HID management never needs a timer on L2CAP    */
/*                 actions like connect and disconnect.                      */
/*                                                                           */
/*                                                                           */
/*  Copyright (c) 2002-2004, WIDCOMM Inc., All Rights Reserved.              */
/*  WIDCOMM Bluetooth Core. Proprietary and confidential.                    */
/*****************************************************************************/

#include <stdlib.h>
#include <string.h>
#include <stdio.h>

#include "gki.h"
#include "bt_types.h"
#include "hidd_api.h"
#include "hiddefs.h"
#include "hidd_int.h"
#include "btu.h"
#include "btm_api.h"


/********************************************************************************/
/*              L O C A L    F U N C T I O N     P R O T O T Y P E S            */
/********************************************************************************/
static tHID_STATUS hidd_no_conn_proc_evt( UINT8 event, void *p_data );
static tHID_STATUS hidd_connecting_proc_evt( UINT8 event, void *p_data );
static tHID_STATUS hidd_connected_proc_evt( UINT8 event, void *p_data );
static tHID_STATUS hidd_disc_ing_proc_evt( UINT8 event, void *p_data );


/* State machine jump table.
*/
tHIDD_MGMT_EVT_HDLR  * const hidd_sm_proc_evt[] = 
{
    hidd_no_conn_proc_evt,
    hidd_connecting_proc_evt,
    hidd_connected_proc_evt,
    hidd_disc_ing_proc_evt
};

/* Main HID control block */
#if HID_DYNAMIC_MEMORY == FALSE
tHIDDEV_CB   hd_cb;
#endif

/*******************************************************************************
**
** Function         hidd_mgmt_process_evt
**
** Description      This function is called by other modules to report an event
**
** Returns          tHID_STATUS
**
*******************************************************************************/
tHID_STATUS hidd_mgmt_process_evt( UINT8 event, void *p_data )
{
    HIDD_TRACE_DEBUG3("hidd_mgmt_process_evt known: %d, s:%d, e:%d", 
        hd_cb.host_known, hd_cb.dev_state, event);
    if( hd_cb.host_known )
    {
        return ((hidd_sm_proc_evt[hd_cb.dev_state]) (event, p_data)); /* Event is passed to main State Machine */
    }

    if( event == HOST_CONN_OPEN )
    {
        hd_cb.host_known = TRUE;
        memcpy( hd_cb.host_addr, (BD_ADDR *) p_data, BD_ADDR_LEN ) ;
        hd_cb.dev_state = HID_DEV_ST_CONNECTED ;
        /* Call-Back the Application with this information */
        hd_cb.callback(HID_DEV_EVT_OPEN, 0, (tHID_DEV_CBACK_DATA *) hd_cb.host_addr ) ;
        return( HID_SUCCESS );
    }

    return( HID_ERR_HOST_UNKNOWN );
}

/*******************************************************************************
**
** Function         hidd_no_conn_proc_evt
**
** Description      NO-CONN state event handler.
**
** Returns          tHID_STATUS
**
*******************************************************************************/
static tHID_STATUS hidd_no_conn_proc_evt( UINT8 event, void *p_data )
{
    tHID_STATUS st = HID_SUCCESS;

    switch( event )
    {
    case HOST_CONN_OPEN:
        hd_cb.dev_state = HID_DEV_ST_CONNECTED ;
        hd_cb.callback(HID_DEV_EVT_OPEN, 0, (tHID_DEV_CBACK_DATA *) hd_cb.host_addr ) ;
        break;
    case HID_API_CONNECT:
        hd_cb.conn_tries = 1;
        hd_cb.dev_state = HID_DEV_ST_CONNECTING ;
        if( (st = hidd_conn_initiate()) != HID_SUCCESS ) 
        {
#if HID_DEV_MAX_CONN_RETRY > 0
            btu_start_timer (&(hd_cb.conn.timer_entry), BTU_TTYPE_USER_FUNC, HID_DEV_REPAGE_WIN);
            return HID_SUCCESS;
#else
            hd_cb.dev_state = HID_DEV_ST_NO_CONN ;
#endif
        }
        break;
    default:
        st = HID_ERR_NO_CONNECTION;
    }

    return st;
}

/*******************************************************************************
**
** Function         hidd_proc_repage_timeout
**
** Description      function to handle timeout.
**
** Returns          void
**
*******************************************************************************/
void hidd_proc_repage_timeout (TIMER_LIST_ENT *p_tle)
{
    HIDD_TRACE_DEBUG0 ("hidd_proc_repage_timeout");
    hd_cb.conn_tries++;
    if( hidd_conn_initiate() != HID_SUCCESS ) 
    {
        if( hd_cb.conn_tries > HID_DEV_MAX_CONN_RETRY )
        {
            hd_cb.dev_state = HID_DEV_ST_NO_CONN ;
            hd_cb.callback(HID_DEV_EVT_CLOSE, 0, NULL );
        }
        else
            btu_start_timer (&(hd_cb.conn.timer_entry), BTU_TTYPE_USER_FUNC, HID_DEV_REPAGE_WIN);
    }
    else
        hd_cb.callback( HID_DEV_EVT_RETRYING, hd_cb.conn_tries, NULL );
}

/*******************************************************************************
**
** Function         hidd_connecting_proc_evt
**
** Description      CONNECTING state event handler
**
** Returns          tHID_STATUS
**
*******************************************************************************/
static tHID_STATUS hidd_connecting_proc_evt( UINT8 event, void *p_data )
{
    switch( event )
    {
    case HOST_CONN_OPEN:
        hd_cb.dev_state = HID_DEV_ST_CONNECTED ;
        hd_cb.callback(HID_DEV_EVT_OPEN, 0, (tHID_DEV_CBACK_DATA *) hd_cb.host_addr ) ;
        break;

    case HOST_CONN_FAIL:
        if( hd_cb.conn_tries > HID_DEV_MAX_CONN_RETRY ) 
        {
            UINT16 reason = *( (UINT16 *) p_data);

            hd_cb.dev_state = HID_DEV_ST_NO_CONN;
            hd_cb.callback(HID_DEV_EVT_CLOSE, reason, NULL );
        }
#if HID_DEV_MAX_CONN_RETRY > 0
        else
        {
            btu_start_timer (&(hd_cb.conn.timer_entry), BTU_TTYPE_USER_FUNC, HID_DEV_REPAGE_WIN);
        }
#endif
        break;

    case HOST_CONN_CLOSE:
    case HOST_CONN_LOST:
        hd_cb.dev_state = HID_DEV_ST_NO_CONN;
        hd_cb.callback(HID_DEV_EVT_CLOSE, *((UINT16 *) p_data), NULL );
        break;

    case HID_API_DISCONNECT:
        hd_cb.dev_state = HID_DEV_ST_NO_CONN ;
        btu_stop_timer (&(hd_cb.conn.timer_entry));
        hidd_conn_disconnect();
        break;

    default:
        return( HID_ERR_CONN_IN_PROCESS ) ;
    }

    return (HID_SUCCESS);
}

/*******************************************************************************
**
** Function         hidd_mgmt_conn_closed
**
** Description      Called when l2cap channels have been released
**
** Returns          void
**
*******************************************************************************/
void hidd_mgmt_conn_closed( UINT16 reason )
{    
    if( hd_cb.unplug_on )
    {
        hd_cb.host_known = FALSE; /* This allows the device to accept connection from other hosts */
    }

    hd_cb.dev_state = HID_DEV_ST_NO_CONN;
    hd_cb.callback(HID_DEV_EVT_CLOSE, reason, NULL );
}

/*******************************************************************************
**
** Function         hidd_connected_proc_evt
**
** Description      CONNECTED state event handler
**
** Returns          tHID_STATUS
**
*******************************************************************************/
static tHID_STATUS hidd_connected_proc_evt( UINT8 event, void *p_data )
{
    switch( event )
    {
    case HOST_CONN_LOST:
#if HID_DEV_RECONN_INITIATE == TRUE
        hd_cb.dev_state = HID_DEV_ST_CONNECTING ;
        hd_cb.conn_tries = 0;
        btu_start_timer (&(hd_cb.conn.timer_entry), BTU_TTYPE_USER_FUNC, HID_DEV_REPAGE_WIN);
#else
        hidd_mgmt_conn_closed( *((UINT16 *) p_data) ) ;
#endif
        break;
    case HOST_CONN_CLOSE:
        hidd_mgmt_conn_closed( *((UINT16 *) p_data) ) ;
        break;
    case HID_API_DISCONNECT:
        hd_cb.dev_state = HID_DEV_ST_DISC_ING ;
        hidd_conn_disconnect();
        break;
    case HID_API_SEND_DATA: /*Send Input reports, handshake or virtual-unplug */
        return hidd_conn_snd_data( (tHID_SND_DATA_PARAMS *) p_data );
    default:
        return( HID_ERR_ALREADY_CONN ) ;
    }

    return( HID_SUCCESS );
}

/*******************************************************************************
**
** Function         hidd_disc_ing_proc_evt
**
** Description      DISCONNECTING state event handler
**
** Returns          tHID_STATUS
**
*******************************************************************************/
static tHID_STATUS hidd_disc_ing_proc_evt( UINT8 event, void *p_data )
{
    switch( event )
    {
    case HOST_CONN_LOST:
    case HOST_CONN_FAIL:
    case HOST_CONN_CLOSE:
        hidd_mgmt_conn_closed( *((UINT16 *) p_data) ) ;
        break;
    default:
        return( HID_ERR_DISCONNECTING ) ;
    }

    return( HID_SUCCESS );
}