|
| 1 | +/* |
| 2 | + This file is part of the WiFiNINA library. |
| 3 | + Copyright (c) 2018 Arduino SA. All rights reserved. |
| 4 | +
|
| 5 | + This library is free software; you can redistribute it and/or |
| 6 | + modify it under the terms of the GNU Lesser General Public |
| 7 | + License as published by the Free Software Foundation; either |
| 8 | + version 2.1 of the License, or (at your option) any later version. |
| 9 | +
|
| 10 | + This library is distributed in the hope that it will be useful, |
| 11 | + but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | + Lesser General Public License for more details. |
| 14 | +
|
| 15 | + You should have received a copy of the GNU Lesser General Public |
| 16 | + License along with this library; if not, write to the Free Software |
| 17 | + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 18 | +*/ |
| 19 | + |
| 20 | +/* |
| 21 | + Created on: Mar 6, 2024 |
| 22 | + Author: gershnik |
| 23 | +*/ |
| 24 | + |
| 25 | +#include "WiFiSocket.h" |
| 26 | + |
| 27 | + |
| 28 | +void WiFiSocket::close() { |
| 29 | + if (m_handle != s_invalidHandle) { |
| 30 | + SocketDrv::close(m_handle); |
| 31 | + m_handle = s_invalidHandle; |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | + |
| 36 | +int32_t WiFiSocket::send(const void * buf, uint16_t size) { |
| 37 | + auto ret = SocketDrv::send(m_handle, buf, size); |
| 38 | + if (ret == 0 && lastError() != 0) |
| 39 | + ret = -1; |
| 40 | + return ret; |
| 41 | +} |
| 42 | + |
| 43 | +int32_t WiFiSocket::recv(void * buf, uint16_t size) { |
| 44 | + auto ret = SocketDrv::recv(m_handle, buf, size); |
| 45 | + if (ret == 0 && lastError() != 0) |
| 46 | + ret = -1; |
| 47 | + return ret; |
| 48 | +} |
| 49 | + |
| 50 | +int32_t WiFiSocket::sendTo(const void * buf, uint16_t size, |
| 51 | + const arduino::IPAddress & ipAddress, uint16_t port) { |
| 52 | + auto ret = SocketDrv::sendTo(m_handle, buf, size, ipAddress, port); |
| 53 | + if (ret == 0 && lastError() != 0) |
| 54 | + ret = -1; |
| 55 | + return ret; |
| 56 | +} |
| 57 | + |
| 58 | +int32_t WiFiSocket::recvFrom(void * buf, uint16_t size, |
| 59 | + arduino::IPAddress & remoteIpAddress, uint16_t & remotePort) { |
| 60 | + auto ret = SocketDrv::recvFrom(m_handle, buf, size, remoteIpAddress, remotePort); |
| 61 | + if (ret == 0 && lastError() != 0) |
| 62 | + ret = -1; |
| 63 | + return ret; |
| 64 | +} |
| 65 | + |
| 66 | +bool WiFiSocket::setNonBlocking(bool val) { |
| 67 | + uint32_t value = val; |
| 68 | + auto size = SocketDrv::ioctl(m_handle, uint32_t(IOControl::NonBlockingIO), |
| 69 | + &value, sizeof(value)); |
| 70 | + return size != 0; |
| 71 | +} |
| 72 | + |
| 73 | +int32_t WiFiSocket::availableToRead() const { |
| 74 | + int32_t value = 0; |
| 75 | + auto size = SocketDrv::ioctl(m_handle, uint32_t(IOControl::NRead), |
| 76 | + &value, sizeof(value)); |
| 77 | + if (size == 0) |
| 78 | + return -1; |
| 79 | + return value; |
| 80 | +} |
| 81 | + |
| 82 | +bool WiFiSocket::poll(State & state) const { |
| 83 | + auto res = SocketDrv::poll(m_handle); |
| 84 | + if (res == SocketDrv::SocketPollFailed) |
| 85 | + return false; |
| 86 | + state = State(res); |
| 87 | + return true; |
| 88 | +} |
0 commit comments