From f9928a1a08c57fe853888119a996c3acc98ee09d Mon Sep 17 00:00:00 2001 From: Johannes Stoelp Date: Fri, 14 Jan 2022 23:51:05 +0100 Subject: Initial version of nodemcuv2 dhcp server Able to offer IP address + DNS/Gateway ... Worked with devices at my hand. --- lib/dhcp/utils.h | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 lib/dhcp/utils.h (limited to 'lib/dhcp/utils.h') 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 + +// Convert from an underlying enum type into an enum variant. +template +constexpr E from_raw(std::underlying_type_t u) { + static_assert(std::is_enum_v); + return static_cast(u); +} + +// Convert from an enum variant into an underlying enum type. +template +constexpr std::underlying_type_t into_raw(E e) { + static_assert(std::is_enum_v); + return static_cast>(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 -- cgit v1.2.3