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.
22 #include "../cokebank.h"
25 extern void Init_Handlers(void);
26 extern void Load_Itemlist(void);
27 extern void Server_Start(void);
28 extern int giServer_Port;
29 extern char *gsItemListFile;
30 extern char *gsCoke_SerialPort;
31 extern char *gsSnack_SerialPort;
32 extern char *gsDoor_Password;
35 void *Periodic_Thread(void *Unused);
39 char *gsCokebankPath = "cokebank.db";
40 // - Functions called every 20s (or so)
41 #define ciMaxPeriodics 10
42 struct sPeriodicCall {
43 void (*Function)(void);
44 } gaPeriodicCalls[ciMaxPeriodics];
52 void PrintUsage(const char *progname)
54 fprintf(stderr, "Usage: %s\n", progname);
55 fprintf(stderr, " -p Set server port (default 11020)\n");
56 fprintf(stderr, " -d Set debug level (0 - 2, default 0)\n");
57 fprintf(stderr, " --itemsfile\n");
58 fprintf(stderr, " Set debug level (0 - 2, default 0)\n");
59 fprintf(stderr, " --cokeport\n");
60 fprintf(stderr, " Coke machine serial port (Default \"/dev/ttyS0\")\n");
61 fprintf(stderr, " --doorpass\n");
62 fprintf(stderr, " Door LAT password file (Default empty password)\n");
63 fprintf(stderr, " --cokebank\n");
64 fprintf(stderr, " Coke bank database file (Default \"cokebank.db\")\n");
67 int main(int argc, char *argv[])
70 pthread_t timer_thread;
73 for( i = 1; i < argc; i++ )
76 if( arg[0] == '-' && arg[1] != '-')
81 if( i + 1 >= argc ) return -1;
82 giServer_Port = atoi(argv[++i]);
85 if( i + 1 >= argc ) return -1;
86 giDebugLevel = atoi(argv[++i]);
94 else if( arg[0] == '-' && arg[1] == '-' ) {
95 if( strcmp(arg, "--itemsfile") == 0 ) {
96 if( i + 1 >= argc ) return -1;
97 gsItemListFile = argv[++i];
99 else if( strcmp(arg, "--cokeport") == 0 ) {
100 if( i + 1 >= argc ) return -1;
101 gsCoke_SerialPort = argv[++i];
103 else if( strcmp(arg, "--snackport") == 0 ) {
104 if( i + 1 >= argc ) return -1;
105 gsSnack_SerialPort = argv[++i];
107 else if( strcmp(arg, "--doorpass") == 0 ) {
110 if( i + 1 >= argc ) return -1;
111 fp = fopen(argv[++i], "r");
113 fprintf(stderr, "ERROR: Unable to read password file\n");
114 perror("reading LAT password");
117 fgets(buf, sizeof buf, fp);
119 gsDoor_Password = strdup(buf);;
121 else if( strcmp(arg, "--cokebank") == 0 ) {
122 if( i + 1 >= argc ) return -1;
123 gsCokebankPath = argv[++i];
138 signal(SIGINT, sigint_handler);
139 signal(SIGTERM, sigint_handler);
141 openlog("odispense2", 0, LOG_LOCAL4);
143 if( Bank_Initialise(gsCokebankPath) )
150 pthread_create( &timer_thread, NULL, Periodic_Thread, NULL );
154 pthread_kill(timer_thread, SIGKILL);
159 void *Periodic_Thread(void *Unused)
162 Unused = NULL; // quiet, gcc
166 sleep(10); // Sleep for a while
167 // printf("Periodic firing\n");
168 for( i = 0; i < ciMaxPeriodics; i ++ )
170 if( gaPeriodicCalls[i].Function )
171 gaPeriodicCalls[i].Function();
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", errorStr);
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 9600: baud = B9600; break;
235 default: close(fd); return -1;
238 info.c_lflag = 0; // Non-Canoical, No Echo
239 info.c_cflag = baud | CS8 | CLOCAL | CREAD; // baud, 8N1
240 cfsetspeed(&info, baud);
241 info.c_cc[VTIME] = 0; // No time limit
242 info.c_cc[VMIN] = 1; // Block until 1 char
244 tcflush(fd, TCIFLUSH);
245 tcsetattr(fd, TCSANOW, &info);
252 * \brief Create a formatted heap string
254 char *mkstr(const char *Format, ...)
260 va_start(args, Format);
261 len = vsnprintf(NULL, 0, Format, args);
264 ret = malloc( len + 1 );
265 if(!ret) return NULL;
267 va_start(args, Format);
268 vsprintf(ret, Format, args);