main.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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, "vVdp")) != -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-p\t\tPowers the machine on program start\n");
  75. printf("Listening to the following signals:\n");
  76. printf("\tSIGUSR1(%u)\tPrints monitor data of all nodes\n",
  77. SIGUSR1);
  78. printf("\tSIGHUP(%u)\tRuns a speedtest starting at default speed\n",
  79. SIGHUP);
  80. return EXIT_SUCCESS;
  81. default:
  82. fprintf(stderr, "Usage: %s [-hvVdp]\n", argv[0]);
  83. return EXIT_FAILURE;
  84. break;
  85. }
  86. }
  87. if (signal(SIGINT, terminationHandler)) { // Calls terminationHandler if SIGINT is received
  88. fprintf(stderr, "Unable to set signal handler for SIGINT: %s\n",
  89. strerror(errno));
  90. }
  91. if (signal(SIGUSR1, usr1Handler)) { // Calls terminationHandler if SIGINT is received
  92. fprintf(stderr, "Unable to set signal handler for SIGUSR1: %s\n",
  93. strerror(errno));
  94. }
  95. if (signal(SIGHUP, hupHandler)) { // Calls terminationHandler if SIGINT is received
  96. fprintf(stderr, "Unable to set signal handler for SIGHUP: %s\n",
  97. strerror(errno));
  98. }
  99. // sets up the wiringPi library
  100. if (wiringPiSetup() < 0) {
  101. fprintf(stderr, "Unable to setup wiringPi: %s\n", strerror(errno));
  102. return 1;
  103. }
  104. logger_reset();
  105. initTimers();
  106. displayInit();
  107. // http://www.tutorialspoint.com/cplusplus/cpp_multithreading.htm
  108. // Initialize and set thread joinable
  109. pthread_attr_init(&attr);
  110. pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
  111. rc = pthread_mutexattr_settype(&mutexattr, PTHREAD_MUTEX_RECURSIVE_NP);
  112. if (rc != 0)
  113. throw(L"pthread_mutexattr_settype returns " + rc);
  114. pthread_mutex_init(&mutex, &mutexattr);
  115. rc = pthread_mutexattr_destroy(&mutexattr);
  116. if (rc != 0)
  117. throw(L"pthread_mutexattr_destroy returns " + rc);
  118. for (int i = 0; i < 4; i++) {
  119. if (i == THREAD_MAIN)
  120. rc = pthread_create(&thread[i], NULL, mainLoop, (void *) i);
  121. else if (i == THREAD_STRIPE)
  122. rc = pthread_create(&thread[i], NULL, stripeThread, (void *) i);
  123. else if (i == THREAD_DISPLAY)
  124. rc = pthread_create(&thread[i], NULL, displayThread, (void *) i);
  125. else if (i == THREAD_COFFEE)
  126. rc = pthread_create(&thread[i], NULL, coffeeThread, (void *) i);
  127. if (rc) {
  128. logger_error("Error:unable to create thread %d (%d)\n", i, rc);
  129. exit(-1);
  130. }
  131. }
  132. halInit();
  133. // free attribute and wait for the other threads
  134. pthread_attr_destroy(&attr);
  135. sleep(2);
  136. rc = pthread_join(thread[0], &status);
  137. if (rc) {
  138. logger_error("Error:unable to join, %d\n", rc);
  139. exit(-1);
  140. }
  141. logger(V_BASIC, "Completed thread with status %d\n", rc);
  142. pthread_exit(NULL);
  143. return EXIT_FAILURE;
  144. }
  145. /**
  146. * Handler for program termination caught via signal
  147. */
  148. void terminationHandler(int signum) {
  149. logger(V_NONE, "Caught signal %d, exiting gracefully..\n", signum);
  150. stopTimers();
  151. //coffeeTerminate();
  152. displayTerminate();
  153. /*logger(V_NONE, "Saving my state to the database..\n");
  154. sqlExecute("begin");
  155. // Save stuff here
  156. sqlExecute("end");*/
  157. sqlClose(); // Closing DB connection
  158. exit(EXIT_SUCCESS);
  159. }
  160. /**
  161. * Handles the signal USR2
  162. */
  163. void usr1Handler(int signum) {
  164. // Do something here?
  165. logger_reset();
  166. }
  167. /**
  168. * Handles the signal HUP and starts a speed test
  169. */
  170. void hupHandler(int signum) {
  171. // Do something here?
  172. }
  173. /**
  174. * Sends a signal to a thread
  175. * @param threadid ID of the thread
  176. * @param sig Signal to send
  177. */
  178. void killThread(int threadid, int sig) {
  179. //logger(V_BASIC, "pthread_kill on thread %d (%d)\n", threadid, sig);
  180. if (pthread_kill(thread[threadid], sig)) {
  181. logger_error("pthread_kill on thread %d failed (%d)\n", threadid, sig);
  182. }
  183. }
  184. /**
  185. * Main Thread, used for some initializations and error detection
  186. * @param threadid Thread ID
  187. */
  188. void *mainLoop(void *threadid) {
  189. sqlOpen();
  190. sqlSetup();
  191. // Do more stuff here
  192. logger(V_BASIC, "Thread goes Sleeping..\n");
  193. while (1) {
  194. pause();
  195. logger(V_NONE, LOG_ERROR, "Whoops. Something went wrong..\n");
  196. }
  197. pthread_exit(NULL);
  198. }