aboutsummaryrefslogtreecommitdiff
path: root/lib/dhcp/utils.h
diff options
context:
space:
mode:
authorJohannes Stoelp <johannes.stoelp@gmail.com>2022-01-14 23:51:05 +0100
committerJohannes Stoelp <johannes.stoelp@gmail.com>2022-01-14 23:51:05 +0100
commitf9928a1a08c57fe853888119a996c3acc98ee09d (patch)
treec9770b76ffcc281da141f3aa2c595600372c0fca /lib/dhcp/utils.h
downloadpio-nodemcuv2-dhcp-server-main.tar.gz
pio-nodemcuv2-dhcp-server-main.zip
Initial version of nodemcuv2 dhcp serverHEADmain
Able to offer IP address + DNS/Gateway ... Worked with devices at my hand.
Diffstat (limited to 'lib/dhcp/utils.h')
-rw-r--r--lib/dhcp/utils.h31
1 files changed, 31 insertions, 0 deletions
diff --git a/lib/dhcp/utils.h b/lib/dhcp/utils.h
new file mode 100644
index 0000000..9016007
--- /dev/null
+++ b/lib/dhcp/utils.h
@@ -0,0 +1,31 @@
+// Copyright (c) 2022 Johannes Stoelp
+
+#ifndef UTILS_H
+#define UTILS_H
+
+#include <type_traits>
+
+// Convert from an underlying enum type into an enum variant.
+template<typename E>
+constexpr E from_raw(std::underlying_type_t<E> u) {
+ static_assert(std::is_enum_v<E>);
+ return static_cast<E>(u);
+}
+
+// Convert from an enum variant into an underlying enum type.
+template<typename E>
+constexpr std::underlying_type_t<E> into_raw(E e) {
+ static_assert(std::is_enum_v<E>);
+ return static_cast<std::underlying_type_t<E>>(e);
+}
+
+// Simple cyclic rotation hash function.
+constexpr u32 hash(const u8* data, usize len) {
+ u32 hash = 0xa5a55a5a /* seed */;
+ for (usize i = 0; i < len; ++i) {
+ hash += ((hash << 25) | (hash >> 7) /* rrot(7) */) ^ data[i];
+ }
+ return hash;
+}
+
+#endif