forked from radarsat1/mapperRec
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackend_binary.c
103 lines (81 loc) · 2.31 KB
/
backend_binary.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#include <stdlib.h>
#include <stdio.h>
#include <sys/param.h>
#include <string.h>
#include <sys/time.h>
#include <lo/lo.h>
#include "backend_binary.h"
#include "command.h"
backend_binary_options_t backend_binary_options;
static FILE* output_file = 0;
extern int num_msgs;
extern mapper_timetag_t start;
void binary_defaults()
{
memset(&backend_binary_options, 0, sizeof(backend_binary_options));
backend_binary_options.file_path = 0;
}
int binary_start()
{
mapper_timetag_now(&start);
if (!backend_binary_options.file_path) {
printf("No output filename specified.\n");
return 1;
}
output_file = fopen(backend_binary_options.file_path, "ab");
if (!output_file) {
printf("Error opening file `%s' for writing.\n",
backend_binary_options.file_path);
return 1;
}
return 0;
}
void binary_stop()
{
if (output_file) {
fclose(output_file);
output_file = 0;
}
}
int binary_poll()
{
return 0;
}
/* TODO: Bundle messages together that happen in the same call to poll(). */
void binary_write_value(mapper_signal sig, const void *v, mapper_timetag_t *tt)
{
char str[1024], *path = str;
lo_timetag now;
lo_timetag_now(&now);
if (!tt || !tt->sec)
fwrite(&now, sizeof(lo_timetag), 1, output_file);
else
fwrite(tt, sizeof(lo_timetag), 1, output_file);
mapper_device dev = mapper_signal_device(sig);
snprintf(path, 1024, "%s/%s", mapper_device_name(dev),
mapper_signal_name(sig));
int len = strlen(path), wrote=len, i;
len = (len / 4 + 1) * 4;
int wlen = lo_htoo32(len);
fwrite(&wlen, 4, 1, output_file);
fwrite(path, wrote, 1, output_file);
while (wrote < len) {
fwrite("", 1, 1, output_file);
wrote ++;
}
char type = mapper_signal_type(sig);
int length = mapper_signal_length(sig);
fwrite(&type, 1, 1, output_file);
wlen = lo_htoo32(length);
fwrite(&wlen, 4, 1, output_file);
if (type == 'i' || type == 'f') {
for (i = 0; i < length; i++) {
int wi = lo_htoo32(((uint32_t*)v)[i]);
fwrite(&wi, 4, 1, output_file);
}
}
fflush(output_file);
printf("\rRecording: %f seconds, %d messages.",
mapper_timetag_difference(*tt, start), num_msgs++);
fflush(stdout);
}