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