AP_HAL_Linux: added TCPClientDevice

This commit is contained in:
Staroselskii Georgii 2015-06-19 16:58:40 +03:00 committed by Randy Mackay
parent 595903f17c
commit 244cdb3507
2 changed files with 89 additions and 0 deletions

View File

@ -0,0 +1,65 @@
#include <AP_HAL.h>
#if CONFIG_HAL_BOARD == HAL_BOARD_LINUX
#include <stdio.h>
#include <unistd.h>
#include <errno.h>
#include <stdlib.h>
#include "TCPClientDevice.h"
TCPClientDevice::TCPClientDevice(const char *ip, uint16_t port):
_ip(ip),
_port(port)
{
}
TCPClientDevice::~TCPClientDevice()
{
}
ssize_t TCPClientDevice::write(const uint8_t *buf, uint16_t n)
{
return listener.send(buf, n);
}
ssize_t TCPClientDevice::read(uint8_t *buf, uint16_t n)
{
return listener.recv(buf, n, 1);
}
bool TCPClientDevice::open()
{
listener.reuseaddress();
if (!listener.connect(_ip, _port)) {
::printf("connect failed on %s port %u - %s\n",
_ip,
_port,
strerror(errno));
::exit(1);
}
listener.set_blocking(false);
return true;
}
bool TCPClientDevice::close()
{
return true;
}
void TCPClientDevice::set_blocking(bool blocking)
{
listener.set_blocking(blocking);
}
void TCPClientDevice::set_speed(uint32_t speed)
{
}
#endif

View File

@ -0,0 +1,24 @@
#ifndef __AP_HAL_LINUX_TCPCLIENTDEVICE_H__
#define __AP_HAL_LINUX_TCPCLIENTDEVICE_H__
#include "SerialDevice.h"
#include "../AP_HAL/utility/Socket.h"
class TCPClientDevice: public SerialDevice {
public:
TCPClientDevice(const char *ip, uint16_t port);
virtual ~TCPClientDevice();
virtual bool open() override;
virtual bool close() override;
virtual void set_blocking(bool blocking) override;
virtual void set_speed(uint32_t speed) override;
virtual ssize_t write(const uint8_t *buf, uint16_t n) override;
virtual ssize_t read(uint8_t *buf, uint16_t n) override;
private:
SocketAPM listener{false};
const char *_ip;
uint16_t _port;
};
#endif