-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTCPClient.cpp
179 lines (159 loc) · 4.62 KB
/
TCPClient.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/**
* @file TCPClient.cpp
* @author Phil Hilger ([email protected])
* @brief
* @version 0.1
* @date 2023-03-02
*
* CAN-talk. A library for microcontrollers that allows decent comms
* over a CAN bus.
*
* Copyright (C) 2023, PeerGum
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https: //www.gnu.org/licenses/>.
*
*/
#include "Wifi.h"
#include "esp_netif.h"
#include "lwip/err.h"
#include "lwip/sockets.h"
#include "lwip/err.h"
#include "lwip/sys.h"
#include "lwip/netdb.h"
#include "lwip/dns.h"
#include "esp_log.h"
#include "TCPClient.h"
static const char *TAG = "TCPClient";
TCPClient::TCPClient() {}
TCPClient::~TCPClient() {}
bool TCPClient::connect(const char *url) {
parseUrl(url, _protocol, _hostname, _port, _path);
if (resolve(_hostname, _ip)) {
return connect(_ip, atoi(_port));
}
return false;
}
bool TCPClient::connect(IPAddress addr, int port) {
// struct sockaddr_in6 dest_addr = {0};
if (!_dest_addr) {
_dest_addr = (struct sockaddr_in *)malloc(sizeof(struct sockaddr_in));
}
if (addr.type() == ESP_IPADDR_TYPE_V4) {
_dest_addr->sin_addr.s_addr = htonl(addr.toUInt());
_dest_addr->sin_family = AF_INET;
_dest_addr->sin_port = htons(port);
_addr_family = AF_INET;
_ip_protocol = IPPROTO_IP;
} /* else {
inet6_aton(addr.ipAddress.u_addr.ip6.addr, &dest_addr.sin6_addr);
dest_addr.sin6_family = AF_INET6;
dest_addr.sin6_port = htons(port);
dest_addr.sin6_scope_id =
esp_netif_get_netif_impl_index(EXAMPLE_INTERFACE); addr_family = AF_INET6;
ip_protocol = IPPROTO_IPV6;
} */
_ip = addr.toAddr();
_sock = lwip_socket(_addr_family, SOCK_STREAM, _ip_protocol);
if (_sock < 0) {
ESP_LOGE(TAG, "Unable to create socket: errno %d", _sock);
return false;
}
ESP_LOGD(TAG, "Socket created, connecting to %s:%d", _ip.toChar(), port);
int err = lwip_connect(_sock, (struct sockaddr *)_dest_addr,
sizeof(struct sockaddr_in));
if (err != 0) {
ESP_LOGE(TAG, "Socket unable to connect: errno %d", err);
return false;
}
ESP_LOGD(TAG, "Successfully connected");
return true;
}
bool TCPClient::connected(void) { return (_sock >= 0); }
bool TCPClient::available() {
if (_rxPtr < _rxLen) {
return true;
}
_rxLen = recv(_sock, _rxBuffer, sizeof(_rxBuffer), 0);
_rxPtr = 0;
if (_rxLen > 0) {
ESP_LOGD(TAG, "rxLen %d bytes from %s:", _rxLen, _ip.toChar());
ESP_LOG_BUFFER_HEXDUMP(TAG, _rxBuffer, _rxLen, ESP_LOG_DEBUG);
}
return (_rxLen > 0);
}
int TCPClient::read(void) {
if (available()) {
return _rxBuffer[_rxPtr++];
}
return -1;
}
int TCPClient::peek(void) {
if (available()) {
return _rxBuffer[_rxPtr];
}
return -1;
}
int TCPClient::read(uint8_t *rxBuffer, size_t rxLen) {
size_t len = 0;
for (; len < rxLen && available(); len++) {
rxBuffer[len] = read();
}
return len;
}
int TCPClient::write(char c) { return write(&c, 1); }
int TCPClient::write(uint8_t *txBuffer, size_t txLen) {
int err = lwip_send(_sock, txBuffer, txLen, 0);
// ESP_LOG_BUFFER_HEXDUMP(TAG, txBuffer, txLen, ESP_LOG_DEBUG);
if (err < 0) {
ESP_LOGE(TAG, "Error occurred during sending: errno %d", errno);
}
return err;
}
int TCPClient::write(char *buffer, size_t len) {
return write((uint8_t *)buffer, len);
}
int TCPClient::sendAndReceive(uint8_t *txBuffer, size_t txLen,
uint8_t *rxBuffer, size_t &rxLen,
uint32_t timeout) {
write(txBuffer, txLen);
return read(rxBuffer, rxLen);
}
void TCPClient::close(void) {
if (_sock != -1) {
lwip_shutdown(_sock, 0);
lwip_close(_sock);
ESP_LOGD(TAG, "Socket shutdown");
_sock = -1;
}
}
void TCPClient::printf(const char *f, ...) {
va_list args;
va_start(args, f);
char buffer[1024];
va_end(args);
sprintf(buffer, f, args);
ESP_LOG_BUFFER_HEXDUMP(TAG, buffer, strlen(buffer), ESP_LOG_DEBUG);
write(buffer, strlen(buffer));
}
std::string TCPClient::readUntil(char until) {
std::string str;
int c;
while ((c = read())>=0) {
str += (char)c;
if (c == until) {
break;
}
}
return str;
}