main.cpp 6.2 KB

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