Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exposing plain BSD sockets available since firmware 1.5 to client code #278

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/WiFi.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ extern "C" {
#include "WiFiSSLClient.h"
#include "WiFiServer.h"
#include "WiFiStorage.h"
#include "WiFiSocket.h"

typedef void(*FeedHostProcessorWatchdogFuncPointer)();

Expand Down
88 changes: 88 additions & 0 deletions src/WiFiSocket.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/*
This file is part of the WiFiNINA library.
Copyright (c) 2018 Arduino SA. All rights reserved.

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/

/*
Created on: Mar 6, 2024
Author: gershnik
*/

#include "WiFiSocket.h"


void WiFiSocket::close() {
if (m_handle != s_invalidHandle) {
SocketDrv::close(m_handle);
m_handle = s_invalidHandle;
}
}


int32_t WiFiSocket::send(const void * buf, uint16_t size) {
auto ret = SocketDrv::send(m_handle, buf, size);
if (ret == 0 && lastError() != 0)
ret = -1;
return ret;
}

int32_t WiFiSocket::recv(void * buf, uint16_t size) {
auto ret = SocketDrv::recv(m_handle, buf, size);
if (ret == 0 && lastError() != 0)
ret = -1;
return ret;
}

int32_t WiFiSocket::sendTo(const void * buf, uint16_t size,
const arduino::IPAddress & ipAddress, uint16_t port) {
auto ret = SocketDrv::sendTo(m_handle, buf, size, ipAddress, port);
if (ret == 0 && lastError() != 0)
ret = -1;
return ret;
}

int32_t WiFiSocket::recvFrom(void * buf, uint16_t size,
arduino::IPAddress & remoteIpAddress, uint16_t & remotePort) {
auto ret = SocketDrv::recvFrom(m_handle, buf, size, remoteIpAddress, remotePort);
if (ret == 0 && lastError() != 0)
ret = -1;
return ret;
}

bool WiFiSocket::setNonBlocking(bool val) {
uint32_t value = val;
auto size = SocketDrv::ioctl(m_handle, uint32_t(IOControl::NonBlockingIO),
&value, sizeof(value));
return size != 0;
}

int32_t WiFiSocket::availableToRead() const {
int32_t value = 0;
auto size = SocketDrv::ioctl(m_handle, uint32_t(IOControl::NRead),
&value, sizeof(value));
if (size == 0)
return -1;
return value;
}

bool WiFiSocket::poll(State & state) const {
auto res = SocketDrv::poll(m_handle);
if (res == SocketDrv::SocketPollFailed)
return false;
state = State(res);
return true;
}
Loading
Loading