main.cpp 5.8 KB

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