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 signal(SIGINT, sigint_handler);
140 signal(SIGTERM, sigint_handler);
142 openlog("odispense2", 0, LOG_LOCAL4);
144 if( Bank_Initialise(gsCokebankPath) )
154 pthread_kill(gTimerThread, SIGKILL);
159 void *Periodic_Thread(void *Unused __attribute__((unused)))
165 sleep(10); // Sleep for a while
166 // printf("Periodic firing\n");
167 for( i = 0; i < ciMaxPeriodics; i ++ )
169 if( gaPeriodicCalls[i].Function )
170 gaPeriodicCalls[i].Function();
176 void StartPeriodicThread(void)
178 pthread_create( &gTimerThread, NULL, Periodic_Thread, NULL );
181 void AddPeriodicFunction(void (*Fcn)(void))
184 for( i = 0; i < ciMaxPeriodics; i ++ )
186 if( gaPeriodicCalls[i].Function ) continue;
187 gaPeriodicCalls[i].Function = Fcn;
191 fprintf(stderr, "Error: No space for %p in periodic list\n", Fcn);
195 int InitSerial(const char *File, int BaudRate)
201 fd = open(File, O_RDWR | O_NOCTTY | O_NONBLOCK);
202 if( fd == -1 ) return -1;
206 case 1200: baud = B1200; break;
207 case 9600: baud = B9600; break;
208 case 115200: baud = B115200; break;
210 fprintf(stderr, "ERROR: Invalid baud rate to InitSerial (%i)\n", BaudRate);
214 info.c_lflag = 0; // Non-Canoical, No Echo
215 info.c_cflag = baud | CS8 | CLOCAL | CREAD; // baud, 8N1
216 info.c_iflag = IGNCR; // Ignore \r
217 info.c_oflag = 0; // ???
218 cfsetspeed(&info, baud);
219 info.c_cc[VTIME] = 0; // No time limit
220 info.c_cc[VMIN] = 1; // Block until 1 char
222 tcflush(fd, TCIFLUSH);
223 tcsetattr(fd, TCSANOW, &info);
230 * \brief Create a formatted heap string
232 char *mkstr(const char *Format, ...)
238 va_start(args, Format);
239 len = vsnprintf(NULL, 0, Format, args);
242 ret = malloc( len + 1 );
243 if(!ret) return NULL;
245 va_start(args, Format);
246 vsprintf(ret, Format, args);