A messenger application for Raspberry Pi Zerofor A.U.TH (Real time Embedded systems).
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

205 lines
5.3 KiB

  1. /*!
  2. * \file msg_impl.h
  3. *
  4. * Contain all the implementation specific types
  5. *
  6. * \author: Christos Choutouridis 8997 <cchoutou@ece.auth.gr>
  7. */
  8. #ifndef __msg_impl__
  9. #define __msg_impl__
  10. #include <stdint.h>
  11. #include <stdbool.h>
  12. #include <time.h>
  13. #include <netinet/ip_icmp.h>
  14. /*!
  15. * General options
  16. */
  17. //! @{
  18. #define LISTENER_PORT 2288 //!< The server port
  19. #define MSG_TEXT_SIZE 256 //!< Maximum size of each message
  20. #define MSG_LIST_SIZE 2000 //!< Maximum size of message history buffer
  21. #define DEVICE_LIST_SIZE 100 //!< Maximum size of the device list
  22. #define MSG_DELIMITER '_' //!< Message delimiter
  23. #define ADHOC_NET_A 10 //!< [A.B.C.D]
  24. #define ADHOC_NET_B 0
  25. #define ADHOC_NET_C 0
  26. #define ADHOC_NET_D 0
  27. //#define NO_DEBUG 1
  28. //! @}
  29. /*!
  30. * Helper macros
  31. */
  32. #define _MK_ADHOC_SUBNET(A, B, C, D) (((A)<<24) | ((B)<<16) | ((C)<<8) | (D))
  33. #define _adhoc_subnet _MK_ADHOC_SUBNET(ADHOC_NET_A, ADHOC_NET_B, ADHOC_NET_C, ADHOC_NET_D)
  34. /*!
  35. * Messenger types
  36. */
  37. //! @{
  38. /*!
  39. * Application status type
  40. */
  41. typedef enum {
  42. MSG_OK =0, //!< Indicate success
  43. MSG_ERROR //!< Indicate error
  44. } status_t;
  45. typedef bool bool_t; //!< Boolean type
  46. typedef char char_t; //!< Application wide character type
  47. typedef uint32_t aem_t; //!< AEM data type
  48. typedef int64_t tstamp_t; //!< UNIX time in 64 bit wide signed integer
  49. /*!
  50. * IP wrapper type
  51. */
  52. typedef struct {
  53. uint16_t A, B, C, D;
  54. }devIP_t;
  55. /*!
  56. * A RTES node device representation
  57. * \note
  58. * Objects of this type are also acting as fwd list nodes.
  59. */
  60. typedef struct device {
  61. aem_t id; //!< AEM of the device
  62. struct device* next; //!< link to the next linked device on the chain
  63. } device_t;
  64. typedef device_t* devList_t; //!< device list alias
  65. /*!
  66. * \brief
  67. * Core message representation as it described in the requirements
  68. *
  69. * Object of this type constructed upon creation or when receiving a message.
  70. * \note
  71. * associate functions -- mutable-like interface:
  72. * \sa cMsg_parse() used for parsing and creation
  73. * \sa cMsg_getFromAEM() used as fromAEM getter
  74. * \sa cMsg_getToAEM() used as toAEM getter
  75. * \sa cMsg_getTs() used as timestamp getter
  76. * \sa cMsg_getText() used as text getter
  77. */
  78. typedef struct {
  79. aem_t from; //!< sender's AEM
  80. aem_t to; //!< destination AEM
  81. tstamp_t ts; //!< UNIX timestamp compatible
  82. size_t text; //!< text offset
  83. char_t msg[MSG_TEXT_SIZE]; //!< The actual message stream
  84. } cMsg_t;
  85. /*!
  86. * \brief
  87. * Mid and application layer message representation
  88. *
  89. * This type
  90. */
  91. typedef struct {
  92. device_t sender; //!< The sender's device
  93. devList_t recipients; //!< List of all devices the message has reach
  94. cMsg_t cMsg; //!< actual message payload
  95. } msg_t;
  96. typedef int32_t mIter_t; //!< message list iterator type
  97. /*!
  98. * \brief Message list
  99. *
  100. * This holds the last \a MSG_LIST_SIZE messages exchanged from this
  101. * device(including the ones we have create).
  102. *
  103. * With this we create a 2 dimensional map of msg/dev where each item
  104. * of the list is linked with all the devices reached by us as a fwd-list.
  105. * The items on the msgList are:
  106. * - Messages we create
  107. * - Messages received by the listener
  108. *
  109. * Here we define 2 directions for iteration. The message direction and the device
  110. * direction.
  111. *
  112. * Every node on the msgList.m array represents a message. Every node in the device
  113. * list inside msgList[m].recipients represent devices we don't anymore need to send
  114. * the current message to them.
  115. *
  116. * Layout example:
  117. *
  118. * msgList.m
  119. * [ 0 ] --> [devx] --> [devy] -- >0
  120. * | [ 1 ] --> [devy] -- > 0
  121. * time | [ 2 ] --> 0
  122. * [*1] | [ 3 ] ...
  123. * \|/
  124. * ...
  125. *
  126. * [MAX]
  127. *
  128. * [*1]: msgList is actually implemented as a ring buffer so in that
  129. * content, "time is a loop".
  130. */
  131. typedef struct {
  132. msg_t m[MSG_LIST_SIZE]; //!< The actual data representation
  133. mIter_t last; //!< A ring buffer iterator marking the last item on .m
  134. size_t size;
  135. } msgList_t;
  136. //! @}
  137. /*!
  138. * Client types
  139. */
  140. //! @{
  141. #define PING_PKT_S (64)
  142. #define PING_MSG_S (PING_PKT_S - sizeof(struct icmphdr))
  143. typedef struct {
  144. struct icmphdr hdr;
  145. char msg[PING_MSG_S];
  146. }ping_pkt_t;
  147. //! @}
  148. /*!
  149. * Application settings
  150. */
  151. //! @{
  152. typedef enum {
  153. OUTLEVEL_0, //!< Output only results [default]
  154. OUTLEVEL_1, //!< Output results and every message also
  155. OUTLEVEL_2 //!< Debug level, use with care!
  156. }outLevel_en;
  157. typedef struct {
  158. uint16_t port;
  159. time_t duration;
  160. time_t msgInterval;
  161. outLevel_en outLevel;
  162. }settings_t;
  163. extern settings_t settings;
  164. #define settings_init(s) s = { \
  165. .port = 2288, \
  166. .duration = 7200, \
  167. .msgInterval = 60, \
  168. .outLevel = OUTLEVEL_1 \
  169. }
  170. //! @}
  171. #endif /* __msg_impl__ */