Merge branch 'master' of https://github.com/szmoore/MCTX3420.git
[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
20 // --- Variable definitions --- //
21 Options g_options; // options passed to program through command line arguments
22
23 // --- Function definitions --- //
24
25 /**
26  * Parse command line arguments, initialise g_options
27  * @param argc - Number of arguments
28  * @param argv - Array of argument strings
29  */
30 void ParseArguments(int argc, char ** argv)
31 {
32
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
38
39         g_options.auth_method = AUTH_NONE;  // Don't use authentication
40         g_options.auth_uri = ""; // 
41         g_options.ldap_base_dn = "";
42         
43
44
45         for (int i = 1; i < argc; ++i)
46         {
47                 if (argv[i][0] != '-')
48                         Fatal("Unexpected argv[%d] - %s", i, argv[i]);
49
50                 if (i+1 >= argc || argv[i+1][0] == '-')
51                         Fatal("No argument following switch %s", argv[i]);
52                 
53                 if (strlen(argv[i]) > 2)
54                         Fatal("Human readable switches are not supported.");
55
56                 char * end = NULL;
57                 switch (argv[i][1])
58                 {
59                         // Set program verbosity
60                         case 'v':
61                                 g_options.verbosity = strtol(argv[++i], &end, 10);
62                                 break;
63                         // Enable/Disable pin test
64                         case 'p':
65                                 g_options.enable_pin = !(strtol(argv[++i], &end, 10));
66                                 break;
67                         // LDAP URI
68                         case 'A':
69                                 g_options.auth_uri = argv[++i];
70                                 break;
71                         // LDAP DN
72                         case 'd':
73                                 g_options.ldap_base_dn = argv[++i];
74                                 break;
75                         default:
76                                 Fatal("Unrecognised switch %s", argv[i]);
77                                 break;
78                 }
79
80                 if (end != NULL && *end != '\0')
81                         Fatal("argv[%d] -%c requires an integer (got \"%s\" instead)", i-1, argv[i-1][0], argv[i]);
82         }       
83
84         Log(LOGDEBUG, "Verbosity: %d", g_options.verbosity);
85         Log(LOGDEBUG, "Pin Module Enabled: %d", g_options.enable_pin);
86         Log(LOGDEBUG, "Auth URI: %s", g_options.auth_uri);
87         Log(LOGDEBUG, "LDAP Base DN: %s", g_options.ldap_base_dn);
88
89         if (g_options.auth_uri[0] != '\0')
90         {
91                 //HACK...
92                 if (PathExists(g_options.auth_uri))
93                         g_options.auth_method = AUTH_SHADOW;
94                 else
95                         g_options.auth_method = AUTH_LDAP;
96         }
97         
98 }
99
100 /**
101  * Cleanup before the program exits
102  */
103 void Cleanup()
104 {
105         Log(LOGDEBUG, "Begin cleanup.");
106         Sensor_Cleanup();
107         //Actuator_Cleanup();
108         Log(LOGDEBUG, "Finish cleanup.");
109 }
110
111 /**
112  * Main entry point; start worker threads, setup signal handling, wait for threads to exit, exit
113  * @param argc - Num args
114  * @param argv - Args
115  * @returns 0 on success, error code on failure
116  * NOTE: NEVER USE exit(3)! Instead call Thread_QuitProgram
117  */
118 int main(int argc, char ** argv)
119 {
120         // Open log before calling ParseArguments (since ParseArguments may call the Log functions)
121         openlog("mctxserv", LOG_PID | LOG_PERROR, LOG_USER);
122         Log(LOGINFO, "Server started");
123
124         ParseArguments(argc, argv); // Setup the g_options structure from program arguments
125
126         Sensor_Init();
127         Actuator_Init();
128         Pin_Init();
129         
130         // Try and start things
131         /*
132         const char *ret;
133         if ((ret = Control_SetMode(CONTROL_START, "test")) != NULL)
134                 Fatal("Control_SetMode failed with '%s'", ret);
135         */
136
137         // run request thread in the main thread
138         FCGI_RequestLoop(NULL);
139
140         /*
141         if ((ret = Control_SetMode(CONTROL_STOP, "test")) != NULL)
142                 Fatal("Control_SetMode failed with '%s'", ret);
143         */
144         //Sensor_StopAll();
145         //Actuator_StopAll();
146
147         Pin_Close();
148
149         Cleanup();
150         return 0;
151 }
152
153

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