Logo Pico-Framework A web-first embedded framework for C++
Loading...
Searching...
No Matches
Tcp.h
Go to the documentation of this file.
1
9#pragma once
10#include <FreeRTOS.h>
11#include <queue.h>
12#include <string>
13#include <vector>
14#include <cstdint>
15#include <lwip/ip_addr.h>
16#include <lwip/sockets.h>
17#if PICO_TCP_ENABLE_TLS
18#include <lwip/altcp.h>
19#include <lwip/altcp_tls.h>
20#endif
21
22
23enum class SocketEvent : uint8_t {
24 Connect = 0,
25 Recv = 1,
26 Sent = 2,
27 Error = 3
28};
29
35
39class Tcp {
40public:
41 Tcp();
42 explicit Tcp(int sockfd);
43 ~Tcp();
44
45 // Disable copy
46 Tcp(const Tcp&) = delete;
47 Tcp& operator=(const Tcp&) = delete;
48
49 // Enable move
50 Tcp(Tcp&& other) noexcept;
51 Tcp& operator=(Tcp&& other) noexcept;
52
53 // Accessors
54 std::string getPeerIp() const;
55
59 void setRootCACertificate(const std::string& pem);
60
64 void setServerTlsConfig(const std::string& certPem, const std::string& keyPem);
65
72 bool connect(const char* host, int port, bool useTls = false);
73 #if PICO_TCP_ENABLE_TLS
74 bool connectTls(const char* host, int port);
75 static err_t onConnected(void* arg, struct altcp_pcb* conn, err_t err);
76 static void onError(void* arg, err_t err);
77 #endif
78
82 int send(const char* buffer, size_t size);
83
87 int recv(char *buffer, size_t size, uint32_t timeout_ms);
88
92 int close();
93
97 bool bindAndListen(int port);
98 bool bindAndListenPlain(int port);
99 bool bindAndListenTls(int port);
100
105 Tcp* accept();
106
110 #if PICO_TCP_ENABLE_TLS
111 bool isValid() const { return sockfd >= 0 || tls_pcb != nullptr; }
112 #else
113 bool isValid() const { return sockfd >= 0; }
114 #endif
115
119 bool isConnected() const { return connected; }
120
124 int getSocketFd() const { return sockfd; }
125
126 void setHostname(const char* name) {
127 if (name) {
128 strncpy(hostname, name, sizeof(hostname) - 1);
129 hostname[sizeof(hostname) - 1] = '\0';
130 }
131 }
132 const char* getHostname() const { return hostname; }
133
134private:
135 // Internal helpers
136 bool connectPlain(const ip_addr_t& ip, int port);
137 bool connectTls(const ip_addr_t& ip, int port);
138
139 static err_t tlsRecvCallback(void* arg, struct altcp_pcb* conn, struct pbuf* p, err_t err);
140 static err_t acceptCallback(void* arg, struct altcp_pcb* new_conn, err_t err);
141
142 int sockfd = -1;
143 bool connected = false;
144 bool use_tls = false;
145 bool is_server_socket = false;
146 int connectResult = ERR_OK;
147
148 std::string root_ca_cert;
149
150 #if PICO_TCP_ENABLE_TLS
151 // TLS state (client)
152 struct altcp_tls_config* tls_config = nullptr;
153 struct altcp_pcb* tls_pcb = nullptr;
154 #endif
155 struct pbuf* recv_buffer = nullptr;
156 size_t recv_offset = 0;
157
158 // TLS server config
159 std::string server_tls_cert;
160 std::string server_tls_key;
161 struct altcp_tls_config* server_tls_config = nullptr;
162
163 // buffering receive pbufs using callabck and queue
164 static constexpr size_t MAX_TLS_SEGMENT_SIZE = 1460; // Typical MSS size
165
166 TaskHandle_t connectingTask = nullptr;
167 TaskHandle_t waiting_task = nullptr;
168 altcp_pcb* pending_client = nullptr;
169
170 char hostname[64] = {0};
171
172};
SocketEvent
Definition Tcp.h:23
NotifyIndex
Definition Tcp.h:30
@ NotifyAccept
Definition Tcp.h:32
@ NotifyConnect
Definition Tcp.h:33
@ NotifyRecv
Definition Tcp.h:31
General-purpose TCP socket wrapper with optional TLS support via mbedTLS (altcp).
Definition Tcp.h:39
int connectResult
Definition Tcp.h:146
Tcp()
Definition Tcp.cpp:24
bool connected
Definition Tcp.h:143
bool isValid() const
Check if the socket is valid.
Definition Tcp.h:113
Tcp(const Tcp &)=delete
Tcp * accept()
Accept a new incoming connection (for server use).
Definition Tcp.cpp:491
int sockfd
Definition Tcp.h:142
bool connect(const char *host, int port, bool useTls=false)
Connect to a remote host.
Definition Tcp.cpp:116
bool is_server_socket
Definition Tcp.h:145
static err_t tlsRecvCallback(void *arg, struct altcp_pcb *conn, struct pbuf *p, err_t err)
Definition Tcp.cpp:351
~Tcp()
Definition Tcp.cpp:44
bool bindAndListenTls(int port)
char hostname[64]
Hostname for TLS connections.
Definition Tcp.h:170
TaskHandle_t waiting_task
Task handle for async operations.
Definition Tcp.h:167
bool use_tls
Definition Tcp.h:144
bool isConnected() const
Check if the socket is connected.
Definition Tcp.h:119
void setRootCACertificate(const std::string &pem)
Set the Root CA certificate to be used for client TLS connections (PEM format).
std::string root_ca_cert
Definition Tcp.h:148
std::string getPeerIp() const
Definition Tcp.cpp:79
static err_t acceptCallback(void *arg, struct altcp_pcb *new_conn, err_t err)
Definition Tcp.cpp:471
struct altcp_tls_config * server_tls_config
Definition Tcp.h:161
bool connectTls(const ip_addr_t &ip, int port)
struct pbuf * recv_buffer
Buffer for TLS receive.
Definition Tcp.h:155
void setHostname(const char *name)
Definition Tcp.h:126
bool connectPlain(const ip_addr_t &ip, int port)
Definition Tcp.cpp:133
size_t recv_offset
Definition Tcp.h:156
int send(const char *buffer, size_t size)
Send data over the connection.
Definition Tcp.cpp:279
Tcp & operator=(const Tcp &)=delete
void setServerTlsConfig(const std::string &certPem, const std::string &keyPem)
Set the certificate and key to use for server-side TLS (PEM format).
bool bindAndListenPlain(int port)
Definition Tcp.cpp:552
TaskHandle_t connectingTask
Task handle for async operations.
Definition Tcp.h:166
int close()
Close the connection and free resources.
Definition Tcp.cpp:443
bool bindAndListen(int port)
Bind and listen on a port for incoming connections (for server use).
Definition Tcp.cpp:542
int recv(char *buffer, size_t size, uint32_t timeout_ms)
Receive data from the connection.
Definition Tcp.cpp:389
std::string server_tls_cert
Definition Tcp.h:159
int getSocketFd() const
Get the raw socket file descriptor (may be -1 for TLS-only connection).
Definition Tcp.h:124
altcp_pcb * pending_client
For TLS: set by acceptCallback.
Definition Tcp.h:168
std::string server_tls_key
Definition Tcp.h:160
const char * getHostname() const
Definition Tcp.h:132
static constexpr size_t MAX_TLS_SEGMENT_SIZE
Definition Tcp.h:164