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 4644be4 commit 55b81b4
Show file tree
Hide file tree
Showing 6 changed files with 372 additions and 35 deletions.
2 changes: 1 addition & 1 deletion libraries/AP_Networking/AP_Networking.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ const AP_Param::GroupInfo AP_Networking::var_info[] = {
// @Param: TESTS
// @DisplayName: Test enable flags
// @Description: Enable/Disable networking tests
// @Bitmask: 0:UDP echo test,1:TCP echo test
// @Bitmask: 0:UDP echo test,1:TCP echo test, 2:TCP discard test
// @RebootRequired: True
// @User: Advanced
AP_GROUPINFO("TESTS", 7, AP_Networking, param.tests, 0),
Expand Down
31 changes: 30 additions & 1 deletion 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,11 +211,22 @@ class AP_Networking
return false;
}

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

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);
void thread_create(AP_HAL::MemberProc);

uint32_t txspace() override;
void _begin(uint32_t b, uint16_t rxS, uint16_t txS) override;
Expand All @@ -222,10 +237,22 @@ 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;
char thread_name[10];
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 All @@ -236,10 +263,12 @@ class AP_Networking
enum {
TEST_UDP_CLIENT = (1U<<0),
TEST_TCP_CLIENT = (1U<<1),
TEST_TCP_DISCARD = (1U<<2),
};
void start_tests(void);
void test_UDP_client(void);
void test_TCP_client(void);
void test_TCP_discard(void);
#endif // AP_NETWORKING_TESTS_ENABLED

// ports for registration with serial manager
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 @@ -6,6 +6,7 @@

#if AP_NETWORKING_ENABLED

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

const AP_Param::GroupInfo AP_Networking_IPV4::var_info[] = {
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 55b81b4

Please sign in to comment.