kopia lustrzana https://github.com/rpp0/gr-lora
Small corrections
rodzic
91a5bfa97f
commit
b73308f184
|
@ -1,42 +1,28 @@
|
|||
<?xml version="1.0"?>
|
||||
<block>
|
||||
<name>Message Socket Source</name>
|
||||
<key>lora_message_socket_source</key>
|
||||
<category>[LoRa]</category>
|
||||
<import>import lora</import>
|
||||
<make>lora.message_socket_source($addr, $port, $mtu, $payload_len)</make>
|
||||
<name>Message Socket Source</name>
|
||||
<key>lora_message_socket_source</key>
|
||||
<category>[LoRa]</category>
|
||||
<import>import lora</import>
|
||||
<make>lora.message_socket_source($addr, $port)</make>
|
||||
|
||||
<param>
|
||||
<name>UDP IP Address</name>
|
||||
<key>addr</key>
|
||||
<value>"127.0.0.1"</value>
|
||||
<type>string</type>
|
||||
</param>
|
||||
<param>
|
||||
<name>UDP IP Address</name>
|
||||
<key>addr</key>
|
||||
<value>"127.0.0.1"</value>
|
||||
<type>string</type>
|
||||
</param>
|
||||
|
||||
<param>
|
||||
<name>UDP port</name>
|
||||
<key>port</key>
|
||||
<value>40868</value>
|
||||
<type>int</type>
|
||||
</param>
|
||||
<param>
|
||||
<name>UDP Port</name>
|
||||
<key>port</key>
|
||||
<value>40868</value>
|
||||
<type>int</type>
|
||||
</param>
|
||||
|
||||
<param>
|
||||
<name>MTU</name>
|
||||
<key>mtu</key>
|
||||
<value>1500</value>
|
||||
<type>int</type>
|
||||
</param>
|
||||
|
||||
<param>
|
||||
<name>Payload length</name>
|
||||
<key>payload_len</key>
|
||||
<value>100</value>
|
||||
<type>int</type>
|
||||
</param>
|
||||
|
||||
<source>
|
||||
<name>out</name>
|
||||
<type>message</type>
|
||||
</source>
|
||||
<source>
|
||||
<name>out</name>
|
||||
<type>message</type>
|
||||
</source>
|
||||
|
||||
</block>
|
||||
|
|
|
@ -690,8 +690,8 @@ namespace gr {
|
|||
* \ingroup lora
|
||||
*
|
||||
* \details This block creates a UDP server at a specific port
|
||||
* and pass received UDP datagrams as a pmt::pmt_t blob message.
|
||||
* The datagrams sent to the UDP server always contain valid
|
||||
* and passes received UDP datagrams as a pmt::pmt_t blob message.
|
||||
* The datagrams sent to the UDP server should contain a valid
|
||||
* LoRaTAP header, LoRaPHY header and payload
|
||||
*/
|
||||
class LORA_API message_socket_source : virtual public gr::block
|
||||
|
@ -700,16 +700,13 @@ namespace gr {
|
|||
typedef boost::shared_ptr<message_socket_source> sptr;
|
||||
|
||||
/*!
|
||||
* \param addr the address to bind the UDP socket
|
||||
* \param port the UDP port to wait for packets
|
||||
* \param mtu the maximum MTU. Used to pre-allocate a maximum packet size
|
||||
* \param payload_len the payload length in bytes
|
||||
* \param addr The address to bind the UDP socket
|
||||
* \param port The UDP port to wait for packets
|
||||
*/
|
||||
static sptr make(const std::string& addr, uint16_t port, size_t mtu, size_t payload_len);
|
||||
static sptr make(const std::string& addr, uint16_t port);
|
||||
};
|
||||
|
||||
} // namespace lora
|
||||
} // namespace gr
|
||||
|
||||
#endif /* INCLUDED_LORA_MESSAGE_SOCKET_SOURCE_H */
|
||||
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
/* -*- c++ -*- */
|
||||
/*
|
||||
* Copyright 2018 Nikos Karamolegkos.
|
||||
* Copyright 2018 Nikos Karamolegkos, Pieter Robyns.
|
||||
*
|
||||
* This is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
|
@ -27,112 +27,82 @@
|
|||
#include <gnuradio/io_signature.h>
|
||||
#include "message_socket_source_impl.h"
|
||||
|
||||
namespace gr
|
||||
{
|
||||
namespace lora
|
||||
{
|
||||
#define RECEIVE_BUFFER_SIZE 1500
|
||||
|
||||
namespace gr {
|
||||
namespace lora {
|
||||
message_socket_source::sptr
|
||||
message_socket_source::make (const std::string& addr, uint16_t port,
|
||||
size_t mtu, size_t payload_len)
|
||||
{
|
||||
return gnuradio::get_initial_sptr (
|
||||
new message_socket_source_impl (addr, port, mtu, payload_len));
|
||||
message_socket_source::make (const std::string& addr, uint16_t port) {
|
||||
return gnuradio::get_initial_sptr(new message_socket_source_impl(addr, port));
|
||||
}
|
||||
|
||||
/*
|
||||
* The private constructor
|
||||
*/
|
||||
message_socket_source_impl::message_socket_source_impl (
|
||||
const std::string& addr, uint16_t port, size_t mtu, size_t payload_len) :
|
||||
gr::block ("message_socket_source",
|
||||
gr::io_signature::make (0, 0, 0),
|
||||
gr::io_signature::make (0, 0, 0)),
|
||||
d_addr (addr),
|
||||
d_udp_port (port),
|
||||
d_mtu (mtu),
|
||||
d_payload_len (payload_len),
|
||||
d_running (true)
|
||||
message_socket_source_impl::message_socket_source_impl (const std::string& addr, uint16_t port) :
|
||||
gr::block("message_socket_source", gr::io_signature::make (0, 0, 0), gr::io_signature::make (0, 0, 0)),
|
||||
d_addr(addr),
|
||||
d_udp_port(port),
|
||||
d_running(true)
|
||||
{
|
||||
message_port_register_out (pmt::mp ("out"));
|
||||
boost::shared_ptr<boost::thread> (
|
||||
new boost::thread (
|
||||
boost::bind (&message_socket_source_impl::msg_receive_udp,
|
||||
this)));
|
||||
|
||||
message_port_register_out(pmt::mp("out"));
|
||||
boost::shared_ptr<boost::thread>(new boost::thread(boost::bind (&message_socket_source_impl::msg_receive_udp, this)));
|
||||
}
|
||||
|
||||
void
|
||||
message_socket_source_impl::msg_receive_udp ()
|
||||
{
|
||||
int sock;
|
||||
struct sockaddr_in sin;
|
||||
struct sockaddr client_addr;
|
||||
socklen_t client_addr_len;
|
||||
ssize_t ret;
|
||||
uint8_t *buf;
|
||||
uint32_t bytes_num;
|
||||
pmt::pmt_t lora_tap;
|
||||
void message_socket_source_impl::msg_receive_udp() {
|
||||
int sock;
|
||||
struct sockaddr_in sin;
|
||||
struct sockaddr client_addr;
|
||||
socklen_t client_addr_len;
|
||||
ssize_t num_bytes_received;
|
||||
uint8_t buf[RECEIVE_BUFFER_SIZE];
|
||||
|
||||
if ((sock = socket (AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1) {
|
||||
perror ("opening UDP socket");
|
||||
exit (EXIT_FAILURE);
|
||||
}
|
||||
if ((sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1) {
|
||||
perror("Error creating UDP socket");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
memset (&client_addr, 0, sizeof(struct sockaddr));
|
||||
memset (&sin, 0, sizeof(struct sockaddr_in));
|
||||
sin.sin_family = AF_INET;
|
||||
sin.sin_port = htons (d_udp_port);
|
||||
memset(&client_addr, 0, sizeof(struct sockaddr));
|
||||
memset(&sin, 0, sizeof(struct sockaddr_in));
|
||||
sin.sin_family = AF_INET;
|
||||
sin.sin_port = htons(d_udp_port);
|
||||
|
||||
if (inet_aton (d_addr.c_str (), &(sin.sin_addr)) == 0) {
|
||||
printf ("Wrong IP address\n");
|
||||
close (sock);
|
||||
exit (EXIT_FAILURE);
|
||||
}
|
||||
if (inet_aton(d_addr.c_str(), &(sin.sin_addr)) == 0) {
|
||||
printf("Invalid IPv4 address\n");
|
||||
close(sock);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
if (bind (sock, (struct sockaddr *) &sin, sizeof(struct sockaddr_in))
|
||||
== -1) {
|
||||
perror ("UDP bind");
|
||||
close (sock);
|
||||
exit (EXIT_FAILURE);
|
||||
}
|
||||
if(bind(sock, (struct sockaddr *)&sin, sizeof(struct sockaddr_in)) == -1) {
|
||||
perror("Failed to bind UDP port");
|
||||
close(sock);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
/* All good until now. Allocate buffer memory and proceed */
|
||||
buf = new uint8_t[d_mtu];
|
||||
while (d_running) {
|
||||
memset(buf, 0, RECEIVE_BUFFER_SIZE);
|
||||
num_bytes_received = recvfrom(sock, buf, RECEIVE_BUFFER_SIZE, 0, &client_addr, &client_addr_len);
|
||||
if (num_bytes_received > 0) {
|
||||
pmt::pmt_t lora_tap = pmt::make_blob(buf, num_bytes_received);
|
||||
message_port_pub(pmt::mp("out"), lora_tap);
|
||||
} else {
|
||||
perror("Error receiving UDP datagram");
|
||||
close(sock);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
}
|
||||
|
||||
while (d_running) {
|
||||
ret = recvfrom (sock, buf, d_mtu, 0, &client_addr, &client_addr_len);
|
||||
if (ret > 0) {
|
||||
bytes_num = (uint32_t) ret;
|
||||
if (bytes_num
|
||||
== (sizeof(loratap_header_t) + sizeof(loraphy_header_t)
|
||||
+ d_payload_len)) {
|
||||
lora_tap = pmt::make_blob (buf, bytes_num);
|
||||
message_port_pub (pmt::mp ("out"), lora_tap);
|
||||
}
|
||||
|
||||
}
|
||||
else {
|
||||
perror ("UDP recvfrom");
|
||||
close (sock);
|
||||
delete[] buf;
|
||||
exit (EXIT_FAILURE);
|
||||
}
|
||||
}
|
||||
close (sock);
|
||||
delete[] buf;
|
||||
exit (EXIT_SUCCESS);
|
||||
close(sock);
|
||||
exit(EXIT_SUCCESS);
|
||||
}
|
||||
|
||||
/*
|
||||
* Our virtual destructor.
|
||||
*/
|
||||
message_socket_source_impl::~message_socket_source_impl ()
|
||||
{
|
||||
d_running = false;
|
||||
d_thread->join ();
|
||||
message_socket_source_impl::~message_socket_source_impl() {
|
||||
d_running = false;
|
||||
d_thread->join();
|
||||
}
|
||||
|
||||
} /* namespace lora */
|
||||
} /* namespace gr */
|
||||
|
||||
|
|
|
@ -31,27 +31,21 @@ namespace gr
|
|||
namespace lora
|
||||
{
|
||||
|
||||
class message_socket_source_impl : public message_socket_source
|
||||
{
|
||||
class message_socket_source_impl : public message_socket_source {
|
||||
private:
|
||||
const std::string d_addr;
|
||||
const uint16_t d_udp_port;
|
||||
const size_t d_mtu;
|
||||
const size_t d_payload_len;
|
||||
bool d_running;
|
||||
boost::shared_ptr<boost::thread> d_thread;
|
||||
const std::string d_addr;
|
||||
const uint16_t d_udp_port;
|
||||
bool d_running;
|
||||
boost::shared_ptr<boost::thread> d_thread;
|
||||
|
||||
|
||||
void msg_receive_udp();
|
||||
void msg_receive_udp();
|
||||
|
||||
public:
|
||||
message_socket_source_impl (const std::string& addr, uint16_t port,
|
||||
size_t mtu, size_t payload_len);
|
||||
~message_socket_source_impl ();
|
||||
message_socket_source_impl(const std::string& addr, uint16_t port);
|
||||
~message_socket_source_impl();
|
||||
};
|
||||
|
||||
} // namespace lora
|
||||
} // namespace gr
|
||||
|
||||
#endif /* INCLUDED_LORA_MESSAGE_SOCKET_SOURCE_IMPL_H */
|
||||
|
||||
|
|
Ładowanie…
Reference in New Issue