From 431bc45d8ac21cc1f9087b9ce17035b7b6c9d15d Mon Sep 17 00:00:00 2001 From: stevie Date: Tue, 18 Jan 2011 03:34:18 +0000 Subject: [PATCH] Add missing p25cai class (lost during check-in). git-svn-id: http://op25.osmocom.org/svn/trunk@252 65a5c917-d112-43f1-993d-58c26a4786be --- decoder/src/lib/p25cai_du_handler.cc | 85 ++++++++++++++++++++++++++++ decoder/src/lib/p25cai_du_handler.h | 84 +++++++++++++++++++++++++++ 2 files changed, 169 insertions(+) create mode 100644 decoder/src/lib/p25cai_du_handler.cc create mode 100644 decoder/src/lib/p25cai_du_handler.h diff --git a/decoder/src/lib/p25cai_du_handler.cc b/decoder/src/lib/p25cai_du_handler.cc new file mode 100644 index 0000000..9df8376 --- /dev/null +++ b/decoder/src/lib/p25cai_du_handler.cc @@ -0,0 +1,85 @@ +/* -*- C++ -*- */ + +/* + * Copyright 2008-2011 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 +#include +#include +#include +#include +#include +#include +#include +#include + +using namespace std; + +p25cai_du_handler::p25cai_du_handler(data_unit_handler_sptr next, const char *addr, int port) : + data_unit_handler(next), + d_cai(-1), + d_address("Unavailable") +{ + struct sockaddr_in sin; + sin.sin_family = AF_INET; + sin.sin_port = htons(port); + inet_aton(addr, &sin.sin_addr); + d_cai = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); + if(-1 != d_cai) { + if(-1 == connect(d_cai, (struct sockaddr*) &sin, sizeof(sin))) { + ostringstream address; + address << addr << ":" << port; + d_address = address.str(); + } else { + perror("connect(d_tap, (struct sockaddr*) &sin, sizeof(sin))"); + close(d_cai); + d_cai = -1; + } + } else { + perror("socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)"); + d_cai = -1; + } +} + +p25cai_du_handler::~p25cai_du_handler() +{ + if(-1 != d_cai) { + close(d_cai); + } +} + +const char* +p25cai_du_handler::destination() const +{ + return d_address.c_str(); +} + +void +p25cai_du_handler::handle(data_unit_sptr du) +{ + if(-1 != d_cai) { + const size_t CAI_SZ = du->size(); + uint8_t cai[CAI_SZ]; + du->decode_frame(CAI_SZ, cai); + write(d_cai, cai, CAI_SZ); + } + data_unit_handler::handle(du); +} diff --git a/decoder/src/lib/p25cai_du_handler.h b/decoder/src/lib/p25cai_du_handler.h new file mode 100644 index 0000000..4a601bb --- /dev/null +++ b/decoder/src/lib/p25cai_du_handler.h @@ -0,0 +1,84 @@ +/* -*- C++ -*- */ + +/* + * Copyright 2008-2011 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_P25CAI_DU_HANDLER_H +#define INCLUDED_P25CAI_DU_HANDLER_H + +#include +#include + +/** + * This data_unit_handler forwards received frames using p25cai - a + * straighforward encapsulation of the P25 CAI in a UDP datagram. This + * format is understood by Wireshark and replaces the use of TUN/TAP + * which require root privileges and are not present by default on + * some platforms. + */ +class p25cai_du_handler : public data_unit_handler +{ + +public: + + /** + * p25cai_du_handler constructor. + * + * \param next The next data_unit_handler. + * \param addr The address of the receiver. + * \param port The port number of the receiver. + */ + p25cai_du_handler(data_unit_handler_sptr next, const char *addr, int port); + + /** + * p25cai_du_handler virtual destructor. + */ + virtual ~p25cai_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 identifying the destination address. + * + * \return A pointer to a NUL-terminated character string. + */ + const char *destination() const; + +private: + + /** + * file descriptor for the UDP socket. + */ + int32_t d_cai; + + /** + * A string identifying the address of the receiver. + */ + std::string d_address; + +}; + +#endif /* INCLUDED_P25CAI_DU_HANDLER_H */