123 lines
2.9 KiB
C++
123 lines
2.9 KiB
C++
|
#include <socket.h>
|
||
|
|
||
|
#include <Arduino.h>
|
||
|
#include <Client.h>
|
||
|
|
||
|
#include <stdlib.h>
|
||
|
#include <stdint.h>
|
||
|
|
||
|
|
||
|
|
||
|
/*
|
||
|
class Client {
|
||
|
public:
|
||
|
Client();
|
||
|
int connect(const char *host, uint16_t port);
|
||
|
int connect(IPAddress ip, uint16_t port);
|
||
|
int available();
|
||
|
void stop();
|
||
|
int read();
|
||
|
size_t write(const uint8_t *buf, size_t size);
|
||
|
size_t write(uint8_t b);
|
||
|
void flush();
|
||
|
uint8_t connected();
|
||
|
};
|
||
|
*/
|
||
|
|
||
|
|
||
|
Client::Client(uint8_t sockNum) : sockNum(sockNum) {
|
||
|
|
||
|
}
|
||
|
|
||
|
|
||
|
int Client::connect(const char *host, uint16_t) {
|
||
|
// DNS request required
|
||
|
|
||
|
return 0;
|
||
|
}
|
||
|
|
||
|
int Client::connect(IPAddress ip, uint16_t port) {
|
||
|
int8_t res = socket(this->sockNum, Sn_MR_TCP, port, SF_IO_NONBLOCK);
|
||
|
if (res != this->sockNum) {
|
||
|
close(this->sockNum);
|
||
|
return INVALID_RESPONSE;
|
||
|
}
|
||
|
logMsg("Client::connect: socket initialized");
|
||
|
|
||
|
res = ::connect(this->sockNum, ip.raw_address(), port);
|
||
|
if (res != SOCK_BUSY) {
|
||
|
close(this->sockNum);
|
||
|
return INVALID_RESPONSE;
|
||
|
}
|
||
|
uint32_t startTime = HAL_GetTick();
|
||
|
while (startTime + TIMEOUT_MS > HAL_GetTick()) {
|
||
|
uint8_t sockState = getSn_SR(this->sockNum);
|
||
|
if (sockState == SOCK_ESTABLISHED) {
|
||
|
logMsg("Client::connect: connection established");
|
||
|
return SUCCESS;
|
||
|
}
|
||
|
}
|
||
|
return TIMED_OUT;
|
||
|
}
|
||
|
|
||
|
int Client::available() {
|
||
|
return getSn_RX_RSR(this->sockNum);
|
||
|
}
|
||
|
|
||
|
void Client::stop() {
|
||
|
int8_t res = disconnect(this->sockNum);
|
||
|
|
||
|
if (res != SOCK_BUSY) {
|
||
|
close(this->sockNum);
|
||
|
logMsg("Client::stop: disconnect returns 0x%02x, invalid response, ignore it", res);
|
||
|
} else {
|
||
|
bool successfullyClosed = false;
|
||
|
uint32_t startTime = HAL_GetTick();
|
||
|
while (startTime + TIMEOUT_MS > HAL_GetTick()) {
|
||
|
uint8_t sockState = getSn_SR(this->sockNum);
|
||
|
if (sockState == SOCK_CLOSED) {
|
||
|
logMsg("Client::stop: connection closed");
|
||
|
successfullyClosed = true;
|
||
|
break;
|
||
|
}
|
||
|
}
|
||
|
if (successfullyClosed) {
|
||
|
logMsg("Client::stop: done");
|
||
|
close(this->sockNum);
|
||
|
} else {
|
||
|
logMsg("Client::stop: timeout when closing, ignore");
|
||
|
close(this->sockNum);
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
int Client::read() {
|
||
|
int res = -1;
|
||
|
if (this->available() >= 1) {
|
||
|
uint8_t buf;
|
||
|
int32_t res = recv(this->sockNum, &buf, 1);
|
||
|
if (res == 1) {
|
||
|
res = (int) buf;
|
||
|
}
|
||
|
}
|
||
|
return res;
|
||
|
}
|
||
|
|
||
|
size_t Client::write(const uint8_t *buf, size_t size) {
|
||
|
int32_t res = send(this->sockNum, (uint8_t*) buf, size);
|
||
|
return (res == size) ? size : 0;
|
||
|
}
|
||
|
|
||
|
size_t Client::write(uint8_t b) {
|
||
|
return this->write(&b, 1);
|
||
|
}
|
||
|
|
||
|
void Client::flush() {
|
||
|
// does nothing
|
||
|
}
|
||
|
|
||
|
uint8_t Client::connected() {
|
||
|
return (getSn_SR(this->sockNum) == SOCK_ESTABLISHED) ? 1 : 0;
|
||
|
}
|
||
|
|