PacketProtoEncoder.c 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /**
  2. * @file PacketProtoEncoder.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 <protocol/packetproto.h>
  31. #include <misc/balign.h>
  32. #include <misc/debug.h>
  33. #include <misc/byteorder.h>
  34. #include <flow/PacketProtoEncoder.h>
  35. static void output_handler_recv (PacketProtoEncoder *enc, uint8_t *data)
  36. {
  37. ASSERT(!enc->output_packet)
  38. ASSERT(data)
  39. DebugObject_Access(&enc->d_obj);
  40. // schedule receive
  41. enc->output_packet = data;
  42. PacketRecvInterface_Receiver_Recv(enc->input, enc->output_packet + sizeof(struct packetproto_header));
  43. }
  44. static void input_handler_done (PacketProtoEncoder *enc, int in_len)
  45. {
  46. ASSERT(enc->output_packet)
  47. DebugObject_Access(&enc->d_obj);
  48. // write length
  49. ((struct packetproto_header *)enc->output_packet)->len = htol16(in_len);
  50. // finish output packet
  51. enc->output_packet = NULL;
  52. PacketRecvInterface_Done(&enc->output, PACKETPROTO_ENCLEN(in_len));
  53. }
  54. void PacketProtoEncoder_Init (PacketProtoEncoder *enc, PacketRecvInterface *input, BPendingGroup *pg)
  55. {
  56. ASSERT(PacketRecvInterface_GetMTU(input) <= PACKETPROTO_MAXPAYLOAD)
  57. // init arguments
  58. enc->input = input;
  59. // init input
  60. PacketRecvInterface_Receiver_Init(enc->input, (PacketRecvInterface_handler_done)input_handler_done, enc);
  61. // init output
  62. PacketRecvInterface_Init(
  63. &enc->output, PACKETPROTO_ENCLEN(PacketRecvInterface_GetMTU(enc->input)),
  64. (PacketRecvInterface_handler_recv)output_handler_recv, enc, pg
  65. );
  66. // set no output packet
  67. enc->output_packet = NULL;
  68. DebugObject_Init(&enc->d_obj);
  69. }
  70. void PacketProtoEncoder_Free (PacketProtoEncoder *enc)
  71. {
  72. DebugObject_Free(&enc->d_obj);
  73. // free input
  74. PacketRecvInterface_Free(&enc->output);
  75. }
  76. PacketRecvInterface * PacketProtoEncoder_GetOutput (PacketProtoEncoder *enc)
  77. {
  78. DebugObject_Access(&enc->d_obj);
  79. return &enc->output;
  80. }