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"
24 #include "../common/config.h"
27 extern void Init_Handlers(void);
28 extern void Load_Itemlist(void);
29 extern void Server_Start(void);
30 extern bool gbServer_RunInBackground;
31 extern int giServer_Port;
32 extern const char *gsItemListFile;
33 extern const char *gsCoke_ModbusAddress;
34 extern int giCoke_ModbusPort;
35 extern const char *gsDoor_SerialPort;
36 extern bool gbSyslogEnabled;
39 void *Periodic_Thread(void *Unused);
43 bool gbNoCostMode = 0;
44 const char *gsCokebankPath = "cokebank.db";
45 // - Functions called every 20s (or so)
46 #define ciMaxPeriodics 10
47 struct sPeriodicCall {
48 void (*Function)(void);
49 } gaPeriodicCalls[ciMaxPeriodics];
50 pthread_t gTimerThread;
58 void PrintUsage(const char *progname)
60 fprintf(stderr, "Usage: %s\n", progname);
61 fprintf(stderr, " -f,--configfile\n");
62 fprintf(stderr, " Set the config file path (default `dispsrv.conf')\n");
63 fprintf(stderr, " -d Set debug level (0 - 2, default 0)\n");
64 fprintf(stderr, " --[dont-]daemonise\n");
65 fprintf(stderr, " Run (or explicitly don't run) the server disconnected from the terminal\n");
68 int main(int argc, char *argv[])
71 const char *config_file = "dispsrv.conf";
74 for( i = 1; i < argc; i++ )
79 // No free arguments please
83 else if( arg[1] != '-' )
85 // Single character arguments
86 // - TODO: Process in a loop
90 if( i + 1 >= argc ) return -1;
91 config_file = argv[++i];
94 if( i + 1 >= argc ) return -1;
95 Config_AddValue("debug_level", argv[++i]);
96 giDebugLevel = atoi(argv[i]);
100 fprintf(stderr, "Unknown option '-%c'\n", arg[1]);
108 if( strcmp(arg, "--configfile") == 0 ) {
109 if( i + 1 >= argc ) return -1;
110 config_file = argv[++i];
112 else if( strcmp(arg, "--daemonise") == 0 ) {
113 Config_AddValue("daemonise", "true");
115 else if( strcmp(arg, "--dont-daemonise") == 0 ) {
116 Config_AddValue("daemonise", "false");
120 fprintf(stderr, "Unknown option '%s'\n", arg);
127 if( Config_ParseFile( config_file ) ) {
128 fprintf(stderr, "NOTICE: Loading of config file '%s' failed, using defaults\n", config_file);
131 // Parse config values
134 #define REQ_CFG(variable, type, name) rv = Config_GetValue_##type(name, &variable) && rv
135 #define OPT_CFG(variable, type, name) Config_GetValue_##type(name, &variable)
136 OPT_CFG(gbServer_RunInBackground, Bool, "daemonise");
137 OPT_CFG(giServer_Port, Int, "server_port");
139 REQ_CFG(gsCokebankPath, Str, "cokebank_database");
140 REQ_CFG(gsItemListFile, Str, "items_file");
142 OPT_CFG(gsDoor_SerialPort, Str, "door_serial_port");
143 REQ_CFG(gsCoke_ModbusAddress, Str, "coke_modbus_address");
144 OPT_CFG(giCoke_ModbusPort, Int, "coke_modbus_port");
146 OPT_CFG(gbNoCostMode, Bool, "test_mode");
147 OPT_CFG(gbSyslogEnabled, Bool, "disable_syslog");
150 fprintf(stderr, "ERROR: Some required configuration items were missing\n");
155 // - Cleanly tear down the server on SIGINT/SIGTERM
156 signal(SIGINT, sigint_handler);
157 signal(SIGTERM, sigint_handler);
158 // - ignore SIGPIPE to prevent a crashing client from bringing the server down too
159 signal(SIGPIPE, SIG_IGN);
161 openlog("odispense2", 0, LOG_LOCAL4);
163 if( Bank_Initialise(gsCokebankPath) )
173 pthread_kill(gTimerThread, SIGKILL);
178 void *Periodic_Thread(void *Unused __attribute__((unused)))
184 sleep(10); // Sleep for a while
185 // printf("Periodic firing\n");
186 for( i = 0; i < ciMaxPeriodics; i ++ )
188 if( gaPeriodicCalls[i].Function )
189 gaPeriodicCalls[i].Function();
195 void StartPeriodicThread(void)
197 pthread_create( &gTimerThread, NULL, Periodic_Thread, NULL );
200 void AddPeriodicFunction(void (*Fcn)(void))
203 for( i = 0; i < ciMaxPeriodics; i ++ )
205 if( gaPeriodicCalls[i].Function ) continue;
206 gaPeriodicCalls[i].Function = Fcn;
210 fprintf(stderr, "Error: No space for %p in periodic list\n", Fcn);
214 int InitSerial(const char *File, int BaudRate)
220 fd = open(File, O_RDWR | O_NOCTTY | O_NONBLOCK);
221 if( fd == -1 ) return -1;
225 case 1200: baud = B1200; break;
226 case 9600: baud = B9600; break;
227 case 115200: baud = B115200; break;
229 fprintf(stderr, "ERROR: Invalid baud rate to InitSerial (%i)\n", BaudRate);
233 info.c_lflag = 0; // Non-Canoical, No Echo
234 info.c_cflag = baud | CS8 | CLOCAL | CREAD; // baud, 8N1
235 info.c_iflag = IGNCR; // Ignore \r
236 info.c_oflag = 0; // ???
237 cfsetspeed(&info, baud);
238 info.c_cc[VTIME] = 0; // No time limit
239 info.c_cc[VMIN] = 1; // Block until 1 char
241 tcflush(fd, TCIFLUSH);
242 tcsetattr(fd, TCSANOW, &info);
249 * \brief Create a formatted heap string
251 char *mkstr(const char *Format, ...)
257 va_start(args, Format);
258 len = vsnprintf(NULL, 0, Format, args);
261 ret = malloc( len + 1 );
262 if(!ret) return NULL;
264 va_start(args, Format);
265 vsprintf(ret, Format, args);