main.cpp 7.3 KB

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