- Fatal("Experiment name \"%s\" too long (>%d)", experiment_name, BUFSIZ);
- }
-
- Log(LOGDEBUG, "Sensor %d with DataFile \"%s\"", s->id, filename);
- // Open DataFile
- Data_Open(&(s->data_file), filename);
-
- s->record_data = true; // Don't forget this!
-
- // Create the thread
- pthread_create(&(s->thread), NULL, Sensor_Loop, (void*)(s));
-}
-
-/**
- * Stop a Sensor from recording DataPoints. Blocks until it has stopped.
- * @param s - The Sensor to stop
- */
-void Sensor_Stop(Sensor * s)
-{
- // Stop
- if (s->record_data)
- {
- s->record_data = false;
- pthread_join(s->thread, NULL); // Wait for thread to exit
- Data_Close(&(s->data_file)); // Close DataFile
- s->newest_data.time_stamp = 0;
- s->newest_data.value = 0;
+ case CONTROL_START:
+ {
+ // Set filename
+ char filename[BUFSIZ];
+ const char *experiment_name = (const char*) arg;
+
+ if (snprintf(filename, BUFSIZ, "%s_s%d", experiment_name, s->id) >= BUFSIZ)
+ {
+ Fatal("Experiment name \"%s\" too long (>%d)", experiment_name, BUFSIZ);
+ }
+
+ Log(LOGDEBUG, "Sensor %d with DataFile \"%s\"", s->id, filename);
+ // Open DataFile
+ Data_Open(&(s->data_file), filename);
+ }
+ case CONTROL_RESUME: //Case fallthrough, no break before
+ {
+ int ret;
+ s->activated = true; // Don't forget this!
+
+ // Create the thread
+ ret = pthread_create(&(s->thread), NULL, Sensor_Loop, (void*)(s));
+ if (ret != 0)
+ {
+ Fatal("Failed to create Sensor_Loop for Sensor %d", s->id);
+ }
+
+ Log(LOGDEBUG, "Resuming sensor %d", s->id);
+ }
+ break;
+
+ case CONTROL_EMERGENCY:
+ case CONTROL_PAUSE:
+ s->activated = false;
+ pthread_join(s->thread, NULL);
+ Log(LOGDEBUG, "Paused sensor %d", s->id);
+ break;
+
+ case CONTROL_STOP:
+ if (s->activated) //May have been paused before
+ {
+ s->activated = false;
+ pthread_join(s->thread, NULL);
+ }
+
+ Data_Close(&(s->data_file)); // Close DataFile
+ s->newest_data.time_stamp = 0;
+ s->newest_data.value = 0;
+ Log(LOGDEBUG, "Stopped sensor %d", s->id);
+ break;
+ default:
+ Fatal("Unknown control mode: %d", mode);