main.cpp 5.7 KB

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