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