included analog and digital fail test sensors
[matches/MCTX3420.git] / server / sensor.c
1 /**
2  * @file sensor.c
3  * @brief Implementation of sensor thread
4  * TODO: Finalise implementation
5  */
6
7 #include "common.h"
8 #include "sensor.h"
9 #include "options.h"
10 #include <math.h>
11
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"
18 };
19
20 /**
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
24  */
25 void CheckSensor( SensorId sensor_id, double value)
26 {
27         switch (sensor_id)
28         {
29                 case ANALOG_FAIL0:
30                 {
31                         if( value > ANALOG_FAIL0_SAFETY || value < ANALOG_FAIL0_MIN_SAFETY)
32                         {
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?
35                         }
36                         else if( value > ANALOG_FAIL0_WARN || value < ANALOG_FAIL0_MIN_WARN)
37                         {
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);        
39                         }
40                         break;
41                 }
42                 case DIGITAL_FAIL0:
43                 {       
44                         if( value != 0 && value != 1)
45                         {
46                                 Log(LOGERR, "Sensor digital_fail0 is not 0 or 1\n");
47                         }
48                         break;
49                 }
50                 default:
51                 {
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)
53                 }
54         }
55 }
56
57 /**
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
62  */
63 DataPoint * GetData(SensorId sensor_id, DataPoint * d)
64 {
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
68
69         
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
73         
74         struct timeval t;
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);
77
78         // Make time relative
79         //d->time_stamp.tv_sec -= g_options.start_time.tv_sec;
80         //d->time_stamp.tv_usec -= g_options.start_time.tv_usec;
81         
82         switch (sensor_id)
83         {
84                 case ANALOG_TEST0:
85                 {
86                         static int count = 0;
87                         d->value = count++;
88                         break;
89                 }
90                 case ANALOG_TEST1:
91                         d->value = (double)(rand() % 100) / 100;
92                         break;
93                 case ANALOG_FAIL0:
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);
97                         break;
98                 //TODO: For digital sensors, consider only updating when sensor is actually changed
99                 case DIGITAL_TEST0:
100                         d->value = t.tv_sec % 2;
101                         break;
102                 case DIGITAL_TEST1:
103                         d->value = (t.tv_sec+1)%2;
104                         break;
105                 case DIGITAL_FAIL0:
106                         if( rand() % 100 > 98)
107                                 d->value = 2;
108                         d->value = rand() % 2; 
109                         //Gives 0 or 1 or a 2 every 1/100 times
110                         CheckSensor(sensor_id, d->value);
111                         break;
112                 default:
113                         Fatal("Unknown sensor id: %d", sensor_id);
114                         break;
115         }       
116         usleep(100000); // simulate delay in sensor polling
117
118         return d;
119 }
120
121 /**
122  * Destroy a sensor
123  * @param s - Sensor to destroy
124  */
125 void Destroy(Sensor * s)
126 {
127         // Maybe move the binary file into long term file storage?
128         fclose(s->file);
129 }
130
131
132
133 /**
134  * Initialise a sensor
135  * @param s - Sensor to initialise
136  */
137 void Init(Sensor * s, int id)
138 {
139         s->write_index = 0;
140         s->id = id;
141         s->points_written = 0;
142         s->points_read = 0;
143
144         #define FILENAMESIZE 3
145         char filename[FILENAMESIZE];
146         if (s->id >= pow(10, FILENAMESIZE))
147         {
148                 Fatal("Too many sensors! FILENAMESIZE is %d; increase it and recompile.", FILENAMESIZE);
149         }
150
151         pthread_mutex_init(&(s->mutex), NULL);
152                 
153         sprintf(filename, "%d", s->id);
154         unlink(filename); //TODO: Move old files somewhere
155
156         s->file = fopen(filename, "a+b"); // open binary file
157         Log(LOGDEBUG, "Initialised sensor %d; binary file is \"%s\"", id, filename);
158 }
159
160
161 /**
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)
165  */
166 void * Sensor_Main(void * arg)
167 {
168         Sensor * s = (Sensor*)(arg);
169
170         while (Thread_Runstate() == RUNNING) //TODO: Exit condition
171         {
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.
174                 // Rinse and repeat
175
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
179
180                 while (s->write_index < SENSOR_DATABUFSIZ)
181                 {
182                         DataPoint * d = &(s->buffer[s->write_index]);
183                         if (GetData(s->id, d) == NULL)
184                         {
185                                 Fatal("Error collecting data");
186                         }
187                         s->write_index += 1;
188                 }
189
190                 //Log(LOGDEBUG, "Filled buffer");
191
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)
199                         {
200                                 Fatal("Wrote %d data points and expected to write %d to \"%s\" - %s", amount_written, SENSOR_DATABUFSIZ, strerror(errno));
201                         }
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
206
207                 s->write_index = 0; // reset position in buffer
208                 
209         }
210         Log(LOGDEBUG, "Thread for sensor %d exits", s->id);
211         return NULL; 
212 }
213
214 /**
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?
222  */
223 int FindTime(Sensor * s, double time_stamp, int * count, DataPoint * found)
224 {
225         DataPoint d;
226
227         int lower = 0;
228         int upper = s->points_written - 1;
229         int index = 0;
230         if (count != NULL)
231                 *count = 0;     
232
233         while (upper - lower > 1)
234         {
235                 index = lower + ((upper - lower)/2);
236
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);
245                 
246                 if (amount_read != 1)
247                 {
248                         Fatal("Couldn't read single data point from sensor %d", s->id);
249                 }
250
251                 if (d.time_stamp > time_stamp)
252                 {
253                         upper = index;
254                 }
255                 else if (d.time_stamp < time_stamp)
256                 {
257                         lower = index;
258                 }
259                 if (count != NULL)
260                         *count += 1;
261         }
262
263         if (found != NULL)
264                 *found = d;
265
266         return index;
267         
268 }
269
270 /**
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
276  */
277 void PrintData(Sensor * s, int start, int end, OutputType output_type)
278 {
279         DataPoint buffer[SENSOR_QUERYBUFSIZ];
280         int index = start;
281
282         if (output_type == JSON)
283         {
284                 FCGI_JSONValue("[");
285         }
286
287
288         while (index < end)
289         {
290                 int to_read = end - index;
291                 if (to_read > SENSOR_QUERYBUFSIZ)
292                 {
293                         to_read = SENSOR_QUERYBUFSIZ;
294                 }
295
296                 int amount_read = 0;
297                 // CRITICAL SECTION
298                 pthread_mutex_lock(&(s->mutex));
299
300                         fseek(s->file, index*sizeof(DataPoint), SEEK_SET);
301                         amount_read = fread(buffer, sizeof(DataPoint), to_read, s->file);
302
303                 pthread_mutex_unlock(&(s->mutex));
304                 // End critical section
305
306                 if (amount_read != to_read)
307                 {
308                         Fatal("Failed to read %d DataPoints from sensor %d; read %d instead", to_read, s->id, amount_read);
309                 }
310
311                 // Print the data
312                 for (int i = 0; i < amount_read; ++i)
313                 {
314                         //TODO: Reformat?
315                         switch (output_type)
316                         {
317                                 case JSON:
318                                         FCGI_JSONValue("[%f, %f]", buffer[i].time_stamp, buffer[i].value);
319                                         if (i+1 < amount_read)
320                                                 FCGI_JSONValue(",");
321                                         break;
322                                 case CSV:
323                                         FCGI_PrintRaw("%f,%f\n", buffer[i].time_stamp, buffer[i].value);
324                                         break;
325                                 case TSV:
326                                 default:
327                                         FCGI_PrintRaw("%f\t%f\n", buffer[i].time_stamp, buffer[i].value);
328                                         break;
329                         }
330                 }
331                 index += amount_read;
332         }
333
334         if (output_type == JSON)
335         {
336                 FCGI_JSONValue("]");
337         }
338 }
339
340 /**
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
347  */
348 int Sensor_Query(Sensor * s, DataPoint * buffer, int bufsiz)
349 {
350         int amount_read = 0;
351         //CRITICAL SECTION (Don't access file while sensor thread is writing to it!)
352         pthread_mutex_lock(&(s->mutex));
353                 
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));
358         return amount_read;
359 }
360
361 /**
362  * Get a Sensor given an ID string
363  * @param id_str ID string
364  * @returns Sensor* identified by the string; NULL on error
365  */
366 Sensor * Sensor_Identify(const char * id_str)
367 {
368         char * end;
369         // Parse string as integer
370         int id = strtol(id_str, &end, 10);
371         if (*end != '\0')
372         {
373                 return NULL;
374         }
375         // Bounds check
376         if (id < 0 || id >= NUMSENSORS)
377                 return NULL;
378
379
380         Log(LOGDEBUG, "Sensor \"%s\" identified", g_sensor_names[id]);
381         return g_sensors+id;
382 }
383
384 /**
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!
389  */
390 void Sensor_Handler(FCGIContext *context, char * params)
391 {
392         StatusCodes status = STATUS_OK;
393
394         OutputType output_type = JSON;
395         
396
397
398         const char * key; const char * value;
399
400         Sensor * sensor = NULL;
401
402         struct timeval now;
403         gettimeofday(&now, NULL);
404
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;
412         int end_index = -1;
413
414
415         while ((params = FCGI_KeyPair(params, &key, &value)) != NULL)
416         {
417                 Log(LOGDEBUG, "Got key=%s and value=%s", key, value);
418                 if (strcmp(key, "id") == 0)
419                 {
420                         if (sensor != NULL)
421                         {
422                                 Log(LOGERR, "Only one sensor id should be specified");
423                                 status = STATUS_ERROR;
424                                 break;
425                         }
426                         if (*value == '\0')
427                         {
428                                 Log(LOGERR, "No id specified.");
429                                 status = STATUS_ERROR;
430                                 break;
431                         }
432
433                         sensor = Sensor_Identify(value);
434                         if (sensor == NULL)
435                         {
436                                 Log(LOGERR, "Invalid sensor id: %s", value);
437                                 status = STATUS_ERROR;
438                                 break;
439                         }
440                 }
441                 else if (strcmp(key, "format") == 0)
442                 {
443                         if (strcmp(value, "json") == 0)
444                                 output_type = JSON;
445                         else if (strcmp(value, "csv") == 0)
446                                 output_type = CSV;
447                         else if (strcmp(value, "tsv") == 0)
448                                 output_type = TSV;                      
449                 }
450                 else if (strcmp(key, "points") == 0)
451                 {
452                         points_specified = true;
453                         if (strcmp(value, "all") == 0)
454                         {
455                                 query_size = sensor->points_written;
456                         }
457                         else
458                         {
459                                 char * end;
460                                 query_size = strtol(value, &end, 10);
461                                 if (*end != '\0')
462                                 {
463                                         Log(LOGERR, "Require \"all\" or an integer value: %s = %s", key, value);
464                                         status = STATUS_ERROR;
465                                         break;
466                                 }
467                         }
468                         
469                 }
470                 else if (strcmp(key, "start_time") == 0)
471                 {
472                         seek_time = true;
473                         char * end;
474                         start_time = strtod(value, &end);
475                         if (*end != '\0')
476                         {
477                                 Log(LOGERR, "Require a double: %s = %s", key, value);
478                                 status = STATUS_ERROR;
479                                 break;
480                         }                       
481
482                         // Treat negative values as being relative to the current time
483                         if (start_time < 0)
484                         {
485                                 start_time = current_time + start_time;
486                         }
487                         start_time = floor(start_time);
488                 }
489                 else if (strcmp(key, "end_time") == 0)
490                 {
491                         seek_time = true;
492                         char * end;
493                         end_time = strtod(value, &end);
494                         if (*end != '\0')
495                         {
496                                 Log(LOGERR, "Require a double: %s = %s", key, value);
497                                 status = STATUS_ERROR;
498                                 break;
499                         }       
500
501                         // Treat negative values as being relative to the current time
502                         if (end_time < 0)
503                         {
504                                 end_time = current_time + end_time;
505                         }               
506                         end_time = ceil(end_time);
507                 }
508                 // For backward compatability:
509                 else if (strcmp(key, "dump") == 0)
510                 {
511                         output_type = TSV;
512                         query_size = sensor->points_written+1;
513                 }
514                 else
515                 {
516                         Log(LOGERR, "Unknown key \"%s\" (value = %s)", key, value);
517                         status = STATUS_ERROR;
518                         break;
519                 }               
520         }
521
522         if (status != STATUS_ERROR && sensor == NULL)
523         {
524                 Log(LOGERR, "No valid sensor id given");
525                 status = STATUS_ERROR;
526         }
527
528         if (status == STATUS_ERROR)
529         {
530                 FCGI_RejectJSON(context, "Invalid input parameters");
531                 return;
532         }
533
534
535         if (seek_time)
536         {
537                 if (end_time < 0 && !points_specified)
538                         end_index = sensor->points_written;
539                 else
540                 {
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);
544                 }
545                 if (start_time < 0)
546                         start_time = 0;
547                 else
548                 {
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);
552                 }
553
554                 if (points_specified)
555                         end_index = start_index + query_size;
556         }
557         else
558         {
559                 start_index = sensor->points_written - query_size;
560                 
561                 end_index = sensor->points_written;
562         }
563         
564         if (start_index < 0)
565         {
566                 Log(LOGNOTE, "start_index = %d => Clamped to 0", start_index);
567                 start_index = 0;
568         }
569         if (end_index > sensor->points_written)
570         {
571                 Log(LOGNOTE, "end_index = %d => Clamped to %d", end_index, sensor->points_written);
572                 end_index = sensor->points_written;
573         }
574         
575         switch (output_type)
576         {
577                 case JSON:
578                         FCGI_BeginJSON(context, status);
579                         FCGI_JSONLong("id", sensor->id);
580                         FCGI_JSONKey("data");
581                         PrintData(sensor, start_index, end_index, output_type);
582                         FCGI_EndJSON();
583                         break;
584                 default:
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);
591                         break;
592         }
593         
594 }
595
596 /**
597  * Setup Sensors, start Sensor polling thread(s)
598  */
599 void Sensor_Spawn()
600 {
601         // start sensor threads
602         for (int i = 0; i < NUMSENSORS; ++i)
603         {
604                 Init(g_sensors+i, i);
605                 pthread_create(&(g_sensors[i].thread), NULL, Sensor_Main, (void*)(g_sensors+i));
606         }
607 }
608
609 /**
610  * Quit Sensor loops
611  */
612 void Sensor_Join()
613 {
614         if (!Thread_Runstate())
615         {
616                 Fatal("This function should not be called before Thread_QuitProgram");
617         }
618         for (int i = 0; i < NUMSENSORS; ++i)
619         {
620                 pthread_join(g_sensors[i].thread, NULL);
621                 Destroy(g_sensors+i);
622         }
623 }

UCC git Repository :: git.ucc.asn.au