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 int
58 ocs_sm_post_event(ocs_sm_ctx_t *ctx, ocs_sm_event_t evt, void *data)
59 {
60  if (ctx->current_state) {
61  ctx->current_state(ctx, evt, data);
62  return 0;
63  } else {
64  return -1;
65  }
66 }
67 
68 /**
69  * @brief Transition to a new state.
70  */
71 void
72 ocs_sm_transition(ocs_sm_ctx_t *ctx, ocs_sm_function_t state, void *data)
73 {
74  if (ctx->current_state == state) {
76  } else {
77  ocs_sm_post_event(ctx, OCS_EVT_EXIT, data);
78  ctx->current_state = state;
79  ocs_sm_post_event(ctx, OCS_EVT_ENTER, data);
80  }
81 }
82 
83 /**
84  * @brief Disable further state machine processing.
85  */
86 void
87 ocs_sm_disable(ocs_sm_ctx_t *ctx)
88 {
89  ctx->current_state = NULL;
90 }
91 
93 {
94  switch (evt) {
95  #define RETEVT(x) case x: return #x;
116 
129 
143 
145 
147 
152 
167 
183 
185 
203 
204  default:
205  break;
206  #undef RETEVT
207  }
208  return "unknown";
209 }
210 
211 /* 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:92
void *(* ocs_sm_function_t)(ocs_sm_ctx_t *, ocs_sm_event_t, void *)
Definition: ocs_sm.h:188
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:72
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:58
#define RETEVT(x)
ocs_sm_event_t
Definition: ocs_sm.h:60
void ocs_sm_disable(ocs_sm_ctx_t *ctx)
Disable further state machine processing.
Definition: ocs_sm.c:87