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.1 KiB

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <getopt.h>
  4. #include<signal.h>
  5. #include <unistd.h>
  6. #include <pthread.h>
  7. #include "listener.h"
  8. #include "client.h"
  9. /*!
  10. * Global data
  11. */
  12. //! @{
  13. settings_t settings_init (settings);
  14. devList_t devList_init(devList[AEMLIST_SIZE]);
  15. stats_t stats;
  16. //! @}
  17. /*!
  18. * CLI short options
  19. */
  20. const char *short_opt = "li:v:p:s:w:th";
  21. /*!
  22. * CLI long options
  23. */
  24. const struct option long_opt[] = {
  25. {"port", required_argument, NULL, 'l'},
  26. {"interval", required_argument, NULL, 'i'},
  27. {"outlevel", required_argument, NULL, 'v'},
  28. {"pingtimeout",required_argument, NULL, 'p'},
  29. {"sendtimeout",required_argument, NULL, 's'},
  30. {"who", required_argument, NULL, 'w'},
  31. {"tracktime", no_argument, NULL, 't'},
  32. {"help", no_argument, NULL, 'h'},
  33. {NULL, 0, NULL, 0}
  34. };
  35. //// Interrupt handler
  36. //void intHandler(int dummy) {
  37. // (void)dummy;
  38. // statsPrint (&stats);
  39. // exit (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. log_init ();
  83. stats_init (&stats);
  84. msgList_init (&msgList);
  85. // if (signal(SIGTERM, intHandler) == SIG_ERR) //catching interrupt
  86. // log_error("Error: Can not catch SIGINT\n");
  87. // Create threads
  88. pthread_t ptL, ptC;
  89. pthread_create (&ptL, NULL, pthListener, NULL);
  90. pthread_create (&ptC, NULL, pthClient, NULL);
  91. // block here
  92. pthread_join (ptL, NULL);
  93. pthread_join (ptC, NULL);
  94. return 0;
  95. }