PacketPassNotifier.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /**
  2. * @file PacketPassNotifier.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 <flow/PacketPassNotifier.h>
  31. void input_handler_send (PacketPassNotifier *o, uint8_t *data, int data_len)
  32. {
  33. DebugObject_Access(&o->d_obj);
  34. // schedule send
  35. PacketPassInterface_Sender_Send(o->output, data, data_len);
  36. // if we have a handler, call it
  37. if (o->handler) {
  38. o->handler(o->handler_user, data, data_len);
  39. return;
  40. }
  41. }
  42. void input_handler_requestcancel (PacketPassNotifier *o)
  43. {
  44. DebugObject_Access(&o->d_obj);
  45. PacketPassInterface_Sender_RequestCancel(o->output);
  46. }
  47. void output_handler_done (PacketPassNotifier *o)
  48. {
  49. DebugObject_Access(&o->d_obj);
  50. PacketPassInterface_Done(&o->input);
  51. }
  52. void PacketPassNotifier_Init (PacketPassNotifier *o, PacketPassInterface *output, BPendingGroup *pg)
  53. {
  54. // init arguments
  55. o->output = output;
  56. // init input
  57. PacketPassInterface_Init(&o->input, PacketPassInterface_GetMTU(o->output), (PacketPassInterface_handler_send)input_handler_send, o, pg);
  58. if (PacketPassInterface_HasCancel(o->output)) {
  59. PacketPassInterface_EnableCancel(&o->input, (PacketPassInterface_handler_requestcancel)input_handler_requestcancel);
  60. }
  61. // init output
  62. PacketPassInterface_Sender_Init(o->output, (PacketPassInterface_handler_done)output_handler_done, o);
  63. // set no handler
  64. o->handler = NULL;
  65. DebugObject_Init(&o->d_obj);
  66. }
  67. void PacketPassNotifier_Free (PacketPassNotifier *o)
  68. {
  69. DebugObject_Free(&o->d_obj);
  70. // free input
  71. PacketPassInterface_Free(&o->input);
  72. }
  73. PacketPassInterface * PacketPassNotifier_GetInput (PacketPassNotifier *o)
  74. {
  75. DebugObject_Access(&o->d_obj);
  76. return &o->input;
  77. }
  78. void PacketPassNotifier_SetHandler (PacketPassNotifier *o, PacketPassNotifier_handler_notify handler, void *user)
  79. {
  80. DebugObject_Access(&o->d_obj);
  81. o->handler = handler;
  82. o->handler_user = user;
  83. }