2016-05-17 23:26:57 -03:00
|
|
|
#include "TCPServerDevice.h"
|
2015-06-19 10:58:40 -03:00
|
|
|
|
|
|
|
#include <errno.h>
|
2016-05-17 23:26:57 -03:00
|
|
|
#include <stdio.h>
|
2015-06-19 10:58:40 -03:00
|
|
|
#include <stdlib.h>
|
2016-05-17 23:26:57 -03:00
|
|
|
#include <unistd.h>
|
2015-06-19 10:58:40 -03:00
|
|
|
|
2016-05-17 23:26:57 -03:00
|
|
|
#include <AP_HAL/AP_HAL.h>
|
2015-06-19 10:58:40 -03:00
|
|
|
|
2015-07-28 20:55:47 -03:00
|
|
|
extern const AP_HAL::HAL& hal;
|
|
|
|
|
2015-07-28 20:37:57 -03:00
|
|
|
TCPServerDevice::TCPServerDevice(const char *ip, uint16_t port, bool wait):
|
2015-06-19 10:58:40 -03:00
|
|
|
_ip(ip),
|
2015-07-28 20:37:57 -03:00
|
|
|
_port(port),
|
|
|
|
_wait(wait)
|
2015-06-19 10:58:40 -03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2015-07-08 14:27:21 -03:00
|
|
|
TCPServerDevice::~TCPServerDevice()
|
2015-06-19 10:58:40 -03:00
|
|
|
{
|
2016-10-30 02:24:21 -03:00
|
|
|
if (sock != nullptr) {
|
2015-07-28 20:37:57 -03:00
|
|
|
delete sock;
|
2016-10-30 02:24:21 -03:00
|
|
|
sock = nullptr;
|
2015-07-28 20:37:57 -03:00
|
|
|
}
|
2015-06-19 10:58:40 -03:00
|
|
|
}
|
|
|
|
|
2015-07-08 14:27:21 -03:00
|
|
|
ssize_t TCPServerDevice::write(const uint8_t *buf, uint16_t n)
|
2015-06-19 10:58:40 -03:00
|
|
|
{
|
2016-10-30 02:24:21 -03:00
|
|
|
if (sock == nullptr) {
|
2015-07-28 20:37:57 -03:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return sock->send(buf, n);
|
2015-06-19 10:58:40 -03:00
|
|
|
}
|
|
|
|
|
2015-07-28 20:37:57 -03:00
|
|
|
/*
|
|
|
|
when we try to read we accept new connections if one isn't already
|
|
|
|
established
|
|
|
|
*/
|
2015-07-08 14:27:21 -03:00
|
|
|
ssize_t TCPServerDevice::read(uint8_t *buf, uint16_t n)
|
2015-06-19 10:58:40 -03:00
|
|
|
{
|
2016-10-30 02:24:21 -03:00
|
|
|
if (sock == nullptr) {
|
2015-07-28 20:37:57 -03:00
|
|
|
sock = listener.accept(0);
|
2016-10-30 02:24:21 -03:00
|
|
|
if (sock != nullptr) {
|
2015-07-28 20:37:57 -03:00
|
|
|
sock->set_blocking(_blocking);
|
|
|
|
}
|
|
|
|
}
|
2016-10-30 02:24:21 -03:00
|
|
|
if (sock == nullptr) {
|
2015-07-28 20:37:57 -03:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
ssize_t ret = sock->recv(buf, n, 1);
|
|
|
|
if (ret == 0) {
|
|
|
|
// EOF, go back to waiting for a new connection
|
|
|
|
delete sock;
|
2016-10-30 02:24:21 -03:00
|
|
|
sock = nullptr;
|
2015-07-28 20:37:57 -03:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return ret;
|
2015-06-19 10:58:40 -03:00
|
|
|
}
|
|
|
|
|
2015-07-08 14:27:21 -03:00
|
|
|
bool TCPServerDevice::open()
|
2015-06-19 10:58:40 -03:00
|
|
|
{
|
|
|
|
listener.reuseaddress();
|
|
|
|
|
2015-07-28 20:37:57 -03:00
|
|
|
if (!listener.bind(_ip, _port)) {
|
2015-11-19 23:10:58 -04:00
|
|
|
if (AP_HAL::millis() - _last_bind_warning > 5000) {
|
2015-07-28 20:55:47 -03:00
|
|
|
::printf("bind failed on %s port %u - %s\n",
|
|
|
|
_ip,
|
|
|
|
_port,
|
|
|
|
strerror(errno));
|
2015-11-19 23:10:58 -04:00
|
|
|
_last_bind_warning = AP_HAL::millis();
|
2015-07-28 20:55:47 -03:00
|
|
|
}
|
|
|
|
return false;
|
2015-07-28 20:37:57 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!listener.listen(1)) {
|
2015-11-19 23:10:58 -04:00
|
|
|
if (AP_HAL::millis() - _last_bind_warning > 5000) {
|
2015-07-28 20:55:47 -03:00
|
|
|
::printf("listen failed on %s port %u - %s\n",
|
|
|
|
_ip,
|
|
|
|
_port,
|
|
|
|
strerror(errno));
|
2015-11-19 23:10:58 -04:00
|
|
|
_last_bind_warning = AP_HAL::millis();
|
2015-07-28 20:55:47 -03:00
|
|
|
}
|
|
|
|
return false;
|
2015-06-19 10:58:40 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
listener.set_blocking(false);
|
|
|
|
|
2015-07-28 20:37:57 -03:00
|
|
|
if (_wait) {
|
|
|
|
::printf("Waiting for connection on %s:%u ....\n",
|
|
|
|
_ip, (unsigned)_port);
|
|
|
|
::fflush(stdout);
|
2016-10-30 02:24:21 -03:00
|
|
|
while (sock == nullptr) {
|
2015-07-28 20:37:57 -03:00
|
|
|
sock = listener.accept(1000);
|
|
|
|
}
|
|
|
|
sock->set_blocking(_blocking);
|
|
|
|
::printf("connected\n");
|
|
|
|
::fflush(stdout);
|
|
|
|
}
|
|
|
|
|
2015-06-19 10:58:40 -03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-07-08 14:27:21 -03:00
|
|
|
bool TCPServerDevice::close()
|
2015-06-19 10:58:40 -03:00
|
|
|
{
|
2016-10-30 02:24:21 -03:00
|
|
|
if (sock != nullptr) {
|
2015-07-28 20:37:57 -03:00
|
|
|
delete sock;
|
2016-10-30 02:24:21 -03:00
|
|
|
sock = nullptr;
|
2015-07-28 20:37:57 -03:00
|
|
|
}
|
2015-06-19 10:58:40 -03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-07-08 14:27:21 -03:00
|
|
|
void TCPServerDevice::set_blocking(bool blocking)
|
2015-06-19 10:58:40 -03:00
|
|
|
{
|
2015-07-28 20:37:57 -03:00
|
|
|
_blocking = blocking;
|
|
|
|
listener.set_blocking(_blocking);
|
2015-06-19 10:58:40 -03:00
|
|
|
}
|
|
|
|
|
2015-07-08 14:27:21 -03:00
|
|
|
void TCPServerDevice::set_speed(uint32_t speed)
|
2015-06-19 10:58:40 -03:00
|
|
|
{
|
|
|
|
}
|