main.cpp 5.6 KB

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