client_utils.h 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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. #ifndef _CLIENT_UTILS_H
  21. #define _CLIENT_UTILS_H
  22. //-------------------------------------- Utility structures to hold the system status data and be updated by the default system callbacks
  23. extern gps_status gps_stat;
  24. extern stop_status stop_stat;
  25. extern driver_status driver_stat;
  26. extern pass_status pass_stat;
  27. extern bill_status bill_stat;
  28. extern state_info_t state_info;
  29. //---------------------------------------------------
  30. //These functions return the effective latitude and longitude
  31. //If there is a good GPS fix, we use this, otherwise we use the
  32. //coordinates of the last stop we were at (even if the driver manually advanced there)
  33. float effective_lat();
  34. float effective_lon();
  35. //---------------------------------------------------
  36. //This enum specifies the values that can be returned by a message callback
  37. typedef enum
  38. {
  39. MESSAGE_UNHANDLED = -1, //This message was unhandled (this is actually only returned by the main message processing function).
  40. MESSAGE_HANDLED_CONT = 0, //The message was handled, and we should attempt no further processing on this message
  41. MESSAGE_HANDLED_STOP, //The message was handled, and we should see if it matches any other callbacks
  42. } message_callback_return;
  43. //These macros are default ident values for some common message handling semantics
  44. #define CALLBACK_PREPROCESS (-1) //the ident value CALLBACK_PREPROCESS is essentially only for things to be marked ignore
  45. #define CALLBACK_SYSTEM (0) //the ident value CALLBACK_SYSTEM is for the default status message handlers which update the global structures
  46. //This macro will generate an ident value for a user-defined behavior
  47. #define CALLBACK_USER(x) (1000 + (x))
  48. //This defines a function pointer type for message callback functions and allows the message handling structures and functions
  49. //to have sane-looking prototypes...
  50. typedef message_callback_return (*message_notify_func)(struct message_record *, void *);
  51. //A function that fits this pointer type will be defined like this:
  52. //
  53. // message_callback_return foo(struct message_record *msg, void *param)
  54. // {
  55. // return MESSAGE_HANDLED_CONT;
  56. // }
  57. //--------------------------------------------------------------------------------------------------------------------------
  58. // CALLBACK HANDLING FUNCTIONS
  59. //--------------------------------------------------------------------------------------------------------------------------
  60. //This function subscribes a client to all of the mailboxes with system callbacks
  61. int subscribe_to_default_messages(int fd);
  62. //----------------------------------------------- UTILITY CALLBACKS
  63. message_callback_return ignore_message(struct message_record *msg, void *param);
  64. //----------------------------------------------- CALLBACK REGISTRATION AND RELATED FUNCTIONS
  65. //This function registers a callback to happen when a certain message is received.
  66. //It takes four parameters:
  67. // mailbox = string containing name of mailbox to trigger this callback
  68. // ident = a numeric identifier specifying both a unique identifier for this callback, and processing order
  69. // func = a message handling callback function
  70. // param = an untyped pointer used to pass any outside contect to the callback
  71. //
  72. int register_dispatch_callback(char *mailbox, int ident, message_notify_func func, void *param);
  73. //This function unregisters a message callback function by its mailbox name and identifier
  74. //this allows for easy selective removal of a specific callback while still keeping the rest of the chain intact
  75. int unregister_dispatch_callback(char *mailbox, int ident);
  76. //This function unregisters ALL callbacks in preparation for a clean exit
  77. int unregister_all_callbacks();
  78. //This function registers the default system status callbacks
  79. int register_system_status_callbacks();
  80. //This function unregisters the default system status callbacks
  81. int unregister_system_status_callbacks();
  82. //----------------------------------------------- CALLBACK PROCESSING FUNCTIONS
  83. //This function actually processes a message through the dispatch list
  84. //using the return values of the callbacks to determine whether to stop or continue
  85. //the return value is the return value of the last handler to have read the message.
  86. //a return value of MESSAGE_UNHANDLED means that no handler successfully handled the message.
  87. message_callback_return process_message(struct message_record *msg);
  88. //--------------------------------------------------------------------------------------------------------------------------
  89. // MESSAGE FORMATTING FUNCTIONS FOR SOME COMMON MESSAGES
  90. //--------------------------------------------------------------------------------------------------------------------------
  91. //This function is a printf-line interface to format user display messages:
  92. //
  93. // target = blank IPC message to fill with this log entry
  94. // line = which line of the display to draw on (0 or 1)
  95. // priority = what message priority?
  96. // duration = how many seconds to display this (0 makes it the default message)
  97. // fmt = printf format
  98. // ... = printf args
  99. int format_piu_message(struct message_record *target, int line, int priority, int duration, const char *fmt, ...);
  100. #define LOGLEVEL_DEBUG ('#')
  101. #define LOGLEVEL_WARN ('*')
  102. #define LOGLEVEL_ERROR ('!')
  103. #define LOGLEVEL_ACCEPT ('>')
  104. #define LOGLEVEL_REJECT ('<')
  105. #define LOGLEVEL_EVENT ('|')
  106. //This function takes a buffer and a buffer size and puts a
  107. //log prefix in that contains the equipment number and the local time
  108. int make_log_prefix(char *prefix, int max);
  109. //This function is a printf-line interface to format log messages for the server: (note, it already applies the above prefix, so you don't have to)
  110. //
  111. // target = blank IPC message to fill with this log entry
  112. // loglevel = one of the following: LOGLEVEL_DEBUG, LOGLEVEL_WARN, LOGLEVEL_ERROR
  113. // fmt = printf format
  114. // ... = printf args
  115. //
  116. // NOTE: DO NOT UNDER ANY CIRCUMSTANCES TERMINATE A LOG MESSAGE WITH A '\n' CHARACTER!
  117. //
  118. int format_log_message(struct message_record *target, char loglevel, const char *fmt, ...);
  119. //This function is a printf-line interface to format messages for the driver:
  120. //
  121. // target = blank IPC message to fill with this log entry
  122. // loglevel = one of the following: LOGLEVEL_DEBUG, LOGLEVEL_WARN, LOGLEVEL_ERROR
  123. // fmt = printf format
  124. // ... = printf args
  125. //
  126. // NOTE: DO NOT UNDER ANY CIRCUMSTANCES TERMINATE A LOG MESSAGE WITH A '\n' CHARACTER!
  127. //
  128. int format_driver_message(struct message_record *target, char loglevel, const char *fmt, ...);
  129. //This function printf-line interface to format messages for debug and trace purposes
  130. //
  131. int format_trace_message(struct message_record *target, const char *fmt, ...);
  132. #define BILLING_ACTION_LEN (16)
  133. #define BILLING_RULE_LEN (24)
  134. #define BILLING_RULEPARAM_LEN (24)
  135. #define BILLING_REASON_LEN (32)
  136. #define BILLING_CREDENTIAL_LEN (32)
  137. //This function formats a billing log entry into a form ready to pass off to the billdb process:
  138. //
  139. // target = blank IPC message to fill with this log entry
  140. // action = string specifying billing action (ACCEPT, REJECT, PASSBACK, etc...)
  141. // rule = string specifying which rule (if any) generated this billing entry
  142. // ruleparam = string specifying the parameter passed to the above rule
  143. // reason = human readable reason for the reject or accept of this rider
  144. // credential = string containing the magnetic or RFID credential used to identify the rider
  145. // rider_id = rider ID from pass table
  146. // cash_value = number of cents paid for this fare if it is a cash fare
  147. //
  148. // All other required values are supplied by the system status structures maintained by the default status callbacks:
  149. //
  150. // equipment number, GPS locaction, driver, paddle, route, trip, stop, and stop name
  151. //
  152. int format_billing_message(struct message_record *target, char *action, char *rule, char *ruleparam, char *reason, char *credential, unsigned long long rider_id, int cash_value);
  153. int update_state_info_with_gps(state_info_t *, gps_status *);
  154. int update_state_info_with_stop(state_info_t *, stop_status *);
  155. int update_state_info_with_driver(state_info_t *, driver_status *);
  156. int init_state_info(void);
  157. #endif