3 * @brief Implementation of sensor thread
4 * TODO: Finalise implementation
12 /** Array of sensors, initialised by Sensor_Init **/
13 static Sensor g_sensors[NUMSENSORS]; //global to this file
14 const char * g_sensor_names[NUMSENSORS] = {
15 "analog_test0", "analog_test1",
16 "analog_fail0", "digital_test0",
17 "digital_test1", "digital_fail0"
21 * Checks the sensor data for unsafe or unexpected results
22 * @param sensor_id - The ID of the sensor
23 * @param value - The value of the sensor to check
25 void CheckSensor( SensorId sensor_id, double value)
31 if( value > ANALOG_FAIL0_SAFETY || value < ANALOG_FAIL0_MIN_SAFETY)
33 Log(LOGERR, "Sensor analog_fail0 is above or below its safety value of %d or %d\n", ANALOG_FAIL0_SAFETY, ANALOG_FAIL0_MIN_SAFETY);
34 //new function that stops actuators?
36 else if( value > ANALOG_FAIL0_WARN || value < ANALOG_FAIL0_MIN_WARN)
38 Log(LOGWARN, "Sensor analog_test0 is above or below its warning value of %d or %d\n", ANALOG_FAIL0_WARN, ANALOG_FAIL0_MIN_WARN);
44 if( value != 0 && value != 1)
46 Log(LOGERR, "Sensor digital_fail0 is not 0 or 1\n");
52 //So it doesn't complain about the missing cases - in practice we will need all sensors to be checked as above, no need to include a default as we should only pass valid sensor_id's; unless for some reason we have a sensor we don't need to check (but then why would you pass to this function in the first place :P)
58 * Read a data value from a sensor; block until value is read
59 * @param sensor_id - The ID of the sensor
60 * @param d - DataPoint to set
61 * @returns NULL for digital sensors when data is unchanged, otherwise d
63 DataPoint * GetData(SensorId sensor_id, DataPoint * d)
65 // switch based on the sensor_id at the moment for testing;
66 // might be able to just directly access ADC from sensor_id?
67 //TODO: Implement for real sensors
70 //TODO: We should ensure the time is *never* allowed to change on the server if we use gettimeofday
71 // Another way people might think of getting the time is to count CPU cycles with clock()
72 // But this will not work because a) CPU clock speed may change on some devices (RPi?) and b) It counts cycles used by all threads
75 gettimeofday(&t, NULL);
76 d->time_stamp = (t.tv_sec - g_options.start_time.tv_sec) + 1e-6*(t.tv_usec - g_options.start_time.tv_usec);
79 //d->time_stamp.tv_sec -= g_options.start_time.tv_sec;
80 //d->time_stamp.tv_usec -= g_options.start_time.tv_usec;
91 d->value = (double)(rand() % 100) / 100;
94 d->value = (double)(rand() % 6) * -( rand() % 2) / ( rand() % 100 + 1);
95 //Gives a value between -5 and 5
96 CheckSensor(sensor_id, d->value);
98 //TODO: For digital sensors, consider only updating when sensor is actually changed
100 d->value = t.tv_sec % 2;
103 d->value = (t.tv_sec+1)%2;
106 if( rand() % 100 > 98)
108 d->value = rand() % 2;
109 //Gives 0 or 1 or a 2 every 1/100 times
110 CheckSensor(sensor_id, d->value);
113 Fatal("Unknown sensor id: %d", sensor_id);
116 usleep(100000); // simulate delay in sensor polling
123 * @param s - Sensor to destroy
125 void Destroy(Sensor * s)
127 // Maybe move the binary file into long term file storage?
134 * Initialise a sensor
135 * @param s - Sensor to initialise
137 void Init(Sensor * s, int id)
141 s->points_written = 0;
144 #define FILENAMESIZE 3
145 char filename[FILENAMESIZE];
146 if (s->id >= pow(10, FILENAMESIZE))
148 Fatal("Too many sensors! FILENAMESIZE is %d; increase it and recompile.", FILENAMESIZE);
151 pthread_mutex_init(&(s->mutex), NULL);
153 sprintf(filename, "%d", s->id);
154 unlink(filename); //TODO: Move old files somewhere
156 s->file = fopen(filename, "a+b"); // open binary file
157 Log(LOGDEBUG, "Initialised sensor %d; binary file is \"%s\"", id, filename);
162 * Run the main sensor polling loop
163 * @param arg - Cast to Sensor* - Sensor that the thread will handle
164 * @returns NULL (void* required to use the function with pthreads)
166 void * Sensor_Main(void * arg)
168 Sensor * s = (Sensor*)(arg);
170 while (Thread_Runstate() == RUNNING) //TODO: Exit condition
172 // The sensor will write data to a buffer until it is full
173 // Then it will open a file and dump the buffer to the end of it.
176 // The reason I've added the buffer is because locks are expensive
177 // But maybe it's better to just write data straight to the file
178 // I'd like to do some tests by changing SENSOR_DATABUFSIZ
180 while (s->write_index < SENSOR_DATABUFSIZ)
182 DataPoint * d = &(s->buffer[s->write_index]);
183 if (GetData(s->id, d) == NULL)
185 Fatal("Error collecting data");
190 //Log(LOGDEBUG, "Filled buffer");
192 // CRITICAL SECTION (no threads should be able to read/write the file at the same time)
193 pthread_mutex_lock(&(s->mutex));
194 //TODO: Valgrind complains about this fseek: "Syscall param write(buf) points to uninitialised byte(s)"
195 // Not sure why, but we should find out and fix it.
196 fseek(s->file, 0, SEEK_END);
197 int amount_written = fwrite(s->buffer, sizeof(DataPoint), SENSOR_DATABUFSIZ, s->file);
198 if (amount_written != SENSOR_DATABUFSIZ)
200 Fatal("Wrote %d data points and expected to write %d to \"%s\" - %s", amount_written, SENSOR_DATABUFSIZ, strerror(errno));
202 s->points_written += amount_written;
203 //Log(LOGDEBUG, "Wrote %d data points for sensor %d", amount_written, s->id);
204 pthread_mutex_unlock(&(s->mutex));
205 // End of critical section
207 s->write_index = 0; // reset position in buffer
210 Log(LOGDEBUG, "Thread for sensor %d exits", s->id);
215 * Get position in a binary sensor file with a timestamp using a binary search
216 * @param s - Sensor to use
217 * @param time_stamp - Timestamp
218 * @param count - If not NULL, used to provide number of searches required
219 * @param found - If not NULL, set to the closest DataPoint
220 * @returns Integer giving the *closest* index in the file
221 * TODO: Refactor or replace?
223 int FindTime(Sensor * s, double time_stamp, int * count, DataPoint * found)
228 int upper = s->points_written - 1;
233 while (upper - lower > 1)
235 index = lower + ((upper - lower)/2);
237 // Basically anything with fseek is critical; if we don't make it critical the sensor thread may alter data at a random point in the file!
238 // CRITICAL SECTION (May need to rethink how this is done, but I can't see how to do it without fseek :S)
239 // Regarding the suggestion that we have 2 file pointers; one for reading and one for writing:
240 // That seems like it will work... but we will have to be very careful and test it first
241 pthread_mutex_lock(&s->mutex);
242 fseek(s->file, index*sizeof(DataPoint), SEEK_SET);
243 int amount_read = fread(&d, sizeof(DataPoint), 1, s->file);
244 pthread_mutex_unlock(&s->mutex);
246 if (amount_read != 1)
248 Fatal("Couldn't read single data point from sensor %d", s->id);
251 if (d.time_stamp > time_stamp)
255 else if (d.time_stamp < time_stamp)
271 * Print sensor data between two indexes in the file, using a given format
272 * @param s - Sensor to use
273 * @param start - Start index
274 * @param end - End index
275 * @param output_type - JSON, CSV or TSV output format
277 void PrintData(Sensor * s, int start, int end, OutputType output_type)
279 DataPoint buffer[SENSOR_QUERYBUFSIZ];
282 if (output_type == JSON)
290 int to_read = end - index;
291 if (to_read > SENSOR_QUERYBUFSIZ)
293 to_read = SENSOR_QUERYBUFSIZ;
298 pthread_mutex_lock(&(s->mutex));
300 fseek(s->file, index*sizeof(DataPoint), SEEK_SET);
301 amount_read = fread(buffer, sizeof(DataPoint), to_read, s->file);
303 pthread_mutex_unlock(&(s->mutex));
304 // End critical section
306 if (amount_read != to_read)
308 Fatal("Failed to read %d DataPoints from sensor %d; read %d instead", to_read, s->id, amount_read);
312 for (int i = 0; i < amount_read; ++i)
318 FCGI_JSONValue("[%f, %f]", buffer[i].time_stamp, buffer[i].value);
319 if (i+1 < amount_read)
323 FCGI_PrintRaw("%f,%f\n", buffer[i].time_stamp, buffer[i].value);
327 FCGI_PrintRaw("%f\t%f\n", buffer[i].time_stamp, buffer[i].value);
331 index += amount_read;
334 if (output_type == JSON)
341 * Fill buffer with most recent sensor data
342 * TODO: This may be obselete; remove?
343 * @param s - Sensor to use
344 * @param buffer - Buffer to fill
345 * @param bufsiz - Size of buffer to fill
346 * @returns The number of DataPoints actually read
348 int Sensor_Query(Sensor * s, DataPoint * buffer, int bufsiz)
351 //CRITICAL SECTION (Don't access file while sensor thread is writing to it!)
352 pthread_mutex_lock(&(s->mutex));
354 fseek(s->file, -bufsiz*sizeof(DataPoint), SEEK_END);
355 amount_read = fread(buffer, sizeof(DataPoint), bufsiz, s->file);
356 //Log(LOGDEBUG, "Read %d data points", amount_read);
357 pthread_mutex_unlock(&(s->mutex));
362 * Get a Sensor given an ID string
363 * @param id_str ID string
364 * @returns Sensor* identified by the string; NULL on error
366 Sensor * Sensor_Identify(const char * id_str)
369 // Parse string as integer
370 int id = strtol(id_str, &end, 10);
376 if (id < 0 || id >= NUMSENSORS)
380 Log(LOGDEBUG, "Sensor \"%s\" identified", g_sensor_names[id]);
385 * Handle a request to the sensor module
386 * @param context - The context to work in
387 * @param params - Parameters passed
388 * TODO: Seriously need to write more helper functions and decrease the size of this function!
390 void Sensor_Handler(FCGIContext *context, char * params)
392 StatusCodes status = STATUS_OK;
394 OutputType output_type = JSON;
398 const char * key; const char * value;
400 Sensor * sensor = NULL;
403 gettimeofday(&now, NULL);
405 double start_time = -1;
406 double end_time = -1;
407 double current_time = (now.tv_sec - g_options.start_time.tv_sec) + 1e-6*(now.tv_usec - g_options.start_time.tv_usec);
408 bool seek_time = false;
409 bool points_specified = false;
410 int query_size = SENSOR_QUERYBUFSIZ;
411 int start_index = -1;
415 while ((params = FCGI_KeyPair(params, &key, &value)) != NULL)
417 Log(LOGDEBUG, "Got key=%s and value=%s", key, value);
418 if (strcmp(key, "id") == 0)
422 Log(LOGERR, "Only one sensor id should be specified");
423 status = STATUS_ERROR;
428 Log(LOGERR, "No id specified.");
429 status = STATUS_ERROR;
433 sensor = Sensor_Identify(value);
436 Log(LOGERR, "Invalid sensor id: %s", value);
437 status = STATUS_ERROR;
441 else if (strcmp(key, "format") == 0)
443 if (strcmp(value, "json") == 0)
445 else if (strcmp(value, "csv") == 0)
447 else if (strcmp(value, "tsv") == 0)
450 else if (strcmp(key, "points") == 0)
452 points_specified = true;
453 if (strcmp(value, "all") == 0)
455 query_size = sensor->points_written;
460 query_size = strtol(value, &end, 10);
463 Log(LOGERR, "Require \"all\" or an integer value: %s = %s", key, value);
464 status = STATUS_ERROR;
470 else if (strcmp(key, "start_time") == 0)
474 start_time = strtod(value, &end);
477 Log(LOGERR, "Require a double: %s = %s", key, value);
478 status = STATUS_ERROR;
482 // Treat negative values as being relative to the current time
485 start_time = current_time + start_time;
487 start_time = floor(start_time);
489 else if (strcmp(key, "end_time") == 0)
493 end_time = strtod(value, &end);
496 Log(LOGERR, "Require a double: %s = %s", key, value);
497 status = STATUS_ERROR;
501 // Treat negative values as being relative to the current time
504 end_time = current_time + end_time;
506 end_time = ceil(end_time);
508 // For backward compatability:
509 else if (strcmp(key, "dump") == 0)
512 query_size = sensor->points_written+1;
516 Log(LOGERR, "Unknown key \"%s\" (value = %s)", key, value);
517 status = STATUS_ERROR;
522 if (status != STATUS_ERROR && sensor == NULL)
524 Log(LOGERR, "No valid sensor id given");
525 status = STATUS_ERROR;
528 if (status == STATUS_ERROR)
530 FCGI_RejectJSON(context, "Invalid input parameters");
537 if (end_time < 0 && !points_specified)
538 end_index = sensor->points_written;
541 int count = 0; DataPoint d;
542 end_index = FindTime(sensor, end_time, &count, &d);
543 Log(LOGDEBUG, "FindTime - Looked for %f; found [%f,%f] after %d iterations; sensor %d, position %d", end_time, d.time_stamp, d.value, count, sensor->id, end_index);
549 int count = 0; DataPoint d;
550 start_index = FindTime(sensor, start_time, &count, &d);
551 Log(LOGDEBUG, "FindTime - Looked for %f; found [%f,%f] after %d iterations; sensor %d, position %d", start_time, d.time_stamp, d.value, count, sensor->id, start_index);
554 if (points_specified)
555 end_index = start_index + query_size;
559 start_index = sensor->points_written - query_size;
561 end_index = sensor->points_written;
566 Log(LOGNOTE, "start_index = %d => Clamped to 0", start_index);
569 if (end_index > sensor->points_written)
571 Log(LOGNOTE, "end_index = %d => Clamped to %d", end_index, sensor->points_written);
572 end_index = sensor->points_written;
578 FCGI_BeginJSON(context, status);
579 FCGI_JSONLong("id", sensor->id);
580 FCGI_JSONKey("data");
581 PrintData(sensor, start_index, end_index, output_type);
585 FCGI_PrintRaw("Content-type: text/plain\r\n\r\n");
586 PrintData(sensor, start_index, end_index, output_type);
587 //Force download with content-disposition
588 // Sam: This is cool, but I don't think we should do it
589 // - letting the user view it in the browser and then save with their own filename is more flexible
590 //"Content-disposition: attachment;filename=%d.csv\r\n\r\n", sensor->id);
597 * Setup Sensors, start Sensor polling thread(s)
601 // start sensor threads
602 for (int i = 0; i < NUMSENSORS; ++i)
604 Init(g_sensors+i, i);
605 pthread_create(&(g_sensors[i].thread), NULL, Sensor_Main, (void*)(g_sensors+i));
614 if (!Thread_Runstate())
616 Fatal("This function should not be called before Thread_QuitProgram");
618 for (int i = 0; i < NUMSENSORS; ++i)
620 pthread_join(g_sensors[i].thread, NULL);
621 Destroy(g_sensors+i);