- if (g_controls.current_mode == CONTROL_EMERGENCY && desired_mode != CONTROL_STOP) {
- ret = "In emergency mode. Stop before doing anything else.";
- } else if (g_controls.current_mode == desired_mode) {
- ret = "Already in desired mode.";
- } else if (desired_mode == CONTROL_START) {
- if (g_controls.current_mode == CONTROL_STOP) {
- //TODO Sanitise name (ensure it contains no special chars eg \ / .. .
- FILE *fp = fopen((const char*) arg, "a");
- if (fp) {
- fclose(fp);
- gettimeofday(&(g_controls.start_time), NULL);
- } else {
- ret = "Cannot open experiment name marker";
- }
- } else {
- ret = "Cannot start when not in a stopped state.";
- }
- } else if (desired_mode == CONTROL_RESUME) {
- if (g_controls.current_mode != CONTROL_PAUSE)
- ret = "Cannot resume when not in a paused state.";
+ if (g_controls.current_mode == desired_mode)
+ ret = "Already in the desired mode.";
+ else if (g_controls.current_mode == CONTROL_EMERGENCY && desired_mode != CONTROL_STOP)
+ ret = "In emergency mode. You must stop before continuing.";
+ else switch (desired_mode) {
+ case CONTROL_START:
+ if (g_controls.current_mode == CONTROL_STOP) {
+ const char * path = arg;
+ if (mkdir(path, 0777) != 0 && errno != EEXIST) {
+ Log(LOGERR, "Couldn't create experiment directory %s - %s",
+ path, strerror(errno));
+ ret = "Couldn't create experiment directory.";
+ } else {
+ clock_gettime(CLOCK_MONOTONIC, &(g_controls.start_time));
+ }
+ } else
+ ret = "Cannot start when not in a stopped state.";
+ break;
+ case CONTROL_PAUSE:
+ if (g_controls.current_mode != CONTROL_START)
+ ret = "Cannot pause when not in a running state.";
+ break;
+ case CONTROL_RESUME:
+ if (g_controls.current_mode != CONTROL_PAUSE)
+ ret = "Cannot resume when not in a paused state.";
+ break;
+ case CONTROL_EMERGENCY:
+ if (g_controls.current_mode != CONTROL_START) //pfft
+ ret = "Not running so how can there be an emergency?";
+ break;
+ default:
+ break;