StreamPassInterface.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /**
  2. * @file StreamPassInterface.h
  3. * @author Ambroz Bizjak <ambrop7@gmail.com>
  4. *
  5. * @section LICENSE
  6. *
  7. * This file is part of BadVPN.
  8. *
  9. * BadVPN is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2
  11. * as published by the Free Software Foundation.
  12. *
  13. * BadVPN is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License along
  19. * with this program; if not, write to the Free Software Foundation, Inc.,
  20. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  21. *
  22. * @section DESCRIPTION
  23. *
  24. * Interface allowing a stream sender to pass stream data to a stream receiver.
  25. *
  26. * Note that this interface behaves exactly the same and has the same code as
  27. * {@link StreamRecvInterface} if names and its external semantics are disregarded.
  28. * If you modify this file, you should probably modify {@link StreamRecvInterface}
  29. * too.
  30. */
  31. #ifndef BADVPN_FLOW_STREAMPASSINTERFACE_H
  32. #define BADVPN_FLOW_STREAMPASSINTERFACE_H
  33. #include <stdint.h>
  34. #include <stddef.h>
  35. #include <misc/debug.h>
  36. #include <system/DebugObject.h>
  37. #include <system/BPending.h>
  38. #define SPI_STATE_NONE 1
  39. #define SPI_STATE_OPERATION_PENDING 2
  40. #define SPI_STATE_BUSY 3
  41. #define SPI_STATE_DONE_PENDING 4
  42. typedef void (*StreamPassInterface_handler_send) (void *user, uint8_t *data, int data_len);
  43. typedef void (*StreamPassInterface_handler_done) (void *user, int data_len);
  44. typedef struct {
  45. // provider data
  46. StreamPassInterface_handler_send handler_operation;
  47. void *user_provider;
  48. // user data
  49. StreamPassInterface_handler_done handler_done;
  50. void *user_user;
  51. // operation job
  52. BPending job_operation;
  53. uint8_t *job_operation_data;
  54. int job_operation_len;
  55. // done job
  56. BPending job_done;
  57. int job_done_len;
  58. // state
  59. int state;
  60. DebugObject d_obj;
  61. } StreamPassInterface;
  62. static void StreamPassInterface_Init (StreamPassInterface *i, StreamPassInterface_handler_send handler_operation, void *user, BPendingGroup *pg);
  63. static void StreamPassInterface_Free (StreamPassInterface *i);
  64. static void StreamPassInterface_Done (StreamPassInterface *i, int data_len);
  65. static void StreamPassInterface_Sender_Init (StreamPassInterface *i, StreamPassInterface_handler_done handler_done, void *user);
  66. static void StreamPassInterface_Sender_Send (StreamPassInterface *i, uint8_t *data, int data_len);
  67. void _StreamPassInterface_job_operation (StreamPassInterface *i);
  68. void _StreamPassInterface_job_done (StreamPassInterface *i);
  69. void StreamPassInterface_Init (StreamPassInterface *i, StreamPassInterface_handler_send handler_operation, void *user, BPendingGroup *pg)
  70. {
  71. // init arguments
  72. i->handler_operation = handler_operation;
  73. i->user_provider = user;
  74. // set no user
  75. i->handler_done = NULL;
  76. // init jobs
  77. BPending_Init(&i->job_operation, pg, (BPending_handler)_StreamPassInterface_job_operation, i);
  78. BPending_Init(&i->job_done, pg, (BPending_handler)_StreamPassInterface_job_done, i);
  79. // set state
  80. i->state = SPI_STATE_NONE;
  81. DebugObject_Init(&i->d_obj);
  82. }
  83. void StreamPassInterface_Free (StreamPassInterface *i)
  84. {
  85. DebugObject_Free(&i->d_obj);
  86. // free jobs
  87. BPending_Free(&i->job_done);
  88. BPending_Free(&i->job_operation);
  89. }
  90. void StreamPassInterface_Done (StreamPassInterface *i, int data_len)
  91. {
  92. ASSERT(i->state == SPI_STATE_BUSY)
  93. ASSERT(data_len > 0)
  94. ASSERT(data_len <= i->job_operation_len)
  95. DebugObject_Access(&i->d_obj);
  96. // schedule done
  97. i->job_done_len = data_len;
  98. BPending_Set(&i->job_done);
  99. // set state
  100. i->state = SPI_STATE_DONE_PENDING;
  101. }
  102. void StreamPassInterface_Sender_Init (StreamPassInterface *i, StreamPassInterface_handler_done handler_done, void *user)
  103. {
  104. ASSERT(handler_done)
  105. ASSERT(!i->handler_done)
  106. DebugObject_Access(&i->d_obj);
  107. i->handler_done = handler_done;
  108. i->user_user = user;
  109. }
  110. void StreamPassInterface_Sender_Send (StreamPassInterface *i, uint8_t *data, int data_len)
  111. {
  112. ASSERT(data_len > 0)
  113. ASSERT(data)
  114. ASSERT(i->state == SPI_STATE_NONE)
  115. ASSERT(i->handler_done)
  116. DebugObject_Access(&i->d_obj);
  117. // schedule operation
  118. i->job_operation_data = data;
  119. i->job_operation_len = data_len;
  120. BPending_Set(&i->job_operation);
  121. // set state
  122. i->state = SPI_STATE_OPERATION_PENDING;
  123. }
  124. #endif