main.cpp 5.9 KB

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