2015-06-15 18:01:14 -03:00
|
|
|
#include <AP_HAL.h>
|
|
|
|
|
|
|
|
#if CONFIG_HAL_BOARD == HAL_BOARD_LINUX
|
|
|
|
|
|
|
|
#include <stdio.h>
|
2015-07-29 01:03:30 -03:00
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <fcntl.h>
|
2015-06-15 18:01:14 -03:00
|
|
|
|
|
|
|
#include "UDPDevice.h"
|
|
|
|
|
2015-07-29 03:46:53 -03:00
|
|
|
UDPDevice::UDPDevice(const char *ip, uint16_t port, bool bcast):
|
2015-06-15 18:01:14 -03:00
|
|
|
_ip(ip),
|
2015-07-29 03:46:53 -03:00
|
|
|
_port(port),
|
|
|
|
_bcast(bcast)
|
2015-06-15 18:01:14 -03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
UDPDevice::~UDPDevice()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
ssize_t UDPDevice::write(const uint8_t *buf, uint16_t n)
|
|
|
|
{
|
2015-07-29 01:03:30 -03:00
|
|
|
if (!socket.pollout(0)) {
|
|
|
|
return -1;
|
|
|
|
}
|
2015-07-29 03:46:53 -03:00
|
|
|
if (_connected) {
|
|
|
|
return socket.send(buf, n);
|
|
|
|
}
|
|
|
|
return socket.sendto(buf, n, _ip, _port);
|
2015-06-15 18:01:14 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
ssize_t UDPDevice::read(uint8_t *buf, uint16_t n)
|
|
|
|
{
|
2015-07-29 03:46:53 -03:00
|
|
|
ssize_t ret = socket.recv(buf, n, 0);
|
|
|
|
if (!_connected && ret > 0) {
|
|
|
|
const char *ip;
|
|
|
|
uint16_t port;
|
|
|
|
socket.last_recv_address(ip, port);
|
|
|
|
_connected = socket.connect(ip, port);
|
|
|
|
}
|
|
|
|
return ret;
|
2015-06-15 18:01:14 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
bool UDPDevice::open()
|
|
|
|
{
|
2015-07-29 03:46:53 -03:00
|
|
|
if (_bcast) {
|
|
|
|
// open now, then connect on first received packet
|
|
|
|
socket.set_broadcast();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
_connected = socket.connect(_ip, _port);
|
|
|
|
return _connected;
|
2015-06-15 18:01:14 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
bool UDPDevice::close()
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void UDPDevice::set_blocking(bool blocking)
|
|
|
|
{
|
|
|
|
socket.set_blocking(blocking);
|
|
|
|
}
|
|
|
|
|
|
|
|
void UDPDevice::set_speed(uint32_t speed)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|