supla-device
Loading...
Searching...
No Matches
ethernet_shield.h
1/*
2 Copyright (C) AC SOFTWARE SP. Z O.O.
3
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License
6 as published by the Free Software Foundation; either version 2
7 of the License, or (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17*/
18
19#ifndef SRC_SUPLA_NETWORK_ETHERNET_SHIELD_H_
20#define SRC_SUPLA_NETWORK_ETHERNET_SHIELD_H_
21
22#ifdef ARDUINO_ARCH_AVR
23#include <Arduino.h>
24#include <Ethernet.h>
25#include <supla/log_wrapper.h>
26
27#include "../supla_lib_config.h"
28#include "network.h"
29
30namespace Supla {
31class EthernetShield : public Supla::Network {
32 public:
33 explicit EthernetShield(uint8_t mac[6], unsigned char *ip = NULL)
34 : Network(ip) {
35 memcpy(this->mac, mac, 6);
36 }
37
38 void disable() override {
39 isDeviceReady = false;
40 }
41
42 bool isReady() override {
43 return isDeviceReady;
44 }
45
46 void setup() override {
47 setSSLEnabled(false); // no SSL support on Arduino MEGA target
48 SUPLA_LOG_INFO("Connecting to network...");
49 if (useLocalIp) {
50 Ethernet.begin(mac, localIp);
51 isDeviceReady = true;
52 } else {
53 int result = false;
54 result = Ethernet.begin(mac, 10000, 4000);
55 SUPLA_LOG_INFO("DHCP connection result: %d", result);
56 isDeviceReady = result == 1 ? true : false;
57 }
58
59 IPAddress localIP = Ethernet.localIP();
60 IPAddress subnetMaskIP = Ethernet.subnetMask();
61 IPAddress gatewayIP = Ethernet.gatewayIP();
62 IPAddress dnsServerIP = Ethernet.dnsServerIP();
63 SUPLA_LOG_INFO(
64 "localIP: %d.%d.%d.%d", localIP[0], localIP[1], localIP[2], localIP[3]);
65 SUPLA_LOG_INFO("subnetMaskIP: %d.%d.%d.%d",
66 subnetMaskIP[0],
67 subnetMaskIP[1],
68 subnetMaskIP[2],
69 subnetMaskIP[3]);
70 SUPLA_LOG_INFO("gatewayIP: %d.%d.%d.%d",
71 gatewayIP[0],
72 gatewayIP[1],
73 gatewayIP[2],
74 gatewayIP[3]);
75 SUPLA_LOG_INFO("dnsServerIP: %d.%d.%d.%d",
76 dnsServerIP[0],
77 dnsServerIP[1],
78 dnsServerIP[2],
79 dnsServerIP[3]);
80 }
81
82 bool iterate() {
83 Ethernet.maintain();
84 return true;
85 }
86
87 void fillStateData(TDSC_ChannelState *channelState) {
88 channelState->Fields |=
89 SUPLA_CHANNELSTATE_FIELD_IPV4 | SUPLA_CHANNELSTATE_FIELD_MAC;
90 channelState->IPv4 = Ethernet.localIP();
91 Ethernet.MACAddress(channelState->MAC);
92 }
93
94 protected:
95 uint8_t mac[6] = {};
96 bool isDeviceReady = false;
97};
98
99}; // namespace Supla
100
101#endif // ARDUINO_ARCH_AVR
102
103#endif // SRC_SUPLA_NETWORK_ETHERNET_SHIELD_H_