Partial implementation of higher level control functions
[matches/MCTX3420.git] / server / control.c
1 /**
2  * @file control.c
3  * @brief Handles all client control requests (admin related)
4  */
5 #include "common.h"
6 #include "control.h"
7 #include "sensor.h"
8 #include "actuator.h"
9
10 typedef enum ControlState {
11         STATE_STOPPED,
12         STATE_PAUSED,
13         STATE_RUNNING
14 } ControlState;
15
16 typedef struct ControlData {
17         ControlState state;
18         pthread_mutex_t mutex;
19         struct timeval start_time;
20 } ControlData;
21
22 ControlData g_controls = {STATE_STOPPED, PTHREAD_MUTEX_INITIALIZER, {0}};
23
24 /**
25  * System control handler. This covers high-level control, including
26  * admin related functions and starting/stopping experiments..
27  * @param context The context to work in
28  * @param params The input parameters
29  */
30 void Control_Handler(FCGIContext *context, char *params) {
31         const char *action, *key = "", *name = "";
32         bool force = false;
33
34         FCGIValue values[4] = {
35                 {"action", &action, FCGI_REQUIRED(FCGI_STRING_T)},
36                 {"key", &key, FCGI_STRING_T},
37                 {"force", &force, FCGI_BOOL_T},
38                 {"name", &name, FCGI_STRING_T}
39         };
40
41         if (!FCGI_ParseRequest(context, params, values, 4))
42                 return;
43         
44         if (!strcmp(action, "lock")) {
45                 FCGI_LockControl(context, force);
46         } else if (FCGI_HasControl(context, key)) {
47                 if (!strcmp(action, "release")) {
48                         FCGI_ReleaseControl(context);
49                 } else if (!strcmp(action, "start")) {
50                         FCGI_BeginJSON(context, STATUS_OK);
51                         FCGI_JSONPair("description", "start");
52                         FCGI_EndJSON();
53                 } else if (!strcmp(action, "pause")) {
54                         FCGI_BeginJSON(context, STATUS_OK);
55                         FCGI_JSONPair("description", "stop");
56                         FCGI_EndJSON();
57                 } else if (!strcmp(action, "end")) {
58                         FCGI_BeginJSON(context, STATUS_OK);
59                         FCGI_JSONPair("description", "stop");
60                         FCGI_EndJSON();
61                 } else {
62                         FCGI_RejectJSON(context, "Unknown action specified.");
63                 }
64         } else {
65                 FCGI_RejectJSONEx(context, STATUS_UNAUTHORIZED, 
66                         "Invalid control key specified.");
67         }
68 }
69
70 bool Control_Start(const char *experiment_name) {
71         bool ret = false;
72
73         pthread_mutex_lock(&(g_controls.mutex));
74         if (g_controls.state == STATE_STOPPED) {
75                 gettimeofday(&(g_controls.start_time), NULL);
76                 Sensor_StartAll(experiment_name);
77                 Actuator_StartAll(experiment_name);
78                 g_controls.state = STATE_RUNNING;
79                 ret = true;
80         }
81         pthread_mutex_unlock(&(g_controls.mutex));
82         return ret;
83 }
84
85
86 void Control_Pause() {
87         pthread_mutex_lock(&(g_controls.mutex));
88         pthread_mutex_unlock(&(g_controls.mutex));
89 }
90
91 bool Control_Stop() {
92         bool ret = false;
93
94         pthread_mutex_lock(&(g_controls.mutex));
95         if (g_controls.state != STATE_STOPPED) {
96                 Actuator_StopAll();
97                 Sensor_StopAll();
98                 g_controls.state = STATE_STOPPED;
99                 ret = true;
100         }
101         pthread_mutex_unlock(&(g_controls.mutex));      
102         return ret;
103 }
104
105 bool Control_Lock() {
106         pthread_mutex_lock(&(g_controls.mutex));
107         if (g_controls.state == STATE_RUNNING)
108                 return true;
109         pthread_mutex_unlock(&(g_controls.mutex));
110         return false;
111 }
112
113 void Control_Unlock() {
114         pthread_mutex_unlock(&(g_controls.mutex));
115 }

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