state.c 3.8 KB

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