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.
 
 
 
 
 

124 lignes
3.8 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: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. {"pingtimeout",required_argument, NULL, 'p'},
  34. {"sendtimeout",required_argument, NULL, 's'},
  35. {"who", required_argument, NULL, 'w'},
  36. {"tracktime", no_argument, NULL, 't'},
  37. {"help", no_argument, NULL, 'h'},
  38. {NULL, 0, NULL, 0}
  39. };
  40. /*!
  41. * \brief
  42. * Parse input argument and fill the kcli_input_t struct
  43. * \param s Pointer to settings_t data to fill
  44. * \param argc The argument count as passed to the main()
  45. * \param argv Argument array as passed to the main()
  46. * \return The status of the operation
  47. * \arg 0 Success
  48. * \arg 1 Fail
  49. */
  50. int parse_args (settings_t *s, int argc, char const *argv[]) {
  51. int c;
  52. while ((c = getopt_long (argc, (char *const *)argv, short_opt, long_opt, NULL)) != -1) {
  53. switch (c) {
  54. case -1: /* no more arguments */
  55. case 0: /* long options toggles */
  56. break;
  57. case 'v':
  58. s->outLevel = atoi (optarg);
  59. if (s->outLevel >= OUTLEVEL_2) s->outLevel = OUTLEVEL_2;
  60. if (s->outLevel < OUTLEVEL_0) s->outLevel = OUTLEVEL_0;
  61. break;
  62. case 'i': s->seekerInterval = atoi (optarg); break;
  63. case 'p': s->pingTimeout = atoi (optarg); break;
  64. case 's': s->sendTimeout.tv_sec = atoi (optarg); break;
  65. case 'w': s->me = atoi (optarg); break;
  66. case 't': s->trackTime = true; break;
  67. case 'h':
  68. printf ("Syntax:\nrtes_final [-t] [-v num] [-i num] [-p num] [-s num] [-w num]\n\n");
  69. printf ("-v, --outlevel num: Change the verbosity of the program, num can be 0, 1 or 2\n");
  70. printf ("-i, --interval sec: Set the interval of the seeker in [sec]\n");
  71. printf ("-p, --pingtimeout sec: Set the ping timeout in [sec]\n");
  72. printf ("-s, --sendtimeout sec: Set the connect/send timeout in [sec]\n");
  73. printf ("-w, --who AEM: Select the AEM of the device\n");
  74. printf ("-t, --tracktime: Enables time tracking in statistics\n");
  75. printf ("-h, --help: Print this and exit\n");
  76. exit(1);
  77. case ':':
  78. default:
  79. case '?':
  80. fprintf (stderr, "%s: invalid option -- %c\n", argv[0], c);
  81. fprintf (stderr, "Try `%s --help' for more information.\n", argv[0]);
  82. exit(1);
  83. }
  84. }
  85. return 0;
  86. }
  87. /*!
  88. * Main function
  89. */
  90. int main (int argc, char const *argv[]) {
  91. // get command line arguments
  92. parse_args (&settings, argc, argv);
  93. // Initialize all subsystems
  94. log_init ();
  95. stats_init (&stats);
  96. devList_init (devList);
  97. msgList_init (&msgList);
  98. // Create threads
  99. pthread_t ptL, ptS, ptC;
  100. pthread_create (&ptL, NULL, pthListener, NULL);
  101. pthread_create (&ptS, NULL, pthSeeker, NULL);
  102. pthread_create (&ptC, NULL, pthClient, NULL);
  103. // block here
  104. pthread_join (ptL, NULL);
  105. pthread_join (ptS, NULL);
  106. pthread_join (ptC, NULL);
  107. return 0;
  108. }