common_defs.h 9.8 KB

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