Added mutex for multiple SPI devices on the same SPI bus
Fork of cc3000_hostdriver_mbedsocket by
Socket/UDPSocket.cpp@12:1c2a856c618a, 2013-10-02 (annotated)
- Committer:
- Martin Kojtal
- Date:
- Wed Oct 02 17:02:34 2013 +0200
- Revision:
- 12:1c2a856c618a
- Parent:
- 11:5e3771b29385
- Child:
- 16:f3676ae62f96
Debug messages (\r\n), socket bsd naming
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Kojto | 5:245ac5b73132 | 1 | /* Copyright (C) 2013 mbed.org, MIT License |
Kojto | 0:615c697c33b0 | 2 | * |
Kojto | 0:615c697c33b0 | 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy of this software |
Kojto | 0:615c697c33b0 | 4 | * and associated documentation files (the "Software"), to deal in the Software without restriction, |
Kojto | 0:615c697c33b0 | 5 | * including without limitation the rights to use, copy, modify, merge, publish, distribute, |
Kojto | 0:615c697c33b0 | 6 | * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is |
Kojto | 0:615c697c33b0 | 7 | * furnished to do so, subject to the following conditions: |
Kojto | 0:615c697c33b0 | 8 | * |
Kojto | 0:615c697c33b0 | 9 | * The above copyright notice and this permission notice shall be included in all copies or |
Kojto | 0:615c697c33b0 | 10 | * substantial portions of the Software. |
Kojto | 0:615c697c33b0 | 11 | * |
Kojto | 0:615c697c33b0 | 12 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING |
Kojto | 0:615c697c33b0 | 13 | * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
Kojto | 0:615c697c33b0 | 14 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
Kojto | 0:615c697c33b0 | 15 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
Kojto | 0:615c697c33b0 | 16 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
Kojto | 0:615c697c33b0 | 17 | */ |
Kojto | 0:615c697c33b0 | 18 | |
Kojto | 0:615c697c33b0 | 19 | #include "UDPSocket.h" |
Kojto | 0:615c697c33b0 | 20 | |
Kojto | 0:615c697c33b0 | 21 | #include <string> |
Kojto | 0:615c697c33b0 | 22 | #include <algorithm> |
Kojto | 0:615c697c33b0 | 23 | |
Kojto | 0:615c697c33b0 | 24 | UDPSocket::UDPSocket() { |
Kojto | 0:615c697c33b0 | 25 | |
Kojto | 0:615c697c33b0 | 26 | } |
Kojto | 0:615c697c33b0 | 27 | |
Kojto | 0:615c697c33b0 | 28 | int UDPSocket::init(void) { |
Kojto | 4:15b58c119a0a | 29 | return init_socket(SOCK_DGRAM, IPPROTO_UDP); |
Kojto | 0:615c697c33b0 | 30 | } |
Kojto | 0:615c697c33b0 | 31 | |
Kojto | 0:615c697c33b0 | 32 | int UDPSocket::bind(int port) { |
Kojto | 4:15b58c119a0a | 33 | if (init_socket(SOCK_DGRAM, IPPROTO_UDP) < 0) { |
Kojto | 4:15b58c119a0a | 34 | return -1; |
Kojto | 4:15b58c119a0a | 35 | } |
Kojto | 0:615c697c33b0 | 36 | |
Kojto | 0:615c697c33b0 | 37 | sockaddr_in localHost; |
Kojto | 4:15b58c119a0a | 38 | std::memset(&localHost, 0, sizeof(sockaddr_in)); |
Kojto | 0:615c697c33b0 | 39 | |
Kojto | 0:615c697c33b0 | 40 | localHost.sin_family = AF_INET; |
Kojto | 0:615c697c33b0 | 41 | localHost.sin_port = htons(port); |
Kojto | 4:15b58c119a0a | 42 | localHost.sin_addr.s_addr = 0; |
Kojto | 0:615c697c33b0 | 43 | |
Kojto | 4:15b58c119a0a | 44 | if (_cc3000_module->_socket.bind(_sock_fd, (sockaddr *)&localHost, sizeof(sockaddr_in)) != 0) { |
Kojto | 0:615c697c33b0 | 45 | #if (CC3000_DEBUG == 1) |
SolderSplashLabs | 11:5e3771b29385 | 46 | printf("DEBUG: Failed to bind a socket (udp). Closing socket.\r\n"); |
Kojto | 0:615c697c33b0 | 47 | #endif |
Kojto | 0:615c697c33b0 | 48 | _cc3000_module->_socket.closesocket(_sock_fd); |
Kojto | 4:15b58c119a0a | 49 | _sock_fd = -1; |
Kojto | 0:615c697c33b0 | 50 | return -1; |
Kojto | 0:615c697c33b0 | 51 | } |
Kojto | 5:245ac5b73132 | 52 | |
Kojto | 0:615c697c33b0 | 53 | return 0; |
Kojto | 0:615c697c33b0 | 54 | } |
Kojto | 0:615c697c33b0 | 55 | |
Kojto | 0:615c697c33b0 | 56 | int UDPSocket::sendTo(Endpoint &remote, char *packet, int length) |
Kojto | 0:615c697c33b0 | 57 | { |
Kojto | 0:615c697c33b0 | 58 | if (_sock_fd < 0) { |
Kojto | 0:615c697c33b0 | 59 | return -1; |
Kojto | 0:615c697c33b0 | 60 | } |
Kojto | 0:615c697c33b0 | 61 | |
Martin Kojtal |
12:1c2a856c618a | 62 | if (!_blocking) { |
Martin Kojtal |
12:1c2a856c618a | 63 | TimeInterval timeout(_timeout); |
Martin Kojtal |
12:1c2a856c618a | 64 | if (wait_writable(timeout) != 0) { |
Martin Kojtal |
12:1c2a856c618a | 65 | #if (CC3000_DEBUG == 1) |
Martin Kojtal |
12:1c2a856c618a | 66 | printf("DEBUG: The socket is not writeable. _sock_fd: %d.\r\n", _sock_fd); |
Martin Kojtal |
12:1c2a856c618a | 67 | #endif |
Martin Kojtal |
12:1c2a856c618a | 68 | return 0; |
Martin Kojtal |
12:1c2a856c618a | 69 | } |
Martin Kojtal |
12:1c2a856c618a | 70 | } |
Kojto | 0:615c697c33b0 | 71 | |
Kojto | 4:15b58c119a0a | 72 | return _cc3000_module->_socket.sendto(_sock_fd, packet, length, 0, (sockaddr *)&remote._remote_host, sizeof(sockaddr)); |
Kojto | 0:615c697c33b0 | 73 | } |
Kojto | 0:615c697c33b0 | 74 | |
Kojto | 0:615c697c33b0 | 75 | int UDPSocket::receiveFrom(Endpoint &remote, char *buffer, int length) |
Kojto | 0:615c697c33b0 | 76 | { |
Kojto | 0:615c697c33b0 | 77 | if (_sock_fd < 0) { |
Kojto | 0:615c697c33b0 | 78 | return -1; |
Kojto | 0:615c697c33b0 | 79 | } |
Kojto | 0:615c697c33b0 | 80 | |
Kojto | 0:615c697c33b0 | 81 | if (!_blocking) { |
Kojto | 0:615c697c33b0 | 82 | TimeInterval timeout(_timeout); |
Kojto | 0:615c697c33b0 | 83 | if (wait_readable(timeout) != 0) { |
Kojto | 4:15b58c119a0a | 84 | #if (CC3000_DEBUG == 1) |
SolderSplashLabs | 11:5e3771b29385 | 85 | printf("DEBUG: The socket is not readable. _sock_fd: %d.\r\n", _sock_fd); |
Kojto | 4:15b58c119a0a | 86 | #endif |
Kojto | 0:615c697c33b0 | 87 | return 0; |
Kojto | 0:615c697c33b0 | 88 | } |
Kojto | 0:615c697c33b0 | 89 | } |
Kojto | 0:615c697c33b0 | 90 | |
Kojto | 0:615c697c33b0 | 91 | remote.reset_address(); |
Kojto | 0:615c697c33b0 | 92 | socklen_t remote_host_length = sizeof(remote._remote_host); |
Kojto | 0:615c697c33b0 | 93 | |
Kojto | 0:615c697c33b0 | 94 | return _cc3000_module->_socket.recvfrom(_sock_fd, buffer, length, 0, (sockaddr *)&remote._remote_host, &remote_host_length); |
Kojto | 0:615c697c33b0 | 95 | } |
Kojto | 0:615c697c33b0 | 96 | |
Kojto | 0:615c697c33b0 | 97 |