client_utils.h 8.6 KB

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