| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include "fpp-pch.h" | ||
| 2 | |||
| 3 | #include <fstream> | ||
| 4 | |||
| 5 | #include "../common.h" | ||
| 6 | |||
| 7 | #include "NetworkOLEDPage.h" | ||
| 8 | |||
| 9 | class EditIPOLEDPage : public TitledOLEDPage { | ||
| 10 | public: | ||
| 11 | ✗ | EditIPOLEDPage(const std::string& type, const std::string& v, FPPNetworkOLEDPage* parent) : | |
| 12 | TitledOLEDPage(type), | ||
| 13 | ✗ | networkPage(parent) { | |
| 14 | ✗ | std::vector<std::string> split = splitWithQuotes(v, '.'); | |
| 15 | ✗ | for (int x = 0; x < 4; x++) { | |
| 16 | ✗ | std::string val = "000"; | |
| 17 | ✗ | if (x < split.size()) { | |
| 18 | ✗ | val = split[x]; | |
| 19 | ✗ | while (val.size() < 3) { | |
| 20 | ✗ | val = "0" + val; | |
| 21 | } | ||
| 22 | } | ||
| 23 | ✗ | ipAs3 += val; | |
| 24 | ✗ | if (x < 3) { | |
| 25 | ✗ | ipAs3 += "."; | |
| 26 | } | ||
| 27 | ✗ | } | |
| 28 | ✗ | } | |
| 29 | ✗ | virtual ~EditIPOLEDPage() { | |
| 30 | ✗ | } | |
| 31 | |||
| 32 | ✗ | void displaying() override { | |
| 33 | ✗ | if (oledForcedOff) | |
| 34 | ✗ | return; | |
| 35 | ✗ | clearDisplay(); | |
| 36 | ✗ | int startY = displayTitle(); | |
| 37 | |||
| 38 | ✗ | int xPos = 9 + posIdx * 6; | |
| 39 | |||
| 40 | ✗ | fillTriangle(xPos, startY + 7, xPos + 3, startY + 3, xPos + 6, startY + 7); | |
| 41 | ✗ | printString(10, startY + 10, ipAs3, true); | |
| 42 | ✗ | fillTriangle(xPos, startY + 20, xPos + 3, startY + 24, xPos + 6, startY + 20); | |
| 43 | |||
| 44 | ✗ | flushDisplay(); | |
| 45 | } | ||
| 46 | |||
| 47 | ✗ | bool doAction(const std::string& action) { | |
| 48 | ✗ | if (action == "Back" && posIdx == 0) { | |
| 49 | ✗ | SetCurrentPage(networkPage); | |
| 50 | ✗ | return true; | |
| 51 | ✗ | } else if (action == "Back") { | |
| 52 | ✗ | posIdx--; | |
| 53 | ✗ | if (posIdx % 4 == 3) { | |
| 54 | ✗ | posIdx--; | |
| 55 | } | ||
| 56 | ✗ | } else if (action == "Enter") { | |
| 57 | ✗ | posIdx++; | |
| 58 | ✗ | if (posIdx >= 15) { | |
| 59 | ✗ | networkPage->setParameterIP(title, ipAs3); | |
| 60 | ✗ | SetCurrentPage(networkPage); | |
| 61 | ✗ | return true; | |
| 62 | } | ||
| 63 | ✗ | if (posIdx % 4 == 3) { | |
| 64 | ✗ | posIdx++; | |
| 65 | } | ||
| 66 | ✗ | } else if (action == "Up") { | |
| 67 | ✗ | ipAs3[posIdx]++; | |
| 68 | ✗ | if (ipAs3[posIdx] > '9') | |
| 69 | ✗ | ipAs3[posIdx] = '0'; | |
| 70 | ✗ | } else if (action == "Down" || action == "Test/Down") { | |
| 71 | ✗ | ipAs3[posIdx]--; | |
| 72 | ✗ | if (ipAs3[posIdx] < '0') | |
| 73 | ✗ | ipAs3[posIdx] = '9'; | |
| 74 | } | ||
| 75 | ✗ | displaying(); | |
| 76 | ✗ | return true; | |
| 77 | } | ||
| 78 | |||
| 79 | std::string ipAs3; | ||
| 80 | int posIdx = 0; | ||
| 81 | FPPNetworkOLEDPage* networkPage; | ||
| 82 | }; | ||
| 83 | |||
| 84 | ✗ | FPPNetworkOLEDPage::FPPNetworkOLEDPage(OLEDPage* parent) : | |
| 85 | ✗ | MenuOLEDPage("eth0", {}, parent) { | |
| 86 | ✗ | protocol = "dhcp"; | |
| 87 | |||
| 88 | char ipc[20]; | ||
| 89 | char netmaskc[20]; | ||
| 90 | char gwc[20]; | ||
| 91 | ✗ | GetInterfaceAddress("eth0", ipc, netmaskc, gwc); | |
| 92 | |||
| 93 | ✗ | ip = ipc; | |
| 94 | ✗ | netmask = netmaskc; | |
| 95 | ✗ | gateway = gwc; | |
| 96 | |||
| 97 | ✗ | if (FileExists("/home/fpp/media/config/interface.eth0")) { | |
| 98 | ✗ | std::fstream file; | |
| 99 | ✗ | file.open("/home/fpp/media/config/interface.eth0", std::ios::in); | |
| 100 | |||
| 101 | ✗ | if (file.is_open()) { | |
| 102 | ✗ | std::string tp; | |
| 103 | ✗ | while (getline(file, tp)) { | |
| 104 | ✗ | std::vector<std::string> v = splitWithQuotes(tp, '='); | |
| 105 | ✗ | if (v[0] == "PROTO") { | |
| 106 | ✗ | protocol = v[1]; | |
| 107 | ✗ | } else if (v[0] == "ADDRESS") { | |
| 108 | ✗ | ip = v[1]; | |
| 109 | ✗ | } else if (v[0] == "NETMASK") { | |
| 110 | ✗ | netmask = v[1]; | |
| 111 | ✗ | } else if (v[0] == "GATEWAY") { | |
| 112 | ✗ | gateway = v[1]; | |
| 113 | } | ||
| 114 | ✗ | } | |
| 115 | ✗ | file.close(); //close the file object. | |
| 116 | ✗ | } | |
| 117 | ✗ | } | |
| 118 | ✗ | if (FileExists("/home/fpp/media/config/dns")) { | |
| 119 | ✗ | std::fstream file; | |
| 120 | ✗ | file.open("/home/fpp/media/config/dns", std::ios::in); | |
| 121 | |||
| 122 | ✗ | if (file.is_open()) { | |
| 123 | ✗ | std::string tp; | |
| 124 | ✗ | while (getline(file, tp)) { | |
| 125 | ✗ | std::vector<std::string> v = splitWithQuotes(tp, '='); | |
| 126 | ✗ | if (v[0] == "DNS1") { | |
| 127 | ✗ | dns1 = v[1]; | |
| 128 | ✗ | if (dns1 == "\"\"") { | |
| 129 | ✗ | dns1 = ""; | |
| 130 | } | ||
| 131 | ✗ | } else if (v[0] == "DNS2") { | |
| 132 | ✗ | dns2 = v[1]; | |
| 133 | ✗ | if (dns2 == "\"\"") { | |
| 134 | ✗ | dns2 = ""; | |
| 135 | } | ||
| 136 | } | ||
| 137 | ✗ | } | |
| 138 | ✗ | file.close(); | |
| 139 | ✗ | } | |
| 140 | ✗ | } | |
| 141 | ✗ | if (dns1 == "" && FileExists("/etc/resolv.conf")) { | |
| 142 | ✗ | std::fstream file; | |
| 143 | ✗ | file.open("/etc/resolv.conf", std::ios::in); | |
| 144 | ✗ | if (file.is_open()) { | |
| 145 | ✗ | std::string tp; | |
| 146 | ✗ | while (getline(file, tp)) { | |
| 147 | ✗ | std::vector<std::string> v = splitWithQuotes(tp, ' '); | |
| 148 | ✗ | if (v[0] == "nameserver") { | |
| 149 | ✗ | if (dns1 == "") { | |
| 150 | ✗ | dns1 = v[1]; | |
| 151 | ✗ | if (dns1 == "\"\"") { | |
| 152 | ✗ | dns1 = ""; | |
| 153 | } | ||
| 154 | ✗ | } else if (dns2 == "") { | |
| 155 | ✗ | dns2 = v[1]; | |
| 156 | ✗ | if (dns2 == "\"\"") { | |
| 157 | ✗ | dns2 = ""; | |
| 158 | } | ||
| 159 | } | ||
| 160 | } | ||
| 161 | ✗ | } | |
| 162 | ✗ | } | |
| 163 | ✗ | file.close(); | |
| 164 | ✗ | } | |
| 165 | |||
| 166 | ✗ | if (protocol == "dhcp") { | |
| 167 | ✗ | items.push_back("Type: DHCP"); | |
| 168 | } else { | ||
| 169 | ✗ | items.push_back("Type: Static"); | |
| 170 | } | ||
| 171 | ✗ | items.push_back("IP:" + ip); | |
| 172 | ✗ | items.push_back("NM:" + netmask); | |
| 173 | ✗ | items.push_back("GW:" + gateway); | |
| 174 | ✗ | items.push_back("D1:" + dns1); | |
| 175 | ✗ | items.push_back("D2:" + dns2); | |
| 176 | ✗ | } | |
| 177 | |||
| 178 | ✗ | FPPNetworkOLEDPage::~FPPNetworkOLEDPage() { | |
| 179 | ✗ | } | |
| 180 | |||
| 181 | ✗ | void FPPNetworkOLEDPage::writeSettings() { | |
| 182 | ✗ | std::fstream file; | |
| 183 | ✗ | file.open("/home/fpp/media/config/interface.eth0", std::ios::out); | |
| 184 | ✗ | file << "INTERFACE=eth0\n"; | |
| 185 | ✗ | file << "PROTO=" << protocol << "\n"; | |
| 186 | ✗ | if (protocol == "static") { | |
| 187 | ✗ | file << "ADDRESS=" << ip << "\n"; | |
| 188 | ✗ | file << "NETMASK=" << netmask << "\n"; | |
| 189 | ✗ | file << "GATEWAY=" << gateway << "\n"; | |
| 190 | } | ||
| 191 | ✗ | file.close(); | |
| 192 | |||
| 193 | ✗ | file.open("/home/fpp/media/config/dns", std::ios::out); | |
| 194 | ✗ | file << "DNS1=\"" << dns1 << "\"\n"; | |
| 195 | ✗ | file << "DNS2=\"" << dns2 << "\"\n"; | |
| 196 | ✗ | file.close(); | |
| 197 | |||
| 198 | ✗ | SetFilePerms("/home/fpp/media/config/interface.eth0"); | |
| 199 | ✗ | SetFilePerms("/home/fpp/media/config/dns"); | |
| 200 | ✗ | } | |
| 201 | |||
| 202 | ✗ | bool FPPNetworkOLEDPage::doAction(const std::string& action) { | |
| 203 | ✗ | if (protocol == "dhcp" && (action != "Enter" && action != "Back")) { | |
| 204 | ✗ | return true; | |
| 205 | } | ||
| 206 | ✗ | if (action == "Back" && parent) { | |
| 207 | ✗ | autoDelete(); | |
| 208 | } | ||
| 209 | ✗ | return MenuOLEDPage::doAction(action); | |
| 210 | } | ||
| 211 | ✗ | void FPPNetworkOLEDPage::setParameterIP(const std::string& tp, const std::string& nipas3) { | |
| 212 | ✗ | std::vector<std::string> splits = splitWithQuotes(nipas3, '.'); | |
| 213 | ✗ | std::string nip; | |
| 214 | ✗ | for (int x = 0; x < 4; x++) { | |
| 215 | ✗ | std::string v = "0"; | |
| 216 | ✗ | if (x < splits.size()) { | |
| 217 | ✗ | v = splits[x]; | |
| 218 | ✗ | while (v[0] == '0') { | |
| 219 | ✗ | v = v.substr(1); | |
| 220 | } | ||
| 221 | ✗ | if (v.empty()) { | |
| 222 | ✗ | v = std::string("0"); | |
| 223 | } | ||
| 224 | } | ||
| 225 | ✗ | if (x < 3) { | |
| 226 | ✗ | v += "."; | |
| 227 | } | ||
| 228 | ✗ | nip += v; | |
| 229 | ✗ | } | |
| 230 | |||
| 231 | ✗ | if (tp == "IP Address") { | |
| 232 | ✗ | ip = nip; | |
| 233 | ✗ | items[1] = "IP:" + ip; | |
| 234 | ✗ | } else if (tp == "Gateway") { | |
| 235 | ✗ | gateway = nip; | |
| 236 | ✗ | items[3] = "GW:" + gateway; | |
| 237 | ✗ | } else if (tp == "Netmask") { | |
| 238 | ✗ | netmask = nip; | |
| 239 | ✗ | items[2] = "NM:" + netmask; | |
| 240 | ✗ | } else if (tp == "DNS 1") { | |
| 241 | ✗ | dns1 = nip; | |
| 242 | ✗ | items[4] = "D1:" + dns1; | |
| 243 | ✗ | } else if (tp == "DNS 2") { | |
| 244 | ✗ | dns2 = nip; | |
| 245 | ✗ | items[5] = "D2:" + dns2; | |
| 246 | } | ||
| 247 | ✗ | writeSettings(); | |
| 248 | ✗ | } | |
| 249 | |||
| 250 | ✗ | void FPPNetworkOLEDPage::itemSelected(const std::string& item) { | |
| 251 | ✗ | if (item == "Type: Static") { | |
| 252 | ✗ | protocol = "dhcp"; | |
| 253 | ✗ | writeSettings(); | |
| 254 | ✗ | items[0] = "Type: DHCP"; | |
| 255 | ✗ | curSelected = 0; | |
| 256 | ✗ | display(); | |
| 257 | ✗ | return; | |
| 258 | ✗ | } else if (item == "Type: DHCP") { | |
| 259 | ✗ | protocol = "static"; | |
| 260 | ✗ | writeSettings(); | |
| 261 | ✗ | items[0] = "Type: Static"; | |
| 262 | ✗ | curSelected = 0; | |
| 263 | ✗ | display(); | |
| 264 | ✗ | return; | |
| 265 | } | ||
| 266 | ✗ | std::vector<std::string> v = splitWithQuotes(item, ':'); | |
| 267 | ✗ | std::string type; | |
| 268 | ✗ | if (v[0] == "IP") { | |
| 269 | ✗ | type = "IP Address"; | |
| 270 | ✗ | } else if (v[0] == "GW") { | |
| 271 | ✗ | type = "Gateway"; | |
| 272 | ✗ | } else if (v[0] == "NM") { | |
| 273 | ✗ | type = "Netmask"; | |
| 274 | ✗ | } else if (v[0] == "D1") { | |
| 275 | ✗ | type = "DNS 1"; | |
| 276 | ✗ | } else if (v[0] == "D2") { | |
| 277 | ✗ | type = "DNS 2"; | |
| 278 | } | ||
| 279 | ✗ | std::string val; | |
| 280 | ✗ | if (v.size() > 1) { | |
| 281 | ✗ | val = v[1]; | |
| 282 | } | ||
| 283 | |||
| 284 | ✗ | EditIPOLEDPage* p = new EditIPOLEDPage(type, val, this); | |
| 285 | ✗ | p->autoDelete(); | |
| 286 | ✗ | SetCurrentPage(p); | |
| 287 | ✗ | } | |
| 288 |