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
26#include <supla/log_wrapper.h>
27
28#include "../supla_lib_config.h"
29#include "network.h"
30
31// TODO(klew): change logs to supla_log
32
33namespace Supla {
34class EthernetShield : public Supla::Network {
35 public:
36 explicit EthernetShield(uint8_t mac[6], unsigned char *ip = NULL)
37 : Network(ip) {
38 memcpy(this->mac, mac, 6);
39 }
40
41 void disable() override {
42 }
43
44 bool isReady() override {
45 return isDeviceReady;
46 }
47
48 void setup() override {
49 setSSLEnabled(false); // no SSL support on Arduino MEGA target
50 Serial.println(F("Connecting to network..."));
51 if (useLocalIp) {
52 Ethernet.begin(mac, localIp);
53 isDeviceReady = true;
54 } else {
55 int result = false;
56 result = Ethernet.begin(mac, 10000, 4000);
57 Serial.print(F("DHCP connection result: "));
58 Serial.println(result);
59 isDeviceReady = result == 1 ? true : false;
60 }
61
62 Serial.print(F("localIP: "));
63 Serial.println(Ethernet.localIP());
64 Serial.print(F("subnetMask: "));
65 Serial.println(Ethernet.subnetMask());
66 Serial.print(F("gatewayIP: "));
67 Serial.println(Ethernet.gatewayIP());
68 Serial.print(F("dnsServerIP: "));
69 Serial.println(Ethernet.dnsServerIP());
70 }
71
72 bool iterate() {
73 Ethernet.maintain();
74 return true;
75 }
76
77 void fillStateData(TDSC_ChannelState *channelState) {
78 channelState->Fields |=
79 SUPLA_CHANNELSTATE_FIELD_IPV4 | SUPLA_CHANNELSTATE_FIELD_MAC;
80 channelState->IPv4 = Ethernet.localIP();
81 Ethernet.MACAddress(channelState->MAC);
82 }
83
84 protected:
85 uint8_t mac[6] = {};
86 bool isDeviceReady = false;
87};
88
89}; // namespace Supla
90
91#endif // ARDUINO_ARCH_AVR
92
93#endif // SRC_SUPLA_NETWORK_ETHERNET_SHIELD_H_