RocketLogger  1.1.6
rl_lib.c
Go to the documentation of this file.
1 
32 #include "rl_lib.h"
33 
39 
40  struct rl_status status;
41 
42  // get pid
43  pid_t pid = get_pid();
44  if (pid == FAILURE || kill(pid, 0) < 0) {
45  // process not running
46  status.state = RL_OFF;
47  } else {
48  // read status
49  read_status(&status);
50  }
51 
52  return status.state;
53 }
54 
61 
62  // get pid
63  pid_t pid = get_pid();
64  if (pid == FAILURE || kill(pid, 0) < 0) {
65  // process not running
66  status->state = RL_OFF;
67  } else {
68  // read status
69  read_status(status);
70  }
71 
72  return status->state;
73 }
79 void rl_read_calibration(struct rl_calibration* calibration_ptr,
80  struct rl_conf* conf) {
81  read_calibration(conf);
82  memcpy(calibration_ptr, &calibration, sizeof(struct rl_calibration));
83 }
84 
91 int rl_start(struct rl_conf* conf, char* file_comment) {
92 
93  // check mode
94  switch (conf->mode) {
95  case LIMIT:
96  break;
97  case CONTINUOUS:
98  // create deamon to run in background
99  if (daemon(1, 1) < 0) {
100  rl_log(ERROR, "failed to create background process");
101  return SUCCESS;
102  }
103  break;
104  case METER:
105  // set meter config
107  conf->sample_limit = 0;
108  conf->enable_web_server = 0;
109  conf->file_format = NO_FILE;
110  if (conf->sample_rate < MIN_ADC_RATE) {
111  rl_log(WARNING, "too low sample rate. Setting rate to 1kSps");
112  conf->sample_rate = MIN_ADC_RATE;
113  }
114  break;
115  default:
116  rl_log(ERROR, "wrong mode");
117  return FAILURE;
118  }
119 
120  // check input
121  if (check_sample_rate(conf->sample_rate) == FAILURE) {
122  rl_log(ERROR, "wrong sampling rate");
123  return FAILURE;
124  }
125  if (check_update_rate(conf->update_rate) == FAILURE) {
126  rl_log(ERROR, "wrong update rate");
127  return FAILURE;
128  }
129  if (conf->update_rate != 1 && conf->enable_web_server == 1) {
130  rl_log(WARNING, "webserver plot does not work with update rates >1. "
131  "Disabling webserver ...");
132  conf->enable_web_server = 0;
133  }
134 
135  // check ambient configuration
136  if (conf->ambient.enabled == AMBIENT_ENABLED &&
137  conf->file_format == NO_FILE) {
138  rl_log(
139  WARNING,
140  "Ambient logging not possible without file. Disabling ambient ...");
142  }
143 
144  // store PID to file
145  pid_t pid = getpid();
146  set_pid(pid);
147 
148  // register signal handler (for stopping)
149  if (signal(SIGQUIT, sig_handler) == SIG_ERR ||
150  signal(SIGINT, sig_handler) == SIG_ERR) {
151  rl_log(ERROR, "can't register signal handler");
152  return FAILURE;
153  }
154 
155  // INITIATION
156  hw_init(conf);
157 
158  // check ambient sensor available
159  if (conf->ambient.enabled == AMBIENT_ENABLED &&
160  conf->ambient.sensor_count == 0) {
162  rl_log(WARNING, "No ambient sensor found. Disabling ambient ...");
163  }
164 
165  // SAMPLING
166  rl_log(INFO, "sampling start");
167  hw_sample(conf, file_comment);
168  rl_log(INFO, "sampling finished");
169 
170  // FINISH
171  hw_close(conf);
172 
173  return SUCCESS;
174 }
175 
180 int rl_stop(void) {
181 
182  // check if running
183  if (rl_get_status() != RL_RUNNING) {
184  rl_log(ERROR, "RocketLogger not running");
185  return FAILURE;
186  }
187 
188  // ged pid
189  pid_t pid = get_pid();
190 
191  // send stop signal
192  kill(pid, SIGQUIT);
193 
194  return SUCCESS;
195 }
Limited sampling mode (limited by number of samples to take)
Definition: types.h:158
rl_mode mode
Sampling mode.
Definition: types.h:251
Continuous sampling mode (in background)
Definition: types.h:159
#define METER_UPDATE_RATE
Data update rate in METER mode.
Definition: types.h:112
#define FAILURE
Definition: types.h:78
#define SUCCESS
Definition: types.h:75
int check_sample_rate(int sample_rate)
Definition: lib_util.c:50
struct rl_status status
Current status of RocketLogger.
Definition: rl_server.c:71
void rl_log(rl_log_type type, const char *format,...)
Definition: log.c:39
int read_status(struct rl_status *status)
Definition: util.c:95
int set_pid(pid_t pid)
Definition: lib_util.c:100
struct rl_ambient ambient
Ambient conf.
Definition: types.h:277
int enable_web_server
En-/disable plots on web interface.
Definition: types.h:267
int enabled
Definition: types.h:213
rl_state state
State.
Definition: types.h:285
int sensor_count
Definition: types.h:214
int check_update_rate(int update_rate)
Definition: lib_util.c:64
int sample_limit
Sample limit (0 for continuous)
Definition: types.h:259
Definition: types.h:247
pid_t get_pid(void)
Definition: lib_util.c:77
Warning.
Definition: types.h:201
Running.
Definition: types.h:142
int hw_sample(struct rl_conf *conf, char *file_comment)
Definition: rl_hw.c:128
void sig_handler(int signo)
Definition: util.c:178
void rl_read_calibration(struct rl_calibration *calibration_ptr, struct rl_conf *conf)
Definition: rl_lib.c:79
rl_file_format file_format
File format.
Definition: types.h:271
int update_rate
Data update rate.
Definition: types.h:257
int rl_start(struct rl_conf *conf, char *file_comment)
Definition: rl_lib.c:91
Error.
Definition: types.h:200
int rl_stop(void)
Definition: rl_lib.c:180
struct rl_conf conf
Current configuration.
Definition: types.h:293
int sample_rate
Sampling rate.
Definition: types.h:253
No file.
Definition: types.h:183
void hw_init(struct rl_conf *conf)
Definition: rl_hw.c:40
rl_state rl_get_status(void)
Definition: rl_lib.c:38
#define AMBIENT_ENABLED
Definition: types.h:211
Information.
Definition: types.h:202
#define MIN_ADC_RATE
Minimal ADC sampling rate.
Definition: types.h:134
#define AMBIENT_DISABLED
Definition: types.h:210
Meter mode (display current values in terminal)
Definition: types.h:160
void hw_close(struct rl_conf *conf)
Definition: rl_hw.c:92
int read_calibration(struct rl_conf *conf)
Definition: calibration.c:61
int rl_read_status(struct rl_status *status)
Definition: rl_lib.c:60
enum state rl_state
Idle.
Definition: types.h:141
struct rl_calibration calibration
Calibration data.
Definition: types.h:335