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