RocketLogger  1.0
lib_util.c
Go to the documentation of this file.
1 
5 #include "rl_util.h"
6 
8 #define NUMBER_SAMPLE_RATES 10
9 int possible_sample_rates[NUMBER_SAMPLE_RATES] = {1, 10, 100, 1000, 2000, 4000, 8000, 16000, 32000, 64000};
11 
13 #define NUMBER_UPDATE_RATES 4
16 
22 int check_sample_rate(int sample_rate) {
23  int i;
24  for(i=0; i<NUMBER_SAMPLE_RATES; i++) {
25  if(possible_sample_rates[i] == sample_rate){
26  return SUCCESS;
27  }
28  }
29  return FAILURE;
30 }
31 
37 int check_update_rate(int update_rate) {
38  int i;
39  for(i=0; i<NUMBER_UPDATE_RATES; i++) {
40  if(possible_update_rates[i] == update_rate){
41  return SUCCESS;
42  }
43  }
44  return FAILURE;
45 }
46 
51 pid_t get_pid(void) {
52 
53  // open file
54  pid_t pid;
55  FILE* file = fopen(PID_FILE, "r");
56  if(file == NULL) { // no pid found -> no process running
57  return FAILURE;
58  }
59 
60  // read pid
61  fread(&pid, sizeof(pid_t), 1, file); // get PID of background process
62 
63  //close file
64  fclose(file);
65 
66  return pid;
67 }
68 
74 int set_pid(pid_t pid) {
75 
76  // open file
77  FILE* file = fopen(PID_FILE, "w");
78  if(file == NULL) {
79  rl_log(ERROR, "failed to create pid file");
80  return FAILURE;
81  }
82 
83  // write pid
84  fwrite(&pid, sizeof(pid_t), 1, file);
85 
86  //close file
87  fclose(file);
88 
89  return SUCCESS;
90 }
#define FAILURE
Definition: types.h:53
#define SUCCESS
Definition: types.h:50
int check_sample_rate(int sample_rate)
Definition: lib_util.c:22
#define NUMBER_SAMPLE_RATES
Number of possible sampling rates.
Definition: lib_util.c:8
void rl_log(rl_log_type type, const char *format,...)
Definition: log.c:12
int possible_update_rates[NUMBER_UPDATE_RATES]
Possible update rates.
Definition: lib_util.c:15
int set_pid(pid_t pid)
Definition: lib_util.c:74
int check_update_rate(int update_rate)
Definition: lib_util.c:37
pid_t get_pid(void)
Definition: lib_util.c:51
Error.
Definition: types.h:167
#define PID_FILE
Process ID file for background process.
Definition: types.h:57
int possible_sample_rates[NUMBER_SAMPLE_RATES]
Possible sampling rates.
Definition: lib_util.c:10
#define NUMBER_UPDATE_RATES
Number of possible update rates.
Definition: lib_util.c:13