Add data_unit_handler classes. Simplify top-level.

git-svn-id: http://op25.osmocom.org/svn/trunk@149 65a5c917-d112-43f1-993d-58c26a4786be
This commit is contained in:
stevie 2009-06-25 20:27:44 +00:00
parent 977c9a6e94
commit 80565d779b
13 changed files with 639 additions and 210 deletions

View File

@ -66,6 +66,10 @@ _op25_la_SOURCES = \
ldu2.cc \
pdu.cc \
tdu.cc \
data_unit_handler.cc \
logfile_du_handler.cc \
snapshot_du_handler.cc \
sniffer_du_handler.cc \
imbe_decoder.cc \
dummy_imbe_decoder.cc \
offline_imbe_decoder.cc \

View File

@ -38,16 +38,29 @@ abstract_data_unit::~abstract_data_unit()
{
}
uint16_t
abstract_data_unit::data_size() const
void
abstract_data_unit::correct_errors()
{
return (7 + frame_size_max()) >> 3;
if(is_complete()) {
correct_errors(d_frame_body);
} else {
ostringstream msg;
msg << "cannot correct errors - frame is not complete" << endl;
msg << "(size now: " << frame_size() << ", expected size: " << frame_size_max() << ")" << endl;
msg << "func: " << __PRETTY_FUNCTION__ << endl;
msg << "file: " << __FILE__ << endl;
msg << "line: " << __LINE__ << endl;
throw logic_error(msg.str());
}
}
void
abstract_data_unit::extend(dibit d)
{
if(frame_size_max() <= frame_size()) {
if(frame_size() < frame_size_max()) {
d_frame_body.push_back(d & 0x2);
d_frame_body.push_back(d & 0x1);
} else {
ostringstream msg;
msg << "cannot extend frame " << endl;
msg << "(size now: " << frame_size() << ", expected size: " << frame_size_max() << ")" << endl;
@ -56,40 +69,71 @@ abstract_data_unit::extend(dibit d)
msg << "line: " << __LINE__ << endl;
throw length_error(msg.str());
}
d_frame_body.push_back(d & 0x2);
d_frame_body.push_back(d & 0x1);
}
size_t
abstract_data_unit::decode(size_t msg_sz, uint8_t *msg, imbe_decoder& imbe, float_queue& audio)
abstract_data_unit::decode_audio(imbe_decoder& imbe, float_queue& audio)
{
if(!is_complete()) {
size_t n = 0;
if(is_complete()) {
n = decode_audio(d_frame_body, imbe, audio);
} else {
ostringstream msg;
msg << "cannot decode frame body - frame is not complete" << endl;
msg << "cannot decode audio - frame is not complete" << endl;
msg << "(size now: " << frame_size() << ", expected size: " << frame_size_max() << ")" << endl;
msg << "func: " << __PRETTY_FUNCTION__ << endl;
msg << "file: " << __FILE__ << endl;
msg << "line: " << __LINE__ << endl;
throw logic_error(msg.str());
}
if(msg_sz != data_size()) {
return n;
}
size_t
abstract_data_unit::decode_frame(size_t msg_sz, uint8_t *msg)
{
return decode_frame(d_frame_body, msg_sz, msg);
}
size_t
abstract_data_unit::decode_frame(const_bit_vector& frame_body, size_t msg_sz, uint8_t *msg)
{
size_t n = 0;
if(is_complete()) {
if(size() <= msg_sz) {
n = extract(frame_body, 0, static_cast<int>(frame_body.size()), msg);
} else {
ostringstream msg;
msg << "cannot decode frame body ";
msg << "(msg size: " << msg_sz << ", actual size: " << size() << ")" << endl;
msg << "func: " << __PRETTY_FUNCTION__ << endl;
msg << "file: " << __FILE__ << endl;
msg << "line: " << __LINE__ << endl;
throw length_error(msg.str());
}
} else {
ostringstream msg;
msg << "cannot decode frame body ";
msg << "(msg size: " << msg_sz << ", expected size: " << data_size() << ")" << endl;
msg << "cannot decode frame - frame is not complete" << endl;
msg << "(size now: " << frame_size() << ", expected size: " << frame_size_max() << ")" << endl;
msg << "func: " << __PRETTY_FUNCTION__ << endl;
msg << "file: " << __FILE__ << endl;
msg << "line: " << __LINE__ << endl;
throw length_error(msg.str());
throw logic_error(msg.str());
}
correct_errors(d_frame_body);
decode_audio(d_frame_body, imbe, audio);
return decode_body(d_frame_body, msg_sz, msg);
return n;
}
bool
abstract_data_unit::is_complete() const
{
return d_frame_body.size() >= frame_size_max();
return frame_size() >= frame_size_max();
}
uint16_t
abstract_data_unit::size() const
{
return (7 + frame_size_max()) >> 3;
}
std::string
@ -127,24 +171,12 @@ abstract_data_unit::decode_audio(const_bit_vector& frame_body, imbe_decoder& imb
return 0;
}
size_t
abstract_data_unit::decode_body(const_bit_vector& frame_body, size_t msg_sz, uint8_t *msg)
{
return extract(frame_body, 0, static_cast<int>(frame_body.size()), msg);
}
const_bit_vector&
abstract_data_unit::frame_body() const
{
return d_frame_body;
}
uint16_t
abstract_data_unit::frame_size_max() const
{
return frame_size_max();
}
uint16_t
abstract_data_unit::frame_size() const
{

View File

@ -42,36 +42,51 @@ public:
virtual ~abstract_data_unit();
/**
* Returns the size (in octets) of the error-corrected and
* de-interleaved data_unit.
* Apply error correction to this data_unit.
*
* \return The expected size (in bits) of this data_unit.
* \precondition is_complete() == true.
*/
virtual uint16_t data_size() const;
virtual void correct_errors();
/**
* Decode compressed audio using the supplied imbe_decoder and
* writes output to audio.
*
* \precondition is_complete() == true.
* \param imbe The imbe_decoder to use to generate the audio.
* \param audio A deque<float> to which the audio (if any) is appended.
* \return The number of samples written to audio.
*/
virtual size_t decode_audio(imbe_decoder& imbe, float_queue& audio);
/**
* Decode the frame into an octet vector.
*
* \precondition is_complete() == true.
* \param msg_sz The size of the message buffer.
* \param msg A pointer to the message buffer.
* \return The number of octets written to msg.
*/
virtual size_t decode_frame(size_t msg_sz, uint8_t *msg);
/**
* Dump this data unit in human readable format to stream s.
*
* \param s The stream to write on
*/
virtual void dump(std::ostream& os) const;
/**
* Extends this data_unit with the specified dibit. If this
* data_unit is already complete a range_error is thrown.
*
* \precondition is_complete() == false.
* \param d The dibit to extend the frame with.
* \throws range_error When the frame already is at its maximum size.
* \return true when the frame is complete otherwise false.
*/
virtual void extend(dibit d);
/**
* Decode, apply error correction and write the decoded frame
* contents to msg. For voice frames decoding causes the compressed
* audio to be decoded using the supplied imbe_decoder.
*
* \param msg_sz The size of the message buffer.
* \param msg A pointer to the message buffer.
* \param imbe The imbe_decoder to use to generate the audio.
* \param audio A float_queue to which the audio (if any) is written.
* \return The number of octets written to msg.
*/
virtual size_t decode(size_t msg_sz, uint8_t *msg, imbe_decoder& imbe, float_queue& audio);
/**
* Tests whether this data unit has enough data to begin decoding.
*
@ -80,22 +95,23 @@ public:
*/
virtual bool is_complete() const;
/**
* Returns the size (in octets) of this data_unit.
*
* \return The size (in octets) of this data_unit.
*/
virtual uint16_t size() const;
/**
* Return a snapshot of the key fields from this frame in a manner
* suitable for display by the UI. The string is encoded as a
* pickled Python dictionary.
*
* \precondition is_complete() == true.
* \return A string containing the fields to display.
*/
virtual std::string snapshot() const;
/**
* Dump this data unit in human readable format to stream s.
*
* \param s The stream to write on
*/
virtual void dump(std::ostream& os) const;
protected:
/**
@ -116,6 +132,7 @@ protected:
* Decode compressed audio using the supplied imbe_decoder and
* writes output to audio.
*
* \precondition is_complete() == true.
* \param frame_body The const_bit_vector to decode.
* \param imbe The imbe_decoder to use to generate the audio.
* \param audio A deque<float> to which the audio (if any) is appended.
@ -131,7 +148,7 @@ protected:
* \param msg A pointer to where the data unit content will be written.
* \return The number of octets written to msg.
*/
virtual size_t decode_body(const_bit_vector& frame_body, size_t msg_sz, uint8_t *msg);
virtual size_t decode_frame(const_bit_vector& frame_body, size_t msg_sz, uint8_t *msg);
/**
* Returns a string describing the Data Unit ID (DUID).

View File

@ -63,30 +63,45 @@ public:
virtual ~data_unit();
/**
* Returns the size (in octets) of the error-corrected and
* de-interleaved data_unit.
* Apply error correction to this data_unit.
*
* \return The expected size (in bits) of this data_unit.
* \precondition is_complete() == true.
*/
virtual uint16_t data_size() const = 0;
virtual void correct_errors() = 0;
/**
* Decode, apply error correction and write the decoded frame
* contents to msg. For voice frames decoding causes the compressed
* audio to be decoded using the supplied imbe_decoder.
* Decode compressed audio using the supplied imbe_decoder and
* writes output to audio.
*
* \precondition is_complete() == true.
* \param imbe The imbe_decoder to use to generate the audio.
* \param audio A deque<float> to which the audio (if any) is appended.
* \return The number of samples written to audio.
*/
virtual size_t decode_audio(imbe_decoder& imbe, float_queue& audio) = 0;
/**
* Decode the frame into an octet vector.
*
* \precondition is_complete() == true.
* \param msg_sz The size of the message buffer.
* \param msg A pointer to the message buffer.
* \param imbe The imbe_decoder to use to generate audio.
* \param audio A float_queue to which the audio (if any) is written.
* \return The number of octets written to msg.
*/
virtual size_t decode(size_t msg_sz, uint8_t *msg, imbe_decoder& imbe, float_queue& audio) = 0;
virtual size_t decode_frame(size_t msg_sz, uint8_t *msg) = 0;
/**
* Dump this data unit in human readable format to stream s.
*
* \param s The stream to write on
*/
virtual void dump(std::ostream& os) const = 0;
/**
* Extends this data_unit with the specified dibit. If this
* data_unit is already complete a range_error is thrown.
*
* \precondition is_complete() == false.
* \param d The dibit to extend the frame with.
* \throws range_error When the frame already is at its maximum size.
* \return true when the frame is complete otherwise false.
@ -94,13 +109,21 @@ public:
virtual void extend(dibit d) = 0;
/**
* Tests whether this data unit has enough data to begin decoding.
* Tests whether this data unit is complete.
*
* \return true when this data_unit is complete; otherwise returns
* false.
* \ see extend()
*/
virtual bool is_complete() const = 0;
/**
* Returns the size (in octets) of the data_unit.
*
* \return The actual size (in octets) of this data_unit.
*/
virtual uint16_t size() const = 0;
/**
* Return a snapshot of the key fields from this frame in a manner
* suitable for display by the UI. The string is encoded using the
@ -111,13 +134,6 @@ public:
*/
virtual std::string snapshot() const = 0;
/**
* Dump this data unit in human readable format to stream s.
*
* \param s The stream to write on
*/
virtual void dump(std::ostream& os) const = 0;
protected:
/**

View File

@ -50,17 +50,31 @@ std::string
hdu::snapshot() const
{
ostringstream os;
os << "(dp0" << endl;
// DUID
os << "(dp0" << endl;
os << "S'duid'" << endl;
os << "p1" << endl;
os << "S'" << duid_str() << "'" << endl;
os << "p2" << endl;
os << "p1" << endl;
os << "S'" << duid_str() << "'" << endl;
// NAC
os << "S'nac'" << endl;
os << "p3" << endl;
os << "p2" << endl;
os << "sS'nac'" << endl;
os << "p3" << endl;
os << "S'" << nac_str() << "'" << endl;
os << "p4" << endl;
#if 0
// MI
os << "p4" << endl;
os << "sS'mi'" << endl;
os << "p5" << endl;
os << "S'" << mi_str() << "'" << endl;
#endif
os << "p4" << endl;
os << "s." << endl;
#if 0
// Source
os << "S'source'" << endl;
os << "p5" << endl;
@ -82,26 +96,22 @@ hdu::snapshot() const
os << "S'" << mfid_str() << "'" << endl;
os << "p12" << endl;
// ALGID
os << "p3" << endl;
os << "S'algid'" << endl;
os << "p13" << endl;
os << "S'" << algid_str() << "'" << endl;
os << "p14" << endl;
os << "p4" << endl;
os << "sS'" << algid_str() << "'" << endl;
// KID
os << "S'kid'" << endl;
os << "p15" << endl;
os << "S''" << endl;
os << "p16" << endl;
// MI
os << "S'mi'" << endl;
os << "p17" << endl;
os << "S'" << mi_str() << "'" << endl;
os << "p18" << endl;
// TGID
os << "S'tgid'" << endl;
os << "p19" << endl;
os << "S''" << endl;
os << "p20" << endl;
os << "s.";
#endif
return os.str();
}

View File

@ -0,0 +1,47 @@
/* -*- C++ -*- */
/*
* Copyright 2008 Steve Glass
*
* This file is part of OP25.
*
* OP25 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.
*
* OP25 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 OP25; see the file COPYING. If not, write to the Free
* Software Foundation, Inc., 51 Franklin Street, Boston, MA
* 02110-1301, USA.
*/
#include <logfile_du_handler.h>
#include <iomanip>
#include <stdint.h>
using namespace std;
logfile_du_handler::logfile_du_handler(data_unit_handler_sptr next, const char *filename) :
data_unit_handler(next),
d_log(filename)
{
}
logfile_du_handler::~logfile_du_handler()
{
d_log.flush();
d_log.close();
}
void
logfile_du_handler::handle(data_unit_sptr du)
{
du->dump(d_log);
data_unit_handler::handle(du);
}

View File

@ -0,0 +1,68 @@
/* -*- C++ -*- */
/*
* Copyright 2008 Steve Glass
*
* This file is part of OP25.
*
* OP25 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.
*
* OP25 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 OP25; see the file COPYING. If not, write to the Free
* Software Foundation, Inc., 51 Franklin Street, Boston, MA
* 02110-1301, USA.
*/
#ifndef INCLUDED_LOGFILE_DU_HANDLER_H
#define INCLUDED_LOGFILE_DU_HANDLER_H
#include <data_unit_handler.h>
#include <boost/noncopyable.hpp>
#include <fstream>
/**
* logfile_data_unit_handler writes frames to a log file for later inspection.
*/
class logfile_du_handler : public data_unit_handler
{
public:
/**
* logfile_du_handler constructor.
*
* \param next The next data_unit_handler in the chain.
* \param filename The path to the log file.
*/
logfile_du_handler(data_unit_handler_sptr next, const char *filename);
/**
* logfile_du_handler virtual destructor.
*/
virtual ~logfile_du_handler();
/**
* Handle a received P25 frame.
*
* \param next The next data_unit_handler in this chain.
*/
virtual void handle(data_unit_sptr du);
private:
/**
* The msg_queue to which decoded frames are written.
*/
std::ofstream d_log;
};
#endif /* INCLUDED_LOGFILE_DU_HANDLER_H */

View File

@ -26,16 +26,12 @@
#endif
#include <algorithm>
#include <fstream>
#include <gr_io_signature.h>
#include <gr_message.h>
#include <iostream>
#include <net/if.h>
#include <linux/if_tun.h>
#include <itpp/comm/bch.h>
#include <logfile_du_handler.h>
#include <op25_decoder_ff.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <snapshot_du_handler.h>
using namespace std;
@ -47,9 +43,6 @@ op25_make_decoder_ff(gr_msg_queue_sptr msgq)
op25_decoder_ff::~op25_decoder_ff()
{
if(-1 != d_tap) {
close(d_tap);
}
}
void
@ -94,59 +87,20 @@ op25_decoder_ff::general_work(int nof_output_items, gr_vector_int& nof_input_ite
const char*
op25_decoder_ff::device_name() const
{
return d_tap_device.c_str();
return d_sniffer_du_handler->device_name();
}
op25_decoder_ff::op25_decoder_ff(gr_msg_queue_sptr msgq) :
gr_block("decoder_ff", gr_make_io_signature(1, 1, sizeof(float)), gr_make_io_signature(0, 1, sizeof(float))),
d_msgq(msgq),
d_state(SYNCHRONIZING),
d_bad_NIDs(0),
d_bch(63, 16, 11,"6 3 3 1 1 4 1 3 6 7 2 3 5 4 5 3", true),
d_data_unit(),
d_data_units(0),
d_data_unit_handler(),
d_frame_hdr(),
d_imbe(imbe_decoder::make_imbe_decoder()),
d_logfile(new ofstream("P25DEMOD.OUT")),
d_tap(-1),
d_tap_device("not available"),
d_unrecognized(0)
d_state(SYNCHRONIZING),
d_sniffer_du_handler(NULL)
{
d_tap = open("/dev/net/tun", O_WRONLY);
if(-1 != d_tap) {
struct ifreq ifr;
memset(&ifr, 0, sizeof(ifr));
ifr.ifr_flags = IFF_TAP | IFF_NO_PI;
strncpy(ifr.ifr_name, "p25-%d", IFNAMSIZ);
if(0 == ioctl(d_tap, TUNSETIFF, &ifr)) {
int s = socket(AF_INET, SOCK_DGRAM, 0);
if(0 <= s) {
if(0 == ioctl(s, SIOCGIFFLAGS, &ifr)) {
ifr.ifr_flags = IFF_UP;
if(0 == ioctl(s, SIOCSIFFLAGS, &ifr)) {
d_tap_device = ifr.ifr_name;
} else {
perror("ioctl(d_tap, SIOCSIFFLAGS, &ifr)");
close(d_tap);
d_tap = -1;
}
} else {
perror("ioctl(d_tap, SIOCGIFFLAGS, &ifr)");
close(d_tap);
d_tap = -1;
}
close(s);
} else {
perror("socket(AF_INET, SOCK_DGRAM, 0)");
close(d_tap);
d_tap = -1;
}
} else {
perror("ioctl(d_tap, TUNSETIFF, &ifr)");
close(d_tap);
d_tap = -1;
}
}
d_sniffer_du_handler = new sniffer_du_handler(d_data_unit_handler);
d_data_unit_handler = data_unit_handler_sptr(d_sniffer_du_handler);
d_data_unit_handler = data_unit_handler_sptr(new snapshot_du_handler(d_data_unit_handler, msgq));
}
bool
@ -187,56 +141,20 @@ op25_decoder_ff::identified()
size_t NID_SZ = sizeof(NID) / sizeof(NID[0]);
itpp::bvec b(63), zeroes(16);
itpp::BCH bch(63, 16, 11,"6 3 3 1 1 4 1 3 6 7 2 3 5 4 5 3", true);
swab(d_frame_hdr, NID, NID_SZ, b, 0);
b = d_bch.decode(b);
b = bch.decode(b);
if(b != zeroes) {
b = d_bch.encode(b);
b = bch.encode(b);
unswab(b, 0, d_frame_hdr, NID, NID_SZ);
d_data_unit = data_unit::make_data_unit(d_frame_hdr);
} else {
cout << __PRETTY_FUNCTION__ << ":" << __LINE__ << "NID decode error" << endl;
cout << "\tNID=";
for(int i = 0; i < b.size(); ++i) {
cout << b[i] << ",";
}
cout << endl;
++d_bad_NIDs;
data_unit_sptr null;
d_data_unit = null;
}
return d_data_unit;
}
void
op25_decoder_ff::process_data_unit()
{
const size_t msg_hdr_sz = 14;
const size_t msg_sz = d_data_unit->data_size();
const size_t total_msg_sz = msg_hdr_sz + msg_sz;
uint8_t msg_data[total_msg_sz];
if(d_data_unit->decode(msg_sz, msg_data + msg_hdr_sz, *d_imbe, d_audio)) {
if(-1 != d_tap) {
memset(&msg_data[0], 0xff, 6);
memset(&msg_data[6], 0x00, 6);
memset(&msg_data[12], 0xff, 2);
write(d_tap, msg_data, total_msg_sz);
}
if(d_msgq) {
string snapshot(d_data_unit->snapshot());
if(snapshot.size() > 0) {
const size_t snapshot_sz = snapshot.size() + 1;
gr_message_sptr msg = gr_make_message(/*type*/0, /*arg1*/++d_data_units, /*arg2*/0, snapshot_sz);
char *snapshot_data = reinterpret_cast<char*>(msg->msg());
memcpy(snapshot_data, snapshot.c_str(), snapshot_sz);
d_msgq->handle(msg);
}
}
}
d_data_unit->dump(*d_logfile);
}
void
op25_decoder_ff::receive_symbol(dibit d)
{
@ -254,11 +172,10 @@ op25_decoder_ff::receive_symbol(dibit d)
}
break;
case IDENTIFYING:
if(114 <= frame_hdr_sz) {
if(114 == frame_hdr_sz) {
if(identified()) {
d_state = READING;
} else {
++d_unrecognized;
d_state = SYNCHRONIZING;
}
}
@ -266,7 +183,7 @@ op25_decoder_ff::receive_symbol(dibit d)
case READING:
d_data_unit->extend(d);
if(d_data_unit->is_complete()) {
process_data_unit();
d_data_unit_handler->handle(d_data_unit);
data_unit_sptr null;
d_data_unit = null;
d_state = SYNCHRONIZING;

View File

@ -26,12 +26,10 @@
#include <boost/scoped_ptr.hpp>
#include <data_unit.h>
#include <data_unit_handler.h>
#include <gr_block.h>
#include <gr_msg_queue.h>
#include <imbe_decoder.h>
#include <iosfwd>
#include <itpp/comm/bch.h>
#include <string>
#include <sniffer_du_handler.h>
typedef boost::shared_ptr<class op25_decoder_ff> op25_decoder_ff_sptr;
@ -106,14 +104,6 @@ private:
*/
bool identified();
/**
* Process the completed data_unit (pointed at by
* d_data_unit). This decodes the data_unit and then sends the
* result to wireshark. Attempts to snapshot the data_unit and, if
* successfull, it passes the result to the user interface.
*/
void process_data_unit();
/**
* Handle a received symbol.
*
@ -122,19 +112,36 @@ private:
void receive_symbol(dibit d);
private:
gr_msg_queue_sptr d_msgq;
enum { SYNCHRONIZING, IDENTIFYING, READING } d_state;
float_queue d_audio;
uint32_t d_bad_NIDs;
itpp::BCH d_bch;
/**
* When d_state == READING the current data unit, otherwise null.
*/
data_unit_sptr d_data_unit;
uint32_t d_data_units;
/**
* The head of a chain of data_unit_handler instances.
*/
data_unit_handler_sptr d_data_unit_handler;
/**
* A bit_queue used to correlate the FS.
*/
bit_queue d_frame_hdr;
imbe_decoder_sptr d_imbe;
boost::scoped_ptr<std::ostream> d_logfile;
int32_t d_tap;
std::string d_tap_device;
uint32_t d_unrecognized;
/**
* The message queue used to send snapshots to the UI.
*/
gr_msg_queue_sptr d_msgq;
/**
* Valid states for the decoder state model.
*/
enum { SYNCHRONIZING, IDENTIFYING, READING } d_state;
/**
* The sniffer (TUN/TAP) data unit handler.
*/
sniffer_du_handler *d_sniffer_du_handler;
};

View File

@ -0,0 +1,54 @@
/* -*- C++ -*- */
/*
* Copyright 2008 Steve Glass
*
* This file is part of OP25.
*
* OP25 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.
*
* OP25 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 OP25; see the file COPYING. If not, write to the Free
* Software Foundation, Inc., 51 Franklin Street, Boston, MA
* 02110-1301, USA.
*/
#include <snapshot_du_handler.h>
#include <string>
using std::string;
snapshot_du_handler::snapshot_du_handler(data_unit_handler_sptr next, gr_msg_queue_sptr msgq) :
data_unit_handler(next),
d_data_units(0),
d_msgq(msgq)
{
}
snapshot_du_handler::~snapshot_du_handler()
{
}
void
snapshot_du_handler::handle(data_unit_sptr du)
{
if(d_msgq) {
string snapshot(du->snapshot());
if(snapshot.size() > 0) {
const size_t snapshot_sz = snapshot.size() + 1;
gr_message_sptr msg = gr_make_message(/*type*/0, /*arg1*/++d_data_units, /*arg2*/0, snapshot_sz);
char *snapshot_data = reinterpret_cast<char*>(msg->msg());
memcpy(snapshot_data, snapshot.c_str(), snapshot_sz);
d_msgq->handle(msg);
}
}
data_unit_handler::handle(du);
}

View File

@ -0,0 +1,76 @@
/* -*- C++ -*- */
/*
* Copyright 2008 Steve Glass
*
* This file is part of OP25.
*
* OP25 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.
*
* OP25 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 OP25; see the file COPYING. If not, write to the Free
* Software Foundation, Inc., 51 Franklin Street, Boston, MA
* 02110-1301, USA.
*/
#ifndef INCLUDED_SNAPSHOT_DU_HANDLER_H
#define INCLUDED_SNAPSHOT_DU_HANDLER_H
#include <data_unit_handler.h>
#include <gr_msg_queue.h>
#include <boost/noncopyable.hpp>
/**
* snapshot_du_handler. Writes traffic snapshots to a msg_queue based
* on the HDU frame contents. The format used is that of a pickled
* python dictionary allowing the other end of the queue to pick only
* those fields of interest and ignore the rest.
*/
class snapshot_du_handler : public data_unit_handler
{
public:
/**
* snapshot_du_handler constructor.
*
* \param next The next data_unit_handler in the chain.
* \param msgq A non-null msg_queue_sptr to the msg_queue to use.
*/
snapshot_du_handler(data_unit_handler_sptr next, gr_msg_queue_sptr msgq);
/**
* snapshot_du_handler virtual destructor.
*/
virtual ~snapshot_du_handler();
/**
* Handle a received P25 frame.
*
* \param du A non-null data_unit_sptr to handle.
*/
virtual void handle(data_unit_sptr du);
private:
/**
* Count of the data units seen so far.
*/
uint32_t d_data_units;
/**
* The msg_queue to which decoded frames are written.
*/
gr_msg_queue_sptr d_msgq;
};
#endif /* INCLUDED_SNAPSHOT_DU_HANDLER_H */

View File

@ -0,0 +1,103 @@
/* -*- C++ -*- */
/*
* Copyright 2008, 2009 Steve Glass
*
* This file is part of OP25.
*
* OP25 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.
*
* OP25 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 OP25; see the file COPYING. If not, write to the Free
* Software Foundation, Inc., 51 Franklin Street, Boston, MA
* 02110-1301, USA.
*/
#include "sniffer_du_handler.h"
#include <net/if.h>
#include <linux/if_tun.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <fcntl.h>
sniffer_du_handler::sniffer_du_handler(data_unit_handler_sptr next) :
data_unit_handler(next),
d_tap(-1),
d_tap_device("unavailable")
{
d_tap = open("/dev/net/tun", O_WRONLY);
if(-1 != d_tap) {
struct ifreq ifr;
memset(&ifr, 0, sizeof(ifr));
ifr.ifr_flags = IFF_TAP | IFF_NO_PI;
strncpy(ifr.ifr_name, "p25-%d", IFNAMSIZ);
if(0 == ioctl(d_tap, TUNSETIFF, &ifr)) {
// force the device into the UP state
int s = socket(AF_INET, SOCK_DGRAM, 0);
if(0 <= s) {
if(0 == ioctl(s, SIOCGIFFLAGS, &ifr)) {
ifr.ifr_flags = IFF_UP;
if(0 == ioctl(s, SIOCSIFFLAGS, &ifr)) {
d_tap_device = ifr.ifr_name;
} else {
perror("ioctl(d_tap, SIOCSIFFLAGS, &ifr)");
close(d_tap);
d_tap = -1;
}
} else {
perror("ioctl(d_tap, SIOCGIFFLAGS, &ifr)");
close(d_tap);
d_tap = -1;
}
close(s);
} else {
perror("socket(AF_INET, SOCK_DGRAM, 0)");
close(d_tap);
d_tap = -1;
}
} else {
perror("ioctl(d_tap, TUNSETIFF, &ifr)");
close(d_tap);
d_tap = -1;
}
}
}
sniffer_du_handler::~sniffer_du_handler()
{
if(-1 != d_tap) {
close(d_tap);
}
}
const char*
sniffer_du_handler::device_name() const
{
return d_tap_device.c_str();
}
void
sniffer_du_handler::handle(data_unit_sptr du)
{
if(-1 != d_tap) {
const size_t du_sz = du->size();
const size_t tap_hdr_sz = 14;
const size_t tap_sz = du_sz + tap_hdr_sz;
uint8_t tap[tap_sz];
memset(&tap[0], 0x00, 6);
memset(&tap[6], 0x00, 6);
memset(&tap[12], 0x00, 2);
du->decode_frame(du_sz, &tap[tap_hdr_sz]);
write(d_tap, tap, tap_sz);
}
data_unit_handler::handle(du);
}

View File

@ -0,0 +1,78 @@
/* -*- C++ -*- */
/*
* Copyright 2008 Steve Glass
*
* This file is part of OP25.
*
* OP25 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.
*
* OP25 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 OP25; see the file COPYING. If not, write to the Free
* Software Foundation, Inc., 51 Franklin Street, Boston, MA
* 02110-1301, USA.
*/
#ifndef INCLUDED_SNIFFER_DU_HANDLER_H
#define INCLUDED_SNIFFER_DU_HANDLER_H
#include <data_unit_handler.h>
#include <string>
/**
* sniffer_du_handler interface. Writes each data_unit to a TUN/TAP device.
*/
class sniffer_du_handler : public data_unit_handler
{
public:
/**
* sniffer_du_handler constructor.
*
* \param next The next data_unit_handler.
*/
explicit sniffer_du_handler(data_unit_handler_sptr next);
/**
* sniffer_du_handler virtual destructor.
*/
virtual ~sniffer_du_handler();
/**
* Handle a received P25 frame.
*
* \param du A non-null data_unit_sptr to handle.
*/
virtual void handle(data_unit_sptr du);
/**
* Return a pointer to a string naming the TUN/TAP device.
*
* \return A pointer to a NUL-terminated character string.
*/
const char *device_name() const;
private:
/**
* file descriptor to the TUN/TAP device.
*/
int32_t d_tap;
/**
* A string naming the TUN/TAP device.
*/
std::string d_tap_device;
};
#endif /* INCLUDED_SNIFFER_DU_HANDLER_H */