Host library for controlling a WiConnect enabled Wi-Fi module.

Dependents:   wiconnect-ota_example wiconnect-web_setup_example wiconnect-test-console wiconnect-tcp_server_example ... more

Committer:
dan_ackme
Date:
Mon Oct 27 13:42:26 2014 -0700
Revision:
29:b6af04b77a56
Child:
32:b2bdbc018665
refactored library layout

Who changed what in which revision?

UserRevisionLine numberNew contents of line
dan_ackme 29:b6af04b77a56 1 /**
dan_ackme 29:b6af04b77a56 2 * ACKme WiConnect Host Library is licensed under the BSD licence:
dan_ackme 29:b6af04b77a56 3 *
dan_ackme 29:b6af04b77a56 4 * Copyright (c)2014 ACKme Networks.
dan_ackme 29:b6af04b77a56 5 * All rights reserved.
dan_ackme 29:b6af04b77a56 6 *
dan_ackme 29:b6af04b77a56 7 * Redistribution and use in source and binary forms, with or without modification,
dan_ackme 29:b6af04b77a56 8 * are permitted provided that the following conditions are met:
dan_ackme 29:b6af04b77a56 9 *
dan_ackme 29:b6af04b77a56 10 * 1. Redistributions of source code must retain the above copyright notice,
dan_ackme 29:b6af04b77a56 11 * this list of conditions and the following disclaimer.
dan_ackme 29:b6af04b77a56 12 * 2. Redistributions in binary form must reproduce the above copyright notice,
dan_ackme 29:b6af04b77a56 13 * this list of conditions and the following disclaimer in the documentation
dan_ackme 29:b6af04b77a56 14 * and/or other materials provided with the distribution.
dan_ackme 29:b6af04b77a56 15 * 3. The name of the author may not be used to endorse or promote products
dan_ackme 29:b6af04b77a56 16 * derived from this software without specific prior written permission.
dan_ackme 29:b6af04b77a56 17 *
dan_ackme 29:b6af04b77a56 18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS AND ANY EXPRESS OR IMPLIED
dan_ackme 29:b6af04b77a56 19 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
dan_ackme 29:b6af04b77a56 20 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
dan_ackme 29:b6af04b77a56 21 * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
dan_ackme 29:b6af04b77a56 22 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
dan_ackme 29:b6af04b77a56 23 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
dan_ackme 29:b6af04b77a56 24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
dan_ackme 29:b6af04b77a56 25 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
dan_ackme 29:b6af04b77a56 26 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
dan_ackme 29:b6af04b77a56 27 * OF SUCH DAMAGE.
dan_ackme 29:b6af04b77a56 28 */
dan_ackme 29:b6af04b77a56 29
dan_ackme 29:b6af04b77a56 30
dan_ackme 29:b6af04b77a56 31 #include "api/types/Socket/TCPSocketConnection.h"
dan_ackme 29:b6af04b77a56 32 #include "common.h"
dan_ackme 29:b6af04b77a56 33
dan_ackme 29:b6af04b77a56 34
dan_ackme 29:b6af04b77a56 35
dan_ackme 29:b6af04b77a56 36 /*************************************************************************************************/
dan_ackme 29:b6af04b77a56 37 TCPSocketConnection::TCPSocketConnection(int rxBufferLen, void *rxBuffer, int txBufferLen, void *txBuffer)
dan_ackme 29:b6af04b77a56 38 : Socket(rxBufferLen, rxBuffer, txBufferLen, txBuffer)
dan_ackme 29:b6af04b77a56 39 {
dan_ackme 29:b6af04b77a56 40 }
dan_ackme 29:b6af04b77a56 41
dan_ackme 29:b6af04b77a56 42 /*************************************************************************************************/
dan_ackme 29:b6af04b77a56 43 int TCPSocketConnection::connect(const char* host, const int port)
dan_ackme 29:b6af04b77a56 44 {
dan_ackme 29:b6af04b77a56 45 return (Wiconnect::getInstance()->tcpConnect(socket, host, (uint16_t)port) == WICONNECT_SUCCESS) ? 0 : -1;
dan_ackme 29:b6af04b77a56 46 }
dan_ackme 29:b6af04b77a56 47
dan_ackme 29:b6af04b77a56 48 /*************************************************************************************************/
dan_ackme 29:b6af04b77a56 49 bool TCPSocketConnection::is_connected(void)
dan_ackme 29:b6af04b77a56 50 {
dan_ackme 29:b6af04b77a56 51 return socket.isConnected();
dan_ackme 29:b6af04b77a56 52 }
dan_ackme 29:b6af04b77a56 53
dan_ackme 29:b6af04b77a56 54 /*************************************************************************************************/
dan_ackme 29:b6af04b77a56 55 // currently there's no way to timeout writing data to WiConnect, so we just have to try and send it all.
dan_ackme 29:b6af04b77a56 56 // NOTE: this WILL be fixed in the next version
dan_ackme 29:b6af04b77a56 57 int TCPSocketConnection::send(char* data, int length)
dan_ackme 29:b6af04b77a56 58 {
dan_ackme 29:b6af04b77a56 59 return send_all(data, length);
dan_ackme 29:b6af04b77a56 60 }
dan_ackme 29:b6af04b77a56 61
dan_ackme 29:b6af04b77a56 62 /*************************************************************************************************/
dan_ackme 29:b6af04b77a56 63 // -1 if unsuccessful, else number of bytes written
dan_ackme 29:b6af04b77a56 64 int TCPSocketConnection::send_all(char* data, int length)
dan_ackme 29:b6af04b77a56 65 {
dan_ackme 29:b6af04b77a56 66 return (socket.write(data, length, true) == WICONNECT_SUCCESS) ? length : -1;
dan_ackme 29:b6af04b77a56 67 }
dan_ackme 29:b6af04b77a56 68
dan_ackme 29:b6af04b77a56 69 /*************************************************************************************************/
dan_ackme 29:b6af04b77a56 70 int TCPSocketConnection::receive(char* data, int length)
dan_ackme 29:b6af04b77a56 71 {
dan_ackme 29:b6af04b77a56 72 uint16_t bytesRead;
dan_ackme 29:b6af04b77a56 73
dan_ackme 29:b6af04b77a56 74 if (!_blocking)
dan_ackme 29:b6af04b77a56 75 {
dan_ackme 29:b6af04b77a56 76 if(socket.getRxBufferSize() == 0 || socket.getRxBufferBytesPending() == 0)
dan_ackme 29:b6af04b77a56 77 {
dan_ackme 29:b6af04b77a56 78 TimeoutTimer timer;
dan_ackme 29:b6af04b77a56 79
dan_ackme 29:b6af04b77a56 80 for(;;)
dan_ackme 29:b6af04b77a56 81 {
dan_ackme 29:b6af04b77a56 82 bool hasData;
dan_ackme 29:b6af04b77a56 83
dan_ackme 29:b6af04b77a56 84 if(socket.poll(&hasData) != WICONNECT_SUCCESS)
dan_ackme 29:b6af04b77a56 85 {
dan_ackme 29:b6af04b77a56 86 return -1;
dan_ackme 29:b6af04b77a56 87 }
dan_ackme 29:b6af04b77a56 88 else if(hasData)
dan_ackme 29:b6af04b77a56 89 {
dan_ackme 29:b6af04b77a56 90 break;
dan_ackme 29:b6af04b77a56 91 }
dan_ackme 29:b6af04b77a56 92 else if(timer.timedOut(_timeout))
dan_ackme 29:b6af04b77a56 93 {
dan_ackme 29:b6af04b77a56 94 return -1;
dan_ackme 29:b6af04b77a56 95 }
dan_ackme 29:b6af04b77a56 96 }
dan_ackme 29:b6af04b77a56 97 }
dan_ackme 29:b6af04b77a56 98 }
dan_ackme 29:b6af04b77a56 99
dan_ackme 29:b6af04b77a56 100 if(length == 1 && socket.getRxBufferSize() > 0)
dan_ackme 29:b6af04b77a56 101 {
dan_ackme 29:b6af04b77a56 102 return (socket.getc((uint8_t*)data) == WICONNECT_SUCCESS) ? 1 : -1;
dan_ackme 29:b6af04b77a56 103 }
dan_ackme 29:b6af04b77a56 104 else
dan_ackme 29:b6af04b77a56 105 {
dan_ackme 29:b6af04b77a56 106 for(;;)
dan_ackme 29:b6af04b77a56 107 {
dan_ackme 29:b6af04b77a56 108 if(socket.read(data, length, &bytesRead) != WICONNECT_SUCCESS)
dan_ackme 29:b6af04b77a56 109 {
dan_ackme 29:b6af04b77a56 110 return -1;
dan_ackme 29:b6af04b77a56 111 }
dan_ackme 29:b6af04b77a56 112 else if(bytesRead > 0)
dan_ackme 29:b6af04b77a56 113 {
dan_ackme 29:b6af04b77a56 114 return bytesRead;
dan_ackme 29:b6af04b77a56 115 }
dan_ackme 29:b6af04b77a56 116 }
dan_ackme 29:b6af04b77a56 117 }
dan_ackme 29:b6af04b77a56 118 }
dan_ackme 29:b6af04b77a56 119
dan_ackme 29:b6af04b77a56 120 /*************************************************************************************************/
dan_ackme 29:b6af04b77a56 121 // -1 if unsuccessful, else number of bytes received
dan_ackme 29:b6af04b77a56 122 int TCPSocketConnection::receive_all(char* data, int length)
dan_ackme 29:b6af04b77a56 123 {
dan_ackme 29:b6af04b77a56 124 char *ptr = data;
dan_ackme 29:b6af04b77a56 125 int totReadSize = length;
dan_ackme 29:b6af04b77a56 126
dan_ackme 29:b6af04b77a56 127 while(length > 0)
dan_ackme 29:b6af04b77a56 128 {
dan_ackme 29:b6af04b77a56 129 int bytesRead = receive(ptr, length);
dan_ackme 29:b6af04b77a56 130 if(bytesRead == -1)
dan_ackme 29:b6af04b77a56 131 {
dan_ackme 29:b6af04b77a56 132 return -1;
dan_ackme 29:b6af04b77a56 133 }
dan_ackme 29:b6af04b77a56 134 ptr += bytesRead;
dan_ackme 29:b6af04b77a56 135 length -= bytesRead;
dan_ackme 29:b6af04b77a56 136 }
dan_ackme 29:b6af04b77a56 137
dan_ackme 29:b6af04b77a56 138 return totReadSize;
dan_ackme 29:b6af04b77a56 139 }
dan_ackme 29:b6af04b77a56 140
dan_ackme 29:b6af04b77a56 141