main.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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 levels:
  58. * 1 Basic debugging
  59. * 2 show CAN communication
  60. * 3 show CAN speed changes
  61. * 4 show SPI communication
  62. */
  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 '?':
  76. case 'h':
  77. printf("Usage: %s [-hvVdp]\n", argv[0]);
  78. printf("\t-h\t\tPrints this help\n");
  79. printf("\t-v\t\tSets verbose output, can be used multiple times\n");
  80. printf("\t-V\t\tPrints only the version and exits\n");
  81. printf("\t-p\t\tPowers the machine on program start\n");
  82. printf("Listening to the following signals:\n");
  83. printf("\tSIGUSR1(%u)\tPrints monitor data of all nodes\n",
  84. SIGUSR1);
  85. printf("\tSIGHUP(%u)\tRuns a speedtest starting at default speed\n",
  86. SIGHUP);
  87. return EXIT_SUCCESS;
  88. default:
  89. fprintf(stderr, "Usage: %s [-hvVdp]\n", argv[0]);
  90. return EXIT_FAILURE;
  91. break;
  92. }
  93. }
  94. if (signal(SIGINT, terminationHandler)) { // Calls terminationHandler if SIGINT is received
  95. fprintf(stderr, "Unable to set signal handler for SIGINT: %s\n",
  96. strerror(errno));
  97. }
  98. if (signal(SIGUSR1, usr1Handler)) { // Calls terminationHandler if SIGINT is received
  99. fprintf(stderr, "Unable to set signal handler for SIGUSR1: %s\n",
  100. strerror(errno));
  101. }
  102. if (signal(SIGHUP, hupHandler)) { // Calls terminationHandler if SIGINT is received
  103. fprintf(stderr, "Unable to set signal handler for SIGHUP: %s\n",
  104. strerror(errno));
  105. }
  106. // sets up the wiringPi library
  107. if (wiringPiSetup() < 0) {
  108. fprintf(stderr, "Unable to setup wiringPi: %s\n", strerror(errno));
  109. return 1;
  110. }
  111. logger_reset();
  112. initTimers();
  113. halInit();
  114. lcd = lcdInit();
  115. if (lcd < 0) logger_error("Error: unable to init LCD (%d)\n", lcd);
  116. lcdClear(lcd);
  117. lcdHome(lcd);
  118. lcdPrintf(lcd," CoffeePi ", buildno);
  119. // http://www.tutorialspoint.com/cplusplus/cpp_multithreading.htm
  120. // Initialize and set thread joinable
  121. pthread_attr_init(&attr);
  122. pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
  123. rc = pthread_mutexattr_settype(&mutexattr, PTHREAD_MUTEX_RECURSIVE_NP);
  124. if (rc != 0)
  125. throw(L"pthread_mutexattr_settype returns " + rc);
  126. pthread_mutex_init(&mutex, &mutexattr);
  127. rc = pthread_mutexattr_destroy(&mutexattr);
  128. if (rc != 0)
  129. throw(L"pthread_mutexattr_destroy returns " + rc);
  130. for (int i = 0; i < 1; i++) {
  131. if (i == THREAD_MAIN)
  132. rc = pthread_create(&thread[i], NULL, mainLoop, (void *) i);
  133. /*else if (i == THREAD_TEST)
  134. rc = pthread_create(&thread[i], NULL, testMain, (void *) i);
  135. else if (i == THREAD_CAN)
  136. rc = pthread_create(&thread[i], NULL, canThread, (void *) i);*/
  137. if (rc) {
  138. logger_error("Error:unable to create thread, %d\n", rc);
  139. exit(-1);
  140. }
  141. }
  142. // free attribute and wait for the other threads
  143. pthread_attr_destroy(&attr);
  144. // Insert main stuff here.
  145. timeTimer.setDivider(20);
  146. timeTimer.start();
  147. sleep(2);
  148. rc = pthread_join(thread[0], &status);
  149. if (rc) {
  150. logger_error("Error:unable to join, %d\n", rc);
  151. exit(-1);
  152. }
  153. logger(V_BASIC, "Completed thread with status %d\n", rc);
  154. pthread_exit(NULL);
  155. return EXIT_FAILURE;
  156. }
  157. /**
  158. * Handler for program termination caught via signal
  159. */
  160. void terminationHandler(int signum) {
  161. logger(V_NONE, "Caught signal %d, exiting gracefully..\n", signum);
  162. timeTimer.stop();
  163. logger(V_NONE, "Saving my state to the database..\n");
  164. sqlExecute("begin");
  165. // Save stuff here
  166. sqlExecute("end");
  167. sqlClose(); // Closing DB connection
  168. exit(EXIT_SUCCESS);
  169. }
  170. /**
  171. * Handles the signal USR2
  172. */
  173. void usr1Handler(int signum) {
  174. // Do something here?
  175. logger_reset();
  176. }
  177. /**
  178. * Handles the signal HUP and starts a speed test
  179. */
  180. void hupHandler(int signum) {
  181. // Do something here?
  182. }
  183. /**
  184. * Sends a signal to a thread
  185. * @param threadid ID of the thread
  186. * @param sig Signal to send
  187. */
  188. void killThread(int threadid, int sig) {
  189. //logger(V_BASIC, "pthread_kill on thread %d (%d)\n", threadid, sig);
  190. if (pthread_kill(thread[threadid], sig)) {
  191. logger_error("pthread_kill on thread %d failed (%d)\n", threadid, sig);
  192. }
  193. }
  194. /**
  195. * Main Thread, used for some initializations and error detection
  196. * @param threadid Thread ID
  197. */
  198. void *mainLoop(void *threadid) {
  199. sqlOpen();
  200. //sqlSetup();
  201. // Do more stuff here
  202. logger(V_BASIC, "Thread goes Sleeping..\n");
  203. while (1) {
  204. pause();
  205. logger(V_NONE, LOG_ERROR, "Whoops. Something went wrong..\n");
  206. }
  207. pthread_exit(NULL);
  208. }
  209. void timeHandler(void) {
  210. time_t rawtime;
  211. struct tm * timeinfo;
  212. time ( &rawtime );
  213. timeinfo = localtime ( &rawtime );
  214. lcdPosition(lcd, 0, 1);
  215. lcdPrintf(lcd, " %.2d:%.2d:%.2d ", timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec);
  216. }