aboutsummaryrefslogtreecommitdiffstats
path: root/op25/gr-op25_repeater/lib/dstar_tx_sb_impl.cc
blob: 7c58dd218d4ff2d6217761cf960783afad6042af (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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/* -*- c++ -*- */
/* 
 * DSTAR Encoder (C) Copyright 2017 Max H. Parke KA1RBI
 * 
 * This file is part of OP25
 * 
 * This is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3, or (at your option)
 * any later version.
 * 
 * This software 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 General Public License
 * along with this software; see the file COPYING.  If not, write to
 * the Free Software Foundation, Inc., 51 Franklin Street,
 * Boston, MA 02110-1301, USA.
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <gnuradio/io_signature.h>
#include "mbelib.h"
#include "p25p2_vf.h"
#include "dstar_tx_sb_impl.h"
// #include "dstar_const.h"
#include <op25_imbe_frame.h>

#include <vector>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <assert.h>

#if 0
static inline void print_result(char title[], const uint8_t r[], int len) {
	uint8_t buf[256];
	for (int i=0; i<len; i++){
		buf[i] = r[i] + '0';
	}
	buf[len] = 0;
	printf("%s: %s\n", title, buf);
}
#endif

static const uint8_t FS[24] = { 1,0,1,0,1,0,1,0,1,0,1,1,0,1,0,0,0,1,1,0,1,0,0,0 };
static const uint8_t FS_DUMMY[24] = { 0,0,0,0,1,1,1,1,0,0,0,0,1,1,1,1,0,0,0,0,1,1,1,1 };

namespace gr {
  namespace op25_repeater {

    dstar_tx_sb::sptr
    dstar_tx_sb::make(int verbose_flag, const char * config_file, bool fullrate_mode)
    {
      return gnuradio::get_initial_sptr
        (new dstar_tx_sb_impl(verbose_flag, config_file, fullrate_mode));
    }

//////////////////////////////////////////////////////////////////////////

static const int MIN_IN = 1;
static const int MAX_IN = 1;

static const int MIN_OUT = 1;
static const int MAX_OUT = 1;

    /*
     * The private constructor
     */
    dstar_tx_sb_impl::dstar_tx_sb_impl(int verbose_flag, const char * config_file, bool fullrate_mode)
      : gr::block("dstar_tx_sb",
              gr::io_signature::make (MIN_IN, MAX_IN, sizeof(short)),
              gr::io_signature::make (MIN_OUT, MAX_OUT, sizeof(char))),
              d_verbose_flag(verbose_flag),
              d_config_file(config_file),
              d_frame_counter(0)
    {
      set_output_multiple(96);
      d_encoder.set_dstar_mode();
      config();
    }

    /*
     * Our virtual destructor.
     */
    dstar_tx_sb_impl::~dstar_tx_sb_impl()
    {
    }

void
dstar_tx_sb_impl::config()
{
	FILE * fp1 = fopen(d_config_file, "r");
	char line[256];
	char * cp;
	if (!fp1) {
		fprintf(stderr, "dstar_tx_sb_impl:config: failed to open %s\n", d_config_file);
		return;
	}
	for (;;) {
		cp = fgets(line, sizeof(line) - 2, fp1);
		if (!cp) break;
		if (line[0] == '#') continue;
#if 0
		if (memcmp(line, "ft=", 3) == 0)
			sscanf(&line[3], "%d", &d_ft);
#endif
	}
	fclose(fp1);
}

void
dstar_tx_sb_impl::forecast(int nof_output_items, gr_vector_int &nof_input_items_reqd)
{
   // each 96-bit output frame contains one voice code word=160 samples input
   const size_t nof_inputs = nof_input_items_reqd.size();
   const int nof_vcw = nof_output_items / 96.0;
   const int nof_samples_reqd = nof_vcw * 160;
   std::fill(&nof_input_items_reqd[0], &nof_input_items_reqd[nof_inputs], nof_samples_reqd);
}

int 
dstar_tx_sb_impl::general_work (int noutput_items,
			       gr_vector_int &ninput_items,
			       gr_vector_const_void_star &input_items,
			       gr_vector_void_star &output_items)
{

  int nconsumed = 0;
  int16_t *in;
  in = (int16_t *) input_items[0];
  uint8_t *out = reinterpret_cast<uint8_t*>(output_items[0]);
  int nframes=0;

  for (int n=0;n < (noutput_items/96);n++) {
    // need (at least) one voice codeword worth of samples
    if (ninput_items[0] - nconsumed < 160)
      break;
    d_encoder.encode(in, out);
    if (d_frame_counter == 0)
        memcpy(out+72, FS, 24);
    else
        memcpy(out+72, FS_DUMMY, 24);
    d_frame_counter += 1;
    d_frame_counter = (d_frame_counter + 1) % 21;
    in += 160;
    nconsumed += 160;
    nframes += 1;
    out += 96;
  }

  // Tell runtime system how many input items we consumed on
  // each input stream.

  if (nconsumed)
    consume_each(nconsumed);

  // Tell runtime system how many output items we produced.
  return (nframes * 96);
}

  } /* namespace op25_repeater */
} /* namespace gr */