A messenger application for Raspberry Pi Zerofor A.U.TH (Real time Embedded systems).
Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.
 
 
 
 
 

242 Zeilen
6.9 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 <sys/time.h>
  14. #include <netinet/ip_icmp.h>
  15. #include <netinet/in.h>
  16. /*!
  17. * AEM list
  18. */
  19. #define AEMLIST_SIZE (10)
  20. #define devList_init(l) l = { \
  21. { 7700, 0, 0, 0}, \
  22. { 8000, 0, 0, 0}, \
  23. { 8765, 0, 0, 0}, \
  24. { 8844, 0, 0, 0}, \
  25. { 8880, 0, 0, 0}, \
  26. { 8861, 0, 0, 0}, \
  27. { 8877, 0, 0, 0}, \
  28. { 8941, 0, 0, 0}, \
  29. { 8934, 0, 0, 0}, \
  30. { 8997, 0, 0, 0} \
  31. }
  32. /*!
  33. * General options
  34. */
  35. //! @{
  36. #define MSG_TEXT_SIZE 256 //!< Maximum size of each message
  37. #define MSG_LIST_SIZE 2000 //!< Maximum size of message history buffer
  38. #define DEVICE_LIST_SIZE 100 //!< Maximum size of the device list
  39. #define MSG_DELIMITER '_' //!< Message delimiter
  40. #define ADHOC_NET_A 10 //!< [A.B.C.D]
  41. #define ADHOC_NET_B 0
  42. #define ADHOC_NET_C 0
  43. #define ADHOC_NET_D 0
  44. #define MESSAGE_BODY "The ships hung in the sky in much the same way that bricks don't!"
  45. //! @}
  46. /*!
  47. * Helper macros
  48. */
  49. /*!
  50. * Messenger types
  51. */
  52. //! @{
  53. /*!
  54. * Application status type
  55. */
  56. typedef enum {
  57. MSG_OK =0, //!< Indicate success
  58. MSG_ERROR //!< Indicate error
  59. } status_t;
  60. typedef bool bool_t; //!< Boolean type
  61. typedef char char_t; //!< Application wide character type
  62. typedef int32_t iter_t; //!< General iterator type
  63. typedef uint32_t aem_t; //!< AEM data type
  64. typedef int64_t tstamp_t; //!< UNIX time in 64 bit wide signed integer
  65. typedef aem_t devAEM_t; //!< device as AEM type
  66. /*!
  67. * device as IP type
  68. */
  69. typedef struct {
  70. uint16_t A, B, C, D;
  71. }devIP_t;
  72. typedef double fpdata_t; //!< Select floating point data type for the application
  73. // Syntactic sugar types
  74. typedef struct sockaddr_in sockaddr_in_t; //!< internet socket address type definition
  75. typedef struct sockaddr sockaddr_t; //!< general socket address type definition
  76. typedef struct timeval timeval_t;
  77. /*!
  78. * AEM list for our mesh network
  79. */
  80. typedef struct {
  81. devAEM_t dev;
  82. bool_t onRange;
  83. tstamp_t begin;
  84. tstamp_t end;
  85. } devList_t;
  86. extern devList_t devList[];
  87. /*!
  88. * \brief
  89. * Core message representation as it described in the requirements
  90. *
  91. * Object of this type constructed upon creation or when receiving a message.
  92. * \note
  93. * associate functions -- mutable-like interface:
  94. * \sa cMsg_parse() used for parsing and creation
  95. * \sa cMsg_getFromAEM() used as fromAEM getter
  96. * \sa cMsg_getToAEM() used as toAEM getter
  97. * \sa cMsg_getTs() used as timestamp getter
  98. * \sa cMsg_getText() used as text getter
  99. */
  100. typedef struct {
  101. devAEM_t from; //!< sender's AEM
  102. devAEM_t to; //!< destination AEM
  103. tstamp_t ts; //!< UNIX timestamp compatible
  104. char_t text[MSG_TEXT_SIZE]; //!< The actual message stream
  105. } cMsg_t;
  106. /*!
  107. * \brief
  108. * Mid and application layer message representation
  109. *
  110. * This type
  111. */
  112. typedef struct {
  113. devAEM_t sender; //!< The sender's device
  114. bool_t recipients[AEMLIST_SIZE]; //!< List of all devices the message has reached.
  115. //! Used as pair mapped in devList array, so each slot here corresponds in
  116. //! the same AEM in devList.
  117. cMsg_t cMsg; //!< actual message payload
  118. } msg_t;
  119. typedef iter_t mIter_t; //!< message list iterator type
  120. typedef iter_t dIter_t; //!< device list iterator type
  121. /*!
  122. * \brief Message list
  123. *
  124. * This holds the last \a MSG_LIST_SIZE messages exchanged from this
  125. * device(including the ones we have create).
  126. *
  127. * With this we create a 2 dimensional map of msg/dev where each item
  128. * of the list is linked with all the devices reached by us as a fwd-list.
  129. * The items on the msgList are:
  130. * - Messages we create
  131. * - Messages received by the listener
  132. *
  133. * Here we define 2 directions for iteration. The message direction and the device
  134. * direction.
  135. *
  136. * Every node on the msgList.m array represents a message. Every node in the device
  137. * list inside msgList[m].recipients represent devices we don't anymore need to send
  138. * the current message to them.
  139. *
  140. * Layout example:
  141. *
  142. * msgList.m
  143. * dev1 dev2 dev3 ...
  144. * [ 0 ] [ ] [x] [ ] <-- x marks "message has been send"
  145. * | [ 1 ] [x] [x] [ ] <-- x marks "message has been send"
  146. * time | [ 2 ]
  147. * [*1] | [ 3 ] ...
  148. * \|/
  149. * ...
  150. *
  151. * [MAX]
  152. *
  153. * [*1]: msgList is actually implemented as a ring buffer so in that
  154. * content, "time is a loop".
  155. */
  156. typedef struct {
  157. msg_t m[MSG_LIST_SIZE]; //!< The actual data representation
  158. mIter_t first; //!< A ring buffer iterator for begin()
  159. mIter_t last; //!< A ring buffer iterator marking the last item on .m
  160. size_t size;
  161. } msgList_t;
  162. //! @}
  163. /*!
  164. * Application settings
  165. */
  166. //! @{
  167. /*!
  168. * Statistical data type
  169. */
  170. typedef struct {
  171. uint32_t totalMsg; //!< Total messages processed (both incoming and created)
  172. uint32_t duplicateMsg; //!< Incoming duplicate messages
  173. uint32_t forMeMsg; //!< Incoming messages for me
  174. uint32_t myMsg; //!< Messages created by me
  175. uint32_t inDirectMsg; //!< Incoming messages created by the sender for me
  176. uint32_t outDirectMsg; //!< Outgoing messages from me for the recipient
  177. fpdata_t avMsgSize; //!< average message payload size
  178. fpdata_t avTimeToMe; //!< average time to me
  179. } stats_t;
  180. extern stats_t stats;
  181. typedef enum {
  182. OUTLEVEL_0, //!< Output only results [default]
  183. OUTLEVEL_1, //!< Output results and every message also
  184. OUTLEVEL_2 //!< Debug level, use with care!
  185. }outLevel_en;
  186. typedef struct {
  187. devAEM_t me;
  188. uint16_t port;
  189. time_t msgInterval;
  190. outLevel_en outLevel;
  191. time_t pingTimeout;
  192. timeval_t sendTimeout;
  193. bool_t trackTime;
  194. }settings_t;
  195. extern settings_t settings;
  196. #define settings_init(s) s = { \
  197. .me = 8997, \
  198. .port = 2288, \
  199. .msgInterval = 60, \
  200. .outLevel = OUTLEVEL_1, \
  201. .pingTimeout = 1, \
  202. .sendTimeout = {4, 0}, \
  203. .trackTime = true \
  204. }
  205. //! @}
  206. #endif /* __msg_impl__ */