supla-device
Loading...
Searching...
No Matches
network.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_NETWORK_H_
20#define SRC_SUPLA_NETWORK_NETWORK_H_
21
22#include <stdint.h>
23
24#include "supla-common/proto.h"
25#include "supla/storage/config.h"
26
28
29namespace Supla {
30
31const char WifiDisableTag[] = "wifi_dis";
32const char EthDisableTag[] = "eth_dis";
33
34class Client;
35
36class Network {
37 public:
38 enum class IntfType {
39 Unknown = 0,
40 Ethernet = 1,
41 WiFi = 2,
42 };
43
44 static Network *Instance();
45 static Network *FirstInstance();
46 static Network *NextInstance(Network *instance);
47 static Network *GetInstanceByIP(uint32_t ip);
48 static int GetNetIntfCount();
49 static void DisconnectProtocols();
50 static void Setup();
51 static void Disable();
52 static void Uninit();
53 static bool IsReady();
54 static bool Iterate();
55 static void SetConfigMode();
56 static void SetNormalMode();
57 static void SetOfflineMode();
58 static void SetTestMode();
59 static void SetSetupNeeded();
60 static bool PopSetupNeeded();
61 // returns MAC addres of the main network interface
62 static bool GetMainMacAddr(uint8_t *);
63 // Initialize hostname on all network interfaces with given prefix
64 static void SetHostname(const char *prefix, int macSize);
65 // Returns true when all network interfaces are in timeout
66 static bool IsIpSetupTimeout();
67 static void LoadConfig();
68
69 static void printData(const char *prefix, const void *buf, const int count);
70
71 explicit Network(uint8_t ip[4]);
72 Network();
73 virtual ~Network();
74 virtual void onLoadConfig();
75 virtual void setup() = 0;
76 virtual void disable() = 0;
77 virtual void uninit();
78 virtual void setConfigMode();
79 virtual void setNormalMode();
80 virtual void setOfflineMode();
81 void setTestMode();
82 virtual bool getMacAddr(uint8_t *);
83 virtual void setHostname(const char *, int macSize);
84 void generateHostname(const char *prefix, int macSize, char *output);
85 virtual bool isIpSetupTimeout();
86 virtual uint32_t getIP();
87
88 virtual Supla::Client *createClient();
89
90 virtual bool isReady() = 0;
91 virtual bool iterate();
92
93 virtual void fillStateData(TDSC_ChannelState *channelState);
94
95 // WiFi specific part
96 virtual bool isWifiConfigRequired();
97 virtual void setSsid(const char *wifiSsid);
98 virtual void setPassword(const char *wifiPassword);
99
100 // SSL configuration
101 static void setSSLEnabled(bool enabled);
102
103 void clearTimeCounters();
104 void setSuplaDeviceClass(SuplaDeviceClass *);
105
106 void setSetupNeeded();
107 bool popSetupNeeded();
108
109 enum IntfType getIntfType() const;
110 virtual const char* getIntfName() const;
111
112 bool isIntfDisabledInConfig() const;
113
114 protected:
115 static Network *netIntf;
116 static Network *firstNetIntf;
117
118 Network *nextNetIntf = nullptr;
119 SuplaDeviceClass *sdc = nullptr;
120 const char *rootCACert = nullptr;
121 unsigned int rootCACertSize = 0;
122 unsigned char localIp[4];
123 char hostname[32] = {};
124
125 enum IntfType intfType = IntfType::Unknown;
126
127 static enum DeviceMode mode;
128 bool setupNeeded = false;
129 bool useLocalIp = false;
130 bool intfDisabledInConfig = false;
131 bool testMode = false;
132};
133
134}; // namespace Supla
135
136#endif // SRC_SUPLA_NETWORK_NETWORK_H_
Definition SuplaDevice.h:96
Definition client.h:31
Definition proto.h:2599