3 * UCC (University [of WA] Computer Club) Electronic Accounting System
5 * main.c - Initialisation Code
7 * This file is licenced under the 3-clause BSD Licence. See the file
8 * COPYING for full details.
23 #include "../cokebank.h"
26 extern void Init_Handlers(void);
27 extern void Load_Itemlist(void);
28 extern void Server_Start(void);
29 extern int gbServer_RunInBackground;
30 extern int giServer_Port;
31 extern const char *gsItemListFile;
32 extern const char *gsCoke_ModbusAddress;
33 extern const char *gsDoor_SerialPort;
34 extern bool gbSyslogEnabled;
37 void *Periodic_Thread(void *Unused);
42 const char *gsCokebankPath = "cokebank.db";
43 // - Functions called every 20s (or so)
44 #define ciMaxPeriodics 10
45 struct sPeriodicCall {
46 void (*Function)(void);
47 } gaPeriodicCalls[ciMaxPeriodics];
48 pthread_t gTimerThread;
56 void PrintUsage(const char *progname)
58 fprintf(stderr, "Usage: %s\n", progname);
59 fprintf(stderr, " -f,--configfile\n");
60 fprintf(stderr, " Set the config file path (default `dispsrv.conf')\n");
61 fprintf(stderr, " -d Set debug level (0 - 2, default 0)\n");
62 fprintf(stderr, " --[dont-]daemonise\n");
63 fprintf(stderr, " Run (or explicitly don't run) the server disconnected from the terminal\n");
66 int main(int argc, char *argv[])
69 const char *config_file = "dispsrv.conf";
72 for( i = 1; i < argc; i++ )
75 if( arg[0] == '-' && arg[1] != '-')
80 if( i + 1 >= argc ) return -1;
81 config_file = argv[++i];
84 if( i + 1 >= argc ) return -1;
85 Config_AddValue("debug_level", argv[++i]);
86 giDebugLevel = atoi(argv[i]);
90 fprintf(stderr, "Unknown option '-%c'\n", arg[1]);
95 else if( arg[0] == '-' && arg[1] == '-' )
97 if( strcmp(arg, "--configfile") == 0 ) {
98 if( i + 1 >= argc ) return -1;
99 config_file = argv[++i];
101 else if( strcmp(arg, "--daemonise") == 0 ) {
102 Config_AddValue("daemonise", "true");
104 else if( strcmp(arg, "--dont-daemonise") == 0 ) {
105 Config_AddValue("daemonise", "false");
109 fprintf(stderr, "Unknown option '%s'\n", arg);
122 Config_ParseFile( config_file );
124 // Parse config values
125 gbServer_RunInBackground = Config_GetValue_Bool("daemonise", 0);
126 gsCokebankPath = Config_GetValue("cokebank_database", 0);
127 gsDoor_SerialPort = Config_GetValue("door_serial_port", 0);
128 gsCoke_ModbusAddress = Config_GetValue("coke_modbus_address", 0);
129 giServer_Port = Config_GetValue_Int("server_port", 0);
130 gsItemListFile = Config_GetValue("items_file", 0);
132 gbNoCostMode = (Config_GetValue_Bool("test_mode", 0) == 1);
133 gbSyslogEnabled = (Config_GetValue_Bool("disable_syslog", 0) == 0);
135 signal(SIGINT, sigint_handler);
136 signal(SIGTERM, sigint_handler);
138 openlog("odispense2", 0, LOG_LOCAL4);
140 if( Bank_Initialise(gsCokebankPath) )
150 pthread_kill(gTimerThread, SIGKILL);
155 void *Periodic_Thread(void *Unused __attribute__((unused)))
161 sleep(10); // Sleep for a while
162 // printf("Periodic firing\n");
163 for( i = 0; i < ciMaxPeriodics; i ++ )
165 if( gaPeriodicCalls[i].Function )
166 gaPeriodicCalls[i].Function();
172 void StartPeriodicThread(void)
174 pthread_create( &gTimerThread, NULL, Periodic_Thread, NULL );
177 void AddPeriodicFunction(void (*Fcn)(void))
180 for( i = 0; i < ciMaxPeriodics; i ++ )
182 if( gaPeriodicCalls[i].Function ) continue;
183 gaPeriodicCalls[i].Function = Fcn;
187 fprintf(stderr, "Error: No space for %p in periodic list\n", Fcn);
190 int RunRegex(regex_t *regex, const char *string, int nMatches, regmatch_t *matches, const char *errorMessage)
194 ret = regexec(regex, string, nMatches, matches, 0);
195 if( ret == REG_NOMATCH ) {
199 size_t len = regerror(ret, regex, NULL, 0);
201 regerror(ret, regex, errorStr, len);
202 printf("string = '%s'\n", string);
203 fprintf(stderr, "%s\n%s", errorMessage, errorStr);
210 void CompileRegex(regex_t *regex, const char *pattern, int flags)
212 int ret = regcomp(regex, pattern, flags);
214 size_t len = regerror(ret, regex, NULL, 0);
216 regerror(ret, regex, errorStr, len);
217 fprintf(stderr, "Regex compilation failed - %s\n%s\n", errorStr, pattern);
223 int InitSerial(const char *File, int BaudRate)
229 fd = open(File, O_RDWR | O_NOCTTY | O_NONBLOCK);
230 if( fd == -1 ) return -1;
234 case 1200: baud = B1200; break;
235 case 9600: baud = B9600; break;
236 case 115200: baud = B115200; break;
238 fprintf(stderr, "ERROR: Invalid baud rate to InitSerial (%i)\n", BaudRate);
242 info.c_lflag = 0; // Non-Canoical, No Echo
243 info.c_cflag = baud | CS8 | CLOCAL | CREAD; // baud, 8N1
244 info.c_iflag = IGNCR; // Ignore \r
245 info.c_oflag = 0; // ???
246 cfsetspeed(&info, baud);
247 info.c_cc[VTIME] = 0; // No time limit
248 info.c_cc[VMIN] = 1; // Block until 1 char
250 tcflush(fd, TCIFLUSH);
251 tcsetattr(fd, TCSANOW, &info);
258 * \brief Create a formatted heap string
260 char *mkstr(const char *Format, ...)
266 va_start(args, Format);
267 len = vsnprintf(NULL, 0, Format, args);
270 ret = malloc( len + 1 );
271 if(!ret) return NULL;
273 va_start(args, Format);
274 vsprintf(ret, Format, args);