main.cpp 5.5 KB

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