Skip to content

Commit 69f1c2f

Browse files
committed
Pass HSM state by value is all APIs
1 parent c73142b commit 69f1c2f

File tree

21 files changed

+161
-173
lines changed

21 files changed

+161
-173
lines changed

apps/examples/dpp/philo.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ void philo_ctor(int id) {
140140
memset(me, 0, sizeof(*me));
141141
me->cnt = 0;
142142
me->id = id;
143-
am_ao_ctor(&me->ao, &AM_HSM_STATE_CTOR(philo_init));
143+
am_ao_ctor(&me->ao, AM_HSM_STATE_CTOR(philo_init));
144144

145145
me->timer = (struct am_event_timer *)am_timer_event_allocate(
146146
/*id=*/EVT_TIMEOUT,

apps/examples/dpp/table.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,6 @@ void table_ctor(int nsession) {
158158
for (int i = 0; i < AM_COUNTOF(me->philo); i++) {
159159
me->philo[i] = PHILO_DONE;
160160
}
161-
am_ao_ctor(&m_table.ao, &AM_HSM_STATE_CTOR(table_init));
161+
am_ao_ctor(&m_table.ao, AM_HSM_STATE_CTOR(table_init));
162162
me->nsession = nsession;
163163
}

apps/examples/ringbuf/reader.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ void ringbuf_reader_ctor(void) {
105105
struct ringbuf_reader *me = &m_ringbuf_reader;
106106
memset(me, 0, sizeof(*me));
107107
me->len = 1;
108-
am_ao_ctor(&me->ao, &AM_HSM_STATE_CTOR(ringbuf_reader_init));
108+
am_ao_ctor(&me->ao, AM_HSM_STATE_CTOR(ringbuf_reader_init));
109109
am_timer_event_ctor(
110110
&me->timer_wait,
111111
/*id=*/AM_EVT_RINGBUF_WAIT,

apps/examples/ringbuf/writer.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ void ringbuf_writer_ctor(void) {
9797
struct ringbuf_writer *me = &m_ringbuf_writer;
9898
memset(me, 0, sizeof(*me));
9999
me->len = 1;
100-
am_ao_ctor(&me->ao, &AM_HSM_STATE_CTOR(ringbuf_writer_init));
100+
am_ao_ctor(&me->ao, AM_HSM_STATE_CTOR(ringbuf_writer_init));
101101
am_timer_event_ctor(
102102
&me->timer_wait,
103103
/*id=*/AM_EVT_RINGBUF_WAIT,

libs/ao/ao.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -232,9 +232,8 @@ void am_ao_unsubscribe_all(const struct am_ao *ao) {
232232
}
233233
}
234234

235-
void am_ao_ctor(struct am_ao *ao, const struct am_hsm_state *state) {
235+
void am_ao_ctor(struct am_ao *ao, struct am_hsm_state state) {
236236
AM_ASSERT(ao);
237-
AM_ASSERT(state);
238237
am_hsm_ctor(&ao->hsm, state);
239238
}
240239

libs/ao/ao.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,7 @@ bool am_ao_post_lifo_x(
261261
* @param ao the active object to construct
262262
* @param state the initial state of the active object
263263
*/
264-
void am_ao_ctor(struct am_ao *ao, const struct am_hsm_state *state);
264+
void am_ao_ctor(struct am_ao *ao, struct am_hsm_state state);
265265

266266
/**
267267
* Start active object.

libs/ao/tests/minimal.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,8 @@ int main(void) {
106106
};
107107
am_ao_state_ctor(&cfg_ao);
108108

109-
am_ao_ctor(&m_loopback.ao, &AM_HSM_STATE_CTOR(loopback_init));
110-
am_ao_ctor(&m_loopback_test.ao, &AM_HSM_STATE_CTOR(loopback_test_init));
109+
am_ao_ctor(&m_loopback.ao, AM_HSM_STATE_CTOR(loopback_init));
110+
am_ao_ctor(&m_loopback_test.ao, AM_HSM_STATE_CTOR(loopback_test_init));
111111

112112
am_ao_start(
113113
&m_loopback.ao,

libs/hsm/hsm.c

Lines changed: 28 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,9 @@ static const struct am_event m_hsm_evt_init = {.id = AM_HSM_EVT_INIT};
4646
static const struct am_event m_hsm_evt_entry = {.id = AM_HSM_EVT_ENTRY};
4747
static const struct am_event m_hsm_evt_exit = {.id = AM_HSM_EVT_EXIT};
4848

49-
static void hsm_set_state(struct am_hsm *hsm, const struct am_hsm_state *s) {
50-
hsm->state = *s;
51-
hsm->ifn = (unsigned char)s->ifn;
49+
static void hsm_set_state(struct am_hsm *hsm, struct am_hsm_state s) {
50+
hsm->state = s;
51+
hsm->ifn = (unsigned char)s.ifn;
5252
}
5353

5454
/**
@@ -76,14 +76,14 @@ static void hsm_build(
7676
return;
7777
}
7878
struct am_hsm hsm_ = *hsm;
79-
hsm_set_state(hsm, from);
79+
hsm_set_state(hsm, *from);
8080
enum am_hsm_rc rc = hsm->state.fn(hsm, &m_hsm_evt_empty);
8181
AM_ASSERT(AM_HSM_RC_SUPER == rc);
82-
while (!am_hsm_state_is_eq(hsm, until)) {
82+
while (!am_hsm_state_is_eq(hsm, *until)) {
8383
AM_ASSERT(path->len < AM_COUNTOF(path->state));
8484
path->state[path->len] = hsm->state;
8585
path->len++;
86-
if (till && am_hsm_state_is_eq(hsm, till)) {
86+
if (till && am_hsm_state_is_eq(hsm, *till)) {
8787
break;
8888
}
8989
rc = hsm->state.fn(hsm, &m_hsm_evt_empty);
@@ -100,10 +100,10 @@ static void hsm_build(
100100
*/
101101
static void hsm_enter(struct am_hsm *hsm, const struct am_hsm_path *path) {
102102
for (int i = path->len; i > 0; --i) {
103-
hsm_set_state(hsm, &path->state[i - 1]);
103+
hsm_set_state(hsm, path->state[i - 1]);
104104
enum am_hsm_rc rc = hsm->state.fn(hsm, &m_hsm_evt_entry);
105105
AM_ASSERT((AM_HSM_RC_SUPER == rc) || (AM_HSM_RC_HANDLED == rc));
106-
hsm_set_state(hsm, &path->state[i - 1]);
106+
hsm_set_state(hsm, path->state[i - 1]);
107107
}
108108
hsm->hierarchy_level = (unsigned)(hsm->hierarchy_level + path->len) &
109109
AM_HSM_HIERARCHY_LEVEL_MASK;
@@ -134,7 +134,7 @@ static void hsm_exit_state(struct am_hsm *hsm) {
134134
* @param hsm exit the states of this HSM
135135
* @param until stop the exit when reaching this state without exiting it
136136
*/
137-
static void hsm_exit(struct am_hsm *hsm, const struct am_hsm_state *until) {
137+
static void hsm_exit(struct am_hsm *hsm, struct am_hsm_state until) {
138138
while (!am_hsm_state_is_eq(hsm, until)) {
139139
hsm_exit_state(hsm);
140140
}
@@ -155,16 +155,16 @@ static void hsm_exit(struct am_hsm *hsm, const struct am_hsm_state *until) {
155155
*/
156156
static void hsm_enter_and_init(struct am_hsm *hsm, struct am_hsm_path *path) {
157157
hsm_enter(hsm, path);
158-
hsm_set_state(hsm, &path->state[0]);
158+
hsm_set_state(hsm, path->state[0]);
159159
enum am_hsm_rc rc;
160160
while ((rc = hsm->state.fn(hsm, &m_hsm_evt_init)) == AM_HSM_RC_TRAN) {
161161
struct am_hsm_state until = path->state[0];
162162
hsm_build(hsm, path, /*from=*/&hsm->state, &until, /*till=*/NULL);
163163
hsm_enter(hsm, path);
164-
hsm_set_state(hsm, &path->state[0]);
164+
hsm_set_state(hsm, path->state[0]);
165165
}
166166
AM_ASSERT(rc != AM_HSM_RC_TRAN_REDISPATCH);
167-
hsm_set_state(hsm, &path->state[0]);
167+
hsm_set_state(hsm, path->state[0]);
168168
}
169169

170170
/**
@@ -177,9 +177,9 @@ static void hsm_enter_and_init(struct am_hsm *hsm, struct am_hsm_path *path) {
177177
static void hsm_transition(
178178
struct am_hsm *hsm, struct am_hsm_state src, struct am_hsm_state dst
179179
) {
180-
if (!am_hsm_state_is_eq(hsm, &src)) {
181-
hsm_exit(hsm, /*until=*/&src);
182-
hsm_set_state(hsm, &src);
180+
if (!am_hsm_state_is_eq(hsm, src)) {
181+
hsm_exit(hsm, /*until=*/src);
182+
hsm_set_state(hsm, src);
183183
}
184184

185185
struct am_hsm_path path;
@@ -215,7 +215,7 @@ static void hsm_transition(
215215
* at lower indices (reversed order)
216216
*/
217217
int i = path.len - hsm->hierarchy_level;
218-
if (am_hsm_state_is_eq(hsm, &path.state[i])) {
218+
if (am_hsm_state_is_eq(hsm, path.state[i])) {
219219
/* LCA is found and it is not am_hsm_top() */
220220
path.len = i;
221221
hsm_enter_and_init(hsm, &path);
@@ -250,15 +250,15 @@ static enum am_hsm_rc hsm_dispatch(
250250

251251
bool tran = (AM_HSM_RC_TRAN == rc) || (AM_HSM_RC_TRAN_REDISPATCH == rc);
252252
if (!tran) { /* event was handled or ignored */
253-
hsm_set_state(hsm, &state);
253+
hsm_set_state(hsm, state);
254254
return rc;
255255
}
256256

257257
/* the event triggered state transition */
258258

259259
struct am_hsm_state dst = hsm->state;
260260
AM_ASSERT(dst.fn != am_hsm_top); /* transition to am_hsm_top() is invalid */
261-
hsm_set_state(hsm, &state);
261+
hsm_set_state(hsm, state);
262262

263263
hsm_transition(hsm, src, dst);
264264

@@ -289,10 +289,10 @@ void am_hsm_dispatch(struct am_hsm *hsm, const struct am_event *event) {
289289
hsm->dispatch_in_progress = false;
290290
}
291291

292-
bool am_hsm_is_in(struct am_hsm *hsm, const struct am_hsm_state *state) {
292+
bool am_hsm_is_in(struct am_hsm *hsm, struct am_hsm_state state) {
293293
AM_ASSERT(hsm);
294294

295-
if ((NULL == state) || (NULL == state->fn)) {
295+
if (NULL == state.fn) {
296296
return NULL == hsm->state.fn;
297297
}
298298

@@ -311,14 +311,12 @@ bool am_hsm_is_in(struct am_hsm *hsm, const struct am_hsm_state *state) {
311311
return in;
312312
}
313313

314-
bool am_hsm_state_is_eq(
315-
const struct am_hsm *hsm, const struct am_hsm_state *state
316-
) {
314+
bool am_hsm_state_is_eq(const struct am_hsm *hsm, struct am_hsm_state state) {
317315
AM_ASSERT(hsm);
318-
if ((NULL == state) || (NULL == state->fn)) {
316+
if (NULL == state.fn) {
319317
return NULL == hsm->state.fn;
320318
}
321-
return (hsm->state.fn == state->fn) && (hsm->state.ifn == state->ifn);
319+
return (hsm->state.fn == state.fn) && (hsm->state.ifn == state.ifn);
322320
}
323321

324322
int am_hsm_instance(const struct am_hsm *hsm) {
@@ -331,18 +329,17 @@ struct am_hsm_state am_hsm_state(const struct am_hsm *hsm) {
331329
return hsm->state;
332330
}
333331

334-
void am_hsm_ctor(struct am_hsm *hsm, const struct am_hsm_state *state) {
332+
void am_hsm_ctor(struct am_hsm *hsm, struct am_hsm_state state) {
335333
AM_ASSERT(hsm);
336-
AM_ASSERT(state);
337-
AM_ASSERT(state->fn);
334+
AM_ASSERT(state.fn);
338335
hsm_set_state(hsm, state);
339336
hsm->ctor_called = true;
340337
}
341338

342339
void am_hsm_dtor(struct am_hsm *hsm) {
343340
AM_ASSERT(hsm);
344-
hsm_exit(hsm, /*until=*/&AM_HSM_STATE_CTOR(am_hsm_top));
345-
hsm_set_state(hsm, &AM_HSM_STATE_CTOR(NULL));
341+
hsm_exit(hsm, /*until=*/AM_HSM_STATE_CTOR(am_hsm_top));
342+
hsm_set_state(hsm, AM_HSM_STATE_CTOR(NULL));
346343
hsm->ctor_called = hsm->init_called = false;
347344
}
348345

@@ -357,7 +354,7 @@ void am_hsm_init(struct am_hsm *hsm, const struct am_event *init_event) {
357354
struct am_hsm_state dst = hsm->state;
358355
struct am_hsm_path path;
359356
struct am_hsm_state until = AM_HSM_STATE_CTOR(am_hsm_top);
360-
hsm_set_state(hsm, &state);
357+
hsm_set_state(hsm, state);
361358
hsm_build(hsm, &path, /*from=*/&dst, &until, /*till=*/NULL);
362359
hsm_enter_and_init(hsm, &path);
363360
hsm->init_called = true;

libs/hsm/hsm.h

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -298,23 +298,21 @@ void am_hsm_dispatch(struct am_hsm *hsm, const struct am_event *event);
298298
* @retval false not in the state in the hierarchical sense
299299
* @retval true in the state
300300
*/
301-
bool am_hsm_is_in(struct am_hsm *hsm, const struct am_hsm_state *state);
301+
bool am_hsm_is_in(struct am_hsm *hsm, struct am_hsm_state state);
302302

303303
/**
304304
* Check if active state equals to #state (not in hierarchical sense).
305305
*
306306
* If active state of hsm is S1, which is substate of S, then
307-
* am_hsm_state_is_eq(hsm, &AM_HSM_STATE_CTOR(S1)) is true, but
308-
* am_hsm_state_is_eq(hsm, &AM_HSM_STATE_CTOR(S)) is false.
307+
* am_hsm_state_is_eq(hsm, AM_HSM_STATE_CTOR(S1)) is true, but
308+
* am_hsm_state_is_eq(hsm, AM_HSM_STATE_CTOR(S)) is false.
309309
*
310310
* @param hsm the HSM handler
311311
* @param state the state to compare against
312312
* @retval true the active HSM state equals #state
313313
* @retval false the active HSM state DOES NOT equal #state
314314
*/
315-
bool am_hsm_state_is_eq(
316-
const struct am_hsm *hsm, const struct am_hsm_state *state
317-
);
315+
bool am_hsm_state_is_eq(const struct am_hsm *hsm, struct am_hsm_state state);
318316

319317
/**
320318
* Get state instance.
@@ -348,7 +346,7 @@ struct am_hsm_state am_hsm_state(const struct am_hsm *hsm);
348346
* The initial state must return
349347
* AM_HSM_TRAN(s) or AM_HSM_TRAN(s, i)
350348
*/
351-
void am_hsm_ctor(struct am_hsm *hsm, const struct am_hsm_state *state);
349+
void am_hsm_ctor(struct am_hsm *hsm, struct am_hsm_state state);
352350

353351
/**
354352
* HSM destructor.

libs/hsm/tests/defer.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ static enum am_hsm_rc defer_sinit(
104104

105105
static void defer_ctor(void (*log)(const char *fmt, ...)) {
106106
struct test_defer *me = &m_test_defer;
107-
am_hsm_ctor(&me->hsm, &AM_HSM_STATE_CTOR(defer_sinit));
107+
am_hsm_ctor(&me->hsm, AM_HSM_STATE_CTOR(defer_sinit));
108108
me->log = log;
109109

110110
/* setup HSM event queue */

0 commit comments

Comments
 (0)