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 0f26e88 commit 027e21a
Show file tree
Hide file tree
Showing 4 changed files with 324 additions and 34 deletions.
29 changes: 28 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 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 027e21a

Please sign in to comment.