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 /* EthernetInterface.cpp */
ethaderu 3:78f223d34f36 2 /* Copyright (C) 2012 mbed.org, MIT License
ethaderu 3:78f223d34f36 3 *
ethaderu 3:78f223d34f36 4 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
ethaderu 3:78f223d34f36 5 * and associated documentation files (the "Software"), to deal in the Software without restriction,
ethaderu 3:78f223d34f36 6 * including without limitation the rights to use, copy, modify, merge, publish, distribute,
ethaderu 3:78f223d34f36 7 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
ethaderu 3:78f223d34f36 8 * furnished to do so, subject to the following conditions:
ethaderu 3:78f223d34f36 9 *
ethaderu 3:78f223d34f36 10 * The above copyright notice and this permission notice shall be included in all copies or
ethaderu 3:78f223d34f36 11 * substantial portions of the Software.
ethaderu 3:78f223d34f36 12 *
ethaderu 3:78f223d34f36 13 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
ethaderu 3:78f223d34f36 14 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
ethaderu 3:78f223d34f36 15 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
ethaderu 3:78f223d34f36 16 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
ethaderu 3:78f223d34f36 17 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
ethaderu 3:78f223d34f36 18 */
ethaderu 3:78f223d34f36 19 #include "EthernetInterface.h"
ethaderu 3:78f223d34f36 20
ethaderu 3:78f223d34f36 21 #include "lwip/inet.h"
ethaderu 3:78f223d34f36 22 #include "lwip/netif.h"
ethaderu 3:78f223d34f36 23 #include "netif/etharp.h"
ethaderu 3:78f223d34f36 24 #include "lwip/dhcp.h"
ethaderu 3:78f223d34f36 25 #include "eth_arch.h"
ethaderu 3:78f223d34f36 26 #include "lwip/tcpip.h"
ethaderu 3:78f223d34f36 27
ethaderu 3:78f223d34f36 28 #include "mbed.h"
ethaderu 3:78f223d34f36 29
ethaderu 3:78f223d34f36 30 /* TCP/IP and Network Interface Initialisation */
ethaderu 3:78f223d34f36 31 static struct netif netif;
ethaderu 3:78f223d34f36 32
ethaderu 3:78f223d34f36 33 static char mac_addr[19];
ethaderu 3:78f223d34f36 34 static char ip_addr[17] = "\0";
ethaderu 3:78f223d34f36 35 static char gateway[17] = "\0";
ethaderu 3:78f223d34f36 36 static char networkmask[17] = "\0";
ethaderu 3:78f223d34f36 37 static bool use_dhcp = false;
ethaderu 3:78f223d34f36 38
ethaderu 3:78f223d34f36 39 static Semaphore tcpip_inited(0);
ethaderu 3:78f223d34f36 40 static Semaphore netif_linked(0);
ethaderu 3:78f223d34f36 41 static Semaphore netif_up(0);
ethaderu 3:78f223d34f36 42
ethaderu 3:78f223d34f36 43 static void tcpip_init_done(void *arg) {
ethaderu 3:78f223d34f36 44 tcpip_inited.release();
ethaderu 3:78f223d34f36 45 }
ethaderu 3:78f223d34f36 46
ethaderu 3:78f223d34f36 47 static void netif_link_callback(struct netif *netif) {
ethaderu 3:78f223d34f36 48 if (netif_is_link_up(netif)) {
ethaderu 3:78f223d34f36 49 netif_linked.release();
ethaderu 3:78f223d34f36 50 }
ethaderu 3:78f223d34f36 51 }
ethaderu 3:78f223d34f36 52
ethaderu 3:78f223d34f36 53 static void netif_status_callback(struct netif *netif) {
ethaderu 3:78f223d34f36 54 if (netif_is_up(netif)) {
ethaderu 3:78f223d34f36 55 strcpy(ip_addr, inet_ntoa(netif->ip_addr));
ethaderu 3:78f223d34f36 56 strcpy(gateway, inet_ntoa(netif->gw));
ethaderu 3:78f223d34f36 57 strcpy(networkmask, inet_ntoa(netif->netmask));
ethaderu 3:78f223d34f36 58 netif_up.release();
ethaderu 3:78f223d34f36 59 }
ethaderu 3:78f223d34f36 60 }
ethaderu 3:78f223d34f36 61
ethaderu 3:78f223d34f36 62 static void init_netif(ip_addr_t *ipaddr, ip_addr_t *netmask, ip_addr_t *gw) {
ethaderu 3:78f223d34f36 63 tcpip_init(tcpip_init_done, NULL);
ethaderu 3:78f223d34f36 64 tcpip_inited.wait();
ethaderu 3:78f223d34f36 65
ethaderu 3:78f223d34f36 66 memset((void*) &netif, 0, sizeof(netif));
ethaderu 3:78f223d34f36 67 netif_add(&netif, ipaddr, netmask, gw, NULL, eth_arch_enetif_init, tcpip_input);
ethaderu 3:78f223d34f36 68 netif_set_default(&netif);
ethaderu 3:78f223d34f36 69
ethaderu 3:78f223d34f36 70 netif_set_link_callback (&netif, netif_link_callback);
ethaderu 3:78f223d34f36 71 netif_set_status_callback(&netif, netif_status_callback);
ethaderu 3:78f223d34f36 72 }
ethaderu 3:78f223d34f36 73
ethaderu 3:78f223d34f36 74 static void set_mac_address(void) {
ethaderu 3:78f223d34f36 75 #if (MBED_MAC_ADDRESS_SUM != MBED_MAC_ADDR_INTERFACE)
ethaderu 3:78f223d34f36 76 snprintf(mac_addr, 19, "%02x:%02x:%02x:%02x:%02x:%02x", MBED_MAC_ADDR_0, MBED_MAC_ADDR_1, MBED_MAC_ADDR_2,
ethaderu 3:78f223d34f36 77 MBED_MAC_ADDR_3, MBED_MAC_ADDR_4, MBED_MAC_ADDR_5);
ethaderu 3:78f223d34f36 78 #else
ethaderu 3:78f223d34f36 79 char mac[6];
ethaderu 3:78f223d34f36 80 mbed_mac_address(mac);
ethaderu 3:78f223d34f36 81 snprintf(mac_addr, 19, "%02x:%02x:%02x:%02x:%02x:%02x", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
ethaderu 3:78f223d34f36 82 #endif
ethaderu 3:78f223d34f36 83 }
ethaderu 3:78f223d34f36 84
ethaderu 3:78f223d34f36 85 int EthernetInterface::init() {
ethaderu 3:78f223d34f36 86 use_dhcp = true;
ethaderu 3:78f223d34f36 87 set_mac_address();
ethaderu 3:78f223d34f36 88 init_netif(NULL, NULL, NULL);
ethaderu 3:78f223d34f36 89 return 0;
ethaderu 3:78f223d34f36 90 }
ethaderu 3:78f223d34f36 91
ethaderu 3:78f223d34f36 92 int EthernetInterface::init(const char* ip, const char* mask, const char* gateway) {
ethaderu 3:78f223d34f36 93 use_dhcp = false;
ethaderu 3:78f223d34f36 94
ethaderu 3:78f223d34f36 95 set_mac_address();
ethaderu 3:78f223d34f36 96 strcpy(ip_addr, ip);
ethaderu 3:78f223d34f36 97
ethaderu 3:78f223d34f36 98 ip_addr_t ip_n, mask_n, gateway_n;
ethaderu 3:78f223d34f36 99 inet_aton(ip, &ip_n);
ethaderu 3:78f223d34f36 100 inet_aton(mask, &mask_n);
ethaderu 3:78f223d34f36 101 inet_aton(gateway, &gateway_n);
ethaderu 3:78f223d34f36 102 init_netif(&ip_n, &mask_n, &gateway_n);
ethaderu 3:78f223d34f36 103
ethaderu 3:78f223d34f36 104 return 0;
ethaderu 3:78f223d34f36 105 }
ethaderu 3:78f223d34f36 106
ethaderu 3:78f223d34f36 107 int EthernetInterface::connect(unsigned int timeout_ms) {
ethaderu 3:78f223d34f36 108 eth_arch_enable_interrupts();
ethaderu 3:78f223d34f36 109
ethaderu 3:78f223d34f36 110 int inited;
ethaderu 3:78f223d34f36 111 if (use_dhcp) {
ethaderu 3:78f223d34f36 112 dhcp_start(&netif);
ethaderu 3:78f223d34f36 113
ethaderu 3:78f223d34f36 114 // Wait for an IP Address
ethaderu 3:78f223d34f36 115 // -1: error, 0: timeout
ethaderu 3:78f223d34f36 116 inited = netif_up.wait(timeout_ms);
ethaderu 3:78f223d34f36 117 } else {
ethaderu 3:78f223d34f36 118 netif_set_up(&netif);
ethaderu 3:78f223d34f36 119
ethaderu 3:78f223d34f36 120 // Wait for the link up
ethaderu 3:78f223d34f36 121 inited = netif_linked.wait(timeout_ms);
ethaderu 3:78f223d34f36 122 }
ethaderu 3:78f223d34f36 123
ethaderu 3:78f223d34f36 124 return (inited > 0) ? (0) : (-1);
ethaderu 3:78f223d34f36 125 }
ethaderu 3:78f223d34f36 126
ethaderu 3:78f223d34f36 127 int EthernetInterface::disconnect() {
ethaderu 3:78f223d34f36 128 if (use_dhcp) {
ethaderu 3:78f223d34f36 129 dhcp_release(&netif);
ethaderu 3:78f223d34f36 130 dhcp_stop(&netif);
ethaderu 3:78f223d34f36 131 } else {
ethaderu 3:78f223d34f36 132 netif_set_down(&netif);
ethaderu 3:78f223d34f36 133 }
ethaderu 3:78f223d34f36 134
ethaderu 3:78f223d34f36 135 eth_arch_disable_interrupts();
ethaderu 3:78f223d34f36 136
ethaderu 3:78f223d34f36 137 return 0;
ethaderu 3:78f223d34f36 138 }
ethaderu 3:78f223d34f36 139
ethaderu 3:78f223d34f36 140 char* EthernetInterface::getMACAddress() {
ethaderu 3:78f223d34f36 141 return mac_addr;
ethaderu 3:78f223d34f36 142 }
ethaderu 3:78f223d34f36 143
ethaderu 3:78f223d34f36 144 char* EthernetInterface::getIPAddress() {
ethaderu 3:78f223d34f36 145 return ip_addr;
ethaderu 3:78f223d34f36 146 }
ethaderu 3:78f223d34f36 147
ethaderu 3:78f223d34f36 148 char* EthernetInterface::getGateway() {
ethaderu 3:78f223d34f36 149 return gateway;
ethaderu 3:78f223d34f36 150 }
ethaderu 3:78f223d34f36 151
ethaderu 3:78f223d34f36 152 char* EthernetInterface::getNetworkMask() {
ethaderu 3:78f223d34f36 153 return networkmask;
ethaderu 3:78f223d34f36 154 }
ethaderu 3:78f223d34f36 155
ethaderu 3:78f223d34f36 156