RocketLogger  1.1
lib_util.c
Go to the documentation of this file.
1 
31 #include "rl_util.h"
32 
34 #define NUMBER_SAMPLE_RATES 10
37  1, 10, 100, 1000, 2000, 4000, 8000, 16000, 32000, 64000};
38 
40 #define NUMBER_UPDATE_RATES 4
41 int possible_update_rates[NUMBER_UPDATE_RATES] = {1, 2, 5, 10};
43 
49 int check_sample_rate(int sample_rate) {
50  for (int i = 0; i < NUMBER_SAMPLE_RATES; i++) {
51  if (possible_sample_rates[i] == sample_rate) {
52  return SUCCESS;
53  }
54  }
55  return FAILURE;
56 }
57 
63 int check_update_rate(int update_rate) {
64  for (int i = 0; i < NUMBER_UPDATE_RATES; i++) {
65  if (possible_update_rates[i] == update_rate) {
66  return SUCCESS;
67  }
68  }
69  return FAILURE;
70 }
71 
76 pid_t get_pid(void) {
77 
78  // open file
79  pid_t pid;
80  FILE* file = fopen(PID_FILE, "r");
81  if (file == NULL) { // no pid found -> no process running
82  return FAILURE;
83  }
84 
85  // read pid
86  fread(&pid, sizeof(pid_t), 1, file); // get PID of background process
87 
88  // close file
89  fclose(file);
90 
91  return pid;
92 }
93 
99 int set_pid(pid_t pid) {
100 
101  // open file
102  FILE* file = fopen(PID_FILE, "w");
103  if (file == NULL) {
104  rl_log(ERROR, "failed to create pid file");
105  return FAILURE;
106  }
107 
108  // write pid
109  fwrite(&pid, sizeof(pid_t), 1, file);
110 
111  // close file
112  fclose(file);
113 
114  return SUCCESS;
115 }
#define FAILURE
Definition: types.h:77
#define SUCCESS
Definition: types.h:74
int check_sample_rate(int sample_rate)
Definition: lib_util.c:49
#define NUMBER_SAMPLE_RATES
Number of possible sampling rates.
Definition: lib_util.c:34
void rl_log(rl_log_type type, const char *format,...)
Definition: log.c:38
int possible_update_rates[NUMBER_UPDATE_RATES]
Possible update rates.
Definition: lib_util.c:42
int set_pid(pid_t pid)
Definition: lib_util.c:99
int check_update_rate(int update_rate)
Definition: lib_util.c:63
pid_t get_pid(void)
Definition: lib_util.c:76
Error.
Definition: types.h:199
#define PID_FILE
Process ID file for background process.
Definition: types.h:81
int possible_sample_rates[NUMBER_SAMPLE_RATES]
Possible sampling rates.
Definition: lib_util.c:36
#define NUMBER_UPDATE_RATES
Number of possible update rates.
Definition: lib_util.c:40