A messenger application for Raspberry Pi Zerofor A.U.TH (Real time Embedded systems).
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 
 

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