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