summaryrefslogtreecommitdiffstats
path: root/bta/pb/bta_pbs_main.c
blob: 1ed892d9c0dd5da69300106e1dca5676fb41b750 (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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
/*****************************************************************************
**
**  Name:           bta_pbs_main.c
**
**  Description:    This file contains the phone book access server main functions 
**                  and state machine.
**
**  Copyright (c) 2003-2011, Broadcom Corp., All Rights Reserved.
**  Broadcom Bluetooth Core. Proprietary and confidential.
**
*****************************************************************************/

#include "bt_target.h"

#if defined(BTA_PBS_INCLUDED) && (BTA_PBS_INCLUDED == TRUE)

#include <string.h>
#include "bta_fs_api.h"
#include "bta_pbs_api.h"
#include "bta_pbs_int.h"
#include "gki.h"
#include "utl.h"
#include "obx_api.h"
#include "rfcdefs.h"    /* BT_PSM_RFCOMM */
#include "bta_fs_co.h"

/*****************************************************************************
** Constants and types
*****************************************************************************/

/* state machine states */
enum
{
  BTA_PBS_IDLE_ST = 0,      /* Idle  */
  BTA_PBS_LISTEN_ST,        /* Listen - waiting for OBX/RFC connection */
  BTA_PBS_W4_AUTH_ST,       /* Wait for Authentication -  (optional) */
  BTA_PBS_CONN_ST,          /* Connected - PBS Session is active */
  BTA_PBS_CLOSING_ST        /* Closing is in progress */
};

/* state machine action enumeration list */
enum
{
    BTA_PBS_API_DISABLE,
    BTA_PBS_API_AUTHRSP,
    BTA_PBS_API_ACCESSRSP,
    BTA_PBS_API_CLOSE,
    BTA_PBS_CI_READ,
    BTA_PBS_CI_OPEN,
    BTA_PBS_CI_VLIST,
    BTA_PBS_OBX_CONNECT,
    BTA_PBS_OBX_DISC,
    BTA_PBS_OBX_CLOSE,
    BTA_PBS_OBX_ABORT,
    BTA_PBS_OBX_PASSWORD,
    BTA_PBS_OBX_GET,
    BTA_PBS_OBX_SETPATH,
    BTA_PBS_APPL_TOUT,
    BTA_PBS_CONN_ERR_RSP,
    BTA_PBS_DISC_ERR_RSP,
    BTA_PBS_GASP_ERR_RSP,
    BTA_PBS_CLOSE_COMPLETE,
    BTA_PBS_IGNORE
};

/* type for action functions */
typedef void (*tBTA_PBS_ACTION)(tBTA_PBS_CB *p_cb, tBTA_PBS_DATA *p_data);

/* action function list */
const tBTA_PBS_ACTION bta_pbs_action[] =
{
    bta_pbs_api_disable,
    bta_pbs_api_authrsp,
    bta_pbs_api_accessrsp,
    bta_pbs_api_close,
    bta_pbs_ci_read_act,
    bta_pbs_ci_open_act,
    bta_pbs_ci_vlist_act,
    bta_pbs_obx_connect,
    bta_pbs_obx_disc,
    bta_pbs_obx_close,
    bta_pbs_obx_abort,
    bta_pbs_obx_password,
    bta_pbs_obx_get,
    bta_pbs_obx_setpath,
    bta_pbs_appl_tout,
    bta_pbs_conn_err_rsp,
    bta_pbs_disc_err_rsp,
    bta_pbs_gasp_err_rsp,
    bta_pbs_close_complete
};


/* state table information */
#define BTA_PBS_ACTIONS             1       /* number of actions */
#define BTA_PBS_NEXT_STATE          1       /* position of next state */
#define BTA_PBS_NUM_COLS            2       /* number of columns in state tables */

/* state table for idle state */
static const UINT8 bta_pbs_st_idle[][BTA_PBS_NUM_COLS] =
{
/* Event                          Action 1                  Next state */
/* BTA_PBS_API_ENABLE_EVT    */   {BTA_PBS_IGNORE,          BTA_PBS_LISTEN_ST}
};

/* state table for obex/rfcomm connection state */
static const UINT8 bta_pbs_st_listen[][BTA_PBS_NUM_COLS] =
{
/* Event                          Action 1               Next state */
/* BTA_PBS_API_DISABLE_EVT   */   {BTA_PBS_API_DISABLE,  BTA_PBS_IDLE_ST},
/* BTA_PBS_API_AUTHRSP_EVT   */   {BTA_PBS_IGNORE,       BTA_PBS_LISTEN_ST},
/* BTA_PBS_API_ACCESSRSP_EVT */   {BTA_PBS_IGNORE,       BTA_PBS_LISTEN_ST},
/* BTA_PBS_API_CLOSE_EVT     */   {BTA_PBS_API_CLOSE,    BTA_PBS_LISTEN_ST},
/* BTA_PBS_CI_READ_EVT       */   {BTA_PBS_IGNORE,       BTA_PBS_LISTEN_ST},
/* BTA_PBS_CI_OPEN_EVT       */   {BTA_PBS_IGNORE,       BTA_PBS_LISTEN_ST},
/* BTA_PBS_CI_VLIST_EVT      */   {BTA_PBS_IGNORE,       BTA_PBS_LISTEN_ST},
/* BTA_PBS_OBX_CONN_EVT      */   {BTA_PBS_OBX_CONNECT,  BTA_PBS_CONN_ST},
/* BTA_PBS_OBX_DISC_EVT      */   {BTA_PBS_IGNORE,       BTA_PBS_LISTEN_ST},
/* BTA_PBS_OBX_ABORT_EVT     */   {BTA_PBS_IGNORE,       BTA_PBS_LISTEN_ST},
/* BTA_PBS_OBX_PASSWORD_EVT  */   {BTA_PBS_OBX_PASSWORD, BTA_PBS_W4_AUTH_ST},
/* BTA_PBS_OBX_CLOSE_EVT     */   {BTA_PBS_IGNORE,       BTA_PBS_LISTEN_ST},
/* BTA_PBS_OBX_GET_EVT       */   {BTA_PBS_IGNORE,       BTA_PBS_LISTEN_ST},
/* BTA_PBS_OBX_SETPATH_EVT   */   {BTA_PBS_IGNORE,       BTA_PBS_LISTEN_ST},
/* BTA_PBS_APPL_TOUT_EVT     */   {BTA_PBS_IGNORE,       BTA_PBS_LISTEN_ST},
/* BTA_PBS_DISC_ERR_EVT      */   {BTA_PBS_IGNORE,       BTA_PBS_LISTEN_ST},
/* BTA_PBS_GASP_ERR_EVT      */   {BTA_PBS_IGNORE,       BTA_PBS_LISTEN_ST},
/* BTA_PBS_CLOSE_CMPL_EVT    */   {BTA_PBS_IGNORE,       BTA_PBS_LISTEN_ST}
};

/* state table for wait for authentication response state */
static const UINT8 bta_pbs_st_w4_auth[][BTA_PBS_NUM_COLS] =
{
/* Event                          Action 1                  Next state */
/* BTA_PBS_API_DISABLE_EVT   */   {BTA_PBS_API_DISABLE,     BTA_PBS_IDLE_ST},
/* BTA_PBS_API_AUTHRSP_EVT   */   {BTA_PBS_API_AUTHRSP,     BTA_PBS_LISTEN_ST},
/* BTA_PBS_API_ACCESSRSP_EVT */   {BTA_PBS_IGNORE,          BTA_PBS_W4_AUTH_ST},
/* BTA_PBS_API_CLOSE_EVT     */   {BTA_PBS_API_CLOSE,       BTA_PBS_CLOSING_ST},
/* BTA_PBS_CI_READ_EVT       */   {BTA_PBS_IGNORE,          BTA_PBS_W4_AUTH_ST},
/* BTA_PBS_CI_OPEN_EVT       */   {BTA_PBS_IGNORE,          BTA_PBS_W4_AUTH_ST},
/* BTA_PBS_CI_VLIST_EVT      */   {BTA_PBS_IGNORE,          BTA_PBS_W4_AUTH_ST},
/* BTA_PBS_OBX_CONN_EVT      */   {BTA_PBS_CONN_ERR_RSP,    BTA_PBS_W4_AUTH_ST},
/* BTA_PBS_OBX_DISC_EVT      */   {BTA_PBS_OBX_DISC,        BTA_PBS_W4_AUTH_ST},
/* BTA_PBS_OBX_ABORT_EVT     */   {BTA_PBS_IGNORE,          BTA_PBS_W4_AUTH_ST},
/* BTA_PBS_OBX_PASSWORD_EVT  */   {BTA_PBS_IGNORE,          BTA_PBS_W4_AUTH_ST},
/* BTA_PBS_OBX_CLOSE_EVT     */   {BTA_PBS_OBX_CLOSE,       BTA_PBS_CLOSING_ST},
/* BTA_PBS_OBX_GET_EVT       */   {BTA_PBS_IGNORE,          BTA_PBS_W4_AUTH_ST},
/* BTA_PBS_OBX_SETPATH_EVT   */   {BTA_PBS_IGNORE,          BTA_PBS_W4_AUTH_ST},
/* BTA_PBS_APPL_TOUT_EVT     */   {BTA_PBS_APPL_TOUT,       BTA_PBS_W4_AUTH_ST},
/* BTA_PBS_DISC_ERR_EVT      */   {BTA_PBS_IGNORE,          BTA_PBS_W4_AUTH_ST},
/* BTA_PBS_GASP_ERR_EVT      */   {BTA_PBS_IGNORE,          BTA_PBS_W4_AUTH_ST},
/* BTA_PBS_CLOSE_CMPL_EVT    */   {BTA_PBS_IGNORE,          BTA_PBS_W4_AUTH_ST}
};

/* state table for open state */
static const UINT8 bta_pbs_st_connected[][BTA_PBS_NUM_COLS] =
{
/* Event                          Action 1                  Next state */
/* BTA_PBS_API_DISABLE_EVT   */   {BTA_PBS_API_DISABLE,     BTA_PBS_IDLE_ST},
/* BTA_PBS_API_AUTHRSP_EVT   */   {BTA_PBS_IGNORE,          BTA_PBS_CONN_ST},
/* BTA_PBS_API_ACCESSRSP_EVT */   {BTA_PBS_API_ACCESSRSP,   BTA_PBS_CONN_ST},
/* BTA_PBS_API_CLOSE_EVT     */   {BTA_PBS_API_CLOSE,       BTA_PBS_CLOSING_ST},
/* BTA_PBS_CI_READ_EVT       */   {BTA_PBS_CI_READ,         BTA_PBS_CONN_ST},
/* BTA_PBS_CI_OPEN_EVT       */   {BTA_PBS_CI_OPEN,         BTA_PBS_CONN_ST},
/* BTA_PBS_CI_VLIST_EVT       */  {BTA_PBS_CI_VLIST,        BTA_PBS_CONN_ST},
/* BTA_PBS_OBX_CONN_EVT      */   {BTA_PBS_CONN_ERR_RSP,    BTA_PBS_CONN_ST},
/* BTA_PBS_OBX_DISC_EVT      */   {BTA_PBS_OBX_DISC,        BTA_PBS_CLOSING_ST},
/* BTA_PBS_OBX_ABORT_EVT     */   {BTA_PBS_OBX_ABORT,       BTA_PBS_CONN_ST},
/* BTA_PBS_OBX_PASSWORD_EVT  */   {BTA_PBS_IGNORE,          BTA_PBS_W4_AUTH_ST},
/* BTA_PBS_OBX_CLOSE_EVT     */   {BTA_PBS_OBX_CLOSE,       BTA_PBS_CLOSING_ST},
/* BTA_PBS_OBX_GET_EVT       */   {BTA_PBS_OBX_GET,         BTA_PBS_CONN_ST},
/* BTA_PBS_OBX_SETPATH_EVT   */   {BTA_PBS_OBX_SETPATH,     BTA_PBS_CONN_ST},
/* BTA_PBS_APPL_TOUT_EVT     */   {BTA_PBS_APPL_TOUT,       BTA_PBS_LISTEN_ST},
/* BTA_PBS_DISC_ERR_EVT      */   {BTA_PBS_IGNORE,          BTA_PBS_CONN_ST},
/* BTA_PBS_GASP_ERR_EVT      */   {BTA_PBS_GASP_ERR_RSP,    BTA_PBS_CONN_ST},
/* BTA_PBS_CLOSE_CMPL_EVT    */   {BTA_PBS_IGNORE,          BTA_PBS_CONN_ST}
};

/* state table for closing state */
static const UINT8 bta_pbs_st_closing[][BTA_PBS_NUM_COLS] =
{
/* Event                          Action 1                  Next state */
/* BTA_PBS_API_DISABLE_EVT   */   {BTA_PBS_API_DISABLE,     BTA_PBS_IDLE_ST},
/* BTA_PBS_API_AUTHRSP_EVT   */   {BTA_PBS_IGNORE,          BTA_PBS_CLOSING_ST},
/* BTA_PBS_API_ACCESSRSP_EVT */   {BTA_PBS_IGNORE,          BTA_PBS_CLOSING_ST},
/* BTA_PBS_API_CLOSE_EVT     */   {BTA_PBS_IGNORE,          BTA_PBS_CLOSING_ST},
/* BTA_PBS_CI_READ_EVT       */   {BTA_PBS_CLOSE_COMPLETE,  BTA_PBS_LISTEN_ST},
/* BTA_PBS_CI_OPEN_EVT       */   {BTA_PBS_CLOSE_COMPLETE,  BTA_PBS_LISTEN_ST},
/* BTA_PBS_CI_VLIST_EVT      */   {BTA_PBS_CLOSE_COMPLETE,  BTA_PBS_LISTEN_ST},
/* BTA_PBS_OBX_CONN_EVT      */   {BTA_PBS_CONN_ERR_RSP,    BTA_PBS_CLOSING_ST},
/* BTA_PBS_OBX_DISC_EVT      */   {BTA_PBS_IGNORE,          BTA_PBS_CLOSING_ST},
/* BTA_PBS_OBX_ABORT_EVT     */   {BTA_PBS_OBX_ABORT,       BTA_PBS_CLOSING_ST},
/* BTA_PBS_OBX_PASSWORD_EVT  */   {BTA_PBS_IGNORE,          BTA_PBS_CLOSING_ST},
/* BTA_PBS_OBX_CLOSE_EVT     */   {BTA_PBS_OBX_CLOSE,       BTA_PBS_CLOSING_ST},
/* BTA_PBS_OBX_GET_EVT       */   {BTA_PBS_IGNORE,          BTA_PBS_CLOSING_ST},
/* BTA_PBS_OBX_SETPATH_EVT   */   {BTA_PBS_IGNORE,          BTA_PBS_CLOSING_ST},
/* BTA_PBS_APPL_TOUT_EVT     */   {BTA_PBS_IGNORE,          BTA_PBS_CLOSING_ST},
/* BTA_PBS_DISC_ERR_EVT      */   {BTA_PBS_DISC_ERR_RSP,    BTA_PBS_CONN_ST},
/* BTA_PBS_GASP_ERR_EVT      */   {BTA_PBS_GASP_ERR_RSP,    BTA_PBS_CLOSING_ST},
/* BTA_PBS_CLOSE_CMPL_EVT    */   {BTA_PBS_CLOSE_COMPLETE,  BTA_PBS_LISTEN_ST}
};

/* type for state table */
typedef const UINT8 (*tBTA_PBS_ST_TBL)[BTA_PBS_NUM_COLS];

/* state table */
const tBTA_PBS_ST_TBL bta_pbs_st_tbl[] =
{
    bta_pbs_st_idle,
    bta_pbs_st_listen,
    bta_pbs_st_w4_auth,
    bta_pbs_st_connected,
    bta_pbs_st_closing
};

/*****************************************************************************
** Global data
*****************************************************************************/

/* PBS control block */
#if BTA_DYNAMIC_MEMORY == FALSE
tBTA_PBS_CB  bta_pbs_cb;
#endif

#if BTA_PBS_DEBUG == TRUE
static char *pbs_evt_code(tBTA_PBS_INT_EVT evt_code);
static char *pbs_state_code(tBTA_PBS_STATE state_code);
#endif

/*******************************************************************************
**
** Function         bta_pbs_sm_execute
**
** Description      State machine event handling function for PBS
**                  
**
** Returns          void
**
*******************************************************************************/
void bta_pbs_sm_execute(tBTA_PBS_CB *p_cb, UINT16 event, tBTA_PBS_DATA *p_data)
{
    tBTA_PBS_ST_TBL     state_table;
    UINT8               action;
    int                 i;
    tBTA_PBS_OBX_EVENT  *p_obex_event;
#if BTA_PBS_DEBUG == TRUE
    tBTA_PBS_STATE in_state = bta_pbs_cb.state;
    UINT16         in_event = event;
    APPL_TRACE_EVENT3("PBS Event        : State 0x%02x [%s], Event [%s]", in_state,
                      pbs_state_code(in_state),
                      pbs_evt_code(event));
#endif

    /* look up the state table for the current state */
    state_table = bta_pbs_st_tbl[p_cb->state];

    event &= 0x00FF;

    /* set next state */
    p_cb->state = state_table[event][BTA_PBS_NEXT_STATE];

    /* execute action functions */
    for (i = 0; i < BTA_PBS_ACTIONS; i++)
    {
        if ((action = state_table[event][i]) != BTA_PBS_IGNORE)
        {
            (*bta_pbs_action[action])(p_cb, p_data);
        }
        else
        {
            if (event >= BTA_PBS_OBX_CONN_EVT && event <= BTA_PBS_OBX_SETPATH_EVT) {
                p_obex_event = (tBTA_PBS_OBX_EVENT *) p_data;
                utl_freebuf((void**)&(p_obex_event->p_pkt));
            }
            break;
        }
    }

#if BTA_PBS_DEBUG == TRUE
    if (in_state != bta_pbs_cb.state)
    {
        APPL_TRACE_DEBUG3("PBS State Change: [%s] -> [%s] after Event [%s]",
                      pbs_state_code(in_state),
                      pbs_state_code(bta_pbs_cb.state),
                      pbs_evt_code(in_event));
    }
#endif
}

/*******************************************************************************
**
** Function         bta_pbs_api_enable
**
** Description      Handle an api enable event.  This function enables the PBS
**                  Server by opening an Obex/Rfcomm channel and placing it into
**                  listen mode.
**                  
**
** Returns          void
**
*******************************************************************************/
static void bta_pbs_api_enable(tBTA_PBS_CB *p_cb, tBTA_PBS_DATA *p_data)
{
    tOBX_StartParams     start_msg;
    tBTA_PBS_API_ENABLE *p_api = &p_data->api_enable;
    tOBX_TARGET          target;
    UINT16               len;
    tOBX_STATUS          status;
    tBTA_UTL_COD         cod;
    
    /* initialize control block */
    memset(p_cb, 0, sizeof(tBTA_PBS_CB));
    
    /* Allocate an aligned memory buffer to hold the root path and working directory */
    /* Add 1 byte for '\0' */
    len = p_bta_fs_cfg->max_path_len + 1;
    if ((p_cb->p_rootpath = (char *)GKI_getbuf((UINT16)(len * 2))) != NULL)
    {
        p_cb->p_workdir = p_cb->p_rootpath + len;
        memcpy(target.target, BTA_PBS_TARGET_UUID, BTA_PBS_UUID_LENGTH);
        target.len = BTA_PBS_UUID_LENGTH;
        
        /* store parameters */
        p_cb->app_id = p_api->app_id;
        p_cb->p_cback = p_api->p_cback;
        p_cb->scn = BTM_AllocateSCN();
        p_cb->auth_enabled = p_api->auth_enabled;
        p_cb->fd = BTA_FS_INVALID_FD;
        p_cb->realm_len = p_api->realm_len;
        memcpy(p_cb->realm, p_api->realm, p_api->realm_len);
        
        /* Initialize the current working directory to be the root directory */
        BCM_STRNCPY_S(p_cb->p_rootpath, len, p_api->p_root_path, p_bta_fs_cfg->max_path_len);
        p_cb->p_rootpath[len-1] = '\0';
        BCM_STRNCPY_S(p_cb->p_workdir, len, p_api->p_root_path, p_bta_fs_cfg->max_path_len);
        p_cb->p_workdir[len-1] = '\0';
        
        /* Register PBS security requirements with BTM */
        BTM_SetSecurityLevel(FALSE, p_api->servicename, BTM_SEC_SERVICE_PBAP,
            p_api->sec_mask, BT_PSM_RFCOMM,
            BTM_SEC_PROTO_RFCOMM, (UINT32)p_cb->scn);
        
        /* Start up the PBS service */
        memset (&start_msg, 0, sizeof(tOBX_StartParams));
        start_msg.p_target = &target;

        /* Make the MTU fit into one RFC frame */
        start_msg.mtu = OBX_MAX_MTU;
        start_msg.scn = p_cb->scn;
        start_msg.authenticate = p_cb->auth_enabled;

        start_msg.auth_option = (p_bta_pbs_cfg->userid_req) ? OBX_AO_USR_ID : OBX_AO_NONE;
        start_msg.p_cback = bta_pbs_obx_cback;
        
        start_msg.realm_len = p_api->realm_len;
        start_msg.p_realm = p_api->realm;
        start_msg.realm_charset = (tOBX_CHARSET) p_bta_pbs_cfg->realm_charset;
 
        if ((status = OBX_StartServer (&start_msg, &p_cb->obx_handle)) == OBX_SUCCESS)
        {
            p_cb->state = BTA_PBS_LISTEN_ST;
            
            /* Set the File Transfer service class bit */
            cod.service = BTM_COD_SERVICE_OBJ_TRANSFER;
            utl_set_device_class(&cod, BTA_UTL_SET_COD_SERVICE_CLASS);
            
            /* Set up the SDP record for pbs service */
            bta_pbs_sdp_register(p_cb, p_api->servicename);
        }
        else
            APPL_TRACE_ERROR1("OBX_StartServer returns error (%d)", status);
    }
    else    /* Cannot allocate resources to run Server */
        APPL_TRACE_ERROR0("Not enough Resources to run PBS Server");

    p_cb->p_cback(BTA_PBS_ENABLE_EVT, 0);
}

/*******************************************************************************
**
** Function         bta_pbs_hdl_event
**
** Description      File transfer server main event handling function.
**                  
**
** Returns          void
**
*******************************************************************************/
BOOLEAN bta_pbs_hdl_event(BT_HDR *p_msg)
{
#if BTA_PBS_DEBUG == TRUE
    tBTA_PBS_STATE in_state = bta_pbs_cb.state;
#endif

    switch (p_msg->event)
    {
        case BTA_PBS_API_ENABLE_EVT:
#if BTA_PBS_DEBUG == TRUE
            APPL_TRACE_EVENT3("PBS Event Handler: State 0x%02x [%s], Event [%s]", in_state,
                              pbs_state_code(in_state),
                              pbs_evt_code(p_msg->event));
#endif
            bta_pbs_api_enable(&bta_pbs_cb, (tBTA_PBS_DATA *) p_msg);

#if BTA_PBS_DEBUG == TRUE
            if (in_state != bta_pbs_cb.state)
            {
                APPL_TRACE_DEBUG3("PBS State Change: [%s] -> [%s] after Event [%s]",
                              pbs_state_code(in_state),
                              pbs_state_code(bta_pbs_cb.state),
                              pbs_evt_code(p_msg->event));
            }
#endif
            break;

        default:
            bta_pbs_sm_execute(&bta_pbs_cb, p_msg->event, (tBTA_PBS_DATA *) p_msg);
            break;
    }

    return (TRUE);
}


/*****************************************************************************
**  Debug Functions
*****************************************************************************/
#if BTA_PBS_DEBUG == TRUE

/*******************************************************************************
**
** Function         pbs_evt_code
**
** Description      
**
** Returns          void
**
*******************************************************************************/
static char *pbs_evt_code(tBTA_PBS_INT_EVT evt_code)
{
    switch(evt_code)
    {
    case BTA_PBS_API_DISABLE_EVT:
        return "BTA_PBS_API_DISABLE_EVT";
    case BTA_PBS_API_AUTHRSP_EVT:
        return "BTA_PBS_API_AUTHRSP_EVT";
    case BTA_PBS_API_ACCESSRSP_EVT:
        return "BTA_PBS_API_ACCESSRSP_EVT";
    case BTA_PBS_API_CLOSE_EVT:
        return "BTA_PBS_API_CLOSE_EVT";
    case BTA_PBS_CI_READ_EVT:
        return "BTA_PBS_CI_READ_EVT";
    case BTA_PBS_CI_OPEN_EVT:
        return "BTA_PBS_CI_OPEN_EVT";
    case BTA_PBS_CI_VLIST_EVT:
        return "BTA_PBS_CI_VLIST_EVT";
    case BTA_PBS_OBX_CONN_EVT:
        return "BTA_PBS_OBX_CONN_EVT";
    case BTA_PBS_OBX_DISC_EVT:
        return "BTA_PBS_OBX_DISC_EVT";
    case BTA_PBS_OBX_ABORT_EVT:
        return "BTA_PBS_OBX_ABORT_EVT";
    case BTA_PBS_OBX_PASSWORD_EVT:
        return "BTA_PBS_OBX_PASSWORD_EVT";
    case BTA_PBS_OBX_CLOSE_EVT:
        return "BTA_PBS_OBX_CLOSE_EVT";
    case BTA_PBS_OBX_GET_EVT:
        return "BTA_PBS_OBX_GET_EVT";
    case BTA_PBS_OBX_SETPATH_EVT:
        return "BTA_PBS_OBX_SETPATH_EVT";
    case BTA_PBS_APPL_TOUT_EVT:
        return "BTA_PBS_APPL_TOUT_EVT";
    case BTA_PBS_DISC_ERR_EVT:
        return "BTA_PBS_DISC_ERR_EVT";
    case BTA_PBS_GASP_ERR_EVT:
        return "BTA_PBS_GASP_ERR_EVT";
    case BTA_PBS_API_ENABLE_EVT:
        return "BTA_PBS_API_ENABLE_EVT";
    case BTA_PBS_CLOSE_CMPL_EVT:
        return "BTA_PBS_CLOSE_CMPL_EVT";
    default:
        APPL_TRACE_EVENT1("unknown PBS Event: %d", evt_code)
        return "unknown PBS event code";
    }
}

/*******************************************************************************
**
** Function         pbs_evt_code
**
** Description      
**
** Returns          void
**
*******************************************************************************/
static char *pbs_state_code(tBTA_PBS_STATE state_code)
{
    switch(state_code)
    {
    case BTA_PBS_IDLE_ST:
        return "BTA_PBS_IDLE_ST";
    case BTA_PBS_LISTEN_ST:
        return "BTA_PBS_LISTEN_ST";
    case BTA_PBS_W4_AUTH_ST:
        return "BTA_PBS_W4_AUTH_ST";
    case BTA_PBS_CONN_ST:
        return "BTA_PBS_CONN_ST";
    case BTA_PBS_CLOSING_ST:
        return "BTA_PBS_CLOSING_ST";
    default:
        return "unknown PBS state code";
    }
}

#endif  /* Debug Functions */
#endif /* BTA_PBS_INCLUDED */