xref: /btstack/src/btstack_hsm.h (revision f05358500e612946f8c340b1f11e5703dafd65f8)
1 /*
2  * Copyright (C) 2024 BlueKitchen GmbH
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the copyright holders nor the names of
14  *    contributors may be used to endorse or promote products derived
15  *    from this software without specific prior written permission.
16  * 4. Any redistribution, use, or modification is done solely for
17  *    personal benefit and not for any commercial purpose or for
18  *    monetary gain.
19  *
20  * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
21  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL BLUEKITCHEN
24  * GMBH OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
27  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
28  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
30  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  * Please inquire about commercial licensing options at
34  * [email protected]
35  *
36  */
37 
38 /**
39  * @title Hierarchical State Machine (HSM)
40  *
41  */
42 
43 #ifndef BTSTACK_HSM_H
44 #define BTSTACK_HSM_H
45 
46 #if defined __cplusplus
47 extern "C" {
48 #endif
49 
50 #include <stdint.h>
51 
52 typedef uint16_t btstack_hsm_signal_t;
53 enum btstack_hsm_reserved_signals_e {
54     BTSTACK_HSM_EMPTY_SIG,
55     BTSTACK_HSM_INIT_SIG,
56     BTSTACK_HSM_ENTRY_SIG,
57     BTSTACK_HSM_EXIT_SIG,
58     BTSTACK_HSM_USER_SIG
59 };
60 
61 typedef struct {
62     btstack_hsm_signal_t sig;
63 } btstack_hsm_event_t;
64 typedef struct btstack_hsm_s btstack_hsm_t;
65 typedef enum {
66     BTSTACK_HSM_TRAN_STATUS,
67     BTSTACK_HSM_SUPER_STATUS,
68     BTSTACK_HSM_HANDLED_STATUS,
69     BTSTACK_HSM_UNHANDLED_STATUS,
70     BTSTACK_HSM_IGNORED_STATUS
71 } btstack_hsm_state_t;
72 
73 typedef btstack_hsm_state_t (*btstack_hsm_state_handler_t)(btstack_hsm_t * const me, btstack_hsm_event_t const * const e);
74 
75 struct btstack_hsm_s {
76     btstack_hsm_state_handler_t state;
77     btstack_hsm_state_handler_t temp;
78     btstack_hsm_state_handler_t *path;
79     int_fast8_t depth;
80 };
81 
82 /* API_START */
83 
84 /*
85  * @brief Request the transition from the current state to the given new state
86  * @param me the current state machine
87  * @param target the new state to transit to
88  * @result transition status
89  */
90 btstack_hsm_state_t btstack_hsm_transit(btstack_hsm_t * const me, btstack_hsm_state_handler_t const target);
91 
92 /*
93  * @brief Specifies the upper state in a state hierarchy
94  * @param me the current state machine
95  * @param target the next parent state in the hierarchy
96  * @result transition status
97  */
98 btstack_hsm_state_t btstack_hsm_super(btstack_hsm_t * const me, btstack_hsm_state_handler_t const target);
99 
100 /*
101  * @brief Placeholder state to mark the top most state in a state hierarchy, the root state. Ignores all events.
102  * @param me the current state machine
103  * @param e event
104  * @result ignored status
105  */
106 btstack_hsm_state_t btstack_hsm_top(btstack_hsm_t * const me, btstack_hsm_event_t const * const e);
107 
108 /*
109  * @brief Constructs a new state hierarchical machine machine, with storage for maximum hierarchy depth.
110  * @param me the current state machine
111  * @param initial the initial state
112  * @param array of btstack_hsm_state_handler_t elements with the same number of elements as the maximum number of nested state machines.
113  * @param The number of nested state machines.
114  */
115 void btstack_hsm_constructor(btstack_hsm_t * const me, btstack_hsm_state_handler_t initial, btstack_hsm_state_handler_t path[], int8_t depth);
116 
117 /*
118  * @brief Takes the initial transition of the state machine and sending it a BTSTACK_HSM_INIT_SIG
119  * @param me the current state machine
120  * @param e event
121  */
122 void btstack_hsm_init(btstack_hsm_t * const me, btstack_hsm_event_t const * const e);
123 
124 /*
125  * @brief Dispatches the given event to the state machine, if a transition is requested, leave the old states and enter the new on.
126  *        Honoring the hierarchy and handling entering/exiting all states on the way.
127  * @param me the current state machine
128  * @param e event
129  */
130 btstack_hsm_state_t btstack_hsm_dispatch(btstack_hsm_t * const me, btstack_hsm_event_t const * const e);
131 
132 /* API_END */
133 
134 #if defined __cplusplus
135 }
136 #endif
137 
138 #endif
139