PacketRecvInterface.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /**
  2. * @file PacketRecvInterface.h
  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. * @section DESCRIPTION
  30. *
  31. * Interface allowing a packet receiver to receive data packets from a packet sender.
  32. */
  33. #ifndef BADVPN_FLOW_PACKETRECVINTERFACE_H
  34. #define BADVPN_FLOW_PACKETRECVINTERFACE_H
  35. #include <stdint.h>
  36. #include <stddef.h>
  37. #include <misc/debug.h>
  38. #include <base/DebugObject.h>
  39. #include <base/BPending.h>
  40. #define PRI_STATE_NONE 1
  41. #define PRI_STATE_OPERATION_PENDING 2
  42. #define PRI_STATE_BUSY 3
  43. #define PRI_STATE_DONE_PENDING 4
  44. typedef void (*PacketRecvInterface_handler_recv) (void *user, uint8_t *data);
  45. typedef void (*PacketRecvInterface_handler_done) (void *user, int data_len);
  46. typedef struct {
  47. // provider data
  48. int mtu;
  49. PacketRecvInterface_handler_recv handler_operation;
  50. void *user_provider;
  51. // user data
  52. PacketRecvInterface_handler_done handler_done;
  53. void *user_user;
  54. // operation job
  55. BPending job_operation;
  56. uint8_t *job_operation_data;
  57. // done job
  58. BPending job_done;
  59. int job_done_len;
  60. // state
  61. int state;
  62. DebugObject d_obj;
  63. } PacketRecvInterface;
  64. static void PacketRecvInterface_Init (PacketRecvInterface *i, int mtu, PacketRecvInterface_handler_recv handler_operation, void *user, BPendingGroup *pg);
  65. static void PacketRecvInterface_Free (PacketRecvInterface *i);
  66. static void PacketRecvInterface_Done (PacketRecvInterface *i, int data_len);
  67. static int PacketRecvInterface_GetMTU (PacketRecvInterface *i);
  68. static void PacketRecvInterface_Receiver_Init (PacketRecvInterface *i, PacketRecvInterface_handler_done handler_done, void *user);
  69. static void PacketRecvInterface_Receiver_Recv (PacketRecvInterface *i, uint8_t *data);
  70. void _PacketRecvInterface_job_operation (PacketRecvInterface *i);
  71. void _PacketRecvInterface_job_done (PacketRecvInterface *i);
  72. void PacketRecvInterface_Init (PacketRecvInterface *i, int mtu, PacketRecvInterface_handler_recv handler_operation, void *user, BPendingGroup *pg)
  73. {
  74. ASSERT(mtu >= 0)
  75. // init arguments
  76. i->mtu = mtu;
  77. i->handler_operation = handler_operation;
  78. i->user_provider = user;
  79. // set no user
  80. i->handler_done = NULL;
  81. // init jobs
  82. BPending_Init(&i->job_operation, pg, (BPending_handler)_PacketRecvInterface_job_operation, i);
  83. BPending_Init(&i->job_done, pg, (BPending_handler)_PacketRecvInterface_job_done, i);
  84. // set state
  85. i->state = PRI_STATE_NONE;
  86. DebugObject_Init(&i->d_obj);
  87. }
  88. void PacketRecvInterface_Free (PacketRecvInterface *i)
  89. {
  90. DebugObject_Free(&i->d_obj);
  91. // free jobs
  92. BPending_Free(&i->job_done);
  93. BPending_Free(&i->job_operation);
  94. }
  95. void PacketRecvInterface_Done (PacketRecvInterface *i, int data_len)
  96. {
  97. ASSERT(data_len >= 0)
  98. ASSERT(data_len <= i->mtu)
  99. ASSERT(i->state == PRI_STATE_BUSY)
  100. DebugObject_Access(&i->d_obj);
  101. // schedule done
  102. i->job_done_len = data_len;
  103. BPending_Set(&i->job_done);
  104. // set state
  105. i->state = PRI_STATE_DONE_PENDING;
  106. }
  107. int PacketRecvInterface_GetMTU (PacketRecvInterface *i)
  108. {
  109. DebugObject_Access(&i->d_obj);
  110. return i->mtu;
  111. }
  112. void PacketRecvInterface_Receiver_Init (PacketRecvInterface *i, PacketRecvInterface_handler_done handler_done, void *user)
  113. {
  114. ASSERT(handler_done)
  115. ASSERT(!i->handler_done)
  116. DebugObject_Access(&i->d_obj);
  117. i->handler_done = handler_done;
  118. i->user_user = user;
  119. }
  120. void PacketRecvInterface_Receiver_Recv (PacketRecvInterface *i, uint8_t *data)
  121. {
  122. ASSERT(!(i->mtu > 0) || data)
  123. ASSERT(i->state == PRI_STATE_NONE)
  124. ASSERT(i->handler_done)
  125. DebugObject_Access(&i->d_obj);
  126. // schedule operation
  127. i->job_operation_data = data;
  128. BPending_Set(&i->job_operation);
  129. // set state
  130. i->state = PRI_STATE_OPERATION_PENDING;
  131. }
  132. #endif