no chance
Fork of WIZnet_Library by
WIZnetInterface/Socket/UDPSocket.cpp@4:3c94b1ba5411, 2014-10-16 (annotated)
- Committer:
- slawcio
- Date:
- Thu Oct 16 09:35:53 2014 +0000
- Revision:
- 4:3c94b1ba5411
- Parent:
- 0:b72d22e10709
W5100
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 | } |
jbkim | 0:b72d22e10709 | 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); |
jbkim | 0:b72d22e10709 | 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 | } |
jbkim | 0:b72d22e10709 | 86 | return eth->recv(_sock_fd, buffer, udp_size); |
jbkim | 0:b72d22e10709 | 87 | } |
jbkim | 0:b72d22e10709 | 88 | |
jbkim | 0:b72d22e10709 | 89 | void UDPSocket::confEndpoint(Endpoint & ep) |
jbkim | 0:b72d22e10709 | 90 | { |
jbkim | 0:b72d22e10709 | 91 | char * host = ep.get_address(); |
jbkim | 0:b72d22e10709 | 92 | // set remote host |
jbkim | 0:b72d22e10709 | 93 | eth->sreg_ip(_sock_fd, Sn_DIPR, host); |
jbkim | 0:b72d22e10709 | 94 | // set remote port |
jbkim | 0:b72d22e10709 | 95 | eth->sreg<uint16_t>(_sock_fd, Sn_DPORT, ep.get_port()); |
jbkim | 0:b72d22e10709 | 96 | } |
jbkim | 0:b72d22e10709 | 97 | |
jbkim | 0:b72d22e10709 | 98 | void UDPSocket::readEndpoint(Endpoint & ep, uint8_t info[]) |
jbkim | 0:b72d22e10709 | 99 | { |
jbkim | 0:b72d22e10709 | 100 | char addr[17]; |
jbkim | 0:b72d22e10709 | 101 | snprintf(addr, sizeof(addr), "%d.%d.%d.%d", info[0], info[1], info[2], info[3]); |
jbkim | 0:b72d22e10709 | 102 | uint16_t port = info[4]<<8|info[5]; |
jbkim | 0:b72d22e10709 | 103 | ep.set_address(addr, port); |
jbkim | 0:b72d22e10709 | 104 | } |
jbkim | 0:b72d22e10709 | 105 |