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.
 
 
 
 
 

131 lines
4.3 KiB

  1. /*!
  2. * \file main.c
  3. * This is the main file of the RTES final task.
  4. *
  5. * \author Christos Choutouridis AEM:8997 <cchoutou@ece.auth.gr>
  6. */
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <getopt.h>
  10. #include<signal.h>
  11. #include <unistd.h>
  12. #include <pthread.h>
  13. #include "listener.h"
  14. #include "client.h"
  15. /*!
  16. * Global data
  17. */
  18. //! @{
  19. settings_t settings_init (settings); //!< Application settings
  20. devList_t devList[AEMLIST_SIZE]; //!< Device list
  21. stats_t stats; //!< Statistical data
  22. //! @}
  23. /*!
  24. * CLI short options
  25. */
  26. const char *short_opt = "v:i:m:M:p:s:w:th";
  27. /*!
  28. * CLI long options
  29. */
  30. const struct option long_opt[] = {
  31. {"outlevel", required_argument, NULL, 'v'},
  32. {"interval", required_argument, NULL, 'i'},
  33. {"msgintervalmin", required_argument, NULL, 'm'},
  34. {"msgintervalmax", required_argument, NULL, 'M'},
  35. {"pingtimeout", required_argument, NULL, 'p'},
  36. {"sendtimeout", required_argument, NULL, 's'},
  37. {"who", required_argument, NULL, 'w'},
  38. {"tracktime", no_argument, NULL, 't'},
  39. {"help", no_argument, NULL, 'h'},
  40. {NULL, 0, NULL, 0}
  41. };
  42. /*!
  43. * \brief
  44. * Parse input argument and fill the kcli_input_t struct
  45. * \param s Pointer to settings_t data to fill
  46. * \param argc The argument count as passed to the main()
  47. * \param argv Argument array as passed to the main()
  48. * \return The status of the operation
  49. * \arg 0 Success
  50. * \arg 1 Fail
  51. */
  52. int parse_args (settings_t *s, int argc, char const *argv[]) {
  53. int c;
  54. while ((c = getopt_long (argc, (char *const *)argv, short_opt, long_opt, NULL)) != -1) {
  55. switch (c) {
  56. case -1: /* no more arguments */
  57. case 0: /* long options toggles */
  58. break;
  59. case 'v':
  60. s->outLevel = atoi (optarg);
  61. if (s->outLevel >= OUTLEVEL_2) s->outLevel = OUTLEVEL_2;
  62. if (s->outLevel < OUTLEVEL_0) s->outLevel = OUTLEVEL_0;
  63. break;
  64. case 'i': s->seekerInterval = atoi (optarg); break;
  65. case 'm': s->msgIntervalMin = atoi (optarg); break;
  66. case 'M': s->msgIntervalMax = atoi (optarg); break;
  67. case 'p': s->pingTimeout = atoi (optarg); break;
  68. case 's': s->sendTimeout.tv_sec = atoi (optarg); break;
  69. case 'w': s->me = atoi (optarg); break;
  70. case 't': s->trackTime = true; break;
  71. case 'h':
  72. printf ("Syntax:\n");
  73. printf ("rtes_final [-t] [-h] [-v num] [-i num] [-m num] [-M num] [-p num] [-s num] [-w num]\n\n");
  74. printf ("-v, --outlevel num: Change the verbosity of the program, num can be 0, 1 or 2\n");
  75. printf ("-i, --interval sec: Set the interval of the seeker in [sec]\n");
  76. printf ("-m, --msgintervalmin sec: Set the interval of the client in [sec]\n");
  77. printf ("-M, --msgintervalmax sec: Set the interval of the client in [sec]\n");
  78. printf ("-p, --pingtimeout sec: Set the ping timeout in [sec]\n");
  79. printf ("-s, --sendtimeout sec: Set the connect/send timeout in [sec]\n");
  80. printf ("-w, --who AEM: Select the AEM of the device\n");
  81. printf ("-t, --tracktime: Enables time tracking in statistics\n");
  82. printf ("-h, --help: Print this and exit\n");
  83. exit(1);
  84. case ':':
  85. default:
  86. case '?':
  87. fprintf (stderr, "%s: invalid option -- %c\n", argv[0], c);
  88. fprintf (stderr, "Try `%s --help' for more information.\n", argv[0]);
  89. exit(1);
  90. }
  91. }
  92. return 0;
  93. }
  94. /*!
  95. * Main function
  96. */
  97. int main (int argc, char const *argv[]) {
  98. // get command line arguments
  99. parse_args (&settings, argc, argv);
  100. // Initialize all subsystems
  101. log_init ();
  102. stats_init (&stats);
  103. devList_init (devList);
  104. msgList_init (&msgList);
  105. // Create threads
  106. pthread_t ptL, ptS, ptC;
  107. pthread_create (&ptL, NULL, pthListener, NULL);
  108. pthread_create (&ptS, NULL, pthSeeker, NULL);
  109. pthread_create (&ptC, NULL, pthClient, NULL);
  110. // block here
  111. pthread_join (ptL, NULL);
  112. pthread_join (ptS, NULL);
  113. pthread_join (ptC, NULL);
  114. return 0;
  115. }