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");
112 fgets(buf, sizeof buf, fp);
114 gsDoor_Password = strdup(buf);;
116 else if( strcmp(arg, "--cokebank") == 0 ) {
117 if( i + 1 >= argc ) return -1;
118 gsCokebankPath = argv[++i];
133 signal(SIGINT, sigint_handler);
135 openlog("odispense2", 0, LOG_LOCAL4);
137 if( Bank_Initialise(gsCokebankPath) )
144 pthread_create( &timer_thread, NULL, Periodic_Thread, NULL );
148 pthread_kill(timer_thread, SIGKILL);
153 void *Periodic_Thread(void *Unused)
156 Unused = NULL; // quiet, gcc
160 sleep(10); // Sleep for a while
161 // printf("Periodic firing\n");
162 for( i = 0; i < ciMaxPeriodics; i ++ )
164 if( gaPeriodicCalls[i].Function )
165 gaPeriodicCalls[i].Function();
171 void AddPeriodicFunction(void (*Fcn)(void))
174 for( i = 0; i < ciMaxPeriodics; i ++ )
176 if( gaPeriodicCalls[i].Function ) continue;
177 gaPeriodicCalls[i].Function = Fcn;
181 fprintf(stderr, "Error: No space for %p in periodic list\n", Fcn);
184 int RunRegex(regex_t *regex, const char *string, int nMatches, regmatch_t *matches, const char *errorMessage)
188 ret = regexec(regex, string, nMatches, matches, 0);
189 if( ret == REG_NOMATCH ) {
193 size_t len = regerror(ret, regex, NULL, 0);
195 regerror(ret, regex, errorStr, len);
196 printf("string = '%s'\n", string);
197 fprintf(stderr, "%s\n%s", errorMessage, errorStr);
204 void CompileRegex(regex_t *regex, const char *pattern, int flags)
206 int ret = regcomp(regex, pattern, flags);
208 size_t len = regerror(ret, regex, NULL, 0);
210 regerror(ret, regex, errorStr, len);
211 fprintf(stderr, "Regex compilation failed - %s\n", errorStr);
217 int InitSerial(const char *File, int BaudRate)
223 fd = open(File, O_RDWR | O_NOCTTY | O_NONBLOCK);
224 if( fd == -1 ) return -1;
228 case 9600: baud = B9600; break;
229 default: close(fd); return -1;
232 info.c_lflag = 0; // Non-Canoical, No Echo
233 info.c_cflag = baud | CS8 | CLOCAL | CREAD; // baud, 8N1
234 cfsetspeed(&info, baud);
235 info.c_cc[VTIME] = 0; // No time limit
236 info.c_cc[VMIN] = 1; // Block until 1 char
238 tcflush(fd, TCIFLUSH);
239 tcsetattr(fd, TCSANOW, &info);
246 * \brief Create a formatted heap string
248 char *mkstr(const char *Format, ...)
254 va_start(args, Format);
255 len = vsnprintf(NULL, 0, Format, args);
258 ret = malloc( len + 1 );
259 if(!ret) return NULL;
261 va_start(args, Format);
262 vsprintf(ret, Format, args);