PacketCopier.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /**
  2. * @file PacketCopier.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 <string.h>
  30. #include <misc/debug.h>
  31. #include <flow/PacketCopier.h>
  32. static void input_handler_send (PacketCopier *o, uint8_t *data, int data_len)
  33. {
  34. ASSERT(o->in_len == -1)
  35. ASSERT(data_len >= 0)
  36. DebugObject_Access(&o->d_obj);
  37. if (!o->out_have) {
  38. o->in_len = data_len;
  39. o->in = data;
  40. return;
  41. }
  42. memcpy(o->out, data, data_len);
  43. // finish input packet
  44. PacketPassInterface_Done(&o->input);
  45. // finish output packet
  46. PacketRecvInterface_Done(&o->output, data_len);
  47. o->out_have = 0;
  48. }
  49. static void input_handler_requestcancel (PacketCopier *o)
  50. {
  51. ASSERT(o->in_len >= 0)
  52. ASSERT(!o->out_have)
  53. DebugObject_Access(&o->d_obj);
  54. // finish input packet
  55. PacketPassInterface_Done(&o->input);
  56. o->in_len = -1;
  57. }
  58. static void output_handler_recv (PacketCopier *o, uint8_t *data)
  59. {
  60. ASSERT(!o->out_have)
  61. DebugObject_Access(&o->d_obj);
  62. if (o->in_len < 0) {
  63. o->out_have = 1;
  64. o->out = data;
  65. return;
  66. }
  67. memcpy(data, o->in, o->in_len);
  68. // finish input packet
  69. PacketPassInterface_Done(&o->input);
  70. // finish output packet
  71. PacketRecvInterface_Done(&o->output, o->in_len);
  72. o->in_len = -1;
  73. }
  74. void PacketCopier_Init (PacketCopier *o, int mtu, BPendingGroup *pg)
  75. {
  76. ASSERT(mtu >= 0)
  77. // init input
  78. PacketPassInterface_Init(&o->input, mtu, (PacketPassInterface_handler_send)input_handler_send, o, pg);
  79. PacketPassInterface_EnableCancel(&o->input, (PacketPassInterface_handler_requestcancel)input_handler_requestcancel);
  80. // init output
  81. PacketRecvInterface_Init(&o->output, mtu, (PacketRecvInterface_handler_recv)output_handler_recv, o, pg);
  82. // set no input packet
  83. o->in_len = -1;
  84. // set no output packet
  85. o->out_have = 0;
  86. DebugObject_Init(&o->d_obj);
  87. }
  88. void PacketCopier_Free (PacketCopier *o)
  89. {
  90. DebugObject_Free(&o->d_obj);
  91. // free output
  92. PacketRecvInterface_Free(&o->output);
  93. // free input
  94. PacketPassInterface_Free(&o->input);
  95. }
  96. PacketPassInterface * PacketCopier_GetInput (PacketCopier *o)
  97. {
  98. DebugObject_Access(&o->d_obj);
  99. return &o->input;
  100. }
  101. PacketRecvInterface * PacketCopier_GetOutput (PacketCopier *o)
  102. {
  103. DebugObject_Access(&o->d_obj);
  104. return &o->output;
  105. }