Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
WifiRobot/cc3000/Socket/UDPSocket.cpp@0:2deb247ab4bd, 2014-12-09 (annotated)
- Committer:
- wueric
- Date:
- Tue Dec 09 05:06:37 2014 +0000
- Revision:
- 0:2deb247ab4bd
sharing stuff;
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| wueric | 0:2deb247ab4bd | 1 | /* Copyright (C) 2013 mbed.org, MIT License |
| wueric | 0:2deb247ab4bd | 2 | * |
| wueric | 0:2deb247ab4bd | 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy of this software |
| wueric | 0:2deb247ab4bd | 4 | * and associated documentation files (the "Software"), to deal in the Software without restriction, |
| wueric | 0:2deb247ab4bd | 5 | * including without limitation the rights to use, copy, modify, merge, publish, distribute, |
| wueric | 0:2deb247ab4bd | 6 | * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is |
| wueric | 0:2deb247ab4bd | 7 | * furnished to do so, subject to the following conditions: |
| wueric | 0:2deb247ab4bd | 8 | * |
| wueric | 0:2deb247ab4bd | 9 | * The above copyright notice and this permission notice shall be included in all copies or |
| wueric | 0:2deb247ab4bd | 10 | * substantial portions of the Software. |
| wueric | 0:2deb247ab4bd | 11 | * |
| wueric | 0:2deb247ab4bd | 12 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING |
| wueric | 0:2deb247ab4bd | 13 | * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| wueric | 0:2deb247ab4bd | 14 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
| wueric | 0:2deb247ab4bd | 15 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| wueric | 0:2deb247ab4bd | 16 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| wueric | 0:2deb247ab4bd | 17 | */ |
| wueric | 0:2deb247ab4bd | 18 | |
| wueric | 0:2deb247ab4bd | 19 | #include "UDPSocket.h" |
| wueric | 0:2deb247ab4bd | 20 | |
| wueric | 0:2deb247ab4bd | 21 | #include <string> |
| wueric | 0:2deb247ab4bd | 22 | #include <algorithm> |
| wueric | 0:2deb247ab4bd | 23 | |
| wueric | 0:2deb247ab4bd | 24 | UDPSocket::UDPSocket() { |
| wueric | 0:2deb247ab4bd | 25 | |
| wueric | 0:2deb247ab4bd | 26 | } |
| wueric | 0:2deb247ab4bd | 27 | |
| wueric | 0:2deb247ab4bd | 28 | int UDPSocket::init(void) { |
| wueric | 0:2deb247ab4bd | 29 | return init_socket(SOCK_DGRAM, IPPROTO_UDP); |
| wueric | 0:2deb247ab4bd | 30 | } |
| wueric | 0:2deb247ab4bd | 31 | |
| wueric | 0:2deb247ab4bd | 32 | int UDPSocket::bind(int port) { |
| wueric | 0:2deb247ab4bd | 33 | if (init_socket(SOCK_DGRAM, IPPROTO_UDP) < 0) { |
| wueric | 0:2deb247ab4bd | 34 | return -1; |
| wueric | 0:2deb247ab4bd | 35 | } |
| wueric | 0:2deb247ab4bd | 36 | |
| wueric | 0:2deb247ab4bd | 37 | sockaddr_in localHost; |
| wueric | 0:2deb247ab4bd | 38 | std::memset(&localHost, 0, sizeof(sockaddr_in)); |
| wueric | 0:2deb247ab4bd | 39 | |
| wueric | 0:2deb247ab4bd | 40 | localHost.sin_family = AF_INET; |
| wueric | 0:2deb247ab4bd | 41 | localHost.sin_port = htons(port); |
| wueric | 0:2deb247ab4bd | 42 | localHost.sin_addr.s_addr = 0; |
| wueric | 0:2deb247ab4bd | 43 | |
| wueric | 0:2deb247ab4bd | 44 | if (_cc3000_module->_socket.bind(_sock_fd, (sockaddr *)&localHost, sizeof(sockaddr_in)) != 0) { |
| wueric | 0:2deb247ab4bd | 45 | DBG_SOCKET("Failed to bind a socket (udp). Closing socket"); |
| wueric | 0:2deb247ab4bd | 46 | _cc3000_module->_socket.closesocket(_sock_fd); |
| wueric | 0:2deb247ab4bd | 47 | _sock_fd = -1; |
| wueric | 0:2deb247ab4bd | 48 | return -1; |
| wueric | 0:2deb247ab4bd | 49 | } |
| wueric | 0:2deb247ab4bd | 50 | |
| wueric | 0:2deb247ab4bd | 51 | return 0; |
| wueric | 0:2deb247ab4bd | 52 | } |
| wueric | 0:2deb247ab4bd | 53 | |
| wueric | 0:2deb247ab4bd | 54 | int UDPSocket::sendTo(Endpoint &remote, char *packet, int length) |
| wueric | 0:2deb247ab4bd | 55 | { |
| wueric | 0:2deb247ab4bd | 56 | if (_sock_fd < 0) { |
| wueric | 0:2deb247ab4bd | 57 | return -1; |
| wueric | 0:2deb247ab4bd | 58 | } |
| wueric | 0:2deb247ab4bd | 59 | // TODO - seems to be a bug, waiting for TI to respond |
| wueric | 0:2deb247ab4bd | 60 | // if (!_blocking) { |
| wueric | 0:2deb247ab4bd | 61 | // TimeInterval timeout(_timeout); |
| wueric | 0:2deb247ab4bd | 62 | // if (wait_writable(timeout) != 0) { |
| wueric | 0:2deb247ab4bd | 63 | // DBG_SOCKET("The socket is not writeable. _sock_fd: %d", _sock_fd); |
| wueric | 0:2deb247ab4bd | 64 | // return 0; |
| wueric | 0:2deb247ab4bd | 65 | // } |
| wueric | 0:2deb247ab4bd | 66 | // } |
| wueric | 0:2deb247ab4bd | 67 | |
| wueric | 0:2deb247ab4bd | 68 | return _cc3000_module->_socket.sendto(_sock_fd, packet, length, 0, (sockaddr *)&remote._remote_host, sizeof(sockaddr)); |
| wueric | 0:2deb247ab4bd | 69 | } |
| wueric | 0:2deb247ab4bd | 70 | |
| wueric | 0:2deb247ab4bd | 71 | int UDPSocket::receiveFrom(Endpoint &remote, char *buffer, int length) |
| wueric | 0:2deb247ab4bd | 72 | { |
| wueric | 0:2deb247ab4bd | 73 | if (_sock_fd < 0) { |
| wueric | 0:2deb247ab4bd | 74 | return -1; |
| wueric | 0:2deb247ab4bd | 75 | } |
| wueric | 0:2deb247ab4bd | 76 | |
| wueric | 0:2deb247ab4bd | 77 | if (!_blocking) { |
| wueric | 0:2deb247ab4bd | 78 | TimeInterval timeout(_timeout); |
| wueric | 0:2deb247ab4bd | 79 | if (wait_readable(timeout) != 0) { |
| wueric | 0:2deb247ab4bd | 80 | DBG_SOCKET("The socket is not readable. _sock_fd: %d", _sock_fd); |
| wueric | 0:2deb247ab4bd | 81 | return 0; |
| wueric | 0:2deb247ab4bd | 82 | } |
| wueric | 0:2deb247ab4bd | 83 | } |
| wueric | 0:2deb247ab4bd | 84 | |
| wueric | 0:2deb247ab4bd | 85 | remote.reset_address(); |
| wueric | 0:2deb247ab4bd | 86 | socklen_t remote_host_length = sizeof(remote._remote_host); |
| wueric | 0:2deb247ab4bd | 87 | |
| wueric | 0:2deb247ab4bd | 88 | return _cc3000_module->_socket.recvfrom(_sock_fd, buffer, length, 0, (sockaddr *)&remote._remote_host, &remote_host_length); |
| wueric | 0:2deb247ab4bd | 89 | } |
| wueric | 0:2deb247ab4bd | 90 | |
| wueric | 0:2deb247ab4bd | 91 |
