main.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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 "led.h"
  19. #include "timer.h"
  20. #include "database.h"
  21. #include "logger.h"
  22. #include "lcd.h"
  23. const int buildno = (1+(int)(
  24. #include "buildno"
  25. ));
  26. int verbose = 0;
  27. int optPoll = 0;
  28. int optRes = 0;
  29. bool optInc = false;
  30. bool optEcho = false;
  31. bool optLom = false;
  32. bool optForce = false;
  33. bool optTerm = false;
  34. bool optAdd = false;
  35. bool optDate = false;
  36. bool optSuppress = 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[3];
  42. pthread_mutex_t mutex;
  43. int main(int argc, char *argv[]) {
  44. pthread_attr_t attr;
  45. pthread_mutexattr_t mutexattr;
  46. void *status;
  47. int rc;
  48. int opt;
  49. int prev_ind;
  50. logger(V_NONE, LOG_INFO, "CoffeePi by Philipp Hinz - "
  51. "Build number %d\n", buildno);
  52. // Argument handling
  53. while (prev_ind = optind, (opt = getopt(argc, argv, "vV:r:teilfhads")) != -1) {
  54. if (argc > 3)
  55. if (optind == prev_ind + 2 && *optarg == '-') {
  56. opt = '?';
  57. --optind;
  58. }
  59. switch (opt) {
  60. case 'v': // be verbose
  61. /* Verbose levels:
  62. * 1 Basic debugging
  63. * 2 show CAN communication
  64. * 3 show CAN speed changes
  65. * 4 show SPI communication
  66. */
  67. verbose++;
  68. break;
  69. case 'r': // set start resistance
  70. optRes = atoi(optarg);
  71. printf("Starting Poti with %d Ohms\n", optRes);
  72. break;
  73. case 'V':
  74. //printf("Build number %d\n", buildno);
  75. return EXIT_SUCCESS;
  76. break;
  77. case 't':
  78. printf(
  79. "I'm counting myself as a terminal node and terminate the CAN bus.\n");
  80. optTerm = true;
  81. break;
  82. case 'e':
  83. printf("Echoing all CAN messages\n");
  84. optEcho = true;
  85. break;
  86. case 'i':
  87. printf("Increasing Wiper step every second.\n");
  88. optInc = true;
  89. break;
  90. case 'l':
  91. printf("Setting MCP2510 to listen only mode on shutdown.\n");
  92. optLom = true;
  93. break;
  94. case 'f':
  95. printf("I'm forcing a speedtest.\n");
  96. optForce = true;
  97. break;
  98. case 'a':
  99. printf(
  100. "I will add new nodes if they don't appear in the nodes file.\n");
  101. optAdd = true;
  102. break;
  103. case 'd':
  104. optDate = true;
  105. break;
  106. case 's':
  107. printf("I will suppress a speedtest due to many RX/TX errors.\n");
  108. optSuppress = true;
  109. break;
  110. case '?':
  111. case 'h':
  112. printf("Usage: %s [-hvVteilfads] [-r ohms]\n", argv[0]);
  113. printf("\t-h\t\tPrints this help\n");
  114. printf("\t-v\t\tSets verbose output, can be used multiple times\n");
  115. printf(
  116. "\t-r ohms\t\tSets the start resistance of the digital potentiometer\n");
  117. printf("\t-V\t\tPrints only the version and exits\n");
  118. printf("\t-t\t\tTerminates the CAN Bus with a connected DiPoti\n");
  119. printf("\t-e\t\tEchoes all incoming CAN messages (Debugging)\n");
  120. printf("\t-i\t\tIncreases the Wiper of the DigiPot every second\n");
  121. printf(
  122. "\t-l\t\tSets the MCP2515 to listen only mode on shutdown\n");
  123. printf("\t-f\t\tForces a speedtest after startup\n");
  124. printf("\t-s\t\tSuppress a speedtest in case of errors\n");
  125. printf("\t-a\t\tAdd new nodes if not known in file nodes\n");
  126. printf("\t-d\t\tPrints a timestamp in front of every message\n");
  127. printf("Listening to the following signals:\n");
  128. printf("\tSIGUSR1(%u)\tPrints monitor data of all nodes\n",
  129. SIGUSR1);
  130. printf("\tSIGHUP(%u)\tRuns a speedtest starting at default speed\n",
  131. SIGHUP);
  132. return EXIT_SUCCESS;
  133. default:
  134. fprintf(stderr, "Usage: %s [-hvVteilfads] [-r ohms]\n", argv[0]);
  135. return EXIT_FAILURE;
  136. break;
  137. }
  138. }
  139. if (signal(SIGINT, terminationHandler)) { // Calls terminationHandler if SIGINT is received
  140. fprintf(stderr, "Unable to set signal handler for SIGINT: %s\n",
  141. strerror(errno));
  142. }
  143. if (signal(SIGUSR1, usr1Handler)) { // Calls terminationHandler if SIGINT is received
  144. fprintf(stderr, "Unable to set signal handler for SIGUSR1: %s\n",
  145. strerror(errno));
  146. }
  147. if (signal(SIGHUP, hupHandler)) { // Calls terminationHandler if SIGINT is received
  148. fprintf(stderr, "Unable to set signal handler for SIGHUP: %s\n",
  149. strerror(errno));
  150. }
  151. // sets up the wiringPi library
  152. if (wiringPiSetup() < 0) {
  153. fprintf(stderr, "Unable to setup wiringPi: %s\n", strerror(errno));
  154. return 1;
  155. }
  156. logger_reset();
  157. initTimers();
  158. //initLed();
  159. // timer statusT = timer(&printStatus);
  160. // statusT.setDivider(40);
  161. // statusT.start();
  162. // statusT.call();
  163. // http://www.tutorialspoint.com/cplusplus/cpp_multithreading.htm
  164. // Initialize and set thread joinable
  165. pthread_attr_init(&attr);
  166. pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
  167. rc = pthread_mutexattr_settype(&mutexattr, PTHREAD_MUTEX_RECURSIVE_NP);
  168. if (rc != 0)
  169. throw(L"pthread_mutexattr_settype returns " + rc);
  170. pthread_mutex_init(&mutex, &mutexattr);
  171. rc = pthread_mutexattr_destroy(&mutexattr);
  172. if (rc != 0)
  173. throw(L"pthread_mutexattr_destroy returns " + rc);
  174. for (int i = 0; i < 1; i++) {
  175. if (i == THREAD_MAIN)
  176. rc = pthread_create(&thread[i], NULL, mainLoop, (void *) i);
  177. /*else if (i == THREAD_TEST)
  178. rc = pthread_create(&thread[i], NULL, testMain, (void *) i);
  179. else if (i == THREAD_CAN)
  180. rc = pthread_create(&thread[i], NULL, canThread, (void *) i);*/
  181. if (rc) {
  182. logger_error("Error:unable to create thread, %d\n", rc);
  183. exit(-1);
  184. }
  185. }
  186. // free attribute and wait for the other threads
  187. pthread_attr_destroy(&attr);
  188. // Insert main stuff here.
  189. int lcd = lcdInit();
  190. if (lcd < 0) logger_error("Error: unable to init LCD (%d)\n", lcd);
  191. lcdClear(lcd);
  192. lcdHome(lcd);
  193. lcdPrintf(lcd,"Wer das liest ist doof.");
  194. sleep(2);
  195. rc = pthread_join(thread[0], &status);
  196. if (rc) {
  197. logger_error("Error:unable to join, %d\n", rc);
  198. exit(-1);
  199. }
  200. logger(V_BASIC, "Completed thread with status %d\n", rc);
  201. pthread_exit(NULL);
  202. return EXIT_FAILURE;
  203. }
  204. /**
  205. * Handler for program termination caught via signal
  206. */
  207. void terminationHandler(int signum) {
  208. logger(V_NONE, "Caught signal %d, exiting gracefully..\n", signum);
  209. logger(V_NONE, "Saving my state to the database..\n");
  210. sqlExecute("begin");
  211. // Save stuff here
  212. sqlExecute("end");
  213. sqlClose(); // Closing DB connection
  214. initLed(); // Turning all LEDs off
  215. exit(EXIT_SUCCESS);
  216. }
  217. /**
  218. * Handles the signal USR2
  219. */
  220. void usr1Handler(int signum) {
  221. // Do something here?
  222. logger_reset();
  223. }
  224. /**
  225. * Handles the signal HUP and starts a speed test
  226. */
  227. void hupHandler(int signum) {
  228. // Do something here?
  229. }
  230. /**
  231. * Sends a signal to a thread
  232. * @param threadid ID of the thread
  233. * @param sig Signal to send
  234. */
  235. void killThread(int threadid, int sig) {
  236. //logger(V_BASIC, "pthread_kill on thread %d (%d)\n", threadid, sig);
  237. if (pthread_kill(thread[threadid], sig)) {
  238. logger_error("pthread_kill on thread %d failed (%d)\n", threadid, sig);
  239. }
  240. }
  241. /**
  242. * Main Thread, used for some initializations and error detection
  243. * @param threadid Thread ID
  244. */
  245. void *mainLoop(void *threadid) {
  246. sqlOpen();
  247. /*sqlInsertNode((uint8_t*)"123 1", true, true);
  248. sqlInsertNode((uint8_t*)"3525 2", false, false);
  249. sqlInsertNode((uint8_t*)"sbw452 3", false, false);
  250. sqlInsertNode((uint8_t*)"s4t4 4", true, true);
  251. sqlInsertNode((uint8_t*)"te456 5", true, false);*/
  252. sqlSetup();
  253. // Do more stuff here
  254. logger(V_BASIC, "Thread goes Sleeping..\n");
  255. while (1) {
  256. pause();
  257. logger(V_NONE, LOG_ERROR, "Whoops. Something went wrong..\n");
  258. }
  259. pthread_exit(NULL);
  260. }