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
|
/*****************************************************************************
**
** Name: obx_ssm.c
**
** File: OBEX Server State Machine and Control Block Access Functions
**
** Copyright (c) 2003-2011, Broadcom Corp., All Rights Reserved.
** Broadcom Bluetooth Core. Proprietary and confidential.
**
*****************************************************************************/
#include <string.h>
#include "btu.h" /* for timer */
#include "obx_int.h"
/* OBEX Server Action Functions Enums (must match obx_sr_action [below] */
enum
{
OBX_SA_CLOSE_PORT,
OBX_SA_CONNECTION_ERROR,
OBX_SA_STATE,
OBX_SA_CONNECT_IND,
OBX_SA_WC_CONN_IND,
OBX_SA_NC_TO,
OBX_SA_CONNECT_RSP,
OBX_SA_AUTH_IND,
OBX_SA_SND_RSP,
OBX_SA_SAVE_REQ,
OBX_SA_SND_PART,
OBX_SA_REJ_REQ,
OBX_SA_ABORT_RSP,
OBX_SA_OP_RSP,
OBX_SA_GET_IND,
OBX_SA_GET_REQ,
OBX_SA_SESSION_IND,
OBX_SA_SESS_CONN_IND,
OBX_SA_WC_SESS_IND,
OBX_SA_SESSION_RSP,
OBX_SA_PUT_IND,
OBX_SA_SRM_PUT_REQ,
OBX_SA_SRM_PUT_RSP,
OBX_SA_SRM_GET_FCS,
OBX_SA_SRM_GET_RSP,
OBX_SA_SRM_GET_REQ,
OBX_SA_CLEAN_PORT
};
/* OBEX Server Action Functions */
static const tOBX_SR_ACT obx_sr_action[] =
{
obx_sa_close_port,
obx_sa_connection_error,
obx_sa_state,
obx_sa_connect_ind,
obx_sa_wc_conn_ind,
obx_sa_nc_to,
obx_sa_connect_rsp,
obx_sa_auth_ind,
obx_sa_snd_rsp,
obx_sa_save_req,
obx_sa_snd_part,
obx_sa_rej_req,
obx_sa_abort_rsp,
obx_sa_op_rsp,
obx_sa_get_ind,
obx_sa_get_req,
obx_sa_session_ind,
obx_sa_sess_conn_ind,
obx_sa_wc_sess_ind,
obx_sa_session_rsp,
obx_sa_put_ind,
obx_sa_srm_put_req,
obx_sa_srm_put_rsp,
obx_sa_srm_get_fcs,
obx_sa_srm_get_rsp,
obx_sa_srm_get_req,
obx_sa_clean_port
};
/************ OBX Server FSM State/Event Indirection Table **************/
/* obx_ssm_event() first looks at obx_ssm_entry_map[][] to get an entry of the event of a particular state
* 0 means the event in the current state is ignored.
* a number with 0x80 bit set, use obx_sr_all_table[][] as the "state table".
* other numbers, look up obx_sr_main_state_table[] for the state table of current state.
*
* once the state table is determined,
* look up the "action" column to find the associated action function
* and the "next state" column to find the "next state" candidate.
*
* The actual next state could be either the state in the "next state" column
* or the state returned from the action function.
*/
static const UINT8 obx_ssm_entry_map[][OBX_SS_MAX-1] =
{
/* state name: NtCon SesIn CntIn WtAut AutIn Conn DscIn StpIn ActIn AbtIn PutIn GetIn Put Get PutS GetS Part WtCls */
/* CONN_R */{ 1, 0x82, 0x82, 1, 0x82, 0x86, 0x82, 0x82, 0x82, 0x82, 0x82, 0x82, 0x82, 0x82, 0x82, 0x82, 0x82 ,1 },
/* SESS_R */{ 3, 0x82, 0x82, 0x82, 0x82, 0x88, 3, 0x85, 0x85, 0x85, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 1 ,3 },
/* DISCNT_R */{ 0x86, 0x82, 0x82, 0x82, 0x82, 0x85, 0x82, 0x85, 0x85, 0x85, 0x85, 0x85, 0x85, 0x85, 0x85, 0x85, 1 ,0x86 },
/* PUT_R */{ 0x86, 0x82, 0x82, 0x82, 0x82, 1, 0x87, 0x87, 0x87, 0x82, 0x87, 0x87, 1, 0x82, 1, 0x82, 0x82 ,0x86 },
/* GET_R */{ 0x86, 0x82, 0x82, 0x82, 0x82, 2, 0x87, 0x87, 0x87, 0x82, 0x87, 0x87, 0x82, 1, 0x82, 1, 0x82 ,0x86 },
/* SETPATH_R*/{ 0x86, 0x82, 0x82, 0x82, 0x82, 3, 0x87, 0x87, 0x87, 0x82, 0x87, 0x87, 0x82, 0x82, 0x82, 0x82, 0x82 ,0x86 },
/* ACTION_R */{ 0x86, 0x82, 0x82, 0x82, 0x82, 4, 0x87, 0x87, 0x87, 0x82, 0x87, 0x87, 0x82, 0x82, 0x82, 0x82, 0x82 ,0x86 },
/* ABORT_R */{ 0x86, 0x82, 0x82, 0x82, 0x82, 0x86, 0x82, 0x82, 0x82, 0x82, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 1 ,0x86 },
/* CONN_C */{ 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ,0 },
/* SESS_C */{ 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ,0 },
/* DISCNT_C */{ 0, 0x82, 0x82, 0x82, 0x82, 0x82, 1, 0x82, 0x82, 0x82, 0x82, 0x82, 0x82, 0x82, 0x82, 0x82, 0x82 ,0 },
/* PUT_C */{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 2, 0, 0 ,0 },
/* GET_C */{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 2, 0 ,0 },
/* SETPATH_C*/{ 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0 ,0 },
/* ACTION_C */{ 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0 ,0 },
/* ABORT_C */{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0 ,0 },
/* PORT_CLS */{ 0x83, 0x83, 0x83, 0x83, 0x83, 0x83, 0x83, 0x83, 0x83, 0x83, 0x83, 0x83, 0x83, 0x83, 0x83, 0x83, 0x83 ,0x83 },
/* FCS_SET */{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 2 ,0 },
/* STATE */{ 0x84, 0x84, 0x84, 0x84, 0x84, 0x84, 0x84, 0x84, 0x84, 0x84, 0x84, 0x84, 0x84, 0x84, 0x84, 0x84, 0x84 ,0x84 },
/* TIMEOUT */{ 2, 0, 0, 2, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ,2 },
/* BAD_REQ */{ 0x86, 0x82, 0x82, 0x82, 0x82, 0x86, 0x82, 0x82, 0x82, 0x82, 0x82, 0x82, 0x82, 0x82, 0x82, 0x82, 0x82 ,0x86 },
/* TX_EMPTY */{ 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ,0 }
};
static const UINT8 obx_sr_all_table[][OBX_SM_NUM_COLS] = {
/* Event Action Next State */
/* ABORT_R */{OBX_SM_NO_ACTION, OBX_SS_ABORT_INDICATED },
/* misc */{OBX_SA_CLOSE_PORT, OBX_SS_NOT_CONNECTED },
/* PORT_CLS */{OBX_SA_CONNECTION_ERROR, OBX_SS_NOT_CONNECTED },
/* STATE */{OBX_SA_STATE, OBX_SS_NULL },
/* DISCNT_R */{OBX_SM_NO_ACTION, OBX_SS_DISCNT_INDICATED },
/* misc */{OBX_SA_REJ_REQ, OBX_SS_NULL },
/* illegalop*/{OBX_SA_CLEAN_PORT, OBX_SS_NOT_CONNECTED },
/* SESS_R */{OBX_SA_SESSION_IND, OBX_SS_SESS_INDICATED }
};
static const UINT8 obx_sr_not_conn_table[][OBX_SM_NUM_COLS] = {
/* Event Action Next State */
/* CONN_R */{OBX_SA_CONNECT_IND, OBX_SS_CONN_INDICATED },
/* TIMEOUT */{OBX_SA_NC_TO, OBX_SS_NOT_CONNECTED},
/* SESS_R */{OBX_SA_SESS_CONN_IND, OBX_SS_SESS_INDICATED }
};
static const UINT8 obx_sr_connect_ind_table[][OBX_SM_NUM_COLS] = {
/* Event Action Next State */
/* CONN_C */{OBX_SA_CONNECT_RSP, OBX_SS_CONNECTED }
};
static const UINT8 obx_sr_session_ind_table[][OBX_SM_NUM_COLS] = {
/* Event Action Next State */
/* SESS_R */{OBX_SA_SESSION_RSP, OBX_SS_NOT_CONNECTED }
};
static const UINT8 obx_sr_wait_auth_table[][OBX_SM_NUM_COLS] = {
/* Event Action Next State */
/* CONN_R */{OBX_SA_AUTH_IND, OBX_SS_AUTH_INDICATED },
/* TIMEOUT */{OBX_SA_CLOSE_PORT, OBX_SS_NOT_CONNECTED}
};
static const UINT8 obx_sr_conn_table[][OBX_SM_NUM_COLS] = {
/* Event Action Next State */
/* PUT_R */{OBX_SA_PUT_IND, OBX_SS_PUT_INDICATED },
/* GET_R */{OBX_SA_GET_IND, OBX_SS_GET_INDICATED },
/* SETPATH_R*/{OBX_SM_NO_ACTION, OBX_SS_SETPATH_INDICATED },
/* ACTION_R */{OBX_SM_NO_ACTION, OBX_SS_ACTION_INDICATED }
};
static const UINT8 obx_sr_disconnect_ind_table[][OBX_SM_NUM_COLS] = {
/* Event Action Next State */
/* DISCNT_C */{OBX_SA_SND_RSP, OBX_SS_WAIT_CLOSE },
/* TIMEOUT */{OBX_SA_CLOSE_PORT, OBX_SS_NOT_CONNECTED},
/* SESS_R */{OBX_SA_SESSION_IND, OBX_SS_DISCNT_INDICATED}
};
static const UINT8 obx_sr_setpath_ind_table[][OBX_SM_NUM_COLS] = {
/* Event Action Next State */
/* SETPATH_C*/{OBX_SA_SND_RSP, OBX_SS_CONNECTED }
};
static const UINT8 obx_sr_abort_ind_table[][OBX_SM_NUM_COLS] = {
/* Event Action Next State */
/* GET/PUT_C*/{OBX_SA_OP_RSP, OBX_SS_ABORT_INDICATED },
/* ABORT_C */{OBX_SA_ABORT_RSP, OBX_SS_CONNECTED }
};
static const UINT8 obx_sr_put_ind_table[][OBX_SM_NUM_COLS] = {
/* Event Action Next State */
/* PUT_C */{OBX_SA_SND_RSP, OBX_SS_PUT_TRANSACTION }
};
static const UINT8 obx_sr_get_ind_table[][OBX_SM_NUM_COLS] = {
/* Event Action Next State */
/* GET_C */{OBX_SA_SND_RSP, OBX_SS_GET_TRANSACTION }
};
static const UINT8 obx_sr_put_table[][OBX_SM_NUM_COLS] = {
/* Event Action Next State */
/* PUT_R */{OBX_SM_NO_ACTION, OBX_SS_PUT_INDICATED }
};
static const UINT8 obx_sr_get_table[][OBX_SM_NUM_COLS] = {
/* Event Action Next State */
/* GET_R */{OBX_SA_GET_REQ, OBX_SS_GET_INDICATED }
};
static const UINT8 obx_sr_put_s_table[][OBX_SM_NUM_COLS] = {
/* Event Action Next State */
/* PUT_R */{OBX_SA_SRM_PUT_REQ, OBX_SS_PUT_SRM },
/* PUT_C */{OBX_SA_SRM_PUT_RSP, OBX_SS_PUT_SRM }
};
static const UINT8 obx_sr_get_s_table[][OBX_SM_NUM_COLS] = {
/* Event Action Next State */
/* GET_R */{OBX_SA_SRM_GET_REQ, OBX_SS_GET_SRM },
/* GET_C */{OBX_SA_SRM_GET_RSP, OBX_SS_GET_SRM },
/* FCS_SET */{OBX_SA_SRM_GET_FCS, OBX_SS_GET_SRM }
};
static const UINT8 obx_sr_part_table[][OBX_SM_NUM_COLS] = {
/* Event Action Next State */
/* ABORT_R */{OBX_SA_SAVE_REQ, OBX_SS_NULL },/* and DISCNT_R */
/* FCS_SET */{OBX_SA_SND_PART, OBX_SS_NULL }
};
static const UINT8 obx_sr_wait_close_table[][OBX_SM_NUM_COLS] = {
/* Event Action Next State */
/* CONN_R */{OBX_SA_WC_CONN_IND, OBX_SS_CONN_INDICATED },
/* TIMEOUT */{OBX_SA_NC_TO, OBX_SS_NOT_CONNECTED},
/* SESS_R */{OBX_SA_WC_SESS_IND, OBX_SS_SESS_INDICATED }
};
static const tOBX_SM_TBL obx_sr_main_state_table[] = {
obx_sr_not_conn_table,
obx_sr_session_ind_table,
obx_sr_connect_ind_table,
obx_sr_wait_auth_table,
obx_sr_connect_ind_table, /* same table for auth ind */
obx_sr_conn_table,
obx_sr_disconnect_ind_table,
obx_sr_setpath_ind_table,
obx_sr_setpath_ind_table, /* same action table for action_ind */
obx_sr_abort_ind_table,
obx_sr_put_ind_table,
obx_sr_get_ind_table,
obx_sr_put_table,
obx_sr_get_table,
obx_sr_put_s_table,
obx_sr_get_s_table,
obx_sr_part_table,
obx_sr_wait_close_table
};
/*******************************************************************************
**
** Function obx_ssm_event
**
** Description Handle events to the server state machine. It looks up the entry
** in the obx_ssm_entry_map array. If it is a valid entry, it gets
** the state table.Set the next state, if not NULL state.Execute
** the action function according to the state table. If the state
** returned by action function is not NULL state, adjust the new
** state to the returned state.If (api_evt != MAX), call callback
** function.
**
** Returns void.
**
*******************************************************************************/
void obx_ssm_event(tOBX_SR_SESS_CB *p_scb, tOBX_SR_EVENT event, BT_HDR *p_msg)
{
UINT8 curr_state = p_scb->state;
tOBX_SM_TBL state_table;
UINT8 action, entry;
tOBX_SR_STATE act_state = OBX_SS_NULL;
UINT8 *p_data;
UINT16 len;
BT_HDR *p_dummy;
tOBX_EVENT api_evt;
tOBX_SR_CB *p_cb;
#if 0
UINT8 srm;
#endif
if( curr_state == OBX_SS_NULL || curr_state >= OBX_SS_MAX)
{
OBX_TRACE_WARNING1( "Invalid state: %d", curr_state) ;
if(p_msg)
GKI_freebuf(p_msg);
return;
}
OBX_TRACE_DEBUG6( "For Server SHandle 0x%x, State: %s, Event: %s/%d srm:0x%x ssn:%d",
p_scb->ll_cb.comm.handle, obx_sr_get_state_name( p_scb->state ),
obx_sr_get_event_name(event), event, p_scb->srm, p_scb->ssn ) ;
/* look up the state table for the current state */
/* lookup entry /w event & curr_state */
/* If entry is ignore, return.
* Otherwise, get state table (according to curr_state or all_state) */
/* coverity [index_parm] */
if( (entry = obx_ssm_entry_map[event][curr_state-1]) != OBX_SM_IGNORE )
{
if(entry&OBX_SM_ALL)
{
entry &= OBX_SM_ENTRY_MASK;
state_table = obx_sr_all_table;
}
else
state_table = obx_sr_main_state_table[curr_state-1];
}
else
{
OBX_TRACE_WARNING2( "Ignore event %d in state %d", event, curr_state );
if(p_msg)
GKI_freebuf(p_msg);
return;
}
/* Get possible next state from state table. */
if( state_table[entry-1][OBX_SME_NEXT_STATE] != OBX_CS_NULL )
p_scb->state = state_table[entry-1][OBX_SME_NEXT_STATE];
p_scb->prev_state = curr_state;
OBX_TRACE_DEBUG3( "possible new state = %s/%s/%d",
obx_sr_get_state_name(p_scb->state), obx_sr_get_state_name( p_scb->prev_state ), p_scb->prev_state) ;
/* If action is not ignore, clear param, exec action and get next state.
* The action function may set the Param for cback.
* Depending on param, call cback or free buffer. */
/* execute action */
action = state_table[entry-1][OBX_SME_ACTION];
if (action != OBX_SM_NO_ACTION)
{
act_state = (*obx_sr_action[action])(p_scb, p_msg);
}
/* adjust next state, if it needs to use the new state returned from action function */
if( act_state != OBX_CS_NULL)
{
p_scb->state = act_state;
OBX_TRACE_DEBUG1( "new state = %s (action)", obx_sr_get_state_name( p_scb->state ) ) ;
}
if(p_scb->api_evt)
{
api_evt = p_scb->api_evt;
p_scb->api_evt = OBX_NULL_EVT;
/* we do not want the operation to be challenged by the client */
if( event <= OBX_SEVT_MAX_REQ && event != OBX_CONNECT_REQ_SEVT &&
OBX_ReadByteStrHdr(p_msg, OBX_HI_CHALLENGE, &p_data, &len, 0) == TRUE)
{
/* send bad request response */
p_dummy = obx_build_dummy_rsp(p_scb, OBX_RSP_BAD_REQUEST);
event += OBX_SEVT_DIFF_REQ_CFM;
obx_ssm_event(p_scb, event, p_dummy);
GKI_freebuf(p_msg);
}
else
{
p_cb = &obx_cb.server[p_scb->handle - 1];
(p_cb->p_cback) (p_scb->ll_cb.comm.handle, api_evt, p_scb->param, p_msg);
}
memset(&p_scb->param, 0, sizeof (p_scb->param) );
}
else if(action == OBX_SM_NO_ACTION && p_msg)
GKI_freebuf(p_msg);
OBX_TRACE_DEBUG2( "result state = %s ssn:%d", obx_sr_get_state_name( p_scb->state ), p_scb->ssn ) ;
}
|