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