3 * @brief Handles all client control requests (admin related)
10 typedef enum ControlState {
16 typedef struct ControlData {
18 pthread_mutex_t mutex;
19 struct timeval start_time;
22 ControlData g_controls = {STATE_STOPPED, PTHREAD_MUTEX_INITIALIZER, {0}};
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
30 void Control_Handler(FCGIContext *context, char *params) {
31 const char *action, *key = "", *name = "";
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}
41 if (!FCGI_ParseRequest(context, params, values, 4))
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");
53 } else if (!strcmp(action, "pause")) {
54 FCGI_BeginJSON(context, STATUS_OK);
55 FCGI_JSONPair("description", "stop");
57 } else if (!strcmp(action, "end")) {
58 FCGI_BeginJSON(context, STATUS_OK);
59 FCGI_JSONPair("description", "stop");
62 FCGI_RejectJSON(context, "Unknown action specified.");
65 FCGI_RejectJSONEx(context, STATUS_UNAUTHORIZED,
66 "Invalid control key specified.");
70 bool Control_Start(const char *experiment_name) {
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;
81 pthread_mutex_unlock(&(g_controls.mutex));
86 void Control_Pause() {
87 pthread_mutex_lock(&(g_controls.mutex));
88 pthread_mutex_unlock(&(g_controls.mutex));
94 pthread_mutex_lock(&(g_controls.mutex));
95 if (g_controls.state != STATE_STOPPED) {
98 g_controls.state = STATE_STOPPED;
101 pthread_mutex_unlock(&(g_controls.mutex));
105 bool Control_Lock() {
106 pthread_mutex_lock(&(g_controls.mutex));
107 if (g_controls.state == STATE_RUNNING)
109 pthread_mutex_unlock(&(g_controls.mutex));
113 void Control_Unlock() {
114 pthread_mutex_unlock(&(g_controls.mutex));