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.
 
 
 
 
 

111 lines
3.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);
  20. devList_t devList_init(devList[AEMLIST_SIZE]);
  21. stats_t stats;
  22. //! @}
  23. /*!
  24. * CLI short options
  25. */
  26. const char *short_opt = "li:v:p:s:w:th";
  27. /*!
  28. * CLI long options
  29. */
  30. const struct option long_opt[] = {
  31. {"port", required_argument, NULL, 'l'},
  32. {"interval", required_argument, NULL, 'i'},
  33. {"outlevel", required_argument, NULL, 'v'},
  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 in Pointer to \ref kcli_input_t structure 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 'l': s->port = atoi(optarg); break;
  59. case 'i': s->msgInterval = atoi (optarg); break;
  60. case 'v':
  61. s->outLevel = atoi (optarg);
  62. if (s->outLevel >= OUTLEVEL_2) s->outLevel = OUTLEVEL_2;
  63. if (s->outLevel < OUTLEVEL_0) s->outLevel = OUTLEVEL_0;
  64. 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': printf ("This will be the help text\n"); break;
  70. case ':':
  71. default:
  72. case '?':
  73. fprintf (stderr, "%s: invalid option -- %c\n", argv[0], c);
  74. fprintf (stderr, "Try `%s --help' for more information.\n", argv[0]);
  75. exit(1);
  76. }
  77. }
  78. return 0;
  79. }
  80. int main(int argc, char const *argv[]) {
  81. parse_args (&settings, argc, argv);
  82. // Initialize all subsystems
  83. log_init ();
  84. stats_init (&stats);
  85. msgList_init (&msgList);
  86. // Create threads
  87. pthread_t ptL, ptC;
  88. pthread_create (&ptL, NULL, pthListener, NULL);
  89. pthread_create (&ptC, NULL, pthClient, NULL);
  90. // block here
  91. pthread_join (ptL, NULL);
  92. pthread_join (ptC, NULL);
  93. return 0;
  94. }