Emulex Logo
OneCoreā„¢ Storage SDK Release 11.2
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
ocs_sm.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2011-2015, Emulex
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright notice,
9  * this list of conditions and the following disclaimer.
10  *
11  * 2. Redistributions in binary form must reproduce the above copyright notice,
12  * this list of conditions and the following disclaimer in the documentation
13  * and/or other materials provided with the distribution.
14  *
15  * 3. Neither the name of the copyright holder nor the names of its contributors
16  * may be used to endorse or promote products derived from this software
17  * without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
23  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  *
31  */
32 
33 /**
34  * @file
35  * Generic state machine framework
36  */
37 
38 #include "ocs_os.h"
39 #include "ocs_sm.h"
40 
41 const char *ocs_sm_id[] = {
42  "common",
43  "domain",
44  "login"
45 };
46 
47 /**
48  * @brief Post an event to a context
49  *
50  * @param ctx State machine context
51  * @param evt Event to post
52  * @param data Event-specific data (if any)
53  *
54  * @return 0 if successfully posted event; -1 if state machine
55  * is disabled
56  *
57  */
58 int
59 ocs_sm_post_event(ocs_sm_ctx_t *ctx, ocs_sm_event_t evt, void *data)
60 {
61  if (ctx->current_state) {
62  ctx->current_state(ctx, evt, data);
63  return 0;
64  } else {
65  return -1;
66  }
67 }
68 
69 /**
70  * @brief Transition to a new state
71  */
72 void
73 ocs_sm_transition(ocs_sm_ctx_t *ctx, ocs_sm_function_t state, void *data)
74 {
75  if (ctx->current_state == state) {
77  } else {
78  ocs_sm_post_event(ctx, OCS_EVT_EXIT, data);
79  ctx->current_state = state;
80  ocs_sm_post_event(ctx, OCS_EVT_ENTER, data);
81  }
82 }
83 
84 /**
85  * @brief Disable further state machine processing
86  */
87 void
88 ocs_sm_disable(ocs_sm_ctx_t *ctx)
89 {
90  ctx->current_state = NULL;
91 }
92 
94 {
95  switch (evt) {
96 #define RETEVT(x) case x: return #x;
105 
117 
130 
131  default:
132  break;
133 #undef RETEVT
134  }
135  return "unknown";
136 }
137 
138 /* vim: set noexpandtab textwidth=120: */
const char * ocs_sm_id[]
Definition: ocs_sm.c:41
const char * ocs_sm_event_name(ocs_sm_event_t evt)
Definition: ocs_sm.c:93
void *(* ocs_sm_function_t)(ocs_sm_ctx_t *, ocs_sm_event_t, void *)
Definition: ocs_sm.h:107
void ocs_sm_transition(ocs_sm_ctx_t *ctx, ocs_sm_function_t state, void *data)
Transition to a new state.
Definition: ocs_sm.c:73
int ocs_sm_post_event(ocs_sm_ctx_t *ctx, ocs_sm_event_t evt, void *data)
Post an event to a context.
Definition: ocs_sm.c:59
#define RETEVT(x)
ocs_sm_event_t
State Machine Events.
Definition: ocs_sm.h:59
void ocs_sm_disable(ocs_sm_ctx_t *ctx)
Disable further state machine processing.
Definition: ocs_sm.c:88