A messenger application for Raspberry Pi Zerofor A.U.TH (Real time Embedded systems).
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
 
 
 
 
 

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