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 #include "private-lib-core.h"
26
27 void
lws_state_reg_notifier(lws_state_manager_t * mgr,lws_state_notify_link_t * notify_link)28 lws_state_reg_notifier(lws_state_manager_t *mgr,
29 lws_state_notify_link_t *notify_link)
30 {
31 lws_dll2_add_head(¬ify_link->list, &mgr->notify_list);
32 }
33
34 void
lws_state_reg_deregister(lws_state_notify_link_t * nl)35 lws_state_reg_deregister(lws_state_notify_link_t *nl)
36 {
37 lws_dll2_remove(&nl->list);
38 }
39
40 void
lws_state_reg_notifier_list(lws_state_manager_t * mgr,lws_state_notify_link_t * const * notify_link_array)41 lws_state_reg_notifier_list(lws_state_manager_t *mgr,
42 lws_state_notify_link_t * const *notify_link_array)
43 {
44 if (notify_link_array)
45 while (*notify_link_array)
46 lws_state_reg_notifier(mgr, *notify_link_array++);
47 }
48
49 #if defined(_DEBUG)
50 static const char *
_systnm(lws_state_manager_t * mgr,int state,char * temp8)51 _systnm(lws_state_manager_t *mgr, int state, char *temp8)
52 {
53 if (!mgr->state_names) {
54 lws_snprintf(temp8, 8, "%d", state);
55 return temp8;
56 }
57
58 return mgr->state_names[state];
59 }
60 #endif
61
62 static int
_report(lws_state_manager_t * mgr,int a,int b)63 _report(lws_state_manager_t *mgr, int a, int b)
64 {
65 #if defined(_DEBUG)
66 char temp8[8];
67 #endif
68
69 lws_start_foreach_dll(struct lws_dll2 *, d, mgr->notify_list.head) {
70 lws_state_notify_link_t *l =
71 lws_container_of(d, lws_state_notify_link_t, list);
72
73 if (l->notify_cb(mgr, l, a, b)) {
74 /* a dependency took responsibility for retry */
75 #if defined(_DEBUG)
76 lwsl_info("%s: %s: %s: rejected '%s' -> '%s'\n",
77 __func__, mgr->name, l->name,
78 _systnm(mgr, a, temp8),
79 _systnm(mgr, b, temp8));
80 #endif
81 return 1;
82 }
83
84 } lws_end_foreach_dll(d);
85
86 return 0;
87 }
88
89 static int
_lws_state_transition(lws_state_manager_t * mgr,int target)90 _lws_state_transition(lws_state_manager_t *mgr, int target)
91 {
92 #if defined(_DEBUG)
93 char temp8[8];
94 #endif
95
96 if (_report(mgr, mgr->state, target))
97 return 1;
98
99 #if defined(_DEBUG)
100 lwsl_debug("%s: %s: changed %d '%s' -> %d '%s'\n", __func__, mgr->name,
101 mgr->state, _systnm(mgr, mgr->state, temp8), target,
102 _systnm(mgr, target, temp8));
103 #endif
104
105 mgr->state = target;
106
107 /* Indicate success by calling the notifers again with both args same */
108 _report(mgr, target, target);
109
110 return 0;
111 }
112
113 int
lws_state_transition_steps(lws_state_manager_t * mgr,int target)114 lws_state_transition_steps(lws_state_manager_t *mgr, int target)
115 {
116 int n = 0;
117 #if defined(_DEBUG)
118 int i = mgr->state;
119 char temp8[8];
120 #endif
121
122 while (!n && mgr->state != target)
123 n = _lws_state_transition(mgr, mgr->state + 1);
124
125 #if defined(_DEBUG)
126 lwsl_info("%s: %s -> %s\n", __func__, _systnm(mgr, i, temp8),
127 _systnm(mgr, mgr->state, temp8));
128 #endif
129
130 return 0;
131 }
132
133 int
lws_state_transition(lws_state_manager_t * mgr,int target)134 lws_state_transition(lws_state_manager_t *mgr, int target)
135 {
136 if (mgr->state != target)
137 _lws_state_transition(mgr, target);
138
139 return 0;
140 }
141