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.
 
 
 
 
 

102 lines
2.9 KiB

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <getopt.h>
  4. #include <pthread.h>
  5. #include "listener.h"
  6. #include "client.h"
  7. /*!
  8. * Global data
  9. */
  10. //! @{
  11. settings_t settings_init (settings);
  12. devList_t devList_init (devList[AEMLIST_SIZE]);
  13. stats_t stats;
  14. //! @}
  15. /*!
  16. * CLI short options
  17. */
  18. const char *short_opt = "nd:i:l:p:s:w:th";
  19. /*!
  20. * CLI long options
  21. */
  22. const struct option long_opt[] = {
  23. {"port", required_argument, NULL, 'n'},
  24. {"duration", required_argument, NULL, 'd'},
  25. {"interval", required_argument, NULL, 'i'},
  26. {"outlevel", required_argument, NULL, 'l'},
  27. {"pingtimeout",required_argument, NULL, 'p'},
  28. {"sendtimeout",required_argument, NULL, 's'},
  29. {"who", required_argument, NULL, 'w'},
  30. {"tracktime", no_argument, NULL, 't'},
  31. {"help", no_argument, NULL, 'h'},
  32. {NULL, 0, NULL, 0}
  33. };
  34. /*!
  35. * \brief
  36. * Parse input argument and fill the kcli_input_t struct
  37. * \param in Pointer to \ref kcli_input_t structure to fill
  38. * \param argc The argument count as passed to the main()
  39. * \param argv Argument array as passed to the main()
  40. * \return The status of the operation
  41. * \arg 0 Success
  42. * \arg 1 Fail
  43. */
  44. int parse_args (settings_t *s, int argc, char const *argv[]) {
  45. int c;
  46. while ((c = getopt_long (argc, (char *const *)argv, short_opt, long_opt, NULL)) != -1) {
  47. switch (c) {
  48. case -1: /* no more arguments */
  49. case 0: /* long options toggles */
  50. break;
  51. case 'n': s->port = atoi(optarg); break;
  52. case 'd': s->duration = atoi (optarg); break;
  53. case 'i': s->msgInterval = atoi (optarg); break;
  54. case 'l':
  55. s->outLevel = atoi (optarg);
  56. if (s->outLevel >= OUTLEVEL_2) s->outLevel = OUTLEVEL_2;
  57. if (s->outLevel < OUTLEVEL_0) s->outLevel = OUTLEVEL_0;
  58. break;
  59. case 'p': s->pingTimeout = atoi (optarg); break;
  60. case 's': s->sendTimeout.tv_sec = atoi (optarg); break;
  61. case 'w': s->me = atoi (optarg); break;
  62. case 't': s->trackTime = true; break;
  63. case 'h': printf ("This will be the help text\n");
  64. case ':':
  65. default:
  66. case '?':
  67. fprintf (stderr, "%s: invalid option -- %c\n", argv[0], c);
  68. fprintf (stderr, "Try `%s --help' for more information.\n", argv[0]);
  69. exit(1);
  70. }
  71. }
  72. return 0;
  73. }
  74. int main(int argc, char const *argv[]) {
  75. parse_args (&settings, argc, argv);
  76. log_init ();
  77. stats_init (&stats);
  78. msgList_init (&msgList);
  79. // Create threads
  80. pthread_t ptL, ptC;
  81. pthread_create (&ptL, NULL, pthListener, NULL);
  82. pthread_create (&ptC, NULL, pthClient, NULL);
  83. // block here
  84. pthread_join (ptL, NULL);
  85. pthread_join (ptC, NULL);
  86. return 0;
  87. }