This is WIZnet Ethernet Interface using Hardware TCP/IP chip, W5500, W5200 and W5100. One of them can be selected by enabling it in wiznet.h.
Fork of WIZnet_Library by
WIZnetInterface/Socket/UDPSocket.cpp@9:8b3bc4434031, 2016-03-15 (annotated)
- Committer:
- rohangala
- Date:
- Tue Mar 15 11:36:55 2016 +0000
- Revision:
- 9:8b3bc4434031
- Parent:
- 5:89edb39d8707
final without caliberated;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
jbkim | 0:b72d22e10709 | 1 | /* Copyright (C) 2012 mbed.org, MIT License |
jbkim | 0:b72d22e10709 | 2 | * |
jbkim | 0:b72d22e10709 | 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy of this software |
jbkim | 0:b72d22e10709 | 4 | * and associated documentation files (the "Software"), to deal in the Software without restriction, |
jbkim | 0:b72d22e10709 | 5 | * including without limitation the rights to use, copy, modify, merge, publish, distribute, |
jbkim | 0:b72d22e10709 | 6 | * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is |
jbkim | 0:b72d22e10709 | 7 | * furnished to do so, subject to the following conditions: |
jbkim | 0:b72d22e10709 | 8 | * |
jbkim | 0:b72d22e10709 | 9 | * The above copyright notice and this permission notice shall be included in all copies or |
jbkim | 0:b72d22e10709 | 10 | * substantial portions of the Software. |
jbkim | 0:b72d22e10709 | 11 | * |
jbkim | 0:b72d22e10709 | 12 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING |
jbkim | 0:b72d22e10709 | 13 | * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
jbkim | 0:b72d22e10709 | 14 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
jbkim | 0:b72d22e10709 | 15 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
jbkim | 0:b72d22e10709 | 16 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
jbkim | 0:b72d22e10709 | 17 | */ |
jbkim | 0:b72d22e10709 | 18 | |
jbkim | 0:b72d22e10709 | 19 | #include "UDPSocket.h" |
jbkim | 0:b72d22e10709 | 20 | |
jbkim | 0:b72d22e10709 | 21 | static int udp_local_port; |
jbkim | 0:b72d22e10709 | 22 | |
jbkim | 0:b72d22e10709 | 23 | UDPSocket::UDPSocket() |
jbkim | 0:b72d22e10709 | 24 | { |
jbkim | 0:b72d22e10709 | 25 | } |
jbkim | 0:b72d22e10709 | 26 | |
jbkim | 0:b72d22e10709 | 27 | // After init function, bind() should be called. |
jbkim | 0:b72d22e10709 | 28 | int UDPSocket::init(void) |
jbkim | 0:b72d22e10709 | 29 | { |
jbkim | 0:b72d22e10709 | 30 | if (_sock_fd < 0) { |
jbkim | 0:b72d22e10709 | 31 | _sock_fd = eth->new_socket(); |
jbkim | 0:b72d22e10709 | 32 | } |
Bongjun | 5:89edb39d8707 | 33 | if (eth->setProtocol(_sock_fd, UDP) == false) return -1; |
jbkim | 0:b72d22e10709 | 34 | return 0; |
jbkim | 0:b72d22e10709 | 35 | } |
jbkim | 0:b72d22e10709 | 36 | |
jbkim | 0:b72d22e10709 | 37 | // Server initialization |
jbkim | 0:b72d22e10709 | 38 | int UDPSocket::bind(int port) |
jbkim | 0:b72d22e10709 | 39 | { |
jbkim | 0:b72d22e10709 | 40 | if (_sock_fd < 0) { |
jbkim | 0:b72d22e10709 | 41 | _sock_fd = eth->new_socket(); |
jbkim | 0:b72d22e10709 | 42 | if (_sock_fd < 0) { |
jbkim | 0:b72d22e10709 | 43 | return -1; |
jbkim | 0:b72d22e10709 | 44 | } |
jbkim | 0:b72d22e10709 | 45 | } |
jbkim | 0:b72d22e10709 | 46 | // set local port |
jbkim | 0:b72d22e10709 | 47 | if (port != 0) { |
jbkim | 0:b72d22e10709 | 48 | eth->sreg<uint16_t>(_sock_fd, Sn_PORT, port); |
jbkim | 0:b72d22e10709 | 49 | } else { |
jbkim | 0:b72d22e10709 | 50 | udp_local_port++; |
jbkim | 0:b72d22e10709 | 51 | eth->sreg<uint16_t>(_sock_fd, Sn_PORT, udp_local_port); |
jbkim | 0:b72d22e10709 | 52 | } |
jbkim | 0:b72d22e10709 | 53 | // set udp protocol |
jbkim | 0:b72d22e10709 | 54 | eth->setProtocol(_sock_fd, UDP); |
jbkim | 0:b72d22e10709 | 55 | eth->scmd(_sock_fd, OPEN); |
jbkim | 0:b72d22e10709 | 56 | return 0; |
jbkim | 0:b72d22e10709 | 57 | } |
jbkim | 0:b72d22e10709 | 58 | |
jbkim | 0:b72d22e10709 | 59 | // -1 if unsuccessful, else number of bytes written |
jbkim | 0:b72d22e10709 | 60 | int UDPSocket::sendTo(Endpoint &remote, char *packet, int length) |
jbkim | 0:b72d22e10709 | 61 | { |
jbkim | 0:b72d22e10709 | 62 | int size = eth->wait_writeable(_sock_fd, _blocking ? -1 : _timeout, length-1); |
jbkim | 0:b72d22e10709 | 63 | if (size < 0) { |
jbkim | 0:b72d22e10709 | 64 | return -1; |
jbkim | 0:b72d22e10709 | 65 | } |
jbkim | 0:b72d22e10709 | 66 | confEndpoint(remote); |
jbkim | 0:b72d22e10709 | 67 | int ret = eth->send(_sock_fd, packet, length); |
jbkim | 0:b72d22e10709 | 68 | return ret; |
jbkim | 0:b72d22e10709 | 69 | } |
jbkim | 0:b72d22e10709 | 70 | |
jbkim | 0:b72d22e10709 | 71 | // -1 if unsuccessful, else number of bytes received |
jbkim | 0:b72d22e10709 | 72 | int UDPSocket::receiveFrom(Endpoint &remote, char *buffer, int length) |
jbkim | 0:b72d22e10709 | 73 | { |
jbkim | 0:b72d22e10709 | 74 | uint8_t info[8]; |
jbkim | 0:b72d22e10709 | 75 | int size = eth->wait_readable(_sock_fd, _blocking ? -1 : _timeout, sizeof(info)); |
jbkim | 0:b72d22e10709 | 76 | if (size < 0) { |
jbkim | 0:b72d22e10709 | 77 | return -1; |
jbkim | 0:b72d22e10709 | 78 | } |
jbkim | 0:b72d22e10709 | 79 | eth->recv(_sock_fd, (char*)info, sizeof(info)); |
jbkim | 0:b72d22e10709 | 80 | readEndpoint(remote, info); |
Bongjun | 5:89edb39d8707 | 81 | int udp_size = info[6]<<8|info[7]; |
jbkim | 0:b72d22e10709 | 82 | //TEST_ASSERT(udp_size <= (size-sizeof(info))); |
jbkim | 0:b72d22e10709 | 83 | if (udp_size > (size-sizeof(info))) { |
jbkim | 0:b72d22e10709 | 84 | return -1; |
jbkim | 0:b72d22e10709 | 85 | } |
Bongjun | 5:89edb39d8707 | 86 | |
Bongjun | 5:89edb39d8707 | 87 | /* Perform Length check here to prevent buffer overrun */ |
Bongjun | 5:89edb39d8707 | 88 | /* fixed by Sean Newton (https://developer.mbed.org/users/SeanNewton/) */ |
Bongjun | 5:89edb39d8707 | 89 | if (udp_size > length) { |
Bongjun | 5:89edb39d8707 | 90 | //printf("udp_size: %d\n",udp_size); |
Bongjun | 5:89edb39d8707 | 91 | return -1; |
Bongjun | 5:89edb39d8707 | 92 | } |
jbkim | 0:b72d22e10709 | 93 | return eth->recv(_sock_fd, buffer, udp_size); |
jbkim | 0:b72d22e10709 | 94 | } |
jbkim | 0:b72d22e10709 | 95 | |
jbkim | 0:b72d22e10709 | 96 | void UDPSocket::confEndpoint(Endpoint & ep) |
jbkim | 0:b72d22e10709 | 97 | { |
jbkim | 0:b72d22e10709 | 98 | char * host = ep.get_address(); |
jbkim | 0:b72d22e10709 | 99 | // set remote host |
jbkim | 0:b72d22e10709 | 100 | eth->sreg_ip(_sock_fd, Sn_DIPR, host); |
jbkim | 0:b72d22e10709 | 101 | // set remote port |
jbkim | 0:b72d22e10709 | 102 | eth->sreg<uint16_t>(_sock_fd, Sn_DPORT, ep.get_port()); |
jbkim | 0:b72d22e10709 | 103 | } |
jbkim | 0:b72d22e10709 | 104 | |
jbkim | 0:b72d22e10709 | 105 | void UDPSocket::readEndpoint(Endpoint & ep, uint8_t info[]) |
jbkim | 0:b72d22e10709 | 106 | { |
jbkim | 0:b72d22e10709 | 107 | char addr[17]; |
jbkim | 0:b72d22e10709 | 108 | snprintf(addr, sizeof(addr), "%d.%d.%d.%d", info[0], info[1], info[2], info[3]); |
jbkim | 0:b72d22e10709 | 109 | uint16_t port = info[4]<<8|info[5]; |
jbkim | 0:b72d22e10709 | 110 | ep.set_address(addr, port); |
jbkim | 0:b72d22e10709 | 111 | } |
jbkim | 0:b72d22e10709 | 112 |