Axeda Ready Demo for Freescale FRDM-KL46Z as accident alert system
Dependencies: FRDM_MMA8451Q KL46Z-USBHost MAG3110 SocketModem TSI mbed FATFileSystem
Fork of AxedaGo-Freescal_FRDM-KL46Z by
SocketModem/Socket/TCPSocketConnection.cpp@0:65004368569c, 2014-07-01 (annotated)
- Committer:
- AxedaCorp
- Date:
- Tue Jul 01 21:31:54 2014 +0000
- Revision:
- 0:65004368569c
Made initial
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
AxedaCorp | 0:65004368569c | 1 | /* Copyright (C) 2012 mbed.org, MIT License |
AxedaCorp | 0:65004368569c | 2 | * |
AxedaCorp | 0:65004368569c | 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy of this software |
AxedaCorp | 0:65004368569c | 4 | * and associated documentation files (the "Software"), to deal in the Software without restriction, |
AxedaCorp | 0:65004368569c | 5 | * including without limitation the rights to use, copy, modify, merge, publish, distribute, |
AxedaCorp | 0:65004368569c | 6 | * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is |
AxedaCorp | 0:65004368569c | 7 | * furnished to do so, subject to the following conditions: |
AxedaCorp | 0:65004368569c | 8 | * |
AxedaCorp | 0:65004368569c | 9 | * The above copyright notice and this permission notice shall be included in all copies or |
AxedaCorp | 0:65004368569c | 10 | * substantial portions of the Software. |
AxedaCorp | 0:65004368569c | 11 | * |
AxedaCorp | 0:65004368569c | 12 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING |
AxedaCorp | 0:65004368569c | 13 | * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
AxedaCorp | 0:65004368569c | 14 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
AxedaCorp | 0:65004368569c | 15 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
AxedaCorp | 0:65004368569c | 16 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
AxedaCorp | 0:65004368569c | 17 | */ |
AxedaCorp | 0:65004368569c | 18 | |
AxedaCorp | 0:65004368569c | 19 | #include "TCPSocketConnection.h" |
AxedaCorp | 0:65004368569c | 20 | #include <algorithm> |
AxedaCorp | 0:65004368569c | 21 | |
AxedaCorp | 0:65004368569c | 22 | TCPSocketConnection::TCPSocketConnection() |
AxedaCorp | 0:65004368569c | 23 | { |
AxedaCorp | 0:65004368569c | 24 | } |
AxedaCorp | 0:65004368569c | 25 | |
AxedaCorp | 0:65004368569c | 26 | int TCPSocketConnection::connect(const char* host, const int port) |
AxedaCorp | 0:65004368569c | 27 | { |
AxedaCorp | 0:65004368569c | 28 | if (!ip->open(host, port, mts::IPStack::TCP)) { |
AxedaCorp | 0:65004368569c | 29 | return -1; |
AxedaCorp | 0:65004368569c | 30 | } |
AxedaCorp | 0:65004368569c | 31 | return 0; |
AxedaCorp | 0:65004368569c | 32 | } |
AxedaCorp | 0:65004368569c | 33 | |
AxedaCorp | 0:65004368569c | 34 | bool TCPSocketConnection::is_connected(void) |
AxedaCorp | 0:65004368569c | 35 | { |
AxedaCorp | 0:65004368569c | 36 | return ip->isOpen(); |
AxedaCorp | 0:65004368569c | 37 | } |
AxedaCorp | 0:65004368569c | 38 | |
AxedaCorp | 0:65004368569c | 39 | int TCPSocketConnection::send(char* data, int length) |
AxedaCorp | 0:65004368569c | 40 | { |
AxedaCorp | 0:65004368569c | 41 | Timer tmr; |
AxedaCorp | 0:65004368569c | 42 | |
AxedaCorp | 0:65004368569c | 43 | if (!_blocking) { |
AxedaCorp | 0:65004368569c | 44 | tmr.start(); |
AxedaCorp | 0:65004368569c | 45 | while (tmr.read_ms() < _timeout) { |
AxedaCorp | 0:65004368569c | 46 | if (ip->writeable()) |
AxedaCorp | 0:65004368569c | 47 | break; |
AxedaCorp | 0:65004368569c | 48 | } |
AxedaCorp | 0:65004368569c | 49 | if (tmr.read_ms() >= _timeout) { |
AxedaCorp | 0:65004368569c | 50 | return -1; |
AxedaCorp | 0:65004368569c | 51 | } |
AxedaCorp | 0:65004368569c | 52 | } |
AxedaCorp | 0:65004368569c | 53 | return ip->write(data, length, 0); |
AxedaCorp | 0:65004368569c | 54 | } |
AxedaCorp | 0:65004368569c | 55 | |
AxedaCorp | 0:65004368569c | 56 | // -1 if unsuccessful, else number of bytes written |
AxedaCorp | 0:65004368569c | 57 | int TCPSocketConnection::send_all(char* data, int length) |
AxedaCorp | 0:65004368569c | 58 | { |
AxedaCorp | 0:65004368569c | 59 | if (_blocking) { |
AxedaCorp | 0:65004368569c | 60 | return ip->write(data, length, -1); |
AxedaCorp | 0:65004368569c | 61 | } else { |
AxedaCorp | 0:65004368569c | 62 | return ip->write(data, length, _timeout); |
AxedaCorp | 0:65004368569c | 63 | } |
AxedaCorp | 0:65004368569c | 64 | } |
AxedaCorp | 0:65004368569c | 65 | |
AxedaCorp | 0:65004368569c | 66 | // -1 if unsuccessful, else number of bytes received |
AxedaCorp | 0:65004368569c | 67 | int TCPSocketConnection::receive(char* data, int length) |
AxedaCorp | 0:65004368569c | 68 | { |
AxedaCorp | 0:65004368569c | 69 | Timer tmr; |
AxedaCorp | 0:65004368569c | 70 | int time = -1; |
AxedaCorp | 0:65004368569c | 71 | |
AxedaCorp | 0:65004368569c | 72 | if (!_blocking) { |
AxedaCorp | 0:65004368569c | 73 | tmr.start(); |
AxedaCorp | 0:65004368569c | 74 | while (time < _timeout + 20) { |
AxedaCorp | 0:65004368569c | 75 | if (ip->readable()) { |
AxedaCorp | 0:65004368569c | 76 | break; |
AxedaCorp | 0:65004368569c | 77 | } |
AxedaCorp | 0:65004368569c | 78 | time = tmr.read_ms(); |
AxedaCorp | 0:65004368569c | 79 | } |
AxedaCorp | 0:65004368569c | 80 | if (time >= _timeout + 20) { |
AxedaCorp | 0:65004368569c | 81 | return -1; |
AxedaCorp | 0:65004368569c | 82 | } |
AxedaCorp | 0:65004368569c | 83 | } else { |
AxedaCorp | 0:65004368569c | 84 | while(!ip->readable()); |
AxedaCorp | 0:65004368569c | 85 | } |
AxedaCorp | 0:65004368569c | 86 | |
AxedaCorp | 0:65004368569c | 87 | return ip->read(data, length, 0); |
AxedaCorp | 0:65004368569c | 88 | } |
AxedaCorp | 0:65004368569c | 89 | |
AxedaCorp | 0:65004368569c | 90 | |
AxedaCorp | 0:65004368569c | 91 | // -1 if unsuccessful, else number of bytes received |
AxedaCorp | 0:65004368569c | 92 | int TCPSocketConnection::receive_all(char* data, int length) |
AxedaCorp | 0:65004368569c | 93 | { |
AxedaCorp | 0:65004368569c | 94 | if (_blocking) { |
AxedaCorp | 0:65004368569c | 95 | return ip->read(data, length, -1); |
AxedaCorp | 0:65004368569c | 96 | } else { |
AxedaCorp | 0:65004368569c | 97 | return ip->read(data, length, _timeout); |
AxedaCorp | 0:65004368569c | 98 | } |
AxedaCorp | 0:65004368569c | 99 | } |