123
UdpSocket.cpp@11:647d53d146f1, 2019-08-30 (annotated)
- Committer:
- hudakz
- Date:
- Fri Aug 30 08:11:40 2019 +0000
- Revision:
- 11:647d53d146f1
- Parent:
- 9:a156d3de5647
- Child:
- 14:7648334eb41b
"set_network" method of Mbed style added.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
hudakz | 9:a156d3de5647 | 1 | /* |
hudakz | 9:a156d3de5647 | 2 | UIPUdp.cpp - Arduino implementation of a UIP wrapper class. |
hudakz | 9:a156d3de5647 | 3 | Copyright (c) 2013 Norbert Truchsess <norbert.truchsess@t-online.de> |
hudakz | 9:a156d3de5647 | 4 | All rights reserved. |
hudakz | 9:a156d3de5647 | 5 | |
hudakz | 9:a156d3de5647 | 6 | This program is free software: you can redistribute it and/or modify |
hudakz | 9:a156d3de5647 | 7 | it under the terms of the GNU General Public License as published by |
hudakz | 9:a156d3de5647 | 8 | the Free Software Foundation, either version 3 of the License, or |
hudakz | 9:a156d3de5647 | 9 | (at your option) any later version. |
hudakz | 9:a156d3de5647 | 10 | |
hudakz | 9:a156d3de5647 | 11 | This program is distributed in the hope that it will be useful, |
hudakz | 9:a156d3de5647 | 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
hudakz | 9:a156d3de5647 | 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
hudakz | 9:a156d3de5647 | 14 | GNU General Public License for more details. |
hudakz | 9:a156d3de5647 | 15 | |
hudakz | 9:a156d3de5647 | 16 | You should have received a copy of the GNU General Public License |
hudakz | 9:a156d3de5647 | 17 | along with this program. If not, see <http://www.gnu.org/licenses/>. |
hudakz | 9:a156d3de5647 | 18 | */ |
hudakz | 9:a156d3de5647 | 19 | #include "UipEthernet.h" |
hudakz | 9:a156d3de5647 | 20 | #include "UdpSocket.h" |
hudakz | 9:a156d3de5647 | 21 | #include "DnsClient.h" |
hudakz | 9:a156d3de5647 | 22 | |
hudakz | 9:a156d3de5647 | 23 | extern "C" |
hudakz | 9:a156d3de5647 | 24 | { |
hudakz | 9:a156d3de5647 | 25 | #include "utility/uip-conf.h" |
hudakz | 9:a156d3de5647 | 26 | #include "utility/uip.h" |
hudakz | 9:a156d3de5647 | 27 | #include "utility/uip_arp.h" |
hudakz | 9:a156d3de5647 | 28 | } |
hudakz | 9:a156d3de5647 | 29 | #if UIP_UDP |
hudakz | 9:a156d3de5647 | 30 | #define UIP_ARPHDRSIZE 42 |
hudakz | 9:a156d3de5647 | 31 | #define UDPBUF ((struct uip_udpip_hdr*) &uip_buf[UIP_LLH_LEN]) |
hudakz | 9:a156d3de5647 | 32 | |
hudakz | 9:a156d3de5647 | 33 | // Constructor |
hudakz | 9:a156d3de5647 | 34 | UdpSocket::UdpSocket() : |
hudakz | 9:a156d3de5647 | 35 | _uip_udp_conn(NULL) |
hudakz | 9:a156d3de5647 | 36 | { |
hudakz | 9:a156d3de5647 | 37 | memset(&appdata, 0, sizeof(appdata)); |
hudakz | 9:a156d3de5647 | 38 | } |
hudakz | 9:a156d3de5647 | 39 | |
hudakz | 9:a156d3de5647 | 40 | // initialize, start listening on specified port. Returns 1 if successful, 0 if there are no sockets available to use |
hudakz | 9:a156d3de5647 | 41 | uint8_t UdpSocket::begin(uint16_t port) |
hudakz | 9:a156d3de5647 | 42 | { |
hudakz | 9:a156d3de5647 | 43 | if (!_uip_udp_conn) { |
hudakz | 9:a156d3de5647 | 44 | _uip_udp_conn = uip_udp_new(NULL, 0); |
hudakz | 9:a156d3de5647 | 45 | } |
hudakz | 9:a156d3de5647 | 46 | |
hudakz | 9:a156d3de5647 | 47 | if (_uip_udp_conn) { |
hudakz | 9:a156d3de5647 | 48 | uip_udp_bind(_uip_udp_conn, htons(port)); |
hudakz | 9:a156d3de5647 | 49 | _uip_udp_conn->appstate = &appdata; |
hudakz | 9:a156d3de5647 | 50 | return 1; |
hudakz | 9:a156d3de5647 | 51 | } |
hudakz | 9:a156d3de5647 | 52 | |
hudakz | 9:a156d3de5647 | 53 | return 0; |
hudakz | 9:a156d3de5647 | 54 | } |
hudakz | 9:a156d3de5647 | 55 | |
hudakz | 9:a156d3de5647 | 56 | // Finish with the UDP socket |
hudakz | 9:a156d3de5647 | 57 | void UdpSocket::stop() |
hudakz | 9:a156d3de5647 | 58 | { |
hudakz | 9:a156d3de5647 | 59 | if (_uip_udp_conn) { |
hudakz | 9:a156d3de5647 | 60 | uip_udp_remove(_uip_udp_conn); |
hudakz | 9:a156d3de5647 | 61 | _uip_udp_conn->appstate = NULL; |
hudakz | 9:a156d3de5647 | 62 | _uip_udp_conn = NULL; |
hudakz | 11:647d53d146f1 | 63 | UipEthernet::ethernet->enc28j60Eth.freeBlock(appdata.packet_in); |
hudakz | 11:647d53d146f1 | 64 | UipEthernet::ethernet->enc28j60Eth.freeBlock(appdata.packet_next); |
hudakz | 11:647d53d146f1 | 65 | UipEthernet::ethernet->enc28j60Eth.freeBlock(appdata.packet_out); |
hudakz | 9:a156d3de5647 | 66 | memset(&appdata, 0, sizeof(appdata)); |
hudakz | 9:a156d3de5647 | 67 | } |
hudakz | 9:a156d3de5647 | 68 | } |
hudakz | 9:a156d3de5647 | 69 | |
hudakz | 9:a156d3de5647 | 70 | // Sending UDP packets |
hudakz | 9:a156d3de5647 | 71 | // Start building up a packet to send to the remote host specific in ip and port |
hudakz | 9:a156d3de5647 | 72 | |
hudakz | 9:a156d3de5647 | 73 | // Returns 1 if successful, 0 if there was a problem with the supplied IP address or port |
hudakz | 9:a156d3de5647 | 74 | int UdpSocket::beginPacket(IpAddress ip, uint16_t port) |
hudakz | 9:a156d3de5647 | 75 | { |
hudakz | 9:a156d3de5647 | 76 | UipEthernet::ethernet->tick(); |
hudakz | 9:a156d3de5647 | 77 | if (ip && port) { |
hudakz | 9:a156d3de5647 | 78 | uip_ipaddr_t ripaddr; |
hudakz | 9:a156d3de5647 | 79 | uip_ip_addr(&ripaddr, ip); |
hudakz | 9:a156d3de5647 | 80 | #ifdef UIPETHERNET_DEBUG_UDP |
hudakz | 9:a156d3de5647 | 81 | printf("udp beginPacket, "); |
hudakz | 9:a156d3de5647 | 82 | #endif |
hudakz | 9:a156d3de5647 | 83 | if (_uip_udp_conn) { |
hudakz | 9:a156d3de5647 | 84 | _uip_udp_conn->rport = htons(port); |
hudakz | 9:a156d3de5647 | 85 | uip_ipaddr_copy(_uip_udp_conn->ripaddr, &ripaddr); |
hudakz | 9:a156d3de5647 | 86 | } |
hudakz | 9:a156d3de5647 | 87 | else { |
hudakz | 9:a156d3de5647 | 88 | _uip_udp_conn = uip_udp_new(&ripaddr, htons(port)); |
hudakz | 9:a156d3de5647 | 89 | if (_uip_udp_conn) |
hudakz | 9:a156d3de5647 | 90 | { |
hudakz | 9:a156d3de5647 | 91 | #ifdef UIPETHERNET_DEBUG_UDP |
hudakz | 9:a156d3de5647 | 92 | printf("new connection, "); |
hudakz | 9:a156d3de5647 | 93 | #endif |
hudakz | 9:a156d3de5647 | 94 | _uip_udp_conn->appstate = &appdata; |
hudakz | 9:a156d3de5647 | 95 | } |
hudakz | 9:a156d3de5647 | 96 | else |
hudakz | 9:a156d3de5647 | 97 | { |
hudakz | 9:a156d3de5647 | 98 | #ifdef UIPETHERNET_DEBUG_UDP |
hudakz | 9:a156d3de5647 | 99 | printf("failed to allocate new connection\r\n"); |
hudakz | 9:a156d3de5647 | 100 | #endif |
hudakz | 9:a156d3de5647 | 101 | return 0; |
hudakz | 9:a156d3de5647 | 102 | } |
hudakz | 9:a156d3de5647 | 103 | } |
hudakz | 9:a156d3de5647 | 104 | |
hudakz | 9:a156d3de5647 | 105 | #ifdef UIPETHERNET_DEBUG_UDP |
hudakz | 9:a156d3de5647 | 106 | printf("rip: %s, port: %d\r\n", ip.asString(), port); |
hudakz | 9:a156d3de5647 | 107 | #endif |
hudakz | 9:a156d3de5647 | 108 | } |
hudakz | 9:a156d3de5647 | 109 | |
hudakz | 9:a156d3de5647 | 110 | if (_uip_udp_conn) { |
hudakz | 9:a156d3de5647 | 111 | if (appdata.packet_out == NOBLOCK) { |
hudakz | 11:647d53d146f1 | 112 | appdata.packet_out = UipEthernet::ethernet->enc28j60Eth.allocBlock(UIP_UDP_MAXPACKETSIZE); |
hudakz | 9:a156d3de5647 | 113 | appdata.out_pos = UIP_UDP_PHYH_LEN; |
hudakz | 9:a156d3de5647 | 114 | if (appdata.packet_out != NOBLOCK) |
hudakz | 9:a156d3de5647 | 115 | return 1; |
hudakz | 9:a156d3de5647 | 116 | #ifdef UIPETHERNET_DEBUG_UDP |
hudakz | 9:a156d3de5647 | 117 | else |
hudakz | 9:a156d3de5647 | 118 | printf("failed to allocate memory for packet\r\n"); |
hudakz | 9:a156d3de5647 | 119 | #endif |
hudakz | 9:a156d3de5647 | 120 | } |
hudakz | 9:a156d3de5647 | 121 | |
hudakz | 9:a156d3de5647 | 122 | #ifdef UIPETHERNET_DEBUG_UDP |
hudakz | 9:a156d3de5647 | 123 | else |
hudakz | 9:a156d3de5647 | 124 | printf("previous packet on that connection not sent yet\r\n"); |
hudakz | 9:a156d3de5647 | 125 | #endif |
hudakz | 9:a156d3de5647 | 126 | } |
hudakz | 9:a156d3de5647 | 127 | |
hudakz | 9:a156d3de5647 | 128 | return 0; |
hudakz | 9:a156d3de5647 | 129 | } |
hudakz | 9:a156d3de5647 | 130 | |
hudakz | 9:a156d3de5647 | 131 | // Start building up a packet to send to the remote host specific in host and port |
hudakz | 9:a156d3de5647 | 132 | |
hudakz | 9:a156d3de5647 | 133 | // Returns 1 if successful, 0 if there was a problem resolving the hostname or port |
hudakz | 9:a156d3de5647 | 134 | int UdpSocket::beginPacket(const char* host, uint16_t port) |
hudakz | 9:a156d3de5647 | 135 | { |
hudakz | 9:a156d3de5647 | 136 | // Look up the host first |
hudakz | 9:a156d3de5647 | 137 | int ret = 0; |
hudakz | 9:a156d3de5647 | 138 | DnsClient dns; |
hudakz | 9:a156d3de5647 | 139 | IpAddress remote_addr; |
hudakz | 9:a156d3de5647 | 140 | |
hudakz | 9:a156d3de5647 | 141 | dns.begin(UipEthernet::ethernet->dnsServerIP()); |
hudakz | 9:a156d3de5647 | 142 | ret = dns.getHostByName(host, remote_addr); |
hudakz | 9:a156d3de5647 | 143 | if (ret == 1) { |
hudakz | 9:a156d3de5647 | 144 | return beginPacket(remote_addr, port); |
hudakz | 9:a156d3de5647 | 145 | } |
hudakz | 9:a156d3de5647 | 146 | else { |
hudakz | 9:a156d3de5647 | 147 | return ret; |
hudakz | 9:a156d3de5647 | 148 | } |
hudakz | 9:a156d3de5647 | 149 | } |
hudakz | 9:a156d3de5647 | 150 | |
hudakz | 9:a156d3de5647 | 151 | // Finish off this packet and send it |
hudakz | 9:a156d3de5647 | 152 | |
hudakz | 9:a156d3de5647 | 153 | // Returns 1 if the packet was sent successfully, 0 if there was an error |
hudakz | 9:a156d3de5647 | 154 | int UdpSocket::endPacket() |
hudakz | 9:a156d3de5647 | 155 | { |
hudakz | 9:a156d3de5647 | 156 | if (_uip_udp_conn && appdata.packet_out != NOBLOCK) { |
hudakz | 9:a156d3de5647 | 157 | appdata.send = true; |
hudakz | 11:647d53d146f1 | 158 | UipEthernet::ethernet->enc28j60Eth.resizeBlock(appdata.packet_out, 0, appdata.out_pos); |
hudakz | 9:a156d3de5647 | 159 | uip_udp_periodic_conn(_uip_udp_conn); |
hudakz | 9:a156d3de5647 | 160 | if (uip_len > 0) { |
hudakz | 9:a156d3de5647 | 161 | _send(&appdata); |
hudakz | 9:a156d3de5647 | 162 | return 1; |
hudakz | 9:a156d3de5647 | 163 | } |
hudakz | 9:a156d3de5647 | 164 | } |
hudakz | 9:a156d3de5647 | 165 | |
hudakz | 9:a156d3de5647 | 166 | return 0; |
hudakz | 9:a156d3de5647 | 167 | } |
hudakz | 9:a156d3de5647 | 168 | |
hudakz | 9:a156d3de5647 | 169 | // Write a single byte into the packet |
hudakz | 9:a156d3de5647 | 170 | size_t UdpSocket::write(uint8_t c) |
hudakz | 9:a156d3de5647 | 171 | { |
hudakz | 9:a156d3de5647 | 172 | return write(&c, 1); |
hudakz | 9:a156d3de5647 | 173 | } |
hudakz | 9:a156d3de5647 | 174 | |
hudakz | 9:a156d3de5647 | 175 | // Write size bytes from buffer into the packet |
hudakz | 9:a156d3de5647 | 176 | size_t UdpSocket::write(const uint8_t* buffer, size_t size) |
hudakz | 9:a156d3de5647 | 177 | { |
hudakz | 9:a156d3de5647 | 178 | if (appdata.packet_out != NOBLOCK) { |
hudakz | 11:647d53d146f1 | 179 | size_t ret = UipEthernet::ethernet->enc28j60Eth.writePacket |
hudakz | 9:a156d3de5647 | 180 | ( |
hudakz | 9:a156d3de5647 | 181 | appdata.packet_out, |
hudakz | 9:a156d3de5647 | 182 | appdata.out_pos, |
hudakz | 9:a156d3de5647 | 183 | (uint8_t*)buffer, |
hudakz | 9:a156d3de5647 | 184 | size |
hudakz | 9:a156d3de5647 | 185 | ); |
hudakz | 9:a156d3de5647 | 186 | appdata.out_pos += ret; |
hudakz | 9:a156d3de5647 | 187 | return ret; |
hudakz | 9:a156d3de5647 | 188 | } |
hudakz | 9:a156d3de5647 | 189 | |
hudakz | 9:a156d3de5647 | 190 | return 0; |
hudakz | 9:a156d3de5647 | 191 | } |
hudakz | 9:a156d3de5647 | 192 | |
hudakz | 9:a156d3de5647 | 193 | // Start processing the next available incoming packet |
hudakz | 9:a156d3de5647 | 194 | |
hudakz | 9:a156d3de5647 | 195 | // Returns the size of the packet in bytes, or 0 if no packets are available |
hudakz | 9:a156d3de5647 | 196 | int UdpSocket::parsePacket() |
hudakz | 9:a156d3de5647 | 197 | { |
hudakz | 9:a156d3de5647 | 198 | UipEthernet::ethernet->tick(); |
hudakz | 9:a156d3de5647 | 199 | #ifdef UIPETHERNET_DEBUG_UDP |
hudakz | 9:a156d3de5647 | 200 | if (appdata.packet_in != NOBLOCK) { |
hudakz | 9:a156d3de5647 | 201 | printf("udp parsePacket freeing previous packet: %d\r\n", appdata.packet_in); |
hudakz | 9:a156d3de5647 | 202 | } |
hudakz | 9:a156d3de5647 | 203 | #endif |
hudakz | 11:647d53d146f1 | 204 | UipEthernet::ethernet->enc28j60Eth.freeBlock(appdata.packet_in); |
hudakz | 9:a156d3de5647 | 205 | |
hudakz | 9:a156d3de5647 | 206 | appdata.packet_in = appdata.packet_next; |
hudakz | 9:a156d3de5647 | 207 | appdata.packet_next = NOBLOCK; |
hudakz | 9:a156d3de5647 | 208 | |
hudakz | 9:a156d3de5647 | 209 | #ifdef UIPETHERNET_DEBUG_UDP |
hudakz | 9:a156d3de5647 | 210 | if (appdata.packet_in != NOBLOCK) { |
hudakz | 9:a156d3de5647 | 211 | printf("udp parsePacket received packet: %d", appdata.packet_in); |
hudakz | 9:a156d3de5647 | 212 | } |
hudakz | 9:a156d3de5647 | 213 | #endif |
hudakz | 9:a156d3de5647 | 214 | |
hudakz | 11:647d53d146f1 | 215 | int size = UipEthernet::ethernet->enc28j60Eth.blockSize(appdata.packet_in); |
hudakz | 9:a156d3de5647 | 216 | #ifdef UIPETHERNET_DEBUG_UDP |
hudakz | 9:a156d3de5647 | 217 | if (appdata.packet_in != NOBLOCK) { |
hudakz | 9:a156d3de5647 | 218 | printf(", size: %d\r\n", size); |
hudakz | 9:a156d3de5647 | 219 | } |
hudakz | 9:a156d3de5647 | 220 | #endif |
hudakz | 9:a156d3de5647 | 221 | return size; |
hudakz | 9:a156d3de5647 | 222 | } |
hudakz | 9:a156d3de5647 | 223 | |
hudakz | 9:a156d3de5647 | 224 | // Number of bytes remaining in the current packet |
hudakz | 9:a156d3de5647 | 225 | int UdpSocket::available() |
hudakz | 9:a156d3de5647 | 226 | { |
hudakz | 9:a156d3de5647 | 227 | UipEthernet::ethernet->tick(); |
hudakz | 11:647d53d146f1 | 228 | return UipEthernet::ethernet->enc28j60Eth.blockSize(appdata.packet_in); |
hudakz | 9:a156d3de5647 | 229 | } |
hudakz | 9:a156d3de5647 | 230 | |
hudakz | 9:a156d3de5647 | 231 | // Read a single byte from the current packet |
hudakz | 9:a156d3de5647 | 232 | int UdpSocket::read() |
hudakz | 9:a156d3de5647 | 233 | { |
hudakz | 9:a156d3de5647 | 234 | static unsigned char c; |
hudakz | 9:a156d3de5647 | 235 | if (read(&c, 1) > 0) { |
hudakz | 9:a156d3de5647 | 236 | return c; |
hudakz | 9:a156d3de5647 | 237 | } |
hudakz | 9:a156d3de5647 | 238 | |
hudakz | 9:a156d3de5647 | 239 | return -1; |
hudakz | 9:a156d3de5647 | 240 | } |
hudakz | 9:a156d3de5647 | 241 | |
hudakz | 9:a156d3de5647 | 242 | // Read up to len bytes from the current packet and place them into buffer |
hudakz | 9:a156d3de5647 | 243 | |
hudakz | 9:a156d3de5647 | 244 | // Returns the number of bytes read, or 0 if none are available |
hudakz | 9:a156d3de5647 | 245 | int UdpSocket::read(unsigned char* buffer, size_t len) |
hudakz | 9:a156d3de5647 | 246 | { |
hudakz | 9:a156d3de5647 | 247 | UipEthernet::ethernet->tick(); |
hudakz | 9:a156d3de5647 | 248 | if (appdata.packet_in != NOBLOCK) { |
hudakz | 11:647d53d146f1 | 249 | memaddress read = UipEthernet::ethernet->enc28j60Eth.readPacket(appdata.packet_in, 0, buffer, len); |
hudakz | 11:647d53d146f1 | 250 | if (read == UipEthernet::ethernet->enc28j60Eth.blockSize(appdata.packet_in)) { |
hudakz | 11:647d53d146f1 | 251 | UipEthernet::ethernet->enc28j60Eth.freeBlock(appdata.packet_in); |
hudakz | 9:a156d3de5647 | 252 | appdata.packet_in = NOBLOCK; |
hudakz | 9:a156d3de5647 | 253 | } |
hudakz | 9:a156d3de5647 | 254 | else |
hudakz | 11:647d53d146f1 | 255 | UipEthernet::ethernet->enc28j60Eth.resizeBlock(appdata.packet_in, read); |
hudakz | 9:a156d3de5647 | 256 | return read; |
hudakz | 9:a156d3de5647 | 257 | } |
hudakz | 9:a156d3de5647 | 258 | |
hudakz | 9:a156d3de5647 | 259 | return 0; |
hudakz | 9:a156d3de5647 | 260 | } |
hudakz | 9:a156d3de5647 | 261 | |
hudakz | 9:a156d3de5647 | 262 | // Return the next byte from the current packet without moving on to the next byte |
hudakz | 9:a156d3de5647 | 263 | int UdpSocket::peek() |
hudakz | 9:a156d3de5647 | 264 | { |
hudakz | 9:a156d3de5647 | 265 | UipEthernet::ethernet->tick(); |
hudakz | 9:a156d3de5647 | 266 | if (appdata.packet_in != NOBLOCK) { |
hudakz | 9:a156d3de5647 | 267 | unsigned char c; |
hudakz | 11:647d53d146f1 | 268 | if (UipEthernet::ethernet->enc28j60Eth.readPacket(appdata.packet_in, 0, &c, 1) == 1) |
hudakz | 9:a156d3de5647 | 269 | return c; |
hudakz | 9:a156d3de5647 | 270 | } |
hudakz | 9:a156d3de5647 | 271 | |
hudakz | 9:a156d3de5647 | 272 | return -1; |
hudakz | 9:a156d3de5647 | 273 | } |
hudakz | 9:a156d3de5647 | 274 | |
hudakz | 9:a156d3de5647 | 275 | // Finish reading the current packet |
hudakz | 9:a156d3de5647 | 276 | void UdpSocket::flush() |
hudakz | 9:a156d3de5647 | 277 | { |
hudakz | 9:a156d3de5647 | 278 | UipEthernet::ethernet->tick(); |
hudakz | 11:647d53d146f1 | 279 | UipEthernet::ethernet->enc28j60Eth.freeBlock(appdata.packet_in); |
hudakz | 9:a156d3de5647 | 280 | appdata.packet_in = NOBLOCK; |
hudakz | 9:a156d3de5647 | 281 | } |
hudakz | 9:a156d3de5647 | 282 | |
hudakz | 9:a156d3de5647 | 283 | // Return the IP address of the host who sent the current incoming packet |
hudakz | 9:a156d3de5647 | 284 | IpAddress UdpSocket::remoteIP() |
hudakz | 9:a156d3de5647 | 285 | { |
hudakz | 9:a156d3de5647 | 286 | return _uip_udp_conn ? ip_addr_uip(_uip_udp_conn->ripaddr) : IpAddress(); |
hudakz | 9:a156d3de5647 | 287 | } |
hudakz | 9:a156d3de5647 | 288 | |
hudakz | 9:a156d3de5647 | 289 | // Return the port of the host who sent the current incoming packet |
hudakz | 9:a156d3de5647 | 290 | uint16_t UdpSocket::remotePort() |
hudakz | 9:a156d3de5647 | 291 | { |
hudakz | 9:a156d3de5647 | 292 | return _uip_udp_conn ? ntohs(_uip_udp_conn->rport) : 0; |
hudakz | 9:a156d3de5647 | 293 | } |
hudakz | 9:a156d3de5647 | 294 | |
hudakz | 9:a156d3de5647 | 295 | // UIP callback function |
hudakz | 9:a156d3de5647 | 296 | void uipudp_appcall() |
hudakz | 9:a156d3de5647 | 297 | { |
hudakz | 9:a156d3de5647 | 298 | if (uip_udp_userdata_t * data = (uip_udp_userdata_t *) (uip_udp_conn->appstate)) { |
hudakz | 9:a156d3de5647 | 299 | if (uip_newdata()) { |
hudakz | 9:a156d3de5647 | 300 | if (data->packet_next == NOBLOCK) { |
hudakz | 9:a156d3de5647 | 301 | uip_udp_conn->rport = UDPBUF->srcport; |
hudakz | 9:a156d3de5647 | 302 | uip_ipaddr_copy(uip_udp_conn->ripaddr, UDPBUF->srcipaddr); |
hudakz | 11:647d53d146f1 | 303 | data->packet_next = UipEthernet::ethernet->enc28j60Eth.allocBlock(ntohs(UDPBUF->udplen) - UIP_UDPH_LEN); |
hudakz | 9:a156d3de5647 | 304 | |
hudakz | 9:a156d3de5647 | 305 | //if we are unable to allocate memory the packet is dropped. udp doesn't guarantee packet delivery |
hudakz | 9:a156d3de5647 | 306 | if (data->packet_next != NOBLOCK) { |
hudakz | 9:a156d3de5647 | 307 | //discard Linklevel and IP and udp-header and any trailing bytes: |
hudakz | 11:647d53d146f1 | 308 | UipEthernet::ethernet->enc28j60Eth.copyPacket |
hudakz | 9:a156d3de5647 | 309 | ( |
hudakz | 9:a156d3de5647 | 310 | data->packet_next, |
hudakz | 9:a156d3de5647 | 311 | 0, |
hudakz | 9:a156d3de5647 | 312 | UipEthernet::inPacket, |
hudakz | 9:a156d3de5647 | 313 | UIP_UDP_PHYH_LEN, |
hudakz | 11:647d53d146f1 | 314 | UipEthernet::ethernet->enc28j60Eth.blockSize(data->packet_next) |
hudakz | 9:a156d3de5647 | 315 | ); |
hudakz | 9:a156d3de5647 | 316 | #ifdef UIPETHERNET_DEBUG_UDP |
hudakz | 9:a156d3de5647 | 317 | printf |
hudakz | 9:a156d3de5647 | 318 | ( |
hudakz | 9:a156d3de5647 | 319 | "udp, uip_newdata received packet: %d, size: %d\r\n", |
hudakz | 9:a156d3de5647 | 320 | data->packet_next, |
hudakz | 9:a156d3de5647 | 321 | UIPEthernet.network.blockSize(data->packet_next) |
hudakz | 9:a156d3de5647 | 322 | ); |
hudakz | 9:a156d3de5647 | 323 | #endif |
hudakz | 9:a156d3de5647 | 324 | } |
hudakz | 9:a156d3de5647 | 325 | } |
hudakz | 9:a156d3de5647 | 326 | } |
hudakz | 9:a156d3de5647 | 327 | |
hudakz | 9:a156d3de5647 | 328 | if (uip_poll() && data->send) |
hudakz | 9:a156d3de5647 | 329 | { |
hudakz | 9:a156d3de5647 | 330 | //set uip_slen (uip private) by calling uip_udp_send |
hudakz | 9:a156d3de5647 | 331 | #ifdef UIPETHERNET_DEBUG_UDP |
hudakz | 9:a156d3de5647 | 332 | printf |
hudakz | 9:a156d3de5647 | 333 | ( |
hudakz | 9:a156d3de5647 | 334 | "udp, uip_poll preparing packet to send: %d, size: %d\r\n", |
hudakz | 9:a156d3de5647 | 335 | data->packet_out, |
hudakz | 9:a156d3de5647 | 336 | UIPEthernet.network.blockSize(data->packet_out) |
hudakz | 9:a156d3de5647 | 337 | ); |
hudakz | 9:a156d3de5647 | 338 | #endif |
hudakz | 9:a156d3de5647 | 339 | UipEthernet::uipPacket = data->packet_out; |
hudakz | 9:a156d3de5647 | 340 | UipEthernet::uipHeaderLen = UIP_UDP_PHYH_LEN; |
hudakz | 9:a156d3de5647 | 341 | uip_udp_send(data->out_pos - (UIP_UDP_PHYH_LEN)); |
hudakz | 9:a156d3de5647 | 342 | } |
hudakz | 9:a156d3de5647 | 343 | } |
hudakz | 9:a156d3de5647 | 344 | } |
hudakz | 9:a156d3de5647 | 345 | |
hudakz | 9:a156d3de5647 | 346 | /** |
hudakz | 9:a156d3de5647 | 347 | * @brief |
hudakz | 9:a156d3de5647 | 348 | * @note |
hudakz | 9:a156d3de5647 | 349 | * @param |
hudakz | 9:a156d3de5647 | 350 | * @retval |
hudakz | 9:a156d3de5647 | 351 | */ |
hudakz | 9:a156d3de5647 | 352 | void UdpSocket::_send(uip_udp_userdata_t* data) |
hudakz | 9:a156d3de5647 | 353 | { |
hudakz | 9:a156d3de5647 | 354 | uip_arp_out(); //add arp |
hudakz | 9:a156d3de5647 | 355 | if (uip_len == UIP_ARPHDRSIZE) { |
hudakz | 9:a156d3de5647 | 356 | UipEthernet::uipPacket = NOBLOCK; |
hudakz | 9:a156d3de5647 | 357 | UipEthernet::packetState &= ~UIPETHERNET_SENDPACKET; |
hudakz | 9:a156d3de5647 | 358 | #ifdef UIPETHERNET_DEBUG_UDP |
hudakz | 9:a156d3de5647 | 359 | printf("udp, uip_poll results in ARP-packet\r\n"); |
hudakz | 9:a156d3de5647 | 360 | #endif |
hudakz | 9:a156d3de5647 | 361 | } |
hudakz | 9:a156d3de5647 | 362 | else { |
hudakz | 9:a156d3de5647 | 363 | //arp found ethaddr for ip (otherwise packet is replaced by arp-request) |
hudakz | 9:a156d3de5647 | 364 | data->send = false; |
hudakz | 9:a156d3de5647 | 365 | data->packet_out = NOBLOCK; |
hudakz | 9:a156d3de5647 | 366 | UipEthernet::packetState |= UIPETHERNET_SENDPACKET; |
hudakz | 9:a156d3de5647 | 367 | #ifdef UIPETHERNET_DEBUG_UDP |
hudakz | 9:a156d3de5647 | 368 | printf("udp, uip_packet to send: %d\r\n", UIPEthernet::uip_packet); |
hudakz | 9:a156d3de5647 | 369 | #endif |
hudakz | 9:a156d3de5647 | 370 | } |
hudakz | 9:a156d3de5647 | 371 | |
hudakz | 9:a156d3de5647 | 372 | UipEthernet::ethernet->network_send(); |
hudakz | 9:a156d3de5647 | 373 | } |
hudakz | 9:a156d3de5647 | 374 | #endif |