2ad9dcc694c9354500b1a3feeae439c06475493f
[matches/MCTX3420.git] / server / main.c
1 /**
2  * @file main.c
3  * @brief main and its helper functions, signal handling and cleanup functions
4  */
5
6 // --- Custom headers --- //
7 #include "common.h"
8 #include "options.h"
9 #include "sensor.h"
10 #include "actuator.h"
11 #include "control.h"
12 #include "pin_test.h"
13 #include "bbb_pin_defines.h"
14
15 // --- Standard headers --- //
16 #include <syslog.h> // for system logging
17 #include <signal.h> // for signal handling
18
19 // --- Variable definitions --- //
20 Options g_options; // options passed to program through command line arguments
21
22 // --- Function definitions --- //
23
24 /**
25  * Parse command line arguments, initialise g_options
26  * @param argc - Number of arguments
27  * @param argv - Array of argument strings
28  */
29 void ParseArguments(int argc, char ** argv)
30 {
31         // horrible horrible hacks
32         g_options.argc = argc;
33         g_options.argv = argv;
34
35         g_options.program = argv[0]; // program name
36         g_options.verbosity = LOGDEBUG; // default log level
37         gettimeofday(&(g_options.start_time), NULL); // Start time
38         g_options.adc_device_path = ADC_DEVICE_PATH;
39         Log(LOGDEBUG, "Called as %s with %d arguments.", g_options.program, argc);
40
41         for (int i = 1; i < argc; ++i)
42         {
43                 if (argv[i][0] != '-')
44                         Fatal("Unexpected argv[%d] - %s", i, argv[i]);
45
46                 if (i+1 >= argc || argv[i+1][0] == '-')
47                         Fatal("No argument following switch %s", argv[i]);
48                 
49                 if (strlen(argv[i]) > 2)
50                         Fatal("Human readable switches are not supported.");
51
52                 switch (argv[i][1])
53                 {
54                         case 'a':
55                                 g_options.adc_device_path = argv[i+1];
56                                 Log(LOGINFO, "ADC Device Path: %s", argv[i+1]);
57                                 ++i;
58                                 break;
59                         default:
60                                 Fatal("Unrecognised switch %s", argv[i]);
61                                 break;
62                 }
63         }       
64 }
65
66 /**
67  * Handle a signal
68  * @param signal - The signal number
69  */
70 //TODO: Something that gets massively annoying with threads is that you can't predict which one gets the signal
71 // There are ways to deal with this, but I can't remember them
72 // Probably sufficient to just call Thread_QuitProgram here
73 void SignalHandler(int signal)
74 {
75         // At the moment just always exit.
76         // Call `exit` so that Cleanup will be called to... clean up.
77         Log(LOGWARN, "Got signal %d (%s). Exiting.", signal, strsignal(signal));
78
79         //exit(signal);
80 }
81
82 /**
83  * Cleanup before the program exits
84  */
85 void Cleanup()
86 {
87         Log(LOGDEBUG, "Begin cleanup.");
88         Log(LOGDEBUG, "Finish cleanup.");
89
90 }
91
92 /**
93  * Main entry point; start worker threads, setup signal handling, wait for threads to exit, exit
94  * @param argc - Num args
95  * @param argv - Args
96  * @returns 0 on success, error code on failure
97  * NOTE: NEVER USE exit(3)! Instead call Thread_QuitProgram
98  */
99 int main(int argc, char ** argv)
100 {
101         // Open log before calling ParseArguments (since ParseArguments may call the Log functions)
102         openlog("mctxserv", LOG_PID | LOG_PERROR, LOG_USER);
103         Log(LOGINFO, "Server started");
104
105         ParseArguments(argc, argv);
106
107         //Open the system log
108
109         // signal handler
110         //TODO: Make this work
111         /*
112         int signals[] = {SIGINT, SIGSEGV, SIGTERM};
113         for (int i = 0; i < sizeof(signals)/sizeof(int); ++i)
114         {
115                 signal(signals[i], SignalHandler);
116         }
117         */
118         Sensor_Init();
119         Actuator_Init();
120         Pin_Init();
121         //Sensor_StartAll("test");
122         //Actuator_StartAll("test");
123         const char *ret;
124         if ((ret = Control_SetMode(CONTROL_START, "test")) != NULL)
125                 Fatal("Control_SetMode failed with '%s'", ret);
126
127         // run request thread in the main thread
128         FCGI_RequestLoop(NULL);
129
130         if ((ret = Control_SetMode(CONTROL_STOP, "test")) != NULL)
131                 Fatal("Control_SetMode failed with '%s'", ret);
132         //Sensor_StopAll();
133         //Actuator_StopAll();
134
135         Pin_Close();
136
137         Cleanup();
138         return 0;
139 }
140
141

UCC git Repository :: git.ucc.asn.au