Skip to content

Commit

Permalink
AP_Networking: support UDP server, TCP client and TCP server
Browse files Browse the repository at this point in the history
and implement mavlink packetisation and flow control return
  • Loading branch information
tridge committed Nov 27, 2023
1 parent 18c2c30 commit 0beb909
Show file tree
Hide file tree
Showing 4 changed files with 317 additions and 26 deletions.
25 changes: 25 additions & 0 deletions libraries/AP_Networking/AP_Networking.h
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,9 @@ class AP_Networking
enum class NetworkPortType {
NONE = 0,
UDP_CLIENT = 1,
UDP_SERVER = 2,
TCP_CLIENT = 3,
TCP_SERVER = 4,
};

// class for NET_Pn_* parameters
Expand All @@ -199,6 +202,7 @@ class AP_Networking
AP_Networking_IPV4 ip {"0.0.0.0"};
AP_Int32 port;
SocketAPM *sock;
SocketAPM *listen_sock;

bool is_initialized() override {
return true;
Expand All @@ -207,8 +211,18 @@ class AP_Networking
return false;
}

void wait_startup();
void udp_client_init(const uint32_t size_rx, const uint32_t size_tx);
void udp_server_init(const uint32_t size_rx, const uint32_t size_tx);
void tcp_server_init(const uint32_t size_rx, const uint32_t size_tx);
void tcp_client_init(const uint32_t size_rx, const uint32_t size_tx);

void udp_client_loop(void);
void udp_server_loop(void);
void tcp_client_loop(void);
void tcp_server_loop(void);

bool send_receive(void);

private:
bool init_buffers(const uint32_t size_rx, const uint32_t size_tx);
Expand All @@ -222,10 +236,21 @@ class AP_Networking
void _flush() override {}
bool _discard_input() override;

enum flow_control get_flow_control(void) override;

uint32_t bw_in_bytes_per_second() const override {
return 1000000UL;
}

ByteBuffer *readbuffer;
ByteBuffer *writebuffer;
uint32_t last_size_tx;
uint32_t last_size_rx;
bool packetise;
bool connected;
bool have_received;
bool close_on_recv_error;

HAL_Semaphore sem;
};

Expand Down
7 changes: 5 additions & 2 deletions libraries/AP_Networking/AP_Networking_address.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
*/

#include "AP_Networking_Config.h"
#include <arpa/inet.h>

#if AP_NETWORKING_ENABLED

Expand Down Expand Up @@ -68,9 +69,11 @@ void AP_Networking_IPV4::set_default_uint32(uint32_t v)
}
}

const char* AP_Networking_IPV4::get_str() const
const char* AP_Networking_IPV4::get_str()
{
return AP_Networking::convert_ip_to_str(get_uint32());
const auto ip = ntohl(get_uint32());
inet_ntop(AF_INET, &ip, strbuf, sizeof(strbuf));
return strbuf;
}

#endif // AP_NETWORKING_ENABLED
5 changes: 4 additions & 1 deletion libraries/AP_Networking/AP_Networking_address.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,15 @@ class AP_Networking_IPV4
void set_uint32(uint32_t addr);

// return address as a null-terminated string
const char* get_str() const;
const char* get_str();

// set default address from a uint32
void set_default_uint32(uint32_t addr);

static const struct AP_Param::GroupInfo var_info[];

private:
char strbuf[16];
};

/*
Expand Down
Loading

0 comments on commit 0beb909

Please sign in to comment.