Skip to content

Commit

Permalink
Test udp client for linux
Browse files Browse the repository at this point in the history
  • Loading branch information
gabryelreyes committed Aug 20, 2024
1 parent f98bd84 commit 148fc61
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 8 deletions.
54 changes: 47 additions & 7 deletions lib/ArduinoNative/src/WiFiUdp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,15 @@

#include "WiFiUdp.h"
#include <Util.h>
#include <Logging.h>

#include <bits/stdc++.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>

/******************************************************************************
* Compiler Switches
Expand Down Expand Up @@ -62,30 +71,61 @@

uint8_t WiFiUDP::begin(uint16_t port)
{
UTIL_NOT_USED(port);
m_socket = socket(AF_INET, SOCK_DGRAM, 0);

if (m_socket == -1)
{
LOG_ERROR("socket creation failed");
}
else
{
m_servaddr.sin_family = AF_INET;
return 1;
}

return 0;
}

void WiFiUDP::stop()
{
close(m_socket);
}

int WiFiUDP::beginPacket(IPAddress ip, uint16_t port) // NOLINT(performance-unnecessary-value-param)
{
UTIL_NOT_USED(ip);
UTIL_NOT_USED(port);
return 0;
LOG_DEBUG("%s %d", ip.toString().c_str(), port);
m_servaddr.sin_port = htons(port);
m_servaddr.sin_addr.s_addr = ip.raw();
m_outBufferLength = 0;

const char* hello = "Hello from client";

sendto(m_socket, (const char*)hello, strlen(hello), 0, (const struct sockaddr*)&m_servaddr, sizeof(m_servaddr));
return 1;
}

size_t WiFiUDP::write(const uint8_t* buffer, size_t length)
{
UTIL_NOT_USED(buffer);
UTIL_NOT_USED(length);
return 0U;
memcpy(m_outBuffer, buffer, length);
m_outBufferLength += length;
return m_outBufferLength;
}

int WiFiUDP::endPacket()
{

int len = sendto(m_socket, reinterpret_cast<const char*>(m_outBuffer), m_outBufferLength, 0,
(const struct sockaddr*)&m_servaddr, sizeof(m_servaddr));

if (len == -1)
{
LOG_ERROR("sendto failed");
}
else
{
LOG_INFO("Hello message sent.");
return 1;
}
return 0;
}

Expand Down
11 changes: 10 additions & 1 deletion lib/ArduinoNative/src/WiFiUdp.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,14 @@

#include <Arduino.h>
#include "IPAddress.h"
#include <netinet/in.h>

/******************************************************************************
* Macros
*****************************************************************************/

#define MAXLINE 1024

/******************************************************************************
* Types and Classes
*****************************************************************************/
Expand All @@ -63,7 +66,7 @@ class WiFiUDP : public Stream
/**
* Default constructor.
*/
WiFiUDP()
WiFiUDP() : Stream(), m_socket(-1), m_servaddr(), m_outBuffer{0}, m_outBufferLength(0)
{
}

Expand Down Expand Up @@ -146,6 +149,12 @@ class WiFiUDP : public Stream
*/
int read(uint8_t* buffer, size_t size);

private:
int m_socket; /**< Socket file descriptor. */
struct sockaddr_in m_servaddr; /**< Server address. */
uint8_t m_outBuffer[MAXLINE]; /**< Buffer for outgoing packets. */
size_t m_outBufferLength; /**< Length of the buffer. */

private:
/**
* Print argument to the Output Stream.
Expand Down

0 comments on commit 148fc61

Please sign in to comment.