RocketLogger  1.0
util.c
Go to the documentation of this file.
1 
5 #include "util.h"
6 
7 // channel functions
13 int is_current(int index) {
14  if(index == I1H_INDEX || index == I1L_INDEX || index == I2H_INDEX || index == I2L_INDEX) {
15  return 1;
16  } else {
17  return 0;
18  }
19 }
20 
26 int is_low_current(int index) {
27  if(index == I1L_INDEX || index == I2L_INDEX) {
28  return 1;
29  } else {
30  return 0;
31  }
32 }
33 
39 int count_channels(int channels[NUM_CHANNELS]) {
40  int i = 0;
41  int c = 0;
42  for(i=0; i<NUM_CHANNELS; i++) {
43  if(channels[i] == CHANNEL_ENABLED) {
44  c++;
45  }
46  }
47  return c;
48 }
49 
50 
57 
58  // map shared memory
59  int shm_id = shmget(SHMEM_STATUS_KEY, sizeof(struct rl_status), SHMEM_PERMISSIONS);
60  if (shm_id == -1) {
61  rl_log(ERROR, "In read_status: failed to get shared status memory id; %d message: %s", errno, strerror(errno));
62  return FAILURE;
63  }
64  struct rl_status* shm_status = (struct rl_status*) shmat(shm_id, NULL, 0);
65 
66  if (shm_status == (void *) -1) {
67  rl_log(ERROR, "In read_status: failed to map shared status memory; %d message: %s", errno, strerror(errno));
68  return FAILURE;
69  }
70 
71  // read status
72  *status = *shm_status;
73 
74  // unmap shared memory
75  shmdt(shm_status);
76 
77  return SUCCESS;
78 }
79 
86 
87  // map shared memory
88  int shm_id = shmget(SHMEM_STATUS_KEY, sizeof(struct rl_status), IPC_CREAT | SHMEM_PERMISSIONS);
89  if (shm_id == -1) {
90  rl_log(ERROR, "In write_status: failed to get shared status memory id; %d message: %s", errno, strerror(errno));
91  return FAILURE;
92  }
93 
94  struct rl_status* shm_status = (struct rl_status*) shmat(shm_id, NULL, 0);
95  if (shm_status == (void *) -1) {
96  rl_log(ERROR, "In write_status: failed to map shared status memory; %d message: %s", errno, strerror(errno));
97  return FAILURE;
98  }
99 
100  // write status
101  *shm_status = *status;
102 
103  // unmap shared memory
104  shmdt(shm_status);
105 
106  return SUCCESS;
107 }
108 
109 
116 int ceil_div(int n, int d) {
117  if(n%d == d || n%d == 0) {
118  return n/d;
119  } else {
120  return n/d + 1;
121  }
122 }
123 
124 
125 // ------------------------------ SIGNAL HANDLER ------------------------------ //
126 
131 void sig_handler(int signo) {
132 
133  // signal generated by stop function
134  if (signo == SIGQUIT) {
135  // stop sampling
137  }
138 
139  // Ctrl+C handling
140  if (signo == SIGINT) {
141  signal(signo, SIG_IGN);
142  printf("Stopping RocketLogger ...\n");
144  }
145 }
146 // TODO: allow forced Ctrl+C
147 
148 
149 // ------------------------------ FILE READING/WRITING ------------------------------ //
150 
156 int read_file_value(char filename[]) {
157  FILE* fp;
158  unsigned int value = 0;
159  fp = fopen(filename, "rt");
160  if (fp == NULL) {
161  rl_log(ERROR, "failed to open file");
162  return FAILURE;
163  }
164  if(fscanf(fp, "%x", &value) <= 0) {
165  rl_log(ERROR, "failed to read from file");
166  return FAILURE;
167  }
168  fclose(fp);
169  return value;
170 }
int write_status(struct rl_status *status)
Definition: util.c:85
int is_low_current(int index)
Definition: util.c:26
#define FAILURE
Definition: types.h:53
#define SUCCESS
Definition: types.h:50
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 read_file_value(char filename[])
Definition: util.c:156
#define SHMEM_STATUS_KEY
Key for status shared memory (used for creation)
Definition: types.h:69
int ceil_div(int n, int d)
Definition: util.c:116
#define I2L_INDEX
Definition: types.h:189
#define NUM_CHANNELS
Maximum number of RocketLogger channels.
Definition: types.h:78
#define CHANNEL_ENABLED
Channel sampling enabled.
Definition: types.h:178
#define I1H_INDEX
Definition: types.h:184
void sig_handler(int signo)
Definition: util.c:131
int is_current(int index)
Definition: util.c:13
#define SHMEM_PERMISSIONS
Permissions for shared memory.
Definition: util.h:26
Error.
Definition: types.h:167
#define I2H_INDEX
Definition: types.h:188
Not sampling.
Definition: types.h:126
rl_sampling sampling
Sampling state.
Definition: types.h:236
int count_channels(int channels[NUM_CHANNELS])
Definition: util.c:39
#define I1L_INDEX
Definition: types.h:185