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.
 
 
 
 
 

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