main.cpp 7.8 KB

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