RocketLogger  1.1.6
ambient.c
Go to the documentation of this file.
1 
32 #include "sensor/sensor.h"
33 
34 #include "ambient.h"
35 
43 void ambient_store_data(FILE* ambient_file,
44  struct time_stamp* timestamp_realtime,
45  struct time_stamp* timestamp_monotonic,
46  struct rl_conf* conf) {
47 
48  // store timestamp
49  fwrite(timestamp_realtime, sizeof(struct time_stamp), 1, ambient_file);
50  fwrite(timestamp_monotonic, sizeof(struct time_stamp), 1, ambient_file);
51 
52  // FETCH VALUES //
53  int32_t sensor_data[conf->ambient.sensor_count];
54 
55  int ch = 0;
56  int mutli_channel_read = -1;
57  for (int i = 0; i < SENSOR_REGISTRY_SIZE; i++) {
58  // only read registered sensors
59  if (conf->ambient.available_sensors[i] > 0) {
60  // read multi-channel sensor data only once
61  if (sensor_registry[i].identifier != mutli_channel_read) {
62  sensor_registry[i].read(sensor_registry[i].identifier);
63  mutli_channel_read = sensor_registry[i].identifier;
64  }
65  sensor_data[ch] = sensor_registry[i].getValue(
66  sensor_registry[i].identifier, sensor_registry[i].channel);
67  ch++;
68  }
69  }
70 
71  // WRITE VALUES //
72  fwrite(sensor_data, sizeof(int32_t), conf->ambient.sensor_count,
73  ambient_file);
74 }
75 
76 // FILE HEADER //
77 
78 void ambient_set_file_name(struct rl_conf* conf) {
79 
80  // determine new file name
81  char ambient_file_name[MAX_PATH_LENGTH] = {0};
82  strcpy(ambient_file_name, conf->file_name);
83 
84  // search for last .
85  char target = '.';
86  char* file_ending = ambient_file_name;
87  while (strchr(file_ending, target) != NULL) {
88  file_ending = strchr(file_ending, target);
89  file_ending++; // Increment file_ending, otherwise we'll find target at
90  // the same location
91  }
92  file_ending--;
93 
94  // add file ending
95  char ambient_file_ending[MAX_PATH_LENGTH] = "-ambient";
96  strcat(ambient_file_ending, file_ending);
97  strcpy(file_ending, ambient_file_ending);
98  strcpy(conf->ambient.file_name, ambient_file_name);
99 }
100 
102  struct rl_conf* conf) {
103 
104  // number channels
105  uint16_t channel_count = conf->ambient.sensor_count;
106 
107  // number binary channels
108  uint16_t channel_bin_count = 0;
109 
110  // comment length
111  uint32_t comment_length = RL_FILE_COMMENT_ALIGNMENT_BYTES;
112 
113  // timestamps
114  struct time_stamp time_real;
115  struct time_stamp time_monotonic;
116  create_time_stamp(&time_real, &time_monotonic);
117 
118  // lead_in setup
119  lead_in->magic = RL_FILE_MAGIC;
120  lead_in->file_version = RL_FILE_VERSION;
121  lead_in->header_length =
122  sizeof(struct rl_file_lead_in) + comment_length +
123  (channel_count + channel_bin_count) * sizeof(struct rl_file_channel);
125  lead_in->data_block_count = 0; // needs to be updated
126  lead_in->sample_count = 0; // needs to be updated
128  get_mac_addr(lead_in->mac_address);
129  lead_in->start_time = time_real;
130  lead_in->comment_length = comment_length;
131  lead_in->channel_bin_count = channel_bin_count;
132  lead_in->channel_count = channel_count;
133 }
134 
135 void ambient_setup_channels(struct rl_file_header* file_header,
136  struct rl_conf* conf) {
137 
138  int total_channel_count = file_header->lead_in.channel_bin_count +
139  file_header->lead_in.channel_count;
140 
141  // reset channels
142  memset(file_header->channel, 0,
143  total_channel_count * sizeof(struct rl_file_channel));
144 
145  // write channels
146  int ch = 0;
147  for (int i = 0; i < SENSOR_REGISTRY_SIZE; i++) {
148  if (conf->ambient.available_sensors[i] > 0) {
149 
150  file_header->channel[ch].unit = sensor_registry[i].unit;
151  file_header->channel[ch].channel_scale = sensor_registry[i].scale;
152  file_header->channel[ch].valid_data_channel = NO_VALID_DATA;
153  file_header->channel[ch].data_size = 4;
154  strcpy(file_header->channel[ch].name, sensor_registry[i].name);
155  ch++;
156  }
157  }
158 }
159 
160 void ambient_setup_header(struct rl_file_header* file_header,
161  struct rl_conf* conf, char* comment) {
162 
163  // comment
164  if (comment == NULL) {
165  file_header->comment = "";
166  } else {
167  file_header->comment = comment;
168  }
169 
170  // channels
171  ambient_setup_channels(file_header, conf);
172 }
int available_sensors[AMBIENT_MAX_SENSOR_COUNT]
Definition: types.h:215
uint16_t data_size
Datum size in bytes (for voltage and current)
Definition: rl_file.h:150
uint16_t header_length
Total size of the header in bytes.
Definition: rl_file.h:108
struct time_stamp start_time
Start time of the measurement in UNIX time, UTC.
Definition: rl_file.h:126
struct rl_file_lead_in lead_in
File header lead in (constant size)
Definition: rl_file.h:165
int identifier
Definition: sensor.h:55
void ambient_set_file_name(struct rl_conf *conf)
Definition: ambient.c:78
#define SENSOR_REGISTRY_SIZE
Number of sensor registred.
Definition: sensor.h:46
#define AMBIENT_DATA_BLOCK_SIZE
Definition: ambient.h:43
void ambient_setup_lead_in(struct rl_file_lead_in *lead_in, struct rl_conf *conf)
Definition: ambient.c:101
char name[SENSOR_NAME_LENGTH]
Definition: sensor.h:54
struct rl_file_channel * channel
Channels definitions (binary and normal)
Definition: rl_file.h:171
#define MAX_PATH_LENGTH
Maximum path length in characters.
Definition: types.h:102
int32_t(* getValue)(int, int)
Definition: sensor.h:62
uint64_t sample_count
Total sample count.
Definition: rl_file.h:117
void ambient_setup_header(struct rl_file_header *file_header, struct rl_conf *conf, char *comment)
Definition: ambient.c:160
uint16_t channel_bin_count
Binary channel count.
Definition: rl_file.h:132
struct rl_ambient ambient
Ambient conf.
Definition: types.h:277
#define RL_FILE_MAGIC
File header magic number (ascii RLD)
Definition: rl_file.h:60
uint16_t file_version
File version number.
Definition: rl_file.h:105
int sensor_count
Definition: types.h:214
rl_unit unit
Channel unit.
Definition: rl_file.h:144
Definition: types.h:247
const struct rl_sensor sensor_registry[SENSOR_REGISTRY_SIZE]
Definition: sensor.c:53
void get_mac_addr(uint8_t mac_address[MAC_ADDRESS_LENGTH])
Definition: util.c:248
char * comment
Comment field.
Definition: rl_file.h:168
int32_t scale
Definition: sensor.h:58
char file_name[MAX_PATH_LENGTH]
Data file name.
Definition: types.h:275
void create_time_stamp(struct time_stamp *timestamp_realtime, struct time_stamp *timestamp_monotonic)
Definition: util.c:223
#define NO_VALID_DATA
No additional range valid information available.
Definition: rl_file.h:70
uint16_t channel_count
Analog channel count.
Definition: rl_file.h:135
uint32_t comment_length
Comment length.
Definition: rl_file.h:129
void ambient_store_data(FILE *ambient_file, struct time_stamp *timestamp_realtime, struct time_stamp *timestamp_monotonic, struct rl_conf *conf)
Definition: ambient.c:43
uint32_t magic
File magic constant.
Definition: rl_file.h:102
uint32_t data_block_size
Size of the data blocks in the file in rows.
Definition: rl_file.h:111
void ambient_setup_channels(struct rl_file_header *file_header, struct rl_conf *conf)
Definition: ambient.c:135
char name[RL_FILE_CHANNEL_NAME_LENGTH]
Channel name/description.
Definition: rl_file.h:156
uint16_t sample_rate
Sampling rate of the measurement.
Definition: rl_file.h:120
char file_name[MAX_PATH_LENGTH]
Definition: types.h:216
int(* read)(int)
Definition: sensor.h:61
rl_unit unit
Definition: sensor.h:57
#define RL_FILE_VERSION
File format version of current implementation.
Definition: rl_file.h:63
int32_t channel_scale
Channel scale (in power of ten, for voltage and current)
Definition: rl_file.h:147
uint16_t valid_data_channel
Link to channel valid data (for low-range current channels)
Definition: rl_file.h:153
uint32_t data_block_count
Number of data blocks stored in the file.
Definition: rl_file.h:114
#define RL_FILE_COMMENT_ALIGNMENT_BYTES
Comment alignment in bytes.
Definition: rl_file.h:76
#define AMBIENT_SAMPLING_RATE
Definition: ambient.h:42
uint8_t mac_address[MAC_ADDRESS_LENGTH]
Instrument ID (mac address)
Definition: rl_file.h:123