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