Interface.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /* vim: set expandtab ts=4 sw=4: */
  2. /*
  3. * You may redistribute this program and/or modify it under the terms of
  4. * the GNU General Public License as published by the Free Software Foundation,
  5. * either version 3 of the License, or (at your option) any later version.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. *
  12. * You should have received a copy of the GNU General Public License
  13. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. #ifndef Interface_H
  16. #define Interface_H
  17. #include <stdint.h>
  18. #include "wire/Message.h"
  19. #define Interface_ERROR_WRONG_STATE 256
  20. #define Interface_CALLBACK(name) \
  21. uint8_t (* name)(struct Message* message, struct Interface* thisInterface)
  22. #define Interface_CONST_CALLBACK(name) \
  23. uint8_t (* const name)(struct Message* message, struct Interface* thisInterface)
  24. /**
  25. * An interface.
  26. * All interfaces are point-to-point, no addressing is done at this level.
  27. * If you have multiple direct connections (eg nodes in an ethernet),
  28. * you must register an interface for each.
  29. */
  30. struct Interface
  31. {
  32. /** Arbitarary data which belongs to the wire side of this interface. */
  33. void* senderContext;
  34. /** The maximum allowable length for a message. */
  35. uint16_t maxMessageLength;
  36. /**
  37. * The number of bytes of padding which must exist *before* the beginning of the message.
  38. * when you call sendMessage, there must be requiredPadding number of bytes of free space before
  39. * the location pointed to by message->bytes.
  40. */
  41. uint16_t requiredPadding;
  42. /**
  43. * Send a message through this interface.
  44. *
  45. * @return 0 If all goes well, non-zero in case of an error.
  46. * See Error.h for more information about interface error codes.
  47. */
  48. Interface_CALLBACK(sendMessage);
  49. /** Used to allocate this interface, the interface will close when this allocator is freed. */
  50. struct Allocator* allocator;
  51. /** Arbitrary data which belongs to the receiver side of this interface. */
  52. void* receiverContext;
  53. /**
  54. * Recieve a message from this interface.
  55. *
  56. * @param received the incoming message from the other node.
  57. * @param senderContext arbitrary data which must be passed to the receiver.
  58. * @return 0 If all goes well, non-zero in case of an error.
  59. * See Error.h for more information about interface error codes.
  60. */
  61. Interface_CALLBACK(receiveMessage);
  62. };
  63. static inline uint8_t Interface_receiveMessage(struct Interface* iface, struct Message* msg)
  64. {
  65. if (iface->receiveMessage) {
  66. return iface->receiveMessage(msg, iface);
  67. }
  68. return 0;
  69. }
  70. static inline uint8_t Interface_sendMessage(struct Interface* iface, struct Message* msg)
  71. {
  72. return iface->sendMessage(msg, iface);
  73. }
  74. #endif