Home Alert System

Dependencies:   PWM_Tone_Library DHT

Committer:
aziz111
Date:
Fri Mar 08 17:15:02 2019 +0000
Revision:
5:569a4894abc1
Parent:
3:78f223d34f36
Final

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
ethaderu 3:78f223d34f36 19 #include "Socket/UDPSocket.h"
ethaderu 3:78f223d34f36 20
ethaderu 3:78f223d34f36 21 #include <cstring>
ethaderu 3:78f223d34f36 22
ethaderu 3:78f223d34f36 23 using std::memset;
ethaderu 3:78f223d34f36 24
ethaderu 3:78f223d34f36 25 UDPSocket::UDPSocket() {
ethaderu 3:78f223d34f36 26 }
ethaderu 3:78f223d34f36 27
ethaderu 3:78f223d34f36 28 int UDPSocket::init(void) {
ethaderu 3:78f223d34f36 29 return init_socket(SOCK_DGRAM);
ethaderu 3:78f223d34f36 30 }
ethaderu 3:78f223d34f36 31
ethaderu 3:78f223d34f36 32 // Server initialization
ethaderu 3:78f223d34f36 33 int UDPSocket::bind(int port) {
ethaderu 3:78f223d34f36 34 if (init_socket(SOCK_DGRAM) < 0)
ethaderu 3:78f223d34f36 35 return -1;
ethaderu 3:78f223d34f36 36
ethaderu 3:78f223d34f36 37 struct sockaddr_in localHost;
ethaderu 3:78f223d34f36 38 std::memset(&localHost, 0, sizeof(localHost));
ethaderu 3:78f223d34f36 39
ethaderu 3:78f223d34f36 40 localHost.sin_family = AF_INET;
ethaderu 3:78f223d34f36 41 localHost.sin_port = htons(port);
ethaderu 3:78f223d34f36 42 localHost.sin_addr.s_addr = INADDR_ANY;
ethaderu 3:78f223d34f36 43
ethaderu 3:78f223d34f36 44 if (lwip_bind(_sock_fd, (const struct sockaddr *) &localHost, sizeof(localHost)) < 0) {
ethaderu 3:78f223d34f36 45 close();
ethaderu 3:78f223d34f36 46 return -1;
ethaderu 3:78f223d34f36 47 }
ethaderu 3:78f223d34f36 48
ethaderu 3:78f223d34f36 49 return 0;
ethaderu 3:78f223d34f36 50 }
ethaderu 3:78f223d34f36 51
ethaderu 3:78f223d34f36 52 int UDPSocket::join_multicast_group(const char* address) {
ethaderu 3:78f223d34f36 53 struct ip_mreq mreq;
ethaderu 3:78f223d34f36 54
ethaderu 3:78f223d34f36 55 // Set up group address
ethaderu 3:78f223d34f36 56 mreq.imr_multiaddr.s_addr = inet_addr(address);
ethaderu 3:78f223d34f36 57 mreq.imr_interface.s_addr = htonl(INADDR_ANY);
ethaderu 3:78f223d34f36 58
ethaderu 3:78f223d34f36 59 return set_option(IPPROTO_IP, IP_ADD_MEMBERSHIP, &mreq, sizeof(mreq));
ethaderu 3:78f223d34f36 60 }
ethaderu 3:78f223d34f36 61
ethaderu 3:78f223d34f36 62 int UDPSocket::set_broadcasting(bool broadcast) {
ethaderu 3:78f223d34f36 63 int option = (broadcast) ? (1) : (0);
ethaderu 3:78f223d34f36 64 return set_option(SOL_SOCKET, SO_BROADCAST, &option, sizeof(option));
ethaderu 3:78f223d34f36 65 }
ethaderu 3:78f223d34f36 66
ethaderu 3:78f223d34f36 67 // -1 if unsuccessful, else number of bytes written
ethaderu 3:78f223d34f36 68 int UDPSocket::sendTo(Endpoint &remote, char *packet, int length) {
ethaderu 3:78f223d34f36 69 if (_sock_fd < 0)
ethaderu 3:78f223d34f36 70 return -1;
ethaderu 3:78f223d34f36 71
ethaderu 3:78f223d34f36 72 if (!_blocking) {
ethaderu 3:78f223d34f36 73 TimeInterval timeout(_timeout);
ethaderu 3:78f223d34f36 74 if (wait_writable(timeout) != 0)
ethaderu 3:78f223d34f36 75 return 0;
ethaderu 3:78f223d34f36 76 }
ethaderu 3:78f223d34f36 77
ethaderu 3:78f223d34f36 78 return lwip_sendto(_sock_fd, packet, length, 0, (const struct sockaddr *) &remote._remoteHost, sizeof(remote._remoteHost));
ethaderu 3:78f223d34f36 79 }
ethaderu 3:78f223d34f36 80
ethaderu 3:78f223d34f36 81 // -1 if unsuccessful, else number of bytes received
ethaderu 3:78f223d34f36 82 int UDPSocket::receiveFrom(Endpoint &remote, char *buffer, int length) {
ethaderu 3:78f223d34f36 83 if (_sock_fd < 0)
ethaderu 3:78f223d34f36 84 return -1;
ethaderu 3:78f223d34f36 85
ethaderu 3:78f223d34f36 86 if (!_blocking) {
ethaderu 3:78f223d34f36 87 TimeInterval timeout(_timeout);
ethaderu 3:78f223d34f36 88 if (wait_readable(timeout) != 0)
ethaderu 3:78f223d34f36 89 return 0;
ethaderu 3:78f223d34f36 90 }
ethaderu 3:78f223d34f36 91 remote.reset_address();
ethaderu 3:78f223d34f36 92 socklen_t remoteHostLen = sizeof(remote._remoteHost);
ethaderu 3:78f223d34f36 93 return lwip_recvfrom(_sock_fd, buffer, length, 0, (struct sockaddr*) &remote._remoteHost, &remoteHostLen);
ethaderu 3:78f223d34f36 94 }