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

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