Home Alert System

Dependencies:   PWM_Tone_Library DHT

Committer:
ethaderu
Date:
Tue Mar 05 02:34:44 2019 +0000
Revision:
3:78f223d34f36
Publish 1

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ethaderu 3:78f223d34f36 1 /* Copyright (C) 2012 mbed.org, MIT License
ethaderu 3:78f223d34f36 2 *
ethaderu 3:78f223d34f36 3 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
ethaderu 3:78f223d34f36 4 * and associated documentation files (the "Software"), to deal in the Software without restriction,
ethaderu 3:78f223d34f36 5 * including without limitation the rights to use, copy, modify, merge, publish, distribute,
ethaderu 3:78f223d34f36 6 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
ethaderu 3:78f223d34f36 7 * furnished to do so, subject to the following conditions:
ethaderu 3:78f223d34f36 8 *
ethaderu 3:78f223d34f36 9 * The above copyright notice and this permission notice shall be included in all copies or
ethaderu 3:78f223d34f36 10 * substantial portions of the Software.
ethaderu 3:78f223d34f36 11 *
ethaderu 3:78f223d34f36 12 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
ethaderu 3:78f223d34f36 13 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
ethaderu 3:78f223d34f36 14 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
ethaderu 3:78f223d34f36 15 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
ethaderu 3:78f223d34f36 16 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
ethaderu 3:78f223d34f36 17 */
ethaderu 3:78f223d34f36 18 #include "TCPSocketConnection.h"
ethaderu 3:78f223d34f36 19 #include <cstring>
ethaderu 3:78f223d34f36 20
ethaderu 3:78f223d34f36 21 using std::memset;
ethaderu 3:78f223d34f36 22 using std::memcpy;
ethaderu 3:78f223d34f36 23
ethaderu 3:78f223d34f36 24 TCPSocketConnection::TCPSocketConnection() :
ethaderu 3:78f223d34f36 25 _is_connected(false) {
ethaderu 3:78f223d34f36 26 }
ethaderu 3:78f223d34f36 27
ethaderu 3:78f223d34f36 28 int TCPSocketConnection::connect(const char* host, const int port) {
ethaderu 3:78f223d34f36 29 if (init_socket(SOCK_STREAM) < 0)
ethaderu 3:78f223d34f36 30 return -1;
ethaderu 3:78f223d34f36 31
ethaderu 3:78f223d34f36 32 if (set_address(host, port) != 0)
ethaderu 3:78f223d34f36 33 return -1;
ethaderu 3:78f223d34f36 34
ethaderu 3:78f223d34f36 35 if (lwip_connect(_sock_fd, (const struct sockaddr *) &_remoteHost, sizeof(_remoteHost)) < 0) {
ethaderu 3:78f223d34f36 36 close();
ethaderu 3:78f223d34f36 37 return -1;
ethaderu 3:78f223d34f36 38 }
ethaderu 3:78f223d34f36 39 _is_connected = true;
ethaderu 3:78f223d34f36 40
ethaderu 3:78f223d34f36 41 return 0;
ethaderu 3:78f223d34f36 42 }
ethaderu 3:78f223d34f36 43
ethaderu 3:78f223d34f36 44 bool TCPSocketConnection::is_connected(void) {
ethaderu 3:78f223d34f36 45 return _is_connected;
ethaderu 3:78f223d34f36 46 }
ethaderu 3:78f223d34f36 47
ethaderu 3:78f223d34f36 48 int TCPSocketConnection::send(char* data, int length) {
ethaderu 3:78f223d34f36 49 if ((_sock_fd < 0) || !_is_connected)
ethaderu 3:78f223d34f36 50 return -1;
ethaderu 3:78f223d34f36 51
ethaderu 3:78f223d34f36 52 if (!_blocking) {
ethaderu 3:78f223d34f36 53 TimeInterval timeout(_timeout);
ethaderu 3:78f223d34f36 54 if (wait_writable(timeout) != 0)
ethaderu 3:78f223d34f36 55 return -1;
ethaderu 3:78f223d34f36 56 }
ethaderu 3:78f223d34f36 57
ethaderu 3:78f223d34f36 58 int n = lwip_send(_sock_fd, data, length, 0);
ethaderu 3:78f223d34f36 59 _is_connected = (n != 0);
ethaderu 3:78f223d34f36 60
ethaderu 3:78f223d34f36 61 return n;
ethaderu 3:78f223d34f36 62 }
ethaderu 3:78f223d34f36 63
ethaderu 3:78f223d34f36 64 // -1 if unsuccessful, else number of bytes written
ethaderu 3:78f223d34f36 65 int TCPSocketConnection::send_all(char* data, int length) {
ethaderu 3:78f223d34f36 66 if ((_sock_fd < 0) || !_is_connected)
ethaderu 3:78f223d34f36 67 return -1;
ethaderu 3:78f223d34f36 68
ethaderu 3:78f223d34f36 69 int writtenLen = 0;
ethaderu 3:78f223d34f36 70 TimeInterval timeout(_timeout);
ethaderu 3:78f223d34f36 71 while (writtenLen < length) {
ethaderu 3:78f223d34f36 72 if (!_blocking) {
ethaderu 3:78f223d34f36 73 // Wait for socket to be writeable
ethaderu 3:78f223d34f36 74 if (wait_writable(timeout) != 0)
ethaderu 3:78f223d34f36 75 return writtenLen;
ethaderu 3:78f223d34f36 76 }
ethaderu 3:78f223d34f36 77
ethaderu 3:78f223d34f36 78 int ret = lwip_send(_sock_fd, data + writtenLen, length - writtenLen, 0);
ethaderu 3:78f223d34f36 79 if (ret > 0) {
ethaderu 3:78f223d34f36 80 writtenLen += ret;
ethaderu 3:78f223d34f36 81 continue;
ethaderu 3:78f223d34f36 82 } else if (ret == 0) {
ethaderu 3:78f223d34f36 83 _is_connected = false;
ethaderu 3:78f223d34f36 84 return writtenLen;
ethaderu 3:78f223d34f36 85 } else {
ethaderu 3:78f223d34f36 86 return -1; //Connnection error
ethaderu 3:78f223d34f36 87 }
ethaderu 3:78f223d34f36 88 }
ethaderu 3:78f223d34f36 89 return writtenLen;
ethaderu 3:78f223d34f36 90 }
ethaderu 3:78f223d34f36 91
ethaderu 3:78f223d34f36 92 int TCPSocketConnection::receive(char* data, int length) {
ethaderu 3:78f223d34f36 93 if ((_sock_fd < 0) || !_is_connected)
ethaderu 3:78f223d34f36 94 return -1;
ethaderu 3:78f223d34f36 95
ethaderu 3:78f223d34f36 96 if (!_blocking) {
ethaderu 3:78f223d34f36 97 TimeInterval timeout(_timeout);
ethaderu 3:78f223d34f36 98 if (wait_readable(timeout) != 0)
ethaderu 3:78f223d34f36 99 return -1;
ethaderu 3:78f223d34f36 100 }
ethaderu 3:78f223d34f36 101
ethaderu 3:78f223d34f36 102 int n = lwip_recv(_sock_fd, data, length, 0);
ethaderu 3:78f223d34f36 103 _is_connected = (n != 0);
ethaderu 3:78f223d34f36 104
ethaderu 3:78f223d34f36 105 return n;
ethaderu 3:78f223d34f36 106 }
ethaderu 3:78f223d34f36 107
ethaderu 3:78f223d34f36 108 // -1 if unsuccessful, else number of bytes received
ethaderu 3:78f223d34f36 109 int TCPSocketConnection::receive_all(char* data, int length) {
ethaderu 3:78f223d34f36 110 if ((_sock_fd < 0) || !_is_connected)
ethaderu 3:78f223d34f36 111 return -1;
ethaderu 3:78f223d34f36 112
ethaderu 3:78f223d34f36 113 int readLen = 0;
ethaderu 3:78f223d34f36 114 TimeInterval timeout(_timeout);
ethaderu 3:78f223d34f36 115 while (readLen < length) {
ethaderu 3:78f223d34f36 116 if (!_blocking) {
ethaderu 3:78f223d34f36 117 //Wait for socket to be readable
ethaderu 3:78f223d34f36 118 if (wait_readable(timeout) != 0)
ethaderu 3:78f223d34f36 119 return readLen;
ethaderu 3:78f223d34f36 120 }
ethaderu 3:78f223d34f36 121
ethaderu 3:78f223d34f36 122 int ret = lwip_recv(_sock_fd, data + readLen, length - readLen, 0);
ethaderu 3:78f223d34f36 123 if (ret > 0) {
ethaderu 3:78f223d34f36 124 readLen += ret;
ethaderu 3:78f223d34f36 125 } else if (ret == 0) {
ethaderu 3:78f223d34f36 126 _is_connected = false;
ethaderu 3:78f223d34f36 127 return readLen;
ethaderu 3:78f223d34f36 128 } else {
ethaderu 3:78f223d34f36 129 return -1; //Connnection error
ethaderu 3:78f223d34f36 130 }
ethaderu 3:78f223d34f36 131 }
ethaderu 3:78f223d34f36 132 return readLen;
ethaderu 3:78f223d34f36 133 }