1  /*
2  * libwebsockets - small server side websockets and web server implementation
3  *
4  * Copyright (C) 2010 - 2019 Andy Green <andy@warmcat.com>
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to
8  * deal in the Software without restriction, including without limitation the
9  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10  * sell copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
22  * IN THE SOFTWARE.
23  */
24 
25 struct lws_state_notify_link;
26 struct lws_state_manager;
27 
28 #if defined(LWS_WITH_SYS_STATE)
29 
30 typedef int (*lws_state_notify_t)(struct lws_state_manager *mgr,
31 				  struct lws_state_notify_link *link,
32 				  int current, int target);
33 
34 typedef struct lws_state_notify_link {
35 	lws_dll2_t		list;
36 	lws_state_notify_t	notify_cb;
37 	const char		*name;
38 } lws_state_notify_link_t;
39 
40 typedef struct lws_state_manager {
41 	lws_dll2_owner_t	notify_list;
42 	struct lws_context	*context;
43 	void			*parent;
44 #if defined(LWS_WITH_SYS_SMD)
45 	lws_smd_class_t		smd_class;
46 #endif
47 	/**< optional opaque pointer to owning object... useful to make such
48 	 * a pointer available to a notification callback.  Ignored by lws */
49 	const char		**state_names;
50 	const char		*name;
51 	int			state;
52 } lws_state_manager_t;
53 
54 /**
55  * lws_state_reg_notifier() - add dep handler for state notifications
56  *
57  * \param context: the lws_context
58  * \param nl: the handler to add to the notifier linked-list
59  *
60  * Add \p notify_link to the context's list of notification handlers for system
61  * state changes.  The handlers can defeat or take over responsibility for
62  * retrying the change after they have initiated some dependency.
63  */
64 
65 LWS_EXTERN LWS_VISIBLE void
66 lws_state_reg_notifier(lws_state_manager_t *mgr, lws_state_notify_link_t *nl);
67 
68 /**
69  * lws_state_reg_deregister() - deregister a notifier
70  *
71  * \param nl: notification hardler to deregister
72  *
73  * Remove a notification handler from its state manager
74  */
75 
76 LWS_EXTERN LWS_VISIBLE void
77 lws_state_reg_deregister(lws_state_notify_link_t *nl);
78 
79 /**
80  * lws_state_reg_notifier_list() - add dep handlers for state notifications
81  *
82  * \param context: the lws_context
83  * \param nl: list of notification handlers
84  *
85  * Add a NULL-terminated list of notification handler pointers to a notification
86  * manager object
87  */
88 
89 LWS_EXTERN LWS_VISIBLE void
90 lws_state_reg_notifier_list(lws_state_manager_t *mgr,
91 			    lws_state_notify_link_t * const *nl);
92 
93 /**
94  * lws_state_transition_steps() - move to state via starting any deps
95  *
96  * \param mgr: the state manager object
97  * \param target: the state we wish to move to
98  *
99  * Advance state by state towards state \p target.  At each state, notifiers
100  * may veto the change and be triggered to perform dependencies, stopping the
101  * advance towards the target state.
102  */
103 LWS_EXTERN LWS_VISIBLE int
104 lws_state_transition_steps(lws_state_manager_t *mgr, int target);
105 
106 /**
107  * lws_state_transition() - move to state via starting any deps
108  *
109  * \param mgr: the state manager object
110  * \param target: the state we wish to move to
111  *
112  * Jump to state target atomically.  Notifiers may veto it.
113  */
114 LWS_EXTERN LWS_VISIBLE int
115 lws_state_transition(lws_state_manager_t *mgr, int target);
116 
117 #else
118 
119 #endif
120