aboutsummaryrefslogtreecommitdiff
path: root/src/ofdm_get_test_bits.c
blob: d5a7210d9e0fa9f9dcc4183ab85000700f468e95 (plain)
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
/*---------------------------------------------------------------------------*\

  FILE........: ofdm_get_test_bits.c
  AUTHOR......: David Rowe
  DATE CREATED: Mar 2018

  Generate input for the OFDM modem in either coded or uncoded mode.

\*---------------------------------------------------------------------------*/

/*
  Copyright (C) 2018 David Rowe

  All rights reserved.

  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU Lesser General Public License version 2.1, as
  published by the Free Software Foundation.  This program is
  distributed in the hope that it will be useful, but WITHOUT ANY
  WARRANTY; without even the implied warranty of MERCHANTABILITY or
  FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public
  License for more details.

  You should have received a copy of the GNU Lesser General Public License
  along with this program; if not, see <http://www.gnu.org/licenses/>.
*/

#define OPTPARSE_IMPLEMENTATION
#define OPTPARSE_API static
#include <assert.h>
#include <errno.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "codec2_ofdm.h"
#include "interldpc.h"
#include "ldpc_codes.h"
#include "ofdm_internal.h"
#include "optparse.h"
#include "varicode.h"

#define IS_DIR_SEPARATOR(c) ((c) == '/')

static const char *progname;

void opt_help() {
  fprintf(stderr, "\nUsage: %s [options]\n\n", progname);
  fprintf(stderr, "  --out     filename  Name of OutputOneCharPerBitFile\n");
  fprintf(stderr,
          "  --frames  n         Number of frames to output (default 10)\n");
  fprintf(stderr, "  --length  n         Frame length in bits (default 238)\n");
  fprintf(stderr,
          "  --bcb               Insert burst control byte at the start of "
          "each frame (FSK_LDPC testing)\n");
  fprintf(
      stderr,
      "  --verbose           Output variable assigned values to stderr\n\n");

  exit(-1);
}

int main(int argc, char *argv[]) {
  FILE *fout;
  char *fout_name = NULL;
  int opt, verbose, n;
  int Nframes, output_specified, bcb_en;
  int Ndatabitsperpacket;
  uint8_t burst_control;

  char *pn = argv[0] + strlen(argv[0]);

  while (pn != argv[0] && !IS_DIR_SEPARATOR(pn[-1])) --pn;

  progname = pn;

  /* Turn off stream buffering */

  setvbuf(stdout, NULL, _IONBF, BUFSIZ);

  fout = stdout;
  output_specified = 0;
  Nframes = 10;
  Ndatabitsperpacket = 224;
  verbose = 0;
  bcb_en = 0;

  struct optparse options;

  struct optparse_long longopts[] = {
      {"bcb", 'b', OPTPARSE_NONE},        {"out", 'o', OPTPARSE_REQUIRED},
      {"frames", 'n', OPTPARSE_REQUIRED}, {"length", 'l', OPTPARSE_REQUIRED},
      {"verbose", 'v', OPTPARSE_NONE},    {0, 0, 0}};

  optparse_init(&options, argv);

  while ((opt = optparse_long(&options, longopts, NULL)) != -1) {
    switch (opt) {
      case '?':
        opt_help();
      case 'b':
        bcb_en = 1;
        break;
      case 'o':
        fout_name = options.optarg;
        output_specified = 1;
        break;
      case 'n':
        Nframes = atoi(options.optarg);
        break;
      case 'l':
        Ndatabitsperpacket = atoi(options.optarg);
        break;
      case 'v':
        verbose = 1;
    }
  }

  /* Print remaining arguments to give user a hint */

  char *arg;

  while ((arg = optparse_arg(&options))) fprintf(stderr, "%s\n", arg);

  if (output_specified) {
    if ((fout = fopen(fout_name, "wb")) == NULL) {
      fprintf(stderr, "Error opening output bit file: %s\n", fout_name);
      exit(-1);
    }
  }

  if (verbose)
    fprintf(stderr, "Nframes: %d Ndatabitsperframe: %d bcb: %d\n", Nframes,
            Ndatabitsperpacket, bcb_en);

  uint8_t data_bits[Ndatabitsperpacket];
  ofdm_generate_payload_data_bits(data_bits, Ndatabitsperpacket);

  burst_control = 1;
  for (n = 0; n < Nframes; n++) {
    if (bcb_en) fwrite(&burst_control, 1, 1, fout);
    fwrite(data_bits, sizeof(char), Ndatabitsperpacket, fout);
    burst_control = 0;
  }
  if (bcb_en) {
    // dummy end frame just to signal end of burst
    burst_control = 2;
    fwrite(&burst_control, 1, 1, fout);
    memset(data_bits, 0, Ndatabitsperpacket);
    fwrite(data_bits, sizeof(char), Ndatabitsperpacket, fout);
  }

  if (output_specified) fclose(fout);

  return 0;
}