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 int main(int argc, char *argv[])
55 pthread_t timer_thread;
58 for( i = 1; i < argc; i++ )
61 if( arg[0] == '-' && arg[1] != '-')
66 if( i + 1 >= argc ) return -1;
67 giServer_Port = atoi(argv[++i]);
70 if( i + 1 >= argc ) return -1;
71 giDebugLevel = atoi(argv[++i]);
78 else if( arg[0] == '-' && arg[1] == '-' ) {
79 if( strcmp(arg, "--itemsfile") == 0 ) {
80 if( i + 1 >= argc ) return -1;
81 gsItemListFile = argv[++i];
83 else if( strcmp(arg, "--cokeport") == 0 ) {
84 if( i + 1 >= argc ) return -1;
85 gsCoke_SerialPort = argv[++i];
87 else if( strcmp(arg, "--snackport") == 0 ) {
88 if( i + 1 >= argc ) return -1;
89 gsSnack_SerialPort = argv[++i];
91 else if( strcmp(arg, "--doorpass") == 0 ) {
92 if( i + 1 >= argc ) return -1;
93 gsDoor_Password = argv[++i];
104 signal(SIGINT, sigint_handler);
106 openlog("odispense2", 0, LOG_LOCAL4);
108 if( Bank_Initialise(gsCokebankPath) )
115 pthread_create( &timer_thread, NULL, Periodic_Thread, NULL );
119 pthread_kill(timer_thread, SIGKILL);
124 void *Periodic_Thread(void *Unused)
127 Unused = NULL; // quiet, gcc
131 sleep(10); // Sleep for a while
132 // printf("Periodic firing\n");
133 for( i = 0; i < ciMaxPeriodics; i ++ )
135 if( gaPeriodicCalls[i].Function )
136 gaPeriodicCalls[i].Function();
142 void AddPeriodicFunction(void (*Fcn)(void))
145 for( i = 0; i < ciMaxPeriodics; i ++ )
147 if( gaPeriodicCalls[i].Function ) continue;
148 gaPeriodicCalls[i].Function = Fcn;
152 fprintf(stderr, "Error: No space for %p in periodic list\n", Fcn);
155 int RunRegex(regex_t *regex, const char *string, int nMatches, regmatch_t *matches, const char *errorMessage)
159 ret = regexec(regex, string, nMatches, matches, 0);
160 if( ret == REG_NOMATCH ) {
164 size_t len = regerror(ret, regex, NULL, 0);
166 regerror(ret, regex, errorStr, len);
167 printf("string = '%s'\n", string);
168 fprintf(stderr, "%s\n%s", errorMessage, errorStr);
175 void CompileRegex(regex_t *regex, const char *pattern, int flags)
177 int ret = regcomp(regex, pattern, flags);
179 size_t len = regerror(ret, regex, NULL, 0);
181 regerror(ret, regex, errorStr, len);
182 fprintf(stderr, "Regex compilation failed - %s\n", errorStr);
188 int InitSerial(const char *File, int BaudRate)
194 fd = open(File, O_RDWR | O_NOCTTY | O_NONBLOCK);
195 if( fd == -1 ) return -1;
199 case 9600: baud = B9600; break;
200 default: close(fd); return -1;
203 info.c_lflag = 0; // Non-Canoical, No Echo
204 info.c_cflag = baud | CS8 | CLOCAL | CREAD; // baud, 8N1
205 cfsetspeed(&info, baud);
206 info.c_cc[VTIME] = 0; // No time limit
207 info.c_cc[VMIN] = 1; // Block until 1 char
209 tcflush(fd, TCIFLUSH);
210 tcsetattr(fd, TCSANOW, &info);
217 * \brief Create a formatted heap string
219 char *mkstr(const char *Format, ...)
225 va_start(args, Format);
226 len = vsnprintf(NULL, 0, Format, args);
229 ret = malloc( len + 1 );
230 if(!ret) return NULL;
232 va_start(args, Format);
233 vsprintf(ret, Format, args);