The code from https://github.com/vpcola/Nucleo

Committer:
sinrab
Date:
Wed Oct 08 11:00:24 2014 +0000
Revision:
0:5464d5e415e5
The code from https://github.com/vpcola/Nucleo

Who changed what in which revision?

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