common_defs.h 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. /*
  2. * Copyright (c) 2019 Clementine Computing LLC.
  3. *
  4. * This file is part of PopuFare.
  5. *
  6. * PopuFare is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU Affero General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * PopuFare is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU Affero General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Affero General Public License
  17. * along with PopuFare. If not, see <https://www.gnu.org/licenses/>.
  18. *
  19. */
  20. #include "common_config.h"
  21. #ifndef _COMMON_DEFS
  22. #define _COMMON_DEFS
  23. //------------------------------- PROCESS DEATH MANAGEMENT ----------------------
  24. #ifdef USE_WATCHDOG_ALARM
  25. // #define WATCHDOG_DEBUG
  26. #ifdef WATCHDOG_DEBUG
  27. #define WD_DEBUG_MSG(cmd, x) printf("WD %s (%d seconds left)\n", (cmd), (x))
  28. #else
  29. #define WD_DEBUG_MSG(cmd, x) (x)
  30. #endif
  31. #endif
  32. //------------------- Watchdog Timer Related
  33. #ifdef USE_WATCHDOG_ALARM
  34. #define RESET_WATCHDOG() {WD_DEBUG_MSG("reset", alarm(MODULE_WATCHDOG_VALUE));}
  35. #else
  36. #define RESET_WATCHDOG() {/*nop*/}
  37. #endif
  38. //-------------------
  39. #ifdef USE_WATCHDOG_ALARM
  40. #define RESET_WATCHDOG_LONG(x) {WD_DEBUG_MSG("long_reset", alarm(MODULE_WATCHDOG_VALUE * (x)));}
  41. #else
  42. #define RESET_WATCHDOG_LONG(x) {/*nop*/}
  43. #endif
  44. //---------
  45. #ifdef USE_WATCHDOG_ALARM
  46. #define STOP_WATCHDOG() {WD_DEBUG_MSG("stopped",alarm(0));}
  47. #else
  48. #define STOP_WATCHDOG() {/*nop*/}
  49. #endif
  50. //----------
  51. #define EXIT_REQUEST_NONE (0) //no exit is requested
  52. #define EXIT_REQUEST_POLITE (1) //we have an IPC message asking us to wind down
  53. #define EXIT_REQUEST_INT_TERM (2) //we caught a SIGINT or SIGTERM
  54. #define EXIT_REQUEST_CRASH (4) //the watchdog timer has overflowed, or a SEGV has occurred
  55. //This variable is populated by the signal handlers, and should be checked in main while loops.
  56. extern volatile int exit_request_status;
  57. //This flag says there is a pending HUP request
  58. extern volatile int hup_request_status;
  59. //This function requests a config reload / HUP request, debug-logging its arguments printf-style
  60. void request_hup(char *fmt, ...);
  61. //This function requests a polite exit (OR-ing its reason into exit_status_request).
  62. //It also takes printf-style arguments to log the reason for the exit if it has to force
  63. //exit.
  64. void request_polite_exit(int reason, char *fmt, ...);
  65. //This function sets up signal handlers to log any occurances of the serious "crash" type errors
  66. //like a watchdog expiration, or the mean four (SIGSEGV, SIGILL, SIGFPE, SIGBUS).
  67. //
  68. //It also sets up signal handlers for SIGINT and SIGTERM to politely request an exit
  69. //
  70. // You should always call this function from main() with argv[0] as its argument so
  71. //that it can register with syslog so any future messages will come with the correct
  72. //module name attached.
  73. //
  74. void configure_signal_handlers(char *procname);
  75. //------------------- database operation returns
  76. #define OKAY_MIN (0)
  77. #define FAIL_PARAM (-1)
  78. #define FAIL_DATABASE (-2)
  79. #define FAIL_DUPKEY (-3)
  80. #define FAIL_FULL (-4)
  81. #define FAIL_MEM (-5)
  82. #define FAIL_MAX (-1)
  83. #define FAIL_MIN (-99)
  84. #define WARN_NOTFOUND (-100)
  85. #define WARN_NOCHANGE (-101)
  86. #define WARN_MAX (-100)
  87. #define WARN_MIN (-199)
  88. #define DB_OKAY(x) ( (x) >= 0 )
  89. #define DB_FAIL(x) ( ((x) <= FAIL_MAX) && ((x) > FAIL_MIN) )
  90. #define DB_WARN(x) ( ((x) <= WARN_MAX) && ((x) > WARN_MIN) )
  91. //----------------- common utility functions
  92. //Extracts the first tab delimited field from src and puts it in dest,
  93. //returning the number of characters consumed and optionally filling
  94. //*eol_flag with the end-of-line state of the returned field.
  95. int get_field(char *dest, char *src, int dest_len, int *eol_flag);
  96. //Returns an unsigned long long hash value for a string.
  97. unsigned long long stringhash(char *string);
  98. //This function takes a string and removes any CR or LF characters from it
  99. //(deleting them out of the middle if needed). Note, it DOES mangle its input.
  100. int strip_crlf(char *buffer);
  101. //This function opens the TTY device named by devname at the speed
  102. //specified by custon_baud (see sys/termios.h for values). Passing 0
  103. //to custom_baud uses the system default of 115200bps.
  104. //The linemode flag determines whether the port will be treated as a raw
  105. //serial strem (linemode = 0) or a line-buffered terminal (linemode = 1).
  106. //
  107. // When linemode = 1 read() will return a whole line at a time, and
  108. // poll() and select() will not unblock on input from this device until
  109. // either A) a whole line is available, or B) it's been ACCUM_SECONDS
  110. // seconds since the last character has been received.
  111. //
  112. int open_rs232_device(char *devname, int custom_baud, int linemode);
  113. //Defines for the above
  114. #define USE_DEFAULT_BAUD (0)
  115. #define RS232_RAW (0)
  116. #define RS232_LINE (1)
  117. typedef struct device_test_vector_struct
  118. {
  119. char *dev_id; //Expected Device ID returned in the "/?: ?=device_id" message from device
  120. char *init_string; //Initialization String to send to device (Example "/0:Foo Bar\n/m:09 00 03 42 5A 44 56\r")
  121. int n_reply_lines; //Number of expected lines coming back from device...
  122. char **reply_strings; //An optional list of n_reply_lines where NULLs mean "don't care" and non-null
  123. //means reply line N much match supplied string (sans pre '/' garbage and
  124. //trailing '\r' and '\n' characters. Example:
  125. // {NULL, "/OK:m:09 00 03 42 5A 44 56", "/M:^"} means line 0 == Don't care, lines 1 and 2 must match their spec
  126. int init_tries; //Number of times to try getting device ID (0 == use default)
  127. int init_timeout; //Number of milliseconds to wait for device ID reply. (0 == use default)
  128. } device_test_vector;
  129. /*
  130. device_test_vector sample_dev_test_vector =
  131. {
  132. .dev_id = "rider_ui",
  133. .init_string = "/0:\r/1:\r/m:09 00 03 42 5A 44 56\r",
  134. .n_reply_lines = 4,
  135. .reply_strings = (char *[]){NULL, NULL, "/OK:m:09 00 03 42 5A 44 56", "/M:^"},
  136. };
  137. */
  138. #define DEV_INIT_BUFFER_SIZE (256)
  139. #define DIAG_BUFFER_SIZE (DEV_INIT_BUFFER_SIZE * 4)
  140. //This function takes a file descriptor, a pointer to a test vector structure,
  141. //and an optional diagnostic output string (this string must be at least DIAG_BUFFER_SIZE
  142. //characters in size).
  143. //
  144. // The routine first sends a "\r" to the device to prompt it to send a device ID line.
  145. // This is retried until either the maximum number of retries is reached or a reply is received.
  146. //
  147. // The device ID is then checked again the expected ID from the test vector. If it is incorrect,
  148. // failure is returned. If it is correct, the device is then flushed and the initialization specified
  149. // in the test vector is performed.
  150. //
  151. // If this initialization succeeds, the call returns 0, wheras a negative return value is returned in the
  152. // case of failure.
  153. //
  154. int test_and_init_device(int fd, device_test_vector *vec, char *diag);
  155. #define PKG_STRING_SIZE (64)
  156. #define PKG_STRING_FORMAT "%63s" //for use in sscanf format string. Eeew...
  157. typedef struct package_signature_struct
  158. {
  159. char pkgname[PKG_STRING_SIZE];
  160. char pkgver[PKG_STRING_SIZE];
  161. char checksum[PKG_STRING_SIZE];
  162. time_t installed;
  163. } package_signature;
  164. // This function fills up to n package_signature structures with the name, version, md5 checksum, and install time of
  165. //installed packages as gleaned from the package management dropfiles in the configuration directory. This is useful for
  166. //status screens and diagnostic purposes.
  167. int find_packages(package_signature *pkgs, int n);
  168. //This function checks the dropfile to see if the tunnel is up...
  169. int tunnel_is_up();
  170. //This function checks the dropfile to see if the GPRS modem is up...
  171. int gprs_is_up();
  172. //This function gets the equipment number from the appropriate config file
  173. //If it cannot be gotten, it returns -1
  174. int get_equip_num();
  175. //This function sets the equipment number in the correct configuration file
  176. int set_equip_num(int num);
  177. #define SERVER_DESC_LEN (64)
  178. #define MAX_SERVER_LIST (16)
  179. //This ges the current server description into the supplied buffer up to N characters
  180. int get_server_desc(char *desc, int len);
  181. #endif