main.cpp 7.4 KB

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