+ Sensor * s = (Sensor*)(arg);
+
+ while (Thread_Runstate() == RUNNING) //TODO: Exit condition
+ {
+ // The sensor will write data to a buffer until it is full
+ // Then it will open a file and dump the buffer to the end of it.
+ // Rinse and repeat
+
+ // The reason I've added the buffer is because locks are expensive
+ // But maybe it's better to just write data straight to the file
+ // I'd like to do some tests by changing SENSOR_DATABUFSIZ
+
+ while (s->write_index < SENSOR_DATABUFSIZ)
+ {
+ DataPoint * d = &(s->buffer[s->write_index]);
+ if (GetData(s->id, d) == NULL)
+ {
+ Fatal("Error collecting data");
+ }
+ s->write_index += 1;
+ }
+
+ //Log(LOGDEBUG, "Filled buffer");
+
+ // CRITICAL SECTION (no threads should be able to read/write the file at the same time)
+ pthread_mutex_lock(&(s->mutex));
+ //TODO: Valgrind complains about this fseek: "Syscall param write(buf) points to uninitialised byte(s)"
+ // Not sure why, but we should find out and fix it.
+ fseek(s->file, 0, SEEK_END);
+ int amount_written = fwrite(s->buffer, sizeof(DataPoint), SENSOR_DATABUFSIZ, s->file);
+ if (amount_written != SENSOR_DATABUFSIZ)
+ {
+ Fatal("Wrote %d data points and expected to write %d to \"%s\" - %s", amount_written, SENSOR_DATABUFSIZ, strerror(errno));
+ }
+ //Log(LOGDEBUG, "Wrote %d data points for sensor %d", amount_written, s->id);
+ pthread_mutex_unlock(&(s->mutex));
+ // End of critical section
+
+ s->write_index = 0; // reset position in buffer
+
+ }
+ Log(LOGDEBUG, "Thread for sensor %d exits", s->id);
+ return NULL;
+}
+
+/**
+ * Fill buffer with most recent sensor data
+ * @param s - Sensor to use
+ * @param buffer - Buffer to fill
+ * @param bufsiz - Size of buffer to fill
+ * @returns The number of DataPoints actually read
+ */
+int Sensor_Query(Sensor * s, DataPoint * buffer, int bufsiz)
+{
+ int amount_read = 0;
+ //CRITICAL SECTION (Don't access file while sensor thread is writing to it!)
+ pthread_mutex_lock(&(s->mutex));
+
+ fseek(s->file, -bufsiz*sizeof(DataPoint), SEEK_END);
+ amount_read = fread(buffer, sizeof(DataPoint), bufsiz, s->file);
+ //Log(LOGDEBUG, "Read %d data points", amount_read);
+ pthread_mutex_unlock(&(s->mutex));
+ return amount_read;
+}
+
+/**
+ * Get a Sensor given an ID string
+ * @param id_str ID string
+ * @returns Sensor* identified by the string; NULL on error
+ */
+Sensor * Sensor_Identify(const char * id_str)
+{
+ char * end;
+ // Parse string as integer
+ int id = strtol(id_str, &end, 10);
+ if (*end != '\0')
+ {
+ return NULL;
+ }
+ // Bounds check
+ if (id < 0 || id >= NUMSENSORS)
+ return NULL;
+
+
+ Log(LOGDEBUG, "Sensor \"%s\" identified", g_sensor_names[id]);
+ return g_sensors+id;
+}
+
+/**
+ * Handle a request to the sensor module
+ * @param context - The context to work in
+ * @param params - Parameters passed
+ */
+void Sensor_Handler(FCGIContext *context, char * params)
+{
+ DataPoint buffer[SENSOR_QUERYBUFSIZ];
+ StatusCodes status = STATUS_OK;
+
+ enum {DEFAULT, DUMP} operation = DEFAULT;
+
+ const char * key; const char * value;
+
+ Sensor * sensor = NULL;
+
+ while ((params = FCGI_KeyPair(params, &key, &value)) != NULL)
+ {
+ Log(LOGDEBUG, "Got key=%s and value=%s", key, value);
+ if (strcmp(key, "id") == 0)
+ {
+ if (sensor != NULL)
+ {
+ Log(LOGERR, "Only one sensor id should be specified");
+ status = STATUS_ERROR;
+ break;
+ }
+ if (*value == '\0')
+ {
+ Log(LOGERR, "No id specified.");
+ status = STATUS_ERROR;
+ break;
+ }
+
+ sensor = Sensor_Identify(value);
+ if (sensor == NULL)
+ {
+ Log(LOGERR, "Invalid sensor id: %s", value);
+ status = STATUS_ERROR;
+ break;
+ }
+ }
+ else if (strcmp(key, "dump") == 0)
+ {
+ if (operation != DEFAULT)
+ {
+ Log(LOGERR, "Operation already specified!");
+ status = STATUS_ERROR;
+ break;
+ }
+ operation = DUMP;
+ }
+ else
+ {
+ Log(LOGERR, "Unknown key \"%s\" (value = %s)", key, value);
+ status = STATUS_ERROR;
+ break;
+ }
+ }
+
+ if (status != STATUS_ERROR && sensor == NULL)
+ {
+ Log(LOGERR, "No valid sensor id given");
+ status = STATUS_ERROR;
+ }
+
+ if (status == STATUS_ERROR)
+ {
+ FCGI_RejectJSON(context, "Invalid input parameters");
+ return;
+ }