RocketLogger  1.0
rl_lib.c
Go to the documentation of this file.
1 
5 #include "rl_lib.h"
6 
12 
13  struct rl_status status;
14 
15  // get pid
16  pid_t pid = get_pid();
17  if(pid == FAILURE || kill(pid, 0) < 0) {
18  // process not running
19  status.state = RL_OFF;
20  } else {
21  // read status
22  read_status(&status);
23  }
24 
25  return status.state;
26 }
27 
34 
35  // get pid
36  pid_t pid = get_pid();
37  if(pid == FAILURE || kill(pid, 0) < 0) {
38  // process not running
39  status->state = RL_OFF;
40  } else {
41  // read status
42  read_status(status);
43  }
44 
45  return status->state;
46 }
52 void rl_read_calibration(struct rl_calibration* calibration_ptr, struct rl_conf* conf) {
53  read_calibration(conf);
54  memcpy(calibration_ptr, &calibration, sizeof(struct rl_calibration));
55 }
56 
62 int rl_start(struct rl_conf* conf) {
63 
64  // check mode
65  switch (conf->mode) {
66  case LIMIT:
67  break;
68  case CONTINUOUS:
69  // create deamon to run in background
70  if (daemon(1, 1) < 0) {
71  rl_log(ERROR, "failed to create background process");
72  return SUCCESS;
73  }
74  break;
75  case METER:
76  // set meter config
78  conf->sample_limit = 0;
79  conf->enable_web_server = 0;
80  conf->file_format = NO_FILE;
81  if(conf->sample_rate < MIN_ADC_RATE) {
82  rl_log(WARNING, "too low sample rate. Setting rate to 1kSps");
83  conf->sample_rate = MIN_ADC_RATE;
84  }
85  break;
86  default:
87  rl_log(ERROR, "wrong mode");
88  return FAILURE;
89  }
90 
91  // check input
92  if(check_sample_rate(conf->sample_rate) == FAILURE) {
93  rl_log(ERROR, "wrong sampling rate");
94  return FAILURE;
95  }
96  if(check_update_rate(conf->update_rate) == FAILURE) {
97  rl_log(ERROR, "wrong update rate");
98  return FAILURE;
99  }
100  if(conf->update_rate != 1 && conf->enable_web_server == 1) {
101  rl_log(WARNING, "webserver plot does not work with update rates >1. Disabling webserver ...");
102  conf->enable_web_server = 0;
103  }
104 
105  // store PID to file
106  pid_t pid = getpid();
107  set_pid(pid);
108 
109  // register signal handler (for stopping)
110  if (signal(SIGQUIT, sig_handler) == SIG_ERR || signal(SIGINT, sig_handler) == SIG_ERR) {
111  rl_log(ERROR, "can't register signal handler");
112  return FAILURE;
113  }
114 
115  // INITIATION
116  hw_init(conf);
117 
118  rl_log(INFO, "sampling started");
119 
120  // SAMPLING
121  hw_sample(conf);
122 
123  rl_log(INFO, "sampling finished");
124 
125  // FINISH
126  hw_close(conf);
127 
128  return SUCCESS;
129 
130 }
131 
132 
137 int rl_stop(void) {
138 
139  // check if running
140  if(rl_get_status() != RL_RUNNING) {
141  rl_log(ERROR, "RocketLogger not running");
142  return FAILURE;
143  }
144 
145  // ged pid
146  pid_t pid = get_pid();
147 
148  // send stop signal
149  kill(pid, SIGQUIT);
150 
151  return SUCCESS;
152 }
Limited sampling mode (limited by number of samples to take)
Definition: types.h:134
rl_mode mode
Sampling mode.
Definition: types.h:204
Continuous sampling mode (in background)
Definition: types.h:135
#define METER_UPDATE_RATE
Data update rate in METER mode.
Definition: types.h:86
#define FAILURE
Definition: types.h:53
#define SUCCESS
Definition: types.h:50
int check_sample_rate(int sample_rate)
Definition: lib_util.c:22
struct rl_status status
Current status of RocketLogger.
Definition: rl_server.c:42
void rl_log(rl_log_type type, const char *format,...)
Definition: log.c:12
int read_status(struct rl_status *status)
Definition: util.c:56
int set_pid(pid_t pid)
Definition: lib_util.c:74
int enable_web_server
En-/disable plots on web interface.
Definition: types.h:218
rl_state state
State.
Definition: types.h:234
int check_update_rate(int update_rate)
Definition: lib_util.c:37
int sample_limit
Sample limit (0 for continuous)
Definition: types.h:210
Definition: types.h:202
pid_t get_pid(void)
Definition: lib_util.c:51
Warning.
Definition: types.h:168
int rl_start(struct rl_conf *conf)
Definition: rl_lib.c:62
Running.
Definition: types.h:118
void sig_handler(int signo)
Definition: util.c:131
void rl_read_calibration(struct rl_calibration *calibration_ptr, struct rl_conf *conf)
Definition: rl_lib.c:52
rl_file_format file_format
File format.
Definition: types.h:222
int update_rate
Data update rate.
Definition: types.h:208
Error.
Definition: types.h:167
int rl_stop(void)
Definition: rl_lib.c:137
struct rl_conf conf
Current configuration.
Definition: types.h:242
int sample_rate
Sampling rate.
Definition: types.h:206
No file.
Definition: types.h:150
void hw_init(struct rl_conf *conf)
Definition: rl_hw.c:13
rl_state rl_get_status(void)
Definition: rl_lib.c:11
Information.
Definition: types.h:169
#define MIN_ADC_RATE
Minimal ADC sampling rate.
Definition: types.h:108
Meter mode (display current values in terminal)
Definition: types.h:136
void hw_close(struct rl_conf *conf)
Definition: rl_hw.c:56
int read_calibration(struct rl_conf *conf)
Definition: calibration.c:33
int hw_sample(struct rl_conf *conf)
Definition: rl_hw.c:85
int rl_read_status(struct rl_status *status)
Definition: rl_lib.c:33
enum state rl_state
Idle.
Definition: types.h:117
struct rl_calibration calibration
Calibration data.
Definition: types.h:284