xref: /btstack/src/btstack_hsm.c (revision bb8a927b64a2bed4835a6cf12ebf71eb29487bd9)
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 #define BTSTACK_FILE__ "btstack_hsm.c"
38 
39 #include <stddef.h>
40 #include <string.h>
41 #include <stdbool.h>
42 
43 #include "btstack_config.h"
44 #include "btstack_debug.h"
45 
46 #include "btstack_hsm.h"
47 
48 btstack_hsm_state_t btstack_hsm_transit(btstack_hsm_t * const me, btstack_hsm_state_handler_t const target) {
49     me->temp = target;
50     return BTSTACK_HSM_TRAN_STATUS;
51 }
52 
53 btstack_hsm_state_t btstack_hsm_super(btstack_hsm_t * const me, btstack_hsm_state_handler_t const target) {
54     me->temp = target;
55     return BTSTACK_HSM_SUPER_STATUS;
56 }
57 
58 btstack_hsm_state_t btstack_hsm_top(btstack_hsm_t * const me, btstack_hsm_event_t const * const e) {
59     UNUSED(me);
60     UNUSED(e);
61     return BTSTACK_HSM_IGNORED_STATUS;
62 }
63 
64 void btstack_hsm_constructor(btstack_hsm_t * const me, btstack_hsm_state_handler_t initial, btstack_hsm_state_handler_t path[], int8_t depth) {
65     me->state = btstack_hsm_top;
66     me->temp = initial;
67     me->path = path;
68     me->depth = depth;
69 }
70 
71 static btstack_hsm_state_t btstack_hsm_get_super( btstack_hsm_t * const me, btstack_hsm_state_handler_t const handler) {
72     // empty event to trigger default state action, a.k. the super state
73     static btstack_hsm_event_t const empty_evt = { BTSTACK_HSM_EMPTY_SIG };
74     return handler( me, &empty_evt );
75 }
76 
77 static btstack_hsm_event_t const entry_evt = { BTSTACK_HSM_ENTRY_SIG };
78 static btstack_hsm_event_t const exit_evt = { BTSTACK_HSM_EXIT_SIG };
79 
80 void btstack_hsm_init(btstack_hsm_t * const me, btstack_hsm_event_t const * const e) {
81     btstack_assert(me->state != NULL);
82     btstack_assert(me->temp != NULL);
83     btstack_hsm_state_handler_t target = me->state;
84     btstack_hsm_state_t status = me->temp(me, e);
85     btstack_assert( status == BTSTACK_HSM_TRAN_STATUS );
86     static btstack_hsm_event_t const init_evt = { BTSTACK_HSM_INIT_SIG };
87 
88     int_fast8_t level;
89     btstack_hsm_state_handler_t *root_path = me->path;
90     memset(root_path, 0, sizeof(btstack_hsm_state_handler_t)*me->depth);
91 
92     do {
93         level = 0;
94         btstack_hsm_state_handler_t current = me->temp;
95         for(; current != target; current=me->temp, level++ ) {
96             root_path[level] = current;
97             btstack_hsm_get_super( me, current );
98         }
99         for(; level>0;) {
100             root_path[--level]( me, &entry_evt );
101         }
102         target = root_path[0];
103         status = target( me, &init_evt );
104     } while (status == BTSTACK_HSM_TRAN_STATUS);
105     btstack_assert( status != BTSTACK_HSM_TRAN_STATUS );
106     me->state = target;
107 }
108 
109 static void btstack_hsm_handler_super_cache( btstack_hsm_t * const me, btstack_hsm_state_handler_t cache[], int idx, btstack_hsm_state_handler_t handler ) {
110     if( cache[idx] == NULL ) {
111         cache[idx] = handler;
112         btstack_hsm_get_super(me, handler);
113     } else {
114         me->temp = cache[idx];
115     }
116 }
117 
118 btstack_hsm_state_t btstack_hsm_dispatch(btstack_hsm_t * const me, btstack_hsm_event_t const * const e) {
119     btstack_hsm_state_t status;
120     btstack_hsm_state_handler_t current = me->state;
121     do {
122         status = current(me, e);
123         // if the state doesn't handle the event try at the super state too
124         if( status == BTSTACK_HSM_UNHANDLED_STATUS ) {
125             status = btstack_hsm_get_super( me, current );
126         }
127         current = me->temp;
128     } while( status == BTSTACK_HSM_SUPER_STATUS );
129 
130     // if we don't switch states we are done now
131     if( status != BTSTACK_HSM_TRAN_STATUS ) {
132         return status;
133     }
134     btstack_hsm_state_handler_t source = me->state;
135     btstack_hsm_state_handler_t target = me->temp;
136     btstack_hsm_state_handler_t *root_path = me->path;
137     memset(root_path, 0, sizeof(btstack_hsm_state_handler_t)*me->depth);
138 
139     // why should that be possible/necessary?
140     btstack_assert( source != target );
141 
142     // handle entry/exit edges
143     int_fast8_t level = 0;
144     bool lca_found = false;
145     // calculates the lowest common ancestor of the state graph
146     for(; source != btstack_hsm_top; source=me->temp) {
147         level = 0;
148         for(current=target; current != btstack_hsm_top; current=me->temp, ++level ) {
149             if( current == source ) {
150                 lca_found = true;
151                 break;
152             }
153 
154             btstack_hsm_handler_super_cache( me, root_path, level, current );
155         }
156         if( lca_found == true ) {
157             break;
158         }
159         source( me, &exit_evt );
160         btstack_hsm_get_super( me, source );
161     }
162 
163     for(level--; level > 0; ) {
164         root_path[--level]( me, &entry_evt );
165     }
166     me->state = target;
167     return status;
168 }
169 
170