RocketLogger  1.1.5
util.c
Go to the documentation of this file.
1 
32 #include <errno.h>
33 #include <signal.h>
34 #include <stdint.h>
35 #include <stdio.h>
36 
37 #include <sys/ipc.h>
38 #include <sys/shm.h>
39 #include <sys/time.h>
40 
41 #include "log.h"
42 #include "types.h"
43 
44 #include "util.h"
45 
46 // ----- CHANNEL FUNCTIONS ----- //
47 
53 int is_current(int index) {
54  if (index == I1H_INDEX || index == I1L_INDEX || index == I2H_INDEX ||
55  index == I2L_INDEX) {
56  return 1;
57  } else {
58  return 0;
59  }
60 }
61 
67 int is_low_current(int index) {
68  if (index == I1L_INDEX || index == I2L_INDEX) {
69  return 1;
70  } else {
71  return 0;
72  }
73 }
74 
80 int count_channels(int channels[NUM_CHANNELS]) {
81  int c = 0;
82  for (int i = 0; i < NUM_CHANNELS; i++) {
83  if (channels[i] == CHANNEL_ENABLED) {
84  c++;
85  }
86  }
87  return c;
88 }
89 
96 
97  // map shared memory
98  int shm_id =
99  shmget(SHMEM_STATUS_KEY, sizeof(struct rl_status), SHMEM_PERMISSIONS);
100  if (shm_id == -1) {
101  rl_log(ERROR, "In read_status: failed to get shared status memory id; "
102  "%d message: %s",
103  errno, strerror(errno));
104  return FAILURE;
105  }
106  struct rl_status* shm_status = (struct rl_status*)shmat(shm_id, NULL, 0);
107 
108  if (shm_status == (void*)-1) {
109  rl_log(ERROR, "In read_status: failed to map shared status memory; %d "
110  "message: %s",
111  errno, strerror(errno));
112  return FAILURE;
113  }
114 
115  // read status
116  *status = *shm_status;
117 
118  // unmap shared memory
119  shmdt(shm_status);
120 
121  return SUCCESS;
122 }
123 
130 
131  // map shared memory
132  int shm_id = shmget(SHMEM_STATUS_KEY, sizeof(struct rl_status),
133  IPC_CREAT | SHMEM_PERMISSIONS);
134  if (shm_id == -1) {
135  rl_log(ERROR, "In write_status: failed to get shared status memory id; "
136  "%d message: %s",
137  errno, strerror(errno));
138  return FAILURE;
139  }
140 
141  struct rl_status* shm_status = (struct rl_status*)shmat(shm_id, NULL, 0);
142  if (shm_status == (void*)-1) {
143  rl_log(ERROR, "In write_status: failed to map shared status memory; %d "
144  "message: %s",
145  errno, strerror(errno));
146  return FAILURE;
147  }
148 
149  // write status
150  *shm_status = *status;
151 
152  // unmap shared memory
153  shmdt(shm_status);
154 
155  return SUCCESS;
156 }
157 
164 int ceil_div(int n, int d) {
165  if (n % d == d || n % d == 0) {
166  return n / d;
167  } else {
168  return n / d + 1;
169  }
170 }
171 
172 // ----- SIGNAL HANDLER ----- //
173 
178 void sig_handler(int signo) {
179 
180  // signal generated by stop function
181  if (signo == SIGQUIT) {
182  // stop sampling
184  }
185 
186  // Ctrl+C handling
187  if (signo == SIGINT) {
188  signal(signo, SIG_IGN);
189  printf("Stopping RocketLogger ...\n");
191  }
192 }
193 // TODO: allow forced Ctrl+C
194 
195 // ----- FILE READING/WRITING ----- //
196 
202 int read_file_value(char filename[]) {
203  FILE* fp;
204  unsigned int value = 0;
205  fp = fopen(filename, "rt");
206  if (fp == NULL) {
207  rl_log(ERROR, "failed to open file");
208  return FAILURE;
209  }
210  if (fscanf(fp, "%x", &value) <= 0) {
211  rl_log(ERROR, "failed to read from file");
212  return FAILURE;
213  }
214  fclose(fp);
215  return value;
216 }
217 
223 void create_time_stamp(struct time_stamp* timestamp_realtime,
224  struct time_stamp* timestamp_monotonic) {
225 
226  struct timespec spec_real;
227  struct timespec spec_monotonic;
228 
229  // get time stamp of real-time and monotonic clock
230  int ret1 = clock_gettime(CLOCK_REALTIME, &spec_real);
231  int ret2 = clock_gettime(CLOCK_MONOTONIC_RAW, &spec_monotonic);
232 
233  if (ret1 < 0 || ret2 < 0) {
234  rl_log(ERROR, "failed to get time");
235  }
236 
237  // convert to own time stamp
238  timestamp_realtime->sec = (int64_t)spec_real.tv_sec;
239  timestamp_realtime->nsec = (int64_t)spec_real.tv_nsec;
240  timestamp_monotonic->sec = (int64_t)spec_monotonic.tv_sec;
241  timestamp_monotonic->nsec = (int64_t)spec_monotonic.tv_nsec;
242 }
243 
248 void get_mac_addr(uint8_t mac_address[MAC_ADDRESS_LENGTH]) {
249  FILE* fp = fopen(MAC_ADDRESS_FILE, "r");
250 
251  unsigned int temp;
252  fscanf(fp, "%x", &temp);
253  mac_address[0] = (uint8_t)temp;
254  for (int i = 1; i < MAC_ADDRESS_LENGTH; i++) {
255  fscanf(fp, ":%x", &temp);
256  mac_address[i] = (uint8_t)temp;
257  }
258  fclose(fp);
259 }
int write_status(struct rl_status *status)
Definition: util.c:129
int is_low_current(int index)
Definition: util.c:67
#define FAILURE
Definition: types.h:78
#define SUCCESS
Definition: types.h:75
#define MAC_ADDRESS_LENGTH
MAC address length in bytes.
Definition: util.h:40
#define MAC_ADDRESS_FILE
File to read MAC address.
Definition: types.h:86
struct rl_status status
Current status of RocketLogger.
Definition: rl_server.c:71
int64_t sec
Seconds in UNIX time (UTC)
Definition: util.h:47
void rl_log(rl_log_type type, const char *format,...)
Definition: log.c:39
int read_status(struct rl_status *status)
Definition: util.c:95
int read_file_value(char filename[])
Definition: util.c:202
#define SHMEM_STATUS_KEY
Key for status shared memory (used for creation)
Definition: types.h:94
int ceil_div(int n, int d)
Definition: util.c:164
#define I2L_INDEX
Definition: types.h:234
#define NUM_CHANNELS
Maximum number of RocketLogger channels.
Definition: types.h:104
#define CHANNEL_ENABLED
Channel sampling enabled.
Definition: types.h:223
void get_mac_addr(uint8_t mac_address[MAC_ADDRESS_LENGTH])
Definition: util.c:248
#define SHMEM_PERMISSIONS
Permissions for shared memory.
Definition: types.h:98
#define I1H_INDEX
Definition: types.h:229
void create_time_stamp(struct time_stamp *timestamp_realtime, struct time_stamp *timestamp_monotonic)
Definition: util.c:223
void sig_handler(int signo)
Definition: util.c:178
int is_current(int index)
Definition: util.c:53
Error.
Definition: types.h:200
int64_t nsec
Nanoseconds.
Definition: util.h:49
#define I2H_INDEX
Definition: types.h:233
Not sampling.
Definition: types.h:150
rl_sampling sampling
Sampling state.
Definition: types.h:287
int count_channels(int channels[NUM_CHANNELS])
Definition: util.c:80
#define I1L_INDEX
Definition: types.h:230