cc3000 hostdriver with the mbed socket interface
Fork of cc3000_hostdriver_mbedsocket by
Socket/UDPSocket.cpp@4:15b58c119a0a, 2013-09-21 (annotated)
- Committer:
- Kojto
- Date:
- Sat Sep 21 15:01:05 2013 +0000
- Revision:
- 4:15b58c119a0a
- Parent:
- 0:615c697c33b0
- Child:
- 5:245ac5b73132
mbed socket interface changes; - socket init (creates upd/tcp socket); - debug prints; host driver changes; - blocking call when closing socket (Frank's recommedation); - delete profiles (user info retrieved, then erased)
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Kojto | 0:615c697c33b0 | 1 | /* Copyright (C) 2012 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 | // Server initialization |
Kojto | 0:615c697c33b0 | 33 | int UDPSocket::bind(int port) { |
Kojto | 4:15b58c119a0a | 34 | if (init_socket(SOCK_DGRAM, IPPROTO_UDP) < 0) { |
Kojto | 4:15b58c119a0a | 35 | return -1; |
Kojto | 4:15b58c119a0a | 36 | } |
Kojto | 0:615c697c33b0 | 37 | |
Kojto | 0:615c697c33b0 | 38 | sockaddr_in localHost; |
Kojto | 4:15b58c119a0a | 39 | std::memset(&localHost, 0, sizeof(sockaddr_in)); |
Kojto | 0:615c697c33b0 | 40 | |
Kojto | 0:615c697c33b0 | 41 | localHost.sin_family = AF_INET; |
Kojto | 0:615c697c33b0 | 42 | localHost.sin_port = htons(port); |
Kojto | 4:15b58c119a0a | 43 | localHost.sin_addr.s_addr = 0; |
Kojto | 4:15b58c119a0a | 44 | // sockaddr localHost; |
Kojto | 4:15b58c119a0a | 45 | // localHost.family = AF_INET; |
Kojto | 4:15b58c119a0a | 46 | // localHost.data[0] = (port & 0xFF00)>> 8; |
Kojto | 4:15b58c119a0a | 47 | // localHost.data[1] = (port & 0x00FF); |
Kojto | 4:15b58c119a0a | 48 | // localHost.data[2] = 0; |
Kojto | 4:15b58c119a0a | 49 | // localHost.data[3] = 0; |
Kojto | 4:15b58c119a0a | 50 | // localHost.data[4] = 0; |
Kojto | 4:15b58c119a0a | 51 | // localHost.data[5] = 0; |
Kojto | 0:615c697c33b0 | 52 | |
Kojto | 4:15b58c119a0a | 53 | if (_cc3000_module->_socket.bind(_sock_fd, (sockaddr *)&localHost, sizeof(sockaddr_in)) != 0) { |
Kojto | 0:615c697c33b0 | 54 | #if (CC3000_DEBUG == 1) |
Kojto | 0:615c697c33b0 | 55 | printf("DEBUG: Failed to bind a socket (udp). Closing socket.\n"); |
Kojto | 0:615c697c33b0 | 56 | #endif |
Kojto | 0:615c697c33b0 | 57 | _cc3000_module->_socket.closesocket(_sock_fd); |
Kojto | 4:15b58c119a0a | 58 | _sock_fd = -1; |
Kojto | 0:615c697c33b0 | 59 | return -1; |
Kojto | 0:615c697c33b0 | 60 | } |
Kojto | 4:15b58c119a0a | 61 | // #if (CC3000_DEBUG == 1) |
Kojto | 4:15b58c119a0a | 62 | // printf("DEBUG: local add: %d\n",localHost.sin_addr.s_addr); |
Kojto | 4:15b58c119a0a | 63 | // #endif |
Kojto | 0:615c697c33b0 | 64 | return 0; |
Kojto | 0:615c697c33b0 | 65 | } |
Kojto | 0:615c697c33b0 | 66 | |
Kojto | 0:615c697c33b0 | 67 | // -1 if unsuccessful, else number of bytes written |
Kojto | 0:615c697c33b0 | 68 | int UDPSocket::sendTo(Endpoint &remote, char *packet, int length) |
Kojto | 0:615c697c33b0 | 69 | { |
Kojto | 0:615c697c33b0 | 70 | if (_sock_fd < 0) { |
Kojto | 0:615c697c33b0 | 71 | return -1; |
Kojto | 0:615c697c33b0 | 72 | } |
Kojto | 0:615c697c33b0 | 73 | |
Kojto | 0:615c697c33b0 | 74 | if (!_blocking) { |
Kojto | 0:615c697c33b0 | 75 | TimeInterval timeout(_timeout); |
Kojto | 4:15b58c119a0a | 76 | if (wait_writable(timeout) != 0) { |
Kojto | 4:15b58c119a0a | 77 | #if (CC3000_DEBUG == 1) |
Kojto | 4:15b58c119a0a | 78 | printf("DEBUG: The socket is not writeable. _sock_fd: %d.\n", _sock_fd); |
Kojto | 4:15b58c119a0a | 79 | #endif |
Kojto | 0:615c697c33b0 | 80 | return 0; |
Kojto | 4:15b58c119a0a | 81 | } |
Kojto | 0:615c697c33b0 | 82 | } |
Kojto | 0:615c697c33b0 | 83 | |
Kojto | 4:15b58c119a0a | 84 | return _cc3000_module->_socket.sendto(_sock_fd, packet, length, 0, (sockaddr *)&remote._remote_host, sizeof(sockaddr)); |
Kojto | 0:615c697c33b0 | 85 | } |
Kojto | 0:615c697c33b0 | 86 | |
Kojto | 0:615c697c33b0 | 87 | // -1 if unsuccessful, else number of bytes received |
Kojto | 0:615c697c33b0 | 88 | int UDPSocket::receiveFrom(Endpoint &remote, char *buffer, int length) |
Kojto | 0:615c697c33b0 | 89 | { |
Kojto | 0:615c697c33b0 | 90 | if (_sock_fd < 0) { |
Kojto | 0:615c697c33b0 | 91 | return -1; |
Kojto | 0:615c697c33b0 | 92 | } |
Kojto | 0:615c697c33b0 | 93 | |
Kojto | 0:615c697c33b0 | 94 | if (!_blocking) { |
Kojto | 0:615c697c33b0 | 95 | TimeInterval timeout(_timeout); |
Kojto | 0:615c697c33b0 | 96 | if (wait_readable(timeout) != 0) { |
Kojto | 4:15b58c119a0a | 97 | #if (CC3000_DEBUG == 1) |
Kojto | 4:15b58c119a0a | 98 | printf("DEBUG: The socket is not readable. _sock_fd: %d.\n", _sock_fd); |
Kojto | 4:15b58c119a0a | 99 | #endif |
Kojto | 0:615c697c33b0 | 100 | return 0; |
Kojto | 0:615c697c33b0 | 101 | } |
Kojto | 0:615c697c33b0 | 102 | } |
Kojto | 0:615c697c33b0 | 103 | |
Kojto | 0:615c697c33b0 | 104 | remote.reset_address(); |
Kojto | 0:615c697c33b0 | 105 | socklen_t remote_host_length = sizeof(remote._remote_host); |
Kojto | 0:615c697c33b0 | 106 | |
Kojto | 0:615c697c33b0 | 107 | return _cc3000_module->_socket.recvfrom(_sock_fd, buffer, length, 0, (sockaddr *)&remote._remote_host, &remote_host_length); |
Kojto | 0:615c697c33b0 | 108 | } |
Kojto | 0:615c697c33b0 | 109 | |
Kojto | 0:615c697c33b0 | 110 |