psrdada-sys 0.3.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
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
#include "dada_pwc.h"
#include "dada_def.h"

#include "ascii_header.h"
#include "tmutil.h"

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

/* #define _DEBUG 1 */

/*! Parse a string and return the state */
dada_pwc_state_t dada_pwc_string_to_state (const char* key)
{
  if (!strcasecmp (key, "pending"))
    return dada_pwc_pending;
  if (!strcasecmp (key, "idle"))
    return dada_pwc_idle;
  if (!strcasecmp (key, "prepared"))
    return dada_pwc_prepared;
  if (!strcasecmp (key, "clocking"))
    return dada_pwc_clocking;
  if (!strcasecmp (key, "recording"))
    return dada_pwc_recording;
  if (!strcasecmp (key, "soft_error"))
    return dada_pwc_soft_error;
  if (!strcasecmp (key, "hard_error"))
    return dada_pwc_hard_error;
  if (!strcasecmp (key, "fatal_error"))
    return dada_pwc_fatal_error;

  return dada_pwc_undefined;
}

/*! Return the string corresponding to the state */
const char* dada_pwc_state_to_string (dada_pwc_state_t state)
{
  switch (state) {

  case dada_pwc_pending:
    return "pending";

  case dada_pwc_idle:
    return "idle";

  case dada_pwc_prepared:
    return "prepared";

  case dada_pwc_clocking:
    return "clocking";

  case dada_pwc_recording:
    return "recording";

  case dada_pwc_soft_error:
    return "soft_error";

  case dada_pwc_hard_error:
    return "hard_error";

  case dada_pwc_fatal_error:
    return "fatal_error";

  default:
    return "undefined";

  }
}

int dada_pwc_cmd_state (void* context, FILE* fptr, char* args)
{
  dada_pwc_t* primary = (dada_pwc_t*) context;
  fprintf (fptr, "%s\n", dada_pwc_state_to_string( primary->state ));
  return 0;
}

int dada_pwc_command_set_byte_count (dada_pwc_t* primary, FILE* output,
                                     dada_pwc_command_t* command)
{

  if (!primary->convert_to_bytes)
  {
#ifdef _DEBUG
    fprintf (stderr, "dada_pwc_command_set_byte_count not converting\n");
#endif
    command->byte_count = 0;
    return 0;
  }

  if (!command->utc)
  {
#ifdef _DEBUG
    fprintf (stderr, "dada_pwc_command_set_byte_count no UTC\n");
#endif
    command->byte_count = 0;
    return 0;
  }

  if (!primary->utc_start)
  {
    fprintf (output, "UTC of first time sample unknown\n");
    return -1;
  }

  if (!primary->bytes_per_second)
  {
    fprintf (output, "bytes per second not known\n");
    return -1;
  }

  if (command->utc < primary->utc_start)
  {
#ifdef _DEBUG
    fprintf (stderr, "requested UTC precedes UTC of first time sample\n");
#endif

    command->utc = 0;
    command->byte_count = 0;
    return 0;
  }

  /* the number of second between start and requested UTC of command */
  command->byte_count = command->utc - primary->utc_start;

  /* the byte count at which to execute the command */
  command->byte_count *= primary->bytes_per_second;

  /* round the byte_count to the nearest resolution boundary */
  if (primary->byte_resolution > 1)
  {
    /* round down to the start of the packet by default */
    uint64_t packets = command->byte_count / primary->byte_resolution;
    command->byte_count = packets * primary->byte_resolution;

    /* round to the end of the packet if stopping */
    if (command->code == dada_pwc_record_stop ||
	command->code == dada_pwc_stop)
      command->byte_count += primary->byte_resolution;
  }

  return 0;
}
  
/*! Set the command state */
int dada_pwc_command_set (dada_pwc_t* primary, FILE* output,
                          dada_pwc_command_t command)
{
  int ret = 0;

  if (!primary)
    return -1;
  
  pthread_mutex_lock (&(primary->mutex));

  while (primary->command.code != dada_pwc_no_command)
    pthread_cond_wait(&(primary->cond), &(primary->mutex));

  switch (command.code) {

  case dada_pwc_header:
    if (primary->state != dada_pwc_idle) {
      fprintf (output, "Cannot set header when not IDLE"
               " (state=%s)\n", dada_pwc_state_to_string (primary->state));
      ret = -1;
    }
    break;

  case dada_pwc_clock:
    if (primary->state != dada_pwc_prepared) {
      fprintf (output, "Cannot start clocking when not PREPARED"
               " (state=%s)\n", dada_pwc_state_to_string (primary->state));
      ret = -1;
    }
    break;

  case dada_pwc_record_start:
    if (primary->state != dada_pwc_clocking) {
      fprintf (output, "Cannot record start when not CLOCKING"
               " (state=%s)\n", dada_pwc_state_to_string (primary->state));
      ret = -1;
    }
    break;

  case dada_pwc_record_stop:
    if (primary->state != dada_pwc_recording) {
      fprintf (output, "Cannot record stop when not RECORDING"
               " (state=%s)\n", dada_pwc_state_to_string (primary->state));
      ret = -1;
    }
    break;

  case dada_pwc_start:
    if (primary->state != dada_pwc_prepared) {
      fprintf (output, "Cannot start when not PREPARED"
               " (state=%s)\n", dada_pwc_state_to_string (primary->state));
      ret = -1;
    }
    break;

  case dada_pwc_stop:
    // always allow stop ... WvS - 6 Oct 2008
    break;

  case dada_pwc_set_utc_start:
    if (primary->state != dada_pwc_clocking &&
        primary->state != dada_pwc_recording) {
      fprintf (output, "Cannot set_utc_start when not CLOCKING or RECORDING"
               " (state=%s)\n", dada_pwc_state_to_string (primary->state));
      ret = -1;
    }
    break;

  case dada_pwc_reset:
    if (primary->state != dada_pwc_soft_error &&
        primary->state != dada_pwc_hard_error) {
      fprintf (output, "Cannot reset when not SOFT ERROR or HARD ERROR"
               " (state=%s)\n", dada_pwc_state_to_string (primary->state));
      ret = -1;
    }
    break;

  case dada_pwc_exit:
    if (primary->state != dada_pwc_idle) {
      fprintf (output, "Cannot exit when not IDLE (state=%s)\n", 
               dada_pwc_state_to_string (primary->state));
      ret = -1;
    }
    break;
  }

  if (ret == 0) {

    primary->command = command;
    pthread_cond_signal (&(primary->cond));

  }

  pthread_mutex_unlock (&(primary->mutex));

  return ret;
}

int dada_pwc_parse_bytes_per_second (dada_pwc_t* primary,
                                     FILE* fptr, const char* header)
{
  unsigned npol;  /* number of polarizations */
  unsigned nbit;  /* nubmer of bits per sample */
  unsigned ndim;  /* number of dimensions */
  unsigned nant;  /* number of antenna */
  unsigned nchan; /* number of channels */

  uint64_t bits_per_second = 0;
  int resolution = 1;

  double sampling_interval;

  if (ascii_header_get (header, "NPOL", "%d", &npol) < 0)
  {
    fprintf (fptr, "failed to parse NPOL - assuming 2\n");
    npol = 2;
  }

  if (ascii_header_get (header, "NBIT", "%d", &nbit) < 0)
  {
    fprintf (fptr, "failed to parse NBIT - assuming 8\n");
    nbit = 8;
  }

  if (ascii_header_get (header, "NDIM", "%d", &ndim) < 0)
  {
    fprintf (fptr, "failed to parse NDIM - assuming 1\n");
    ndim = 1;
  }

  if (ascii_header_get (header, "NCHAN", "%d", &nchan) < 0)
  {
    fprintf (fptr, "failed to parse NCHAN - assuming 1\n");
    nchan = 1;
  }

  if (ascii_header_get (header, "NANT", "%d", &nant) < 0)
  {
    fprintf (fptr, "failed to parse NANT - assuming 1\n");
    nant = 1;
  }

  if (ascii_header_get (header, "RESOLUTION", "%d", &resolution) < 0)
  {
    //fprintf (fptr, "failed to parse RESOLUTION - assuming 1\n");
    resolution = 1;
  }

  primary->byte_resolution = resolution;

  primary->bits_per_sample = nbit * npol * ndim;

  if (ascii_header_get (header, "TSAMP", "%lf", &sampling_interval) < 0)
  {
    fprintf (fptr, "failed to parse TSAMP\n");
    primary->bytes_per_second = 0;
    return -1;
  }

  /* IMPORTANT: TSAMP is the sampling period in microseconds */
  uint64_t bits_per_megasample = (uint64_t) 1e6 * primary->bits_per_sample * nchan * nant;
  bits_per_second = (uint64_t) (bits_per_megasample / (double) sampling_interval);

  primary->bytes_per_second = bits_per_second / 8;

  return 0;
}

int dada_pwc_cmd_header (void* context, FILE* fptr, char* args)
{
  dada_pwc_command_t command = DADA_PWC_COMMAND_INIT;
  dada_pwc_t* primary = (dada_pwc_t*) context;
  char* hdr = args;
  size_t length = 0;

  if (!args) {
    fprintf (fptr, "no header specified\n");
    return -1;
  }

  length = strlen (args);

  if (length > primary->header_size) {
    fprintf (fptr, "header too large (max %d bytes)\n", primary->header_size);
    return -1;
  }

  /* replace \ with new line */
  while ( (hdr = strchr(hdr, '\\')) != 0 )
    *hdr = '\n';

  if (dada_pwc_parse_bytes_per_second (primary, fptr, args) < 0)
    return -1;

  strcpy (primary->header, args);

  /* add a new line to the end */
  if (primary->header[length-1] != '\n' && length+1 < primary->header_size) {
    primary->header[length] = '\n';
    primary->header[length+1] = '\0';
  }

  /* set the number of bytes per second in the header */
  ascii_header_set (primary->header, "BYTES_PER_SECOND", "%"PRIu64,
                    primary->bytes_per_second);

  command.code = dada_pwc_header;
  command.header = primary->header;

  if (dada_pwc_command_set (primary, fptr, command) < 0)
    return -1;

  return 0;
}

uint64_t dada_pwc_parse_duration (dada_pwc_t* primary,
                                  FILE* fptr, const char* args)
{
  unsigned hh, mm, ss;
  uint64_t byte_count; 

  if (sscanf (args, "%u:%u:%u", &hh, &mm, &ss) == 3) {
   
    if (!primary->bytes_per_second) {
      fprintf (fptr, "bytes per second not known\n");
      return 0;
    }

    if (mm > 59) {
      fprintf (fptr, "invalid minutes = %u\n", mm);
      return 0;
    }

    if (ss > 59) {
      fprintf (fptr, "invalid seconds = %u\n", ss);
      return 0;
    }

    byte_count = hh;
    byte_count *= 3600;
    byte_count += 60 * mm;
    byte_count += ss;
    byte_count *= primary->bytes_per_second;

    return byte_count;

  }

  if (sscanf (args, "%"PRIu64"", &byte_count) == 1) {

    if (!primary->bits_per_sample) {
      fprintf (fptr, "bits per sample not known\n");
      return 0;
    }

    byte_count *= primary->bits_per_sample;
    byte_count /= 8;

    return byte_count;

  }

  fprintf (fptr, "could not parse duration from '%s'\n", args);

  return 0;
}

int dada_pwc_cmd_duration (void* context, FILE* fptr, char* args)
{
  dada_pwc_t* primary = (dada_pwc_t*) context;
  uint64_t byte_count = 0;

  int status = 0;

  if (!args) {
    fprintf (fptr, "please specify duration\n");
    return -1;
  }

  byte_count = dada_pwc_parse_duration (primary, fptr, args);

  if (!byte_count)
    return -1;

  pthread_mutex_lock (&(primary->mutex));

  while (primary->command.code != dada_pwc_no_command)
    pthread_cond_wait (&(primary->cond), &(primary->mutex));

  if (primary->state == dada_pwc_recording) {
    fprintf (stderr, "Cannot set DURATION while recording\n");
    status = -1;
  }
  else
    primary->command.byte_count = byte_count;

  pthread_mutex_unlock (&(primary->mutex));

  return status;
}


time_t dada_pwc_parse_time (FILE* fptr, char* args)  
{
  time_t utc = 0;

  if (!args)
    return utc;

  utc = str2utctime (args);
  if (utc == (time_t)-1) {
    fprintf (fptr, "Could not parse start time from '%s'\n", args);
    return -1;
  }

  return utc;
}

int dada_pwc_cmd_clock (void* context, FILE* fptr, char* args)
{
  dada_pwc_t* primary = (dada_pwc_t*) context;

  dada_pwc_command_t command = DADA_PWC_COMMAND_INIT;
  command.code = dada_pwc_clock;
  command.utc = 0;

  return dada_pwc_command_set (primary, fptr, command);
}

int dada_pwc_cmd_record_start (void* context, FILE* fptr, char* args)
{

  dada_pwc_t* primary = (dada_pwc_t*) context;

  dada_pwc_command_t command = DADA_PWC_COMMAND_INIT;
  command.code = dada_pwc_record_start;
  command.utc = dada_pwc_parse_time (fptr, args);

  if (!command.utc) {
    fprintf (fptr, "rec_start requires a valid utc argument\n");
    return -1;
  }

  if (dada_pwc_command_set_byte_count (primary, fptr, &command) < 0)
    return -1;

  return dada_pwc_command_set (primary, fptr, command);
}

int dada_pwc_cmd_record_stop (void* context, FILE* fptr, char* args)
{
  dada_pwc_t* primary = (dada_pwc_t*) context;

  dada_pwc_command_t command = DADA_PWC_COMMAND_INIT;
  command.code = dada_pwc_record_stop;
  command.utc = dada_pwc_parse_time (fptr, args);

  if (!command.utc) {
    fprintf (fptr, "rec_stop requires a valid utc argument\n");
    return -1;
  }

  if (dada_pwc_command_set_byte_count (primary, fptr, &command) < 0)
    return -1;

  return dada_pwc_command_set (primary, fptr, command);
}

int dada_pwc_cmd_start (void* context, FILE* fptr, char* args)
{
  dada_pwc_t* primary = (dada_pwc_t*) context;

  dada_pwc_command_t command = DADA_PWC_COMMAND_INIT;
  command.code = dada_pwc_start;
  command.utc = dada_pwc_parse_time (fptr, args);
  command.byte_count = primary->command.byte_count;

  return dada_pwc_command_set (primary, fptr, command);
}

int dada_pwc_cmd_stop (void* context, FILE* fptr, char* args)
{
  dada_pwc_t* primary = (dada_pwc_t*) context;

  dada_pwc_command_t command = DADA_PWC_COMMAND_INIT;
  command.code = dada_pwc_stop;
  command.utc = dada_pwc_parse_time (fptr, args);

  if (dada_pwc_command_set_byte_count (primary, fptr, &command) < 0)
    return -1;

  return dada_pwc_command_set (primary, fptr, command);
}


int dada_pwc_cmd_set_utc_start (void* context, FILE* fptr, char* args)
{
  dada_pwc_t* primary = (dada_pwc_t*) context;

  dada_pwc_command_t command = DADA_PWC_COMMAND_INIT;
  command.code = dada_pwc_set_utc_start;
  command.utc = dada_pwc_parse_time (fptr, args);

  /* Since the utc of the first time sample is now known, update the
   * primary's utc_start time to reflect this 
   * n.b. this will result in a byte_count of 0 for this command */
  primary->utc_start = command.utc;

  if (dada_pwc_command_set_byte_count (primary, fptr, &command) < 0)
    return -1;

  return dada_pwc_command_set (primary, fptr, command);
        
}

int dada_pwc_cmd_reset (void* context, FILE* fptr, char* args)
{
  dada_pwc_t* primary = (dada_pwc_t*) context;

  dada_pwc_command_t command = DADA_PWC_COMMAND_INIT;
  command.code = dada_pwc_reset;
  command.utc = 0;
                                                                                
  return dada_pwc_command_set (primary, fptr, command);

}

int dada_pwc_cmd_quit (void* context, FILE* fptr, char* args)
{
  dada_pwc_t* primary = (dada_pwc_t*) context;

  dada_pwc_command_t command = DADA_PWC_COMMAND_INIT;
  command.code = dada_pwc_exit;
  command.utc = 0;

  primary->quit = 1;

  return dada_pwc_command_set (primary, fptr, command);
                                                                                                                                                      
}


/*! Create a new DADA primary write client connection */
dada_pwc_t* dada_pwc_create ()
{
  dada_pwc_t* primary = (dada_pwc_t*) malloc (sizeof(dada_pwc_t));
  assert (primary != 0);

  primary -> state = dada_pwc_idle;
  primary -> command.code = dada_pwc_no_command;
  primary -> convert_to_bytes = 1;

  primary -> bytes_per_second = 0;
  primary -> bits_per_sample = 0;
  primary -> byte_resolution = 0;
  primary -> utc_start = 0;

  /* default header size */
  primary -> header_size = DADA_DEFAULT_HEADER_SIZE;
  primary -> header = (char *) malloc (primary->header_size);
  assert (primary->header != 0);

  /* default command port */
  primary -> port = DADA_DEFAULT_PWC_PORT;

#ifdef _DEBUG
  fprintf (stderr, "dada_pwc on port %d\n", primary->port);
#endif

  /* for multi-threaded use of primary */
  pthread_mutex_init(&(primary->mutex), NULL);
  pthread_cond_init (&(primary->cond), NULL);

  /* command parser */
  primary -> parser = command_parse_create ();

  command_parse_add (primary->parser, dada_pwc_cmd_state, primary,
                     "state", "get the current state", NULL);

  command_parse_add (primary->parser, dada_pwc_cmd_header, primary,
                     "header", "set the primary header", NULL);

  command_parse_add (primary->parser, dada_pwc_cmd_duration, primary,
                     "duration", "set the duration of next recording", NULL);

  command_parse_add (primary->parser, dada_pwc_cmd_start, primary,
                     "start", "enter the recording state", NULL);

  command_parse_add (primary->parser, dada_pwc_cmd_stop, primary,
                     "stop", "enter the idle state", NULL);

  command_parse_add (primary->parser, dada_pwc_cmd_clock, primary,
                     "clock", "enter the clocking state", NULL);

  command_parse_add (primary->parser, dada_pwc_cmd_record_start, primary,
                     "rec_start", "enter the recording state", NULL);

  command_parse_add (primary->parser, dada_pwc_cmd_record_stop, primary,
                     "rec_stop", "enter the clocking state", NULL);

  command_parse_add (primary->parser, dada_pwc_cmd_set_utc_start, primary,
                     "set_utc_start", "set the UTC_START header parameter", NULL);

  command_parse_add (primary->parser, dada_pwc_cmd_quit, primary,
                     "quit", "quit", NULL);

  command_parse_add (primary->parser, dada_pwc_cmd_reset, primary,
                     "reset", "reset error state to idle", NULL);

  primary -> server = 0;
  primary -> log = 0;
  primary -> quit = 0;

  return primary;
}

int dada_pwc_set_header_size (dada_pwc_t* primary, unsigned header_size)
{
  if (!primary)
    return -1;

  pthread_mutex_lock (&(primary->mutex));

  primary -> header_size = header_size;
  primary -> header = (char *) realloc (primary->header, header_size);
  assert (primary->header != 0);

  pthread_mutex_unlock (&(primary->mutex));

  return 0;
}


/*! */
int dada_pwc_serve (dada_pwc_t* primary)
{
  if (!primary)
    return -1;

  if (primary->server) {
    fprintf (stderr, "dada_pwc_serve: server already launched");
    return -1;
  }

  primary -> server = command_parse_server_create (primary -> parser);

  command_parse_server_set_welcome (primary -> server,
                                    "DADA primary write client command");

  /* open the command/control port */
  return command_parse_serve (primary->server, primary->port);
}

/*! Destroy a DADA primary write client connection */
int dada_pwc_destroy (dada_pwc_t* primary)
{
  return 0;
}

/*! Primary write client should exit when this is true */
int dada_pwc_quit (dada_pwc_t* primary)
{
  return primary->quit;
}

/*! Check to see if a command has arrived */
int dada_pwc_command_check (dada_pwc_t* primary)
{
  if (!primary)
    return -1;

  if (primary->command.code == dada_pwc_no_command)
    return 0;

  return 1;
}

/*! Get the next command from the connection; wait until command received */
dada_pwc_command_t dada_pwc_command_get (dada_pwc_t* primary)
{
  dada_pwc_command_t command = DADA_PWC_COMMAND_INIT;

  if (!primary) {
    command.code = -1;
    return command;
  }

  pthread_mutex_lock (&(primary->mutex));

  while (primary->command.code == dada_pwc_no_command)
    pthread_cond_wait(&(primary->cond), &(primary->mutex));

  command = primary->command;
  primary->command.code = dada_pwc_no_command;
  primary->command.byte_count = 0;

  pthread_mutex_unlock (&(primary->mutex));

  return command;
}

/*! \param new_state the state that the primary write client has entered
    \param utc the UTC time at which this state was entered */
int dada_pwc_set_state (dada_pwc_t* primary, int new_state, time_t utc)
{
  if (!primary)
    return -1;

  switch (primary->state) {

  case dada_pwc_idle:
    if (new_state != dada_pwc_prepared &&
        new_state != dada_pwc_soft_error &&
        new_state != dada_pwc_hard_error &&
        new_state != dada_pwc_fatal_error) 
    {
      fprintf (stderr, "IDLE can change only to PREPARED\n");
      return -1;
    }
    break;

  case dada_pwc_prepared:
    if (new_state != dada_pwc_idle &&
        new_state != dada_pwc_clocking &&
        new_state != dada_pwc_recording &&
        new_state != dada_pwc_soft_error &&
        new_state != dada_pwc_hard_error &&
        new_state != dada_pwc_fatal_error) 
    {
      fprintf (stderr, "PREPARED can change only to CLOCKING or RECORDING\n");
      return -1;
    }
    primary->utc_start = utc;
    break;

  case dada_pwc_clocking:
    if (new_state != dada_pwc_recording &&
        new_state != dada_pwc_idle &&
        new_state != dada_pwc_soft_error &&
        new_state != dada_pwc_hard_error &&
        new_state != dada_pwc_fatal_error) 
    {
      fprintf (stderr, "CLOCKING can change only to RECORDING or IDLE\n");
      return -1;
    }
    break;

  case dada_pwc_recording:
    if (new_state != dada_pwc_clocking &&
        new_state != dada_pwc_idle &&
        new_state != dada_pwc_soft_error &&
        new_state != dada_pwc_hard_error &&
        new_state != dada_pwc_fatal_error) 
    {
      fprintf (stderr, "RECORDING can change only to CLOCKING or IDLE\n");
      return -1;
    }
    break;

  case dada_pwc_soft_error:
    if (new_state != dada_pwc_idle) 
    {
      fprintf (stderr, "SOFT ERROR state can only change to IDLE\n");
      return -1;
    }
    break;

  case dada_pwc_hard_error:
    if (new_state != dada_pwc_idle) 
    {
      fprintf (stderr, "HARD ERROR state can only change to IDLE\n");
      return -1;
    }
    break;

  case dada_pwc_fatal_error:
    fprintf (stderr, "FATAL ERROR state cannot be changed\n");
    return -1;
    break;
    
  default:
    fprintf (stderr, "current state is UNDEFINED, new state=%d\n", new_state);
    return -1;

  }

  pthread_mutex_lock (&(primary->mutex));

  primary->state = new_state;

  if (primary->log)
    multilog (primary->log, LOG_INFO, 
              "STATE = %s\n", dada_pwc_state_to_string (new_state));
  
  pthread_cond_signal (&(primary->cond));
  pthread_mutex_unlock (&(primary->mutex));

  return 0;
}

const char * dada_pwc_cmd_code_string(int command_code)
{
  switch (command_code)
  {
    case dada_pwc_no_command:
      return "no_command";
                                                                                                                                                                                                                    
    case dada_pwc_header:
      return "header";
                                                                                                                                                                                                                    
    case dada_pwc_clock:
      return "clock";
                                                                                                                                                                                                                    
    case dada_pwc_record_start:
      return "record_start";
                                                                                                                                                                                                                    
    case dada_pwc_record_stop:
      return "record_stop";
                                                                                                                                                                                                                    
    case dada_pwc_start:
      return "start";
                                                                                                                                                                                                                    
    case dada_pwc_stop:
      return "stop";
                                                                                                                                                                                                                    
    case dada_pwc_set_utc_start:
      return "set_utc_start";
                                                                                                                                                                                                                    
    case dada_pwc_reset:
      return "reset";
                                                                                                                                                                                                                    
    case dada_pwc_exit:
      return "exit";
                                                                                                                                                                                                                    
    default:
      return "unknown";
  }
                                                                                                                                                                                                                    
}