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.
Dependents: cc3000_hello_world_demo cc3000_simple_socket_demo cc3000_ntp_demo cc3000_ping_demo ... more
Socket/TCPSocketConnection.cpp@11:5e3771b29385, 2013-10-01 (annotated)
- Committer:
 - SolderSplashLabs
 - Date:
 - Tue Oct 01 21:17:44 2013 +0000
 - Revision:
 - 11:5e3771b29385
 - Parent:
 - 5:245ac5b73132
 - Child:
 - 13:5e36c267e62f
 
Added \r\n to all debug messages; Fixed debug message displaying the port no incorrectly
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 "TCPSocketConnection.h" | 
| Kojto | 0:615c697c33b0 | 20 | #include <algorithm> | 
| Kojto | 0:615c697c33b0 | 21 | |
| Kojto | 5:245ac5b73132 | 22 | TCPSocketConnection::TCPSocketConnection() : _is_connected(false) { | 
| Kojto | 5:245ac5b73132 | 23 | _cc3000_module = cc3000::get_instance(); | 
| Kojto | 5:245ac5b73132 | 24 | if (_cc3000_module == NULL) { | 
| Kojto | 5:245ac5b73132 | 25 | error("Endpoint constructor error: no cc3000 instance available!\r\n"); | 
| Kojto | 5:245ac5b73132 | 26 | } | 
| Kojto | 0:615c697c33b0 | 27 | } | 
| Kojto | 0:615c697c33b0 | 28 | |
| Kojto | 5:245ac5b73132 | 29 | int TCPSocketConnection::connect(const char *host, const int port) { | 
| Kojto | 5:245ac5b73132 | 30 | if (init_socket(SOCK_STREAM, IPPROTO_TCP) < 0) { | 
| Kojto | 5:245ac5b73132 | 31 | #if (CC3000_DEBUG == 1) | 
| SolderSplashLabs | 11:5e3771b29385 | 32 | printf("DEBUG: Failed to create tcp socket.\r\n"); | 
| Kojto | 5:245ac5b73132 | 33 | #endif | 
| Kojto | 5:245ac5b73132 | 34 | return -1; | 
| Kojto | 5:245ac5b73132 | 35 | } | 
| Kojto | 0:615c697c33b0 | 36 | |
| Kojto | 5:245ac5b73132 | 37 | if (set_address(host, port) != 0) { | 
| Kojto | 5:245ac5b73132 | 38 | #if (CC3000_DEBUG == 1) | 
| SolderSplashLabs | 11:5e3771b29385 | 39 | printf("DEBUG: Failed to set address (tcp).\r\n"); | 
| Kojto | 5:245ac5b73132 | 40 | #endif | 
| Kojto | 5:245ac5b73132 | 41 | return -1; | 
| Kojto | 5:245ac5b73132 | 42 | } | 
| Kojto | 5:245ac5b73132 | 43 | |
| Kojto | 5:245ac5b73132 | 44 | if (_cc3000_module->_socket.connect(_sock_fd, (const sockaddr *)&_remote_host, sizeof(_remote_host)) < 0) { | 
| Kojto | 5:245ac5b73132 | 45 | #if (CC3000_DEBUG == 1) | 
| SolderSplashLabs | 11:5e3771b29385 | 46 | printf("DEBUG: Failed to connect (tcp).\r\n"); | 
| Kojto | 5:245ac5b73132 | 47 | #endif | 
| Kojto | 5:245ac5b73132 | 48 | close(); | 
| Kojto | 5:245ac5b73132 | 49 | return -1; | 
| Kojto | 5:245ac5b73132 | 50 | } | 
| Kojto | 5:245ac5b73132 | 51 | |
| Kojto | 5:245ac5b73132 | 52 | _is_connected = true; | 
| Kojto | 5:245ac5b73132 | 53 | |
| Kojto | 5:245ac5b73132 | 54 | return 0; | 
| Kojto | 0:615c697c33b0 | 55 | } | 
| Kojto | 0:615c697c33b0 | 56 | |
| Kojto | 5:245ac5b73132 | 57 | bool TCPSocketConnection::is_connected(void) { | 
| Kojto | 5:245ac5b73132 | 58 | return _is_connected; | 
| Kojto | 5:245ac5b73132 | 59 | } | 
| Kojto | 5:245ac5b73132 | 60 | |
| Kojto | 5:245ac5b73132 | 61 | int TCPSocketConnection::send(char* data, int length) { | 
| Kojto | 5:245ac5b73132 | 62 | if ((_sock_fd < 0) || !_is_connected) { | 
| Kojto | 5:245ac5b73132 | 63 | return -1; | 
| Kojto | 5:245ac5b73132 | 64 | } | 
| Kojto | 0:615c697c33b0 | 65 | |
| Kojto | 5:245ac5b73132 | 66 | if (!_blocking) { | 
| Kojto | 5:245ac5b73132 | 67 | TimeInterval timeout(_timeout); | 
| Kojto | 5:245ac5b73132 | 68 | if (wait_writable(timeout) != 0) { | 
| Kojto | 5:245ac5b73132 | 69 | return -1; | 
| Kojto | 5:245ac5b73132 | 70 | } | 
| Kojto | 5:245ac5b73132 | 71 | } | 
| Kojto | 5:245ac5b73132 | 72 | |
| Kojto | 5:245ac5b73132 | 73 | int n = _cc3000_module->_socket.send(_sock_fd, data, length, 0); | 
| Kojto | 5:245ac5b73132 | 74 | _is_connected = (n != 0); | 
| Kojto | 5:245ac5b73132 | 75 | |
| Kojto | 5:245ac5b73132 | 76 | return n; | 
| Kojto | 0:615c697c33b0 | 77 | } | 
| Kojto | 0:615c697c33b0 | 78 | |
| Kojto | 5:245ac5b73132 | 79 | int TCPSocketConnection::send_all(char *data, int length) { | 
| Kojto | 5:245ac5b73132 | 80 | if ((_sock_fd < 0) || !_is_connected) { | 
| Kojto | 5:245ac5b73132 | 81 | return -1; | 
| Kojto | 5:245ac5b73132 | 82 | } | 
| Kojto | 0:615c697c33b0 | 83 | |
| Kojto | 5:245ac5b73132 | 84 | int writtenLen = 0; | 
| Kojto | 5:245ac5b73132 | 85 | TimeInterval timeout(_timeout); | 
| Kojto | 5:245ac5b73132 | 86 | while (writtenLen < length) { | 
| Kojto | 5:245ac5b73132 | 87 | if (!_blocking) { | 
| Kojto | 5:245ac5b73132 | 88 | // Wait for socket to be writeable | 
| Kojto | 5:245ac5b73132 | 89 | if (wait_writable(timeout) != 0) { | 
| Kojto | 5:245ac5b73132 | 90 | return writtenLen; | 
| Kojto | 5:245ac5b73132 | 91 | } | 
| Kojto | 5:245ac5b73132 | 92 | } | 
| Kojto | 5:245ac5b73132 | 93 | |
| Kojto | 5:245ac5b73132 | 94 | int ret = _cc3000_module->_socket.send(_sock_fd, data + writtenLen, length - writtenLen, 0); | 
| Kojto | 5:245ac5b73132 | 95 | if (ret > 0) { | 
| Kojto | 5:245ac5b73132 | 96 | writtenLen += ret; | 
| Kojto | 5:245ac5b73132 | 97 | continue; | 
| Kojto | 5:245ac5b73132 | 98 | } else if (ret == 0) { | 
| Kojto | 5:245ac5b73132 | 99 | _is_connected = false; | 
| Kojto | 5:245ac5b73132 | 100 | return writtenLen; | 
| Kojto | 5:245ac5b73132 | 101 | } else { | 
| Kojto | 5:245ac5b73132 | 102 | return -1; //Connnection error | 
| Kojto | 5:245ac5b73132 | 103 | } | 
| Kojto | 5:245ac5b73132 | 104 | } | 
| Kojto | 5:245ac5b73132 | 105 | |
| Kojto | 5:245ac5b73132 | 106 | return writtenLen; | 
| Kojto | 0:615c697c33b0 | 107 | } | 
| Kojto | 0:615c697c33b0 | 108 | |
| Kojto | 5:245ac5b73132 | 109 | int TCPSocketConnection::receive(char *data, int length) { | 
| Kojto | 5:245ac5b73132 | 110 | if ((_sock_fd < 0) || !_is_connected) { | 
| Kojto | 5:245ac5b73132 | 111 | return -1; | 
| Kojto | 5:245ac5b73132 | 112 | } | 
| Kojto | 0:615c697c33b0 | 113 | |
| Kojto | 5:245ac5b73132 | 114 | if (!_blocking) { | 
| Kojto | 5:245ac5b73132 | 115 | TimeInterval timeout(_timeout); | 
| Kojto | 5:245ac5b73132 | 116 | if (wait_readable(timeout) != 0) | 
| Kojto | 5:245ac5b73132 | 117 | return -1; | 
| Kojto | 5:245ac5b73132 | 118 | } | 
| Kojto | 5:245ac5b73132 | 119 | |
| Kojto | 5:245ac5b73132 | 120 | int n = _cc3000_module->_socket.recv(_sock_fd, data, length, 0); | 
| Kojto | 5:245ac5b73132 | 121 | _is_connected = (n != 0); | 
| Kojto | 5:245ac5b73132 | 122 | |
| Kojto | 5:245ac5b73132 | 123 | return n; | 
| Kojto | 0:615c697c33b0 | 124 | } | 
| Kojto | 0:615c697c33b0 | 125 | |
| Kojto | 5:245ac5b73132 | 126 | int TCPSocketConnection::receive_all(char *data, int length) { | 
| Kojto | 5:245ac5b73132 | 127 | if ((_sock_fd < 0) || !_is_connected) { | 
| Kojto | 5:245ac5b73132 | 128 | return -1; | 
| Kojto | 5:245ac5b73132 | 129 | } | 
| Kojto | 0:615c697c33b0 | 130 | |
| Kojto | 5:245ac5b73132 | 131 | int readLen = 0; | 
| Kojto | 5:245ac5b73132 | 132 | TimeInterval timeout(_timeout); | 
| Kojto | 5:245ac5b73132 | 133 | while (readLen < length) { | 
| Kojto | 5:245ac5b73132 | 134 | if (!_blocking) { | 
| Kojto | 5:245ac5b73132 | 135 | //Wait for socket to be readable | 
| Kojto | 5:245ac5b73132 | 136 | if (wait_readable(timeout) != 0) | 
| Kojto | 5:245ac5b73132 | 137 | return readLen; | 
| Kojto | 5:245ac5b73132 | 138 | } | 
| Kojto | 0:615c697c33b0 | 139 | |
| Kojto | 5:245ac5b73132 | 140 | int ret = _cc3000_module->_socket.recv(_sock_fd, data + readLen, length - readLen, 0); | 
| Kojto | 5:245ac5b73132 | 141 | if (ret > 0) { | 
| Kojto | 5:245ac5b73132 | 142 | readLen += ret; | 
| Kojto | 5:245ac5b73132 | 143 | } else if (ret == 0) { | 
| Kojto | 5:245ac5b73132 | 144 | _is_connected = false; | 
| Kojto | 5:245ac5b73132 | 145 | return readLen; | 
| Kojto | 5:245ac5b73132 | 146 | } else { | 
| Kojto | 5:245ac5b73132 | 147 | return -1; //Connnection error | 
| Kojto | 5:245ac5b73132 | 148 | } | 
| Kojto | 5:245ac5b73132 | 149 | } | 
| Kojto | 5:245ac5b73132 | 150 | return readLen; | 
| Kojto | 0:615c697c33b0 | 151 | } | 
            SimpleLink Wi-Fi CC3000
        
            Avnet Wi-Go System