psrdada-sys 0.2.0

Bindgen wrappers for psrdada
Documentation
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
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
#include "dada_hdu.h"
#include "dada_def.h"
#include "ascii_header.h"

#include <stdlib.h>
#include <string.h>
#include <assert.h>

// #define _DEBUG

/*! Create a new DADA Header plus Data Unit */
dada_hdu_t* dada_hdu_create (multilog_t* log)
{
  dada_hdu_t* hdu = malloc (sizeof(dada_hdu_t));
  assert (hdu != 0);

  hdu -> log = log;
  hdu -> data_block = 0;
  hdu -> header_block = 0;

  hdu -> header = 0;
  hdu -> header_size = 0;

  dada_hdu_set_key( hdu, DADA_DEFAULT_BLOCK_KEY );
  return hdu;
}

/*! Set the key of the DADA Header plus Data Unit */
void dada_hdu_set_key (dada_hdu_t* hdu, key_t key)
{
  hdu -> data_block_key = key;
  hdu -> header_block_key = key + 1;
}

/*! Destroy a DADA primary write client main loop */
void dada_hdu_destroy (dada_hdu_t* hdu)
{
  assert (hdu != 0);

  if (hdu->data_block)
    dada_hdu_disconnect (hdu);

  free (hdu);
}

/*! Connect the DADA Header plus Data Unit */
int dada_hdu_connect (dada_hdu_t* hdu)
{
  ipcbuf_t ipcbuf_init = IPCBUF_INIT;
  ipcio_t ipcio_init = IPCIO_INIT;

  assert (hdu != 0);

  if (hdu->data_block) {
    fprintf (stderr, "dada_hdu_connect: already connected\n");
    return -1;
  }

  hdu->header_block = malloc (sizeof(ipcbuf_t));
  assert (hdu->header_block != 0);
  *(hdu->header_block) = ipcbuf_init;

  hdu->data_block = malloc (sizeof(ipcio_t));
  assert (hdu->data_block != 0);
  *(hdu->data_block) = ipcio_init;

  /* connect to the shared memory */
#ifdef _DEBUG
  fprintf (stderr, "dada_hdu_connect: ipcbuf_connect (header_block, %x)\n", hdu->header_block_key);
#endif
  if (ipcbuf_connect (hdu->header_block, hdu->header_block_key) < 0)
  {
    multilog (hdu->log, LOG_ERR, "Failed to connect to Header Block\n");
    free (hdu->header_block);
    hdu->header_block = 0;
    free (hdu->data_block);
    hdu->data_block = 0;
    return -1;
  }

#ifdef _DEBUG
  fprintf (stderr, "dada_hdu_connect: ipcio_connect (data_block, %x)\n", hdu->data_block_key);
#endif
  if (ipcio_connect (hdu->data_block, hdu->data_block_key) < 0)
  {
    multilog (hdu->log, LOG_ERR, "Failed to connect to Data Block\n");
    free (hdu->header_block);
    hdu->header_block = 0;
    free (hdu->data_block);
    hdu->data_block = 0;
    return -1;
  }

  return 0;
}


/*! Disconnect the DADA Header plus Data Unit */
int dada_hdu_disconnect (dada_hdu_t* hdu)
{
  int status = 0;

  assert (hdu != 0);

  if (!hdu->data_block) {
    fprintf (stderr, "dada_hdu_disconnect: not connected\n");
    return -1;
  }

  if (ipcio_disconnect (hdu->data_block) < 0) {
    multilog (hdu->log, LOG_ERR, "Failed to disconnect from Data Block\n");
    status = -1;
  }

  if (ipcbuf_disconnect (hdu->header_block) < 0) {
    multilog (hdu->log, LOG_ERR, "Failed to disconnect from Header Block\n");
    status = -1;
  }

  free (hdu->header_block);
  hdu->header_block = 0;
  free (hdu->data_block);
  hdu->data_block = 0;
  
  return status;
}

/*! Lock DADA Header plus Data Unit designated reader */
int dada_hdu_lock_read (dada_hdu_t* hdu)
{
#ifdef _DEBUG
  fprintf (stderr, "dada_hdu_lock_read\n");
#endif
  assert (hdu != 0);

  if (!hdu->data_block) {
    fprintf (stderr, "dada_hdu_disconnect: not connected\n");
    return -1;
  }

#ifdef _DEBUG
  fprintf (stderr, "dada_hdu_lock_read: ipcbuf_lock_read(hdu->header_block)\n");
#endif
  if (ipcbuf_lock_read (hdu->header_block) < 0) {
    multilog (hdu->log, LOG_ERR, "Could not lock Header Block for reading\n");
    return -1;
  }

#ifdef _DEBUG
  fprintf (stderr, "dada_hdu_lock_read: ipcio_open(hdu->data_block)\n");
#endif
  if (ipcio_open (hdu->data_block, 'R') < 0) {
    multilog (hdu->log, LOG_ERR, "Could not lock Data Block for reading\n");
    return -1;
  }

  return 0;
}

/*! Unlock DADA Header plus Data Unit designated reader */
int dada_hdu_unlock_read (dada_hdu_t* hdu)
{
  assert (hdu != 0);

  if (!hdu->data_block)
  {
    fprintf (stderr, "dada_hdu_unlock_read: not connected\n");
    return -1;
  }

#ifdef _DEBUG
  fprintf (stderr, "dada_hdu_unlock_read: ipcio_close (hdu->data_block)\n");
#endif
  if (ipcio_close (hdu->data_block) < 0)
  {
    multilog (hdu->log, LOG_ERR, "Could not unlock Data Block read\n");
    return -1;
  }

  if (hdu->header)
  {
#ifdef _DEBUG
    fprintf (stderr, "dada_hdu_unlock_read: free (hdu->header)\n");
#endif
    free (hdu->header);
    hdu->header = 0;
    if (ipcbuf_is_reader (hdu->header_block))
    {
#ifdef _DEBUG
      fprintf (stderr, "dada_hdu_unlock_read: ipcbuf_mark_cleared (hdu->header_block)\n");
#endif
      ipcbuf_mark_cleared (hdu->header_block);
    }
  }

#ifdef _DEBUG
  fprintf (stderr, "dada_hdu_unlock_read: ipcbuf_unlock_read (hdu->header_block)\n");
#endif
  if (ipcbuf_unlock_read (hdu->header_block) < 0) {
    multilog (hdu->log, LOG_ERR,"Could not unlock Header Block read\n");
    return -1;
  }

  return 0;
}

/*! Lock DADA Header plus Data Unit designated writer */
int dada_hdu_lock_write (dada_hdu_t* hdu)
{
   return dada_hdu_lock_write_spec (hdu, 'W');
}

/*! Lock DADA Header plus Data Unit designated writer with specified mode */
int dada_hdu_lock_write_spec (dada_hdu_t* hdu, char writemode)
{
#ifdef _DEBUG
  fprintf (stderr, "dada_hdu_lock_write_spec()\n");
#endif
  assert (hdu != 0);

  if (!hdu->data_block) {
    fprintf (stderr, "dada_hdu_lock_write_spec: not connected\n");
    return -1;
  }

  if (ipcbuf_lock_write (hdu->header_block) < 0) {
    multilog (hdu->log, LOG_ERR, "Could not lock Header Block for writing\n");
    return -1;
  }

#ifdef _DEBUG
  fprintf (stderr, "dada_hdu_lock_write_spec: ipcio_open()\n");
#endif
  if (ipcio_open (hdu->data_block, writemode) < 0) {
    multilog (hdu->log, LOG_ERR, "Could not lock Data Block for writing\n");
    return -1;
  }

  return 0;
}

/*! Unlock DADA Header plus Data Unit designated writer */
int dada_hdu_unlock_write (dada_hdu_t* hdu)
{
  assert (hdu != 0);

  if (!hdu->data_block) {
    fprintf (stderr, "dada_hdu_disconnect: not connected\n");
    return -1;
  }

  if (ipcio_is_open (hdu->data_block))
  {
#ifdef _DEBUG
    fprintf (stderr, "dada_hdu_unlock_write: ipcio_close (hdu->data_block)\n");
#endif
    if (ipcio_close (hdu->data_block) < 0) {
      multilog (hdu->log, LOG_ERR, "Could not unlock Data Block write\n");
      return -1;
    }
  }

#ifdef _DEBUG
    fprintf (stderr, "dada_hdu_unlock_write: ipcbuf_unlock_write (hdu->header_block)\n");
#endif
  if (ipcbuf_unlock_write (hdu->header_block) < 0) {
    multilog (hdu->log, LOG_ERR, "Could not unlock Header Block write\n");
    return -1;
  }

  return 0;
}

/*! Lock DADA Header plus Data Unit designated reader */
int dada_hdu_open_view (dada_hdu_t* hdu)
{
  assert (hdu != 0);

  if (!hdu->data_block)
  {
    fprintf (stderr, "dada_hdu_open_view: not connected\n");
    return -1;
  }

  if (ipcio_open (hdu->data_block, 'r') < 0)
  {
    multilog (hdu->log, LOG_ERR, "Could not open Data Block for viewing\n");
    return -1;
  }

  return 0;
}

/*! Unlock DADA Header plus Data Unit designated reader */
int dada_hdu_close_view (dada_hdu_t* hdu)
{
  assert (hdu != 0);

  if (!hdu->data_block)
  {
    fprintf (stderr, "dada_hdu_close_view: not connected\n");
    return -1;
  }

  if (ipcio_close (hdu->data_block) < 0)
  {
    multilog (hdu->log, LOG_ERR, "Could not close Data Block view\n");
    return -1;
  }

  return 0;
}

int dada_hdu_open (dada_hdu_t* hdu)
{
  /* pointer to the status and error logging facility */
  multilog_t* log = 0;

  /* The header from the ring buffer */
  char* header = 0;
  uint64_t header_size = 0;

  /* header size, as defined by HDR_SIZE attribute */
  uint64_t hdr_size = 0;

  assert (hdu != 0);
  assert (hdu->header == 0);

  log = hdu->log;

  while (!header_size)
  {
    /* Wait for the next valid header sub-block */
    header = ipcbuf_get_next_read (hdu->header_block, &header_size);

    if (!header) {
      multilog (log, LOG_ERR, "Could not get next header\n");
      return -1;
    }

    if (!header_size)
    {
      if (ipcbuf_is_reader (hdu->header_block))
        ipcbuf_mark_cleared (hdu->header_block);

      if (ipcbuf_eod (hdu->header_block))
      {
        multilog (log, LOG_INFO, "End of data on header block\n");
        if (ipcbuf_is_reader (hdu->header_block))
          ipcbuf_reset (hdu->header_block);
      }
      else
      {
        multilog (log, LOG_ERR, "Empty header block\n");
        return -1;
      }
    }
  }

  header_size = ipcbuf_get_bufsz (hdu->header_block);

  /* Check that header is of advertised size */
  if (ascii_header_get (header, "HDR_SIZE", "%"PRIu64, &hdr_size) != 1) {
    multilog (log, LOG_ERR, "Header with no HDR_SIZE. Setting to %"PRIu64"\n",
              header_size);
    hdr_size = header_size;
    if (ascii_header_set (header, "HDR_SIZE", "%"PRIu64, hdr_size) < 0) {
      multilog (log, LOG_ERR, "Error setting HDR_SIZE\n");
      return -1;
    }
  }

  if (hdr_size < header_size)
    header_size = hdr_size;

  else if (hdr_size > header_size) {
    multilog (log, LOG_ERR, "HDR_SIZE=%"PRIu64
              " > Header Block size=%"PRIu64"\n", hdr_size, header_size);
    multilog (log, LOG_DEBUG, "ASCII header dump\n%s", header);
    return -1;
  }

  /* Duplicate the header */
  if (header_size > hdu->header_size)
  {
    hdu->header = realloc (hdu->header, header_size);
    assert (hdu->header != 0);
    hdu->header_size = header_size;
  }

  memcpy (hdu->header, header, header_size);
  return 0;
}

// return the base addresses and sizes of the datablock
char ** dada_hdu_db_addresses(dada_hdu_t * hdu, uint64_t * nbufs, uint64_t * bufsz)
{
  ipcbuf_t *db = (ipcbuf_t *) hdu->data_block;
  *nbufs = ipcbuf_get_nbufs (db);
  *bufsz = ipcbuf_get_bufsz (db);

  return db->buffer;
}

// return the base addresses and sizes of the datablock
char ** dada_hdu_hb_addresses(dada_hdu_t * hdu, uint64_t * nbufs, uint64_t * bufsz)
{
  ipcbuf_t *hb = hdu->header_block;
  *nbufs = ipcbuf_get_nbufs (hb);
  *bufsz = ipcbuf_get_bufsz (hb);

  return hb->buffer;
}