REDAC HybridController
Firmware for LUCIDAC/REDAC Teensy
Loading...
Searching...
No Matches
mac.cpp
Go to the documentation of this file.
1// Copyright (c) 2024 anabrid GmbH
2// Contact: https://www.anabrid.com/licensing/
3//
4// SPDX-License-Identifier: MIT OR GPL-2.0-or-later
5
6#include "utils/mac.h"
7#include <IPAddress.h>
8
9using namespace utils;
10
11FLASHMEM utils::MacAddress::operator std::string() const {
12 return toString(*this); // not inline to avoid forward declaration
13}
14
16FLASHMEM
17bool utils::MacAddress::fromString(const char *address) {
18 if (strlen(address) != 17)
19 return false;
20 for (int i = 0; i < 6; i++, address += 3)
21 mac[i] = std::stoul(address, nullptr, 16);
22 return true;
23}
24
25FLASHMEM std::string utils::toString(const MacAddress &mac, char sep) {
26 char mac_str[20];
27 sprintf(mac_str, "%02X%c%02X%c%02X%c%02X%c%02X%c%02X", mac[0], sep, mac[1], sep, mac[2], sep, mac[3], sep,
28 mac[4], sep, mac[5]);
29 return mac_str;
30}
31
32FLASHMEM std::string utils::toString(const std::array<uint8_t, 8> &mac, char sep) {
33 char mac_str[26];
34 sprintf(mac_str, "%02X%c%02X%c%02X%c%02X%c%02X%c%02X%c%02X%c%02X", mac[0], sep, mac[1], sep, mac[2], sep,
35 mac[3], sep, mac[4], sep, mac[5], sep, mac[6], sep, mac[7]);
36 return mac_str;
37}
38
39FLASHMEM std::string utils::toString(const IPAddress &ip) {
40 char ip_str[16];
41 sprintf(ip_str, "%d.%d.%d.%d", ip[0], ip[1], ip[2], ip[3]);
42 return ip_str;
43}
44
45FLASHMEM bool utils::valid(const IPAddress &ip) {
46 uint8_t sum = 0;
47 for (int i = 0; i < 4; i++)
48 sum += ip[i];
49 return sum != 0;
50}
51
52FLASHMEM bool utils::valid(const MacAddress &mac) {
53 uint8_t sum = 0;
54 for (int i = 0; i < 4; i++)
55 sum += mac[i];
56 return sum != 0;
57}
58
59FLASHMEM void utils::convertFromJson(JsonVariantConst macjson, MacAddress &mac) {
60 mac.fromString(macjson.as<const char *>());
61}
62
63FLASHMEM void convertFromJson(JsonVariantConst ipjson, IPAddress &ip) {
64 ip.fromString(ipjson.as<const char *>());
65}
66
67FLASHMEM void convertToJson(const IPAddress &ip, JsonVariant ipjson) { ipjson.set(toString(ip)); }
68
69FLASHMEM void utils::convertToJson(const MacAddress &mac, JsonVariant macjson) { macjson.set(toString(mac)); }
FLASHMEM void convertToJson(const IPAddress &ip, JsonVariant ipjson)
Definition mac.cpp:67
FLASHMEM void convertFromJson(JsonVariantConst ipjson, IPAddress &ip)
Definition mac.cpp:63