main.cpp 6.3 KB

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