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 const char *gsDoor_SerialPort;
35 extern bool gbSyslogEnabled;
38 void *Periodic_Thread(void *Unused);
43 const char *gsCokebankPath = "cokebank.db";
44 // - Functions called every 20s (or so)
45 #define ciMaxPeriodics 10
46 struct sPeriodicCall {
47 void (*Function)(void);
48 } gaPeriodicCalls[ciMaxPeriodics];
49 pthread_t gTimerThread;
57 void PrintUsage(const char *progname)
59 fprintf(stderr, "Usage: %s\n", progname);
60 fprintf(stderr, " -f,--configfile\n");
61 fprintf(stderr, " Set the config file path (default `dispsrv.conf')\n");
62 fprintf(stderr, " -d Set debug level (0 - 2, default 0)\n");
63 fprintf(stderr, " --[dont-]daemonise\n");
64 fprintf(stderr, " Run (or explicitly don't run) the server disconnected from the terminal\n");
67 int main(int argc, char *argv[])
70 const char *config_file = "dispsrv.conf";
73 for( i = 1; i < argc; i++ )
76 if( arg[0] == '-' && arg[1] != '-')
81 if( i + 1 >= argc ) return -1;
82 config_file = argv[++i];
85 if( i + 1 >= argc ) return -1;
86 Config_AddValue("debug_level", argv[++i]);
87 giDebugLevel = atoi(argv[i]);
91 fprintf(stderr, "Unknown option '-%c'\n", arg[1]);
96 else if( arg[0] == '-' && arg[1] == '-' )
98 if( strcmp(arg, "--configfile") == 0 ) {
99 if( i + 1 >= argc ) return -1;
100 config_file = argv[++i];
102 else if( strcmp(arg, "--daemonise") == 0 ) {
103 Config_AddValue("daemonise", "true");
105 else if( strcmp(arg, "--dont-daemonise") == 0 ) {
106 Config_AddValue("daemonise", "false");
110 fprintf(stderr, "Unknown option '%s'\n", arg);
123 Config_ParseFile( config_file );
125 // Parse config values
126 gbServer_RunInBackground = Config_GetValue_Bool("daemonise", 0);
127 gsCokebankPath = Config_GetValue("cokebank_database", 0);
128 gsDoor_SerialPort = Config_GetValue("door_serial_port", 0);
129 gsCoke_ModbusAddress = Config_GetValue("coke_modbus_address", 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 signal(SIGINT, sigint_handler);
137 signal(SIGTERM, sigint_handler);
139 openlog("odispense2", 0, LOG_LOCAL4);
141 if( Bank_Initialise(gsCokebankPath) )
151 pthread_kill(gTimerThread, SIGKILL);
156 void *Periodic_Thread(void *Unused __attribute__((unused)))
162 sleep(10); // Sleep for a while
163 // printf("Periodic firing\n");
164 for( i = 0; i < ciMaxPeriodics; i ++ )
166 if( gaPeriodicCalls[i].Function )
167 gaPeriodicCalls[i].Function();
173 void StartPeriodicThread(void)
175 pthread_create( &gTimerThread, NULL, Periodic_Thread, NULL );
178 void AddPeriodicFunction(void (*Fcn)(void))
181 for( i = 0; i < ciMaxPeriodics; i ++ )
183 if( gaPeriodicCalls[i].Function ) continue;
184 gaPeriodicCalls[i].Function = Fcn;
188 fprintf(stderr, "Error: No space for %p in periodic list\n", Fcn);
192 int InitSerial(const char *File, int BaudRate)
198 fd = open(File, O_RDWR | O_NOCTTY | O_NONBLOCK);
199 if( fd == -1 ) return -1;
203 case 1200: baud = B1200; break;
204 case 9600: baud = B9600; break;
205 case 115200: baud = B115200; break;
207 fprintf(stderr, "ERROR: Invalid baud rate to InitSerial (%i)\n", BaudRate);
211 info.c_lflag = 0; // Non-Canoical, No Echo
212 info.c_cflag = baud | CS8 | CLOCAL | CREAD; // baud, 8N1
213 info.c_iflag = IGNCR; // Ignore \r
214 info.c_oflag = 0; // ???
215 cfsetspeed(&info, baud);
216 info.c_cc[VTIME] = 0; // No time limit
217 info.c_cc[VMIN] = 1; // Block until 1 char
219 tcflush(fd, TCIFLUSH);
220 tcsetattr(fd, TCSANOW, &info);
227 * \brief Create a formatted heap string
229 char *mkstr(const char *Format, ...)
235 va_start(args, Format);
236 len = vsnprintf(NULL, 0, Format, args);
239 ret = malloc( len + 1 );
240 if(!ret) return NULL;
242 va_start(args, Format);
243 vsprintf(ret, Format, args);