1 /*
2  * SPDX-FileCopyrightText: 2015-2021 Espressif Systems (Shanghai) CO LTD
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 
8 /*****************************************************************************
9  *
10  *  Filename:      btc/btc_sm.h
11  *
12  *  Description:   Generic BTC state machine API
13  *
14  *****************************************************************************/
15 
16 #ifndef __BTC_SM_H__
17 #define __BTC_SM_H__
18 
19 /*****************************************************************************
20 **  Constants & Macros
21 ******************************************************************************/
22 
23 /* Generic Enter/Exit state machine events */
24 #define BTC_SM_ENTER_EVT 0xFFFF
25 #define BTC_SM_EXIT_EVT  0xFFFE
26 
27 
28 /*****************************************************************************
29 **  Type definitions and return values
30 ******************************************************************************/
31 typedef UINT32 btc_sm_state_t;
32 typedef UINT32 btc_sm_event_t;
33 typedef void *btc_sm_handle_t;
34 typedef BOOLEAN (* btc_sm_handler_t)(btc_sm_event_t event, void *data);
35 
36 
37 /*****************************************************************************
38 **  Functions
39 **
40 **  NOTE: THESE APIs SHOULD BE INVOKED ONLY IN THE BTC CONTEXT
41 **
42 ******************************************************************************/
43 
44 /*****************************************************************************
45 **
46 ** Function     btc_sm_init
47 **
48 ** Description  Initializes the state machine with the state handlers
49 **              The caller should ensure that the table and the corresponding
50 **              states match. The location that 'p_handlers' points to shall
51 **              be available until the btc_sm_shutdown API is invoked.
52 **
53 ** Returns      Returns a pointer to the initialized state machine handle.
54 **
55 ******************************************************************************/
56 btc_sm_handle_t btc_sm_init(const btc_sm_handler_t *p_handlers,
57                             btc_sm_state_t initial_state);
58 
59 /*****************************************************************************
60 **
61 ** Function     btc_sm_shutdown
62 **
63 ** Description  Tears down the state machine
64 **
65 ** Returns      None
66 **
67 ******************************************************************************/
68 void btc_sm_shutdown(btc_sm_handle_t handle);
69 
70 /*****************************************************************************
71 **
72 ** Function     btc_sm_get_state
73 **
74 ** Description  Fetches the current state of the state machine
75 **
76 ** Returns      Current state
77 **
78 ******************************************************************************/
79 btc_sm_state_t btc_sm_get_state(btc_sm_handle_t handle);
80 
81 /*****************************************************************************
82 **
83 ** Function     btc_sm_dispatch
84 **
85 ** Description  Dispatches the 'event' along with 'data' to the current state handler
86 **
87 ** Returns      Returns BT_STATUS_OK on success, BT_STATUS_FAIL otherwise
88 **
89 ******************************************************************************/
90 bt_status_t btc_sm_dispatch(btc_sm_handle_t handle, btc_sm_event_t event,
91                             void *data);
92 
93 /*****************************************************************************
94 **
95 ** Function     btc_sm_change_state
96 **
97 ** Description  Make a transition to the new 'state'. The 'BTC_SM_EXIT_EVT'
98 **              shall be invoked before exiting the current state. The
99 **              'BTC_SM_ENTER_EVT' shall be invoked before entering the new state
100 **
101 **
102 ** Returns      Returns BT_STATUS_OK on success, BT_STATUS_FAIL otherwise
103 **
104 ******************************************************************************/
105 bt_status_t btc_sm_change_state(btc_sm_handle_t handle, btc_sm_state_t state);
106 
107 #endif /* __BTC_SM_H__ */
108