main.cpp 5.8 KB

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