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 static btstack_hsm_event_t const init_evt = { BTSTACK_HSM_INIT_SIG }; 80 81 void btstack_hsm_init(btstack_hsm_t * const me, btstack_hsm_event_t const * const e) { 82 btstack_assert(me->state != NULL); 83 btstack_assert(me->temp != NULL); 84 btstack_hsm_state_handler_t target = me->state; 85 btstack_hsm_state_t status = me->temp(me, e); 86 btstack_assert( status == BTSTACK_HSM_TRAN_STATUS ); 87 88 btstack_hsm_state_handler_t *root_path = me->path; 89 memset(root_path, 0, sizeof(btstack_hsm_state_handler_t)*me->depth); 90 91 do { 92 int_fast8_t level = 0; 93 btstack_hsm_state_handler_t current = me->temp; 94 for(; current != target; current=me->temp, level++ ) { 95 root_path[level] = current; 96 btstack_hsm_get_super( me, current ); 97 } 98 for(--level; level>=0;--level) { 99 root_path[level]( me, &entry_evt ); 100 } 101 target = root_path[0]; 102 status = target( me, &init_evt ); 103 } while (status == BTSTACK_HSM_TRAN_STATUS); 104 btstack_assert( status != BTSTACK_HSM_TRAN_STATUS ); 105 me->state = target; 106 } 107 108 static void btstack_hsm_handler_super_cache( 109 btstack_hsm_t * const me, 110 btstack_hsm_state_handler_t cache[], 111 int idx, btstack_hsm_state_handler_t handler ) { 112 113 if( idx == me->depth ) { 114 btstack_hsm_get_super(me, handler); 115 if( me->temp != btstack_hsm_top ) { 116 log_error("state machine has higher depth (%d) than specified!", me->depth); 117 btstack_assert( 0 ); 118 } 119 return; 120 } 121 if( cache[idx] == NULL ) { 122 btstack_hsm_get_super(me, handler); 123 cache[idx] = me->temp; 124 } else { 125 me->temp = cache[idx]; 126 } 127 } 128 129 btstack_hsm_state_t btstack_hsm_dispatch(btstack_hsm_t * const me, btstack_hsm_event_t const * const e) { 130 btstack_hsm_state_t status; 131 btstack_hsm_state_handler_t current; 132 // forward event to next hierarchy level if not handled in current state 133 me->temp = me->state; 134 do { 135 current = me->temp; 136 status = current(me, e); 137 // if the state doesn't handle the event try at the super state too 138 if( status == BTSTACK_HSM_UNHANDLED_STATUS ) { 139 status = btstack_hsm_get_super( me, current ); 140 } 141 } while( status == BTSTACK_HSM_SUPER_STATUS ); 142 143 // if we don't switch states we are done now 144 if( status != BTSTACK_HSM_TRAN_STATUS ) { 145 return status; 146 } 147 // save the destination of the previous transition 148 btstack_hsm_state_handler_t dest = me->temp; 149 150 // if the transaction came from an higher hierarchical level, go there 151 btstack_hsm_state_handler_t target = current; 152 current = me->state; 153 for(; current != target; current = me->temp) { 154 current( me, &exit_evt ); 155 btstack_hsm_get_super( me, current ); 156 } 157 158 btstack_hsm_state_handler_t source = target; 159 target = dest; 160 btstack_hsm_state_handler_t *root_path = me->path; 161 memset(root_path, 0, sizeof(btstack_hsm_state_handler_t)*me->depth); 162 163 // the state handlers form a single linked list with the default transition pointing to the previous hierarchy level, 164 // so if we only record the previous pointer we miss the first element of the list to reproduce it fully. 165 // So now the array contains head->prev->prev->prev... 166 root_path[0] = target; 167 168 // self transition 169 if( source == target ) { 170 source( me, &exit_evt ); 171 target( me, &entry_evt ); 172 } 173 // handle entry/exit edges 174 int_fast8_t level = 0; 175 bool lca_found = false; 176 // calculates the lowest common ancestor of the state graph 177 for(; source != btstack_hsm_top; source=me->temp) { 178 level = 1; 179 for(current=target; current != btstack_hsm_top; current=me->temp, ++level ) { 180 if( current == source ) { 181 lca_found = true; 182 break; 183 } 184 185 btstack_hsm_handler_super_cache( me, root_path, level, current ); 186 } 187 if( lca_found == true ) { 188 break; 189 } 190 source( me, &exit_evt ); 191 btstack_hsm_get_super( me, source ); 192 } 193 194 // handle entry in reverse order 195 for(level-=2; level >= 0; --level) { 196 root_path[level]( me, &entry_evt ); 197 } 198 // initial transitions are only allowed to point deeper into the state machine hierarchy, 199 // so we only follow this direction here, deeper. 200 for(; target( me, &init_evt ) == BTSTACK_HSM_TRAN_STATUS ;) { 201 current = me->temp; 202 for(level = 0; current != target; current=me->temp, ++level ) { 203 root_path[level] = current; 204 btstack_hsm_get_super( me, current ); 205 } 206 for(--level; level >= 0; --level) { 207 root_path[level]( me, &entry_evt ); 208 } 209 target = root_path[0]; 210 } 211 me->state = target; 212 return status; 213 } 214 215