p25p2 decoder integration

This commit is contained in:
Max 2014-08-23 16:41:33 -04:00
parent 53953a7c7d
commit 468bbc47b0
18 changed files with 654 additions and 819 deletions

View File

@ -2,7 +2,7 @@
# Copyright 2008-2011 Steve Glass
#
# Copyright 2011, 2012, 2013 KA1RBI
# Copyright 2011, 2012, 2013, 2014 Max H. Parke KA1RBI
#
# Copyright 2003,2004,2005,2006 Free Software Foundation, Inc.
# (from radiorausch)
@ -269,29 +269,19 @@ class p25_rx_block (stdgui2.std_top_block):
do_imbe = 1
do_output = 1
do_msgq = 1
self.sink_s = op25_repeater.p25_frame_assembler(self.options.wireshark_host, udp_port, self.options.verbosity, do_imbe, do_output, do_msgq, self.rx_q)
if self.options.vocoder and self.options.phase2_tdma:
print 'simultaneous phase I and II not supported in this version'
sys.exit(1)
do_audio_output = True
self.p25decoder = op25_repeater.p25_frame_assembler(self.options.wireshark_host, udp_port, self.options.verbosity, do_imbe, do_output, do_msgq, self.rx_q, do_audio_output, self.options.phase2_tdma)
if self.options.vocoder or self.options.phase2_tdma:
self.audio_s2f = blocks.short_to_float()
self.audio_scaler = blocks.multiply_const_ff(1 / 32768.0)
self.audio_output = audio.sink(8000, self.options.audio_output, True)
self.connect(self.audio_s2f, self.audio_scaler, self.audio_output)
if self.options.vocoder:
self.sink_imbe = op25_repeater.vocoder(0, 0, 0, '', 0, 0)
self.connect(self.sink_imbe, self.audio_s2f)
self.connect(self.p25decoder, self.audio_s2f, self.audio_scaler, self.audio_output)
else:
self.sink_imbe = blocks.null_sink(gr.sizeof_char)
if self.options.phase2_tdma:
slotid = 0
do_msgq = True
debug = self.options.verbosity
self.p2decoder = op25_repeater.p25p2_frame(debug, slotid, do_msgq, self.rx_q)
self.connect(self.slicer, self.p2decoder, self.audio_s2f)
self.sink_imbe = blocks.null_sink(gr.sizeof_short)
self.connect(self.p25decoder, self.sink_imbe)
self.tdma_state = False
self.xor_cache = {}
self.connect(self.buffer, self.slicer, self.sink_s, self.sink_imbe)
self.connect(self.buffer, self.slicer, self.p25decoder)
if self.baseband_input:
gain = self.options.gain
else:
@ -737,14 +727,14 @@ class p25_rx_block (stdgui2.std_top_block):
return # already in desired state
self.tdma_state = set_tdma
if set_tdma:
self.p2decoder.set_slotid(params['tdma'])
self.p25decoder.set_slotid(params['tdma'])
hash = '%x%x%x' % (params['nac'], params['sysid'], params['wacn'])
if hash not in self.xor_cache:
sreg = lfsr.p25p2_lfsr(params['nac'], params['sysid'], params['wacn'])
self.xor_cache[hash] = ''
for c in sreg.xorsyms:
self.xor_cache[hash] += chr(c)
self.p2decoder.set_xormask(self.xor_cache[hash])
self.p25decoder.set_xormask(self.xor_cache[hash])
sps = self.basic_rate / 6000
else:
sps = self.basic_rate / 4800
@ -857,7 +847,7 @@ class p25_rx_block (stdgui2.std_top_block):
notebook_sel = self.notebook.GetSelection()
if notebook_sel == 0 or notebook_sel == 4: # spectrum or demod symbols
self.connect_demods()
elif notebook_sel == 1 or notebook_sel == 2 or notebook_sel == 5:
elif notebook_sel == 1 or notebook_sel == 2 or notebook_sel == 6:
self.connect_fsk4_demod()
elif notebook_sel == 3: # constellation
self.connect_psk_demod()

View File

@ -22,7 +22,18 @@ class app_top_block(gr.top_block):
slotid = options.tdma_slotid
msgq = gr.msg_queue(2)
do_msgq = False
FRAMER = op25_repeater.p25p2_frame(0, slotid, do_msgq, msgq)
wireshark_host = ''
udp_port = 0
verbosity = 100
do_imbe = 1
do_output = 1
do_msgq = 0
rx_q = gr.msg_queue(1)
do_audio_output = 1
phase2_tdma = 1
FRAMER = op25_repeater.p25_frame_assembler(wireshark_host, udp_port, verbosity, do_imbe, do_output, do_msgq, rx_q, do_audio_output, phase2_tdma)
FRAMER.set_xormask(xor_mask)
S2F = blocks.short_to_float()

View File

@ -130,7 +130,7 @@ class my_top_block(gr.top_block):
do_output = True # enable block's output stream
do_msgq = False # msgq output not yet implemented
msgq = gr.msg_queue(2)
DECODER = op25_repeater.p25_frame_assembler(hostname, port, debug, do_imbe, do_output, do_msgq, msgq)
DECODER = op25_repeater.p25_frame_assembler(hostname, port, debug, do_imbe, do_output, do_msgq, msgq, False, False)
OUT = blocks.file_sink(gr.sizeof_char, options.output_file)

View File

@ -20,6 +20,5 @@ install(FILES
op25_repeater_vocoder.xml
op25_repeater_gardner_costas_cc.xml
op25_repeater_p25_frame_assembler.xml
op25_repeater_fsk4_slicer_fb.xml
op25_repeater_p25p2_frame.xml DESTINATION share/gnuradio/grc/blocks
op25_repeater_fsk4_slicer_fb.xml DESTINATION share/gnuradio/grc/blocks
)

View File

@ -25,6 +25,5 @@ install(FILES
vocoder.h
gardner_costas_cc.h
p25_frame_assembler.h
fsk4_slicer_fb.h
p25p2_frame.h DESTINATION include/op25_repeater
fsk4_slicer_fb.h DESTINATION include/op25_repeater
)

View File

@ -47,7 +47,9 @@ namespace gr {
* class. op25_repeater::p25_frame_assembler::make is the public interface for
* creating new instances.
*/
static sptr make(const char* udp_host, int port, int debug, bool do_imbe, bool do_output, bool do_msgq, gr::msg_queue::sptr queue);
static sptr make(const char* udp_host, int port, int debug, bool do_imbe, bool do_output, bool do_msgq, gr::msg_queue::sptr queue, bool do_audio_output, bool do_phase2_tdma);
virtual void set_xormask(const char*p) {}
virtual void set_slotid(int slotid) {}
};
} // namespace op25_repeater

View File

@ -28,13 +28,15 @@ list(APPEND op25_repeater_sources
vocoder_impl.cc
gardner_costas_cc_impl.cc
p25_frame_assembler_impl.cc
fsk4_slicer_fb_impl.cc
p25p2_frame_impl.cc )
fsk4_slicer_fb_impl.cc )
list(APPEND op25_repeater_sources
bch.cc
rs.cc
p25_framer.cc
p25p1_fdma.cc
p25p1_voice_encode.cc
p25p1_voice_decode.cc
p25p2_framer.cc
p25p2_isch.cc
p25p2_duid.cc

View File

@ -33,22 +33,34 @@
#include <errno.h>
#include <vector>
#include <sys/time.h>
#include "bch.h"
#include "op25_imbe_frame.h"
#include "p25_frame.h"
#include "p25_framer.h"
#include "rs.h"
static const int64_t TIMEOUT_THRESHOLD = 1000000;
namespace gr {
namespace op25_repeater {
void p25_frame_assembler_impl::p25p2_queue_msg(int duid)
{
static const char wbuf[2] = {0xff, 0xff}; // dummy NAC
if (!d_do_msgq)
return;
if (d_msg_queue->full_p())
return;
gr::message::sptr msg = gr::message::make_from_string(std::string(wbuf, 2), duid, 0, 0);
d_msg_queue->insert_tail(msg);
}
void p25_frame_assembler_impl::set_xormask(const char*p) {
p2tdma.set_xormask(p);
}
void p25_frame_assembler_impl::set_slotid(int slotid) {
p2tdma.set_slotid(slotid);
}
p25_frame_assembler::sptr
p25_frame_assembler::make(const char* udp_host, int port, int debug, bool do_imbe, bool do_output, bool do_msgq, gr::msg_queue::sptr queue)
p25_frame_assembler::make(const char* udp_host, int port, int debug, bool do_imbe, bool do_output, bool do_msgq, gr::msg_queue::sptr queue, bool do_audio_output, bool do_phase2_tdma)
{
return gnuradio::get_initial_sptr
(new p25_frame_assembler_impl(udp_host, port, debug, do_imbe, do_output, do_msgq, queue));
(new p25_frame_assembler_impl(udp_host, port, debug, do_imbe, do_output, do_msgq, queue, do_audio_output, do_phase2_tdma));
}
/*
@ -60,183 +72,34 @@ namespace gr {
*/
p25_frame_assembler_impl::~p25_frame_assembler_impl()
{
if (write_sock > 0)
close(write_sock);
delete framer;
}
static const int MIN_IN = 1; // mininum number of input streams
static const int MAX_IN = 1; // maximum number of input streams
static uint16_t crc16(uint8_t buf[], int len) {
uint32_t poly = (1<<12) + (1<<5) + (1<<0);
uint32_t crc = 0;
for(int i=0; i<len; i++) {
uint8_t bits = buf[i];
for (int j=0; j<8; j++) {
uint8_t bit = (bits >> (7-j)) & 1;
crc = ((crc << 1) | bit) & 0x1ffff;
if (crc & 0x10000)
crc = (crc & 0xffff) ^ poly;
}
}
crc = crc ^ 0xffff;
return crc & 0xffff;
}
/* translated from p25craft.py Michael Ossmann <mike@ossmann.com> */
static uint32_t crc32(uint8_t buf[], int len) { /* length is nr. of bits */
uint32_t g = 0x04c11db7;
uint64_t crc = 0;
for (int i = 0; i < len; i++) {
crc <<= 1;
int b = ( buf [i / 8] >> (7 - (i % 8)) ) & 1;
if (((crc >> 32) ^ b) & 1)
crc ^= g;
}
crc = (crc & 0xffffffff) ^ 0xffffffff;
return crc;
}
/* find_min is from wireshark/plugins/p25/packet-p25cai.c */
/* Copyright 2008, Michael Ossmann <mike@ossmann.com> */
/* return the index of the lowest value in a list */
static int
find_min(uint8_t list[], int len)
{
int min = list[0];
int index = 0;
int unique = 1;
int i;
for (i = 1; i < len; i++) {
if (list[i] < min) {
min = list[i];
index = i;
unique = 1;
} else if (list[i] == min) {
unique = 0;
}
}
/* return -1 if a minimum can't be found */
if (!unique)
return -1;
return index;
}
/* count_bits is from wireshark/plugins/p25/packet-p25cai.c */
/* Copyright 2008, Michael Ossmann <mike@ossmann.com> */
/* count the number of 1 bits in an int */
static int
count_bits(unsigned int n)
{
int i = 0;
for (i = 0; n != 0; i++)
n &= n - 1;
return i;
}
/* adapted from wireshark/plugins/p25/packet-p25cai.c */
/* Copyright 2008, Michael Ossmann <mike@ossmann.com> */
/* deinterleave and trellis1_2 decoding */
/* buf is assumed to be a buffer of 12 bytes */
static int
block_deinterleave(bit_vector& bv, unsigned int start, uint8_t* buf)
{
static const uint16_t deinterleave_tb[] = {
0, 1, 2, 3, 52, 53, 54, 55, 100,101,102,103, 148,149,150,151,
4, 5, 6, 7, 56, 57, 58, 59, 104,105,106,107, 152,153,154,155,
8, 9, 10, 11, 60, 61, 62, 63, 108,109,110,111, 156,157,158,159,
12, 13, 14, 15, 64, 65, 66, 67, 112,113,114,115, 160,161,162,163,
16, 17, 18, 19, 68, 69, 70, 71, 116,117,118,119, 164,165,166,167,
20, 21, 22, 23, 72, 73, 74, 75, 120,121,122,123, 168,169,170,171,
24, 25, 26, 27, 76, 77, 78, 79, 124,125,126,127, 172,173,174,175,
28, 29, 30, 31, 80, 81, 82, 83, 128,129,130,131, 176,177,178,179,
32, 33, 34, 35, 84, 85, 86, 87, 132,133,134,135, 180,181,182,183,
36, 37, 38, 39, 88, 89, 90, 91, 136,137,138,139, 184,185,186,187,
40, 41, 42, 43, 92, 93, 94, 95, 140,141,142,143, 188,189,190,191,
44, 45, 46, 47, 96, 97, 98, 99, 144,145,146,147, 192,193,194,195,
48, 49, 50, 51 };
uint8_t hd[4];
int b, d, j;
int state = 0;
uint8_t codeword;
uint16_t crc;
uint32_t crc1;
uint32_t crc2;
static const uint8_t next_words[4][4] = {
{0x2, 0xC, 0x1, 0xF},
{0xE, 0x0, 0xD, 0x3},
{0x9, 0x7, 0xA, 0x4},
{0x5, 0xB, 0x6, 0x8}
};
memset(buf, 0, 12);
for (b=0; b < 98*2; b += 4) {
codeword = (bv[start+deinterleave_tb[b+0]] << 3) +
(bv[start+deinterleave_tb[b+1]] << 2) +
(bv[start+deinterleave_tb[b+2]] << 1) +
bv[start+deinterleave_tb[b+3]] ;
/* try each codeword in a row of the state transition table */
for (j = 0; j < 4; j++) {
/* find Hamming distance for candidate */
hd[j] = count_bits(codeword ^ next_words[state][j]);
}
/* find the dibit that matches the most codeword bits (minimum Hamming distance) */
state = find_min(hd, 4);
/* error if minimum can't be found */
if(state == -1)
return -1; // decode error, return failure
/* It also might be nice to report a condition where the minimum is
* non-zero, i.e. an error has been corrected. It probably shouldn't
* be a permanent failure, though.
*
* DISSECTOR_ASSERT(hd[state] == 0);
*/
/* append dibit onto output buffer */
d = b >> 2; // dibit ctr
if (d < 48) {
buf[d >> 2] |= state << (6 - ((d%4) * 2));
}
}
crc = crc16(buf, 12);
if (crc == 0)
return 0; // return OK code
crc1 = crc32(buf, 8*8); // try crc32
crc2 = (buf[8] << 24) + (buf[9] << 16) + (buf[10] << 8) + buf[11];
if (crc1 == crc2)
return 0; // return OK code
return -2; // trellis decode OK, but CRC error occurred
}
/*
* The private constructor
*/
p25_frame_assembler_impl::p25_frame_assembler_impl(const char* udp_host, int port, int debug, bool do_imbe, bool do_output, bool do_msgq, gr::msg_queue::sptr queue)
p25_frame_assembler_impl::p25_frame_assembler_impl(const char* udp_host, int port, int debug, bool do_imbe, bool do_output, bool do_msgq, gr::msg_queue::sptr queue, bool do_audio_output, bool do_phase2_tdma)
: gr::block("p25_frame_assembler",
gr::io_signature::make (MIN_IN, MAX_IN, sizeof (char)),
gr::io_signature::make ((do_output) ? 1 : 0, (do_output) ? 1 : 0, (do_output) ? sizeof(char) : 0 )),
write_bufp(0),
write_sock(0),
d_udp_host(udp_host),
d_port(port),
d_debug(debug),
gr::io_signature::make ((do_output || do_audio_output) ? 1 : 0, (do_output || do_audio_output) ? 1 : 0, (do_audio_output) ? sizeof(int16_t) : ((do_output) ? sizeof(char) : 0 ))),
d_do_imbe(do_imbe),
d_do_output(do_output),
output_queue(),
p1fdma(udp_host, port, debug, do_imbe, do_output, do_msgq, queue, output_queue, do_audio_output),
d_do_audio_output(do_audio_output),
d_do_phase2_tdma(do_phase2_tdma),
p2tdma(0, debug, output_queue),
d_do_msgq(do_msgq),
d_msg_queue(queue),
symbol_queue(),
framer(new p25_framer())
d_msg_queue(queue)
{
gettimeofday(&last_qtime, 0);
if (port > 0)
init_sock(d_udp_host, d_port);
if (d_do_audio_output && !d_do_output)
fprintf(stderr, "p25_frame_assembler: error: do_output must be enabled if do_audio_output is enabled\n");
if (d_do_audio_output && !d_do_imbe)
fprintf(stderr, "p25_frame_assembler: error: do_imbe must be enabled if do_audio_output is enabled\n");
if (d_do_phase2_tdma && !d_do_audio_output)
fprintf(stderr, "p25_frame_assembler: error: do_audio_output must be enabled if do_phase2_tdma is enabled\n");
}
void
@ -244,33 +107,16 @@ p25_frame_assembler_impl::forecast(int nof_output_items, gr_vector_int &nof_inpu
{
// for do_imbe=false: we output packed bytes (4:1 ratio)
// for do_imbe=true: input rate= 4800, output rate= 1600 = 32 * 50 (3:1)
// for do_audio_output: output rate=8000 (ratio 0.6:1)
const size_t nof_inputs = nof_input_items_reqd.size();
int nof_samples_reqd = 4.0 * nof_output_items;
if (d_do_imbe)
nof_samples_reqd = 3.0 * nof_output_items;
nof_samples_reqd = nof_output_items;
if (d_do_audio_output)
nof_samples_reqd = 0.6 * nof_output_items;
std::fill(&nof_input_items_reqd[0], &nof_input_items_reqd[nof_inputs], nof_samples_reqd);
}
void
p25_frame_assembler_impl::process_duid(uint32_t const duid, uint32_t const nac, uint8_t const buf[], int const len)
{
char wbuf[256];
int p = 0;
if (!d_do_msgq)
return;
if (d_msg_queue->full_p())
return;
assert (len+2 <= sizeof(wbuf));
wbuf[p++] = (nac >> 8) & 0xff;
wbuf[p++] = nac & 0xff;
if (buf) {
memcpy(&wbuf[p], buf, len); // copy data
p += len;
}
gr::message::sptr msg = gr::message::make_from_string(std::string(wbuf, p), duid, 0, 0);
d_msg_queue->insert_tail(msg);
gettimeofday(&last_qtime, 0);
// msg.reset();
}
int
p25_frame_assembler_impl::general_work (int noutput_items,
@ -280,172 +126,41 @@ p25_frame_assembler_impl::general_work (int noutput_items,
{
const uint8_t *in = (const uint8_t *) input_items[0];
struct timeval currtime;
for (int i = 0; i < noutput_items; i++){
if(framer->rx_sym(in[i])) { // complete frame was detected
if (d_debug >= 10) {
fprintf (stderr, "NAC 0x%X DUID 0x%X len %u errs %u ", framer->nac, framer->duid, framer->frame_size >> 1, framer->bch_errors);
p1fdma.rx_sym(in, ninput_items[0]);
if(d_do_phase2_tdma) {
for (int i = 0; i < ninput_items[0]; i++) {
if(p2tdma.rx_sym(in[i])) {
int rc = p2tdma.handle_frame();
if (rc > -1)
p25p2_queue_msg(rc);
}
if (framer->bch_errors >= 0 &&
((framer->duid == 0x03) ||
(framer->duid == 0x05) ||
(framer->duid == 0x0A) ||
(framer->duid == 0x0F))) {
process_duid(framer->duid, framer->nac, NULL, 0);
}
if ((framer->duid == 0x07 || framer->duid == 0x0c) && framer->bch_errors >= 0) {
unsigned int d, b;
int rc[3];
bit_vector bv1(720);
int sizes[3] = {360, 576, 720};
uint8_t deinterleave_buf[3][12];
if (framer->frame_size > 720) {
fprintf(stderr, "warning trunk frame size %u exceeds maximum\n", framer->frame_size);
framer->frame_size = 720;
}
for (d=0, b=0; d < framer->frame_size >> 1; d++) {
if ((d+1) % 36 == 0)
continue; // skip SS
bv1[b++] = framer->frame_body[d*2];
bv1[b++] = framer->frame_body[d*2+1];
}
for(int sz=0; sz < 3; sz++) {
if (framer->frame_size >= sizes[sz]) {
rc[sz] = block_deinterleave(bv1,48+64+sz*196 , deinterleave_buf[sz]);
if (framer->duid == 0x07 && rc[sz] == 0)
process_duid(framer->duid, framer->nac, deinterleave_buf[sz], 10);
}
}
// two-block mbt is the only format currently supported
if (framer->duid == 0x0c
&& framer->frame_size == 576
&& rc[0] == 0
&& rc[1] == 0) {
// we copy first 10 bytes from first and
// first 8 from second (removes CRC's)
uint8_t mbt_block[18];
memcpy(mbt_block, deinterleave_buf[0], 10);
memcpy(&mbt_block[10], deinterleave_buf[1], 8);
process_duid(framer->duid, framer->nac, mbt_block, sizeof(mbt_block));
}
}
if (d_debug >= 10 && framer->duid == 0x00) {
ProcHDU(framer->frame_body);
} else if (d_debug > 10 && framer->duid == 0x05) {
ProcLDU1(framer->frame_body);
} else if (d_debug >= 10 && framer->duid == 0x0a) {
ProcLDU2(framer->frame_body);
} else if (d_debug > 10 && framer->duid == 0x0f) {
ProcTDU(framer->frame_body);
}
if (d_debug >= 10)
fprintf(stderr, "\n");
if (d_do_imbe && (framer->duid == 0x5 || framer->duid == 0xa)) { // if voice - ldu1 or ldu2
for(size_t i = 0; i < nof_voice_codewords; ++i) {
voice_codeword cw(voice_codeword_sz);
uint32_t E0, ET;
uint32_t u[8];
char s[128];
imbe_deinterleave(framer->frame_body, cw, i);
// recover 88-bit IMBE voice code word
imbe_header_decode(cw, u[0], u[1], u[2], u[3], u[4], u[5], u[6], u[7], E0, ET);
// output one 32-byte msg per 0.020 sec.
// also, 32*9 = 288 byte pkts (for use via UDP)
sprintf(s, "%03x %03x %03x %03x %03x %03x %03x %03x\n", u[0], u[1], u[2], u[3], u[4], u[5], u[6], u[7]);
if (d_do_output) {
for (size_t j=0; j < strlen(s); j++) {
symbol_queue.push_back(s[j]);
}
}
if (write_sock > 0) {
memcpy(&write_buf[write_bufp], s, strlen(s));
write_bufp += strlen(s);
if (write_bufp >= 288) { // 9 * 32 = 288
sendto(write_sock, write_buf, 288, 0, (struct sockaddr *)&write_sock_addr, sizeof(write_sock_addr));
// FIXME check sendto() rc
write_bufp = 0;
}
}
}
} // end of imbe/voice
if (!d_do_imbe) {
// pack the bits into bytes, MSB first
size_t obuf_ct = 0;
uint8_t obuf[P25_VOICE_FRAME_SIZE/2];
for (uint32_t i = 0; i < framer->frame_size; i += 8) {
uint8_t b =
(framer->frame_body[i+0] << 7) +
(framer->frame_body[i+1] << 6) +
(framer->frame_body[i+2] << 5) +
(framer->frame_body[i+3] << 4) +
(framer->frame_body[i+4] << 3) +
(framer->frame_body[i+5] << 2) +
(framer->frame_body[i+6] << 1) +
(framer->frame_body[i+7] );
obuf[obuf_ct++] = b;
}
if (write_sock > 0) {
sendto(write_sock, obuf, obuf_ct, 0, (struct sockaddr*)&write_sock_addr, sizeof(write_sock_addr));
}
if (d_do_output) {
for (size_t j=0; j < obuf_ct; j++) {
symbol_queue.push_back(obuf[j]);
}
}
}
} // end of complete frame
}
if (d_do_msgq && !d_msg_queue->full_p()) {
// check for timeout
gettimeofday(&currtime, 0);
int64_t diff_usec = currtime.tv_usec - last_qtime.tv_usec;
int64_t diff_sec = currtime.tv_sec - last_qtime.tv_sec ;
if (diff_usec < 0) {
diff_usec += 1000000;
diff_sec -= 1;
}
diff_usec += diff_sec * 1000000;
if (diff_usec >= TIMEOUT_THRESHOLD) {
gettimeofday(&last_qtime, 0);
gr::message::sptr msg = gr::message::make(-1, 0, 0);
d_msg_queue->insert_tail(msg);
}
}
}
int amt_produce = 0;
amt_produce = noutput_items;
if (amt_produce > (int)symbol_queue.size())
amt_produce = symbol_queue.size();
if (amt_produce > 0) {
unsigned char *out = (unsigned char *) output_items[0];
copy(symbol_queue.begin(), symbol_queue.begin() + amt_produce, out);
symbol_queue.erase(symbol_queue.begin(), symbol_queue.begin() + amt_produce);
if (d_do_output) {
amt_produce = noutput_items;
if (amt_produce > (int)output_queue.size())
amt_produce = output_queue.size();
if (amt_produce > 0) {
if (d_do_audio_output) {
int16_t *out = (int16_t *) output_items[0];
for (int i=0; i < amt_produce; i++) {
out[i] = output_queue[i];
}
} else {
unsigned char *out = (unsigned char *) output_items[0];
for (int i=0; i < amt_produce; i++) {
out[i] = output_queue[i];
}
}
output_queue.erase(output_queue.begin(), output_queue.begin() + amt_produce);
}
}
// printf ("work: ninp[0]: %d nout: %d size %d produce: %d surplus %d\n", ninput_items[0], noutput_items, symbol_queue.size(), amt_produce, surplus);
consume_each(noutput_items);
consume_each(ninput_items[0]);
// Tell runtime system how many output items we produced.
return amt_produce;
}
void p25_frame_assembler_impl::init_sock(const char* udp_host, int udp_port)
{
memset (&write_sock_addr, 0, sizeof(write_sock_addr));
write_sock = socket(PF_INET, SOCK_DGRAM, 17); // UDP socket
if (write_sock < 0) {
fprintf(stderr, "op25_imbe_vocoder: socket: %d\n", errno);
write_sock = 0;
return;
}
if (!inet_aton(udp_host, &write_sock_addr.sin_addr)) {
fprintf(stderr, "op25_imbe_vocoder: inet_aton: bad IP address\n");
close(write_sock);
write_sock = 0;
return;
}
write_sock_addr.sin_family = AF_INET;
write_sock_addr.sin_port = htons(udp_port);
}
} /* namespace op25_repeater */
} /* namespace gr */

View File

@ -30,7 +30,8 @@
#include <arpa/inet.h>
#include <deque>
#include "p25_framer.h"
#include "p25p1_fdma.h"
#include "p25p2_tdma.h"
typedef std::deque<uint8_t> dibit_queue;
@ -40,36 +41,29 @@ namespace gr {
class p25_frame_assembler_impl : public p25_frame_assembler
{
private:
void init_sock(const char* udp_host, int udp_port);
// internal functions
typedef std::vector<bool> bit_vector;
bool header_codeword(uint64_t acc, uint32_t& nac, uint32_t& duid);
void proc_voice_unit(bit_vector& frame_body) ;
void process_duid(uint32_t const duid, uint32_t const nac, uint8_t const buf[], int const len);
// internal instance variables and state
int write_bufp;
int write_sock;
struct sockaddr_in write_sock_addr;
char write_buf[512];
const char* d_udp_host;
int d_port;
int d_debug;
bool d_do_imbe;
bool d_do_output;
p25p1_fdma p1fdma;
bool d_do_audio_output;
bool d_do_phase2_tdma;
p25p2_tdma p2tdma;
bool d_do_msgq;
gr::msg_queue::sptr d_msg_queue;
dibit_queue symbol_queue;
p25_framer* framer;
struct timeval last_qtime;
// internal functions
void p25p2_queue_msg(int duid);
void set_xormask(const char*p) ;
void set_slotid(int slotid) ;
typedef std::vector<bool> bit_vector;
std::deque<int16_t> output_queue;
public:
virtual void forecast(int nof_output_items, gr_vector_int &nof_input_items_reqd);
// Nothing to declare in this block.
public:
p25_frame_assembler_impl(const char* udp_host, int port, int debug, bool do_imbe, bool do_output, bool do_msgq, gr::msg_queue::sptr queue);
p25_frame_assembler_impl(const char* udp_host, int port, int debug, bool do_imbe, bool do_output, bool do_msgq, gr::msg_queue::sptr queue, bool do_audio_output, bool do_phase2_tdma);
~p25_frame_assembler_impl();
// Where all the action really happens

View File

@ -142,6 +142,15 @@ bool p25_framer::rx_sym(uint8_t dibit) {
reverse_p ^= 0x02; // auto flip polarity reversal
fprintf(stderr, "Reversed FS polarity detected - autocorrecting\n");
}
if(check_frame_sync((nid_accum & P25_FRAME_SYNC_MASK) ^ 0x001050551155LL, 0)) {
fprintf(stderr, "tuning error -1200\n");
}
if(check_frame_sync((nid_accum & P25_FRAME_SYNC_MASK) ^ 0xFFEFAFAAEEAALL, 0)) {
fprintf(stderr, "tuning error +1200\n");
}
if(check_frame_sync((nid_accum & P25_FRAME_SYNC_MASK) ^ 0xAA8A0A008800LL, 0)) {
fprintf(stderr, "tuning error +/- 2400\n");
}
if (next_bit > 0) {
frame_body[next_bit++] = (dibit >> 1) & 1;
frame_body[next_bit++] = dibit & 1;

View File

@ -0,0 +1,403 @@
/* -*- c++ -*- */
/*
* Copyright 2010, 2011, 2012, 2013, 2014 Max H. Parke KA1RBI
*
* 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 "p25p1_fdma.h"
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <errno.h>
#include <vector>
#include <sys/time.h>
#include "bch.h"
#include "op25_imbe_frame.h"
#include "p25_frame.h"
#include "p25_framer.h"
#include "rs.h"
namespace gr {
namespace op25_repeater {
static const int64_t TIMEOUT_THRESHOLD = 1000000;
p25p1_fdma::~p25p1_fdma()
{
if (write_sock > 0)
close(write_sock);
delete framer;
}
static uint16_t crc16(uint8_t buf[], int len) {
uint32_t poly = (1<<12) + (1<<5) + (1<<0);
uint32_t crc = 0;
for(int i=0; i<len; i++) {
uint8_t bits = buf[i];
for (int j=0; j<8; j++) {
uint8_t bit = (bits >> (7-j)) & 1;
crc = ((crc << 1) | bit) & 0x1ffff;
if (crc & 0x10000)
crc = (crc & 0xffff) ^ poly;
}
}
crc = crc ^ 0xffff;
return crc & 0xffff;
}
/* translated from p25craft.py Michael Ossmann <mike@ossmann.com> */
static uint32_t crc32(uint8_t buf[], int len) { /* length is nr. of bits */
uint32_t g = 0x04c11db7;
uint64_t crc = 0;
for (int i = 0; i < len; i++) {
crc <<= 1;
int b = ( buf [i / 8] >> (7 - (i % 8)) ) & 1;
if (((crc >> 32) ^ b) & 1)
crc ^= g;
}
crc = (crc & 0xffffffff) ^ 0xffffffff;
return crc;
}
/* find_min is from wireshark/plugins/p25/packet-p25cai.c */
/* Copyright 2008, Michael Ossmann <mike@ossmann.com> */
/* return the index of the lowest value in a list */
static int
find_min(uint8_t list[], int len)
{
int min = list[0];
int index = 0;
int unique = 1;
int i;
for (i = 1; i < len; i++) {
if (list[i] < min) {
min = list[i];
index = i;
unique = 1;
} else if (list[i] == min) {
unique = 0;
}
}
/* return -1 if a minimum can't be found */
if (!unique)
return -1;
return index;
}
/* count_bits is from wireshark/plugins/p25/packet-p25cai.c */
/* Copyright 2008, Michael Ossmann <mike@ossmann.com> */
/* count the number of 1 bits in an int */
static int
count_bits(unsigned int n)
{
int i = 0;
for (i = 0; n != 0; i++)
n &= n - 1;
return i;
}
/* adapted from wireshark/plugins/p25/packet-p25cai.c */
/* Copyright 2008, Michael Ossmann <mike@ossmann.com> */
/* deinterleave and trellis1_2 decoding */
/* buf is assumed to be a buffer of 12 bytes */
static int
block_deinterleave(bit_vector& bv, unsigned int start, uint8_t* buf)
{
static const uint16_t deinterleave_tb[] = {
0, 1, 2, 3, 52, 53, 54, 55, 100,101,102,103, 148,149,150,151,
4, 5, 6, 7, 56, 57, 58, 59, 104,105,106,107, 152,153,154,155,
8, 9, 10, 11, 60, 61, 62, 63, 108,109,110,111, 156,157,158,159,
12, 13, 14, 15, 64, 65, 66, 67, 112,113,114,115, 160,161,162,163,
16, 17, 18, 19, 68, 69, 70, 71, 116,117,118,119, 164,165,166,167,
20, 21, 22, 23, 72, 73, 74, 75, 120,121,122,123, 168,169,170,171,
24, 25, 26, 27, 76, 77, 78, 79, 124,125,126,127, 172,173,174,175,
28, 29, 30, 31, 80, 81, 82, 83, 128,129,130,131, 176,177,178,179,
32, 33, 34, 35, 84, 85, 86, 87, 132,133,134,135, 180,181,182,183,
36, 37, 38, 39, 88, 89, 90, 91, 136,137,138,139, 184,185,186,187,
40, 41, 42, 43, 92, 93, 94, 95, 140,141,142,143, 188,189,190,191,
44, 45, 46, 47, 96, 97, 98, 99, 144,145,146,147, 192,193,194,195,
48, 49, 50, 51 };
uint8_t hd[4];
int b, d, j;
int state = 0;
uint8_t codeword;
uint16_t crc;
uint32_t crc1;
uint32_t crc2;
static const uint8_t next_words[4][4] = {
{0x2, 0xC, 0x1, 0xF},
{0xE, 0x0, 0xD, 0x3},
{0x9, 0x7, 0xA, 0x4},
{0x5, 0xB, 0x6, 0x8}
};
memset(buf, 0, 12);
for (b=0; b < 98*2; b += 4) {
codeword = (bv[start+deinterleave_tb[b+0]] << 3) +
(bv[start+deinterleave_tb[b+1]] << 2) +
(bv[start+deinterleave_tb[b+2]] << 1) +
bv[start+deinterleave_tb[b+3]] ;
/* try each codeword in a row of the state transition table */
for (j = 0; j < 4; j++) {
/* find Hamming distance for candidate */
hd[j] = count_bits(codeword ^ next_words[state][j]);
}
/* find the dibit that matches the most codeword bits (minimum Hamming distance) */
state = find_min(hd, 4);
/* error if minimum can't be found */
if(state == -1)
return -1; // decode error, return failure
/* It also might be nice to report a condition where the minimum is
* non-zero, i.e. an error has been corrected. It probably shouldn't
* be a permanent failure, though.
*
* DISSECTOR_ASSERT(hd[state] == 0);
*/
/* append dibit onto output buffer */
d = b >> 2; // dibit ctr
if (d < 48) {
buf[d >> 2] |= state << (6 - ((d%4) * 2));
}
}
crc = crc16(buf, 12);
if (crc == 0)
return 0; // return OK code
crc1 = crc32(buf, 8*8); // try crc32
crc2 = (buf[8] << 24) + (buf[9] << 16) + (buf[10] << 8) + buf[11];
if (crc1 == crc2)
return 0; // return OK code
return -2; // trellis decode OK, but CRC error occurred
}
p25p1_fdma::p25p1_fdma(const char* udp_host, int port, int debug, bool do_imbe, bool do_output, bool do_msgq, gr::msg_queue::sptr queue, std::deque<int16_t> &output_queue, bool do_audio_output) :
write_bufp(0),
write_sock(0),
d_udp_host(udp_host),
d_port(port),
d_debug(debug),
d_do_imbe(do_imbe),
d_do_output(do_output),
d_do_msgq(do_msgq),
d_msg_queue(queue),
output_queue(output_queue),
framer(new p25_framer()),
d_do_audio_output(do_audio_output),
p1voice_decode((debug > 0), udp_host, port, output_queue)
{
gettimeofday(&last_qtime, 0);
if (port > 0)
init_sock(d_udp_host, d_port);
}
void
p25p1_fdma::process_duid(uint32_t const duid, uint32_t const nac, uint8_t const buf[], int const len)
{
char wbuf[256];
int p = 0;
if (!d_do_msgq)
return;
if (d_msg_queue->full_p())
return;
assert (len+2 <= sizeof(wbuf));
wbuf[p++] = (nac >> 8) & 0xff;
wbuf[p++] = nac & 0xff;
if (buf) {
memcpy(&wbuf[p], buf, len); // copy data
p += len;
}
gr::message::sptr msg = gr::message::make_from_string(std::string(wbuf, p), duid, 0, 0);
d_msg_queue->insert_tail(msg);
gettimeofday(&last_qtime, 0);
// msg.reset();
}
void
p25p1_fdma::rx_sym (const uint8_t *syms, int nsyms)
{
struct timeval currtime;
for (int i1 = 0; i1 < nsyms; i1++){
if(framer->rx_sym(syms[i1])) { // complete frame was detected
if (d_debug >= 10) {
fprintf (stderr, "NAC 0x%X DUID 0x%X len %u errs %u ", framer->nac, framer->duid, framer->frame_size >> 1, framer->bch_errors);
}
if (framer->bch_errors >= 0 &&
((framer->duid == 0x03) ||
(framer->duid == 0x05) ||
(framer->duid == 0x0A) ||
(framer->duid == 0x0F))) {
process_duid(framer->duid, framer->nac, NULL, 0);
}
if ((framer->duid == 0x07 || framer->duid == 0x0c) && framer->bch_errors >= 0) {
unsigned int d, b;
int rc[3];
bit_vector bv1(720);
int sizes[3] = {360, 576, 720};
uint8_t deinterleave_buf[3][12];
if (framer->frame_size > 720) {
fprintf(stderr, "warning trunk frame size %u exceeds maximum\n", framer->frame_size);
framer->frame_size = 720;
}
for (d=0, b=0; d < framer->frame_size >> 1; d++) {
if ((d+1) % 36 == 0)
continue; // skip SS
bv1[b++] = framer->frame_body[d*2];
bv1[b++] = framer->frame_body[d*2+1];
}
for(int sz=0; sz < 3; sz++) {
if (framer->frame_size >= sizes[sz]) {
rc[sz] = block_deinterleave(bv1,48+64+sz*196 , deinterleave_buf[sz]);
if (framer->duid == 0x07 && rc[sz] == 0)
process_duid(framer->duid, framer->nac, deinterleave_buf[sz], 10);
}
}
// two-block mbt is the only format currently supported
if (framer->duid == 0x0c
&& framer->frame_size == 576
&& rc[0] == 0
&& rc[1] == 0) {
// we copy first 10 bytes from first and
// first 8 from second (removes CRC's)
uint8_t mbt_block[18];
memcpy(mbt_block, deinterleave_buf[0], 10);
memcpy(&mbt_block[10], deinterleave_buf[1], 8);
process_duid(framer->duid, framer->nac, mbt_block, sizeof(mbt_block));
}
}
if (d_debug >= 10 && framer->duid == 0x00) {
ProcHDU(framer->frame_body);
} else if (d_debug > 10 && framer->duid == 0x05) {
ProcLDU1(framer->frame_body);
} else if (d_debug >= 10 && framer->duid == 0x0a) {
ProcLDU2(framer->frame_body);
} else if (d_debug > 10 && framer->duid == 0x0f) {
ProcTDU(framer->frame_body);
}
if (d_debug >= 10)
fprintf(stderr, "\n");
if ((d_do_imbe || d_do_audio_output) && (framer->duid == 0x5 || framer->duid == 0xa)) { // if voice - ldu1 or ldu2
for(size_t i = 0; i < nof_voice_codewords; ++i) {
voice_codeword cw(voice_codeword_sz);
uint32_t E0, ET;
uint32_t u[8];
char s[128];
imbe_deinterleave(framer->frame_body, cw, i);
// recover 88-bit IMBE voice code word
imbe_header_decode(cw, u[0], u[1], u[2], u[3], u[4], u[5], u[6], u[7], E0, ET);
// output one 32-byte msg per 0.020 sec.
// also, 32*9 = 288 byte pkts (for use via UDP)
sprintf(s, "%03x %03x %03x %03x %03x %03x %03x %03x\n", u[0], u[1], u[2], u[3], u[4], u[5], u[6], u[7]);
if (d_do_output) {
if (d_do_audio_output) {
p1voice_decode.rxframe(u);
} else {
for (size_t j=0; j < strlen(s); j++) {
output_queue.push_back(s[j]);
}
}
}
if (write_sock > 0) {
memcpy(&write_buf[write_bufp], s, strlen(s));
write_bufp += strlen(s);
if (write_bufp >= 288) { // 9 * 32 = 288
sendto(write_sock, write_buf, 288, 0, (struct sockaddr *)&write_sock_addr, sizeof(write_sock_addr));
// FIXME check sendto() rc
write_bufp = 0;
}
}
}
} // end of imbe/voice
if (!d_do_imbe) {
// pack the bits into bytes, MSB first
size_t obuf_ct = 0;
uint8_t obuf[P25_VOICE_FRAME_SIZE/2];
for (uint32_t i = 0; i < framer->frame_size; i += 8) {
uint8_t b =
(framer->frame_body[i+0] << 7) +
(framer->frame_body[i+1] << 6) +
(framer->frame_body[i+2] << 5) +
(framer->frame_body[i+3] << 4) +
(framer->frame_body[i+4] << 3) +
(framer->frame_body[i+5] << 2) +
(framer->frame_body[i+6] << 1) +
(framer->frame_body[i+7] );
obuf[obuf_ct++] = b;
}
if (write_sock > 0) {
sendto(write_sock, obuf, obuf_ct, 0, (struct sockaddr*)&write_sock_addr, sizeof(write_sock_addr));
}
if (d_do_output) {
for (size_t j=0; j < obuf_ct; j++) {
output_queue.push_back(obuf[j]);
}
}
}
} // end of complete frame
}
if (d_do_msgq && !d_msg_queue->full_p()) {
// check for timeout
gettimeofday(&currtime, 0);
int64_t diff_usec = currtime.tv_usec - last_qtime.tv_usec;
int64_t diff_sec = currtime.tv_sec - last_qtime.tv_sec ;
if (diff_usec < 0) {
diff_usec += 1000000;
diff_sec -= 1;
}
diff_usec += diff_sec * 1000000;
if (diff_usec >= TIMEOUT_THRESHOLD) {
gettimeofday(&last_qtime, 0);
gr::message::sptr msg = gr::message::make(-1, 0, 0);
d_msg_queue->insert_tail(msg);
}
}
}
void p25p1_fdma::init_sock(const char* udp_host, int udp_port)
{
memset (&write_sock_addr, 0, sizeof(write_sock_addr));
write_sock = socket(PF_INET, SOCK_DGRAM, 17); // UDP socket
if (write_sock < 0) {
fprintf(stderr, "op25_imbe_vocoder: socket: %d\n", errno);
write_sock = 0;
return;
}
if (!inet_aton(udp_host, &write_sock_addr.sin_addr)) {
fprintf(stderr, "op25_imbe_vocoder: inet_aton: bad IP address\n");
close(write_sock);
write_sock = 0;
return;
}
write_sock_addr.sin_family = AF_INET;
write_sock_addr.sin_port = htons(udp_port);
}
} // namespace
} // namespace

View File

@ -0,0 +1,80 @@
/* -*- c++ -*- */
/*
* Copyright 2009, 2010, 2011, 2012, 2013, 2014 Max H. Parke KA1RBI
*
* 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.
*/
#ifndef INCLUDED_OP25_REPEATER_P25P1_FDMA_H
#define INCLUDED_OP25_REPEATER_P25P1_FDMA_H
#include <gnuradio/msg_queue.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <deque>
#include "p25_framer.h"
#include "p25p1_voice_encode.h"
#include "p25p1_voice_decode.h"
namespace gr {
namespace op25_repeater {
class p25p1_fdma
{
private:
void init_sock(const char* udp_host, int udp_port);
// internal functions
typedef std::vector<bool> bit_vector;
bool header_codeword(uint64_t acc, uint32_t& nac, uint32_t& duid);
void proc_voice_unit(bit_vector& frame_body) ;
void process_duid(uint32_t const duid, uint32_t const nac, uint8_t const buf[], int const len);
// internal instance variables and state
int write_bufp;
int write_sock;
struct sockaddr_in write_sock_addr;
char write_buf[512];
const char* d_udp_host;
int d_port;
int d_debug;
bool d_do_imbe;
bool d_do_output;
bool d_do_msgq;
gr::msg_queue::sptr d_msg_queue;
std::deque<int16_t> &output_queue;
p25_framer* framer;
struct timeval last_qtime;
bool d_do_audio_output;
p25p1_voice_decode p1voice_decode;
public:
void rx_sym (const uint8_t *syms, int nsyms);
p25p1_fdma(const char* udp_host, int port, int debug, bool do_imbe, bool do_output, bool do_msgq, gr::msg_queue::sptr queue, std::deque<int16_t> &output_queue, bool do_audio_output);
~p25p1_fdma();
// Where all the action really happens
int general_work(int noutput_items,
gr_vector_int &ninput_items,
gr_vector_const_void_star &input_items,
gr_vector_void_star &output_items);
};
} // namespace
} // namespace
#endif /* INCLUDED_OP25_REPEATER_P25P1_FDMA_H */

View File

@ -65,18 +65,26 @@ static bool crc12_ok(const uint8_t bits[], unsigned int len) {
return (crc == crc12(bits,len));
}
p25p2_tdma::p25p2_tdma(int slotid, int debug, std::deque<int16_t> *qptr) : // constructor
p25p2_tdma::p25p2_tdma(int slotid, int debug, std::deque<int16_t> &qptr) : // constructor
tdma_xormask(new uint8_t[SUPERFRAME_SIZE]),
symbols_received(0),
packets(0),
d_slotid(slotid),
output_queue_decode(qptr),
d_debug(debug),
crc_errors(0)
crc_errors(0),
p2framer()
{
assert (slotid == 0 || slotid == 1);
mbe_initMbeParms (&cur_mp, &prev_mp, &enh_mp);
}
bool p25p2_tdma::rx_sym(uint8_t sym)
{
symbols_received++;
return p2framer.rx_sym(sym);
}
void p25p2_tdma::set_slotid(int slotid)
{
assert (slotid == 0 || slotid == 1);
@ -183,12 +191,22 @@ void p25p2_tdma::handle_voice_frame(const uint8_t dibits[])
} else {
snd = 0;
}
output_queue_decode->push_back(snd);
output_queue_decode.push_back(snd);
}
mbe_moveMbeParms (&cur_mp, &prev_mp);
mbe_moveMbeParms (&cur_mp, &enh_mp);
}
int p25p2_tdma::handle_frame(void)
{
uint8_t dibits[180];
int rc;
for (int i=0; i<sizeof(dibits); i++)
dibits[i] = p2framer.d_frame_body[i*2+1] + (p2framer.d_frame_body[i*2] << 1);
rc = handle_packet(dibits);
return rc;
}
/* returns true if in sync and slot matches current active slot d_slotid */
int p25p2_tdma::handle_packet(const uint8_t dibits[])
{
@ -206,6 +224,9 @@ int p25p2_tdma::handle_packet(const uint8_t dibits[])
for (int i=0; i<BURST_SIZE - 10; i++) {
xored_burst[i] = burstp[i] ^ tdma_xormask[sync.tdma_slotid() * BURST_SIZE + i];
}
if (d_debug) {
fprintf(stderr, "p25p2_tdma: burst type %d symbols %u packets %u\n", burst_type, symbols_received, packets);
}
if (burst_type == 0 || burst_type == 6) { // 4v or 2v (voice) ?
handle_voice_frame(&xored_burst[11]);
handle_voice_frame(&xored_burst[48]);

View File

@ -28,32 +28,38 @@
#include "p25p2_duid.h"
#include "p25p2_sync.h"
#include "p25p2_vf.h"
#include "p25p2_framer.h"
class p25p2_tdma;
class p25p2_tdma
{
public:
p25p2_tdma(int slotid, int debug, std::deque<int16_t> *qptr) ; // constructor
p25p2_tdma(int slotid, int debug, std::deque<int16_t> &qptr) ; // constructor
int handle_packet(const uint8_t dibits[]) ;
void set_slotid(int slotid);
uint8_t* tdma_xormask;
uint32_t symbols_received;
uint32_t packets;
~p25p2_tdma(); // destructor
void set_xormask(const char*p);
bool rx_sym(uint8_t sym);
int handle_frame(void) ;
private:
p25p2_sync sync;
p25p2_duid duid;
p25p2_vf vf;
uint32_t packets;
int d_slotid;
mbe_parms cur_mp;
mbe_parms prev_mp;
mbe_parms enh_mp;
software_imbe_decoder software_decoder;
std::deque<int16_t> *output_queue_decode;
std::deque<int16_t> &output_queue_decode;
int d_debug;
unsigned long int crc_errors;
p25p2_framer p2framer;
int handle_acch_frame(const uint8_t dibits[], bool fast) ;
void handle_voice_frame(const uint8_t dibits[]) ;
int process_mac_pdu(const uint8_t byte_buf[], unsigned int len) ;

View File

@ -3,7 +3,7 @@
* GNU Radio interface for Pavel Yazev's Project 25 IMBE Encoder/Decoder
*
* Copyright 2009 Pavel Yazev E-mail: pyazev@gmail.com
* Copyright 2009, 2010, 2011, 2012, 2013 KA1RBI
* Copyright 2009, 2010, 2011, 2012, 2013, 2014 KA1RBI
*
* This is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@ -37,122 +37,9 @@
#include <netinet/in.h>
#include <arpa/inet.h>
#include "imbe_vocoder/imbe_vocoder.h"
#include "p25_frame.h"
#include "op25_imbe_frame.h"
namespace gr {
namespace op25_repeater {
/* bits to be preset to 1 for all transmitted frames */
static const bool ldu1_preset[] = {
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0-35 */
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,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,0, /* 1440-1475 */
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0, /* 1476-1511 */
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 1512-1547 */
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0, /* 1548-1583 */
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 1584-1619 */
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0, /* 1620-1655 */
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 1656-1691 */
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0, /* 1692-1727 */
};
static void clear_bits(bit_vector& v) {
for (size_t i=0; i<v.size(); i++) {
v[i]=0;
}
}
vocoder::sptr
vocoder::make(bool encode_flag, bool verbose_flag, int stretch_amt, char* udp_host, int udp_port, bool raw_vectors_flag)
{
@ -179,53 +66,13 @@ static void clear_bits(bit_vector& v) {
: gr::block("vocoder",
gr::io_signature::make (M_IN(encode_flag, udp_port), M_IN(encode_flag, udp_port), S_IN(encode_flag, udp_port)),
gr::io_signature::make (M_OUT(encode_flag, udp_port), M_OUT(encode_flag, udp_port), S_OUT(encode_flag, udp_port))),
frame_cnt(0),
write_sock(0),
write_bufp(0),
peak_amplitude(0),
peak(0),
samp_ct(0),
rxbufp(0),
codeword_ct(0),
sampbuf_ct(0),
stretch_count(0),
save_l(0),
f_body(P25_VOICE_FRAME_SIZE)
output_queue(),
output_queue_decode(),
opt_udp_port(udp_port),
opt_encode_flag(encode_flag),
p1voice_encode(verbose_flag, stretch_amt, udp_host, udp_port, raw_vectors_flag, output_queue),
p1voice_decode(verbose_flag, udp_host, udp_port, output_queue_decode)
{
opt_encode_flag = encode_flag;
opt_dump_raw_vectors = raw_vectors_flag;
opt_verbose = verbose_flag;
opt_udp_port = udp_port;
opt_stretch_amt = 0;
if (stretch_amt < 0) {
opt_stretch_sign = -1;
opt_stretch_amt = 0 - stretch_amt;
} else {
opt_stretch_sign = 1;
opt_stretch_amt = stretch_amt;
}
if (opt_udp_port == 0)
if (encode_flag) {
// local output to gr source
// encoding: an LDU's worth of dibits
set_output_multiple(P25_VOICE_FRAME_SIZE >> 1);
} else {
// decoding: an LDU's worth of audio samples
// set_output_multiple(FRAME * nof_voice_codewords);
}
else
// remote UDP output
init_sock(udp_host, opt_udp_port);
clear_bits(f_body);
const char *p = getenv("IMBE");
if (p && strcasecmp(p, "soft") == 0)
d_software_imbe_decoder = true;
else
d_software_imbe_decoder = false;
}
/*
@ -235,204 +82,6 @@ static void clear_bits(bit_vector& v) {
{
}
static const int STATS_INTERVAL = 20;
static const int SAMP_INTERVAL = 8192;
void vocoder_impl::append_imbe_codeword(bit_vector& frame_body, int16_t frame_vector[], unsigned int& codeword_ct)
{
voice_codeword cw(voice_codeword_sz);
uint8_t obuf[P25_VOICE_FRAME_SIZE/2];
// construct 144-bit codeword from 88 bits of parameters
imbe_header_encode(cw, frame_vector[0], frame_vector[1], frame_vector[2], frame_vector[3], frame_vector[4], frame_vector[5], frame_vector[6], frame_vector[7]);
// add codeword to voice data unit
imbe_interleave(frame_body, cw, codeword_ct);
// after the ninth and final codeword added, dispose of frame
if (++codeword_ct >= nof_voice_codewords) {
static const uint64_t hws[2] = { 0x293555ef2c653437LL, 0x293aba93bec26a2bLL };
int ldu_type = frame_cnt & 1; // set ldu_type = 0(LDU1) or 1(LDU2)
const bool* ldu_preset = (ldu_type == 0) ? ldu1_preset : ldu2_preset;
p25_setup_frame_header(frame_body, hws[ldu_type]);
for (size_t i = 0; i < frame_body.size(); i++) {
frame_body[i] = frame_body[i] | ldu_preset[i];
}
// finally, output the frame
if (opt_udp_port > 0) {
// pack the bits into bytes, MSB first
size_t obuf_ct = 0;
for (uint32_t i = 0; i < P25_VOICE_FRAME_SIZE; i += 8) {
uint8_t b =
(frame_body[i+0] << 7) +
(frame_body[i+1] << 6) +
(frame_body[i+2] << 5) +
(frame_body[i+3] << 4) +
(frame_body[i+4] << 3) +
(frame_body[i+5] << 2) +
(frame_body[i+6] << 1) +
(frame_body[i+7] );
obuf[obuf_ct++] = b;
}
sendto(write_sock, obuf, obuf_ct, 0, (struct sockaddr*)&write_sock_addr, sizeof(write_sock_addr));
} else {
for (uint32_t i = 0; i < P25_VOICE_FRAME_SIZE; i += 2) {
uint8_t dibit =
(frame_body[i+0] << 1) +
(frame_body[i+1] );
output_queue.push_back(dibit);
}
}
codeword_ct = 0;
frame_cnt++;
if (opt_verbose && (frame_cnt % STATS_INTERVAL) == 0) {
gettimeofday(&tv, &tz);
int s = tv.tv_sec - oldtv.tv_sec;
int us = tv.tv_usec - oldtv.tv_usec;
if (us < 0) {
us = us + 1000000;
s = s - 1;
}
float f = us;
f /= 1000000;
f += s;
fprintf (stderr, "time %f peak %5d\n", f / STATS_INTERVAL, peak_amplitude);
oldtv = tv;
}
clear_bits(f_body);
}
}
void vocoder_impl::rxchar(char c)
{
int16_t snd[FRAME];
int16_t frame_vector[8];
int u[8];
if (c < ' ') {
if (c == '\n') {
rxbuf[rxbufp] = 0;
sscanf(rxbuf, "%x %x %x %x %x %x %x %x", &u[0], &u[1], &u[2], &u[3], &u[4], &u[5], &u[6], &u[7]);
rxbufp = 0;
// decode 88 bits, outputs 160 sound samples (8000 rate)
if (d_software_imbe_decoder) {
voice_codeword cw(voice_codeword_sz);
imbe_header_encode(cw, u[0], u[1], u[2], u[3], u[4], u[5], u[6], u[7]);
software_decoder.decode(cw);
audio_samples *samples = software_decoder.audio();
for (int i=0; i < FRAME; i++) {
if (samples->size() > 0) {
snd[i] = (int16_t)(samples->front() * 32768.0);
samples->pop_front();
} else {
snd[i] = 0;
}
}
} else {
for (int i=0; i < 8; i++) {
frame_vector[i] = u[i];
}
/* TEST*/ frame_vector[7] >>= 1;
vocoder.imbe_decode(frame_vector, snd);
}
if (opt_udp_port > 0) {
sendto(write_sock, snd, FRAME * sizeof(int16_t), 0, (struct sockaddr*)&write_sock_addr, sizeof(write_sock_addr));
} else {
// add generated samples to output queue
for (int i = 0; i < FRAME; i++) {
output_queue_decode.push_back(snd[i]);
}
}
}
return;
}
rxbuf[rxbufp++] = c;
if (rxbufp >= RXBUF_MAX) {
rxbufp = RXBUF_MAX - 1;
}
}
void vocoder_impl::compress_frame(int16_t snd[])
{
int16_t frame_vector[8];
// encode 160 audio samples into 88 bits (u0-u7)
vocoder.imbe_encode(frame_vector, snd);
// if dump option, dump u0-u7 to output
if (opt_dump_raw_vectors) {
char s[128];
sprintf(s, "%03x %03x %03x %03x %03x %03x %03x %03x\n", frame_vector[0], frame_vector[1], frame_vector[2], frame_vector[3], frame_vector[4], frame_vector[5], frame_vector[6], frame_vector[7]);
memcpy(&write_buf[write_bufp], s, strlen(s));
write_bufp += strlen(s);
if (write_bufp >= 288) {
sendto(write_sock, write_buf, 288, 0, (struct sockaddr*)&write_sock_addr, sizeof(write_sock_addr));
write_bufp = 0;
}
return;
}
append_imbe_codeword(f_body, frame_vector, codeword_ct);
}
void vocoder_impl::add_sample(int16_t samp)
{
// add one sample to 160-sample frame buffer and process if filled
sampbuf[sampbuf_ct++] = samp;
if (sampbuf_ct >= FRAME) {
compress_frame(sampbuf);
sampbuf_ct = 0;
}
// track signal amplitudes
int16_t asamp = (samp < 0) ? 0 - samp : samp;
peak = (asamp > peak) ? asamp : peak;
if (++samp_ct >= SAMP_INTERVAL) {
peak_amplitude = peak;
peak = 0;
samp_ct = 0;
}
}
void vocoder_impl::compress_samp(int16_t samp)
{
// Apply sample rate slew to accomodate sound card rate discrepancy -
// workaround for USRP underrun problem occurring when sound card
// capture rate is slower than the correct rate
// FIXME: autodetect proper value for opt_stretch_amt
// perhaps by steering the LDU output rate to a 180.0 msec. rate
stretch_count++;
if (opt_stretch_amt != 0 && stretch_count >= opt_stretch_amt) {
stretch_count = 0;
if (opt_stretch_sign < 0)
// spill this samp
return;
// repeat this samp
add_sample(samp);
}
add_sample(samp);
}
void vocoder_impl::init_sock(char* udp_host, int udp_port)
{
memset (&write_sock_addr, 0, sizeof(write_sock_addr));
write_sock = socket(PF_INET, SOCK_DGRAM, 17); // UDP socket
if (write_sock < 0) {
fprintf(stderr, "vocoder: socket: %d\n", errno);
write_sock = 0;
return;
}
if (!inet_aton(udp_host, &write_sock_addr.sin_addr)) {
fprintf(stderr, "vocoder: bad IP address\n");
close(write_sock);
write_sock = 0;
return;
}
write_sock_addr.sin_family = AF_INET;
write_sock_addr.sin_port = htons(udp_port);
}
void
vocoder_impl::forecast(int nof_output_items, gr_vector_int &nof_input_items_reqd)
{
@ -458,9 +107,7 @@ vocoder_impl::general_work_decode (int noutput_items,
{
const char *in = (const char *) input_items[0];
for (int i = 0; i < ninput_items[0]; i++){
rxchar(in[i]);
}
p1voice_decode.rxchar(in, ninput_items[0]);
// Tell runtime system how many input items we consumed on
// each input stream.
@ -485,9 +132,7 @@ vocoder_impl::general_work_encode (int noutput_items,
{
const short *in = (const short *) input_items[0];
for (int i = 0; i < ninput_items[0]; i++){
compress_samp(in[i]);
}
p1voice_encode.compress_samp(in, ninput_items[0]);
// Tell runtime system how many input items we consumed on
// each input stream.

View File

@ -29,15 +29,12 @@
#include <vector>
#include <deque>
#include "imbe_vocoder/imbe_vocoder.h"
#include "imbe_decoder.h"
#include "software_imbe_decoder.h"
#include "p25p1_voice_encode.h"
#include "p25p1_voice_decode.h"
namespace gr {
namespace op25_repeater {
typedef std::vector<bool> bit_vector;
class vocoder_impl : public vocoder
{
private:
@ -64,48 +61,14 @@ namespace gr {
gr_vector_void_star &output_items);
private:
static const int RXBUF_MAX = 80;
/* data items */
int frame_cnt ;
int write_sock;
struct sockaddr_in write_sock_addr;
int write_bufp;
char write_buf[512];
struct timeval tv;
struct timezone tz;
struct timeval oldtv;
int peak_amplitude;
int peak;
int samp_ct;
char rxbuf[RXBUF_MAX];
int rxbufp ;
unsigned int codeword_ct ;
int16_t sampbuf[FRAME];
size_t sampbuf_ct ;
int stretch_count ;
uint8_t save_l;
bit_vector f_body;
imbe_vocoder vocoder;
software_imbe_decoder software_decoder;
bool d_software_imbe_decoder;
std::deque<uint8_t> output_queue;
std::deque<uint16_t> output_queue_decode;
bool opt_encode_flag;
bool opt_dump_raw_vectors;
bool opt_verbose;
int opt_stretch_amt;
int opt_stretch_sign;
std::deque<int16_t> output_queue_decode;
int opt_udp_port;
/* local methods */
void append_imbe_codeword(bit_vector& frame_body, int16_t frame_vector[], unsigned int& codeword_ct);
void rxchar(char c);
void compress_frame(int16_t snd[]);
void add_sample(int16_t samp);
void compress_samp(int16_t samp);
void init_sock(char* udp_host, int udp_port);
bool opt_encode_flag;
p25p1_voice_encode p1voice_encode;
p25p1_voice_decode p1voice_decode;
};
} // namespace op25_repeater

View File

@ -45,4 +45,3 @@ GR_ADD_TEST(qa_vocoder ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/qa_vocod
GR_ADD_TEST(qa_gardner_costas_cc ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/qa_gardner_costas_cc.py)
GR_ADD_TEST(qa_p25_frame_assembler ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/qa_p25_frame_assembler.py)
GR_ADD_TEST(qa_fsk4_slicer_fb ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/qa_fsk4_slicer_fb.py)
GR_ADD_TEST(qa_p25p2_frame ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/qa_p25p2_frame.py)

View File

@ -12,7 +12,6 @@
#include "op25_repeater/gardner_costas_cc.h"
#include "op25_repeater/p25_frame_assembler.h"
#include "op25_repeater/fsk4_slicer_fb.h"
#include "op25_repeater/p25p2_frame.h"
%}
%include "op25_repeater/vocoder.h"
@ -25,5 +24,3 @@ GR_SWIG_BLOCK_MAGIC2(op25_repeater, p25_frame_assembler);
%include "op25_repeater/fsk4_slicer_fb.h"
GR_SWIG_BLOCK_MAGIC2(op25_repeater, fsk4_slicer_fb);
%include "op25_repeater/p25p2_frame.h"
GR_SWIG_BLOCK_MAGIC2(op25_repeater, p25p2_frame);