PacketPassConnector.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /**
  2. * @file PacketPassConnector.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 <stddef.h>
  30. #include <misc/debug.h>
  31. #include <flow/PacketPassConnector.h>
  32. static void input_handler_send (PacketPassConnector *o, uint8_t *data, int data_len)
  33. {
  34. ASSERT(data_len >= 0)
  35. ASSERT(data_len <= o->input_mtu)
  36. ASSERT(o->in_len == -1)
  37. DebugObject_Access(&o->d_obj);
  38. // remember input packet
  39. o->in_len = data_len;
  40. o->in = data;
  41. if (o->output) {
  42. // schedule send
  43. PacketPassInterface_Sender_Send(o->output, o->in, o->in_len);
  44. }
  45. }
  46. static void output_handler_done (PacketPassConnector *o)
  47. {
  48. ASSERT(o->in_len >= 0)
  49. ASSERT(o->output)
  50. DebugObject_Access(&o->d_obj);
  51. // have no input packet
  52. o->in_len = -1;
  53. // allow input to send more packets
  54. PacketPassInterface_Done(&o->input);
  55. }
  56. void PacketPassConnector_Init (PacketPassConnector *o, int mtu, BPendingGroup *pg)
  57. {
  58. ASSERT(mtu >= 0)
  59. // init arguments
  60. o->input_mtu = mtu;
  61. // init input
  62. PacketPassInterface_Init(&o->input, o->input_mtu, (PacketPassInterface_handler_send)input_handler_send, o, pg);
  63. // have no input packet
  64. o->in_len = -1;
  65. // have no output
  66. o->output = NULL;
  67. DebugObject_Init(&o->d_obj);
  68. }
  69. void PacketPassConnector_Free (PacketPassConnector *o)
  70. {
  71. DebugObject_Free(&o->d_obj);
  72. // free input
  73. PacketPassInterface_Free(&o->input);
  74. }
  75. PacketPassInterface * PacketPassConnector_GetInput (PacketPassConnector *o)
  76. {
  77. DebugObject_Access(&o->d_obj);
  78. return &o->input;
  79. }
  80. void PacketPassConnector_ConnectOutput (PacketPassConnector *o, PacketPassInterface *output)
  81. {
  82. ASSERT(!o->output)
  83. ASSERT(PacketPassInterface_GetMTU(output) >= o->input_mtu)
  84. DebugObject_Access(&o->d_obj);
  85. // set output
  86. o->output = output;
  87. // init output
  88. PacketPassInterface_Sender_Init(o->output, (PacketPassInterface_handler_done)output_handler_done, o);
  89. // if we have an input packet, schedule send
  90. if (o->in_len >= 0) {
  91. PacketPassInterface_Sender_Send(o->output, o->in, o->in_len);
  92. }
  93. }
  94. void PacketPassConnector_DisconnectOutput (PacketPassConnector *o)
  95. {
  96. ASSERT(o->output)
  97. DebugObject_Access(&o->d_obj);
  98. // set no output
  99. o->output = NULL;
  100. }