PacketPassInactivityMonitor.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /**
  2. * @file PacketPassInactivityMonitor.c
  3. * @author Ambroz Bizjak <ambrop7@gmail.com>
  4. *
  5. * @section LICENSE
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * 3. Neither the name of the author nor the
  15. * names of its contributors may be used to endorse or promote products
  16. * derived from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  19. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  22. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  24. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  25. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  27. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. */
  29. #include "PacketPassInactivityMonitor.h"
  30. static void input_handler_send (PacketPassInactivityMonitor *o, uint8_t *data, int data_len)
  31. {
  32. DebugObject_Access(&o->d_obj);
  33. // schedule send
  34. PacketPassInterface_Sender_Send(o->output, data, data_len);
  35. // stop timer
  36. BReactor_RemoveTimer(o->reactor, &o->timer);
  37. }
  38. static void input_handler_requestcancel (PacketPassInactivityMonitor *o)
  39. {
  40. DebugObject_Access(&o->d_obj);
  41. // request cancel
  42. PacketPassInterface_Sender_RequestCancel(o->output);
  43. }
  44. static void output_handler_done (PacketPassInactivityMonitor *o)
  45. {
  46. DebugObject_Access(&o->d_obj);
  47. // output no longer busy, restart timer
  48. BReactor_SetTimer(o->reactor, &o->timer);
  49. // call done
  50. PacketPassInterface_Done(&o->input);
  51. }
  52. static void timer_handler (PacketPassInactivityMonitor *o)
  53. {
  54. DebugObject_Access(&o->d_obj);
  55. // restart timer
  56. BReactor_SetTimer(o->reactor, &o->timer);
  57. // call handler
  58. if (o->handler) {
  59. o->handler(o->user);
  60. return;
  61. }
  62. }
  63. void PacketPassInactivityMonitor_Init (PacketPassInactivityMonitor *o, PacketPassInterface *output, BReactor *reactor, btime_t interval, PacketPassInactivityMonitor_handler handler, void *user)
  64. {
  65. // init arguments
  66. o->output = output;
  67. o->reactor = reactor;
  68. o->handler = handler;
  69. o->user = user;
  70. // init input
  71. PacketPassInterface_Init(&o->input, PacketPassInterface_GetMTU(o->output), (PacketPassInterface_handler_send)input_handler_send, o, BReactor_PendingGroup(o->reactor));
  72. if (PacketPassInterface_HasCancel(o->output)) {
  73. PacketPassInterface_EnableCancel(&o->input, (PacketPassInterface_handler_requestcancel)input_handler_requestcancel);
  74. }
  75. // init output
  76. PacketPassInterface_Sender_Init(o->output, (PacketPassInterface_handler_done)output_handler_done, o);
  77. // init timer
  78. BTimer_Init(&o->timer, interval, (BTimer_handler)timer_handler, o);
  79. BReactor_SetTimer(o->reactor, &o->timer);
  80. DebugObject_Init(&o->d_obj);
  81. }
  82. void PacketPassInactivityMonitor_Free (PacketPassInactivityMonitor *o)
  83. {
  84. DebugObject_Free(&o->d_obj);
  85. // free timer
  86. BReactor_RemoveTimer(o->reactor, &o->timer);
  87. // free input
  88. PacketPassInterface_Free(&o->input);
  89. }
  90. PacketPassInterface * PacketPassInactivityMonitor_GetInput (PacketPassInactivityMonitor *o)
  91. {
  92. DebugObject_Access(&o->d_obj);
  93. return &o->input;
  94. }
  95. void PacketPassInactivityMonitor_SetHandler (PacketPassInactivityMonitor *o, PacketPassInactivityMonitor_handler handler, void *user)
  96. {
  97. DebugObject_Access(&o->d_obj);
  98. o->handler = handler;
  99. o->user = user;
  100. }
  101. void PacketPassInactivityMonitor_Force (PacketPassInactivityMonitor *o)
  102. {
  103. DebugObject_Access(&o->d_obj);
  104. BReactor_SetTimerAfter(o->reactor, &o->timer, 0);
  105. }