framelog.c 4.08 KB
Newer Older
1
2
#include "logger.h"
#include "logger_defs.h"
Cedric Roux's avatar
Cedric Roux committed
3
4
#include "handler.h"
#include "database.h"
Cedric Roux's avatar
Cedric Roux committed
5
#include "filter/filter.h"
Cedric Roux's avatar
Cedric Roux committed
6
7
8
9
10
11
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <math.h>

struct framelog {
12
  struct logger common;
Cedric Roux's avatar
Cedric Roux committed
13
14
15
16
17
18
  void *database;
  int subframe_arg;
  int buffer_arg;
  float *x;
  float *buffer;
  int blength;
19
20
21
22
23
  int skip_delay;       /* one frame over 'skip_delay' is truly processed
                         * 0 to disable (process all data)
                         */
  int skip_current;     /* internal data for the skip mechanism */
  int skip_on;          /* internal data for the skip mechanism */
Cedric Roux's avatar
Cedric Roux committed
24
25
26
27
28
29
30
31
32
33
34
};

static void _event(void *p, event e)
{
  struct framelog *l = p;
  int i;
  int subframe;
  void *buffer;
  int bsize;
  int nsamples;

Cedric Roux's avatar
Cedric Roux committed
35
36
37
  if (l->common.filter != NULL && filter_eval(l->common.filter, e) == 0)
    return;

Cedric Roux's avatar
Cedric Roux committed
38
39
40
41
  subframe = e.e[l->subframe_arg].i;
  buffer = e.e[l->buffer_arg].b;
  bsize = e.e[l->buffer_arg].bsize;

42
43
44
45
46
47
48
49
50
51
52
53
  if (l->skip_delay != 0) {
    if (subframe == 0) {
      l->skip_current++;
      if (l->skip_current >= l->skip_delay) {
        l->skip_on = 0;
        l->skip_current = 0;
      } else
        l->skip_on = 1;
    }
  }
  if (l->skip_on) return;

Cedric Roux's avatar
Cedric Roux committed
54
55
56
57
58
59
  nsamples = bsize / (2*sizeof(int16_t));

  if (l->blength != nsamples * 10) {
    l->blength = nsamples * 10;
    free(l->x);
    free(l->buffer);
60
    l->x = calloc(l->blength, sizeof(float));
Cedric Roux's avatar
Cedric Roux committed
61
    if (l->x == NULL) abort();
62
    l->buffer = calloc(l->blength, sizeof(float));
Cedric Roux's avatar
Cedric Roux committed
63
64
65
66
67
    if (l->buffer == NULL) abort();
    /* update 'x' */
    for (i = 0; i < l->blength; i++)
      l->x[i] = i;
    /* update 'length' of views */
68
69
    for (i = 0; i < l->common.vsize; i++)
      l->common.v[i]->set(l->common.v[i], "length", l->blength);
Cedric Roux's avatar
Cedric Roux committed
70
71
72
73
74
75
76
77
78
79
  }

  /* TODO: compute the LOGs in the plotter (too much useless computations) */
  for (i = 0; i < nsamples; i++) {
    int I = ((int16_t *)buffer)[i*2];
    int Q = ((int16_t *)buffer)[i*2+1];
    l->buffer[subframe * nsamples + i] = 10*log10(1.0+(float)(I*I+Q*Q));
  }

  if (subframe == 9)
80
81
    for (i = 0; i < l->common.vsize; i++)
      l->common.v[i]->append(l->common.v[i], l->x, l->buffer, l->blength);
Cedric Roux's avatar
Cedric Roux committed
82
83
}

84
logger *new_framelog(event_handler *h, void *database,
Cedric Roux's avatar
Cedric Roux committed
85
86
87
88
89
90
91
92
93
    char *event_name, char *subframe_varname, char *buffer_varname)
{
  struct framelog *ret;
  int event_id;
  database_event_format f;
  int i;

  ret = calloc(1, sizeof(struct framelog)); if (ret == NULL) abort();

94
95
  ret->common.event_name = strdup(event_name);
  if (ret->common.event_name == NULL) abort();
Cedric Roux's avatar
Cedric Roux committed
96
97
98
99
  ret->database = database;

  event_id = event_id_from_name(database, event_name);

100
  ret->common.handler_id = register_handler_function(h,event_id,_event,ret);
Cedric Roux's avatar
Cedric Roux committed
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133

  f = get_format(database, event_id);

  /* look for subframe and buffer args */
  ret->subframe_arg = -1;
  ret->buffer_arg = -1;
  for (i = 0; i < f.count; i++) {
    if (!strcmp(f.name[i], subframe_varname)) ret->subframe_arg = i;
    if (!strcmp(f.name[i], buffer_varname)) ret->buffer_arg = i;
  }
  if (ret->subframe_arg == -1) {
    printf("%s:%d: subframe argument '%s' not found in event '%s'\n",
        __FILE__, __LINE__, subframe_varname, event_name);
    abort();
  }
  if (ret->buffer_arg == -1) {
    printf("%s:%d: buffer argument '%s' not found in event '%s'\n",
        __FILE__, __LINE__, buffer_varname, event_name);
    abort();
  }
  if (strcmp(f.type[ret->subframe_arg], "int") != 0) {
    printf("%s:%d: argument '%s' has wrong type (should be 'int')\n",
        __FILE__, __LINE__, subframe_varname);
    abort();
  }
  if (strcmp(f.type[ret->buffer_arg], "buffer") != 0) {
    printf("%s:%d: argument '%s' has wrong type (should be 'buffer')\n",
        __FILE__, __LINE__, buffer_varname);
    abort();
  }

  return ret;
}
134
135
136
137
138
139
140
141
142
143
144
145
146

/****************************************************************************/
/*                             public functions                             */
/****************************************************************************/

void framelog_set_skip(logger *_this, int skip_delay)
{
  struct framelog *l = _this;
  /* TODO: protect with a lock? */
  l->skip_delay = skip_delay;
  l->skip_current = 0;
  l->skip_on = 0;
}