Custom and bugfix
Dependents: HTTPClient_HelloWorld_WIZ820io NTPClient_HelloWorld_WIZ820io TinyHTTPServer_WIZ820io
Fork of WIZ820ioInterface by
Socket/UDPSocket.cpp@5:fb15c35d1e28, 2013-08-27 (annotated)
- Committer:
- va009039
- Date:
- Tue Aug 27 12:50:11 2013 +0000
- Revision:
- 5:fb15c35d1e28
- Parent:
- 4:0bcec6272784
- Child:
- 7:c0cd6680bcb7
WIZ820ioInterface?first commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
samux | 1:fb4494783863 | 1 | /* Copyright (C) 2012 mbed.org, MIT License |
samux | 1:fb4494783863 | 2 | * |
samux | 1:fb4494783863 | 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy of this software |
samux | 1:fb4494783863 | 4 | * and associated documentation files (the "Software"), to deal in the Software without restriction, |
samux | 1:fb4494783863 | 5 | * including without limitation the rights to use, copy, modify, merge, publish, distribute, |
samux | 1:fb4494783863 | 6 | * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is |
samux | 1:fb4494783863 | 7 | * furnished to do so, subject to the following conditions: |
samux | 1:fb4494783863 | 8 | * |
samux | 1:fb4494783863 | 9 | * The above copyright notice and this permission notice shall be included in all copies or |
samux | 1:fb4494783863 | 10 | * substantial portions of the Software. |
samux | 1:fb4494783863 | 11 | * |
samux | 1:fb4494783863 | 12 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING |
samux | 1:fb4494783863 | 13 | * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
samux | 1:fb4494783863 | 14 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
samux | 1:fb4494783863 | 15 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
samux | 1:fb4494783863 | 16 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
samux | 1:fb4494783863 | 17 | */ |
samux | 1:fb4494783863 | 18 | |
samux | 1:fb4494783863 | 19 | #include "UDPSocket.h" |
samux | 1:fb4494783863 | 20 | |
samux | 1:fb4494783863 | 21 | UDPSocket::UDPSocket() |
samux | 1:fb4494783863 | 22 | { |
samux | 1:fb4494783863 | 23 | } |
samux | 1:fb4494783863 | 24 | |
samux | 1:fb4494783863 | 25 | int UDPSocket::init(void) |
samux | 1:fb4494783863 | 26 | { |
va009039 | 5:fb15c35d1e28 | 27 | if (_sock_fd < 0) { |
va009039 | 5:fb15c35d1e28 | 28 | _sock_fd = eth->new_socket(); |
va009039 | 5:fb15c35d1e28 | 29 | } |
va009039 | 5:fb15c35d1e28 | 30 | eth->setProtocol(_sock_fd, UDP); |
samux | 1:fb4494783863 | 31 | return 0; |
samux | 1:fb4494783863 | 32 | } |
samux | 1:fb4494783863 | 33 | |
samux | 1:fb4494783863 | 34 | // Server initialization |
samux | 1:fb4494783863 | 35 | int UDPSocket::bind(int port) |
samux | 1:fb4494783863 | 36 | { |
va009039 | 5:fb15c35d1e28 | 37 | if (_sock_fd < 0) { |
va009039 | 5:fb15c35d1e28 | 38 | _sock_fd = eth->new_socket(); |
va009039 | 5:fb15c35d1e28 | 39 | if (_sock_fd < 0) { |
va009039 | 5:fb15c35d1e28 | 40 | return -1; |
va009039 | 5:fb15c35d1e28 | 41 | } |
va009039 | 5:fb15c35d1e28 | 42 | } |
samux | 1:fb4494783863 | 43 | // set local port |
va009039 | 5:fb15c35d1e28 | 44 | eth->sreg<uint16_t>(_sock_fd, Sn_PORT, port); |
samux | 4:0bcec6272784 | 45 | // set udp protocol |
va009039 | 5:fb15c35d1e28 | 46 | eth->setProtocol(_sock_fd, UDP); |
va009039 | 5:fb15c35d1e28 | 47 | eth->scmd(_sock_fd, OPEN); |
samux | 1:fb4494783863 | 48 | return 0; |
samux | 1:fb4494783863 | 49 | } |
samux | 1:fb4494783863 | 50 | |
samux | 1:fb4494783863 | 51 | // -1 if unsuccessful, else number of bytes written |
samux | 1:fb4494783863 | 52 | int UDPSocket::sendTo(Endpoint &remote, char *packet, int length) |
samux | 1:fb4494783863 | 53 | { |
va009039 | 5:fb15c35d1e28 | 54 | int size = eth->wait_writeable(_sock_fd, _blocking ? -1 : _timeout, length-1); |
va009039 | 5:fb15c35d1e28 | 55 | if (size < 0) { |
va009039 | 5:fb15c35d1e28 | 56 | return -1; |
va009039 | 5:fb15c35d1e28 | 57 | } |
samux | 1:fb4494783863 | 58 | confEndpoint(remote); |
va009039 | 5:fb15c35d1e28 | 59 | int ret = eth->send(_sock_fd, packet, length); |
va009039 | 5:fb15c35d1e28 | 60 | return ret; |
samux | 1:fb4494783863 | 61 | } |
samux | 1:fb4494783863 | 62 | |
samux | 1:fb4494783863 | 63 | // -1 if unsuccessful, else number of bytes received |
samux | 1:fb4494783863 | 64 | int UDPSocket::receiveFrom(Endpoint &remote, char *buffer, int length) |
samux | 1:fb4494783863 | 65 | { |
va009039 | 5:fb15c35d1e28 | 66 | uint8_t info[8]; |
va009039 | 5:fb15c35d1e28 | 67 | int size = eth->wait_readable(_sock_fd, _blocking ? -1 : _timeout, sizeof(info)); |
va009039 | 5:fb15c35d1e28 | 68 | if (size < 0) { |
va009039 | 5:fb15c35d1e28 | 69 | return -1; |
samux | 1:fb4494783863 | 70 | } |
va009039 | 5:fb15c35d1e28 | 71 | eth->recv(_sock_fd, (char*)info, sizeof(info)); |
va009039 | 5:fb15c35d1e28 | 72 | readEndpoint(remote, info); |
va009039 | 5:fb15c35d1e28 | 73 | int udp_size = info[6]<<8|info[7]; |
va009039 | 5:fb15c35d1e28 | 74 | //TEST_ASSERT(udp_size <= (size-sizeof(info))); |
va009039 | 5:fb15c35d1e28 | 75 | if (udp_size > (size-sizeof(info))) { |
va009039 | 5:fb15c35d1e28 | 76 | return -1; |
samux | 1:fb4494783863 | 77 | } |
va009039 | 5:fb15c35d1e28 | 78 | return eth->recv(_sock_fd, buffer, udp_size); |
samux | 1:fb4494783863 | 79 | } |
samux | 1:fb4494783863 | 80 | |
va009039 | 5:fb15c35d1e28 | 81 | void UDPSocket::confEndpoint(Endpoint & ep) |
samux | 1:fb4494783863 | 82 | { |
va009039 | 5:fb15c35d1e28 | 83 | char * host = ep.get_address(); |
va009039 | 5:fb15c35d1e28 | 84 | // set remote host |
va009039 | 5:fb15c35d1e28 | 85 | eth->sreg_ip(_sock_fd, Sn_DIPR, host); |
va009039 | 5:fb15c35d1e28 | 86 | // set remote port |
va009039 | 5:fb15c35d1e28 | 87 | eth->sreg<uint16_t>(_sock_fd, Sn_DPORT, ep.get_port()); |
samux | 1:fb4494783863 | 88 | } |
samux | 1:fb4494783863 | 89 | |
va009039 | 5:fb15c35d1e28 | 90 | void UDPSocket::readEndpoint(Endpoint & ep, uint8_t info[]) |
samux | 1:fb4494783863 | 91 | { |
va009039 | 5:fb15c35d1e28 | 92 | char addr[17]; |
va009039 | 5:fb15c35d1e28 | 93 | snprintf(addr, sizeof(addr), "%d.%d.%d.%d", info[0], info[1], info[2], info[3]); |
va009039 | 5:fb15c35d1e28 | 94 | uint16_t port = info[4]<<8|info[5]; |
va009039 | 5:fb15c35d1e28 | 95 | ep.set_address(addr, port); |
samux | 1:fb4494783863 | 96 | } |