main.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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. logger(V_BASIC, "main.cpp creating threads...\n");
  127. pthread_attr_init(&attr);
  128. pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
  129. rc = pthread_mutexattr_settype(&mutexattr, PTHREAD_MUTEX_RECURSIVE_NP);
  130. if (rc != 0)
  131. throw(L"pthread_mutexattr_settype returns " + rc);
  132. pthread_mutex_init(&mutex_spi, &mutexattr);
  133. pthread_mutex_init(&mutex_i2c, &mutexattr);
  134. pthread_mutex_init(&mutex_logger, &mutexattr);
  135. rc = pthread_mutexattr_destroy(&mutexattr);
  136. if (rc != 0)
  137. throw(L"pthread_mutexattr_destroy returns " + rc);
  138. for (int i = 0; i < 5; i++) {//TODO this can be simplified!
  139. if (i == THREAD_MAIN)
  140. rc = pthread_create(&thread[i], &attr, mainLoop, (void *) i);
  141. else if (i == THREAD_STRIPE)
  142. rc = pthread_create(&thread[i], NULL, stripeThread, (void *) i);
  143. else if (i == THREAD_DISPLAY)
  144. rc = pthread_create(&thread[i], NULL, displayThread, (void *) i);
  145. else if (i == THREAD_COFFEE)
  146. rc = pthread_create(&thread[i], NULL, coffeeThread, (void *) i);
  147. else if (i == THREAD_SERVER && !optNoNet)
  148. rc = pthread_create(&thread[i], NULL, serverThread, (void *) i);
  149. if (rc) {
  150. logger_error("Error:unable to create thread %d (%d)\n", i, rc);
  151. exit(-1);
  152. }
  153. }
  154. //hal might send signals to processes which must exist!
  155. halInit();
  156. // free attribute and wait for the other threads
  157. pthread_attr_destroy(&attr);
  158. sleep(2);
  159. rc = pthread_join(thread[0], &status);
  160. if (rc) {
  161. logger_error("Error:unable to join, %d\n", rc);
  162. exit(-1);
  163. }
  164. logger(V_BASIC, "Completed thread with status %d\n", rc);
  165. pthread_exit(NULL);
  166. return EXIT_FAILURE;
  167. }
  168. /**
  169. * Handler for program termination caught via signal
  170. */
  171. void terminationHandler(int signum) {
  172. logger(V_NONE, "Caught signal %d, exiting gracefully..\n", signum);
  173. stopTimers();
  174. //TODO This event is not fired anymore...
  175. event_trigger("terminate");
  176. /*logger(V_NONE, "Saving my state to the database..\n");
  177. sqlExecute("begin");
  178. // Save stuff here
  179. sqlExecute("end");*/
  180. sqlClose(); // Closing DB connection
  181. exit(EXIT_SUCCESS);
  182. }
  183. /**
  184. * Handles the signal USR2
  185. */
  186. void usr1Handler(int signum) {
  187. // Do something here?
  188. logger_reset();
  189. }
  190. /**
  191. * Handles the signal HUP and starts a speed test
  192. */
  193. void hupHandler(int signum) {
  194. // Do something here?
  195. }
  196. /**
  197. * Sends a signal to a thread
  198. * @param threadid ID of the thread
  199. * @param sig Signal to send
  200. */
  201. void killThread(int threadid, int sig) {
  202. //logger(V_BASIC, "pthread_kill on thread %d (%d)\n", threadid, sig);
  203. if (pthread_kill(thread[threadid], sig)) {
  204. logger_error("pthread_kill on thread %d failed (%d)\n", threadid, sig);
  205. }
  206. }
  207. /**
  208. * Main Thread, used for some initializations and error detection
  209. * @param threadid Thread ID
  210. */
  211. void *mainLoop(void *threadid) {
  212. // Do more stuff here
  213. while (1){
  214. sleep(4);
  215. /*int8_t temp = DS18B20_readTemp();
  216. logger(V_BASIC, "Temperature: %d\n", temp);*/
  217. }
  218. logger(V_BASIC, "Thread goes Sleeping..\n");
  219. while (1) {
  220. pause();
  221. logger(V_NONE, LOG_ERROR, "Whoops. Something went wrong..\n");
  222. }
  223. pthread_exit(NULL);
  224. }