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 int 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);
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++ )
77 if( arg[0] == '-' && arg[1] != '-')
82 if( i + 1 >= argc ) return -1;
83 config_file = argv[++i];
86 if( i + 1 >= argc ) return -1;
87 Config_AddValue("debug_level", argv[++i]);
88 giDebugLevel = atoi(argv[i]);
92 fprintf(stderr, "Unknown option '-%c'\n", arg[1]);
97 else if( arg[0] == '-' && arg[1] == '-' )
99 if( strcmp(arg, "--configfile") == 0 ) {
100 if( i + 1 >= argc ) return -1;
101 config_file = argv[++i];
103 else if( strcmp(arg, "--daemonise") == 0 ) {
104 Config_AddValue("daemonise", "true");
106 else if( strcmp(arg, "--dont-daemonise") == 0 ) {
107 Config_AddValue("daemonise", "false");
111 fprintf(stderr, "Unknown option '%s'\n", arg);
124 Config_ParseFile( config_file );
126 // Parse config values
127 gbServer_RunInBackground = Config_GetValue_Bool("daemonise", 0);
128 gsCokebankPath = Config_GetValue("cokebank_database", 0);
129 gsDoor_SerialPort = Config_GetValue("door_serial_port", 0);
130 giServer_Port = Config_GetValue_Int("server_port", 0);
131 gsItemListFile = Config_GetValue("items_file", 0);
133 gbNoCostMode = (Config_GetValue_Bool("test_mode", 0) == 1);
134 gbSyslogEnabled = (Config_GetValue_Bool("disable_syslog", 0) == 0);
136 gsCoke_ModbusAddress = Config_GetValue("coke_modbus_address", 0);
137 giCoke_ModbusPort = Config_GetValue_Int("coke_modbus_port", 0);
139 // - Cleanly tear down the server on SIGINT/SIGTERM
140 signal(SIGINT, sigint_handler);
141 signal(SIGTERM, sigint_handler);
142 // - ignore SIGPIPE to prevent a crashing client from bringing the server down too
143 signal(SIGPIPE, SIG_IGN);
145 openlog("odispense2", 0, LOG_LOCAL4);
147 if( Bank_Initialise(gsCokebankPath) )
157 pthread_kill(gTimerThread, SIGKILL);
162 void *Periodic_Thread(void *Unused __attribute__((unused)))
168 sleep(10); // Sleep for a while
169 // printf("Periodic firing\n");
170 for( i = 0; i < ciMaxPeriodics; i ++ )
172 if( gaPeriodicCalls[i].Function )
173 gaPeriodicCalls[i].Function();
179 void StartPeriodicThread(void)
181 pthread_create( &gTimerThread, NULL, Periodic_Thread, NULL );
184 void AddPeriodicFunction(void (*Fcn)(void))
187 for( i = 0; i < ciMaxPeriodics; i ++ )
189 if( gaPeriodicCalls[i].Function ) continue;
190 gaPeriodicCalls[i].Function = Fcn;
194 fprintf(stderr, "Error: No space for %p in periodic list\n", Fcn);
198 int InitSerial(const char *File, int BaudRate)
204 fd = open(File, O_RDWR | O_NOCTTY | O_NONBLOCK);
205 if( fd == -1 ) return -1;
209 case 1200: baud = B1200; break;
210 case 9600: baud = B9600; break;
211 case 115200: baud = B115200; break;
213 fprintf(stderr, "ERROR: Invalid baud rate to InitSerial (%i)\n", BaudRate);
217 info.c_lflag = 0; // Non-Canoical, No Echo
218 info.c_cflag = baud | CS8 | CLOCAL | CREAD; // baud, 8N1
219 info.c_iflag = IGNCR; // Ignore \r
220 info.c_oflag = 0; // ???
221 cfsetspeed(&info, baud);
222 info.c_cc[VTIME] = 0; // No time limit
223 info.c_cc[VMIN] = 1; // Block until 1 char
225 tcflush(fd, TCIFLUSH);
226 tcsetattr(fd, TCSANOW, &info);
233 * \brief Create a formatted heap string
235 char *mkstr(const char *Format, ...)
241 va_start(args, Format);
242 len = vsnprintf(NULL, 0, Format, args);
245 ret = malloc( len + 1 );
246 if(!ret) return NULL;
248 va_start(args, Format);
249 vsprintf(ret, Format, args);