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 "Socket/Socket.h"
ethaderu 3:78f223d34f36 19 #include <cstring>
ethaderu 3:78f223d34f36 20
ethaderu 3:78f223d34f36 21 using std::memset;
ethaderu 3:78f223d34f36 22
ethaderu 3:78f223d34f36 23 Socket::Socket() : _sock_fd(-1), _blocking(true), _timeout(1500) {
ethaderu 3:78f223d34f36 24
ethaderu 3:78f223d34f36 25 }
ethaderu 3:78f223d34f36 26
ethaderu 3:78f223d34f36 27 void Socket::set_blocking(bool blocking, unsigned int timeout) {
ethaderu 3:78f223d34f36 28 _blocking = blocking;
ethaderu 3:78f223d34f36 29 _timeout = timeout;
ethaderu 3:78f223d34f36 30 }
ethaderu 3:78f223d34f36 31
ethaderu 3:78f223d34f36 32 int Socket::init_socket(int type) {
ethaderu 3:78f223d34f36 33 if (_sock_fd != -1)
ethaderu 3:78f223d34f36 34 return -1;
ethaderu 3:78f223d34f36 35
ethaderu 3:78f223d34f36 36 int fd = lwip_socket(AF_INET, type, 0);
ethaderu 3:78f223d34f36 37 if (fd < 0)
ethaderu 3:78f223d34f36 38 return -1;
ethaderu 3:78f223d34f36 39
ethaderu 3:78f223d34f36 40 _sock_fd = fd;
ethaderu 3:78f223d34f36 41 return 0;
ethaderu 3:78f223d34f36 42 }
ethaderu 3:78f223d34f36 43
ethaderu 3:78f223d34f36 44 int Socket::set_option(int level, int optname, const void *optval, socklen_t optlen) {
ethaderu 3:78f223d34f36 45 return lwip_setsockopt(_sock_fd, level, optname, optval, optlen);
ethaderu 3:78f223d34f36 46 }
ethaderu 3:78f223d34f36 47
ethaderu 3:78f223d34f36 48 int Socket::get_option(int level, int optname, void *optval, socklen_t *optlen) {
ethaderu 3:78f223d34f36 49 return lwip_getsockopt(_sock_fd, level, optname, optval, optlen);
ethaderu 3:78f223d34f36 50 }
ethaderu 3:78f223d34f36 51
ethaderu 3:78f223d34f36 52 int Socket::select(struct timeval *timeout, bool read, bool write) {
ethaderu 3:78f223d34f36 53 fd_set fdSet;
ethaderu 3:78f223d34f36 54 FD_ZERO(&fdSet);
ethaderu 3:78f223d34f36 55 FD_SET(_sock_fd, &fdSet);
ethaderu 3:78f223d34f36 56
ethaderu 3:78f223d34f36 57 fd_set* readset = (read ) ? (&fdSet) : (NULL);
ethaderu 3:78f223d34f36 58 fd_set* writeset = (write) ? (&fdSet) : (NULL);
ethaderu 3:78f223d34f36 59
ethaderu 3:78f223d34f36 60 int ret = lwip_select(FD_SETSIZE, readset, writeset, NULL, timeout);
ethaderu 3:78f223d34f36 61 return (ret <= 0 || !FD_ISSET(_sock_fd, &fdSet)) ? (-1) : (0);
ethaderu 3:78f223d34f36 62 }
ethaderu 3:78f223d34f36 63
ethaderu 3:78f223d34f36 64 int Socket::wait_readable(TimeInterval& timeout) {
ethaderu 3:78f223d34f36 65 return select(&timeout._time, true, false);
ethaderu 3:78f223d34f36 66 }
ethaderu 3:78f223d34f36 67
ethaderu 3:78f223d34f36 68 int Socket::wait_writable(TimeInterval& timeout) {
ethaderu 3:78f223d34f36 69 return select(&timeout._time, false, true);
ethaderu 3:78f223d34f36 70 }
ethaderu 3:78f223d34f36 71
ethaderu 3:78f223d34f36 72 int Socket::close(bool shutdown) {
ethaderu 3:78f223d34f36 73 if (_sock_fd < 0)
ethaderu 3:78f223d34f36 74 return -1;
ethaderu 3:78f223d34f36 75
ethaderu 3:78f223d34f36 76 if (shutdown)
ethaderu 3:78f223d34f36 77 lwip_shutdown(_sock_fd, SHUT_RDWR);
ethaderu 3:78f223d34f36 78 lwip_close(_sock_fd);
ethaderu 3:78f223d34f36 79 _sock_fd = -1;
ethaderu 3:78f223d34f36 80
ethaderu 3:78f223d34f36 81 return 0;
ethaderu 3:78f223d34f36 82 }
ethaderu 3:78f223d34f36 83
ethaderu 3:78f223d34f36 84 Socket::~Socket() {
ethaderu 3:78f223d34f36 85 close(); //Don't want to leak
ethaderu 3:78f223d34f36 86 }
ethaderu 3:78f223d34f36 87
ethaderu 3:78f223d34f36 88 TimeInterval::TimeInterval(unsigned int ms) {
ethaderu 3:78f223d34f36 89 _time.tv_sec = ms / 1000;
ethaderu 3:78f223d34f36 90 _time.tv_usec = (ms - (_time.tv_sec * 1000)) * 1000;
ethaderu 3:78f223d34f36 91 }