main.cpp 6.6 KB

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