-
Notifications
You must be signed in to change notification settings - Fork 1
/
sensor_db.h
93 lines (76 loc) · 3.17 KB
/
sensor_db.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#ifndef _SENSOR_DB_H_
#define _SENSOR_DB_H_
#include <stdio.h>
#include <stdlib.h>
#include <sqlite3.h>
#include "config.h"
#include "sbuffer.h"
// stringify preprocessor directives using 2-level preprocessor magic
// this avoids using directives like -DDB_NAME=\"some_db_name\"
#define REAL_TO_STRING(s) #s
#define TO_STRING(s) REAL_TO_STRING(s) //force macro-expansion on s before stringify s
#ifndef DB_NAME
#define DB_NAME Sensor.db
#endif
#ifndef TABLE_NAME
#define TABLE_NAME SensorData
#endif
#define DBCONN sqlite3
typedef int (*callback_t)(void *, int, char **, char **);
/*
* Reads continiously all data from the shared buffer data structure and stores this into the database
* When *buffer becomes NULL the method finishes. This method will NOT automatically disconnect from the db
*/
void storagemgr_parse_sensor_data(DBCONN * conn, sbuffer_t ** buffer);
/*
* Make a connection to the database server
* Create (open) a database with name DB_NAME having 1 table named TABLE_NAME
* If the table existed, clear up the existing data if clear_up_flag is set to 1
* Return the connection for success, NULL if an error occurs
*/
DBCONN * init_connection(char clear_up_flag);
/*
* Disconnect from the database server, and free all used memory
*/
void disconnect(DBCONN * conn);
/*
* Write an INSERT query to insert a single sensor measurement
* Return zero for success, and non-zero if an error occurs
*/
int insert_sensor(DBCONN * conn, sensor_id_t id, sensor_value_t value, sensor_ts_t ts);
/*
* Write a SELECT query to select all sensor measurements in the table
* The callback function is applied to every row in the result
* Return zero for success, and non-zero if an error occurs
*/
int find_sensor_all(DBCONN * conn, callback_t f);
/*
* Write a SELECT query to return all sensor measurements having a temperature of 'value'
* The callback function is applied to every row in the result
* Return zero for success, and non-zero if an error occurs
*/
int find_sensor_by_value(DBCONN * conn, sensor_value_t value, callback_t f);
/*
* Write a SELECT query to return all sensor measurements of which the temperature exceeds 'value'
* The callback function is applied to every row in the result
* Return zero for success, and non-zero if an error occurs
*/
int find_sensor_exceed_value(DBCONN * conn, sensor_value_t value, callback_t f);
/*
* Write a SELECT query to return all sensor measurements having a timestamp 'ts'
* The callback function is applied to every row in the result
* Return zero for success, and non-zero if an error occurs
*/
int find_sensor_by_timestamp(DBCONN * conn, sensor_ts_t ts, callback_t f);
/*
* Write a SELECT query to return all sensor measurements recorded after timestamp 'ts'
* The callback function is applied to every row in the result
* return zero for success, and non-zero if an error occurs
*/
int find_sensor_after_timestamp(DBCONN * conn, sensor_ts_t ts, callback_t f);
/**
* This method shares variables from threads space to carry out more functionality,
* like having access to IPC mutex/rwlock and updating the return value of the thread
**/
void storagemgr_init(storagemgr_init_arg_t * arg);
#endif /* _SENSOR_DB_H_ */