Ethernet for Nucleo and Disco board STM32F746 works with gcc and arm. IAC is untested

Fork of F7_Ethernet by Dieter Graef

Committer:
TudaPellini
Date:
Sun Aug 20 22:09:22 2017 +0000
Revision:
4:c63cab7b2bda
Parent:
0:d26c1b55cfca
Minor adjustments;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
DieterGraef 0:d26c1b55cfca 1 /* Copyright (C) 2012 mbed.org, MIT License
DieterGraef 0:d26c1b55cfca 2 *
DieterGraef 0:d26c1b55cfca 3 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
DieterGraef 0:d26c1b55cfca 4 * and associated documentation files (the "Software"), to deal in the Software without restriction,
DieterGraef 0:d26c1b55cfca 5 * including without limitation the rights to use, copy, modify, merge, publish, distribute,
DieterGraef 0:d26c1b55cfca 6 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
DieterGraef 0:d26c1b55cfca 7 * furnished to do so, subject to the following conditions:
DieterGraef 0:d26c1b55cfca 8 *
DieterGraef 0:d26c1b55cfca 9 * The above copyright notice and this permission notice shall be included in all copies or
DieterGraef 0:d26c1b55cfca 10 * substantial portions of the Software.
DieterGraef 0:d26c1b55cfca 11 *
DieterGraef 0:d26c1b55cfca 12 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
DieterGraef 0:d26c1b55cfca 13 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
DieterGraef 0:d26c1b55cfca 14 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
DieterGraef 0:d26c1b55cfca 15 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
DieterGraef 0:d26c1b55cfca 16 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
DieterGraef 0:d26c1b55cfca 17 */
DieterGraef 0:d26c1b55cfca 18
DieterGraef 0:d26c1b55cfca 19 #include "Socket/UDPSocket.h"
DieterGraef 0:d26c1b55cfca 20
DieterGraef 0:d26c1b55cfca 21 #include <cstring>
DieterGraef 0:d26c1b55cfca 22
DieterGraef 0:d26c1b55cfca 23 using std::memset;
DieterGraef 0:d26c1b55cfca 24
DieterGraef 0:d26c1b55cfca 25 UDPSocket::UDPSocket() {
DieterGraef 0:d26c1b55cfca 26 }
DieterGraef 0:d26c1b55cfca 27
DieterGraef 0:d26c1b55cfca 28 int UDPSocket::init(void) {
DieterGraef 0:d26c1b55cfca 29 return init_socket(SOCK_DGRAM);
DieterGraef 0:d26c1b55cfca 30 }
DieterGraef 0:d26c1b55cfca 31
DieterGraef 0:d26c1b55cfca 32 // Server initialization
DieterGraef 0:d26c1b55cfca 33 int UDPSocket::bind(int port) {
DieterGraef 0:d26c1b55cfca 34 if (init_socket(SOCK_DGRAM) < 0)
DieterGraef 0:d26c1b55cfca 35 return -1;
DieterGraef 0:d26c1b55cfca 36
DieterGraef 0:d26c1b55cfca 37 struct sockaddr_in localHost;
DieterGraef 0:d26c1b55cfca 38 std::memset(&localHost, 0, sizeof(localHost));
DieterGraef 0:d26c1b55cfca 39
DieterGraef 0:d26c1b55cfca 40 localHost.sin_family = AF_INET;
DieterGraef 0:d26c1b55cfca 41 localHost.sin_port = htons(port);
DieterGraef 0:d26c1b55cfca 42 localHost.sin_addr.s_addr = INADDR_ANY;
DieterGraef 0:d26c1b55cfca 43
DieterGraef 0:d26c1b55cfca 44 if (lwip_bind(_sock_fd, (const struct sockaddr *) &localHost, sizeof(localHost)) < 0) {
DieterGraef 0:d26c1b55cfca 45 close();
DieterGraef 0:d26c1b55cfca 46 return -1;
DieterGraef 0:d26c1b55cfca 47 }
DieterGraef 0:d26c1b55cfca 48
DieterGraef 0:d26c1b55cfca 49 return 0;
DieterGraef 0:d26c1b55cfca 50 }
DieterGraef 0:d26c1b55cfca 51
DieterGraef 0:d26c1b55cfca 52 int UDPSocket::join_multicast_group(const char* address) {
DieterGraef 0:d26c1b55cfca 53 struct ip_mreq mreq;
DieterGraef 0:d26c1b55cfca 54
DieterGraef 0:d26c1b55cfca 55 // Set up group address
DieterGraef 0:d26c1b55cfca 56 mreq.imr_multiaddr.s_addr = inet_addr(address);
DieterGraef 0:d26c1b55cfca 57 mreq.imr_interface.s_addr = htonl(INADDR_ANY);
DieterGraef 0:d26c1b55cfca 58
DieterGraef 0:d26c1b55cfca 59 return set_option(IPPROTO_IP, IP_ADD_MEMBERSHIP, &mreq, sizeof(mreq));
DieterGraef 0:d26c1b55cfca 60 }
DieterGraef 0:d26c1b55cfca 61
DieterGraef 0:d26c1b55cfca 62 int UDPSocket::set_broadcasting(bool broadcast) {
DieterGraef 0:d26c1b55cfca 63 int option = (broadcast) ? (1) : (0);
DieterGraef 0:d26c1b55cfca 64 return set_option(SOL_SOCKET, SO_BROADCAST, &option, sizeof(option));
DieterGraef 0:d26c1b55cfca 65 }
DieterGraef 0:d26c1b55cfca 66
DieterGraef 0:d26c1b55cfca 67 // -1 if unsuccessful, else number of bytes written
DieterGraef 0:d26c1b55cfca 68 int UDPSocket::sendTo(Endpoint &remote, char *packet, int length) {
DieterGraef 0:d26c1b55cfca 69 if (_sock_fd < 0)
DieterGraef 0:d26c1b55cfca 70 return -1;
DieterGraef 0:d26c1b55cfca 71
DieterGraef 0:d26c1b55cfca 72 if (!_blocking) {
DieterGraef 0:d26c1b55cfca 73 TimeInterval timeout(_timeout);
DieterGraef 0:d26c1b55cfca 74 if (wait_writable(timeout) != 0)
DieterGraef 0:d26c1b55cfca 75 return 0;
DieterGraef 0:d26c1b55cfca 76 }
DieterGraef 0:d26c1b55cfca 77
DieterGraef 0:d26c1b55cfca 78 return lwip_sendto(_sock_fd, packet, length, 0, (const struct sockaddr *) &remote._remoteHost, sizeof(remote._remoteHost));
DieterGraef 0:d26c1b55cfca 79 }
DieterGraef 0:d26c1b55cfca 80
DieterGraef 0:d26c1b55cfca 81 // -1 if unsuccessful, else number of bytes received
DieterGraef 0:d26c1b55cfca 82 int UDPSocket::receiveFrom(Endpoint &remote, char *buffer, int length) {
DieterGraef 0:d26c1b55cfca 83 if (_sock_fd < 0)
DieterGraef 0:d26c1b55cfca 84 return -1;
DieterGraef 0:d26c1b55cfca 85
DieterGraef 0:d26c1b55cfca 86 if (!_blocking) {
DieterGraef 0:d26c1b55cfca 87 TimeInterval timeout(_timeout);
DieterGraef 0:d26c1b55cfca 88 if (wait_readable(timeout) != 0)
DieterGraef 0:d26c1b55cfca 89 return 0;
DieterGraef 0:d26c1b55cfca 90 }
DieterGraef 0:d26c1b55cfca 91 remote.reset_address();
DieterGraef 0:d26c1b55cfca 92 socklen_t remoteHostLen = sizeof(remote._remoteHost);
DieterGraef 0:d26c1b55cfca 93 return lwip_recvfrom(_sock_fd, buffer, length, 0, (struct sockaddr*) &remote._remoteHost, &remoteHostLen);
DieterGraef 0:d26c1b55cfca 94 }