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

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