main.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. /*
  2. * main.cpp
  3. *
  4. * Created on: Nov 2, 2015
  5. * Author: Philipp Hinz
  6. */
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <string.h>
  10. #include <errno.h>
  11. #include <stdint.h>
  12. #include <wiringPi.h>
  13. #include <pthread.h>
  14. #include <unistd.h>
  15. #include <iostream>
  16. #include <csignal>
  17. #include "global.h"
  18. #include "timer.h"
  19. #include "database.h"
  20. #include "logger.h"
  21. #include "lcd.h"
  22. #include "hal.h"
  23. #include "stripe.h"
  24. #include "coffee.h"
  25. #include "display.h"
  26. #include "server.h"
  27. #include "events.h"
  28. const int buildno = (1+(int)(
  29. #include "buildno"
  30. ));
  31. int verbose = 0;
  32. bool optDate = false;
  33. bool optPower = false;
  34. bool optNoNet = false;
  35. void *mainLoop(void *threadid);
  36. void terminationHandler(int signum);
  37. void usr1Handler(int signum);
  38. void hupHandler(int signum);
  39. pthread_t thread[4];
  40. pthread_mutex_t mutex_spi;
  41. pthread_mutex_t mutex_i2c;
  42. pthread_mutex_t mutex_logger;
  43. int main(int argc, char *argv[]) {
  44. pthread_attr_t attr;
  45. pthread_mutexattr_t mutexattr;
  46. void *status;
  47. int rc;
  48. int opt;
  49. int prev_ind;
  50. logger(V_NONE, LOG_INFO, "CoffeePi by Philipp Hinz and Sebastian Vendt - "
  51. "Build number %d\n", buildno);
  52. // Argument handling
  53. while (prev_ind = optind, (opt = getopt(argc, argv, "?hvVdpn")) != -1) {
  54. if (argc > 3)
  55. if (optind == prev_ind + 2 && *optarg == '-') {
  56. opt = '?';
  57. --optind;
  58. }
  59. switch (opt) {
  60. case 'v': // be verbose
  61. verbose++;
  62. break;
  63. case 'V':
  64. //printf("Build number %d\n", buildno);
  65. return EXIT_SUCCESS;
  66. break;
  67. case 'd': // Print timestamp in every line
  68. optDate = true;
  69. break;
  70. case 'p': // power machine on program start
  71. optPower = true;
  72. break;
  73. case 'n': // disable network functionality
  74. optNoNet = true;
  75. break;
  76. case '?':
  77. case 'h':
  78. printf("Usage: %s [-hvVdp]\n", argv[0]);
  79. printf("\t-h\t\tPrints this help\n");
  80. printf("\t-v\t\tSets verbose output, can be used multiple times\n");
  81. printf("\t-V\t\tPrints only the version and exits\n");
  82. printf("\t-d\t\tPrints a timestamp before every line\n");
  83. printf("\t-p\t\tPowers the machine on program start\n");
  84. printf("\t-n\t\tDisable network functionality\n");
  85. printf("Listening to the following signals:\n");
  86. printf("\tSIGUSR1(%u)\tPrints monitor data of all nodes\n",
  87. SIGUSR1);
  88. printf("\tSIGHUP(%u)\tRuns a speedtest starting at default speed\n",
  89. SIGHUP);
  90. return EXIT_SUCCESS;
  91. default:
  92. fprintf(stderr, "Usage: %s [-hvVdpn]\n", argv[0]);
  93. return EXIT_FAILURE;
  94. break;
  95. }
  96. }
  97. if (signal(SIGINT, terminationHandler)) { // Calls terminationHandler if SIGINT is received
  98. fprintf(stderr, "Unable to set signal handler for SIGINT: %s\n",
  99. strerror(errno));
  100. }
  101. if (signal(SIGUSR1, usr1Handler)) { // Calls terminationHandler if SIGINT is received
  102. fprintf(stderr, "Unable to set signal handler for SIGUSR1: %s\n",
  103. strerror(errno));
  104. }
  105. if (signal(SIGHUP, hupHandler)) { // Calls terminationHandler if SIGINT is received
  106. fprintf(stderr, "Unable to set signal handler for SIGHUP: %s\n",
  107. strerror(errno));
  108. }
  109. // sets up the wiringPi library
  110. if (wiringPiSetup() < 0) {
  111. fprintf(stderr, "Unable to setup wiringPi: %s\n", strerror(errno));
  112. return 1;
  113. }
  114. logger_reset();
  115. initTimers();
  116. displayInit();
  117. // http://www.tutorialspoint.com/cplusplus/cpp_multithreading.htm
  118. // Initialize and set thread joinable
  119. pthread_attr_init(&attr);
  120. pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
  121. rc = pthread_mutexattr_settype(&mutexattr, PTHREAD_MUTEX_RECURSIVE_NP);
  122. if (rc != 0)
  123. throw(L"pthread_mutexattr_settype returns " + rc);
  124. pthread_mutex_init(&mutex_spi, &mutexattr);
  125. pthread_mutex_init(&mutex_i2c, &mutexattr);
  126. pthread_mutex_init(&mutex_logger, &mutexattr);
  127. rc = pthread_mutexattr_destroy(&mutexattr);
  128. if (rc != 0)
  129. throw(L"pthread_mutexattr_destroy returns " + rc);
  130. for (int i = 0; i < 5; i++) {
  131. if (i == THREAD_MAIN)
  132. rc = pthread_create(&thread[i], NULL, mainLoop, (void *) i);
  133. else if (i == THREAD_STRIPE)
  134. rc = pthread_create(&thread[i], NULL, stripeThread, (void *) i);
  135. else if (i == THREAD_DISPLAY)
  136. rc = pthread_create(&thread[i], NULL, displayThread, (void *) i);
  137. else if (i == THREAD_COFFEE)
  138. rc = pthread_create(&thread[i], NULL, coffeeThread, (void *) i);
  139. else if (i == THREAD_SERVER && !optNoNet)
  140. rc = pthread_create(&thread[i], NULL, serverThread, (void *) i);
  141. if (rc) {
  142. logger_error("Error:unable to create thread %d (%d)\n", i, rc);
  143. exit(-1);
  144. }
  145. }
  146. halInit();
  147. // free attribute and wait for the other threads
  148. pthread_attr_destroy(&attr);
  149. sleep(2);
  150. rc = pthread_join(thread[0], &status);
  151. if (rc) {
  152. logger_error("Error:unable to join, %d\n", rc);
  153. exit(-1);
  154. }
  155. logger(V_BASIC, "Completed thread with status %d\n", rc);
  156. pthread_exit(NULL);
  157. return EXIT_FAILURE;
  158. }
  159. /**
  160. * Handler for program termination caught via signal
  161. */
  162. void terminationHandler(int signum) {
  163. logger(V_NONE, "Caught signal %d, exiting gracefully..\n", signum);
  164. stopTimers();
  165. event_trigger("terminate");
  166. /*logger(V_NONE, "Saving my state to the database..\n");
  167. sqlExecute("begin");
  168. // Save stuff here
  169. sqlExecute("end");*/
  170. sqlClose(); // Closing DB connection
  171. exit(EXIT_SUCCESS);
  172. }
  173. /**
  174. * Handles the signal USR2
  175. */
  176. void usr1Handler(int signum) {
  177. // Do something here?
  178. logger_reset();
  179. }
  180. /**
  181. * Handles the signal HUP and starts a speed test
  182. */
  183. void hupHandler(int signum) {
  184. // Do something here?
  185. }
  186. /**
  187. * Sends a signal to a thread
  188. * @param threadid ID of the thread
  189. * @param sig Signal to send
  190. */
  191. void killThread(int threadid, int sig) {
  192. //logger(V_BASIC, "pthread_kill on thread %d (%d)\n", threadid, sig);
  193. if (pthread_kill(thread[threadid], sig)) {
  194. logger_error("pthread_kill on thread %d failed (%d)\n", threadid, sig);
  195. }
  196. }
  197. /**
  198. * Main Thread, used for some initializations and error detection
  199. * @param threadid Thread ID
  200. */
  201. void *mainLoop(void *threadid) {
  202. sqlOpen();
  203. sqlSetup();
  204. // Do more stuff here
  205. logger(V_BASIC, "Thread goes Sleeping..\n");
  206. while (1) {
  207. pause();
  208. logger(V_NONE, LOG_ERROR, "Whoops. Something went wrong..\n");
  209. }
  210. pthread_exit(NULL);
  211. }